blob: 2b36f6bfd3070b2eb0e2225cd3f1c88555c6a9b3 [file] [log] [blame]
Owen Linf9a0a432011-08-17 22:07:43 +08001/*
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
Owen Lind6db8ea2011-08-18 21:48:19 +080017package com.android.gallery3d.gadget;
Owen Linf9a0a432011-08-17 22:07:43 +080018
Owen Linf9a0a432011-08-17 22:07:43 +080019import android.app.PendingIntent;
20import android.appwidget.AppWidgetManager;
21import android.appwidget.AppWidgetProvider;
22import android.content.Context;
23import android.content.Intent;
Owen Lind6db8ea2011-08-18 21:48:19 +080024import android.graphics.Bitmap;
25import android.graphics.BitmapFactory;
Owen Linf9a0a432011-08-17 22:07:43 +080026import android.net.Uri;
27import android.util.Log;
28import android.widget.RemoteViews;
29
Owen Lin2b3ee0e2012-03-14 17:27:24 +080030import com.android.gallery3d.R;
31import com.android.gallery3d.gadget.WidgetDatabaseHelper.Entry;
Hung-ying Tyan40234dd2012-06-26 21:15:30 +080032import com.android.gallery3d.onetimeinitializer.GalleryWidgetMigrator;
Owen Lin2b3ee0e2012-03-14 17:27:24 +080033
Owen Lind6db8ea2011-08-18 21:48:19 +080034public class PhotoAppWidgetProvider extends AppWidgetProvider {
Owen Linf9a0a432011-08-17 22:07:43 +080035
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) {
Hung-ying Tyan40234dd2012-06-26 21:15:30 +080053 // migrate gallery widgets from pre-JB releases to JB due to bucket ID change
54 GalleryWidgetMigrator.migrateGalleryWidgets(context);
55
Owen Linf9a0a432011-08-17 22:07:43 +080056 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);
Owen Lind6db8ea2011-08-18 21:48:19 +080097 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 }
Owen Linf9a0a432011-08-17 22:07:43 +0800117 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 }
Hung-ying Tyan40234dd2012-06-26 21:15:30 +0800129}