This source file includes following definitions.
- JNINamespace
- isEnabled
- setAppDetailsDelegate
- createTabObserver
- updatePointers
- prepareBanner
- onAppDetailsRetrieved
- createBanner
- dismissCurrentBanner
- onBannerRemoved
- onBannerBlocked
- onBannerDismissEvent
- onBannerInstallEvent
- onFireIntent
- resetState
- isBannerForCurrentPage
- nativeIsEnabled
- nativeInit
- nativeDestroy
- nativeReplaceWebContents
- nativeBlockBanner
- nativeFetchIcon
- nativeRecordDismissEvent
- nativeRecordInstallEvent
package org.chromium.chrome.browser.banners;
import android.app.PendingIntent;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.text.TextUtils;
import org.chromium.base.CalledByNative;
import org.chromium.base.JNINamespace;
import org.chromium.chrome.browser.EmptyTabObserver;
import org.chromium.chrome.browser.Tab;
import org.chromium.chrome.browser.TabObserver;
import org.chromium.content.browser.ContentView;
import org.chromium.content_public.browser.WebContents;
import org.chromium.ui.R;
@JNINamespace("banners")
public class AppBannerManager implements AppBannerView.Observer, AppDetailsDelegate.Observer {
private static final String TAG = "AppBannerManager";
private static AppDetailsDelegate sAppDetailsDelegate;
private final long mNativePointer;
private final Tab mTab;
private ContentView mContentView;
private AppBannerView mBannerView;
private AppData mAppData;
public static boolean isEnabled() {
return nativeIsEnabled();
}
public static void setAppDetailsDelegate(AppDetailsDelegate delegate) {
if (sAppDetailsDelegate != null) sAppDetailsDelegate.destroy();
sAppDetailsDelegate = delegate;
}
public AppBannerManager(Tab tab) {
mNativePointer = nativeInit();
mTab = tab;
mTab.addObserver(createTabObserver());
updatePointers();
}
private TabObserver createTabObserver() {
return new EmptyTabObserver() {
@Override
public void onWebContentsSwapped(Tab tab, boolean didStartLoad,
boolean didFinishLoad) {
updatePointers();
}
@Override
public void onContentChanged(Tab tab) {
updatePointers();
}
@Override
public void onDestroyed(Tab tab) {
nativeDestroy(mNativePointer);
mContentView = null;
resetState();
}
};
}
private void updatePointers() {
if (mContentView != mTab.getContentView()) mContentView = mTab.getContentView();
nativeReplaceWebContents(mNativePointer, mTab.getWebContents());
}
@CalledByNative
private void prepareBanner(String url, String packageName) {
if (mBannerView != null) dismissCurrentBanner(AppBannerMetricsIds.DISMISS_ERROR);
if (sAppDetailsDelegate == null || !isBannerForCurrentPage(url)) return;
int iconSize = AppBannerView.getIconSize(mContentView.getContext());
sAppDetailsDelegate.getAppDetailsAsynchronously(this, url, packageName, iconSize);
}
@Override
public void onAppDetailsRetrieved(AppData data) {
if (data == null || !isBannerForCurrentPage(data.siteUrl())) return;
mAppData = data;
String imageUrl = data.imageUrl();
if (TextUtils.isEmpty(imageUrl) || !nativeFetchIcon(mNativePointer, imageUrl)) resetState();
}
@CalledByNative
private boolean createBanner(String imageUrl, Bitmap appIcon) {
if (mAppData == null || !isBannerForCurrentPage(mAppData.siteUrl())) return false;
if (!TextUtils.equals(mAppData.imageUrl(), imageUrl)) {
resetState();
return false;
}
mAppData.setIcon(new BitmapDrawable(mContentView.getContext().getResources(), appIcon));
mBannerView = AppBannerView.create(mContentView, this, mAppData);
return true;
}
@CalledByNative
private void dismissCurrentBanner(int dismissalType) {
if (mBannerView != null) mBannerView.dismiss(dismissalType);
resetState();
}
@Override
public void onBannerRemoved(AppBannerView banner) {
if (mBannerView != banner) return;
resetState();
}
@Override
public void onBannerBlocked(AppBannerView banner, String url, String packageName) {
if (mBannerView != banner) return;
nativeBlockBanner(mNativePointer, url, packageName);
}
@Override
public void onBannerDismissEvent(AppBannerView banner, int eventType) {
if (mBannerView != banner) return;
nativeRecordDismissEvent(eventType);
}
@Override
public void onBannerInstallEvent(AppBannerView banner, int eventType) {
if (mBannerView != banner) return;
nativeRecordInstallEvent(eventType);
}
@Override
public boolean onFireIntent(AppBannerView banner, PendingIntent intent) {
if (mBannerView != banner) return false;
return mTab.getWindowAndroid().showIntent(intent, banner, R.string.low_memory_error);
}
private void resetState() {
if (mBannerView != null) {
mBannerView.destroy();
mBannerView = null;
}
mAppData = null;
}
private boolean isBannerForCurrentPage(String bannerUrl) {
return mContentView != null && TextUtils.equals(mContentView.getUrl(), bannerUrl);
}
private static native boolean nativeIsEnabled();
private native long nativeInit();
private native void nativeDestroy(long nativeAppBannerManager);
private native void nativeReplaceWebContents(long nativeAppBannerManager,
WebContents webContents);
private native void nativeBlockBanner(
long nativeAppBannerManager, String url, String packageName);
private native boolean nativeFetchIcon(long nativeAppBannerManager, String imageUrl);
private static native void nativeRecordDismissEvent(int metric);
private static native void nativeRecordInstallEvent(int metric);
}