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
17package com.android.camera;
18
19import com.android.camera.PhotoAppWidgetProvider.PhotoDatabaseHelper;
20
21import android.app.Activity;
22import android.appwidget.AppWidgetManager;
23import android.content.Intent;
24import android.graphics.Bitmap;
25import android.os.Bundle;
26import android.util.Log;
27import android.widget.RemoteViews;
28
29import java.util.ArrayList;
30
31class PhotoAppWidgetBind extends NoSearchActivity {
32    private static final String TAG = "PhotoAppWidgetBind";
33    private static final String EXTRA_APPWIDGET_BITMAPS =
34            "com.android.camera.appwidgetbitmaps";
35
36    @Override
37    protected void onCreate(Bundle icicle) {
38        super.onCreate(icicle);
39        finish();
40
41        // The caller has requested that we bind a given bitmap to a specific
42        // appWidgetId, which probably is happening during a Launcher upgrade.
43        // This is dangerous because the caller could set bitmaps on
44        // appWidgetIds they don't own, so we guard this call at the manifest
45        // level by requiring the BIND_APPWIDGET permission.
46
47        final Intent intent = getIntent();
48        final Bundle extras = intent.getExtras();
49
50        final int[] appWidgetIds =
51                extras.getIntArray(AppWidgetManager.EXTRA_APPWIDGET_IDS);
52        final ArrayList<Bitmap> bitmaps =
53                extras.getParcelableArrayList(EXTRA_APPWIDGET_BITMAPS);
54
55        if (appWidgetIds == null || bitmaps == null
56                || 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 =
70                    PhotoAppWidgetProvider.buildUpdate(this, appWidgetId,
71                    helper);
72            appWidgetManager.updateAppWidget(new int[] { appWidgetId }, views);
73        }
74        helper.close();
75    }
76}
77