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.bookmarkswidget; |
| 6 |
| 7 import android.app.PendingIntent; |
| 8 import android.appwidget.AppWidgetManager; |
| 9 import android.appwidget.AppWidgetProvider; |
| 10 import android.content.ComponentName; |
| 11 import android.content.Context; |
| 12 import android.content.Intent; |
| 13 import android.net.Uri; |
| 14 import android.widget.RemoteViews; |
| 15 |
| 16 import com.google.android.apps.chrome.R; |
| 17 import com.google.android.apps.chrome.appwidget.bookmarks.BookmarkThumbnailWidge
tProvider; |
| 18 |
| 19 import org.chromium.chrome.browser.util.IntentUtils; |
| 20 |
| 21 /** |
| 22 * Widget that shows a preview of the user's bookmarks. |
| 23 */ |
| 24 public class BookmarkThumbnailWidgetProviderBase extends AppWidgetProvider { |
| 25 private static final String ACTION_BOOKMARK_APPWIDGET_UPDATE_SUFFIX = |
| 26 ".BOOKMARK_APPWIDGET_UPDATE"; |
| 27 |
| 28 @Override |
| 29 public void onReceive(Context context, Intent intent) { |
| 30 // Handle bookmark-specific updates ourselves because they might be |
| 31 // coming in without extras, which AppWidgetProvider then blocks. |
| 32 final String action = intent.getAction(); |
| 33 if (getBookmarkAppWidgetUpdateAction(context).equals(action)) { |
| 34 AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(con
text); |
| 35 if (intent.hasExtra(AppWidgetManager.EXTRA_APPWIDGET_ID)) { |
| 36 performUpdate(context, appWidgetManager, |
| 37 new int[] {IntentUtils.safeGetIntExtra(intent, |
| 38 AppWidgetManager.EXTRA_APPWIDGET_ID, -1)}); |
| 39 } else { |
| 40 performUpdate(context, appWidgetManager, |
| 41 appWidgetManager.getAppWidgetIds(getComponentName(contex
t))); |
| 42 } |
| 43 } else { |
| 44 super.onReceive(context, intent); |
| 45 } |
| 46 } |
| 47 |
| 48 @Override |
| 49 public void onUpdate(Context context, AppWidgetManager manager, int[] ids) { |
| 50 super.onUpdate(context, manager, ids); |
| 51 performUpdate(context, manager, ids); |
| 52 } |
| 53 |
| 54 @Override |
| 55 public void onDeleted(Context context, int[] appWidgetIds) { |
| 56 super.onDeleted(context, appWidgetIds); |
| 57 for (int widgetId : appWidgetIds) { |
| 58 BookmarkThumbnailWidgetService.deleteWidgetState(context, widgetId); |
| 59 } |
| 60 removeOrphanedStates(context); |
| 61 } |
| 62 |
| 63 @Override |
| 64 public void onDisabled(Context context) { |
| 65 super.onDisabled(context); |
| 66 removeOrphanedStates(context); |
| 67 } |
| 68 |
| 69 /** |
| 70 * Refreshes all Chrome Bookmark widgets. |
| 71 */ |
| 72 public static void refreshAllWidgets(Context context) { |
| 73 context.sendBroadcast(new Intent( |
| 74 getBookmarkAppWidgetUpdateAction(context), |
| 75 null, context, BookmarkThumbnailWidgetProvider.class)); |
| 76 } |
| 77 |
| 78 static String getBookmarkAppWidgetUpdateAction(Context context) { |
| 79 return context.getPackageName() + ACTION_BOOKMARK_APPWIDGET_UPDATE_SUFFI
X; |
| 80 } |
| 81 |
| 82 /** |
| 83 * Checks for any states that may have not received onDeleted. |
| 84 */ |
| 85 private void removeOrphanedStates(Context context) { |
| 86 AppWidgetManager wm = AppWidgetManager.getInstance(context); |
| 87 int[] ids = wm.getAppWidgetIds(getComponentName(context)); |
| 88 for (int id : ids) { |
| 89 BookmarkThumbnailWidgetService.deleteWidgetState(context, id); |
| 90 } |
| 91 } |
| 92 |
| 93 private void performUpdate(Context context, |
| 94 AppWidgetManager appWidgetManager, int[] appWidgetIds) { |
| 95 Intent chromeIntent = new Intent(Intent.ACTION_MAIN); |
| 96 chromeIntent.setPackage(context.getPackageName()); |
| 97 |
| 98 PendingIntent launchChrome = PendingIntent.getActivity(context, 0, chrom
eIntent, |
| 99 PendingIntent.FLAG_UPDATE_CURRENT); |
| 100 |
| 101 for (int appWidgetId : appWidgetIds) { |
| 102 Intent updateIntent = new Intent(context, BookmarkThumbnailWidgetSer
vice.class); |
| 103 updateIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidget
Id); |
| 104 updateIntent.setData(Uri.parse(updateIntent.toUri(Intent.URI_INTENT_
SCHEME))); |
| 105 |
| 106 RemoteViews views = new RemoteViews(context.getPackageName(), |
| 107 R.layout.bookmark_thumbnail_widget); |
| 108 views.setOnClickPendingIntent(R.id.app_shortcut, launchChrome); |
| 109 views.setRemoteAdapter(R.id.bookmarks_list, updateIntent); |
| 110 |
| 111 appWidgetManager.notifyAppWidgetViewDataChanged(appWidgetId, R.id.bo
okmarks_list); |
| 112 Intent ic = new Intent(context, BookmarkWidgetProxy.class); |
| 113 views.setPendingIntentTemplate(R.id.bookmarks_list, |
| 114 PendingIntent.getBroadcast(context, 0, ic, |
| 115 PendingIntent.FLAG_UPDATE_CURRENT)); |
| 116 appWidgetManager.updateAppWidget(appWidgetId, views); |
| 117 } |
| 118 } |
| 119 |
| 120 /** |
| 121 * Build {@link ComponentName} describing this specific |
| 122 * {@link AppWidgetProvider} |
| 123 */ |
| 124 private static ComponentName getComponentName(Context context) { |
| 125 return new ComponentName(context, BookmarkThumbnailWidgetProvider.class)
; |
| 126 } |
| 127 } |
OLD | NEW |