Home | History | Annotate | Download | only in gadget
      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.gallery3d.gadget;
     18 
     19 import android.app.PendingIntent;
     20 import android.appwidget.AppWidgetManager;
     21 import android.appwidget.AppWidgetProvider;
     22 import android.content.Context;
     23 import android.content.Intent;
     24 import android.graphics.Bitmap;
     25 import android.graphics.BitmapFactory;
     26 import android.net.Uri;
     27 import android.util.Log;
     28 import android.widget.RemoteViews;
     29 
     30 import com.android.gallery3d.R;
     31 import com.android.gallery3d.gadget.WidgetDatabaseHelper.Entry;
     32 import com.android.gallery3d.onetimeinitializer.GalleryWidgetMigrator;
     33 
     34 public class PhotoAppWidgetProvider extends AppWidgetProvider {
     35 
     36     private static final String TAG = "WidgetProvider";
     37 
     38     static RemoteViews buildWidget(Context context, int id, Entry entry) {
     39 
     40         switch (entry.type) {
     41             case WidgetDatabaseHelper.TYPE_ALBUM:
     42             case WidgetDatabaseHelper.TYPE_SHUFFLE:
     43                 return buildStackWidget(context, id, entry);
     44             case WidgetDatabaseHelper.TYPE_SINGLE_PHOTO:
     45                 return buildFrameWidget(context, id, entry);
     46         }
     47         throw new RuntimeException("invalid type - " + entry.type);
     48     }
     49 
     50     @Override
     51     public void onUpdate(Context context,
     52             AppWidgetManager appWidgetManager, int[] appWidgetIds) {
     53         // migrate gallery widgets from pre-JB releases to JB due to bucket ID change
     54         GalleryWidgetMigrator.migrateGalleryWidgets(context);
     55 
     56         WidgetDatabaseHelper helper = new WidgetDatabaseHelper(context);
     57         try {
     58             for (int id : appWidgetIds) {
     59                 Entry entry = helper.getEntry(id);
     60                 if (entry != null) {
     61                     RemoteViews views = buildWidget(context, id, entry);
     62                     appWidgetManager.updateAppWidget(id, views);
     63                 } else {
     64                     Log.e(TAG, "cannot load widget: " + id);
     65                 }
     66             }
     67         } finally {
     68             helper.close();
     69         }
     70         super.onUpdate(context, appWidgetManager, appWidgetIds);
     71     }
     72 
     73     private static RemoteViews buildStackWidget(Context context, int widgetId, Entry entry) {
     74         RemoteViews views = new RemoteViews(
     75                 context.getPackageName(), R.layout.appwidget_main);
     76 
     77         Intent intent = new Intent(context, WidgetService.class);
     78         intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, widgetId);
     79         intent.putExtra(WidgetService.EXTRA_WIDGET_TYPE, entry.type);
     80         intent.putExtra(WidgetService.EXTRA_ALBUM_PATH, entry.albumPath);
     81         intent.setData(Uri.parse("widget://gallery/" + widgetId));
     82 
     83         views.setRemoteAdapter(R.id.appwidget_stack_view, intent);
     84         views.setEmptyView(R.id.appwidget_stack_view, R.id.appwidget_empty_view);
     85 
     86         Intent clickIntent = new Intent(context, WidgetClickHandler.class);
     87         PendingIntent pendingIntent = PendingIntent.getActivity(
     88                 context, 0, clickIntent, PendingIntent.FLAG_UPDATE_CURRENT);
     89         views.setPendingIntentTemplate(R.id.appwidget_stack_view, pendingIntent);
     90 
     91         return views;
     92     }
     93 
     94     static RemoteViews buildFrameWidget(Context context, int appWidgetId, Entry entry) {
     95         RemoteViews views = new RemoteViews(
     96                 context.getPackageName(), R.layout.photo_frame);
     97         try {
     98             byte[] data = entry.imageData;
     99             Bitmap bitmap = BitmapFactory.decodeByteArray(data, 0, data.length);
    100             views.setImageViewBitmap(R.id.photo, bitmap);
    101         } catch (Throwable t) {
    102             Log.w(TAG, "cannot load widget image: " + appWidgetId, t);
    103         }
    104 
    105         if (entry.imageUri != null) {
    106             try {
    107                 Uri uri = Uri.parse(entry.imageUri);
    108                 Intent clickIntent = new Intent(context, WidgetClickHandler.class)
    109                         .setData(uri);
    110                 PendingIntent pendingClickIntent = PendingIntent.getActivity(context, 0,
    111                         clickIntent, PendingIntent.FLAG_CANCEL_CURRENT);
    112                 views.setOnClickPendingIntent(R.id.photo, pendingClickIntent);
    113             } catch (Throwable t) {
    114                 Log.w(TAG, "cannot load widget uri: " + appWidgetId, t);
    115             }
    116         }
    117         return views;
    118     }
    119 
    120     @Override
    121     public void onDeleted(Context context, int[] appWidgetIds) {
    122         // Clean deleted photos out of our database
    123         WidgetDatabaseHelper helper = new WidgetDatabaseHelper(context);
    124         for (int appWidgetId : appWidgetIds) {
    125             helper.deleteEntry(appWidgetId);
    126         }
    127         helper.close();
    128     }
    129 }
    130