OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 package org.chromium.chrome.browser.banners; | 5 package org.chromium.chrome.browser.banners; |
6 | 6 |
7 import android.app.Activity; | 7 import android.app.Activity; |
8 import android.content.ActivityNotFoundException; | 8 import android.content.ActivityNotFoundException; |
9 import android.content.ContentResolver; | 9 import android.content.ContentResolver; |
10 import android.content.Intent; | 10 import android.content.Intent; |
11 import android.content.IntentSender; | 11 import android.content.IntentSender; |
12 import android.content.pm.PackageManager; | 12 import android.content.pm.PackageManager; |
13 import android.graphics.Bitmap; | 13 import android.graphics.Bitmap; |
14 import android.graphics.drawable.BitmapDrawable; | 14 import android.graphics.drawable.BitmapDrawable; |
15 import android.text.TextUtils; | 15 import android.text.TextUtils; |
16 import android.util.Log; | 16 import android.util.Log; |
17 | 17 |
18 import org.chromium.base.CalledByNative; | 18 import org.chromium.base.CalledByNative; |
19 import org.chromium.chrome.browser.EmptyTabObserver; | 19 import org.chromium.chrome.browser.EmptyTabObserver; |
20 import org.chromium.chrome.browser.TabBase; | 20 import org.chromium.chrome.browser.Tab; |
21 import org.chromium.chrome.browser.TabObserver; | 21 import org.chromium.chrome.browser.TabObserver; |
22 import org.chromium.content.browser.ContentView; | 22 import org.chromium.content.browser.ContentView; |
23 import org.chromium.content_public.browser.WebContents; | 23 import org.chromium.content_public.browser.WebContents; |
24 import org.chromium.ui.R; | 24 import org.chromium.ui.R; |
25 import org.chromium.ui.base.WindowAndroid; | 25 import org.chromium.ui.base.WindowAndroid; |
26 import org.chromium.ui.base.WindowAndroid.IntentCallback; | 26 import org.chromium.ui.base.WindowAndroid.IntentCallback; |
27 | 27 |
28 /** | 28 /** |
29 * Manages an AppBannerView for a TabBase and its ContentView. | 29 * Manages an AppBannerView for a Tab and its ContentView. |
30 * | 30 * |
31 * The AppBannerManager manages a single AppBannerView, dismissing it when the u
ser navigates to a | 31 * The AppBannerManager manages a single AppBannerView, dismissing it when the u
ser navigates to a |
32 * new page or creating a new one when it detects that the current webpage is re
questing a banner | 32 * new page or creating a new one when it detects that the current webpage is re
questing a banner |
33 * to be built. The actual observation of the WebContents (which triggers the a
utomatic creation | 33 * to be built. The actual observation of the WebContents (which triggers the a
utomatic creation |
34 * and removal of banners, among other things) is done by the native-side AppBan
nerManager. | 34 * and removal of banners, among other things) is done by the native-side AppBan
nerManager. |
35 * | 35 * |
36 * This Java-side class owns its native-side counterpart, which is basically use
d to grab resources | 36 * This Java-side class owns its native-side counterpart, which is basically use
d to grab resources |
37 * from the network. | 37 * from the network. |
38 */ | 38 */ |
39 public class AppBannerManager implements AppBannerView.Observer, InstallerDelega
te.Observer, | 39 public class AppBannerManager implements AppBannerView.Observer, InstallerDelega
te.Observer, |
40 AppDetailsDelegate.Observer, IntentCallback { | 40 AppDetailsDelegate.Observer, IntentCallback { |
41 private static final String TAG = "AppBannerManager"; | 41 private static final String TAG = "AppBannerManager"; |
42 | 42 |
43 /** Retrieves information about a given package. */ | 43 /** Retrieves information about a given package. */ |
44 private static AppDetailsDelegate sAppDetailsDelegate; | 44 private static AppDetailsDelegate sAppDetailsDelegate; |
45 | 45 |
46 /** Pointer to the native side AppBannerManager. */ | 46 /** Pointer to the native side AppBannerManager. */ |
47 private final long mNativePointer; | 47 private final long mNativePointer; |
48 | 48 |
49 /** TabBase that the AppBannerView/AppBannerManager is owned by. */ | 49 /** Tab that the AppBannerView/AppBannerManager is owned by. */ |
50 private final TabBase mTabBase; | 50 private final Tab mTab; |
51 | 51 |
52 /** ContentView that the AppBannerView/AppBannerManager is currently attache
d to. */ | 52 /** ContentView that the AppBannerView/AppBannerManager is currently attache
d to. */ |
53 private ContentView mContentView; | 53 private ContentView mContentView; |
54 | 54 |
55 /** Current banner being shown. */ | 55 /** Current banner being shown. */ |
56 private AppBannerView mBannerView; | 56 private AppBannerView mBannerView; |
57 | 57 |
58 /** Data about the app being advertised. */ | 58 /** Data about the app being advertised. */ |
59 private AppData mAppData; | 59 private AppData mAppData; |
60 | 60 |
(...skipping 14 matching lines...) Expand all Loading... |
75 */ | 75 */ |
76 public static void setAppDetailsDelegate(AppDetailsDelegate delegate) { | 76 public static void setAppDetailsDelegate(AppDetailsDelegate delegate) { |
77 if (sAppDetailsDelegate != null) sAppDetailsDelegate.destroy(); | 77 if (sAppDetailsDelegate != null) sAppDetailsDelegate.destroy(); |
78 sAppDetailsDelegate = delegate; | 78 sAppDetailsDelegate = delegate; |
79 } | 79 } |
80 | 80 |
81 /** | 81 /** |
82 * Constructs an AppBannerManager for the given tab. | 82 * Constructs an AppBannerManager for the given tab. |
83 * @param tab Tab that the AppBannerManager will be attached to. | 83 * @param tab Tab that the AppBannerManager will be attached to. |
84 */ | 84 */ |
85 public AppBannerManager(TabBase tab) { | 85 public AppBannerManager(Tab tab) { |
86 mNativePointer = nativeInit(); | 86 mNativePointer = nativeInit(); |
87 mTabBase = tab; | 87 mTab = tab; |
88 mTabBase.addObserver(createTabObserver()); | 88 mTab.addObserver(createTabObserver()); |
89 updatePointers(); | 89 updatePointers(); |
90 } | 90 } |
91 | 91 |
92 /** | 92 /** |
93 * Creates a TabObserver for monitoring a TabBase, used to react to changes
in the ContentView | 93 * Creates a TabObserver for monitoring a Tab, used to react to changes in t
he ContentView |
94 * or to trigger its own destruction. | 94 * or to trigger its own destruction. |
95 * @return TabObserver that can be used to monitor a TabBase. | 95 * @return TabObserver that can be used to monitor a Tab. |
96 */ | 96 */ |
97 private TabObserver createTabObserver() { | 97 private TabObserver createTabObserver() { |
98 return new EmptyTabObserver() { | 98 return new EmptyTabObserver() { |
99 @Override | 99 @Override |
100 public void onWebContentsSwapped(TabBase tab, boolean didStartLoad, | 100 public void onWebContentsSwapped(Tab tab, boolean didStartLoad, |
101 boolean didFinishLoad) { | 101 boolean didFinishLoad) { |
102 updatePointers(); | 102 updatePointers(); |
103 } | 103 } |
104 | 104 |
105 @Override | 105 @Override |
106 public void onContentChanged(TabBase tab) { | 106 public void onContentChanged(Tab tab) { |
107 updatePointers(); | 107 updatePointers(); |
108 } | 108 } |
109 | 109 |
110 @Override | 110 @Override |
111 public void onDestroyed(TabBase tab) { | 111 public void onDestroyed(Tab tab) { |
112 nativeDestroy(mNativePointer); | 112 nativeDestroy(mNativePointer); |
113 resetState(); | 113 resetState(); |
114 } | 114 } |
115 }; | 115 }; |
116 } | 116 } |
117 | 117 |
118 /** | 118 /** |
119 * Updates which ContentView and WebContents the AppBannerView is monitoring
. | 119 * Updates which ContentView and WebContents the AppBannerView is monitoring
. |
120 */ | 120 */ |
121 private void updatePointers() { | 121 private void updatePointers() { |
122 if (mContentView != mTabBase.getContentView()) mContentView = mTabBase.g
etContentView(); | 122 if (mContentView != mTab.getContentView()) mContentView = mTab.getConten
tView(); |
123 nativeReplaceWebContents(mNativePointer, mTabBase.getWebContents()); | 123 nativeReplaceWebContents(mNativePointer, mTab.getWebContents()); |
124 } | 124 } |
125 | 125 |
126 /** | 126 /** |
127 * Grabs package information for the banner asynchronously. | 127 * Grabs package information for the banner asynchronously. |
128 * @param url URL for the page that is triggering the banner. | 128 * @param url URL for the page that is triggering the banner. |
129 * @param packageName Name of the package that is being advertised. | 129 * @param packageName Name of the package that is being advertised. |
130 */ | 130 */ |
131 @CalledByNative | 131 @CalledByNative |
132 private void prepareBanner(String url, String packageName) { | 132 private void prepareBanner(String url, String packageName) { |
133 // Get rid of whatever banner is there currently. | 133 // Get rid of whatever banner is there currently. |
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
183 if (mBannerView != null) mBannerView.dismiss(); | 183 if (mBannerView != null) mBannerView.dismiss(); |
184 resetState(); | 184 resetState(); |
185 } | 185 } |
186 | 186 |
187 @Override | 187 @Override |
188 public void onButtonClicked(AppBannerView banner) { | 188 public void onButtonClicked(AppBannerView banner) { |
189 if (mBannerView != banner) return; | 189 if (mBannerView != banner) return; |
190 | 190 |
191 if (mAppData.installState() == AppData.INSTALL_STATE_NOT_INSTALLED) { | 191 if (mAppData.installState() == AppData.INSTALL_STATE_NOT_INSTALLED) { |
192 // The user initiated an install. | 192 // The user initiated an install. |
193 WindowAndroid window = mTabBase.getWindowAndroid(); | 193 WindowAndroid window = mTab.getWindowAndroid(); |
194 if (window.showIntent(mAppData.installIntent(), this, R.string.low_m
emory_error)) { | 194 if (window.showIntent(mAppData.installIntent(), this, R.string.low_m
emory_error)) { |
195 // Temporarily hide the banner. | 195 // Temporarily hide the banner. |
196 mBannerView.createVerticalSnapAnimation(false); | 196 mBannerView.createVerticalSnapAnimation(false); |
197 } else { | 197 } else { |
198 Log.e(TAG, "Failed to fire install intent."); | 198 Log.e(TAG, "Failed to fire install intent."); |
199 dismissCurrentBanner(); | 199 dismissCurrentBanner(); |
200 } | 200 } |
201 } else if (mAppData.installState() == AppData.INSTALL_STATE_INSTALLED) { | 201 } else if (mAppData.installState() == AppData.INSTALL_STATE_INSTALLED) { |
202 // The app is installed. Open it. | 202 // The app is installed. Open it. |
203 String packageName = mAppData.packageName(); | 203 String packageName = mAppData.packageName(); |
(...skipping 95 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
299 | 299 |
300 private static native boolean nativeIsEnabled(); | 300 private static native boolean nativeIsEnabled(); |
301 private native long nativeInit(); | 301 private native long nativeInit(); |
302 private native void nativeDestroy(long nativeAppBannerManager); | 302 private native void nativeDestroy(long nativeAppBannerManager); |
303 private native void nativeReplaceWebContents( | 303 private native void nativeReplaceWebContents( |
304 long nativeAppBannerManager, WebContents webContents); | 304 long nativeAppBannerManager, WebContents webContents); |
305 private native void nativeBlockBanner( | 305 private native void nativeBlockBanner( |
306 long nativeAppBannerManager, String url, String packageName); | 306 long nativeAppBannerManager, String url, String packageName); |
307 private native boolean nativeFetchIcon(long nativeAppBannerManager, String i
mageUrl); | 307 private native boolean nativeFetchIcon(long nativeAppBannerManager, String i
mageUrl); |
308 } | 308 } |
OLD | NEW |