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