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.android.camera;
18 
19 import com.android.camera.PhotoAppWidgetProvider.PhotoDatabaseHelper;
20 
21 import android.app.Activity;
22 import android.appwidget.AppWidgetManager;
23 import android.content.Intent;
24 import android.graphics.Bitmap;
25 import android.os.Bundle;
26 import android.util.DisplayMetrics;
27 import android.widget.RemoteViews;
28 
29 public class PhotoAppWidgetConfigure extends NoSearchActivity {
30 
31     @SuppressWarnings("unused")
32     private static final String TAG = "PhotoAppWidgetConfigure";
33     static final int REQUEST_GET_PHOTO = 2;
34 
35     int mAppWidgetId = -1;
36 
37     @Override
onCreate(Bundle icicle)38     protected void onCreate(Bundle icicle) {
39         super.onCreate(icicle);
40 
41         // Someone is requesting that we configure the given mAppWidgetId, which
42         // means we prompt the user to pick and crop a photo.
43 
44         mAppWidgetId = getIntent().getIntExtra(
45                 AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
46         if (mAppWidgetId == -1) {
47             setResult(Activity.RESULT_CANCELED);
48             finish();
49         }
50 
51         // Assume the widget will be 1/4 of the screen.
52         // This will be slightly too large, but there is not a good way to know the
53         // actual widget size from here. The image will be scaled to fit since the layout
54         // file specifies android:scaleType="centerCrop"
55         DisplayMetrics display = getResources().getDisplayMetrics();
56         int maxDimension = Math.max(display.heightPixels, display.widthPixels);
57         maxDimension /= 2;
58 
59         // TODO: Adjust the PhotoFrame's image size to avoid on the fly scaling
60         Intent intent = new Intent(Intent.ACTION_GET_CONTENT, null);
61         intent.setType("image/*");
62         intent.putExtra("crop", "true");
63         intent.putExtra("aspectX", 1);
64         intent.putExtra("aspectY", 1);
65         intent.putExtra("outputX", maxDimension);
66         intent.putExtra("outputY", maxDimension);
67         intent.putExtra("noFaceDetection", true);
68         intent.putExtra("return-data", true);
69 
70         startActivityForResult(intent, REQUEST_GET_PHOTO);
71     }
72 
73     @Override
onActivityResult(int requestCode, int resultCode, Intent data)74     protected void onActivityResult(int requestCode, int resultCode,
75                                     Intent data) {
76         if (resultCode == RESULT_OK && mAppWidgetId != -1) {
77             // Store the cropped photo in our database
78             Bitmap bitmap = (Bitmap) data.getParcelableExtra("data");
79 
80             PhotoDatabaseHelper helper = new PhotoDatabaseHelper(this);
81             if (helper.setPhoto(mAppWidgetId, bitmap)) {
82                 resultCode = Activity.RESULT_OK;
83 
84                 // Push newly updated widget to surface
85                 RemoteViews views = PhotoAppWidgetProvider.buildUpdate(this,
86                         mAppWidgetId, helper);
87                 AppWidgetManager appWidgetManager =
88                         AppWidgetManager.getInstance(this);
89                 appWidgetManager.updateAppWidget(new int[] {mAppWidgetId},
90                                                  views);
91             }
92             helper.close();
93         } else {
94             resultCode = Activity.RESULT_CANCELED;
95         }
96 
97         // Make sure we pass back the original mAppWidgetId
98         Intent resultValue = new Intent();
99         resultValue.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, mAppWidgetId);
100         setResult(resultCode, resultValue);
101         finish();
102     }
103 
104 }
105