blob: c3512eca3b50f98ce7ecfd3a28a95a14bbfbd6ad [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;
Christopher Tatebe132e62016-02-10 12:59:49 -080052import android.os.UserHandle;
Jeff Sharkey28f08772014-04-16 09:41:58 -070053import android.text.TextUtils;
Dianne Hackborn7341d7a2009-08-14 11:37:52 -070054import android.util.Log;
Jeff Brown98365d72012-08-19 20:30:52 -070055import android.view.WindowManagerGlobal;
Dianne Hackborn8cc6a502009-08-05 21:29:42 -070056
Christopher Tateffa6a882015-12-11 15:08:46 -080057import libcore.io.IoUtils;
58
Michael Jurkab668d0b2013-10-04 15:11:05 -070059import java.io.BufferedInputStream;
Jeff Sharkey28f08772014-04-16 09:41:58 -070060import java.io.File;
61import java.io.FileInputStream;
Dianne Hackborn8cc6a502009-08-05 21:29:42 -070062import java.io.FileOutputStream;
63import java.io.IOException;
64import java.io.InputStream;
Christopher Tatead3c2592016-01-20 18:13:17 -080065import java.lang.annotation.Retention;
66import java.lang.annotation.RetentionPolicy;
Michael Jurkae8d1bf72013-09-09 15:58:54 +020067import java.util.List;
Christopher Tate1e1e2e02016-01-25 15:34:36 -080068import java.util.concurrent.CountDownLatch;
69import java.util.concurrent.TimeUnit;
Dianne Hackborn8cc6a502009-08-05 21:29:42 -070070
Scott Main8b2e0002009-09-29 18:17:31 -070071/**
72 * Provides access to the system wallpaper. With WallpaperManager, you can
73 * get the current wallpaper, get the desired dimensions for the wallpaper, set
74 * the wallpaper, and more. Get an instance of WallpaperManager with
Benjamin Franzf3ece362015-02-11 10:51:10 +000075 * {@link #getInstance(android.content.Context) getInstance()}.
76 *
77 * <p> An app can check whether wallpapers are supported for the current user, by calling
Oleksandr Peletskyif2519812016-01-26 20:16:06 +010078 * {@link #isWallpaperSupported()}, and whether setting of wallpapers is allowed, by calling
79 * {@link #isWallpaperSettingAllowed()}.
Scott Main8b2e0002009-09-29 18:17:31 -070080 */
Dianne Hackborn8cc6a502009-08-05 21:29:42 -070081public class WallpaperManager {
82 private static String TAG = "WallpaperManager";
83 private static boolean DEBUG = false;
Marco Nelissenbf6956b2009-11-09 15:21:13 -080084 private float mWallpaperXStep = -1;
85 private float mWallpaperYStep = -1;
Dianne Hackborn8cc6a502009-08-05 21:29:42 -070086
Jeff Sharkey28f08772014-04-16 09:41:58 -070087 /** {@hide} */
88 private static final String PROP_WALLPAPER = "ro.config.wallpaper";
89 /** {@hide} */
90 private static final String PROP_WALLPAPER_COMPONENT = "ro.config.wallpaper_component";
91
Dianne Hackborn4c62fc02009-08-08 20:40:27 -070092 /**
Michael Jurkae8d1bf72013-09-09 15:58:54 +020093 * Activity Action: Show settings for choosing wallpaper. Do not use directly to construct
94 * an intent; instead, use {@link #getCropAndSetWallpaperIntent}.
95 * <p>Input: {@link Intent#getData} is the URI of the image to crop and set as wallpaper.
96 * <p>Output: RESULT_OK if user decided to crop/set the wallpaper, RESULT_CANCEL otherwise
Adam Lesinskibba72d12013-10-11 18:10:56 -070097 * Activities that support this intent should specify a MIME filter of "image/*"
Michael Jurkae8d1bf72013-09-09 15:58:54 +020098 */
99 public static final String ACTION_CROP_AND_SET_WALLPAPER =
100 "android.service.wallpaper.CROP_AND_SET_WALLPAPER";
101
102 /**
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700103 * Launch an activity for the user to pick the current global live
104 * wallpaper.
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700105 */
106 public static final String ACTION_LIVE_WALLPAPER_CHOOSER
107 = "android.service.wallpaper.LIVE_WALLPAPER_CHOOSER";
Adam Cohen5a242ec2010-12-07 21:07:07 -0800108
109 /**
Dianne Hackborn7df7d202012-04-19 18:00:04 -0700110 * Directly launch live wallpaper preview, allowing the user to immediately
111 * confirm to switch to a specific live wallpaper. You must specify
112 * {@link #EXTRA_LIVE_WALLPAPER_COMPONENT} with the ComponentName of
113 * a live wallpaper component that is to be shown.
114 */
115 public static final String ACTION_CHANGE_LIVE_WALLPAPER
116 = "android.service.wallpaper.CHANGE_LIVE_WALLPAPER";
117
118 /**
119 * Extra in {@link #ACTION_CHANGE_LIVE_WALLPAPER} that specifies the
120 * ComponentName of a live wallpaper that should be shown as a preview,
121 * for the user to confirm.
122 */
123 public static final String EXTRA_LIVE_WALLPAPER_COMPONENT
124 = "android.service.wallpaper.extra.LIVE_WALLPAPER_COMPONENT";
125
126 /**
Adam Cohen5a242ec2010-12-07 21:07:07 -0800127 * Manifest entry for activities that respond to {@link Intent#ACTION_SET_WALLPAPER}
128 * which allows them to provide a custom large icon associated with this action.
129 */
130 public static final String WALLPAPER_PREVIEW_META_DATA = "android.wallpaper.preview";
131
Dianne Hackborn23ef7b42009-11-18 18:20:39 -0800132 /**
133 * Command for {@link #sendWallpaperCommand}: reported by the wallpaper
134 * host when the user taps on an empty area (not performing an action
135 * in the host). The x and y arguments are the location of the tap in
136 * screen coordinates.
137 */
138 public static final String COMMAND_TAP = "android.wallpaper.tap";
Oleksandr Peletskyif2519812016-01-26 20:16:06 +0100139
Dianne Hackborn23ef7b42009-11-18 18:20:39 -0800140 /**
141 * Command for {@link #sendWallpaperCommand}: reported by the wallpaper
Jeff Brown9f3bdfe2010-10-13 06:01:27 -0700142 * host when the user releases a secondary pointer on an empty area
143 * (not performing an action in the host). The x and y arguments are
144 * the location of the secondary tap in screen coordinates.
145 */
146 public static final String COMMAND_SECONDARY_TAP = "android.wallpaper.secondaryTap";
147
148 /**
149 * Command for {@link #sendWallpaperCommand}: reported by the wallpaper
Dianne Hackborn23ef7b42009-11-18 18:20:39 -0800150 * host when the user drops an object into an area of the host. The x
151 * and y arguments are the location of the drop.
152 */
153 public static final String COMMAND_DROP = "android.home.drop";
Christopher Tatead3c2592016-01-20 18:13:17 -0800154
155 /**
156 * Extra passed back from setWallpaper() giving the new wallpaper's assigned ID.
157 * @hide
158 */
159 public static final String EXTRA_NEW_WALLPAPER_ID = "android.service.wallpaper.extra.ID";
160
161 // flags for which kind of wallpaper to set
162
163 /** @hide */
164 @IntDef(flag = true, value = {
165 FLAG_SET_SYSTEM,
166 FLAG_SET_LOCK
167 })
168 @Retention(RetentionPolicy.SOURCE)
169 public @interface SetWallpaperFlags {}
170
171 /**
172 * Flag: use the supplied imagery as the general system wallpaper.
173 */
174 public static final int FLAG_SET_SYSTEM = 1 << 0;
175
176 /**
177 * Flag: use the supplied imagery as the lock-screen wallpaper.
178 */
179 public static final int FLAG_SET_LOCK = 1 << 1;
180
Dianne Hackborn8cc6a502009-08-05 21:29:42 -0700181 private final Context mContext;
Oleksandr Peletskyif2519812016-01-26 20:16:06 +0100182
Dianne Hackborn19382ac2009-09-11 21:13:37 -0700183 /**
184 * Special drawable that draws a wallpaper as fast as possible. Assumes
185 * no scaling or placement off (0,0) of the wallpaper (this should be done
186 * at the time the bitmap is loaded).
187 */
188 static class FastBitmapDrawable extends Drawable {
189 private final Bitmap mBitmap;
190 private final int mWidth;
191 private final int mHeight;
192 private int mDrawLeft;
193 private int mDrawTop;
Romain Guy407ec782011-08-24 17:06:58 -0700194 private final Paint mPaint;
Dianne Hackborn19382ac2009-09-11 21:13:37 -0700195
196 private FastBitmapDrawable(Bitmap bitmap) {
197 mBitmap = bitmap;
198 mWidth = bitmap.getWidth();
199 mHeight = bitmap.getHeight();
Romain Guy407ec782011-08-24 17:06:58 -0700200
Dianne Hackborn19382ac2009-09-11 21:13:37 -0700201 setBounds(0, 0, mWidth, mHeight);
Romain Guy407ec782011-08-24 17:06:58 -0700202
203 mPaint = new Paint();
204 mPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC));
Dianne Hackborn19382ac2009-09-11 21:13:37 -0700205 }
206
207 @Override
208 public void draw(Canvas canvas) {
Romain Guy407ec782011-08-24 17:06:58 -0700209 canvas.drawBitmap(mBitmap, mDrawLeft, mDrawTop, mPaint);
Dianne Hackborn19382ac2009-09-11 21:13:37 -0700210 }
211
212 @Override
213 public int getOpacity() {
214 return PixelFormat.OPAQUE;
215 }
216
217 @Override
218 public void setBounds(int left, int top, int right, int bottom) {
219 mDrawLeft = left + (right-left - mWidth) / 2;
220 mDrawTop = top + (bottom-top - mHeight) / 2;
221 }
222
223 @Override
Dianne Hackborn19382ac2009-09-11 21:13:37 -0700224 public void setAlpha(int alpha) {
Romain Guy407ec782011-08-24 17:06:58 -0700225 throw new UnsupportedOperationException("Not supported with this drawable");
Dianne Hackborn19382ac2009-09-11 21:13:37 -0700226 }
227
228 @Override
Chris Craikbd3bfc52015-03-02 10:43:29 -0800229 public void setColorFilter(ColorFilter colorFilter) {
Romain Guy407ec782011-08-24 17:06:58 -0700230 throw new UnsupportedOperationException("Not supported with this drawable");
Dianne Hackborn19382ac2009-09-11 21:13:37 -0700231 }
232
233 @Override
234 public void setDither(boolean dither) {
Romain Guy407ec782011-08-24 17:06:58 -0700235 throw new UnsupportedOperationException("Not supported with this drawable");
Dianne Hackborn19382ac2009-09-11 21:13:37 -0700236 }
237
238 @Override
239 public void setFilterBitmap(boolean filter) {
Romain Guy407ec782011-08-24 17:06:58 -0700240 throw new UnsupportedOperationException("Not supported with this drawable");
Dianne Hackborn19382ac2009-09-11 21:13:37 -0700241 }
242
243 @Override
244 public int getIntrinsicWidth() {
245 return mWidth;
246 }
247
248 @Override
249 public int getIntrinsicHeight() {
250 return mHeight;
251 }
252
253 @Override
254 public int getMinimumWidth() {
255 return mWidth;
256 }
257
258 @Override
259 public int getMinimumHeight() {
260 return mHeight;
261 }
262 }
Oleksandr Peletskyif2519812016-01-26 20:16:06 +0100263
Dianne Hackborn8cc6a502009-08-05 21:29:42 -0700264 static class Globals extends IWallpaperManagerCallback.Stub {
265 private IWallpaperManager mService;
Dianne Hackborn284ac932009-08-28 10:34:25 -0700266 private Bitmap mWallpaper;
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) {
296 // Ignore
297 }
298 }
Dianne Hackborn8cc6a502009-08-05 21:29:42 -0700299 if (mWallpaper != null) {
300 return mWallpaper;
301 }
Dianne Hackborn19382ac2009-09-11 21:13:37 -0700302 if (mDefaultWallpaper != null) {
303 return mDefaultWallpaper;
304 }
Dianne Hackbornc9dbbe22009-11-11 22:50:37 -0800305 mWallpaper = null;
306 try {
Yorke Leedcd93cc2016-01-08 14:12:55 -0800307 mWallpaper = getCurrentWallpaperLocked(userId);
Dianne Hackbornc9dbbe22009-11-11 22:50:37 -0800308 } catch (OutOfMemoryError e) {
309 Log.w(TAG, "No memory load current wallpaper", e);
310 }
Dianne Hackbornba398392011-08-01 16:11:57 -0700311 if (returnDefault) {
312 if (mWallpaper == null) {
313 mDefaultWallpaper = getDefaultWallpaperLocked(context);
314 return mDefaultWallpaper;
315 } else {
316 mDefaultWallpaper = null;
317 }
Dianne Hackborn19382ac2009-09-11 21:13:37 -0700318 }
Dianne Hackborn8cc6a502009-08-05 21:29:42 -0700319 return mWallpaper;
320 }
321 }
Dianne Hackbornba398392011-08-01 16:11:57 -0700322
323 public void forgetLoadedWallpaper() {
324 synchronized (this) {
325 mWallpaper = null;
326 mDefaultWallpaper = null;
327 }
328 }
329
Yorke Leedcd93cc2016-01-08 14:12:55 -0800330 private Bitmap getCurrentWallpaperLocked(int userId) {
Adam Lesinski2c8d67c2014-04-23 13:46:21 -0700331 if (mService == null) {
332 Log.w(TAG, "WallpaperService not running");
333 return null;
334 }
335
Dianne Hackborn8cc6a502009-08-05 21:29:42 -0700336 try {
Dianne Hackborn284ac932009-08-28 10:34:25 -0700337 Bundle params = new Bundle();
Christopher Tatebe132e62016-02-10 12:59:49 -0800338 ParcelFileDescriptor fd = mService.getWallpaper(this, FLAG_SET_SYSTEM,
Yorke Leedcd93cc2016-01-08 14:12:55 -0800339 params, userId);
Dianne Hackborn8cc6a502009-08-05 21:29:42 -0700340 if (fd != null) {
Jeff Brown24572372011-06-09 19:05:15 -0700341 try {
342 BitmapFactory.Options options = new BitmapFactory.Options();
Michael Jurka824a4b52013-12-18 17:10:16 +0100343 return BitmapFactory.decodeFileDescriptor(
Jeff Brown24572372011-06-09 19:05:15 -0700344 fd.getFileDescriptor(), null, options);
Jeff Brown24572372011-06-09 19:05:15 -0700345 } catch (OutOfMemoryError e) {
346 Log.w(TAG, "Can't decode file", e);
347 } finally {
Christopher Tateffa6a882015-12-11 15:08:46 -0800348 IoUtils.closeQuietly(fd);
Dianne Hackborn8cc6a502009-08-05 21:29:42 -0700349 }
Dianne Hackborn19382ac2009-09-11 21:13:37 -0700350 }
351 } catch (RemoteException e) {
Romain Guy407ec782011-08-24 17:06:58 -0700352 // Ignore
Dianne Hackborn19382ac2009-09-11 21:13:37 -0700353 }
354 return null;
355 }
Christopher Tateffa6a882015-12-11 15:08:46 -0800356
Dianne Hackborn19382ac2009-09-11 21:13:37 -0700357 private Bitmap getDefaultWallpaperLocked(Context context) {
Adam Lesinskie929bee2014-04-28 14:15:06 -0700358 InputStream is = openDefaultWallpaper(context);
Adam Lesinski2c8d67c2014-04-23 13:46:21 -0700359 if (is != null) {
360 try {
361 BitmapFactory.Options options = new BitmapFactory.Options();
362 return BitmapFactory.decodeStream(is, null, options);
363 } catch (OutOfMemoryError e) {
364 Log.w(TAG, "Can't decode stream", e);
365 } finally {
Christopher Tateffa6a882015-12-11 15:08:46 -0800366 IoUtils.closeQuietly(is);
Dianne Hackborn8cc6a502009-08-05 21:29:42 -0700367 }
Dianne Hackborn8cc6a502009-08-05 21:29:42 -0700368 }
369 return null;
370 }
371 }
Oleksandr Peletskyif2519812016-01-26 20:16:06 +0100372
Romain Guy407ec782011-08-24 17:06:58 -0700373 private static final Object sSync = new Object[0];
Dianne Hackborn8cc6a502009-08-05 21:29:42 -0700374 private static Globals sGlobals;
375
Dianne Hackborn840c3a22009-09-02 17:35:17 -0700376 static void initGlobals(Looper looper) {
Romain Guy407ec782011-08-24 17:06:58 -0700377 synchronized (sSync) {
Dianne Hackborn8cc6a502009-08-05 21:29:42 -0700378 if (sGlobals == null) {
Dianne Hackborn5e802fb2009-09-02 21:50:50 -0700379 sGlobals = new Globals(looper);
Dianne Hackborn8cc6a502009-08-05 21:29:42 -0700380 }
Dianne Hackborn8cc6a502009-08-05 21:29:42 -0700381 }
382 }
Oleksandr Peletskyif2519812016-01-26 20:16:06 +0100383
Dianne Hackborn8cc6a502009-08-05 21:29:42 -0700384 /*package*/ WallpaperManager(Context context, Handler handler) {
385 mContext = context;
Dianne Hackborn840c3a22009-09-02 17:35:17 -0700386 initGlobals(context.getMainLooper());
Dianne Hackborn8cc6a502009-08-05 21:29:42 -0700387 }
388
389 /**
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700390 * Retrieve a WallpaperManager associated with the given Context.
391 */
392 public static WallpaperManager getInstance(Context context) {
393 return (WallpaperManager)context.getSystemService(
394 Context.WALLPAPER_SERVICE);
395 }
Oleksandr Peletskyif2519812016-01-26 20:16:06 +0100396
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700397 /** @hide */
398 public IWallpaperManager getIWallpaperManager() {
Dianne Hackborn840c3a22009-09-02 17:35:17 -0700399 return sGlobals.mService;
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700400 }
Oleksandr Peletskyif2519812016-01-26 20:16:06 +0100401
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700402 /**
Scott Main8b2e0002009-09-29 18:17:31 -0700403 * Retrieve the current system wallpaper; if
Michael Jurkab668d0b2013-10-04 15:11:05 -0700404 * no wallpaper is set, the system built-in static wallpaper is returned.
Scott Main8b2e0002009-09-29 18:17:31 -0700405 * This is returned as an
406 * abstract Drawable that you can install in a View to display whatever
Oleksandr Peletskyif2519812016-01-26 20:16:06 +0100407 * wallpaper the user has currently set.
Dianne Hackborn8cc6a502009-08-05 21:29:42 -0700408 *
409 * @return Returns a Drawable object that will draw the wallpaper.
410 */
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700411 public Drawable getDrawable() {
Dianne Hackborn19382ac2009-09-11 21:13:37 -0700412 Bitmap bm = sGlobals.peekWallpaperBitmap(mContext, true);
413 if (bm != null) {
414 Drawable dr = new BitmapDrawable(mContext.getResources(), bm);
415 dr.setDither(false);
416 return dr;
417 }
418 return null;
Dianne Hackborn8cc6a502009-08-05 21:29:42 -0700419 }
420
421 /**
Michael Jurkab668d0b2013-10-04 15:11:05 -0700422 * Returns a drawable for the system built-in static wallpaper .
423 *
424 */
425 public Drawable getBuiltInDrawable() {
426 return getBuiltInDrawable(0, 0, false, 0, 0);
427 }
428
429 /**
430 * Returns a drawable for the system built-in static wallpaper. Based on the parameters, the
431 * drawable can be cropped and scaled
432 *
433 * @param outWidth The width of the returned drawable
434 * @param outWidth The height of the returned drawable
435 * @param scaleToFit If true, scale the wallpaper down rather than just cropping it
436 * @param horizontalAlignment A float value between 0 and 1 specifying where to crop the image;
437 * 0 for left-aligned, 0.5 for horizontal center-aligned, and 1 for right-aligned
438 * @param verticalAlignment A float value between 0 and 1 specifying where to crop the image;
439 * 0 for top-aligned, 0.5 for vertical center-aligned, and 1 for bottom-aligned
440 *
441 */
442 public Drawable getBuiltInDrawable(int outWidth, int outHeight,
443 boolean scaleToFit, float horizontalAlignment, float verticalAlignment) {
444 if (sGlobals.mService == null) {
445 Log.w(TAG, "WallpaperService not running");
446 return null;
447 }
448 Resources resources = mContext.getResources();
449 horizontalAlignment = Math.max(0, Math.min(1, horizontalAlignment));
450 verticalAlignment = Math.max(0, Math.min(1, verticalAlignment));
451
Jeff Sharkey28f08772014-04-16 09:41:58 -0700452 InputStream is = new BufferedInputStream(openDefaultWallpaper(mContext));
Michael Jurkab668d0b2013-10-04 15:11:05 -0700453
454 if (is == null) {
455 Log.e(TAG, "default wallpaper input stream is null");
456 return null;
457 } else {
458 if (outWidth <= 0 || outHeight <= 0) {
459 Bitmap fullSize = BitmapFactory.decodeStream(is, null, null);
460 return new BitmapDrawable(resources, fullSize);
461 } else {
462 int inWidth;
463 int inHeight;
464 {
465 BitmapFactory.Options options = new BitmapFactory.Options();
466 options.inJustDecodeBounds = true;
467 BitmapFactory.decodeStream(is, null, options);
468 if (options.outWidth != 0 && options.outHeight != 0) {
469 inWidth = options.outWidth;
470 inHeight = options.outHeight;
471 } else {
472 Log.e(TAG, "default wallpaper dimensions are 0");
473 return null;
474 }
475 }
476
Jeff Sharkey28f08772014-04-16 09:41:58 -0700477 is = new BufferedInputStream(openDefaultWallpaper(mContext));
Michael Jurkab668d0b2013-10-04 15:11:05 -0700478
479 RectF cropRectF;
480
481 outWidth = Math.min(inWidth, outWidth);
482 outHeight = Math.min(inHeight, outHeight);
483 if (scaleToFit) {
484 cropRectF = getMaxCropRect(inWidth, inHeight, outWidth, outHeight,
485 horizontalAlignment, verticalAlignment);
486 } else {
487 float left = (inWidth - outWidth) * horizontalAlignment;
488 float right = left + outWidth;
489 float top = (inHeight - outHeight) * verticalAlignment;
490 float bottom = top + outHeight;
Michael Jurka05391532013-10-14 20:44:42 -0700491 cropRectF = new RectF(left, top, right, bottom);
Michael Jurkab668d0b2013-10-04 15:11:05 -0700492 }
493 Rect roundedTrueCrop = new Rect();
494 cropRectF.roundOut(roundedTrueCrop);
495
496 if (roundedTrueCrop.width() <= 0 || roundedTrueCrop.height() <= 0) {
497 Log.w(TAG, "crop has bad values for full size image");
498 return null;
499 }
500
501 // See how much we're reducing the size of the image
502 int scaleDownSampleSize = Math.min(roundedTrueCrop.width() / outWidth,
503 roundedTrueCrop.height() / outHeight);
504
505 // Attempt to open a region decoder
506 BitmapRegionDecoder decoder = null;
507 try {
508 decoder = BitmapRegionDecoder.newInstance(is, true);
509 } catch (IOException e) {
510 Log.w(TAG, "cannot open region decoder for default wallpaper");
511 }
512
513 Bitmap crop = null;
514 if (decoder != null) {
515 // Do region decoding to get crop bitmap
516 BitmapFactory.Options options = new BitmapFactory.Options();
517 if (scaleDownSampleSize > 1) {
518 options.inSampleSize = scaleDownSampleSize;
519 }
520 crop = decoder.decodeRegion(roundedTrueCrop, options);
521 decoder.recycle();
522 }
523
524 if (crop == null) {
525 // BitmapRegionDecoder has failed, try to crop in-memory
Jeff Sharkey28f08772014-04-16 09:41:58 -0700526 is = new BufferedInputStream(openDefaultWallpaper(mContext));
Michael Jurkab668d0b2013-10-04 15:11:05 -0700527 Bitmap fullSize = null;
528 if (is != null) {
529 BitmapFactory.Options options = new BitmapFactory.Options();
530 if (scaleDownSampleSize > 1) {
531 options.inSampleSize = scaleDownSampleSize;
532 }
533 fullSize = BitmapFactory.decodeStream(is, null, options);
534 }
535 if (fullSize != null) {
536 crop = Bitmap.createBitmap(fullSize, roundedTrueCrop.left,
537 roundedTrueCrop.top, roundedTrueCrop.width(),
538 roundedTrueCrop.height());
539 }
540 }
541
542 if (crop == null) {
543 Log.w(TAG, "cannot decode default wallpaper");
544 return null;
545 }
546
547 // Scale down if necessary
548 if (outWidth > 0 && outHeight > 0 &&
549 (crop.getWidth() != outWidth || crop.getHeight() != outHeight)) {
550 Matrix m = new Matrix();
551 RectF cropRect = new RectF(0, 0, crop.getWidth(), crop.getHeight());
552 RectF returnRect = new RectF(0, 0, outWidth, outHeight);
553 m.setRectToRect(cropRect, returnRect, Matrix.ScaleToFit.FILL);
554 Bitmap tmp = Bitmap.createBitmap((int) returnRect.width(),
555 (int) returnRect.height(), Bitmap.Config.ARGB_8888);
556 if (tmp != null) {
557 Canvas c = new Canvas(tmp);
558 Paint p = new Paint();
559 p.setFilterBitmap(true);
560 c.drawBitmap(crop, m, p);
561 crop = tmp;
562 }
563 }
564
565 return new BitmapDrawable(resources, crop);
566 }
567 }
568 }
569
570 private static RectF getMaxCropRect(int inWidth, int inHeight, int outWidth, int outHeight,
571 float horizontalAlignment, float verticalAlignment) {
572 RectF cropRect = new RectF();
573 // Get a crop rect that will fit this
574 if (inWidth / (float) inHeight > outWidth / (float) outHeight) {
575 cropRect.top = 0;
576 cropRect.bottom = inHeight;
577 float cropWidth = outWidth * (inHeight / (float) outHeight);
578 cropRect.left = (inWidth - cropWidth) * horizontalAlignment;
579 cropRect.right = cropRect.left + cropWidth;
580 } else {
581 cropRect.left = 0;
582 cropRect.right = inWidth;
583 float cropHeight = outHeight * (inWidth / (float) outWidth);
584 cropRect.top = (inHeight - cropHeight) * verticalAlignment;
585 cropRect.bottom = cropRect.top + cropHeight;
586 }
587 return cropRect;
588 }
589
590 /**
Scott Main8b2e0002009-09-29 18:17:31 -0700591 * Retrieve the current system wallpaper; if there is no wallpaper set,
592 * a null pointer is returned. This is returned as an
Dianne Hackborn8cc6a502009-08-05 21:29:42 -0700593 * abstract Drawable that you can install in a View to display whatever
Oleksandr Peletskyif2519812016-01-26 20:16:06 +0100594 * wallpaper the user has currently set.
Dianne Hackborn8cc6a502009-08-05 21:29:42 -0700595 *
596 * @return Returns a Drawable object that will draw the wallpaper or a
597 * null pointer if these is none.
598 */
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700599 public Drawable peekDrawable() {
Dianne Hackborn19382ac2009-09-11 21:13:37 -0700600 Bitmap bm = sGlobals.peekWallpaperBitmap(mContext, false);
601 if (bm != null) {
602 Drawable dr = new BitmapDrawable(mContext.getResources(), bm);
603 dr.setDither(false);
604 return dr;
605 }
606 return null;
607 }
608
609 /**
Scott Main8b2e0002009-09-29 18:17:31 -0700610 * Like {@link #getDrawable()}, but the returned Drawable has a number
611 * of limitations to reduce its overhead as much as possible. It will
612 * never scale the wallpaper (only centering it if the requested bounds
613 * do match the bitmap bounds, which should not be typical), doesn't
614 * allow setting an alpha, color filter, or other attributes, etc. The
615 * bounds of the returned drawable will be initialized to the same bounds
616 * as the wallpaper, so normally you will not need to touch it. The
617 * drawable also assumes that it will be used in a context running in
618 * the same density as the screen (not in density compatibility mode).
Dianne Hackborn19382ac2009-09-11 21:13:37 -0700619 *
620 * @return Returns a Drawable object that will draw the wallpaper.
621 */
622 public Drawable getFastDrawable() {
623 Bitmap bm = sGlobals.peekWallpaperBitmap(mContext, true);
624 if (bm != null) {
Romain Guy407ec782011-08-24 17:06:58 -0700625 return new FastBitmapDrawable(bm);
Dianne Hackborn19382ac2009-09-11 21:13:37 -0700626 }
627 return null;
628 }
629
630 /**
Scott Main8b2e0002009-09-29 18:17:31 -0700631 * Like {@link #getFastDrawable()}, but if there is no wallpaper set,
632 * a null pointer is returned.
Dianne Hackborn19382ac2009-09-11 21:13:37 -0700633 *
634 * @return Returns an optimized Drawable object that will draw the
635 * wallpaper or a null pointer if these is none.
636 */
637 public Drawable peekFastDrawable() {
Christopher Tatebe132e62016-02-10 12:59:49 -0800638 Bitmap bm = sGlobals.peekWallpaperBitmap(mContext, false);
Dianne Hackborn19382ac2009-09-11 21:13:37 -0700639 if (bm != null) {
Romain Guy407ec782011-08-24 17:06:58 -0700640 return new FastBitmapDrawable(bm);
Dianne Hackborn19382ac2009-09-11 21:13:37 -0700641 }
642 return null;
Dianne Hackborn8cc6a502009-08-05 21:29:42 -0700643 }
644
645 /**
Romain Guy407ec782011-08-24 17:06:58 -0700646 * Like {@link #getDrawable()} but returns a Bitmap.
Oleksandr Peletskyif2519812016-01-26 20:16:06 +0100647 *
Romain Guy407ec782011-08-24 17:06:58 -0700648 * @hide
649 */
650 public Bitmap getBitmap() {
Yorke Leedcd93cc2016-01-08 14:12:55 -0800651 return getBitmapAsUser(mContext.getUserId());
652 }
653
654 /**
655 * Like {@link #getDrawable()} but returns a Bitmap for the provided user.
656 *
657 * @hide
658 */
659 public Bitmap getBitmapAsUser(int userId) {
660 return sGlobals.peekWallpaperBitmap(mContext, true, userId);
Romain Guy407ec782011-08-24 17:06:58 -0700661 }
662
663 /**
Christopher Tatebe132e62016-02-10 12:59:49 -0800664 * Get an open, readable file descriptor to the given wallpaper image file.
665 * The callee is resopnsible for closing the fd when done ingesting the file.
666 *
667 * <p>If no lock-specific wallpaper has been configured for the given user, then
668 * this method will return {@code null} when requesting {@link #FLAG_SET_LOCK} rather than
669 * returning the system wallpaper's image file.
670 */
671 public ParcelFileDescriptor getWallpaperFile(int which) {
672 return getWallpaperFile(which, mContext.getUserId());
673 }
674
675 /**
676 * Version of {@link #getWallpaperFile(int)} that can access the wallpaper data
677 * for a given user. The caller must hold the INTERACT_ACROSS_USERS_FULL
678 * permission to access another user's wallpaper data.
679 * @hide
680 */
681 public ParcelFileDescriptor getWallpaperFile(int which, int userId) {
682 if (which != FLAG_SET_SYSTEM && which != FLAG_SET_LOCK) {
683 throw new IllegalArgumentException("Must request exactly one kind of wallpaper");
684 }
685
686 if (sGlobals.mService == null) {
687 Log.w(TAG, "WallpaperService not running");
688 return null;
689 } else {
690 try {
691 Bundle outParams = new Bundle();
692 return sGlobals.mService.getWallpaper(null, which, outParams, userId);
693 } catch (RemoteException e) {
694 return null;
695 }
696 }
697 }
698
699 /**
Dianne Hackbornba398392011-08-01 16:11:57 -0700700 * Remove all internal references to the last loaded wallpaper. Useful
701 * for apps that want to reduce memory usage when they only temporarily
702 * need to have the wallpaper. After calling, the next request for the
703 * wallpaper will require reloading it again from disk.
704 */
705 public void forgetLoadedWallpaper() {
Christopher Tatebe132e62016-02-10 12:59:49 -0800706 sGlobals.forgetLoadedWallpaper();
Dianne Hackbornba398392011-08-01 16:11:57 -0700707 }
708
709 /**
Dianne Hackborneb034652009-09-07 00:49:58 -0700710 * If the current wallpaper is a live wallpaper component, return the
711 * information about that wallpaper. Otherwise, if it is a static image,
712 * simply return null.
Dianne Hackborneb034652009-09-07 00:49:58 -0700713 */
714 public WallpaperInfo getWallpaperInfo() {
715 try {
Mike Lockwoodc067c9c2011-10-31 12:50:12 -0400716 if (sGlobals.mService == null) {
717 Log.w(TAG, "WallpaperService not running");
718 return null;
719 } else {
720 return sGlobals.mService.getWallpaperInfo();
721 }
Dianne Hackborneb034652009-09-07 00:49:58 -0700722 } catch (RemoteException e) {
723 return null;
724 }
725 }
Michael Jurkae8d1bf72013-09-09 15:58:54 +0200726
727 /**
728 * Gets an Intent that will launch an activity that crops the given
729 * image and sets the device's wallpaper. If there is a default HOME activity
730 * that supports cropping wallpapers, it will be preferred as the default.
Ying Wang930d4e52013-09-14 11:57:17 -0700731 * Use this method instead of directly creating a {@link #ACTION_CROP_AND_SET_WALLPAPER}
Michael Jurkae8d1bf72013-09-09 15:58:54 +0200732 * intent.
Adam Lesinskibba72d12013-10-11 18:10:56 -0700733 *
734 * @param imageUri The image URI that will be set in the intent. The must be a content
735 * URI and its provider must resolve its type to "image/*"
736 *
737 * @throws IllegalArgumentException if the URI is not a content URI or its MIME type is
738 * not "image/*"
Michael Jurkae8d1bf72013-09-09 15:58:54 +0200739 */
740 public Intent getCropAndSetWallpaperIntent(Uri imageUri) {
Allen Shen1c0f21e2014-04-29 16:16:29 +0800741 if (imageUri == null) {
742 throw new IllegalArgumentException("Image URI must not be null");
743 }
744
Adam Lesinskibba72d12013-10-11 18:10:56 -0700745 if (!ContentResolver.SCHEME_CONTENT.equals(imageUri.getScheme())) {
746 throw new IllegalArgumentException("Image URI must be of the "
747 + ContentResolver.SCHEME_CONTENT + " scheme type");
748 }
749
Michael Jurkae8d1bf72013-09-09 15:58:54 +0200750 final PackageManager packageManager = mContext.getPackageManager();
751 Intent cropAndSetWallpaperIntent =
752 new Intent(ACTION_CROP_AND_SET_WALLPAPER, imageUri);
753 cropAndSetWallpaperIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
754
755 // Find out if the default HOME activity supports CROP_AND_SET_WALLPAPER
756 Intent homeIntent = new Intent(Intent.ACTION_MAIN).addCategory(Intent.CATEGORY_HOME);
757 ResolveInfo resolvedHome = packageManager.resolveActivity(homeIntent,
758 PackageManager.MATCH_DEFAULT_ONLY);
759 if (resolvedHome != null) {
760 cropAndSetWallpaperIntent.setPackage(resolvedHome.activityInfo.packageName);
761
762 List<ResolveInfo> cropAppList = packageManager.queryIntentActivities(
763 cropAndSetWallpaperIntent, 0);
764 if (cropAppList.size() > 0) {
765 return cropAndSetWallpaperIntent;
766 }
767 }
768
769 // fallback crop activity
770 cropAndSetWallpaperIntent.setPackage("com.android.wallpapercropper");
Adam Lesinskibba72d12013-10-11 18:10:56 -0700771 List<ResolveInfo> cropAppList = packageManager.queryIntentActivities(
772 cropAndSetWallpaperIntent, 0);
773 if (cropAppList.size() > 0) {
774 return cropAndSetWallpaperIntent;
775 }
776 // If the URI is not of the right type, or for some reason the system wallpaper
777 // cropper doesn't exist, return null
778 throw new IllegalArgumentException("Cannot use passed URI to set wallpaper; " +
779 "check that the type returned by ContentProvider matches image/*");
Michael Jurkae8d1bf72013-09-09 15:58:54 +0200780 }
781
Dianne Hackborneb034652009-09-07 00:49:58 -0700782 /**
Dianne Hackborn8cc6a502009-08-05 21:29:42 -0700783 * Change the current system wallpaper to the bitmap in the given resource.
784 * The resource is opened as a raw data stream and copied into the
785 * wallpaper; it must be a valid PNG or JPEG image. On success, the intent
786 * {@link Intent#ACTION_WALLPAPER_CHANGED} is broadcast.
787 *
Nicolas Falliere9530e3a2012-06-18 17:21:06 -0700788 * <p>This method requires the caller to hold the permission
789 * {@link android.Manifest.permission#SET_WALLPAPER}.
790 *
Dianne Hackborn8cc6a502009-08-05 21:29:42 -0700791 * @param resid The bitmap to save.
792 *
Michael Jurkab668d0b2013-10-04 15:11:05 -0700793 * @throws IOException If an error occurs reverting to the built-in
Dianne Hackborn8cc6a502009-08-05 21:29:42 -0700794 * wallpaper.
795 */
Tor Norbye7b9c9122013-05-30 16:48:33 -0700796 public void setResource(@RawRes int resid) throws IOException {
Christopher Tatead3c2592016-01-20 18:13:17 -0800797 setResource(resid, FLAG_SET_SYSTEM);
798 }
799
800 /**
801 * Version of {@link #setResource(int)} that takes an optional Bundle for returning
802 * metadata about the operation to the caller.
803 *
804 * @param resid
805 * @param which Flags indicating which wallpaper(s) to configure with the new imagery.
806 *
807 * @see #FLAG_SET_LOCK
808 * @see #FLAG_SET_SYSTEM
809 *
810 * @return An integer ID assigned to the newly active wallpaper; or zero on failure.
811 *
812 * @throws IOException
813 */
814 public int setResource(@RawRes int resid, @SetWallpaperFlags int which)
815 throws IOException {
Mike Lockwoodc067c9c2011-10-31 12:50:12 -0400816 if (sGlobals.mService == null) {
817 Log.w(TAG, "WallpaperService not running");
Christopher Tatead3c2592016-01-20 18:13:17 -0800818 return 0;
Mike Lockwoodc067c9c2011-10-31 12:50:12 -0400819 }
Christopher Tatead3c2592016-01-20 18:13:17 -0800820 final Bundle result = new Bundle();
Christopher Tate1e1e2e02016-01-25 15:34:36 -0800821 final WallpaperSetCompletion completion = new WallpaperSetCompletion();
Dianne Hackborn8cc6a502009-08-05 21:29:42 -0700822 try {
823 Resources resources = mContext.getResources();
824 /* Set the wallpaper to the default values */
Dianne Hackborn840c3a22009-09-02 17:35:17 -0700825 ParcelFileDescriptor fd = sGlobals.mService.setWallpaper(
Christopher Tatead3c2592016-01-20 18:13:17 -0800826 "res:" + resources.getResourceName(resid),
Christopher Tate1e1e2e02016-01-25 15:34:36 -0800827 mContext.getOpPackageName(), null, result, which, completion);
Dianne Hackborn8cc6a502009-08-05 21:29:42 -0700828 if (fd != null) {
829 FileOutputStream fos = null;
Christopher Tate1e1e2e02016-01-25 15:34:36 -0800830 boolean ok = false;
Dianne Hackborn8cc6a502009-08-05 21:29:42 -0700831 try {
832 fos = new ParcelFileDescriptor.AutoCloseOutputStream(fd);
Christopher Tatead3c2592016-01-20 18:13:17 -0800833 copyStreamToWallpaperFile(resources.openRawResource(resid), fos);
Christopher Tate1e1e2e02016-01-25 15:34:36 -0800834 // The 'close()' is the trigger for any server-side image manipulation,
835 // so we must do that before waiting for completion.
836 fos.close();
837 completion.waitForCompletion();
Dianne Hackborn8cc6a502009-08-05 21:29:42 -0700838 } finally {
Christopher Tate1e1e2e02016-01-25 15:34:36 -0800839 // Might be redundant but completion shouldn't wait unless the write
840 // succeeded; this is a fallback if it threw past the close+wait.
Christopher Tateffa6a882015-12-11 15:08:46 -0800841 IoUtils.closeQuietly(fos);
Dianne Hackborn8cc6a502009-08-05 21:29:42 -0700842 }
843 }
844 } catch (RemoteException e) {
Romain Guy407ec782011-08-24 17:06:58 -0700845 // Ignore
Dianne Hackborn8cc6a502009-08-05 21:29:42 -0700846 }
Christopher Tatead3c2592016-01-20 18:13:17 -0800847 return result.getInt(EXTRA_NEW_WALLPAPER_ID, 0);
Dianne Hackborn8cc6a502009-08-05 21:29:42 -0700848 }
Christopher Tateffa6a882015-12-11 15:08:46 -0800849
Dianne Hackborn8cc6a502009-08-05 21:29:42 -0700850 /**
851 * Change the current system wallpaper to a bitmap. The given bitmap is
852 * converted to a PNG and stored as the wallpaper. On success, the intent
853 * {@link Intent#ACTION_WALLPAPER_CHANGED} is broadcast.
854 *
Christopher Tateffa6a882015-12-11 15:08:46 -0800855 * <p>This method is equivalent to calling
856 * {@link #setBitmap(Bitmap, Rect, boolean)} and passing {@code null} for the
857 * {@code visibleCrop} rectangle and {@code true} for the {@code allowBackup}
858 * parameter.
859 *
Nicolas Falliere9530e3a2012-06-18 17:21:06 -0700860 * <p>This method requires the caller to hold the permission
861 * {@link android.Manifest.permission#SET_WALLPAPER}.
862 *
Christopher Tatead3c2592016-01-20 18:13:17 -0800863 * @param bitmap The bitmap to be used as the new system wallpaper.
Dianne Hackborn8cc6a502009-08-05 21:29:42 -0700864 *
Christopher Tateffa6a882015-12-11 15:08:46 -0800865 * @throws IOException If an error occurs when attempting to set the wallpaper
866 * to the provided image.
Dianne Hackborn8cc6a502009-08-05 21:29:42 -0700867 */
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700868 public void setBitmap(Bitmap bitmap) throws IOException {
Christopher Tateffa6a882015-12-11 15:08:46 -0800869 setBitmap(bitmap, null, true);
870 }
871
872 /**
873 * Change the current system wallpaper to a bitmap, specifying a hint about
874 * which subrectangle of the full image is to be visible. The OS will then
875 * try to best present the given portion of the full image as the static system
876 * wallpaper image. On success, the intent
877 * {@link Intent#ACTION_WALLPAPER_CHANGED} is broadcast.
878 *
879 * <p>Passing {@code null} as the {@code visibleHint} parameter is equivalent to
880 * passing (0, 0, {@code fullImage.getWidth()}, {@code fullImage.getHeight()}).
881 *
882 * <p>This method requires the caller to hold the permission
883 * {@link android.Manifest.permission#SET_WALLPAPER}.
884 *
Christopher Tatead3c2592016-01-20 18:13:17 -0800885 * @param fullImage A bitmap that will supply the wallpaper imagery.
Christopher Tateffa6a882015-12-11 15:08:46 -0800886 * @param visibleCropHint The rectangular subregion of {@code fullImage} that should be
887 * displayed as wallpaper. Passing {@code null} for this parameter means that
888 * the full image should be displayed if possible given the image's and device's
Christopher Tatead3c2592016-01-20 18:13:17 -0800889 * aspect ratios, etc.
Christopher Tateffa6a882015-12-11 15:08:46 -0800890 * @param allowBackup {@code true} if the OS is permitted to back up this wallpaper
891 * image for restore to a future device; {@code false} otherwise.
892 *
Christopher Tatead3c2592016-01-20 18:13:17 -0800893 * @return An integer ID assigned to the newly active wallpaper; or zero on failure.
894 *
Christopher Tateffa6a882015-12-11 15:08:46 -0800895 * @throws IOException If an error occurs when attempting to set the wallpaper
896 * to the provided image.
897 * @throws IllegalArgumentException If the {@code visibleCropHint} rectangle is
898 * empty or invalid.
899 */
Christopher Tatead3c2592016-01-20 18:13:17 -0800900 public int setBitmap(Bitmap fullImage, Rect visibleCropHint, boolean allowBackup)
901 throws IOException {
902 return setBitmap(fullImage, visibleCropHint, allowBackup, FLAG_SET_SYSTEM);
903 }
904
905 /**
906 /**
907 * Version of {@link #setBitmap(Bitmap, Rect, boolean)} that allows the caller
908 * to specify which of the supported wallpaper categories to set.
909 *
910 * @param fullImage A bitmap that will supply the wallpaper imagery.
911 * @param visibleCropHint The rectangular subregion of {@code fullImage} that should be
912 * displayed as wallpaper. Passing {@code null} for this parameter means that
913 * the full image should be displayed if possible given the image's and device's
914 * aspect ratios, etc.
915 * @param allowBackup {@code true} if the OS is permitted to back up this wallpaper
916 * image for restore to a future device; {@code false} otherwise.
917 * @param which Flags indicating which wallpaper(s) to configure with the new imagery.
918 *
919 * @see #FLAG_SET_LOCK_WALLPAPER
920 * @see #FLAG_SET_SYSTEM_WALLPAPER
921 *
922 * @return An integer ID assigned to the newly active wallpaper; or zero on failure.
923 *
924 * @throws IOException
925 */
926 public int setBitmap(Bitmap fullImage, Rect visibleCropHint,
927 boolean allowBackup, @SetWallpaperFlags int which)
Christopher Tateffa6a882015-12-11 15:08:46 -0800928 throws IOException {
929 validateRect(visibleCropHint);
Mike Lockwoodc067c9c2011-10-31 12:50:12 -0400930 if (sGlobals.mService == null) {
931 Log.w(TAG, "WallpaperService not running");
Christopher Tatead3c2592016-01-20 18:13:17 -0800932 return 0;
Mike Lockwoodc067c9c2011-10-31 12:50:12 -0400933 }
Christopher Tatead3c2592016-01-20 18:13:17 -0800934 final Bundle result = new Bundle();
Christopher Tate1e1e2e02016-01-25 15:34:36 -0800935 final WallpaperSetCompletion completion = new WallpaperSetCompletion();
Dianne Hackborn8cc6a502009-08-05 21:29:42 -0700936 try {
Benjamin Franzf3ece362015-02-11 10:51:10 +0000937 ParcelFileDescriptor fd = sGlobals.mService.setWallpaper(null,
Christopher Tate1e1e2e02016-01-25 15:34:36 -0800938 mContext.getOpPackageName(), visibleCropHint, result, which, completion);
Christopher Tatead3c2592016-01-20 18:13:17 -0800939 if (fd != null) {
940 FileOutputStream fos = null;
941 try {
942 fos = new ParcelFileDescriptor.AutoCloseOutputStream(fd);
943 fullImage.compress(Bitmap.CompressFormat.PNG, 90, fos);
Christopher Tate1e1e2e02016-01-25 15:34:36 -0800944 fos.close();
945 completion.waitForCompletion();
Christopher Tatead3c2592016-01-20 18:13:17 -0800946 } finally {
947 IoUtils.closeQuietly(fos);
948 }
Dianne Hackborn8cc6a502009-08-05 21:29:42 -0700949 }
950 } catch (RemoteException e) {
Romain Guy407ec782011-08-24 17:06:58 -0700951 // Ignore
Dianne Hackborn8cc6a502009-08-05 21:29:42 -0700952 }
Christopher Tatead3c2592016-01-20 18:13:17 -0800953 return result.getInt(EXTRA_NEW_WALLPAPER_ID, 0);
Dianne Hackborn8cc6a502009-08-05 21:29:42 -0700954 }
955
Christopher Tateffa6a882015-12-11 15:08:46 -0800956 private final void validateRect(Rect rect) {
957 if (rect != null && rect.isEmpty()) {
958 throw new IllegalArgumentException("visibleCrop rectangle must be valid and non-empty");
959 }
960 }
961
Dianne Hackborn8cc6a502009-08-05 21:29:42 -0700962 /**
963 * Change the current system wallpaper to a specific byte stream. The
964 * give InputStream is copied into persistent storage and will now be
965 * used as the wallpaper. Currently it must be either a JPEG or PNG
966 * image. On success, the intent {@link Intent#ACTION_WALLPAPER_CHANGED}
967 * is broadcast.
968 *
Christopher Tateffa6a882015-12-11 15:08:46 -0800969 * <p>This method is equivalent to calling
970 * {@link #setStream(InputStream, Rect, boolean)} and passing {@code null} for the
971 * {@code visibleCrop} rectangle and {@code true} for the {@code allowBackup}
972 * parameter.
973 *
Nicolas Falliere9530e3a2012-06-18 17:21:06 -0700974 * <p>This method requires the caller to hold the permission
975 * {@link android.Manifest.permission#SET_WALLPAPER}.
976 *
Christopher Tateffa6a882015-12-11 15:08:46 -0800977 * @param bitmapData A stream containing the raw data to install as a wallpaper.
Dianne Hackborn8cc6a502009-08-05 21:29:42 -0700978 *
Christopher Tateffa6a882015-12-11 15:08:46 -0800979 * @throws IOException If an error occurs when attempting to set the wallpaper
980 * based on the provided image data.
Dianne Hackborn8cc6a502009-08-05 21:29:42 -0700981 */
Christopher Tateffa6a882015-12-11 15:08:46 -0800982 public void setStream(InputStream bitmapData) throws IOException {
983 setStream(bitmapData, null, true);
984 }
985
Christopher Tatead3c2592016-01-20 18:13:17 -0800986 private void copyStreamToWallpaperFile(InputStream data, FileOutputStream fos)
Christopher Tateffa6a882015-12-11 15:08:46 -0800987 throws IOException {
988 byte[] buffer = new byte[32768];
989 int amt;
990 while ((amt=data.read(buffer)) > 0) {
991 fos.write(buffer, 0, amt);
992 }
993 }
994
995 /**
996 * Change the current system wallpaper to a specific byte stream, specifying a
997 * hint about which subrectangle of the full image is to be visible. The OS will
998 * then try to best present the given portion of the full image as the static system
999 * wallpaper image. The data from the given InputStream is copied into persistent
1000 * storage and will then be used as the system wallpaper. Currently the data must
1001 * be either a JPEG or PNG image. On success, the intent
1002 * {@link Intent#ACTION_WALLPAPER_CHANGED} is broadcast.
1003 *
1004 * <p>This method requires the caller to hold the permission
1005 * {@link android.Manifest.permission#SET_WALLPAPER}.
1006 *
1007 * @param bitmapData A stream containing the raw data to install as a wallpaper.
1008 * @param visibleCropHint The rectangular subregion of the streamed image that should be
1009 * displayed as wallpaper. Passing {@code null} for this parameter means that
1010 * the full image should be displayed if possible given the image's and device's
1011 * aspect ratios, etc.
1012 * @param allowBackup {@code true} if the OS is permitted to back up this wallpaper
1013 * image for restore to a future device; {@code false} otherwise.
1014 *
1015 * @throws IOException If an error occurs when attempting to set the wallpaper
1016 * based on the provided image data.
1017 * @throws IllegalArgumentException If the {@code visibleCropHint} rectangle is
1018 * empty or invalid.
1019 */
Christopher Tatead3c2592016-01-20 18:13:17 -08001020 public int setStream(InputStream bitmapData, Rect visibleCropHint, boolean allowBackup)
Christopher Tateffa6a882015-12-11 15:08:46 -08001021 throws IOException {
Christopher Tatead3c2592016-01-20 18:13:17 -08001022 return setStream(bitmapData, visibleCropHint, allowBackup, FLAG_SET_SYSTEM);
1023 }
1024
1025 /**
1026 * Version of {@link #setStream(InputStream, Rect, boolean)} that allows the caller
1027 * to specify which of the supported wallpaper categories to set.
1028 *
1029 * @param bitmapData A stream containing the raw data to install as a wallpaper.
1030 * @param visibleCropHint The rectangular subregion of the streamed image that should be
1031 * displayed as wallpaper. Passing {@code null} for this parameter means that
1032 * the full image should be displayed if possible given the image's and device's
1033 * aspect ratios, etc.
1034 * @param allowBackup {@code true} if the OS is permitted to back up this wallpaper
1035 * image for restore to a future device; {@code false} otherwise.
1036 * @param which Flags indicating which wallpaper(s) to configure with the new imagery.
1037 *
1038 * @see #FLAG_SET_LOCK_WALLPAPER
1039 * @see #FLAG_SET_SYSTEM_WALLPAPER
1040 *
1041 * @throws IOException
1042 */
1043 public int setStream(InputStream bitmapData, Rect visibleCropHint,
1044 boolean allowBackup, @SetWallpaperFlags int which)
1045 throws IOException {
Christopher Tateffa6a882015-12-11 15:08:46 -08001046 validateRect(visibleCropHint);
Mike Lockwoodc067c9c2011-10-31 12:50:12 -04001047 if (sGlobals.mService == null) {
1048 Log.w(TAG, "WallpaperService not running");
Christopher Tatead3c2592016-01-20 18:13:17 -08001049 return 0;
Mike Lockwoodc067c9c2011-10-31 12:50:12 -04001050 }
Christopher Tatead3c2592016-01-20 18:13:17 -08001051 final Bundle result = new Bundle();
Christopher Tate1e1e2e02016-01-25 15:34:36 -08001052 final WallpaperSetCompletion completion = new WallpaperSetCompletion();
Dianne Hackborn8cc6a502009-08-05 21:29:42 -07001053 try {
Benjamin Franzf3ece362015-02-11 10:51:10 +00001054 ParcelFileDescriptor fd = sGlobals.mService.setWallpaper(null,
Christopher Tate1e1e2e02016-01-25 15:34:36 -08001055 mContext.getOpPackageName(), visibleCropHint, result, which, completion);
Christopher Tatead3c2592016-01-20 18:13:17 -08001056 if (fd != null) {
1057 FileOutputStream fos = null;
1058 try {
1059 fos = new ParcelFileDescriptor.AutoCloseOutputStream(fd);
1060 copyStreamToWallpaperFile(bitmapData, fos);
Christopher Tate1e1e2e02016-01-25 15:34:36 -08001061 fos.close();
1062 completion.waitForCompletion();
Christopher Tatead3c2592016-01-20 18:13:17 -08001063 } finally {
1064 IoUtils.closeQuietly(fos);
1065 }
Dianne Hackborn8cc6a502009-08-05 21:29:42 -07001066 }
1067 } catch (RemoteException e) {
Romain Guy407ec782011-08-24 17:06:58 -07001068 // Ignore
Dianne Hackborn8cc6a502009-08-05 21:29:42 -07001069 }
Christopher Tatead3c2592016-01-20 18:13:17 -08001070
1071 return result.getInt(EXTRA_NEW_WALLPAPER_ID, 0);
Dianne Hackborn8cc6a502009-08-05 21:29:42 -07001072 }
1073
Dianne Hackborn8cc6a502009-08-05 21:29:42 -07001074 /**
Dianne Hackborn5dc5a002012-09-15 19:33:48 -07001075 * Return whether any users are currently set to use the wallpaper
1076 * with the given resource ID. That is, their wallpaper has been
1077 * set through {@link #setResource(int)} with the same resource id.
1078 */
Tor Norbye7b9c9122013-05-30 16:48:33 -07001079 public boolean hasResourceWallpaper(@RawRes int resid) {
Dianne Hackborn5dc5a002012-09-15 19:33:48 -07001080 if (sGlobals.mService == null) {
1081 Log.w(TAG, "WallpaperService not running");
1082 return false;
1083 }
1084 try {
1085 Resources resources = mContext.getResources();
1086 String name = "res:" + resources.getResourceName(resid);
1087 return sGlobals.mService.hasNamedWallpaper(name);
1088 } catch (RemoteException e) {
1089 return false;
1090 }
1091 }
1092
1093 /**
Dianne Hackborn8cc6a502009-08-05 21:29:42 -07001094 * Returns the desired minimum width for the wallpaper. Callers of
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001095 * {@link #setBitmap(android.graphics.Bitmap)} or
1096 * {@link #setStream(java.io.InputStream)} should check this value
Dianne Hackborn8cc6a502009-08-05 21:29:42 -07001097 * beforehand to make sure the supplied wallpaper respects the desired
1098 * minimum width.
1099 *
1100 * If the returned value is <= 0, the caller should use the width of
1101 * the default display instead.
1102 *
1103 * @return The desired minimum width for the wallpaper. This value should
1104 * be honored by applications that set the wallpaper but it is not
1105 * mandatory.
1106 */
1107 public int getDesiredMinimumWidth() {
Mike Lockwoodc067c9c2011-10-31 12:50:12 -04001108 if (sGlobals.mService == null) {
1109 Log.w(TAG, "WallpaperService not running");
1110 return 0;
1111 }
Dianne Hackborn8cc6a502009-08-05 21:29:42 -07001112 try {
Dianne Hackborn840c3a22009-09-02 17:35:17 -07001113 return sGlobals.mService.getWidthHint();
Dianne Hackborn8cc6a502009-08-05 21:29:42 -07001114 } catch (RemoteException e) {
1115 // Shouldn't happen!
1116 return 0;
1117 }
1118 }
1119
1120 /**
1121 * Returns the desired minimum height for the wallpaper. Callers of
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001122 * {@link #setBitmap(android.graphics.Bitmap)} or
1123 * {@link #setStream(java.io.InputStream)} should check this value
Dianne Hackborn8cc6a502009-08-05 21:29:42 -07001124 * beforehand to make sure the supplied wallpaper respects the desired
1125 * minimum height.
1126 *
1127 * If the returned value is <= 0, the caller should use the height of
1128 * the default display instead.
1129 *
1130 * @return The desired minimum height for the wallpaper. This value should
1131 * be honored by applications that set the wallpaper but it is not
1132 * mandatory.
1133 */
1134 public int getDesiredMinimumHeight() {
Mike Lockwoodc067c9c2011-10-31 12:50:12 -04001135 if (sGlobals.mService == null) {
1136 Log.w(TAG, "WallpaperService not running");
1137 return 0;
1138 }
Dianne Hackborn8cc6a502009-08-05 21:29:42 -07001139 try {
Dianne Hackborn840c3a22009-09-02 17:35:17 -07001140 return sGlobals.mService.getHeightHint();
Dianne Hackborn8cc6a502009-08-05 21:29:42 -07001141 } catch (RemoteException e) {
1142 // Shouldn't happen!
1143 return 0;
1144 }
1145 }
1146
1147 /**
1148 * For use only by the current home application, to specify the size of
1149 * wallpaper it would like to use. This allows such applications to have
1150 * a virtual wallpaper that is larger than the physical screen, matching
1151 * the size of their workspace.
Dianne Hackbornc5bf7582012-04-25 19:12:07 -07001152 *
1153 * <p>Note developers, who don't seem to be reading this. This is
Christopher Tateffa6a882015-12-11 15:08:46 -08001154 * for <em>home apps</em> to tell what size wallpaper they would like.
1155 * Nobody else should be calling this! Certainly not other non-home
Dianne Hackbornc5bf7582012-04-25 19:12:07 -07001156 * apps that change the wallpaper. Those apps are supposed to
1157 * <b>retrieve</b> the suggested size so they can construct a wallpaper
1158 * that matches it.
1159 *
Nicolas Falliere9530e3a2012-06-18 17:21:06 -07001160 * <p>This method requires the caller to hold the permission
1161 * {@link android.Manifest.permission#SET_WALLPAPER_HINTS}.
1162 *
Dianne Hackborn8cc6a502009-08-05 21:29:42 -07001163 * @param minimumWidth Desired minimum width
1164 * @param minimumHeight Desired minimum height
1165 */
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001166 public void suggestDesiredDimensions(int minimumWidth, int minimumHeight) {
Dianne Hackborn8cc6a502009-08-05 21:29:42 -07001167 try {
Donghan Ryu289c2732011-11-14 18:56:11 -08001168 /**
1169 * The framework makes no attempt to limit the window size
1170 * to the maximum texture size. Any window larger than this
1171 * cannot be composited.
1172 *
1173 * Read maximum texture size from system property and scale down
1174 * minimumWidth and minimumHeight accordingly.
1175 */
1176 int maximumTextureSize;
1177 try {
1178 maximumTextureSize = SystemProperties.getInt("sys.max_texture_size", 0);
1179 } catch (Exception e) {
1180 maximumTextureSize = 0;
1181 }
1182
1183 if (maximumTextureSize > 0) {
1184 if ((minimumWidth > maximumTextureSize) ||
1185 (minimumHeight > maximumTextureSize)) {
1186 float aspect = (float)minimumHeight / (float)minimumWidth;
1187 if (minimumWidth > minimumHeight) {
1188 minimumWidth = maximumTextureSize;
1189 minimumHeight = (int)((minimumWidth * aspect) + 0.5);
1190 } else {
1191 minimumHeight = maximumTextureSize;
1192 minimumWidth = (int)((minimumHeight / aspect) + 0.5);
1193 }
1194 }
1195 }
1196
Mike Lockwoodc067c9c2011-10-31 12:50:12 -04001197 if (sGlobals.mService == null) {
1198 Log.w(TAG, "WallpaperService not running");
1199 } else {
Benjamin Franzf3ece362015-02-11 10:51:10 +00001200 sGlobals.mService.setDimensionHints(minimumWidth, minimumHeight,
1201 mContext.getOpPackageName());
Mike Lockwoodc067c9c2011-10-31 12:50:12 -04001202 }
Dianne Hackborn8cc6a502009-08-05 21:29:42 -07001203 } catch (RemoteException e) {
Romain Guy407ec782011-08-24 17:06:58 -07001204 // Ignore
Dianne Hackborn8cc6a502009-08-05 21:29:42 -07001205 }
1206 }
Adam Cohen041a0ba2011-11-09 20:10:27 -08001207
Dianne Hackborn8cc6a502009-08-05 21:29:42 -07001208 /**
Dianne Hackborn067e5f62014-09-07 23:14:30 -07001209 * Specify extra padding that the wallpaper should have outside of the display.
1210 * That is, the given padding supplies additional pixels the wallpaper should extend
1211 * outside of the display itself.
1212 * @param padding The number of pixels the wallpaper should extend beyond the display,
1213 * on its left, top, right, and bottom sides.
1214 * @hide
1215 */
1216 @SystemApi
1217 public void setDisplayPadding(Rect padding) {
1218 try {
1219 if (sGlobals.mService == null) {
1220 Log.w(TAG, "WallpaperService not running");
1221 } else {
Benjamin Franzf3ece362015-02-11 10:51:10 +00001222 sGlobals.mService.setDisplayPadding(padding, mContext.getOpPackageName());
Dianne Hackborn067e5f62014-09-07 23:14:30 -07001223 }
1224 } catch (RemoteException e) {
1225 // Ignore
1226 }
1227 }
1228
1229 /**
1230 * Apply a raw offset to the wallpaper window. Should only be used in
1231 * combination with {@link #setDisplayPadding(android.graphics.Rect)} when you
1232 * have ensured that the wallpaper will extend outside of the display area so that
1233 * it can be moved without leaving part of the display uncovered.
1234 * @param x The offset, in pixels, to apply to the left edge.
1235 * @param y The offset, in pixels, to apply to the top edge.
1236 * @hide
1237 */
1238 @SystemApi
1239 public void setDisplayOffset(IBinder windowToken, int x, int y) {
1240 try {
1241 //Log.v(TAG, "Sending new wallpaper display offsets from app...");
1242 WindowManagerGlobal.getWindowSession().setWallpaperDisplayOffset(
1243 windowToken, x, y);
1244 //Log.v(TAG, "...app returning after sending display offset!");
1245 } catch (RemoteException e) {
1246 // Ignore.
1247 }
1248 }
1249
1250 /**
Filip Gruszczynski9fedc722015-01-28 15:59:46 -08001251 * Clear the wallpaper.
1252 *
1253 * @hide
1254 */
1255 @SystemApi
1256 public void clearWallpaper() {
Christopher Tatebe132e62016-02-10 12:59:49 -08001257 clearWallpaper(FLAG_SET_SYSTEM, mContext.getUserId());
1258 }
1259
1260 /**
1261 * Clear the wallpaper for a specific user. The caller must hold the
1262 * INTERACT_ACROSS_USERS_FULL permission to clear another user's
1263 * wallpaper.
1264 * @hide
1265 */
1266 @SystemApi
1267 public void clearWallpaper(int which, int userId) {
Filip Gruszczynski9fedc722015-01-28 15:59:46 -08001268 if (sGlobals.mService == null) {
1269 Log.w(TAG, "WallpaperService not running");
1270 return;
1271 }
1272 try {
Christopher Tatebe132e62016-02-10 12:59:49 -08001273 sGlobals.mService.clearWallpaper(mContext.getOpPackageName(), which, userId);
Filip Gruszczynski9fedc722015-01-28 15:59:46 -08001274 } catch (RemoteException e) {
1275 // Ignore
1276 }
1277 }
1278
1279 /**
1280 * Set the live wallpaper.
1281 *
1282 * This can only be called by packages with android.permission.SET_WALLPAPER_COMPONENT
1283 * permission.
1284 *
1285 * @hide
1286 */
1287 @SystemApi
1288 public boolean setWallpaperComponent(ComponentName name) {
1289 if (sGlobals.mService == null) {
1290 Log.w(TAG, "WallpaperService not running");
1291 return false;
1292 }
1293 try {
Benjamin Franzf3ece362015-02-11 10:51:10 +00001294 sGlobals.mService.setWallpaperComponentChecked(name, mContext.getOpPackageName());
Filip Gruszczynski9fedc722015-01-28 15:59:46 -08001295 return true;
1296 } catch (RemoteException e) {
1297 // Ignore
1298 }
1299 return false;
1300 }
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) {
1322 // Ignore.
1323 }
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) {
1360 // Ignore.
1361 }
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) {
1376 // Ignore
1377 }
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) {
1396 // Ignore
1397 }
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) {
1417 // Ignore.
1418 }
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 /**
1437 * Open stream representing the default static image wallpaper.
1438 *
1439 * @hide
1440 */
1441 public static InputStream openDefaultWallpaper(Context context) {
1442 final String path = SystemProperties.get(PROP_WALLPAPER);
1443 if (!TextUtils.isEmpty(path)) {
1444 final File file = new File(path);
1445 if (file.exists()) {
1446 try {
1447 return new FileInputStream(file);
1448 } catch (IOException e) {
1449 // Ignored, fall back to platform default below
1450 }
1451 }
1452 }
1453 return context.getResources().openRawResource(
1454 com.android.internal.R.drawable.default_wallpaper);
1455 }
1456
1457 /**
1458 * Return {@link ComponentName} of the default live wallpaper, or
1459 * {@code null} if none is defined.
1460 *
1461 * @hide
1462 */
1463 public static ComponentName getDefaultWallpaperComponent(Context context) {
1464 String flat = SystemProperties.get(PROP_WALLPAPER_COMPONENT);
1465 if (!TextUtils.isEmpty(flat)) {
1466 final ComponentName cn = ComponentName.unflattenFromString(flat);
1467 if (cn != null) {
1468 return cn;
1469 }
1470 }
1471
1472 flat = context.getString(com.android.internal.R.string.default_wallpaper_component);
1473 if (!TextUtils.isEmpty(flat)) {
1474 final ComponentName cn = ComponentName.unflattenFromString(flat);
1475 if (cn != null) {
1476 return cn;
1477 }
1478 }
1479
1480 return null;
Dianne Hackborn8cc6a502009-08-05 21:29:42 -07001481 }
Christopher Tate1e1e2e02016-01-25 15:34:36 -08001482
Christopher Tatebe132e62016-02-10 12:59:49 -08001483 /**
1484 * Register a callback for lock wallpaper observation. Only the OS may use this.
1485 *
1486 * @return true on success; false on error.
1487 * @hide
1488 */
1489 public boolean setLockWallpaperCallback(IWallpaperManagerCallback callback) {
1490 if (sGlobals.mService == null) {
1491 Log.w(TAG, "WallpaperService not running");
1492 return false;
1493 }
1494
1495 try {
1496 return sGlobals.mService.setLockWallpaperCallback(callback);
1497 } catch (RemoteException e) {
1498 Log.e(TAG, "Unable to contact wallpaper service");
1499 }
1500 return false;
1501 }
1502
Christopher Tate1e1e2e02016-01-25 15:34:36 -08001503 // Private completion callback for setWallpaper() synchronization
1504 private class WallpaperSetCompletion extends IWallpaperManagerCallback.Stub {
1505 final CountDownLatch mLatch;
1506
1507 public WallpaperSetCompletion() {
1508 mLatch = new CountDownLatch(1);
1509 }
1510
1511 public void waitForCompletion() {
1512 try {
1513 mLatch.await(30, TimeUnit.SECONDS);
1514 } catch (InterruptedException e) {
1515 // This might be legit: the crop may take a very long time. Don't sweat
1516 // it in that case; we are okay with display lagging behind in order to
1517 // keep the caller from locking up indeterminately.
1518 }
1519 }
1520
1521 @Override
1522 public void onWallpaperChanged() throws RemoteException {
1523 mLatch.countDown();
1524 }
1525 }
Dianne Hackborn8cc6a502009-08-05 21:29:42 -07001526}