FaviconHelper.java revision f2477e01787aa58f445919b809d89e252beef54f
1// Copyright 2013 The Chromium Authors. All rights reserved. 2// Use of this source code is governed by a BSD-style license that can be 3// found in the LICENSE file. 4 5package org.chromium.chrome.browser.favicon; 6 7import android.graphics.Bitmap; 8import android.graphics.Color; 9 10import org.chromium.base.CalledByNative; 11import org.chromium.chrome.browser.profiles.Profile; 12 13/** 14 * This is a helper class to use favicon_service.cc's functionality. 15 * 16 * You can request a favicon image by web page URL. Note that an instance of this class should be 17 * created & used & destroyed (by destroy()) in the same thread due to the C++ CancelableTaskTracker 18 * class requirement. 19 */ 20public class FaviconHelper { 21 22 // Please keep in sync with favicon_types.h's IconType. 23 public static final int INVALID_ICON = 0; 24 public static final int FAVICON = 1 << 0; 25 public static final int TOUCH_ICON = 1 << 1; 26 public static final int TOUCH_PRECOMPOSED_ICON = 1 << 2; 27 28 private long mNativeFaviconHelper; 29 30 /** 31 * Callback interface for getting the result from getLocalFaviconImageForURL method. 32 */ 33 public interface FaviconImageCallback { 34 /** 35 * This method will be called when the result favicon is ready. 36 * @param image Favicon image. 37 * @param iconUrl Favicon image's icon url. 38 */ 39 @CalledByNative("FaviconImageCallback") 40 public void onFaviconAvailable(Bitmap image, String iconUrl); 41 } 42 43 /** 44 * Allocate and initialize the C++ side of this class. 45 */ 46 public FaviconHelper() { 47 mNativeFaviconHelper = nativeInit(); 48 } 49 50 @Override 51 protected void finalize() { 52 // Ensure that destroy() was called. 53 assert mNativeFaviconHelper == 0; 54 } 55 56 /** 57 * Clean up the C++ side of this class. After the call, this class instance shouldn't be used. 58 */ 59 public void destroy() { 60 assert mNativeFaviconHelper != 0; 61 nativeDestroy(mNativeFaviconHelper); 62 mNativeFaviconHelper = 0; 63 } 64 65 /** 66 * Get Favicon bitmap for the requested arguments. Retrieves favicons only for pages the user 67 * has visited on the current device. 68 * @param profile Profile used for the FaviconService construction. 69 * @param pageUrl The target Page URL to get the favicon. 70 * @param iconTypes One of the IconType class values. 71 * @param desiredSizeInDip The size of the favicon in dip we want to get. 72 * @param faviconImageCallback A method to be called back when the result is available. 73 * Note that this callback is not called if this method returns 74 * false. 75 * @return True if GetLocalFaviconImageForURL is successfully called. 76 */ 77 public boolean getLocalFaviconImageForURL( 78 Profile profile, String pageUrl, int iconTypes, 79 int desiredSizeInDip, FaviconImageCallback faviconImageCallback) { 80 assert mNativeFaviconHelper != 0; 81 return nativeGetLocalFaviconImageForURL(mNativeFaviconHelper, profile, pageUrl, iconTypes, 82 desiredSizeInDip, faviconImageCallback); 83 } 84 85 /** 86 * Return the dominant color of a given bitmap in {@link Color} format. 87 * @param image The bitmap image to find the dominant color for. 88 * @return The dominant color in {@link Color} format. 89 */ 90 public int getDominantColorForBitmap(Bitmap image) { 91 return nativeGetDominantColorForBitmap(mNativeFaviconHelper, image); 92 } 93 94 /** 95 * Get 16x16 Favicon bitmap for the requested arguments. Only retrives favicons in synced 96 * session storage. (e.g. favicons synced from other devices). 97 * TODO(apiccion): provide a way to obtain higher resolution favicons. 98 * @param profile Profile used for the FaviconService construction. 99 * @param pageUrl The target Page URL to get the favicon. 100 * 101 * @return 16x16 favicon Bitmap corresponding to the pageUrl. 102 */ 103 public Bitmap getSyncedFaviconImageForURL(Profile profile, String pageUrl) { 104 assert mNativeFaviconHelper != 0; 105 return nativeGetSyncedFaviconImageForURL(mNativeFaviconHelper, profile, pageUrl); 106 } 107 108 private static native long nativeInit(); 109 private static native void nativeDestroy(long nativeFaviconHelper); 110 private static native boolean nativeGetLocalFaviconImageForURL(long nativeFaviconHelper, 111 Profile profile, String pageUrl, int iconTypes, int desiredSizeInDip, 112 FaviconImageCallback faviconImageCallback); 113 private static native Bitmap nativeGetSyncedFaviconImageForURL(long nativeFaviconHelper, 114 Profile profile, String pageUrl); 115 private static native int nativeGetDominantColorForBitmap(long nativeFaviconHelper, 116 Bitmap image); 117} 118