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