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
17package com.cooliris.media;
18
19import com.cooliris.app.App;
20import com.cooliris.media.PhotoAppWidgetProvider.PhotoDatabaseHelper;
21
22import android.app.Activity;
23import android.appwidget.AppWidgetManager;
24import android.content.Intent;
25import android.graphics.Bitmap;
26import android.os.Bundle;
27import android.widget.RemoteViews;
28
29public class PhotoAppWidgetConfigure extends Activity {
30
31    @SuppressWarnings("unused")
32    private static final String TAG = "PhotoAppWidgetConfigure";
33    static final int REQUEST_GET_PHOTO = 2;
34
35    private App mApp = null;
36    int mAppWidgetId = -1;
37
38    @Override
39    protected void onCreate(Bundle icicle) {
40        super.onCreate(icicle);
41        mApp = new App(PhotoAppWidgetConfigure.this);
42
43        // Someone is requesting that we configure the given mAppWidgetId, which
44        // means we prompt the user to pick and crop a photo.
45
46        mAppWidgetId = getIntent().getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
47        if (mAppWidgetId == -1) {
48            setResult(Activity.RESULT_CANCELED);
49            finish();
50        }
51
52        // TODO: get these values from constants somewhere
53        // TODO: Adjust the PhotoFrame's image size to avoid on the fly scaling
54        Intent intent = new Intent(Intent.ACTION_GET_CONTENT, null);
55        intent.setType("image/*");
56        intent.putExtra("crop", "true");
57        intent.putExtra("aspectX", 1);
58        intent.putExtra("aspectY", 1);
59        intent.putExtra("outputX", 192);
60        intent.putExtra("outputY", 192);
61        intent.putExtra("noFaceDetection", true);
62        intent.putExtra("return-data", true);
63
64        startActivityForResult(intent, REQUEST_GET_PHOTO);
65    }
66
67    @Override
68    public void onPause() {
69        super.onPause();
70    	mApp.onPause();
71    }
72
73    @Override
74    public void onResume() {
75        super.onResume();
76    	mApp.onResume();
77    }
78
79    @Override
80    public void onDestroy() {
81    	mApp.shutdown();
82    	super.onDestroy();
83    }
84
85    @Override
86    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
87        if (resultCode == RESULT_OK && mAppWidgetId != -1) {
88            // Store the cropped photo in our database
89            Bitmap bitmap = (Bitmap) data.getParcelableExtra("data");
90
91            PhotoDatabaseHelper helper = new PhotoDatabaseHelper(this);
92            if (helper.setPhoto(mAppWidgetId, bitmap)) {
93                resultCode = Activity.RESULT_OK;
94
95                // Push newly updated widget to surface
96                RemoteViews views = PhotoAppWidgetProvider.buildUpdate(this, mAppWidgetId, helper);
97                AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(this);
98                appWidgetManager.updateAppWidget(new int[] { mAppWidgetId }, views);
99            }
100            helper.close();
101        } else {
102            resultCode = Activity.RESULT_CANCELED;
103        }
104
105        // Make sure we pass back the original mAppWidgetId
106        Intent resultValue = new Intent();
107        resultValue.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, mAppWidgetId);
108        setResult(resultCode, resultValue);
109        finish();
110    }
111
112}
113