Home | History | Annotate | Download | only in gadget
      1 /*
      2  * Copyright (C) 2011 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.gallery3d.gadget;
     18 
     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;
     27 
     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 
     33 public class WidgetConfigure extends Activity {
     34     @SuppressWarnings("unused")
     35     private static final String TAG = "WidgetConfigure";
     36 
     37     public static final String KEY_WIDGET_TYPE = "widget-type";
     38 
     39     private static final int REQUEST_WIDGET_TYPE = 1;
     40     private static final int REQUEST_CHOOSE_ALBUM = 2;
     41     private static final int REQUEST_CROP_IMAGE = 3;
     42     private static final int REQUEST_GET_PHOTO = 4;
     43 
     44     public static final int RESULT_ERROR = RESULT_FIRST_USER;
     45 
     46     // Scale up the widget size since we only specified the minimized
     47     // size of the gadget. The real size could be larger.
     48     // Note: There is also a limit on the size of data that can be
     49     // passed in Binder's transaction.
     50     private static float WIDGET_SCALE_FACTOR = 1.5f;
     51     private static int MAX_WIDGET_SIDE = 360;
     52 
     53     private int mAppWidgetId = -1;
     54     private int mWidgetType = 0;
     55     private Uri mPickedItem;
     56 
     57     @Override
     58     protected void onCreate(Bundle bundle) {
     59         super.onCreate(bundle);
     60         mAppWidgetId = getIntent().getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
     61 
     62         if (mAppWidgetId == -1) {
     63             setResult(Activity.RESULT_CANCELED);
     64             finish();
     65             return;
     66         }
     67 
     68         if (mWidgetType == 0) {
     69             Intent intent = new Intent(this, WidgetTypeChooser.class);
     70             startActivityForResult(intent, REQUEST_WIDGET_TYPE);
     71         }
     72     }
     73 
     74     private void updateWidgetAndFinish(WidgetDatabaseHelper.Entry entry) {
     75         AppWidgetManager manager = AppWidgetManager.getInstance(this);
     76         RemoteViews views = PhotoAppWidgetProvider.buildWidget(this, mAppWidgetId, entry);
     77         manager.updateAppWidget(mAppWidgetId, views);
     78         setResult(RESULT_OK, new Intent().putExtra(
     79                 AppWidgetManager.EXTRA_APPWIDGET_ID, mAppWidgetId));
     80         finish();
     81     }
     82 
     83     @Override
     84     protected void onActivityResult(int requestCode, int resultCode, Intent data) {
     85         if (resultCode != RESULT_OK) {
     86             setResult(resultCode, new Intent().putExtra(
     87                     AppWidgetManager.EXTRA_APPWIDGET_ID, mAppWidgetId));
     88             finish();
     89             return;
     90         }
     91 
     92         if (requestCode == REQUEST_WIDGET_TYPE) {
     93             setWidgetType(data);
     94         } else if (requestCode == REQUEST_CHOOSE_ALBUM) {
     95             setChoosenAlbum(data);
     96         } else if (requestCode == REQUEST_GET_PHOTO) {
     97             setChoosenPhoto(data);
     98         } else if (requestCode == REQUEST_CROP_IMAGE) {
     99             setPhotoWidget(data);
    100         } else {
    101             throw new AssertionError("unknown request: " + requestCode);
    102         }
    103     }
    104 
    105     private void setPhotoWidget(Intent data) {
    106         // Store the cropped photo in our database
    107         Bitmap bitmap = (Bitmap) data.getParcelableExtra("data");
    108         WidgetDatabaseHelper helper = new WidgetDatabaseHelper(this);
    109         try {
    110             helper.setPhoto(mAppWidgetId, mPickedItem, bitmap);
    111             updateWidgetAndFinish(helper.getEntry(mAppWidgetId));
    112         } finally {
    113             helper.close();
    114         }
    115     }
    116 
    117     private void setChoosenPhoto(Intent data) {
    118         Resources res = getResources();
    119 
    120         float width = res.getDimension(R.dimen.appwidget_width);
    121         float height = res.getDimension(R.dimen.appwidget_height);
    122 
    123         // We try to crop a larger image (by scale factor), but there is still
    124         // a bound on the binder limit.
    125         float scale = Math.min(WIDGET_SCALE_FACTOR,
    126                 MAX_WIDGET_SIDE / Math.max(width, height));
    127 
    128         int widgetWidth = Math.round(width * scale);
    129         int widgetHeight = Math.round(height * scale);
    130 
    131         mPickedItem = data.getData();
    132         Intent request = new Intent(CropImage.ACTION_CROP, mPickedItem)
    133                 .putExtra(CropImage.KEY_OUTPUT_X, widgetWidth)
    134                 .putExtra(CropImage.KEY_OUTPUT_Y, widgetHeight)
    135                 .putExtra(CropImage.KEY_ASPECT_X, widgetWidth)
    136                 .putExtra(CropImage.KEY_ASPECT_Y, widgetHeight)
    137                 .putExtra(CropImage.KEY_SCALE_UP_IF_NEEDED, true)
    138                 .putExtra(CropImage.KEY_SCALE, true)
    139                 .putExtra(CropImage.KEY_RETURN_DATA, true);
    140         startActivityForResult(request, REQUEST_CROP_IMAGE);
    141     }
    142 
    143     private void setChoosenAlbum(Intent data) {
    144         String albumPath = data.getStringExtra(AlbumPicker.KEY_ALBUM_PATH);
    145         WidgetDatabaseHelper helper = new WidgetDatabaseHelper(this);
    146         try {
    147             helper.setWidget(mAppWidgetId,
    148                     WidgetDatabaseHelper.TYPE_ALBUM, albumPath);
    149             updateWidgetAndFinish(helper.getEntry(mAppWidgetId));
    150         } finally {
    151             helper.close();
    152         }
    153     }
    154 
    155     private void setWidgetType(Intent data) {
    156         mWidgetType = data.getIntExtra(KEY_WIDGET_TYPE, R.id.widget_type_shuffle);
    157         if (mWidgetType == R.id.widget_type_album) {
    158             Intent intent = new Intent(this, AlbumPicker.class);
    159             startActivityForResult(intent, REQUEST_CHOOSE_ALBUM);
    160         } else if (mWidgetType == R.id.widget_type_shuffle) {
    161             WidgetDatabaseHelper helper = new WidgetDatabaseHelper(this);
    162             try {
    163                 helper.setWidget(mAppWidgetId, WidgetDatabaseHelper.TYPE_SHUFFLE, null);
    164                 updateWidgetAndFinish(helper.getEntry(mAppWidgetId));
    165             } finally {
    166                 helper.close();
    167             }
    168         } else {
    169             // Explicitly send the intent to the DialogPhotoPicker
    170             Intent request = new Intent(this, DialogPicker.class)
    171                     .setAction(Intent.ACTION_GET_CONTENT)
    172                     .setType("image/*");
    173             startActivityForResult(request, REQUEST_GET_PHOTO);
    174         }
    175     }
    176 }
    177