Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(43)

Side by Side Diff: chrome/android/java/src/org/chromium/chrome/browser/ShortcutHelper.java

Issue 563843002: Refactor ShortcutHelper (Java/C++) to allow asynchronous init. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@manifest_manager_content
Patch Set: review comments Created 6 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2013 The Chromium Authors. All rights reserved. 1 // Copyright 2013 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; 5 package org.chromium.chrome.browser;
6 6
7 import android.app.ActivityManager; 7 import android.app.ActivityManager;
8 import android.content.Context; 8 import android.content.Context;
9 import android.content.Intent; 9 import android.content.Intent;
10 import android.graphics.Bitmap; 10 import android.graphics.Bitmap;
(...skipping 20 matching lines...) Expand all
31 31
32 /** 32 /**
33 * Sets the class name used when launching the shortcuts. 33 * Sets the class name used when launching the shortcuts.
34 * @param fullScreenAction Class name of the fullscreen Activity. 34 * @param fullScreenAction Class name of the fullscreen Activity.
35 */ 35 */
36 public static void setFullScreenAction(String fullScreenAction) { 36 public static void setFullScreenAction(String fullScreenAction) {
37 sFullScreenAction = fullScreenAction; 37 sFullScreenAction = fullScreenAction;
38 } 38 }
39 39
40 /** 40 /**
41 * Callback to be passed to the initialized() method.
42 */
43 public interface OnInitialized {
44 public void onInitialized(String title);
45 }
46
47 private final Context mAppContext;
48 private final Tab mTab;
49
50 private OnInitialized mCallback;
51 private boolean mIsInitialized = false;
gone 2014/09/11 20:32:21 defaults to false
mlamouri (slow - plz ping) 2014/09/12 10:15:16 Interesting.
52 private long mNativeShortcutHelper = 0;
gone 2014/09/11 20:32:21 defaults to 0
mlamouri (slow - plz ping) 2014/09/12 10:15:16 Done.
53
54 public ShortcutHelper(Context appContext, Tab tab) {
55 mAppContext = appContext;
56 mTab = tab;
57 }
58
59 /**
60 * Gets all the information required to initialize the UI, the passed
61 * callback will be run when those information will be available.
62 * @param callback Callback to be run when initialized.
63 */
64 public void initialize(OnInitialized callback) {
65 mCallback = callback;
66 mNativeShortcutHelper = nativeInitialize(mTab.getNativePtr());
67 }
68
69 /**
70 * Returns whether the object is initialized.
71 */
72 public boolean isInitialized() {
73 return mIsInitialized;
74 }
75
76 /**
77 * Puts the object in a state where it is safe to be destroyed.
78 */
79 public void tearDown() {
80 nativeTearDown(mNativeShortcutHelper);
81
82 // Make sure the callback isn't run if the tear down happens before
83 // onInitialized() is called.
84 mCallback = null;
85 mNativeShortcutHelper = 0;
86 }
87
88 @CalledByNative
89 private void onInitialized(String title) {
90 mIsInitialized = true;
91
92 if (mCallback != null) {
93 mCallback.onInitialized(title);
94 }
95 }
96
97 /**
41 * Adds a shortcut for the current Tab. 98 * Adds a shortcut for the current Tab.
42 * @param appContext The application context.
43 * @param tab Tab to create a shortcut for.
44 * @param userRequestedTitle Updated title for the shortcut. 99 * @param userRequestedTitle Updated title for the shortcut.
45 */ 100 */
46 public static void addShortcut(Context appContext, Tab tab, String userReque stedTitle) { 101 public void addShortcut(String userRequestedTitle) {
47 if (TextUtils.isEmpty(sFullScreenAction)) { 102 if (TextUtils.isEmpty(sFullScreenAction)) {
48 Log.e("ShortcutHelper", "ShortcutHelper is uninitialized. Aborting. "); 103 Log.e("ShortcutHelper", "ShortcutHelper is uninitialized. Aborting. ");
49 return; 104 return;
50 } 105 }
51 ActivityManager am = (ActivityManager) appContext.getSystemService( 106 ActivityManager am = (ActivityManager) mAppContext.getSystemService(
52 Context.ACTIVITY_SERVICE); 107 Context.ACTIVITY_SERVICE);
53 nativeAddShortcut(tab.getNativePtr(), userRequestedTitle, am.getLauncher LargeIconSize()); 108 nativeAddShortcut(mNativeShortcutHelper, userRequestedTitle, am.getLaunc herLargeIconSize());
109
110 // The C++ instance is no longer owned by the Java object.
111 mCallback = null;
112 mNativeShortcutHelper = 0;
54 } 113 }
55 114
56 /** 115 /**
57 * Called when we have to fire an Intent to add a shortcut to the homescreen . 116 * Called when we have to fire an Intent to add a shortcut to the homescreen .
58 * If the webpage indicated that it was capable of functioning as a webapp, it is added as a 117 * If the webpage indicated that it was capable of functioning as a webapp, it is added as a
59 * shortcut to a webapp Activity rather than as a general bookmark. User is sent to the 118 * shortcut to a webapp Activity rather than as a general bookmark. User is sent to the
60 * homescreen as soon as the shortcut is created. 119 * homescreen as soon as the shortcut is created.
61 */ 120 */
62 @SuppressWarnings("unused") 121 @SuppressWarnings("unused")
63 @CalledByNative 122 @CalledByNative
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after
99 context.sendBroadcast(BookmarkUtils.createAddToHomeIntent(context, short cutIntent, title, 158 context.sendBroadcast(BookmarkUtils.createAddToHomeIntent(context, short cutIntent, title,
100 favicon, red, green, blue)); 159 favicon, red, green, blue));
101 160
102 // User is sent to the homescreen as soon as the shortcut is created. 161 // User is sent to the homescreen as soon as the shortcut is created.
103 Intent homeIntent = new Intent(Intent.ACTION_MAIN); 162 Intent homeIntent = new Intent(Intent.ACTION_MAIN);
104 homeIntent.addCategory(Intent.CATEGORY_HOME); 163 homeIntent.addCategory(Intent.CATEGORY_HOME);
105 homeIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 164 homeIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
106 context.startActivity(homeIntent); 165 context.startActivity(homeIntent);
107 } 166 }
108 167
109 private static native void nativeAddShortcut(long tabAndroidPtr, String user RequestedTitle, 168 private native long nativeInitialize(long tabAndroidPtr);
169 private native void nativeAddShortcut(long nativeShortcutHelper, String user RequestedTitle,
110 int launcherLargeIconSize); 170 int launcherLargeIconSize);
171 private native void nativeTearDown(long nativeShortcutHelper);
111 } 172 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698