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