2 * Copyright (C) 2011 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package com.android.gallery3d.gadget;
19 import android.app.Activity;
20 import android.appwidget.AppWidgetManager;
21 import android.content.Intent;
22 import android.content.res.Resources;
23 import android.graphics.Bitmap;
24 import android.net.Uri;
25 import android.os.Bundle;
26 import android.widget.RemoteViews;
28 import com.android.gallery3d.R;
29 import com.android.gallery3d.app.AlbumPicker;
30 import com.android.gallery3d.app.CropImage;
31 import com.android.gallery3d.app.DialogPicker;
32 import com.android.gallery3d.common.ApiHelper;
34 public class WidgetConfigure extends Activity {
35 @SuppressWarnings("unused")
36 private static final String TAG = "WidgetConfigure";
38 public static final String KEY_WIDGET_TYPE = "widget-type";
39 private static final String KEY_PICKED_ITEM = "picked-item";
41 private static final int REQUEST_WIDGET_TYPE = 1;
42 private static final int REQUEST_CHOOSE_ALBUM = 2;
43 private static final int REQUEST_CROP_IMAGE = 3;
44 private static final int REQUEST_GET_PHOTO = 4;
46 public static final int RESULT_ERROR = RESULT_FIRST_USER;
48 // Scale up the widget size since we only specified the minimized
49 // size of the gadget. The real size could be larger.
50 // Note: There is also a limit on the size of data that can be
51 // passed in Binder's transaction.
52 private static float WIDGET_SCALE_FACTOR = 1.5f;
53 private static int MAX_WIDGET_SIDE = 360;
55 private int mAppWidgetId = -1;
56 private Uri mPickedItem;
59 protected void onCreate(Bundle savedState) {
60 super.onCreate(savedState);
61 mAppWidgetId = getIntent().getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
63 if (mAppWidgetId == -1) {
64 setResult(Activity.RESULT_CANCELED);
69 if (savedState == null) {
70 if (ApiHelper.HAS_REMOTE_VIEWS_SERVICE) {
71 Intent intent = new Intent(this, WidgetTypeChooser.class);
72 startActivityForResult(intent, REQUEST_WIDGET_TYPE);
73 } else { // Choose the photo type widget
74 setWidgetType(new Intent()
75 .putExtra(KEY_WIDGET_TYPE, R.id.widget_type_photo));
78 mPickedItem = savedState.getParcelable(KEY_PICKED_ITEM);
82 protected void onSaveInstanceStates(Bundle outState) {
83 super.onSaveInstanceState(outState);
84 outState.putParcelable(KEY_PICKED_ITEM, mPickedItem);
87 private void updateWidgetAndFinish(WidgetDatabaseHelper.Entry entry) {
88 AppWidgetManager manager = AppWidgetManager.getInstance(this);
89 RemoteViews views = PhotoAppWidgetProvider.buildWidget(this, mAppWidgetId, entry);
90 manager.updateAppWidget(mAppWidgetId, views);
91 setResult(RESULT_OK, new Intent().putExtra(
92 AppWidgetManager.EXTRA_APPWIDGET_ID, mAppWidgetId));
97 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
98 if (resultCode != RESULT_OK) {
99 setResult(resultCode, new Intent().putExtra(
100 AppWidgetManager.EXTRA_APPWIDGET_ID, mAppWidgetId));
105 if (requestCode == REQUEST_WIDGET_TYPE) {
107 } else if (requestCode == REQUEST_CHOOSE_ALBUM) {
108 setChoosenAlbum(data);
109 } else if (requestCode == REQUEST_GET_PHOTO) {
110 setChoosenPhoto(data);
111 } else if (requestCode == REQUEST_CROP_IMAGE) {
112 setPhotoWidget(data);
114 throw new AssertionError("unknown request: " + requestCode);
118 private void setPhotoWidget(Intent data) {
119 // Store the cropped photo in our database
120 Bitmap bitmap = (Bitmap) data.getParcelableExtra("data");
121 WidgetDatabaseHelper helper = new WidgetDatabaseHelper(this);
123 helper.setPhoto(mAppWidgetId, mPickedItem, bitmap);
124 updateWidgetAndFinish(helper.getEntry(mAppWidgetId));
130 private void setChoosenPhoto(Intent data) {
131 Resources res = getResources();
133 float width = res.getDimension(R.dimen.appwidget_width);
134 float height = res.getDimension(R.dimen.appwidget_height);
136 // We try to crop a larger image (by scale factor), but there is still
137 // a bound on the binder limit.
138 float scale = Math.min(WIDGET_SCALE_FACTOR,
139 MAX_WIDGET_SIDE / Math.max(width, height));
141 int widgetWidth = Math.round(width * scale);
142 int widgetHeight = Math.round(height * scale);
144 mPickedItem = data.getData();
145 Intent request = new Intent(CropImage.ACTION_CROP, mPickedItem)
146 .putExtra(CropImage.KEY_OUTPUT_X, widgetWidth)
147 .putExtra(CropImage.KEY_OUTPUT_Y, widgetHeight)
148 .putExtra(CropImage.KEY_ASPECT_X, widgetWidth)
149 .putExtra(CropImage.KEY_ASPECT_Y, widgetHeight)
150 .putExtra(CropImage.KEY_SCALE_UP_IF_NEEDED, true)
151 .putExtra(CropImage.KEY_SCALE, true)
152 .putExtra(CropImage.KEY_RETURN_DATA, true);
153 startActivityForResult(request, REQUEST_CROP_IMAGE);
156 private void setChoosenAlbum(Intent data) {
157 String albumPath = data.getStringExtra(AlbumPicker.KEY_ALBUM_PATH);
158 WidgetDatabaseHelper helper = new WidgetDatabaseHelper(this);
160 helper.setWidget(mAppWidgetId,
161 WidgetDatabaseHelper.TYPE_ALBUM, albumPath);
162 updateWidgetAndFinish(helper.getEntry(mAppWidgetId));
168 private void setWidgetType(Intent data) {
169 int widgetType = data.getIntExtra(KEY_WIDGET_TYPE, R.id.widget_type_shuffle);
170 if (widgetType == R.id.widget_type_album) {
171 Intent intent = new Intent(this, AlbumPicker.class);
172 startActivityForResult(intent, REQUEST_CHOOSE_ALBUM);
173 } else if (widgetType == R.id.widget_type_shuffle) {
174 WidgetDatabaseHelper helper = new WidgetDatabaseHelper(this);
176 helper.setWidget(mAppWidgetId, WidgetDatabaseHelper.TYPE_SHUFFLE, null);
177 updateWidgetAndFinish(helper.getEntry(mAppWidgetId));
182 // Explicitly send the intent to the DialogPhotoPicker
183 Intent request = new Intent(this, DialogPicker.class)
184 .setAction(Intent.ACTION_GET_CONTENT)
186 startActivityForResult(request, REQUEST_GET_PHOTO);