blob: a929fe0f688e11e57e08ce64690a482f6f6ac88a [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;
Christopher Tatedbf4a9c2017-06-27 12:37:23 -070025import android.annotation.SdkConstant.SdkConstantType;
Dianne Hackborn067e5f62014-09-07 23:14:30 -070026import android.annotation.SystemApi;
Jeff Sharkeyd86b8fe2017-06-02 17:36:26 -060027import android.annotation.SystemService;
Mathew Inwood61e8ae62018-08-14 14:17:44 +010028import android.annotation.UnsupportedAppUsage;
Jeff Sharkey28f08772014-04-16 09:41:58 -070029import android.content.ComponentName;
Adam Lesinskibba72d12013-10-11 18:10:56 -070030import android.content.ContentResolver;
Dianne Hackborn8cc6a502009-08-05 21:29:42 -070031import android.content.Context;
32import android.content.Intent;
Michael Jurkae8d1bf72013-09-09 15:58:54 +020033import android.content.pm.PackageManager;
34import android.content.pm.ResolveInfo;
Dianne Hackborn8cc6a502009-08-05 21:29:42 -070035import android.content.res.Resources;
Christopher Tate5d99d472016-05-06 17:59:27 -070036import android.content.res.Resources.NotFoundException;
Dianne Hackborn8cc6a502009-08-05 21:29:42 -070037import android.graphics.Bitmap;
38import android.graphics.BitmapFactory;
Michael Jurkab668d0b2013-10-04 15:11:05 -070039import android.graphics.BitmapRegionDecoder;
Dianne Hackborn284ac932009-08-28 10:34:25 -070040import android.graphics.Canvas;
Dianne Hackborn19382ac2009-09-11 21:13:37 -070041import android.graphics.ColorFilter;
Michael Jurkab668d0b2013-10-04 15:11:05 -070042import android.graphics.Matrix;
Dianne Hackborn284ac932009-08-28 10:34:25 -070043import android.graphics.Paint;
Dianne Hackborn19382ac2009-09-11 21:13:37 -070044import android.graphics.PixelFormat;
Jeff Brown24572372011-06-09 19:05:15 -070045import android.graphics.PorterDuff;
46import android.graphics.PorterDuffXfermode;
Dianne Hackborn284ac932009-08-28 10:34:25 -070047import android.graphics.Rect;
Michael Jurkab668d0b2013-10-04 15:11:05 -070048import android.graphics.RectF;
Dianne Hackborn8cc6a502009-08-05 21:29:42 -070049import android.graphics.drawable.BitmapDrawable;
50import android.graphics.drawable.Drawable;
Michael Jurkae8d1bf72013-09-09 15:58:54 +020051import android.net.Uri;
Christopher Tate93252de2017-06-15 14:48:41 -070052import android.os.Build;
Dianne Hackborn284ac932009-08-28 10:34:25 -070053import android.os.Bundle;
Christopher Tate98d609c2016-05-18 17:31:58 -070054import android.os.DeadSystemException;
Jeff Sharkey45c97df2018-02-01 16:01:52 -070055import android.os.FileUtils;
Dianne Hackborn8cc6a502009-08-05 21:29:42 -070056import android.os.Handler;
57import android.os.IBinder;
Dianne Hackborn840c3a22009-09-02 17:35:17 -070058import android.os.Looper;
Dianne Hackborn8cc6a502009-08-05 21:29:42 -070059import android.os.ParcelFileDescriptor;
60import android.os.RemoteException;
Jeff Sharkey28f08772014-04-16 09:41:58 -070061import android.os.SystemProperties;
62import android.text.TextUtils;
Dianne Hackborn7341d7a2009-08-14 11:37:52 -070063import android.util.Log;
Lucas Dupinea1fb1e2017-04-05 17:39:44 -070064import android.util.Pair;
Jeff Brown98365d72012-08-19 20:30:52 -070065import android.view.WindowManagerGlobal;
Dianne Hackborn8cc6a502009-08-05 21:29:42 -070066
Christopher Tateffa6a882015-12-11 15:08:46 -080067import libcore.io.IoUtils;
68
Michael Jurkab668d0b2013-10-04 15:11:05 -070069import java.io.BufferedInputStream;
Jeff Sharkey28f08772014-04-16 09:41:58 -070070import java.io.File;
71import java.io.FileInputStream;
Dianne Hackborn8cc6a502009-08-05 21:29:42 -070072import java.io.FileOutputStream;
73import java.io.IOException;
74import java.io.InputStream;
Christopher Tatead3c2592016-01-20 18:13:17 -080075import java.lang.annotation.Retention;
76import java.lang.annotation.RetentionPolicy;
Lucas Dupinea1fb1e2017-04-05 17:39:44 -070077import java.util.ArrayList;
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 */
Jeff Sharkeyce8db992017-12-13 20:05:05 -0700178 @IntDef(flag = true, prefix = { "FLAG_" }, 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
Svet Ganov8f90bcc2017-12-22 23:29:24 -0800288 Globals(IWallpaperManager service, Looper looper) {
289 mService = service;
Lucas Dupinea1fb1e2017-04-05 17:39:44 -0700290 mMainLooperHandler = new Handler(looper);
Christopher Tatebe132e62016-02-10 12:59:49 -0800291 forgetLoadedWallpaper();
Dianne Hackborn8cc6a502009-08-05 21:29:42 -0700292 }
Oleksandr Peletskyif2519812016-01-26 20:16:06 +0100293
Dianne Hackborn8cc6a502009-08-05 21:29:42 -0700294 public void onWallpaperChanged() {
295 /* The wallpaper has changed but we shouldn't eagerly load the
296 * wallpaper as that would be inefficient. Reset the cached wallpaper
297 * to null so if the user requests the wallpaper again then we'll
298 * fetch it.
299 */
Christopher Tatebe132e62016-02-10 12:59:49 -0800300 forgetLoadedWallpaper();
Dianne Hackborn8cc6a502009-08-05 21:29:42 -0700301 }
Adam Cohen041a0ba2011-11-09 20:10:27 -0800302
Lucas Dupinea1fb1e2017-04-05 17:39:44 -0700303 /**
304 * Start listening to wallpaper color events.
305 * Will be called whenever someone changes their wallpaper or if a live wallpaper
306 * changes its colors.
307 * @param callback Listener
308 * @param handler Thread to call it from. Main thread if null.
Lucas Dupin50ba9912017-07-14 11:55:05 -0700309 * @param userId Owner of the wallpaper or UserHandle.USER_ALL
wilsonshih36597d42018-12-05 18:56:39 +0800310 * @param displayId Caller comes from which display
Lucas Dupinea1fb1e2017-04-05 17:39:44 -0700311 */
312 public void addOnColorsChangedListener(@NonNull OnColorsChangedListener callback,
wilsonshih36597d42018-12-05 18:56:39 +0800313 @Nullable Handler handler, int userId, int displayId) {
Lucas Dupinea1fb1e2017-04-05 17:39:44 -0700314 synchronized (this) {
315 if (!mColorCallbackRegistered) {
316 try {
wilsonshih36597d42018-12-05 18:56:39 +0800317 mService.registerWallpaperColorsCallback(this, userId, displayId);
Lucas Dupinea1fb1e2017-04-05 17:39:44 -0700318 mColorCallbackRegistered = true;
319 } catch (RemoteException e) {
320 // Failed, service is gone
321 Log.w(TAG, "Can't register for color updates", e);
322 }
323 }
324 mColorListeners.add(new Pair<>(callback, handler));
325 }
326 }
327
328 /**
329 * Stop listening to wallpaper color events.
330 *
331 * @param callback listener
Lucas Dupin50ba9912017-07-14 11:55:05 -0700332 * @param userId Owner of the wallpaper or UserHandle.USER_ALL
wilsonshih36597d42018-12-05 18:56:39 +0800333 * @param displayId Which display is interested
Lucas Dupinea1fb1e2017-04-05 17:39:44 -0700334 */
Lucas Dupin50ba9912017-07-14 11:55:05 -0700335 public void removeOnColorsChangedListener(@NonNull OnColorsChangedListener callback,
wilsonshih36597d42018-12-05 18:56:39 +0800336 int userId, int displayId) {
Lucas Dupinea1fb1e2017-04-05 17:39:44 -0700337 synchronized (this) {
338 mColorListeners.removeIf(pair -> pair.first == callback);
339
340 if (mColorListeners.size() == 0 && mColorCallbackRegistered) {
341 mColorCallbackRegistered = false;
342 try {
wilsonshih36597d42018-12-05 18:56:39 +0800343 mService.unregisterWallpaperColorsCallback(this, userId, displayId);
Lucas Dupinea1fb1e2017-04-05 17:39:44 -0700344 } catch (RemoteException e) {
345 // Failed, service is gone
346 Log.w(TAG, "Can't unregister color updates", e);
347 }
348 }
349 }
350 }
351
352 @Override
Lucas Dupin50ba9912017-07-14 11:55:05 -0700353 public void onWallpaperColorsChanged(WallpaperColors colors, int which, int userId) {
Lucas Dupinea1fb1e2017-04-05 17:39:44 -0700354 synchronized (this) {
355 for (Pair<OnColorsChangedListener, Handler> listener : mColorListeners) {
356 Handler handler = listener.second;
357 if (listener.second == null) {
358 handler = mMainLooperHandler;
359 }
360 handler.post(() -> {
361 // Dealing with race conditions between posting a callback and
362 // removeOnColorsChangedListener being called.
363 boolean stillExists;
364 synchronized (sGlobals) {
365 stillExists = mColorListeners.contains(listener);
366 }
367 if (stillExists) {
Lucas Dupin50ba9912017-07-14 11:55:05 -0700368 listener.first.onColorsChanged(colors, which, userId);
Lucas Dupinea1fb1e2017-04-05 17:39:44 -0700369 }
370 });
371 }
372 }
373 }
374
wilsonshih36597d42018-12-05 18:56:39 +0800375 WallpaperColors getWallpaperColors(int which, int userId, int displayId) {
Christopher Tatedbf4a9c2017-06-27 12:37:23 -0700376 if (which != FLAG_LOCK && which != FLAG_SYSTEM) {
377 throw new IllegalArgumentException(
378 "Must request colors for exactly one kind of wallpaper");
Lucas Dupinea1fb1e2017-04-05 17:39:44 -0700379 }
Christopher Tatedbf4a9c2017-06-27 12:37:23 -0700380
381 try {
wilsonshih36597d42018-12-05 18:56:39 +0800382 return mService.getWallpaperColors(which, userId, displayId);
Christopher Tatedbf4a9c2017-06-27 12:37:23 -0700383 } catch (RemoteException e) {
384 // Can't get colors, connection lost.
385 }
386 return null;
Lucas Dupinea1fb1e2017-04-05 17:39:44 -0700387 }
388
Christopher Tate5d99d472016-05-06 17:59:27 -0700389 public Bitmap peekWallpaperBitmap(Context context, boolean returnDefault,
390 @SetWallpaperFlags int which) {
Lucas Dupin049978e2017-10-02 11:38:13 -0700391 return peekWallpaperBitmap(context, returnDefault, which, context.getUserId(),
392 false /* hardware */);
Yorke Leedcd93cc2016-01-08 14:12:55 -0800393 }
394
Christopher Tate5d99d472016-05-06 17:59:27 -0700395 public Bitmap peekWallpaperBitmap(Context context, boolean returnDefault,
Lucas Dupin049978e2017-10-02 11:38:13 -0700396 @SetWallpaperFlags int which, int userId, boolean hardware) {
Fyodor Kupolovb6378cf2016-07-12 10:32:08 -0700397 if (mService != null) {
398 try {
399 if (!mService.isWallpaperSupported(context.getOpPackageName())) {
400 return null;
Benjamin Franzf3ece362015-02-11 10:51:10 +0000401 }
Fyodor Kupolovb6378cf2016-07-12 10:32:08 -0700402 } catch (RemoteException e) {
403 throw e.rethrowFromSystemServer();
Benjamin Franzf3ece362015-02-11 10:51:10 +0000404 }
Fyodor Kupolovb6378cf2016-07-12 10:32:08 -0700405 }
406 synchronized (this) {
Lucas Dupin61df45c2018-04-07 11:08:15 -0700407 if (mCachedWallpaper != null && mCachedWallpaperUserId == userId
408 && !mCachedWallpaper.isRecycled()) {
Vadim Tryshev93a914a2016-03-07 12:48:51 -0800409 return mCachedWallpaper;
Dianne Hackborn8cc6a502009-08-05 21:29:42 -0700410 }
Vadim Tryshev93a914a2016-03-07 12:48:51 -0800411 mCachedWallpaper = null;
412 mCachedWallpaperUserId = 0;
Dianne Hackbornc9dbbe22009-11-11 22:50:37 -0800413 try {
Lucas Dupin049978e2017-10-02 11:38:13 -0700414 mCachedWallpaper = getCurrentWallpaperLocked(context, userId, hardware);
Vadim Tryshev93a914a2016-03-07 12:48:51 -0800415 mCachedWallpaperUserId = userId;
Dianne Hackbornc9dbbe22009-11-11 22:50:37 -0800416 } catch (OutOfMemoryError e) {
Christopher Tate93252de2017-06-15 14:48:41 -0700417 Log.w(TAG, "Out of memory loading the current wallpaper: " + e);
418 } catch (SecurityException e) {
Jeff Sharkeyaa1a9112018-04-10 15:18:12 -0600419 if (context.getApplicationInfo().targetSdkVersion < Build.VERSION_CODES.O_MR1) {
Christopher Tate93252de2017-06-15 14:48:41 -0700420 Log.w(TAG, "No permission to access wallpaper, suppressing"
421 + " exception to avoid crashing legacy app.");
422 } else {
423 // Post-O apps really most sincerely need the permission.
424 throw e;
425 }
Dianne Hackbornc9dbbe22009-11-11 22:50:37 -0800426 }
Vadim Tryshev93a914a2016-03-07 12:48:51 -0800427 if (mCachedWallpaper != null) {
428 return mCachedWallpaper;
Dianne Hackborn19382ac2009-09-11 21:13:37 -0700429 }
Dianne Hackborn8cc6a502009-08-05 21:29:42 -0700430 }
Fyodor Kupolovb6378cf2016-07-12 10:32:08 -0700431 if (returnDefault) {
432 Bitmap defaultWallpaper = mDefaultWallpaper;
433 if (defaultWallpaper == null) {
434 defaultWallpaper = getDefaultWallpaper(context, which);
435 synchronized (this) {
436 mDefaultWallpaper = defaultWallpaper;
437 }
438 }
439 return defaultWallpaper;
440 }
441 return null;
Dianne Hackborn8cc6a502009-08-05 21:29:42 -0700442 }
Dianne Hackbornba398392011-08-01 16:11:57 -0700443
Fyodor Kupolovb6378cf2016-07-12 10:32:08 -0700444 void forgetLoadedWallpaper() {
Dianne Hackbornba398392011-08-01 16:11:57 -0700445 synchronized (this) {
Vadim Tryshev93a914a2016-03-07 12:48:51 -0800446 mCachedWallpaper = null;
447 mCachedWallpaperUserId = 0;
Dianne Hackbornba398392011-08-01 16:11:57 -0700448 mDefaultWallpaper = null;
449 }
450 }
451
Lucas Dupin049978e2017-10-02 11:38:13 -0700452 private Bitmap getCurrentWallpaperLocked(Context context, int userId, boolean hardware) {
Adam Lesinski2c8d67c2014-04-23 13:46:21 -0700453 if (mService == null) {
454 Log.w(TAG, "WallpaperService not running");
455 return null;
456 }
457
Dianne Hackborn8cc6a502009-08-05 21:29:42 -0700458 try {
Dianne Hackborn284ac932009-08-28 10:34:25 -0700459 Bundle params = new Bundle();
Christopher Tate93252de2017-06-15 14:48:41 -0700460 ParcelFileDescriptor fd = mService.getWallpaper(context.getOpPackageName(),
461 this, FLAG_SYSTEM, params, userId);
Dianne Hackborn8cc6a502009-08-05 21:29:42 -0700462 if (fd != null) {
Jeff Brown24572372011-06-09 19:05:15 -0700463 try {
464 BitmapFactory.Options options = new BitmapFactory.Options();
Lucas Dupin049978e2017-10-02 11:38:13 -0700465 if (hardware) {
466 options.inPreferredConfig = Bitmap.Config.HARDWARE;
467 }
Michael Jurka824a4b52013-12-18 17:10:16 +0100468 return BitmapFactory.decodeFileDescriptor(
Jeff Brown24572372011-06-09 19:05:15 -0700469 fd.getFileDescriptor(), null, options);
Jeff Brown24572372011-06-09 19:05:15 -0700470 } catch (OutOfMemoryError e) {
471 Log.w(TAG, "Can't decode file", e);
472 } finally {
Christopher Tateffa6a882015-12-11 15:08:46 -0800473 IoUtils.closeQuietly(fd);
Dianne Hackborn8cc6a502009-08-05 21:29:42 -0700474 }
Dianne Hackborn19382ac2009-09-11 21:13:37 -0700475 }
476 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -0700477 throw e.rethrowFromSystemServer();
Dianne Hackborn19382ac2009-09-11 21:13:37 -0700478 }
479 return null;
480 }
Christopher Tateffa6a882015-12-11 15:08:46 -0800481
Fyodor Kupolovb6378cf2016-07-12 10:32:08 -0700482 private Bitmap getDefaultWallpaper(Context context, @SetWallpaperFlags int which) {
Christopher Tate5d99d472016-05-06 17:59:27 -0700483 InputStream is = openDefaultWallpaper(context, which);
Adam Lesinski2c8d67c2014-04-23 13:46:21 -0700484 if (is != null) {
485 try {
486 BitmapFactory.Options options = new BitmapFactory.Options();
487 return BitmapFactory.decodeStream(is, null, options);
488 } catch (OutOfMemoryError e) {
489 Log.w(TAG, "Can't decode stream", e);
490 } finally {
Christopher Tateffa6a882015-12-11 15:08:46 -0800491 IoUtils.closeQuietly(is);
Dianne Hackborn8cc6a502009-08-05 21:29:42 -0700492 }
Dianne Hackborn8cc6a502009-08-05 21:29:42 -0700493 }
494 return null;
495 }
496 }
Oleksandr Peletskyif2519812016-01-26 20:16:06 +0100497
Romain Guy407ec782011-08-24 17:06:58 -0700498 private static final Object sSync = new Object[0];
Mathew Inwood61e8ae62018-08-14 14:17:44 +0100499 @UnsupportedAppUsage
Dianne Hackborn8cc6a502009-08-05 21:29:42 -0700500 private static Globals sGlobals;
501
Svet Ganov8f90bcc2017-12-22 23:29:24 -0800502 static void initGlobals(IWallpaperManager service, Looper looper) {
Romain Guy407ec782011-08-24 17:06:58 -0700503 synchronized (sSync) {
Dianne Hackborn8cc6a502009-08-05 21:29:42 -0700504 if (sGlobals == null) {
Svet Ganov8f90bcc2017-12-22 23:29:24 -0800505 sGlobals = new Globals(service, looper);
Dianne Hackborn8cc6a502009-08-05 21:29:42 -0700506 }
Dianne Hackborn8cc6a502009-08-05 21:29:42 -0700507 }
508 }
Oleksandr Peletskyif2519812016-01-26 20:16:06 +0100509
Svet Ganov8f90bcc2017-12-22 23:29:24 -0800510 /*package*/ WallpaperManager(IWallpaperManager service, Context context, Handler handler) {
Dianne Hackborn8cc6a502009-08-05 21:29:42 -0700511 mContext = context;
Svet Ganov8f90bcc2017-12-22 23:29:24 -0800512 initGlobals(service, context.getMainLooper());
Dianne Hackborn8cc6a502009-08-05 21:29:42 -0700513 }
514
515 /**
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700516 * Retrieve a WallpaperManager associated with the given Context.
517 */
518 public static WallpaperManager getInstance(Context context) {
519 return (WallpaperManager)context.getSystemService(
520 Context.WALLPAPER_SERVICE);
521 }
Oleksandr Peletskyif2519812016-01-26 20:16:06 +0100522
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700523 /** @hide */
Mathew Inwood61e8ae62018-08-14 14:17:44 +0100524 @UnsupportedAppUsage
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700525 public IWallpaperManager getIWallpaperManager() {
Dianne Hackborn840c3a22009-09-02 17:35:17 -0700526 return sGlobals.mService;
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700527 }
Oleksandr Peletskyif2519812016-01-26 20:16:06 +0100528
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700529 /**
Scott Main8b2e0002009-09-29 18:17:31 -0700530 * Retrieve the current system wallpaper; if
Michael Jurkab668d0b2013-10-04 15:11:05 -0700531 * no wallpaper is set, the system built-in static wallpaper is returned.
Scott Main8b2e0002009-09-29 18:17:31 -0700532 * This is returned as an
533 * abstract Drawable that you can install in a View to display whatever
Oleksandr Peletskyif2519812016-01-26 20:16:06 +0100534 * wallpaper the user has currently set.
Christopher Tate41dc83bd2016-07-15 15:59:23 -0700535 * <p>
536 * This method can return null if there is no system wallpaper available, if
537 * wallpapers are not supported in the current user, or if the calling app is not
538 * permitted to access the system wallpaper.
Dianne Hackborn8cc6a502009-08-05 21:29:42 -0700539 *
Christopher Tate41dc83bd2016-07-15 15:59:23 -0700540 * @return Returns a Drawable object that will draw the system wallpaper,
541 * or {@code null} if no system wallpaper exists or if the calling application
542 * is not able to access the wallpaper.
Dianne Hackborn8cc6a502009-08-05 21:29:42 -0700543 */
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700544 public Drawable getDrawable() {
Christopher Tate5d99d472016-05-06 17:59:27 -0700545 Bitmap bm = sGlobals.peekWallpaperBitmap(mContext, true, FLAG_SYSTEM);
Dianne Hackborn19382ac2009-09-11 21:13:37 -0700546 if (bm != null) {
547 Drawable dr = new BitmapDrawable(mContext.getResources(), bm);
548 dr.setDither(false);
549 return dr;
550 }
551 return null;
Dianne Hackborn8cc6a502009-08-05 21:29:42 -0700552 }
553
554 /**
Christopher Tate5d99d472016-05-06 17:59:27 -0700555 * Obtain a drawable for the built-in static system wallpaper.
Michael Jurkab668d0b2013-10-04 15:11:05 -0700556 */
557 public Drawable getBuiltInDrawable() {
Christopher Tate5d99d472016-05-06 17:59:27 -0700558 return getBuiltInDrawable(0, 0, false, 0, 0, FLAG_SYSTEM);
559 }
560
561 /**
562 * Obtain a drawable for the specified built-in static system wallpaper.
563 *
564 * @param which The {@code FLAG_*} identifier of a valid wallpaper type. Throws
565 * IllegalArgumentException if an invalid wallpaper is requested.
566 * @return A Drawable presenting the specified wallpaper image, or {@code null}
567 * if no built-in default image for that wallpaper type exists.
568 */
569 public Drawable getBuiltInDrawable(@SetWallpaperFlags int which) {
570 return getBuiltInDrawable(0, 0, false, 0, 0, which);
Michael Jurkab668d0b2013-10-04 15:11:05 -0700571 }
572
573 /**
574 * Returns a drawable for the system built-in static wallpaper. Based on the parameters, the
575 * drawable can be cropped and scaled
576 *
577 * @param outWidth The width of the returned drawable
578 * @param outWidth The height of the returned drawable
579 * @param scaleToFit If true, scale the wallpaper down rather than just cropping it
580 * @param horizontalAlignment A float value between 0 and 1 specifying where to crop the image;
581 * 0 for left-aligned, 0.5 for horizontal center-aligned, and 1 for right-aligned
582 * @param verticalAlignment A float value between 0 and 1 specifying where to crop the image;
583 * 0 for top-aligned, 0.5 for vertical center-aligned, and 1 for bottom-aligned
Christopher Tate5d99d472016-05-06 17:59:27 -0700584 * @return A Drawable presenting the built-in default system wallpaper image,
585 * or {@code null} if no such default image is defined on this device.
Michael Jurkab668d0b2013-10-04 15:11:05 -0700586 */
587 public Drawable getBuiltInDrawable(int outWidth, int outHeight,
588 boolean scaleToFit, float horizontalAlignment, float verticalAlignment) {
Christopher Tate5d99d472016-05-06 17:59:27 -0700589 return getBuiltInDrawable(outWidth, outHeight, scaleToFit,
590 horizontalAlignment, verticalAlignment, FLAG_SYSTEM);
591 }
592
593 /**
594 * Returns a drawable for the built-in static wallpaper of the specified type. Based on the
595 * parameters, the drawable can be cropped and scaled.
596 *
597 * @param outWidth The width of the returned drawable
598 * @param outWidth The height of the returned drawable
599 * @param scaleToFit If true, scale the wallpaper down rather than just cropping it
600 * @param horizontalAlignment A float value between 0 and 1 specifying where to crop the image;
601 * 0 for left-aligned, 0.5 for horizontal center-aligned, and 1 for right-aligned
602 * @param verticalAlignment A float value between 0 and 1 specifying where to crop the image;
603 * 0 for top-aligned, 0.5 for vertical center-aligned, and 1 for bottom-aligned
604 * @param which The {@code FLAG_*} identifier of a valid wallpaper type. Throws
605 * IllegalArgumentException if an invalid wallpaper is requested.
606 * @return A Drawable presenting the built-in default wallpaper image of the given type,
607 * or {@code null} if no default image of that type is defined on this device.
608 */
609 public Drawable getBuiltInDrawable(int outWidth, int outHeight, boolean scaleToFit,
610 float horizontalAlignment, float verticalAlignment, @SetWallpaperFlags int which) {
Michael Jurkab668d0b2013-10-04 15:11:05 -0700611 if (sGlobals.mService == null) {
612 Log.w(TAG, "WallpaperService not running");
Christopher Tate98d609c2016-05-18 17:31:58 -0700613 throw new RuntimeException(new DeadSystemException());
Michael Jurkab668d0b2013-10-04 15:11:05 -0700614 }
Christopher Tate5d99d472016-05-06 17:59:27 -0700615
616 if (which != FLAG_SYSTEM && which != FLAG_LOCK) {
617 throw new IllegalArgumentException("Must request exactly one kind of wallpaper");
618 }
619
Michael Jurkab668d0b2013-10-04 15:11:05 -0700620 Resources resources = mContext.getResources();
621 horizontalAlignment = Math.max(0, Math.min(1, horizontalAlignment));
622 verticalAlignment = Math.max(0, Math.min(1, verticalAlignment));
623
Christopher Tate5d99d472016-05-06 17:59:27 -0700624 InputStream wpStream = openDefaultWallpaper(mContext, which);
625 if (wpStream == null) {
626 if (DEBUG) {
627 Log.w(TAG, "default wallpaper stream " + which + " is null");
628 }
Michael Jurkab668d0b2013-10-04 15:11:05 -0700629 return null;
630 } else {
Christopher Tate5d99d472016-05-06 17:59:27 -0700631 InputStream is = new BufferedInputStream(wpStream);
Michael Jurkab668d0b2013-10-04 15:11:05 -0700632 if (outWidth <= 0 || outHeight <= 0) {
633 Bitmap fullSize = BitmapFactory.decodeStream(is, null, null);
634 return new BitmapDrawable(resources, fullSize);
635 } else {
636 int inWidth;
637 int inHeight;
Christopher Tate5d99d472016-05-06 17:59:27 -0700638 // Just measure this time through...
Michael Jurkab668d0b2013-10-04 15:11:05 -0700639 {
640 BitmapFactory.Options options = new BitmapFactory.Options();
641 options.inJustDecodeBounds = true;
642 BitmapFactory.decodeStream(is, null, options);
643 if (options.outWidth != 0 && options.outHeight != 0) {
644 inWidth = options.outWidth;
645 inHeight = options.outHeight;
646 } else {
647 Log.e(TAG, "default wallpaper dimensions are 0");
648 return null;
649 }
650 }
651
Christopher Tate5d99d472016-05-06 17:59:27 -0700652 // Reopen the stream to do the full decode. We know at this point
653 // that openDefaultWallpaper() will return non-null.
654 is = new BufferedInputStream(openDefaultWallpaper(mContext, which));
Michael Jurkab668d0b2013-10-04 15:11:05 -0700655
656 RectF cropRectF;
657
658 outWidth = Math.min(inWidth, outWidth);
659 outHeight = Math.min(inHeight, outHeight);
660 if (scaleToFit) {
661 cropRectF = getMaxCropRect(inWidth, inHeight, outWidth, outHeight,
662 horizontalAlignment, verticalAlignment);
663 } else {
664 float left = (inWidth - outWidth) * horizontalAlignment;
665 float right = left + outWidth;
666 float top = (inHeight - outHeight) * verticalAlignment;
667 float bottom = top + outHeight;
Michael Jurka05391532013-10-14 20:44:42 -0700668 cropRectF = new RectF(left, top, right, bottom);
Michael Jurkab668d0b2013-10-04 15:11:05 -0700669 }
670 Rect roundedTrueCrop = new Rect();
671 cropRectF.roundOut(roundedTrueCrop);
672
673 if (roundedTrueCrop.width() <= 0 || roundedTrueCrop.height() <= 0) {
674 Log.w(TAG, "crop has bad values for full size image");
675 return null;
676 }
677
678 // See how much we're reducing the size of the image
679 int scaleDownSampleSize = Math.min(roundedTrueCrop.width() / outWidth,
680 roundedTrueCrop.height() / outHeight);
681
682 // Attempt to open a region decoder
683 BitmapRegionDecoder decoder = null;
684 try {
685 decoder = BitmapRegionDecoder.newInstance(is, true);
686 } catch (IOException e) {
687 Log.w(TAG, "cannot open region decoder for default wallpaper");
688 }
689
690 Bitmap crop = null;
691 if (decoder != null) {
692 // Do region decoding to get crop bitmap
693 BitmapFactory.Options options = new BitmapFactory.Options();
694 if (scaleDownSampleSize > 1) {
695 options.inSampleSize = scaleDownSampleSize;
696 }
697 crop = decoder.decodeRegion(roundedTrueCrop, options);
698 decoder.recycle();
699 }
700
701 if (crop == null) {
Christopher Tate5d99d472016-05-06 17:59:27 -0700702 // BitmapRegionDecoder has failed, try to crop in-memory. We know at
703 // this point that openDefaultWallpaper() will return non-null.
704 is = new BufferedInputStream(openDefaultWallpaper(mContext, which));
Michael Jurkab668d0b2013-10-04 15:11:05 -0700705 Bitmap fullSize = null;
Christopher Tate5d99d472016-05-06 17:59:27 -0700706 BitmapFactory.Options options = new BitmapFactory.Options();
707 if (scaleDownSampleSize > 1) {
708 options.inSampleSize = scaleDownSampleSize;
Michael Jurkab668d0b2013-10-04 15:11:05 -0700709 }
Christopher Tate5d99d472016-05-06 17:59:27 -0700710 fullSize = BitmapFactory.decodeStream(is, null, options);
Michael Jurkab668d0b2013-10-04 15:11:05 -0700711 if (fullSize != null) {
712 crop = Bitmap.createBitmap(fullSize, roundedTrueCrop.left,
713 roundedTrueCrop.top, roundedTrueCrop.width(),
714 roundedTrueCrop.height());
715 }
716 }
717
718 if (crop == null) {
719 Log.w(TAG, "cannot decode default wallpaper");
720 return null;
721 }
722
723 // Scale down if necessary
724 if (outWidth > 0 && outHeight > 0 &&
725 (crop.getWidth() != outWidth || crop.getHeight() != outHeight)) {
726 Matrix m = new Matrix();
727 RectF cropRect = new RectF(0, 0, crop.getWidth(), crop.getHeight());
728 RectF returnRect = new RectF(0, 0, outWidth, outHeight);
729 m.setRectToRect(cropRect, returnRect, Matrix.ScaleToFit.FILL);
730 Bitmap tmp = Bitmap.createBitmap((int) returnRect.width(),
731 (int) returnRect.height(), Bitmap.Config.ARGB_8888);
732 if (tmp != null) {
733 Canvas c = new Canvas(tmp);
734 Paint p = new Paint();
735 p.setFilterBitmap(true);
736 c.drawBitmap(crop, m, p);
737 crop = tmp;
738 }
739 }
740
741 return new BitmapDrawable(resources, crop);
742 }
743 }
744 }
745
746 private static RectF getMaxCropRect(int inWidth, int inHeight, int outWidth, int outHeight,
747 float horizontalAlignment, float verticalAlignment) {
748 RectF cropRect = new RectF();
749 // Get a crop rect that will fit this
750 if (inWidth / (float) inHeight > outWidth / (float) outHeight) {
751 cropRect.top = 0;
752 cropRect.bottom = inHeight;
753 float cropWidth = outWidth * (inHeight / (float) outHeight);
754 cropRect.left = (inWidth - cropWidth) * horizontalAlignment;
755 cropRect.right = cropRect.left + cropWidth;
756 } else {
757 cropRect.left = 0;
758 cropRect.right = inWidth;
759 float cropHeight = outHeight * (inWidth / (float) outWidth);
760 cropRect.top = (inHeight - cropHeight) * verticalAlignment;
761 cropRect.bottom = cropRect.top + cropHeight;
762 }
763 return cropRect;
764 }
765
766 /**
Scott Main8b2e0002009-09-29 18:17:31 -0700767 * Retrieve the current system wallpaper; if there is no wallpaper set,
768 * a null pointer is returned. This is returned as an
Dianne Hackborn8cc6a502009-08-05 21:29:42 -0700769 * abstract Drawable that you can install in a View to display whatever
Oleksandr Peletskyif2519812016-01-26 20:16:06 +0100770 * wallpaper the user has currently set.
Dianne Hackborn8cc6a502009-08-05 21:29:42 -0700771 *
772 * @return Returns a Drawable object that will draw the wallpaper or a
773 * null pointer if these is none.
774 */
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700775 public Drawable peekDrawable() {
Christopher Tate5d99d472016-05-06 17:59:27 -0700776 Bitmap bm = sGlobals.peekWallpaperBitmap(mContext, false, FLAG_SYSTEM);
Dianne Hackborn19382ac2009-09-11 21:13:37 -0700777 if (bm != null) {
778 Drawable dr = new BitmapDrawable(mContext.getResources(), bm);
779 dr.setDither(false);
780 return dr;
781 }
782 return null;
783 }
784
785 /**
Scott Main8b2e0002009-09-29 18:17:31 -0700786 * Like {@link #getDrawable()}, but the returned Drawable has a number
787 * of limitations to reduce its overhead as much as possible. It will
788 * never scale the wallpaper (only centering it if the requested bounds
789 * do match the bitmap bounds, which should not be typical), doesn't
790 * allow setting an alpha, color filter, or other attributes, etc. The
791 * bounds of the returned drawable will be initialized to the same bounds
792 * as the wallpaper, so normally you will not need to touch it. The
793 * drawable also assumes that it will be used in a context running in
794 * the same density as the screen (not in density compatibility mode).
Dianne Hackborn19382ac2009-09-11 21:13:37 -0700795 *
796 * @return Returns a Drawable object that will draw the wallpaper.
797 */
Christopher Tate93252de2017-06-15 14:48:41 -0700798 @RequiresPermission(android.Manifest.permission.READ_EXTERNAL_STORAGE)
Dianne Hackborn19382ac2009-09-11 21:13:37 -0700799 public Drawable getFastDrawable() {
Christopher Tate5d99d472016-05-06 17:59:27 -0700800 Bitmap bm = sGlobals.peekWallpaperBitmap(mContext, true, FLAG_SYSTEM);
Dianne Hackborn19382ac2009-09-11 21:13:37 -0700801 if (bm != null) {
Romain Guy407ec782011-08-24 17:06:58 -0700802 return new FastBitmapDrawable(bm);
Dianne Hackborn19382ac2009-09-11 21:13:37 -0700803 }
804 return null;
805 }
806
807 /**
Scott Main8b2e0002009-09-29 18:17:31 -0700808 * Like {@link #getFastDrawable()}, but if there is no wallpaper set,
809 * a null pointer is returned.
Dianne Hackborn19382ac2009-09-11 21:13:37 -0700810 *
811 * @return Returns an optimized Drawable object that will draw the
812 * wallpaper or a null pointer if these is none.
813 */
Christopher Tate93252de2017-06-15 14:48:41 -0700814 @RequiresPermission(android.Manifest.permission.READ_EXTERNAL_STORAGE)
Dianne Hackborn19382ac2009-09-11 21:13:37 -0700815 public Drawable peekFastDrawable() {
Christopher Tate5d99d472016-05-06 17:59:27 -0700816 Bitmap bm = sGlobals.peekWallpaperBitmap(mContext, false, FLAG_SYSTEM);
Dianne Hackborn19382ac2009-09-11 21:13:37 -0700817 if (bm != null) {
Romain Guy407ec782011-08-24 17:06:58 -0700818 return new FastBitmapDrawable(bm);
Dianne Hackborn19382ac2009-09-11 21:13:37 -0700819 }
820 return null;
Dianne Hackborn8cc6a502009-08-05 21:29:42 -0700821 }
822
823 /**
Lucas Dupin049978e2017-10-02 11:38:13 -0700824 * Like {@link #getDrawable()} but returns a Bitmap with default {@link Bitmap.Config}.
Oleksandr Peletskyif2519812016-01-26 20:16:06 +0100825 *
Romain Guy407ec782011-08-24 17:06:58 -0700826 * @hide
827 */
Mathew Inwood61e8ae62018-08-14 14:17:44 +0100828 @UnsupportedAppUsage
Romain Guy407ec782011-08-24 17:06:58 -0700829 public Bitmap getBitmap() {
Lucas Dupin049978e2017-10-02 11:38:13 -0700830 return getBitmap(false);
831 }
832
833 /**
834 * Like {@link #getDrawable()} but returns a Bitmap.
835 *
836 * @param hardware Asks for a hardware backed bitmap.
837 * @see Bitmap.Config#HARDWARE
838 * @hide
839 */
Mathew Inwood61e8ae62018-08-14 14:17:44 +0100840 @UnsupportedAppUsage
Lucas Dupin049978e2017-10-02 11:38:13 -0700841 public Bitmap getBitmap(boolean hardware) {
842 return getBitmapAsUser(mContext.getUserId(), hardware);
Yorke Leedcd93cc2016-01-08 14:12:55 -0800843 }
844
845 /**
846 * Like {@link #getDrawable()} but returns a Bitmap for the provided user.
847 *
848 * @hide
849 */
Lucas Dupin049978e2017-10-02 11:38:13 -0700850 public Bitmap getBitmapAsUser(int userId, boolean hardware) {
851 return sGlobals.peekWallpaperBitmap(mContext, true, FLAG_SYSTEM, userId, hardware);
Romain Guy407ec782011-08-24 17:06:58 -0700852 }
853
854 /**
Christopher Tatebe132e62016-02-10 12:59:49 -0800855 * Get an open, readable file descriptor to the given wallpaper image file.
Christopher Tateedf7d042016-03-29 18:24:25 -0700856 * The caller is responsible for closing the file descriptor when done ingesting the file.
Christopher Tatebe132e62016-02-10 12:59:49 -0800857 *
858 * <p>If no lock-specific wallpaper has been configured for the given user, then
Christopher Tateedf7d042016-03-29 18:24:25 -0700859 * this method will return {@code null} when requesting {@link #FLAG_LOCK} rather than
Christopher Tatebe132e62016-02-10 12:59:49 -0800860 * returning the system wallpaper's image file.
Christopher Tateedf7d042016-03-29 18:24:25 -0700861 *
862 * @param which The wallpaper whose image file is to be retrieved. Must be a single
863 * defined kind of wallpaper, either {@link #FLAG_SYSTEM} or
864 * {@link #FLAG_LOCK}.
Christopher Tate93252de2017-06-15 14:48:41 -0700865 * @return An open, readable file desriptor to the requested wallpaper image file;
866 * or {@code null} if no such wallpaper is configured or if the calling app does
867 * not have permission to read the current wallpaper.
Christopher Tateedf7d042016-03-29 18:24:25 -0700868 *
869 * @see #FLAG_LOCK
870 * @see #FLAG_SYSTEM
Christopher Tatebe132e62016-02-10 12:59:49 -0800871 */
Christopher Tate93252de2017-06-15 14:48:41 -0700872 @RequiresPermission(android.Manifest.permission.READ_EXTERNAL_STORAGE)
Christopher Tate5d99d472016-05-06 17:59:27 -0700873 public ParcelFileDescriptor getWallpaperFile(@SetWallpaperFlags int which) {
Christopher Tatebe132e62016-02-10 12:59:49 -0800874 return getWallpaperFile(which, mContext.getUserId());
875 }
876
Lucas Dupinc40608c2017-04-14 18:33:08 -0700877 /**
878 * Registers a listener to get notified when the wallpaper colors change.
Lucas Dupinc40608c2017-04-14 18:33:08 -0700879 * @param listener A listener to register
Lucas Dupinea1fb1e2017-04-05 17:39:44 -0700880 * @param handler Where to call it from. Will be called from the main thread
Lucas Dupinc40608c2017-04-14 18:33:08 -0700881 * if null.
882 */
883 public void addOnColorsChangedListener(@NonNull OnColorsChangedListener listener,
Lucas Dupinea1fb1e2017-04-05 17:39:44 -0700884 @NonNull Handler handler) {
Lucas Dupin50ba9912017-07-14 11:55:05 -0700885 addOnColorsChangedListener(listener, handler, mContext.getUserId());
886 }
887
888 /**
889 * Registers a listener to get notified when the wallpaper colors change
890 * @param listener A listener to register
891 * @param handler Where to call it from. Will be called from the main thread
892 * if null.
893 * @param userId Owner of the wallpaper or UserHandle.USER_ALL.
894 * @hide
895 */
Mathew Inwood61e8ae62018-08-14 14:17:44 +0100896 @UnsupportedAppUsage
Lucas Dupin50ba9912017-07-14 11:55:05 -0700897 public void addOnColorsChangedListener(@NonNull OnColorsChangedListener listener,
898 @NonNull Handler handler, int userId) {
wilsonshih36597d42018-12-05 18:56:39 +0800899 sGlobals.addOnColorsChangedListener(listener, handler, userId, mContext.getDisplayId());
Lucas Dupinc40608c2017-04-14 18:33:08 -0700900 }
901
902 /**
903 * Stop listening to color updates.
Lucas Dupin50ba9912017-07-14 11:55:05 -0700904 * @param callback A callback to unsubscribe.
Lucas Dupinc40608c2017-04-14 18:33:08 -0700905 */
906 public void removeOnColorsChangedListener(@NonNull OnColorsChangedListener callback) {
Lucas Dupin50ba9912017-07-14 11:55:05 -0700907 removeOnColorsChangedListener(callback, mContext.getUserId());
908 }
909
910 /**
911 * Stop listening to color updates.
912 * @param callback A callback to unsubscribe.
913 * @param userId Owner of the wallpaper or UserHandle.USER_ALL.
914 * @hide
915 */
916 public void removeOnColorsChangedListener(@NonNull OnColorsChangedListener callback,
917 int userId) {
wilsonshih36597d42018-12-05 18:56:39 +0800918 sGlobals.removeOnColorsChangedListener(callback, userId, mContext.getDisplayId());
Lucas Dupinc40608c2017-04-14 18:33:08 -0700919 }
920
921 /**
Lucas Dupineeb36be2017-08-04 10:45:12 -0700922 * Get the primary colors of a wallpaper.
923 *
Christopher Tate105540d2018-03-21 13:03:09 -0700924 * <p>This method can return {@code null} when:
925 * <ul>
926 * <li>Colors are still being processed by the system.</li>
927 * <li>The user has chosen to use a live wallpaper: live wallpapers might not
928 * implement
929 * {@link android.service.wallpaper.WallpaperService.Engine#onComputeColors()
930 * WallpaperService.Engine#onComputeColors()}.</li>
931 * </ul>
Lucas Dupineeb36be2017-08-04 10:45:12 -0700932 *
933 * @param which Wallpaper type. Must be either {@link #FLAG_SYSTEM} or
934 * {@link #FLAG_LOCK}.
935 * @return Current {@link WallpaperColors} or null if colors are unknown.
936 * @see #addOnColorsChangedListener(OnColorsChangedListener, Handler)
Lucas Dupinc40608c2017-04-14 18:33:08 -0700937 */
938 public @Nullable WallpaperColors getWallpaperColors(int which) {
Lucas Dupin50ba9912017-07-14 11:55:05 -0700939 return getWallpaperColors(which, mContext.getUserId());
940 }
941
942 /**
Christopher Tate105540d2018-03-21 13:03:09 -0700943 * Get the primary colors of the wallpaper configured in the given user.
Lucas Dupin50ba9912017-07-14 11:55:05 -0700944 * @param which wallpaper type. Must be either {@link #FLAG_SYSTEM} or
945 * {@link #FLAG_LOCK}
946 * @param userId Owner of the wallpaper.
947 * @return {@link WallpaperColors} or null if colors are unknown.
948 * @hide
949 */
Mathew Inwood61e8ae62018-08-14 14:17:44 +0100950 @UnsupportedAppUsage
Lucas Dupin50ba9912017-07-14 11:55:05 -0700951 public @Nullable WallpaperColors getWallpaperColors(int which, int userId) {
wilsonshih36597d42018-12-05 18:56:39 +0800952 return sGlobals.getWallpaperColors(which, userId, mContext.getDisplayId());
Lucas Dupinc40608c2017-04-14 18:33:08 -0700953 }
954
Christopher Tatebe132e62016-02-10 12:59:49 -0800955 /**
956 * Version of {@link #getWallpaperFile(int)} that can access the wallpaper data
957 * for a given user. The caller must hold the INTERACT_ACROSS_USERS_FULL
958 * permission to access another user's wallpaper data.
Christopher Tateedf7d042016-03-29 18:24:25 -0700959 *
960 * @param which The wallpaper whose image file is to be retrieved. Must be a single
961 * defined kind of wallpaper, either {@link #FLAG_SYSTEM} or
962 * {@link #FLAG_LOCK}.
963 * @param userId The user or profile whose imagery is to be retrieved
964 *
965 * @see #FLAG_LOCK
966 * @see #FLAG_SYSTEM
967 *
Christopher Tatebe132e62016-02-10 12:59:49 -0800968 * @hide
969 */
Mathew Inwood61e8ae62018-08-14 14:17:44 +0100970 @UnsupportedAppUsage
Christopher Tate5d99d472016-05-06 17:59:27 -0700971 public ParcelFileDescriptor getWallpaperFile(@SetWallpaperFlags int which, int userId) {
Christopher Tateedf7d042016-03-29 18:24:25 -0700972 if (which != FLAG_SYSTEM && which != FLAG_LOCK) {
Christopher Tatebe132e62016-02-10 12:59:49 -0800973 throw new IllegalArgumentException("Must request exactly one kind of wallpaper");
974 }
975
976 if (sGlobals.mService == null) {
977 Log.w(TAG, "WallpaperService not running");
Christopher Tate98d609c2016-05-18 17:31:58 -0700978 throw new RuntimeException(new DeadSystemException());
Christopher Tatebe132e62016-02-10 12:59:49 -0800979 } else {
980 try {
981 Bundle outParams = new Bundle();
Christopher Tate93252de2017-06-15 14:48:41 -0700982 return sGlobals.mService.getWallpaper(mContext.getOpPackageName(), null, which,
983 outParams, userId);
Christopher Tatebe132e62016-02-10 12:59:49 -0800984 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -0700985 throw e.rethrowFromSystemServer();
Christopher Tate93252de2017-06-15 14:48:41 -0700986 } catch (SecurityException e) {
Jeff Sharkeyaa1a9112018-04-10 15:18:12 -0600987 if (mContext.getApplicationInfo().targetSdkVersion < Build.VERSION_CODES.O_MR1) {
Christopher Tate93252de2017-06-15 14:48:41 -0700988 Log.w(TAG, "No permission to access wallpaper, suppressing"
989 + " exception to avoid crashing legacy app.");
990 return null;
991 } else {
992 throw e;
993 }
Christopher Tatebe132e62016-02-10 12:59:49 -0800994 }
995 }
996 }
997
998 /**
Dianne Hackbornba398392011-08-01 16:11:57 -0700999 * Remove all internal references to the last loaded wallpaper. Useful
1000 * for apps that want to reduce memory usage when they only temporarily
1001 * need to have the wallpaper. After calling, the next request for the
1002 * wallpaper will require reloading it again from disk.
1003 */
1004 public void forgetLoadedWallpaper() {
Christopher Tatebe132e62016-02-10 12:59:49 -08001005 sGlobals.forgetLoadedWallpaper();
Dianne Hackbornba398392011-08-01 16:11:57 -07001006 }
1007
1008 /**
Lucas Dupinf132a8d2018-06-19 16:05:09 -07001009 * Returns the information about the wallpaper if the current wallpaper is
1010 * a live wallpaper component. Otherwise, if the wallpaper is a static image,
1011 * this returns null.
Dianne Hackborneb034652009-09-07 00:49:58 -07001012 */
1013 public WallpaperInfo getWallpaperInfo() {
Lucas Dupinf132a8d2018-06-19 16:05:09 -07001014 return getWallpaperInfo(mContext.getUserId());
1015 }
1016
1017 /**
1018 * Returns the information about the wallpaper if the current wallpaper is
1019 * a live wallpaper component. Otherwise, if the wallpaper is a static image,
1020 * this returns null.
1021 *
1022 * @param userId Owner of the wallpaper.
1023 * @hide
1024 */
1025 public WallpaperInfo getWallpaperInfo(int userId) {
Dianne Hackborneb034652009-09-07 00:49:58 -07001026 try {
Mike Lockwoodc067c9c2011-10-31 12:50:12 -04001027 if (sGlobals.mService == null) {
1028 Log.w(TAG, "WallpaperService not running");
Christopher Tate98d609c2016-05-18 17:31:58 -07001029 throw new RuntimeException(new DeadSystemException());
Mike Lockwoodc067c9c2011-10-31 12:50:12 -04001030 } else {
Lucas Dupinf132a8d2018-06-19 16:05:09 -07001031 return sGlobals.mService.getWallpaperInfo(userId);
Mike Lockwoodc067c9c2011-10-31 12:50:12 -04001032 }
Dianne Hackborneb034652009-09-07 00:49:58 -07001033 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07001034 throw e.rethrowFromSystemServer();
Dianne Hackborneb034652009-09-07 00:49:58 -07001035 }
1036 }
Michael Jurkae8d1bf72013-09-09 15:58:54 +02001037
1038 /**
Christopher Tatee409f0e2016-03-21 14:53:15 -07001039 * Get the ID of the current wallpaper of the given kind. If there is no
1040 * such wallpaper configured, returns a negative number.
1041 *
Christopher Tate98d609c2016-05-18 17:31:58 -07001042 * <p>Every time the wallpaper image is set, a new ID is assigned to it.
1043 * This method allows the caller to determine whether the wallpaper imagery
1044 * has changed, regardless of how that change happened.
1045 *
Christopher Tatee409f0e2016-03-21 14:53:15 -07001046 * @param which The wallpaper whose ID is to be returned. Must be a single
Christopher Tateedf7d042016-03-29 18:24:25 -07001047 * defined kind of wallpaper, either {@link #FLAG_SYSTEM} or
1048 * {@link #FLAG_LOCK}.
Christopher Tatee409f0e2016-03-21 14:53:15 -07001049 * @return The positive numeric ID of the current wallpaper of the given kind,
1050 * or a negative value if no such wallpaper is configured.
1051 */
Christopher Tate5d99d472016-05-06 17:59:27 -07001052 public int getWallpaperId(@SetWallpaperFlags int which) {
Christopher Tatee409f0e2016-03-21 14:53:15 -07001053 return getWallpaperIdForUser(which, mContext.getUserId());
1054 }
1055
1056 /**
1057 * Get the ID of the given user's current wallpaper of the given kind. If there
1058 * is no such wallpaper configured, returns a negative number.
1059 * @hide
1060 */
Christopher Tate5d99d472016-05-06 17:59:27 -07001061 public int getWallpaperIdForUser(@SetWallpaperFlags int which, int userId) {
Christopher Tatee409f0e2016-03-21 14:53:15 -07001062 try {
1063 if (sGlobals.mService == null) {
1064 Log.w(TAG, "WallpaperService not running");
Christopher Tate98d609c2016-05-18 17:31:58 -07001065 throw new RuntimeException(new DeadSystemException());
Christopher Tatee409f0e2016-03-21 14:53:15 -07001066 } else {
1067 return sGlobals.mService.getWallpaperIdForUser(which, userId);
1068 }
1069 } catch (RemoteException e) {
1070 throw e.rethrowFromSystemServer();
1071 }
1072 }
1073
1074 /**
Michael Jurkae8d1bf72013-09-09 15:58:54 +02001075 * Gets an Intent that will launch an activity that crops the given
1076 * image and sets the device's wallpaper. If there is a default HOME activity
1077 * that supports cropping wallpapers, it will be preferred as the default.
Ying Wang930d4e52013-09-14 11:57:17 -07001078 * Use this method instead of directly creating a {@link #ACTION_CROP_AND_SET_WALLPAPER}
Michael Jurkae8d1bf72013-09-09 15:58:54 +02001079 * intent.
Adam Lesinskibba72d12013-10-11 18:10:56 -07001080 *
1081 * @param imageUri The image URI that will be set in the intent. The must be a content
1082 * URI and its provider must resolve its type to "image/*"
1083 *
1084 * @throws IllegalArgumentException if the URI is not a content URI or its MIME type is
1085 * not "image/*"
Michael Jurkae8d1bf72013-09-09 15:58:54 +02001086 */
1087 public Intent getCropAndSetWallpaperIntent(Uri imageUri) {
Allen Shen1c0f21e2014-04-29 16:16:29 +08001088 if (imageUri == null) {
1089 throw new IllegalArgumentException("Image URI must not be null");
1090 }
1091
Adam Lesinskibba72d12013-10-11 18:10:56 -07001092 if (!ContentResolver.SCHEME_CONTENT.equals(imageUri.getScheme())) {
1093 throw new IllegalArgumentException("Image URI must be of the "
1094 + ContentResolver.SCHEME_CONTENT + " scheme type");
1095 }
1096
Michael Jurkae8d1bf72013-09-09 15:58:54 +02001097 final PackageManager packageManager = mContext.getPackageManager();
1098 Intent cropAndSetWallpaperIntent =
1099 new Intent(ACTION_CROP_AND_SET_WALLPAPER, imageUri);
1100 cropAndSetWallpaperIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
1101
1102 // Find out if the default HOME activity supports CROP_AND_SET_WALLPAPER
1103 Intent homeIntent = new Intent(Intent.ACTION_MAIN).addCategory(Intent.CATEGORY_HOME);
1104 ResolveInfo resolvedHome = packageManager.resolveActivity(homeIntent,
1105 PackageManager.MATCH_DEFAULT_ONLY);
1106 if (resolvedHome != null) {
1107 cropAndSetWallpaperIntent.setPackage(resolvedHome.activityInfo.packageName);
1108
1109 List<ResolveInfo> cropAppList = packageManager.queryIntentActivities(
1110 cropAndSetWallpaperIntent, 0);
1111 if (cropAppList.size() > 0) {
1112 return cropAndSetWallpaperIntent;
1113 }
1114 }
1115
1116 // fallback crop activity
Oren Blasberg60598ea0a2016-02-26 11:01:23 -08001117 final String cropperPackage = mContext.getString(
1118 com.android.internal.R.string.config_wallpaperCropperPackage);
1119 cropAndSetWallpaperIntent.setPackage(cropperPackage);
Adam Lesinskibba72d12013-10-11 18:10:56 -07001120 List<ResolveInfo> cropAppList = packageManager.queryIntentActivities(
1121 cropAndSetWallpaperIntent, 0);
1122 if (cropAppList.size() > 0) {
1123 return cropAndSetWallpaperIntent;
1124 }
1125 // If the URI is not of the right type, or for some reason the system wallpaper
1126 // cropper doesn't exist, return null
1127 throw new IllegalArgumentException("Cannot use passed URI to set wallpaper; " +
1128 "check that the type returned by ContentProvider matches image/*");
Michael Jurkae8d1bf72013-09-09 15:58:54 +02001129 }
1130
Dianne Hackborneb034652009-09-07 00:49:58 -07001131 /**
Dianne Hackborn8cc6a502009-08-05 21:29:42 -07001132 * Change the current system wallpaper to the bitmap in the given resource.
1133 * The resource is opened as a raw data stream and copied into the
1134 * wallpaper; it must be a valid PNG or JPEG image. On success, the intent
1135 * {@link Intent#ACTION_WALLPAPER_CHANGED} is broadcast.
1136 *
Nicolas Falliere9530e3a2012-06-18 17:21:06 -07001137 * <p>This method requires the caller to hold the permission
1138 * {@link android.Manifest.permission#SET_WALLPAPER}.
1139 *
Christopher Tateedf7d042016-03-29 18:24:25 -07001140 * @param resid The resource ID of the bitmap to be used as the wallpaper image
Dianne Hackborn8cc6a502009-08-05 21:29:42 -07001141 *
Michael Jurkab668d0b2013-10-04 15:11:05 -07001142 * @throws IOException If an error occurs reverting to the built-in
Dianne Hackborn8cc6a502009-08-05 21:29:42 -07001143 * wallpaper.
1144 */
Christopher Tate93252de2017-06-15 14:48:41 -07001145 @RequiresPermission(android.Manifest.permission.SET_WALLPAPER)
Tor Norbye7b9c9122013-05-30 16:48:33 -07001146 public void setResource(@RawRes int resid) throws IOException {
Edward Savage-Jones6009d9d2016-08-30 18:44:19 +02001147 setResource(resid, FLAG_SYSTEM | FLAG_LOCK);
Christopher Tatead3c2592016-01-20 18:13:17 -08001148 }
1149
1150 /**
Christopher Tateedf7d042016-03-29 18:24:25 -07001151 * Version of {@link #setResource(int)} that allows the caller to specify which
1152 * of the supported wallpaper categories to set.
Christopher Tatead3c2592016-01-20 18:13:17 -08001153 *
Christopher Tateedf7d042016-03-29 18:24:25 -07001154 * @param resid The resource ID of the bitmap to be used as the wallpaper image
1155 * @param which Flags indicating which wallpaper(s) to configure with the new imagery
Christopher Tatead3c2592016-01-20 18:13:17 -08001156 *
Christopher Tateedf7d042016-03-29 18:24:25 -07001157 * @see #FLAG_LOCK
1158 * @see #FLAG_SYSTEM
Christopher Tatead3c2592016-01-20 18:13:17 -08001159 *
1160 * @return An integer ID assigned to the newly active wallpaper; or zero on failure.
1161 *
1162 * @throws IOException
1163 */
Christopher Tate93252de2017-06-15 14:48:41 -07001164 @RequiresPermission(android.Manifest.permission.SET_WALLPAPER)
Christopher Tatead3c2592016-01-20 18:13:17 -08001165 public int setResource(@RawRes int resid, @SetWallpaperFlags int which)
1166 throws IOException {
Mike Lockwoodc067c9c2011-10-31 12:50:12 -04001167 if (sGlobals.mService == null) {
1168 Log.w(TAG, "WallpaperService not running");
Christopher Tate98d609c2016-05-18 17:31:58 -07001169 throw new RuntimeException(new DeadSystemException());
Mike Lockwoodc067c9c2011-10-31 12:50:12 -04001170 }
Christopher Tatead3c2592016-01-20 18:13:17 -08001171 final Bundle result = new Bundle();
Christopher Tate1e1e2e02016-01-25 15:34:36 -08001172 final WallpaperSetCompletion completion = new WallpaperSetCompletion();
Dianne Hackborn8cc6a502009-08-05 21:29:42 -07001173 try {
1174 Resources resources = mContext.getResources();
1175 /* Set the wallpaper to the default values */
Dianne Hackborn840c3a22009-09-02 17:35:17 -07001176 ParcelFileDescriptor fd = sGlobals.mService.setWallpaper(
Christopher Tatead3c2592016-01-20 18:13:17 -08001177 "res:" + resources.getResourceName(resid),
Jorim Jaggi6c902d02016-08-18 10:44:54 -07001178 mContext.getOpPackageName(), null, false, result, which, completion,
Jeff Sharkeyad357d12018-02-02 13:25:31 -07001179 mContext.getUserId());
Dianne Hackborn8cc6a502009-08-05 21:29:42 -07001180 if (fd != null) {
1181 FileOutputStream fos = null;
Christopher Tate1e1e2e02016-01-25 15:34:36 -08001182 boolean ok = false;
Dianne Hackborn8cc6a502009-08-05 21:29:42 -07001183 try {
1184 fos = new ParcelFileDescriptor.AutoCloseOutputStream(fd);
Christopher Tatead3c2592016-01-20 18:13:17 -08001185 copyStreamToWallpaperFile(resources.openRawResource(resid), fos);
Christopher Tate1e1e2e02016-01-25 15:34:36 -08001186 // The 'close()' is the trigger for any server-side image manipulation,
1187 // so we must do that before waiting for completion.
1188 fos.close();
1189 completion.waitForCompletion();
Dianne Hackborn8cc6a502009-08-05 21:29:42 -07001190 } finally {
Christopher Tate1e1e2e02016-01-25 15:34:36 -08001191 // Might be redundant but completion shouldn't wait unless the write
1192 // succeeded; this is a fallback if it threw past the close+wait.
Christopher Tateffa6a882015-12-11 15:08:46 -08001193 IoUtils.closeQuietly(fos);
Dianne Hackborn8cc6a502009-08-05 21:29:42 -07001194 }
1195 }
1196 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07001197 throw e.rethrowFromSystemServer();
Dianne Hackborn8cc6a502009-08-05 21:29:42 -07001198 }
Christopher Tatead3c2592016-01-20 18:13:17 -08001199 return result.getInt(EXTRA_NEW_WALLPAPER_ID, 0);
Dianne Hackborn8cc6a502009-08-05 21:29:42 -07001200 }
Christopher Tateffa6a882015-12-11 15:08:46 -08001201
Dianne Hackborn8cc6a502009-08-05 21:29:42 -07001202 /**
1203 * Change the current system wallpaper to a bitmap. The given bitmap is
1204 * converted to a PNG and stored as the wallpaper. On success, the intent
1205 * {@link Intent#ACTION_WALLPAPER_CHANGED} is broadcast.
1206 *
Christopher Tateffa6a882015-12-11 15:08:46 -08001207 * <p>This method is equivalent to calling
1208 * {@link #setBitmap(Bitmap, Rect, boolean)} and passing {@code null} for the
1209 * {@code visibleCrop} rectangle and {@code true} for the {@code allowBackup}
1210 * parameter.
1211 *
Nicolas Falliere9530e3a2012-06-18 17:21:06 -07001212 * <p>This method requires the caller to hold the permission
1213 * {@link android.Manifest.permission#SET_WALLPAPER}.
1214 *
Christopher Tatead3c2592016-01-20 18:13:17 -08001215 * @param bitmap The bitmap to be used as the new system wallpaper.
Dianne Hackborn8cc6a502009-08-05 21:29:42 -07001216 *
Christopher Tateffa6a882015-12-11 15:08:46 -08001217 * @throws IOException If an error occurs when attempting to set the wallpaper
1218 * to the provided image.
Dianne Hackborn8cc6a502009-08-05 21:29:42 -07001219 */
Christopher Tate93252de2017-06-15 14:48:41 -07001220 @RequiresPermission(android.Manifest.permission.SET_WALLPAPER)
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001221 public void setBitmap(Bitmap bitmap) throws IOException {
Christopher Tateffa6a882015-12-11 15:08:46 -08001222 setBitmap(bitmap, null, true);
1223 }
1224
1225 /**
1226 * Change the current system wallpaper to a bitmap, specifying a hint about
1227 * which subrectangle of the full image is to be visible. The OS will then
1228 * try to best present the given portion of the full image as the static system
1229 * wallpaper image. On success, the intent
1230 * {@link Intent#ACTION_WALLPAPER_CHANGED} is broadcast.
1231 *
1232 * <p>Passing {@code null} as the {@code visibleHint} parameter is equivalent to
1233 * passing (0, 0, {@code fullImage.getWidth()}, {@code fullImage.getHeight()}).
1234 *
1235 * <p>This method requires the caller to hold the permission
1236 * {@link android.Manifest.permission#SET_WALLPAPER}.
1237 *
Christopher Tatead3c2592016-01-20 18:13:17 -08001238 * @param fullImage A bitmap that will supply the wallpaper imagery.
Christopher Tateffa6a882015-12-11 15:08:46 -08001239 * @param visibleCropHint The rectangular subregion of {@code fullImage} that should be
1240 * displayed as wallpaper. Passing {@code null} for this parameter means that
1241 * the full image should be displayed if possible given the image's and device's
Christopher Tatead3c2592016-01-20 18:13:17 -08001242 * aspect ratios, etc.
Christopher Tateffa6a882015-12-11 15:08:46 -08001243 * @param allowBackup {@code true} if the OS is permitted to back up this wallpaper
1244 * image for restore to a future device; {@code false} otherwise.
1245 *
Christopher Tatead3c2592016-01-20 18:13:17 -08001246 * @return An integer ID assigned to the newly active wallpaper; or zero on failure.
1247 *
Christopher Tateffa6a882015-12-11 15:08:46 -08001248 * @throws IOException If an error occurs when attempting to set the wallpaper
1249 * to the provided image.
1250 * @throws IllegalArgumentException If the {@code visibleCropHint} rectangle is
1251 * empty or invalid.
1252 */
Christopher Tate93252de2017-06-15 14:48:41 -07001253 @RequiresPermission(android.Manifest.permission.SET_WALLPAPER)
Christopher Tatead3c2592016-01-20 18:13:17 -08001254 public int setBitmap(Bitmap fullImage, Rect visibleCropHint, boolean allowBackup)
1255 throws IOException {
Edward Savage-Jones6009d9d2016-08-30 18:44:19 +02001256 return setBitmap(fullImage, visibleCropHint, allowBackup, FLAG_SYSTEM | FLAG_LOCK);
Christopher Tatead3c2592016-01-20 18:13:17 -08001257 }
1258
1259 /**
Christopher Tatead3c2592016-01-20 18:13:17 -08001260 * Version of {@link #setBitmap(Bitmap, Rect, boolean)} that allows the caller
1261 * to specify which of the supported wallpaper categories to set.
1262 *
1263 * @param fullImage A bitmap that will supply the wallpaper imagery.
1264 * @param visibleCropHint The rectangular subregion of {@code fullImage} that should be
1265 * displayed as wallpaper. Passing {@code null} for this parameter means that
1266 * the full image should be displayed if possible given the image's and device's
1267 * aspect ratios, etc.
1268 * @param allowBackup {@code true} if the OS is permitted to back up this wallpaper
1269 * image for restore to a future device; {@code false} otherwise.
1270 * @param which Flags indicating which wallpaper(s) to configure with the new imagery.
1271 *
Christopher Tateedf7d042016-03-29 18:24:25 -07001272 * @see #FLAG_LOCK
1273 * @see #FLAG_SYSTEM
Christopher Tatead3c2592016-01-20 18:13:17 -08001274 *
1275 * @return An integer ID assigned to the newly active wallpaper; or zero on failure.
1276 *
1277 * @throws IOException
1278 */
Christopher Tate93252de2017-06-15 14:48:41 -07001279 @RequiresPermission(android.Manifest.permission.SET_WALLPAPER)
Christopher Tatead3c2592016-01-20 18:13:17 -08001280 public int setBitmap(Bitmap fullImage, Rect visibleCropHint,
1281 boolean allowBackup, @SetWallpaperFlags int which)
Christopher Tateffa6a882015-12-11 15:08:46 -08001282 throws IOException {
Jorim Jaggi6c902d02016-08-18 10:44:54 -07001283 return setBitmap(fullImage, visibleCropHint, allowBackup, which,
Jeff Sharkeyad357d12018-02-02 13:25:31 -07001284 mContext.getUserId());
Jorim Jaggi6c902d02016-08-18 10:44:54 -07001285 }
1286
1287 /**
1288 * Like {@link #setBitmap(Bitmap, Rect, boolean, int)}, but allows to pass in an explicit user
1289 * id. If the user id doesn't match the user id the process is running under, calling this
1290 * requires permission {@link android.Manifest.permission#INTERACT_ACROSS_USERS_FULL}.
1291 * @hide
1292 */
Mathew Inwood8c854f82018-09-14 12:35:36 +01001293 @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P, trackingBug = 115609023)
Jorim Jaggi6c902d02016-08-18 10:44:54 -07001294 public int setBitmap(Bitmap fullImage, Rect visibleCropHint,
1295 boolean allowBackup, @SetWallpaperFlags int which, int userId)
1296 throws IOException {
Christopher Tateffa6a882015-12-11 15:08:46 -08001297 validateRect(visibleCropHint);
Mike Lockwoodc067c9c2011-10-31 12:50:12 -04001298 if (sGlobals.mService == null) {
1299 Log.w(TAG, "WallpaperService not running");
Christopher Tate98d609c2016-05-18 17:31:58 -07001300 throw new RuntimeException(new DeadSystemException());
Mike Lockwoodc067c9c2011-10-31 12:50:12 -04001301 }
Christopher Tatead3c2592016-01-20 18:13:17 -08001302 final Bundle result = new Bundle();
Christopher Tate1e1e2e02016-01-25 15:34:36 -08001303 final WallpaperSetCompletion completion = new WallpaperSetCompletion();
Dianne Hackborn8cc6a502009-08-05 21:29:42 -07001304 try {
Benjamin Franzf3ece362015-02-11 10:51:10 +00001305 ParcelFileDescriptor fd = sGlobals.mService.setWallpaper(null,
Christopher Tated7faf532016-02-25 12:43:38 -08001306 mContext.getOpPackageName(), visibleCropHint, allowBackup,
Jorim Jaggi6c902d02016-08-18 10:44:54 -07001307 result, which, completion, userId);
Christopher Tatead3c2592016-01-20 18:13:17 -08001308 if (fd != null) {
1309 FileOutputStream fos = null;
1310 try {
1311 fos = new ParcelFileDescriptor.AutoCloseOutputStream(fd);
1312 fullImage.compress(Bitmap.CompressFormat.PNG, 90, fos);
Christopher Tate1e1e2e02016-01-25 15:34:36 -08001313 fos.close();
1314 completion.waitForCompletion();
Christopher Tatead3c2592016-01-20 18:13:17 -08001315 } finally {
1316 IoUtils.closeQuietly(fos);
1317 }
Dianne Hackborn8cc6a502009-08-05 21:29:42 -07001318 }
1319 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07001320 throw e.rethrowFromSystemServer();
Dianne Hackborn8cc6a502009-08-05 21:29:42 -07001321 }
Christopher Tatead3c2592016-01-20 18:13:17 -08001322 return result.getInt(EXTRA_NEW_WALLPAPER_ID, 0);
Dianne Hackborn8cc6a502009-08-05 21:29:42 -07001323 }
1324
Christopher Tateffa6a882015-12-11 15:08:46 -08001325 private final void validateRect(Rect rect) {
1326 if (rect != null && rect.isEmpty()) {
1327 throw new IllegalArgumentException("visibleCrop rectangle must be valid and non-empty");
1328 }
1329 }
1330
Dianne Hackborn8cc6a502009-08-05 21:29:42 -07001331 /**
1332 * Change the current system wallpaper to a specific byte stream. The
1333 * give InputStream is copied into persistent storage and will now be
1334 * used as the wallpaper. Currently it must be either a JPEG or PNG
1335 * image. On success, the intent {@link Intent#ACTION_WALLPAPER_CHANGED}
1336 * is broadcast.
1337 *
Christopher Tateffa6a882015-12-11 15:08:46 -08001338 * <p>This method is equivalent to calling
1339 * {@link #setStream(InputStream, Rect, boolean)} and passing {@code null} for the
1340 * {@code visibleCrop} rectangle and {@code true} for the {@code allowBackup}
1341 * parameter.
1342 *
Nicolas Falliere9530e3a2012-06-18 17:21:06 -07001343 * <p>This method requires the caller to hold the permission
1344 * {@link android.Manifest.permission#SET_WALLPAPER}.
1345 *
Christopher Tate98d609c2016-05-18 17:31:58 -07001346 * @param bitmapData A stream containing the raw data to install as a wallpaper. This
1347 * data can be in any format handled by {@link BitmapRegionDecoder}.
Dianne Hackborn8cc6a502009-08-05 21:29:42 -07001348 *
Christopher Tateffa6a882015-12-11 15:08:46 -08001349 * @throws IOException If an error occurs when attempting to set the wallpaper
1350 * based on the provided image data.
Dianne Hackborn8cc6a502009-08-05 21:29:42 -07001351 */
Christopher Tate93252de2017-06-15 14:48:41 -07001352 @RequiresPermission(android.Manifest.permission.SET_WALLPAPER)
Christopher Tateffa6a882015-12-11 15:08:46 -08001353 public void setStream(InputStream bitmapData) throws IOException {
1354 setStream(bitmapData, null, true);
1355 }
1356
Christopher Tatead3c2592016-01-20 18:13:17 -08001357 private void copyStreamToWallpaperFile(InputStream data, FileOutputStream fos)
Christopher Tateffa6a882015-12-11 15:08:46 -08001358 throws IOException {
Jeff Sharkey45c97df2018-02-01 16:01:52 -07001359 FileUtils.copy(data, fos);
Christopher Tateffa6a882015-12-11 15:08:46 -08001360 }
1361
1362 /**
1363 * Change the current system wallpaper to a specific byte stream, specifying a
1364 * hint about which subrectangle of the full image is to be visible. The OS will
1365 * then try to best present the given portion of the full image as the static system
1366 * wallpaper image. The data from the given InputStream is copied into persistent
1367 * storage and will then be used as the system wallpaper. Currently the data must
1368 * be either a JPEG or PNG image. On success, the intent
1369 * {@link Intent#ACTION_WALLPAPER_CHANGED} is broadcast.
1370 *
1371 * <p>This method requires the caller to hold the permission
1372 * {@link android.Manifest.permission#SET_WALLPAPER}.
1373 *
Christopher Tate98d609c2016-05-18 17:31:58 -07001374 * @param bitmapData A stream containing the raw data to install as a wallpaper. This
1375 * data can be in any format handled by {@link BitmapRegionDecoder}.
Christopher Tateffa6a882015-12-11 15:08:46 -08001376 * @param visibleCropHint The rectangular subregion of the streamed image that should be
1377 * displayed as wallpaper. Passing {@code null} for this parameter means that
1378 * the full image should be displayed if possible given the image's and device's
1379 * aspect ratios, etc.
1380 * @param allowBackup {@code true} if the OS is permitted to back up this wallpaper
1381 * image for restore to a future device; {@code false} otherwise.
Christopher Tate98d609c2016-05-18 17:31:58 -07001382 * @return An integer ID assigned to the newly active wallpaper; or zero on failure.
1383 *
1384 * @see #getWallpaperId(int)
Christopher Tateffa6a882015-12-11 15:08:46 -08001385 *
1386 * @throws IOException If an error occurs when attempting to set the wallpaper
1387 * based on the provided image data.
1388 * @throws IllegalArgumentException If the {@code visibleCropHint} rectangle is
1389 * empty or invalid.
1390 */
Christopher Tate93252de2017-06-15 14:48:41 -07001391 @RequiresPermission(android.Manifest.permission.SET_WALLPAPER)
Christopher Tatead3c2592016-01-20 18:13:17 -08001392 public int setStream(InputStream bitmapData, Rect visibleCropHint, boolean allowBackup)
Christopher Tateffa6a882015-12-11 15:08:46 -08001393 throws IOException {
Edward Savage-Jones6009d9d2016-08-30 18:44:19 +02001394 return setStream(bitmapData, visibleCropHint, allowBackup, FLAG_SYSTEM | FLAG_LOCK);
Christopher Tatead3c2592016-01-20 18:13:17 -08001395 }
1396
1397 /**
1398 * Version of {@link #setStream(InputStream, Rect, boolean)} that allows the caller
1399 * to specify which of the supported wallpaper categories to set.
1400 *
Christopher Tate98d609c2016-05-18 17:31:58 -07001401 * @param bitmapData A stream containing the raw data to install as a wallpaper. This
1402 * data can be in any format handled by {@link BitmapRegionDecoder}.
Christopher Tatead3c2592016-01-20 18:13:17 -08001403 * @param visibleCropHint The rectangular subregion of the streamed image that should be
1404 * displayed as wallpaper. Passing {@code null} for this parameter means that
1405 * the full image should be displayed if possible given the image's and device's
1406 * aspect ratios, etc.
1407 * @param allowBackup {@code true} if the OS is permitted to back up this wallpaper
1408 * image for restore to a future device; {@code false} otherwise.
1409 * @param which Flags indicating which wallpaper(s) to configure with the new imagery.
Christopher Tate98d609c2016-05-18 17:31:58 -07001410 * @return An integer ID assigned to the newly active wallpaper; or zero on failure.
Christopher Tatead3c2592016-01-20 18:13:17 -08001411 *
Christopher Tate98d609c2016-05-18 17:31:58 -07001412 * @see #getWallpaperId(int)
Christopher Tateedf7d042016-03-29 18:24:25 -07001413 * @see #FLAG_LOCK
1414 * @see #FLAG_SYSTEM
Christopher Tatead3c2592016-01-20 18:13:17 -08001415 *
1416 * @throws IOException
1417 */
Christopher Tate93252de2017-06-15 14:48:41 -07001418 @RequiresPermission(android.Manifest.permission.SET_WALLPAPER)
Christopher Tatead3c2592016-01-20 18:13:17 -08001419 public int setStream(InputStream bitmapData, Rect visibleCropHint,
1420 boolean allowBackup, @SetWallpaperFlags int which)
1421 throws IOException {
Christopher Tateffa6a882015-12-11 15:08:46 -08001422 validateRect(visibleCropHint);
Mike Lockwoodc067c9c2011-10-31 12:50:12 -04001423 if (sGlobals.mService == null) {
1424 Log.w(TAG, "WallpaperService not running");
Christopher Tate98d609c2016-05-18 17:31:58 -07001425 throw new RuntimeException(new DeadSystemException());
Mike Lockwoodc067c9c2011-10-31 12:50:12 -04001426 }
Christopher Tatead3c2592016-01-20 18:13:17 -08001427 final Bundle result = new Bundle();
Christopher Tate1e1e2e02016-01-25 15:34:36 -08001428 final WallpaperSetCompletion completion = new WallpaperSetCompletion();
Dianne Hackborn8cc6a502009-08-05 21:29:42 -07001429 try {
Benjamin Franzf3ece362015-02-11 10:51:10 +00001430 ParcelFileDescriptor fd = sGlobals.mService.setWallpaper(null,
Christopher Tated7faf532016-02-25 12:43:38 -08001431 mContext.getOpPackageName(), visibleCropHint, allowBackup,
Jeff Sharkeyad357d12018-02-02 13:25:31 -07001432 result, which, completion, mContext.getUserId());
Christopher Tatead3c2592016-01-20 18:13:17 -08001433 if (fd != null) {
1434 FileOutputStream fos = null;
1435 try {
1436 fos = new ParcelFileDescriptor.AutoCloseOutputStream(fd);
1437 copyStreamToWallpaperFile(bitmapData, fos);
Christopher Tate1e1e2e02016-01-25 15:34:36 -08001438 fos.close();
1439 completion.waitForCompletion();
Christopher Tatead3c2592016-01-20 18:13:17 -08001440 } finally {
1441 IoUtils.closeQuietly(fos);
1442 }
Dianne Hackborn8cc6a502009-08-05 21:29:42 -07001443 }
1444 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07001445 throw e.rethrowFromSystemServer();
Dianne Hackborn8cc6a502009-08-05 21:29:42 -07001446 }
Christopher Tatead3c2592016-01-20 18:13:17 -08001447
1448 return result.getInt(EXTRA_NEW_WALLPAPER_ID, 0);
Dianne Hackborn8cc6a502009-08-05 21:29:42 -07001449 }
1450
Dianne Hackborn8cc6a502009-08-05 21:29:42 -07001451 /**
Dianne Hackborn5dc5a002012-09-15 19:33:48 -07001452 * Return whether any users are currently set to use the wallpaper
1453 * with the given resource ID. That is, their wallpaper has been
1454 * set through {@link #setResource(int)} with the same resource id.
1455 */
Tor Norbye7b9c9122013-05-30 16:48:33 -07001456 public boolean hasResourceWallpaper(@RawRes int resid) {
Dianne Hackborn5dc5a002012-09-15 19:33:48 -07001457 if (sGlobals.mService == null) {
1458 Log.w(TAG, "WallpaperService not running");
Christopher Tate98d609c2016-05-18 17:31:58 -07001459 throw new RuntimeException(new DeadSystemException());
Dianne Hackborn5dc5a002012-09-15 19:33:48 -07001460 }
1461 try {
1462 Resources resources = mContext.getResources();
1463 String name = "res:" + resources.getResourceName(resid);
1464 return sGlobals.mService.hasNamedWallpaper(name);
1465 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07001466 throw e.rethrowFromSystemServer();
Dianne Hackborn5dc5a002012-09-15 19:33:48 -07001467 }
1468 }
1469
1470 /**
Dianne Hackborn8cc6a502009-08-05 21:29:42 -07001471 * Returns the desired minimum width for the wallpaper. Callers of
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001472 * {@link #setBitmap(android.graphics.Bitmap)} or
1473 * {@link #setStream(java.io.InputStream)} should check this value
Dianne Hackborn8cc6a502009-08-05 21:29:42 -07001474 * beforehand to make sure the supplied wallpaper respects the desired
1475 * minimum width.
1476 *
1477 * If the returned value is <= 0, the caller should use the width of
1478 * the default display instead.
1479 *
1480 * @return The desired minimum width for the wallpaper. This value should
1481 * be honored by applications that set the wallpaper but it is not
1482 * mandatory.
1483 */
1484 public int getDesiredMinimumWidth() {
Mike Lockwoodc067c9c2011-10-31 12:50:12 -04001485 if (sGlobals.mService == null) {
1486 Log.w(TAG, "WallpaperService not running");
Christopher Tate98d609c2016-05-18 17:31:58 -07001487 throw new RuntimeException(new DeadSystemException());
Mike Lockwoodc067c9c2011-10-31 12:50:12 -04001488 }
Dianne Hackborn8cc6a502009-08-05 21:29:42 -07001489 try {
wilsonshih81e10a72018-11-15 10:54:21 +08001490 return sGlobals.mService.getWidthHint(mContext.getDisplayId());
Dianne Hackborn8cc6a502009-08-05 21:29:42 -07001491 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07001492 throw e.rethrowFromSystemServer();
Dianne Hackborn8cc6a502009-08-05 21:29:42 -07001493 }
1494 }
1495
1496 /**
1497 * Returns the desired minimum height for the wallpaper. Callers of
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001498 * {@link #setBitmap(android.graphics.Bitmap)} or
1499 * {@link #setStream(java.io.InputStream)} should check this value
Dianne Hackborn8cc6a502009-08-05 21:29:42 -07001500 * beforehand to make sure the supplied wallpaper respects the desired
1501 * minimum height.
1502 *
1503 * If the returned value is <= 0, the caller should use the height of
1504 * the default display instead.
1505 *
1506 * @return The desired minimum height for the wallpaper. This value should
1507 * be honored by applications that set the wallpaper but it is not
1508 * mandatory.
1509 */
1510 public int getDesiredMinimumHeight() {
Mike Lockwoodc067c9c2011-10-31 12:50:12 -04001511 if (sGlobals.mService == null) {
1512 Log.w(TAG, "WallpaperService not running");
Christopher Tate98d609c2016-05-18 17:31:58 -07001513 throw new RuntimeException(new DeadSystemException());
Mike Lockwoodc067c9c2011-10-31 12:50:12 -04001514 }
Dianne Hackborn8cc6a502009-08-05 21:29:42 -07001515 try {
wilsonshih81e10a72018-11-15 10:54:21 +08001516 return sGlobals.mService.getHeightHint(mContext.getDisplayId());
Dianne Hackborn8cc6a502009-08-05 21:29:42 -07001517 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07001518 throw e.rethrowFromSystemServer();
Dianne Hackborn8cc6a502009-08-05 21:29:42 -07001519 }
1520 }
1521
1522 /**
1523 * For use only by the current home application, to specify the size of
1524 * wallpaper it would like to use. This allows such applications to have
1525 * a virtual wallpaper that is larger than the physical screen, matching
1526 * the size of their workspace.
Dianne Hackbornc5bf7582012-04-25 19:12:07 -07001527 *
1528 * <p>Note developers, who don't seem to be reading this. This is
Christopher Tateffa6a882015-12-11 15:08:46 -08001529 * for <em>home apps</em> to tell what size wallpaper they would like.
1530 * Nobody else should be calling this! Certainly not other non-home
Dianne Hackbornc5bf7582012-04-25 19:12:07 -07001531 * apps that change the wallpaper. Those apps are supposed to
1532 * <b>retrieve</b> the suggested size so they can construct a wallpaper
1533 * that matches it.
1534 *
Nicolas Falliere9530e3a2012-06-18 17:21:06 -07001535 * <p>This method requires the caller to hold the permission
1536 * {@link android.Manifest.permission#SET_WALLPAPER_HINTS}.
1537 *
Dianne Hackborn8cc6a502009-08-05 21:29:42 -07001538 * @param minimumWidth Desired minimum width
1539 * @param minimumHeight Desired minimum height
1540 */
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001541 public void suggestDesiredDimensions(int minimumWidth, int minimumHeight) {
Dianne Hackborn8cc6a502009-08-05 21:29:42 -07001542 try {
Donghan Ryu289c2732011-11-14 18:56:11 -08001543 /**
1544 * The framework makes no attempt to limit the window size
1545 * to the maximum texture size. Any window larger than this
1546 * cannot be composited.
1547 *
1548 * Read maximum texture size from system property and scale down
1549 * minimumWidth and minimumHeight accordingly.
1550 */
1551 int maximumTextureSize;
1552 try {
1553 maximumTextureSize = SystemProperties.getInt("sys.max_texture_size", 0);
1554 } catch (Exception e) {
1555 maximumTextureSize = 0;
1556 }
1557
1558 if (maximumTextureSize > 0) {
1559 if ((minimumWidth > maximumTextureSize) ||
1560 (minimumHeight > maximumTextureSize)) {
1561 float aspect = (float)minimumHeight / (float)minimumWidth;
1562 if (minimumWidth > minimumHeight) {
1563 minimumWidth = maximumTextureSize;
1564 minimumHeight = (int)((minimumWidth * aspect) + 0.5);
1565 } else {
1566 minimumHeight = maximumTextureSize;
1567 minimumWidth = (int)((minimumHeight / aspect) + 0.5);
1568 }
1569 }
1570 }
1571
Mike Lockwoodc067c9c2011-10-31 12:50:12 -04001572 if (sGlobals.mService == null) {
1573 Log.w(TAG, "WallpaperService not running");
Christopher Tate98d609c2016-05-18 17:31:58 -07001574 throw new RuntimeException(new DeadSystemException());
Mike Lockwoodc067c9c2011-10-31 12:50:12 -04001575 } else {
Benjamin Franzf3ece362015-02-11 10:51:10 +00001576 sGlobals.mService.setDimensionHints(minimumWidth, minimumHeight,
wilsonshih81e10a72018-11-15 10:54:21 +08001577 mContext.getOpPackageName(), mContext.getDisplayId());
Mike Lockwoodc067c9c2011-10-31 12:50:12 -04001578 }
Dianne Hackborn8cc6a502009-08-05 21:29:42 -07001579 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07001580 throw e.rethrowFromSystemServer();
Dianne Hackborn8cc6a502009-08-05 21:29:42 -07001581 }
1582 }
Adam Cohen041a0ba2011-11-09 20:10:27 -08001583
Dianne Hackborn8cc6a502009-08-05 21:29:42 -07001584 /**
Dianne Hackborn067e5f62014-09-07 23:14:30 -07001585 * Specify extra padding that the wallpaper should have outside of the display.
1586 * That is, the given padding supplies additional pixels the wallpaper should extend
1587 * outside of the display itself.
Christopher Tate105540d2018-03-21 13:03:09 -07001588 *
1589 * <p>This method requires the caller to hold the permission
1590 * {@link android.Manifest.permission#SET_WALLPAPER_HINTS}.
1591 *
Dianne Hackborn067e5f62014-09-07 23:14:30 -07001592 * @param padding The number of pixels the wallpaper should extend beyond the display,
1593 * on its left, top, right, and bottom sides.
Dianne Hackborn067e5f62014-09-07 23:14:30 -07001594 */
Jeff Sharkeyd86b8fe2017-06-02 17:36:26 -06001595 @RequiresPermission(android.Manifest.permission.SET_WALLPAPER_HINTS)
Dianne Hackborn067e5f62014-09-07 23:14:30 -07001596 public void setDisplayPadding(Rect padding) {
1597 try {
1598 if (sGlobals.mService == null) {
1599 Log.w(TAG, "WallpaperService not running");
Christopher Tate98d609c2016-05-18 17:31:58 -07001600 throw new RuntimeException(new DeadSystemException());
Dianne Hackborn067e5f62014-09-07 23:14:30 -07001601 } else {
wilsonshih81e10a72018-11-15 10:54:21 +08001602 sGlobals.mService.setDisplayPadding(padding, mContext.getOpPackageName(),
1603 mContext.getDisplayId());
Dianne Hackborn067e5f62014-09-07 23:14:30 -07001604 }
1605 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07001606 throw e.rethrowFromSystemServer();
Dianne Hackborn067e5f62014-09-07 23:14:30 -07001607 }
1608 }
1609
1610 /**
1611 * Apply a raw offset to the wallpaper window. Should only be used in
1612 * combination with {@link #setDisplayPadding(android.graphics.Rect)} when you
1613 * have ensured that the wallpaper will extend outside of the display area so that
1614 * it can be moved without leaving part of the display uncovered.
1615 * @param x The offset, in pixels, to apply to the left edge.
1616 * @param y The offset, in pixels, to apply to the top edge.
1617 * @hide
1618 */
1619 @SystemApi
1620 public void setDisplayOffset(IBinder windowToken, int x, int y) {
1621 try {
1622 //Log.v(TAG, "Sending new wallpaper display offsets from app...");
1623 WindowManagerGlobal.getWindowSession().setWallpaperDisplayOffset(
1624 windowToken, x, y);
1625 //Log.v(TAG, "...app returning after sending display offset!");
1626 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07001627 throw e.rethrowFromSystemServer();
Dianne Hackborn067e5f62014-09-07 23:14:30 -07001628 }
1629 }
1630
1631 /**
Christopher Tate105540d2018-03-21 13:03:09 -07001632 * Reset all wallpaper to the factory default.
Filip Gruszczynski9fedc722015-01-28 15:59:46 -08001633 *
Christopher Tate105540d2018-03-21 13:03:09 -07001634 * <p>This method requires the caller to hold the permission
1635 * {@link android.Manifest.permission#SET_WALLPAPER}.
Filip Gruszczynski9fedc722015-01-28 15:59:46 -08001636 */
Jeff Sharkeyd86b8fe2017-06-02 17:36:26 -06001637 @RequiresPermission(android.Manifest.permission.SET_WALLPAPER)
Filip Gruszczynski9fedc722015-01-28 15:59:46 -08001638 public void clearWallpaper() {
Christopher Tatea2bd5122016-09-22 13:18:05 -07001639 clearWallpaper(FLAG_LOCK, mContext.getUserId());
1640 clearWallpaper(FLAG_SYSTEM, mContext.getUserId());
Christopher Tatebe132e62016-02-10 12:59:49 -08001641 }
1642
1643 /**
1644 * Clear the wallpaper for a specific user. The caller must hold the
1645 * INTERACT_ACROSS_USERS_FULL permission to clear another user's
Christopher Tate11620ed2017-08-28 14:16:22 -07001646 * wallpaper, and must hold the SET_WALLPAPER permission in all
1647 * circumstances.
Christopher Tatebe132e62016-02-10 12:59:49 -08001648 * @hide
1649 */
1650 @SystemApi
Christopher Tate11620ed2017-08-28 14:16:22 -07001651 @RequiresPermission(android.Manifest.permission.INTERACT_ACROSS_USERS_FULL)
Christopher Tate5d99d472016-05-06 17:59:27 -07001652 public void clearWallpaper(@SetWallpaperFlags int which, int userId) {
Filip Gruszczynski9fedc722015-01-28 15:59:46 -08001653 if (sGlobals.mService == null) {
1654 Log.w(TAG, "WallpaperService not running");
Christopher Tate98d609c2016-05-18 17:31:58 -07001655 throw new RuntimeException(new DeadSystemException());
Filip Gruszczynski9fedc722015-01-28 15:59:46 -08001656 }
1657 try {
Christopher Tatebe132e62016-02-10 12:59:49 -08001658 sGlobals.mService.clearWallpaper(mContext.getOpPackageName(), which, userId);
Filip Gruszczynski9fedc722015-01-28 15:59:46 -08001659 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07001660 throw e.rethrowFromSystemServer();
Filip Gruszczynski9fedc722015-01-28 15:59:46 -08001661 }
1662 }
1663
1664 /**
1665 * Set the live wallpaper.
1666 *
Filip Gruszczynski9fedc722015-01-28 15:59:46 -08001667 * @hide
1668 */
1669 @SystemApi
Jeff Sharkeyd86b8fe2017-06-02 17:36:26 -06001670 @RequiresPermission(android.Manifest.permission.SET_WALLPAPER_COMPONENT)
Filip Gruszczynski9fedc722015-01-28 15:59:46 -08001671 public boolean setWallpaperComponent(ComponentName name) {
Jeff Sharkeyad357d12018-02-02 13:25:31 -07001672 return setWallpaperComponent(name, mContext.getUserId());
Adrian Roos40ea0832016-07-14 14:19:55 -07001673 }
1674
1675 /**
1676 * Set the live wallpaper.
1677 *
1678 * This can only be called by packages with android.permission.SET_WALLPAPER_COMPONENT
1679 * permission. The caller must hold the INTERACT_ACROSS_USERS_FULL permission to change
1680 * another user's wallpaper.
1681 *
1682 * @hide
1683 */
Christopher Tate93252de2017-06-15 14:48:41 -07001684 @RequiresPermission(android.Manifest.permission.SET_WALLPAPER_COMPONENT)
Mathew Inwood61e8ae62018-08-14 14:17:44 +01001685 @UnsupportedAppUsage
Adrian Roos40ea0832016-07-14 14:19:55 -07001686 public boolean setWallpaperComponent(ComponentName name, int userId) {
Filip Gruszczynski9fedc722015-01-28 15:59:46 -08001687 if (sGlobals.mService == null) {
1688 Log.w(TAG, "WallpaperService not running");
Christopher Tate98d609c2016-05-18 17:31:58 -07001689 throw new RuntimeException(new DeadSystemException());
Filip Gruszczynski9fedc722015-01-28 15:59:46 -08001690 }
1691 try {
Adrian Roos40ea0832016-07-14 14:19:55 -07001692 sGlobals.mService.setWallpaperComponentChecked(name, mContext.getOpPackageName(),
1693 userId);
Filip Gruszczynski9fedc722015-01-28 15:59:46 -08001694 return true;
1695 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07001696 throw e.rethrowFromSystemServer();
Filip Gruszczynski9fedc722015-01-28 15:59:46 -08001697 }
Filip Gruszczynski9fedc722015-01-28 15:59:46 -08001698 }
1699
1700 /**
Christopher Tateffa6a882015-12-11 15:08:46 -08001701 * Set the display position of the current wallpaper within any larger space, when
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07001702 * that wallpaper is visible behind the given window. The X and Y offsets
1703 * are floating point numbers ranging from 0 to 1, representing where the
1704 * wallpaper should be positioned within the screen space. These only
Christopher Tateffa6a882015-12-11 15:08:46 -08001705 * make sense when the wallpaper is larger than the display.
Oleksandr Peletskyif2519812016-01-26 20:16:06 +01001706 *
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07001707 * @param windowToken The window who these offsets should be associated
Scott Main8b2e0002009-09-29 18:17:31 -07001708 * with, as returned by {@link android.view.View#getWindowToken()
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07001709 * View.getWindowToken()}.
Marco Nelissenbf6956b2009-11-09 15:21:13 -08001710 * @param xOffset The offset along the X dimension, from 0 to 1.
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07001711 * @param yOffset The offset along the Y dimension, from 0 to 1.
1712 */
1713 public void setWallpaperOffsets(IBinder windowToken, float xOffset, float yOffset) {
Adam Cohen791a6332012-01-12 14:38:38 -08001714 try {
1715 //Log.v(TAG, "Sending new wallpaper offsets from app...");
Jeff Brownf9e989d2013-04-04 23:04:03 -07001716 WindowManagerGlobal.getWindowSession().setWallpaperPosition(
Adam Cohen791a6332012-01-12 14:38:38 -08001717 windowToken, xOffset, yOffset, mWallpaperXStep, mWallpaperYStep);
1718 //Log.v(TAG, "...app returning after sending offsets!");
1719 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07001720 throw e.rethrowFromSystemServer();
Adam Cohen791a6332012-01-12 14:38:38 -08001721 }
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07001722 }
Adam Cohen041a0ba2011-11-09 20:10:27 -08001723
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07001724 /**
Marco Nelissenbf6956b2009-11-09 15:21:13 -08001725 * For applications that use multiple virtual screens showing a wallpaper,
1726 * specify the step size between virtual screens. For example, if the
Dianne Hackborn23ef7b42009-11-18 18:20:39 -08001727 * launcher has 3 virtual screens, it would specify an xStep of 0.5,
Marco Nelissenbf6956b2009-11-09 15:21:13 -08001728 * since the X offset for those screens are 0.0, 0.5 and 1.0
Oleksandr Peletskyif2519812016-01-26 20:16:06 +01001729 * @param xStep The X offset delta from one screen to the next one
Marco Nelissenbf6956b2009-11-09 15:21:13 -08001730 * @param yStep The Y offset delta from one screen to the next one
1731 */
1732 public void setWallpaperOffsetSteps(float xStep, float yStep) {
1733 mWallpaperXStep = xStep;
1734 mWallpaperYStep = yStep;
1735 }
Oleksandr Peletskyif2519812016-01-26 20:16:06 +01001736
Marco Nelissenbf6956b2009-11-09 15:21:13 -08001737 /**
Dianne Hackborn75804932009-10-20 20:15:20 -07001738 * Send an arbitrary command to the current active wallpaper.
Oleksandr Peletskyif2519812016-01-26 20:16:06 +01001739 *
Dianne Hackborn75804932009-10-20 20:15:20 -07001740 * @param windowToken The window who these offsets should be associated
1741 * with, as returned by {@link android.view.View#getWindowToken()
1742 * View.getWindowToken()}.
1743 * @param action Name of the command to perform. This must be a scoped
1744 * name to avoid collisions, such as "com.mycompany.wallpaper.DOIT".
1745 * @param x Arbitrary integer argument based on command.
1746 * @param y Arbitrary integer argument based on command.
1747 * @param z Arbitrary integer argument based on command.
1748 * @param extras Optional additional information for the command, or null.
1749 */
1750 public void sendWallpaperCommand(IBinder windowToken, String action,
1751 int x, int y, int z, Bundle extras) {
1752 try {
1753 //Log.v(TAG, "Sending new wallpaper offsets from app...");
Jeff Brownf9e989d2013-04-04 23:04:03 -07001754 WindowManagerGlobal.getWindowSession().sendWallpaperCommand(
Dianne Hackborn75804932009-10-20 20:15:20 -07001755 windowToken, action, x, y, z, extras, false);
1756 //Log.v(TAG, "...app returning after sending offsets!");
1757 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07001758 throw e.rethrowFromSystemServer();
Dianne Hackborn75804932009-10-20 20:15:20 -07001759 }
1760 }
Benjamin Franzf3ece362015-02-11 10:51:10 +00001761
1762 /**
1763 * Returns whether wallpapers are supported for the calling user. If this function returns
Oleksandr Peletskyif2519812016-01-26 20:16:06 +01001764 * {@code false}, any attempts to changing the wallpaper will have no effect,
1765 * and any attempt to obtain of the wallpaper will return {@code null}.
Benjamin Franzf3ece362015-02-11 10:51:10 +00001766 */
1767 public boolean isWallpaperSupported() {
1768 if (sGlobals.mService == null) {
1769 Log.w(TAG, "WallpaperService not running");
Christopher Tate98d609c2016-05-18 17:31:58 -07001770 throw new RuntimeException(new DeadSystemException());
Benjamin Franzf3ece362015-02-11 10:51:10 +00001771 } else {
1772 try {
1773 return sGlobals.mService.isWallpaperSupported(mContext.getOpPackageName());
1774 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07001775 throw e.rethrowFromSystemServer();
Benjamin Franzf3ece362015-02-11 10:51:10 +00001776 }
1777 }
Benjamin Franzf3ece362015-02-11 10:51:10 +00001778 }
1779
Dianne Hackborn75804932009-10-20 20:15:20 -07001780 /**
Oleksandr Peletskyif2519812016-01-26 20:16:06 +01001781 * Returns whether the calling package is allowed to set the wallpaper for the calling user.
1782 * If this function returns {@code false}, any attempts to change the wallpaper will have
1783 * no effect. Always returns {@code true} for device owner and profile owner.
1784 *
1785 * @see android.os.UserManager#DISALLOW_SET_WALLPAPER
1786 */
Christopher Tate98d609c2016-05-18 17:31:58 -07001787 public boolean isSetWallpaperAllowed() {
Oleksandr Peletskyif2519812016-01-26 20:16:06 +01001788 if (sGlobals.mService == null) {
1789 Log.w(TAG, "WallpaperService not running");
Christopher Tate98d609c2016-05-18 17:31:58 -07001790 throw new RuntimeException(new DeadSystemException());
Oleksandr Peletskyif2519812016-01-26 20:16:06 +01001791 } else {
1792 try {
Christopher Tate98d609c2016-05-18 17:31:58 -07001793 return sGlobals.mService.isSetWallpaperAllowed(mContext.getOpPackageName());
Oleksandr Peletskyif2519812016-01-26 20:16:06 +01001794 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07001795 throw e.rethrowFromSystemServer();
Oleksandr Peletskyif2519812016-01-26 20:16:06 +01001796 }
1797 }
Oleksandr Peletskyif2519812016-01-26 20:16:06 +01001798 }
1799
1800 /**
Dianne Hackborn72c82ab2009-08-11 21:13:54 -07001801 * Clear the offsets previously associated with this window through
1802 * {@link #setWallpaperOffsets(IBinder, float, float)}. This reverts
1803 * the window to its default state, where it does not cause the wallpaper
1804 * to scroll from whatever its last offsets were.
Oleksandr Peletskyif2519812016-01-26 20:16:06 +01001805 *
Dianne Hackborn72c82ab2009-08-11 21:13:54 -07001806 * @param windowToken The window who these offsets should be associated
Scott Main8b2e0002009-09-29 18:17:31 -07001807 * with, as returned by {@link android.view.View#getWindowToken()
Dianne Hackborn72c82ab2009-08-11 21:13:54 -07001808 * View.getWindowToken()}.
1809 */
1810 public void clearWallpaperOffsets(IBinder windowToken) {
1811 try {
Jeff Brownf9e989d2013-04-04 23:04:03 -07001812 WindowManagerGlobal.getWindowSession().setWallpaperPosition(
Marco Nelissenbf6956b2009-11-09 15:21:13 -08001813 windowToken, -1, -1, -1, -1);
Dianne Hackborn72c82ab2009-08-11 21:13:54 -07001814 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07001815 throw e.rethrowFromSystemServer();
Dianne Hackborn72c82ab2009-08-11 21:13:54 -07001816 }
1817 }
Oleksandr Peletskyif2519812016-01-26 20:16:06 +01001818
Dianne Hackborn72c82ab2009-08-11 21:13:54 -07001819 /**
Christopher Tatebe132e62016-02-10 12:59:49 -08001820 * Remove any currently set system wallpaper, reverting to the system's built-in
Dianne Hackborn8cc6a502009-08-05 21:29:42 -07001821 * wallpaper. On success, the intent {@link Intent#ACTION_WALLPAPER_CHANGED}
1822 * is broadcast.
1823 *
Nicolas Falliere9530e3a2012-06-18 17:21:06 -07001824 * <p>This method requires the caller to hold the permission
1825 * {@link android.Manifest.permission#SET_WALLPAPER}.
1826 *
Michael Jurkab668d0b2013-10-04 15:11:05 -07001827 * @throws IOException If an error occurs reverting to the built-in
Dianne Hackborn8cc6a502009-08-05 21:29:42 -07001828 * wallpaper.
1829 */
Christopher Tate93252de2017-06-15 14:48:41 -07001830 @RequiresPermission(android.Manifest.permission.SET_WALLPAPER)
Dianne Hackborn8cc6a502009-08-05 21:29:42 -07001831 public void clear() throws IOException {
Christopher Tate5d99d472016-05-06 17:59:27 -07001832 setStream(openDefaultWallpaper(mContext, FLAG_SYSTEM), null, false);
Jeff Sharkey28f08772014-04-16 09:41:58 -07001833 }
1834
1835 /**
Christopher Tate79a24572016-03-02 14:42:44 -08001836 * Remove one or more currently set wallpapers, reverting to the system default
Christopher Tateedf7d042016-03-29 18:24:25 -07001837 * display for each one. If {@link #FLAG_SYSTEM} is set in the {@code which}
Christopher Tate79a24572016-03-02 14:42:44 -08001838 * parameter, the intent {@link Intent#ACTION_WALLPAPER_CHANGED} will be broadcast
1839 * upon success.
1840 *
Christopher Tateedf7d042016-03-29 18:24:25 -07001841 * @param which A bitwise combination of {@link #FLAG_SYSTEM} or
1842 * {@link #FLAG_LOCK}
Christopher Tate79a24572016-03-02 14:42:44 -08001843 * @throws IOException If an error occurs reverting to the built-in wallpaper.
1844 */
Christopher Tate93252de2017-06-15 14:48:41 -07001845 @RequiresPermission(android.Manifest.permission.SET_WALLPAPER)
Christopher Tate5d99d472016-05-06 17:59:27 -07001846 public void clear(@SetWallpaperFlags int which) throws IOException {
Christopher Tateedf7d042016-03-29 18:24:25 -07001847 if ((which & FLAG_SYSTEM) != 0) {
Christopher Tate79a24572016-03-02 14:42:44 -08001848 clear();
1849 }
Christopher Tateedf7d042016-03-29 18:24:25 -07001850 if ((which & FLAG_LOCK) != 0) {
1851 clearWallpaper(FLAG_LOCK, mContext.getUserId());
Christopher Tate79a24572016-03-02 14:42:44 -08001852 }
1853 }
1854
1855 /**
Jeff Sharkey28f08772014-04-16 09:41:58 -07001856 * Open stream representing the default static image wallpaper.
1857 *
Christopher Tate5d99d472016-05-06 17:59:27 -07001858 * If the device defines no default wallpaper of the requested kind,
1859 * {@code null} is returned.
1860 *
Jeff Sharkey28f08772014-04-16 09:41:58 -07001861 * @hide
1862 */
Mathew Inwood61e8ae62018-08-14 14:17:44 +01001863 @UnsupportedAppUsage
Christopher Tate5d99d472016-05-06 17:59:27 -07001864 public static InputStream openDefaultWallpaper(Context context, @SetWallpaperFlags int which) {
1865 final String whichProp;
1866 final int defaultResId;
1867 if (which == FLAG_LOCK) {
Christopher Tate36cb2a7a2016-06-09 13:44:48 -07001868 /* Factory-default lock wallpapers are not yet supported
Christopher Tate5d99d472016-05-06 17:59:27 -07001869 whichProp = PROP_LOCK_WALLPAPER;
1870 defaultResId = com.android.internal.R.drawable.default_lock_wallpaper;
Christopher Tate36cb2a7a2016-06-09 13:44:48 -07001871 */
1872 return null;
Christopher Tate5d99d472016-05-06 17:59:27 -07001873 } else {
1874 whichProp = PROP_WALLPAPER;
1875 defaultResId = com.android.internal.R.drawable.default_wallpaper;
1876 }
1877 final String path = SystemProperties.get(whichProp);
Jeff Sharkey28f08772014-04-16 09:41:58 -07001878 if (!TextUtils.isEmpty(path)) {
1879 final File file = new File(path);
1880 if (file.exists()) {
1881 try {
1882 return new FileInputStream(file);
1883 } catch (IOException e) {
1884 // Ignored, fall back to platform default below
1885 }
1886 }
1887 }
Christopher Tate5d99d472016-05-06 17:59:27 -07001888 try {
1889 return context.getResources().openRawResource(defaultResId);
1890 } catch (NotFoundException e) {
1891 // no default defined for this device; this is not a failure
1892 }
1893 return null;
Jeff Sharkey28f08772014-04-16 09:41:58 -07001894 }
1895
1896 /**
1897 * Return {@link ComponentName} of the default live wallpaper, or
1898 * {@code null} if none is defined.
1899 *
1900 * @hide
1901 */
1902 public static ComponentName getDefaultWallpaperComponent(Context context) {
SzuWei Lin1c7ae312018-11-22 14:33:01 +08001903 ComponentName cn = null;
1904
Jeff Sharkey28f08772014-04-16 09:41:58 -07001905 String flat = SystemProperties.get(PROP_WALLPAPER_COMPONENT);
1906 if (!TextUtils.isEmpty(flat)) {
SzuWei Lin1c7ae312018-11-22 14:33:01 +08001907 cn = ComponentName.unflattenFromString(flat);
1908 }
1909
1910 if (cn == null) {
1911 flat = context.getString(com.android.internal.R.string.default_wallpaper_component);
1912 if (!TextUtils.isEmpty(flat)) {
1913 cn = ComponentName.unflattenFromString(flat);
Jeff Sharkey28f08772014-04-16 09:41:58 -07001914 }
1915 }
1916
SzuWei Lin1c7ae312018-11-22 14:33:01 +08001917 // Check if the package exists
1918 if (cn != null) {
1919 try {
1920 final PackageManager packageManager = context.getPackageManager();
1921 packageManager.getPackageInfo(cn.getPackageName(),
1922 PackageManager.MATCH_DIRECT_BOOT_AWARE
1923 | PackageManager.MATCH_DIRECT_BOOT_UNAWARE);
1924 } catch (PackageManager.NameNotFoundException e) {
1925 cn = null;
Jeff Sharkey28f08772014-04-16 09:41:58 -07001926 }
1927 }
1928
SzuWei Lin1c7ae312018-11-22 14:33:01 +08001929 return cn;
Dianne Hackborn8cc6a502009-08-05 21:29:42 -07001930 }
Christopher Tate1e1e2e02016-01-25 15:34:36 -08001931
Christopher Tatebe132e62016-02-10 12:59:49 -08001932 /**
1933 * Register a callback for lock wallpaper observation. Only the OS may use this.
1934 *
1935 * @return true on success; false on error.
1936 * @hide
1937 */
1938 public boolean setLockWallpaperCallback(IWallpaperManagerCallback callback) {
1939 if (sGlobals.mService == null) {
1940 Log.w(TAG, "WallpaperService not running");
Christopher Tate98d609c2016-05-18 17:31:58 -07001941 throw new RuntimeException(new DeadSystemException());
Christopher Tatebe132e62016-02-10 12:59:49 -08001942 }
1943
1944 try {
1945 return sGlobals.mService.setLockWallpaperCallback(callback);
1946 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07001947 throw e.rethrowFromSystemServer();
Christopher Tatebe132e62016-02-10 12:59:49 -08001948 }
Christopher Tatebe132e62016-02-10 12:59:49 -08001949 }
1950
Christopher Tated7faf532016-02-25 12:43:38 -08001951 /**
1952 * Is the current system wallpaper eligible for backup?
1953 *
1954 * Only the OS itself may use this method.
1955 * @hide
1956 */
Christopher Tate61722662016-08-10 16:13:14 -07001957 public boolean isWallpaperBackupEligible(int which) {
Christopher Tated7faf532016-02-25 12:43:38 -08001958 if (sGlobals.mService == null) {
1959 Log.w(TAG, "WallpaperService not running");
Christopher Tate98d609c2016-05-18 17:31:58 -07001960 throw new RuntimeException(new DeadSystemException());
Christopher Tated7faf532016-02-25 12:43:38 -08001961 }
1962 try {
Christopher Tate61722662016-08-10 16:13:14 -07001963 return sGlobals.mService.isWallpaperBackupEligible(which, mContext.getUserId());
Christopher Tated7faf532016-02-25 12:43:38 -08001964 } catch (RemoteException e) {
1965 Log.e(TAG, "Exception querying wallpaper backup eligibility: " + e.getMessage());
1966 }
1967 return false;
1968 }
1969
Christopher Tate1e1e2e02016-01-25 15:34:36 -08001970 // Private completion callback for setWallpaper() synchronization
1971 private class WallpaperSetCompletion extends IWallpaperManagerCallback.Stub {
1972 final CountDownLatch mLatch;
1973
1974 public WallpaperSetCompletion() {
1975 mLatch = new CountDownLatch(1);
1976 }
1977
1978 public void waitForCompletion() {
1979 try {
1980 mLatch.await(30, TimeUnit.SECONDS);
1981 } catch (InterruptedException e) {
1982 // This might be legit: the crop may take a very long time. Don't sweat
1983 // it in that case; we are okay with display lagging behind in order to
1984 // keep the caller from locking up indeterminately.
1985 }
1986 }
1987
1988 @Override
1989 public void onWallpaperChanged() throws RemoteException {
1990 mLatch.countDown();
1991 }
Lucas Dupinea1fb1e2017-04-05 17:39:44 -07001992
1993 @Override
Lucas Dupin50ba9912017-07-14 11:55:05 -07001994 public void onWallpaperColorsChanged(WallpaperColors colors, int which, int userId)
Lucas Dupinea1fb1e2017-04-05 17:39:44 -07001995 throws RemoteException {
Lucas Dupin50ba9912017-07-14 11:55:05 -07001996 sGlobals.onWallpaperColorsChanged(colors, which, userId);
Lucas Dupinea1fb1e2017-04-05 17:39:44 -07001997 }
Christopher Tate1e1e2e02016-01-25 15:34:36 -08001998 }
Lucas Dupinc40608c2017-04-14 18:33:08 -07001999
2000 /**
2001 * Interface definition for a callback to be invoked when colors change on a wallpaper.
2002 */
2003 public interface OnColorsChangedListener {
2004 /**
2005 * Called when colors change.
2006 * A {@link android.app.WallpaperColors} object containing a simplified
2007 * color histogram will be given.
2008 *
2009 * @param colors Wallpaper color info
2010 * @param which A combination of {@link #FLAG_LOCK} and {@link #FLAG_SYSTEM}
2011 */
2012 void onColorsChanged(WallpaperColors colors, int which);
Lucas Dupin50ba9912017-07-14 11:55:05 -07002013
2014 /**
2015 * Called when colors change.
2016 * A {@link android.app.WallpaperColors} object containing a simplified
2017 * color histogram will be given.
2018 *
2019 * @param colors Wallpaper color info
2020 * @param which A combination of {@link #FLAG_LOCK} and {@link #FLAG_SYSTEM}
2021 * @param userId Owner of the wallpaper
2022 * @hide
2023 */
2024 default void onColorsChanged(WallpaperColors colors, int which, int userId) {
2025 onColorsChanged(colors, which);
2026 }
Lucas Dupinc40608c2017-04-14 18:33:08 -07002027 }
Dianne Hackborn8cc6a502009-08-05 21:29:42 -07002028}