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.enhancedbookmarks; |
| 6 |
| 7 import android.app.Activity; |
| 8 import android.content.Context; |
| 9 import android.content.Intent; |
| 10 import android.graphics.Bitmap; |
| 11 import android.net.Uri; |
| 12 import android.os.Bundle; |
| 13 import android.provider.Browser; |
| 14 import android.util.Pair; |
| 15 |
| 16 import com.google.android.apps.chrome.R; |
| 17 |
| 18 import org.chromium.base.ApiCompatibilityUtils; |
| 19 import org.chromium.chrome.browser.BookmarksBridge; |
| 20 import org.chromium.chrome.browser.BookmarksBridge.BookmarkItem; |
| 21 import org.chromium.chrome.browser.IntentHandler; |
| 22 import org.chromium.chrome.browser.Tab; |
| 23 import org.chromium.chrome.browser.UrlConstants; |
| 24 import org.chromium.chrome.browser.document.ChromeLauncherActivity; |
| 25 import org.chromium.chrome.browser.enhanced_bookmarks.EnhancedBookmarksModel; |
| 26 import org.chromium.chrome.browser.favicon.FaviconHelper; |
| 27 import org.chromium.chrome.browser.profiles.Profile; |
| 28 import org.chromium.chrome.browser.snackbar.SnackbarManager; |
| 29 import org.chromium.chrome.browser.snackbar.SnackbarManager.SnackbarController; |
| 30 import org.chromium.chrome.browser.util.FeatureUtilities; |
| 31 import org.chromium.chrome.browser.util.MathUtils; |
| 32 import org.chromium.components.bookmarks.BookmarkId; |
| 33 import org.chromium.ui.base.DeviceFormFactor; |
| 34 |
| 35 /** |
| 36 * A class holding static util functions for enhanced bookmark. |
| 37 */ |
| 38 public class EnhancedBookmarkUtils { |
| 39 |
| 40 private static final String BOOKMARK_SAVE_NAME = "SaveBookmark"; |
| 41 private static final int[] DEFAULT_BACKGROUND_COLORS = { |
| 42 0xFFE64A19, |
| 43 0xFFF09300, |
| 44 0xFFAFB42B, |
| 45 0xFF689F38, |
| 46 0xFF0B8043, |
| 47 0xFF0097A7, |
| 48 0xFF7B1FA2, |
| 49 0xFFC2185B |
| 50 }; |
| 51 |
| 52 /** |
| 53 * @return True if enhanced bookmark feature is enabled for given profile. F
alse otherwise. |
| 54 */ |
| 55 public static boolean isEnhancedBookmarkEnabled(Profile profile) { |
| 56 return BookmarksBridge.isEnhancedBookmarksEnabled(profile); |
| 57 } |
| 58 |
| 59 /** |
| 60 * Static method used for activities to show snackbar that notifies user tha
t the bookmark has |
| 61 * been added successfully. Note this method also starts fetching salient im
age in background. |
| 62 */ |
| 63 public static void addBookmarkAndShowSnackbar(EnhancedBookmarksModel bookmar
kModel, Tab tab, |
| 64 final SnackbarManager snackbarManager, final Activity activity) { |
| 65 // TODO(ianwen): remove activity from argument list. |
| 66 final BookmarkId enhancedId = bookmarkModel.addBookmark(bookmarkModel.ge
tDefaultFolder(), |
| 67 0, tab.getTitle(), tab.getUrl()); |
| 68 bookmarkModel.fetchImageForTab(tab.getWebContents()); |
| 69 |
| 70 Pair<EnhancedBookmarksModel, BookmarkId> pair = Pair.create(bookmarkMode
l, enhancedId); |
| 71 |
| 72 SnackbarController snackbarController = new SnackbarController() { |
| 73 @Override |
| 74 public void onDismissForEachType(boolean isTimeout) {} |
| 75 |
| 76 @Override |
| 77 public void onDismissNoAction(Object actionData) { |
| 78 // This method will be called only if the snackbar is dismissed
by timeout. |
| 79 @SuppressWarnings("unchecked") |
| 80 Pair<EnhancedBookmarksModel, BookmarkId> pair = (Pair< |
| 81 EnhancedBookmarksModel, BookmarkId>) actionData; |
| 82 pair.first.destroy(); |
| 83 } |
| 84 |
| 85 @Override |
| 86 public void onAction(Object actionData) { |
| 87 @SuppressWarnings("unchecked") |
| 88 Pair<EnhancedBookmarksModel, BookmarkId> pair = (Pair< |
| 89 EnhancedBookmarksModel, BookmarkId>) actionData; |
| 90 // Show detail page with the name of parent folder highlighted. |
| 91 startDetailActivity(activity, enhancedId); |
| 92 pair.first.destroy(); |
| 93 } |
| 94 }; |
| 95 snackbarManager.showSnackbar(null, |
| 96 activity.getString(R.string.enhanced_bookmark_page_saved), |
| 97 activity.getString(R.string.enhanced_bookmark_item_edit), pair, |
| 98 snackbarController); |
| 99 } |
| 100 |
| 101 /** |
| 102 * Shows enhanced bookmark main UI, if it is turned on. Does nothing if it i
s turned off. |
| 103 * @return True if enhanced bookmark is on, false otherwise. |
| 104 */ |
| 105 public static boolean showEnhancedBookmarkIfEnabled(Activity activity) { |
| 106 if (!isEnhancedBookmarkEnabled(Profile.getLastUsedProfile().getOriginalP
rofile())) { |
| 107 return false; |
| 108 } |
| 109 if (DeviceFormFactor.isTablet(activity)) { |
| 110 openBookmark(activity, UrlConstants.BOOKMARKS_URL); |
| 111 } else { |
| 112 activity.startActivity(new Intent(activity, EnhancedBookmarkActivity
.class)); |
| 113 } |
| 114 return true; |
| 115 } |
| 116 |
| 117 public static void startDetailActivity(Context context, BookmarkId bookmarkI
d) { |
| 118 Intent intent = new Intent(context, EnhancedBookmarkDetailActivity.class
); |
| 119 intent.putExtra(EnhancedBookmarkDetailActivity.INTENT_BOOKMARK_ID, bookm
arkId.toString()); |
| 120 context.startActivity(intent); |
| 121 } |
| 122 |
| 123 /** |
| 124 * Generate color based on bookmarked url's hash code. Same color will |
| 125 * always be returned given same bookmark item. |
| 126 * |
| 127 * @param item bookmark the color represents for |
| 128 * @return int for the generated color |
| 129 */ |
| 130 public static int generateBackgroundColor(BookmarkItem item) { |
| 131 int normalizedIndex = MathUtils.positiveModulo(item.getUrl().hashCode(), |
| 132 DEFAULT_BACKGROUND_COLORS.length); |
| 133 return DEFAULT_BACKGROUND_COLORS[normalizedIndex]; |
| 134 } |
| 135 |
| 136 /** |
| 137 * Save the bookmark in bundle to save state of a fragment/activity. |
| 138 * @param bundle Argument holder or savedInstanceState of the fragment/activ
ity. |
| 139 * @param bookmark The bookmark to save. |
| 140 */ |
| 141 public static void saveBookmarkIdToBundle(Bundle bundle, BookmarkId bookmark
) { |
| 142 bundle.putString(BOOKMARK_SAVE_NAME, bookmark.toString()); |
| 143 } |
| 144 |
| 145 /** |
| 146 * Retrieve the bookmark previously saved in the arguments bundle. |
| 147 * @param bundle Argument holder or savedInstanceState of the fragment/activ
ity. |
| 148 * @return The ID of the bookmark to retrieve. |
| 149 */ |
| 150 public static BookmarkId getBookmarkIdFromBundle(Bundle bundle) { |
| 151 return BookmarkId.getBookmarkIdFromString(bundle.getString(BOOKMARK_SAVE
_NAME)); |
| 152 } |
| 153 |
| 154 public static void openBookmark(Activity activity, String url) { |
| 155 Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url)); |
| 156 intent.setClassName(activity.getApplicationContext().getPackageName(), |
| 157 ChromeLauncherActivity.class.getName()); |
| 158 intent.putExtra(Browser.EXTRA_APPLICATION_ID, |
| 159 activity.getApplicationContext().getPackageName()); |
| 160 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); |
| 161 IntentHandler.startActivityForTrustedIntent(intent, activity); |
| 162 } |
| 163 |
| 164 /** |
| 165 * Get dominant color from bitmap. This function uses favicon helper to fulf
il its task. |
| 166 * @param bitmap The bitmap to extract color from. |
| 167 * @return The dominant color in ARGB format. |
| 168 */ |
| 169 public static int getDominantColorForBitmap(Bitmap bitmap) { |
| 170 int mDominantColor = FaviconHelper.getDominantColorForBitmap(bitmap); |
| 171 // FaviconHelper returns color in ABGR format, do a manual conversion he
re. |
| 172 int red = (mDominantColor & 0xff) << 16; |
| 173 int green = mDominantColor & 0xff00; |
| 174 int blue = (mDominantColor & 0xff0000) >> 16; |
| 175 int alpha = mDominantColor & 0xff000000; |
| 176 return alpha + red + green + blue; |
| 177 } |
| 178 |
| 179 /** |
| 180 * Updates the title of chrome shown in recent tasks. It only takes effect i
n document mode. |
| 181 */ |
| 182 public static void setTaskDescriptionInDocumentMode(Activity activity, Strin
g description) { |
| 183 if (FeatureUtilities.isDocumentMode(activity)) { |
| 184 // Setting icon to be null and color to be 0 will means "take no eff
ect". |
| 185 ApiCompatibilityUtils.setTaskDescription(activity, description, null
, 0); |
| 186 } |
| 187 } |
| 188 } |
OLD | NEW |