1/*
2 * Copyright (C) 2007 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.android.gallery3d.app;
18
19import android.app.Activity;
20import android.content.Intent;
21import android.net.Uri;
22import android.os.Bundle;
23import android.view.Display;
24
25/**
26 * Wallpaper picker for the gallery application. This just redirects to the
27 * standard pick action.
28 */
29public class Wallpaper extends Activity {
30    @SuppressWarnings("unused")
31    private static final String TAG = "Wallpaper";
32
33    private static final String IMAGE_TYPE = "image/*";
34    private static final String KEY_STATE = "activity-state";
35    private static final String KEY_PICKED_ITEM = "picked-item";
36
37    private static final int STATE_INIT = 0;
38    private static final int STATE_PHOTO_PICKED = 1;
39
40    private int mState = STATE_INIT;
41    private Uri mPickedItem;
42
43    @Override
44    protected void onCreate(Bundle bundle) {
45        super.onCreate(bundle);
46        if (bundle != null) {
47            mState = bundle.getInt(KEY_STATE);
48            mPickedItem = (Uri) bundle.getParcelable(KEY_PICKED_ITEM);
49        }
50    }
51
52    @Override
53    protected void onSaveInstanceState(Bundle saveState) {
54        saveState.putInt(KEY_STATE, mState);
55        if (mPickedItem != null) {
56            saveState.putParcelable(KEY_PICKED_ITEM, mPickedItem);
57        }
58    }
59
60    @SuppressWarnings("fallthrough")
61    @Override
62    protected void onResume() {
63        super.onResume();
64        Intent intent = getIntent();
65        switch (mState) {
66            case STATE_INIT: {
67                mPickedItem = intent.getData();
68                if (mPickedItem == null) {
69                    Intent request = new Intent(Intent.ACTION_GET_CONTENT)
70                            .setClass(this, DialogPicker.class)
71                            .setType(IMAGE_TYPE);
72                    startActivityForResult(request, STATE_PHOTO_PICKED);
73                    return;
74                }
75                mState = STATE_PHOTO_PICKED;
76                // fall-through
77            }
78            case STATE_PHOTO_PICKED: {
79                int width = getWallpaperDesiredMinimumWidth();
80                int height = getWallpaperDesiredMinimumHeight();
81                Display display = getWindowManager().getDefaultDisplay();
82                float spotlightX = (float) display.getWidth() / width;
83                float spotlightY = (float) display.getHeight() / height;
84                Intent request = new Intent(CropImage.ACTION_CROP)
85                        .setDataAndType(mPickedItem, IMAGE_TYPE)
86                        .addFlags(Intent.FLAG_ACTIVITY_FORWARD_RESULT)
87                        .putExtra(CropImage.KEY_OUTPUT_X, width)
88                        .putExtra(CropImage.KEY_OUTPUT_Y, height)
89                        .putExtra(CropImage.KEY_ASPECT_X, width)
90                        .putExtra(CropImage.KEY_ASPECT_Y, height)
91                        .putExtra(CropImage.KEY_SPOTLIGHT_X, spotlightX)
92                        .putExtra(CropImage.KEY_SPOTLIGHT_Y, spotlightY)
93                        .putExtra(CropImage.KEY_SCALE, true)
94                        .putExtra(CropImage.KEY_NO_FACE_DETECTION, true)
95                        .putExtra(CropImage.KEY_SET_AS_WALLPAPER, true);
96                startActivity(request);
97                finish();
98            }
99        }
100    }
101
102    @Override
103    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
104        if (resultCode != RESULT_OK) {
105            setResult(resultCode);
106            finish();
107            return;
108        }
109        mState = requestCode;
110        if (mState == STATE_PHOTO_PICKED) {
111            mPickedItem = data.getData();
112        }
113
114        // onResume() would be called next
115    }
116}
117