2 * Copyright (C) 2007 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package com.android.gallery3d.app;
19 import android.annotation.TargetApi;
20 import android.app.Activity;
21 import android.content.Intent;
22 import android.graphics.Point;
23 import android.net.Uri;
24 import android.os.Build;
25 import android.os.Bundle;
26 import android.view.Display;
28 import com.android.gallery3d.common.ApiHelper;
31 * Wallpaper picker for the gallery application. This just redirects to the
32 * standard pick action.
34 public class Wallpaper extends Activity {
35 @SuppressWarnings("unused")
36 private static final String TAG = "Wallpaper";
38 private static final String IMAGE_TYPE = "image/*";
39 private static final String KEY_STATE = "activity-state";
40 private static final String KEY_PICKED_ITEM = "picked-item";
42 private static final int STATE_INIT = 0;
43 private static final int STATE_PHOTO_PICKED = 1;
45 private int mState = STATE_INIT;
46 private Uri mPickedItem;
49 protected void onCreate(Bundle bundle) {
50 super.onCreate(bundle);
52 mState = bundle.getInt(KEY_STATE);
53 mPickedItem = (Uri) bundle.getParcelable(KEY_PICKED_ITEM);
58 protected void onSaveInstanceState(Bundle saveState) {
59 saveState.putInt(KEY_STATE, mState);
60 if (mPickedItem != null) {
61 saveState.putParcelable(KEY_PICKED_ITEM, mPickedItem);
65 @SuppressWarnings("deprecation")
66 @TargetApi(ApiHelper.VERSION_CODES.HONEYCOMB_MR2)
67 private Point getDefaultDisplaySize(Point size) {
68 Display d = getWindowManager().getDefaultDisplay();
69 if (Build.VERSION.SDK_INT >= ApiHelper.VERSION_CODES.HONEYCOMB_MR2) {
72 size.set(d.getWidth(), d.getHeight());
77 @SuppressWarnings("fallthrough")
79 protected void onResume() {
81 Intent intent = getIntent();
84 mPickedItem = intent.getData();
85 if (mPickedItem == null) {
86 Intent request = new Intent(Intent.ACTION_GET_CONTENT)
87 .setClass(this, DialogPicker.class)
89 startActivityForResult(request, STATE_PHOTO_PICKED);
92 mState = STATE_PHOTO_PICKED;
95 case STATE_PHOTO_PICKED: {
96 int width = getWallpaperDesiredMinimumWidth();
97 int height = getWallpaperDesiredMinimumHeight();
98 Point size = getDefaultDisplaySize(new Point());
99 float spotlightX = (float) size.x / width;
100 float spotlightY = (float) size.y / height;
101 Intent request = new Intent(CropImage.ACTION_CROP)
102 .setDataAndType(mPickedItem, IMAGE_TYPE)
103 .addFlags(Intent.FLAG_ACTIVITY_FORWARD_RESULT)
104 .putExtra(CropImage.KEY_OUTPUT_X, width)
105 .putExtra(CropImage.KEY_OUTPUT_Y, height)
106 .putExtra(CropImage.KEY_ASPECT_X, width)
107 .putExtra(CropImage.KEY_ASPECT_Y, height)
108 .putExtra(CropImage.KEY_SPOTLIGHT_X, spotlightX)
109 .putExtra(CropImage.KEY_SPOTLIGHT_Y, spotlightY)
110 .putExtra(CropImage.KEY_SCALE, true)
111 .putExtra(CropImage.KEY_SCALE_UP_IF_NEEDED, true)
112 .putExtra(CropImage.KEY_NO_FACE_DETECTION, true)
113 .putExtra(CropImage.KEY_SET_AS_WALLPAPER, true);
114 startActivity(request);
121 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
122 if (resultCode != RESULT_OK) {
123 setResult(resultCode);
127 mState = requestCode;
128 if (mState == STATE_PHOTO_PICKED) {
129 mPickedItem = data.getData();
132 // onResume() would be called next