Owen Lin | f9a0a43 | 2011-08-17 22:07:43 +0800 | [diff] [blame] | 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 | |
Owen Lin | d6db8ea | 2011-08-18 21:48:19 +0800 | [diff] [blame] | 17 | package com.android.gallery3d.gadget; |
Owen Lin | f9a0a43 | 2011-08-17 22:07:43 +0800 | [diff] [blame] | 18 | |
Owen Lin | f9a0a43 | 2011-08-17 22:07:43 +0800 | [diff] [blame] | 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; |
Owen Lin | d6db8ea | 2011-08-18 21:48:19 +0800 | [diff] [blame] | 24 | import android.graphics.Bitmap; |
| 25 | import android.graphics.BitmapFactory; |
Owen Lin | f9a0a43 | 2011-08-17 22:07:43 +0800 | [diff] [blame] | 26 | import android.net.Uri; |
| 27 | import android.util.Log; |
| 28 | import android.widget.RemoteViews; |
| 29 | |
Owen Lin | 2b3ee0e | 2012-03-14 17:27:24 +0800 | [diff] [blame^] | 30 | import com.android.gallery3d.R; |
| 31 | import com.android.gallery3d.gadget.WidgetDatabaseHelper.Entry; |
| 32 | |
Owen Lin | d6db8ea | 2011-08-18 21:48:19 +0800 | [diff] [blame] | 33 | public class PhotoAppWidgetProvider extends AppWidgetProvider { |
Owen Lin | f9a0a43 | 2011-08-17 22:07:43 +0800 | [diff] [blame] | 34 | |
| 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 Lin | d6db8ea | 2011-08-18 21:48:19 +0800 | [diff] [blame] | 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 | } |
Owen Lin | f9a0a43 | 2011-08-17 22:07:43 +0800 | [diff] [blame] | 113 | 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 | } |