[App banners] Add UMA metrics
[chromium-blink-merge.git] / chrome / android / java / src / org / chromium / chrome / browser / banners / AppBannerManager.java
blob2906df5f5fd456502c5438e837363212bd93addc
1 // Copyright 2014 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.banners;
7 import android.text.TextUtils;
9 import org.chromium.base.ApplicationStatus;
10 import org.chromium.base.CalledByNative;
11 import org.chromium.base.JNINamespace;
12 import org.chromium.base.VisibleForTesting;
13 import org.chromium.chrome.R;
14 import org.chromium.chrome.browser.EmptyTabObserver;
15 import org.chromium.chrome.browser.Tab;
16 import org.chromium.content_public.browser.WebContents;
18 /**
19 * Manages an AppBannerInfoBar for a Tab.
21 * The AppBannerManager manages a single AppBannerInfoBar, creating a new one when it detects that
22 * the current webpage is requesting a banner to be built. The actual observation of the WebContents
23 * (which triggers the automatic creation and removal of banners, among other things) is done by the
24 * native-side AppBannerManager.
26 * This Java-side class owns its native-side counterpart, which is basically used to grab resources
27 * from the network.
29 @JNINamespace("banners")
30 public class AppBannerManager extends EmptyTabObserver {
31 private static final String TAG = "AppBannerManager";
33 /** Retrieves information about a given package. */
34 private static AppDetailsDelegate sAppDetailsDelegate;
36 /** Whether the banners are enabled. */
37 private static Boolean sIsEnabled;
39 /** Pointer to the native side AppBannerManager. */
40 private final long mNativePointer;
42 /** Tab that the AppBannerView/AppBannerManager is owned by. */
43 private final Tab mTab;
45 /**
46 * Checks if app banners are enabled.
47 * @return True if banners are enabled, false otherwise.
49 public static boolean isEnabled() {
50 if (sIsEnabled == null) sIsEnabled = nativeIsEnabled();
51 return sIsEnabled;
54 /**
55 * Sets the delegate that provides information about a given package.
56 * @param delegate Delegate to use. Previously set ones are destroyed.
58 public static void setAppDetailsDelegate(AppDetailsDelegate delegate) {
59 if (sAppDetailsDelegate != null) sAppDetailsDelegate.destroy();
60 sAppDetailsDelegate = delegate;
63 /**
64 * Constructs an AppBannerManager for the given tab.
65 * @param tab Tab that the AppBannerManager will be attached to.
67 public AppBannerManager(Tab tab) {
68 mNativePointer = nativeInit();
69 mTab = tab;
70 updatePointers();
73 @Override
74 public void onWebContentsSwapped(Tab tab, boolean didStartLoad,
75 boolean didFinishLoad) {
76 updatePointers();
79 @Override
80 public void onContentChanged(Tab tab) {
81 updatePointers();
84 /**
85 * Destroys the native AppBannerManager.
87 public void destroy() {
88 nativeDestroy(mNativePointer);
91 /**
92 * Updates which WebContents the native AppBannerManager is monitoring.
94 private void updatePointers() {
95 nativeReplaceWebContents(mNativePointer, mTab.getWebContents());
98 @CalledByNative
99 private int getPreferredIconSize() {
100 return ApplicationStatus.getApplicationContext().getResources().getDimensionPixelSize(
101 R.dimen.app_banner_icon_size);
105 * Grabs package information for the banner asynchronously.
106 * @param url URL for the page that is triggering the banner.
107 * @param packageName Name of the package that is being advertised.
109 @CalledByNative
110 private void fetchAppDetails(String url, String packageName) {
111 if (sAppDetailsDelegate == null) return;
112 int iconSize = getPreferredIconSize();
113 sAppDetailsDelegate.getAppDetailsAsynchronously(
114 createAppDetailsObserver(), url, packageName, iconSize);
117 private AppDetailsDelegate.Observer createAppDetailsObserver() {
118 return new AppDetailsDelegate.Observer() {
120 * Called when data about the package has been retrieved, which includes the url for the
121 * app's icon but not the icon Bitmap itself.
122 * @param data Data about the app. Null if the task failed.
124 @Override
125 public void onAppDetailsRetrieved(AppData data) {
126 if (data == null) return;
128 String imageUrl = data.imageUrl();
129 if (TextUtils.isEmpty(imageUrl)) return;
131 nativeOnAppDetailsRetrieved(
132 mNativePointer, data, data.title(), data.packageName(), data.imageUrl());
137 /** Enables the app banners for testing. */
138 @VisibleForTesting
139 static void setIsEnabledForTesting() {
140 sIsEnabled = true;
143 /** Sets a constant (in days) that gets added to the time when the current time is requested. */
144 @VisibleForTesting
145 static void setTimeDeltaForTesting(int days) {
146 nativeSetTimeDeltaForTesting(days);
149 /** Returns whether a BitmapFetcher is actively retrieving an app icon. */
150 @VisibleForTesting
151 public boolean isFetcherActiveForTesting() {
152 return nativeIsFetcherActive(mNativePointer);
155 private static native boolean nativeIsEnabled();
156 private native long nativeInit();
157 private native void nativeDestroy(long nativeAppBannerManager);
158 private native void nativeReplaceWebContents(long nativeAppBannerManager,
159 WebContents webContents);
160 private native boolean nativeOnAppDetailsRetrieved(long nativeAppBannerManager, AppData data,
161 String title, String packageName, String imageUrl);
163 // Testing methods.
164 private static native void nativeSetTimeDeltaForTesting(int days);
165 private native boolean nativeIsFetcherActive(long nativeAppBannerManager);