Dianne Hackborn | 4c62fc0 | 2009-08-08 20:40:27 -0700 | [diff] [blame] | 1 | /* |
Christopher Tate | be132e6 | 2016-02-10 12:59:49 -0800 | [diff] [blame] | 2 | h * Copyright (C) 2009 The Android Open Source Project |
Dianne Hackborn | 4c62fc0 | 2009-08-08 20:40:27 -0700 | [diff] [blame] | 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 | |
Dianne Hackborn | 8cc6a50 | 2009-08-05 21:29:42 -0700 | [diff] [blame] | 17 | package android.app; |
| 18 | |
Christopher Tate | ad3c259 | 2016-01-20 18:13:17 -0800 | [diff] [blame] | 19 | import android.annotation.IntDef; |
Tor Norbye | 7b9c912 | 2013-05-30 16:48:33 -0700 | [diff] [blame] | 20 | import android.annotation.RawRes; |
Dianne Hackborn | 067e5f6 | 2014-09-07 23:14:30 -0700 | [diff] [blame] | 21 | import android.annotation.SystemApi; |
Jeff Sharkey | 28f0877 | 2014-04-16 09:41:58 -0700 | [diff] [blame] | 22 | import android.content.ComponentName; |
Adam Lesinski | bba72d1 | 2013-10-11 18:10:56 -0700 | [diff] [blame] | 23 | import android.content.ContentResolver; |
Dianne Hackborn | 8cc6a50 | 2009-08-05 21:29:42 -0700 | [diff] [blame] | 24 | import android.content.Context; |
| 25 | import android.content.Intent; |
Michael Jurka | e8d1bf7 | 2013-09-09 15:58:54 +0200 | [diff] [blame] | 26 | import android.content.pm.PackageManager; |
| 27 | import android.content.pm.ResolveInfo; |
Dianne Hackborn | 8cc6a50 | 2009-08-05 21:29:42 -0700 | [diff] [blame] | 28 | import android.content.res.Resources; |
| 29 | import android.graphics.Bitmap; |
| 30 | import android.graphics.BitmapFactory; |
Michael Jurka | b668d0b | 2013-10-04 15:11:05 -0700 | [diff] [blame] | 31 | import android.graphics.BitmapRegionDecoder; |
Dianne Hackborn | 284ac93 | 2009-08-28 10:34:25 -0700 | [diff] [blame] | 32 | import android.graphics.Canvas; |
Dianne Hackborn | 19382ac | 2009-09-11 21:13:37 -0700 | [diff] [blame] | 33 | import android.graphics.ColorFilter; |
Michael Jurka | b668d0b | 2013-10-04 15:11:05 -0700 | [diff] [blame] | 34 | import android.graphics.Matrix; |
Dianne Hackborn | 284ac93 | 2009-08-28 10:34:25 -0700 | [diff] [blame] | 35 | import android.graphics.Paint; |
Dianne Hackborn | 19382ac | 2009-09-11 21:13:37 -0700 | [diff] [blame] | 36 | import android.graphics.PixelFormat; |
Jeff Brown | 2457237 | 2011-06-09 19:05:15 -0700 | [diff] [blame] | 37 | import android.graphics.PorterDuff; |
| 38 | import android.graphics.PorterDuffXfermode; |
Dianne Hackborn | 284ac93 | 2009-08-28 10:34:25 -0700 | [diff] [blame] | 39 | import android.graphics.Rect; |
Michael Jurka | b668d0b | 2013-10-04 15:11:05 -0700 | [diff] [blame] | 40 | import android.graphics.RectF; |
Dianne Hackborn | 8cc6a50 | 2009-08-05 21:29:42 -0700 | [diff] [blame] | 41 | import android.graphics.drawable.BitmapDrawable; |
| 42 | import android.graphics.drawable.Drawable; |
Michael Jurka | e8d1bf7 | 2013-09-09 15:58:54 +0200 | [diff] [blame] | 43 | import android.net.Uri; |
Dianne Hackborn | 284ac93 | 2009-08-28 10:34:25 -0700 | [diff] [blame] | 44 | import android.os.Bundle; |
Dianne Hackborn | 8cc6a50 | 2009-08-05 21:29:42 -0700 | [diff] [blame] | 45 | import android.os.Handler; |
| 46 | import android.os.IBinder; |
Dianne Hackborn | 840c3a2 | 2009-09-02 17:35:17 -0700 | [diff] [blame] | 47 | import android.os.Looper; |
Dianne Hackborn | 8cc6a50 | 2009-08-05 21:29:42 -0700 | [diff] [blame] | 48 | import android.os.ParcelFileDescriptor; |
| 49 | import android.os.RemoteException; |
| 50 | import android.os.ServiceManager; |
Jeff Sharkey | 28f0877 | 2014-04-16 09:41:58 -0700 | [diff] [blame] | 51 | import android.os.SystemProperties; |
Christopher Tate | be132e6 | 2016-02-10 12:59:49 -0800 | [diff] [blame] | 52 | import android.os.UserHandle; |
Jeff Sharkey | 28f0877 | 2014-04-16 09:41:58 -0700 | [diff] [blame] | 53 | import android.text.TextUtils; |
Dianne Hackborn | 7341d7a | 2009-08-14 11:37:52 -0700 | [diff] [blame] | 54 | import android.util.Log; |
Jeff Brown | 98365d7 | 2012-08-19 20:30:52 -0700 | [diff] [blame] | 55 | import android.view.WindowManagerGlobal; |
Dianne Hackborn | 8cc6a50 | 2009-08-05 21:29:42 -0700 | [diff] [blame] | 56 | |
Christopher Tate | ffa6a88 | 2015-12-11 15:08:46 -0800 | [diff] [blame] | 57 | import libcore.io.IoUtils; |
| 58 | |
Michael Jurka | b668d0b | 2013-10-04 15:11:05 -0700 | [diff] [blame] | 59 | import java.io.BufferedInputStream; |
Jeff Sharkey | 28f0877 | 2014-04-16 09:41:58 -0700 | [diff] [blame] | 60 | import java.io.File; |
| 61 | import java.io.FileInputStream; |
Dianne Hackborn | 8cc6a50 | 2009-08-05 21:29:42 -0700 | [diff] [blame] | 62 | import java.io.FileOutputStream; |
| 63 | import java.io.IOException; |
| 64 | import java.io.InputStream; |
Christopher Tate | ad3c259 | 2016-01-20 18:13:17 -0800 | [diff] [blame] | 65 | import java.lang.annotation.Retention; |
| 66 | import java.lang.annotation.RetentionPolicy; |
Michael Jurka | e8d1bf7 | 2013-09-09 15:58:54 +0200 | [diff] [blame] | 67 | import java.util.List; |
Christopher Tate | 1e1e2e0 | 2016-01-25 15:34:36 -0800 | [diff] [blame] | 68 | import java.util.concurrent.CountDownLatch; |
| 69 | import java.util.concurrent.TimeUnit; |
Dianne Hackborn | 8cc6a50 | 2009-08-05 21:29:42 -0700 | [diff] [blame] | 70 | |
Scott Main | 8b2e000 | 2009-09-29 18:17:31 -0700 | [diff] [blame] | 71 | /** |
| 72 | * Provides access to the system wallpaper. With WallpaperManager, you can |
| 73 | * get the current wallpaper, get the desired dimensions for the wallpaper, set |
| 74 | * the wallpaper, and more. Get an instance of WallpaperManager with |
Benjamin Franz | f3ece36 | 2015-02-11 10:51:10 +0000 | [diff] [blame] | 75 | * {@link #getInstance(android.content.Context) getInstance()}. |
| 76 | * |
| 77 | * <p> An app can check whether wallpapers are supported for the current user, by calling |
Oleksandr Peletskyi | f251981 | 2016-01-26 20:16:06 +0100 | [diff] [blame] | 78 | * {@link #isWallpaperSupported()}, and whether setting of wallpapers is allowed, by calling |
| 79 | * {@link #isWallpaperSettingAllowed()}. |
Scott Main | 8b2e000 | 2009-09-29 18:17:31 -0700 | [diff] [blame] | 80 | */ |
Dianne Hackborn | 8cc6a50 | 2009-08-05 21:29:42 -0700 | [diff] [blame] | 81 | public class WallpaperManager { |
| 82 | private static String TAG = "WallpaperManager"; |
| 83 | private static boolean DEBUG = false; |
Marco Nelissen | bf6956b | 2009-11-09 15:21:13 -0800 | [diff] [blame] | 84 | private float mWallpaperXStep = -1; |
| 85 | private float mWallpaperYStep = -1; |
Dianne Hackborn | 8cc6a50 | 2009-08-05 21:29:42 -0700 | [diff] [blame] | 86 | |
Jeff Sharkey | 28f0877 | 2014-04-16 09:41:58 -0700 | [diff] [blame] | 87 | /** {@hide} */ |
| 88 | private static final String PROP_WALLPAPER = "ro.config.wallpaper"; |
| 89 | /** {@hide} */ |
| 90 | private static final String PROP_WALLPAPER_COMPONENT = "ro.config.wallpaper_component"; |
| 91 | |
Dianne Hackborn | 4c62fc0 | 2009-08-08 20:40:27 -0700 | [diff] [blame] | 92 | /** |
Michael Jurka | e8d1bf7 | 2013-09-09 15:58:54 +0200 | [diff] [blame] | 93 | * Activity Action: Show settings for choosing wallpaper. Do not use directly to construct |
| 94 | * an intent; instead, use {@link #getCropAndSetWallpaperIntent}. |
| 95 | * <p>Input: {@link Intent#getData} is the URI of the image to crop and set as wallpaper. |
| 96 | * <p>Output: RESULT_OK if user decided to crop/set the wallpaper, RESULT_CANCEL otherwise |
Adam Lesinski | bba72d1 | 2013-10-11 18:10:56 -0700 | [diff] [blame] | 97 | * Activities that support this intent should specify a MIME filter of "image/*" |
Michael Jurka | e8d1bf7 | 2013-09-09 15:58:54 +0200 | [diff] [blame] | 98 | */ |
| 99 | public static final String ACTION_CROP_AND_SET_WALLPAPER = |
| 100 | "android.service.wallpaper.CROP_AND_SET_WALLPAPER"; |
| 101 | |
| 102 | /** |
Dianne Hackborn | 4c62fc0 | 2009-08-08 20:40:27 -0700 | [diff] [blame] | 103 | * Launch an activity for the user to pick the current global live |
| 104 | * wallpaper. |
Dianne Hackborn | 4c62fc0 | 2009-08-08 20:40:27 -0700 | [diff] [blame] | 105 | */ |
| 106 | public static final String ACTION_LIVE_WALLPAPER_CHOOSER |
| 107 | = "android.service.wallpaper.LIVE_WALLPAPER_CHOOSER"; |
Adam Cohen | 5a242ec | 2010-12-07 21:07:07 -0800 | [diff] [blame] | 108 | |
| 109 | /** |
Dianne Hackborn | 7df7d20 | 2012-04-19 18:00:04 -0700 | [diff] [blame] | 110 | * Directly launch live wallpaper preview, allowing the user to immediately |
| 111 | * confirm to switch to a specific live wallpaper. You must specify |
| 112 | * {@link #EXTRA_LIVE_WALLPAPER_COMPONENT} with the ComponentName of |
| 113 | * a live wallpaper component that is to be shown. |
| 114 | */ |
| 115 | public static final String ACTION_CHANGE_LIVE_WALLPAPER |
| 116 | = "android.service.wallpaper.CHANGE_LIVE_WALLPAPER"; |
| 117 | |
| 118 | /** |
| 119 | * Extra in {@link #ACTION_CHANGE_LIVE_WALLPAPER} that specifies the |
| 120 | * ComponentName of a live wallpaper that should be shown as a preview, |
| 121 | * for the user to confirm. |
| 122 | */ |
| 123 | public static final String EXTRA_LIVE_WALLPAPER_COMPONENT |
| 124 | = "android.service.wallpaper.extra.LIVE_WALLPAPER_COMPONENT"; |
| 125 | |
| 126 | /** |
Adam Cohen | 5a242ec | 2010-12-07 21:07:07 -0800 | [diff] [blame] | 127 | * Manifest entry for activities that respond to {@link Intent#ACTION_SET_WALLPAPER} |
| 128 | * which allows them to provide a custom large icon associated with this action. |
| 129 | */ |
| 130 | public static final String WALLPAPER_PREVIEW_META_DATA = "android.wallpaper.preview"; |
| 131 | |
Dianne Hackborn | 23ef7b4 | 2009-11-18 18:20:39 -0800 | [diff] [blame] | 132 | /** |
| 133 | * Command for {@link #sendWallpaperCommand}: reported by the wallpaper |
| 134 | * host when the user taps on an empty area (not performing an action |
| 135 | * in the host). The x and y arguments are the location of the tap in |
| 136 | * screen coordinates. |
| 137 | */ |
| 138 | public static final String COMMAND_TAP = "android.wallpaper.tap"; |
Oleksandr Peletskyi | f251981 | 2016-01-26 20:16:06 +0100 | [diff] [blame] | 139 | |
Dianne Hackborn | 23ef7b4 | 2009-11-18 18:20:39 -0800 | [diff] [blame] | 140 | /** |
| 141 | * Command for {@link #sendWallpaperCommand}: reported by the wallpaper |
Jeff Brown | 9f3bdfe | 2010-10-13 06:01:27 -0700 | [diff] [blame] | 142 | * host when the user releases a secondary pointer on an empty area |
| 143 | * (not performing an action in the host). The x and y arguments are |
| 144 | * the location of the secondary tap in screen coordinates. |
| 145 | */ |
| 146 | public static final String COMMAND_SECONDARY_TAP = "android.wallpaper.secondaryTap"; |
| 147 | |
| 148 | /** |
| 149 | * Command for {@link #sendWallpaperCommand}: reported by the wallpaper |
Dianne Hackborn | 23ef7b4 | 2009-11-18 18:20:39 -0800 | [diff] [blame] | 150 | * host when the user drops an object into an area of the host. The x |
| 151 | * and y arguments are the location of the drop. |
| 152 | */ |
| 153 | public static final String COMMAND_DROP = "android.home.drop"; |
Christopher Tate | ad3c259 | 2016-01-20 18:13:17 -0800 | [diff] [blame] | 154 | |
| 155 | /** |
| 156 | * Extra passed back from setWallpaper() giving the new wallpaper's assigned ID. |
| 157 | * @hide |
| 158 | */ |
| 159 | public static final String EXTRA_NEW_WALLPAPER_ID = "android.service.wallpaper.extra.ID"; |
| 160 | |
| 161 | // flags for which kind of wallpaper to set |
| 162 | |
| 163 | /** @hide */ |
| 164 | @IntDef(flag = true, value = { |
| 165 | FLAG_SET_SYSTEM, |
| 166 | FLAG_SET_LOCK |
| 167 | }) |
| 168 | @Retention(RetentionPolicy.SOURCE) |
| 169 | public @interface SetWallpaperFlags {} |
| 170 | |
| 171 | /** |
| 172 | * Flag: use the supplied imagery as the general system wallpaper. |
| 173 | */ |
| 174 | public static final int FLAG_SET_SYSTEM = 1 << 0; |
| 175 | |
| 176 | /** |
| 177 | * Flag: use the supplied imagery as the lock-screen wallpaper. |
| 178 | */ |
| 179 | public static final int FLAG_SET_LOCK = 1 << 1; |
| 180 | |
Dianne Hackborn | 8cc6a50 | 2009-08-05 21:29:42 -0700 | [diff] [blame] | 181 | private final Context mContext; |
Oleksandr Peletskyi | f251981 | 2016-01-26 20:16:06 +0100 | [diff] [blame] | 182 | |
Dianne Hackborn | 19382ac | 2009-09-11 21:13:37 -0700 | [diff] [blame] | 183 | /** |
| 184 | * Special drawable that draws a wallpaper as fast as possible. Assumes |
| 185 | * no scaling or placement off (0,0) of the wallpaper (this should be done |
| 186 | * at the time the bitmap is loaded). |
| 187 | */ |
| 188 | static class FastBitmapDrawable extends Drawable { |
| 189 | private final Bitmap mBitmap; |
| 190 | private final int mWidth; |
| 191 | private final int mHeight; |
| 192 | private int mDrawLeft; |
| 193 | private int mDrawTop; |
Romain Guy | 407ec78 | 2011-08-24 17:06:58 -0700 | [diff] [blame] | 194 | private final Paint mPaint; |
Dianne Hackborn | 19382ac | 2009-09-11 21:13:37 -0700 | [diff] [blame] | 195 | |
| 196 | private FastBitmapDrawable(Bitmap bitmap) { |
| 197 | mBitmap = bitmap; |
| 198 | mWidth = bitmap.getWidth(); |
| 199 | mHeight = bitmap.getHeight(); |
Romain Guy | 407ec78 | 2011-08-24 17:06:58 -0700 | [diff] [blame] | 200 | |
Dianne Hackborn | 19382ac | 2009-09-11 21:13:37 -0700 | [diff] [blame] | 201 | setBounds(0, 0, mWidth, mHeight); |
Romain Guy | 407ec78 | 2011-08-24 17:06:58 -0700 | [diff] [blame] | 202 | |
| 203 | mPaint = new Paint(); |
| 204 | mPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC)); |
Dianne Hackborn | 19382ac | 2009-09-11 21:13:37 -0700 | [diff] [blame] | 205 | } |
| 206 | |
| 207 | @Override |
| 208 | public void draw(Canvas canvas) { |
Romain Guy | 407ec78 | 2011-08-24 17:06:58 -0700 | [diff] [blame] | 209 | canvas.drawBitmap(mBitmap, mDrawLeft, mDrawTop, mPaint); |
Dianne Hackborn | 19382ac | 2009-09-11 21:13:37 -0700 | [diff] [blame] | 210 | } |
| 211 | |
| 212 | @Override |
| 213 | public int getOpacity() { |
| 214 | return PixelFormat.OPAQUE; |
| 215 | } |
| 216 | |
| 217 | @Override |
| 218 | public void setBounds(int left, int top, int right, int bottom) { |
| 219 | mDrawLeft = left + (right-left - mWidth) / 2; |
| 220 | mDrawTop = top + (bottom-top - mHeight) / 2; |
| 221 | } |
| 222 | |
| 223 | @Override |
Dianne Hackborn | 19382ac | 2009-09-11 21:13:37 -0700 | [diff] [blame] | 224 | public void setAlpha(int alpha) { |
Romain Guy | 407ec78 | 2011-08-24 17:06:58 -0700 | [diff] [blame] | 225 | throw new UnsupportedOperationException("Not supported with this drawable"); |
Dianne Hackborn | 19382ac | 2009-09-11 21:13:37 -0700 | [diff] [blame] | 226 | } |
| 227 | |
| 228 | @Override |
Chris Craik | bd3bfc5 | 2015-03-02 10:43:29 -0800 | [diff] [blame] | 229 | public void setColorFilter(ColorFilter colorFilter) { |
Romain Guy | 407ec78 | 2011-08-24 17:06:58 -0700 | [diff] [blame] | 230 | throw new UnsupportedOperationException("Not supported with this drawable"); |
Dianne Hackborn | 19382ac | 2009-09-11 21:13:37 -0700 | [diff] [blame] | 231 | } |
| 232 | |
| 233 | @Override |
| 234 | public void setDither(boolean dither) { |
Romain Guy | 407ec78 | 2011-08-24 17:06:58 -0700 | [diff] [blame] | 235 | throw new UnsupportedOperationException("Not supported with this drawable"); |
Dianne Hackborn | 19382ac | 2009-09-11 21:13:37 -0700 | [diff] [blame] | 236 | } |
| 237 | |
| 238 | @Override |
| 239 | public void setFilterBitmap(boolean filter) { |
Romain Guy | 407ec78 | 2011-08-24 17:06:58 -0700 | [diff] [blame] | 240 | throw new UnsupportedOperationException("Not supported with this drawable"); |
Dianne Hackborn | 19382ac | 2009-09-11 21:13:37 -0700 | [diff] [blame] | 241 | } |
| 242 | |
| 243 | @Override |
| 244 | public int getIntrinsicWidth() { |
| 245 | return mWidth; |
| 246 | } |
| 247 | |
| 248 | @Override |
| 249 | public int getIntrinsicHeight() { |
| 250 | return mHeight; |
| 251 | } |
| 252 | |
| 253 | @Override |
| 254 | public int getMinimumWidth() { |
| 255 | return mWidth; |
| 256 | } |
| 257 | |
| 258 | @Override |
| 259 | public int getMinimumHeight() { |
| 260 | return mHeight; |
| 261 | } |
| 262 | } |
Oleksandr Peletskyi | f251981 | 2016-01-26 20:16:06 +0100 | [diff] [blame] | 263 | |
Dianne Hackborn | 8cc6a50 | 2009-08-05 21:29:42 -0700 | [diff] [blame] | 264 | static class Globals extends IWallpaperManagerCallback.Stub { |
| 265 | private IWallpaperManager mService; |
Dianne Hackborn | 284ac93 | 2009-08-28 10:34:25 -0700 | [diff] [blame] | 266 | private Bitmap mWallpaper; |
Dianne Hackborn | 19382ac | 2009-09-11 21:13:37 -0700 | [diff] [blame] | 267 | private Bitmap mDefaultWallpaper; |
Oleksandr Peletskyi | f251981 | 2016-01-26 20:16:06 +0100 | [diff] [blame] | 268 | |
Dianne Hackborn | 5e802fb | 2009-09-02 21:50:50 -0700 | [diff] [blame] | 269 | Globals(Looper looper) { |
Dianne Hackborn | 8cc6a50 | 2009-08-05 21:29:42 -0700 | [diff] [blame] | 270 | IBinder b = ServiceManager.getService(Context.WALLPAPER_SERVICE); |
| 271 | mService = IWallpaperManager.Stub.asInterface(b); |
Christopher Tate | be132e6 | 2016-02-10 12:59:49 -0800 | [diff] [blame] | 272 | forgetLoadedWallpaper(); |
Dianne Hackborn | 8cc6a50 | 2009-08-05 21:29:42 -0700 | [diff] [blame] | 273 | } |
Oleksandr Peletskyi | f251981 | 2016-01-26 20:16:06 +0100 | [diff] [blame] | 274 | |
Dianne Hackborn | 8cc6a50 | 2009-08-05 21:29:42 -0700 | [diff] [blame] | 275 | public void onWallpaperChanged() { |
| 276 | /* The wallpaper has changed but we shouldn't eagerly load the |
| 277 | * wallpaper as that would be inefficient. Reset the cached wallpaper |
| 278 | * to null so if the user requests the wallpaper again then we'll |
| 279 | * fetch it. |
| 280 | */ |
Christopher Tate | be132e6 | 2016-02-10 12:59:49 -0800 | [diff] [blame] | 281 | forgetLoadedWallpaper(); |
Dianne Hackborn | 8cc6a50 | 2009-08-05 21:29:42 -0700 | [diff] [blame] | 282 | } |
Adam Cohen | 041a0ba | 2011-11-09 20:10:27 -0800 | [diff] [blame] | 283 | |
Dianne Hackborn | 19382ac | 2009-09-11 21:13:37 -0700 | [diff] [blame] | 284 | public Bitmap peekWallpaperBitmap(Context context, boolean returnDefault) { |
Yorke Lee | dcd93cc | 2016-01-08 14:12:55 -0800 | [diff] [blame] | 285 | return peekWallpaperBitmap(context, returnDefault, context.getUserId()); |
| 286 | } |
| 287 | |
| 288 | public Bitmap peekWallpaperBitmap(Context context, boolean returnDefault, int userId) { |
Dianne Hackborn | 8cc6a50 | 2009-08-05 21:29:42 -0700 | [diff] [blame] | 289 | synchronized (this) { |
Benjamin Franz | f3ece36 | 2015-02-11 10:51:10 +0000 | [diff] [blame] | 290 | if (mService != null) { |
| 291 | try { |
| 292 | if (!mService.isWallpaperSupported(context.getOpPackageName())) { |
| 293 | return null; |
| 294 | } |
| 295 | } catch (RemoteException e) { |
| 296 | // Ignore |
| 297 | } |
| 298 | } |
Dianne Hackborn | 8cc6a50 | 2009-08-05 21:29:42 -0700 | [diff] [blame] | 299 | if (mWallpaper != null) { |
| 300 | return mWallpaper; |
| 301 | } |
Dianne Hackborn | 19382ac | 2009-09-11 21:13:37 -0700 | [diff] [blame] | 302 | if (mDefaultWallpaper != null) { |
| 303 | return mDefaultWallpaper; |
| 304 | } |
Dianne Hackborn | c9dbbe2 | 2009-11-11 22:50:37 -0800 | [diff] [blame] | 305 | mWallpaper = null; |
| 306 | try { |
Yorke Lee | dcd93cc | 2016-01-08 14:12:55 -0800 | [diff] [blame] | 307 | mWallpaper = getCurrentWallpaperLocked(userId); |
Dianne Hackborn | c9dbbe2 | 2009-11-11 22:50:37 -0800 | [diff] [blame] | 308 | } catch (OutOfMemoryError e) { |
| 309 | Log.w(TAG, "No memory load current wallpaper", e); |
| 310 | } |
Dianne Hackborn | ba39839 | 2011-08-01 16:11:57 -0700 | [diff] [blame] | 311 | if (returnDefault) { |
| 312 | if (mWallpaper == null) { |
| 313 | mDefaultWallpaper = getDefaultWallpaperLocked(context); |
| 314 | return mDefaultWallpaper; |
| 315 | } else { |
| 316 | mDefaultWallpaper = null; |
| 317 | } |
Dianne Hackborn | 19382ac | 2009-09-11 21:13:37 -0700 | [diff] [blame] | 318 | } |
Dianne Hackborn | 8cc6a50 | 2009-08-05 21:29:42 -0700 | [diff] [blame] | 319 | return mWallpaper; |
| 320 | } |
| 321 | } |
Dianne Hackborn | ba39839 | 2011-08-01 16:11:57 -0700 | [diff] [blame] | 322 | |
| 323 | public void forgetLoadedWallpaper() { |
| 324 | synchronized (this) { |
| 325 | mWallpaper = null; |
| 326 | mDefaultWallpaper = null; |
| 327 | } |
| 328 | } |
| 329 | |
Yorke Lee | dcd93cc | 2016-01-08 14:12:55 -0800 | [diff] [blame] | 330 | private Bitmap getCurrentWallpaperLocked(int userId) { |
Adam Lesinski | 2c8d67c | 2014-04-23 13:46:21 -0700 | [diff] [blame] | 331 | if (mService == null) { |
| 332 | Log.w(TAG, "WallpaperService not running"); |
| 333 | return null; |
| 334 | } |
| 335 | |
Dianne Hackborn | 8cc6a50 | 2009-08-05 21:29:42 -0700 | [diff] [blame] | 336 | try { |
Dianne Hackborn | 284ac93 | 2009-08-28 10:34:25 -0700 | [diff] [blame] | 337 | Bundle params = new Bundle(); |
Christopher Tate | be132e6 | 2016-02-10 12:59:49 -0800 | [diff] [blame] | 338 | ParcelFileDescriptor fd = mService.getWallpaper(this, FLAG_SET_SYSTEM, |
Yorke Lee | dcd93cc | 2016-01-08 14:12:55 -0800 | [diff] [blame] | 339 | params, userId); |
Dianne Hackborn | 8cc6a50 | 2009-08-05 21:29:42 -0700 | [diff] [blame] | 340 | if (fd != null) { |
Jeff Brown | 2457237 | 2011-06-09 19:05:15 -0700 | [diff] [blame] | 341 | try { |
| 342 | BitmapFactory.Options options = new BitmapFactory.Options(); |
Michael Jurka | 824a4b5 | 2013-12-18 17:10:16 +0100 | [diff] [blame] | 343 | return BitmapFactory.decodeFileDescriptor( |
Jeff Brown | 2457237 | 2011-06-09 19:05:15 -0700 | [diff] [blame] | 344 | fd.getFileDescriptor(), null, options); |
Jeff Brown | 2457237 | 2011-06-09 19:05:15 -0700 | [diff] [blame] | 345 | } catch (OutOfMemoryError e) { |
| 346 | Log.w(TAG, "Can't decode file", e); |
| 347 | } finally { |
Christopher Tate | ffa6a88 | 2015-12-11 15:08:46 -0800 | [diff] [blame] | 348 | IoUtils.closeQuietly(fd); |
Dianne Hackborn | 8cc6a50 | 2009-08-05 21:29:42 -0700 | [diff] [blame] | 349 | } |
Dianne Hackborn | 19382ac | 2009-09-11 21:13:37 -0700 | [diff] [blame] | 350 | } |
| 351 | } catch (RemoteException e) { |
Romain Guy | 407ec78 | 2011-08-24 17:06:58 -0700 | [diff] [blame] | 352 | // Ignore |
Dianne Hackborn | 19382ac | 2009-09-11 21:13:37 -0700 | [diff] [blame] | 353 | } |
| 354 | return null; |
| 355 | } |
Christopher Tate | ffa6a88 | 2015-12-11 15:08:46 -0800 | [diff] [blame] | 356 | |
Dianne Hackborn | 19382ac | 2009-09-11 21:13:37 -0700 | [diff] [blame] | 357 | private Bitmap getDefaultWallpaperLocked(Context context) { |
Adam Lesinski | e929bee | 2014-04-28 14:15:06 -0700 | [diff] [blame] | 358 | InputStream is = openDefaultWallpaper(context); |
Adam Lesinski | 2c8d67c | 2014-04-23 13:46:21 -0700 | [diff] [blame] | 359 | if (is != null) { |
| 360 | try { |
| 361 | BitmapFactory.Options options = new BitmapFactory.Options(); |
| 362 | return BitmapFactory.decodeStream(is, null, options); |
| 363 | } catch (OutOfMemoryError e) { |
| 364 | Log.w(TAG, "Can't decode stream", e); |
| 365 | } finally { |
Christopher Tate | ffa6a88 | 2015-12-11 15:08:46 -0800 | [diff] [blame] | 366 | IoUtils.closeQuietly(is); |
Dianne Hackborn | 8cc6a50 | 2009-08-05 21:29:42 -0700 | [diff] [blame] | 367 | } |
Dianne Hackborn | 8cc6a50 | 2009-08-05 21:29:42 -0700 | [diff] [blame] | 368 | } |
| 369 | return null; |
| 370 | } |
| 371 | } |
Oleksandr Peletskyi | f251981 | 2016-01-26 20:16:06 +0100 | [diff] [blame] | 372 | |
Romain Guy | 407ec78 | 2011-08-24 17:06:58 -0700 | [diff] [blame] | 373 | private static final Object sSync = new Object[0]; |
Dianne Hackborn | 8cc6a50 | 2009-08-05 21:29:42 -0700 | [diff] [blame] | 374 | private static Globals sGlobals; |
| 375 | |
Dianne Hackborn | 840c3a2 | 2009-09-02 17:35:17 -0700 | [diff] [blame] | 376 | static void initGlobals(Looper looper) { |
Romain Guy | 407ec78 | 2011-08-24 17:06:58 -0700 | [diff] [blame] | 377 | synchronized (sSync) { |
Dianne Hackborn | 8cc6a50 | 2009-08-05 21:29:42 -0700 | [diff] [blame] | 378 | if (sGlobals == null) { |
Dianne Hackborn | 5e802fb | 2009-09-02 21:50:50 -0700 | [diff] [blame] | 379 | sGlobals = new Globals(looper); |
Dianne Hackborn | 8cc6a50 | 2009-08-05 21:29:42 -0700 | [diff] [blame] | 380 | } |
Dianne Hackborn | 8cc6a50 | 2009-08-05 21:29:42 -0700 | [diff] [blame] | 381 | } |
| 382 | } |
Oleksandr Peletskyi | f251981 | 2016-01-26 20:16:06 +0100 | [diff] [blame] | 383 | |
Dianne Hackborn | 8cc6a50 | 2009-08-05 21:29:42 -0700 | [diff] [blame] | 384 | /*package*/ WallpaperManager(Context context, Handler handler) { |
| 385 | mContext = context; |
Dianne Hackborn | 840c3a2 | 2009-09-02 17:35:17 -0700 | [diff] [blame] | 386 | initGlobals(context.getMainLooper()); |
Dianne Hackborn | 8cc6a50 | 2009-08-05 21:29:42 -0700 | [diff] [blame] | 387 | } |
| 388 | |
| 389 | /** |
Dianne Hackborn | 4c62fc0 | 2009-08-08 20:40:27 -0700 | [diff] [blame] | 390 | * Retrieve a WallpaperManager associated with the given Context. |
| 391 | */ |
| 392 | public static WallpaperManager getInstance(Context context) { |
| 393 | return (WallpaperManager)context.getSystemService( |
| 394 | Context.WALLPAPER_SERVICE); |
| 395 | } |
Oleksandr Peletskyi | f251981 | 2016-01-26 20:16:06 +0100 | [diff] [blame] | 396 | |
Dianne Hackborn | 4c62fc0 | 2009-08-08 20:40:27 -0700 | [diff] [blame] | 397 | /** @hide */ |
| 398 | public IWallpaperManager getIWallpaperManager() { |
Dianne Hackborn | 840c3a2 | 2009-09-02 17:35:17 -0700 | [diff] [blame] | 399 | return sGlobals.mService; |
Dianne Hackborn | 4c62fc0 | 2009-08-08 20:40:27 -0700 | [diff] [blame] | 400 | } |
Oleksandr Peletskyi | f251981 | 2016-01-26 20:16:06 +0100 | [diff] [blame] | 401 | |
Dianne Hackborn | 4c62fc0 | 2009-08-08 20:40:27 -0700 | [diff] [blame] | 402 | /** |
Scott Main | 8b2e000 | 2009-09-29 18:17:31 -0700 | [diff] [blame] | 403 | * Retrieve the current system wallpaper; if |
Michael Jurka | b668d0b | 2013-10-04 15:11:05 -0700 | [diff] [blame] | 404 | * no wallpaper is set, the system built-in static wallpaper is returned. |
Scott Main | 8b2e000 | 2009-09-29 18:17:31 -0700 | [diff] [blame] | 405 | * This is returned as an |
| 406 | * abstract Drawable that you can install in a View to display whatever |
Oleksandr Peletskyi | f251981 | 2016-01-26 20:16:06 +0100 | [diff] [blame] | 407 | * wallpaper the user has currently set. |
Dianne Hackborn | 8cc6a50 | 2009-08-05 21:29:42 -0700 | [diff] [blame] | 408 | * |
| 409 | * @return Returns a Drawable object that will draw the wallpaper. |
| 410 | */ |
Dianne Hackborn | 4c62fc0 | 2009-08-08 20:40:27 -0700 | [diff] [blame] | 411 | public Drawable getDrawable() { |
Dianne Hackborn | 19382ac | 2009-09-11 21:13:37 -0700 | [diff] [blame] | 412 | Bitmap bm = sGlobals.peekWallpaperBitmap(mContext, true); |
| 413 | if (bm != null) { |
| 414 | Drawable dr = new BitmapDrawable(mContext.getResources(), bm); |
| 415 | dr.setDither(false); |
| 416 | return dr; |
| 417 | } |
| 418 | return null; |
Dianne Hackborn | 8cc6a50 | 2009-08-05 21:29:42 -0700 | [diff] [blame] | 419 | } |
| 420 | |
| 421 | /** |
Michael Jurka | b668d0b | 2013-10-04 15:11:05 -0700 | [diff] [blame] | 422 | * Returns a drawable for the system built-in static wallpaper . |
| 423 | * |
| 424 | */ |
| 425 | public Drawable getBuiltInDrawable() { |
| 426 | return getBuiltInDrawable(0, 0, false, 0, 0); |
| 427 | } |
| 428 | |
| 429 | /** |
| 430 | * Returns a drawable for the system built-in static wallpaper. Based on the parameters, the |
| 431 | * drawable can be cropped and scaled |
| 432 | * |
| 433 | * @param outWidth The width of the returned drawable |
| 434 | * @param outWidth The height of the returned drawable |
| 435 | * @param scaleToFit If true, scale the wallpaper down rather than just cropping it |
| 436 | * @param horizontalAlignment A float value between 0 and 1 specifying where to crop the image; |
| 437 | * 0 for left-aligned, 0.5 for horizontal center-aligned, and 1 for right-aligned |
| 438 | * @param verticalAlignment A float value between 0 and 1 specifying where to crop the image; |
| 439 | * 0 for top-aligned, 0.5 for vertical center-aligned, and 1 for bottom-aligned |
| 440 | * |
| 441 | */ |
| 442 | public Drawable getBuiltInDrawable(int outWidth, int outHeight, |
| 443 | boolean scaleToFit, float horizontalAlignment, float verticalAlignment) { |
| 444 | if (sGlobals.mService == null) { |
| 445 | Log.w(TAG, "WallpaperService not running"); |
| 446 | return null; |
| 447 | } |
| 448 | Resources resources = mContext.getResources(); |
| 449 | horizontalAlignment = Math.max(0, Math.min(1, horizontalAlignment)); |
| 450 | verticalAlignment = Math.max(0, Math.min(1, verticalAlignment)); |
| 451 | |
Jeff Sharkey | 28f0877 | 2014-04-16 09:41:58 -0700 | [diff] [blame] | 452 | InputStream is = new BufferedInputStream(openDefaultWallpaper(mContext)); |
Michael Jurka | b668d0b | 2013-10-04 15:11:05 -0700 | [diff] [blame] | 453 | |
| 454 | if (is == null) { |
| 455 | Log.e(TAG, "default wallpaper input stream is null"); |
| 456 | return null; |
| 457 | } else { |
| 458 | if (outWidth <= 0 || outHeight <= 0) { |
| 459 | Bitmap fullSize = BitmapFactory.decodeStream(is, null, null); |
| 460 | return new BitmapDrawable(resources, fullSize); |
| 461 | } else { |
| 462 | int inWidth; |
| 463 | int inHeight; |
| 464 | { |
| 465 | BitmapFactory.Options options = new BitmapFactory.Options(); |
| 466 | options.inJustDecodeBounds = true; |
| 467 | BitmapFactory.decodeStream(is, null, options); |
| 468 | if (options.outWidth != 0 && options.outHeight != 0) { |
| 469 | inWidth = options.outWidth; |
| 470 | inHeight = options.outHeight; |
| 471 | } else { |
| 472 | Log.e(TAG, "default wallpaper dimensions are 0"); |
| 473 | return null; |
| 474 | } |
| 475 | } |
| 476 | |
Jeff Sharkey | 28f0877 | 2014-04-16 09:41:58 -0700 | [diff] [blame] | 477 | is = new BufferedInputStream(openDefaultWallpaper(mContext)); |
Michael Jurka | b668d0b | 2013-10-04 15:11:05 -0700 | [diff] [blame] | 478 | |
| 479 | RectF cropRectF; |
| 480 | |
| 481 | outWidth = Math.min(inWidth, outWidth); |
| 482 | outHeight = Math.min(inHeight, outHeight); |
| 483 | if (scaleToFit) { |
| 484 | cropRectF = getMaxCropRect(inWidth, inHeight, outWidth, outHeight, |
| 485 | horizontalAlignment, verticalAlignment); |
| 486 | } else { |
| 487 | float left = (inWidth - outWidth) * horizontalAlignment; |
| 488 | float right = left + outWidth; |
| 489 | float top = (inHeight - outHeight) * verticalAlignment; |
| 490 | float bottom = top + outHeight; |
Michael Jurka | 0539153 | 2013-10-14 20:44:42 -0700 | [diff] [blame] | 491 | cropRectF = new RectF(left, top, right, bottom); |
Michael Jurka | b668d0b | 2013-10-04 15:11:05 -0700 | [diff] [blame] | 492 | } |
| 493 | Rect roundedTrueCrop = new Rect(); |
| 494 | cropRectF.roundOut(roundedTrueCrop); |
| 495 | |
| 496 | if (roundedTrueCrop.width() <= 0 || roundedTrueCrop.height() <= 0) { |
| 497 | Log.w(TAG, "crop has bad values for full size image"); |
| 498 | return null; |
| 499 | } |
| 500 | |
| 501 | // See how much we're reducing the size of the image |
| 502 | int scaleDownSampleSize = Math.min(roundedTrueCrop.width() / outWidth, |
| 503 | roundedTrueCrop.height() / outHeight); |
| 504 | |
| 505 | // Attempt to open a region decoder |
| 506 | BitmapRegionDecoder decoder = null; |
| 507 | try { |
| 508 | decoder = BitmapRegionDecoder.newInstance(is, true); |
| 509 | } catch (IOException e) { |
| 510 | Log.w(TAG, "cannot open region decoder for default wallpaper"); |
| 511 | } |
| 512 | |
| 513 | Bitmap crop = null; |
| 514 | if (decoder != null) { |
| 515 | // Do region decoding to get crop bitmap |
| 516 | BitmapFactory.Options options = new BitmapFactory.Options(); |
| 517 | if (scaleDownSampleSize > 1) { |
| 518 | options.inSampleSize = scaleDownSampleSize; |
| 519 | } |
| 520 | crop = decoder.decodeRegion(roundedTrueCrop, options); |
| 521 | decoder.recycle(); |
| 522 | } |
| 523 | |
| 524 | if (crop == null) { |
| 525 | // BitmapRegionDecoder has failed, try to crop in-memory |
Jeff Sharkey | 28f0877 | 2014-04-16 09:41:58 -0700 | [diff] [blame] | 526 | is = new BufferedInputStream(openDefaultWallpaper(mContext)); |
Michael Jurka | b668d0b | 2013-10-04 15:11:05 -0700 | [diff] [blame] | 527 | Bitmap fullSize = null; |
| 528 | if (is != null) { |
| 529 | BitmapFactory.Options options = new BitmapFactory.Options(); |
| 530 | if (scaleDownSampleSize > 1) { |
| 531 | options.inSampleSize = scaleDownSampleSize; |
| 532 | } |
| 533 | fullSize = BitmapFactory.decodeStream(is, null, options); |
| 534 | } |
| 535 | if (fullSize != null) { |
| 536 | crop = Bitmap.createBitmap(fullSize, roundedTrueCrop.left, |
| 537 | roundedTrueCrop.top, roundedTrueCrop.width(), |
| 538 | roundedTrueCrop.height()); |
| 539 | } |
| 540 | } |
| 541 | |
| 542 | if (crop == null) { |
| 543 | Log.w(TAG, "cannot decode default wallpaper"); |
| 544 | return null; |
| 545 | } |
| 546 | |
| 547 | // Scale down if necessary |
| 548 | if (outWidth > 0 && outHeight > 0 && |
| 549 | (crop.getWidth() != outWidth || crop.getHeight() != outHeight)) { |
| 550 | Matrix m = new Matrix(); |
| 551 | RectF cropRect = new RectF(0, 0, crop.getWidth(), crop.getHeight()); |
| 552 | RectF returnRect = new RectF(0, 0, outWidth, outHeight); |
| 553 | m.setRectToRect(cropRect, returnRect, Matrix.ScaleToFit.FILL); |
| 554 | Bitmap tmp = Bitmap.createBitmap((int) returnRect.width(), |
| 555 | (int) returnRect.height(), Bitmap.Config.ARGB_8888); |
| 556 | if (tmp != null) { |
| 557 | Canvas c = new Canvas(tmp); |
| 558 | Paint p = new Paint(); |
| 559 | p.setFilterBitmap(true); |
| 560 | c.drawBitmap(crop, m, p); |
| 561 | crop = tmp; |
| 562 | } |
| 563 | } |
| 564 | |
| 565 | return new BitmapDrawable(resources, crop); |
| 566 | } |
| 567 | } |
| 568 | } |
| 569 | |
| 570 | private static RectF getMaxCropRect(int inWidth, int inHeight, int outWidth, int outHeight, |
| 571 | float horizontalAlignment, float verticalAlignment) { |
| 572 | RectF cropRect = new RectF(); |
| 573 | // Get a crop rect that will fit this |
| 574 | if (inWidth / (float) inHeight > outWidth / (float) outHeight) { |
| 575 | cropRect.top = 0; |
| 576 | cropRect.bottom = inHeight; |
| 577 | float cropWidth = outWidth * (inHeight / (float) outHeight); |
| 578 | cropRect.left = (inWidth - cropWidth) * horizontalAlignment; |
| 579 | cropRect.right = cropRect.left + cropWidth; |
| 580 | } else { |
| 581 | cropRect.left = 0; |
| 582 | cropRect.right = inWidth; |
| 583 | float cropHeight = outHeight * (inWidth / (float) outWidth); |
| 584 | cropRect.top = (inHeight - cropHeight) * verticalAlignment; |
| 585 | cropRect.bottom = cropRect.top + cropHeight; |
| 586 | } |
| 587 | return cropRect; |
| 588 | } |
| 589 | |
| 590 | /** |
Scott Main | 8b2e000 | 2009-09-29 18:17:31 -0700 | [diff] [blame] | 591 | * Retrieve the current system wallpaper; if there is no wallpaper set, |
| 592 | * a null pointer is returned. This is returned as an |
Dianne Hackborn | 8cc6a50 | 2009-08-05 21:29:42 -0700 | [diff] [blame] | 593 | * abstract Drawable that you can install in a View to display whatever |
Oleksandr Peletskyi | f251981 | 2016-01-26 20:16:06 +0100 | [diff] [blame] | 594 | * wallpaper the user has currently set. |
Dianne Hackborn | 8cc6a50 | 2009-08-05 21:29:42 -0700 | [diff] [blame] | 595 | * |
| 596 | * @return Returns a Drawable object that will draw the wallpaper or a |
| 597 | * null pointer if these is none. |
| 598 | */ |
Dianne Hackborn | 4c62fc0 | 2009-08-08 20:40:27 -0700 | [diff] [blame] | 599 | public Drawable peekDrawable() { |
Dianne Hackborn | 19382ac | 2009-09-11 21:13:37 -0700 | [diff] [blame] | 600 | Bitmap bm = sGlobals.peekWallpaperBitmap(mContext, false); |
| 601 | if (bm != null) { |
| 602 | Drawable dr = new BitmapDrawable(mContext.getResources(), bm); |
| 603 | dr.setDither(false); |
| 604 | return dr; |
| 605 | } |
| 606 | return null; |
| 607 | } |
| 608 | |
| 609 | /** |
Scott Main | 8b2e000 | 2009-09-29 18:17:31 -0700 | [diff] [blame] | 610 | * Like {@link #getDrawable()}, but the returned Drawable has a number |
| 611 | * of limitations to reduce its overhead as much as possible. It will |
| 612 | * never scale the wallpaper (only centering it if the requested bounds |
| 613 | * do match the bitmap bounds, which should not be typical), doesn't |
| 614 | * allow setting an alpha, color filter, or other attributes, etc. The |
| 615 | * bounds of the returned drawable will be initialized to the same bounds |
| 616 | * as the wallpaper, so normally you will not need to touch it. The |
| 617 | * drawable also assumes that it will be used in a context running in |
| 618 | * the same density as the screen (not in density compatibility mode). |
Dianne Hackborn | 19382ac | 2009-09-11 21:13:37 -0700 | [diff] [blame] | 619 | * |
| 620 | * @return Returns a Drawable object that will draw the wallpaper. |
| 621 | */ |
| 622 | public Drawable getFastDrawable() { |
| 623 | Bitmap bm = sGlobals.peekWallpaperBitmap(mContext, true); |
| 624 | if (bm != null) { |
Romain Guy | 407ec78 | 2011-08-24 17:06:58 -0700 | [diff] [blame] | 625 | return new FastBitmapDrawable(bm); |
Dianne Hackborn | 19382ac | 2009-09-11 21:13:37 -0700 | [diff] [blame] | 626 | } |
| 627 | return null; |
| 628 | } |
| 629 | |
| 630 | /** |
Scott Main | 8b2e000 | 2009-09-29 18:17:31 -0700 | [diff] [blame] | 631 | * Like {@link #getFastDrawable()}, but if there is no wallpaper set, |
| 632 | * a null pointer is returned. |
Dianne Hackborn | 19382ac | 2009-09-11 21:13:37 -0700 | [diff] [blame] | 633 | * |
| 634 | * @return Returns an optimized Drawable object that will draw the |
| 635 | * wallpaper or a null pointer if these is none. |
| 636 | */ |
| 637 | public Drawable peekFastDrawable() { |
Christopher Tate | be132e6 | 2016-02-10 12:59:49 -0800 | [diff] [blame] | 638 | Bitmap bm = sGlobals.peekWallpaperBitmap(mContext, false); |
Dianne Hackborn | 19382ac | 2009-09-11 21:13:37 -0700 | [diff] [blame] | 639 | if (bm != null) { |
Romain Guy | 407ec78 | 2011-08-24 17:06:58 -0700 | [diff] [blame] | 640 | return new FastBitmapDrawable(bm); |
Dianne Hackborn | 19382ac | 2009-09-11 21:13:37 -0700 | [diff] [blame] | 641 | } |
| 642 | return null; |
Dianne Hackborn | 8cc6a50 | 2009-08-05 21:29:42 -0700 | [diff] [blame] | 643 | } |
| 644 | |
| 645 | /** |
Romain Guy | 407ec78 | 2011-08-24 17:06:58 -0700 | [diff] [blame] | 646 | * Like {@link #getDrawable()} but returns a Bitmap. |
Oleksandr Peletskyi | f251981 | 2016-01-26 20:16:06 +0100 | [diff] [blame] | 647 | * |
Romain Guy | 407ec78 | 2011-08-24 17:06:58 -0700 | [diff] [blame] | 648 | * @hide |
| 649 | */ |
| 650 | public Bitmap getBitmap() { |
Yorke Lee | dcd93cc | 2016-01-08 14:12:55 -0800 | [diff] [blame] | 651 | return getBitmapAsUser(mContext.getUserId()); |
| 652 | } |
| 653 | |
| 654 | /** |
| 655 | * Like {@link #getDrawable()} but returns a Bitmap for the provided user. |
| 656 | * |
| 657 | * @hide |
| 658 | */ |
| 659 | public Bitmap getBitmapAsUser(int userId) { |
| 660 | return sGlobals.peekWallpaperBitmap(mContext, true, userId); |
Romain Guy | 407ec78 | 2011-08-24 17:06:58 -0700 | [diff] [blame] | 661 | } |
| 662 | |
| 663 | /** |
Christopher Tate | be132e6 | 2016-02-10 12:59:49 -0800 | [diff] [blame] | 664 | * Get an open, readable file descriptor to the given wallpaper image file. |
| 665 | * The callee is resopnsible for closing the fd when done ingesting the file. |
| 666 | * |
| 667 | * <p>If no lock-specific wallpaper has been configured for the given user, then |
| 668 | * this method will return {@code null} when requesting {@link #FLAG_SET_LOCK} rather than |
| 669 | * returning the system wallpaper's image file. |
| 670 | */ |
| 671 | public ParcelFileDescriptor getWallpaperFile(int which) { |
| 672 | return getWallpaperFile(which, mContext.getUserId()); |
| 673 | } |
| 674 | |
| 675 | /** |
| 676 | * Version of {@link #getWallpaperFile(int)} that can access the wallpaper data |
| 677 | * for a given user. The caller must hold the INTERACT_ACROSS_USERS_FULL |
| 678 | * permission to access another user's wallpaper data. |
| 679 | * @hide |
| 680 | */ |
| 681 | public ParcelFileDescriptor getWallpaperFile(int which, int userId) { |
| 682 | if (which != FLAG_SET_SYSTEM && which != FLAG_SET_LOCK) { |
| 683 | throw new IllegalArgumentException("Must request exactly one kind of wallpaper"); |
| 684 | } |
| 685 | |
| 686 | if (sGlobals.mService == null) { |
| 687 | Log.w(TAG, "WallpaperService not running"); |
| 688 | return null; |
| 689 | } else { |
| 690 | try { |
| 691 | Bundle outParams = new Bundle(); |
| 692 | return sGlobals.mService.getWallpaper(null, which, outParams, userId); |
| 693 | } catch (RemoteException e) { |
| 694 | return null; |
| 695 | } |
| 696 | } |
| 697 | } |
| 698 | |
| 699 | /** |
Dianne Hackborn | ba39839 | 2011-08-01 16:11:57 -0700 | [diff] [blame] | 700 | * Remove all internal references to the last loaded wallpaper. Useful |
| 701 | * for apps that want to reduce memory usage when they only temporarily |
| 702 | * need to have the wallpaper. After calling, the next request for the |
| 703 | * wallpaper will require reloading it again from disk. |
| 704 | */ |
| 705 | public void forgetLoadedWallpaper() { |
Christopher Tate | be132e6 | 2016-02-10 12:59:49 -0800 | [diff] [blame] | 706 | sGlobals.forgetLoadedWallpaper(); |
Dianne Hackborn | ba39839 | 2011-08-01 16:11:57 -0700 | [diff] [blame] | 707 | } |
| 708 | |
| 709 | /** |
Dianne Hackborn | eb03465 | 2009-09-07 00:49:58 -0700 | [diff] [blame] | 710 | * If the current wallpaper is a live wallpaper component, return the |
| 711 | * information about that wallpaper. Otherwise, if it is a static image, |
| 712 | * simply return null. |
Dianne Hackborn | eb03465 | 2009-09-07 00:49:58 -0700 | [diff] [blame] | 713 | */ |
| 714 | public WallpaperInfo getWallpaperInfo() { |
| 715 | try { |
Mike Lockwood | c067c9c | 2011-10-31 12:50:12 -0400 | [diff] [blame] | 716 | if (sGlobals.mService == null) { |
| 717 | Log.w(TAG, "WallpaperService not running"); |
| 718 | return null; |
| 719 | } else { |
| 720 | return sGlobals.mService.getWallpaperInfo(); |
| 721 | } |
Dianne Hackborn | eb03465 | 2009-09-07 00:49:58 -0700 | [diff] [blame] | 722 | } catch (RemoteException e) { |
| 723 | return null; |
| 724 | } |
| 725 | } |
Michael Jurka | e8d1bf7 | 2013-09-09 15:58:54 +0200 | [diff] [blame] | 726 | |
| 727 | /** |
| 728 | * Gets an Intent that will launch an activity that crops the given |
| 729 | * image and sets the device's wallpaper. If there is a default HOME activity |
| 730 | * that supports cropping wallpapers, it will be preferred as the default. |
Ying Wang | 930d4e5 | 2013-09-14 11:57:17 -0700 | [diff] [blame] | 731 | * Use this method instead of directly creating a {@link #ACTION_CROP_AND_SET_WALLPAPER} |
Michael Jurka | e8d1bf7 | 2013-09-09 15:58:54 +0200 | [diff] [blame] | 732 | * intent. |
Adam Lesinski | bba72d1 | 2013-10-11 18:10:56 -0700 | [diff] [blame] | 733 | * |
| 734 | * @param imageUri The image URI that will be set in the intent. The must be a content |
| 735 | * URI and its provider must resolve its type to "image/*" |
| 736 | * |
| 737 | * @throws IllegalArgumentException if the URI is not a content URI or its MIME type is |
| 738 | * not "image/*" |
Michael Jurka | e8d1bf7 | 2013-09-09 15:58:54 +0200 | [diff] [blame] | 739 | */ |
| 740 | public Intent getCropAndSetWallpaperIntent(Uri imageUri) { |
Allen Shen | 1c0f21e | 2014-04-29 16:16:29 +0800 | [diff] [blame] | 741 | if (imageUri == null) { |
| 742 | throw new IllegalArgumentException("Image URI must not be null"); |
| 743 | } |
| 744 | |
Adam Lesinski | bba72d1 | 2013-10-11 18:10:56 -0700 | [diff] [blame] | 745 | if (!ContentResolver.SCHEME_CONTENT.equals(imageUri.getScheme())) { |
| 746 | throw new IllegalArgumentException("Image URI must be of the " |
| 747 | + ContentResolver.SCHEME_CONTENT + " scheme type"); |
| 748 | } |
| 749 | |
Michael Jurka | e8d1bf7 | 2013-09-09 15:58:54 +0200 | [diff] [blame] | 750 | final PackageManager packageManager = mContext.getPackageManager(); |
| 751 | Intent cropAndSetWallpaperIntent = |
| 752 | new Intent(ACTION_CROP_AND_SET_WALLPAPER, imageUri); |
| 753 | cropAndSetWallpaperIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); |
| 754 | |
| 755 | // Find out if the default HOME activity supports CROP_AND_SET_WALLPAPER |
| 756 | Intent homeIntent = new Intent(Intent.ACTION_MAIN).addCategory(Intent.CATEGORY_HOME); |
| 757 | ResolveInfo resolvedHome = packageManager.resolveActivity(homeIntent, |
| 758 | PackageManager.MATCH_DEFAULT_ONLY); |
| 759 | if (resolvedHome != null) { |
| 760 | cropAndSetWallpaperIntent.setPackage(resolvedHome.activityInfo.packageName); |
| 761 | |
| 762 | List<ResolveInfo> cropAppList = packageManager.queryIntentActivities( |
| 763 | cropAndSetWallpaperIntent, 0); |
| 764 | if (cropAppList.size() > 0) { |
| 765 | return cropAndSetWallpaperIntent; |
| 766 | } |
| 767 | } |
| 768 | |
| 769 | // fallback crop activity |
Oren Blasberg | 60598ea0a | 2016-02-26 11:01:23 -0800 | [diff] [blame^] | 770 | final String cropperPackage = mContext.getString( |
| 771 | com.android.internal.R.string.config_wallpaperCropperPackage); |
| 772 | cropAndSetWallpaperIntent.setPackage(cropperPackage); |
Adam Lesinski | bba72d1 | 2013-10-11 18:10:56 -0700 | [diff] [blame] | 773 | List<ResolveInfo> cropAppList = packageManager.queryIntentActivities( |
| 774 | cropAndSetWallpaperIntent, 0); |
| 775 | if (cropAppList.size() > 0) { |
| 776 | return cropAndSetWallpaperIntent; |
| 777 | } |
| 778 | // If the URI is not of the right type, or for some reason the system wallpaper |
| 779 | // cropper doesn't exist, return null |
| 780 | throw new IllegalArgumentException("Cannot use passed URI to set wallpaper; " + |
| 781 | "check that the type returned by ContentProvider matches image/*"); |
Michael Jurka | e8d1bf7 | 2013-09-09 15:58:54 +0200 | [diff] [blame] | 782 | } |
| 783 | |
Dianne Hackborn | eb03465 | 2009-09-07 00:49:58 -0700 | [diff] [blame] | 784 | /** |
Dianne Hackborn | 8cc6a50 | 2009-08-05 21:29:42 -0700 | [diff] [blame] | 785 | * Change the current system wallpaper to the bitmap in the given resource. |
| 786 | * The resource is opened as a raw data stream and copied into the |
| 787 | * wallpaper; it must be a valid PNG or JPEG image. On success, the intent |
| 788 | * {@link Intent#ACTION_WALLPAPER_CHANGED} is broadcast. |
| 789 | * |
Nicolas Falliere | 9530e3a | 2012-06-18 17:21:06 -0700 | [diff] [blame] | 790 | * <p>This method requires the caller to hold the permission |
| 791 | * {@link android.Manifest.permission#SET_WALLPAPER}. |
| 792 | * |
Dianne Hackborn | 8cc6a50 | 2009-08-05 21:29:42 -0700 | [diff] [blame] | 793 | * @param resid The bitmap to save. |
| 794 | * |
Michael Jurka | b668d0b | 2013-10-04 15:11:05 -0700 | [diff] [blame] | 795 | * @throws IOException If an error occurs reverting to the built-in |
Dianne Hackborn | 8cc6a50 | 2009-08-05 21:29:42 -0700 | [diff] [blame] | 796 | * wallpaper. |
| 797 | */ |
Tor Norbye | 7b9c912 | 2013-05-30 16:48:33 -0700 | [diff] [blame] | 798 | public void setResource(@RawRes int resid) throws IOException { |
Christopher Tate | ad3c259 | 2016-01-20 18:13:17 -0800 | [diff] [blame] | 799 | setResource(resid, FLAG_SET_SYSTEM); |
| 800 | } |
| 801 | |
| 802 | /** |
| 803 | * Version of {@link #setResource(int)} that takes an optional Bundle for returning |
| 804 | * metadata about the operation to the caller. |
| 805 | * |
| 806 | * @param resid |
| 807 | * @param which Flags indicating which wallpaper(s) to configure with the new imagery. |
| 808 | * |
| 809 | * @see #FLAG_SET_LOCK |
| 810 | * @see #FLAG_SET_SYSTEM |
| 811 | * |
| 812 | * @return An integer ID assigned to the newly active wallpaper; or zero on failure. |
| 813 | * |
| 814 | * @throws IOException |
| 815 | */ |
| 816 | public int setResource(@RawRes int resid, @SetWallpaperFlags int which) |
| 817 | throws IOException { |
Mike Lockwood | c067c9c | 2011-10-31 12:50:12 -0400 | [diff] [blame] | 818 | if (sGlobals.mService == null) { |
| 819 | Log.w(TAG, "WallpaperService not running"); |
Christopher Tate | ad3c259 | 2016-01-20 18:13:17 -0800 | [diff] [blame] | 820 | return 0; |
Mike Lockwood | c067c9c | 2011-10-31 12:50:12 -0400 | [diff] [blame] | 821 | } |
Christopher Tate | ad3c259 | 2016-01-20 18:13:17 -0800 | [diff] [blame] | 822 | final Bundle result = new Bundle(); |
Christopher Tate | 1e1e2e0 | 2016-01-25 15:34:36 -0800 | [diff] [blame] | 823 | final WallpaperSetCompletion completion = new WallpaperSetCompletion(); |
Dianne Hackborn | 8cc6a50 | 2009-08-05 21:29:42 -0700 | [diff] [blame] | 824 | try { |
| 825 | Resources resources = mContext.getResources(); |
| 826 | /* Set the wallpaper to the default values */ |
Dianne Hackborn | 840c3a2 | 2009-09-02 17:35:17 -0700 | [diff] [blame] | 827 | ParcelFileDescriptor fd = sGlobals.mService.setWallpaper( |
Christopher Tate | ad3c259 | 2016-01-20 18:13:17 -0800 | [diff] [blame] | 828 | "res:" + resources.getResourceName(resid), |
Christopher Tate | 1e1e2e0 | 2016-01-25 15:34:36 -0800 | [diff] [blame] | 829 | mContext.getOpPackageName(), null, result, which, completion); |
Dianne Hackborn | 8cc6a50 | 2009-08-05 21:29:42 -0700 | [diff] [blame] | 830 | if (fd != null) { |
| 831 | FileOutputStream fos = null; |
Christopher Tate | 1e1e2e0 | 2016-01-25 15:34:36 -0800 | [diff] [blame] | 832 | boolean ok = false; |
Dianne Hackborn | 8cc6a50 | 2009-08-05 21:29:42 -0700 | [diff] [blame] | 833 | try { |
| 834 | fos = new ParcelFileDescriptor.AutoCloseOutputStream(fd); |
Christopher Tate | ad3c259 | 2016-01-20 18:13:17 -0800 | [diff] [blame] | 835 | copyStreamToWallpaperFile(resources.openRawResource(resid), fos); |
Christopher Tate | 1e1e2e0 | 2016-01-25 15:34:36 -0800 | [diff] [blame] | 836 | // The 'close()' is the trigger for any server-side image manipulation, |
| 837 | // so we must do that before waiting for completion. |
| 838 | fos.close(); |
| 839 | completion.waitForCompletion(); |
Dianne Hackborn | 8cc6a50 | 2009-08-05 21:29:42 -0700 | [diff] [blame] | 840 | } finally { |
Christopher Tate | 1e1e2e0 | 2016-01-25 15:34:36 -0800 | [diff] [blame] | 841 | // Might be redundant but completion shouldn't wait unless the write |
| 842 | // succeeded; this is a fallback if it threw past the close+wait. |
Christopher Tate | ffa6a88 | 2015-12-11 15:08:46 -0800 | [diff] [blame] | 843 | IoUtils.closeQuietly(fos); |
Dianne Hackborn | 8cc6a50 | 2009-08-05 21:29:42 -0700 | [diff] [blame] | 844 | } |
| 845 | } |
| 846 | } catch (RemoteException e) { |
Romain Guy | 407ec78 | 2011-08-24 17:06:58 -0700 | [diff] [blame] | 847 | // Ignore |
Dianne Hackborn | 8cc6a50 | 2009-08-05 21:29:42 -0700 | [diff] [blame] | 848 | } |
Christopher Tate | ad3c259 | 2016-01-20 18:13:17 -0800 | [diff] [blame] | 849 | return result.getInt(EXTRA_NEW_WALLPAPER_ID, 0); |
Dianne Hackborn | 8cc6a50 | 2009-08-05 21:29:42 -0700 | [diff] [blame] | 850 | } |
Christopher Tate | ffa6a88 | 2015-12-11 15:08:46 -0800 | [diff] [blame] | 851 | |
Dianne Hackborn | 8cc6a50 | 2009-08-05 21:29:42 -0700 | [diff] [blame] | 852 | /** |
| 853 | * Change the current system wallpaper to a bitmap. The given bitmap is |
| 854 | * converted to a PNG and stored as the wallpaper. On success, the intent |
| 855 | * {@link Intent#ACTION_WALLPAPER_CHANGED} is broadcast. |
| 856 | * |
Christopher Tate | ffa6a88 | 2015-12-11 15:08:46 -0800 | [diff] [blame] | 857 | * <p>This method is equivalent to calling |
| 858 | * {@link #setBitmap(Bitmap, Rect, boolean)} and passing {@code null} for the |
| 859 | * {@code visibleCrop} rectangle and {@code true} for the {@code allowBackup} |
| 860 | * parameter. |
| 861 | * |
Nicolas Falliere | 9530e3a | 2012-06-18 17:21:06 -0700 | [diff] [blame] | 862 | * <p>This method requires the caller to hold the permission |
| 863 | * {@link android.Manifest.permission#SET_WALLPAPER}. |
| 864 | * |
Christopher Tate | ad3c259 | 2016-01-20 18:13:17 -0800 | [diff] [blame] | 865 | * @param bitmap The bitmap to be used as the new system wallpaper. |
Dianne Hackborn | 8cc6a50 | 2009-08-05 21:29:42 -0700 | [diff] [blame] | 866 | * |
Christopher Tate | ffa6a88 | 2015-12-11 15:08:46 -0800 | [diff] [blame] | 867 | * @throws IOException If an error occurs when attempting to set the wallpaper |
| 868 | * to the provided image. |
Dianne Hackborn | 8cc6a50 | 2009-08-05 21:29:42 -0700 | [diff] [blame] | 869 | */ |
Dianne Hackborn | 4c62fc0 | 2009-08-08 20:40:27 -0700 | [diff] [blame] | 870 | public void setBitmap(Bitmap bitmap) throws IOException { |
Christopher Tate | ffa6a88 | 2015-12-11 15:08:46 -0800 | [diff] [blame] | 871 | setBitmap(bitmap, null, true); |
| 872 | } |
| 873 | |
| 874 | /** |
| 875 | * Change the current system wallpaper to a bitmap, specifying a hint about |
| 876 | * which subrectangle of the full image is to be visible. The OS will then |
| 877 | * try to best present the given portion of the full image as the static system |
| 878 | * wallpaper image. On success, the intent |
| 879 | * {@link Intent#ACTION_WALLPAPER_CHANGED} is broadcast. |
| 880 | * |
| 881 | * <p>Passing {@code null} as the {@code visibleHint} parameter is equivalent to |
| 882 | * passing (0, 0, {@code fullImage.getWidth()}, {@code fullImage.getHeight()}). |
| 883 | * |
| 884 | * <p>This method requires the caller to hold the permission |
| 885 | * {@link android.Manifest.permission#SET_WALLPAPER}. |
| 886 | * |
Christopher Tate | ad3c259 | 2016-01-20 18:13:17 -0800 | [diff] [blame] | 887 | * @param fullImage A bitmap that will supply the wallpaper imagery. |
Christopher Tate | ffa6a88 | 2015-12-11 15:08:46 -0800 | [diff] [blame] | 888 | * @param visibleCropHint The rectangular subregion of {@code fullImage} that should be |
| 889 | * displayed as wallpaper. Passing {@code null} for this parameter means that |
| 890 | * the full image should be displayed if possible given the image's and device's |
Christopher Tate | ad3c259 | 2016-01-20 18:13:17 -0800 | [diff] [blame] | 891 | * aspect ratios, etc. |
Christopher Tate | ffa6a88 | 2015-12-11 15:08:46 -0800 | [diff] [blame] | 892 | * @param allowBackup {@code true} if the OS is permitted to back up this wallpaper |
| 893 | * image for restore to a future device; {@code false} otherwise. |
| 894 | * |
Christopher Tate | ad3c259 | 2016-01-20 18:13:17 -0800 | [diff] [blame] | 895 | * @return An integer ID assigned to the newly active wallpaper; or zero on failure. |
| 896 | * |
Christopher Tate | ffa6a88 | 2015-12-11 15:08:46 -0800 | [diff] [blame] | 897 | * @throws IOException If an error occurs when attempting to set the wallpaper |
| 898 | * to the provided image. |
| 899 | * @throws IllegalArgumentException If the {@code visibleCropHint} rectangle is |
| 900 | * empty or invalid. |
| 901 | */ |
Christopher Tate | ad3c259 | 2016-01-20 18:13:17 -0800 | [diff] [blame] | 902 | public int setBitmap(Bitmap fullImage, Rect visibleCropHint, boolean allowBackup) |
| 903 | throws IOException { |
| 904 | return setBitmap(fullImage, visibleCropHint, allowBackup, FLAG_SET_SYSTEM); |
| 905 | } |
| 906 | |
| 907 | /** |
| 908 | /** |
| 909 | * Version of {@link #setBitmap(Bitmap, Rect, boolean)} that allows the caller |
| 910 | * to specify which of the supported wallpaper categories to set. |
| 911 | * |
| 912 | * @param fullImage A bitmap that will supply the wallpaper imagery. |
| 913 | * @param visibleCropHint The rectangular subregion of {@code fullImage} that should be |
| 914 | * displayed as wallpaper. Passing {@code null} for this parameter means that |
| 915 | * the full image should be displayed if possible given the image's and device's |
| 916 | * aspect ratios, etc. |
| 917 | * @param allowBackup {@code true} if the OS is permitted to back up this wallpaper |
| 918 | * image for restore to a future device; {@code false} otherwise. |
| 919 | * @param which Flags indicating which wallpaper(s) to configure with the new imagery. |
| 920 | * |
| 921 | * @see #FLAG_SET_LOCK_WALLPAPER |
| 922 | * @see #FLAG_SET_SYSTEM_WALLPAPER |
| 923 | * |
| 924 | * @return An integer ID assigned to the newly active wallpaper; or zero on failure. |
| 925 | * |
| 926 | * @throws IOException |
| 927 | */ |
| 928 | public int setBitmap(Bitmap fullImage, Rect visibleCropHint, |
| 929 | boolean allowBackup, @SetWallpaperFlags int which) |
Christopher Tate | ffa6a88 | 2015-12-11 15:08:46 -0800 | [diff] [blame] | 930 | throws IOException { |
| 931 | validateRect(visibleCropHint); |
Mike Lockwood | c067c9c | 2011-10-31 12:50:12 -0400 | [diff] [blame] | 932 | if (sGlobals.mService == null) { |
| 933 | Log.w(TAG, "WallpaperService not running"); |
Christopher Tate | ad3c259 | 2016-01-20 18:13:17 -0800 | [diff] [blame] | 934 | return 0; |
Mike Lockwood | c067c9c | 2011-10-31 12:50:12 -0400 | [diff] [blame] | 935 | } |
Christopher Tate | ad3c259 | 2016-01-20 18:13:17 -0800 | [diff] [blame] | 936 | final Bundle result = new Bundle(); |
Christopher Tate | 1e1e2e0 | 2016-01-25 15:34:36 -0800 | [diff] [blame] | 937 | final WallpaperSetCompletion completion = new WallpaperSetCompletion(); |
Dianne Hackborn | 8cc6a50 | 2009-08-05 21:29:42 -0700 | [diff] [blame] | 938 | try { |
Benjamin Franz | f3ece36 | 2015-02-11 10:51:10 +0000 | [diff] [blame] | 939 | ParcelFileDescriptor fd = sGlobals.mService.setWallpaper(null, |
Christopher Tate | 1e1e2e0 | 2016-01-25 15:34:36 -0800 | [diff] [blame] | 940 | mContext.getOpPackageName(), visibleCropHint, result, which, completion); |
Christopher Tate | ad3c259 | 2016-01-20 18:13:17 -0800 | [diff] [blame] | 941 | if (fd != null) { |
| 942 | FileOutputStream fos = null; |
| 943 | try { |
| 944 | fos = new ParcelFileDescriptor.AutoCloseOutputStream(fd); |
| 945 | fullImage.compress(Bitmap.CompressFormat.PNG, 90, fos); |
Christopher Tate | 1e1e2e0 | 2016-01-25 15:34:36 -0800 | [diff] [blame] | 946 | fos.close(); |
| 947 | completion.waitForCompletion(); |
Christopher Tate | ad3c259 | 2016-01-20 18:13:17 -0800 | [diff] [blame] | 948 | } finally { |
| 949 | IoUtils.closeQuietly(fos); |
| 950 | } |
Dianne Hackborn | 8cc6a50 | 2009-08-05 21:29:42 -0700 | [diff] [blame] | 951 | } |
| 952 | } catch (RemoteException e) { |
Romain Guy | 407ec78 | 2011-08-24 17:06:58 -0700 | [diff] [blame] | 953 | // Ignore |
Dianne Hackborn | 8cc6a50 | 2009-08-05 21:29:42 -0700 | [diff] [blame] | 954 | } |
Christopher Tate | ad3c259 | 2016-01-20 18:13:17 -0800 | [diff] [blame] | 955 | return result.getInt(EXTRA_NEW_WALLPAPER_ID, 0); |
Dianne Hackborn | 8cc6a50 | 2009-08-05 21:29:42 -0700 | [diff] [blame] | 956 | } |
| 957 | |
Christopher Tate | ffa6a88 | 2015-12-11 15:08:46 -0800 | [diff] [blame] | 958 | private final void validateRect(Rect rect) { |
| 959 | if (rect != null && rect.isEmpty()) { |
| 960 | throw new IllegalArgumentException("visibleCrop rectangle must be valid and non-empty"); |
| 961 | } |
| 962 | } |
| 963 | |
Dianne Hackborn | 8cc6a50 | 2009-08-05 21:29:42 -0700 | [diff] [blame] | 964 | /** |
| 965 | * Change the current system wallpaper to a specific byte stream. The |
| 966 | * give InputStream is copied into persistent storage and will now be |
| 967 | * used as the wallpaper. Currently it must be either a JPEG or PNG |
| 968 | * image. On success, the intent {@link Intent#ACTION_WALLPAPER_CHANGED} |
| 969 | * is broadcast. |
| 970 | * |
Christopher Tate | ffa6a88 | 2015-12-11 15:08:46 -0800 | [diff] [blame] | 971 | * <p>This method is equivalent to calling |
| 972 | * {@link #setStream(InputStream, Rect, boolean)} and passing {@code null} for the |
| 973 | * {@code visibleCrop} rectangle and {@code true} for the {@code allowBackup} |
| 974 | * parameter. |
| 975 | * |
Nicolas Falliere | 9530e3a | 2012-06-18 17:21:06 -0700 | [diff] [blame] | 976 | * <p>This method requires the caller to hold the permission |
| 977 | * {@link android.Manifest.permission#SET_WALLPAPER}. |
| 978 | * |
Christopher Tate | ffa6a88 | 2015-12-11 15:08:46 -0800 | [diff] [blame] | 979 | * @param bitmapData A stream containing the raw data to install as a wallpaper. |
Dianne Hackborn | 8cc6a50 | 2009-08-05 21:29:42 -0700 | [diff] [blame] | 980 | * |
Christopher Tate | ffa6a88 | 2015-12-11 15:08:46 -0800 | [diff] [blame] | 981 | * @throws IOException If an error occurs when attempting to set the wallpaper |
| 982 | * based on the provided image data. |
Dianne Hackborn | 8cc6a50 | 2009-08-05 21:29:42 -0700 | [diff] [blame] | 983 | */ |
Christopher Tate | ffa6a88 | 2015-12-11 15:08:46 -0800 | [diff] [blame] | 984 | public void setStream(InputStream bitmapData) throws IOException { |
| 985 | setStream(bitmapData, null, true); |
| 986 | } |
| 987 | |
Christopher Tate | ad3c259 | 2016-01-20 18:13:17 -0800 | [diff] [blame] | 988 | private void copyStreamToWallpaperFile(InputStream data, FileOutputStream fos) |
Christopher Tate | ffa6a88 | 2015-12-11 15:08:46 -0800 | [diff] [blame] | 989 | throws IOException { |
| 990 | byte[] buffer = new byte[32768]; |
| 991 | int amt; |
| 992 | while ((amt=data.read(buffer)) > 0) { |
| 993 | fos.write(buffer, 0, amt); |
| 994 | } |
| 995 | } |
| 996 | |
| 997 | /** |
| 998 | * Change the current system wallpaper to a specific byte stream, specifying a |
| 999 | * hint about which subrectangle of the full image is to be visible. The OS will |
| 1000 | * then try to best present the given portion of the full image as the static system |
| 1001 | * wallpaper image. The data from the given InputStream is copied into persistent |
| 1002 | * storage and will then be used as the system wallpaper. Currently the data must |
| 1003 | * be either a JPEG or PNG image. On success, the intent |
| 1004 | * {@link Intent#ACTION_WALLPAPER_CHANGED} is broadcast. |
| 1005 | * |
| 1006 | * <p>This method requires the caller to hold the permission |
| 1007 | * {@link android.Manifest.permission#SET_WALLPAPER}. |
| 1008 | * |
| 1009 | * @param bitmapData A stream containing the raw data to install as a wallpaper. |
| 1010 | * @param visibleCropHint The rectangular subregion of the streamed image that should be |
| 1011 | * displayed as wallpaper. Passing {@code null} for this parameter means that |
| 1012 | * the full image should be displayed if possible given the image's and device's |
| 1013 | * aspect ratios, etc. |
| 1014 | * @param allowBackup {@code true} if the OS is permitted to back up this wallpaper |
| 1015 | * image for restore to a future device; {@code false} otherwise. |
| 1016 | * |
| 1017 | * @throws IOException If an error occurs when attempting to set the wallpaper |
| 1018 | * based on the provided image data. |
| 1019 | * @throws IllegalArgumentException If the {@code visibleCropHint} rectangle is |
| 1020 | * empty or invalid. |
| 1021 | */ |
Christopher Tate | ad3c259 | 2016-01-20 18:13:17 -0800 | [diff] [blame] | 1022 | public int setStream(InputStream bitmapData, Rect visibleCropHint, boolean allowBackup) |
Christopher Tate | ffa6a88 | 2015-12-11 15:08:46 -0800 | [diff] [blame] | 1023 | throws IOException { |
Christopher Tate | ad3c259 | 2016-01-20 18:13:17 -0800 | [diff] [blame] | 1024 | return setStream(bitmapData, visibleCropHint, allowBackup, FLAG_SET_SYSTEM); |
| 1025 | } |
| 1026 | |
| 1027 | /** |
| 1028 | * Version of {@link #setStream(InputStream, Rect, boolean)} that allows the caller |
| 1029 | * to specify which of the supported wallpaper categories to set. |
| 1030 | * |
| 1031 | * @param bitmapData A stream containing the raw data to install as a wallpaper. |
| 1032 | * @param visibleCropHint The rectangular subregion of the streamed image that should be |
| 1033 | * displayed as wallpaper. Passing {@code null} for this parameter means that |
| 1034 | * the full image should be displayed if possible given the image's and device's |
| 1035 | * aspect ratios, etc. |
| 1036 | * @param allowBackup {@code true} if the OS is permitted to back up this wallpaper |
| 1037 | * image for restore to a future device; {@code false} otherwise. |
| 1038 | * @param which Flags indicating which wallpaper(s) to configure with the new imagery. |
| 1039 | * |
| 1040 | * @see #FLAG_SET_LOCK_WALLPAPER |
| 1041 | * @see #FLAG_SET_SYSTEM_WALLPAPER |
| 1042 | * |
| 1043 | * @throws IOException |
| 1044 | */ |
| 1045 | public int setStream(InputStream bitmapData, Rect visibleCropHint, |
| 1046 | boolean allowBackup, @SetWallpaperFlags int which) |
| 1047 | throws IOException { |
Christopher Tate | ffa6a88 | 2015-12-11 15:08:46 -0800 | [diff] [blame] | 1048 | validateRect(visibleCropHint); |
Mike Lockwood | c067c9c | 2011-10-31 12:50:12 -0400 | [diff] [blame] | 1049 | if (sGlobals.mService == null) { |
| 1050 | Log.w(TAG, "WallpaperService not running"); |
Christopher Tate | ad3c259 | 2016-01-20 18:13:17 -0800 | [diff] [blame] | 1051 | return 0; |
Mike Lockwood | c067c9c | 2011-10-31 12:50:12 -0400 | [diff] [blame] | 1052 | } |
Christopher Tate | ad3c259 | 2016-01-20 18:13:17 -0800 | [diff] [blame] | 1053 | final Bundle result = new Bundle(); |
Christopher Tate | 1e1e2e0 | 2016-01-25 15:34:36 -0800 | [diff] [blame] | 1054 | final WallpaperSetCompletion completion = new WallpaperSetCompletion(); |
Dianne Hackborn | 8cc6a50 | 2009-08-05 21:29:42 -0700 | [diff] [blame] | 1055 | try { |
Benjamin Franz | f3ece36 | 2015-02-11 10:51:10 +0000 | [diff] [blame] | 1056 | ParcelFileDescriptor fd = sGlobals.mService.setWallpaper(null, |
Christopher Tate | 1e1e2e0 | 2016-01-25 15:34:36 -0800 | [diff] [blame] | 1057 | mContext.getOpPackageName(), visibleCropHint, result, which, completion); |
Christopher Tate | ad3c259 | 2016-01-20 18:13:17 -0800 | [diff] [blame] | 1058 | if (fd != null) { |
| 1059 | FileOutputStream fos = null; |
| 1060 | try { |
| 1061 | fos = new ParcelFileDescriptor.AutoCloseOutputStream(fd); |
| 1062 | copyStreamToWallpaperFile(bitmapData, fos); |
Christopher Tate | 1e1e2e0 | 2016-01-25 15:34:36 -0800 | [diff] [blame] | 1063 | fos.close(); |
| 1064 | completion.waitForCompletion(); |
Christopher Tate | ad3c259 | 2016-01-20 18:13:17 -0800 | [diff] [blame] | 1065 | } finally { |
| 1066 | IoUtils.closeQuietly(fos); |
| 1067 | } |
Dianne Hackborn | 8cc6a50 | 2009-08-05 21:29:42 -0700 | [diff] [blame] | 1068 | } |
| 1069 | } catch (RemoteException e) { |
Romain Guy | 407ec78 | 2011-08-24 17:06:58 -0700 | [diff] [blame] | 1070 | // Ignore |
Dianne Hackborn | 8cc6a50 | 2009-08-05 21:29:42 -0700 | [diff] [blame] | 1071 | } |
Christopher Tate | ad3c259 | 2016-01-20 18:13:17 -0800 | [diff] [blame] | 1072 | |
| 1073 | return result.getInt(EXTRA_NEW_WALLPAPER_ID, 0); |
Dianne Hackborn | 8cc6a50 | 2009-08-05 21:29:42 -0700 | [diff] [blame] | 1074 | } |
| 1075 | |
Dianne Hackborn | 8cc6a50 | 2009-08-05 21:29:42 -0700 | [diff] [blame] | 1076 | /** |
Dianne Hackborn | 5dc5a00 | 2012-09-15 19:33:48 -0700 | [diff] [blame] | 1077 | * Return whether any users are currently set to use the wallpaper |
| 1078 | * with the given resource ID. That is, their wallpaper has been |
| 1079 | * set through {@link #setResource(int)} with the same resource id. |
| 1080 | */ |
Tor Norbye | 7b9c912 | 2013-05-30 16:48:33 -0700 | [diff] [blame] | 1081 | public boolean hasResourceWallpaper(@RawRes int resid) { |
Dianne Hackborn | 5dc5a00 | 2012-09-15 19:33:48 -0700 | [diff] [blame] | 1082 | if (sGlobals.mService == null) { |
| 1083 | Log.w(TAG, "WallpaperService not running"); |
| 1084 | return false; |
| 1085 | } |
| 1086 | try { |
| 1087 | Resources resources = mContext.getResources(); |
| 1088 | String name = "res:" + resources.getResourceName(resid); |
| 1089 | return sGlobals.mService.hasNamedWallpaper(name); |
| 1090 | } catch (RemoteException e) { |
| 1091 | return false; |
| 1092 | } |
| 1093 | } |
| 1094 | |
| 1095 | /** |
Dianne Hackborn | 8cc6a50 | 2009-08-05 21:29:42 -0700 | [diff] [blame] | 1096 | * Returns the desired minimum width for the wallpaper. Callers of |
Dianne Hackborn | 4c62fc0 | 2009-08-08 20:40:27 -0700 | [diff] [blame] | 1097 | * {@link #setBitmap(android.graphics.Bitmap)} or |
| 1098 | * {@link #setStream(java.io.InputStream)} should check this value |
Dianne Hackborn | 8cc6a50 | 2009-08-05 21:29:42 -0700 | [diff] [blame] | 1099 | * beforehand to make sure the supplied wallpaper respects the desired |
| 1100 | * minimum width. |
| 1101 | * |
| 1102 | * If the returned value is <= 0, the caller should use the width of |
| 1103 | * the default display instead. |
| 1104 | * |
| 1105 | * @return The desired minimum width for the wallpaper. This value should |
| 1106 | * be honored by applications that set the wallpaper but it is not |
| 1107 | * mandatory. |
| 1108 | */ |
| 1109 | public int getDesiredMinimumWidth() { |
Mike Lockwood | c067c9c | 2011-10-31 12:50:12 -0400 | [diff] [blame] | 1110 | if (sGlobals.mService == null) { |
| 1111 | Log.w(TAG, "WallpaperService not running"); |
| 1112 | return 0; |
| 1113 | } |
Dianne Hackborn | 8cc6a50 | 2009-08-05 21:29:42 -0700 | [diff] [blame] | 1114 | try { |
Dianne Hackborn | 840c3a2 | 2009-09-02 17:35:17 -0700 | [diff] [blame] | 1115 | return sGlobals.mService.getWidthHint(); |
Dianne Hackborn | 8cc6a50 | 2009-08-05 21:29:42 -0700 | [diff] [blame] | 1116 | } catch (RemoteException e) { |
| 1117 | // Shouldn't happen! |
| 1118 | return 0; |
| 1119 | } |
| 1120 | } |
| 1121 | |
| 1122 | /** |
| 1123 | * Returns the desired minimum height for the wallpaper. Callers of |
Dianne Hackborn | 4c62fc0 | 2009-08-08 20:40:27 -0700 | [diff] [blame] | 1124 | * {@link #setBitmap(android.graphics.Bitmap)} or |
| 1125 | * {@link #setStream(java.io.InputStream)} should check this value |
Dianne Hackborn | 8cc6a50 | 2009-08-05 21:29:42 -0700 | [diff] [blame] | 1126 | * beforehand to make sure the supplied wallpaper respects the desired |
| 1127 | * minimum height. |
| 1128 | * |
| 1129 | * If the returned value is <= 0, the caller should use the height of |
| 1130 | * the default display instead. |
| 1131 | * |
| 1132 | * @return The desired minimum height for the wallpaper. This value should |
| 1133 | * be honored by applications that set the wallpaper but it is not |
| 1134 | * mandatory. |
| 1135 | */ |
| 1136 | public int getDesiredMinimumHeight() { |
Mike Lockwood | c067c9c | 2011-10-31 12:50:12 -0400 | [diff] [blame] | 1137 | if (sGlobals.mService == null) { |
| 1138 | Log.w(TAG, "WallpaperService not running"); |
| 1139 | return 0; |
| 1140 | } |
Dianne Hackborn | 8cc6a50 | 2009-08-05 21:29:42 -0700 | [diff] [blame] | 1141 | try { |
Dianne Hackborn | 840c3a2 | 2009-09-02 17:35:17 -0700 | [diff] [blame] | 1142 | return sGlobals.mService.getHeightHint(); |
Dianne Hackborn | 8cc6a50 | 2009-08-05 21:29:42 -0700 | [diff] [blame] | 1143 | } catch (RemoteException e) { |
| 1144 | // Shouldn't happen! |
| 1145 | return 0; |
| 1146 | } |
| 1147 | } |
| 1148 | |
| 1149 | /** |
| 1150 | * For use only by the current home application, to specify the size of |
| 1151 | * wallpaper it would like to use. This allows such applications to have |
| 1152 | * a virtual wallpaper that is larger than the physical screen, matching |
| 1153 | * the size of their workspace. |
Dianne Hackborn | c5bf758 | 2012-04-25 19:12:07 -0700 | [diff] [blame] | 1154 | * |
| 1155 | * <p>Note developers, who don't seem to be reading this. This is |
Christopher Tate | ffa6a88 | 2015-12-11 15:08:46 -0800 | [diff] [blame] | 1156 | * for <em>home apps</em> to tell what size wallpaper they would like. |
| 1157 | * Nobody else should be calling this! Certainly not other non-home |
Dianne Hackborn | c5bf758 | 2012-04-25 19:12:07 -0700 | [diff] [blame] | 1158 | * apps that change the wallpaper. Those apps are supposed to |
| 1159 | * <b>retrieve</b> the suggested size so they can construct a wallpaper |
| 1160 | * that matches it. |
| 1161 | * |
Nicolas Falliere | 9530e3a | 2012-06-18 17:21:06 -0700 | [diff] [blame] | 1162 | * <p>This method requires the caller to hold the permission |
| 1163 | * {@link android.Manifest.permission#SET_WALLPAPER_HINTS}. |
| 1164 | * |
Dianne Hackborn | 8cc6a50 | 2009-08-05 21:29:42 -0700 | [diff] [blame] | 1165 | * @param minimumWidth Desired minimum width |
| 1166 | * @param minimumHeight Desired minimum height |
| 1167 | */ |
Dianne Hackborn | 4c62fc0 | 2009-08-08 20:40:27 -0700 | [diff] [blame] | 1168 | public void suggestDesiredDimensions(int minimumWidth, int minimumHeight) { |
Dianne Hackborn | 8cc6a50 | 2009-08-05 21:29:42 -0700 | [diff] [blame] | 1169 | try { |
Donghan Ryu | 289c273 | 2011-11-14 18:56:11 -0800 | [diff] [blame] | 1170 | /** |
| 1171 | * The framework makes no attempt to limit the window size |
| 1172 | * to the maximum texture size. Any window larger than this |
| 1173 | * cannot be composited. |
| 1174 | * |
| 1175 | * Read maximum texture size from system property and scale down |
| 1176 | * minimumWidth and minimumHeight accordingly. |
| 1177 | */ |
| 1178 | int maximumTextureSize; |
| 1179 | try { |
| 1180 | maximumTextureSize = SystemProperties.getInt("sys.max_texture_size", 0); |
| 1181 | } catch (Exception e) { |
| 1182 | maximumTextureSize = 0; |
| 1183 | } |
| 1184 | |
| 1185 | if (maximumTextureSize > 0) { |
| 1186 | if ((minimumWidth > maximumTextureSize) || |
| 1187 | (minimumHeight > maximumTextureSize)) { |
| 1188 | float aspect = (float)minimumHeight / (float)minimumWidth; |
| 1189 | if (minimumWidth > minimumHeight) { |
| 1190 | minimumWidth = maximumTextureSize; |
| 1191 | minimumHeight = (int)((minimumWidth * aspect) + 0.5); |
| 1192 | } else { |
| 1193 | minimumHeight = maximumTextureSize; |
| 1194 | minimumWidth = (int)((minimumHeight / aspect) + 0.5); |
| 1195 | } |
| 1196 | } |
| 1197 | } |
| 1198 | |
Mike Lockwood | c067c9c | 2011-10-31 12:50:12 -0400 | [diff] [blame] | 1199 | if (sGlobals.mService == null) { |
| 1200 | Log.w(TAG, "WallpaperService not running"); |
| 1201 | } else { |
Benjamin Franz | f3ece36 | 2015-02-11 10:51:10 +0000 | [diff] [blame] | 1202 | sGlobals.mService.setDimensionHints(minimumWidth, minimumHeight, |
| 1203 | mContext.getOpPackageName()); |
Mike Lockwood | c067c9c | 2011-10-31 12:50:12 -0400 | [diff] [blame] | 1204 | } |
Dianne Hackborn | 8cc6a50 | 2009-08-05 21:29:42 -0700 | [diff] [blame] | 1205 | } catch (RemoteException e) { |
Romain Guy | 407ec78 | 2011-08-24 17:06:58 -0700 | [diff] [blame] | 1206 | // Ignore |
Dianne Hackborn | 8cc6a50 | 2009-08-05 21:29:42 -0700 | [diff] [blame] | 1207 | } |
| 1208 | } |
Adam Cohen | 041a0ba | 2011-11-09 20:10:27 -0800 | [diff] [blame] | 1209 | |
Dianne Hackborn | 8cc6a50 | 2009-08-05 21:29:42 -0700 | [diff] [blame] | 1210 | /** |
Dianne Hackborn | 067e5f6 | 2014-09-07 23:14:30 -0700 | [diff] [blame] | 1211 | * Specify extra padding that the wallpaper should have outside of the display. |
| 1212 | * That is, the given padding supplies additional pixels the wallpaper should extend |
| 1213 | * outside of the display itself. |
| 1214 | * @param padding The number of pixels the wallpaper should extend beyond the display, |
| 1215 | * on its left, top, right, and bottom sides. |
| 1216 | * @hide |
| 1217 | */ |
| 1218 | @SystemApi |
| 1219 | public void setDisplayPadding(Rect padding) { |
| 1220 | try { |
| 1221 | if (sGlobals.mService == null) { |
| 1222 | Log.w(TAG, "WallpaperService not running"); |
| 1223 | } else { |
Benjamin Franz | f3ece36 | 2015-02-11 10:51:10 +0000 | [diff] [blame] | 1224 | sGlobals.mService.setDisplayPadding(padding, mContext.getOpPackageName()); |
Dianne Hackborn | 067e5f6 | 2014-09-07 23:14:30 -0700 | [diff] [blame] | 1225 | } |
| 1226 | } catch (RemoteException e) { |
| 1227 | // Ignore |
| 1228 | } |
| 1229 | } |
| 1230 | |
| 1231 | /** |
| 1232 | * Apply a raw offset to the wallpaper window. Should only be used in |
| 1233 | * combination with {@link #setDisplayPadding(android.graphics.Rect)} when you |
| 1234 | * have ensured that the wallpaper will extend outside of the display area so that |
| 1235 | * it can be moved without leaving part of the display uncovered. |
| 1236 | * @param x The offset, in pixels, to apply to the left edge. |
| 1237 | * @param y The offset, in pixels, to apply to the top edge. |
| 1238 | * @hide |
| 1239 | */ |
| 1240 | @SystemApi |
| 1241 | public void setDisplayOffset(IBinder windowToken, int x, int y) { |
| 1242 | try { |
| 1243 | //Log.v(TAG, "Sending new wallpaper display offsets from app..."); |
| 1244 | WindowManagerGlobal.getWindowSession().setWallpaperDisplayOffset( |
| 1245 | windowToken, x, y); |
| 1246 | //Log.v(TAG, "...app returning after sending display offset!"); |
| 1247 | } catch (RemoteException e) { |
| 1248 | // Ignore. |
| 1249 | } |
| 1250 | } |
| 1251 | |
| 1252 | /** |
Filip Gruszczynski | 9fedc72 | 2015-01-28 15:59:46 -0800 | [diff] [blame] | 1253 | * Clear the wallpaper. |
| 1254 | * |
| 1255 | * @hide |
| 1256 | */ |
| 1257 | @SystemApi |
| 1258 | public void clearWallpaper() { |
Christopher Tate | be132e6 | 2016-02-10 12:59:49 -0800 | [diff] [blame] | 1259 | clearWallpaper(FLAG_SET_SYSTEM, mContext.getUserId()); |
| 1260 | } |
| 1261 | |
| 1262 | /** |
| 1263 | * Clear the wallpaper for a specific user. The caller must hold the |
| 1264 | * INTERACT_ACROSS_USERS_FULL permission to clear another user's |
| 1265 | * wallpaper. |
| 1266 | * @hide |
| 1267 | */ |
| 1268 | @SystemApi |
| 1269 | public void clearWallpaper(int which, int userId) { |
Filip Gruszczynski | 9fedc72 | 2015-01-28 15:59:46 -0800 | [diff] [blame] | 1270 | if (sGlobals.mService == null) { |
| 1271 | Log.w(TAG, "WallpaperService not running"); |
| 1272 | return; |
| 1273 | } |
| 1274 | try { |
Christopher Tate | be132e6 | 2016-02-10 12:59:49 -0800 | [diff] [blame] | 1275 | sGlobals.mService.clearWallpaper(mContext.getOpPackageName(), which, userId); |
Filip Gruszczynski | 9fedc72 | 2015-01-28 15:59:46 -0800 | [diff] [blame] | 1276 | } catch (RemoteException e) { |
| 1277 | // Ignore |
| 1278 | } |
| 1279 | } |
| 1280 | |
| 1281 | /** |
| 1282 | * Set the live wallpaper. |
| 1283 | * |
| 1284 | * This can only be called by packages with android.permission.SET_WALLPAPER_COMPONENT |
| 1285 | * permission. |
| 1286 | * |
| 1287 | * @hide |
| 1288 | */ |
| 1289 | @SystemApi |
| 1290 | public boolean setWallpaperComponent(ComponentName name) { |
| 1291 | if (sGlobals.mService == null) { |
| 1292 | Log.w(TAG, "WallpaperService not running"); |
| 1293 | return false; |
| 1294 | } |
| 1295 | try { |
Benjamin Franz | f3ece36 | 2015-02-11 10:51:10 +0000 | [diff] [blame] | 1296 | sGlobals.mService.setWallpaperComponentChecked(name, mContext.getOpPackageName()); |
Filip Gruszczynski | 9fedc72 | 2015-01-28 15:59:46 -0800 | [diff] [blame] | 1297 | return true; |
| 1298 | } catch (RemoteException e) { |
| 1299 | // Ignore |
| 1300 | } |
| 1301 | return false; |
| 1302 | } |
| 1303 | |
| 1304 | /** |
Christopher Tate | ffa6a88 | 2015-12-11 15:08:46 -0800 | [diff] [blame] | 1305 | * Set the display position of the current wallpaper within any larger space, when |
Dianne Hackborn | c8a0a75 | 2009-08-10 23:05:49 -0700 | [diff] [blame] | 1306 | * that wallpaper is visible behind the given window. The X and Y offsets |
| 1307 | * are floating point numbers ranging from 0 to 1, representing where the |
| 1308 | * wallpaper should be positioned within the screen space. These only |
Christopher Tate | ffa6a88 | 2015-12-11 15:08:46 -0800 | [diff] [blame] | 1309 | * make sense when the wallpaper is larger than the display. |
Oleksandr Peletskyi | f251981 | 2016-01-26 20:16:06 +0100 | [diff] [blame] | 1310 | * |
Dianne Hackborn | c8a0a75 | 2009-08-10 23:05:49 -0700 | [diff] [blame] | 1311 | * @param windowToken The window who these offsets should be associated |
Scott Main | 8b2e000 | 2009-09-29 18:17:31 -0700 | [diff] [blame] | 1312 | * with, as returned by {@link android.view.View#getWindowToken() |
Dianne Hackborn | c8a0a75 | 2009-08-10 23:05:49 -0700 | [diff] [blame] | 1313 | * View.getWindowToken()}. |
Marco Nelissen | bf6956b | 2009-11-09 15:21:13 -0800 | [diff] [blame] | 1314 | * @param xOffset The offset along the X dimension, from 0 to 1. |
Dianne Hackborn | c8a0a75 | 2009-08-10 23:05:49 -0700 | [diff] [blame] | 1315 | * @param yOffset The offset along the Y dimension, from 0 to 1. |
| 1316 | */ |
| 1317 | public void setWallpaperOffsets(IBinder windowToken, float xOffset, float yOffset) { |
Adam Cohen | 791a633 | 2012-01-12 14:38:38 -0800 | [diff] [blame] | 1318 | try { |
| 1319 | //Log.v(TAG, "Sending new wallpaper offsets from app..."); |
Jeff Brown | f9e989d | 2013-04-04 23:04:03 -0700 | [diff] [blame] | 1320 | WindowManagerGlobal.getWindowSession().setWallpaperPosition( |
Adam Cohen | 791a633 | 2012-01-12 14:38:38 -0800 | [diff] [blame] | 1321 | windowToken, xOffset, yOffset, mWallpaperXStep, mWallpaperYStep); |
| 1322 | //Log.v(TAG, "...app returning after sending offsets!"); |
| 1323 | } catch (RemoteException e) { |
| 1324 | // Ignore. |
| 1325 | } |
Dianne Hackborn | c8a0a75 | 2009-08-10 23:05:49 -0700 | [diff] [blame] | 1326 | } |
Adam Cohen | 041a0ba | 2011-11-09 20:10:27 -0800 | [diff] [blame] | 1327 | |
Dianne Hackborn | c8a0a75 | 2009-08-10 23:05:49 -0700 | [diff] [blame] | 1328 | /** |
Marco Nelissen | bf6956b | 2009-11-09 15:21:13 -0800 | [diff] [blame] | 1329 | * For applications that use multiple virtual screens showing a wallpaper, |
| 1330 | * specify the step size between virtual screens. For example, if the |
Dianne Hackborn | 23ef7b4 | 2009-11-18 18:20:39 -0800 | [diff] [blame] | 1331 | * launcher has 3 virtual screens, it would specify an xStep of 0.5, |
Marco Nelissen | bf6956b | 2009-11-09 15:21:13 -0800 | [diff] [blame] | 1332 | * since the X offset for those screens are 0.0, 0.5 and 1.0 |
Oleksandr Peletskyi | f251981 | 2016-01-26 20:16:06 +0100 | [diff] [blame] | 1333 | * @param xStep The X offset delta from one screen to the next one |
Marco Nelissen | bf6956b | 2009-11-09 15:21:13 -0800 | [diff] [blame] | 1334 | * @param yStep The Y offset delta from one screen to the next one |
| 1335 | */ |
| 1336 | public void setWallpaperOffsetSteps(float xStep, float yStep) { |
| 1337 | mWallpaperXStep = xStep; |
| 1338 | mWallpaperYStep = yStep; |
| 1339 | } |
Oleksandr Peletskyi | f251981 | 2016-01-26 20:16:06 +0100 | [diff] [blame] | 1340 | |
Marco Nelissen | bf6956b | 2009-11-09 15:21:13 -0800 | [diff] [blame] | 1341 | /** |
Dianne Hackborn | 7580493 | 2009-10-20 20:15:20 -0700 | [diff] [blame] | 1342 | * Send an arbitrary command to the current active wallpaper. |
Oleksandr Peletskyi | f251981 | 2016-01-26 20:16:06 +0100 | [diff] [blame] | 1343 | * |
Dianne Hackborn | 7580493 | 2009-10-20 20:15:20 -0700 | [diff] [blame] | 1344 | * @param windowToken The window who these offsets should be associated |
| 1345 | * with, as returned by {@link android.view.View#getWindowToken() |
| 1346 | * View.getWindowToken()}. |
| 1347 | * @param action Name of the command to perform. This must be a scoped |
| 1348 | * name to avoid collisions, such as "com.mycompany.wallpaper.DOIT". |
| 1349 | * @param x Arbitrary integer argument based on command. |
| 1350 | * @param y Arbitrary integer argument based on command. |
| 1351 | * @param z Arbitrary integer argument based on command. |
| 1352 | * @param extras Optional additional information for the command, or null. |
| 1353 | */ |
| 1354 | public void sendWallpaperCommand(IBinder windowToken, String action, |
| 1355 | int x, int y, int z, Bundle extras) { |
| 1356 | try { |
| 1357 | //Log.v(TAG, "Sending new wallpaper offsets from app..."); |
Jeff Brown | f9e989d | 2013-04-04 23:04:03 -0700 | [diff] [blame] | 1358 | WindowManagerGlobal.getWindowSession().sendWallpaperCommand( |
Dianne Hackborn | 7580493 | 2009-10-20 20:15:20 -0700 | [diff] [blame] | 1359 | windowToken, action, x, y, z, extras, false); |
| 1360 | //Log.v(TAG, "...app returning after sending offsets!"); |
| 1361 | } catch (RemoteException e) { |
| 1362 | // Ignore. |
| 1363 | } |
| 1364 | } |
Benjamin Franz | f3ece36 | 2015-02-11 10:51:10 +0000 | [diff] [blame] | 1365 | |
| 1366 | /** |
| 1367 | * Returns whether wallpapers are supported for the calling user. If this function returns |
Oleksandr Peletskyi | f251981 | 2016-01-26 20:16:06 +0100 | [diff] [blame] | 1368 | * {@code false}, any attempts to changing the wallpaper will have no effect, |
| 1369 | * and any attempt to obtain of the wallpaper will return {@code null}. |
Benjamin Franz | f3ece36 | 2015-02-11 10:51:10 +0000 | [diff] [blame] | 1370 | */ |
| 1371 | public boolean isWallpaperSupported() { |
| 1372 | if (sGlobals.mService == null) { |
| 1373 | Log.w(TAG, "WallpaperService not running"); |
| 1374 | } else { |
| 1375 | try { |
| 1376 | return sGlobals.mService.isWallpaperSupported(mContext.getOpPackageName()); |
| 1377 | } catch (RemoteException e) { |
| 1378 | // Ignore |
| 1379 | } |
| 1380 | } |
| 1381 | return false; |
| 1382 | } |
| 1383 | |
Dianne Hackborn | 7580493 | 2009-10-20 20:15:20 -0700 | [diff] [blame] | 1384 | /** |
Oleksandr Peletskyi | f251981 | 2016-01-26 20:16:06 +0100 | [diff] [blame] | 1385 | * Returns whether the calling package is allowed to set the wallpaper for the calling user. |
| 1386 | * If this function returns {@code false}, any attempts to change the wallpaper will have |
| 1387 | * no effect. Always returns {@code true} for device owner and profile owner. |
| 1388 | * |
| 1389 | * @see android.os.UserManager#DISALLOW_SET_WALLPAPER |
| 1390 | */ |
| 1391 | public boolean isWallpaperSettingAllowed() { |
| 1392 | if (sGlobals.mService == null) { |
| 1393 | Log.w(TAG, "WallpaperService not running"); |
| 1394 | } else { |
| 1395 | try { |
| 1396 | return sGlobals.mService.isWallpaperSettingAllowed(mContext.getOpPackageName()); |
| 1397 | } catch (RemoteException e) { |
| 1398 | // Ignore |
| 1399 | } |
| 1400 | } |
| 1401 | return false; |
| 1402 | } |
| 1403 | |
| 1404 | /** |
Dianne Hackborn | 72c82ab | 2009-08-11 21:13:54 -0700 | [diff] [blame] | 1405 | * Clear the offsets previously associated with this window through |
| 1406 | * {@link #setWallpaperOffsets(IBinder, float, float)}. This reverts |
| 1407 | * the window to its default state, where it does not cause the wallpaper |
| 1408 | * to scroll from whatever its last offsets were. |
Oleksandr Peletskyi | f251981 | 2016-01-26 20:16:06 +0100 | [diff] [blame] | 1409 | * |
Dianne Hackborn | 72c82ab | 2009-08-11 21:13:54 -0700 | [diff] [blame] | 1410 | * @param windowToken The window who these offsets should be associated |
Scott Main | 8b2e000 | 2009-09-29 18:17:31 -0700 | [diff] [blame] | 1411 | * with, as returned by {@link android.view.View#getWindowToken() |
Dianne Hackborn | 72c82ab | 2009-08-11 21:13:54 -0700 | [diff] [blame] | 1412 | * View.getWindowToken()}. |
| 1413 | */ |
| 1414 | public void clearWallpaperOffsets(IBinder windowToken) { |
| 1415 | try { |
Jeff Brown | f9e989d | 2013-04-04 23:04:03 -0700 | [diff] [blame] | 1416 | WindowManagerGlobal.getWindowSession().setWallpaperPosition( |
Marco Nelissen | bf6956b | 2009-11-09 15:21:13 -0800 | [diff] [blame] | 1417 | windowToken, -1, -1, -1, -1); |
Dianne Hackborn | 72c82ab | 2009-08-11 21:13:54 -0700 | [diff] [blame] | 1418 | } catch (RemoteException e) { |
| 1419 | // Ignore. |
| 1420 | } |
| 1421 | } |
Oleksandr Peletskyi | f251981 | 2016-01-26 20:16:06 +0100 | [diff] [blame] | 1422 | |
Dianne Hackborn | 72c82ab | 2009-08-11 21:13:54 -0700 | [diff] [blame] | 1423 | /** |
Christopher Tate | be132e6 | 2016-02-10 12:59:49 -0800 | [diff] [blame] | 1424 | * Remove any currently set system wallpaper, reverting to the system's built-in |
Dianne Hackborn | 8cc6a50 | 2009-08-05 21:29:42 -0700 | [diff] [blame] | 1425 | * wallpaper. On success, the intent {@link Intent#ACTION_WALLPAPER_CHANGED} |
| 1426 | * is broadcast. |
| 1427 | * |
Nicolas Falliere | 9530e3a | 2012-06-18 17:21:06 -0700 | [diff] [blame] | 1428 | * <p>This method requires the caller to hold the permission |
| 1429 | * {@link android.Manifest.permission#SET_WALLPAPER}. |
| 1430 | * |
Michael Jurka | b668d0b | 2013-10-04 15:11:05 -0700 | [diff] [blame] | 1431 | * @throws IOException If an error occurs reverting to the built-in |
Dianne Hackborn | 8cc6a50 | 2009-08-05 21:29:42 -0700 | [diff] [blame] | 1432 | * wallpaper. |
| 1433 | */ |
| 1434 | public void clear() throws IOException { |
Jeff Sharkey | 28f0877 | 2014-04-16 09:41:58 -0700 | [diff] [blame] | 1435 | setStream(openDefaultWallpaper(mContext)); |
| 1436 | } |
| 1437 | |
| 1438 | /** |
| 1439 | * Open stream representing the default static image wallpaper. |
| 1440 | * |
| 1441 | * @hide |
| 1442 | */ |
| 1443 | public static InputStream openDefaultWallpaper(Context context) { |
| 1444 | final String path = SystemProperties.get(PROP_WALLPAPER); |
| 1445 | if (!TextUtils.isEmpty(path)) { |
| 1446 | final File file = new File(path); |
| 1447 | if (file.exists()) { |
| 1448 | try { |
| 1449 | return new FileInputStream(file); |
| 1450 | } catch (IOException e) { |
| 1451 | // Ignored, fall back to platform default below |
| 1452 | } |
| 1453 | } |
| 1454 | } |
| 1455 | return context.getResources().openRawResource( |
| 1456 | com.android.internal.R.drawable.default_wallpaper); |
| 1457 | } |
| 1458 | |
| 1459 | /** |
| 1460 | * Return {@link ComponentName} of the default live wallpaper, or |
| 1461 | * {@code null} if none is defined. |
| 1462 | * |
| 1463 | * @hide |
| 1464 | */ |
| 1465 | public static ComponentName getDefaultWallpaperComponent(Context context) { |
| 1466 | String flat = SystemProperties.get(PROP_WALLPAPER_COMPONENT); |
| 1467 | if (!TextUtils.isEmpty(flat)) { |
| 1468 | final ComponentName cn = ComponentName.unflattenFromString(flat); |
| 1469 | if (cn != null) { |
| 1470 | return cn; |
| 1471 | } |
| 1472 | } |
| 1473 | |
| 1474 | flat = context.getString(com.android.internal.R.string.default_wallpaper_component); |
| 1475 | if (!TextUtils.isEmpty(flat)) { |
| 1476 | final ComponentName cn = ComponentName.unflattenFromString(flat); |
| 1477 | if (cn != null) { |
| 1478 | return cn; |
| 1479 | } |
| 1480 | } |
| 1481 | |
| 1482 | return null; |
Dianne Hackborn | 8cc6a50 | 2009-08-05 21:29:42 -0700 | [diff] [blame] | 1483 | } |
Christopher Tate | 1e1e2e0 | 2016-01-25 15:34:36 -0800 | [diff] [blame] | 1484 | |
Christopher Tate | be132e6 | 2016-02-10 12:59:49 -0800 | [diff] [blame] | 1485 | /** |
| 1486 | * Register a callback for lock wallpaper observation. Only the OS may use this. |
| 1487 | * |
| 1488 | * @return true on success; false on error. |
| 1489 | * @hide |
| 1490 | */ |
| 1491 | public boolean setLockWallpaperCallback(IWallpaperManagerCallback callback) { |
| 1492 | if (sGlobals.mService == null) { |
| 1493 | Log.w(TAG, "WallpaperService not running"); |
| 1494 | return false; |
| 1495 | } |
| 1496 | |
| 1497 | try { |
| 1498 | return sGlobals.mService.setLockWallpaperCallback(callback); |
| 1499 | } catch (RemoteException e) { |
| 1500 | Log.e(TAG, "Unable to contact wallpaper service"); |
| 1501 | } |
| 1502 | return false; |
| 1503 | } |
| 1504 | |
Christopher Tate | 1e1e2e0 | 2016-01-25 15:34:36 -0800 | [diff] [blame] | 1505 | // Private completion callback for setWallpaper() synchronization |
| 1506 | private class WallpaperSetCompletion extends IWallpaperManagerCallback.Stub { |
| 1507 | final CountDownLatch mLatch; |
| 1508 | |
| 1509 | public WallpaperSetCompletion() { |
| 1510 | mLatch = new CountDownLatch(1); |
| 1511 | } |
| 1512 | |
| 1513 | public void waitForCompletion() { |
| 1514 | try { |
| 1515 | mLatch.await(30, TimeUnit.SECONDS); |
| 1516 | } catch (InterruptedException e) { |
| 1517 | // This might be legit: the crop may take a very long time. Don't sweat |
| 1518 | // it in that case; we are okay with display lagging behind in order to |
| 1519 | // keep the caller from locking up indeterminately. |
| 1520 | } |
| 1521 | } |
| 1522 | |
| 1523 | @Override |
| 1524 | public void onWallpaperChanged() throws RemoteException { |
| 1525 | mLatch.countDown(); |
| 1526 | } |
| 1527 | } |
Dianne Hackborn | 8cc6a50 | 2009-08-05 21:29:42 -0700 | [diff] [blame] | 1528 | } |