OLD | NEW |
---|---|
(Empty) | |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 package org.chromium.chrome.browser.webapps; | |
6 | |
7 import android.content.Context; | |
8 import android.graphics.Bitmap; | |
9 | |
10 import org.chromium.base.ApplicationStatus; | |
11 import org.chromium.base.annotations.CalledByNative; | |
12 import org.chromium.chrome.browser.BookmarkUtils; | |
13 import org.chromium.chrome.browser.tab.Tab; | |
14 import org.chromium.content_public.browser.WebContents; | |
15 | |
16 /** | |
17 * 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
| |
18 */ | |
19 public class AddToHomescreenHelper { | |
20 | |
21 /** Observes the data fetching pipeline. */ | |
22 public interface AddToHomescreenHelperObserver { | |
23 /** Called when the title of the page is available. */ | |
24 void onUserTitleAvailable(String title); | |
25 | |
26 /** Called when the icon to use in the launcher is available. */ | |
27 void onIconAvailable(Bitmap icon); | |
28 } | |
29 | |
30 private final Context mAppContext; | |
31 private final Tab mTab; | |
32 | |
33 private AddToHomescreenHelperObserver mObserver; | |
34 private boolean mIsInitialized; | |
35 private long mNativeAddToHomescreenHelper; | |
36 | |
37 public AddToHomescreenHelper(Context appContext, Tab tab) { | |
38 mAppContext = appContext; | |
39 mTab = tab; | |
40 } | |
41 | |
42 /** | |
43 * Gets all the information required to initialize the UI. The observer wil l be notified as | |
44 * information required for the shortcut become available. | |
45 * @param observer Observer to notify. | |
46 */ | |
47 public void initialize(AddToHomescreenHelperObserver observer) { | |
48 mObserver = observer; | |
49 mNativeAddToHomescreenHelper = nativeInitialize(mTab.getWebContents()); | |
50 } | |
51 | |
52 /** | |
53 * Returns whether the object is initialized. | |
54 */ | |
55 public boolean isInitialized() { | |
56 return mIsInitialized; | |
57 } | |
58 | |
59 /** | |
60 * Puts the object in a state where it is safe to be destroyed. | |
61 */ | |
62 public void destroy() { | |
63 nativeDestroy(mNativeAddToHomescreenHelper); | |
64 | |
65 // Make sure the callback isn't run if the tear down happens before | |
66 // onInitialized() is called. | |
67 mObserver = null; | |
68 mNativeAddToHomescreenHelper = 0; | |
69 } | |
70 | |
71 @CalledByNative | |
72 private void onUserTitleAvailable(String title) { | |
73 mObserver.onUserTitleAvailable(title); | |
74 } | |
75 | |
76 @CalledByNative | |
77 private void onIconAvailable(Bitmap icon) { | |
78 mObserver.onIconAvailable(icon); | |
79 mIsInitialized = true; | |
80 } | |
81 | |
82 /** | |
83 * Adds a shortcut for the current Tab. | |
84 * @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
| |
85 */ | |
86 public void addShortcut(String userRequestedTitle) { | |
87 nativeAddShortcut(mNativeAddToHomescreenHelper, userRequestedTitle); | |
88 } | |
89 | |
90 /** | |
91 * Creates an icon that is acceptable to show on the launcher. | |
92 */ | |
93 @CalledByNative | |
94 private static Bitmap finalizeLauncherIcon( | |
95 String url, Bitmap icon, int red, int green, int blue) { | |
96 return BookmarkUtils.createLauncherIcon( | |
97 ApplicationStatus.getApplicationContext(), icon, url, red, green , blue); | |
98 } | |
99 | |
100 private native long nativeInitialize(WebContents webContents); | |
101 private native void nativeAddShortcut(long nativeAddToHomescreenHelper, | |
102 String userRequestedTitle); | |
103 private native void nativeDestroy(long nativeAddToHomescreenHelper); | |
104 } | |
OLD | NEW |