blob: aa0eaaebf9758d3cb0c555eaa50659c430a5448a [file] [log] [blame]
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001/*
Christopher Tated7faf532016-02-25 12:43:38 -08002 * Copyright (C) 2009 The Android Open Source Project
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07003 *
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 Hackborn8cc6a502009-08-05 21:29:42 -070017package android.app;
18
Christopher Tatead3c2592016-01-20 18:13:17 -080019import android.annotation.IntDef;
Tor Norbye7b9c9122013-05-30 16:48:33 -070020import android.annotation.RawRes;
Dianne Hackborn067e5f62014-09-07 23:14:30 -070021import android.annotation.SystemApi;
Jeff Sharkey28f08772014-04-16 09:41:58 -070022import android.content.ComponentName;
Adam Lesinskibba72d12013-10-11 18:10:56 -070023import android.content.ContentResolver;
Dianne Hackborn8cc6a502009-08-05 21:29:42 -070024import android.content.Context;
25import android.content.Intent;
Michael Jurkae8d1bf72013-09-09 15:58:54 +020026import android.content.pm.PackageManager;
27import android.content.pm.ResolveInfo;
Dianne Hackborn8cc6a502009-08-05 21:29:42 -070028import android.content.res.Resources;
Christopher Tate5d99d472016-05-06 17:59:27 -070029import android.content.res.Resources.NotFoundException;
Dianne Hackborn8cc6a502009-08-05 21:29:42 -070030import android.graphics.Bitmap;
31import android.graphics.BitmapFactory;
Michael Jurkab668d0b2013-10-04 15:11:05 -070032import android.graphics.BitmapRegionDecoder;
Dianne Hackborn284ac932009-08-28 10:34:25 -070033import android.graphics.Canvas;
Dianne Hackborn19382ac2009-09-11 21:13:37 -070034import android.graphics.ColorFilter;
Michael Jurkab668d0b2013-10-04 15:11:05 -070035import android.graphics.Matrix;
Dianne Hackborn284ac932009-08-28 10:34:25 -070036import android.graphics.Paint;
Dianne Hackborn19382ac2009-09-11 21:13:37 -070037import android.graphics.PixelFormat;
Jeff Brown24572372011-06-09 19:05:15 -070038import android.graphics.PorterDuff;
39import android.graphics.PorterDuffXfermode;
Dianne Hackborn284ac932009-08-28 10:34:25 -070040import android.graphics.Rect;
Michael Jurkab668d0b2013-10-04 15:11:05 -070041import android.graphics.RectF;
Dianne Hackborn8cc6a502009-08-05 21:29:42 -070042import android.graphics.drawable.BitmapDrawable;
43import android.graphics.drawable.Drawable;
Michael Jurkae8d1bf72013-09-09 15:58:54 +020044import android.net.Uri;
Dianne Hackborn284ac932009-08-28 10:34:25 -070045import android.os.Bundle;
Christopher Tate98d609c2016-05-18 17:31:58 -070046import android.os.DeadSystemException;
Dianne Hackborn8cc6a502009-08-05 21:29:42 -070047import android.os.Handler;
48import android.os.IBinder;
Dianne Hackborn840c3a22009-09-02 17:35:17 -070049import android.os.Looper;
Dianne Hackborn8cc6a502009-08-05 21:29:42 -070050import android.os.ParcelFileDescriptor;
Jorim Jaggi6c902d02016-08-18 10:44:54 -070051import android.os.Process;
Dianne Hackborn8cc6a502009-08-05 21:29:42 -070052import android.os.RemoteException;
53import android.os.ServiceManager;
Jeff Sharkey28f08772014-04-16 09:41:58 -070054import android.os.SystemProperties;
Jorim Jaggie31f6b82016-07-01 16:15:09 -070055import android.os.UserHandle;
Jeff Sharkey28f08772014-04-16 09:41:58 -070056import android.text.TextUtils;
Dianne Hackborn7341d7a2009-08-14 11:37:52 -070057import android.util.Log;
Jeff Brown98365d72012-08-19 20:30:52 -070058import android.view.WindowManagerGlobal;
Dianne Hackborn8cc6a502009-08-05 21:29:42 -070059
Christopher Tateffa6a882015-12-11 15:08:46 -080060import libcore.io.IoUtils;
61
Michael Jurkab668d0b2013-10-04 15:11:05 -070062import java.io.BufferedInputStream;
Jeff Sharkey28f08772014-04-16 09:41:58 -070063import java.io.File;
64import java.io.FileInputStream;
Dianne Hackborn8cc6a502009-08-05 21:29:42 -070065import java.io.FileOutputStream;
66import java.io.IOException;
67import java.io.InputStream;
Christopher Tatead3c2592016-01-20 18:13:17 -080068import java.lang.annotation.Retention;
69import java.lang.annotation.RetentionPolicy;
Michael Jurkae8d1bf72013-09-09 15:58:54 +020070import java.util.List;
Christopher Tate1e1e2e02016-01-25 15:34:36 -080071import java.util.concurrent.CountDownLatch;
72import java.util.concurrent.TimeUnit;
Dianne Hackborn8cc6a502009-08-05 21:29:42 -070073
Scott Main8b2e0002009-09-29 18:17:31 -070074/**
75 * Provides access to the system wallpaper. With WallpaperManager, you can
76 * get the current wallpaper, get the desired dimensions for the wallpaper, set
77 * the wallpaper, and more. Get an instance of WallpaperManager with
Benjamin Franzf3ece362015-02-11 10:51:10 +000078 * {@link #getInstance(android.content.Context) getInstance()}.
79 *
80 * <p> An app can check whether wallpapers are supported for the current user, by calling
Oleksandr Peletskyif2519812016-01-26 20:16:06 +010081 * {@link #isWallpaperSupported()}, and whether setting of wallpapers is allowed, by calling
Christopher Tate98d609c2016-05-18 17:31:58 -070082 * {@link #isSetWallpaperAllowed()}.
Scott Main8b2e0002009-09-29 18:17:31 -070083 */
Dianne Hackborn8cc6a502009-08-05 21:29:42 -070084public class WallpaperManager {
85 private static String TAG = "WallpaperManager";
86 private static boolean DEBUG = false;
Marco Nelissenbf6956b2009-11-09 15:21:13 -080087 private float mWallpaperXStep = -1;
88 private float mWallpaperYStep = -1;
Dianne Hackborn8cc6a502009-08-05 21:29:42 -070089
Jeff Sharkey28f08772014-04-16 09:41:58 -070090 /** {@hide} */
91 private static final String PROP_WALLPAPER = "ro.config.wallpaper";
92 /** {@hide} */
Christopher Tate5d99d472016-05-06 17:59:27 -070093 private static final String PROP_LOCK_WALLPAPER = "ro.config.lock_wallpaper";
94 /** {@hide} */
Jeff Sharkey28f08772014-04-16 09:41:58 -070095 private static final String PROP_WALLPAPER_COMPONENT = "ro.config.wallpaper_component";
96
Dianne Hackborn4c62fc02009-08-08 20:40:27 -070097 /**
Michael Jurkae8d1bf72013-09-09 15:58:54 +020098 * Activity Action: Show settings for choosing wallpaper. Do not use directly to construct
99 * an intent; instead, use {@link #getCropAndSetWallpaperIntent}.
100 * <p>Input: {@link Intent#getData} is the URI of the image to crop and set as wallpaper.
101 * <p>Output: RESULT_OK if user decided to crop/set the wallpaper, RESULT_CANCEL otherwise
Adam Lesinskibba72d12013-10-11 18:10:56 -0700102 * Activities that support this intent should specify a MIME filter of "image/*"
Michael Jurkae8d1bf72013-09-09 15:58:54 +0200103 */
104 public static final String ACTION_CROP_AND_SET_WALLPAPER =
105 "android.service.wallpaper.CROP_AND_SET_WALLPAPER";
106
107 /**
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700108 * Launch an activity for the user to pick the current global live
109 * wallpaper.
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700110 */
111 public static final String ACTION_LIVE_WALLPAPER_CHOOSER
112 = "android.service.wallpaper.LIVE_WALLPAPER_CHOOSER";
Adam Cohen5a242ec2010-12-07 21:07:07 -0800113
114 /**
Dianne Hackborn7df7d202012-04-19 18:00:04 -0700115 * Directly launch live wallpaper preview, allowing the user to immediately
116 * confirm to switch to a specific live wallpaper. You must specify
117 * {@link #EXTRA_LIVE_WALLPAPER_COMPONENT} with the ComponentName of
118 * a live wallpaper component that is to be shown.
119 */
120 public static final String ACTION_CHANGE_LIVE_WALLPAPER
121 = "android.service.wallpaper.CHANGE_LIVE_WALLPAPER";
122
123 /**
124 * Extra in {@link #ACTION_CHANGE_LIVE_WALLPAPER} that specifies the
125 * ComponentName of a live wallpaper that should be shown as a preview,
126 * for the user to confirm.
127 */
128 public static final String EXTRA_LIVE_WALLPAPER_COMPONENT
129 = "android.service.wallpaper.extra.LIVE_WALLPAPER_COMPONENT";
130
131 /**
Adam Cohen5a242ec2010-12-07 21:07:07 -0800132 * Manifest entry for activities that respond to {@link Intent#ACTION_SET_WALLPAPER}
133 * which allows them to provide a custom large icon associated with this action.
134 */
135 public static final String WALLPAPER_PREVIEW_META_DATA = "android.wallpaper.preview";
136
Dianne Hackborn23ef7b42009-11-18 18:20:39 -0800137 /**
138 * Command for {@link #sendWallpaperCommand}: reported by the wallpaper
139 * host when the user taps on an empty area (not performing an action
140 * in the host). The x and y arguments are the location of the tap in
141 * screen coordinates.
142 */
143 public static final String COMMAND_TAP = "android.wallpaper.tap";
Oleksandr Peletskyif2519812016-01-26 20:16:06 +0100144
Dianne Hackborn23ef7b42009-11-18 18:20:39 -0800145 /**
146 * Command for {@link #sendWallpaperCommand}: reported by the wallpaper
Jeff Brown9f3bdfe2010-10-13 06:01:27 -0700147 * host when the user releases a secondary pointer on an empty area
148 * (not performing an action in the host). The x and y arguments are
149 * the location of the secondary tap in screen coordinates.
150 */
151 public static final String COMMAND_SECONDARY_TAP = "android.wallpaper.secondaryTap";
152
153 /**
154 * Command for {@link #sendWallpaperCommand}: reported by the wallpaper
Dianne Hackborn23ef7b42009-11-18 18:20:39 -0800155 * host when the user drops an object into an area of the host. The x
156 * and y arguments are the location of the drop.
157 */
158 public static final String COMMAND_DROP = "android.home.drop";
Christopher Tatead3c2592016-01-20 18:13:17 -0800159
160 /**
161 * Extra passed back from setWallpaper() giving the new wallpaper's assigned ID.
162 * @hide
163 */
164 public static final String EXTRA_NEW_WALLPAPER_ID = "android.service.wallpaper.extra.ID";
165
Christopher Tate5d99d472016-05-06 17:59:27 -0700166 // flags for which kind of wallpaper to act on
Christopher Tatead3c2592016-01-20 18:13:17 -0800167
168 /** @hide */
169 @IntDef(flag = true, value = {
Christopher Tateedf7d042016-03-29 18:24:25 -0700170 FLAG_SYSTEM,
171 FLAG_LOCK
Christopher Tatead3c2592016-01-20 18:13:17 -0800172 })
173 @Retention(RetentionPolicy.SOURCE)
174 public @interface SetWallpaperFlags {}
175
176 /**
Christopher Tate5d99d472016-05-06 17:59:27 -0700177 * Flag: set or retrieve the general system wallpaper.
Christopher Tatead3c2592016-01-20 18:13:17 -0800178 */
Christopher Tateedf7d042016-03-29 18:24:25 -0700179 public static final int FLAG_SYSTEM = 1 << 0;
Christopher Tatead3c2592016-01-20 18:13:17 -0800180
181 /**
Christopher Tate5d99d472016-05-06 17:59:27 -0700182 * Flag: set or retrieve the lock-screen-specific wallpaper.
Christopher Tatead3c2592016-01-20 18:13:17 -0800183 */
Christopher Tateedf7d042016-03-29 18:24:25 -0700184 public static final int FLAG_LOCK = 1 << 1;
Christopher Tatead3c2592016-01-20 18:13:17 -0800185
Dianne Hackborn8cc6a502009-08-05 21:29:42 -0700186 private final Context mContext;
Oleksandr Peletskyif2519812016-01-26 20:16:06 +0100187
Dianne Hackborn19382ac2009-09-11 21:13:37 -0700188 /**
189 * Special drawable that draws a wallpaper as fast as possible. Assumes
190 * no scaling or placement off (0,0) of the wallpaper (this should be done
191 * at the time the bitmap is loaded).
192 */
193 static class FastBitmapDrawable extends Drawable {
194 private final Bitmap mBitmap;
195 private final int mWidth;
196 private final int mHeight;
197 private int mDrawLeft;
198 private int mDrawTop;
Romain Guy407ec782011-08-24 17:06:58 -0700199 private final Paint mPaint;
Dianne Hackborn19382ac2009-09-11 21:13:37 -0700200
201 private FastBitmapDrawable(Bitmap bitmap) {
202 mBitmap = bitmap;
203 mWidth = bitmap.getWidth();
204 mHeight = bitmap.getHeight();
Romain Guy407ec782011-08-24 17:06:58 -0700205
Dianne Hackborn19382ac2009-09-11 21:13:37 -0700206 setBounds(0, 0, mWidth, mHeight);
Romain Guy407ec782011-08-24 17:06:58 -0700207
208 mPaint = new Paint();
209 mPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC));
Dianne Hackborn19382ac2009-09-11 21:13:37 -0700210 }
211
212 @Override
213 public void draw(Canvas canvas) {
Romain Guy407ec782011-08-24 17:06:58 -0700214 canvas.drawBitmap(mBitmap, mDrawLeft, mDrawTop, mPaint);
Dianne Hackborn19382ac2009-09-11 21:13:37 -0700215 }
216
217 @Override
218 public int getOpacity() {
219 return PixelFormat.OPAQUE;
220 }
221
222 @Override
223 public void setBounds(int left, int top, int right, int bottom) {
224 mDrawLeft = left + (right-left - mWidth) / 2;
225 mDrawTop = top + (bottom-top - mHeight) / 2;
226 }
227
228 @Override
Dianne Hackborn19382ac2009-09-11 21:13:37 -0700229 public void setAlpha(int alpha) {
Romain Guy407ec782011-08-24 17:06:58 -0700230 throw new UnsupportedOperationException("Not supported with this drawable");
Dianne Hackborn19382ac2009-09-11 21:13:37 -0700231 }
232
233 @Override
Chris Craikbd3bfc52015-03-02 10:43:29 -0800234 public void setColorFilter(ColorFilter colorFilter) {
Romain Guy407ec782011-08-24 17:06:58 -0700235 throw new UnsupportedOperationException("Not supported with this drawable");
Dianne Hackborn19382ac2009-09-11 21:13:37 -0700236 }
237
238 @Override
239 public void setDither(boolean dither) {
Romain Guy407ec782011-08-24 17:06:58 -0700240 throw new UnsupportedOperationException("Not supported with this drawable");
Dianne Hackborn19382ac2009-09-11 21:13:37 -0700241 }
242
243 @Override
244 public void setFilterBitmap(boolean filter) {
Romain Guy407ec782011-08-24 17:06:58 -0700245 throw new UnsupportedOperationException("Not supported with this drawable");
Dianne Hackborn19382ac2009-09-11 21:13:37 -0700246 }
247
248 @Override
249 public int getIntrinsicWidth() {
250 return mWidth;
251 }
252
253 @Override
254 public int getIntrinsicHeight() {
255 return mHeight;
256 }
257
258 @Override
259 public int getMinimumWidth() {
260 return mWidth;
261 }
262
263 @Override
264 public int getMinimumHeight() {
265 return mHeight;
266 }
267 }
Oleksandr Peletskyif2519812016-01-26 20:16:06 +0100268
Dianne Hackborn8cc6a502009-08-05 21:29:42 -0700269 static class Globals extends IWallpaperManagerCallback.Stub {
Fyodor Kupolovb6378cf2016-07-12 10:32:08 -0700270 private final IWallpaperManager mService;
Vadim Tryshev93a914a2016-03-07 12:48:51 -0800271 private Bitmap mCachedWallpaper;
272 private int mCachedWallpaperUserId;
Dianne Hackborn19382ac2009-09-11 21:13:37 -0700273 private Bitmap mDefaultWallpaper;
Oleksandr Peletskyif2519812016-01-26 20:16:06 +0100274
Dianne Hackborn5e802fb2009-09-02 21:50:50 -0700275 Globals(Looper looper) {
Dianne Hackborn8cc6a502009-08-05 21:29:42 -0700276 IBinder b = ServiceManager.getService(Context.WALLPAPER_SERVICE);
277 mService = IWallpaperManager.Stub.asInterface(b);
Christopher Tatebe132e62016-02-10 12:59:49 -0800278 forgetLoadedWallpaper();
Dianne Hackborn8cc6a502009-08-05 21:29:42 -0700279 }
Oleksandr Peletskyif2519812016-01-26 20:16:06 +0100280
Dianne Hackborn8cc6a502009-08-05 21:29:42 -0700281 public void onWallpaperChanged() {
282 /* The wallpaper has changed but we shouldn't eagerly load the
283 * wallpaper as that would be inefficient. Reset the cached wallpaper
284 * to null so if the user requests the wallpaper again then we'll
285 * fetch it.
286 */
Christopher Tatebe132e62016-02-10 12:59:49 -0800287 forgetLoadedWallpaper();
Dianne Hackborn8cc6a502009-08-05 21:29:42 -0700288 }
Adam Cohen041a0ba2011-11-09 20:10:27 -0800289
Christopher Tate5d99d472016-05-06 17:59:27 -0700290 public Bitmap peekWallpaperBitmap(Context context, boolean returnDefault,
291 @SetWallpaperFlags int which) {
292 return peekWallpaperBitmap(context, returnDefault, which, context.getUserId());
Yorke Leedcd93cc2016-01-08 14:12:55 -0800293 }
294
Christopher Tate5d99d472016-05-06 17:59:27 -0700295 public Bitmap peekWallpaperBitmap(Context context, boolean returnDefault,
296 @SetWallpaperFlags int which, int userId) {
Fyodor Kupolovb6378cf2016-07-12 10:32:08 -0700297 if (mService != null) {
298 try {
299 if (!mService.isWallpaperSupported(context.getOpPackageName())) {
300 return null;
Benjamin Franzf3ece362015-02-11 10:51:10 +0000301 }
Fyodor Kupolovb6378cf2016-07-12 10:32:08 -0700302 } catch (RemoteException e) {
303 throw e.rethrowFromSystemServer();
Benjamin Franzf3ece362015-02-11 10:51:10 +0000304 }
Fyodor Kupolovb6378cf2016-07-12 10:32:08 -0700305 }
306 synchronized (this) {
Vadim Tryshev93a914a2016-03-07 12:48:51 -0800307 if (mCachedWallpaper != null && mCachedWallpaperUserId == userId) {
308 return mCachedWallpaper;
Dianne Hackborn8cc6a502009-08-05 21:29:42 -0700309 }
Vadim Tryshev93a914a2016-03-07 12:48:51 -0800310 mCachedWallpaper = null;
311 mCachedWallpaperUserId = 0;
Dianne Hackbornc9dbbe22009-11-11 22:50:37 -0800312 try {
Vadim Tryshev93a914a2016-03-07 12:48:51 -0800313 mCachedWallpaper = getCurrentWallpaperLocked(userId);
314 mCachedWallpaperUserId = userId;
Dianne Hackbornc9dbbe22009-11-11 22:50:37 -0800315 } catch (OutOfMemoryError e) {
316 Log.w(TAG, "No memory load current wallpaper", e);
317 }
Vadim Tryshev93a914a2016-03-07 12:48:51 -0800318 if (mCachedWallpaper != null) {
319 return mCachedWallpaper;
Dianne Hackborn19382ac2009-09-11 21:13:37 -0700320 }
Dianne Hackborn8cc6a502009-08-05 21:29:42 -0700321 }
Fyodor Kupolovb6378cf2016-07-12 10:32:08 -0700322 if (returnDefault) {
323 Bitmap defaultWallpaper = mDefaultWallpaper;
324 if (defaultWallpaper == null) {
325 defaultWallpaper = getDefaultWallpaper(context, which);
326 synchronized (this) {
327 mDefaultWallpaper = defaultWallpaper;
328 }
329 }
330 return defaultWallpaper;
331 }
332 return null;
Dianne Hackborn8cc6a502009-08-05 21:29:42 -0700333 }
Dianne Hackbornba398392011-08-01 16:11:57 -0700334
Fyodor Kupolovb6378cf2016-07-12 10:32:08 -0700335 void forgetLoadedWallpaper() {
Dianne Hackbornba398392011-08-01 16:11:57 -0700336 synchronized (this) {
Vadim Tryshev93a914a2016-03-07 12:48:51 -0800337 mCachedWallpaper = null;
338 mCachedWallpaperUserId = 0;
Dianne Hackbornba398392011-08-01 16:11:57 -0700339 mDefaultWallpaper = null;
340 }
341 }
342
Yorke Leedcd93cc2016-01-08 14:12:55 -0800343 private Bitmap getCurrentWallpaperLocked(int userId) {
Adam Lesinski2c8d67c2014-04-23 13:46:21 -0700344 if (mService == null) {
345 Log.w(TAG, "WallpaperService not running");
346 return null;
347 }
348
Dianne Hackborn8cc6a502009-08-05 21:29:42 -0700349 try {
Dianne Hackborn284ac932009-08-28 10:34:25 -0700350 Bundle params = new Bundle();
Christopher Tateedf7d042016-03-29 18:24:25 -0700351 ParcelFileDescriptor fd = mService.getWallpaper(this, FLAG_SYSTEM,
Yorke Leedcd93cc2016-01-08 14:12:55 -0800352 params, userId);
Dianne Hackborn8cc6a502009-08-05 21:29:42 -0700353 if (fd != null) {
Jeff Brown24572372011-06-09 19:05:15 -0700354 try {
355 BitmapFactory.Options options = new BitmapFactory.Options();
Michael Jurka824a4b52013-12-18 17:10:16 +0100356 return BitmapFactory.decodeFileDescriptor(
Jeff Brown24572372011-06-09 19:05:15 -0700357 fd.getFileDescriptor(), null, options);
Jeff Brown24572372011-06-09 19:05:15 -0700358 } catch (OutOfMemoryError e) {
359 Log.w(TAG, "Can't decode file", e);
360 } finally {
Christopher Tateffa6a882015-12-11 15:08:46 -0800361 IoUtils.closeQuietly(fd);
Dianne Hackborn8cc6a502009-08-05 21:29:42 -0700362 }
Dianne Hackborn19382ac2009-09-11 21:13:37 -0700363 }
364 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -0700365 throw e.rethrowFromSystemServer();
Dianne Hackborn19382ac2009-09-11 21:13:37 -0700366 }
367 return null;
368 }
Christopher Tateffa6a882015-12-11 15:08:46 -0800369
Fyodor Kupolovb6378cf2016-07-12 10:32:08 -0700370 private Bitmap getDefaultWallpaper(Context context, @SetWallpaperFlags int which) {
Christopher Tate5d99d472016-05-06 17:59:27 -0700371 InputStream is = openDefaultWallpaper(context, which);
Adam Lesinski2c8d67c2014-04-23 13:46:21 -0700372 if (is != null) {
373 try {
374 BitmapFactory.Options options = new BitmapFactory.Options();
375 return BitmapFactory.decodeStream(is, null, options);
376 } catch (OutOfMemoryError e) {
377 Log.w(TAG, "Can't decode stream", e);
378 } finally {
Christopher Tateffa6a882015-12-11 15:08:46 -0800379 IoUtils.closeQuietly(is);
Dianne Hackborn8cc6a502009-08-05 21:29:42 -0700380 }
Dianne Hackborn8cc6a502009-08-05 21:29:42 -0700381 }
382 return null;
383 }
384 }
Oleksandr Peletskyif2519812016-01-26 20:16:06 +0100385
Romain Guy407ec782011-08-24 17:06:58 -0700386 private static final Object sSync = new Object[0];
Dianne Hackborn8cc6a502009-08-05 21:29:42 -0700387 private static Globals sGlobals;
388
Dianne Hackborn840c3a22009-09-02 17:35:17 -0700389 static void initGlobals(Looper looper) {
Romain Guy407ec782011-08-24 17:06:58 -0700390 synchronized (sSync) {
Dianne Hackborn8cc6a502009-08-05 21:29:42 -0700391 if (sGlobals == null) {
Dianne Hackborn5e802fb2009-09-02 21:50:50 -0700392 sGlobals = new Globals(looper);
Dianne Hackborn8cc6a502009-08-05 21:29:42 -0700393 }
Dianne Hackborn8cc6a502009-08-05 21:29:42 -0700394 }
395 }
Oleksandr Peletskyif2519812016-01-26 20:16:06 +0100396
Dianne Hackborn8cc6a502009-08-05 21:29:42 -0700397 /*package*/ WallpaperManager(Context context, Handler handler) {
398 mContext = context;
Dianne Hackborn840c3a22009-09-02 17:35:17 -0700399 initGlobals(context.getMainLooper());
Dianne Hackborn8cc6a502009-08-05 21:29:42 -0700400 }
401
402 /**
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700403 * Retrieve a WallpaperManager associated with the given Context.
404 */
405 public static WallpaperManager getInstance(Context context) {
406 return (WallpaperManager)context.getSystemService(
407 Context.WALLPAPER_SERVICE);
408 }
Oleksandr Peletskyif2519812016-01-26 20:16:06 +0100409
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700410 /** @hide */
411 public IWallpaperManager getIWallpaperManager() {
Dianne Hackborn840c3a22009-09-02 17:35:17 -0700412 return sGlobals.mService;
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700413 }
Oleksandr Peletskyif2519812016-01-26 20:16:06 +0100414
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700415 /**
Scott Main8b2e0002009-09-29 18:17:31 -0700416 * Retrieve the current system wallpaper; if
Michael Jurkab668d0b2013-10-04 15:11:05 -0700417 * no wallpaper is set, the system built-in static wallpaper is returned.
Scott Main8b2e0002009-09-29 18:17:31 -0700418 * This is returned as an
419 * abstract Drawable that you can install in a View to display whatever
Oleksandr Peletskyif2519812016-01-26 20:16:06 +0100420 * wallpaper the user has currently set.
Christopher Tate41dc83bd2016-07-15 15:59:23 -0700421 * <p>
422 * This method can return null if there is no system wallpaper available, if
423 * wallpapers are not supported in the current user, or if the calling app is not
424 * permitted to access the system wallpaper.
Dianne Hackborn8cc6a502009-08-05 21:29:42 -0700425 *
Christopher Tate41dc83bd2016-07-15 15:59:23 -0700426 * @return Returns a Drawable object that will draw the system wallpaper,
427 * or {@code null} if no system wallpaper exists or if the calling application
428 * is not able to access the wallpaper.
Dianne Hackborn8cc6a502009-08-05 21:29:42 -0700429 */
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700430 public Drawable getDrawable() {
Christopher Tate5d99d472016-05-06 17:59:27 -0700431 Bitmap bm = sGlobals.peekWallpaperBitmap(mContext, true, FLAG_SYSTEM);
Dianne Hackborn19382ac2009-09-11 21:13:37 -0700432 if (bm != null) {
433 Drawable dr = new BitmapDrawable(mContext.getResources(), bm);
434 dr.setDither(false);
435 return dr;
436 }
437 return null;
Dianne Hackborn8cc6a502009-08-05 21:29:42 -0700438 }
439
440 /**
Christopher Tate5d99d472016-05-06 17:59:27 -0700441 * Obtain a drawable for the built-in static system wallpaper.
Michael Jurkab668d0b2013-10-04 15:11:05 -0700442 */
443 public Drawable getBuiltInDrawable() {
Christopher Tate5d99d472016-05-06 17:59:27 -0700444 return getBuiltInDrawable(0, 0, false, 0, 0, FLAG_SYSTEM);
445 }
446
447 /**
448 * Obtain a drawable for the specified built-in static system wallpaper.
449 *
450 * @param which The {@code FLAG_*} identifier of a valid wallpaper type. Throws
451 * IllegalArgumentException if an invalid wallpaper is requested.
452 * @return A Drawable presenting the specified wallpaper image, or {@code null}
453 * if no built-in default image for that wallpaper type exists.
454 */
455 public Drawable getBuiltInDrawable(@SetWallpaperFlags int which) {
456 return getBuiltInDrawable(0, 0, false, 0, 0, which);
Michael Jurkab668d0b2013-10-04 15:11:05 -0700457 }
458
459 /**
460 * Returns a drawable for the system built-in static wallpaper. Based on the parameters, the
461 * drawable can be cropped and scaled
462 *
463 * @param outWidth The width of the returned drawable
464 * @param outWidth The height of the returned drawable
465 * @param scaleToFit If true, scale the wallpaper down rather than just cropping it
466 * @param horizontalAlignment A float value between 0 and 1 specifying where to crop the image;
467 * 0 for left-aligned, 0.5 for horizontal center-aligned, and 1 for right-aligned
468 * @param verticalAlignment A float value between 0 and 1 specifying where to crop the image;
469 * 0 for top-aligned, 0.5 for vertical center-aligned, and 1 for bottom-aligned
Christopher Tate5d99d472016-05-06 17:59:27 -0700470 * @return A Drawable presenting the built-in default system wallpaper image,
471 * or {@code null} if no such default image is defined on this device.
Michael Jurkab668d0b2013-10-04 15:11:05 -0700472 */
473 public Drawable getBuiltInDrawable(int outWidth, int outHeight,
474 boolean scaleToFit, float horizontalAlignment, float verticalAlignment) {
Christopher Tate5d99d472016-05-06 17:59:27 -0700475 return getBuiltInDrawable(outWidth, outHeight, scaleToFit,
476 horizontalAlignment, verticalAlignment, FLAG_SYSTEM);
477 }
478
479 /**
480 * Returns a drawable for the built-in static wallpaper of the specified type. Based on the
481 * parameters, the drawable can be cropped and scaled.
482 *
483 * @param outWidth The width of the returned drawable
484 * @param outWidth The height of the returned drawable
485 * @param scaleToFit If true, scale the wallpaper down rather than just cropping it
486 * @param horizontalAlignment A float value between 0 and 1 specifying where to crop the image;
487 * 0 for left-aligned, 0.5 for horizontal center-aligned, and 1 for right-aligned
488 * @param verticalAlignment A float value between 0 and 1 specifying where to crop the image;
489 * 0 for top-aligned, 0.5 for vertical center-aligned, and 1 for bottom-aligned
490 * @param which The {@code FLAG_*} identifier of a valid wallpaper type. Throws
491 * IllegalArgumentException if an invalid wallpaper is requested.
492 * @return A Drawable presenting the built-in default wallpaper image of the given type,
493 * or {@code null} if no default image of that type is defined on this device.
494 */
495 public Drawable getBuiltInDrawable(int outWidth, int outHeight, boolean scaleToFit,
496 float horizontalAlignment, float verticalAlignment, @SetWallpaperFlags int which) {
Michael Jurkab668d0b2013-10-04 15:11:05 -0700497 if (sGlobals.mService == null) {
498 Log.w(TAG, "WallpaperService not running");
Christopher Tate98d609c2016-05-18 17:31:58 -0700499 throw new RuntimeException(new DeadSystemException());
Michael Jurkab668d0b2013-10-04 15:11:05 -0700500 }
Christopher Tate5d99d472016-05-06 17:59:27 -0700501
502 if (which != FLAG_SYSTEM && which != FLAG_LOCK) {
503 throw new IllegalArgumentException("Must request exactly one kind of wallpaper");
504 }
505
Michael Jurkab668d0b2013-10-04 15:11:05 -0700506 Resources resources = mContext.getResources();
507 horizontalAlignment = Math.max(0, Math.min(1, horizontalAlignment));
508 verticalAlignment = Math.max(0, Math.min(1, verticalAlignment));
509
Christopher Tate5d99d472016-05-06 17:59:27 -0700510 InputStream wpStream = openDefaultWallpaper(mContext, which);
511 if (wpStream == null) {
512 if (DEBUG) {
513 Log.w(TAG, "default wallpaper stream " + which + " is null");
514 }
Michael Jurkab668d0b2013-10-04 15:11:05 -0700515 return null;
516 } else {
Christopher Tate5d99d472016-05-06 17:59:27 -0700517 InputStream is = new BufferedInputStream(wpStream);
Michael Jurkab668d0b2013-10-04 15:11:05 -0700518 if (outWidth <= 0 || outHeight <= 0) {
519 Bitmap fullSize = BitmapFactory.decodeStream(is, null, null);
520 return new BitmapDrawable(resources, fullSize);
521 } else {
522 int inWidth;
523 int inHeight;
Christopher Tate5d99d472016-05-06 17:59:27 -0700524 // Just measure this time through...
Michael Jurkab668d0b2013-10-04 15:11:05 -0700525 {
526 BitmapFactory.Options options = new BitmapFactory.Options();
527 options.inJustDecodeBounds = true;
528 BitmapFactory.decodeStream(is, null, options);
529 if (options.outWidth != 0 && options.outHeight != 0) {
530 inWidth = options.outWidth;
531 inHeight = options.outHeight;
532 } else {
533 Log.e(TAG, "default wallpaper dimensions are 0");
534 return null;
535 }
536 }
537
Christopher Tate5d99d472016-05-06 17:59:27 -0700538 // Reopen the stream to do the full decode. We know at this point
539 // that openDefaultWallpaper() will return non-null.
540 is = new BufferedInputStream(openDefaultWallpaper(mContext, which));
Michael Jurkab668d0b2013-10-04 15:11:05 -0700541
542 RectF cropRectF;
543
544 outWidth = Math.min(inWidth, outWidth);
545 outHeight = Math.min(inHeight, outHeight);
546 if (scaleToFit) {
547 cropRectF = getMaxCropRect(inWidth, inHeight, outWidth, outHeight,
548 horizontalAlignment, verticalAlignment);
549 } else {
550 float left = (inWidth - outWidth) * horizontalAlignment;
551 float right = left + outWidth;
552 float top = (inHeight - outHeight) * verticalAlignment;
553 float bottom = top + outHeight;
Michael Jurka05391532013-10-14 20:44:42 -0700554 cropRectF = new RectF(left, top, right, bottom);
Michael Jurkab668d0b2013-10-04 15:11:05 -0700555 }
556 Rect roundedTrueCrop = new Rect();
557 cropRectF.roundOut(roundedTrueCrop);
558
559 if (roundedTrueCrop.width() <= 0 || roundedTrueCrop.height() <= 0) {
560 Log.w(TAG, "crop has bad values for full size image");
561 return null;
562 }
563
564 // See how much we're reducing the size of the image
565 int scaleDownSampleSize = Math.min(roundedTrueCrop.width() / outWidth,
566 roundedTrueCrop.height() / outHeight);
567
568 // Attempt to open a region decoder
569 BitmapRegionDecoder decoder = null;
570 try {
571 decoder = BitmapRegionDecoder.newInstance(is, true);
572 } catch (IOException e) {
573 Log.w(TAG, "cannot open region decoder for default wallpaper");
574 }
575
576 Bitmap crop = null;
577 if (decoder != null) {
578 // Do region decoding to get crop bitmap
579 BitmapFactory.Options options = new BitmapFactory.Options();
580 if (scaleDownSampleSize > 1) {
581 options.inSampleSize = scaleDownSampleSize;
582 }
583 crop = decoder.decodeRegion(roundedTrueCrop, options);
584 decoder.recycle();
585 }
586
587 if (crop == null) {
Christopher Tate5d99d472016-05-06 17:59:27 -0700588 // BitmapRegionDecoder has failed, try to crop in-memory. We know at
589 // this point that openDefaultWallpaper() will return non-null.
590 is = new BufferedInputStream(openDefaultWallpaper(mContext, which));
Michael Jurkab668d0b2013-10-04 15:11:05 -0700591 Bitmap fullSize = null;
Christopher Tate5d99d472016-05-06 17:59:27 -0700592 BitmapFactory.Options options = new BitmapFactory.Options();
593 if (scaleDownSampleSize > 1) {
594 options.inSampleSize = scaleDownSampleSize;
Michael Jurkab668d0b2013-10-04 15:11:05 -0700595 }
Christopher Tate5d99d472016-05-06 17:59:27 -0700596 fullSize = BitmapFactory.decodeStream(is, null, options);
Michael Jurkab668d0b2013-10-04 15:11:05 -0700597 if (fullSize != null) {
598 crop = Bitmap.createBitmap(fullSize, roundedTrueCrop.left,
599 roundedTrueCrop.top, roundedTrueCrop.width(),
600 roundedTrueCrop.height());
601 }
602 }
603
604 if (crop == null) {
605 Log.w(TAG, "cannot decode default wallpaper");
606 return null;
607 }
608
609 // Scale down if necessary
610 if (outWidth > 0 && outHeight > 0 &&
611 (crop.getWidth() != outWidth || crop.getHeight() != outHeight)) {
612 Matrix m = new Matrix();
613 RectF cropRect = new RectF(0, 0, crop.getWidth(), crop.getHeight());
614 RectF returnRect = new RectF(0, 0, outWidth, outHeight);
615 m.setRectToRect(cropRect, returnRect, Matrix.ScaleToFit.FILL);
616 Bitmap tmp = Bitmap.createBitmap((int) returnRect.width(),
617 (int) returnRect.height(), Bitmap.Config.ARGB_8888);
618 if (tmp != null) {
619 Canvas c = new Canvas(tmp);
620 Paint p = new Paint();
621 p.setFilterBitmap(true);
622 c.drawBitmap(crop, m, p);
623 crop = tmp;
624 }
625 }
626
627 return new BitmapDrawable(resources, crop);
628 }
629 }
630 }
631
632 private static RectF getMaxCropRect(int inWidth, int inHeight, int outWidth, int outHeight,
633 float horizontalAlignment, float verticalAlignment) {
634 RectF cropRect = new RectF();
635 // Get a crop rect that will fit this
636 if (inWidth / (float) inHeight > outWidth / (float) outHeight) {
637 cropRect.top = 0;
638 cropRect.bottom = inHeight;
639 float cropWidth = outWidth * (inHeight / (float) outHeight);
640 cropRect.left = (inWidth - cropWidth) * horizontalAlignment;
641 cropRect.right = cropRect.left + cropWidth;
642 } else {
643 cropRect.left = 0;
644 cropRect.right = inWidth;
645 float cropHeight = outHeight * (inWidth / (float) outWidth);
646 cropRect.top = (inHeight - cropHeight) * verticalAlignment;
647 cropRect.bottom = cropRect.top + cropHeight;
648 }
649 return cropRect;
650 }
651
652 /**
Scott Main8b2e0002009-09-29 18:17:31 -0700653 * Retrieve the current system wallpaper; if there is no wallpaper set,
654 * a null pointer is returned. This is returned as an
Dianne Hackborn8cc6a502009-08-05 21:29:42 -0700655 * abstract Drawable that you can install in a View to display whatever
Oleksandr Peletskyif2519812016-01-26 20:16:06 +0100656 * wallpaper the user has currently set.
Dianne Hackborn8cc6a502009-08-05 21:29:42 -0700657 *
658 * @return Returns a Drawable object that will draw the wallpaper or a
659 * null pointer if these is none.
660 */
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700661 public Drawable peekDrawable() {
Christopher Tate5d99d472016-05-06 17:59:27 -0700662 Bitmap bm = sGlobals.peekWallpaperBitmap(mContext, false, FLAG_SYSTEM);
Dianne Hackborn19382ac2009-09-11 21:13:37 -0700663 if (bm != null) {
664 Drawable dr = new BitmapDrawable(mContext.getResources(), bm);
665 dr.setDither(false);
666 return dr;
667 }
668 return null;
669 }
670
671 /**
Scott Main8b2e0002009-09-29 18:17:31 -0700672 * Like {@link #getDrawable()}, but the returned Drawable has a number
673 * of limitations to reduce its overhead as much as possible. It will
674 * never scale the wallpaper (only centering it if the requested bounds
675 * do match the bitmap bounds, which should not be typical), doesn't
676 * allow setting an alpha, color filter, or other attributes, etc. The
677 * bounds of the returned drawable will be initialized to the same bounds
678 * as the wallpaper, so normally you will not need to touch it. The
679 * drawable also assumes that it will be used in a context running in
680 * the same density as the screen (not in density compatibility mode).
Dianne Hackborn19382ac2009-09-11 21:13:37 -0700681 *
682 * @return Returns a Drawable object that will draw the wallpaper.
683 */
684 public Drawable getFastDrawable() {
Christopher Tate5d99d472016-05-06 17:59:27 -0700685 Bitmap bm = sGlobals.peekWallpaperBitmap(mContext, true, FLAG_SYSTEM);
Dianne Hackborn19382ac2009-09-11 21:13:37 -0700686 if (bm != null) {
Romain Guy407ec782011-08-24 17:06:58 -0700687 return new FastBitmapDrawable(bm);
Dianne Hackborn19382ac2009-09-11 21:13:37 -0700688 }
689 return null;
690 }
691
692 /**
Scott Main8b2e0002009-09-29 18:17:31 -0700693 * Like {@link #getFastDrawable()}, but if there is no wallpaper set,
694 * a null pointer is returned.
Dianne Hackborn19382ac2009-09-11 21:13:37 -0700695 *
696 * @return Returns an optimized Drawable object that will draw the
697 * wallpaper or a null pointer if these is none.
698 */
699 public Drawable peekFastDrawable() {
Christopher Tate5d99d472016-05-06 17:59:27 -0700700 Bitmap bm = sGlobals.peekWallpaperBitmap(mContext, false, FLAG_SYSTEM);
Dianne Hackborn19382ac2009-09-11 21:13:37 -0700701 if (bm != null) {
Romain Guy407ec782011-08-24 17:06:58 -0700702 return new FastBitmapDrawable(bm);
Dianne Hackborn19382ac2009-09-11 21:13:37 -0700703 }
704 return null;
Dianne Hackborn8cc6a502009-08-05 21:29:42 -0700705 }
706
707 /**
Romain Guy407ec782011-08-24 17:06:58 -0700708 * Like {@link #getDrawable()} but returns a Bitmap.
Oleksandr Peletskyif2519812016-01-26 20:16:06 +0100709 *
Romain Guy407ec782011-08-24 17:06:58 -0700710 * @hide
711 */
712 public Bitmap getBitmap() {
Yorke Leedcd93cc2016-01-08 14:12:55 -0800713 return getBitmapAsUser(mContext.getUserId());
714 }
715
716 /**
717 * Like {@link #getDrawable()} but returns a Bitmap for the provided user.
718 *
719 * @hide
720 */
721 public Bitmap getBitmapAsUser(int userId) {
Christopher Tate5d99d472016-05-06 17:59:27 -0700722 return sGlobals.peekWallpaperBitmap(mContext, true, FLAG_SYSTEM, userId);
Romain Guy407ec782011-08-24 17:06:58 -0700723 }
724
725 /**
Christopher Tatebe132e62016-02-10 12:59:49 -0800726 * Get an open, readable file descriptor to the given wallpaper image file.
Christopher Tateedf7d042016-03-29 18:24:25 -0700727 * The caller is responsible for closing the file descriptor when done ingesting the file.
Christopher Tatebe132e62016-02-10 12:59:49 -0800728 *
729 * <p>If no lock-specific wallpaper has been configured for the given user, then
Christopher Tateedf7d042016-03-29 18:24:25 -0700730 * this method will return {@code null} when requesting {@link #FLAG_LOCK} rather than
Christopher Tatebe132e62016-02-10 12:59:49 -0800731 * returning the system wallpaper's image file.
Christopher Tateedf7d042016-03-29 18:24:25 -0700732 *
733 * @param which The wallpaper whose image file is to be retrieved. Must be a single
734 * defined kind of wallpaper, either {@link #FLAG_SYSTEM} or
735 * {@link #FLAG_LOCK}.
736 *
737 * @see #FLAG_LOCK
738 * @see #FLAG_SYSTEM
Christopher Tatebe132e62016-02-10 12:59:49 -0800739 */
Christopher Tate5d99d472016-05-06 17:59:27 -0700740 public ParcelFileDescriptor getWallpaperFile(@SetWallpaperFlags int which) {
Christopher Tatebe132e62016-02-10 12:59:49 -0800741 return getWallpaperFile(which, mContext.getUserId());
742 }
743
744 /**
745 * Version of {@link #getWallpaperFile(int)} that can access the wallpaper data
746 * for a given user. The caller must hold the INTERACT_ACROSS_USERS_FULL
747 * permission to access another user's wallpaper data.
Christopher Tateedf7d042016-03-29 18:24:25 -0700748 *
749 * @param which The wallpaper whose image file is to be retrieved. Must be a single
750 * defined kind of wallpaper, either {@link #FLAG_SYSTEM} or
751 * {@link #FLAG_LOCK}.
752 * @param userId The user or profile whose imagery is to be retrieved
753 *
754 * @see #FLAG_LOCK
755 * @see #FLAG_SYSTEM
756 *
Christopher Tatebe132e62016-02-10 12:59:49 -0800757 * @hide
758 */
Christopher Tate5d99d472016-05-06 17:59:27 -0700759 public ParcelFileDescriptor getWallpaperFile(@SetWallpaperFlags int which, int userId) {
Christopher Tateedf7d042016-03-29 18:24:25 -0700760 if (which != FLAG_SYSTEM && which != FLAG_LOCK) {
Christopher Tatebe132e62016-02-10 12:59:49 -0800761 throw new IllegalArgumentException("Must request exactly one kind of wallpaper");
762 }
763
764 if (sGlobals.mService == null) {
765 Log.w(TAG, "WallpaperService not running");
Christopher Tate98d609c2016-05-18 17:31:58 -0700766 throw new RuntimeException(new DeadSystemException());
Christopher Tatebe132e62016-02-10 12:59:49 -0800767 } else {
768 try {
769 Bundle outParams = new Bundle();
770 return sGlobals.mService.getWallpaper(null, which, outParams, userId);
771 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -0700772 throw e.rethrowFromSystemServer();
Christopher Tatebe132e62016-02-10 12:59:49 -0800773 }
774 }
775 }
776
777 /**
Dianne Hackbornba398392011-08-01 16:11:57 -0700778 * Remove all internal references to the last loaded wallpaper. Useful
779 * for apps that want to reduce memory usage when they only temporarily
780 * need to have the wallpaper. After calling, the next request for the
781 * wallpaper will require reloading it again from disk.
782 */
783 public void forgetLoadedWallpaper() {
Christopher Tatebe132e62016-02-10 12:59:49 -0800784 sGlobals.forgetLoadedWallpaper();
Dianne Hackbornba398392011-08-01 16:11:57 -0700785 }
786
787 /**
Dianne Hackborneb034652009-09-07 00:49:58 -0700788 * If the current wallpaper is a live wallpaper component, return the
789 * information about that wallpaper. Otherwise, if it is a static image,
790 * simply return null.
Dianne Hackborneb034652009-09-07 00:49:58 -0700791 */
792 public WallpaperInfo getWallpaperInfo() {
793 try {
Mike Lockwoodc067c9c2011-10-31 12:50:12 -0400794 if (sGlobals.mService == null) {
795 Log.w(TAG, "WallpaperService not running");
Christopher Tate98d609c2016-05-18 17:31:58 -0700796 throw new RuntimeException(new DeadSystemException());
Mike Lockwoodc067c9c2011-10-31 12:50:12 -0400797 } else {
Jorim Jaggie31f6b82016-07-01 16:15:09 -0700798 return sGlobals.mService.getWallpaperInfo(UserHandle.myUserId());
Mike Lockwoodc067c9c2011-10-31 12:50:12 -0400799 }
Dianne Hackborneb034652009-09-07 00:49:58 -0700800 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -0700801 throw e.rethrowFromSystemServer();
Dianne Hackborneb034652009-09-07 00:49:58 -0700802 }
803 }
Michael Jurkae8d1bf72013-09-09 15:58:54 +0200804
805 /**
Christopher Tatee409f0e2016-03-21 14:53:15 -0700806 * Get the ID of the current wallpaper of the given kind. If there is no
807 * such wallpaper configured, returns a negative number.
808 *
Christopher Tate98d609c2016-05-18 17:31:58 -0700809 * <p>Every time the wallpaper image is set, a new ID is assigned to it.
810 * This method allows the caller to determine whether the wallpaper imagery
811 * has changed, regardless of how that change happened.
812 *
Christopher Tatee409f0e2016-03-21 14:53:15 -0700813 * @param which The wallpaper whose ID is to be returned. Must be a single
Christopher Tateedf7d042016-03-29 18:24:25 -0700814 * defined kind of wallpaper, either {@link #FLAG_SYSTEM} or
815 * {@link #FLAG_LOCK}.
Christopher Tatee409f0e2016-03-21 14:53:15 -0700816 * @return The positive numeric ID of the current wallpaper of the given kind,
817 * or a negative value if no such wallpaper is configured.
818 */
Christopher Tate5d99d472016-05-06 17:59:27 -0700819 public int getWallpaperId(@SetWallpaperFlags int which) {
Christopher Tatee409f0e2016-03-21 14:53:15 -0700820 return getWallpaperIdForUser(which, mContext.getUserId());
821 }
822
823 /**
824 * Get the ID of the given user's current wallpaper of the given kind. If there
825 * is no such wallpaper configured, returns a negative number.
826 * @hide
827 */
Christopher Tate5d99d472016-05-06 17:59:27 -0700828 public int getWallpaperIdForUser(@SetWallpaperFlags int which, int userId) {
Christopher Tatee409f0e2016-03-21 14:53:15 -0700829 try {
830 if (sGlobals.mService == null) {
831 Log.w(TAG, "WallpaperService not running");
Christopher Tate98d609c2016-05-18 17:31:58 -0700832 throw new RuntimeException(new DeadSystemException());
Christopher Tatee409f0e2016-03-21 14:53:15 -0700833 } else {
834 return sGlobals.mService.getWallpaperIdForUser(which, userId);
835 }
836 } catch (RemoteException e) {
837 throw e.rethrowFromSystemServer();
838 }
839 }
840
841 /**
Michael Jurkae8d1bf72013-09-09 15:58:54 +0200842 * Gets an Intent that will launch an activity that crops the given
843 * image and sets the device's wallpaper. If there is a default HOME activity
844 * that supports cropping wallpapers, it will be preferred as the default.
Ying Wang930d4e52013-09-14 11:57:17 -0700845 * Use this method instead of directly creating a {@link #ACTION_CROP_AND_SET_WALLPAPER}
Michael Jurkae8d1bf72013-09-09 15:58:54 +0200846 * intent.
Adam Lesinskibba72d12013-10-11 18:10:56 -0700847 *
848 * @param imageUri The image URI that will be set in the intent. The must be a content
849 * URI and its provider must resolve its type to "image/*"
850 *
851 * @throws IllegalArgumentException if the URI is not a content URI or its MIME type is
852 * not "image/*"
Michael Jurkae8d1bf72013-09-09 15:58:54 +0200853 */
854 public Intent getCropAndSetWallpaperIntent(Uri imageUri) {
Allen Shen1c0f21e2014-04-29 16:16:29 +0800855 if (imageUri == null) {
856 throw new IllegalArgumentException("Image URI must not be null");
857 }
858
Adam Lesinskibba72d12013-10-11 18:10:56 -0700859 if (!ContentResolver.SCHEME_CONTENT.equals(imageUri.getScheme())) {
860 throw new IllegalArgumentException("Image URI must be of the "
861 + ContentResolver.SCHEME_CONTENT + " scheme type");
862 }
863
Michael Jurkae8d1bf72013-09-09 15:58:54 +0200864 final PackageManager packageManager = mContext.getPackageManager();
865 Intent cropAndSetWallpaperIntent =
866 new Intent(ACTION_CROP_AND_SET_WALLPAPER, imageUri);
867 cropAndSetWallpaperIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
868
869 // Find out if the default HOME activity supports CROP_AND_SET_WALLPAPER
870 Intent homeIntent = new Intent(Intent.ACTION_MAIN).addCategory(Intent.CATEGORY_HOME);
871 ResolveInfo resolvedHome = packageManager.resolveActivity(homeIntent,
872 PackageManager.MATCH_DEFAULT_ONLY);
873 if (resolvedHome != null) {
874 cropAndSetWallpaperIntent.setPackage(resolvedHome.activityInfo.packageName);
875
876 List<ResolveInfo> cropAppList = packageManager.queryIntentActivities(
877 cropAndSetWallpaperIntent, 0);
878 if (cropAppList.size() > 0) {
879 return cropAndSetWallpaperIntent;
880 }
881 }
882
883 // fallback crop activity
Oren Blasberg60598ea0a2016-02-26 11:01:23 -0800884 final String cropperPackage = mContext.getString(
885 com.android.internal.R.string.config_wallpaperCropperPackage);
886 cropAndSetWallpaperIntent.setPackage(cropperPackage);
Adam Lesinskibba72d12013-10-11 18:10:56 -0700887 List<ResolveInfo> cropAppList = packageManager.queryIntentActivities(
888 cropAndSetWallpaperIntent, 0);
889 if (cropAppList.size() > 0) {
890 return cropAndSetWallpaperIntent;
891 }
892 // If the URI is not of the right type, or for some reason the system wallpaper
893 // cropper doesn't exist, return null
894 throw new IllegalArgumentException("Cannot use passed URI to set wallpaper; " +
895 "check that the type returned by ContentProvider matches image/*");
Michael Jurkae8d1bf72013-09-09 15:58:54 +0200896 }
897
Dianne Hackborneb034652009-09-07 00:49:58 -0700898 /**
Dianne Hackborn8cc6a502009-08-05 21:29:42 -0700899 * Change the current system wallpaper to the bitmap in the given resource.
900 * The resource is opened as a raw data stream and copied into the
901 * wallpaper; it must be a valid PNG or JPEG image. On success, the intent
902 * {@link Intent#ACTION_WALLPAPER_CHANGED} is broadcast.
903 *
Nicolas Falliere9530e3a2012-06-18 17:21:06 -0700904 * <p>This method requires the caller to hold the permission
905 * {@link android.Manifest.permission#SET_WALLPAPER}.
906 *
Christopher Tateedf7d042016-03-29 18:24:25 -0700907 * @param resid The resource ID of the bitmap to be used as the wallpaper image
Dianne Hackborn8cc6a502009-08-05 21:29:42 -0700908 *
Michael Jurkab668d0b2013-10-04 15:11:05 -0700909 * @throws IOException If an error occurs reverting to the built-in
Dianne Hackborn8cc6a502009-08-05 21:29:42 -0700910 * wallpaper.
911 */
Tor Norbye7b9c9122013-05-30 16:48:33 -0700912 public void setResource(@RawRes int resid) throws IOException {
Edward Savage-Jones6009d9d2016-08-30 18:44:19 +0200913 setResource(resid, FLAG_SYSTEM | FLAG_LOCK);
Christopher Tatead3c2592016-01-20 18:13:17 -0800914 }
915
916 /**
Christopher Tateedf7d042016-03-29 18:24:25 -0700917 * Version of {@link #setResource(int)} that allows the caller to specify which
918 * of the supported wallpaper categories to set.
Christopher Tatead3c2592016-01-20 18:13:17 -0800919 *
Christopher Tateedf7d042016-03-29 18:24:25 -0700920 * @param resid The resource ID of the bitmap to be used as the wallpaper image
921 * @param which Flags indicating which wallpaper(s) to configure with the new imagery
Christopher Tatead3c2592016-01-20 18:13:17 -0800922 *
Christopher Tateedf7d042016-03-29 18:24:25 -0700923 * @see #FLAG_LOCK
924 * @see #FLAG_SYSTEM
Christopher Tatead3c2592016-01-20 18:13:17 -0800925 *
926 * @return An integer ID assigned to the newly active wallpaper; or zero on failure.
927 *
928 * @throws IOException
929 */
930 public int setResource(@RawRes int resid, @SetWallpaperFlags int which)
931 throws IOException {
Mike Lockwoodc067c9c2011-10-31 12:50:12 -0400932 if (sGlobals.mService == null) {
933 Log.w(TAG, "WallpaperService not running");
Christopher Tate98d609c2016-05-18 17:31:58 -0700934 throw new RuntimeException(new DeadSystemException());
Mike Lockwoodc067c9c2011-10-31 12:50:12 -0400935 }
Christopher Tatead3c2592016-01-20 18:13:17 -0800936 final Bundle result = new Bundle();
Christopher Tate1e1e2e02016-01-25 15:34:36 -0800937 final WallpaperSetCompletion completion = new WallpaperSetCompletion();
Dianne Hackborn8cc6a502009-08-05 21:29:42 -0700938 try {
939 Resources resources = mContext.getResources();
940 /* Set the wallpaper to the default values */
Dianne Hackborn840c3a22009-09-02 17:35:17 -0700941 ParcelFileDescriptor fd = sGlobals.mService.setWallpaper(
Christopher Tatead3c2592016-01-20 18:13:17 -0800942 "res:" + resources.getResourceName(resid),
Jorim Jaggi6c902d02016-08-18 10:44:54 -0700943 mContext.getOpPackageName(), null, false, result, which, completion,
944 UserHandle.myUserId());
Dianne Hackborn8cc6a502009-08-05 21:29:42 -0700945 if (fd != null) {
946 FileOutputStream fos = null;
Christopher Tate1e1e2e02016-01-25 15:34:36 -0800947 boolean ok = false;
Dianne Hackborn8cc6a502009-08-05 21:29:42 -0700948 try {
949 fos = new ParcelFileDescriptor.AutoCloseOutputStream(fd);
Christopher Tatead3c2592016-01-20 18:13:17 -0800950 copyStreamToWallpaperFile(resources.openRawResource(resid), fos);
Christopher Tate1e1e2e02016-01-25 15:34:36 -0800951 // The 'close()' is the trigger for any server-side image manipulation,
952 // so we must do that before waiting for completion.
953 fos.close();
954 completion.waitForCompletion();
Dianne Hackborn8cc6a502009-08-05 21:29:42 -0700955 } finally {
Christopher Tate1e1e2e02016-01-25 15:34:36 -0800956 // Might be redundant but completion shouldn't wait unless the write
957 // succeeded; this is a fallback if it threw past the close+wait.
Christopher Tateffa6a882015-12-11 15:08:46 -0800958 IoUtils.closeQuietly(fos);
Dianne Hackborn8cc6a502009-08-05 21:29:42 -0700959 }
960 }
961 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -0700962 throw e.rethrowFromSystemServer();
Dianne Hackborn8cc6a502009-08-05 21:29:42 -0700963 }
Christopher Tatead3c2592016-01-20 18:13:17 -0800964 return result.getInt(EXTRA_NEW_WALLPAPER_ID, 0);
Dianne Hackborn8cc6a502009-08-05 21:29:42 -0700965 }
Christopher Tateffa6a882015-12-11 15:08:46 -0800966
Dianne Hackborn8cc6a502009-08-05 21:29:42 -0700967 /**
968 * Change the current system wallpaper to a bitmap. The given bitmap is
969 * converted to a PNG and stored as the wallpaper. On success, the intent
970 * {@link Intent#ACTION_WALLPAPER_CHANGED} is broadcast.
971 *
Christopher Tateffa6a882015-12-11 15:08:46 -0800972 * <p>This method is equivalent to calling
973 * {@link #setBitmap(Bitmap, Rect, boolean)} and passing {@code null} for the
974 * {@code visibleCrop} rectangle and {@code true} for the {@code allowBackup}
975 * parameter.
976 *
Nicolas Falliere9530e3a2012-06-18 17:21:06 -0700977 * <p>This method requires the caller to hold the permission
978 * {@link android.Manifest.permission#SET_WALLPAPER}.
979 *
Christopher Tatead3c2592016-01-20 18:13:17 -0800980 * @param bitmap The bitmap to be used as the new system wallpaper.
Dianne Hackborn8cc6a502009-08-05 21:29:42 -0700981 *
Christopher Tateffa6a882015-12-11 15:08:46 -0800982 * @throws IOException If an error occurs when attempting to set the wallpaper
983 * to the provided image.
Dianne Hackborn8cc6a502009-08-05 21:29:42 -0700984 */
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700985 public void setBitmap(Bitmap bitmap) throws IOException {
Christopher Tateffa6a882015-12-11 15:08:46 -0800986 setBitmap(bitmap, null, true);
987 }
988
989 /**
990 * Change the current system wallpaper to a bitmap, specifying a hint about
991 * which subrectangle of the full image is to be visible. The OS will then
992 * try to best present the given portion of the full image as the static system
993 * wallpaper image. On success, the intent
994 * {@link Intent#ACTION_WALLPAPER_CHANGED} is broadcast.
995 *
996 * <p>Passing {@code null} as the {@code visibleHint} parameter is equivalent to
997 * passing (0, 0, {@code fullImage.getWidth()}, {@code fullImage.getHeight()}).
998 *
999 * <p>This method requires the caller to hold the permission
1000 * {@link android.Manifest.permission#SET_WALLPAPER}.
1001 *
Christopher Tatead3c2592016-01-20 18:13:17 -08001002 * @param fullImage A bitmap that will supply the wallpaper imagery.
Christopher Tateffa6a882015-12-11 15:08:46 -08001003 * @param visibleCropHint The rectangular subregion of {@code fullImage} that should be
1004 * displayed as wallpaper. Passing {@code null} for this parameter means that
1005 * the full image should be displayed if possible given the image's and device's
Christopher Tatead3c2592016-01-20 18:13:17 -08001006 * aspect ratios, etc.
Christopher Tateffa6a882015-12-11 15:08:46 -08001007 * @param allowBackup {@code true} if the OS is permitted to back up this wallpaper
1008 * image for restore to a future device; {@code false} otherwise.
1009 *
Christopher Tatead3c2592016-01-20 18:13:17 -08001010 * @return An integer ID assigned to the newly active wallpaper; or zero on failure.
1011 *
Christopher Tateffa6a882015-12-11 15:08:46 -08001012 * @throws IOException If an error occurs when attempting to set the wallpaper
1013 * to the provided image.
1014 * @throws IllegalArgumentException If the {@code visibleCropHint} rectangle is
1015 * empty or invalid.
1016 */
Christopher Tatead3c2592016-01-20 18:13:17 -08001017 public int setBitmap(Bitmap fullImage, Rect visibleCropHint, boolean allowBackup)
1018 throws IOException {
Edward Savage-Jones6009d9d2016-08-30 18:44:19 +02001019 return setBitmap(fullImage, visibleCropHint, allowBackup, FLAG_SYSTEM | FLAG_LOCK);
Christopher Tatead3c2592016-01-20 18:13:17 -08001020 }
1021
1022 /**
Christopher Tatead3c2592016-01-20 18:13:17 -08001023 * Version of {@link #setBitmap(Bitmap, Rect, boolean)} that allows the caller
1024 * to specify which of the supported wallpaper categories to set.
1025 *
1026 * @param fullImage A bitmap that will supply the wallpaper imagery.
1027 * @param visibleCropHint The rectangular subregion of {@code fullImage} that should be
1028 * displayed as wallpaper. Passing {@code null} for this parameter means that
1029 * the full image should be displayed if possible given the image's and device's
1030 * aspect ratios, etc.
1031 * @param allowBackup {@code true} if the OS is permitted to back up this wallpaper
1032 * image for restore to a future device; {@code false} otherwise.
1033 * @param which Flags indicating which wallpaper(s) to configure with the new imagery.
1034 *
Christopher Tateedf7d042016-03-29 18:24:25 -07001035 * @see #FLAG_LOCK
1036 * @see #FLAG_SYSTEM
Christopher Tatead3c2592016-01-20 18:13:17 -08001037 *
1038 * @return An integer ID assigned to the newly active wallpaper; or zero on failure.
1039 *
1040 * @throws IOException
1041 */
1042 public int setBitmap(Bitmap fullImage, Rect visibleCropHint,
1043 boolean allowBackup, @SetWallpaperFlags int which)
Christopher Tateffa6a882015-12-11 15:08:46 -08001044 throws IOException {
Jorim Jaggi6c902d02016-08-18 10:44:54 -07001045 return setBitmap(fullImage, visibleCropHint, allowBackup, which,
1046 UserHandle.myUserId());
1047 }
1048
1049 /**
1050 * Like {@link #setBitmap(Bitmap, Rect, boolean, int)}, but allows to pass in an explicit user
1051 * id. If the user id doesn't match the user id the process is running under, calling this
1052 * requires permission {@link android.Manifest.permission#INTERACT_ACROSS_USERS_FULL}.
1053 * @hide
1054 */
1055 public int setBitmap(Bitmap fullImage, Rect visibleCropHint,
1056 boolean allowBackup, @SetWallpaperFlags int which, int userId)
1057 throws IOException {
Christopher Tateffa6a882015-12-11 15:08:46 -08001058 validateRect(visibleCropHint);
Mike Lockwoodc067c9c2011-10-31 12:50:12 -04001059 if (sGlobals.mService == null) {
1060 Log.w(TAG, "WallpaperService not running");
Christopher Tate98d609c2016-05-18 17:31:58 -07001061 throw new RuntimeException(new DeadSystemException());
Mike Lockwoodc067c9c2011-10-31 12:50:12 -04001062 }
Christopher Tatead3c2592016-01-20 18:13:17 -08001063 final Bundle result = new Bundle();
Christopher Tate1e1e2e02016-01-25 15:34:36 -08001064 final WallpaperSetCompletion completion = new WallpaperSetCompletion();
Dianne Hackborn8cc6a502009-08-05 21:29:42 -07001065 try {
Benjamin Franzf3ece362015-02-11 10:51:10 +00001066 ParcelFileDescriptor fd = sGlobals.mService.setWallpaper(null,
Christopher Tated7faf532016-02-25 12:43:38 -08001067 mContext.getOpPackageName(), visibleCropHint, allowBackup,
Jorim Jaggi6c902d02016-08-18 10:44:54 -07001068 result, which, completion, userId);
Christopher Tatead3c2592016-01-20 18:13:17 -08001069 if (fd != null) {
1070 FileOutputStream fos = null;
1071 try {
1072 fos = new ParcelFileDescriptor.AutoCloseOutputStream(fd);
1073 fullImage.compress(Bitmap.CompressFormat.PNG, 90, fos);
Christopher Tate1e1e2e02016-01-25 15:34:36 -08001074 fos.close();
1075 completion.waitForCompletion();
Christopher Tatead3c2592016-01-20 18:13:17 -08001076 } finally {
1077 IoUtils.closeQuietly(fos);
1078 }
Dianne Hackborn8cc6a502009-08-05 21:29:42 -07001079 }
1080 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07001081 throw e.rethrowFromSystemServer();
Dianne Hackborn8cc6a502009-08-05 21:29:42 -07001082 }
Christopher Tatead3c2592016-01-20 18:13:17 -08001083 return result.getInt(EXTRA_NEW_WALLPAPER_ID, 0);
Dianne Hackborn8cc6a502009-08-05 21:29:42 -07001084 }
1085
Christopher Tateffa6a882015-12-11 15:08:46 -08001086 private final void validateRect(Rect rect) {
1087 if (rect != null && rect.isEmpty()) {
1088 throw new IllegalArgumentException("visibleCrop rectangle must be valid and non-empty");
1089 }
1090 }
1091
Dianne Hackborn8cc6a502009-08-05 21:29:42 -07001092 /**
1093 * Change the current system wallpaper to a specific byte stream. The
1094 * give InputStream is copied into persistent storage and will now be
1095 * used as the wallpaper. Currently it must be either a JPEG or PNG
1096 * image. On success, the intent {@link Intent#ACTION_WALLPAPER_CHANGED}
1097 * is broadcast.
1098 *
Christopher Tateffa6a882015-12-11 15:08:46 -08001099 * <p>This method is equivalent to calling
1100 * {@link #setStream(InputStream, Rect, boolean)} and passing {@code null} for the
1101 * {@code visibleCrop} rectangle and {@code true} for the {@code allowBackup}
1102 * parameter.
1103 *
Nicolas Falliere9530e3a2012-06-18 17:21:06 -07001104 * <p>This method requires the caller to hold the permission
1105 * {@link android.Manifest.permission#SET_WALLPAPER}.
1106 *
Christopher Tate98d609c2016-05-18 17:31:58 -07001107 * @param bitmapData A stream containing the raw data to install as a wallpaper. This
1108 * data can be in any format handled by {@link BitmapRegionDecoder}.
Dianne Hackborn8cc6a502009-08-05 21:29:42 -07001109 *
Christopher Tateffa6a882015-12-11 15:08:46 -08001110 * @throws IOException If an error occurs when attempting to set the wallpaper
1111 * based on the provided image data.
Dianne Hackborn8cc6a502009-08-05 21:29:42 -07001112 */
Christopher Tateffa6a882015-12-11 15:08:46 -08001113 public void setStream(InputStream bitmapData) throws IOException {
1114 setStream(bitmapData, null, true);
1115 }
1116
Christopher Tatead3c2592016-01-20 18:13:17 -08001117 private void copyStreamToWallpaperFile(InputStream data, FileOutputStream fos)
Christopher Tateffa6a882015-12-11 15:08:46 -08001118 throws IOException {
1119 byte[] buffer = new byte[32768];
1120 int amt;
1121 while ((amt=data.read(buffer)) > 0) {
1122 fos.write(buffer, 0, amt);
1123 }
1124 }
1125
1126 /**
1127 * Change the current system wallpaper to a specific byte stream, specifying a
1128 * hint about which subrectangle of the full image is to be visible. The OS will
1129 * then try to best present the given portion of the full image as the static system
1130 * wallpaper image. The data from the given InputStream is copied into persistent
1131 * storage and will then be used as the system wallpaper. Currently the data must
1132 * be either a JPEG or PNG image. On success, the intent
1133 * {@link Intent#ACTION_WALLPAPER_CHANGED} is broadcast.
1134 *
1135 * <p>This method requires the caller to hold the permission
1136 * {@link android.Manifest.permission#SET_WALLPAPER}.
1137 *
Christopher Tate98d609c2016-05-18 17:31:58 -07001138 * @param bitmapData A stream containing the raw data to install as a wallpaper. This
1139 * data can be in any format handled by {@link BitmapRegionDecoder}.
Christopher Tateffa6a882015-12-11 15:08:46 -08001140 * @param visibleCropHint The rectangular subregion of the streamed image that should be
1141 * displayed as wallpaper. Passing {@code null} for this parameter means that
1142 * the full image should be displayed if possible given the image's and device's
1143 * aspect ratios, etc.
1144 * @param allowBackup {@code true} if the OS is permitted to back up this wallpaper
1145 * image for restore to a future device; {@code false} otherwise.
Christopher Tate98d609c2016-05-18 17:31:58 -07001146 * @return An integer ID assigned to the newly active wallpaper; or zero on failure.
1147 *
1148 * @see #getWallpaperId(int)
Christopher Tateffa6a882015-12-11 15:08:46 -08001149 *
1150 * @throws IOException If an error occurs when attempting to set the wallpaper
1151 * based on the provided image data.
1152 * @throws IllegalArgumentException If the {@code visibleCropHint} rectangle is
1153 * empty or invalid.
1154 */
Christopher Tatead3c2592016-01-20 18:13:17 -08001155 public int setStream(InputStream bitmapData, Rect visibleCropHint, boolean allowBackup)
Christopher Tateffa6a882015-12-11 15:08:46 -08001156 throws IOException {
Edward Savage-Jones6009d9d2016-08-30 18:44:19 +02001157 return setStream(bitmapData, visibleCropHint, allowBackup, FLAG_SYSTEM | FLAG_LOCK);
Christopher Tatead3c2592016-01-20 18:13:17 -08001158 }
1159
1160 /**
1161 * Version of {@link #setStream(InputStream, Rect, boolean)} that allows the caller
1162 * to specify which of the supported wallpaper categories to set.
1163 *
Christopher Tate98d609c2016-05-18 17:31:58 -07001164 * @param bitmapData A stream containing the raw data to install as a wallpaper. This
1165 * data can be in any format handled by {@link BitmapRegionDecoder}.
Christopher Tatead3c2592016-01-20 18:13:17 -08001166 * @param visibleCropHint The rectangular subregion of the streamed image that should be
1167 * displayed as wallpaper. Passing {@code null} for this parameter means that
1168 * the full image should be displayed if possible given the image's and device's
1169 * aspect ratios, etc.
1170 * @param allowBackup {@code true} if the OS is permitted to back up this wallpaper
1171 * image for restore to a future device; {@code false} otherwise.
1172 * @param which Flags indicating which wallpaper(s) to configure with the new imagery.
Christopher Tate98d609c2016-05-18 17:31:58 -07001173 * @return An integer ID assigned to the newly active wallpaper; or zero on failure.
Christopher Tatead3c2592016-01-20 18:13:17 -08001174 *
Christopher Tate98d609c2016-05-18 17:31:58 -07001175 * @see #getWallpaperId(int)
Christopher Tateedf7d042016-03-29 18:24:25 -07001176 * @see #FLAG_LOCK
1177 * @see #FLAG_SYSTEM
Christopher Tatead3c2592016-01-20 18:13:17 -08001178 *
1179 * @throws IOException
1180 */
1181 public int setStream(InputStream bitmapData, Rect visibleCropHint,
1182 boolean allowBackup, @SetWallpaperFlags int which)
1183 throws IOException {
Christopher Tateffa6a882015-12-11 15:08:46 -08001184 validateRect(visibleCropHint);
Mike Lockwoodc067c9c2011-10-31 12:50:12 -04001185 if (sGlobals.mService == null) {
1186 Log.w(TAG, "WallpaperService not running");
Christopher Tate98d609c2016-05-18 17:31:58 -07001187 throw new RuntimeException(new DeadSystemException());
Mike Lockwoodc067c9c2011-10-31 12:50:12 -04001188 }
Christopher Tatead3c2592016-01-20 18:13:17 -08001189 final Bundle result = new Bundle();
Christopher Tate1e1e2e02016-01-25 15:34:36 -08001190 final WallpaperSetCompletion completion = new WallpaperSetCompletion();
Dianne Hackborn8cc6a502009-08-05 21:29:42 -07001191 try {
Benjamin Franzf3ece362015-02-11 10:51:10 +00001192 ParcelFileDescriptor fd = sGlobals.mService.setWallpaper(null,
Christopher Tated7faf532016-02-25 12:43:38 -08001193 mContext.getOpPackageName(), visibleCropHint, allowBackup,
Jorim Jaggi6c902d02016-08-18 10:44:54 -07001194 result, which, completion, UserHandle.myUserId());
Christopher Tatead3c2592016-01-20 18:13:17 -08001195 if (fd != null) {
1196 FileOutputStream fos = null;
1197 try {
1198 fos = new ParcelFileDescriptor.AutoCloseOutputStream(fd);
1199 copyStreamToWallpaperFile(bitmapData, fos);
Christopher Tate1e1e2e02016-01-25 15:34:36 -08001200 fos.close();
1201 completion.waitForCompletion();
Christopher Tatead3c2592016-01-20 18:13:17 -08001202 } finally {
1203 IoUtils.closeQuietly(fos);
1204 }
Dianne Hackborn8cc6a502009-08-05 21:29:42 -07001205 }
1206 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07001207 throw e.rethrowFromSystemServer();
Dianne Hackborn8cc6a502009-08-05 21:29:42 -07001208 }
Christopher Tatead3c2592016-01-20 18:13:17 -08001209
1210 return result.getInt(EXTRA_NEW_WALLPAPER_ID, 0);
Dianne Hackborn8cc6a502009-08-05 21:29:42 -07001211 }
1212
Dianne Hackborn8cc6a502009-08-05 21:29:42 -07001213 /**
Dianne Hackborn5dc5a002012-09-15 19:33:48 -07001214 * Return whether any users are currently set to use the wallpaper
1215 * with the given resource ID. That is, their wallpaper has been
1216 * set through {@link #setResource(int)} with the same resource id.
1217 */
Tor Norbye7b9c9122013-05-30 16:48:33 -07001218 public boolean hasResourceWallpaper(@RawRes int resid) {
Dianne Hackborn5dc5a002012-09-15 19:33:48 -07001219 if (sGlobals.mService == null) {
1220 Log.w(TAG, "WallpaperService not running");
Christopher Tate98d609c2016-05-18 17:31:58 -07001221 throw new RuntimeException(new DeadSystemException());
Dianne Hackborn5dc5a002012-09-15 19:33:48 -07001222 }
1223 try {
1224 Resources resources = mContext.getResources();
1225 String name = "res:" + resources.getResourceName(resid);
1226 return sGlobals.mService.hasNamedWallpaper(name);
1227 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07001228 throw e.rethrowFromSystemServer();
Dianne Hackborn5dc5a002012-09-15 19:33:48 -07001229 }
1230 }
1231
1232 /**
Dianne Hackborn8cc6a502009-08-05 21:29:42 -07001233 * Returns the desired minimum width for the wallpaper. Callers of
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001234 * {@link #setBitmap(android.graphics.Bitmap)} or
1235 * {@link #setStream(java.io.InputStream)} should check this value
Dianne Hackborn8cc6a502009-08-05 21:29:42 -07001236 * beforehand to make sure the supplied wallpaper respects the desired
1237 * minimum width.
1238 *
1239 * If the returned value is <= 0, the caller should use the width of
1240 * the default display instead.
1241 *
1242 * @return The desired minimum width for the wallpaper. This value should
1243 * be honored by applications that set the wallpaper but it is not
1244 * mandatory.
1245 */
1246 public int getDesiredMinimumWidth() {
Mike Lockwoodc067c9c2011-10-31 12:50:12 -04001247 if (sGlobals.mService == null) {
1248 Log.w(TAG, "WallpaperService not running");
Christopher Tate98d609c2016-05-18 17:31:58 -07001249 throw new RuntimeException(new DeadSystemException());
Mike Lockwoodc067c9c2011-10-31 12:50:12 -04001250 }
Dianne Hackborn8cc6a502009-08-05 21:29:42 -07001251 try {
Dianne Hackborn840c3a22009-09-02 17:35:17 -07001252 return sGlobals.mService.getWidthHint();
Dianne Hackborn8cc6a502009-08-05 21:29:42 -07001253 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07001254 throw e.rethrowFromSystemServer();
Dianne Hackborn8cc6a502009-08-05 21:29:42 -07001255 }
1256 }
1257
1258 /**
1259 * Returns the desired minimum height for the wallpaper. Callers of
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001260 * {@link #setBitmap(android.graphics.Bitmap)} or
1261 * {@link #setStream(java.io.InputStream)} should check this value
Dianne Hackborn8cc6a502009-08-05 21:29:42 -07001262 * beforehand to make sure the supplied wallpaper respects the desired
1263 * minimum height.
1264 *
1265 * If the returned value is <= 0, the caller should use the height of
1266 * the default display instead.
1267 *
1268 * @return The desired minimum height for the wallpaper. This value should
1269 * be honored by applications that set the wallpaper but it is not
1270 * mandatory.
1271 */
1272 public int getDesiredMinimumHeight() {
Mike Lockwoodc067c9c2011-10-31 12:50:12 -04001273 if (sGlobals.mService == null) {
1274 Log.w(TAG, "WallpaperService not running");
Christopher Tate98d609c2016-05-18 17:31:58 -07001275 throw new RuntimeException(new DeadSystemException());
Mike Lockwoodc067c9c2011-10-31 12:50:12 -04001276 }
Dianne Hackborn8cc6a502009-08-05 21:29:42 -07001277 try {
Dianne Hackborn840c3a22009-09-02 17:35:17 -07001278 return sGlobals.mService.getHeightHint();
Dianne Hackborn8cc6a502009-08-05 21:29:42 -07001279 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07001280 throw e.rethrowFromSystemServer();
Dianne Hackborn8cc6a502009-08-05 21:29:42 -07001281 }
1282 }
1283
1284 /**
1285 * For use only by the current home application, to specify the size of
1286 * wallpaper it would like to use. This allows such applications to have
1287 * a virtual wallpaper that is larger than the physical screen, matching
1288 * the size of their workspace.
Dianne Hackbornc5bf7582012-04-25 19:12:07 -07001289 *
1290 * <p>Note developers, who don't seem to be reading this. This is
Christopher Tateffa6a882015-12-11 15:08:46 -08001291 * for <em>home apps</em> to tell what size wallpaper they would like.
1292 * Nobody else should be calling this! Certainly not other non-home
Dianne Hackbornc5bf7582012-04-25 19:12:07 -07001293 * apps that change the wallpaper. Those apps are supposed to
1294 * <b>retrieve</b> the suggested size so they can construct a wallpaper
1295 * that matches it.
1296 *
Nicolas Falliere9530e3a2012-06-18 17:21:06 -07001297 * <p>This method requires the caller to hold the permission
1298 * {@link android.Manifest.permission#SET_WALLPAPER_HINTS}.
1299 *
Dianne Hackborn8cc6a502009-08-05 21:29:42 -07001300 * @param minimumWidth Desired minimum width
1301 * @param minimumHeight Desired minimum height
1302 */
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001303 public void suggestDesiredDimensions(int minimumWidth, int minimumHeight) {
Dianne Hackborn8cc6a502009-08-05 21:29:42 -07001304 try {
Donghan Ryu289c2732011-11-14 18:56:11 -08001305 /**
1306 * The framework makes no attempt to limit the window size
1307 * to the maximum texture size. Any window larger than this
1308 * cannot be composited.
1309 *
1310 * Read maximum texture size from system property and scale down
1311 * minimumWidth and minimumHeight accordingly.
1312 */
1313 int maximumTextureSize;
1314 try {
1315 maximumTextureSize = SystemProperties.getInt("sys.max_texture_size", 0);
1316 } catch (Exception e) {
1317 maximumTextureSize = 0;
1318 }
1319
1320 if (maximumTextureSize > 0) {
1321 if ((minimumWidth > maximumTextureSize) ||
1322 (minimumHeight > maximumTextureSize)) {
1323 float aspect = (float)minimumHeight / (float)minimumWidth;
1324 if (minimumWidth > minimumHeight) {
1325 minimumWidth = maximumTextureSize;
1326 minimumHeight = (int)((minimumWidth * aspect) + 0.5);
1327 } else {
1328 minimumHeight = maximumTextureSize;
1329 minimumWidth = (int)((minimumHeight / aspect) + 0.5);
1330 }
1331 }
1332 }
1333
Mike Lockwoodc067c9c2011-10-31 12:50:12 -04001334 if (sGlobals.mService == null) {
1335 Log.w(TAG, "WallpaperService not running");
Christopher Tate98d609c2016-05-18 17:31:58 -07001336 throw new RuntimeException(new DeadSystemException());
Mike Lockwoodc067c9c2011-10-31 12:50:12 -04001337 } else {
Benjamin Franzf3ece362015-02-11 10:51:10 +00001338 sGlobals.mService.setDimensionHints(minimumWidth, minimumHeight,
1339 mContext.getOpPackageName());
Mike Lockwoodc067c9c2011-10-31 12:50:12 -04001340 }
Dianne Hackborn8cc6a502009-08-05 21:29:42 -07001341 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07001342 throw e.rethrowFromSystemServer();
Dianne Hackborn8cc6a502009-08-05 21:29:42 -07001343 }
1344 }
Adam Cohen041a0ba2011-11-09 20:10:27 -08001345
Dianne Hackborn8cc6a502009-08-05 21:29:42 -07001346 /**
Dianne Hackborn067e5f62014-09-07 23:14:30 -07001347 * Specify extra padding that the wallpaper should have outside of the display.
1348 * That is, the given padding supplies additional pixels the wallpaper should extend
1349 * outside of the display itself.
1350 * @param padding The number of pixels the wallpaper should extend beyond the display,
1351 * on its left, top, right, and bottom sides.
1352 * @hide
1353 */
1354 @SystemApi
1355 public void setDisplayPadding(Rect padding) {
1356 try {
1357 if (sGlobals.mService == null) {
1358 Log.w(TAG, "WallpaperService not running");
Christopher Tate98d609c2016-05-18 17:31:58 -07001359 throw new RuntimeException(new DeadSystemException());
Dianne Hackborn067e5f62014-09-07 23:14:30 -07001360 } else {
Benjamin Franzf3ece362015-02-11 10:51:10 +00001361 sGlobals.mService.setDisplayPadding(padding, mContext.getOpPackageName());
Dianne Hackborn067e5f62014-09-07 23:14:30 -07001362 }
1363 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07001364 throw e.rethrowFromSystemServer();
Dianne Hackborn067e5f62014-09-07 23:14:30 -07001365 }
1366 }
1367
1368 /**
1369 * Apply a raw offset to the wallpaper window. Should only be used in
1370 * combination with {@link #setDisplayPadding(android.graphics.Rect)} when you
1371 * have ensured that the wallpaper will extend outside of the display area so that
1372 * it can be moved without leaving part of the display uncovered.
1373 * @param x The offset, in pixels, to apply to the left edge.
1374 * @param y The offset, in pixels, to apply to the top edge.
1375 * @hide
1376 */
1377 @SystemApi
1378 public void setDisplayOffset(IBinder windowToken, int x, int y) {
1379 try {
1380 //Log.v(TAG, "Sending new wallpaper display offsets from app...");
1381 WindowManagerGlobal.getWindowSession().setWallpaperDisplayOffset(
1382 windowToken, x, y);
1383 //Log.v(TAG, "...app returning after sending display offset!");
1384 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07001385 throw e.rethrowFromSystemServer();
Dianne Hackborn067e5f62014-09-07 23:14:30 -07001386 }
1387 }
1388
1389 /**
Filip Gruszczynski9fedc722015-01-28 15:59:46 -08001390 * Clear the wallpaper.
1391 *
1392 * @hide
1393 */
1394 @SystemApi
1395 public void clearWallpaper() {
Christopher Tatea2bd5122016-09-22 13:18:05 -07001396 clearWallpaper(FLAG_LOCK, mContext.getUserId());
1397 clearWallpaper(FLAG_SYSTEM, mContext.getUserId());
Christopher Tatebe132e62016-02-10 12:59:49 -08001398 }
1399
1400 /**
1401 * Clear the wallpaper for a specific user. The caller must hold the
1402 * INTERACT_ACROSS_USERS_FULL permission to clear another user's
1403 * wallpaper.
1404 * @hide
1405 */
1406 @SystemApi
Christopher Tate5d99d472016-05-06 17:59:27 -07001407 public void clearWallpaper(@SetWallpaperFlags int which, int userId) {
Filip Gruszczynski9fedc722015-01-28 15:59:46 -08001408 if (sGlobals.mService == null) {
1409 Log.w(TAG, "WallpaperService not running");
Christopher Tate98d609c2016-05-18 17:31:58 -07001410 throw new RuntimeException(new DeadSystemException());
Filip Gruszczynski9fedc722015-01-28 15:59:46 -08001411 }
1412 try {
Christopher Tatebe132e62016-02-10 12:59:49 -08001413 sGlobals.mService.clearWallpaper(mContext.getOpPackageName(), which, userId);
Filip Gruszczynski9fedc722015-01-28 15:59:46 -08001414 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07001415 throw e.rethrowFromSystemServer();
Filip Gruszczynski9fedc722015-01-28 15:59:46 -08001416 }
1417 }
1418
1419 /**
1420 * Set the live wallpaper.
1421 *
1422 * This can only be called by packages with android.permission.SET_WALLPAPER_COMPONENT
1423 * permission.
1424 *
1425 * @hide
1426 */
1427 @SystemApi
1428 public boolean setWallpaperComponent(ComponentName name) {
Adrian Roos40ea0832016-07-14 14:19:55 -07001429 return setWallpaperComponent(name, UserHandle.myUserId());
1430 }
1431
1432 /**
1433 * Set the live wallpaper.
1434 *
1435 * This can only be called by packages with android.permission.SET_WALLPAPER_COMPONENT
1436 * permission. The caller must hold the INTERACT_ACROSS_USERS_FULL permission to change
1437 * another user's wallpaper.
1438 *
1439 * @hide
1440 */
1441 public boolean setWallpaperComponent(ComponentName name, int userId) {
Filip Gruszczynski9fedc722015-01-28 15:59:46 -08001442 if (sGlobals.mService == null) {
1443 Log.w(TAG, "WallpaperService not running");
Christopher Tate98d609c2016-05-18 17:31:58 -07001444 throw new RuntimeException(new DeadSystemException());
Filip Gruszczynski9fedc722015-01-28 15:59:46 -08001445 }
1446 try {
Adrian Roos40ea0832016-07-14 14:19:55 -07001447 sGlobals.mService.setWallpaperComponentChecked(name, mContext.getOpPackageName(),
1448 userId);
Filip Gruszczynski9fedc722015-01-28 15:59:46 -08001449 return true;
1450 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07001451 throw e.rethrowFromSystemServer();
Filip Gruszczynski9fedc722015-01-28 15:59:46 -08001452 }
Filip Gruszczynski9fedc722015-01-28 15:59:46 -08001453 }
1454
1455 /**
Christopher Tateffa6a882015-12-11 15:08:46 -08001456 * Set the display position of the current wallpaper within any larger space, when
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07001457 * that wallpaper is visible behind the given window. The X and Y offsets
1458 * are floating point numbers ranging from 0 to 1, representing where the
1459 * wallpaper should be positioned within the screen space. These only
Christopher Tateffa6a882015-12-11 15:08:46 -08001460 * make sense when the wallpaper is larger than the display.
Oleksandr Peletskyif2519812016-01-26 20:16:06 +01001461 *
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07001462 * @param windowToken The window who these offsets should be associated
Scott Main8b2e0002009-09-29 18:17:31 -07001463 * with, as returned by {@link android.view.View#getWindowToken()
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07001464 * View.getWindowToken()}.
Marco Nelissenbf6956b2009-11-09 15:21:13 -08001465 * @param xOffset The offset along the X dimension, from 0 to 1.
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07001466 * @param yOffset The offset along the Y dimension, from 0 to 1.
1467 */
1468 public void setWallpaperOffsets(IBinder windowToken, float xOffset, float yOffset) {
Adam Cohen791a6332012-01-12 14:38:38 -08001469 try {
1470 //Log.v(TAG, "Sending new wallpaper offsets from app...");
Jeff Brownf9e989d2013-04-04 23:04:03 -07001471 WindowManagerGlobal.getWindowSession().setWallpaperPosition(
Adam Cohen791a6332012-01-12 14:38:38 -08001472 windowToken, xOffset, yOffset, mWallpaperXStep, mWallpaperYStep);
1473 //Log.v(TAG, "...app returning after sending offsets!");
1474 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07001475 throw e.rethrowFromSystemServer();
Adam Cohen791a6332012-01-12 14:38:38 -08001476 }
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07001477 }
Adam Cohen041a0ba2011-11-09 20:10:27 -08001478
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07001479 /**
Marco Nelissenbf6956b2009-11-09 15:21:13 -08001480 * For applications that use multiple virtual screens showing a wallpaper,
1481 * specify the step size between virtual screens. For example, if the
Dianne Hackborn23ef7b42009-11-18 18:20:39 -08001482 * launcher has 3 virtual screens, it would specify an xStep of 0.5,
Marco Nelissenbf6956b2009-11-09 15:21:13 -08001483 * since the X offset for those screens are 0.0, 0.5 and 1.0
Oleksandr Peletskyif2519812016-01-26 20:16:06 +01001484 * @param xStep The X offset delta from one screen to the next one
Marco Nelissenbf6956b2009-11-09 15:21:13 -08001485 * @param yStep The Y offset delta from one screen to the next one
1486 */
1487 public void setWallpaperOffsetSteps(float xStep, float yStep) {
1488 mWallpaperXStep = xStep;
1489 mWallpaperYStep = yStep;
1490 }
Oleksandr Peletskyif2519812016-01-26 20:16:06 +01001491
Marco Nelissenbf6956b2009-11-09 15:21:13 -08001492 /**
Dianne Hackborn75804932009-10-20 20:15:20 -07001493 * Send an arbitrary command to the current active wallpaper.
Oleksandr Peletskyif2519812016-01-26 20:16:06 +01001494 *
Dianne Hackborn75804932009-10-20 20:15:20 -07001495 * @param windowToken The window who these offsets should be associated
1496 * with, as returned by {@link android.view.View#getWindowToken()
1497 * View.getWindowToken()}.
1498 * @param action Name of the command to perform. This must be a scoped
1499 * name to avoid collisions, such as "com.mycompany.wallpaper.DOIT".
1500 * @param x Arbitrary integer argument based on command.
1501 * @param y Arbitrary integer argument based on command.
1502 * @param z Arbitrary integer argument based on command.
1503 * @param extras Optional additional information for the command, or null.
1504 */
1505 public void sendWallpaperCommand(IBinder windowToken, String action,
1506 int x, int y, int z, Bundle extras) {
1507 try {
1508 //Log.v(TAG, "Sending new wallpaper offsets from app...");
Jeff Brownf9e989d2013-04-04 23:04:03 -07001509 WindowManagerGlobal.getWindowSession().sendWallpaperCommand(
Dianne Hackborn75804932009-10-20 20:15:20 -07001510 windowToken, action, x, y, z, extras, false);
1511 //Log.v(TAG, "...app returning after sending offsets!");
1512 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07001513 throw e.rethrowFromSystemServer();
Dianne Hackborn75804932009-10-20 20:15:20 -07001514 }
1515 }
Benjamin Franzf3ece362015-02-11 10:51:10 +00001516
1517 /**
1518 * Returns whether wallpapers are supported for the calling user. If this function returns
Oleksandr Peletskyif2519812016-01-26 20:16:06 +01001519 * {@code false}, any attempts to changing the wallpaper will have no effect,
1520 * and any attempt to obtain of the wallpaper will return {@code null}.
Benjamin Franzf3ece362015-02-11 10:51:10 +00001521 */
1522 public boolean isWallpaperSupported() {
1523 if (sGlobals.mService == null) {
1524 Log.w(TAG, "WallpaperService not running");
Christopher Tate98d609c2016-05-18 17:31:58 -07001525 throw new RuntimeException(new DeadSystemException());
Benjamin Franzf3ece362015-02-11 10:51:10 +00001526 } else {
1527 try {
1528 return sGlobals.mService.isWallpaperSupported(mContext.getOpPackageName());
1529 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07001530 throw e.rethrowFromSystemServer();
Benjamin Franzf3ece362015-02-11 10:51:10 +00001531 }
1532 }
Benjamin Franzf3ece362015-02-11 10:51:10 +00001533 }
1534
Dianne Hackborn75804932009-10-20 20:15:20 -07001535 /**
Oleksandr Peletskyif2519812016-01-26 20:16:06 +01001536 * Returns whether the calling package is allowed to set the wallpaper for the calling user.
1537 * If this function returns {@code false}, any attempts to change the wallpaper will have
1538 * no effect. Always returns {@code true} for device owner and profile owner.
1539 *
1540 * @see android.os.UserManager#DISALLOW_SET_WALLPAPER
1541 */
Christopher Tate98d609c2016-05-18 17:31:58 -07001542 public boolean isSetWallpaperAllowed() {
Oleksandr Peletskyif2519812016-01-26 20:16:06 +01001543 if (sGlobals.mService == null) {
1544 Log.w(TAG, "WallpaperService not running");
Christopher Tate98d609c2016-05-18 17:31:58 -07001545 throw new RuntimeException(new DeadSystemException());
Oleksandr Peletskyif2519812016-01-26 20:16:06 +01001546 } else {
1547 try {
Christopher Tate98d609c2016-05-18 17:31:58 -07001548 return sGlobals.mService.isSetWallpaperAllowed(mContext.getOpPackageName());
Oleksandr Peletskyif2519812016-01-26 20:16:06 +01001549 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07001550 throw e.rethrowFromSystemServer();
Oleksandr Peletskyif2519812016-01-26 20:16:06 +01001551 }
1552 }
Oleksandr Peletskyif2519812016-01-26 20:16:06 +01001553 }
1554
1555 /**
Dianne Hackborn72c82ab2009-08-11 21:13:54 -07001556 * Clear the offsets previously associated with this window through
1557 * {@link #setWallpaperOffsets(IBinder, float, float)}. This reverts
1558 * the window to its default state, where it does not cause the wallpaper
1559 * to scroll from whatever its last offsets were.
Oleksandr Peletskyif2519812016-01-26 20:16:06 +01001560 *
Dianne Hackborn72c82ab2009-08-11 21:13:54 -07001561 * @param windowToken The window who these offsets should be associated
Scott Main8b2e0002009-09-29 18:17:31 -07001562 * with, as returned by {@link android.view.View#getWindowToken()
Dianne Hackborn72c82ab2009-08-11 21:13:54 -07001563 * View.getWindowToken()}.
1564 */
1565 public void clearWallpaperOffsets(IBinder windowToken) {
1566 try {
Jeff Brownf9e989d2013-04-04 23:04:03 -07001567 WindowManagerGlobal.getWindowSession().setWallpaperPosition(
Marco Nelissenbf6956b2009-11-09 15:21:13 -08001568 windowToken, -1, -1, -1, -1);
Dianne Hackborn72c82ab2009-08-11 21:13:54 -07001569 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07001570 throw e.rethrowFromSystemServer();
Dianne Hackborn72c82ab2009-08-11 21:13:54 -07001571 }
1572 }
Oleksandr Peletskyif2519812016-01-26 20:16:06 +01001573
Dianne Hackborn72c82ab2009-08-11 21:13:54 -07001574 /**
Christopher Tatebe132e62016-02-10 12:59:49 -08001575 * Remove any currently set system wallpaper, reverting to the system's built-in
Dianne Hackborn8cc6a502009-08-05 21:29:42 -07001576 * wallpaper. On success, the intent {@link Intent#ACTION_WALLPAPER_CHANGED}
1577 * is broadcast.
1578 *
Nicolas Falliere9530e3a2012-06-18 17:21:06 -07001579 * <p>This method requires the caller to hold the permission
1580 * {@link android.Manifest.permission#SET_WALLPAPER}.
1581 *
Michael Jurkab668d0b2013-10-04 15:11:05 -07001582 * @throws IOException If an error occurs reverting to the built-in
Dianne Hackborn8cc6a502009-08-05 21:29:42 -07001583 * wallpaper.
1584 */
1585 public void clear() throws IOException {
Christopher Tate5d99d472016-05-06 17:59:27 -07001586 setStream(openDefaultWallpaper(mContext, FLAG_SYSTEM), null, false);
Jeff Sharkey28f08772014-04-16 09:41:58 -07001587 }
1588
1589 /**
Christopher Tate79a24572016-03-02 14:42:44 -08001590 * Remove one or more currently set wallpapers, reverting to the system default
Christopher Tateedf7d042016-03-29 18:24:25 -07001591 * display for each one. If {@link #FLAG_SYSTEM} is set in the {@code which}
Christopher Tate79a24572016-03-02 14:42:44 -08001592 * parameter, the intent {@link Intent#ACTION_WALLPAPER_CHANGED} will be broadcast
1593 * upon success.
1594 *
Christopher Tateedf7d042016-03-29 18:24:25 -07001595 * @param which A bitwise combination of {@link #FLAG_SYSTEM} or
1596 * {@link #FLAG_LOCK}
Christopher Tate79a24572016-03-02 14:42:44 -08001597 * @throws IOException If an error occurs reverting to the built-in wallpaper.
1598 */
Christopher Tate5d99d472016-05-06 17:59:27 -07001599 public void clear(@SetWallpaperFlags int which) throws IOException {
Christopher Tateedf7d042016-03-29 18:24:25 -07001600 if ((which & FLAG_SYSTEM) != 0) {
Christopher Tate79a24572016-03-02 14:42:44 -08001601 clear();
1602 }
Christopher Tateedf7d042016-03-29 18:24:25 -07001603 if ((which & FLAG_LOCK) != 0) {
1604 clearWallpaper(FLAG_LOCK, mContext.getUserId());
Christopher Tate79a24572016-03-02 14:42:44 -08001605 }
1606 }
1607
1608 /**
Jeff Sharkey28f08772014-04-16 09:41:58 -07001609 * Open stream representing the default static image wallpaper.
1610 *
Christopher Tate5d99d472016-05-06 17:59:27 -07001611 * If the device defines no default wallpaper of the requested kind,
1612 * {@code null} is returned.
1613 *
Jeff Sharkey28f08772014-04-16 09:41:58 -07001614 * @hide
1615 */
Christopher Tate5d99d472016-05-06 17:59:27 -07001616 public static InputStream openDefaultWallpaper(Context context, @SetWallpaperFlags int which) {
1617 final String whichProp;
1618 final int defaultResId;
1619 if (which == FLAG_LOCK) {
Christopher Tate36cb2a7a2016-06-09 13:44:48 -07001620 /* Factory-default lock wallpapers are not yet supported
Christopher Tate5d99d472016-05-06 17:59:27 -07001621 whichProp = PROP_LOCK_WALLPAPER;
1622 defaultResId = com.android.internal.R.drawable.default_lock_wallpaper;
Christopher Tate36cb2a7a2016-06-09 13:44:48 -07001623 */
1624 return null;
Christopher Tate5d99d472016-05-06 17:59:27 -07001625 } else {
1626 whichProp = PROP_WALLPAPER;
1627 defaultResId = com.android.internal.R.drawable.default_wallpaper;
1628 }
1629 final String path = SystemProperties.get(whichProp);
Jeff Sharkey28f08772014-04-16 09:41:58 -07001630 if (!TextUtils.isEmpty(path)) {
1631 final File file = new File(path);
1632 if (file.exists()) {
1633 try {
1634 return new FileInputStream(file);
1635 } catch (IOException e) {
1636 // Ignored, fall back to platform default below
1637 }
1638 }
1639 }
Christopher Tate5d99d472016-05-06 17:59:27 -07001640 try {
1641 return context.getResources().openRawResource(defaultResId);
1642 } catch (NotFoundException e) {
1643 // no default defined for this device; this is not a failure
1644 }
1645 return null;
Jeff Sharkey28f08772014-04-16 09:41:58 -07001646 }
1647
1648 /**
1649 * Return {@link ComponentName} of the default live wallpaper, or
1650 * {@code null} if none is defined.
1651 *
1652 * @hide
1653 */
1654 public static ComponentName getDefaultWallpaperComponent(Context context) {
1655 String flat = SystemProperties.get(PROP_WALLPAPER_COMPONENT);
1656 if (!TextUtils.isEmpty(flat)) {
1657 final ComponentName cn = ComponentName.unflattenFromString(flat);
1658 if (cn != null) {
1659 return cn;
1660 }
1661 }
1662
1663 flat = context.getString(com.android.internal.R.string.default_wallpaper_component);
1664 if (!TextUtils.isEmpty(flat)) {
1665 final ComponentName cn = ComponentName.unflattenFromString(flat);
1666 if (cn != null) {
1667 return cn;
1668 }
1669 }
1670
1671 return null;
Dianne Hackborn8cc6a502009-08-05 21:29:42 -07001672 }
Christopher Tate1e1e2e02016-01-25 15:34:36 -08001673
Christopher Tatebe132e62016-02-10 12:59:49 -08001674 /**
1675 * Register a callback for lock wallpaper observation. Only the OS may use this.
1676 *
1677 * @return true on success; false on error.
1678 * @hide
1679 */
1680 public boolean setLockWallpaperCallback(IWallpaperManagerCallback callback) {
1681 if (sGlobals.mService == null) {
1682 Log.w(TAG, "WallpaperService not running");
Christopher Tate98d609c2016-05-18 17:31:58 -07001683 throw new RuntimeException(new DeadSystemException());
Christopher Tatebe132e62016-02-10 12:59:49 -08001684 }
1685
1686 try {
1687 return sGlobals.mService.setLockWallpaperCallback(callback);
1688 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07001689 throw e.rethrowFromSystemServer();
Christopher Tatebe132e62016-02-10 12:59:49 -08001690 }
Christopher Tatebe132e62016-02-10 12:59:49 -08001691 }
1692
Christopher Tated7faf532016-02-25 12:43:38 -08001693 /**
1694 * Is the current system wallpaper eligible for backup?
1695 *
1696 * Only the OS itself may use this method.
1697 * @hide
1698 */
Christopher Tate61722662016-08-10 16:13:14 -07001699 public boolean isWallpaperBackupEligible(int which) {
Christopher Tated7faf532016-02-25 12:43:38 -08001700 if (sGlobals.mService == null) {
1701 Log.w(TAG, "WallpaperService not running");
Christopher Tate98d609c2016-05-18 17:31:58 -07001702 throw new RuntimeException(new DeadSystemException());
Christopher Tated7faf532016-02-25 12:43:38 -08001703 }
1704 try {
Christopher Tate61722662016-08-10 16:13:14 -07001705 return sGlobals.mService.isWallpaperBackupEligible(which, mContext.getUserId());
Christopher Tated7faf532016-02-25 12:43:38 -08001706 } catch (RemoteException e) {
1707 Log.e(TAG, "Exception querying wallpaper backup eligibility: " + e.getMessage());
1708 }
1709 return false;
1710 }
1711
Christopher Tate1e1e2e02016-01-25 15:34:36 -08001712 // Private completion callback for setWallpaper() synchronization
1713 private class WallpaperSetCompletion extends IWallpaperManagerCallback.Stub {
1714 final CountDownLatch mLatch;
1715
1716 public WallpaperSetCompletion() {
1717 mLatch = new CountDownLatch(1);
1718 }
1719
1720 public void waitForCompletion() {
1721 try {
1722 mLatch.await(30, TimeUnit.SECONDS);
1723 } catch (InterruptedException e) {
1724 // This might be legit: the crop may take a very long time. Don't sweat
1725 // it in that case; we are okay with display lagging behind in order to
1726 // keep the caller from locking up indeterminately.
1727 }
1728 }
1729
1730 @Override
1731 public void onWallpaperChanged() throws RemoteException {
1732 mLatch.countDown();
1733 }
1734 }
Dianne Hackborn8cc6a502009-08-05 21:29:42 -07001735}