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