Wed, 31 Dec 2014 07:22:50 +0100
Correct previous dual key logic pending first delivery installment.
michael@0 | 1 | /* This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 2 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 3 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 4 | |
michael@0 | 5 | package org.mozilla.gecko.favicons.cache; |
michael@0 | 6 | |
michael@0 | 7 | import android.graphics.Bitmap; |
michael@0 | 8 | import android.util.Log; |
michael@0 | 9 | import org.mozilla.gecko.gfx.BitmapUtils; |
michael@0 | 10 | |
michael@0 | 11 | import java.util.ArrayList; |
michael@0 | 12 | import java.util.Collections; |
michael@0 | 13 | |
michael@0 | 14 | public class FaviconsForURL { |
michael@0 | 15 | private static final String LOGTAG = "FaviconForURL"; |
michael@0 | 16 | |
michael@0 | 17 | private volatile int dominantColor = -1; |
michael@0 | 18 | |
michael@0 | 19 | final long downloadTimestamp; |
michael@0 | 20 | final ArrayList<FaviconCacheElement> favicons; |
michael@0 | 21 | |
michael@0 | 22 | public final boolean hasFailed; |
michael@0 | 23 | |
michael@0 | 24 | public FaviconsForURL(int size) { |
michael@0 | 25 | this(size, false); |
michael@0 | 26 | } |
michael@0 | 27 | |
michael@0 | 28 | public FaviconsForURL(int size, boolean failed) { |
michael@0 | 29 | hasFailed = failed; |
michael@0 | 30 | downloadTimestamp = System.currentTimeMillis(); |
michael@0 | 31 | favicons = new ArrayList<FaviconCacheElement>(size); |
michael@0 | 32 | } |
michael@0 | 33 | |
michael@0 | 34 | public FaviconCacheElement addSecondary(Bitmap favicon, int imageSize) { |
michael@0 | 35 | return addInternal(favicon, false, imageSize); |
michael@0 | 36 | } |
michael@0 | 37 | |
michael@0 | 38 | public FaviconCacheElement addPrimary(Bitmap favicon) { |
michael@0 | 39 | return addInternal(favicon, true, favicon.getWidth()); |
michael@0 | 40 | } |
michael@0 | 41 | |
michael@0 | 42 | private FaviconCacheElement addInternal(Bitmap favicon, boolean isPrimary, int imageSize) { |
michael@0 | 43 | FaviconCacheElement c = new FaviconCacheElement(favicon, isPrimary, imageSize, this); |
michael@0 | 44 | |
michael@0 | 45 | int index = Collections.binarySearch(favicons, c); |
michael@0 | 46 | |
michael@0 | 47 | // We've already got an equivalent one. We don't care about this new one. This only occurs in certain obscure |
michael@0 | 48 | // case conditions. |
michael@0 | 49 | if (index >= 0) { |
michael@0 | 50 | return favicons.get(index); |
michael@0 | 51 | } |
michael@0 | 52 | |
michael@0 | 53 | // binarySearch returns -x - 1 where x is the insertion point of the element. Convert |
michael@0 | 54 | // this to the actual insertion point.. |
michael@0 | 55 | index++; |
michael@0 | 56 | index = -index; |
michael@0 | 57 | favicons.add(index, c); |
michael@0 | 58 | |
michael@0 | 59 | return c; |
michael@0 | 60 | } |
michael@0 | 61 | |
michael@0 | 62 | /** |
michael@0 | 63 | * Get the index of the smallest image in this collection larger than or equal to |
michael@0 | 64 | * the given target size. |
michael@0 | 65 | * |
michael@0 | 66 | * @param targetSize Minimum size for the desired result. |
michael@0 | 67 | * @return The index of the smallest image larger than the target size, or -1 if none exists. |
michael@0 | 68 | */ |
michael@0 | 69 | public int getNextHighestIndex(int targetSize) { |
michael@0 | 70 | // Create a dummy object to hold the target value for comparable. |
michael@0 | 71 | FaviconCacheElement dummy = new FaviconCacheElement(null, false, targetSize, null); |
michael@0 | 72 | |
michael@0 | 73 | int index = Collections.binarySearch(favicons, dummy); |
michael@0 | 74 | |
michael@0 | 75 | // The search routine returns the index of an element equal to dummy, if present. |
michael@0 | 76 | // Otherwise, it returns -x - 1, where x is the index in the ArrayList where dummy would be |
michael@0 | 77 | // inserted if the list were to remain sorted. |
michael@0 | 78 | if (index < 0) { |
michael@0 | 79 | index++; |
michael@0 | 80 | index = -index; |
michael@0 | 81 | } |
michael@0 | 82 | |
michael@0 | 83 | // index is now 'x', as described above. |
michael@0 | 84 | |
michael@0 | 85 | // The routine will return favicons.size() as the index iff dummy is larger than all elements |
michael@0 | 86 | // present (So the "index at which it should be inserted" is the index after the end. |
michael@0 | 87 | // In this case, we set the sentinel value -1 to indicate that we just requested something |
michael@0 | 88 | // larger than all primaries. |
michael@0 | 89 | if (index == favicons.size()) { |
michael@0 | 90 | index = -1; |
michael@0 | 91 | } |
michael@0 | 92 | |
michael@0 | 93 | return index; |
michael@0 | 94 | } |
michael@0 | 95 | |
michael@0 | 96 | /** |
michael@0 | 97 | * Get the next valid primary icon from this collection, starting at the given index. |
michael@0 | 98 | * If the appropriate icon is found, but is invalid, we return null - the proper response is to |
michael@0 | 99 | * reacquire the primary from the database. |
michael@0 | 100 | * If no icon is found, the search is repeated going backwards from the start index to find any |
michael@0 | 101 | * primary at all (The input index may be a secondary which is larger than the actual available |
michael@0 | 102 | * primary.) |
michael@0 | 103 | * |
michael@0 | 104 | * @param fromIndex The index into favicons from which to start the search. |
michael@0 | 105 | * @return The FaviconCacheElement of the next valid primary from the given index. If none exists, |
michael@0 | 106 | * then returns the previous valid primary. If none exists, returns null (Insanity.). |
michael@0 | 107 | */ |
michael@0 | 108 | public FaviconCacheElement getNextPrimary(final int fromIndex) { |
michael@0 | 109 | final int numIcons = favicons.size(); |
michael@0 | 110 | |
michael@0 | 111 | int searchIndex = fromIndex; |
michael@0 | 112 | while (searchIndex < numIcons) { |
michael@0 | 113 | FaviconCacheElement element = favicons.get(searchIndex); |
michael@0 | 114 | |
michael@0 | 115 | if (element.isPrimary) { |
michael@0 | 116 | if (element.invalidated) { |
michael@0 | 117 | // We return null here, despite the possible existence of other primaries, |
michael@0 | 118 | // because we know the most suitable primary for this request exists, but is |
michael@0 | 119 | // no longer in the cache. By returning null, we cause the caller to load the |
michael@0 | 120 | // missing primary from the database and call again. |
michael@0 | 121 | return null; |
michael@0 | 122 | } |
michael@0 | 123 | return element; |
michael@0 | 124 | } |
michael@0 | 125 | searchIndex++; |
michael@0 | 126 | } |
michael@0 | 127 | |
michael@0 | 128 | // No larger primary available. Let's look for smaller ones... |
michael@0 | 129 | searchIndex = fromIndex - 1; |
michael@0 | 130 | while (searchIndex >= 0) { |
michael@0 | 131 | FaviconCacheElement element = favicons.get(searchIndex); |
michael@0 | 132 | |
michael@0 | 133 | if (element.isPrimary) { |
michael@0 | 134 | if (element.invalidated) { |
michael@0 | 135 | return null; |
michael@0 | 136 | } |
michael@0 | 137 | return element; |
michael@0 | 138 | } |
michael@0 | 139 | searchIndex--; |
michael@0 | 140 | } |
michael@0 | 141 | |
michael@0 | 142 | Log.e(LOGTAG, "No primaries found in Favicon cache structure. This is madness!"); |
michael@0 | 143 | |
michael@0 | 144 | return null; |
michael@0 | 145 | } |
michael@0 | 146 | |
michael@0 | 147 | /** |
michael@0 | 148 | * Ensure the dominant colour field is populated for this favicon. |
michael@0 | 149 | */ |
michael@0 | 150 | public int ensureDominantColor() { |
michael@0 | 151 | if (dominantColor == -1) { |
michael@0 | 152 | // Find a payload, any payload, that is not invalidated. |
michael@0 | 153 | for (FaviconCacheElement element : favicons) { |
michael@0 | 154 | if (!element.invalidated) { |
michael@0 | 155 | dominantColor = BitmapUtils.getDominantColor(element.faviconPayload); |
michael@0 | 156 | return dominantColor; |
michael@0 | 157 | } |
michael@0 | 158 | } |
michael@0 | 159 | dominantColor = 0xFFFFFF; |
michael@0 | 160 | } |
michael@0 | 161 | |
michael@0 | 162 | return dominantColor; |
michael@0 | 163 | } |
michael@0 | 164 | } |