1 /* 2 * Copyright (C) 2009 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.cooliris.media; 18 19 import com.cooliris.app.App; 20 import com.cooliris.media.PhotoAppWidgetProvider.PhotoDatabaseHelper; 21 22 import android.app.Activity; 23 import android.appwidget.AppWidgetManager; 24 import android.content.Intent; 25 import android.graphics.Bitmap; 26 import android.os.Bundle; 27 import android.util.Log; 28 import android.widget.RemoteViews; 29 30 import java.util.ArrayList; 31 32 public class PhotoAppWidgetBind extends Activity { 33 private static final String TAG = "PhotoAppWidgetBind"; 34 private static final String EXTRA_APPWIDGET_BITMAPS = "com.android.camera.appwidgetbitmaps"; 35 36 private App mApp = null; 37 38 @Override onCreate(Bundle icicle)39 protected void onCreate(Bundle icicle) { 40 super.onCreate(icicle); 41 mApp = new App(PhotoAppWidgetBind.this); 42 finish(); 43 44 // The caller has requested that we bind a given bitmap to a specific 45 // appWidgetId, which probably is happening during a Launcher upgrade. 46 // This is dangerous because the caller could set bitmaps on 47 // appWidgetIds they don't own, so we guard this call at the manifest 48 // level by requiring the BIND_APPWIDGET permission. 49 50 final Intent intent = getIntent(); 51 final Bundle extras = intent.getExtras(); 52 53 final int[] appWidgetIds = extras.getIntArray(AppWidgetManager.EXTRA_APPWIDGET_IDS); 54 final ArrayList<Bitmap> bitmaps = extras.getParcelableArrayList(EXTRA_APPWIDGET_BITMAPS); 55 56 if (appWidgetIds == null || bitmaps == null || appWidgetIds.length != bitmaps.size()) { 57 Log.e(TAG, "Problem parsing photo widget bind request"); 58 return; 59 } 60 61 AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(this); 62 PhotoDatabaseHelper helper = new PhotoDatabaseHelper(this); 63 for (int i = 0; i < appWidgetIds.length; i++) { 64 // Store the cropped photo in our database 65 int appWidgetId = appWidgetIds[i]; 66 helper.setPhoto(appWidgetId, bitmaps.get(i)); 67 68 // Push newly updated widget to surface 69 RemoteViews views = PhotoAppWidgetProvider.buildUpdate(this, appWidgetId, helper); 70 appWidgetManager.updateAppWidget(new int[] { appWidgetId }, views); 71 } 72 helper.close(); 73 } 74 75 @Override onPause()76 public void onPause() { 77 super.onPause(); 78 mApp.onPause(); 79 } 80 81 @Override onResume()82 public void onResume() { 83 super.onResume(); 84 mApp.onResume(); 85 } 86 87 @Override onDestroy()88 public void onDestroy() { 89 mApp.shutdown(); 90 super.onDestroy(); 91 } 92 } 93