Wallpaper.java revision 4b9c37d08dc78118de53c1f3349024164abf2512
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.graphics.Point;
22import android.net.Uri;
23import android.os.Bundle;
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                Point size = new Point();
82                getWindowManager().getDefaultDisplay().getSize(size);
83                float spotlightX = (float) size.x / width;
84                float spotlightY = (float) size.y / height;
85                Intent request = new Intent(CropImage.ACTION_CROP)
86                        .setDataAndType(mPickedItem, IMAGE_TYPE)
87                        .addFlags(Intent.FLAG_ACTIVITY_FORWARD_RESULT)
88                        .putExtra(CropImage.KEY_OUTPUT_X, width)
89                        .putExtra(CropImage.KEY_OUTPUT_Y, height)
90                        .putExtra(CropImage.KEY_ASPECT_X, width)
91                        .putExtra(CropImage.KEY_ASPECT_Y, height)
92                        .putExtra(CropImage.KEY_SPOTLIGHT_X, spotlightX)
93                        .putExtra(CropImage.KEY_SPOTLIGHT_Y, spotlightY)
94                        .putExtra(CropImage.KEY_SCALE, true)
95                        .putExtra(CropImage.KEY_SCALE_UP_IF_NEEDED, true)
96                        .putExtra(CropImage.KEY_NO_FACE_DETECTION, true)
97                        .putExtra(CropImage.KEY_SET_AS_WALLPAPER, true);
98                startActivity(request);
99                finish();
100            }
101        }
102    }
103
104    @Override
105    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
106        if (resultCode != RESULT_OK) {
107            setResult(resultCode);
108            finish();
109            return;
110        }
111        mState = requestCode;
112        if (mState == STATE_PHOTO_PICKED) {
113            mPickedItem = data.getData();
114        }
115
116        // onResume() would be called next
117    }
118}
119