blob: 1755a71f3528e316f5e51fbd7640120e31d7e884 [file] [log] [blame]
The Android Open Source Projectb64d3452009-03-03 19:32:20 -08001/*
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
The Android Open Source Projecte3f45162009-03-11 12:11:58 -070019import com.android.camera.PhotoAppWidgetProvider.PhotoDatabaseHelper;
The Android Open Source Projectb64d3452009-03-03 19:32:20 -080020
21import android.app.Activity;
The Android Open Source Projecte3f45162009-03-11 12:11:58 -070022import android.appwidget.AppWidgetManager;
The Android Open Source Projectb64d3452009-03-03 19:32:20 -080023import android.content.Intent;
The Android Open Source Projectb64d3452009-03-03 19:32:20 -080024import android.graphics.Bitmap;
25import android.os.Bundle;
Mike Cleron30e761e2009-09-20 15:25:11 -070026import android.util.DisplayMetrics;
The Android Open Source Projectb64d3452009-03-03 19:32:20 -080027import android.widget.RemoteViews;
28
Chih-Chung Chang6d8ecc22009-04-23 19:12:48 +080029public class PhotoAppWidgetConfigure extends Activity {
Owen Lin937fc482009-04-14 02:02:51 -070030
31 @SuppressWarnings("unused")
Chih-Chung Changb8af1c52009-04-01 02:48:59 -070032 private static final String TAG = "PhotoAppWidgetConfigure";
The Android Open Source Projectb64d3452009-03-03 19:32:20 -080033 static final int REQUEST_GET_PHOTO = 2;
Owen Lin937fc482009-04-14 02:02:51 -070034
Chih-Chung Changb8af1c52009-04-01 02:48:59 -070035 int mAppWidgetId = -1;
The Android Open Source Projectb64d3452009-03-03 19:32:20 -080036
37 @Override
38 protected void onCreate(Bundle icicle) {
39 super.onCreate(icicle);
Owen Lin937fc482009-04-14 02:02:51 -070040
Chih-Chung Changb8af1c52009-04-01 02:48:59 -070041 // Someone is requesting that we configure the given mAppWidgetId, which
42 // means we prompt the user to pick and crop a photo.
Owen Lin937fc482009-04-14 02:02:51 -070043
Chih-Chung Changb8af1c52009-04-01 02:48:59 -070044 mAppWidgetId = getIntent().getIntExtra(
45 AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
46 if (mAppWidgetId == -1) {
The Android Open Source Projectb64d3452009-03-03 19:32:20 -080047 setResult(Activity.RESULT_CANCELED);
48 finish();
49 }
50
Mike Cleron30e761e2009-09-20 15:25:11 -070051 // 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;
Ray Chen4e3fd262009-09-21 11:05:58 -070058
The Android Open Source Projectb64d3452009-03-03 19:32:20 -080059 // 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);
Mike Cleron30e761e2009-09-20 15:25:11 -070065 intent.putExtra("outputX", maxDimension);
66 intent.putExtra("outputY", maxDimension);
The Android Open Source Projectb64d3452009-03-03 19:32:20 -080067 intent.putExtra("noFaceDetection", true);
68 intent.putExtra("return-data", true);
Owen Lin937fc482009-04-14 02:02:51 -070069
The Android Open Source Projectb64d3452009-03-03 19:32:20 -080070 startActivityForResult(intent, REQUEST_GET_PHOTO);
71 }
Owen Lin937fc482009-04-14 02:02:51 -070072
The Android Open Source Projectb64d3452009-03-03 19:32:20 -080073 @Override
Chih-Chung Changb8af1c52009-04-01 02:48:59 -070074 protected void onActivityResult(int requestCode, int resultCode,
75 Intent data) {
76 if (resultCode == RESULT_OK && mAppWidgetId != -1) {
The Android Open Source Projectb64d3452009-03-03 19:32:20 -080077 // Store the cropped photo in our database
78 Bitmap bitmap = (Bitmap) data.getParcelableExtra("data");
Owen Lin937fc482009-04-14 02:02:51 -070079
The Android Open Source Projectb64d3452009-03-03 19:32:20 -080080 PhotoDatabaseHelper helper = new PhotoDatabaseHelper(this);
Chih-Chung Changb8af1c52009-04-01 02:48:59 -070081 if (helper.setPhoto(mAppWidgetId, bitmap)) {
The Android Open Source Projectb64d3452009-03-03 19:32:20 -080082 resultCode = Activity.RESULT_OK;
83
The Android Open Source Projecte3f45162009-03-11 12:11:58 -070084 // Push newly updated widget to surface
Chih-Chung Changb8af1c52009-04-01 02:48:59 -070085 RemoteViews views = PhotoAppWidgetProvider.buildUpdate(this,
86 mAppWidgetId, helper);
87 AppWidgetManager appWidgetManager =
88 AppWidgetManager.getInstance(this);
89 appWidgetManager.updateAppWidget(new int[] {mAppWidgetId},
90 views);
The Android Open Source Projectb64d3452009-03-03 19:32:20 -080091 }
92 helper.close();
93 } else {
94 resultCode = Activity.RESULT_CANCELED;
95 }
Owen Lin937fc482009-04-14 02:02:51 -070096
Chih-Chung Changb8af1c52009-04-01 02:48:59 -070097 // Make sure we pass back the original mAppWidgetId
The Android Open Source Projectb64d3452009-03-03 19:32:20 -080098 Intent resultValue = new Intent();
Chih-Chung Changb8af1c52009-04-01 02:48:59 -070099 resultValue.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, mAppWidgetId);
The Android Open Source Projectb64d3452009-03-03 19:32:20 -0800100 setResult(resultCode, resultValue);
101 finish();
102 }
Owen Lin937fc482009-04-14 02:02:51 -0700103
The Android Open Source Projectb64d3452009-03-03 19:32:20 -0800104}