diff options
Diffstat (limited to 'src/com/android/gallery3d/widget/WidgetConfigure.java')
-rw-r--r-- | src/com/android/gallery3d/widget/WidgetConfigure.java | 167 |
1 files changed, 167 insertions, 0 deletions
diff --git a/src/com/android/gallery3d/widget/WidgetConfigure.java b/src/com/android/gallery3d/widget/WidgetConfigure.java new file mode 100644 index 000000000..3bcd9c46e --- /dev/null +++ b/src/com/android/gallery3d/widget/WidgetConfigure.java @@ -0,0 +1,167 @@ +/* + * Copyright (C) 2011 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.android.gallery3d.widget; + +import com.android.gallery3d.R; +import com.android.gallery3d.app.AlbumPicker; +import com.android.gallery3d.app.CropImage; +import com.android.gallery3d.app.DialogPicker; + +import android.app.Activity; +import android.appwidget.AppWidgetManager; +import android.content.Intent; +import android.content.res.Resources; +import android.graphics.Bitmap; +import android.net.Uri; +import android.os.Bundle; +import android.widget.RemoteViews; + +public class WidgetConfigure extends Activity { + @SuppressWarnings("unused") + private static final String TAG = "WidgetConfigure"; + + public static final String KEY_WIDGET_TYPE = "widget-type"; + + private static final int REQUEST_WIDGET_TYPE = 1; + private static final int REQUEST_CHOOSE_ALBUM = 2; + private static final int REQUEST_CROP_IMAGE = 3; + private static final int REQUEST_GET_PHOTO = 4; + + public static final int RESULT_ERROR = RESULT_FIRST_USER; + + // Scale up the widget size since we only specified the minimized + // size of the gadget. The real size could be larger. + // Note: There is also a limit on the size of data that can be + // passed in Binder's transaction. + private static float WIDGET_SCALE_FACTOR = 1.5f; + + private int mAppWidgetId = -1; + private int mWidgetType = 0; + private Uri mPickedItem; + + @Override + protected void onCreate(Bundle bundle) { + super.onCreate(bundle); + mAppWidgetId = getIntent().getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1); + + if (mAppWidgetId == -1) { + setResult(Activity.RESULT_CANCELED); + finish(); + return; + } + + if (mWidgetType == 0) { + Intent intent = new Intent(this, WidgetTypeChooser.class); + startActivityForResult(intent, REQUEST_WIDGET_TYPE); + } + } + + private void updateWidgetAndFinish(WidgetDatabaseHelper.Entry entry) { + AppWidgetManager manager = AppWidgetManager.getInstance(this); + RemoteViews views = WidgetProvider.buildWidget(this, mAppWidgetId, entry); + manager.updateAppWidget(mAppWidgetId, views); + setResult(RESULT_OK, new Intent().putExtra( + AppWidgetManager.EXTRA_APPWIDGET_ID, mAppWidgetId)); + finish(); + } + + @Override + protected void onActivityResult(int requestCode, int resultCode, Intent data) { + if (resultCode != RESULT_OK) { + setResult(resultCode, new Intent().putExtra( + AppWidgetManager.EXTRA_APPWIDGET_ID, mAppWidgetId)); + finish(); + return; + } + + if (requestCode == REQUEST_WIDGET_TYPE) { + setWidgetType(data); + } else if (requestCode == REQUEST_CHOOSE_ALBUM) { + setChoosenAlbum(data); + } else if (requestCode == REQUEST_GET_PHOTO) { + setChoosenPhoto(data); + } else if (requestCode == REQUEST_CROP_IMAGE) { + setPhotoWidget(data); + } else { + throw new AssertionError("unknown request: " + requestCode); + } + } + + private void setPhotoWidget(Intent data) { + // Store the cropped photo in our database + Bitmap bitmap = (Bitmap) data.getParcelableExtra("data"); + WidgetDatabaseHelper helper = new WidgetDatabaseHelper(this); + try { + helper.setPhoto(mAppWidgetId, mPickedItem, bitmap); + updateWidgetAndFinish(helper.getEntry(mAppWidgetId)); + } finally { + helper.close(); + } + } + + private void setChoosenPhoto(Intent data) { + Resources res = getResources(); + int widgetWidth = Math.round(WIDGET_SCALE_FACTOR + * res.getDimension(R.dimen.appwidget_width)); + int widgetHeight = Math.round(WIDGET_SCALE_FACTOR + * res.getDimension(R.dimen.appwidget_height)); + mPickedItem = data.getData(); + Intent request = new Intent(CropImage.ACTION_CROP, mPickedItem) + .putExtra(CropImage.KEY_OUTPUT_X, widgetWidth) + .putExtra(CropImage.KEY_OUTPUT_Y, widgetHeight) + .putExtra(CropImage.KEY_ASPECT_X, widgetWidth) + .putExtra(CropImage.KEY_ASPECT_Y, widgetHeight) + .putExtra(CropImage.KEY_SCALE_UP_IF_NEEDED, true) + .putExtra(CropImage.KEY_SCALE, true) + .putExtra(CropImage.KEY_RETURN_DATA, true); + startActivityForResult(request, REQUEST_CROP_IMAGE); + } + + private void setChoosenAlbum(Intent data) { + String albumPath = data.getStringExtra(AlbumPicker.KEY_ALBUM_PATH); + WidgetDatabaseHelper helper = new WidgetDatabaseHelper(this); + try { + helper.setWidget(mAppWidgetId, + WidgetDatabaseHelper.TYPE_ALBUM, albumPath); + updateWidgetAndFinish(helper.getEntry(mAppWidgetId)); + } finally { + helper.close(); + } + } + + private void setWidgetType(Intent data) { + mWidgetType = data.getIntExtra(KEY_WIDGET_TYPE, R.id.widget_type_shuffle); + if (mWidgetType == R.id.widget_type_album) { + Intent intent = new Intent(this, AlbumPicker.class); + startActivityForResult(intent, REQUEST_CHOOSE_ALBUM); + } else if (mWidgetType == R.id.widget_type_shuffle) { + WidgetDatabaseHelper helper = new WidgetDatabaseHelper(this); + try { + helper.setWidget(mAppWidgetId, WidgetDatabaseHelper.TYPE_SHUFFLE, null); + updateWidgetAndFinish(helper.getEntry(mAppWidgetId)); + } finally { + helper.close(); + } + } else { + // Explicitly send the intent to the DialogPhotoPicker + Intent request = new Intent(this, DialogPicker.class) + .setAction(Intent.ACTION_GET_CONTENT) + .setType("image/*"); + startActivityForResult(request, REQUEST_GET_PHOTO); + } + } +} |