Chromium Code Reviews| Index: chrome/android/java/src/org/chromium/chrome/browser/webapps/AddToHomescreenHelper.java |
| diff --git a/chrome/android/java/src/org/chromium/chrome/browser/webapps/AddToHomescreenHelper.java b/chrome/android/java/src/org/chromium/chrome/browser/webapps/AddToHomescreenHelper.java |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..5baa607238a488a7ab6f6a7cc93db4fae27d24d8 |
| --- /dev/null |
| +++ b/chrome/android/java/src/org/chromium/chrome/browser/webapps/AddToHomescreenHelper.java |
| @@ -0,0 +1,104 @@ |
| +// Copyright 2015 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +package org.chromium.chrome.browser.webapps; |
| + |
| +import android.content.Context; |
| +import android.graphics.Bitmap; |
| + |
| +import org.chromium.base.ApplicationStatus; |
| +import org.chromium.base.annotations.CalledByNative; |
| +import org.chromium.chrome.browser.BookmarkUtils; |
| +import org.chromium.chrome.browser.tab.Tab; |
| +import org.chromium.content_public.browser.WebContents; |
| + |
| +/** |
| + * This is a helper class to create shortcuts on the Android home screen. |
|
gone
2015/08/13 21:43:15
Change the comment to better describe what this th
Lalit Maganti
2015/08/26 13:57:07
Done in https://codereview.chromium.org/1321463002
|
| + */ |
| +public class AddToHomescreenHelper { |
| + |
| + /** Observes the data fetching pipeline. */ |
| + public interface AddToHomescreenHelperObserver { |
| + /** Called when the title of the page is available. */ |
| + void onUserTitleAvailable(String title); |
| + |
| + /** Called when the icon to use in the launcher is available. */ |
| + void onIconAvailable(Bitmap icon); |
| + } |
| + |
| + private final Context mAppContext; |
| + private final Tab mTab; |
| + |
| + private AddToHomescreenHelperObserver mObserver; |
| + private boolean mIsInitialized; |
| + private long mNativeAddToHomescreenHelper; |
| + |
| + public AddToHomescreenHelper(Context appContext, Tab tab) { |
| + mAppContext = appContext; |
| + mTab = tab; |
| + } |
| + |
| + /** |
| + * Gets all the information required to initialize the UI. The observer will be notified as |
| + * information required for the shortcut become available. |
| + * @param observer Observer to notify. |
| + */ |
| + public void initialize(AddToHomescreenHelperObserver observer) { |
| + mObserver = observer; |
| + mNativeAddToHomescreenHelper = nativeInitialize(mTab.getWebContents()); |
| + } |
| + |
| + /** |
| + * Returns whether the object is initialized. |
| + */ |
| + public boolean isInitialized() { |
| + return mIsInitialized; |
| + } |
| + |
| + /** |
| + * Puts the object in a state where it is safe to be destroyed. |
| + */ |
| + public void destroy() { |
| + nativeDestroy(mNativeAddToHomescreenHelper); |
| + |
| + // Make sure the callback isn't run if the tear down happens before |
| + // onInitialized() is called. |
| + mObserver = null; |
| + mNativeAddToHomescreenHelper = 0; |
| + } |
| + |
| + @CalledByNative |
| + private void onUserTitleAvailable(String title) { |
| + mObserver.onUserTitleAvailable(title); |
| + } |
| + |
| + @CalledByNative |
| + private void onIconAvailable(Bitmap icon) { |
| + mObserver.onIconAvailable(icon); |
| + mIsInitialized = true; |
| + } |
| + |
| + /** |
| + * Adds a shortcut for the current Tab. |
| + * @param userRequestedTitle Updated title for the shortcut. |
|
gone
2015/08/13 21:43:15
nit: Not sure what an "Updated title for the short
Lalit Maganti
2015/08/26 13:57:07
Done in https://codereview.chromium.org/1321463002
|
| + */ |
| + public void addShortcut(String userRequestedTitle) { |
| + nativeAddShortcut(mNativeAddToHomescreenHelper, userRequestedTitle); |
| + } |
| + |
| + /** |
| + * Creates an icon that is acceptable to show on the launcher. |
| + */ |
| + @CalledByNative |
| + private static Bitmap finalizeLauncherIcon( |
| + String url, Bitmap icon, int red, int green, int blue) { |
| + return BookmarkUtils.createLauncherIcon( |
| + ApplicationStatus.getApplicationContext(), icon, url, red, green, blue); |
| + } |
| + |
| + private native long nativeInitialize(WebContents webContents); |
| + private native void nativeAddShortcut(long nativeAddToHomescreenHelper, |
| + String userRequestedTitle); |
| + private native void nativeDestroy(long nativeAddToHomescreenHelper); |
| +} |