blob: b8c98420aadaa98c70dd7e63e00a270989059d1e [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 Inwood4fb17d12018-08-14 14:25: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
Lucas Dupinea1fb1e2017-04-05 17:39:44 -0700310 */
311 public void addOnColorsChangedListener(@NonNull OnColorsChangedListener callback,
Lucas Dupin50ba9912017-07-14 11:55:05 -0700312 @Nullable Handler handler, int userId) {
Lucas Dupinea1fb1e2017-04-05 17:39:44 -0700313 synchronized (this) {
314 if (!mColorCallbackRegistered) {
315 try {
Lucas Dupin50ba9912017-07-14 11:55:05 -0700316 mService.registerWallpaperColorsCallback(this, userId);
Lucas Dupinea1fb1e2017-04-05 17:39:44 -0700317 mColorCallbackRegistered = true;
318 } catch (RemoteException e) {
319 // Failed, service is gone
320 Log.w(TAG, "Can't register for color updates", e);
321 }
322 }
323 mColorListeners.add(new Pair<>(callback, handler));
324 }
325 }
326
327 /**
328 * Stop listening to wallpaper color events.
329 *
330 * @param callback listener
Lucas Dupin50ba9912017-07-14 11:55:05 -0700331 * @param userId Owner of the wallpaper or UserHandle.USER_ALL
Lucas Dupinea1fb1e2017-04-05 17:39:44 -0700332 */
Lucas Dupin50ba9912017-07-14 11:55:05 -0700333 public void removeOnColorsChangedListener(@NonNull OnColorsChangedListener callback,
334 int userId) {
Lucas Dupinea1fb1e2017-04-05 17:39:44 -0700335 synchronized (this) {
336 mColorListeners.removeIf(pair -> pair.first == callback);
337
338 if (mColorListeners.size() == 0 && mColorCallbackRegistered) {
339 mColorCallbackRegistered = false;
340 try {
Lucas Dupin50ba9912017-07-14 11:55:05 -0700341 mService.unregisterWallpaperColorsCallback(this, userId);
Lucas Dupinea1fb1e2017-04-05 17:39:44 -0700342 } catch (RemoteException e) {
343 // Failed, service is gone
344 Log.w(TAG, "Can't unregister color updates", e);
345 }
346 }
347 }
348 }
349
350 @Override
Lucas Dupin50ba9912017-07-14 11:55:05 -0700351 public void onWallpaperColorsChanged(WallpaperColors colors, int which, int userId) {
Lucas Dupinea1fb1e2017-04-05 17:39:44 -0700352 synchronized (this) {
353 for (Pair<OnColorsChangedListener, Handler> listener : mColorListeners) {
354 Handler handler = listener.second;
355 if (listener.second == null) {
356 handler = mMainLooperHandler;
357 }
358 handler.post(() -> {
359 // Dealing with race conditions between posting a callback and
360 // removeOnColorsChangedListener being called.
361 boolean stillExists;
362 synchronized (sGlobals) {
363 stillExists = mColorListeners.contains(listener);
364 }
365 if (stillExists) {
Lucas Dupin50ba9912017-07-14 11:55:05 -0700366 listener.first.onColorsChanged(colors, which, userId);
Lucas Dupinea1fb1e2017-04-05 17:39:44 -0700367 }
368 });
369 }
370 }
371 }
372
Lucas Dupin50ba9912017-07-14 11:55:05 -0700373 WallpaperColors getWallpaperColors(int which, int userId) {
Christopher Tatedbf4a9c2017-06-27 12:37:23 -0700374 if (which != FLAG_LOCK && which != FLAG_SYSTEM) {
375 throw new IllegalArgumentException(
376 "Must request colors for exactly one kind of wallpaper");
Lucas Dupinea1fb1e2017-04-05 17:39:44 -0700377 }
Christopher Tatedbf4a9c2017-06-27 12:37:23 -0700378
379 try {
Lucas Dupin50ba9912017-07-14 11:55:05 -0700380 return mService.getWallpaperColors(which, userId);
Christopher Tatedbf4a9c2017-06-27 12:37:23 -0700381 } catch (RemoteException e) {
382 // Can't get colors, connection lost.
383 }
384 return null;
Lucas Dupinea1fb1e2017-04-05 17:39:44 -0700385 }
386
Christopher Tate5d99d472016-05-06 17:59:27 -0700387 public Bitmap peekWallpaperBitmap(Context context, boolean returnDefault,
388 @SetWallpaperFlags int which) {
Lucas Dupin049978e2017-10-02 11:38:13 -0700389 return peekWallpaperBitmap(context, returnDefault, which, context.getUserId(),
390 false /* hardware */);
Yorke Leedcd93cc2016-01-08 14:12:55 -0800391 }
392
Christopher Tate5d99d472016-05-06 17:59:27 -0700393 public Bitmap peekWallpaperBitmap(Context context, boolean returnDefault,
Lucas Dupin049978e2017-10-02 11:38:13 -0700394 @SetWallpaperFlags int which, int userId, boolean hardware) {
Fyodor Kupolovb6378cf2016-07-12 10:32:08 -0700395 if (mService != null) {
396 try {
397 if (!mService.isWallpaperSupported(context.getOpPackageName())) {
398 return null;
Benjamin Franzf3ece362015-02-11 10:51:10 +0000399 }
Fyodor Kupolovb6378cf2016-07-12 10:32:08 -0700400 } catch (RemoteException e) {
401 throw e.rethrowFromSystemServer();
Benjamin Franzf3ece362015-02-11 10:51:10 +0000402 }
Fyodor Kupolovb6378cf2016-07-12 10:32:08 -0700403 }
404 synchronized (this) {
Lucas Dupin61df45c2018-04-07 11:08:15 -0700405 if (mCachedWallpaper != null && mCachedWallpaperUserId == userId
406 && !mCachedWallpaper.isRecycled()) {
Vadim Tryshev93a914a2016-03-07 12:48:51 -0800407 return mCachedWallpaper;
Dianne Hackborn8cc6a502009-08-05 21:29:42 -0700408 }
Vadim Tryshev93a914a2016-03-07 12:48:51 -0800409 mCachedWallpaper = null;
410 mCachedWallpaperUserId = 0;
Dianne Hackbornc9dbbe22009-11-11 22:50:37 -0800411 try {
Lucas Dupin049978e2017-10-02 11:38:13 -0700412 mCachedWallpaper = getCurrentWallpaperLocked(context, userId, hardware);
Vadim Tryshev93a914a2016-03-07 12:48:51 -0800413 mCachedWallpaperUserId = userId;
Dianne Hackbornc9dbbe22009-11-11 22:50:37 -0800414 } catch (OutOfMemoryError e) {
Christopher Tate93252de2017-06-15 14:48:41 -0700415 Log.w(TAG, "Out of memory loading the current wallpaper: " + e);
416 } catch (SecurityException e) {
Jeff Sharkeyaa1a9112018-04-10 15:18:12 -0600417 if (context.getApplicationInfo().targetSdkVersion < Build.VERSION_CODES.O_MR1) {
Christopher Tate93252de2017-06-15 14:48:41 -0700418 Log.w(TAG, "No permission to access wallpaper, suppressing"
419 + " exception to avoid crashing legacy app.");
420 } else {
421 // Post-O apps really most sincerely need the permission.
422 throw e;
423 }
Dianne Hackbornc9dbbe22009-11-11 22:50:37 -0800424 }
Vadim Tryshev93a914a2016-03-07 12:48:51 -0800425 if (mCachedWallpaper != null) {
426 return mCachedWallpaper;
Dianne Hackborn19382ac2009-09-11 21:13:37 -0700427 }
Dianne Hackborn8cc6a502009-08-05 21:29:42 -0700428 }
Fyodor Kupolovb6378cf2016-07-12 10:32:08 -0700429 if (returnDefault) {
430 Bitmap defaultWallpaper = mDefaultWallpaper;
431 if (defaultWallpaper == null) {
432 defaultWallpaper = getDefaultWallpaper(context, which);
433 synchronized (this) {
434 mDefaultWallpaper = defaultWallpaper;
435 }
436 }
437 return defaultWallpaper;
438 }
439 return null;
Dianne Hackborn8cc6a502009-08-05 21:29:42 -0700440 }
Dianne Hackbornba398392011-08-01 16:11:57 -0700441
Fyodor Kupolovb6378cf2016-07-12 10:32:08 -0700442 void forgetLoadedWallpaper() {
Dianne Hackbornba398392011-08-01 16:11:57 -0700443 synchronized (this) {
Vadim Tryshev93a914a2016-03-07 12:48:51 -0800444 mCachedWallpaper = null;
445 mCachedWallpaperUserId = 0;
Dianne Hackbornba398392011-08-01 16:11:57 -0700446 mDefaultWallpaper = null;
447 }
448 }
449
Lucas Dupin049978e2017-10-02 11:38:13 -0700450 private Bitmap getCurrentWallpaperLocked(Context context, int userId, boolean hardware) {
Adam Lesinski2c8d67c2014-04-23 13:46:21 -0700451 if (mService == null) {
452 Log.w(TAG, "WallpaperService not running");
453 return null;
454 }
455
Dianne Hackborn8cc6a502009-08-05 21:29:42 -0700456 try {
Dianne Hackborn284ac932009-08-28 10:34:25 -0700457 Bundle params = new Bundle();
Christopher Tate93252de2017-06-15 14:48:41 -0700458 ParcelFileDescriptor fd = mService.getWallpaper(context.getOpPackageName(),
459 this, FLAG_SYSTEM, params, userId);
Dianne Hackborn8cc6a502009-08-05 21:29:42 -0700460 if (fd != null) {
Jeff Brown24572372011-06-09 19:05:15 -0700461 try {
462 BitmapFactory.Options options = new BitmapFactory.Options();
Lucas Dupin049978e2017-10-02 11:38:13 -0700463 if (hardware) {
464 options.inPreferredConfig = Bitmap.Config.HARDWARE;
465 }
Michael Jurka824a4b52013-12-18 17:10:16 +0100466 return BitmapFactory.decodeFileDescriptor(
Jeff Brown24572372011-06-09 19:05:15 -0700467 fd.getFileDescriptor(), null, options);
Jeff Brown24572372011-06-09 19:05:15 -0700468 } catch (OutOfMemoryError e) {
469 Log.w(TAG, "Can't decode file", e);
470 } finally {
Christopher Tateffa6a882015-12-11 15:08:46 -0800471 IoUtils.closeQuietly(fd);
Dianne Hackborn8cc6a502009-08-05 21:29:42 -0700472 }
Dianne Hackborn19382ac2009-09-11 21:13:37 -0700473 }
474 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -0700475 throw e.rethrowFromSystemServer();
Dianne Hackborn19382ac2009-09-11 21:13:37 -0700476 }
477 return null;
478 }
Christopher Tateffa6a882015-12-11 15:08:46 -0800479
Fyodor Kupolovb6378cf2016-07-12 10:32:08 -0700480 private Bitmap getDefaultWallpaper(Context context, @SetWallpaperFlags int which) {
Christopher Tate5d99d472016-05-06 17:59:27 -0700481 InputStream is = openDefaultWallpaper(context, which);
Adam Lesinski2c8d67c2014-04-23 13:46:21 -0700482 if (is != null) {
483 try {
484 BitmapFactory.Options options = new BitmapFactory.Options();
485 return BitmapFactory.decodeStream(is, null, options);
486 } catch (OutOfMemoryError e) {
487 Log.w(TAG, "Can't decode stream", e);
488 } finally {
Christopher Tateffa6a882015-12-11 15:08:46 -0800489 IoUtils.closeQuietly(is);
Dianne Hackborn8cc6a502009-08-05 21:29:42 -0700490 }
Dianne Hackborn8cc6a502009-08-05 21:29:42 -0700491 }
492 return null;
493 }
494 }
Oleksandr Peletskyif2519812016-01-26 20:16:06 +0100495
Romain Guy407ec782011-08-24 17:06:58 -0700496 private static final Object sSync = new Object[0];
Mathew Inwood4fb17d12018-08-14 14:25:44 +0100497 @UnsupportedAppUsage
Dianne Hackborn8cc6a502009-08-05 21:29:42 -0700498 private static Globals sGlobals;
499
Svet Ganov8f90bcc2017-12-22 23:29:24 -0800500 static void initGlobals(IWallpaperManager service, Looper looper) {
Romain Guy407ec782011-08-24 17:06:58 -0700501 synchronized (sSync) {
Dianne Hackborn8cc6a502009-08-05 21:29:42 -0700502 if (sGlobals == null) {
Svet Ganov8f90bcc2017-12-22 23:29:24 -0800503 sGlobals = new Globals(service, looper);
Dianne Hackborn8cc6a502009-08-05 21:29:42 -0700504 }
Dianne Hackborn8cc6a502009-08-05 21:29:42 -0700505 }
506 }
Oleksandr Peletskyif2519812016-01-26 20:16:06 +0100507
Svet Ganov8f90bcc2017-12-22 23:29:24 -0800508 /*package*/ WallpaperManager(IWallpaperManager service, Context context, Handler handler) {
Dianne Hackborn8cc6a502009-08-05 21:29:42 -0700509 mContext = context;
Svet Ganov8f90bcc2017-12-22 23:29:24 -0800510 initGlobals(service, context.getMainLooper());
Dianne Hackborn8cc6a502009-08-05 21:29:42 -0700511 }
512
513 /**
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700514 * Retrieve a WallpaperManager associated with the given Context.
515 */
516 public static WallpaperManager getInstance(Context context) {
517 return (WallpaperManager)context.getSystemService(
518 Context.WALLPAPER_SERVICE);
519 }
Oleksandr Peletskyif2519812016-01-26 20:16:06 +0100520
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700521 /** @hide */
Mathew Inwood4fb17d12018-08-14 14:25:44 +0100522 @UnsupportedAppUsage
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700523 public IWallpaperManager getIWallpaperManager() {
Dianne Hackborn840c3a22009-09-02 17:35:17 -0700524 return sGlobals.mService;
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700525 }
Oleksandr Peletskyif2519812016-01-26 20:16:06 +0100526
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700527 /**
Scott Main8b2e0002009-09-29 18:17:31 -0700528 * Retrieve the current system wallpaper; if
Michael Jurkab668d0b2013-10-04 15:11:05 -0700529 * no wallpaper is set, the system built-in static wallpaper is returned.
Scott Main8b2e0002009-09-29 18:17:31 -0700530 * This is returned as an
531 * abstract Drawable that you can install in a View to display whatever
Oleksandr Peletskyif2519812016-01-26 20:16:06 +0100532 * wallpaper the user has currently set.
Christopher Tate41dc83bd2016-07-15 15:59:23 -0700533 * <p>
534 * This method can return null if there is no system wallpaper available, if
535 * wallpapers are not supported in the current user, or if the calling app is not
536 * permitted to access the system wallpaper.
Dianne Hackborn8cc6a502009-08-05 21:29:42 -0700537 *
Christopher Tate41dc83bd2016-07-15 15:59:23 -0700538 * @return Returns a Drawable object that will draw the system wallpaper,
539 * or {@code null} if no system wallpaper exists or if the calling application
540 * is not able to access the wallpaper.
Dianne Hackborn8cc6a502009-08-05 21:29:42 -0700541 */
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700542 public Drawable getDrawable() {
Christopher Tate5d99d472016-05-06 17:59:27 -0700543 Bitmap bm = sGlobals.peekWallpaperBitmap(mContext, true, FLAG_SYSTEM);
Dianne Hackborn19382ac2009-09-11 21:13:37 -0700544 if (bm != null) {
545 Drawable dr = new BitmapDrawable(mContext.getResources(), bm);
546 dr.setDither(false);
547 return dr;
548 }
549 return null;
Dianne Hackborn8cc6a502009-08-05 21:29:42 -0700550 }
551
552 /**
Christopher Tate5d99d472016-05-06 17:59:27 -0700553 * Obtain a drawable for the built-in static system wallpaper.
Michael Jurkab668d0b2013-10-04 15:11:05 -0700554 */
555 public Drawable getBuiltInDrawable() {
Christopher Tate5d99d472016-05-06 17:59:27 -0700556 return getBuiltInDrawable(0, 0, false, 0, 0, FLAG_SYSTEM);
557 }
558
559 /**
560 * Obtain a drawable for the specified built-in static system wallpaper.
561 *
562 * @param which The {@code FLAG_*} identifier of a valid wallpaper type. Throws
563 * IllegalArgumentException if an invalid wallpaper is requested.
564 * @return A Drawable presenting the specified wallpaper image, or {@code null}
565 * if no built-in default image for that wallpaper type exists.
566 */
567 public Drawable getBuiltInDrawable(@SetWallpaperFlags int which) {
568 return getBuiltInDrawable(0, 0, false, 0, 0, which);
Michael Jurkab668d0b2013-10-04 15:11:05 -0700569 }
570
571 /**
572 * Returns a drawable for the system built-in static wallpaper. Based on the parameters, the
573 * drawable can be cropped and scaled
574 *
575 * @param outWidth The width of the returned drawable
576 * @param outWidth The height of the returned drawable
577 * @param scaleToFit If true, scale the wallpaper down rather than just cropping it
578 * @param horizontalAlignment A float value between 0 and 1 specifying where to crop the image;
579 * 0 for left-aligned, 0.5 for horizontal center-aligned, and 1 for right-aligned
580 * @param verticalAlignment A float value between 0 and 1 specifying where to crop the image;
581 * 0 for top-aligned, 0.5 for vertical center-aligned, and 1 for bottom-aligned
Christopher Tate5d99d472016-05-06 17:59:27 -0700582 * @return A Drawable presenting the built-in default system wallpaper image,
583 * or {@code null} if no such default image is defined on this device.
Michael Jurkab668d0b2013-10-04 15:11:05 -0700584 */
585 public Drawable getBuiltInDrawable(int outWidth, int outHeight,
586 boolean scaleToFit, float horizontalAlignment, float verticalAlignment) {
Christopher Tate5d99d472016-05-06 17:59:27 -0700587 return getBuiltInDrawable(outWidth, outHeight, scaleToFit,
588 horizontalAlignment, verticalAlignment, FLAG_SYSTEM);
589 }
590
591 /**
592 * Returns a drawable for the built-in static wallpaper of the specified type. Based on the
593 * parameters, the drawable can be cropped and scaled.
594 *
595 * @param outWidth The width of the returned drawable
596 * @param outWidth The height of the returned drawable
597 * @param scaleToFit If true, scale the wallpaper down rather than just cropping it
598 * @param horizontalAlignment A float value between 0 and 1 specifying where to crop the image;
599 * 0 for left-aligned, 0.5 for horizontal center-aligned, and 1 for right-aligned
600 * @param verticalAlignment A float value between 0 and 1 specifying where to crop the image;
601 * 0 for top-aligned, 0.5 for vertical center-aligned, and 1 for bottom-aligned
602 * @param which The {@code FLAG_*} identifier of a valid wallpaper type. Throws
603 * IllegalArgumentException if an invalid wallpaper is requested.
604 * @return A Drawable presenting the built-in default wallpaper image of the given type,
605 * or {@code null} if no default image of that type is defined on this device.
606 */
607 public Drawable getBuiltInDrawable(int outWidth, int outHeight, boolean scaleToFit,
608 float horizontalAlignment, float verticalAlignment, @SetWallpaperFlags int which) {
Michael Jurkab668d0b2013-10-04 15:11:05 -0700609 if (sGlobals.mService == null) {
610 Log.w(TAG, "WallpaperService not running");
Christopher Tate98d609c2016-05-18 17:31:58 -0700611 throw new RuntimeException(new DeadSystemException());
Michael Jurkab668d0b2013-10-04 15:11:05 -0700612 }
Christopher Tate5d99d472016-05-06 17:59:27 -0700613
614 if (which != FLAG_SYSTEM && which != FLAG_LOCK) {
615 throw new IllegalArgumentException("Must request exactly one kind of wallpaper");
616 }
617
Michael Jurkab668d0b2013-10-04 15:11:05 -0700618 Resources resources = mContext.getResources();
619 horizontalAlignment = Math.max(0, Math.min(1, horizontalAlignment));
620 verticalAlignment = Math.max(0, Math.min(1, verticalAlignment));
621
Christopher Tate5d99d472016-05-06 17:59:27 -0700622 InputStream wpStream = openDefaultWallpaper(mContext, which);
623 if (wpStream == null) {
624 if (DEBUG) {
625 Log.w(TAG, "default wallpaper stream " + which + " is null");
626 }
Michael Jurkab668d0b2013-10-04 15:11:05 -0700627 return null;
628 } else {
Christopher Tate5d99d472016-05-06 17:59:27 -0700629 InputStream is = new BufferedInputStream(wpStream);
Michael Jurkab668d0b2013-10-04 15:11:05 -0700630 if (outWidth <= 0 || outHeight <= 0) {
631 Bitmap fullSize = BitmapFactory.decodeStream(is, null, null);
632 return new BitmapDrawable(resources, fullSize);
633 } else {
634 int inWidth;
635 int inHeight;
Christopher Tate5d99d472016-05-06 17:59:27 -0700636 // Just measure this time through...
Michael Jurkab668d0b2013-10-04 15:11:05 -0700637 {
638 BitmapFactory.Options options = new BitmapFactory.Options();
639 options.inJustDecodeBounds = true;
640 BitmapFactory.decodeStream(is, null, options);
641 if (options.outWidth != 0 && options.outHeight != 0) {
642 inWidth = options.outWidth;
643 inHeight = options.outHeight;
644 } else {
645 Log.e(TAG, "default wallpaper dimensions are 0");
646 return null;
647 }
648 }
649
Christopher Tate5d99d472016-05-06 17:59:27 -0700650 // Reopen the stream to do the full decode. We know at this point
651 // that openDefaultWallpaper() will return non-null.
652 is = new BufferedInputStream(openDefaultWallpaper(mContext, which));
Michael Jurkab668d0b2013-10-04 15:11:05 -0700653
654 RectF cropRectF;
655
656 outWidth = Math.min(inWidth, outWidth);
657 outHeight = Math.min(inHeight, outHeight);
658 if (scaleToFit) {
659 cropRectF = getMaxCropRect(inWidth, inHeight, outWidth, outHeight,
660 horizontalAlignment, verticalAlignment);
661 } else {
662 float left = (inWidth - outWidth) * horizontalAlignment;
663 float right = left + outWidth;
664 float top = (inHeight - outHeight) * verticalAlignment;
665 float bottom = top + outHeight;
Michael Jurka05391532013-10-14 20:44:42 -0700666 cropRectF = new RectF(left, top, right, bottom);
Michael Jurkab668d0b2013-10-04 15:11:05 -0700667 }
668 Rect roundedTrueCrop = new Rect();
669 cropRectF.roundOut(roundedTrueCrop);
670
671 if (roundedTrueCrop.width() <= 0 || roundedTrueCrop.height() <= 0) {
672 Log.w(TAG, "crop has bad values for full size image");
673 return null;
674 }
675
676 // See how much we're reducing the size of the image
677 int scaleDownSampleSize = Math.min(roundedTrueCrop.width() / outWidth,
678 roundedTrueCrop.height() / outHeight);
679
680 // Attempt to open a region decoder
681 BitmapRegionDecoder decoder = null;
682 try {
683 decoder = BitmapRegionDecoder.newInstance(is, true);
684 } catch (IOException e) {
685 Log.w(TAG, "cannot open region decoder for default wallpaper");
686 }
687
688 Bitmap crop = null;
689 if (decoder != null) {
690 // Do region decoding to get crop bitmap
691 BitmapFactory.Options options = new BitmapFactory.Options();
692 if (scaleDownSampleSize > 1) {
693 options.inSampleSize = scaleDownSampleSize;
694 }
695 crop = decoder.decodeRegion(roundedTrueCrop, options);
696 decoder.recycle();
697 }
698
699 if (crop == null) {
Christopher Tate5d99d472016-05-06 17:59:27 -0700700 // BitmapRegionDecoder has failed, try to crop in-memory. We know at
701 // this point that openDefaultWallpaper() will return non-null.
702 is = new BufferedInputStream(openDefaultWallpaper(mContext, which));
Michael Jurkab668d0b2013-10-04 15:11:05 -0700703 Bitmap fullSize = null;
Christopher Tate5d99d472016-05-06 17:59:27 -0700704 BitmapFactory.Options options = new BitmapFactory.Options();
705 if (scaleDownSampleSize > 1) {
706 options.inSampleSize = scaleDownSampleSize;
Michael Jurkab668d0b2013-10-04 15:11:05 -0700707 }
Christopher Tate5d99d472016-05-06 17:59:27 -0700708 fullSize = BitmapFactory.decodeStream(is, null, options);
Michael Jurkab668d0b2013-10-04 15:11:05 -0700709 if (fullSize != null) {
710 crop = Bitmap.createBitmap(fullSize, roundedTrueCrop.left,
711 roundedTrueCrop.top, roundedTrueCrop.width(),
712 roundedTrueCrop.height());
713 }
714 }
715
716 if (crop == null) {
717 Log.w(TAG, "cannot decode default wallpaper");
718 return null;
719 }
720
721 // Scale down if necessary
722 if (outWidth > 0 && outHeight > 0 &&
723 (crop.getWidth() != outWidth || crop.getHeight() != outHeight)) {
724 Matrix m = new Matrix();
725 RectF cropRect = new RectF(0, 0, crop.getWidth(), crop.getHeight());
726 RectF returnRect = new RectF(0, 0, outWidth, outHeight);
727 m.setRectToRect(cropRect, returnRect, Matrix.ScaleToFit.FILL);
728 Bitmap tmp = Bitmap.createBitmap((int) returnRect.width(),
729 (int) returnRect.height(), Bitmap.Config.ARGB_8888);
730 if (tmp != null) {
731 Canvas c = new Canvas(tmp);
732 Paint p = new Paint();
733 p.setFilterBitmap(true);
734 c.drawBitmap(crop, m, p);
735 crop = tmp;
736 }
737 }
738
739 return new BitmapDrawable(resources, crop);
740 }
741 }
742 }
743
744 private static RectF getMaxCropRect(int inWidth, int inHeight, int outWidth, int outHeight,
745 float horizontalAlignment, float verticalAlignment) {
746 RectF cropRect = new RectF();
747 // Get a crop rect that will fit this
748 if (inWidth / (float) inHeight > outWidth / (float) outHeight) {
749 cropRect.top = 0;
750 cropRect.bottom = inHeight;
751 float cropWidth = outWidth * (inHeight / (float) outHeight);
752 cropRect.left = (inWidth - cropWidth) * horizontalAlignment;
753 cropRect.right = cropRect.left + cropWidth;
754 } else {
755 cropRect.left = 0;
756 cropRect.right = inWidth;
757 float cropHeight = outHeight * (inWidth / (float) outWidth);
758 cropRect.top = (inHeight - cropHeight) * verticalAlignment;
759 cropRect.bottom = cropRect.top + cropHeight;
760 }
761 return cropRect;
762 }
763
764 /**
Scott Main8b2e0002009-09-29 18:17:31 -0700765 * Retrieve the current system wallpaper; if there is no wallpaper set,
766 * a null pointer is returned. This is returned as an
Dianne Hackborn8cc6a502009-08-05 21:29:42 -0700767 * abstract Drawable that you can install in a View to display whatever
Oleksandr Peletskyif2519812016-01-26 20:16:06 +0100768 * wallpaper the user has currently set.
Dianne Hackborn8cc6a502009-08-05 21:29:42 -0700769 *
770 * @return Returns a Drawable object that will draw the wallpaper or a
771 * null pointer if these is none.
772 */
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700773 public Drawable peekDrawable() {
Christopher Tate5d99d472016-05-06 17:59:27 -0700774 Bitmap bm = sGlobals.peekWallpaperBitmap(mContext, false, FLAG_SYSTEM);
Dianne Hackborn19382ac2009-09-11 21:13:37 -0700775 if (bm != null) {
776 Drawable dr = new BitmapDrawable(mContext.getResources(), bm);
777 dr.setDither(false);
778 return dr;
779 }
780 return null;
781 }
782
783 /**
Scott Main8b2e0002009-09-29 18:17:31 -0700784 * Like {@link #getDrawable()}, but the returned Drawable has a number
785 * of limitations to reduce its overhead as much as possible. It will
786 * never scale the wallpaper (only centering it if the requested bounds
787 * do match the bitmap bounds, which should not be typical), doesn't
788 * allow setting an alpha, color filter, or other attributes, etc. The
789 * bounds of the returned drawable will be initialized to the same bounds
790 * as the wallpaper, so normally you will not need to touch it. The
791 * drawable also assumes that it will be used in a context running in
792 * the same density as the screen (not in density compatibility mode).
Dianne Hackborn19382ac2009-09-11 21:13:37 -0700793 *
794 * @return Returns a Drawable object that will draw the wallpaper.
795 */
Christopher Tate93252de2017-06-15 14:48:41 -0700796 @RequiresPermission(android.Manifest.permission.READ_EXTERNAL_STORAGE)
Dianne Hackborn19382ac2009-09-11 21:13:37 -0700797 public Drawable getFastDrawable() {
Christopher Tate5d99d472016-05-06 17:59:27 -0700798 Bitmap bm = sGlobals.peekWallpaperBitmap(mContext, true, FLAG_SYSTEM);
Dianne Hackborn19382ac2009-09-11 21:13:37 -0700799 if (bm != null) {
Romain Guy407ec782011-08-24 17:06:58 -0700800 return new FastBitmapDrawable(bm);
Dianne Hackborn19382ac2009-09-11 21:13:37 -0700801 }
802 return null;
803 }
804
805 /**
Scott Main8b2e0002009-09-29 18:17:31 -0700806 * Like {@link #getFastDrawable()}, but if there is no wallpaper set,
807 * a null pointer is returned.
Dianne Hackborn19382ac2009-09-11 21:13:37 -0700808 *
809 * @return Returns an optimized Drawable object that will draw the
810 * wallpaper or a null pointer if these is none.
811 */
Christopher Tate93252de2017-06-15 14:48:41 -0700812 @RequiresPermission(android.Manifest.permission.READ_EXTERNAL_STORAGE)
Dianne Hackborn19382ac2009-09-11 21:13:37 -0700813 public Drawable peekFastDrawable() {
Christopher Tate5d99d472016-05-06 17:59:27 -0700814 Bitmap bm = sGlobals.peekWallpaperBitmap(mContext, false, FLAG_SYSTEM);
Dianne Hackborn19382ac2009-09-11 21:13:37 -0700815 if (bm != null) {
Romain Guy407ec782011-08-24 17:06:58 -0700816 return new FastBitmapDrawable(bm);
Dianne Hackborn19382ac2009-09-11 21:13:37 -0700817 }
818 return null;
Dianne Hackborn8cc6a502009-08-05 21:29:42 -0700819 }
820
821 /**
Lucas Dupin049978e2017-10-02 11:38:13 -0700822 * Like {@link #getDrawable()} but returns a Bitmap with default {@link Bitmap.Config}.
Oleksandr Peletskyif2519812016-01-26 20:16:06 +0100823 *
Romain Guy407ec782011-08-24 17:06:58 -0700824 * @hide
825 */
Mathew Inwood4fb17d12018-08-14 14:25:44 +0100826 @UnsupportedAppUsage
Romain Guy407ec782011-08-24 17:06:58 -0700827 public Bitmap getBitmap() {
Lucas Dupin049978e2017-10-02 11:38:13 -0700828 return getBitmap(false);
829 }
830
831 /**
832 * Like {@link #getDrawable()} but returns a Bitmap.
833 *
834 * @param hardware Asks for a hardware backed bitmap.
835 * @see Bitmap.Config#HARDWARE
836 * @hide
837 */
Mathew Inwood4fb17d12018-08-14 14:25:44 +0100838 @UnsupportedAppUsage
Lucas Dupin049978e2017-10-02 11:38:13 -0700839 public Bitmap getBitmap(boolean hardware) {
840 return getBitmapAsUser(mContext.getUserId(), hardware);
Yorke Leedcd93cc2016-01-08 14:12:55 -0800841 }
842
843 /**
844 * Like {@link #getDrawable()} but returns a Bitmap for the provided user.
845 *
846 * @hide
847 */
Lucas Dupin049978e2017-10-02 11:38:13 -0700848 public Bitmap getBitmapAsUser(int userId, boolean hardware) {
849 return sGlobals.peekWallpaperBitmap(mContext, true, FLAG_SYSTEM, userId, hardware);
Romain Guy407ec782011-08-24 17:06:58 -0700850 }
851
852 /**
Christopher Tatebe132e62016-02-10 12:59:49 -0800853 * Get an open, readable file descriptor to the given wallpaper image file.
Christopher Tateedf7d042016-03-29 18:24:25 -0700854 * The caller is responsible for closing the file descriptor when done ingesting the file.
Christopher Tatebe132e62016-02-10 12:59:49 -0800855 *
856 * <p>If no lock-specific wallpaper has been configured for the given user, then
Christopher Tateedf7d042016-03-29 18:24:25 -0700857 * this method will return {@code null} when requesting {@link #FLAG_LOCK} rather than
Christopher Tatebe132e62016-02-10 12:59:49 -0800858 * returning the system wallpaper's image file.
Christopher Tateedf7d042016-03-29 18:24:25 -0700859 *
860 * @param which The wallpaper whose image file is to be retrieved. Must be a single
861 * defined kind of wallpaper, either {@link #FLAG_SYSTEM} or
862 * {@link #FLAG_LOCK}.
Christopher Tate93252de2017-06-15 14:48:41 -0700863 * @return An open, readable file desriptor to the requested wallpaper image file;
864 * or {@code null} if no such wallpaper is configured or if the calling app does
865 * not have permission to read the current wallpaper.
Christopher Tateedf7d042016-03-29 18:24:25 -0700866 *
867 * @see #FLAG_LOCK
868 * @see #FLAG_SYSTEM
Christopher Tatebe132e62016-02-10 12:59:49 -0800869 */
Christopher Tate93252de2017-06-15 14:48:41 -0700870 @RequiresPermission(android.Manifest.permission.READ_EXTERNAL_STORAGE)
Christopher Tate5d99d472016-05-06 17:59:27 -0700871 public ParcelFileDescriptor getWallpaperFile(@SetWallpaperFlags int which) {
Christopher Tatebe132e62016-02-10 12:59:49 -0800872 return getWallpaperFile(which, mContext.getUserId());
873 }
874
Lucas Dupinc40608c2017-04-14 18:33:08 -0700875 /**
876 * Registers a listener to get notified when the wallpaper colors change.
Lucas Dupinc40608c2017-04-14 18:33:08 -0700877 * @param listener A listener to register
Lucas Dupinea1fb1e2017-04-05 17:39:44 -0700878 * @param handler Where to call it from. Will be called from the main thread
Lucas Dupinc40608c2017-04-14 18:33:08 -0700879 * if null.
880 */
881 public void addOnColorsChangedListener(@NonNull OnColorsChangedListener listener,
Lucas Dupinea1fb1e2017-04-05 17:39:44 -0700882 @NonNull Handler handler) {
Lucas Dupin50ba9912017-07-14 11:55:05 -0700883 addOnColorsChangedListener(listener, handler, mContext.getUserId());
884 }
885
886 /**
887 * Registers a listener to get notified when the wallpaper colors change
888 * @param listener A listener to register
889 * @param handler Where to call it from. Will be called from the main thread
890 * if null.
891 * @param userId Owner of the wallpaper or UserHandle.USER_ALL.
892 * @hide
893 */
Mathew Inwood4fb17d12018-08-14 14:25:44 +0100894 @UnsupportedAppUsage
Lucas Dupin50ba9912017-07-14 11:55:05 -0700895 public void addOnColorsChangedListener(@NonNull OnColorsChangedListener listener,
896 @NonNull Handler handler, int userId) {
897 sGlobals.addOnColorsChangedListener(listener, handler, userId);
Lucas Dupinc40608c2017-04-14 18:33:08 -0700898 }
899
900 /**
901 * Stop listening to color updates.
Lucas Dupin50ba9912017-07-14 11:55:05 -0700902 * @param callback A callback to unsubscribe.
Lucas Dupinc40608c2017-04-14 18:33:08 -0700903 */
904 public void removeOnColorsChangedListener(@NonNull OnColorsChangedListener callback) {
Lucas Dupin50ba9912017-07-14 11:55:05 -0700905 removeOnColorsChangedListener(callback, mContext.getUserId());
906 }
907
908 /**
909 * Stop listening to color updates.
910 * @param callback A callback to unsubscribe.
911 * @param userId Owner of the wallpaper or UserHandle.USER_ALL.
912 * @hide
913 */
914 public void removeOnColorsChangedListener(@NonNull OnColorsChangedListener callback,
915 int userId) {
916 sGlobals.removeOnColorsChangedListener(callback, userId);
Lucas Dupinc40608c2017-04-14 18:33:08 -0700917 }
918
919 /**
Lucas Dupineeb36be2017-08-04 10:45:12 -0700920 * Get the primary colors of a wallpaper.
921 *
Christopher Tate105540d2018-03-21 13:03:09 -0700922 * <p>This method can return {@code null} when:
923 * <ul>
924 * <li>Colors are still being processed by the system.</li>
925 * <li>The user has chosen to use a live wallpaper: live wallpapers might not
926 * implement
927 * {@link android.service.wallpaper.WallpaperService.Engine#onComputeColors()
928 * WallpaperService.Engine#onComputeColors()}.</li>
929 * </ul>
Lucas Dupineeb36be2017-08-04 10:45:12 -0700930 *
931 * @param which Wallpaper type. Must be either {@link #FLAG_SYSTEM} or
932 * {@link #FLAG_LOCK}.
933 * @return Current {@link WallpaperColors} or null if colors are unknown.
934 * @see #addOnColorsChangedListener(OnColorsChangedListener, Handler)
Lucas Dupinc40608c2017-04-14 18:33:08 -0700935 */
936 public @Nullable WallpaperColors getWallpaperColors(int which) {
Lucas Dupin50ba9912017-07-14 11:55:05 -0700937 return getWallpaperColors(which, mContext.getUserId());
938 }
939
940 /**
Christopher Tate105540d2018-03-21 13:03:09 -0700941 * Get the primary colors of the wallpaper configured in the given user.
Lucas Dupin50ba9912017-07-14 11:55:05 -0700942 * @param which wallpaper type. Must be either {@link #FLAG_SYSTEM} or
943 * {@link #FLAG_LOCK}
944 * @param userId Owner of the wallpaper.
945 * @return {@link WallpaperColors} or null if colors are unknown.
946 * @hide
947 */
Mathew Inwood4fb17d12018-08-14 14:25:44 +0100948 @UnsupportedAppUsage
Lucas Dupin50ba9912017-07-14 11:55:05 -0700949 public @Nullable WallpaperColors getWallpaperColors(int which, int userId) {
950 return sGlobals.getWallpaperColors(which, userId);
Lucas Dupinc40608c2017-04-14 18:33:08 -0700951 }
952
Christopher Tatebe132e62016-02-10 12:59:49 -0800953 /**
954 * Version of {@link #getWallpaperFile(int)} that can access the wallpaper data
955 * for a given user. The caller must hold the INTERACT_ACROSS_USERS_FULL
956 * permission to access another user's wallpaper data.
Christopher Tateedf7d042016-03-29 18:24:25 -0700957 *
958 * @param which The wallpaper whose image file is to be retrieved. Must be a single
959 * defined kind of wallpaper, either {@link #FLAG_SYSTEM} or
960 * {@link #FLAG_LOCK}.
961 * @param userId The user or profile whose imagery is to be retrieved
962 *
963 * @see #FLAG_LOCK
964 * @see #FLAG_SYSTEM
965 *
Christopher Tatebe132e62016-02-10 12:59:49 -0800966 * @hide
967 */
Mathew Inwood4fb17d12018-08-14 14:25:44 +0100968 @UnsupportedAppUsage
Christopher Tate5d99d472016-05-06 17:59:27 -0700969 public ParcelFileDescriptor getWallpaperFile(@SetWallpaperFlags int which, int userId) {
Christopher Tateedf7d042016-03-29 18:24:25 -0700970 if (which != FLAG_SYSTEM && which != FLAG_LOCK) {
Christopher Tatebe132e62016-02-10 12:59:49 -0800971 throw new IllegalArgumentException("Must request exactly one kind of wallpaper");
972 }
973
974 if (sGlobals.mService == null) {
975 Log.w(TAG, "WallpaperService not running");
Christopher Tate98d609c2016-05-18 17:31:58 -0700976 throw new RuntimeException(new DeadSystemException());
Christopher Tatebe132e62016-02-10 12:59:49 -0800977 } else {
978 try {
979 Bundle outParams = new Bundle();
Christopher Tate93252de2017-06-15 14:48:41 -0700980 return sGlobals.mService.getWallpaper(mContext.getOpPackageName(), null, which,
981 outParams, userId);
Christopher Tatebe132e62016-02-10 12:59:49 -0800982 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -0700983 throw e.rethrowFromSystemServer();
Christopher Tate93252de2017-06-15 14:48:41 -0700984 } catch (SecurityException e) {
Jeff Sharkeyaa1a9112018-04-10 15:18:12 -0600985 if (mContext.getApplicationInfo().targetSdkVersion < Build.VERSION_CODES.O_MR1) {
Christopher Tate93252de2017-06-15 14:48:41 -0700986 Log.w(TAG, "No permission to access wallpaper, suppressing"
987 + " exception to avoid crashing legacy app.");
988 return null;
989 } else {
990 throw e;
991 }
Christopher Tatebe132e62016-02-10 12:59:49 -0800992 }
993 }
994 }
995
996 /**
Dianne Hackbornba398392011-08-01 16:11:57 -0700997 * Remove all internal references to the last loaded wallpaper. Useful
998 * for apps that want to reduce memory usage when they only temporarily
999 * need to have the wallpaper. After calling, the next request for the
1000 * wallpaper will require reloading it again from disk.
1001 */
1002 public void forgetLoadedWallpaper() {
Christopher Tatebe132e62016-02-10 12:59:49 -08001003 sGlobals.forgetLoadedWallpaper();
Dianne Hackbornba398392011-08-01 16:11:57 -07001004 }
1005
1006 /**
Dianne Hackborneb034652009-09-07 00:49:58 -07001007 * If the current wallpaper is a live wallpaper component, return the
1008 * information about that wallpaper. Otherwise, if it is a static image,
1009 * simply return null.
Dianne Hackborneb034652009-09-07 00:49:58 -07001010 */
1011 public WallpaperInfo getWallpaperInfo() {
1012 try {
Mike Lockwoodc067c9c2011-10-31 12:50:12 -04001013 if (sGlobals.mService == null) {
1014 Log.w(TAG, "WallpaperService not running");
Christopher Tate98d609c2016-05-18 17:31:58 -07001015 throw new RuntimeException(new DeadSystemException());
Mike Lockwoodc067c9c2011-10-31 12:50:12 -04001016 } else {
Jeff Sharkeyad357d12018-02-02 13:25:31 -07001017 return sGlobals.mService.getWallpaperInfo(mContext.getUserId());
Mike Lockwoodc067c9c2011-10-31 12:50:12 -04001018 }
Dianne Hackborneb034652009-09-07 00:49:58 -07001019 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07001020 throw e.rethrowFromSystemServer();
Dianne Hackborneb034652009-09-07 00:49:58 -07001021 }
1022 }
Michael Jurkae8d1bf72013-09-09 15:58:54 +02001023
1024 /**
Christopher Tatee409f0e2016-03-21 14:53:15 -07001025 * Get the ID of the current wallpaper of the given kind. If there is no
1026 * such wallpaper configured, returns a negative number.
1027 *
Christopher Tate98d609c2016-05-18 17:31:58 -07001028 * <p>Every time the wallpaper image is set, a new ID is assigned to it.
1029 * This method allows the caller to determine whether the wallpaper imagery
1030 * has changed, regardless of how that change happened.
1031 *
Christopher Tatee409f0e2016-03-21 14:53:15 -07001032 * @param which The wallpaper whose ID is to be returned. Must be a single
Christopher Tateedf7d042016-03-29 18:24:25 -07001033 * defined kind of wallpaper, either {@link #FLAG_SYSTEM} or
1034 * {@link #FLAG_LOCK}.
Christopher Tatee409f0e2016-03-21 14:53:15 -07001035 * @return The positive numeric ID of the current wallpaper of the given kind,
1036 * or a negative value if no such wallpaper is configured.
1037 */
Christopher Tate5d99d472016-05-06 17:59:27 -07001038 public int getWallpaperId(@SetWallpaperFlags int which) {
Christopher Tatee409f0e2016-03-21 14:53:15 -07001039 return getWallpaperIdForUser(which, mContext.getUserId());
1040 }
1041
1042 /**
1043 * Get the ID of the given user's current wallpaper of the given kind. If there
1044 * is no such wallpaper configured, returns a negative number.
1045 * @hide
1046 */
Christopher Tate5d99d472016-05-06 17:59:27 -07001047 public int getWallpaperIdForUser(@SetWallpaperFlags int which, int userId) {
Christopher Tatee409f0e2016-03-21 14:53:15 -07001048 try {
1049 if (sGlobals.mService == null) {
1050 Log.w(TAG, "WallpaperService not running");
Christopher Tate98d609c2016-05-18 17:31:58 -07001051 throw new RuntimeException(new DeadSystemException());
Christopher Tatee409f0e2016-03-21 14:53:15 -07001052 } else {
1053 return sGlobals.mService.getWallpaperIdForUser(which, userId);
1054 }
1055 } catch (RemoteException e) {
1056 throw e.rethrowFromSystemServer();
1057 }
1058 }
1059
1060 /**
Michael Jurkae8d1bf72013-09-09 15:58:54 +02001061 * Gets an Intent that will launch an activity that crops the given
1062 * image and sets the device's wallpaper. If there is a default HOME activity
1063 * that supports cropping wallpapers, it will be preferred as the default.
Ying Wang930d4e52013-09-14 11:57:17 -07001064 * Use this method instead of directly creating a {@link #ACTION_CROP_AND_SET_WALLPAPER}
Michael Jurkae8d1bf72013-09-09 15:58:54 +02001065 * intent.
Adam Lesinskibba72d12013-10-11 18:10:56 -07001066 *
1067 * @param imageUri The image URI that will be set in the intent. The must be a content
1068 * URI and its provider must resolve its type to "image/*"
1069 *
1070 * @throws IllegalArgumentException if the URI is not a content URI or its MIME type is
1071 * not "image/*"
Michael Jurkae8d1bf72013-09-09 15:58:54 +02001072 */
1073 public Intent getCropAndSetWallpaperIntent(Uri imageUri) {
Allen Shen1c0f21e2014-04-29 16:16:29 +08001074 if (imageUri == null) {
1075 throw new IllegalArgumentException("Image URI must not be null");
1076 }
1077
Adam Lesinskibba72d12013-10-11 18:10:56 -07001078 if (!ContentResolver.SCHEME_CONTENT.equals(imageUri.getScheme())) {
1079 throw new IllegalArgumentException("Image URI must be of the "
1080 + ContentResolver.SCHEME_CONTENT + " scheme type");
1081 }
1082
Michael Jurkae8d1bf72013-09-09 15:58:54 +02001083 final PackageManager packageManager = mContext.getPackageManager();
1084 Intent cropAndSetWallpaperIntent =
1085 new Intent(ACTION_CROP_AND_SET_WALLPAPER, imageUri);
1086 cropAndSetWallpaperIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
1087
1088 // Find out if the default HOME activity supports CROP_AND_SET_WALLPAPER
1089 Intent homeIntent = new Intent(Intent.ACTION_MAIN).addCategory(Intent.CATEGORY_HOME);
1090 ResolveInfo resolvedHome = packageManager.resolveActivity(homeIntent,
1091 PackageManager.MATCH_DEFAULT_ONLY);
1092 if (resolvedHome != null) {
1093 cropAndSetWallpaperIntent.setPackage(resolvedHome.activityInfo.packageName);
1094
1095 List<ResolveInfo> cropAppList = packageManager.queryIntentActivities(
1096 cropAndSetWallpaperIntent, 0);
1097 if (cropAppList.size() > 0) {
1098 return cropAndSetWallpaperIntent;
1099 }
1100 }
1101
1102 // fallback crop activity
Oren Blasberg60598ea0a2016-02-26 11:01:23 -08001103 final String cropperPackage = mContext.getString(
1104 com.android.internal.R.string.config_wallpaperCropperPackage);
1105 cropAndSetWallpaperIntent.setPackage(cropperPackage);
Adam Lesinskibba72d12013-10-11 18:10:56 -07001106 List<ResolveInfo> cropAppList = packageManager.queryIntentActivities(
1107 cropAndSetWallpaperIntent, 0);
1108 if (cropAppList.size() > 0) {
1109 return cropAndSetWallpaperIntent;
1110 }
1111 // If the URI is not of the right type, or for some reason the system wallpaper
1112 // cropper doesn't exist, return null
1113 throw new IllegalArgumentException("Cannot use passed URI to set wallpaper; " +
1114 "check that the type returned by ContentProvider matches image/*");
Michael Jurkae8d1bf72013-09-09 15:58:54 +02001115 }
1116
Dianne Hackborneb034652009-09-07 00:49:58 -07001117 /**
Dianne Hackborn8cc6a502009-08-05 21:29:42 -07001118 * Change the current system wallpaper to the bitmap in the given resource.
1119 * The resource is opened as a raw data stream and copied into the
1120 * wallpaper; it must be a valid PNG or JPEG image. On success, the intent
1121 * {@link Intent#ACTION_WALLPAPER_CHANGED} is broadcast.
1122 *
Nicolas Falliere9530e3a2012-06-18 17:21:06 -07001123 * <p>This method requires the caller to hold the permission
1124 * {@link android.Manifest.permission#SET_WALLPAPER}.
1125 *
Christopher Tateedf7d042016-03-29 18:24:25 -07001126 * @param resid The resource ID of the bitmap to be used as the wallpaper image
Dianne Hackborn8cc6a502009-08-05 21:29:42 -07001127 *
Michael Jurkab668d0b2013-10-04 15:11:05 -07001128 * @throws IOException If an error occurs reverting to the built-in
Dianne Hackborn8cc6a502009-08-05 21:29:42 -07001129 * wallpaper.
1130 */
Christopher Tate93252de2017-06-15 14:48:41 -07001131 @RequiresPermission(android.Manifest.permission.SET_WALLPAPER)
Tor Norbye7b9c9122013-05-30 16:48:33 -07001132 public void setResource(@RawRes int resid) throws IOException {
Edward Savage-Jones6009d9d2016-08-30 18:44:19 +02001133 setResource(resid, FLAG_SYSTEM | FLAG_LOCK);
Christopher Tatead3c2592016-01-20 18:13:17 -08001134 }
1135
1136 /**
Christopher Tateedf7d042016-03-29 18:24:25 -07001137 * Version of {@link #setResource(int)} that allows the caller to specify which
1138 * of the supported wallpaper categories to set.
Christopher Tatead3c2592016-01-20 18:13:17 -08001139 *
Christopher Tateedf7d042016-03-29 18:24:25 -07001140 * @param resid The resource ID of the bitmap to be used as the wallpaper image
1141 * @param which Flags indicating which wallpaper(s) to configure with the new imagery
Christopher Tatead3c2592016-01-20 18:13:17 -08001142 *
Christopher Tateedf7d042016-03-29 18:24:25 -07001143 * @see #FLAG_LOCK
1144 * @see #FLAG_SYSTEM
Christopher Tatead3c2592016-01-20 18:13:17 -08001145 *
1146 * @return An integer ID assigned to the newly active wallpaper; or zero on failure.
1147 *
1148 * @throws IOException
1149 */
Christopher Tate93252de2017-06-15 14:48:41 -07001150 @RequiresPermission(android.Manifest.permission.SET_WALLPAPER)
Christopher Tatead3c2592016-01-20 18:13:17 -08001151 public int setResource(@RawRes int resid, @SetWallpaperFlags int which)
1152 throws IOException {
Mike Lockwoodc067c9c2011-10-31 12:50:12 -04001153 if (sGlobals.mService == null) {
1154 Log.w(TAG, "WallpaperService not running");
Christopher Tate98d609c2016-05-18 17:31:58 -07001155 throw new RuntimeException(new DeadSystemException());
Mike Lockwoodc067c9c2011-10-31 12:50:12 -04001156 }
Christopher Tatead3c2592016-01-20 18:13:17 -08001157 final Bundle result = new Bundle();
Christopher Tate1e1e2e02016-01-25 15:34:36 -08001158 final WallpaperSetCompletion completion = new WallpaperSetCompletion();
Dianne Hackborn8cc6a502009-08-05 21:29:42 -07001159 try {
1160 Resources resources = mContext.getResources();
1161 /* Set the wallpaper to the default values */
Dianne Hackborn840c3a22009-09-02 17:35:17 -07001162 ParcelFileDescriptor fd = sGlobals.mService.setWallpaper(
Christopher Tatead3c2592016-01-20 18:13:17 -08001163 "res:" + resources.getResourceName(resid),
Jorim Jaggi6c902d02016-08-18 10:44:54 -07001164 mContext.getOpPackageName(), null, false, result, which, completion,
Jeff Sharkeyad357d12018-02-02 13:25:31 -07001165 mContext.getUserId());
Dianne Hackborn8cc6a502009-08-05 21:29:42 -07001166 if (fd != null) {
1167 FileOutputStream fos = null;
Christopher Tate1e1e2e02016-01-25 15:34:36 -08001168 boolean ok = false;
Dianne Hackborn8cc6a502009-08-05 21:29:42 -07001169 try {
1170 fos = new ParcelFileDescriptor.AutoCloseOutputStream(fd);
Christopher Tatead3c2592016-01-20 18:13:17 -08001171 copyStreamToWallpaperFile(resources.openRawResource(resid), fos);
Christopher Tate1e1e2e02016-01-25 15:34:36 -08001172 // The 'close()' is the trigger for any server-side image manipulation,
1173 // so we must do that before waiting for completion.
1174 fos.close();
1175 completion.waitForCompletion();
Dianne Hackborn8cc6a502009-08-05 21:29:42 -07001176 } finally {
Christopher Tate1e1e2e02016-01-25 15:34:36 -08001177 // Might be redundant but completion shouldn't wait unless the write
1178 // succeeded; this is a fallback if it threw past the close+wait.
Christopher Tateffa6a882015-12-11 15:08:46 -08001179 IoUtils.closeQuietly(fos);
Dianne Hackborn8cc6a502009-08-05 21:29:42 -07001180 }
1181 }
1182 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07001183 throw e.rethrowFromSystemServer();
Dianne Hackborn8cc6a502009-08-05 21:29:42 -07001184 }
Christopher Tatead3c2592016-01-20 18:13:17 -08001185 return result.getInt(EXTRA_NEW_WALLPAPER_ID, 0);
Dianne Hackborn8cc6a502009-08-05 21:29:42 -07001186 }
Christopher Tateffa6a882015-12-11 15:08:46 -08001187
Dianne Hackborn8cc6a502009-08-05 21:29:42 -07001188 /**
1189 * Change the current system wallpaper to a bitmap. The given bitmap is
1190 * converted to a PNG and stored as the wallpaper. On success, the intent
1191 * {@link Intent#ACTION_WALLPAPER_CHANGED} is broadcast.
1192 *
Christopher Tateffa6a882015-12-11 15:08:46 -08001193 * <p>This method is equivalent to calling
1194 * {@link #setBitmap(Bitmap, Rect, boolean)} and passing {@code null} for the
1195 * {@code visibleCrop} rectangle and {@code true} for the {@code allowBackup}
1196 * parameter.
1197 *
Nicolas Falliere9530e3a2012-06-18 17:21:06 -07001198 * <p>This method requires the caller to hold the permission
1199 * {@link android.Manifest.permission#SET_WALLPAPER}.
1200 *
Christopher Tatead3c2592016-01-20 18:13:17 -08001201 * @param bitmap The bitmap to be used as the new system wallpaper.
Dianne Hackborn8cc6a502009-08-05 21:29:42 -07001202 *
Christopher Tateffa6a882015-12-11 15:08:46 -08001203 * @throws IOException If an error occurs when attempting to set the wallpaper
1204 * to the provided image.
Dianne Hackborn8cc6a502009-08-05 21:29:42 -07001205 */
Christopher Tate93252de2017-06-15 14:48:41 -07001206 @RequiresPermission(android.Manifest.permission.SET_WALLPAPER)
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001207 public void setBitmap(Bitmap bitmap) throws IOException {
Christopher Tateffa6a882015-12-11 15:08:46 -08001208 setBitmap(bitmap, null, true);
1209 }
1210
1211 /**
1212 * Change the current system wallpaper to a bitmap, specifying a hint about
1213 * which subrectangle of the full image is to be visible. The OS will then
1214 * try to best present the given portion of the full image as the static system
1215 * wallpaper image. On success, the intent
1216 * {@link Intent#ACTION_WALLPAPER_CHANGED} is broadcast.
1217 *
1218 * <p>Passing {@code null} as the {@code visibleHint} parameter is equivalent to
1219 * passing (0, 0, {@code fullImage.getWidth()}, {@code fullImage.getHeight()}).
1220 *
1221 * <p>This method requires the caller to hold the permission
1222 * {@link android.Manifest.permission#SET_WALLPAPER}.
1223 *
Christopher Tatead3c2592016-01-20 18:13:17 -08001224 * @param fullImage A bitmap that will supply the wallpaper imagery.
Christopher Tateffa6a882015-12-11 15:08:46 -08001225 * @param visibleCropHint The rectangular subregion of {@code fullImage} that should be
1226 * displayed as wallpaper. Passing {@code null} for this parameter means that
1227 * the full image should be displayed if possible given the image's and device's
Christopher Tatead3c2592016-01-20 18:13:17 -08001228 * aspect ratios, etc.
Christopher Tateffa6a882015-12-11 15:08:46 -08001229 * @param allowBackup {@code true} if the OS is permitted to back up this wallpaper
1230 * image for restore to a future device; {@code false} otherwise.
1231 *
Christopher Tatead3c2592016-01-20 18:13:17 -08001232 * @return An integer ID assigned to the newly active wallpaper; or zero on failure.
1233 *
Christopher Tateffa6a882015-12-11 15:08:46 -08001234 * @throws IOException If an error occurs when attempting to set the wallpaper
1235 * to the provided image.
1236 * @throws IllegalArgumentException If the {@code visibleCropHint} rectangle is
1237 * empty or invalid.
1238 */
Christopher Tate93252de2017-06-15 14:48:41 -07001239 @RequiresPermission(android.Manifest.permission.SET_WALLPAPER)
Christopher Tatead3c2592016-01-20 18:13:17 -08001240 public int setBitmap(Bitmap fullImage, Rect visibleCropHint, boolean allowBackup)
1241 throws IOException {
Edward Savage-Jones6009d9d2016-08-30 18:44:19 +02001242 return setBitmap(fullImage, visibleCropHint, allowBackup, FLAG_SYSTEM | FLAG_LOCK);
Christopher Tatead3c2592016-01-20 18:13:17 -08001243 }
1244
1245 /**
Christopher Tatead3c2592016-01-20 18:13:17 -08001246 * Version of {@link #setBitmap(Bitmap, Rect, boolean)} that allows the caller
1247 * to specify which of the supported wallpaper categories to set.
1248 *
1249 * @param fullImage A bitmap that will supply the wallpaper imagery.
1250 * @param visibleCropHint The rectangular subregion of {@code fullImage} that should be
1251 * displayed as wallpaper. Passing {@code null} for this parameter means that
1252 * the full image should be displayed if possible given the image's and device's
1253 * aspect ratios, etc.
1254 * @param allowBackup {@code true} if the OS is permitted to back up this wallpaper
1255 * image for restore to a future device; {@code false} otherwise.
1256 * @param which Flags indicating which wallpaper(s) to configure with the new imagery.
1257 *
Christopher Tateedf7d042016-03-29 18:24:25 -07001258 * @see #FLAG_LOCK
1259 * @see #FLAG_SYSTEM
Christopher Tatead3c2592016-01-20 18:13:17 -08001260 *
1261 * @return An integer ID assigned to the newly active wallpaper; or zero on failure.
1262 *
1263 * @throws IOException
1264 */
Christopher Tate93252de2017-06-15 14:48:41 -07001265 @RequiresPermission(android.Manifest.permission.SET_WALLPAPER)
Christopher Tatead3c2592016-01-20 18:13:17 -08001266 public int setBitmap(Bitmap fullImage, Rect visibleCropHint,
1267 boolean allowBackup, @SetWallpaperFlags int which)
Christopher Tateffa6a882015-12-11 15:08:46 -08001268 throws IOException {
Jorim Jaggi6c902d02016-08-18 10:44:54 -07001269 return setBitmap(fullImage, visibleCropHint, allowBackup, which,
Jeff Sharkeyad357d12018-02-02 13:25:31 -07001270 mContext.getUserId());
Jorim Jaggi6c902d02016-08-18 10:44:54 -07001271 }
1272
1273 /**
1274 * Like {@link #setBitmap(Bitmap, Rect, boolean, int)}, but allows to pass in an explicit user
1275 * id. If the user id doesn't match the user id the process is running under, calling this
1276 * requires permission {@link android.Manifest.permission#INTERACT_ACROSS_USERS_FULL}.
1277 * @hide
1278 */
Mathew Inwood45d2c252018-09-14 12:35:36 +01001279 @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P, trackingBug = 115609023)
Jorim Jaggi6c902d02016-08-18 10:44:54 -07001280 public int setBitmap(Bitmap fullImage, Rect visibleCropHint,
1281 boolean allowBackup, @SetWallpaperFlags int which, int userId)
1282 throws IOException {
Christopher Tateffa6a882015-12-11 15:08:46 -08001283 validateRect(visibleCropHint);
Mike Lockwoodc067c9c2011-10-31 12:50:12 -04001284 if (sGlobals.mService == null) {
1285 Log.w(TAG, "WallpaperService not running");
Christopher Tate98d609c2016-05-18 17:31:58 -07001286 throw new RuntimeException(new DeadSystemException());
Mike Lockwoodc067c9c2011-10-31 12:50:12 -04001287 }
Christopher Tatead3c2592016-01-20 18:13:17 -08001288 final Bundle result = new Bundle();
Christopher Tate1e1e2e02016-01-25 15:34:36 -08001289 final WallpaperSetCompletion completion = new WallpaperSetCompletion();
Dianne Hackborn8cc6a502009-08-05 21:29:42 -07001290 try {
Benjamin Franzf3ece362015-02-11 10:51:10 +00001291 ParcelFileDescriptor fd = sGlobals.mService.setWallpaper(null,
Christopher Tated7faf532016-02-25 12:43:38 -08001292 mContext.getOpPackageName(), visibleCropHint, allowBackup,
Jorim Jaggi6c902d02016-08-18 10:44:54 -07001293 result, which, completion, userId);
Christopher Tatead3c2592016-01-20 18:13:17 -08001294 if (fd != null) {
1295 FileOutputStream fos = null;
1296 try {
1297 fos = new ParcelFileDescriptor.AutoCloseOutputStream(fd);
1298 fullImage.compress(Bitmap.CompressFormat.PNG, 90, fos);
Christopher Tate1e1e2e02016-01-25 15:34:36 -08001299 fos.close();
1300 completion.waitForCompletion();
Christopher Tatead3c2592016-01-20 18:13:17 -08001301 } finally {
1302 IoUtils.closeQuietly(fos);
1303 }
Dianne Hackborn8cc6a502009-08-05 21:29:42 -07001304 }
1305 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07001306 throw e.rethrowFromSystemServer();
Dianne Hackborn8cc6a502009-08-05 21:29:42 -07001307 }
Christopher Tatead3c2592016-01-20 18:13:17 -08001308 return result.getInt(EXTRA_NEW_WALLPAPER_ID, 0);
Dianne Hackborn8cc6a502009-08-05 21:29:42 -07001309 }
1310
Christopher Tateffa6a882015-12-11 15:08:46 -08001311 private final void validateRect(Rect rect) {
1312 if (rect != null && rect.isEmpty()) {
1313 throw new IllegalArgumentException("visibleCrop rectangle must be valid and non-empty");
1314 }
1315 }
1316
Dianne Hackborn8cc6a502009-08-05 21:29:42 -07001317 /**
1318 * Change the current system wallpaper to a specific byte stream. The
1319 * give InputStream is copied into persistent storage and will now be
1320 * used as the wallpaper. Currently it must be either a JPEG or PNG
1321 * image. On success, the intent {@link Intent#ACTION_WALLPAPER_CHANGED}
1322 * is broadcast.
1323 *
Christopher Tateffa6a882015-12-11 15:08:46 -08001324 * <p>This method is equivalent to calling
1325 * {@link #setStream(InputStream, Rect, boolean)} and passing {@code null} for the
1326 * {@code visibleCrop} rectangle and {@code true} for the {@code allowBackup}
1327 * parameter.
1328 *
Nicolas Falliere9530e3a2012-06-18 17:21:06 -07001329 * <p>This method requires the caller to hold the permission
1330 * {@link android.Manifest.permission#SET_WALLPAPER}.
1331 *
Christopher Tate98d609c2016-05-18 17:31:58 -07001332 * @param bitmapData A stream containing the raw data to install as a wallpaper. This
1333 * data can be in any format handled by {@link BitmapRegionDecoder}.
Dianne Hackborn8cc6a502009-08-05 21:29:42 -07001334 *
Christopher Tateffa6a882015-12-11 15:08:46 -08001335 * @throws IOException If an error occurs when attempting to set the wallpaper
1336 * based on the provided image data.
Dianne Hackborn8cc6a502009-08-05 21:29:42 -07001337 */
Christopher Tate93252de2017-06-15 14:48:41 -07001338 @RequiresPermission(android.Manifest.permission.SET_WALLPAPER)
Christopher Tateffa6a882015-12-11 15:08:46 -08001339 public void setStream(InputStream bitmapData) throws IOException {
1340 setStream(bitmapData, null, true);
1341 }
1342
Christopher Tatead3c2592016-01-20 18:13:17 -08001343 private void copyStreamToWallpaperFile(InputStream data, FileOutputStream fos)
Christopher Tateffa6a882015-12-11 15:08:46 -08001344 throws IOException {
Jeff Sharkey45c97df2018-02-01 16:01:52 -07001345 FileUtils.copy(data, fos);
Christopher Tateffa6a882015-12-11 15:08:46 -08001346 }
1347
1348 /**
1349 * Change the current system wallpaper to a specific byte stream, specifying a
1350 * hint about which subrectangle of the full image is to be visible. The OS will
1351 * then try to best present the given portion of the full image as the static system
1352 * wallpaper image. The data from the given InputStream is copied into persistent
1353 * storage and will then be used as the system wallpaper. Currently the data must
1354 * be either a JPEG or PNG image. On success, the intent
1355 * {@link Intent#ACTION_WALLPAPER_CHANGED} is broadcast.
1356 *
1357 * <p>This method requires the caller to hold the permission
1358 * {@link android.Manifest.permission#SET_WALLPAPER}.
1359 *
Christopher Tate98d609c2016-05-18 17:31:58 -07001360 * @param bitmapData A stream containing the raw data to install as a wallpaper. This
1361 * data can be in any format handled by {@link BitmapRegionDecoder}.
Christopher Tateffa6a882015-12-11 15:08:46 -08001362 * @param visibleCropHint The rectangular subregion of the streamed image that should be
1363 * displayed as wallpaper. Passing {@code null} for this parameter means that
1364 * the full image should be displayed if possible given the image's and device's
1365 * aspect ratios, etc.
1366 * @param allowBackup {@code true} if the OS is permitted to back up this wallpaper
1367 * image for restore to a future device; {@code false} otherwise.
Christopher Tate98d609c2016-05-18 17:31:58 -07001368 * @return An integer ID assigned to the newly active wallpaper; or zero on failure.
1369 *
1370 * @see #getWallpaperId(int)
Christopher Tateffa6a882015-12-11 15:08:46 -08001371 *
1372 * @throws IOException If an error occurs when attempting to set the wallpaper
1373 * based on the provided image data.
1374 * @throws IllegalArgumentException If the {@code visibleCropHint} rectangle is
1375 * empty or invalid.
1376 */
Christopher Tate93252de2017-06-15 14:48:41 -07001377 @RequiresPermission(android.Manifest.permission.SET_WALLPAPER)
Christopher Tatead3c2592016-01-20 18:13:17 -08001378 public int setStream(InputStream bitmapData, Rect visibleCropHint, boolean allowBackup)
Christopher Tateffa6a882015-12-11 15:08:46 -08001379 throws IOException {
Edward Savage-Jones6009d9d2016-08-30 18:44:19 +02001380 return setStream(bitmapData, visibleCropHint, allowBackup, FLAG_SYSTEM | FLAG_LOCK);
Christopher Tatead3c2592016-01-20 18:13:17 -08001381 }
1382
1383 /**
1384 * Version of {@link #setStream(InputStream, Rect, boolean)} that allows the caller
1385 * to specify which of the supported wallpaper categories to set.
1386 *
Christopher Tate98d609c2016-05-18 17:31:58 -07001387 * @param bitmapData A stream containing the raw data to install as a wallpaper. This
1388 * data can be in any format handled by {@link BitmapRegionDecoder}.
Christopher Tatead3c2592016-01-20 18:13:17 -08001389 * @param visibleCropHint The rectangular subregion of the streamed image that should be
1390 * displayed as wallpaper. Passing {@code null} for this parameter means that
1391 * the full image should be displayed if possible given the image's and device's
1392 * aspect ratios, etc.
1393 * @param allowBackup {@code true} if the OS is permitted to back up this wallpaper
1394 * image for restore to a future device; {@code false} otherwise.
1395 * @param which Flags indicating which wallpaper(s) to configure with the new imagery.
Christopher Tate98d609c2016-05-18 17:31:58 -07001396 * @return An integer ID assigned to the newly active wallpaper; or zero on failure.
Christopher Tatead3c2592016-01-20 18:13:17 -08001397 *
Christopher Tate98d609c2016-05-18 17:31:58 -07001398 * @see #getWallpaperId(int)
Christopher Tateedf7d042016-03-29 18:24:25 -07001399 * @see #FLAG_LOCK
1400 * @see #FLAG_SYSTEM
Christopher Tatead3c2592016-01-20 18:13:17 -08001401 *
1402 * @throws IOException
1403 */
Christopher Tate93252de2017-06-15 14:48:41 -07001404 @RequiresPermission(android.Manifest.permission.SET_WALLPAPER)
Christopher Tatead3c2592016-01-20 18:13:17 -08001405 public int setStream(InputStream bitmapData, Rect visibleCropHint,
1406 boolean allowBackup, @SetWallpaperFlags int which)
1407 throws IOException {
Christopher Tateffa6a882015-12-11 15:08:46 -08001408 validateRect(visibleCropHint);
Mike Lockwoodc067c9c2011-10-31 12:50:12 -04001409 if (sGlobals.mService == null) {
1410 Log.w(TAG, "WallpaperService not running");
Christopher Tate98d609c2016-05-18 17:31:58 -07001411 throw new RuntimeException(new DeadSystemException());
Mike Lockwoodc067c9c2011-10-31 12:50:12 -04001412 }
Christopher Tatead3c2592016-01-20 18:13:17 -08001413 final Bundle result = new Bundle();
Christopher Tate1e1e2e02016-01-25 15:34:36 -08001414 final WallpaperSetCompletion completion = new WallpaperSetCompletion();
Dianne Hackborn8cc6a502009-08-05 21:29:42 -07001415 try {
Benjamin Franzf3ece362015-02-11 10:51:10 +00001416 ParcelFileDescriptor fd = sGlobals.mService.setWallpaper(null,
Christopher Tated7faf532016-02-25 12:43:38 -08001417 mContext.getOpPackageName(), visibleCropHint, allowBackup,
Jeff Sharkeyad357d12018-02-02 13:25:31 -07001418 result, which, completion, mContext.getUserId());
Christopher Tatead3c2592016-01-20 18:13:17 -08001419 if (fd != null) {
1420 FileOutputStream fos = null;
1421 try {
1422 fos = new ParcelFileDescriptor.AutoCloseOutputStream(fd);
1423 copyStreamToWallpaperFile(bitmapData, fos);
Christopher Tate1e1e2e02016-01-25 15:34:36 -08001424 fos.close();
1425 completion.waitForCompletion();
Christopher Tatead3c2592016-01-20 18:13:17 -08001426 } finally {
1427 IoUtils.closeQuietly(fos);
1428 }
Dianne Hackborn8cc6a502009-08-05 21:29:42 -07001429 }
1430 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07001431 throw e.rethrowFromSystemServer();
Dianne Hackborn8cc6a502009-08-05 21:29:42 -07001432 }
Christopher Tatead3c2592016-01-20 18:13:17 -08001433
1434 return result.getInt(EXTRA_NEW_WALLPAPER_ID, 0);
Dianne Hackborn8cc6a502009-08-05 21:29:42 -07001435 }
1436
Dianne Hackborn8cc6a502009-08-05 21:29:42 -07001437 /**
Dianne Hackborn5dc5a002012-09-15 19:33:48 -07001438 * Return whether any users are currently set to use the wallpaper
1439 * with the given resource ID. That is, their wallpaper has been
1440 * set through {@link #setResource(int)} with the same resource id.
1441 */
Tor Norbye7b9c9122013-05-30 16:48:33 -07001442 public boolean hasResourceWallpaper(@RawRes int resid) {
Dianne Hackborn5dc5a002012-09-15 19:33:48 -07001443 if (sGlobals.mService == null) {
1444 Log.w(TAG, "WallpaperService not running");
Christopher Tate98d609c2016-05-18 17:31:58 -07001445 throw new RuntimeException(new DeadSystemException());
Dianne Hackborn5dc5a002012-09-15 19:33:48 -07001446 }
1447 try {
1448 Resources resources = mContext.getResources();
1449 String name = "res:" + resources.getResourceName(resid);
1450 return sGlobals.mService.hasNamedWallpaper(name);
1451 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07001452 throw e.rethrowFromSystemServer();
Dianne Hackborn5dc5a002012-09-15 19:33:48 -07001453 }
1454 }
1455
1456 /**
Dianne Hackborn8cc6a502009-08-05 21:29:42 -07001457 * Returns the desired minimum width for the wallpaper. Callers of
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001458 * {@link #setBitmap(android.graphics.Bitmap)} or
1459 * {@link #setStream(java.io.InputStream)} should check this value
Dianne Hackborn8cc6a502009-08-05 21:29:42 -07001460 * beforehand to make sure the supplied wallpaper respects the desired
1461 * minimum width.
1462 *
1463 * If the returned value is <= 0, the caller should use the width of
1464 * the default display instead.
1465 *
1466 * @return The desired minimum width for the wallpaper. This value should
1467 * be honored by applications that set the wallpaper but it is not
1468 * mandatory.
1469 */
1470 public int getDesiredMinimumWidth() {
Mike Lockwoodc067c9c2011-10-31 12:50:12 -04001471 if (sGlobals.mService == null) {
1472 Log.w(TAG, "WallpaperService not running");
Christopher Tate98d609c2016-05-18 17:31:58 -07001473 throw new RuntimeException(new DeadSystemException());
Mike Lockwoodc067c9c2011-10-31 12:50:12 -04001474 }
Dianne Hackborn8cc6a502009-08-05 21:29:42 -07001475 try {
Dianne Hackborn840c3a22009-09-02 17:35:17 -07001476 return sGlobals.mService.getWidthHint();
Dianne Hackborn8cc6a502009-08-05 21:29:42 -07001477 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07001478 throw e.rethrowFromSystemServer();
Dianne Hackborn8cc6a502009-08-05 21:29:42 -07001479 }
1480 }
1481
1482 /**
1483 * Returns the desired minimum height for the wallpaper. Callers of
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001484 * {@link #setBitmap(android.graphics.Bitmap)} or
1485 * {@link #setStream(java.io.InputStream)} should check this value
Dianne Hackborn8cc6a502009-08-05 21:29:42 -07001486 * beforehand to make sure the supplied wallpaper respects the desired
1487 * minimum height.
1488 *
1489 * If the returned value is <= 0, the caller should use the height of
1490 * the default display instead.
1491 *
1492 * @return The desired minimum height for the wallpaper. This value should
1493 * be honored by applications that set the wallpaper but it is not
1494 * mandatory.
1495 */
1496 public int getDesiredMinimumHeight() {
Mike Lockwoodc067c9c2011-10-31 12:50:12 -04001497 if (sGlobals.mService == null) {
1498 Log.w(TAG, "WallpaperService not running");
Christopher Tate98d609c2016-05-18 17:31:58 -07001499 throw new RuntimeException(new DeadSystemException());
Mike Lockwoodc067c9c2011-10-31 12:50:12 -04001500 }
Dianne Hackborn8cc6a502009-08-05 21:29:42 -07001501 try {
Dianne Hackborn840c3a22009-09-02 17:35:17 -07001502 return sGlobals.mService.getHeightHint();
Dianne Hackborn8cc6a502009-08-05 21:29:42 -07001503 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07001504 throw e.rethrowFromSystemServer();
Dianne Hackborn8cc6a502009-08-05 21:29:42 -07001505 }
1506 }
1507
1508 /**
1509 * For use only by the current home application, to specify the size of
1510 * wallpaper it would like to use. This allows such applications to have
1511 * a virtual wallpaper that is larger than the physical screen, matching
1512 * the size of their workspace.
Dianne Hackbornc5bf7582012-04-25 19:12:07 -07001513 *
1514 * <p>Note developers, who don't seem to be reading this. This is
Christopher Tateffa6a882015-12-11 15:08:46 -08001515 * for <em>home apps</em> to tell what size wallpaper they would like.
1516 * Nobody else should be calling this! Certainly not other non-home
Dianne Hackbornc5bf7582012-04-25 19:12:07 -07001517 * apps that change the wallpaper. Those apps are supposed to
1518 * <b>retrieve</b> the suggested size so they can construct a wallpaper
1519 * that matches it.
1520 *
Nicolas Falliere9530e3a2012-06-18 17:21:06 -07001521 * <p>This method requires the caller to hold the permission
1522 * {@link android.Manifest.permission#SET_WALLPAPER_HINTS}.
1523 *
Dianne Hackborn8cc6a502009-08-05 21:29:42 -07001524 * @param minimumWidth Desired minimum width
1525 * @param minimumHeight Desired minimum height
1526 */
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001527 public void suggestDesiredDimensions(int minimumWidth, int minimumHeight) {
Dianne Hackborn8cc6a502009-08-05 21:29:42 -07001528 try {
Donghan Ryu289c2732011-11-14 18:56:11 -08001529 /**
1530 * The framework makes no attempt to limit the window size
1531 * to the maximum texture size. Any window larger than this
1532 * cannot be composited.
1533 *
1534 * Read maximum texture size from system property and scale down
1535 * minimumWidth and minimumHeight accordingly.
1536 */
1537 int maximumTextureSize;
1538 try {
1539 maximumTextureSize = SystemProperties.getInt("sys.max_texture_size", 0);
1540 } catch (Exception e) {
1541 maximumTextureSize = 0;
1542 }
1543
1544 if (maximumTextureSize > 0) {
1545 if ((minimumWidth > maximumTextureSize) ||
1546 (minimumHeight > maximumTextureSize)) {
1547 float aspect = (float)minimumHeight / (float)minimumWidth;
1548 if (minimumWidth > minimumHeight) {
1549 minimumWidth = maximumTextureSize;
1550 minimumHeight = (int)((minimumWidth * aspect) + 0.5);
1551 } else {
1552 minimumHeight = maximumTextureSize;
1553 minimumWidth = (int)((minimumHeight / aspect) + 0.5);
1554 }
1555 }
1556 }
1557
Mike Lockwoodc067c9c2011-10-31 12:50:12 -04001558 if (sGlobals.mService == null) {
1559 Log.w(TAG, "WallpaperService not running");
Christopher Tate98d609c2016-05-18 17:31:58 -07001560 throw new RuntimeException(new DeadSystemException());
Mike Lockwoodc067c9c2011-10-31 12:50:12 -04001561 } else {
Benjamin Franzf3ece362015-02-11 10:51:10 +00001562 sGlobals.mService.setDimensionHints(minimumWidth, minimumHeight,
1563 mContext.getOpPackageName());
Mike Lockwoodc067c9c2011-10-31 12:50:12 -04001564 }
Dianne Hackborn8cc6a502009-08-05 21:29:42 -07001565 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07001566 throw e.rethrowFromSystemServer();
Dianne Hackborn8cc6a502009-08-05 21:29:42 -07001567 }
1568 }
Adam Cohen041a0ba2011-11-09 20:10:27 -08001569
Dianne Hackborn8cc6a502009-08-05 21:29:42 -07001570 /**
Dianne Hackborn067e5f62014-09-07 23:14:30 -07001571 * Specify extra padding that the wallpaper should have outside of the display.
1572 * That is, the given padding supplies additional pixels the wallpaper should extend
1573 * outside of the display itself.
Christopher Tate105540d2018-03-21 13:03:09 -07001574 *
1575 * <p>This method requires the caller to hold the permission
1576 * {@link android.Manifest.permission#SET_WALLPAPER_HINTS}.
1577 *
Dianne Hackborn067e5f62014-09-07 23:14:30 -07001578 * @param padding The number of pixels the wallpaper should extend beyond the display,
1579 * on its left, top, right, and bottom sides.
Dianne Hackborn067e5f62014-09-07 23:14:30 -07001580 */
Jeff Sharkeyd86b8fe2017-06-02 17:36:26 -06001581 @RequiresPermission(android.Manifest.permission.SET_WALLPAPER_HINTS)
Dianne Hackborn067e5f62014-09-07 23:14:30 -07001582 public void setDisplayPadding(Rect padding) {
1583 try {
1584 if (sGlobals.mService == null) {
1585 Log.w(TAG, "WallpaperService not running");
Christopher Tate98d609c2016-05-18 17:31:58 -07001586 throw new RuntimeException(new DeadSystemException());
Dianne Hackborn067e5f62014-09-07 23:14:30 -07001587 } else {
Benjamin Franzf3ece362015-02-11 10:51:10 +00001588 sGlobals.mService.setDisplayPadding(padding, mContext.getOpPackageName());
Dianne Hackborn067e5f62014-09-07 23:14:30 -07001589 }
1590 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07001591 throw e.rethrowFromSystemServer();
Dianne Hackborn067e5f62014-09-07 23:14:30 -07001592 }
1593 }
1594
1595 /**
1596 * Apply a raw offset to the wallpaper window. Should only be used in
1597 * combination with {@link #setDisplayPadding(android.graphics.Rect)} when you
1598 * have ensured that the wallpaper will extend outside of the display area so that
1599 * it can be moved without leaving part of the display uncovered.
1600 * @param x The offset, in pixels, to apply to the left edge.
1601 * @param y The offset, in pixels, to apply to the top edge.
1602 * @hide
1603 */
1604 @SystemApi
1605 public void setDisplayOffset(IBinder windowToken, int x, int y) {
1606 try {
1607 //Log.v(TAG, "Sending new wallpaper display offsets from app...");
1608 WindowManagerGlobal.getWindowSession().setWallpaperDisplayOffset(
1609 windowToken, x, y);
1610 //Log.v(TAG, "...app returning after sending display offset!");
1611 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07001612 throw e.rethrowFromSystemServer();
Dianne Hackborn067e5f62014-09-07 23:14:30 -07001613 }
1614 }
1615
1616 /**
Christopher Tate105540d2018-03-21 13:03:09 -07001617 * Reset all wallpaper to the factory default.
Filip Gruszczynski9fedc722015-01-28 15:59:46 -08001618 *
Christopher Tate105540d2018-03-21 13:03:09 -07001619 * <p>This method requires the caller to hold the permission
1620 * {@link android.Manifest.permission#SET_WALLPAPER}.
Filip Gruszczynski9fedc722015-01-28 15:59:46 -08001621 */
Jeff Sharkeyd86b8fe2017-06-02 17:36:26 -06001622 @RequiresPermission(android.Manifest.permission.SET_WALLPAPER)
Filip Gruszczynski9fedc722015-01-28 15:59:46 -08001623 public void clearWallpaper() {
Christopher Tatea2bd5122016-09-22 13:18:05 -07001624 clearWallpaper(FLAG_LOCK, mContext.getUserId());
1625 clearWallpaper(FLAG_SYSTEM, mContext.getUserId());
Christopher Tatebe132e62016-02-10 12:59:49 -08001626 }
1627
1628 /**
1629 * Clear the wallpaper for a specific user. The caller must hold the
1630 * INTERACT_ACROSS_USERS_FULL permission to clear another user's
Christopher Tate11620ed2017-08-28 14:16:22 -07001631 * wallpaper, and must hold the SET_WALLPAPER permission in all
1632 * circumstances.
Christopher Tatebe132e62016-02-10 12:59:49 -08001633 * @hide
1634 */
1635 @SystemApi
Christopher Tate11620ed2017-08-28 14:16:22 -07001636 @RequiresPermission(android.Manifest.permission.INTERACT_ACROSS_USERS_FULL)
Christopher Tate5d99d472016-05-06 17:59:27 -07001637 public void clearWallpaper(@SetWallpaperFlags int which, int userId) {
Filip Gruszczynski9fedc722015-01-28 15:59:46 -08001638 if (sGlobals.mService == null) {
1639 Log.w(TAG, "WallpaperService not running");
Christopher Tate98d609c2016-05-18 17:31:58 -07001640 throw new RuntimeException(new DeadSystemException());
Filip Gruszczynski9fedc722015-01-28 15:59:46 -08001641 }
1642 try {
Christopher Tatebe132e62016-02-10 12:59:49 -08001643 sGlobals.mService.clearWallpaper(mContext.getOpPackageName(), which, userId);
Filip Gruszczynski9fedc722015-01-28 15:59:46 -08001644 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07001645 throw e.rethrowFromSystemServer();
Filip Gruszczynski9fedc722015-01-28 15:59:46 -08001646 }
1647 }
1648
1649 /**
1650 * Set the live wallpaper.
1651 *
Filip Gruszczynski9fedc722015-01-28 15:59:46 -08001652 * @hide
1653 */
1654 @SystemApi
Jeff Sharkeyd86b8fe2017-06-02 17:36:26 -06001655 @RequiresPermission(android.Manifest.permission.SET_WALLPAPER_COMPONENT)
Filip Gruszczynski9fedc722015-01-28 15:59:46 -08001656 public boolean setWallpaperComponent(ComponentName name) {
Jeff Sharkeyad357d12018-02-02 13:25:31 -07001657 return setWallpaperComponent(name, mContext.getUserId());
Adrian Roos40ea0832016-07-14 14:19:55 -07001658 }
1659
1660 /**
1661 * Set the live wallpaper.
1662 *
1663 * This can only be called by packages with android.permission.SET_WALLPAPER_COMPONENT
1664 * permission. The caller must hold the INTERACT_ACROSS_USERS_FULL permission to change
1665 * another user's wallpaper.
1666 *
1667 * @hide
1668 */
Christopher Tate93252de2017-06-15 14:48:41 -07001669 @RequiresPermission(android.Manifest.permission.SET_WALLPAPER_COMPONENT)
Mathew Inwood4fb17d12018-08-14 14:25:44 +01001670 @UnsupportedAppUsage
Adrian Roos40ea0832016-07-14 14:19:55 -07001671 public boolean setWallpaperComponent(ComponentName name, int userId) {
Filip Gruszczynski9fedc722015-01-28 15:59:46 -08001672 if (sGlobals.mService == null) {
1673 Log.w(TAG, "WallpaperService not running");
Christopher Tate98d609c2016-05-18 17:31:58 -07001674 throw new RuntimeException(new DeadSystemException());
Filip Gruszczynski9fedc722015-01-28 15:59:46 -08001675 }
1676 try {
Adrian Roos40ea0832016-07-14 14:19:55 -07001677 sGlobals.mService.setWallpaperComponentChecked(name, mContext.getOpPackageName(),
1678 userId);
Filip Gruszczynski9fedc722015-01-28 15:59:46 -08001679 return true;
1680 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07001681 throw e.rethrowFromSystemServer();
Filip Gruszczynski9fedc722015-01-28 15:59:46 -08001682 }
Filip Gruszczynski9fedc722015-01-28 15:59:46 -08001683 }
1684
1685 /**
Christopher Tateffa6a882015-12-11 15:08:46 -08001686 * Set the display position of the current wallpaper within any larger space, when
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07001687 * that wallpaper is visible behind the given window. The X and Y offsets
1688 * are floating point numbers ranging from 0 to 1, representing where the
1689 * wallpaper should be positioned within the screen space. These only
Christopher Tateffa6a882015-12-11 15:08:46 -08001690 * make sense when the wallpaper is larger than the display.
Oleksandr Peletskyif2519812016-01-26 20:16:06 +01001691 *
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07001692 * @param windowToken The window who these offsets should be associated
Scott Main8b2e0002009-09-29 18:17:31 -07001693 * with, as returned by {@link android.view.View#getWindowToken()
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07001694 * View.getWindowToken()}.
Marco Nelissenbf6956b2009-11-09 15:21:13 -08001695 * @param xOffset The offset along the X dimension, from 0 to 1.
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07001696 * @param yOffset The offset along the Y dimension, from 0 to 1.
1697 */
1698 public void setWallpaperOffsets(IBinder windowToken, float xOffset, float yOffset) {
Adam Cohen791a6332012-01-12 14:38:38 -08001699 try {
1700 //Log.v(TAG, "Sending new wallpaper offsets from app...");
Jeff Brownf9e989d2013-04-04 23:04:03 -07001701 WindowManagerGlobal.getWindowSession().setWallpaperPosition(
Adam Cohen791a6332012-01-12 14:38:38 -08001702 windowToken, xOffset, yOffset, mWallpaperXStep, mWallpaperYStep);
1703 //Log.v(TAG, "...app returning after sending offsets!");
1704 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07001705 throw e.rethrowFromSystemServer();
Adam Cohen791a6332012-01-12 14:38:38 -08001706 }
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07001707 }
Adam Cohen041a0ba2011-11-09 20:10:27 -08001708
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07001709 /**
Marco Nelissenbf6956b2009-11-09 15:21:13 -08001710 * For applications that use multiple virtual screens showing a wallpaper,
1711 * specify the step size between virtual screens. For example, if the
Dianne Hackborn23ef7b42009-11-18 18:20:39 -08001712 * launcher has 3 virtual screens, it would specify an xStep of 0.5,
Marco Nelissenbf6956b2009-11-09 15:21:13 -08001713 * since the X offset for those screens are 0.0, 0.5 and 1.0
Oleksandr Peletskyif2519812016-01-26 20:16:06 +01001714 * @param xStep The X offset delta from one screen to the next one
Marco Nelissenbf6956b2009-11-09 15:21:13 -08001715 * @param yStep The Y offset delta from one screen to the next one
1716 */
1717 public void setWallpaperOffsetSteps(float xStep, float yStep) {
1718 mWallpaperXStep = xStep;
1719 mWallpaperYStep = yStep;
1720 }
Oleksandr Peletskyif2519812016-01-26 20:16:06 +01001721
Marco Nelissenbf6956b2009-11-09 15:21:13 -08001722 /**
Dianne Hackborn75804932009-10-20 20:15:20 -07001723 * Send an arbitrary command to the current active wallpaper.
Oleksandr Peletskyif2519812016-01-26 20:16:06 +01001724 *
Dianne Hackborn75804932009-10-20 20:15:20 -07001725 * @param windowToken The window who these offsets should be associated
1726 * with, as returned by {@link android.view.View#getWindowToken()
1727 * View.getWindowToken()}.
1728 * @param action Name of the command to perform. This must be a scoped
1729 * name to avoid collisions, such as "com.mycompany.wallpaper.DOIT".
1730 * @param x Arbitrary integer argument based on command.
1731 * @param y Arbitrary integer argument based on command.
1732 * @param z Arbitrary integer argument based on command.
1733 * @param extras Optional additional information for the command, or null.
1734 */
1735 public void sendWallpaperCommand(IBinder windowToken, String action,
1736 int x, int y, int z, Bundle extras) {
1737 try {
1738 //Log.v(TAG, "Sending new wallpaper offsets from app...");
Jeff Brownf9e989d2013-04-04 23:04:03 -07001739 WindowManagerGlobal.getWindowSession().sendWallpaperCommand(
Dianne Hackborn75804932009-10-20 20:15:20 -07001740 windowToken, action, x, y, z, extras, false);
1741 //Log.v(TAG, "...app returning after sending offsets!");
1742 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07001743 throw e.rethrowFromSystemServer();
Dianne Hackborn75804932009-10-20 20:15:20 -07001744 }
1745 }
Benjamin Franzf3ece362015-02-11 10:51:10 +00001746
1747 /**
1748 * Returns whether wallpapers are supported for the calling user. If this function returns
Oleksandr Peletskyif2519812016-01-26 20:16:06 +01001749 * {@code false}, any attempts to changing the wallpaper will have no effect,
1750 * and any attempt to obtain of the wallpaper will return {@code null}.
Benjamin Franzf3ece362015-02-11 10:51:10 +00001751 */
1752 public boolean isWallpaperSupported() {
1753 if (sGlobals.mService == null) {
1754 Log.w(TAG, "WallpaperService not running");
Christopher Tate98d609c2016-05-18 17:31:58 -07001755 throw new RuntimeException(new DeadSystemException());
Benjamin Franzf3ece362015-02-11 10:51:10 +00001756 } else {
1757 try {
1758 return sGlobals.mService.isWallpaperSupported(mContext.getOpPackageName());
1759 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07001760 throw e.rethrowFromSystemServer();
Benjamin Franzf3ece362015-02-11 10:51:10 +00001761 }
1762 }
Benjamin Franzf3ece362015-02-11 10:51:10 +00001763 }
1764
Dianne Hackborn75804932009-10-20 20:15:20 -07001765 /**
Oleksandr Peletskyif2519812016-01-26 20:16:06 +01001766 * Returns whether the calling package is allowed to set the wallpaper for the calling user.
1767 * If this function returns {@code false}, any attempts to change the wallpaper will have
1768 * no effect. Always returns {@code true} for device owner and profile owner.
1769 *
1770 * @see android.os.UserManager#DISALLOW_SET_WALLPAPER
1771 */
Christopher Tate98d609c2016-05-18 17:31:58 -07001772 public boolean isSetWallpaperAllowed() {
Oleksandr Peletskyif2519812016-01-26 20:16:06 +01001773 if (sGlobals.mService == null) {
1774 Log.w(TAG, "WallpaperService not running");
Christopher Tate98d609c2016-05-18 17:31:58 -07001775 throw new RuntimeException(new DeadSystemException());
Oleksandr Peletskyif2519812016-01-26 20:16:06 +01001776 } else {
1777 try {
Christopher Tate98d609c2016-05-18 17:31:58 -07001778 return sGlobals.mService.isSetWallpaperAllowed(mContext.getOpPackageName());
Oleksandr Peletskyif2519812016-01-26 20:16:06 +01001779 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07001780 throw e.rethrowFromSystemServer();
Oleksandr Peletskyif2519812016-01-26 20:16:06 +01001781 }
1782 }
Oleksandr Peletskyif2519812016-01-26 20:16:06 +01001783 }
1784
1785 /**
Dianne Hackborn72c82ab2009-08-11 21:13:54 -07001786 * Clear the offsets previously associated with this window through
1787 * {@link #setWallpaperOffsets(IBinder, float, float)}. This reverts
1788 * the window to its default state, where it does not cause the wallpaper
1789 * to scroll from whatever its last offsets were.
Oleksandr Peletskyif2519812016-01-26 20:16:06 +01001790 *
Dianne Hackborn72c82ab2009-08-11 21:13:54 -07001791 * @param windowToken The window who these offsets should be associated
Scott Main8b2e0002009-09-29 18:17:31 -07001792 * with, as returned by {@link android.view.View#getWindowToken()
Dianne Hackborn72c82ab2009-08-11 21:13:54 -07001793 * View.getWindowToken()}.
1794 */
1795 public void clearWallpaperOffsets(IBinder windowToken) {
1796 try {
Jeff Brownf9e989d2013-04-04 23:04:03 -07001797 WindowManagerGlobal.getWindowSession().setWallpaperPosition(
Marco Nelissenbf6956b2009-11-09 15:21:13 -08001798 windowToken, -1, -1, -1, -1);
Dianne Hackborn72c82ab2009-08-11 21:13:54 -07001799 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07001800 throw e.rethrowFromSystemServer();
Dianne Hackborn72c82ab2009-08-11 21:13:54 -07001801 }
1802 }
Oleksandr Peletskyif2519812016-01-26 20:16:06 +01001803
Dianne Hackborn72c82ab2009-08-11 21:13:54 -07001804 /**
Christopher Tatebe132e62016-02-10 12:59:49 -08001805 * Remove any currently set system wallpaper, reverting to the system's built-in
Dianne Hackborn8cc6a502009-08-05 21:29:42 -07001806 * wallpaper. On success, the intent {@link Intent#ACTION_WALLPAPER_CHANGED}
1807 * is broadcast.
1808 *
Nicolas Falliere9530e3a2012-06-18 17:21:06 -07001809 * <p>This method requires the caller to hold the permission
1810 * {@link android.Manifest.permission#SET_WALLPAPER}.
1811 *
Michael Jurkab668d0b2013-10-04 15:11:05 -07001812 * @throws IOException If an error occurs reverting to the built-in
Dianne Hackborn8cc6a502009-08-05 21:29:42 -07001813 * wallpaper.
1814 */
Christopher Tate93252de2017-06-15 14:48:41 -07001815 @RequiresPermission(android.Manifest.permission.SET_WALLPAPER)
Dianne Hackborn8cc6a502009-08-05 21:29:42 -07001816 public void clear() throws IOException {
Christopher Tate5d99d472016-05-06 17:59:27 -07001817 setStream(openDefaultWallpaper(mContext, FLAG_SYSTEM), null, false);
Jeff Sharkey28f08772014-04-16 09:41:58 -07001818 }
1819
1820 /**
Christopher Tate79a24572016-03-02 14:42:44 -08001821 * Remove one or more currently set wallpapers, reverting to the system default
Christopher Tateedf7d042016-03-29 18:24:25 -07001822 * display for each one. If {@link #FLAG_SYSTEM} is set in the {@code which}
Christopher Tate79a24572016-03-02 14:42:44 -08001823 * parameter, the intent {@link Intent#ACTION_WALLPAPER_CHANGED} will be broadcast
1824 * upon success.
1825 *
Christopher Tateedf7d042016-03-29 18:24:25 -07001826 * @param which A bitwise combination of {@link #FLAG_SYSTEM} or
1827 * {@link #FLAG_LOCK}
Christopher Tate79a24572016-03-02 14:42:44 -08001828 * @throws IOException If an error occurs reverting to the built-in wallpaper.
1829 */
Christopher Tate93252de2017-06-15 14:48:41 -07001830 @RequiresPermission(android.Manifest.permission.SET_WALLPAPER)
Christopher Tate5d99d472016-05-06 17:59:27 -07001831 public void clear(@SetWallpaperFlags int which) throws IOException {
Christopher Tateedf7d042016-03-29 18:24:25 -07001832 if ((which & FLAG_SYSTEM) != 0) {
Christopher Tate79a24572016-03-02 14:42:44 -08001833 clear();
1834 }
Christopher Tateedf7d042016-03-29 18:24:25 -07001835 if ((which & FLAG_LOCK) != 0) {
1836 clearWallpaper(FLAG_LOCK, mContext.getUserId());
Christopher Tate79a24572016-03-02 14:42:44 -08001837 }
1838 }
1839
1840 /**
Jeff Sharkey28f08772014-04-16 09:41:58 -07001841 * Open stream representing the default static image wallpaper.
1842 *
Christopher Tate5d99d472016-05-06 17:59:27 -07001843 * If the device defines no default wallpaper of the requested kind,
1844 * {@code null} is returned.
1845 *
Jeff Sharkey28f08772014-04-16 09:41:58 -07001846 * @hide
1847 */
Mathew Inwood4fb17d12018-08-14 14:25:44 +01001848 @UnsupportedAppUsage
Christopher Tate5d99d472016-05-06 17:59:27 -07001849 public static InputStream openDefaultWallpaper(Context context, @SetWallpaperFlags int which) {
1850 final String whichProp;
1851 final int defaultResId;
1852 if (which == FLAG_LOCK) {
Christopher Tate36cb2a7a2016-06-09 13:44:48 -07001853 /* Factory-default lock wallpapers are not yet supported
Christopher Tate5d99d472016-05-06 17:59:27 -07001854 whichProp = PROP_LOCK_WALLPAPER;
1855 defaultResId = com.android.internal.R.drawable.default_lock_wallpaper;
Christopher Tate36cb2a7a2016-06-09 13:44:48 -07001856 */
1857 return null;
Christopher Tate5d99d472016-05-06 17:59:27 -07001858 } else {
1859 whichProp = PROP_WALLPAPER;
1860 defaultResId = com.android.internal.R.drawable.default_wallpaper;
1861 }
1862 final String path = SystemProperties.get(whichProp);
Jeff Sharkey28f08772014-04-16 09:41:58 -07001863 if (!TextUtils.isEmpty(path)) {
1864 final File file = new File(path);
1865 if (file.exists()) {
1866 try {
1867 return new FileInputStream(file);
1868 } catch (IOException e) {
1869 // Ignored, fall back to platform default below
1870 }
1871 }
1872 }
Christopher Tate5d99d472016-05-06 17:59:27 -07001873 try {
1874 return context.getResources().openRawResource(defaultResId);
1875 } catch (NotFoundException e) {
1876 // no default defined for this device; this is not a failure
1877 }
1878 return null;
Jeff Sharkey28f08772014-04-16 09:41:58 -07001879 }
1880
1881 /**
1882 * Return {@link ComponentName} of the default live wallpaper, or
1883 * {@code null} if none is defined.
1884 *
1885 * @hide
1886 */
1887 public static ComponentName getDefaultWallpaperComponent(Context context) {
SzuWei Lin1c7ae312018-11-22 14:33:01 +08001888 ComponentName cn = null;
1889
Jeff Sharkey28f08772014-04-16 09:41:58 -07001890 String flat = SystemProperties.get(PROP_WALLPAPER_COMPONENT);
1891 if (!TextUtils.isEmpty(flat)) {
SzuWei Lin1c7ae312018-11-22 14:33:01 +08001892 cn = ComponentName.unflattenFromString(flat);
1893 }
1894
1895 if (cn == null) {
1896 flat = context.getString(com.android.internal.R.string.default_wallpaper_component);
1897 if (!TextUtils.isEmpty(flat)) {
1898 cn = ComponentName.unflattenFromString(flat);
Jeff Sharkey28f08772014-04-16 09:41:58 -07001899 }
1900 }
1901
SzuWei Lin1c7ae312018-11-22 14:33:01 +08001902 // Check if the package exists
1903 if (cn != null) {
1904 try {
1905 final PackageManager packageManager = context.getPackageManager();
1906 packageManager.getPackageInfo(cn.getPackageName(),
1907 PackageManager.MATCH_DIRECT_BOOT_AWARE
1908 | PackageManager.MATCH_DIRECT_BOOT_UNAWARE);
1909 } catch (PackageManager.NameNotFoundException e) {
1910 cn = null;
Jeff Sharkey28f08772014-04-16 09:41:58 -07001911 }
1912 }
1913
SzuWei Lin1c7ae312018-11-22 14:33:01 +08001914 return cn;
Dianne Hackborn8cc6a502009-08-05 21:29:42 -07001915 }
Christopher Tate1e1e2e02016-01-25 15:34:36 -08001916
Christopher Tatebe132e62016-02-10 12:59:49 -08001917 /**
1918 * Register a callback for lock wallpaper observation. Only the OS may use this.
1919 *
1920 * @return true on success; false on error.
1921 * @hide
1922 */
1923 public boolean setLockWallpaperCallback(IWallpaperManagerCallback callback) {
1924 if (sGlobals.mService == null) {
1925 Log.w(TAG, "WallpaperService not running");
Christopher Tate98d609c2016-05-18 17:31:58 -07001926 throw new RuntimeException(new DeadSystemException());
Christopher Tatebe132e62016-02-10 12:59:49 -08001927 }
1928
1929 try {
1930 return sGlobals.mService.setLockWallpaperCallback(callback);
1931 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07001932 throw e.rethrowFromSystemServer();
Christopher Tatebe132e62016-02-10 12:59:49 -08001933 }
Christopher Tatebe132e62016-02-10 12:59:49 -08001934 }
1935
Christopher Tated7faf532016-02-25 12:43:38 -08001936 /**
1937 * Is the current system wallpaper eligible for backup?
1938 *
1939 * Only the OS itself may use this method.
1940 * @hide
1941 */
Christopher Tate61722662016-08-10 16:13:14 -07001942 public boolean isWallpaperBackupEligible(int which) {
Christopher Tated7faf532016-02-25 12:43:38 -08001943 if (sGlobals.mService == null) {
1944 Log.w(TAG, "WallpaperService not running");
Christopher Tate98d609c2016-05-18 17:31:58 -07001945 throw new RuntimeException(new DeadSystemException());
Christopher Tated7faf532016-02-25 12:43:38 -08001946 }
1947 try {
Christopher Tate61722662016-08-10 16:13:14 -07001948 return sGlobals.mService.isWallpaperBackupEligible(which, mContext.getUserId());
Christopher Tated7faf532016-02-25 12:43:38 -08001949 } catch (RemoteException e) {
1950 Log.e(TAG, "Exception querying wallpaper backup eligibility: " + e.getMessage());
1951 }
1952 return false;
1953 }
1954
Christopher Tate1e1e2e02016-01-25 15:34:36 -08001955 // Private completion callback for setWallpaper() synchronization
1956 private class WallpaperSetCompletion extends IWallpaperManagerCallback.Stub {
1957 final CountDownLatch mLatch;
1958
1959 public WallpaperSetCompletion() {
1960 mLatch = new CountDownLatch(1);
1961 }
1962
1963 public void waitForCompletion() {
1964 try {
1965 mLatch.await(30, TimeUnit.SECONDS);
1966 } catch (InterruptedException e) {
1967 // This might be legit: the crop may take a very long time. Don't sweat
1968 // it in that case; we are okay with display lagging behind in order to
1969 // keep the caller from locking up indeterminately.
1970 }
1971 }
1972
1973 @Override
1974 public void onWallpaperChanged() throws RemoteException {
1975 mLatch.countDown();
1976 }
Lucas Dupinea1fb1e2017-04-05 17:39:44 -07001977
1978 @Override
Lucas Dupin50ba9912017-07-14 11:55:05 -07001979 public void onWallpaperColorsChanged(WallpaperColors colors, int which, int userId)
Lucas Dupinea1fb1e2017-04-05 17:39:44 -07001980 throws RemoteException {
Lucas Dupin50ba9912017-07-14 11:55:05 -07001981 sGlobals.onWallpaperColorsChanged(colors, which, userId);
Lucas Dupinea1fb1e2017-04-05 17:39:44 -07001982 }
Christopher Tate1e1e2e02016-01-25 15:34:36 -08001983 }
Lucas Dupinc40608c2017-04-14 18:33:08 -07001984
1985 /**
1986 * Interface definition for a callback to be invoked when colors change on a wallpaper.
1987 */
1988 public interface OnColorsChangedListener {
1989 /**
1990 * Called when colors change.
1991 * A {@link android.app.WallpaperColors} object containing a simplified
1992 * color histogram will be given.
1993 *
1994 * @param colors Wallpaper color info
1995 * @param which A combination of {@link #FLAG_LOCK} and {@link #FLAG_SYSTEM}
1996 */
1997 void onColorsChanged(WallpaperColors colors, int which);
Lucas Dupin50ba9912017-07-14 11:55:05 -07001998
1999 /**
2000 * Called when colors change.
2001 * A {@link android.app.WallpaperColors} object containing a simplified
2002 * color histogram will be given.
2003 *
2004 * @param colors Wallpaper color info
2005 * @param which A combination of {@link #FLAG_LOCK} and {@link #FLAG_SYSTEM}
2006 * @param userId Owner of the wallpaper
2007 * @hide
2008 */
2009 default void onColorsChanged(WallpaperColors colors, int which, int userId) {
2010 onColorsChanged(colors, which);
2011 }
Lucas Dupinc40608c2017-04-14 18:33:08 -07002012 }
Dianne Hackborn8cc6a502009-08-05 21:29:42 -07002013}