The Android Open Source Project | b64d345 | 2009-03-03 19:32:20 -0800 | [diff] [blame] | 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 | |
The Android Open Source Project | e3f4516 | 2009-03-11 12:11:58 -0700 | [diff] [blame] | 19 | import com.android.camera.PhotoAppWidgetProvider.PhotoDatabaseHelper; |
The Android Open Source Project | b64d345 | 2009-03-03 19:32:20 -0800 | [diff] [blame] | 20 | |
| 21 | import android.app.Activity; |
The Android Open Source Project | e3f4516 | 2009-03-11 12:11:58 -0700 | [diff] [blame] | 22 | import android.appwidget.AppWidgetManager; |
The Android Open Source Project | b64d345 | 2009-03-03 19:32:20 -0800 | [diff] [blame] | 23 | import android.content.Intent; |
The Android Open Source Project | b64d345 | 2009-03-03 19:32:20 -0800 | [diff] [blame] | 24 | import android.graphics.Bitmap; |
| 25 | import android.os.Bundle; |
Mike Cleron | 30e761e | 2009-09-20 15:25:11 -0700 | [diff] [blame] | 26 | import android.util.DisplayMetrics; |
The Android Open Source Project | b64d345 | 2009-03-03 19:32:20 -0800 | [diff] [blame] | 27 | import android.widget.RemoteViews; |
| 28 | |
Chih-Chung Chang | 6d8ecc2 | 2009-04-23 19:12:48 +0800 | [diff] [blame] | 29 | public class PhotoAppWidgetConfigure extends Activity { |
Owen Lin | 937fc48 | 2009-04-14 02:02:51 -0700 | [diff] [blame] | 30 | |
| 31 | @SuppressWarnings("unused") |
Chih-Chung Chang | b8af1c5 | 2009-04-01 02:48:59 -0700 | [diff] [blame] | 32 | private static final String TAG = "PhotoAppWidgetConfigure"; |
The Android Open Source Project | b64d345 | 2009-03-03 19:32:20 -0800 | [diff] [blame] | 33 | static final int REQUEST_GET_PHOTO = 2; |
Owen Lin | 937fc48 | 2009-04-14 02:02:51 -0700 | [diff] [blame] | 34 | |
Chih-Chung Chang | b8af1c5 | 2009-04-01 02:48:59 -0700 | [diff] [blame] | 35 | int mAppWidgetId = -1; |
The Android Open Source Project | b64d345 | 2009-03-03 19:32:20 -0800 | [diff] [blame] | 36 | |
| 37 | @Override |
| 38 | protected void onCreate(Bundle icicle) { |
| 39 | super.onCreate(icicle); |
Owen Lin | 937fc48 | 2009-04-14 02:02:51 -0700 | [diff] [blame] | 40 | |
Chih-Chung Chang | b8af1c5 | 2009-04-01 02:48:59 -0700 | [diff] [blame] | 41 | // Someone is requesting that we configure the given mAppWidgetId, which |
| 42 | // means we prompt the user to pick and crop a photo. |
Owen Lin | 937fc48 | 2009-04-14 02:02:51 -0700 | [diff] [blame] | 43 | |
Chih-Chung Chang | b8af1c5 | 2009-04-01 02:48:59 -0700 | [diff] [blame] | 44 | mAppWidgetId = getIntent().getIntExtra( |
| 45 | AppWidgetManager.EXTRA_APPWIDGET_ID, -1); |
| 46 | if (mAppWidgetId == -1) { |
The Android Open Source Project | b64d345 | 2009-03-03 19:32:20 -0800 | [diff] [blame] | 47 | setResult(Activity.RESULT_CANCELED); |
| 48 | finish(); |
| 49 | } |
| 50 | |
Mike Cleron | 30e761e | 2009-09-20 15:25:11 -0700 | [diff] [blame] | 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; |
Ray Chen | 4e3fd26 | 2009-09-21 11:05:58 -0700 | [diff] [blame] | 58 | |
The Android Open Source Project | b64d345 | 2009-03-03 19:32:20 -0800 | [diff] [blame] | 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); |
Mike Cleron | 30e761e | 2009-09-20 15:25:11 -0700 | [diff] [blame] | 65 | intent.putExtra("outputX", maxDimension); |
| 66 | intent.putExtra("outputY", maxDimension); |
The Android Open Source Project | b64d345 | 2009-03-03 19:32:20 -0800 | [diff] [blame] | 67 | intent.putExtra("noFaceDetection", true); |
| 68 | intent.putExtra("return-data", true); |
Owen Lin | 937fc48 | 2009-04-14 02:02:51 -0700 | [diff] [blame] | 69 | |
The Android Open Source Project | b64d345 | 2009-03-03 19:32:20 -0800 | [diff] [blame] | 70 | startActivityForResult(intent, REQUEST_GET_PHOTO); |
| 71 | } |
Owen Lin | 937fc48 | 2009-04-14 02:02:51 -0700 | [diff] [blame] | 72 | |
The Android Open Source Project | b64d345 | 2009-03-03 19:32:20 -0800 | [diff] [blame] | 73 | @Override |
Chih-Chung Chang | b8af1c5 | 2009-04-01 02:48:59 -0700 | [diff] [blame] | 74 | protected void onActivityResult(int requestCode, int resultCode, |
| 75 | Intent data) { |
| 76 | if (resultCode == RESULT_OK && mAppWidgetId != -1) { |
The Android Open Source Project | b64d345 | 2009-03-03 19:32:20 -0800 | [diff] [blame] | 77 | // Store the cropped photo in our database |
| 78 | Bitmap bitmap = (Bitmap) data.getParcelableExtra("data"); |
Owen Lin | 937fc48 | 2009-04-14 02:02:51 -0700 | [diff] [blame] | 79 | |
The Android Open Source Project | b64d345 | 2009-03-03 19:32:20 -0800 | [diff] [blame] | 80 | PhotoDatabaseHelper helper = new PhotoDatabaseHelper(this); |
Chih-Chung Chang | b8af1c5 | 2009-04-01 02:48:59 -0700 | [diff] [blame] | 81 | if (helper.setPhoto(mAppWidgetId, bitmap)) { |
The Android Open Source Project | b64d345 | 2009-03-03 19:32:20 -0800 | [diff] [blame] | 82 | resultCode = Activity.RESULT_OK; |
| 83 | |
The Android Open Source Project | e3f4516 | 2009-03-11 12:11:58 -0700 | [diff] [blame] | 84 | // Push newly updated widget to surface |
Chih-Chung Chang | b8af1c5 | 2009-04-01 02:48:59 -0700 | [diff] [blame] | 85 | 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 Project | b64d345 | 2009-03-03 19:32:20 -0800 | [diff] [blame] | 91 | } |
| 92 | helper.close(); |
| 93 | } else { |
| 94 | resultCode = Activity.RESULT_CANCELED; |
| 95 | } |
Owen Lin | 937fc48 | 2009-04-14 02:02:51 -0700 | [diff] [blame] | 96 | |
Chih-Chung Chang | b8af1c5 | 2009-04-01 02:48:59 -0700 | [diff] [blame] | 97 | // Make sure we pass back the original mAppWidgetId |
The Android Open Source Project | b64d345 | 2009-03-03 19:32:20 -0800 | [diff] [blame] | 98 | Intent resultValue = new Intent(); |
Chih-Chung Chang | b8af1c5 | 2009-04-01 02:48:59 -0700 | [diff] [blame] | 99 | resultValue.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, mAppWidgetId); |
The Android Open Source Project | b64d345 | 2009-03-03 19:32:20 -0800 | [diff] [blame] | 100 | setResult(resultCode, resultValue); |
| 101 | finish(); |
| 102 | } |
Owen Lin | 937fc48 | 2009-04-14 02:02:51 -0700 | [diff] [blame] | 103 | |
The Android Open Source Project | b64d345 | 2009-03-03 19:32:20 -0800 | [diff] [blame] | 104 | } |