Home | History | Annotate | Download | only in widget
      1 /*
      2  * Copyright (C) 2010 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 package com.android.browser.widget;
     18 
     19 import android.app.PendingIntent;
     20 import android.appwidget.AppWidgetManager;
     21 import android.appwidget.AppWidgetProvider;
     22 import android.content.ComponentName;
     23 import android.content.Context;
     24 import android.content.Intent;
     25 import android.net.Uri;
     26 import android.widget.RemoteViews;
     27 
     28 import com.android.browser.BrowserActivity;
     29 import com.android.browser.R;
     30 
     31 /**
     32  * Widget that shows a preview of the user's bookmarks.
     33  */
     34 public class BookmarkThumbnailWidgetProvider extends AppWidgetProvider {
     35     public static final String ACTION_BOOKMARK_APPWIDGET_UPDATE =
     36         "com.android.browser.BOOKMARK_APPWIDGET_UPDATE";
     37 
     38     @Override
     39     public void onReceive(Context context, Intent intent) {
     40         // Handle bookmark-specific updates ourselves because they might be
     41         // coming in without extras, which AppWidgetProvider then blocks.
     42         final String action = intent.getAction();
     43         if (ACTION_BOOKMARK_APPWIDGET_UPDATE.equals(action)) {
     44             AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);
     45             performUpdate(context, appWidgetManager,
     46                     appWidgetManager.getAppWidgetIds(getComponentName(context)));
     47         } else {
     48             super.onReceive(context, intent);
     49         }
     50     }
     51 
     52     @Override
     53     public void onUpdate(Context context, AppWidgetManager mngr, int[] ids) {
     54         performUpdate(context, mngr, ids);
     55     }
     56 
     57     @Override
     58     public void onDeleted(Context context, int[] appWidgetIds) {
     59         super.onDeleted(context, appWidgetIds);
     60         for (int widgetId : appWidgetIds) {
     61             BookmarkThumbnailWidgetService.deleteWidgetState(context, widgetId);
     62         }
     63         removeOrphanedFiles(context);
     64     }
     65 
     66     @Override
     67     public void onDisabled(Context context) {
     68         super.onDisabled(context);
     69         removeOrphanedFiles(context);
     70     }
     71 
     72     /**
     73      *  Checks for any state files that may have not received onDeleted
     74      */
     75     void removeOrphanedFiles(Context context) {
     76         AppWidgetManager wm = AppWidgetManager.getInstance(context);
     77         int[] ids = wm.getAppWidgetIds(getComponentName(context));
     78         BookmarkThumbnailWidgetService.removeOrphanedStates(context, ids);
     79     }
     80 
     81     private void performUpdate(Context context,
     82             AppWidgetManager appWidgetManager, int[] appWidgetIds) {
     83         PendingIntent launchBrowser = PendingIntent.getActivity(context, 0,
     84                 new Intent(BrowserActivity.ACTION_SHOW_BROWSER, null, context,
     85                     BrowserActivity.class),
     86                 PendingIntent.FLAG_UPDATE_CURRENT);
     87         for (int appWidgetId : appWidgetIds) {
     88             Intent updateIntent = new Intent(context, BookmarkThumbnailWidgetService.class);
     89             updateIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
     90             updateIntent.setData(Uri.parse(updateIntent.toUri(Intent.URI_INTENT_SCHEME)));
     91             RemoteViews views = new RemoteViews(context.getPackageName(),
     92                     R.layout.bookmarkthumbnailwidget);
     93             views.setOnClickPendingIntent(R.id.app_shortcut, launchBrowser);
     94             views.setRemoteAdapter(R.id.bookmarks_list, updateIntent);
     95             appWidgetManager.notifyAppWidgetViewDataChanged(appWidgetId, R.id.bookmarks_list);
     96             Intent ic = new Intent(context, BookmarkWidgetProxy.class);
     97             views.setPendingIntentTemplate(R.id.bookmarks_list,
     98                     PendingIntent.getBroadcast(context, 0, ic,
     99                     PendingIntent.FLAG_UPDATE_CURRENT));
    100             appWidgetManager.updateAppWidget(appWidgetId, views);
    101         }
    102     }
    103 
    104     /**
    105      * Build {@link ComponentName} describing this specific
    106      * {@link AppWidgetProvider}
    107      */
    108     static ComponentName getComponentName(Context context) {
    109         return new ComponentName(context, BookmarkThumbnailWidgetProvider.class);
    110     }
    111 
    112     public static void refreshWidgets(Context context) {
    113         context.sendBroadcast(new Intent(
    114                 BookmarkThumbnailWidgetProvider.ACTION_BOOKMARK_APPWIDGET_UPDATE,
    115                 null, context, BookmarkThumbnailWidgetProvider.class));
    116     }
    117 
    118 }
    119