blob: c18652d5b40cea76436ee701526f8949f30360c0 [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;
32
Owen Lind6db8ea2011-08-18 21:48:19 +080033public class PhotoAppWidgetProvider extends AppWidgetProvider {
Owen Linf9a0a432011-08-17 22:07:43 +080034
35 private static final String TAG = "WidgetProvider";
36
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
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
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
90 static RemoteViews buildFrameWidget(Context context, int appWidgetId, Entry entry) {
91 RemoteViews views = new RemoteViews(
92 context.getPackageName(), R.layout.photo_frame);
Owen Lind6db8ea2011-08-18 21:48:19 +080093 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 }
Owen Linf9a0a432011-08-17 22:07:43 +0800113 return views;
114 }
115
116 @Override
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}