blob: dfcbfc4f8e0911367aad4acb4be5f59f6bc7fb33 [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;
Lucas Dupinc40608c2017-04-14 18:33:08 -070020import android.annotation.NonNull;
21import android.annotation.Nullable;
Tor Norbye7b9c9122013-05-30 16:48:33 -070022import android.annotation.RawRes;
Jeff Sharkeyd86b8fe2017-06-02 17:36:26 -060023import android.annotation.RequiresPermission;
Jeff Sharkey0f3f60b2017-04-24 18:06:20 -060024import android.annotation.SdkConstant;
Dianne Hackborn067e5f62014-09-07 23:14:30 -070025import android.annotation.SystemApi;
Jeff Sharkeyd86b8fe2017-06-02 17:36:26 -060026import android.annotation.SystemService;
Jeff Sharkey0f3f60b2017-04-24 18:06:20 -060027import android.annotation.SdkConstant.SdkConstantType;
Jeff Sharkey28f08772014-04-16 09:41:58 -070028import android.content.ComponentName;
Adam Lesinskibba72d12013-10-11 18:10:56 -070029import android.content.ContentResolver;
Dianne Hackborn8cc6a502009-08-05 21:29:42 -070030import android.content.Context;
31import android.content.Intent;
Michael Jurkae8d1bf72013-09-09 15:58:54 +020032import android.content.pm.PackageManager;
33import android.content.pm.ResolveInfo;
Dianne Hackborn8cc6a502009-08-05 21:29:42 -070034import android.content.res.Resources;
Christopher Tate5d99d472016-05-06 17:59:27 -070035import android.content.res.Resources.NotFoundException;
Dianne Hackborn8cc6a502009-08-05 21:29:42 -070036import android.graphics.Bitmap;
37import android.graphics.BitmapFactory;
Michael Jurkab668d0b2013-10-04 15:11:05 -070038import android.graphics.BitmapRegionDecoder;
Dianne Hackborn284ac932009-08-28 10:34:25 -070039import android.graphics.Canvas;
Dianne Hackborn19382ac2009-09-11 21:13:37 -070040import android.graphics.ColorFilter;
Michael Jurkab668d0b2013-10-04 15:11:05 -070041import android.graphics.Matrix;
Dianne Hackborn284ac932009-08-28 10:34:25 -070042import android.graphics.Paint;
Dianne Hackborn19382ac2009-09-11 21:13:37 -070043import android.graphics.PixelFormat;
Jeff Brown24572372011-06-09 19:05:15 -070044import android.graphics.PorterDuff;
45import android.graphics.PorterDuffXfermode;
Dianne Hackborn284ac932009-08-28 10:34:25 -070046import android.graphics.Rect;
Michael Jurkab668d0b2013-10-04 15:11:05 -070047import android.graphics.RectF;
Dianne Hackborn8cc6a502009-08-05 21:29:42 -070048import android.graphics.drawable.BitmapDrawable;
49import android.graphics.drawable.Drawable;
Michael Jurkae8d1bf72013-09-09 15:58:54 +020050import android.net.Uri;
Dianne Hackborn284ac932009-08-28 10:34:25 -070051import android.os.Bundle;
Christopher Tate98d609c2016-05-18 17:31:58 -070052import android.os.DeadSystemException;
Dianne Hackborn8cc6a502009-08-05 21:29:42 -070053import android.os.Handler;
54import android.os.IBinder;
Dianne Hackborn840c3a22009-09-02 17:35:17 -070055import android.os.Looper;
Dianne Hackborn8cc6a502009-08-05 21:29:42 -070056import android.os.ParcelFileDescriptor;
57import android.os.RemoteException;
58import android.os.ServiceManager;
Jeff Sharkey28f08772014-04-16 09:41:58 -070059import android.os.SystemProperties;
Jorim Jaggie31f6b82016-07-01 16:15:09 -070060import android.os.UserHandle;
Jeff Sharkey28f08772014-04-16 09:41:58 -070061import android.text.TextUtils;
Dianne Hackborn7341d7a2009-08-14 11:37:52 -070062import android.util.Log;
Lucas Dupinea1fb1e2017-04-05 17:39:44 -070063import android.util.Pair;
Jeff Brown98365d72012-08-19 20:30:52 -070064import android.view.WindowManagerGlobal;
Dianne Hackborn8cc6a502009-08-05 21:29:42 -070065
Christopher Tateffa6a882015-12-11 15:08:46 -080066import libcore.io.IoUtils;
67
Michael Jurkab668d0b2013-10-04 15:11:05 -070068import java.io.BufferedInputStream;
Jeff Sharkey28f08772014-04-16 09:41:58 -070069import java.io.File;
70import java.io.FileInputStream;
Dianne Hackborn8cc6a502009-08-05 21:29:42 -070071import java.io.FileOutputStream;
72import java.io.IOException;
73import java.io.InputStream;
Christopher Tatead3c2592016-01-20 18:13:17 -080074import java.lang.annotation.Retention;
75import java.lang.annotation.RetentionPolicy;
Lucas Dupinea1fb1e2017-04-05 17:39:44 -070076import java.util.ArrayList;
77import java.util.Iterator;
Michael Jurkae8d1bf72013-09-09 15:58:54 +020078import java.util.List;
Christopher Tate1e1e2e02016-01-25 15:34:36 -080079import java.util.concurrent.CountDownLatch;
80import java.util.concurrent.TimeUnit;
Dianne Hackborn8cc6a502009-08-05 21:29:42 -070081
Scott Main8b2e0002009-09-29 18:17:31 -070082/**
83 * Provides access to the system wallpaper. With WallpaperManager, you can
84 * get the current wallpaper, get the desired dimensions for the wallpaper, set
Jeff Sharkeyd86b8fe2017-06-02 17:36:26 -060085 * the wallpaper, and more.
Benjamin Franzf3ece362015-02-11 10:51:10 +000086 *
87 * <p> An app can check whether wallpapers are supported for the current user, by calling
Oleksandr Peletskyif2519812016-01-26 20:16:06 +010088 * {@link #isWallpaperSupported()}, and whether setting of wallpapers is allowed, by calling
Christopher Tate98d609c2016-05-18 17:31:58 -070089 * {@link #isSetWallpaperAllowed()}.
Scott Main8b2e0002009-09-29 18:17:31 -070090 */
Jeff Sharkeyd86b8fe2017-06-02 17:36:26 -060091@SystemService(Context.WALLPAPER_SERVICE)
Dianne Hackborn8cc6a502009-08-05 21:29:42 -070092public class WallpaperManager {
93 private static String TAG = "WallpaperManager";
94 private static boolean DEBUG = false;
Marco Nelissenbf6956b2009-11-09 15:21:13 -080095 private float mWallpaperXStep = -1;
96 private float mWallpaperYStep = -1;
Dianne Hackborn8cc6a502009-08-05 21:29:42 -070097
Jeff Sharkey28f08772014-04-16 09:41:58 -070098 /** {@hide} */
99 private static final String PROP_WALLPAPER = "ro.config.wallpaper";
100 /** {@hide} */
Christopher Tate5d99d472016-05-06 17:59:27 -0700101 private static final String PROP_LOCK_WALLPAPER = "ro.config.lock_wallpaper";
102 /** {@hide} */
Jeff Sharkey28f08772014-04-16 09:41:58 -0700103 private static final String PROP_WALLPAPER_COMPONENT = "ro.config.wallpaper_component";
104
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700105 /**
Michael Jurkae8d1bf72013-09-09 15:58:54 +0200106 * Activity Action: Show settings for choosing wallpaper. Do not use directly to construct
107 * an intent; instead, use {@link #getCropAndSetWallpaperIntent}.
108 * <p>Input: {@link Intent#getData} is the URI of the image to crop and set as wallpaper.
109 * <p>Output: RESULT_OK if user decided to crop/set the wallpaper, RESULT_CANCEL otherwise
Adam Lesinskibba72d12013-10-11 18:10:56 -0700110 * Activities that support this intent should specify a MIME filter of "image/*"
Michael Jurkae8d1bf72013-09-09 15:58:54 +0200111 */
Jeff Sharkey0f3f60b2017-04-24 18:06:20 -0600112 @SdkConstant(SdkConstantType.ACTIVITY_INTENT_ACTION)
Michael Jurkae8d1bf72013-09-09 15:58:54 +0200113 public static final String ACTION_CROP_AND_SET_WALLPAPER =
114 "android.service.wallpaper.CROP_AND_SET_WALLPAPER";
115
116 /**
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700117 * Launch an activity for the user to pick the current global live
118 * wallpaper.
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700119 */
120 public static final String ACTION_LIVE_WALLPAPER_CHOOSER
121 = "android.service.wallpaper.LIVE_WALLPAPER_CHOOSER";
Adam Cohen5a242ec2010-12-07 21:07:07 -0800122
123 /**
Dianne Hackborn7df7d202012-04-19 18:00:04 -0700124 * Directly launch live wallpaper preview, allowing the user to immediately
125 * confirm to switch to a specific live wallpaper. You must specify
126 * {@link #EXTRA_LIVE_WALLPAPER_COMPONENT} with the ComponentName of
127 * a live wallpaper component that is to be shown.
128 */
129 public static final String ACTION_CHANGE_LIVE_WALLPAPER
130 = "android.service.wallpaper.CHANGE_LIVE_WALLPAPER";
131
132 /**
133 * Extra in {@link #ACTION_CHANGE_LIVE_WALLPAPER} that specifies the
134 * ComponentName of a live wallpaper that should be shown as a preview,
135 * for the user to confirm.
136 */
137 public static final String EXTRA_LIVE_WALLPAPER_COMPONENT
138 = "android.service.wallpaper.extra.LIVE_WALLPAPER_COMPONENT";
139
140 /**
Adam Cohen5a242ec2010-12-07 21:07:07 -0800141 * Manifest entry for activities that respond to {@link Intent#ACTION_SET_WALLPAPER}
142 * which allows them to provide a custom large icon associated with this action.
143 */
144 public static final String WALLPAPER_PREVIEW_META_DATA = "android.wallpaper.preview";
145
Dianne Hackborn23ef7b42009-11-18 18:20:39 -0800146 /**
147 * Command for {@link #sendWallpaperCommand}: reported by the wallpaper
148 * host when the user taps on an empty area (not performing an action
149 * in the host). The x and y arguments are the location of the tap in
150 * screen coordinates.
151 */
152 public static final String COMMAND_TAP = "android.wallpaper.tap";
Oleksandr Peletskyif2519812016-01-26 20:16:06 +0100153
Dianne Hackborn23ef7b42009-11-18 18:20:39 -0800154 /**
155 * Command for {@link #sendWallpaperCommand}: reported by the wallpaper
Jeff Brown9f3bdfe2010-10-13 06:01:27 -0700156 * host when the user releases a secondary pointer on an empty area
157 * (not performing an action in the host). The x and y arguments are
158 * the location of the secondary tap in screen coordinates.
159 */
160 public static final String COMMAND_SECONDARY_TAP = "android.wallpaper.secondaryTap";
161
162 /**
163 * Command for {@link #sendWallpaperCommand}: reported by the wallpaper
Dianne Hackborn23ef7b42009-11-18 18:20:39 -0800164 * host when the user drops an object into an area of the host. The x
165 * and y arguments are the location of the drop.
166 */
167 public static final String COMMAND_DROP = "android.home.drop";
Christopher Tatead3c2592016-01-20 18:13:17 -0800168
169 /**
170 * Extra passed back from setWallpaper() giving the new wallpaper's assigned ID.
171 * @hide
172 */
173 public static final String EXTRA_NEW_WALLPAPER_ID = "android.service.wallpaper.extra.ID";
174
Christopher Tate5d99d472016-05-06 17:59:27 -0700175 // flags for which kind of wallpaper to act on
Christopher Tatead3c2592016-01-20 18:13:17 -0800176
177 /** @hide */
178 @IntDef(flag = true, value = {
Christopher Tateedf7d042016-03-29 18:24:25 -0700179 FLAG_SYSTEM,
180 FLAG_LOCK
Christopher Tatead3c2592016-01-20 18:13:17 -0800181 })
182 @Retention(RetentionPolicy.SOURCE)
183 public @interface SetWallpaperFlags {}
184
185 /**
Christopher Tate5d99d472016-05-06 17:59:27 -0700186 * Flag: set or retrieve the general system wallpaper.
Christopher Tatead3c2592016-01-20 18:13:17 -0800187 */
Christopher Tateedf7d042016-03-29 18:24:25 -0700188 public static final int FLAG_SYSTEM = 1 << 0;
Christopher Tatead3c2592016-01-20 18:13:17 -0800189
190 /**
Christopher Tate5d99d472016-05-06 17:59:27 -0700191 * Flag: set or retrieve the lock-screen-specific wallpaper.
Christopher Tatead3c2592016-01-20 18:13:17 -0800192 */
Christopher Tateedf7d042016-03-29 18:24:25 -0700193 public static final int FLAG_LOCK = 1 << 1;
Christopher Tatead3c2592016-01-20 18:13:17 -0800194
Dianne Hackborn8cc6a502009-08-05 21:29:42 -0700195 private final Context mContext;
Oleksandr Peletskyif2519812016-01-26 20:16:06 +0100196
Dianne Hackborn19382ac2009-09-11 21:13:37 -0700197 /**
198 * Special drawable that draws a wallpaper as fast as possible. Assumes
199 * no scaling or placement off (0,0) of the wallpaper (this should be done
200 * at the time the bitmap is loaded).
201 */
202 static class FastBitmapDrawable extends Drawable {
203 private final Bitmap mBitmap;
204 private final int mWidth;
205 private final int mHeight;
206 private int mDrawLeft;
207 private int mDrawTop;
Romain Guy407ec782011-08-24 17:06:58 -0700208 private final Paint mPaint;
Dianne Hackborn19382ac2009-09-11 21:13:37 -0700209
210 private FastBitmapDrawable(Bitmap bitmap) {
211 mBitmap = bitmap;
212 mWidth = bitmap.getWidth();
213 mHeight = bitmap.getHeight();
Romain Guy407ec782011-08-24 17:06:58 -0700214
Dianne Hackborn19382ac2009-09-11 21:13:37 -0700215 setBounds(0, 0, mWidth, mHeight);
Romain Guy407ec782011-08-24 17:06:58 -0700216
217 mPaint = new Paint();
218 mPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC));
Dianne Hackborn19382ac2009-09-11 21:13:37 -0700219 }
220
221 @Override
222 public void draw(Canvas canvas) {
Romain Guy407ec782011-08-24 17:06:58 -0700223 canvas.drawBitmap(mBitmap, mDrawLeft, mDrawTop, mPaint);
Dianne Hackborn19382ac2009-09-11 21:13:37 -0700224 }
225
226 @Override
227 public int getOpacity() {
228 return PixelFormat.OPAQUE;
229 }
230
231 @Override
232 public void setBounds(int left, int top, int right, int bottom) {
233 mDrawLeft = left + (right-left - mWidth) / 2;
234 mDrawTop = top + (bottom-top - mHeight) / 2;
235 }
236
237 @Override
Dianne Hackborn19382ac2009-09-11 21:13:37 -0700238 public void setAlpha(int alpha) {
Romain Guy407ec782011-08-24 17:06:58 -0700239 throw new UnsupportedOperationException("Not supported with this drawable");
Dianne Hackborn19382ac2009-09-11 21:13:37 -0700240 }
241
242 @Override
Chris Craikbd3bfc52015-03-02 10:43:29 -0800243 public void setColorFilter(ColorFilter colorFilter) {
Romain Guy407ec782011-08-24 17:06:58 -0700244 throw new UnsupportedOperationException("Not supported with this drawable");
Dianne Hackborn19382ac2009-09-11 21:13:37 -0700245 }
246
247 @Override
248 public void setDither(boolean dither) {
Romain Guy407ec782011-08-24 17:06:58 -0700249 throw new UnsupportedOperationException("Not supported with this drawable");
Dianne Hackborn19382ac2009-09-11 21:13:37 -0700250 }
251
252 @Override
253 public void setFilterBitmap(boolean filter) {
Romain Guy407ec782011-08-24 17:06:58 -0700254 throw new UnsupportedOperationException("Not supported with this drawable");
Dianne Hackborn19382ac2009-09-11 21:13:37 -0700255 }
256
257 @Override
258 public int getIntrinsicWidth() {
259 return mWidth;
260 }
261
262 @Override
263 public int getIntrinsicHeight() {
264 return mHeight;
265 }
266
267 @Override
268 public int getMinimumWidth() {
269 return mWidth;
270 }
271
272 @Override
273 public int getMinimumHeight() {
274 return mHeight;
275 }
276 }
Oleksandr Peletskyif2519812016-01-26 20:16:06 +0100277
Lucas Dupinea1fb1e2017-04-05 17:39:44 -0700278 private static class Globals extends IWallpaperManagerCallback.Stub {
Fyodor Kupolovb6378cf2016-07-12 10:32:08 -0700279 private final IWallpaperManager mService;
Lucas Dupinea1fb1e2017-04-05 17:39:44 -0700280 private boolean mColorCallbackRegistered;
281 private final ArrayList<Pair<OnColorsChangedListener, Handler>> mColorListeners =
282 new ArrayList<>();
Vadim Tryshev93a914a2016-03-07 12:48:51 -0800283 private Bitmap mCachedWallpaper;
284 private int mCachedWallpaperUserId;
Dianne Hackborn19382ac2009-09-11 21:13:37 -0700285 private Bitmap mDefaultWallpaper;
Lucas Dupinea1fb1e2017-04-05 17:39:44 -0700286 private Handler mMainLooperHandler;
Oleksandr Peletskyif2519812016-01-26 20:16:06 +0100287
Dianne Hackborn5e802fb2009-09-02 21:50:50 -0700288 Globals(Looper looper) {
Dianne Hackborn8cc6a502009-08-05 21:29:42 -0700289 IBinder b = ServiceManager.getService(Context.WALLPAPER_SERVICE);
290 mService = IWallpaperManager.Stub.asInterface(b);
Lucas Dupinea1fb1e2017-04-05 17:39:44 -0700291 mMainLooperHandler = new Handler(looper);
Christopher Tatebe132e62016-02-10 12:59:49 -0800292 forgetLoadedWallpaper();
Dianne Hackborn8cc6a502009-08-05 21:29:42 -0700293 }
Oleksandr Peletskyif2519812016-01-26 20:16:06 +0100294
Dianne Hackborn8cc6a502009-08-05 21:29:42 -0700295 public void onWallpaperChanged() {
296 /* The wallpaper has changed but we shouldn't eagerly load the
297 * wallpaper as that would be inefficient. Reset the cached wallpaper
298 * to null so if the user requests the wallpaper again then we'll
299 * fetch it.
300 */
Christopher Tatebe132e62016-02-10 12:59:49 -0800301 forgetLoadedWallpaper();
Dianne Hackborn8cc6a502009-08-05 21:29:42 -0700302 }
Adam Cohen041a0ba2011-11-09 20:10:27 -0800303
Lucas Dupinea1fb1e2017-04-05 17:39:44 -0700304 /**
305 * Start listening to wallpaper color events.
306 * Will be called whenever someone changes their wallpaper or if a live wallpaper
307 * changes its colors.
308 * @param callback Listener
309 * @param handler Thread to call it from. Main thread if null.
310 */
311 public void addOnColorsChangedListener(@NonNull OnColorsChangedListener callback,
312 @Nullable Handler handler) {
313 synchronized (this) {
314 if (!mColorCallbackRegistered) {
315 try {
316 mService.registerWallpaperColorsCallback(this);
317 mColorCallbackRegistered = true;
318 } catch (RemoteException e) {
319 // Failed, service is gone
320 Log.w(TAG, "Can't register for color updates", e);
321 }
322 }
323 mColorListeners.add(new Pair<>(callback, handler));
324 }
325 }
326
327 /**
328 * Stop listening to wallpaper color events.
329 *
330 * @param callback listener
331 */
332 public void removeOnColorsChangedListener(@NonNull OnColorsChangedListener callback) {
333 synchronized (this) {
334 mColorListeners.removeIf(pair -> pair.first == callback);
335
336 if (mColorListeners.size() == 0 && mColorCallbackRegistered) {
337 mColorCallbackRegistered = false;
338 try {
339 mService.unregisterWallpaperColorsCallback(this);
340 } catch (RemoteException e) {
341 // Failed, service is gone
342 Log.w(TAG, "Can't unregister color updates", e);
343 }
344 }
345 }
346 }
347
348 @Override
349 public void onWallpaperColorsChanged(WallpaperColors colors, int which) {
350 synchronized (this) {
351 for (Pair<OnColorsChangedListener, Handler> listener : mColorListeners) {
352 Handler handler = listener.second;
353 if (listener.second == null) {
354 handler = mMainLooperHandler;
355 }
356 handler.post(() -> {
357 // Dealing with race conditions between posting a callback and
358 // removeOnColorsChangedListener being called.
359 boolean stillExists;
360 synchronized (sGlobals) {
361 stillExists = mColorListeners.contains(listener);
362 }
363 if (stillExists) {
364 listener.first.onColorsChanged(colors, which);
365 }
366 });
367 }
368 }
369 }
370
371 WallpaperColors getWallpaperColors(int which) {
372 synchronized (this) {
373 if (which != FLAG_LOCK && which != FLAG_SYSTEM)
374 throw new IllegalArgumentException(
375 "which should be either FLAG_LOCK or FLAG_SYSTEM");
376
377 try {
378 return mService.getWallpaperColors(which);
379 } catch (RemoteException e) {
380 // Can't get colors, connection lost.
381 }
382 return null;
383 }
384 }
385
Christopher Tate5d99d472016-05-06 17:59:27 -0700386 public Bitmap peekWallpaperBitmap(Context context, boolean returnDefault,
387 @SetWallpaperFlags int which) {
388 return peekWallpaperBitmap(context, returnDefault, which, context.getUserId());
Yorke Leedcd93cc2016-01-08 14:12:55 -0800389 }
390
Christopher Tate5d99d472016-05-06 17:59:27 -0700391 public Bitmap peekWallpaperBitmap(Context context, boolean returnDefault,
392 @SetWallpaperFlags int which, int userId) {
Fyodor Kupolovb6378cf2016-07-12 10:32:08 -0700393 if (mService != null) {
394 try {
395 if (!mService.isWallpaperSupported(context.getOpPackageName())) {
396 return null;
Benjamin Franzf3ece362015-02-11 10:51:10 +0000397 }
Fyodor Kupolovb6378cf2016-07-12 10:32:08 -0700398 } catch (RemoteException e) {
399 throw e.rethrowFromSystemServer();
Benjamin Franzf3ece362015-02-11 10:51:10 +0000400 }
Fyodor Kupolovb6378cf2016-07-12 10:32:08 -0700401 }
402 synchronized (this) {
Vadim Tryshev93a914a2016-03-07 12:48:51 -0800403 if (mCachedWallpaper != null && mCachedWallpaperUserId == userId) {
404 return mCachedWallpaper;
Dianne Hackborn8cc6a502009-08-05 21:29:42 -0700405 }
Vadim Tryshev93a914a2016-03-07 12:48:51 -0800406 mCachedWallpaper = null;
407 mCachedWallpaperUserId = 0;
Dianne Hackbornc9dbbe22009-11-11 22:50:37 -0800408 try {
Vadim Tryshev93a914a2016-03-07 12:48:51 -0800409 mCachedWallpaper = getCurrentWallpaperLocked(userId);
410 mCachedWallpaperUserId = userId;
Dianne Hackbornc9dbbe22009-11-11 22:50:37 -0800411 } catch (OutOfMemoryError e) {
412 Log.w(TAG, "No memory load current wallpaper", e);
413 }
Vadim Tryshev93a914a2016-03-07 12:48:51 -0800414 if (mCachedWallpaper != null) {
415 return mCachedWallpaper;
Dianne Hackborn19382ac2009-09-11 21:13:37 -0700416 }
Dianne Hackborn8cc6a502009-08-05 21:29:42 -0700417 }
Fyodor Kupolovb6378cf2016-07-12 10:32:08 -0700418 if (returnDefault) {
419 Bitmap defaultWallpaper = mDefaultWallpaper;
420 if (defaultWallpaper == null) {
421 defaultWallpaper = getDefaultWallpaper(context, which);
422 synchronized (this) {
423 mDefaultWallpaper = defaultWallpaper;
424 }
425 }
426 return defaultWallpaper;
427 }
428 return null;
Dianne Hackborn8cc6a502009-08-05 21:29:42 -0700429 }
Dianne Hackbornba398392011-08-01 16:11:57 -0700430
Fyodor Kupolovb6378cf2016-07-12 10:32:08 -0700431 void forgetLoadedWallpaper() {
Dianne Hackbornba398392011-08-01 16:11:57 -0700432 synchronized (this) {
Vadim Tryshev93a914a2016-03-07 12:48:51 -0800433 mCachedWallpaper = null;
434 mCachedWallpaperUserId = 0;
Dianne Hackbornba398392011-08-01 16:11:57 -0700435 mDefaultWallpaper = null;
436 }
437 }
438
Yorke Leedcd93cc2016-01-08 14:12:55 -0800439 private Bitmap getCurrentWallpaperLocked(int userId) {
Adam Lesinski2c8d67c2014-04-23 13:46:21 -0700440 if (mService == null) {
441 Log.w(TAG, "WallpaperService not running");
442 return null;
443 }
444
Dianne Hackborn8cc6a502009-08-05 21:29:42 -0700445 try {
Dianne Hackborn284ac932009-08-28 10:34:25 -0700446 Bundle params = new Bundle();
Christopher Tateedf7d042016-03-29 18:24:25 -0700447 ParcelFileDescriptor fd = mService.getWallpaper(this, FLAG_SYSTEM,
Yorke Leedcd93cc2016-01-08 14:12:55 -0800448 params, userId);
Dianne Hackborn8cc6a502009-08-05 21:29:42 -0700449 if (fd != null) {
Jeff Brown24572372011-06-09 19:05:15 -0700450 try {
451 BitmapFactory.Options options = new BitmapFactory.Options();
Michael Jurka824a4b52013-12-18 17:10:16 +0100452 return BitmapFactory.decodeFileDescriptor(
Jeff Brown24572372011-06-09 19:05:15 -0700453 fd.getFileDescriptor(), null, options);
Jeff Brown24572372011-06-09 19:05:15 -0700454 } catch (OutOfMemoryError e) {
455 Log.w(TAG, "Can't decode file", e);
456 } finally {
Christopher Tateffa6a882015-12-11 15:08:46 -0800457 IoUtils.closeQuietly(fd);
Dianne Hackborn8cc6a502009-08-05 21:29:42 -0700458 }
Dianne Hackborn19382ac2009-09-11 21:13:37 -0700459 }
460 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -0700461 throw e.rethrowFromSystemServer();
Dianne Hackborn19382ac2009-09-11 21:13:37 -0700462 }
463 return null;
464 }
Christopher Tateffa6a882015-12-11 15:08:46 -0800465
Fyodor Kupolovb6378cf2016-07-12 10:32:08 -0700466 private Bitmap getDefaultWallpaper(Context context, @SetWallpaperFlags int which) {
Christopher Tate5d99d472016-05-06 17:59:27 -0700467 InputStream is = openDefaultWallpaper(context, which);
Adam Lesinski2c8d67c2014-04-23 13:46:21 -0700468 if (is != null) {
469 try {
470 BitmapFactory.Options options = new BitmapFactory.Options();
471 return BitmapFactory.decodeStream(is, null, options);
472 } catch (OutOfMemoryError e) {
473 Log.w(TAG, "Can't decode stream", e);
474 } finally {
Christopher Tateffa6a882015-12-11 15:08:46 -0800475 IoUtils.closeQuietly(is);
Dianne Hackborn8cc6a502009-08-05 21:29:42 -0700476 }
Dianne Hackborn8cc6a502009-08-05 21:29:42 -0700477 }
478 return null;
479 }
480 }
Oleksandr Peletskyif2519812016-01-26 20:16:06 +0100481
Romain Guy407ec782011-08-24 17:06:58 -0700482 private static final Object sSync = new Object[0];
Dianne Hackborn8cc6a502009-08-05 21:29:42 -0700483 private static Globals sGlobals;
484
Dianne Hackborn840c3a22009-09-02 17:35:17 -0700485 static void initGlobals(Looper looper) {
Romain Guy407ec782011-08-24 17:06:58 -0700486 synchronized (sSync) {
Dianne Hackborn8cc6a502009-08-05 21:29:42 -0700487 if (sGlobals == null) {
Dianne Hackborn5e802fb2009-09-02 21:50:50 -0700488 sGlobals = new Globals(looper);
Dianne Hackborn8cc6a502009-08-05 21:29:42 -0700489 }
Dianne Hackborn8cc6a502009-08-05 21:29:42 -0700490 }
491 }
Oleksandr Peletskyif2519812016-01-26 20:16:06 +0100492
Dianne Hackborn8cc6a502009-08-05 21:29:42 -0700493 /*package*/ WallpaperManager(Context context, Handler handler) {
494 mContext = context;
Dianne Hackborn840c3a22009-09-02 17:35:17 -0700495 initGlobals(context.getMainLooper());
Dianne Hackborn8cc6a502009-08-05 21:29:42 -0700496 }
497
498 /**
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700499 * Retrieve a WallpaperManager associated with the given Context.
500 */
501 public static WallpaperManager getInstance(Context context) {
502 return (WallpaperManager)context.getSystemService(
503 Context.WALLPAPER_SERVICE);
504 }
Oleksandr Peletskyif2519812016-01-26 20:16:06 +0100505
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700506 /** @hide */
507 public IWallpaperManager getIWallpaperManager() {
Dianne Hackborn840c3a22009-09-02 17:35:17 -0700508 return sGlobals.mService;
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700509 }
Oleksandr Peletskyif2519812016-01-26 20:16:06 +0100510
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700511 /**
Scott Main8b2e0002009-09-29 18:17:31 -0700512 * Retrieve the current system wallpaper; if
Michael Jurkab668d0b2013-10-04 15:11:05 -0700513 * no wallpaper is set, the system built-in static wallpaper is returned.
Scott Main8b2e0002009-09-29 18:17:31 -0700514 * This is returned as an
515 * abstract Drawable that you can install in a View to display whatever
Oleksandr Peletskyif2519812016-01-26 20:16:06 +0100516 * wallpaper the user has currently set.
Christopher Tate41dc83bd2016-07-15 15:59:23 -0700517 * <p>
518 * This method can return null if there is no system wallpaper available, if
519 * wallpapers are not supported in the current user, or if the calling app is not
520 * permitted to access the system wallpaper.
Dianne Hackborn8cc6a502009-08-05 21:29:42 -0700521 *
Christopher Tate41dc83bd2016-07-15 15:59:23 -0700522 * @return Returns a Drawable object that will draw the system wallpaper,
523 * or {@code null} if no system wallpaper exists or if the calling application
524 * is not able to access the wallpaper.
Dianne Hackborn8cc6a502009-08-05 21:29:42 -0700525 */
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700526 public Drawable getDrawable() {
Christopher Tate5d99d472016-05-06 17:59:27 -0700527 Bitmap bm = sGlobals.peekWallpaperBitmap(mContext, true, FLAG_SYSTEM);
Dianne Hackborn19382ac2009-09-11 21:13:37 -0700528 if (bm != null) {
529 Drawable dr = new BitmapDrawable(mContext.getResources(), bm);
530 dr.setDither(false);
531 return dr;
532 }
533 return null;
Dianne Hackborn8cc6a502009-08-05 21:29:42 -0700534 }
535
536 /**
Christopher Tate5d99d472016-05-06 17:59:27 -0700537 * Obtain a drawable for the built-in static system wallpaper.
Michael Jurkab668d0b2013-10-04 15:11:05 -0700538 */
539 public Drawable getBuiltInDrawable() {
Christopher Tate5d99d472016-05-06 17:59:27 -0700540 return getBuiltInDrawable(0, 0, false, 0, 0, FLAG_SYSTEM);
541 }
542
543 /**
544 * Obtain a drawable for the specified built-in static system wallpaper.
545 *
546 * @param which The {@code FLAG_*} identifier of a valid wallpaper type. Throws
547 * IllegalArgumentException if an invalid wallpaper is requested.
548 * @return A Drawable presenting the specified wallpaper image, or {@code null}
549 * if no built-in default image for that wallpaper type exists.
550 */
551 public Drawable getBuiltInDrawable(@SetWallpaperFlags int which) {
552 return getBuiltInDrawable(0, 0, false, 0, 0, which);
Michael Jurkab668d0b2013-10-04 15:11:05 -0700553 }
554
555 /**
556 * Returns a drawable for the system built-in static wallpaper. Based on the parameters, the
557 * drawable can be cropped and scaled
558 *
559 * @param outWidth The width of the returned drawable
560 * @param outWidth The height of the returned drawable
561 * @param scaleToFit If true, scale the wallpaper down rather than just cropping it
562 * @param horizontalAlignment A float value between 0 and 1 specifying where to crop the image;
563 * 0 for left-aligned, 0.5 for horizontal center-aligned, and 1 for right-aligned
564 * @param verticalAlignment A float value between 0 and 1 specifying where to crop the image;
565 * 0 for top-aligned, 0.5 for vertical center-aligned, and 1 for bottom-aligned
Christopher Tate5d99d472016-05-06 17:59:27 -0700566 * @return A Drawable presenting the built-in default system wallpaper image,
567 * or {@code null} if no such default image is defined on this device.
Michael Jurkab668d0b2013-10-04 15:11:05 -0700568 */
569 public Drawable getBuiltInDrawable(int outWidth, int outHeight,
570 boolean scaleToFit, float horizontalAlignment, float verticalAlignment) {
Christopher Tate5d99d472016-05-06 17:59:27 -0700571 return getBuiltInDrawable(outWidth, outHeight, scaleToFit,
572 horizontalAlignment, verticalAlignment, FLAG_SYSTEM);
573 }
574
575 /**
576 * Returns a drawable for the built-in static wallpaper of the specified type. Based on the
577 * parameters, the drawable can be cropped and scaled.
578 *
579 * @param outWidth The width of the returned drawable
580 * @param outWidth The height of the returned drawable
581 * @param scaleToFit If true, scale the wallpaper down rather than just cropping it
582 * @param horizontalAlignment A float value between 0 and 1 specifying where to crop the image;
583 * 0 for left-aligned, 0.5 for horizontal center-aligned, and 1 for right-aligned
584 * @param verticalAlignment A float value between 0 and 1 specifying where to crop the image;
585 * 0 for top-aligned, 0.5 for vertical center-aligned, and 1 for bottom-aligned
586 * @param which The {@code FLAG_*} identifier of a valid wallpaper type. Throws
587 * IllegalArgumentException if an invalid wallpaper is requested.
588 * @return A Drawable presenting the built-in default wallpaper image of the given type,
589 * or {@code null} if no default image of that type is defined on this device.
590 */
591 public Drawable getBuiltInDrawable(int outWidth, int outHeight, boolean scaleToFit,
592 float horizontalAlignment, float verticalAlignment, @SetWallpaperFlags int which) {
Michael Jurkab668d0b2013-10-04 15:11:05 -0700593 if (sGlobals.mService == null) {
594 Log.w(TAG, "WallpaperService not running");
Christopher Tate98d609c2016-05-18 17:31:58 -0700595 throw new RuntimeException(new DeadSystemException());
Michael Jurkab668d0b2013-10-04 15:11:05 -0700596 }
Christopher Tate5d99d472016-05-06 17:59:27 -0700597
598 if (which != FLAG_SYSTEM && which != FLAG_LOCK) {
599 throw new IllegalArgumentException("Must request exactly one kind of wallpaper");
600 }
601
Michael Jurkab668d0b2013-10-04 15:11:05 -0700602 Resources resources = mContext.getResources();
603 horizontalAlignment = Math.max(0, Math.min(1, horizontalAlignment));
604 verticalAlignment = Math.max(0, Math.min(1, verticalAlignment));
605
Christopher Tate5d99d472016-05-06 17:59:27 -0700606 InputStream wpStream = openDefaultWallpaper(mContext, which);
607 if (wpStream == null) {
608 if (DEBUG) {
609 Log.w(TAG, "default wallpaper stream " + which + " is null");
610 }
Michael Jurkab668d0b2013-10-04 15:11:05 -0700611 return null;
612 } else {
Christopher Tate5d99d472016-05-06 17:59:27 -0700613 InputStream is = new BufferedInputStream(wpStream);
Michael Jurkab668d0b2013-10-04 15:11:05 -0700614 if (outWidth <= 0 || outHeight <= 0) {
615 Bitmap fullSize = BitmapFactory.decodeStream(is, null, null);
616 return new BitmapDrawable(resources, fullSize);
617 } else {
618 int inWidth;
619 int inHeight;
Christopher Tate5d99d472016-05-06 17:59:27 -0700620 // Just measure this time through...
Michael Jurkab668d0b2013-10-04 15:11:05 -0700621 {
622 BitmapFactory.Options options = new BitmapFactory.Options();
623 options.inJustDecodeBounds = true;
624 BitmapFactory.decodeStream(is, null, options);
625 if (options.outWidth != 0 && options.outHeight != 0) {
626 inWidth = options.outWidth;
627 inHeight = options.outHeight;
628 } else {
629 Log.e(TAG, "default wallpaper dimensions are 0");
630 return null;
631 }
632 }
633
Christopher Tate5d99d472016-05-06 17:59:27 -0700634 // Reopen the stream to do the full decode. We know at this point
635 // that openDefaultWallpaper() will return non-null.
636 is = new BufferedInputStream(openDefaultWallpaper(mContext, which));
Michael Jurkab668d0b2013-10-04 15:11:05 -0700637
638 RectF cropRectF;
639
640 outWidth = Math.min(inWidth, outWidth);
641 outHeight = Math.min(inHeight, outHeight);
642 if (scaleToFit) {
643 cropRectF = getMaxCropRect(inWidth, inHeight, outWidth, outHeight,
644 horizontalAlignment, verticalAlignment);
645 } else {
646 float left = (inWidth - outWidth) * horizontalAlignment;
647 float right = left + outWidth;
648 float top = (inHeight - outHeight) * verticalAlignment;
649 float bottom = top + outHeight;
Michael Jurka05391532013-10-14 20:44:42 -0700650 cropRectF = new RectF(left, top, right, bottom);
Michael Jurkab668d0b2013-10-04 15:11:05 -0700651 }
652 Rect roundedTrueCrop = new Rect();
653 cropRectF.roundOut(roundedTrueCrop);
654
655 if (roundedTrueCrop.width() <= 0 || roundedTrueCrop.height() <= 0) {
656 Log.w(TAG, "crop has bad values for full size image");
657 return null;
658 }
659
660 // See how much we're reducing the size of the image
661 int scaleDownSampleSize = Math.min(roundedTrueCrop.width() / outWidth,
662 roundedTrueCrop.height() / outHeight);
663
664 // Attempt to open a region decoder
665 BitmapRegionDecoder decoder = null;
666 try {
667 decoder = BitmapRegionDecoder.newInstance(is, true);
668 } catch (IOException e) {
669 Log.w(TAG, "cannot open region decoder for default wallpaper");
670 }
671
672 Bitmap crop = null;
673 if (decoder != null) {
674 // Do region decoding to get crop bitmap
675 BitmapFactory.Options options = new BitmapFactory.Options();
676 if (scaleDownSampleSize > 1) {
677 options.inSampleSize = scaleDownSampleSize;
678 }
679 crop = decoder.decodeRegion(roundedTrueCrop, options);
680 decoder.recycle();
681 }
682
683 if (crop == null) {
Christopher Tate5d99d472016-05-06 17:59:27 -0700684 // BitmapRegionDecoder has failed, try to crop in-memory. We know at
685 // this point that openDefaultWallpaper() will return non-null.
686 is = new BufferedInputStream(openDefaultWallpaper(mContext, which));
Michael Jurkab668d0b2013-10-04 15:11:05 -0700687 Bitmap fullSize = null;
Christopher Tate5d99d472016-05-06 17:59:27 -0700688 BitmapFactory.Options options = new BitmapFactory.Options();
689 if (scaleDownSampleSize > 1) {
690 options.inSampleSize = scaleDownSampleSize;
Michael Jurkab668d0b2013-10-04 15:11:05 -0700691 }
Christopher Tate5d99d472016-05-06 17:59:27 -0700692 fullSize = BitmapFactory.decodeStream(is, null, options);
Michael Jurkab668d0b2013-10-04 15:11:05 -0700693 if (fullSize != null) {
694 crop = Bitmap.createBitmap(fullSize, roundedTrueCrop.left,
695 roundedTrueCrop.top, roundedTrueCrop.width(),
696 roundedTrueCrop.height());
697 }
698 }
699
700 if (crop == null) {
701 Log.w(TAG, "cannot decode default wallpaper");
702 return null;
703 }
704
705 // Scale down if necessary
706 if (outWidth > 0 && outHeight > 0 &&
707 (crop.getWidth() != outWidth || crop.getHeight() != outHeight)) {
708 Matrix m = new Matrix();
709 RectF cropRect = new RectF(0, 0, crop.getWidth(), crop.getHeight());
710 RectF returnRect = new RectF(0, 0, outWidth, outHeight);
711 m.setRectToRect(cropRect, returnRect, Matrix.ScaleToFit.FILL);
712 Bitmap tmp = Bitmap.createBitmap((int) returnRect.width(),
713 (int) returnRect.height(), Bitmap.Config.ARGB_8888);
714 if (tmp != null) {
715 Canvas c = new Canvas(tmp);
716 Paint p = new Paint();
717 p.setFilterBitmap(true);
718 c.drawBitmap(crop, m, p);
719 crop = tmp;
720 }
721 }
722
723 return new BitmapDrawable(resources, crop);
724 }
725 }
726 }
727
728 private static RectF getMaxCropRect(int inWidth, int inHeight, int outWidth, int outHeight,
729 float horizontalAlignment, float verticalAlignment) {
730 RectF cropRect = new RectF();
731 // Get a crop rect that will fit this
732 if (inWidth / (float) inHeight > outWidth / (float) outHeight) {
733 cropRect.top = 0;
734 cropRect.bottom = inHeight;
735 float cropWidth = outWidth * (inHeight / (float) outHeight);
736 cropRect.left = (inWidth - cropWidth) * horizontalAlignment;
737 cropRect.right = cropRect.left + cropWidth;
738 } else {
739 cropRect.left = 0;
740 cropRect.right = inWidth;
741 float cropHeight = outHeight * (inWidth / (float) outWidth);
742 cropRect.top = (inHeight - cropHeight) * verticalAlignment;
743 cropRect.bottom = cropRect.top + cropHeight;
744 }
745 return cropRect;
746 }
747
748 /**
Scott Main8b2e0002009-09-29 18:17:31 -0700749 * Retrieve the current system wallpaper; if there is no wallpaper set,
750 * a null pointer is returned. This is returned as an
Dianne Hackborn8cc6a502009-08-05 21:29:42 -0700751 * abstract Drawable that you can install in a View to display whatever
Oleksandr Peletskyif2519812016-01-26 20:16:06 +0100752 * wallpaper the user has currently set.
Dianne Hackborn8cc6a502009-08-05 21:29:42 -0700753 *
754 * @return Returns a Drawable object that will draw the wallpaper or a
755 * null pointer if these is none.
756 */
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700757 public Drawable peekDrawable() {
Christopher Tate5d99d472016-05-06 17:59:27 -0700758 Bitmap bm = sGlobals.peekWallpaperBitmap(mContext, false, FLAG_SYSTEM);
Dianne Hackborn19382ac2009-09-11 21:13:37 -0700759 if (bm != null) {
760 Drawable dr = new BitmapDrawable(mContext.getResources(), bm);
761 dr.setDither(false);
762 return dr;
763 }
764 return null;
765 }
766
767 /**
Scott Main8b2e0002009-09-29 18:17:31 -0700768 * Like {@link #getDrawable()}, but the returned Drawable has a number
769 * of limitations to reduce its overhead as much as possible. It will
770 * never scale the wallpaper (only centering it if the requested bounds
771 * do match the bitmap bounds, which should not be typical), doesn't
772 * allow setting an alpha, color filter, or other attributes, etc. The
773 * bounds of the returned drawable will be initialized to the same bounds
774 * as the wallpaper, so normally you will not need to touch it. The
775 * drawable also assumes that it will be used in a context running in
776 * the same density as the screen (not in density compatibility mode).
Dianne Hackborn19382ac2009-09-11 21:13:37 -0700777 *
778 * @return Returns a Drawable object that will draw the wallpaper.
779 */
780 public Drawable getFastDrawable() {
Christopher Tate5d99d472016-05-06 17:59:27 -0700781 Bitmap bm = sGlobals.peekWallpaperBitmap(mContext, true, FLAG_SYSTEM);
Dianne Hackborn19382ac2009-09-11 21:13:37 -0700782 if (bm != null) {
Romain Guy407ec782011-08-24 17:06:58 -0700783 return new FastBitmapDrawable(bm);
Dianne Hackborn19382ac2009-09-11 21:13:37 -0700784 }
785 return null;
786 }
787
788 /**
Scott Main8b2e0002009-09-29 18:17:31 -0700789 * Like {@link #getFastDrawable()}, but if there is no wallpaper set,
790 * a null pointer is returned.
Dianne Hackborn19382ac2009-09-11 21:13:37 -0700791 *
792 * @return Returns an optimized Drawable object that will draw the
793 * wallpaper or a null pointer if these is none.
794 */
795 public Drawable peekFastDrawable() {
Christopher Tate5d99d472016-05-06 17:59:27 -0700796 Bitmap bm = sGlobals.peekWallpaperBitmap(mContext, false, FLAG_SYSTEM);
Dianne Hackborn19382ac2009-09-11 21:13:37 -0700797 if (bm != null) {
Romain Guy407ec782011-08-24 17:06:58 -0700798 return new FastBitmapDrawable(bm);
Dianne Hackborn19382ac2009-09-11 21:13:37 -0700799 }
800 return null;
Dianne Hackborn8cc6a502009-08-05 21:29:42 -0700801 }
802
803 /**
Romain Guy407ec782011-08-24 17:06:58 -0700804 * Like {@link #getDrawable()} but returns a Bitmap.
Oleksandr Peletskyif2519812016-01-26 20:16:06 +0100805 *
Romain Guy407ec782011-08-24 17:06:58 -0700806 * @hide
807 */
808 public Bitmap getBitmap() {
Yorke Leedcd93cc2016-01-08 14:12:55 -0800809 return getBitmapAsUser(mContext.getUserId());
810 }
811
812 /**
813 * Like {@link #getDrawable()} but returns a Bitmap for the provided user.
814 *
815 * @hide
816 */
817 public Bitmap getBitmapAsUser(int userId) {
Christopher Tate5d99d472016-05-06 17:59:27 -0700818 return sGlobals.peekWallpaperBitmap(mContext, true, FLAG_SYSTEM, userId);
Romain Guy407ec782011-08-24 17:06:58 -0700819 }
820
821 /**
Christopher Tatebe132e62016-02-10 12:59:49 -0800822 * Get an open, readable file descriptor to the given wallpaper image file.
Christopher Tateedf7d042016-03-29 18:24:25 -0700823 * The caller is responsible for closing the file descriptor when done ingesting the file.
Christopher Tatebe132e62016-02-10 12:59:49 -0800824 *
825 * <p>If no lock-specific wallpaper has been configured for the given user, then
Christopher Tateedf7d042016-03-29 18:24:25 -0700826 * this method will return {@code null} when requesting {@link #FLAG_LOCK} rather than
Christopher Tatebe132e62016-02-10 12:59:49 -0800827 * returning the system wallpaper's image file.
Christopher Tateedf7d042016-03-29 18:24:25 -0700828 *
829 * @param which The wallpaper whose image file is to be retrieved. Must be a single
830 * defined kind of wallpaper, either {@link #FLAG_SYSTEM} or
831 * {@link #FLAG_LOCK}.
832 *
833 * @see #FLAG_LOCK
834 * @see #FLAG_SYSTEM
Christopher Tatebe132e62016-02-10 12:59:49 -0800835 */
Christopher Tate5d99d472016-05-06 17:59:27 -0700836 public ParcelFileDescriptor getWallpaperFile(@SetWallpaperFlags int which) {
Christopher Tatebe132e62016-02-10 12:59:49 -0800837 return getWallpaperFile(which, mContext.getUserId());
838 }
839
Lucas Dupinc40608c2017-04-14 18:33:08 -0700840 /**
841 * Registers a listener to get notified when the wallpaper colors change.
842 * Callback might be called from an arbitrary background thread.
843 *
844 * @param listener A listener to register
845 */
846 public void addOnColorsChangedListener(@NonNull OnColorsChangedListener listener) {
Lucas Dupinea1fb1e2017-04-05 17:39:44 -0700847 sGlobals.addOnColorsChangedListener(listener, null);
Lucas Dupinc40608c2017-04-14 18:33:08 -0700848 }
849
850 /**
851 * Registers a listener to get notified when the wallpaper colors change
852 * @param listener A listener to register
Lucas Dupinea1fb1e2017-04-05 17:39:44 -0700853 * @param handler Where to call it from. Will be called from the main thread
Lucas Dupinc40608c2017-04-14 18:33:08 -0700854 * if null.
855 */
856 public void addOnColorsChangedListener(@NonNull OnColorsChangedListener listener,
Lucas Dupinea1fb1e2017-04-05 17:39:44 -0700857 @NonNull Handler handler) {
858 sGlobals.addOnColorsChangedListener(listener, handler);
Lucas Dupinc40608c2017-04-14 18:33:08 -0700859 }
860
861 /**
862 * Stop listening to color updates.
863 * @param callback A callback to unsubscribe
864 */
865 public void removeOnColorsChangedListener(@NonNull OnColorsChangedListener callback) {
Lucas Dupinea1fb1e2017-04-05 17:39:44 -0700866 sGlobals.removeOnColorsChangedListener(callback);
Lucas Dupinc40608c2017-04-14 18:33:08 -0700867 }
868
869 /**
870 * Get the primary colors of a wallpaper
871 * @param which wallpaper type. Must be either {@link #FLAG_SYSTEM} or
872 * {@link #FLAG_LOCK}
873 * @return a list of colors ordered by priority
874 */
875 public @Nullable WallpaperColors getWallpaperColors(int which) {
Lucas Dupinea1fb1e2017-04-05 17:39:44 -0700876 return sGlobals.getWallpaperColors(which);
Lucas Dupinc40608c2017-04-14 18:33:08 -0700877 }
878
Christopher Tatebe132e62016-02-10 12:59:49 -0800879 /**
880 * Version of {@link #getWallpaperFile(int)} that can access the wallpaper data
881 * for a given user. The caller must hold the INTERACT_ACROSS_USERS_FULL
882 * permission to access another user's wallpaper data.
Christopher Tateedf7d042016-03-29 18:24:25 -0700883 *
884 * @param which The wallpaper whose image file is to be retrieved. Must be a single
885 * defined kind of wallpaper, either {@link #FLAG_SYSTEM} or
886 * {@link #FLAG_LOCK}.
887 * @param userId The user or profile whose imagery is to be retrieved
888 *
889 * @see #FLAG_LOCK
890 * @see #FLAG_SYSTEM
891 *
Christopher Tatebe132e62016-02-10 12:59:49 -0800892 * @hide
893 */
Christopher Tate5d99d472016-05-06 17:59:27 -0700894 public ParcelFileDescriptor getWallpaperFile(@SetWallpaperFlags int which, int userId) {
Christopher Tateedf7d042016-03-29 18:24:25 -0700895 if (which != FLAG_SYSTEM && which != FLAG_LOCK) {
Christopher Tatebe132e62016-02-10 12:59:49 -0800896 throw new IllegalArgumentException("Must request exactly one kind of wallpaper");
897 }
898
899 if (sGlobals.mService == null) {
900 Log.w(TAG, "WallpaperService not running");
Christopher Tate98d609c2016-05-18 17:31:58 -0700901 throw new RuntimeException(new DeadSystemException());
Christopher Tatebe132e62016-02-10 12:59:49 -0800902 } else {
903 try {
904 Bundle outParams = new Bundle();
905 return sGlobals.mService.getWallpaper(null, which, outParams, userId);
906 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -0700907 throw e.rethrowFromSystemServer();
Christopher Tatebe132e62016-02-10 12:59:49 -0800908 }
909 }
910 }
911
912 /**
Dianne Hackbornba398392011-08-01 16:11:57 -0700913 * Remove all internal references to the last loaded wallpaper. Useful
914 * for apps that want to reduce memory usage when they only temporarily
915 * need to have the wallpaper. After calling, the next request for the
916 * wallpaper will require reloading it again from disk.
917 */
918 public void forgetLoadedWallpaper() {
Christopher Tatebe132e62016-02-10 12:59:49 -0800919 sGlobals.forgetLoadedWallpaper();
Dianne Hackbornba398392011-08-01 16:11:57 -0700920 }
921
922 /**
Dianne Hackborneb034652009-09-07 00:49:58 -0700923 * If the current wallpaper is a live wallpaper component, return the
924 * information about that wallpaper. Otherwise, if it is a static image,
925 * simply return null.
Dianne Hackborneb034652009-09-07 00:49:58 -0700926 */
927 public WallpaperInfo getWallpaperInfo() {
928 try {
Mike Lockwoodc067c9c2011-10-31 12:50:12 -0400929 if (sGlobals.mService == null) {
930 Log.w(TAG, "WallpaperService not running");
Christopher Tate98d609c2016-05-18 17:31:58 -0700931 throw new RuntimeException(new DeadSystemException());
Mike Lockwoodc067c9c2011-10-31 12:50:12 -0400932 } else {
Jorim Jaggie31f6b82016-07-01 16:15:09 -0700933 return sGlobals.mService.getWallpaperInfo(UserHandle.myUserId());
Mike Lockwoodc067c9c2011-10-31 12:50:12 -0400934 }
Dianne Hackborneb034652009-09-07 00:49:58 -0700935 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -0700936 throw e.rethrowFromSystemServer();
Dianne Hackborneb034652009-09-07 00:49:58 -0700937 }
938 }
Michael Jurkae8d1bf72013-09-09 15:58:54 +0200939
940 /**
Christopher Tatee409f0e2016-03-21 14:53:15 -0700941 * Get the ID of the current wallpaper of the given kind. If there is no
942 * such wallpaper configured, returns a negative number.
943 *
Christopher Tate98d609c2016-05-18 17:31:58 -0700944 * <p>Every time the wallpaper image is set, a new ID is assigned to it.
945 * This method allows the caller to determine whether the wallpaper imagery
946 * has changed, regardless of how that change happened.
947 *
Christopher Tatee409f0e2016-03-21 14:53:15 -0700948 * @param which The wallpaper whose ID is to be returned. Must be a single
Christopher Tateedf7d042016-03-29 18:24:25 -0700949 * defined kind of wallpaper, either {@link #FLAG_SYSTEM} or
950 * {@link #FLAG_LOCK}.
Christopher Tatee409f0e2016-03-21 14:53:15 -0700951 * @return The positive numeric ID of the current wallpaper of the given kind,
952 * or a negative value if no such wallpaper is configured.
953 */
Christopher Tate5d99d472016-05-06 17:59:27 -0700954 public int getWallpaperId(@SetWallpaperFlags int which) {
Christopher Tatee409f0e2016-03-21 14:53:15 -0700955 return getWallpaperIdForUser(which, mContext.getUserId());
956 }
957
958 /**
959 * Get the ID of the given user's current wallpaper of the given kind. If there
960 * is no such wallpaper configured, returns a negative number.
961 * @hide
962 */
Christopher Tate5d99d472016-05-06 17:59:27 -0700963 public int getWallpaperIdForUser(@SetWallpaperFlags int which, int userId) {
Christopher Tatee409f0e2016-03-21 14:53:15 -0700964 try {
965 if (sGlobals.mService == null) {
966 Log.w(TAG, "WallpaperService not running");
Christopher Tate98d609c2016-05-18 17:31:58 -0700967 throw new RuntimeException(new DeadSystemException());
Christopher Tatee409f0e2016-03-21 14:53:15 -0700968 } else {
969 return sGlobals.mService.getWallpaperIdForUser(which, userId);
970 }
971 } catch (RemoteException e) {
972 throw e.rethrowFromSystemServer();
973 }
974 }
975
976 /**
Michael Jurkae8d1bf72013-09-09 15:58:54 +0200977 * Gets an Intent that will launch an activity that crops the given
978 * image and sets the device's wallpaper. If there is a default HOME activity
979 * that supports cropping wallpapers, it will be preferred as the default.
Ying Wang930d4e52013-09-14 11:57:17 -0700980 * Use this method instead of directly creating a {@link #ACTION_CROP_AND_SET_WALLPAPER}
Michael Jurkae8d1bf72013-09-09 15:58:54 +0200981 * intent.
Adam Lesinskibba72d12013-10-11 18:10:56 -0700982 *
983 * @param imageUri The image URI that will be set in the intent. The must be a content
984 * URI and its provider must resolve its type to "image/*"
985 *
986 * @throws IllegalArgumentException if the URI is not a content URI or its MIME type is
987 * not "image/*"
Michael Jurkae8d1bf72013-09-09 15:58:54 +0200988 */
989 public Intent getCropAndSetWallpaperIntent(Uri imageUri) {
Allen Shen1c0f21e2014-04-29 16:16:29 +0800990 if (imageUri == null) {
991 throw new IllegalArgumentException("Image URI must not be null");
992 }
993
Adam Lesinskibba72d12013-10-11 18:10:56 -0700994 if (!ContentResolver.SCHEME_CONTENT.equals(imageUri.getScheme())) {
995 throw new IllegalArgumentException("Image URI must be of the "
996 + ContentResolver.SCHEME_CONTENT + " scheme type");
997 }
998
Michael Jurkae8d1bf72013-09-09 15:58:54 +0200999 final PackageManager packageManager = mContext.getPackageManager();
1000 Intent cropAndSetWallpaperIntent =
1001 new Intent(ACTION_CROP_AND_SET_WALLPAPER, imageUri);
1002 cropAndSetWallpaperIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
1003
1004 // Find out if the default HOME activity supports CROP_AND_SET_WALLPAPER
1005 Intent homeIntent = new Intent(Intent.ACTION_MAIN).addCategory(Intent.CATEGORY_HOME);
1006 ResolveInfo resolvedHome = packageManager.resolveActivity(homeIntent,
1007 PackageManager.MATCH_DEFAULT_ONLY);
1008 if (resolvedHome != null) {
1009 cropAndSetWallpaperIntent.setPackage(resolvedHome.activityInfo.packageName);
1010
1011 List<ResolveInfo> cropAppList = packageManager.queryIntentActivities(
1012 cropAndSetWallpaperIntent, 0);
1013 if (cropAppList.size() > 0) {
1014 return cropAndSetWallpaperIntent;
1015 }
1016 }
1017
1018 // fallback crop activity
Oren Blasberg60598ea0a2016-02-26 11:01:23 -08001019 final String cropperPackage = mContext.getString(
1020 com.android.internal.R.string.config_wallpaperCropperPackage);
1021 cropAndSetWallpaperIntent.setPackage(cropperPackage);
Adam Lesinskibba72d12013-10-11 18:10:56 -07001022 List<ResolveInfo> cropAppList = packageManager.queryIntentActivities(
1023 cropAndSetWallpaperIntent, 0);
1024 if (cropAppList.size() > 0) {
1025 return cropAndSetWallpaperIntent;
1026 }
1027 // If the URI is not of the right type, or for some reason the system wallpaper
1028 // cropper doesn't exist, return null
1029 throw new IllegalArgumentException("Cannot use passed URI to set wallpaper; " +
1030 "check that the type returned by ContentProvider matches image/*");
Michael Jurkae8d1bf72013-09-09 15:58:54 +02001031 }
1032
Dianne Hackborneb034652009-09-07 00:49:58 -07001033 /**
Dianne Hackborn8cc6a502009-08-05 21:29:42 -07001034 * Change the current system wallpaper to the bitmap in the given resource.
1035 * The resource is opened as a raw data stream and copied into the
1036 * wallpaper; it must be a valid PNG or JPEG image. On success, the intent
1037 * {@link Intent#ACTION_WALLPAPER_CHANGED} is broadcast.
1038 *
Nicolas Falliere9530e3a2012-06-18 17:21:06 -07001039 * <p>This method requires the caller to hold the permission
1040 * {@link android.Manifest.permission#SET_WALLPAPER}.
1041 *
Christopher Tateedf7d042016-03-29 18:24:25 -07001042 * @param resid The resource ID of the bitmap to be used as the wallpaper image
Dianne Hackborn8cc6a502009-08-05 21:29:42 -07001043 *
Michael Jurkab668d0b2013-10-04 15:11:05 -07001044 * @throws IOException If an error occurs reverting to the built-in
Dianne Hackborn8cc6a502009-08-05 21:29:42 -07001045 * wallpaper.
1046 */
Tor Norbye7b9c9122013-05-30 16:48:33 -07001047 public void setResource(@RawRes int resid) throws IOException {
Edward Savage-Jones6009d9d2016-08-30 18:44:19 +02001048 setResource(resid, FLAG_SYSTEM | FLAG_LOCK);
Christopher Tatead3c2592016-01-20 18:13:17 -08001049 }
1050
1051 /**
Christopher Tateedf7d042016-03-29 18:24:25 -07001052 * Version of {@link #setResource(int)} that allows the caller to specify which
1053 * of the supported wallpaper categories to set.
Christopher Tatead3c2592016-01-20 18:13:17 -08001054 *
Christopher Tateedf7d042016-03-29 18:24:25 -07001055 * @param resid The resource ID of the bitmap to be used as the wallpaper image
1056 * @param which Flags indicating which wallpaper(s) to configure with the new imagery
Christopher Tatead3c2592016-01-20 18:13:17 -08001057 *
Christopher Tateedf7d042016-03-29 18:24:25 -07001058 * @see #FLAG_LOCK
1059 * @see #FLAG_SYSTEM
Christopher Tatead3c2592016-01-20 18:13:17 -08001060 *
1061 * @return An integer ID assigned to the newly active wallpaper; or zero on failure.
1062 *
1063 * @throws IOException
1064 */
1065 public int setResource(@RawRes int resid, @SetWallpaperFlags int which)
1066 throws IOException {
Mike Lockwoodc067c9c2011-10-31 12:50:12 -04001067 if (sGlobals.mService == null) {
1068 Log.w(TAG, "WallpaperService not running");
Christopher Tate98d609c2016-05-18 17:31:58 -07001069 throw new RuntimeException(new DeadSystemException());
Mike Lockwoodc067c9c2011-10-31 12:50:12 -04001070 }
Christopher Tatead3c2592016-01-20 18:13:17 -08001071 final Bundle result = new Bundle();
Christopher Tate1e1e2e02016-01-25 15:34:36 -08001072 final WallpaperSetCompletion completion = new WallpaperSetCompletion();
Dianne Hackborn8cc6a502009-08-05 21:29:42 -07001073 try {
1074 Resources resources = mContext.getResources();
1075 /* Set the wallpaper to the default values */
Dianne Hackborn840c3a22009-09-02 17:35:17 -07001076 ParcelFileDescriptor fd = sGlobals.mService.setWallpaper(
Christopher Tatead3c2592016-01-20 18:13:17 -08001077 "res:" + resources.getResourceName(resid),
Jorim Jaggi6c902d02016-08-18 10:44:54 -07001078 mContext.getOpPackageName(), null, false, result, which, completion,
1079 UserHandle.myUserId());
Dianne Hackborn8cc6a502009-08-05 21:29:42 -07001080 if (fd != null) {
1081 FileOutputStream fos = null;
Christopher Tate1e1e2e02016-01-25 15:34:36 -08001082 boolean ok = false;
Dianne Hackborn8cc6a502009-08-05 21:29:42 -07001083 try {
1084 fos = new ParcelFileDescriptor.AutoCloseOutputStream(fd);
Christopher Tatead3c2592016-01-20 18:13:17 -08001085 copyStreamToWallpaperFile(resources.openRawResource(resid), fos);
Christopher Tate1e1e2e02016-01-25 15:34:36 -08001086 // The 'close()' is the trigger for any server-side image manipulation,
1087 // so we must do that before waiting for completion.
1088 fos.close();
1089 completion.waitForCompletion();
Dianne Hackborn8cc6a502009-08-05 21:29:42 -07001090 } finally {
Christopher Tate1e1e2e02016-01-25 15:34:36 -08001091 // Might be redundant but completion shouldn't wait unless the write
1092 // succeeded; this is a fallback if it threw past the close+wait.
Christopher Tateffa6a882015-12-11 15:08:46 -08001093 IoUtils.closeQuietly(fos);
Dianne Hackborn8cc6a502009-08-05 21:29:42 -07001094 }
1095 }
1096 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07001097 throw e.rethrowFromSystemServer();
Dianne Hackborn8cc6a502009-08-05 21:29:42 -07001098 }
Christopher Tatead3c2592016-01-20 18:13:17 -08001099 return result.getInt(EXTRA_NEW_WALLPAPER_ID, 0);
Dianne Hackborn8cc6a502009-08-05 21:29:42 -07001100 }
Christopher Tateffa6a882015-12-11 15:08:46 -08001101
Dianne Hackborn8cc6a502009-08-05 21:29:42 -07001102 /**
1103 * Change the current system wallpaper to a bitmap. The given bitmap is
1104 * converted to a PNG and stored as the wallpaper. On success, the intent
1105 * {@link Intent#ACTION_WALLPAPER_CHANGED} is broadcast.
1106 *
Christopher Tateffa6a882015-12-11 15:08:46 -08001107 * <p>This method is equivalent to calling
1108 * {@link #setBitmap(Bitmap, Rect, boolean)} and passing {@code null} for the
1109 * {@code visibleCrop} rectangle and {@code true} for the {@code allowBackup}
1110 * parameter.
1111 *
Nicolas Falliere9530e3a2012-06-18 17:21:06 -07001112 * <p>This method requires the caller to hold the permission
1113 * {@link android.Manifest.permission#SET_WALLPAPER}.
1114 *
Christopher Tatead3c2592016-01-20 18:13:17 -08001115 * @param bitmap The bitmap to be used as the new system wallpaper.
Dianne Hackborn8cc6a502009-08-05 21:29:42 -07001116 *
Christopher Tateffa6a882015-12-11 15:08:46 -08001117 * @throws IOException If an error occurs when attempting to set the wallpaper
1118 * to the provided image.
Dianne Hackborn8cc6a502009-08-05 21:29:42 -07001119 */
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001120 public void setBitmap(Bitmap bitmap) throws IOException {
Christopher Tateffa6a882015-12-11 15:08:46 -08001121 setBitmap(bitmap, null, true);
1122 }
1123
1124 /**
1125 * Change the current system wallpaper to a bitmap, specifying a hint about
1126 * which subrectangle of the full image is to be visible. The OS will then
1127 * try to best present the given portion of the full image as the static system
1128 * wallpaper image. On success, the intent
1129 * {@link Intent#ACTION_WALLPAPER_CHANGED} is broadcast.
1130 *
1131 * <p>Passing {@code null} as the {@code visibleHint} parameter is equivalent to
1132 * passing (0, 0, {@code fullImage.getWidth()}, {@code fullImage.getHeight()}).
1133 *
1134 * <p>This method requires the caller to hold the permission
1135 * {@link android.Manifest.permission#SET_WALLPAPER}.
1136 *
Christopher Tatead3c2592016-01-20 18:13:17 -08001137 * @param fullImage A bitmap that will supply the wallpaper imagery.
Christopher Tateffa6a882015-12-11 15:08:46 -08001138 * @param visibleCropHint The rectangular subregion of {@code fullImage} that should be
1139 * displayed as wallpaper. Passing {@code null} for this parameter means that
1140 * the full image should be displayed if possible given the image's and device's
Christopher Tatead3c2592016-01-20 18:13:17 -08001141 * aspect ratios, etc.
Christopher Tateffa6a882015-12-11 15:08:46 -08001142 * @param allowBackup {@code true} if the OS is permitted to back up this wallpaper
1143 * image for restore to a future device; {@code false} otherwise.
1144 *
Christopher Tatead3c2592016-01-20 18:13:17 -08001145 * @return An integer ID assigned to the newly active wallpaper; or zero on failure.
1146 *
Christopher Tateffa6a882015-12-11 15:08:46 -08001147 * @throws IOException If an error occurs when attempting to set the wallpaper
1148 * to the provided image.
1149 * @throws IllegalArgumentException If the {@code visibleCropHint} rectangle is
1150 * empty or invalid.
1151 */
Christopher Tatead3c2592016-01-20 18:13:17 -08001152 public int setBitmap(Bitmap fullImage, Rect visibleCropHint, boolean allowBackup)
1153 throws IOException {
Edward Savage-Jones6009d9d2016-08-30 18:44:19 +02001154 return setBitmap(fullImage, visibleCropHint, allowBackup, FLAG_SYSTEM | FLAG_LOCK);
Christopher Tatead3c2592016-01-20 18:13:17 -08001155 }
1156
1157 /**
Christopher Tatead3c2592016-01-20 18:13:17 -08001158 * Version of {@link #setBitmap(Bitmap, Rect, boolean)} that allows the caller
1159 * to specify which of the supported wallpaper categories to set.
1160 *
1161 * @param fullImage A bitmap that will supply the wallpaper imagery.
1162 * @param visibleCropHint The rectangular subregion of {@code fullImage} that should be
1163 * displayed as wallpaper. Passing {@code null} for this parameter means that
1164 * the full image should be displayed if possible given the image's and device's
1165 * aspect ratios, etc.
1166 * @param allowBackup {@code true} if the OS is permitted to back up this wallpaper
1167 * image for restore to a future device; {@code false} otherwise.
1168 * @param which Flags indicating which wallpaper(s) to configure with the new imagery.
1169 *
Christopher Tateedf7d042016-03-29 18:24:25 -07001170 * @see #FLAG_LOCK
1171 * @see #FLAG_SYSTEM
Christopher Tatead3c2592016-01-20 18:13:17 -08001172 *
1173 * @return An integer ID assigned to the newly active wallpaper; or zero on failure.
1174 *
1175 * @throws IOException
1176 */
1177 public int setBitmap(Bitmap fullImage, Rect visibleCropHint,
1178 boolean allowBackup, @SetWallpaperFlags int which)
Christopher Tateffa6a882015-12-11 15:08:46 -08001179 throws IOException {
Jorim Jaggi6c902d02016-08-18 10:44:54 -07001180 return setBitmap(fullImage, visibleCropHint, allowBackup, which,
1181 UserHandle.myUserId());
1182 }
1183
1184 /**
1185 * Like {@link #setBitmap(Bitmap, Rect, boolean, int)}, but allows to pass in an explicit user
1186 * id. If the user id doesn't match the user id the process is running under, calling this
1187 * requires permission {@link android.Manifest.permission#INTERACT_ACROSS_USERS_FULL}.
1188 * @hide
1189 */
1190 public int setBitmap(Bitmap fullImage, Rect visibleCropHint,
1191 boolean allowBackup, @SetWallpaperFlags int which, int userId)
1192 throws IOException {
Christopher Tateffa6a882015-12-11 15:08:46 -08001193 validateRect(visibleCropHint);
Mike Lockwoodc067c9c2011-10-31 12:50:12 -04001194 if (sGlobals.mService == null) {
1195 Log.w(TAG, "WallpaperService not running");
Christopher Tate98d609c2016-05-18 17:31:58 -07001196 throw new RuntimeException(new DeadSystemException());
Mike Lockwoodc067c9c2011-10-31 12:50:12 -04001197 }
Christopher Tatead3c2592016-01-20 18:13:17 -08001198 final Bundle result = new Bundle();
Christopher Tate1e1e2e02016-01-25 15:34:36 -08001199 final WallpaperSetCompletion completion = new WallpaperSetCompletion();
Dianne Hackborn8cc6a502009-08-05 21:29:42 -07001200 try {
Benjamin Franzf3ece362015-02-11 10:51:10 +00001201 ParcelFileDescriptor fd = sGlobals.mService.setWallpaper(null,
Christopher Tated7faf532016-02-25 12:43:38 -08001202 mContext.getOpPackageName(), visibleCropHint, allowBackup,
Jorim Jaggi6c902d02016-08-18 10:44:54 -07001203 result, which, completion, userId);
Christopher Tatead3c2592016-01-20 18:13:17 -08001204 if (fd != null) {
1205 FileOutputStream fos = null;
1206 try {
1207 fos = new ParcelFileDescriptor.AutoCloseOutputStream(fd);
1208 fullImage.compress(Bitmap.CompressFormat.PNG, 90, fos);
Christopher Tate1e1e2e02016-01-25 15:34:36 -08001209 fos.close();
1210 completion.waitForCompletion();
Christopher Tatead3c2592016-01-20 18:13:17 -08001211 } finally {
1212 IoUtils.closeQuietly(fos);
1213 }
Dianne Hackborn8cc6a502009-08-05 21:29:42 -07001214 }
1215 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07001216 throw e.rethrowFromSystemServer();
Dianne Hackborn8cc6a502009-08-05 21:29:42 -07001217 }
Christopher Tatead3c2592016-01-20 18:13:17 -08001218 return result.getInt(EXTRA_NEW_WALLPAPER_ID, 0);
Dianne Hackborn8cc6a502009-08-05 21:29:42 -07001219 }
1220
Christopher Tateffa6a882015-12-11 15:08:46 -08001221 private final void validateRect(Rect rect) {
1222 if (rect != null && rect.isEmpty()) {
1223 throw new IllegalArgumentException("visibleCrop rectangle must be valid and non-empty");
1224 }
1225 }
1226
Dianne Hackborn8cc6a502009-08-05 21:29:42 -07001227 /**
1228 * Change the current system wallpaper to a specific byte stream. The
1229 * give InputStream is copied into persistent storage and will now be
1230 * used as the wallpaper. Currently it must be either a JPEG or PNG
1231 * image. On success, the intent {@link Intent#ACTION_WALLPAPER_CHANGED}
1232 * is broadcast.
1233 *
Christopher Tateffa6a882015-12-11 15:08:46 -08001234 * <p>This method is equivalent to calling
1235 * {@link #setStream(InputStream, Rect, boolean)} and passing {@code null} for the
1236 * {@code visibleCrop} rectangle and {@code true} for the {@code allowBackup}
1237 * parameter.
1238 *
Nicolas Falliere9530e3a2012-06-18 17:21:06 -07001239 * <p>This method requires the caller to hold the permission
1240 * {@link android.Manifest.permission#SET_WALLPAPER}.
1241 *
Christopher Tate98d609c2016-05-18 17:31:58 -07001242 * @param bitmapData A stream containing the raw data to install as a wallpaper. This
1243 * data can be in any format handled by {@link BitmapRegionDecoder}.
Dianne Hackborn8cc6a502009-08-05 21:29:42 -07001244 *
Christopher Tateffa6a882015-12-11 15:08:46 -08001245 * @throws IOException If an error occurs when attempting to set the wallpaper
1246 * based on the provided image data.
Dianne Hackborn8cc6a502009-08-05 21:29:42 -07001247 */
Christopher Tateffa6a882015-12-11 15:08:46 -08001248 public void setStream(InputStream bitmapData) throws IOException {
1249 setStream(bitmapData, null, true);
1250 }
1251
Christopher Tatead3c2592016-01-20 18:13:17 -08001252 private void copyStreamToWallpaperFile(InputStream data, FileOutputStream fos)
Christopher Tateffa6a882015-12-11 15:08:46 -08001253 throws IOException {
1254 byte[] buffer = new byte[32768];
1255 int amt;
1256 while ((amt=data.read(buffer)) > 0) {
1257 fos.write(buffer, 0, amt);
1258 }
1259 }
1260
1261 /**
1262 * Change the current system wallpaper to a specific byte stream, specifying a
1263 * hint about which subrectangle of the full image is to be visible. The OS will
1264 * then try to best present the given portion of the full image as the static system
1265 * wallpaper image. The data from the given InputStream is copied into persistent
1266 * storage and will then be used as the system wallpaper. Currently the data must
1267 * be either a JPEG or PNG image. On success, the intent
1268 * {@link Intent#ACTION_WALLPAPER_CHANGED} is broadcast.
1269 *
1270 * <p>This method requires the caller to hold the permission
1271 * {@link android.Manifest.permission#SET_WALLPAPER}.
1272 *
Christopher Tate98d609c2016-05-18 17:31:58 -07001273 * @param bitmapData A stream containing the raw data to install as a wallpaper. This
1274 * data can be in any format handled by {@link BitmapRegionDecoder}.
Christopher Tateffa6a882015-12-11 15:08:46 -08001275 * @param visibleCropHint The rectangular subregion of the streamed image that should be
1276 * displayed as wallpaper. Passing {@code null} for this parameter means that
1277 * the full image should be displayed if possible given the image's and device's
1278 * aspect ratios, etc.
1279 * @param allowBackup {@code true} if the OS is permitted to back up this wallpaper
1280 * image for restore to a future device; {@code false} otherwise.
Christopher Tate98d609c2016-05-18 17:31:58 -07001281 * @return An integer ID assigned to the newly active wallpaper; or zero on failure.
1282 *
1283 * @see #getWallpaperId(int)
Christopher Tateffa6a882015-12-11 15:08:46 -08001284 *
1285 * @throws IOException If an error occurs when attempting to set the wallpaper
1286 * based on the provided image data.
1287 * @throws IllegalArgumentException If the {@code visibleCropHint} rectangle is
1288 * empty or invalid.
1289 */
Christopher Tatead3c2592016-01-20 18:13:17 -08001290 public int setStream(InputStream bitmapData, Rect visibleCropHint, boolean allowBackup)
Christopher Tateffa6a882015-12-11 15:08:46 -08001291 throws IOException {
Edward Savage-Jones6009d9d2016-08-30 18:44:19 +02001292 return setStream(bitmapData, visibleCropHint, allowBackup, FLAG_SYSTEM | FLAG_LOCK);
Christopher Tatead3c2592016-01-20 18:13:17 -08001293 }
1294
1295 /**
1296 * Version of {@link #setStream(InputStream, Rect, boolean)} that allows the caller
1297 * to specify which of the supported wallpaper categories to set.
1298 *
Christopher Tate98d609c2016-05-18 17:31:58 -07001299 * @param bitmapData A stream containing the raw data to install as a wallpaper. This
1300 * data can be in any format handled by {@link BitmapRegionDecoder}.
Christopher Tatead3c2592016-01-20 18:13:17 -08001301 * @param visibleCropHint The rectangular subregion of the streamed image that should be
1302 * displayed as wallpaper. Passing {@code null} for this parameter means that
1303 * the full image should be displayed if possible given the image's and device's
1304 * aspect ratios, etc.
1305 * @param allowBackup {@code true} if the OS is permitted to back up this wallpaper
1306 * image for restore to a future device; {@code false} otherwise.
1307 * @param which Flags indicating which wallpaper(s) to configure with the new imagery.
Christopher Tate98d609c2016-05-18 17:31:58 -07001308 * @return An integer ID assigned to the newly active wallpaper; or zero on failure.
Christopher Tatead3c2592016-01-20 18:13:17 -08001309 *
Christopher Tate98d609c2016-05-18 17:31:58 -07001310 * @see #getWallpaperId(int)
Christopher Tateedf7d042016-03-29 18:24:25 -07001311 * @see #FLAG_LOCK
1312 * @see #FLAG_SYSTEM
Christopher Tatead3c2592016-01-20 18:13:17 -08001313 *
1314 * @throws IOException
1315 */
1316 public int setStream(InputStream bitmapData, Rect visibleCropHint,
1317 boolean allowBackup, @SetWallpaperFlags int which)
1318 throws IOException {
Christopher Tateffa6a882015-12-11 15:08:46 -08001319 validateRect(visibleCropHint);
Mike Lockwoodc067c9c2011-10-31 12:50:12 -04001320 if (sGlobals.mService == null) {
1321 Log.w(TAG, "WallpaperService not running");
Christopher Tate98d609c2016-05-18 17:31:58 -07001322 throw new RuntimeException(new DeadSystemException());
Mike Lockwoodc067c9c2011-10-31 12:50:12 -04001323 }
Christopher Tatead3c2592016-01-20 18:13:17 -08001324 final Bundle result = new Bundle();
Christopher Tate1e1e2e02016-01-25 15:34:36 -08001325 final WallpaperSetCompletion completion = new WallpaperSetCompletion();
Dianne Hackborn8cc6a502009-08-05 21:29:42 -07001326 try {
Benjamin Franzf3ece362015-02-11 10:51:10 +00001327 ParcelFileDescriptor fd = sGlobals.mService.setWallpaper(null,
Christopher Tated7faf532016-02-25 12:43:38 -08001328 mContext.getOpPackageName(), visibleCropHint, allowBackup,
Jorim Jaggi6c902d02016-08-18 10:44:54 -07001329 result, which, completion, UserHandle.myUserId());
Christopher Tatead3c2592016-01-20 18:13:17 -08001330 if (fd != null) {
1331 FileOutputStream fos = null;
1332 try {
1333 fos = new ParcelFileDescriptor.AutoCloseOutputStream(fd);
1334 copyStreamToWallpaperFile(bitmapData, fos);
Christopher Tate1e1e2e02016-01-25 15:34:36 -08001335 fos.close();
1336 completion.waitForCompletion();
Christopher Tatead3c2592016-01-20 18:13:17 -08001337 } finally {
1338 IoUtils.closeQuietly(fos);
1339 }
Dianne Hackborn8cc6a502009-08-05 21:29:42 -07001340 }
1341 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07001342 throw e.rethrowFromSystemServer();
Dianne Hackborn8cc6a502009-08-05 21:29:42 -07001343 }
Christopher Tatead3c2592016-01-20 18:13:17 -08001344
1345 return result.getInt(EXTRA_NEW_WALLPAPER_ID, 0);
Dianne Hackborn8cc6a502009-08-05 21:29:42 -07001346 }
1347
Dianne Hackborn8cc6a502009-08-05 21:29:42 -07001348 /**
Dianne Hackborn5dc5a002012-09-15 19:33:48 -07001349 * Return whether any users are currently set to use the wallpaper
1350 * with the given resource ID. That is, their wallpaper has been
1351 * set through {@link #setResource(int)} with the same resource id.
1352 */
Tor Norbye7b9c9122013-05-30 16:48:33 -07001353 public boolean hasResourceWallpaper(@RawRes int resid) {
Dianne Hackborn5dc5a002012-09-15 19:33:48 -07001354 if (sGlobals.mService == null) {
1355 Log.w(TAG, "WallpaperService not running");
Christopher Tate98d609c2016-05-18 17:31:58 -07001356 throw new RuntimeException(new DeadSystemException());
Dianne Hackborn5dc5a002012-09-15 19:33:48 -07001357 }
1358 try {
1359 Resources resources = mContext.getResources();
1360 String name = "res:" + resources.getResourceName(resid);
1361 return sGlobals.mService.hasNamedWallpaper(name);
1362 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07001363 throw e.rethrowFromSystemServer();
Dianne Hackborn5dc5a002012-09-15 19:33:48 -07001364 }
1365 }
1366
1367 /**
Dianne Hackborn8cc6a502009-08-05 21:29:42 -07001368 * Returns the desired minimum width for the wallpaper. Callers of
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001369 * {@link #setBitmap(android.graphics.Bitmap)} or
1370 * {@link #setStream(java.io.InputStream)} should check this value
Dianne Hackborn8cc6a502009-08-05 21:29:42 -07001371 * beforehand to make sure the supplied wallpaper respects the desired
1372 * minimum width.
1373 *
1374 * If the returned value is <= 0, the caller should use the width of
1375 * the default display instead.
1376 *
1377 * @return The desired minimum width for the wallpaper. This value should
1378 * be honored by applications that set the wallpaper but it is not
1379 * mandatory.
1380 */
1381 public int getDesiredMinimumWidth() {
Mike Lockwoodc067c9c2011-10-31 12:50:12 -04001382 if (sGlobals.mService == null) {
1383 Log.w(TAG, "WallpaperService not running");
Christopher Tate98d609c2016-05-18 17:31:58 -07001384 throw new RuntimeException(new DeadSystemException());
Mike Lockwoodc067c9c2011-10-31 12:50:12 -04001385 }
Dianne Hackborn8cc6a502009-08-05 21:29:42 -07001386 try {
Dianne Hackborn840c3a22009-09-02 17:35:17 -07001387 return sGlobals.mService.getWidthHint();
Dianne Hackborn8cc6a502009-08-05 21:29:42 -07001388 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07001389 throw e.rethrowFromSystemServer();
Dianne Hackborn8cc6a502009-08-05 21:29:42 -07001390 }
1391 }
1392
1393 /**
1394 * Returns the desired minimum height for the wallpaper. Callers of
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001395 * {@link #setBitmap(android.graphics.Bitmap)} or
1396 * {@link #setStream(java.io.InputStream)} should check this value
Dianne Hackborn8cc6a502009-08-05 21:29:42 -07001397 * beforehand to make sure the supplied wallpaper respects the desired
1398 * minimum height.
1399 *
1400 * If the returned value is <= 0, the caller should use the height of
1401 * the default display instead.
1402 *
1403 * @return The desired minimum height for the wallpaper. This value should
1404 * be honored by applications that set the wallpaper but it is not
1405 * mandatory.
1406 */
1407 public int getDesiredMinimumHeight() {
Mike Lockwoodc067c9c2011-10-31 12:50:12 -04001408 if (sGlobals.mService == null) {
1409 Log.w(TAG, "WallpaperService not running");
Christopher Tate98d609c2016-05-18 17:31:58 -07001410 throw new RuntimeException(new DeadSystemException());
Mike Lockwoodc067c9c2011-10-31 12:50:12 -04001411 }
Dianne Hackborn8cc6a502009-08-05 21:29:42 -07001412 try {
Dianne Hackborn840c3a22009-09-02 17:35:17 -07001413 return sGlobals.mService.getHeightHint();
Dianne Hackborn8cc6a502009-08-05 21:29:42 -07001414 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07001415 throw e.rethrowFromSystemServer();
Dianne Hackborn8cc6a502009-08-05 21:29:42 -07001416 }
1417 }
1418
1419 /**
1420 * For use only by the current home application, to specify the size of
1421 * wallpaper it would like to use. This allows such applications to have
1422 * a virtual wallpaper that is larger than the physical screen, matching
1423 * the size of their workspace.
Dianne Hackbornc5bf7582012-04-25 19:12:07 -07001424 *
1425 * <p>Note developers, who don't seem to be reading this. This is
Christopher Tateffa6a882015-12-11 15:08:46 -08001426 * for <em>home apps</em> to tell what size wallpaper they would like.
1427 * Nobody else should be calling this! Certainly not other non-home
Dianne Hackbornc5bf7582012-04-25 19:12:07 -07001428 * apps that change the wallpaper. Those apps are supposed to
1429 * <b>retrieve</b> the suggested size so they can construct a wallpaper
1430 * that matches it.
1431 *
Nicolas Falliere9530e3a2012-06-18 17:21:06 -07001432 * <p>This method requires the caller to hold the permission
1433 * {@link android.Manifest.permission#SET_WALLPAPER_HINTS}.
1434 *
Dianne Hackborn8cc6a502009-08-05 21:29:42 -07001435 * @param minimumWidth Desired minimum width
1436 * @param minimumHeight Desired minimum height
1437 */
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001438 public void suggestDesiredDimensions(int minimumWidth, int minimumHeight) {
Dianne Hackborn8cc6a502009-08-05 21:29:42 -07001439 try {
Donghan Ryu289c2732011-11-14 18:56:11 -08001440 /**
1441 * The framework makes no attempt to limit the window size
1442 * to the maximum texture size. Any window larger than this
1443 * cannot be composited.
1444 *
1445 * Read maximum texture size from system property and scale down
1446 * minimumWidth and minimumHeight accordingly.
1447 */
1448 int maximumTextureSize;
1449 try {
1450 maximumTextureSize = SystemProperties.getInt("sys.max_texture_size", 0);
1451 } catch (Exception e) {
1452 maximumTextureSize = 0;
1453 }
1454
1455 if (maximumTextureSize > 0) {
1456 if ((minimumWidth > maximumTextureSize) ||
1457 (minimumHeight > maximumTextureSize)) {
1458 float aspect = (float)minimumHeight / (float)minimumWidth;
1459 if (minimumWidth > minimumHeight) {
1460 minimumWidth = maximumTextureSize;
1461 minimumHeight = (int)((minimumWidth * aspect) + 0.5);
1462 } else {
1463 minimumHeight = maximumTextureSize;
1464 minimumWidth = (int)((minimumHeight / aspect) + 0.5);
1465 }
1466 }
1467 }
1468
Mike Lockwoodc067c9c2011-10-31 12:50:12 -04001469 if (sGlobals.mService == null) {
1470 Log.w(TAG, "WallpaperService not running");
Christopher Tate98d609c2016-05-18 17:31:58 -07001471 throw new RuntimeException(new DeadSystemException());
Mike Lockwoodc067c9c2011-10-31 12:50:12 -04001472 } else {
Benjamin Franzf3ece362015-02-11 10:51:10 +00001473 sGlobals.mService.setDimensionHints(minimumWidth, minimumHeight,
1474 mContext.getOpPackageName());
Mike Lockwoodc067c9c2011-10-31 12:50:12 -04001475 }
Dianne Hackborn8cc6a502009-08-05 21:29:42 -07001476 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07001477 throw e.rethrowFromSystemServer();
Dianne Hackborn8cc6a502009-08-05 21:29:42 -07001478 }
1479 }
Adam Cohen041a0ba2011-11-09 20:10:27 -08001480
Dianne Hackborn8cc6a502009-08-05 21:29:42 -07001481 /**
Dianne Hackborn067e5f62014-09-07 23:14:30 -07001482 * Specify extra padding that the wallpaper should have outside of the display.
1483 * That is, the given padding supplies additional pixels the wallpaper should extend
1484 * outside of the display itself.
1485 * @param padding The number of pixels the wallpaper should extend beyond the display,
1486 * on its left, top, right, and bottom sides.
1487 * @hide
1488 */
1489 @SystemApi
Jeff Sharkeyd86b8fe2017-06-02 17:36:26 -06001490 @RequiresPermission(android.Manifest.permission.SET_WALLPAPER_HINTS)
Dianne Hackborn067e5f62014-09-07 23:14:30 -07001491 public void setDisplayPadding(Rect padding) {
1492 try {
1493 if (sGlobals.mService == null) {
1494 Log.w(TAG, "WallpaperService not running");
Christopher Tate98d609c2016-05-18 17:31:58 -07001495 throw new RuntimeException(new DeadSystemException());
Dianne Hackborn067e5f62014-09-07 23:14:30 -07001496 } else {
Benjamin Franzf3ece362015-02-11 10:51:10 +00001497 sGlobals.mService.setDisplayPadding(padding, mContext.getOpPackageName());
Dianne Hackborn067e5f62014-09-07 23:14:30 -07001498 }
1499 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07001500 throw e.rethrowFromSystemServer();
Dianne Hackborn067e5f62014-09-07 23:14:30 -07001501 }
1502 }
1503
1504 /**
1505 * Apply a raw offset to the wallpaper window. Should only be used in
1506 * combination with {@link #setDisplayPadding(android.graphics.Rect)} when you
1507 * have ensured that the wallpaper will extend outside of the display area so that
1508 * it can be moved without leaving part of the display uncovered.
1509 * @param x The offset, in pixels, to apply to the left edge.
1510 * @param y The offset, in pixels, to apply to the top edge.
1511 * @hide
1512 */
1513 @SystemApi
1514 public void setDisplayOffset(IBinder windowToken, int x, int y) {
1515 try {
1516 //Log.v(TAG, "Sending new wallpaper display offsets from app...");
1517 WindowManagerGlobal.getWindowSession().setWallpaperDisplayOffset(
1518 windowToken, x, y);
1519 //Log.v(TAG, "...app returning after sending display offset!");
1520 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07001521 throw e.rethrowFromSystemServer();
Dianne Hackborn067e5f62014-09-07 23:14:30 -07001522 }
1523 }
1524
1525 /**
Filip Gruszczynski9fedc722015-01-28 15:59:46 -08001526 * Clear the wallpaper.
1527 *
1528 * @hide
1529 */
1530 @SystemApi
Jeff Sharkeyd86b8fe2017-06-02 17:36:26 -06001531 @RequiresPermission(android.Manifest.permission.SET_WALLPAPER)
Filip Gruszczynski9fedc722015-01-28 15:59:46 -08001532 public void clearWallpaper() {
Christopher Tatea2bd5122016-09-22 13:18:05 -07001533 clearWallpaper(FLAG_LOCK, mContext.getUserId());
1534 clearWallpaper(FLAG_SYSTEM, mContext.getUserId());
Christopher Tatebe132e62016-02-10 12:59:49 -08001535 }
1536
1537 /**
1538 * Clear the wallpaper for a specific user. The caller must hold the
1539 * INTERACT_ACROSS_USERS_FULL permission to clear another user's
1540 * wallpaper.
1541 * @hide
1542 */
1543 @SystemApi
Jeff Sharkeyd86b8fe2017-06-02 17:36:26 -06001544 @RequiresPermission(android.Manifest.permission.SET_WALLPAPER)
Christopher Tate5d99d472016-05-06 17:59:27 -07001545 public void clearWallpaper(@SetWallpaperFlags int which, int userId) {
Filip Gruszczynski9fedc722015-01-28 15:59:46 -08001546 if (sGlobals.mService == null) {
1547 Log.w(TAG, "WallpaperService not running");
Christopher Tate98d609c2016-05-18 17:31:58 -07001548 throw new RuntimeException(new DeadSystemException());
Filip Gruszczynski9fedc722015-01-28 15:59:46 -08001549 }
1550 try {
Christopher Tatebe132e62016-02-10 12:59:49 -08001551 sGlobals.mService.clearWallpaper(mContext.getOpPackageName(), which, userId);
Filip Gruszczynski9fedc722015-01-28 15:59:46 -08001552 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07001553 throw e.rethrowFromSystemServer();
Filip Gruszczynski9fedc722015-01-28 15:59:46 -08001554 }
1555 }
1556
1557 /**
1558 * Set the live wallpaper.
1559 *
Filip Gruszczynski9fedc722015-01-28 15:59:46 -08001560 * @hide
1561 */
1562 @SystemApi
Jeff Sharkeyd86b8fe2017-06-02 17:36:26 -06001563 @RequiresPermission(android.Manifest.permission.SET_WALLPAPER_COMPONENT)
Filip Gruszczynski9fedc722015-01-28 15:59:46 -08001564 public boolean setWallpaperComponent(ComponentName name) {
Adrian Roos40ea0832016-07-14 14:19:55 -07001565 return setWallpaperComponent(name, UserHandle.myUserId());
1566 }
1567
1568 /**
1569 * Set the live wallpaper.
1570 *
1571 * This can only be called by packages with android.permission.SET_WALLPAPER_COMPONENT
1572 * permission. The caller must hold the INTERACT_ACROSS_USERS_FULL permission to change
1573 * another user's wallpaper.
1574 *
1575 * @hide
1576 */
1577 public boolean setWallpaperComponent(ComponentName name, int userId) {
Filip Gruszczynski9fedc722015-01-28 15:59:46 -08001578 if (sGlobals.mService == null) {
1579 Log.w(TAG, "WallpaperService not running");
Christopher Tate98d609c2016-05-18 17:31:58 -07001580 throw new RuntimeException(new DeadSystemException());
Filip Gruszczynski9fedc722015-01-28 15:59:46 -08001581 }
1582 try {
Adrian Roos40ea0832016-07-14 14:19:55 -07001583 sGlobals.mService.setWallpaperComponentChecked(name, mContext.getOpPackageName(),
1584 userId);
Filip Gruszczynski9fedc722015-01-28 15:59:46 -08001585 return true;
1586 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07001587 throw e.rethrowFromSystemServer();
Filip Gruszczynski9fedc722015-01-28 15:59:46 -08001588 }
Filip Gruszczynski9fedc722015-01-28 15:59:46 -08001589 }
1590
1591 /**
Christopher Tateffa6a882015-12-11 15:08:46 -08001592 * Set the display position of the current wallpaper within any larger space, when
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07001593 * that wallpaper is visible behind the given window. The X and Y offsets
1594 * are floating point numbers ranging from 0 to 1, representing where the
1595 * wallpaper should be positioned within the screen space. These only
Christopher Tateffa6a882015-12-11 15:08:46 -08001596 * make sense when the wallpaper is larger than the display.
Oleksandr Peletskyif2519812016-01-26 20:16:06 +01001597 *
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07001598 * @param windowToken The window who these offsets should be associated
Scott Main8b2e0002009-09-29 18:17:31 -07001599 * with, as returned by {@link android.view.View#getWindowToken()
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07001600 * View.getWindowToken()}.
Marco Nelissenbf6956b2009-11-09 15:21:13 -08001601 * @param xOffset The offset along the X dimension, from 0 to 1.
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07001602 * @param yOffset The offset along the Y dimension, from 0 to 1.
1603 */
1604 public void setWallpaperOffsets(IBinder windowToken, float xOffset, float yOffset) {
Adam Cohen791a6332012-01-12 14:38:38 -08001605 try {
1606 //Log.v(TAG, "Sending new wallpaper offsets from app...");
Jeff Brownf9e989d2013-04-04 23:04:03 -07001607 WindowManagerGlobal.getWindowSession().setWallpaperPosition(
Adam Cohen791a6332012-01-12 14:38:38 -08001608 windowToken, xOffset, yOffset, mWallpaperXStep, mWallpaperYStep);
1609 //Log.v(TAG, "...app returning after sending offsets!");
1610 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07001611 throw e.rethrowFromSystemServer();
Adam Cohen791a6332012-01-12 14:38:38 -08001612 }
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07001613 }
Adam Cohen041a0ba2011-11-09 20:10:27 -08001614
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07001615 /**
Marco Nelissenbf6956b2009-11-09 15:21:13 -08001616 * For applications that use multiple virtual screens showing a wallpaper,
1617 * specify the step size between virtual screens. For example, if the
Dianne Hackborn23ef7b42009-11-18 18:20:39 -08001618 * launcher has 3 virtual screens, it would specify an xStep of 0.5,
Marco Nelissenbf6956b2009-11-09 15:21:13 -08001619 * since the X offset for those screens are 0.0, 0.5 and 1.0
Oleksandr Peletskyif2519812016-01-26 20:16:06 +01001620 * @param xStep The X offset delta from one screen to the next one
Marco Nelissenbf6956b2009-11-09 15:21:13 -08001621 * @param yStep The Y offset delta from one screen to the next one
1622 */
1623 public void setWallpaperOffsetSteps(float xStep, float yStep) {
1624 mWallpaperXStep = xStep;
1625 mWallpaperYStep = yStep;
1626 }
Oleksandr Peletskyif2519812016-01-26 20:16:06 +01001627
Marco Nelissenbf6956b2009-11-09 15:21:13 -08001628 /**
Dianne Hackborn75804932009-10-20 20:15:20 -07001629 * Send an arbitrary command to the current active wallpaper.
Oleksandr Peletskyif2519812016-01-26 20:16:06 +01001630 *
Dianne Hackborn75804932009-10-20 20:15:20 -07001631 * @param windowToken The window who these offsets should be associated
1632 * with, as returned by {@link android.view.View#getWindowToken()
1633 * View.getWindowToken()}.
1634 * @param action Name of the command to perform. This must be a scoped
1635 * name to avoid collisions, such as "com.mycompany.wallpaper.DOIT".
1636 * @param x Arbitrary integer argument based on command.
1637 * @param y Arbitrary integer argument based on command.
1638 * @param z Arbitrary integer argument based on command.
1639 * @param extras Optional additional information for the command, or null.
1640 */
1641 public void sendWallpaperCommand(IBinder windowToken, String action,
1642 int x, int y, int z, Bundle extras) {
1643 try {
1644 //Log.v(TAG, "Sending new wallpaper offsets from app...");
Jeff Brownf9e989d2013-04-04 23:04:03 -07001645 WindowManagerGlobal.getWindowSession().sendWallpaperCommand(
Dianne Hackborn75804932009-10-20 20:15:20 -07001646 windowToken, action, x, y, z, extras, false);
1647 //Log.v(TAG, "...app returning after sending offsets!");
1648 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07001649 throw e.rethrowFromSystemServer();
Dianne Hackborn75804932009-10-20 20:15:20 -07001650 }
1651 }
Benjamin Franzf3ece362015-02-11 10:51:10 +00001652
1653 /**
1654 * Returns whether wallpapers are supported for the calling user. If this function returns
Oleksandr Peletskyif2519812016-01-26 20:16:06 +01001655 * {@code false}, any attempts to changing the wallpaper will have no effect,
1656 * and any attempt to obtain of the wallpaper will return {@code null}.
Benjamin Franzf3ece362015-02-11 10:51:10 +00001657 */
1658 public boolean isWallpaperSupported() {
1659 if (sGlobals.mService == null) {
1660 Log.w(TAG, "WallpaperService not running");
Christopher Tate98d609c2016-05-18 17:31:58 -07001661 throw new RuntimeException(new DeadSystemException());
Benjamin Franzf3ece362015-02-11 10:51:10 +00001662 } else {
1663 try {
1664 return sGlobals.mService.isWallpaperSupported(mContext.getOpPackageName());
1665 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07001666 throw e.rethrowFromSystemServer();
Benjamin Franzf3ece362015-02-11 10:51:10 +00001667 }
1668 }
Benjamin Franzf3ece362015-02-11 10:51:10 +00001669 }
1670
Dianne Hackborn75804932009-10-20 20:15:20 -07001671 /**
Oleksandr Peletskyif2519812016-01-26 20:16:06 +01001672 * Returns whether the calling package is allowed to set the wallpaper for the calling user.
1673 * If this function returns {@code false}, any attempts to change the wallpaper will have
1674 * no effect. Always returns {@code true} for device owner and profile owner.
1675 *
1676 * @see android.os.UserManager#DISALLOW_SET_WALLPAPER
1677 */
Christopher Tate98d609c2016-05-18 17:31:58 -07001678 public boolean isSetWallpaperAllowed() {
Oleksandr Peletskyif2519812016-01-26 20:16:06 +01001679 if (sGlobals.mService == null) {
1680 Log.w(TAG, "WallpaperService not running");
Christopher Tate98d609c2016-05-18 17:31:58 -07001681 throw new RuntimeException(new DeadSystemException());
Oleksandr Peletskyif2519812016-01-26 20:16:06 +01001682 } else {
1683 try {
Christopher Tate98d609c2016-05-18 17:31:58 -07001684 return sGlobals.mService.isSetWallpaperAllowed(mContext.getOpPackageName());
Oleksandr Peletskyif2519812016-01-26 20:16:06 +01001685 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07001686 throw e.rethrowFromSystemServer();
Oleksandr Peletskyif2519812016-01-26 20:16:06 +01001687 }
1688 }
Oleksandr Peletskyif2519812016-01-26 20:16:06 +01001689 }
1690
1691 /**
Dianne Hackborn72c82ab2009-08-11 21:13:54 -07001692 * Clear the offsets previously associated with this window through
1693 * {@link #setWallpaperOffsets(IBinder, float, float)}. This reverts
1694 * the window to its default state, where it does not cause the wallpaper
1695 * to scroll from whatever its last offsets were.
Oleksandr Peletskyif2519812016-01-26 20:16:06 +01001696 *
Dianne Hackborn72c82ab2009-08-11 21:13:54 -07001697 * @param windowToken The window who these offsets should be associated
Scott Main8b2e0002009-09-29 18:17:31 -07001698 * with, as returned by {@link android.view.View#getWindowToken()
Dianne Hackborn72c82ab2009-08-11 21:13:54 -07001699 * View.getWindowToken()}.
1700 */
1701 public void clearWallpaperOffsets(IBinder windowToken) {
1702 try {
Jeff Brownf9e989d2013-04-04 23:04:03 -07001703 WindowManagerGlobal.getWindowSession().setWallpaperPosition(
Marco Nelissenbf6956b2009-11-09 15:21:13 -08001704 windowToken, -1, -1, -1, -1);
Dianne Hackborn72c82ab2009-08-11 21:13:54 -07001705 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07001706 throw e.rethrowFromSystemServer();
Dianne Hackborn72c82ab2009-08-11 21:13:54 -07001707 }
1708 }
Oleksandr Peletskyif2519812016-01-26 20:16:06 +01001709
Dianne Hackborn72c82ab2009-08-11 21:13:54 -07001710 /**
Christopher Tatebe132e62016-02-10 12:59:49 -08001711 * Remove any currently set system wallpaper, reverting to the system's built-in
Dianne Hackborn8cc6a502009-08-05 21:29:42 -07001712 * wallpaper. On success, the intent {@link Intent#ACTION_WALLPAPER_CHANGED}
1713 * is broadcast.
1714 *
Nicolas Falliere9530e3a2012-06-18 17:21:06 -07001715 * <p>This method requires the caller to hold the permission
1716 * {@link android.Manifest.permission#SET_WALLPAPER}.
1717 *
Michael Jurkab668d0b2013-10-04 15:11:05 -07001718 * @throws IOException If an error occurs reverting to the built-in
Dianne Hackborn8cc6a502009-08-05 21:29:42 -07001719 * wallpaper.
1720 */
1721 public void clear() throws IOException {
Christopher Tate5d99d472016-05-06 17:59:27 -07001722 setStream(openDefaultWallpaper(mContext, FLAG_SYSTEM), null, false);
Jeff Sharkey28f08772014-04-16 09:41:58 -07001723 }
1724
1725 /**
Christopher Tate79a24572016-03-02 14:42:44 -08001726 * Remove one or more currently set wallpapers, reverting to the system default
Christopher Tateedf7d042016-03-29 18:24:25 -07001727 * display for each one. If {@link #FLAG_SYSTEM} is set in the {@code which}
Christopher Tate79a24572016-03-02 14:42:44 -08001728 * parameter, the intent {@link Intent#ACTION_WALLPAPER_CHANGED} will be broadcast
1729 * upon success.
1730 *
Christopher Tateedf7d042016-03-29 18:24:25 -07001731 * @param which A bitwise combination of {@link #FLAG_SYSTEM} or
1732 * {@link #FLAG_LOCK}
Christopher Tate79a24572016-03-02 14:42:44 -08001733 * @throws IOException If an error occurs reverting to the built-in wallpaper.
1734 */
Christopher Tate5d99d472016-05-06 17:59:27 -07001735 public void clear(@SetWallpaperFlags int which) throws IOException {
Christopher Tateedf7d042016-03-29 18:24:25 -07001736 if ((which & FLAG_SYSTEM) != 0) {
Christopher Tate79a24572016-03-02 14:42:44 -08001737 clear();
1738 }
Christopher Tateedf7d042016-03-29 18:24:25 -07001739 if ((which & FLAG_LOCK) != 0) {
1740 clearWallpaper(FLAG_LOCK, mContext.getUserId());
Christopher Tate79a24572016-03-02 14:42:44 -08001741 }
1742 }
1743
1744 /**
Jeff Sharkey28f08772014-04-16 09:41:58 -07001745 * Open stream representing the default static image wallpaper.
1746 *
Christopher Tate5d99d472016-05-06 17:59:27 -07001747 * If the device defines no default wallpaper of the requested kind,
1748 * {@code null} is returned.
1749 *
Jeff Sharkey28f08772014-04-16 09:41:58 -07001750 * @hide
1751 */
Christopher Tate5d99d472016-05-06 17:59:27 -07001752 public static InputStream openDefaultWallpaper(Context context, @SetWallpaperFlags int which) {
1753 final String whichProp;
1754 final int defaultResId;
1755 if (which == FLAG_LOCK) {
Christopher Tate36cb2a7a2016-06-09 13:44:48 -07001756 /* Factory-default lock wallpapers are not yet supported
Christopher Tate5d99d472016-05-06 17:59:27 -07001757 whichProp = PROP_LOCK_WALLPAPER;
1758 defaultResId = com.android.internal.R.drawable.default_lock_wallpaper;
Christopher Tate36cb2a7a2016-06-09 13:44:48 -07001759 */
1760 return null;
Christopher Tate5d99d472016-05-06 17:59:27 -07001761 } else {
1762 whichProp = PROP_WALLPAPER;
1763 defaultResId = com.android.internal.R.drawable.default_wallpaper;
1764 }
1765 final String path = SystemProperties.get(whichProp);
Jeff Sharkey28f08772014-04-16 09:41:58 -07001766 if (!TextUtils.isEmpty(path)) {
1767 final File file = new File(path);
1768 if (file.exists()) {
1769 try {
1770 return new FileInputStream(file);
1771 } catch (IOException e) {
1772 // Ignored, fall back to platform default below
1773 }
1774 }
1775 }
Christopher Tate5d99d472016-05-06 17:59:27 -07001776 try {
1777 return context.getResources().openRawResource(defaultResId);
1778 } catch (NotFoundException e) {
1779 // no default defined for this device; this is not a failure
1780 }
1781 return null;
Jeff Sharkey28f08772014-04-16 09:41:58 -07001782 }
1783
1784 /**
1785 * Return {@link ComponentName} of the default live wallpaper, or
1786 * {@code null} if none is defined.
1787 *
1788 * @hide
1789 */
1790 public static ComponentName getDefaultWallpaperComponent(Context context) {
1791 String flat = SystemProperties.get(PROP_WALLPAPER_COMPONENT);
1792 if (!TextUtils.isEmpty(flat)) {
1793 final ComponentName cn = ComponentName.unflattenFromString(flat);
1794 if (cn != null) {
1795 return cn;
1796 }
1797 }
1798
1799 flat = context.getString(com.android.internal.R.string.default_wallpaper_component);
1800 if (!TextUtils.isEmpty(flat)) {
1801 final ComponentName cn = ComponentName.unflattenFromString(flat);
1802 if (cn != null) {
1803 return cn;
1804 }
1805 }
1806
1807 return null;
Dianne Hackborn8cc6a502009-08-05 21:29:42 -07001808 }
Christopher Tate1e1e2e02016-01-25 15:34:36 -08001809
Christopher Tatebe132e62016-02-10 12:59:49 -08001810 /**
1811 * Register a callback for lock wallpaper observation. Only the OS may use this.
1812 *
1813 * @return true on success; false on error.
1814 * @hide
1815 */
1816 public boolean setLockWallpaperCallback(IWallpaperManagerCallback callback) {
1817 if (sGlobals.mService == null) {
1818 Log.w(TAG, "WallpaperService not running");
Christopher Tate98d609c2016-05-18 17:31:58 -07001819 throw new RuntimeException(new DeadSystemException());
Christopher Tatebe132e62016-02-10 12:59:49 -08001820 }
1821
1822 try {
1823 return sGlobals.mService.setLockWallpaperCallback(callback);
1824 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07001825 throw e.rethrowFromSystemServer();
Christopher Tatebe132e62016-02-10 12:59:49 -08001826 }
Christopher Tatebe132e62016-02-10 12:59:49 -08001827 }
1828
Christopher Tated7faf532016-02-25 12:43:38 -08001829 /**
1830 * Is the current system wallpaper eligible for backup?
1831 *
1832 * Only the OS itself may use this method.
1833 * @hide
1834 */
Christopher Tate61722662016-08-10 16:13:14 -07001835 public boolean isWallpaperBackupEligible(int which) {
Christopher Tated7faf532016-02-25 12:43:38 -08001836 if (sGlobals.mService == null) {
1837 Log.w(TAG, "WallpaperService not running");
Christopher Tate98d609c2016-05-18 17:31:58 -07001838 throw new RuntimeException(new DeadSystemException());
Christopher Tated7faf532016-02-25 12:43:38 -08001839 }
1840 try {
Christopher Tate61722662016-08-10 16:13:14 -07001841 return sGlobals.mService.isWallpaperBackupEligible(which, mContext.getUserId());
Christopher Tated7faf532016-02-25 12:43:38 -08001842 } catch (RemoteException e) {
1843 Log.e(TAG, "Exception querying wallpaper backup eligibility: " + e.getMessage());
1844 }
1845 return false;
1846 }
1847
Christopher Tate1e1e2e02016-01-25 15:34:36 -08001848 // Private completion callback for setWallpaper() synchronization
1849 private class WallpaperSetCompletion extends IWallpaperManagerCallback.Stub {
1850 final CountDownLatch mLatch;
1851
1852 public WallpaperSetCompletion() {
1853 mLatch = new CountDownLatch(1);
1854 }
1855
1856 public void waitForCompletion() {
1857 try {
1858 mLatch.await(30, TimeUnit.SECONDS);
1859 } catch (InterruptedException e) {
1860 // This might be legit: the crop may take a very long time. Don't sweat
1861 // it in that case; we are okay with display lagging behind in order to
1862 // keep the caller from locking up indeterminately.
1863 }
1864 }
1865
1866 @Override
1867 public void onWallpaperChanged() throws RemoteException {
1868 mLatch.countDown();
1869 }
Lucas Dupinea1fb1e2017-04-05 17:39:44 -07001870
1871 @Override
1872 public void onWallpaperColorsChanged(WallpaperColors colors, int which)
1873 throws RemoteException {
1874 sGlobals.onWallpaperColorsChanged(colors, which);
1875 }
Christopher Tate1e1e2e02016-01-25 15:34:36 -08001876 }
Lucas Dupinc40608c2017-04-14 18:33:08 -07001877
1878 /**
1879 * Interface definition for a callback to be invoked when colors change on a wallpaper.
1880 */
1881 public interface OnColorsChangedListener {
1882 /**
1883 * Called when colors change.
1884 * A {@link android.app.WallpaperColors} object containing a simplified
1885 * color histogram will be given.
1886 *
1887 * @param colors Wallpaper color info
1888 * @param which A combination of {@link #FLAG_LOCK} and {@link #FLAG_SYSTEM}
1889 */
1890 void onColorsChanged(WallpaperColors colors, int which);
1891 }
Dianne Hackborn8cc6a502009-08-05 21:29:42 -07001892}