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.
5 package org.chromium.chrome.browser.favicon;
7 import android.graphics.Bitmap;
8 import android.graphics.Color;
10 import org.chromium.base.CalledByNative;
11 import org.chromium.chrome.browser.profiles.Profile;
14 * This is a helper class to use favicon_service.cc's functionality.
16 * You can request a favicon image by web page URL. Note that an instance of
17 * this class should be created & used & destroyed (by destroy()) in the same
18 * thread due to the C++ base::CancelableTaskTracker class
21 public class FaviconHelper {
23 // Please keep in sync with favicon_types.h's IconType.
24 public static final int INVALID_ICON = 0;
25 public static final int FAVICON = 1 << 0;
26 public static final int TOUCH_ICON = 1 << 1;
27 public static final int TOUCH_PRECOMPOSED_ICON = 1 << 2;
29 private long mNativeFaviconHelper;
32 * Callback interface for getting the result from getLocalFaviconImageForURL method.
34 public interface FaviconImageCallback {
36 * This method will be called when the result favicon is ready.
37 * @param image Favicon image.
38 * @param iconUrl Favicon image's icon url.
40 @CalledByNative("FaviconImageCallback")
41 public void onFaviconAvailable(Bitmap image, String iconUrl);
45 * Allocate and initialize the C++ side of this class.
47 public FaviconHelper() {
48 mNativeFaviconHelper = nativeInit();
52 protected void finalize() {
53 // Ensure that destroy() was called.
54 assert mNativeFaviconHelper == 0;
58 * Clean up the C++ side of this class. After the call, this class instance shouldn't be used.
60 public void destroy() {
61 assert mNativeFaviconHelper != 0;
62 nativeDestroy(mNativeFaviconHelper);
63 mNativeFaviconHelper = 0;
67 * Get Favicon bitmap for the requested arguments. Retrieves favicons only for pages the user
68 * has visited on the current device.
69 * @param profile Profile used for the FaviconService construction.
70 * @param pageUrl The target Page URL to get the favicon.
71 * @param iconTypes One of the IconType class values.
72 * @param desiredSizeInDip The size of the favicon in dip we want to get.
73 * @param faviconImageCallback A method to be called back when the result is available.
74 * Note that this callback is not called if this method returns
76 * @return True if GetLocalFaviconImageForURL is successfully called.
78 public boolean getLocalFaviconImageForURL(
79 Profile profile, String pageUrl, int iconTypes,
80 int desiredSizeInDip, FaviconImageCallback faviconImageCallback) {
81 assert mNativeFaviconHelper != 0;
82 return nativeGetLocalFaviconImageForURL(mNativeFaviconHelper, profile, pageUrl, iconTypes,
83 desiredSizeInDip, faviconImageCallback);
87 * Return the dominant color of a given bitmap in {@link Color} format.
88 * @param image The bitmap image to find the dominant color for.
89 * @return The dominant color in {@link Color} format.
91 public int getDominantColorForBitmap(Bitmap image) {
92 return nativeGetDominantColorForBitmap(mNativeFaviconHelper, image);
96 * Get 16x16 Favicon bitmap for the requested arguments. Only retrives favicons in synced
97 * session storage. (e.g. favicons synced from other devices).
98 * TODO(apiccion): provide a way to obtain higher resolution favicons.
99 * @param profile Profile used for the FaviconService construction.
100 * @param pageUrl The target Page URL to get the favicon.
102 * @return 16x16 favicon Bitmap corresponding to the pageUrl.
104 public Bitmap getSyncedFaviconImageForURL(Profile profile, String pageUrl) {
105 assert mNativeFaviconHelper != 0;
106 return nativeGetSyncedFaviconImageForURL(mNativeFaviconHelper, profile, pageUrl);
109 private static native long nativeInit();
110 private static native void nativeDestroy(long nativeFaviconHelper);
111 private static native boolean nativeGetLocalFaviconImageForURL(long nativeFaviconHelper,
112 Profile profile, String pageUrl, int iconTypes, int desiredSizeInDip,
113 FaviconImageCallback faviconImageCallback);
114 private static native Bitmap nativeGetSyncedFaviconImageForURL(long nativeFaviconHelper,
115 Profile profile, String pageUrl);
116 private static native int nativeGetDominantColorForBitmap(long nativeFaviconHelper,