Index: chrome/android/java/src/org/chromium/chrome/browser/ShortcutHelper.java |
diff --git a/chrome/android/java/src/org/chromium/chrome/browser/ShortcutHelper.java b/chrome/android/java/src/org/chromium/chrome/browser/ShortcutHelper.java |
index 9762af96c1a8ccab4b72e70771067bab3da9eda8..e2778838229bfc5ec9d9e82a6c762e93561f3bb1 100644 |
--- a/chrome/android/java/src/org/chromium/chrome/browser/ShortcutHelper.java |
+++ b/chrome/android/java/src/org/chromium/chrome/browser/ShortcutHelper.java |
@@ -38,19 +38,79 @@ public class ShortcutHelper { |
} |
/** |
+ * Callback to be passed to the initialized() method. |
+ */ |
+ public interface OnInitialized { |
+ public void onInitialized(String title); |
+ } |
+ |
+ private OnInitialized mCallback = null; |
gone
2014/09/11 18:24:36
these are all initialized by default to the values
mlamouri (slow - plz ping)
2014/09/11 19:31:02
I actually prefer explicit init but I've updated t
gone
2014/09/11 20:32:21
Eh, AFAIK it's been the standard to not define the
|
+ private Context mAppContext = null; |
+ private Tab mTab = null; |
gone
2014/09/11 18:24:36
private final Context mAppContext;
private final T
mlamouri (slow - plz ping)
2014/09/11 19:31:02
Done.
|
+ private boolean mInitialized = false; |
gone
2014/09/11 18:24:36
nit: private boolean mIsInitialized;
mlamouri (slow - plz ping)
2014/09/11 19:31:02
Done.
|
+ private long mNativeShortcutHelper = 0; |
+ |
+ public ShortcutHelper(Context appContext, Tab tab) { |
+ mAppContext = appContext; |
+ mTab = tab; |
+ } |
+ |
+ /** |
+ * Gets all the information required to initialize the UI, the passed |
+ * callback will be run when those information will be available. |
+ * @param callback Callback to be run when initialized. |
+ */ |
+ public void initialize(OnInitialized callback) { |
+ mCallback = callback; |
+ mNativeShortcutHelper = nativeInitialize(mTab.getNativePtr()); |
+ } |
+ |
+ /** |
+ * Returns whether the object is initialized. |
+ */ |
+ public boolean initialized() { |
gone
2014/09/11 18:24:36
isInitialized()
mlamouri (slow - plz ping)
2014/09/11 19:31:02
Done.
|
+ return mInitialized; |
+ } |
+ |
+ /** |
+ * Puts the object in a state where it is safe to be destroyed. |
+ */ |
+ public void tearDown() { |
+ nativeTearDown(mNativeShortcutHelper); |
+ |
+ // Make sure the callback isn't run if the tear down happens before |
+ // onInitialized() is called. |
+ mCallback = null; |
+ mNativeShortcutHelper = 0; |
+ } |
+ |
+ @CalledByNative |
+ private void onInitialized(String title) { |
+ mInitialized = true; |
+ |
+ if (mCallback == null) { |
gone
2014/09/11 18:24:36
just make this:
if (mCallback != null) mCallback.
mlamouri (slow - plz ping)
2014/09/11 19:31:02
Done.
|
+ return; |
+ } |
+ |
+ mCallback.onInitialized(title); |
+ } |
+ |
+ /** |
* Adds a shortcut for the current Tab. |
- * @param appContext The application context. |
- * @param tab Tab to create a shortcut for. |
* @param userRequestedTitle Updated title for the shortcut. |
*/ |
- public static void addShortcut(Context appContext, Tab tab, String userRequestedTitle) { |
+ public void addShortcut(String userRequestedTitle) { |
if (TextUtils.isEmpty(sFullScreenAction)) { |
Log.e("ShortcutHelper", "ShortcutHelper is uninitialized. Aborting."); |
return; |
} |
- ActivityManager am = (ActivityManager) appContext.getSystemService( |
+ ActivityManager am = (ActivityManager) mAppContext.getSystemService( |
Context.ACTIVITY_SERVICE); |
- nativeAddShortcut(tab.getNativePtr(), userRequestedTitle, am.getLauncherLargeIconSize()); |
+ nativeAddShortcut(mNativeShortcutHelper, userRequestedTitle, am.getLauncherLargeIconSize()); |
+ |
+ // The C++ instance is no longer owned by the Java object. |
+ mCallback = null; |
+ mNativeShortcutHelper = 0; |
} |
/** |
@@ -106,6 +166,8 @@ public class ShortcutHelper { |
context.startActivity(homeIntent); |
} |
- private static native void nativeAddShortcut(long tabAndroidPtr, String userRequestedTitle, |
+ private native long nativeInitialize(long tabAndroidPtr); |
+ private native void nativeAddShortcut(long nativeShortcutHelper, String userRequestedTitle, |
int launcherLargeIconSize); |
+ private native void nativeTearDown(long nativeShortcutHelper); |
} |