blob: b52af2762fed74577cd6c68e002a45c1a113392f [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 /**
Christopher Tatee409f0e2016-03-21 14:53:15 -0700729 * Get the ID of the current wallpaper of the given kind. If there is no
730 * such wallpaper configured, returns a negative number.
731 *
732 * @param which The wallpaper whose ID is to be returned. Must be a single
733 * defined kind of wallpaper, either {@link #FLAG_SET_SYSTEM} or
734 * {@link #FLAG_SET_LOCK}.
735 * @return The positive numeric ID of the current wallpaper of the given kind,
736 * or a negative value if no such wallpaper is configured.
737 */
738 public int getWallpaperId(int which) {
739 return getWallpaperIdForUser(which, mContext.getUserId());
740 }
741
742 /**
743 * Get the ID of the given user's current wallpaper of the given kind. If there
744 * is no such wallpaper configured, returns a negative number.
745 * @hide
746 */
747 public int getWallpaperIdForUser(int which, int userId) {
748 try {
749 if (sGlobals.mService == null) {
750 Log.w(TAG, "WallpaperService not running");
751 return -1;
752 } else {
753 return sGlobals.mService.getWallpaperIdForUser(which, userId);
754 }
755 } catch (RemoteException e) {
756 throw e.rethrowFromSystemServer();
757 }
758 }
759
760 /**
Michael Jurkae8d1bf72013-09-09 15:58:54 +0200761 * Gets an Intent that will launch an activity that crops the given
762 * image and sets the device's wallpaper. If there is a default HOME activity
763 * that supports cropping wallpapers, it will be preferred as the default.
Ying Wang930d4e52013-09-14 11:57:17 -0700764 * Use this method instead of directly creating a {@link #ACTION_CROP_AND_SET_WALLPAPER}
Michael Jurkae8d1bf72013-09-09 15:58:54 +0200765 * intent.
Adam Lesinskibba72d12013-10-11 18:10:56 -0700766 *
767 * @param imageUri The image URI that will be set in the intent. The must be a content
768 * URI and its provider must resolve its type to "image/*"
769 *
770 * @throws IllegalArgumentException if the URI is not a content URI or its MIME type is
771 * not "image/*"
Michael Jurkae8d1bf72013-09-09 15:58:54 +0200772 */
773 public Intent getCropAndSetWallpaperIntent(Uri imageUri) {
Allen Shen1c0f21e2014-04-29 16:16:29 +0800774 if (imageUri == null) {
775 throw new IllegalArgumentException("Image URI must not be null");
776 }
777
Adam Lesinskibba72d12013-10-11 18:10:56 -0700778 if (!ContentResolver.SCHEME_CONTENT.equals(imageUri.getScheme())) {
779 throw new IllegalArgumentException("Image URI must be of the "
780 + ContentResolver.SCHEME_CONTENT + " scheme type");
781 }
782
Michael Jurkae8d1bf72013-09-09 15:58:54 +0200783 final PackageManager packageManager = mContext.getPackageManager();
784 Intent cropAndSetWallpaperIntent =
785 new Intent(ACTION_CROP_AND_SET_WALLPAPER, imageUri);
786 cropAndSetWallpaperIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
787
788 // Find out if the default HOME activity supports CROP_AND_SET_WALLPAPER
789 Intent homeIntent = new Intent(Intent.ACTION_MAIN).addCategory(Intent.CATEGORY_HOME);
790 ResolveInfo resolvedHome = packageManager.resolveActivity(homeIntent,
791 PackageManager.MATCH_DEFAULT_ONLY);
792 if (resolvedHome != null) {
793 cropAndSetWallpaperIntent.setPackage(resolvedHome.activityInfo.packageName);
794
795 List<ResolveInfo> cropAppList = packageManager.queryIntentActivities(
796 cropAndSetWallpaperIntent, 0);
797 if (cropAppList.size() > 0) {
798 return cropAndSetWallpaperIntent;
799 }
800 }
801
802 // fallback crop activity
Oren Blasberg60598ea0a2016-02-26 11:01:23 -0800803 final String cropperPackage = mContext.getString(
804 com.android.internal.R.string.config_wallpaperCropperPackage);
805 cropAndSetWallpaperIntent.setPackage(cropperPackage);
Adam Lesinskibba72d12013-10-11 18:10:56 -0700806 List<ResolveInfo> cropAppList = packageManager.queryIntentActivities(
807 cropAndSetWallpaperIntent, 0);
808 if (cropAppList.size() > 0) {
809 return cropAndSetWallpaperIntent;
810 }
811 // If the URI is not of the right type, or for some reason the system wallpaper
812 // cropper doesn't exist, return null
813 throw new IllegalArgumentException("Cannot use passed URI to set wallpaper; " +
814 "check that the type returned by ContentProvider matches image/*");
Michael Jurkae8d1bf72013-09-09 15:58:54 +0200815 }
816
Dianne Hackborneb034652009-09-07 00:49:58 -0700817 /**
Dianne Hackborn8cc6a502009-08-05 21:29:42 -0700818 * Change the current system wallpaper to the bitmap in the given resource.
819 * The resource is opened as a raw data stream and copied into the
820 * wallpaper; it must be a valid PNG or JPEG image. On success, the intent
821 * {@link Intent#ACTION_WALLPAPER_CHANGED} is broadcast.
822 *
Nicolas Falliere9530e3a2012-06-18 17:21:06 -0700823 * <p>This method requires the caller to hold the permission
824 * {@link android.Manifest.permission#SET_WALLPAPER}.
825 *
Dianne Hackborn8cc6a502009-08-05 21:29:42 -0700826 * @param resid The bitmap to save.
827 *
Michael Jurkab668d0b2013-10-04 15:11:05 -0700828 * @throws IOException If an error occurs reverting to the built-in
Dianne Hackborn8cc6a502009-08-05 21:29:42 -0700829 * wallpaper.
830 */
Tor Norbye7b9c9122013-05-30 16:48:33 -0700831 public void setResource(@RawRes int resid) throws IOException {
Christopher Tatead3c2592016-01-20 18:13:17 -0800832 setResource(resid, FLAG_SET_SYSTEM);
833 }
834
835 /**
836 * Version of {@link #setResource(int)} that takes an optional Bundle for returning
837 * metadata about the operation to the caller.
838 *
839 * @param resid
840 * @param which Flags indicating which wallpaper(s) to configure with the new imagery.
841 *
842 * @see #FLAG_SET_LOCK
843 * @see #FLAG_SET_SYSTEM
844 *
845 * @return An integer ID assigned to the newly active wallpaper; or zero on failure.
846 *
847 * @throws IOException
848 */
849 public int setResource(@RawRes int resid, @SetWallpaperFlags int which)
850 throws IOException {
Mike Lockwoodc067c9c2011-10-31 12:50:12 -0400851 if (sGlobals.mService == null) {
852 Log.w(TAG, "WallpaperService not running");
Christopher Tatead3c2592016-01-20 18:13:17 -0800853 return 0;
Mike Lockwoodc067c9c2011-10-31 12:50:12 -0400854 }
Christopher Tatead3c2592016-01-20 18:13:17 -0800855 final Bundle result = new Bundle();
Christopher Tate1e1e2e02016-01-25 15:34:36 -0800856 final WallpaperSetCompletion completion = new WallpaperSetCompletion();
Dianne Hackborn8cc6a502009-08-05 21:29:42 -0700857 try {
858 Resources resources = mContext.getResources();
859 /* Set the wallpaper to the default values */
Dianne Hackborn840c3a22009-09-02 17:35:17 -0700860 ParcelFileDescriptor fd = sGlobals.mService.setWallpaper(
Christopher Tatead3c2592016-01-20 18:13:17 -0800861 "res:" + resources.getResourceName(resid),
Christopher Tate1e1e2e02016-01-25 15:34:36 -0800862 mContext.getOpPackageName(), null, result, which, completion);
Dianne Hackborn8cc6a502009-08-05 21:29:42 -0700863 if (fd != null) {
864 FileOutputStream fos = null;
Christopher Tate1e1e2e02016-01-25 15:34:36 -0800865 boolean ok = false;
Dianne Hackborn8cc6a502009-08-05 21:29:42 -0700866 try {
867 fos = new ParcelFileDescriptor.AutoCloseOutputStream(fd);
Christopher Tatead3c2592016-01-20 18:13:17 -0800868 copyStreamToWallpaperFile(resources.openRawResource(resid), fos);
Christopher Tate1e1e2e02016-01-25 15:34:36 -0800869 // The 'close()' is the trigger for any server-side image manipulation,
870 // so we must do that before waiting for completion.
871 fos.close();
872 completion.waitForCompletion();
Dianne Hackborn8cc6a502009-08-05 21:29:42 -0700873 } finally {
Christopher Tate1e1e2e02016-01-25 15:34:36 -0800874 // Might be redundant but completion shouldn't wait unless the write
875 // succeeded; this is a fallback if it threw past the close+wait.
Christopher Tateffa6a882015-12-11 15:08:46 -0800876 IoUtils.closeQuietly(fos);
Dianne Hackborn8cc6a502009-08-05 21:29:42 -0700877 }
878 }
879 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -0700880 throw e.rethrowFromSystemServer();
Dianne Hackborn8cc6a502009-08-05 21:29:42 -0700881 }
Christopher Tatead3c2592016-01-20 18:13:17 -0800882 return result.getInt(EXTRA_NEW_WALLPAPER_ID, 0);
Dianne Hackborn8cc6a502009-08-05 21:29:42 -0700883 }
Christopher Tateffa6a882015-12-11 15:08:46 -0800884
Dianne Hackborn8cc6a502009-08-05 21:29:42 -0700885 /**
886 * Change the current system wallpaper to a bitmap. The given bitmap is
887 * converted to a PNG and stored as the wallpaper. On success, the intent
888 * {@link Intent#ACTION_WALLPAPER_CHANGED} is broadcast.
889 *
Christopher Tateffa6a882015-12-11 15:08:46 -0800890 * <p>This method is equivalent to calling
891 * {@link #setBitmap(Bitmap, Rect, boolean)} and passing {@code null} for the
892 * {@code visibleCrop} rectangle and {@code true} for the {@code allowBackup}
893 * parameter.
894 *
Nicolas Falliere9530e3a2012-06-18 17:21:06 -0700895 * <p>This method requires the caller to hold the permission
896 * {@link android.Manifest.permission#SET_WALLPAPER}.
897 *
Christopher Tatead3c2592016-01-20 18:13:17 -0800898 * @param bitmap The bitmap to be used as the new system wallpaper.
Dianne Hackborn8cc6a502009-08-05 21:29:42 -0700899 *
Christopher Tateffa6a882015-12-11 15:08:46 -0800900 * @throws IOException If an error occurs when attempting to set the wallpaper
901 * to the provided image.
Dianne Hackborn8cc6a502009-08-05 21:29:42 -0700902 */
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700903 public void setBitmap(Bitmap bitmap) throws IOException {
Christopher Tateffa6a882015-12-11 15:08:46 -0800904 setBitmap(bitmap, null, true);
905 }
906
907 /**
908 * Change the current system wallpaper to a bitmap, specifying a hint about
909 * which subrectangle of the full image is to be visible. The OS will then
910 * try to best present the given portion of the full image as the static system
911 * wallpaper image. On success, the intent
912 * {@link Intent#ACTION_WALLPAPER_CHANGED} is broadcast.
913 *
914 * <p>Passing {@code null} as the {@code visibleHint} parameter is equivalent to
915 * passing (0, 0, {@code fullImage.getWidth()}, {@code fullImage.getHeight()}).
916 *
917 * <p>This method requires the caller to hold the permission
918 * {@link android.Manifest.permission#SET_WALLPAPER}.
919 *
Christopher Tatead3c2592016-01-20 18:13:17 -0800920 * @param fullImage A bitmap that will supply the wallpaper imagery.
Christopher Tateffa6a882015-12-11 15:08:46 -0800921 * @param visibleCropHint The rectangular subregion of {@code fullImage} that should be
922 * displayed as wallpaper. Passing {@code null} for this parameter means that
923 * the full image should be displayed if possible given the image's and device's
Christopher Tatead3c2592016-01-20 18:13:17 -0800924 * aspect ratios, etc.
Christopher Tateffa6a882015-12-11 15:08:46 -0800925 * @param allowBackup {@code true} if the OS is permitted to back up this wallpaper
926 * image for restore to a future device; {@code false} otherwise.
927 *
Christopher Tatead3c2592016-01-20 18:13:17 -0800928 * @return An integer ID assigned to the newly active wallpaper; or zero on failure.
929 *
Christopher Tateffa6a882015-12-11 15:08:46 -0800930 * @throws IOException If an error occurs when attempting to set the wallpaper
931 * to the provided image.
932 * @throws IllegalArgumentException If the {@code visibleCropHint} rectangle is
933 * empty or invalid.
934 */
Christopher Tatead3c2592016-01-20 18:13:17 -0800935 public int setBitmap(Bitmap fullImage, Rect visibleCropHint, boolean allowBackup)
936 throws IOException {
937 return setBitmap(fullImage, visibleCropHint, allowBackup, FLAG_SET_SYSTEM);
938 }
939
940 /**
941 /**
942 * Version of {@link #setBitmap(Bitmap, Rect, boolean)} that allows the caller
943 * to specify which of the supported wallpaper categories to set.
944 *
945 * @param fullImage A bitmap that will supply the wallpaper imagery.
946 * @param visibleCropHint The rectangular subregion of {@code fullImage} that should be
947 * displayed as wallpaper. Passing {@code null} for this parameter means that
948 * the full image should be displayed if possible given the image's and device's
949 * aspect ratios, etc.
950 * @param allowBackup {@code true} if the OS is permitted to back up this wallpaper
951 * image for restore to a future device; {@code false} otherwise.
952 * @param which Flags indicating which wallpaper(s) to configure with the new imagery.
953 *
Oren Blasberg2eee8c62016-03-14 10:06:08 -0700954 * @see #FLAG_SET_LOCK
955 * @see #FLAG_SET_SYSTEM
Christopher Tatead3c2592016-01-20 18:13:17 -0800956 *
957 * @return An integer ID assigned to the newly active wallpaper; or zero on failure.
958 *
959 * @throws IOException
960 */
961 public int setBitmap(Bitmap fullImage, Rect visibleCropHint,
962 boolean allowBackup, @SetWallpaperFlags int which)
Christopher Tateffa6a882015-12-11 15:08:46 -0800963 throws IOException {
964 validateRect(visibleCropHint);
Mike Lockwoodc067c9c2011-10-31 12:50:12 -0400965 if (sGlobals.mService == null) {
966 Log.w(TAG, "WallpaperService not running");
Christopher Tatead3c2592016-01-20 18:13:17 -0800967 return 0;
Mike Lockwoodc067c9c2011-10-31 12:50:12 -0400968 }
Christopher Tatead3c2592016-01-20 18:13:17 -0800969 final Bundle result = new Bundle();
Christopher Tate1e1e2e02016-01-25 15:34:36 -0800970 final WallpaperSetCompletion completion = new WallpaperSetCompletion();
Dianne Hackborn8cc6a502009-08-05 21:29:42 -0700971 try {
Benjamin Franzf3ece362015-02-11 10:51:10 +0000972 ParcelFileDescriptor fd = sGlobals.mService.setWallpaper(null,
Christopher Tate1e1e2e02016-01-25 15:34:36 -0800973 mContext.getOpPackageName(), visibleCropHint, result, which, completion);
Christopher Tatead3c2592016-01-20 18:13:17 -0800974 if (fd != null) {
975 FileOutputStream fos = null;
976 try {
977 fos = new ParcelFileDescriptor.AutoCloseOutputStream(fd);
978 fullImage.compress(Bitmap.CompressFormat.PNG, 90, fos);
Christopher Tate1e1e2e02016-01-25 15:34:36 -0800979 fos.close();
980 completion.waitForCompletion();
Christopher Tatead3c2592016-01-20 18:13:17 -0800981 } finally {
982 IoUtils.closeQuietly(fos);
983 }
Dianne Hackborn8cc6a502009-08-05 21:29:42 -0700984 }
985 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -0700986 throw e.rethrowFromSystemServer();
Dianne Hackborn8cc6a502009-08-05 21:29:42 -0700987 }
Christopher Tatead3c2592016-01-20 18:13:17 -0800988 return result.getInt(EXTRA_NEW_WALLPAPER_ID, 0);
Dianne Hackborn8cc6a502009-08-05 21:29:42 -0700989 }
990
Christopher Tateffa6a882015-12-11 15:08:46 -0800991 private final void validateRect(Rect rect) {
992 if (rect != null && rect.isEmpty()) {
993 throw new IllegalArgumentException("visibleCrop rectangle must be valid and non-empty");
994 }
995 }
996
Dianne Hackborn8cc6a502009-08-05 21:29:42 -0700997 /**
998 * Change the current system wallpaper to a specific byte stream. The
999 * give InputStream is copied into persistent storage and will now be
1000 * used as the wallpaper. Currently it must be either a JPEG or PNG
1001 * image. On success, the intent {@link Intent#ACTION_WALLPAPER_CHANGED}
1002 * is broadcast.
1003 *
Christopher Tateffa6a882015-12-11 15:08:46 -08001004 * <p>This method is equivalent to calling
1005 * {@link #setStream(InputStream, Rect, boolean)} and passing {@code null} for the
1006 * {@code visibleCrop} rectangle and {@code true} for the {@code allowBackup}
1007 * parameter.
1008 *
Nicolas Falliere9530e3a2012-06-18 17:21:06 -07001009 * <p>This method requires the caller to hold the permission
1010 * {@link android.Manifest.permission#SET_WALLPAPER}.
1011 *
Christopher Tateffa6a882015-12-11 15:08:46 -08001012 * @param bitmapData A stream containing the raw data to install as a wallpaper.
Dianne Hackborn8cc6a502009-08-05 21:29:42 -07001013 *
Christopher Tateffa6a882015-12-11 15:08:46 -08001014 * @throws IOException If an error occurs when attempting to set the wallpaper
1015 * based on the provided image data.
Dianne Hackborn8cc6a502009-08-05 21:29:42 -07001016 */
Christopher Tateffa6a882015-12-11 15:08:46 -08001017 public void setStream(InputStream bitmapData) throws IOException {
1018 setStream(bitmapData, null, true);
1019 }
1020
Christopher Tatead3c2592016-01-20 18:13:17 -08001021 private void copyStreamToWallpaperFile(InputStream data, FileOutputStream fos)
Christopher Tateffa6a882015-12-11 15:08:46 -08001022 throws IOException {
1023 byte[] buffer = new byte[32768];
1024 int amt;
1025 while ((amt=data.read(buffer)) > 0) {
1026 fos.write(buffer, 0, amt);
1027 }
1028 }
1029
1030 /**
1031 * Change the current system wallpaper to a specific byte stream, specifying a
1032 * hint about which subrectangle of the full image is to be visible. The OS will
1033 * then try to best present the given portion of the full image as the static system
1034 * wallpaper image. The data from the given InputStream is copied into persistent
1035 * storage and will then be used as the system wallpaper. Currently the data must
1036 * be either a JPEG or PNG image. On success, the intent
1037 * {@link Intent#ACTION_WALLPAPER_CHANGED} is broadcast.
1038 *
1039 * <p>This method requires the caller to hold the permission
1040 * {@link android.Manifest.permission#SET_WALLPAPER}.
1041 *
1042 * @param bitmapData A stream containing the raw data to install as a wallpaper.
1043 * @param visibleCropHint The rectangular subregion of the streamed image that should be
1044 * displayed as wallpaper. Passing {@code null} for this parameter means that
1045 * the full image should be displayed if possible given the image's and device's
1046 * aspect ratios, etc.
1047 * @param allowBackup {@code true} if the OS is permitted to back up this wallpaper
1048 * image for restore to a future device; {@code false} otherwise.
1049 *
1050 * @throws IOException If an error occurs when attempting to set the wallpaper
1051 * based on the provided image data.
1052 * @throws IllegalArgumentException If the {@code visibleCropHint} rectangle is
1053 * empty or invalid.
1054 */
Christopher Tatead3c2592016-01-20 18:13:17 -08001055 public int setStream(InputStream bitmapData, Rect visibleCropHint, boolean allowBackup)
Christopher Tateffa6a882015-12-11 15:08:46 -08001056 throws IOException {
Christopher Tatead3c2592016-01-20 18:13:17 -08001057 return setStream(bitmapData, visibleCropHint, allowBackup, FLAG_SET_SYSTEM);
1058 }
1059
1060 /**
1061 * Version of {@link #setStream(InputStream, Rect, boolean)} that allows the caller
1062 * to specify which of the supported wallpaper categories to set.
1063 *
1064 * @param bitmapData A stream containing the raw data to install as a wallpaper.
1065 * @param visibleCropHint The rectangular subregion of the streamed image that should be
1066 * displayed as wallpaper. Passing {@code null} for this parameter means that
1067 * the full image should be displayed if possible given the image's and device's
1068 * aspect ratios, etc.
1069 * @param allowBackup {@code true} if the OS is permitted to back up this wallpaper
1070 * image for restore to a future device; {@code false} otherwise.
1071 * @param which Flags indicating which wallpaper(s) to configure with the new imagery.
1072 *
Oren Blasberg2eee8c62016-03-14 10:06:08 -07001073 * @see #FLAG_SET_LOCK
1074 * @see #FLAG_SET_SYSTEM
Christopher Tatead3c2592016-01-20 18:13:17 -08001075 *
1076 * @throws IOException
1077 */
1078 public int setStream(InputStream bitmapData, Rect visibleCropHint,
1079 boolean allowBackup, @SetWallpaperFlags int which)
1080 throws IOException {
Christopher Tateffa6a882015-12-11 15:08:46 -08001081 validateRect(visibleCropHint);
Mike Lockwoodc067c9c2011-10-31 12:50:12 -04001082 if (sGlobals.mService == null) {
1083 Log.w(TAG, "WallpaperService not running");
Christopher Tatead3c2592016-01-20 18:13:17 -08001084 return 0;
Mike Lockwoodc067c9c2011-10-31 12:50:12 -04001085 }
Christopher Tatead3c2592016-01-20 18:13:17 -08001086 final Bundle result = new Bundle();
Christopher Tate1e1e2e02016-01-25 15:34:36 -08001087 final WallpaperSetCompletion completion = new WallpaperSetCompletion();
Dianne Hackborn8cc6a502009-08-05 21:29:42 -07001088 try {
Benjamin Franzf3ece362015-02-11 10:51:10 +00001089 ParcelFileDescriptor fd = sGlobals.mService.setWallpaper(null,
Christopher Tate1e1e2e02016-01-25 15:34:36 -08001090 mContext.getOpPackageName(), visibleCropHint, result, which, completion);
Christopher Tatead3c2592016-01-20 18:13:17 -08001091 if (fd != null) {
1092 FileOutputStream fos = null;
1093 try {
1094 fos = new ParcelFileDescriptor.AutoCloseOutputStream(fd);
1095 copyStreamToWallpaperFile(bitmapData, fos);
Christopher Tate1e1e2e02016-01-25 15:34:36 -08001096 fos.close();
1097 completion.waitForCompletion();
Christopher Tatead3c2592016-01-20 18:13:17 -08001098 } finally {
1099 IoUtils.closeQuietly(fos);
1100 }
Dianne Hackborn8cc6a502009-08-05 21:29:42 -07001101 }
1102 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07001103 throw e.rethrowFromSystemServer();
Dianne Hackborn8cc6a502009-08-05 21:29:42 -07001104 }
Christopher Tatead3c2592016-01-20 18:13:17 -08001105
1106 return result.getInt(EXTRA_NEW_WALLPAPER_ID, 0);
Dianne Hackborn8cc6a502009-08-05 21:29:42 -07001107 }
1108
Dianne Hackborn8cc6a502009-08-05 21:29:42 -07001109 /**
Dianne Hackborn5dc5a002012-09-15 19:33:48 -07001110 * Return whether any users are currently set to use the wallpaper
1111 * with the given resource ID. That is, their wallpaper has been
1112 * set through {@link #setResource(int)} with the same resource id.
1113 */
Tor Norbye7b9c9122013-05-30 16:48:33 -07001114 public boolean hasResourceWallpaper(@RawRes int resid) {
Dianne Hackborn5dc5a002012-09-15 19:33:48 -07001115 if (sGlobals.mService == null) {
1116 Log.w(TAG, "WallpaperService not running");
1117 return false;
1118 }
1119 try {
1120 Resources resources = mContext.getResources();
1121 String name = "res:" + resources.getResourceName(resid);
1122 return sGlobals.mService.hasNamedWallpaper(name);
1123 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07001124 throw e.rethrowFromSystemServer();
Dianne Hackborn5dc5a002012-09-15 19:33:48 -07001125 }
1126 }
1127
1128 /**
Dianne Hackborn8cc6a502009-08-05 21:29:42 -07001129 * Returns the desired minimum width for the wallpaper. Callers of
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001130 * {@link #setBitmap(android.graphics.Bitmap)} or
1131 * {@link #setStream(java.io.InputStream)} should check this value
Dianne Hackborn8cc6a502009-08-05 21:29:42 -07001132 * beforehand to make sure the supplied wallpaper respects the desired
1133 * minimum width.
1134 *
1135 * If the returned value is <= 0, the caller should use the width of
1136 * the default display instead.
1137 *
1138 * @return The desired minimum width for the wallpaper. This value should
1139 * be honored by applications that set the wallpaper but it is not
1140 * mandatory.
1141 */
1142 public int getDesiredMinimumWidth() {
Mike Lockwoodc067c9c2011-10-31 12:50:12 -04001143 if (sGlobals.mService == null) {
1144 Log.w(TAG, "WallpaperService not running");
1145 return 0;
1146 }
Dianne Hackborn8cc6a502009-08-05 21:29:42 -07001147 try {
Dianne Hackborn840c3a22009-09-02 17:35:17 -07001148 return sGlobals.mService.getWidthHint();
Dianne Hackborn8cc6a502009-08-05 21:29:42 -07001149 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07001150 throw e.rethrowFromSystemServer();
Dianne Hackborn8cc6a502009-08-05 21:29:42 -07001151 }
1152 }
1153
1154 /**
1155 * Returns the desired minimum height for the wallpaper. Callers of
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001156 * {@link #setBitmap(android.graphics.Bitmap)} or
1157 * {@link #setStream(java.io.InputStream)} should check this value
Dianne Hackborn8cc6a502009-08-05 21:29:42 -07001158 * beforehand to make sure the supplied wallpaper respects the desired
1159 * minimum height.
1160 *
1161 * If the returned value is <= 0, the caller should use the height of
1162 * the default display instead.
1163 *
1164 * @return The desired minimum height for the wallpaper. This value should
1165 * be honored by applications that set the wallpaper but it is not
1166 * mandatory.
1167 */
1168 public int getDesiredMinimumHeight() {
Mike Lockwoodc067c9c2011-10-31 12:50:12 -04001169 if (sGlobals.mService == null) {
1170 Log.w(TAG, "WallpaperService not running");
1171 return 0;
1172 }
Dianne Hackborn8cc6a502009-08-05 21:29:42 -07001173 try {
Dianne Hackborn840c3a22009-09-02 17:35:17 -07001174 return sGlobals.mService.getHeightHint();
Dianne Hackborn8cc6a502009-08-05 21:29:42 -07001175 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07001176 throw e.rethrowFromSystemServer();
Dianne Hackborn8cc6a502009-08-05 21:29:42 -07001177 }
1178 }
1179
1180 /**
1181 * For use only by the current home application, to specify the size of
1182 * wallpaper it would like to use. This allows such applications to have
1183 * a virtual wallpaper that is larger than the physical screen, matching
1184 * the size of their workspace.
Dianne Hackbornc5bf7582012-04-25 19:12:07 -07001185 *
1186 * <p>Note developers, who don't seem to be reading this. This is
Christopher Tateffa6a882015-12-11 15:08:46 -08001187 * for <em>home apps</em> to tell what size wallpaper they would like.
1188 * Nobody else should be calling this! Certainly not other non-home
Dianne Hackbornc5bf7582012-04-25 19:12:07 -07001189 * apps that change the wallpaper. Those apps are supposed to
1190 * <b>retrieve</b> the suggested size so they can construct a wallpaper
1191 * that matches it.
1192 *
Nicolas Falliere9530e3a2012-06-18 17:21:06 -07001193 * <p>This method requires the caller to hold the permission
1194 * {@link android.Manifest.permission#SET_WALLPAPER_HINTS}.
1195 *
Dianne Hackborn8cc6a502009-08-05 21:29:42 -07001196 * @param minimumWidth Desired minimum width
1197 * @param minimumHeight Desired minimum height
1198 */
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001199 public void suggestDesiredDimensions(int minimumWidth, int minimumHeight) {
Dianne Hackborn8cc6a502009-08-05 21:29:42 -07001200 try {
Donghan Ryu289c2732011-11-14 18:56:11 -08001201 /**
1202 * The framework makes no attempt to limit the window size
1203 * to the maximum texture size. Any window larger than this
1204 * cannot be composited.
1205 *
1206 * Read maximum texture size from system property and scale down
1207 * minimumWidth and minimumHeight accordingly.
1208 */
1209 int maximumTextureSize;
1210 try {
1211 maximumTextureSize = SystemProperties.getInt("sys.max_texture_size", 0);
1212 } catch (Exception e) {
1213 maximumTextureSize = 0;
1214 }
1215
1216 if (maximumTextureSize > 0) {
1217 if ((minimumWidth > maximumTextureSize) ||
1218 (minimumHeight > maximumTextureSize)) {
1219 float aspect = (float)minimumHeight / (float)minimumWidth;
1220 if (minimumWidth > minimumHeight) {
1221 minimumWidth = maximumTextureSize;
1222 minimumHeight = (int)((minimumWidth * aspect) + 0.5);
1223 } else {
1224 minimumHeight = maximumTextureSize;
1225 minimumWidth = (int)((minimumHeight / aspect) + 0.5);
1226 }
1227 }
1228 }
1229
Mike Lockwoodc067c9c2011-10-31 12:50:12 -04001230 if (sGlobals.mService == null) {
1231 Log.w(TAG, "WallpaperService not running");
1232 } else {
Benjamin Franzf3ece362015-02-11 10:51:10 +00001233 sGlobals.mService.setDimensionHints(minimumWidth, minimumHeight,
1234 mContext.getOpPackageName());
Mike Lockwoodc067c9c2011-10-31 12:50:12 -04001235 }
Dianne Hackborn8cc6a502009-08-05 21:29:42 -07001236 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07001237 throw e.rethrowFromSystemServer();
Dianne Hackborn8cc6a502009-08-05 21:29:42 -07001238 }
1239 }
Adam Cohen041a0ba2011-11-09 20:10:27 -08001240
Dianne Hackborn8cc6a502009-08-05 21:29:42 -07001241 /**
Dianne Hackborn067e5f62014-09-07 23:14:30 -07001242 * Specify extra padding that the wallpaper should have outside of the display.
1243 * That is, the given padding supplies additional pixels the wallpaper should extend
1244 * outside of the display itself.
1245 * @param padding The number of pixels the wallpaper should extend beyond the display,
1246 * on its left, top, right, and bottom sides.
1247 * @hide
1248 */
1249 @SystemApi
1250 public void setDisplayPadding(Rect padding) {
1251 try {
1252 if (sGlobals.mService == null) {
1253 Log.w(TAG, "WallpaperService not running");
1254 } else {
Benjamin Franzf3ece362015-02-11 10:51:10 +00001255 sGlobals.mService.setDisplayPadding(padding, mContext.getOpPackageName());
Dianne Hackborn067e5f62014-09-07 23:14:30 -07001256 }
1257 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07001258 throw e.rethrowFromSystemServer();
Dianne Hackborn067e5f62014-09-07 23:14:30 -07001259 }
1260 }
1261
1262 /**
1263 * Apply a raw offset to the wallpaper window. Should only be used in
1264 * combination with {@link #setDisplayPadding(android.graphics.Rect)} when you
1265 * have ensured that the wallpaper will extend outside of the display area so that
1266 * it can be moved without leaving part of the display uncovered.
1267 * @param x The offset, in pixels, to apply to the left edge.
1268 * @param y The offset, in pixels, to apply to the top edge.
1269 * @hide
1270 */
1271 @SystemApi
1272 public void setDisplayOffset(IBinder windowToken, int x, int y) {
1273 try {
1274 //Log.v(TAG, "Sending new wallpaper display offsets from app...");
1275 WindowManagerGlobal.getWindowSession().setWallpaperDisplayOffset(
1276 windowToken, x, y);
1277 //Log.v(TAG, "...app returning after sending display offset!");
1278 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07001279 throw e.rethrowFromSystemServer();
Dianne Hackborn067e5f62014-09-07 23:14:30 -07001280 }
1281 }
1282
1283 /**
Filip Gruszczynski9fedc722015-01-28 15:59:46 -08001284 * Clear the wallpaper.
1285 *
1286 * @hide
1287 */
1288 @SystemApi
1289 public void clearWallpaper() {
Christopher Tatebe132e62016-02-10 12:59:49 -08001290 clearWallpaper(FLAG_SET_SYSTEM, mContext.getUserId());
1291 }
1292
1293 /**
1294 * Clear the wallpaper for a specific user. The caller must hold the
1295 * INTERACT_ACROSS_USERS_FULL permission to clear another user's
1296 * wallpaper.
1297 * @hide
1298 */
1299 @SystemApi
1300 public void clearWallpaper(int which, int userId) {
Filip Gruszczynski9fedc722015-01-28 15:59:46 -08001301 if (sGlobals.mService == null) {
1302 Log.w(TAG, "WallpaperService not running");
1303 return;
1304 }
1305 try {
Christopher Tatebe132e62016-02-10 12:59:49 -08001306 sGlobals.mService.clearWallpaper(mContext.getOpPackageName(), which, userId);
Filip Gruszczynski9fedc722015-01-28 15:59:46 -08001307 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07001308 throw e.rethrowFromSystemServer();
Filip Gruszczynski9fedc722015-01-28 15:59:46 -08001309 }
1310 }
1311
1312 /**
1313 * Set the live wallpaper.
1314 *
1315 * This can only be called by packages with android.permission.SET_WALLPAPER_COMPONENT
1316 * permission.
1317 *
1318 * @hide
1319 */
1320 @SystemApi
1321 public boolean setWallpaperComponent(ComponentName name) {
1322 if (sGlobals.mService == null) {
1323 Log.w(TAG, "WallpaperService not running");
1324 return false;
1325 }
1326 try {
Benjamin Franzf3ece362015-02-11 10:51:10 +00001327 sGlobals.mService.setWallpaperComponentChecked(name, mContext.getOpPackageName());
Filip Gruszczynski9fedc722015-01-28 15:59:46 -08001328 return true;
1329 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07001330 throw e.rethrowFromSystemServer();
Filip Gruszczynski9fedc722015-01-28 15:59:46 -08001331 }
Filip Gruszczynski9fedc722015-01-28 15:59:46 -08001332 }
1333
1334 /**
Christopher Tateffa6a882015-12-11 15:08:46 -08001335 * Set the display position of the current wallpaper within any larger space, when
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07001336 * that wallpaper is visible behind the given window. The X and Y offsets
1337 * are floating point numbers ranging from 0 to 1, representing where the
1338 * wallpaper should be positioned within the screen space. These only
Christopher Tateffa6a882015-12-11 15:08:46 -08001339 * make sense when the wallpaper is larger than the display.
Oleksandr Peletskyif2519812016-01-26 20:16:06 +01001340 *
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07001341 * @param windowToken The window who these offsets should be associated
Scott Main8b2e0002009-09-29 18:17:31 -07001342 * with, as returned by {@link android.view.View#getWindowToken()
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07001343 * View.getWindowToken()}.
Marco Nelissenbf6956b2009-11-09 15:21:13 -08001344 * @param xOffset The offset along the X dimension, from 0 to 1.
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07001345 * @param yOffset The offset along the Y dimension, from 0 to 1.
1346 */
1347 public void setWallpaperOffsets(IBinder windowToken, float xOffset, float yOffset) {
Adam Cohen791a6332012-01-12 14:38:38 -08001348 try {
1349 //Log.v(TAG, "Sending new wallpaper offsets from app...");
Jeff Brownf9e989d2013-04-04 23:04:03 -07001350 WindowManagerGlobal.getWindowSession().setWallpaperPosition(
Adam Cohen791a6332012-01-12 14:38:38 -08001351 windowToken, xOffset, yOffset, mWallpaperXStep, mWallpaperYStep);
1352 //Log.v(TAG, "...app returning after sending offsets!");
1353 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07001354 throw e.rethrowFromSystemServer();
Adam Cohen791a6332012-01-12 14:38:38 -08001355 }
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07001356 }
Adam Cohen041a0ba2011-11-09 20:10:27 -08001357
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07001358 /**
Marco Nelissenbf6956b2009-11-09 15:21:13 -08001359 * For applications that use multiple virtual screens showing a wallpaper,
1360 * specify the step size between virtual screens. For example, if the
Dianne Hackborn23ef7b42009-11-18 18:20:39 -08001361 * launcher has 3 virtual screens, it would specify an xStep of 0.5,
Marco Nelissenbf6956b2009-11-09 15:21:13 -08001362 * since the X offset for those screens are 0.0, 0.5 and 1.0
Oleksandr Peletskyif2519812016-01-26 20:16:06 +01001363 * @param xStep The X offset delta from one screen to the next one
Marco Nelissenbf6956b2009-11-09 15:21:13 -08001364 * @param yStep The Y offset delta from one screen to the next one
1365 */
1366 public void setWallpaperOffsetSteps(float xStep, float yStep) {
1367 mWallpaperXStep = xStep;
1368 mWallpaperYStep = yStep;
1369 }
Oleksandr Peletskyif2519812016-01-26 20:16:06 +01001370
Marco Nelissenbf6956b2009-11-09 15:21:13 -08001371 /**
Dianne Hackborn75804932009-10-20 20:15:20 -07001372 * Send an arbitrary command to the current active wallpaper.
Oleksandr Peletskyif2519812016-01-26 20:16:06 +01001373 *
Dianne Hackborn75804932009-10-20 20:15:20 -07001374 * @param windowToken The window who these offsets should be associated
1375 * with, as returned by {@link android.view.View#getWindowToken()
1376 * View.getWindowToken()}.
1377 * @param action Name of the command to perform. This must be a scoped
1378 * name to avoid collisions, such as "com.mycompany.wallpaper.DOIT".
1379 * @param x Arbitrary integer argument based on command.
1380 * @param y Arbitrary integer argument based on command.
1381 * @param z Arbitrary integer argument based on command.
1382 * @param extras Optional additional information for the command, or null.
1383 */
1384 public void sendWallpaperCommand(IBinder windowToken, String action,
1385 int x, int y, int z, Bundle extras) {
1386 try {
1387 //Log.v(TAG, "Sending new wallpaper offsets from app...");
Jeff Brownf9e989d2013-04-04 23:04:03 -07001388 WindowManagerGlobal.getWindowSession().sendWallpaperCommand(
Dianne Hackborn75804932009-10-20 20:15:20 -07001389 windowToken, action, x, y, z, extras, false);
1390 //Log.v(TAG, "...app returning after sending offsets!");
1391 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07001392 throw e.rethrowFromSystemServer();
Dianne Hackborn75804932009-10-20 20:15:20 -07001393 }
1394 }
Benjamin Franzf3ece362015-02-11 10:51:10 +00001395
1396 /**
1397 * Returns whether wallpapers are supported for the calling user. If this function returns
Oleksandr Peletskyif2519812016-01-26 20:16:06 +01001398 * {@code false}, any attempts to changing the wallpaper will have no effect,
1399 * and any attempt to obtain of the wallpaper will return {@code null}.
Benjamin Franzf3ece362015-02-11 10:51:10 +00001400 */
1401 public boolean isWallpaperSupported() {
1402 if (sGlobals.mService == null) {
1403 Log.w(TAG, "WallpaperService not running");
1404 } else {
1405 try {
1406 return sGlobals.mService.isWallpaperSupported(mContext.getOpPackageName());
1407 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07001408 throw e.rethrowFromSystemServer();
Benjamin Franzf3ece362015-02-11 10:51:10 +00001409 }
1410 }
1411 return false;
1412 }
1413
Dianne Hackborn75804932009-10-20 20:15:20 -07001414 /**
Oleksandr Peletskyif2519812016-01-26 20:16:06 +01001415 * Returns whether the calling package is allowed to set the wallpaper for the calling user.
1416 * If this function returns {@code false}, any attempts to change the wallpaper will have
1417 * no effect. Always returns {@code true} for device owner and profile owner.
1418 *
1419 * @see android.os.UserManager#DISALLOW_SET_WALLPAPER
1420 */
1421 public boolean isWallpaperSettingAllowed() {
1422 if (sGlobals.mService == null) {
1423 Log.w(TAG, "WallpaperService not running");
1424 } else {
1425 try {
1426 return sGlobals.mService.isWallpaperSettingAllowed(mContext.getOpPackageName());
1427 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07001428 throw e.rethrowFromSystemServer();
Oleksandr Peletskyif2519812016-01-26 20:16:06 +01001429 }
1430 }
1431 return false;
1432 }
1433
1434 /**
Dianne Hackborn72c82ab2009-08-11 21:13:54 -07001435 * Clear the offsets previously associated with this window through
1436 * {@link #setWallpaperOffsets(IBinder, float, float)}. This reverts
1437 * the window to its default state, where it does not cause the wallpaper
1438 * to scroll from whatever its last offsets were.
Oleksandr Peletskyif2519812016-01-26 20:16:06 +01001439 *
Dianne Hackborn72c82ab2009-08-11 21:13:54 -07001440 * @param windowToken The window who these offsets should be associated
Scott Main8b2e0002009-09-29 18:17:31 -07001441 * with, as returned by {@link android.view.View#getWindowToken()
Dianne Hackborn72c82ab2009-08-11 21:13:54 -07001442 * View.getWindowToken()}.
1443 */
1444 public void clearWallpaperOffsets(IBinder windowToken) {
1445 try {
Jeff Brownf9e989d2013-04-04 23:04:03 -07001446 WindowManagerGlobal.getWindowSession().setWallpaperPosition(
Marco Nelissenbf6956b2009-11-09 15:21:13 -08001447 windowToken, -1, -1, -1, -1);
Dianne Hackborn72c82ab2009-08-11 21:13:54 -07001448 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07001449 throw e.rethrowFromSystemServer();
Dianne Hackborn72c82ab2009-08-11 21:13:54 -07001450 }
1451 }
Oleksandr Peletskyif2519812016-01-26 20:16:06 +01001452
Dianne Hackborn72c82ab2009-08-11 21:13:54 -07001453 /**
Christopher Tatebe132e62016-02-10 12:59:49 -08001454 * Remove any currently set system wallpaper, reverting to the system's built-in
Dianne Hackborn8cc6a502009-08-05 21:29:42 -07001455 * wallpaper. On success, the intent {@link Intent#ACTION_WALLPAPER_CHANGED}
1456 * is broadcast.
1457 *
Nicolas Falliere9530e3a2012-06-18 17:21:06 -07001458 * <p>This method requires the caller to hold the permission
1459 * {@link android.Manifest.permission#SET_WALLPAPER}.
1460 *
Michael Jurkab668d0b2013-10-04 15:11:05 -07001461 * @throws IOException If an error occurs reverting to the built-in
Dianne Hackborn8cc6a502009-08-05 21:29:42 -07001462 * wallpaper.
1463 */
1464 public void clear() throws IOException {
Jeff Sharkey28f08772014-04-16 09:41:58 -07001465 setStream(openDefaultWallpaper(mContext));
1466 }
1467
1468 /**
Christopher Tate79a24572016-03-02 14:42:44 -08001469 * Remove one or more currently set wallpapers, reverting to the system default
1470 * display for each one. If {@link #FLAG_SET_SYSTEM} is set in the {@code which}
1471 * parameter, the intent {@link Intent#ACTION_WALLPAPER_CHANGED} will be broadcast
1472 * upon success.
1473 *
1474 * @param which A bitwise combination of {@link #FLAG_SET_SYSTEM} or
1475 * {@link #FLAG_SET_LOCK}
1476 * @throws IOException If an error occurs reverting to the built-in wallpaper.
1477 */
1478 public void clear(int which) throws IOException {
1479 if ((which & FLAG_SET_SYSTEM) != 0) {
1480 clear();
1481 }
1482 if ((which & FLAG_SET_LOCK) != 0) {
1483 clearWallpaper(FLAG_SET_LOCK, mContext.getUserId());
1484 }
1485 }
1486
1487 /**
Jeff Sharkey28f08772014-04-16 09:41:58 -07001488 * Open stream representing the default static image wallpaper.
1489 *
1490 * @hide
1491 */
1492 public static InputStream openDefaultWallpaper(Context context) {
1493 final String path = SystemProperties.get(PROP_WALLPAPER);
1494 if (!TextUtils.isEmpty(path)) {
1495 final File file = new File(path);
1496 if (file.exists()) {
1497 try {
1498 return new FileInputStream(file);
1499 } catch (IOException e) {
1500 // Ignored, fall back to platform default below
1501 }
1502 }
1503 }
1504 return context.getResources().openRawResource(
1505 com.android.internal.R.drawable.default_wallpaper);
1506 }
1507
1508 /**
1509 * Return {@link ComponentName} of the default live wallpaper, or
1510 * {@code null} if none is defined.
1511 *
1512 * @hide
1513 */
1514 public static ComponentName getDefaultWallpaperComponent(Context context) {
1515 String flat = SystemProperties.get(PROP_WALLPAPER_COMPONENT);
1516 if (!TextUtils.isEmpty(flat)) {
1517 final ComponentName cn = ComponentName.unflattenFromString(flat);
1518 if (cn != null) {
1519 return cn;
1520 }
1521 }
1522
1523 flat = context.getString(com.android.internal.R.string.default_wallpaper_component);
1524 if (!TextUtils.isEmpty(flat)) {
1525 final ComponentName cn = ComponentName.unflattenFromString(flat);
1526 if (cn != null) {
1527 return cn;
1528 }
1529 }
1530
1531 return null;
Dianne Hackborn8cc6a502009-08-05 21:29:42 -07001532 }
Christopher Tate1e1e2e02016-01-25 15:34:36 -08001533
Christopher Tatebe132e62016-02-10 12:59:49 -08001534 /**
1535 * Register a callback for lock wallpaper observation. Only the OS may use this.
1536 *
1537 * @return true on success; false on error.
1538 * @hide
1539 */
1540 public boolean setLockWallpaperCallback(IWallpaperManagerCallback callback) {
1541 if (sGlobals.mService == null) {
1542 Log.w(TAG, "WallpaperService not running");
1543 return false;
1544 }
1545
1546 try {
1547 return sGlobals.mService.setLockWallpaperCallback(callback);
1548 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07001549 throw e.rethrowFromSystemServer();
Christopher Tatebe132e62016-02-10 12:59:49 -08001550 }
Christopher Tatebe132e62016-02-10 12:59:49 -08001551 }
1552
Christopher Tate1e1e2e02016-01-25 15:34:36 -08001553 // Private completion callback for setWallpaper() synchronization
1554 private class WallpaperSetCompletion extends IWallpaperManagerCallback.Stub {
1555 final CountDownLatch mLatch;
1556
1557 public WallpaperSetCompletion() {
1558 mLatch = new CountDownLatch(1);
1559 }
1560
1561 public void waitForCompletion() {
1562 try {
1563 mLatch.await(30, TimeUnit.SECONDS);
1564 } catch (InterruptedException e) {
1565 // This might be legit: the crop may take a very long time. Don't sweat
1566 // it in that case; we are okay with display lagging behind in order to
1567 // keep the caller from locking up indeterminately.
1568 }
1569 }
1570
1571 @Override
1572 public void onWallpaperChanged() throws RemoteException {
1573 mLatch.countDown();
1574 }
1575 }
Dianne Hackborn8cc6a502009-08-05 21:29:42 -07001576}