blob: 2d06dcca35f62694f2db717094889f5d091d9e9e [file] [log] [blame]
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001/*
Christopher Tatebe132e62016-02-10 12:59:49 -08002h * Copyright (C) 2009 The Android Open Source Project
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07003 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Dianne Hackborn8cc6a502009-08-05 21:29:42 -070017package android.app;
18
Christopher Tatead3c2592016-01-20 18:13:17 -080019import android.annotation.IntDef;
Tor Norbye7b9c9122013-05-30 16:48:33 -070020import android.annotation.RawRes;
Dianne Hackborn067e5f62014-09-07 23:14:30 -070021import android.annotation.SystemApi;
Jeff Sharkey28f08772014-04-16 09:41:58 -070022import android.content.ComponentName;
Adam Lesinskibba72d12013-10-11 18:10:56 -070023import android.content.ContentResolver;
Dianne Hackborn8cc6a502009-08-05 21:29:42 -070024import android.content.Context;
25import android.content.Intent;
Michael Jurkae8d1bf72013-09-09 15:58:54 +020026import android.content.pm.PackageManager;
27import android.content.pm.ResolveInfo;
Dianne Hackborn8cc6a502009-08-05 21:29:42 -070028import android.content.res.Resources;
29import android.graphics.Bitmap;
30import android.graphics.BitmapFactory;
Michael Jurkab668d0b2013-10-04 15:11:05 -070031import android.graphics.BitmapRegionDecoder;
Dianne Hackborn284ac932009-08-28 10:34:25 -070032import android.graphics.Canvas;
Dianne Hackborn19382ac2009-09-11 21:13:37 -070033import android.graphics.ColorFilter;
Michael Jurkab668d0b2013-10-04 15:11:05 -070034import android.graphics.Matrix;
Dianne Hackborn284ac932009-08-28 10:34:25 -070035import android.graphics.Paint;
Dianne Hackborn19382ac2009-09-11 21:13:37 -070036import android.graphics.PixelFormat;
Jeff Brown24572372011-06-09 19:05:15 -070037import android.graphics.PorterDuff;
38import android.graphics.PorterDuffXfermode;
Dianne Hackborn284ac932009-08-28 10:34:25 -070039import android.graphics.Rect;
Michael Jurkab668d0b2013-10-04 15:11:05 -070040import android.graphics.RectF;
Dianne Hackborn8cc6a502009-08-05 21:29:42 -070041import android.graphics.drawable.BitmapDrawable;
42import android.graphics.drawable.Drawable;
Michael Jurkae8d1bf72013-09-09 15:58:54 +020043import android.net.Uri;
Dianne Hackborn284ac932009-08-28 10:34:25 -070044import android.os.Bundle;
Dianne Hackborn8cc6a502009-08-05 21:29:42 -070045import android.os.Handler;
46import android.os.IBinder;
Dianne Hackborn840c3a22009-09-02 17:35:17 -070047import android.os.Looper;
Dianne Hackborn8cc6a502009-08-05 21:29:42 -070048import android.os.ParcelFileDescriptor;
49import android.os.RemoteException;
50import android.os.ServiceManager;
Jeff Sharkey28f08772014-04-16 09:41:58 -070051import android.os.SystemProperties;
52import android.text.TextUtils;
Dianne Hackborn7341d7a2009-08-14 11:37:52 -070053import android.util.Log;
Jeff Brown98365d72012-08-19 20:30:52 -070054import android.view.WindowManagerGlobal;
Dianne Hackborn8cc6a502009-08-05 21:29:42 -070055
Christopher Tateffa6a882015-12-11 15:08:46 -080056import libcore.io.IoUtils;
57
Michael Jurkab668d0b2013-10-04 15:11:05 -070058import java.io.BufferedInputStream;
Jeff Sharkey28f08772014-04-16 09:41:58 -070059import java.io.File;
60import java.io.FileInputStream;
Dianne Hackborn8cc6a502009-08-05 21:29:42 -070061import java.io.FileOutputStream;
62import java.io.IOException;
63import java.io.InputStream;
Christopher Tatead3c2592016-01-20 18:13:17 -080064import java.lang.annotation.Retention;
65import java.lang.annotation.RetentionPolicy;
Michael Jurkae8d1bf72013-09-09 15:58:54 +020066import java.util.List;
Christopher Tate1e1e2e02016-01-25 15:34:36 -080067import java.util.concurrent.CountDownLatch;
68import java.util.concurrent.TimeUnit;
Dianne Hackborn8cc6a502009-08-05 21:29:42 -070069
Scott Main8b2e0002009-09-29 18:17:31 -070070/**
71 * Provides access to the system wallpaper. With WallpaperManager, you can
72 * get the current wallpaper, get the desired dimensions for the wallpaper, set
73 * the wallpaper, and more. Get an instance of WallpaperManager with
Benjamin Franzf3ece362015-02-11 10:51:10 +000074 * {@link #getInstance(android.content.Context) getInstance()}.
75 *
76 * <p> An app can check whether wallpapers are supported for the current user, by calling
Oleksandr Peletskyif2519812016-01-26 20:16:06 +010077 * {@link #isWallpaperSupported()}, and whether setting of wallpapers is allowed, by calling
78 * {@link #isWallpaperSettingAllowed()}.
Scott Main8b2e0002009-09-29 18:17:31 -070079 */
Dianne Hackborn8cc6a502009-08-05 21:29:42 -070080public class WallpaperManager {
81 private static String TAG = "WallpaperManager";
82 private static boolean DEBUG = false;
Marco Nelissenbf6956b2009-11-09 15:21:13 -080083 private float mWallpaperXStep = -1;
84 private float mWallpaperYStep = -1;
Dianne Hackborn8cc6a502009-08-05 21:29:42 -070085
Jeff Sharkey28f08772014-04-16 09:41:58 -070086 /** {@hide} */
87 private static final String PROP_WALLPAPER = "ro.config.wallpaper";
88 /** {@hide} */
89 private static final String PROP_WALLPAPER_COMPONENT = "ro.config.wallpaper_component";
90
Dianne Hackborn4c62fc02009-08-08 20:40:27 -070091 /**
Michael Jurkae8d1bf72013-09-09 15:58:54 +020092 * Activity Action: Show settings for choosing wallpaper. Do not use directly to construct
93 * an intent; instead, use {@link #getCropAndSetWallpaperIntent}.
94 * <p>Input: {@link Intent#getData} is the URI of the image to crop and set as wallpaper.
95 * <p>Output: RESULT_OK if user decided to crop/set the wallpaper, RESULT_CANCEL otherwise
Adam Lesinskibba72d12013-10-11 18:10:56 -070096 * Activities that support this intent should specify a MIME filter of "image/*"
Michael Jurkae8d1bf72013-09-09 15:58:54 +020097 */
98 public static final String ACTION_CROP_AND_SET_WALLPAPER =
99 "android.service.wallpaper.CROP_AND_SET_WALLPAPER";
100
101 /**
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700102 * Launch an activity for the user to pick the current global live
103 * wallpaper.
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700104 */
105 public static final String ACTION_LIVE_WALLPAPER_CHOOSER
106 = "android.service.wallpaper.LIVE_WALLPAPER_CHOOSER";
Adam Cohen5a242ec2010-12-07 21:07:07 -0800107
108 /**
Dianne Hackborn7df7d202012-04-19 18:00:04 -0700109 * Directly launch live wallpaper preview, allowing the user to immediately
110 * confirm to switch to a specific live wallpaper. You must specify
111 * {@link #EXTRA_LIVE_WALLPAPER_COMPONENT} with the ComponentName of
112 * a live wallpaper component that is to be shown.
113 */
114 public static final String ACTION_CHANGE_LIVE_WALLPAPER
115 = "android.service.wallpaper.CHANGE_LIVE_WALLPAPER";
116
117 /**
118 * Extra in {@link #ACTION_CHANGE_LIVE_WALLPAPER} that specifies the
119 * ComponentName of a live wallpaper that should be shown as a preview,
120 * for the user to confirm.
121 */
122 public static final String EXTRA_LIVE_WALLPAPER_COMPONENT
123 = "android.service.wallpaper.extra.LIVE_WALLPAPER_COMPONENT";
124
125 /**
Adam Cohen5a242ec2010-12-07 21:07:07 -0800126 * Manifest entry for activities that respond to {@link Intent#ACTION_SET_WALLPAPER}
127 * which allows them to provide a custom large icon associated with this action.
128 */
129 public static final String WALLPAPER_PREVIEW_META_DATA = "android.wallpaper.preview";
130
Dianne Hackborn23ef7b42009-11-18 18:20:39 -0800131 /**
132 * Command for {@link #sendWallpaperCommand}: reported by the wallpaper
133 * host when the user taps on an empty area (not performing an action
134 * in the host). The x and y arguments are the location of the tap in
135 * screen coordinates.
136 */
137 public static final String COMMAND_TAP = "android.wallpaper.tap";
Oleksandr Peletskyif2519812016-01-26 20:16:06 +0100138
Dianne Hackborn23ef7b42009-11-18 18:20:39 -0800139 /**
140 * Command for {@link #sendWallpaperCommand}: reported by the wallpaper
Jeff Brown9f3bdfe2010-10-13 06:01:27 -0700141 * host when the user releases a secondary pointer on an empty area
142 * (not performing an action in the host). The x and y arguments are
143 * the location of the secondary tap in screen coordinates.
144 */
145 public static final String COMMAND_SECONDARY_TAP = "android.wallpaper.secondaryTap";
146
147 /**
148 * Command for {@link #sendWallpaperCommand}: reported by the wallpaper
Dianne Hackborn23ef7b42009-11-18 18:20:39 -0800149 * host when the user drops an object into an area of the host. The x
150 * and y arguments are the location of the drop.
151 */
152 public static final String COMMAND_DROP = "android.home.drop";
Christopher Tatead3c2592016-01-20 18:13:17 -0800153
154 /**
155 * Extra passed back from setWallpaper() giving the new wallpaper's assigned ID.
156 * @hide
157 */
158 public static final String EXTRA_NEW_WALLPAPER_ID = "android.service.wallpaper.extra.ID";
159
160 // flags for which kind of wallpaper to set
161
162 /** @hide */
163 @IntDef(flag = true, value = {
164 FLAG_SET_SYSTEM,
165 FLAG_SET_LOCK
166 })
167 @Retention(RetentionPolicy.SOURCE)
168 public @interface SetWallpaperFlags {}
169
170 /**
171 * Flag: use the supplied imagery as the general system wallpaper.
172 */
173 public static final int FLAG_SET_SYSTEM = 1 << 0;
174
175 /**
176 * Flag: use the supplied imagery as the lock-screen wallpaper.
177 */
178 public static final int FLAG_SET_LOCK = 1 << 1;
179
Dianne Hackborn8cc6a502009-08-05 21:29:42 -0700180 private final Context mContext;
Oleksandr Peletskyif2519812016-01-26 20:16:06 +0100181
Dianne Hackborn19382ac2009-09-11 21:13:37 -0700182 /**
183 * Special drawable that draws a wallpaper as fast as possible. Assumes
184 * no scaling or placement off (0,0) of the wallpaper (this should be done
185 * at the time the bitmap is loaded).
186 */
187 static class FastBitmapDrawable extends Drawable {
188 private final Bitmap mBitmap;
189 private final int mWidth;
190 private final int mHeight;
191 private int mDrawLeft;
192 private int mDrawTop;
Romain Guy407ec782011-08-24 17:06:58 -0700193 private final Paint mPaint;
Dianne Hackborn19382ac2009-09-11 21:13:37 -0700194
195 private FastBitmapDrawable(Bitmap bitmap) {
196 mBitmap = bitmap;
197 mWidth = bitmap.getWidth();
198 mHeight = bitmap.getHeight();
Romain Guy407ec782011-08-24 17:06:58 -0700199
Dianne Hackborn19382ac2009-09-11 21:13:37 -0700200 setBounds(0, 0, mWidth, mHeight);
Romain Guy407ec782011-08-24 17:06:58 -0700201
202 mPaint = new Paint();
203 mPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC));
Dianne Hackborn19382ac2009-09-11 21:13:37 -0700204 }
205
206 @Override
207 public void draw(Canvas canvas) {
Romain Guy407ec782011-08-24 17:06:58 -0700208 canvas.drawBitmap(mBitmap, mDrawLeft, mDrawTop, mPaint);
Dianne Hackborn19382ac2009-09-11 21:13:37 -0700209 }
210
211 @Override
212 public int getOpacity() {
213 return PixelFormat.OPAQUE;
214 }
215
216 @Override
217 public void setBounds(int left, int top, int right, int bottom) {
218 mDrawLeft = left + (right-left - mWidth) / 2;
219 mDrawTop = top + (bottom-top - mHeight) / 2;
220 }
221
222 @Override
Dianne Hackborn19382ac2009-09-11 21:13:37 -0700223 public void setAlpha(int alpha) {
Romain Guy407ec782011-08-24 17:06:58 -0700224 throw new UnsupportedOperationException("Not supported with this drawable");
Dianne Hackborn19382ac2009-09-11 21:13:37 -0700225 }
226
227 @Override
Chris Craikbd3bfc52015-03-02 10:43:29 -0800228 public void setColorFilter(ColorFilter colorFilter) {
Romain Guy407ec782011-08-24 17:06:58 -0700229 throw new UnsupportedOperationException("Not supported with this drawable");
Dianne Hackborn19382ac2009-09-11 21:13:37 -0700230 }
231
232 @Override
233 public void setDither(boolean dither) {
Romain Guy407ec782011-08-24 17:06:58 -0700234 throw new UnsupportedOperationException("Not supported with this drawable");
Dianne Hackborn19382ac2009-09-11 21:13:37 -0700235 }
236
237 @Override
238 public void setFilterBitmap(boolean filter) {
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
243 public int getIntrinsicWidth() {
244 return mWidth;
245 }
246
247 @Override
248 public int getIntrinsicHeight() {
249 return mHeight;
250 }
251
252 @Override
253 public int getMinimumWidth() {
254 return mWidth;
255 }
256
257 @Override
258 public int getMinimumHeight() {
259 return mHeight;
260 }
261 }
Oleksandr Peletskyif2519812016-01-26 20:16:06 +0100262
Dianne Hackborn8cc6a502009-08-05 21:29:42 -0700263 static class Globals extends IWallpaperManagerCallback.Stub {
264 private IWallpaperManager mService;
Vadim Tryshev93a914a2016-03-07 12:48:51 -0800265 private Bitmap mCachedWallpaper;
266 private int mCachedWallpaperUserId;
Dianne Hackborn19382ac2009-09-11 21:13:37 -0700267 private Bitmap mDefaultWallpaper;
Oleksandr Peletskyif2519812016-01-26 20:16:06 +0100268
Dianne Hackborn5e802fb2009-09-02 21:50:50 -0700269 Globals(Looper looper) {
Dianne Hackborn8cc6a502009-08-05 21:29:42 -0700270 IBinder b = ServiceManager.getService(Context.WALLPAPER_SERVICE);
271 mService = IWallpaperManager.Stub.asInterface(b);
Christopher Tatebe132e62016-02-10 12:59:49 -0800272 forgetLoadedWallpaper();
Dianne Hackborn8cc6a502009-08-05 21:29:42 -0700273 }
Oleksandr Peletskyif2519812016-01-26 20:16:06 +0100274
Dianne Hackborn8cc6a502009-08-05 21:29:42 -0700275 public void onWallpaperChanged() {
276 /* The wallpaper has changed but we shouldn't eagerly load the
277 * wallpaper as that would be inefficient. Reset the cached wallpaper
278 * to null so if the user requests the wallpaper again then we'll
279 * fetch it.
280 */
Christopher Tatebe132e62016-02-10 12:59:49 -0800281 forgetLoadedWallpaper();
Dianne Hackborn8cc6a502009-08-05 21:29:42 -0700282 }
Adam Cohen041a0ba2011-11-09 20:10:27 -0800283
Dianne Hackborn19382ac2009-09-11 21:13:37 -0700284 public Bitmap peekWallpaperBitmap(Context context, boolean returnDefault) {
Yorke Leedcd93cc2016-01-08 14:12:55 -0800285 return peekWallpaperBitmap(context, returnDefault, context.getUserId());
286 }
287
288 public Bitmap peekWallpaperBitmap(Context context, boolean returnDefault, int userId) {
Dianne Hackborn8cc6a502009-08-05 21:29:42 -0700289 synchronized (this) {
Benjamin Franzf3ece362015-02-11 10:51:10 +0000290 if (mService != null) {
291 try {
292 if (!mService.isWallpaperSupported(context.getOpPackageName())) {
293 return null;
294 }
295 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -0700296 throw e.rethrowFromSystemServer();
Benjamin Franzf3ece362015-02-11 10:51:10 +0000297 }
298 }
Vadim Tryshev93a914a2016-03-07 12:48:51 -0800299 if (mCachedWallpaper != null && mCachedWallpaperUserId == userId) {
300 return mCachedWallpaper;
Dianne Hackborn8cc6a502009-08-05 21:29:42 -0700301 }
Vadim Tryshev93a914a2016-03-07 12:48:51 -0800302 mCachedWallpaper = null;
303 mCachedWallpaperUserId = 0;
Dianne Hackbornc9dbbe22009-11-11 22:50:37 -0800304 try {
Vadim Tryshev93a914a2016-03-07 12:48:51 -0800305 mCachedWallpaper = getCurrentWallpaperLocked(userId);
306 mCachedWallpaperUserId = userId;
Dianne Hackbornc9dbbe22009-11-11 22:50:37 -0800307 } catch (OutOfMemoryError e) {
308 Log.w(TAG, "No memory load current wallpaper", e);
309 }
Vadim Tryshev93a914a2016-03-07 12:48:51 -0800310 if (mCachedWallpaper != null) {
311 return mCachedWallpaper;
Dianne Hackborn19382ac2009-09-11 21:13:37 -0700312 }
Vadim Tryshev93a914a2016-03-07 12:48:51 -0800313 if (returnDefault) {
314 if (mDefaultWallpaper == null) {
315 mDefaultWallpaper = getDefaultWallpaperLocked(context);
316 }
317 return mDefaultWallpaper;
318 }
319 return null;
Dianne Hackborn8cc6a502009-08-05 21:29:42 -0700320 }
321 }
Dianne Hackbornba398392011-08-01 16:11:57 -0700322
323 public void forgetLoadedWallpaper() {
324 synchronized (this) {
Vadim Tryshev93a914a2016-03-07 12:48:51 -0800325 mCachedWallpaper = null;
326 mCachedWallpaperUserId = 0;
Dianne Hackbornba398392011-08-01 16:11:57 -0700327 mDefaultWallpaper = null;
328 }
329 }
330
Yorke Leedcd93cc2016-01-08 14:12:55 -0800331 private Bitmap getCurrentWallpaperLocked(int userId) {
Adam Lesinski2c8d67c2014-04-23 13:46:21 -0700332 if (mService == null) {
333 Log.w(TAG, "WallpaperService not running");
334 return null;
335 }
336
Dianne Hackborn8cc6a502009-08-05 21:29:42 -0700337 try {
Dianne Hackborn284ac932009-08-28 10:34:25 -0700338 Bundle params = new Bundle();
Christopher Tatebe132e62016-02-10 12:59:49 -0800339 ParcelFileDescriptor fd = mService.getWallpaper(this, FLAG_SET_SYSTEM,
Yorke Leedcd93cc2016-01-08 14:12:55 -0800340 params, userId);
Dianne Hackborn8cc6a502009-08-05 21:29:42 -0700341 if (fd != null) {
Jeff Brown24572372011-06-09 19:05:15 -0700342 try {
343 BitmapFactory.Options options = new BitmapFactory.Options();
Michael Jurka824a4b52013-12-18 17:10:16 +0100344 return BitmapFactory.decodeFileDescriptor(
Jeff Brown24572372011-06-09 19:05:15 -0700345 fd.getFileDescriptor(), null, options);
Jeff Brown24572372011-06-09 19:05:15 -0700346 } catch (OutOfMemoryError e) {
347 Log.w(TAG, "Can't decode file", e);
348 } finally {
Christopher Tateffa6a882015-12-11 15:08:46 -0800349 IoUtils.closeQuietly(fd);
Dianne Hackborn8cc6a502009-08-05 21:29:42 -0700350 }
Dianne Hackborn19382ac2009-09-11 21:13:37 -0700351 }
352 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -0700353 throw e.rethrowFromSystemServer();
Dianne Hackborn19382ac2009-09-11 21:13:37 -0700354 }
355 return null;
356 }
Christopher Tateffa6a882015-12-11 15:08:46 -0800357
Dianne Hackborn19382ac2009-09-11 21:13:37 -0700358 private Bitmap getDefaultWallpaperLocked(Context context) {
Adam Lesinskie929bee2014-04-28 14:15:06 -0700359 InputStream is = openDefaultWallpaper(context);
Adam Lesinski2c8d67c2014-04-23 13:46:21 -0700360 if (is != null) {
361 try {
362 BitmapFactory.Options options = new BitmapFactory.Options();
363 return BitmapFactory.decodeStream(is, null, options);
364 } catch (OutOfMemoryError e) {
365 Log.w(TAG, "Can't decode stream", e);
366 } finally {
Christopher Tateffa6a882015-12-11 15:08:46 -0800367 IoUtils.closeQuietly(is);
Dianne Hackborn8cc6a502009-08-05 21:29:42 -0700368 }
Dianne Hackborn8cc6a502009-08-05 21:29:42 -0700369 }
370 return null;
371 }
372 }
Oleksandr Peletskyif2519812016-01-26 20:16:06 +0100373
Romain Guy407ec782011-08-24 17:06:58 -0700374 private static final Object sSync = new Object[0];
Dianne Hackborn8cc6a502009-08-05 21:29:42 -0700375 private static Globals sGlobals;
376
Dianne Hackborn840c3a22009-09-02 17:35:17 -0700377 static void initGlobals(Looper looper) {
Romain Guy407ec782011-08-24 17:06:58 -0700378 synchronized (sSync) {
Dianne Hackborn8cc6a502009-08-05 21:29:42 -0700379 if (sGlobals == null) {
Dianne Hackborn5e802fb2009-09-02 21:50:50 -0700380 sGlobals = new Globals(looper);
Dianne Hackborn8cc6a502009-08-05 21:29:42 -0700381 }
Dianne Hackborn8cc6a502009-08-05 21:29:42 -0700382 }
383 }
Oleksandr Peletskyif2519812016-01-26 20:16:06 +0100384
Dianne Hackborn8cc6a502009-08-05 21:29:42 -0700385 /*package*/ WallpaperManager(Context context, Handler handler) {
386 mContext = context;
Dianne Hackborn840c3a22009-09-02 17:35:17 -0700387 initGlobals(context.getMainLooper());
Dianne Hackborn8cc6a502009-08-05 21:29:42 -0700388 }
389
390 /**
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700391 * Retrieve a WallpaperManager associated with the given Context.
392 */
393 public static WallpaperManager getInstance(Context context) {
394 return (WallpaperManager)context.getSystemService(
395 Context.WALLPAPER_SERVICE);
396 }
Oleksandr Peletskyif2519812016-01-26 20:16:06 +0100397
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700398 /** @hide */
399 public IWallpaperManager getIWallpaperManager() {
Dianne Hackborn840c3a22009-09-02 17:35:17 -0700400 return sGlobals.mService;
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700401 }
Oleksandr Peletskyif2519812016-01-26 20:16:06 +0100402
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700403 /**
Scott Main8b2e0002009-09-29 18:17:31 -0700404 * Retrieve the current system wallpaper; if
Michael Jurkab668d0b2013-10-04 15:11:05 -0700405 * no wallpaper is set, the system built-in static wallpaper is returned.
Scott Main8b2e0002009-09-29 18:17:31 -0700406 * This is returned as an
407 * abstract Drawable that you can install in a View to display whatever
Oleksandr Peletskyif2519812016-01-26 20:16:06 +0100408 * wallpaper the user has currently set.
Dianne Hackborn8cc6a502009-08-05 21:29:42 -0700409 *
410 * @return Returns a Drawable object that will draw the wallpaper.
411 */
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700412 public Drawable getDrawable() {
Dianne Hackborn19382ac2009-09-11 21:13:37 -0700413 Bitmap bm = sGlobals.peekWallpaperBitmap(mContext, true);
414 if (bm != null) {
415 Drawable dr = new BitmapDrawable(mContext.getResources(), bm);
416 dr.setDither(false);
417 return dr;
418 }
419 return null;
Dianne Hackborn8cc6a502009-08-05 21:29:42 -0700420 }
421
422 /**
Michael Jurkab668d0b2013-10-04 15:11:05 -0700423 * Returns a drawable for the system built-in static wallpaper .
424 *
425 */
426 public Drawable getBuiltInDrawable() {
427 return getBuiltInDrawable(0, 0, false, 0, 0);
428 }
429
430 /**
431 * Returns a drawable for the system built-in static wallpaper. Based on the parameters, the
432 * drawable can be cropped and scaled
433 *
434 * @param outWidth The width of the returned drawable
435 * @param outWidth The height of the returned drawable
436 * @param scaleToFit If true, scale the wallpaper down rather than just cropping it
437 * @param horizontalAlignment A float value between 0 and 1 specifying where to crop the image;
438 * 0 for left-aligned, 0.5 for horizontal center-aligned, and 1 for right-aligned
439 * @param verticalAlignment A float value between 0 and 1 specifying where to crop the image;
440 * 0 for top-aligned, 0.5 for vertical center-aligned, and 1 for bottom-aligned
441 *
442 */
443 public Drawable getBuiltInDrawable(int outWidth, int outHeight,
444 boolean scaleToFit, float horizontalAlignment, float verticalAlignment) {
445 if (sGlobals.mService == null) {
446 Log.w(TAG, "WallpaperService not running");
447 return null;
448 }
449 Resources resources = mContext.getResources();
450 horizontalAlignment = Math.max(0, Math.min(1, horizontalAlignment));
451 verticalAlignment = Math.max(0, Math.min(1, verticalAlignment));
452
Jeff Sharkey28f08772014-04-16 09:41:58 -0700453 InputStream is = new BufferedInputStream(openDefaultWallpaper(mContext));
Michael Jurkab668d0b2013-10-04 15:11:05 -0700454
455 if (is == null) {
456 Log.e(TAG, "default wallpaper input stream is null");
457 return null;
458 } else {
459 if (outWidth <= 0 || outHeight <= 0) {
460 Bitmap fullSize = BitmapFactory.decodeStream(is, null, null);
461 return new BitmapDrawable(resources, fullSize);
462 } else {
463 int inWidth;
464 int inHeight;
465 {
466 BitmapFactory.Options options = new BitmapFactory.Options();
467 options.inJustDecodeBounds = true;
468 BitmapFactory.decodeStream(is, null, options);
469 if (options.outWidth != 0 && options.outHeight != 0) {
470 inWidth = options.outWidth;
471 inHeight = options.outHeight;
472 } else {
473 Log.e(TAG, "default wallpaper dimensions are 0");
474 return null;
475 }
476 }
477
Jeff Sharkey28f08772014-04-16 09:41:58 -0700478 is = new BufferedInputStream(openDefaultWallpaper(mContext));
Michael Jurkab668d0b2013-10-04 15:11:05 -0700479
480 RectF cropRectF;
481
482 outWidth = Math.min(inWidth, outWidth);
483 outHeight = Math.min(inHeight, outHeight);
484 if (scaleToFit) {
485 cropRectF = getMaxCropRect(inWidth, inHeight, outWidth, outHeight,
486 horizontalAlignment, verticalAlignment);
487 } else {
488 float left = (inWidth - outWidth) * horizontalAlignment;
489 float right = left + outWidth;
490 float top = (inHeight - outHeight) * verticalAlignment;
491 float bottom = top + outHeight;
Michael Jurka05391532013-10-14 20:44:42 -0700492 cropRectF = new RectF(left, top, right, bottom);
Michael Jurkab668d0b2013-10-04 15:11:05 -0700493 }
494 Rect roundedTrueCrop = new Rect();
495 cropRectF.roundOut(roundedTrueCrop);
496
497 if (roundedTrueCrop.width() <= 0 || roundedTrueCrop.height() <= 0) {
498 Log.w(TAG, "crop has bad values for full size image");
499 return null;
500 }
501
502 // See how much we're reducing the size of the image
503 int scaleDownSampleSize = Math.min(roundedTrueCrop.width() / outWidth,
504 roundedTrueCrop.height() / outHeight);
505
506 // Attempt to open a region decoder
507 BitmapRegionDecoder decoder = null;
508 try {
509 decoder = BitmapRegionDecoder.newInstance(is, true);
510 } catch (IOException e) {
511 Log.w(TAG, "cannot open region decoder for default wallpaper");
512 }
513
514 Bitmap crop = null;
515 if (decoder != null) {
516 // Do region decoding to get crop bitmap
517 BitmapFactory.Options options = new BitmapFactory.Options();
518 if (scaleDownSampleSize > 1) {
519 options.inSampleSize = scaleDownSampleSize;
520 }
521 crop = decoder.decodeRegion(roundedTrueCrop, options);
522 decoder.recycle();
523 }
524
525 if (crop == null) {
526 // BitmapRegionDecoder has failed, try to crop in-memory
Jeff Sharkey28f08772014-04-16 09:41:58 -0700527 is = new BufferedInputStream(openDefaultWallpaper(mContext));
Michael Jurkab668d0b2013-10-04 15:11:05 -0700528 Bitmap fullSize = null;
529 if (is != null) {
530 BitmapFactory.Options options = new BitmapFactory.Options();
531 if (scaleDownSampleSize > 1) {
532 options.inSampleSize = scaleDownSampleSize;
533 }
534 fullSize = BitmapFactory.decodeStream(is, null, options);
535 }
536 if (fullSize != null) {
537 crop = Bitmap.createBitmap(fullSize, roundedTrueCrop.left,
538 roundedTrueCrop.top, roundedTrueCrop.width(),
539 roundedTrueCrop.height());
540 }
541 }
542
543 if (crop == null) {
544 Log.w(TAG, "cannot decode default wallpaper");
545 return null;
546 }
547
548 // Scale down if necessary
549 if (outWidth > 0 && outHeight > 0 &&
550 (crop.getWidth() != outWidth || crop.getHeight() != outHeight)) {
551 Matrix m = new Matrix();
552 RectF cropRect = new RectF(0, 0, crop.getWidth(), crop.getHeight());
553 RectF returnRect = new RectF(0, 0, outWidth, outHeight);
554 m.setRectToRect(cropRect, returnRect, Matrix.ScaleToFit.FILL);
555 Bitmap tmp = Bitmap.createBitmap((int) returnRect.width(),
556 (int) returnRect.height(), Bitmap.Config.ARGB_8888);
557 if (tmp != null) {
558 Canvas c = new Canvas(tmp);
559 Paint p = new Paint();
560 p.setFilterBitmap(true);
561 c.drawBitmap(crop, m, p);
562 crop = tmp;
563 }
564 }
565
566 return new BitmapDrawable(resources, crop);
567 }
568 }
569 }
570
571 private static RectF getMaxCropRect(int inWidth, int inHeight, int outWidth, int outHeight,
572 float horizontalAlignment, float verticalAlignment) {
573 RectF cropRect = new RectF();
574 // Get a crop rect that will fit this
575 if (inWidth / (float) inHeight > outWidth / (float) outHeight) {
576 cropRect.top = 0;
577 cropRect.bottom = inHeight;
578 float cropWidth = outWidth * (inHeight / (float) outHeight);
579 cropRect.left = (inWidth - cropWidth) * horizontalAlignment;
580 cropRect.right = cropRect.left + cropWidth;
581 } else {
582 cropRect.left = 0;
583 cropRect.right = inWidth;
584 float cropHeight = outHeight * (inWidth / (float) outWidth);
585 cropRect.top = (inHeight - cropHeight) * verticalAlignment;
586 cropRect.bottom = cropRect.top + cropHeight;
587 }
588 return cropRect;
589 }
590
591 /**
Scott Main8b2e0002009-09-29 18:17:31 -0700592 * Retrieve the current system wallpaper; if there is no wallpaper set,
593 * a null pointer is returned. This is returned as an
Dianne Hackborn8cc6a502009-08-05 21:29:42 -0700594 * abstract Drawable that you can install in a View to display whatever
Oleksandr Peletskyif2519812016-01-26 20:16:06 +0100595 * wallpaper the user has currently set.
Dianne Hackborn8cc6a502009-08-05 21:29:42 -0700596 *
597 * @return Returns a Drawable object that will draw the wallpaper or a
598 * null pointer if these is none.
599 */
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700600 public Drawable peekDrawable() {
Dianne Hackborn19382ac2009-09-11 21:13:37 -0700601 Bitmap bm = sGlobals.peekWallpaperBitmap(mContext, false);
602 if (bm != null) {
603 Drawable dr = new BitmapDrawable(mContext.getResources(), bm);
604 dr.setDither(false);
605 return dr;
606 }
607 return null;
608 }
609
610 /**
Scott Main8b2e0002009-09-29 18:17:31 -0700611 * Like {@link #getDrawable()}, but the returned Drawable has a number
612 * of limitations to reduce its overhead as much as possible. It will
613 * never scale the wallpaper (only centering it if the requested bounds
614 * do match the bitmap bounds, which should not be typical), doesn't
615 * allow setting an alpha, color filter, or other attributes, etc. The
616 * bounds of the returned drawable will be initialized to the same bounds
617 * as the wallpaper, so normally you will not need to touch it. The
618 * drawable also assumes that it will be used in a context running in
619 * the same density as the screen (not in density compatibility mode).
Dianne Hackborn19382ac2009-09-11 21:13:37 -0700620 *
621 * @return Returns a Drawable object that will draw the wallpaper.
622 */
623 public Drawable getFastDrawable() {
624 Bitmap bm = sGlobals.peekWallpaperBitmap(mContext, true);
625 if (bm != null) {
Romain Guy407ec782011-08-24 17:06:58 -0700626 return new FastBitmapDrawable(bm);
Dianne Hackborn19382ac2009-09-11 21:13:37 -0700627 }
628 return null;
629 }
630
631 /**
Scott Main8b2e0002009-09-29 18:17:31 -0700632 * Like {@link #getFastDrawable()}, but if there is no wallpaper set,
633 * a null pointer is returned.
Dianne Hackborn19382ac2009-09-11 21:13:37 -0700634 *
635 * @return Returns an optimized Drawable object that will draw the
636 * wallpaper or a null pointer if these is none.
637 */
638 public Drawable peekFastDrawable() {
Christopher Tatebe132e62016-02-10 12:59:49 -0800639 Bitmap bm = sGlobals.peekWallpaperBitmap(mContext, false);
Dianne Hackborn19382ac2009-09-11 21:13:37 -0700640 if (bm != null) {
Romain Guy407ec782011-08-24 17:06:58 -0700641 return new FastBitmapDrawable(bm);
Dianne Hackborn19382ac2009-09-11 21:13:37 -0700642 }
643 return null;
Dianne Hackborn8cc6a502009-08-05 21:29:42 -0700644 }
645
646 /**
Romain Guy407ec782011-08-24 17:06:58 -0700647 * Like {@link #getDrawable()} but returns a Bitmap.
Oleksandr Peletskyif2519812016-01-26 20:16:06 +0100648 *
Romain Guy407ec782011-08-24 17:06:58 -0700649 * @hide
650 */
651 public Bitmap getBitmap() {
Yorke Leedcd93cc2016-01-08 14:12:55 -0800652 return getBitmapAsUser(mContext.getUserId());
653 }
654
655 /**
656 * Like {@link #getDrawable()} but returns a Bitmap for the provided user.
657 *
658 * @hide
659 */
660 public Bitmap getBitmapAsUser(int userId) {
661 return sGlobals.peekWallpaperBitmap(mContext, true, userId);
Romain Guy407ec782011-08-24 17:06:58 -0700662 }
663
664 /**
Christopher Tatebe132e62016-02-10 12:59:49 -0800665 * Get an open, readable file descriptor to the given wallpaper image file.
666 * The callee is resopnsible for closing the fd when done ingesting the file.
667 *
668 * <p>If no lock-specific wallpaper has been configured for the given user, then
669 * this method will return {@code null} when requesting {@link #FLAG_SET_LOCK} rather than
670 * returning the system wallpaper's image file.
671 */
672 public ParcelFileDescriptor getWallpaperFile(int which) {
673 return getWallpaperFile(which, mContext.getUserId());
674 }
675
676 /**
677 * Version of {@link #getWallpaperFile(int)} that can access the wallpaper data
678 * for a given user. The caller must hold the INTERACT_ACROSS_USERS_FULL
679 * permission to access another user's wallpaper data.
680 * @hide
681 */
682 public ParcelFileDescriptor getWallpaperFile(int which, int userId) {
683 if (which != FLAG_SET_SYSTEM && which != FLAG_SET_LOCK) {
684 throw new IllegalArgumentException("Must request exactly one kind of wallpaper");
685 }
686
687 if (sGlobals.mService == null) {
688 Log.w(TAG, "WallpaperService not running");
689 return null;
690 } else {
691 try {
692 Bundle outParams = new Bundle();
693 return sGlobals.mService.getWallpaper(null, which, outParams, userId);
694 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -0700695 throw e.rethrowFromSystemServer();
Christopher Tatebe132e62016-02-10 12:59:49 -0800696 }
697 }
698 }
699
700 /**
Dianne Hackbornba398392011-08-01 16:11:57 -0700701 * Remove all internal references to the last loaded wallpaper. Useful
702 * for apps that want to reduce memory usage when they only temporarily
703 * need to have the wallpaper. After calling, the next request for the
704 * wallpaper will require reloading it again from disk.
705 */
706 public void forgetLoadedWallpaper() {
Christopher Tatebe132e62016-02-10 12:59:49 -0800707 sGlobals.forgetLoadedWallpaper();
Dianne Hackbornba398392011-08-01 16:11:57 -0700708 }
709
710 /**
Dianne Hackborneb034652009-09-07 00:49:58 -0700711 * If the current wallpaper is a live wallpaper component, return the
712 * information about that wallpaper. Otherwise, if it is a static image,
713 * simply return null.
Dianne Hackborneb034652009-09-07 00:49:58 -0700714 */
715 public WallpaperInfo getWallpaperInfo() {
716 try {
Mike Lockwoodc067c9c2011-10-31 12:50:12 -0400717 if (sGlobals.mService == null) {
718 Log.w(TAG, "WallpaperService not running");
719 return null;
720 } else {
721 return sGlobals.mService.getWallpaperInfo();
722 }
Dianne Hackborneb034652009-09-07 00:49:58 -0700723 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -0700724 throw e.rethrowFromSystemServer();
Dianne Hackborneb034652009-09-07 00:49:58 -0700725 }
726 }
Michael Jurkae8d1bf72013-09-09 15:58:54 +0200727
728 /**
729 * Gets an Intent that will launch an activity that crops the given
730 * image and sets the device's wallpaper. If there is a default HOME activity
731 * that supports cropping wallpapers, it will be preferred as the default.
Ying Wang930d4e52013-09-14 11:57:17 -0700732 * Use this method instead of directly creating a {@link #ACTION_CROP_AND_SET_WALLPAPER}
Michael Jurkae8d1bf72013-09-09 15:58:54 +0200733 * intent.
Adam Lesinskibba72d12013-10-11 18:10:56 -0700734 *
735 * @param imageUri The image URI that will be set in the intent. The must be a content
736 * URI and its provider must resolve its type to "image/*"
737 *
738 * @throws IllegalArgumentException if the URI is not a content URI or its MIME type is
739 * not "image/*"
Michael Jurkae8d1bf72013-09-09 15:58:54 +0200740 */
741 public Intent getCropAndSetWallpaperIntent(Uri imageUri) {
Allen Shen1c0f21e2014-04-29 16:16:29 +0800742 if (imageUri == null) {
743 throw new IllegalArgumentException("Image URI must not be null");
744 }
745
Adam Lesinskibba72d12013-10-11 18:10:56 -0700746 if (!ContentResolver.SCHEME_CONTENT.equals(imageUri.getScheme())) {
747 throw new IllegalArgumentException("Image URI must be of the "
748 + ContentResolver.SCHEME_CONTENT + " scheme type");
749 }
750
Michael Jurkae8d1bf72013-09-09 15:58:54 +0200751 final PackageManager packageManager = mContext.getPackageManager();
752 Intent cropAndSetWallpaperIntent =
753 new Intent(ACTION_CROP_AND_SET_WALLPAPER, imageUri);
754 cropAndSetWallpaperIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
755
756 // Find out if the default HOME activity supports CROP_AND_SET_WALLPAPER
757 Intent homeIntent = new Intent(Intent.ACTION_MAIN).addCategory(Intent.CATEGORY_HOME);
758 ResolveInfo resolvedHome = packageManager.resolveActivity(homeIntent,
759 PackageManager.MATCH_DEFAULT_ONLY);
760 if (resolvedHome != null) {
761 cropAndSetWallpaperIntent.setPackage(resolvedHome.activityInfo.packageName);
762
763 List<ResolveInfo> cropAppList = packageManager.queryIntentActivities(
764 cropAndSetWallpaperIntent, 0);
765 if (cropAppList.size() > 0) {
766 return cropAndSetWallpaperIntent;
767 }
768 }
769
770 // fallback crop activity
Oren Blasberg60598ea0a2016-02-26 11:01:23 -0800771 final String cropperPackage = mContext.getString(
772 com.android.internal.R.string.config_wallpaperCropperPackage);
773 cropAndSetWallpaperIntent.setPackage(cropperPackage);
Adam Lesinskibba72d12013-10-11 18:10:56 -0700774 List<ResolveInfo> cropAppList = packageManager.queryIntentActivities(
775 cropAndSetWallpaperIntent, 0);
776 if (cropAppList.size() > 0) {
777 return cropAndSetWallpaperIntent;
778 }
779 // If the URI is not of the right type, or for some reason the system wallpaper
780 // cropper doesn't exist, return null
781 throw new IllegalArgumentException("Cannot use passed URI to set wallpaper; " +
782 "check that the type returned by ContentProvider matches image/*");
Michael Jurkae8d1bf72013-09-09 15:58:54 +0200783 }
784
Dianne Hackborneb034652009-09-07 00:49:58 -0700785 /**
Dianne Hackborn8cc6a502009-08-05 21:29:42 -0700786 * Change the current system wallpaper to the bitmap in the given resource.
787 * The resource is opened as a raw data stream and copied into the
788 * wallpaper; it must be a valid PNG or JPEG image. On success, the intent
789 * {@link Intent#ACTION_WALLPAPER_CHANGED} is broadcast.
790 *
Nicolas Falliere9530e3a2012-06-18 17:21:06 -0700791 * <p>This method requires the caller to hold the permission
792 * {@link android.Manifest.permission#SET_WALLPAPER}.
793 *
Dianne Hackborn8cc6a502009-08-05 21:29:42 -0700794 * @param resid The bitmap to save.
795 *
Michael Jurkab668d0b2013-10-04 15:11:05 -0700796 * @throws IOException If an error occurs reverting to the built-in
Dianne Hackborn8cc6a502009-08-05 21:29:42 -0700797 * wallpaper.
798 */
Tor Norbye7b9c9122013-05-30 16:48:33 -0700799 public void setResource(@RawRes int resid) throws IOException {
Christopher Tatead3c2592016-01-20 18:13:17 -0800800 setResource(resid, FLAG_SET_SYSTEM);
801 }
802
803 /**
804 * Version of {@link #setResource(int)} that takes an optional Bundle for returning
805 * metadata about the operation to the caller.
806 *
807 * @param resid
808 * @param which Flags indicating which wallpaper(s) to configure with the new imagery.
809 *
810 * @see #FLAG_SET_LOCK
811 * @see #FLAG_SET_SYSTEM
812 *
813 * @return An integer ID assigned to the newly active wallpaper; or zero on failure.
814 *
815 * @throws IOException
816 */
817 public int setResource(@RawRes int resid, @SetWallpaperFlags int which)
818 throws IOException {
Mike Lockwoodc067c9c2011-10-31 12:50:12 -0400819 if (sGlobals.mService == null) {
820 Log.w(TAG, "WallpaperService not running");
Christopher Tatead3c2592016-01-20 18:13:17 -0800821 return 0;
Mike Lockwoodc067c9c2011-10-31 12:50:12 -0400822 }
Christopher Tatead3c2592016-01-20 18:13:17 -0800823 final Bundle result = new Bundle();
Christopher Tate1e1e2e02016-01-25 15:34:36 -0800824 final WallpaperSetCompletion completion = new WallpaperSetCompletion();
Dianne Hackborn8cc6a502009-08-05 21:29:42 -0700825 try {
826 Resources resources = mContext.getResources();
827 /* Set the wallpaper to the default values */
Dianne Hackborn840c3a22009-09-02 17:35:17 -0700828 ParcelFileDescriptor fd = sGlobals.mService.setWallpaper(
Christopher Tatead3c2592016-01-20 18:13:17 -0800829 "res:" + resources.getResourceName(resid),
Christopher Tate1e1e2e02016-01-25 15:34:36 -0800830 mContext.getOpPackageName(), null, result, which, completion);
Dianne Hackborn8cc6a502009-08-05 21:29:42 -0700831 if (fd != null) {
832 FileOutputStream fos = null;
Christopher Tate1e1e2e02016-01-25 15:34:36 -0800833 boolean ok = false;
Dianne Hackborn8cc6a502009-08-05 21:29:42 -0700834 try {
835 fos = new ParcelFileDescriptor.AutoCloseOutputStream(fd);
Christopher Tatead3c2592016-01-20 18:13:17 -0800836 copyStreamToWallpaperFile(resources.openRawResource(resid), fos);
Christopher Tate1e1e2e02016-01-25 15:34:36 -0800837 // The 'close()' is the trigger for any server-side image manipulation,
838 // so we must do that before waiting for completion.
839 fos.close();
840 completion.waitForCompletion();
Dianne Hackborn8cc6a502009-08-05 21:29:42 -0700841 } finally {
Christopher Tate1e1e2e02016-01-25 15:34:36 -0800842 // Might be redundant but completion shouldn't wait unless the write
843 // succeeded; this is a fallback if it threw past the close+wait.
Christopher Tateffa6a882015-12-11 15:08:46 -0800844 IoUtils.closeQuietly(fos);
Dianne Hackborn8cc6a502009-08-05 21:29:42 -0700845 }
846 }
847 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -0700848 throw e.rethrowFromSystemServer();
Dianne Hackborn8cc6a502009-08-05 21:29:42 -0700849 }
Christopher Tatead3c2592016-01-20 18:13:17 -0800850 return result.getInt(EXTRA_NEW_WALLPAPER_ID, 0);
Dianne Hackborn8cc6a502009-08-05 21:29:42 -0700851 }
Christopher Tateffa6a882015-12-11 15:08:46 -0800852
Dianne Hackborn8cc6a502009-08-05 21:29:42 -0700853 /**
854 * Change the current system wallpaper to a bitmap. The given bitmap is
855 * converted to a PNG and stored as the wallpaper. On success, the intent
856 * {@link Intent#ACTION_WALLPAPER_CHANGED} is broadcast.
857 *
Christopher Tateffa6a882015-12-11 15:08:46 -0800858 * <p>This method is equivalent to calling
859 * {@link #setBitmap(Bitmap, Rect, boolean)} and passing {@code null} for the
860 * {@code visibleCrop} rectangle and {@code true} for the {@code allowBackup}
861 * parameter.
862 *
Nicolas Falliere9530e3a2012-06-18 17:21:06 -0700863 * <p>This method requires the caller to hold the permission
864 * {@link android.Manifest.permission#SET_WALLPAPER}.
865 *
Christopher Tatead3c2592016-01-20 18:13:17 -0800866 * @param bitmap The bitmap to be used as the new system wallpaper.
Dianne Hackborn8cc6a502009-08-05 21:29:42 -0700867 *
Christopher Tateffa6a882015-12-11 15:08:46 -0800868 * @throws IOException If an error occurs when attempting to set the wallpaper
869 * to the provided image.
Dianne Hackborn8cc6a502009-08-05 21:29:42 -0700870 */
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700871 public void setBitmap(Bitmap bitmap) throws IOException {
Christopher Tateffa6a882015-12-11 15:08:46 -0800872 setBitmap(bitmap, null, true);
873 }
874
875 /**
876 * Change the current system wallpaper to a bitmap, specifying a hint about
877 * which subrectangle of the full image is to be visible. The OS will then
878 * try to best present the given portion of the full image as the static system
879 * wallpaper image. On success, the intent
880 * {@link Intent#ACTION_WALLPAPER_CHANGED} is broadcast.
881 *
882 * <p>Passing {@code null} as the {@code visibleHint} parameter is equivalent to
883 * passing (0, 0, {@code fullImage.getWidth()}, {@code fullImage.getHeight()}).
884 *
885 * <p>This method requires the caller to hold the permission
886 * {@link android.Manifest.permission#SET_WALLPAPER}.
887 *
Christopher Tatead3c2592016-01-20 18:13:17 -0800888 * @param fullImage A bitmap that will supply the wallpaper imagery.
Christopher Tateffa6a882015-12-11 15:08:46 -0800889 * @param visibleCropHint The rectangular subregion of {@code fullImage} that should be
890 * displayed as wallpaper. Passing {@code null} for this parameter means that
891 * the full image should be displayed if possible given the image's and device's
Christopher Tatead3c2592016-01-20 18:13:17 -0800892 * aspect ratios, etc.
Christopher Tateffa6a882015-12-11 15:08:46 -0800893 * @param allowBackup {@code true} if the OS is permitted to back up this wallpaper
894 * image for restore to a future device; {@code false} otherwise.
895 *
Christopher Tatead3c2592016-01-20 18:13:17 -0800896 * @return An integer ID assigned to the newly active wallpaper; or zero on failure.
897 *
Christopher Tateffa6a882015-12-11 15:08:46 -0800898 * @throws IOException If an error occurs when attempting to set the wallpaper
899 * to the provided image.
900 * @throws IllegalArgumentException If the {@code visibleCropHint} rectangle is
901 * empty or invalid.
902 */
Christopher Tatead3c2592016-01-20 18:13:17 -0800903 public int setBitmap(Bitmap fullImage, Rect visibleCropHint, boolean allowBackup)
904 throws IOException {
905 return setBitmap(fullImage, visibleCropHint, allowBackup, FLAG_SET_SYSTEM);
906 }
907
908 /**
909 /**
910 * Version of {@link #setBitmap(Bitmap, Rect, boolean)} that allows the caller
911 * to specify which of the supported wallpaper categories to set.
912 *
913 * @param fullImage A bitmap that will supply the wallpaper imagery.
914 * @param visibleCropHint The rectangular subregion of {@code fullImage} that should be
915 * displayed as wallpaper. Passing {@code null} for this parameter means that
916 * the full image should be displayed if possible given the image's and device's
917 * aspect ratios, etc.
918 * @param allowBackup {@code true} if the OS is permitted to back up this wallpaper
919 * image for restore to a future device; {@code false} otherwise.
920 * @param which Flags indicating which wallpaper(s) to configure with the new imagery.
921 *
922 * @see #FLAG_SET_LOCK_WALLPAPER
923 * @see #FLAG_SET_SYSTEM_WALLPAPER
924 *
925 * @return An integer ID assigned to the newly active wallpaper; or zero on failure.
926 *
927 * @throws IOException
928 */
929 public int setBitmap(Bitmap fullImage, Rect visibleCropHint,
930 boolean allowBackup, @SetWallpaperFlags int which)
Christopher Tateffa6a882015-12-11 15:08:46 -0800931 throws IOException {
932 validateRect(visibleCropHint);
Mike Lockwoodc067c9c2011-10-31 12:50:12 -0400933 if (sGlobals.mService == null) {
934 Log.w(TAG, "WallpaperService not running");
Christopher Tatead3c2592016-01-20 18:13:17 -0800935 return 0;
Mike Lockwoodc067c9c2011-10-31 12:50:12 -0400936 }
Christopher Tatead3c2592016-01-20 18:13:17 -0800937 final Bundle result = new Bundle();
Christopher Tate1e1e2e02016-01-25 15:34:36 -0800938 final WallpaperSetCompletion completion = new WallpaperSetCompletion();
Dianne Hackborn8cc6a502009-08-05 21:29:42 -0700939 try {
Benjamin Franzf3ece362015-02-11 10:51:10 +0000940 ParcelFileDescriptor fd = sGlobals.mService.setWallpaper(null,
Christopher Tate1e1e2e02016-01-25 15:34:36 -0800941 mContext.getOpPackageName(), visibleCropHint, result, which, completion);
Christopher Tatead3c2592016-01-20 18:13:17 -0800942 if (fd != null) {
943 FileOutputStream fos = null;
944 try {
945 fos = new ParcelFileDescriptor.AutoCloseOutputStream(fd);
946 fullImage.compress(Bitmap.CompressFormat.PNG, 90, fos);
Christopher Tate1e1e2e02016-01-25 15:34:36 -0800947 fos.close();
948 completion.waitForCompletion();
Christopher Tatead3c2592016-01-20 18:13:17 -0800949 } finally {
950 IoUtils.closeQuietly(fos);
951 }
Dianne Hackborn8cc6a502009-08-05 21:29:42 -0700952 }
953 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -0700954 throw e.rethrowFromSystemServer();
Dianne Hackborn8cc6a502009-08-05 21:29:42 -0700955 }
Christopher Tatead3c2592016-01-20 18:13:17 -0800956 return result.getInt(EXTRA_NEW_WALLPAPER_ID, 0);
Dianne Hackborn8cc6a502009-08-05 21:29:42 -0700957 }
958
Christopher Tateffa6a882015-12-11 15:08:46 -0800959 private final void validateRect(Rect rect) {
960 if (rect != null && rect.isEmpty()) {
961 throw new IllegalArgumentException("visibleCrop rectangle must be valid and non-empty");
962 }
963 }
964
Dianne Hackborn8cc6a502009-08-05 21:29:42 -0700965 /**
966 * Change the current system wallpaper to a specific byte stream. The
967 * give InputStream is copied into persistent storage and will now be
968 * used as the wallpaper. Currently it must be either a JPEG or PNG
969 * image. On success, the intent {@link Intent#ACTION_WALLPAPER_CHANGED}
970 * is broadcast.
971 *
Christopher Tateffa6a882015-12-11 15:08:46 -0800972 * <p>This method is equivalent to calling
973 * {@link #setStream(InputStream, Rect, boolean)} and passing {@code null} for the
974 * {@code visibleCrop} rectangle and {@code true} for the {@code allowBackup}
975 * parameter.
976 *
Nicolas Falliere9530e3a2012-06-18 17:21:06 -0700977 * <p>This method requires the caller to hold the permission
978 * {@link android.Manifest.permission#SET_WALLPAPER}.
979 *
Christopher Tateffa6a882015-12-11 15:08:46 -0800980 * @param bitmapData A stream containing the raw data to install as a wallpaper.
Dianne Hackborn8cc6a502009-08-05 21:29:42 -0700981 *
Christopher Tateffa6a882015-12-11 15:08:46 -0800982 * @throws IOException If an error occurs when attempting to set the wallpaper
983 * based on the provided image data.
Dianne Hackborn8cc6a502009-08-05 21:29:42 -0700984 */
Christopher Tateffa6a882015-12-11 15:08:46 -0800985 public void setStream(InputStream bitmapData) throws IOException {
986 setStream(bitmapData, null, true);
987 }
988
Christopher Tatead3c2592016-01-20 18:13:17 -0800989 private void copyStreamToWallpaperFile(InputStream data, FileOutputStream fos)
Christopher Tateffa6a882015-12-11 15:08:46 -0800990 throws IOException {
991 byte[] buffer = new byte[32768];
992 int amt;
993 while ((amt=data.read(buffer)) > 0) {
994 fos.write(buffer, 0, amt);
995 }
996 }
997
998 /**
999 * Change the current system wallpaper to a specific byte stream, specifying a
1000 * hint about which subrectangle of the full image is to be visible. The OS will
1001 * then try to best present the given portion of the full image as the static system
1002 * wallpaper image. The data from the given InputStream is copied into persistent
1003 * storage and will then be used as the system wallpaper. Currently the data must
1004 * be either a JPEG or PNG image. On success, the intent
1005 * {@link Intent#ACTION_WALLPAPER_CHANGED} is broadcast.
1006 *
1007 * <p>This method requires the caller to hold the permission
1008 * {@link android.Manifest.permission#SET_WALLPAPER}.
1009 *
1010 * @param bitmapData A stream containing the raw data to install as a wallpaper.
1011 * @param visibleCropHint The rectangular subregion of the streamed image that should be
1012 * displayed as wallpaper. Passing {@code null} for this parameter means that
1013 * the full image should be displayed if possible given the image's and device's
1014 * aspect ratios, etc.
1015 * @param allowBackup {@code true} if the OS is permitted to back up this wallpaper
1016 * image for restore to a future device; {@code false} otherwise.
1017 *
1018 * @throws IOException If an error occurs when attempting to set the wallpaper
1019 * based on the provided image data.
1020 * @throws IllegalArgumentException If the {@code visibleCropHint} rectangle is
1021 * empty or invalid.
1022 */
Christopher Tatead3c2592016-01-20 18:13:17 -08001023 public int setStream(InputStream bitmapData, Rect visibleCropHint, boolean allowBackup)
Christopher Tateffa6a882015-12-11 15:08:46 -08001024 throws IOException {
Christopher Tatead3c2592016-01-20 18:13:17 -08001025 return setStream(bitmapData, visibleCropHint, allowBackup, FLAG_SET_SYSTEM);
1026 }
1027
1028 /**
1029 * Version of {@link #setStream(InputStream, Rect, boolean)} that allows the caller
1030 * to specify which of the supported wallpaper categories to set.
1031 *
1032 * @param bitmapData A stream containing the raw data to install as a wallpaper.
1033 * @param visibleCropHint The rectangular subregion of the streamed image that should be
1034 * displayed as wallpaper. Passing {@code null} for this parameter means that
1035 * the full image should be displayed if possible given the image's and device's
1036 * aspect ratios, etc.
1037 * @param allowBackup {@code true} if the OS is permitted to back up this wallpaper
1038 * image for restore to a future device; {@code false} otherwise.
1039 * @param which Flags indicating which wallpaper(s) to configure with the new imagery.
1040 *
1041 * @see #FLAG_SET_LOCK_WALLPAPER
1042 * @see #FLAG_SET_SYSTEM_WALLPAPER
1043 *
1044 * @throws IOException
1045 */
1046 public int setStream(InputStream bitmapData, Rect visibleCropHint,
1047 boolean allowBackup, @SetWallpaperFlags int which)
1048 throws IOException {
Christopher Tateffa6a882015-12-11 15:08:46 -08001049 validateRect(visibleCropHint);
Mike Lockwoodc067c9c2011-10-31 12:50:12 -04001050 if (sGlobals.mService == null) {
1051 Log.w(TAG, "WallpaperService not running");
Christopher Tatead3c2592016-01-20 18:13:17 -08001052 return 0;
Mike Lockwoodc067c9c2011-10-31 12:50:12 -04001053 }
Christopher Tatead3c2592016-01-20 18:13:17 -08001054 final Bundle result = new Bundle();
Christopher Tate1e1e2e02016-01-25 15:34:36 -08001055 final WallpaperSetCompletion completion = new WallpaperSetCompletion();
Dianne Hackborn8cc6a502009-08-05 21:29:42 -07001056 try {
Benjamin Franzf3ece362015-02-11 10:51:10 +00001057 ParcelFileDescriptor fd = sGlobals.mService.setWallpaper(null,
Christopher Tate1e1e2e02016-01-25 15:34:36 -08001058 mContext.getOpPackageName(), visibleCropHint, result, which, completion);
Christopher Tatead3c2592016-01-20 18:13:17 -08001059 if (fd != null) {
1060 FileOutputStream fos = null;
1061 try {
1062 fos = new ParcelFileDescriptor.AutoCloseOutputStream(fd);
1063 copyStreamToWallpaperFile(bitmapData, fos);
Christopher Tate1e1e2e02016-01-25 15:34:36 -08001064 fos.close();
1065 completion.waitForCompletion();
Christopher Tatead3c2592016-01-20 18:13:17 -08001066 } finally {
1067 IoUtils.closeQuietly(fos);
1068 }
Dianne Hackborn8cc6a502009-08-05 21:29:42 -07001069 }
1070 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07001071 throw e.rethrowFromSystemServer();
Dianne Hackborn8cc6a502009-08-05 21:29:42 -07001072 }
Christopher Tatead3c2592016-01-20 18:13:17 -08001073
1074 return result.getInt(EXTRA_NEW_WALLPAPER_ID, 0);
Dianne Hackborn8cc6a502009-08-05 21:29:42 -07001075 }
1076
Dianne Hackborn8cc6a502009-08-05 21:29:42 -07001077 /**
Dianne Hackborn5dc5a002012-09-15 19:33:48 -07001078 * Return whether any users are currently set to use the wallpaper
1079 * with the given resource ID. That is, their wallpaper has been
1080 * set through {@link #setResource(int)} with the same resource id.
1081 */
Tor Norbye7b9c9122013-05-30 16:48:33 -07001082 public boolean hasResourceWallpaper(@RawRes int resid) {
Dianne Hackborn5dc5a002012-09-15 19:33:48 -07001083 if (sGlobals.mService == null) {
1084 Log.w(TAG, "WallpaperService not running");
1085 return false;
1086 }
1087 try {
1088 Resources resources = mContext.getResources();
1089 String name = "res:" + resources.getResourceName(resid);
1090 return sGlobals.mService.hasNamedWallpaper(name);
1091 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07001092 throw e.rethrowFromSystemServer();
Dianne Hackborn5dc5a002012-09-15 19:33:48 -07001093 }
1094 }
1095
1096 /**
Dianne Hackborn8cc6a502009-08-05 21:29:42 -07001097 * Returns the desired minimum width for the wallpaper. Callers of
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001098 * {@link #setBitmap(android.graphics.Bitmap)} or
1099 * {@link #setStream(java.io.InputStream)} should check this value
Dianne Hackborn8cc6a502009-08-05 21:29:42 -07001100 * beforehand to make sure the supplied wallpaper respects the desired
1101 * minimum width.
1102 *
1103 * If the returned value is <= 0, the caller should use the width of
1104 * the default display instead.
1105 *
1106 * @return The desired minimum width for the wallpaper. This value should
1107 * be honored by applications that set the wallpaper but it is not
1108 * mandatory.
1109 */
1110 public int getDesiredMinimumWidth() {
Mike Lockwoodc067c9c2011-10-31 12:50:12 -04001111 if (sGlobals.mService == null) {
1112 Log.w(TAG, "WallpaperService not running");
1113 return 0;
1114 }
Dianne Hackborn8cc6a502009-08-05 21:29:42 -07001115 try {
Dianne Hackborn840c3a22009-09-02 17:35:17 -07001116 return sGlobals.mService.getWidthHint();
Dianne Hackborn8cc6a502009-08-05 21:29:42 -07001117 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07001118 throw e.rethrowFromSystemServer();
Dianne Hackborn8cc6a502009-08-05 21:29:42 -07001119 }
1120 }
1121
1122 /**
1123 * Returns the desired minimum height for the wallpaper. Callers of
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001124 * {@link #setBitmap(android.graphics.Bitmap)} or
1125 * {@link #setStream(java.io.InputStream)} should check this value
Dianne Hackborn8cc6a502009-08-05 21:29:42 -07001126 * beforehand to make sure the supplied wallpaper respects the desired
1127 * minimum height.
1128 *
1129 * If the returned value is <= 0, the caller should use the height of
1130 * the default display instead.
1131 *
1132 * @return The desired minimum height for the wallpaper. This value should
1133 * be honored by applications that set the wallpaper but it is not
1134 * mandatory.
1135 */
1136 public int getDesiredMinimumHeight() {
Mike Lockwoodc067c9c2011-10-31 12:50:12 -04001137 if (sGlobals.mService == null) {
1138 Log.w(TAG, "WallpaperService not running");
1139 return 0;
1140 }
Dianne Hackborn8cc6a502009-08-05 21:29:42 -07001141 try {
Dianne Hackborn840c3a22009-09-02 17:35:17 -07001142 return sGlobals.mService.getHeightHint();
Dianne Hackborn8cc6a502009-08-05 21:29:42 -07001143 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07001144 throw e.rethrowFromSystemServer();
Dianne Hackborn8cc6a502009-08-05 21:29:42 -07001145 }
1146 }
1147
1148 /**
1149 * For use only by the current home application, to specify the size of
1150 * wallpaper it would like to use. This allows such applications to have
1151 * a virtual wallpaper that is larger than the physical screen, matching
1152 * the size of their workspace.
Dianne Hackbornc5bf7582012-04-25 19:12:07 -07001153 *
1154 * <p>Note developers, who don't seem to be reading this. This is
Christopher Tateffa6a882015-12-11 15:08:46 -08001155 * for <em>home apps</em> to tell what size wallpaper they would like.
1156 * Nobody else should be calling this! Certainly not other non-home
Dianne Hackbornc5bf7582012-04-25 19:12:07 -07001157 * apps that change the wallpaper. Those apps are supposed to
1158 * <b>retrieve</b> the suggested size so they can construct a wallpaper
1159 * that matches it.
1160 *
Nicolas Falliere9530e3a2012-06-18 17:21:06 -07001161 * <p>This method requires the caller to hold the permission
1162 * {@link android.Manifest.permission#SET_WALLPAPER_HINTS}.
1163 *
Dianne Hackborn8cc6a502009-08-05 21:29:42 -07001164 * @param minimumWidth Desired minimum width
1165 * @param minimumHeight Desired minimum height
1166 */
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001167 public void suggestDesiredDimensions(int minimumWidth, int minimumHeight) {
Dianne Hackborn8cc6a502009-08-05 21:29:42 -07001168 try {
Donghan Ryu289c2732011-11-14 18:56:11 -08001169 /**
1170 * The framework makes no attempt to limit the window size
1171 * to the maximum texture size. Any window larger than this
1172 * cannot be composited.
1173 *
1174 * Read maximum texture size from system property and scale down
1175 * minimumWidth and minimumHeight accordingly.
1176 */
1177 int maximumTextureSize;
1178 try {
1179 maximumTextureSize = SystemProperties.getInt("sys.max_texture_size", 0);
1180 } catch (Exception e) {
1181 maximumTextureSize = 0;
1182 }
1183
1184 if (maximumTextureSize > 0) {
1185 if ((minimumWidth > maximumTextureSize) ||
1186 (minimumHeight > maximumTextureSize)) {
1187 float aspect = (float)minimumHeight / (float)minimumWidth;
1188 if (minimumWidth > minimumHeight) {
1189 minimumWidth = maximumTextureSize;
1190 minimumHeight = (int)((minimumWidth * aspect) + 0.5);
1191 } else {
1192 minimumHeight = maximumTextureSize;
1193 minimumWidth = (int)((minimumHeight / aspect) + 0.5);
1194 }
1195 }
1196 }
1197
Mike Lockwoodc067c9c2011-10-31 12:50:12 -04001198 if (sGlobals.mService == null) {
1199 Log.w(TAG, "WallpaperService not running");
1200 } else {
Benjamin Franzf3ece362015-02-11 10:51:10 +00001201 sGlobals.mService.setDimensionHints(minimumWidth, minimumHeight,
1202 mContext.getOpPackageName());
Mike Lockwoodc067c9c2011-10-31 12:50:12 -04001203 }
Dianne Hackborn8cc6a502009-08-05 21:29:42 -07001204 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07001205 throw e.rethrowFromSystemServer();
Dianne Hackborn8cc6a502009-08-05 21:29:42 -07001206 }
1207 }
Adam Cohen041a0ba2011-11-09 20:10:27 -08001208
Dianne Hackborn8cc6a502009-08-05 21:29:42 -07001209 /**
Dianne Hackborn067e5f62014-09-07 23:14:30 -07001210 * Specify extra padding that the wallpaper should have outside of the display.
1211 * That is, the given padding supplies additional pixels the wallpaper should extend
1212 * outside of the display itself.
1213 * @param padding The number of pixels the wallpaper should extend beyond the display,
1214 * on its left, top, right, and bottom sides.
1215 * @hide
1216 */
1217 @SystemApi
1218 public void setDisplayPadding(Rect padding) {
1219 try {
1220 if (sGlobals.mService == null) {
1221 Log.w(TAG, "WallpaperService not running");
1222 } else {
Benjamin Franzf3ece362015-02-11 10:51:10 +00001223 sGlobals.mService.setDisplayPadding(padding, mContext.getOpPackageName());
Dianne Hackborn067e5f62014-09-07 23:14:30 -07001224 }
1225 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07001226 throw e.rethrowFromSystemServer();
Dianne Hackborn067e5f62014-09-07 23:14:30 -07001227 }
1228 }
1229
1230 /**
1231 * Apply a raw offset to the wallpaper window. Should only be used in
1232 * combination with {@link #setDisplayPadding(android.graphics.Rect)} when you
1233 * have ensured that the wallpaper will extend outside of the display area so that
1234 * it can be moved without leaving part of the display uncovered.
1235 * @param x The offset, in pixels, to apply to the left edge.
1236 * @param y The offset, in pixels, to apply to the top edge.
1237 * @hide
1238 */
1239 @SystemApi
1240 public void setDisplayOffset(IBinder windowToken, int x, int y) {
1241 try {
1242 //Log.v(TAG, "Sending new wallpaper display offsets from app...");
1243 WindowManagerGlobal.getWindowSession().setWallpaperDisplayOffset(
1244 windowToken, x, y);
1245 //Log.v(TAG, "...app returning after sending display offset!");
1246 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07001247 throw e.rethrowFromSystemServer();
Dianne Hackborn067e5f62014-09-07 23:14:30 -07001248 }
1249 }
1250
1251 /**
Filip Gruszczynski9fedc722015-01-28 15:59:46 -08001252 * Clear the wallpaper.
1253 *
1254 * @hide
1255 */
1256 @SystemApi
1257 public void clearWallpaper() {
Christopher Tatebe132e62016-02-10 12:59:49 -08001258 clearWallpaper(FLAG_SET_SYSTEM, mContext.getUserId());
1259 }
1260
1261 /**
1262 * Clear the wallpaper for a specific user. The caller must hold the
1263 * INTERACT_ACROSS_USERS_FULL permission to clear another user's
1264 * wallpaper.
1265 * @hide
1266 */
1267 @SystemApi
1268 public void clearWallpaper(int which, int userId) {
Filip Gruszczynski9fedc722015-01-28 15:59:46 -08001269 if (sGlobals.mService == null) {
1270 Log.w(TAG, "WallpaperService not running");
1271 return;
1272 }
1273 try {
Christopher Tatebe132e62016-02-10 12:59:49 -08001274 sGlobals.mService.clearWallpaper(mContext.getOpPackageName(), which, userId);
Filip Gruszczynski9fedc722015-01-28 15:59:46 -08001275 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07001276 throw e.rethrowFromSystemServer();
Filip Gruszczynski9fedc722015-01-28 15:59:46 -08001277 }
1278 }
1279
1280 /**
1281 * Set the live wallpaper.
1282 *
1283 * This can only be called by packages with android.permission.SET_WALLPAPER_COMPONENT
1284 * permission.
1285 *
1286 * @hide
1287 */
1288 @SystemApi
1289 public boolean setWallpaperComponent(ComponentName name) {
1290 if (sGlobals.mService == null) {
1291 Log.w(TAG, "WallpaperService not running");
1292 return false;
1293 }
1294 try {
Benjamin Franzf3ece362015-02-11 10:51:10 +00001295 sGlobals.mService.setWallpaperComponentChecked(name, mContext.getOpPackageName());
Filip Gruszczynski9fedc722015-01-28 15:59:46 -08001296 return true;
1297 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07001298 throw e.rethrowFromSystemServer();
Filip Gruszczynski9fedc722015-01-28 15:59:46 -08001299 }
Filip Gruszczynski9fedc722015-01-28 15:59:46 -08001300 }
1301
1302 /**
Christopher Tateffa6a882015-12-11 15:08:46 -08001303 * Set the display position of the current wallpaper within any larger space, when
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07001304 * that wallpaper is visible behind the given window. The X and Y offsets
1305 * are floating point numbers ranging from 0 to 1, representing where the
1306 * wallpaper should be positioned within the screen space. These only
Christopher Tateffa6a882015-12-11 15:08:46 -08001307 * make sense when the wallpaper is larger than the display.
Oleksandr Peletskyif2519812016-01-26 20:16:06 +01001308 *
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07001309 * @param windowToken The window who these offsets should be associated
Scott Main8b2e0002009-09-29 18:17:31 -07001310 * with, as returned by {@link android.view.View#getWindowToken()
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07001311 * View.getWindowToken()}.
Marco Nelissenbf6956b2009-11-09 15:21:13 -08001312 * @param xOffset The offset along the X dimension, from 0 to 1.
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07001313 * @param yOffset The offset along the Y dimension, from 0 to 1.
1314 */
1315 public void setWallpaperOffsets(IBinder windowToken, float xOffset, float yOffset) {
Adam Cohen791a6332012-01-12 14:38:38 -08001316 try {
1317 //Log.v(TAG, "Sending new wallpaper offsets from app...");
Jeff Brownf9e989d2013-04-04 23:04:03 -07001318 WindowManagerGlobal.getWindowSession().setWallpaperPosition(
Adam Cohen791a6332012-01-12 14:38:38 -08001319 windowToken, xOffset, yOffset, mWallpaperXStep, mWallpaperYStep);
1320 //Log.v(TAG, "...app returning after sending offsets!");
1321 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07001322 throw e.rethrowFromSystemServer();
Adam Cohen791a6332012-01-12 14:38:38 -08001323 }
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07001324 }
Adam Cohen041a0ba2011-11-09 20:10:27 -08001325
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07001326 /**
Marco Nelissenbf6956b2009-11-09 15:21:13 -08001327 * For applications that use multiple virtual screens showing a wallpaper,
1328 * specify the step size between virtual screens. For example, if the
Dianne Hackborn23ef7b42009-11-18 18:20:39 -08001329 * launcher has 3 virtual screens, it would specify an xStep of 0.5,
Marco Nelissenbf6956b2009-11-09 15:21:13 -08001330 * since the X offset for those screens are 0.0, 0.5 and 1.0
Oleksandr Peletskyif2519812016-01-26 20:16:06 +01001331 * @param xStep The X offset delta from one screen to the next one
Marco Nelissenbf6956b2009-11-09 15:21:13 -08001332 * @param yStep The Y offset delta from one screen to the next one
1333 */
1334 public void setWallpaperOffsetSteps(float xStep, float yStep) {
1335 mWallpaperXStep = xStep;
1336 mWallpaperYStep = yStep;
1337 }
Oleksandr Peletskyif2519812016-01-26 20:16:06 +01001338
Marco Nelissenbf6956b2009-11-09 15:21:13 -08001339 /**
Dianne Hackborn75804932009-10-20 20:15:20 -07001340 * Send an arbitrary command to the current active wallpaper.
Oleksandr Peletskyif2519812016-01-26 20:16:06 +01001341 *
Dianne Hackborn75804932009-10-20 20:15:20 -07001342 * @param windowToken The window who these offsets should be associated
1343 * with, as returned by {@link android.view.View#getWindowToken()
1344 * View.getWindowToken()}.
1345 * @param action Name of the command to perform. This must be a scoped
1346 * name to avoid collisions, such as "com.mycompany.wallpaper.DOIT".
1347 * @param x Arbitrary integer argument based on command.
1348 * @param y Arbitrary integer argument based on command.
1349 * @param z Arbitrary integer argument based on command.
1350 * @param extras Optional additional information for the command, or null.
1351 */
1352 public void sendWallpaperCommand(IBinder windowToken, String action,
1353 int x, int y, int z, Bundle extras) {
1354 try {
1355 //Log.v(TAG, "Sending new wallpaper offsets from app...");
Jeff Brownf9e989d2013-04-04 23:04:03 -07001356 WindowManagerGlobal.getWindowSession().sendWallpaperCommand(
Dianne Hackborn75804932009-10-20 20:15:20 -07001357 windowToken, action, x, y, z, extras, false);
1358 //Log.v(TAG, "...app returning after sending offsets!");
1359 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07001360 throw e.rethrowFromSystemServer();
Dianne Hackborn75804932009-10-20 20:15:20 -07001361 }
1362 }
Benjamin Franzf3ece362015-02-11 10:51:10 +00001363
1364 /**
1365 * Returns whether wallpapers are supported for the calling user. If this function returns
Oleksandr Peletskyif2519812016-01-26 20:16:06 +01001366 * {@code false}, any attempts to changing the wallpaper will have no effect,
1367 * and any attempt to obtain of the wallpaper will return {@code null}.
Benjamin Franzf3ece362015-02-11 10:51:10 +00001368 */
1369 public boolean isWallpaperSupported() {
1370 if (sGlobals.mService == null) {
1371 Log.w(TAG, "WallpaperService not running");
1372 } else {
1373 try {
1374 return sGlobals.mService.isWallpaperSupported(mContext.getOpPackageName());
1375 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07001376 throw e.rethrowFromSystemServer();
Benjamin Franzf3ece362015-02-11 10:51:10 +00001377 }
1378 }
1379 return false;
1380 }
1381
Dianne Hackborn75804932009-10-20 20:15:20 -07001382 /**
Oleksandr Peletskyif2519812016-01-26 20:16:06 +01001383 * Returns whether the calling package is allowed to set the wallpaper for the calling user.
1384 * If this function returns {@code false}, any attempts to change the wallpaper will have
1385 * no effect. Always returns {@code true} for device owner and profile owner.
1386 *
1387 * @see android.os.UserManager#DISALLOW_SET_WALLPAPER
1388 */
1389 public boolean isWallpaperSettingAllowed() {
1390 if (sGlobals.mService == null) {
1391 Log.w(TAG, "WallpaperService not running");
1392 } else {
1393 try {
1394 return sGlobals.mService.isWallpaperSettingAllowed(mContext.getOpPackageName());
1395 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07001396 throw e.rethrowFromSystemServer();
Oleksandr Peletskyif2519812016-01-26 20:16:06 +01001397 }
1398 }
1399 return false;
1400 }
1401
1402 /**
Dianne Hackborn72c82ab2009-08-11 21:13:54 -07001403 * Clear the offsets previously associated with this window through
1404 * {@link #setWallpaperOffsets(IBinder, float, float)}. This reverts
1405 * the window to its default state, where it does not cause the wallpaper
1406 * to scroll from whatever its last offsets were.
Oleksandr Peletskyif2519812016-01-26 20:16:06 +01001407 *
Dianne Hackborn72c82ab2009-08-11 21:13:54 -07001408 * @param windowToken The window who these offsets should be associated
Scott Main8b2e0002009-09-29 18:17:31 -07001409 * with, as returned by {@link android.view.View#getWindowToken()
Dianne Hackborn72c82ab2009-08-11 21:13:54 -07001410 * View.getWindowToken()}.
1411 */
1412 public void clearWallpaperOffsets(IBinder windowToken) {
1413 try {
Jeff Brownf9e989d2013-04-04 23:04:03 -07001414 WindowManagerGlobal.getWindowSession().setWallpaperPosition(
Marco Nelissenbf6956b2009-11-09 15:21:13 -08001415 windowToken, -1, -1, -1, -1);
Dianne Hackborn72c82ab2009-08-11 21:13:54 -07001416 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07001417 throw e.rethrowFromSystemServer();
Dianne Hackborn72c82ab2009-08-11 21:13:54 -07001418 }
1419 }
Oleksandr Peletskyif2519812016-01-26 20:16:06 +01001420
Dianne Hackborn72c82ab2009-08-11 21:13:54 -07001421 /**
Christopher Tatebe132e62016-02-10 12:59:49 -08001422 * Remove any currently set system wallpaper, reverting to the system's built-in
Dianne Hackborn8cc6a502009-08-05 21:29:42 -07001423 * wallpaper. On success, the intent {@link Intent#ACTION_WALLPAPER_CHANGED}
1424 * is broadcast.
1425 *
Nicolas Falliere9530e3a2012-06-18 17:21:06 -07001426 * <p>This method requires the caller to hold the permission
1427 * {@link android.Manifest.permission#SET_WALLPAPER}.
1428 *
Michael Jurkab668d0b2013-10-04 15:11:05 -07001429 * @throws IOException If an error occurs reverting to the built-in
Dianne Hackborn8cc6a502009-08-05 21:29:42 -07001430 * wallpaper.
1431 */
1432 public void clear() throws IOException {
Jeff Sharkey28f08772014-04-16 09:41:58 -07001433 setStream(openDefaultWallpaper(mContext));
1434 }
1435
1436 /**
Christopher Tate79a24572016-03-02 14:42:44 -08001437 * Remove one or more currently set wallpapers, reverting to the system default
1438 * display for each one. If {@link #FLAG_SET_SYSTEM} is set in the {@code which}
1439 * parameter, the intent {@link Intent#ACTION_WALLPAPER_CHANGED} will be broadcast
1440 * upon success.
1441 *
1442 * @param which A bitwise combination of {@link #FLAG_SET_SYSTEM} or
1443 * {@link #FLAG_SET_LOCK}
1444 * @throws IOException If an error occurs reverting to the built-in wallpaper.
1445 */
1446 public void clear(int which) throws IOException {
1447 if ((which & FLAG_SET_SYSTEM) != 0) {
1448 clear();
1449 }
1450 if ((which & FLAG_SET_LOCK) != 0) {
1451 clearWallpaper(FLAG_SET_LOCK, mContext.getUserId());
1452 }
1453 }
1454
1455 /**
Jeff Sharkey28f08772014-04-16 09:41:58 -07001456 * Open stream representing the default static image wallpaper.
1457 *
1458 * @hide
1459 */
1460 public static InputStream openDefaultWallpaper(Context context) {
1461 final String path = SystemProperties.get(PROP_WALLPAPER);
1462 if (!TextUtils.isEmpty(path)) {
1463 final File file = new File(path);
1464 if (file.exists()) {
1465 try {
1466 return new FileInputStream(file);
1467 } catch (IOException e) {
1468 // Ignored, fall back to platform default below
1469 }
1470 }
1471 }
1472 return context.getResources().openRawResource(
1473 com.android.internal.R.drawable.default_wallpaper);
1474 }
1475
1476 /**
1477 * Return {@link ComponentName} of the default live wallpaper, or
1478 * {@code null} if none is defined.
1479 *
1480 * @hide
1481 */
1482 public static ComponentName getDefaultWallpaperComponent(Context context) {
1483 String flat = SystemProperties.get(PROP_WALLPAPER_COMPONENT);
1484 if (!TextUtils.isEmpty(flat)) {
1485 final ComponentName cn = ComponentName.unflattenFromString(flat);
1486 if (cn != null) {
1487 return cn;
1488 }
1489 }
1490
1491 flat = context.getString(com.android.internal.R.string.default_wallpaper_component);
1492 if (!TextUtils.isEmpty(flat)) {
1493 final ComponentName cn = ComponentName.unflattenFromString(flat);
1494 if (cn != null) {
1495 return cn;
1496 }
1497 }
1498
1499 return null;
Dianne Hackborn8cc6a502009-08-05 21:29:42 -07001500 }
Christopher Tate1e1e2e02016-01-25 15:34:36 -08001501
Christopher Tatebe132e62016-02-10 12:59:49 -08001502 /**
1503 * Register a callback for lock wallpaper observation. Only the OS may use this.
1504 *
1505 * @return true on success; false on error.
1506 * @hide
1507 */
1508 public boolean setLockWallpaperCallback(IWallpaperManagerCallback callback) {
1509 if (sGlobals.mService == null) {
1510 Log.w(TAG, "WallpaperService not running");
1511 return false;
1512 }
1513
1514 try {
1515 return sGlobals.mService.setLockWallpaperCallback(callback);
1516 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07001517 throw e.rethrowFromSystemServer();
Christopher Tatebe132e62016-02-10 12:59:49 -08001518 }
Christopher Tatebe132e62016-02-10 12:59:49 -08001519 }
1520
Christopher Tate1e1e2e02016-01-25 15:34:36 -08001521 // Private completion callback for setWallpaper() synchronization
1522 private class WallpaperSetCompletion extends IWallpaperManagerCallback.Stub {
1523 final CountDownLatch mLatch;
1524
1525 public WallpaperSetCompletion() {
1526 mLatch = new CountDownLatch(1);
1527 }
1528
1529 public void waitForCompletion() {
1530 try {
1531 mLatch.await(30, TimeUnit.SECONDS);
1532 } catch (InterruptedException e) {
1533 // This might be legit: the crop may take a very long time. Don't sweat
1534 // it in that case; we are okay with display lagging behind in order to
1535 // keep the caller from locking up indeterminately.
1536 }
1537 }
1538
1539 @Override
1540 public void onWallpaperChanged() throws RemoteException {
1541 mLatch.countDown();
1542 }
1543 }
Dianne Hackborn8cc6a502009-08-05 21:29:42 -07001544}