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