blob: 72b9318c2acd3193ae8d690592c096ac276a16c3 [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 = {
Christopher Tateedf7d042016-03-29 18:24:25 -0700164 FLAG_SYSTEM,
165 FLAG_LOCK
Christopher Tatead3c2592016-01-20 18:13:17 -0800166 })
167 @Retention(RetentionPolicy.SOURCE)
168 public @interface SetWallpaperFlags {}
169
170 /**
171 * Flag: use the supplied imagery as the general system wallpaper.
172 */
Christopher Tateedf7d042016-03-29 18:24:25 -0700173 public static final int FLAG_SYSTEM = 1 << 0;
Christopher Tatead3c2592016-01-20 18:13:17 -0800174
175 /**
176 * Flag: use the supplied imagery as the lock-screen wallpaper.
177 */
Christopher Tateedf7d042016-03-29 18:24:25 -0700178 public static final int FLAG_LOCK = 1 << 1;
Christopher Tatead3c2592016-01-20 18:13:17 -0800179
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 Tateedf7d042016-03-29 18:24:25 -0700339 ParcelFileDescriptor fd = mService.getWallpaper(this, FLAG_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.
Christopher Tateedf7d042016-03-29 18:24:25 -0700666 * The caller is responsible for closing the file descriptor when done ingesting the file.
Christopher Tatebe132e62016-02-10 12:59:49 -0800667 *
668 * <p>If no lock-specific wallpaper has been configured for the given user, then
Christopher Tateedf7d042016-03-29 18:24:25 -0700669 * this method will return {@code null} when requesting {@link #FLAG_LOCK} rather than
Christopher Tatebe132e62016-02-10 12:59:49 -0800670 * returning the system wallpaper's image file.
Christopher Tateedf7d042016-03-29 18:24:25 -0700671 *
672 * @param which The wallpaper whose image file is to be retrieved. Must be a single
673 * defined kind of wallpaper, either {@link #FLAG_SYSTEM} or
674 * {@link #FLAG_LOCK}.
675 *
676 * @see #FLAG_LOCK
677 * @see #FLAG_SYSTEM
Christopher Tatebe132e62016-02-10 12:59:49 -0800678 */
679 public ParcelFileDescriptor getWallpaperFile(int which) {
680 return getWallpaperFile(which, mContext.getUserId());
681 }
682
683 /**
684 * Version of {@link #getWallpaperFile(int)} that can access the wallpaper data
685 * for a given user. The caller must hold the INTERACT_ACROSS_USERS_FULL
686 * permission to access another user's wallpaper data.
Christopher Tateedf7d042016-03-29 18:24:25 -0700687 *
688 * @param which The wallpaper whose image file is to be retrieved. Must be a single
689 * defined kind of wallpaper, either {@link #FLAG_SYSTEM} or
690 * {@link #FLAG_LOCK}.
691 * @param userId The user or profile whose imagery is to be retrieved
692 *
693 * @see #FLAG_LOCK
694 * @see #FLAG_SYSTEM
695 *
Christopher Tatebe132e62016-02-10 12:59:49 -0800696 * @hide
697 */
698 public ParcelFileDescriptor getWallpaperFile(int which, int userId) {
Christopher Tateedf7d042016-03-29 18:24:25 -0700699 if (which != FLAG_SYSTEM && which != FLAG_LOCK) {
Christopher Tatebe132e62016-02-10 12:59:49 -0800700 throw new IllegalArgumentException("Must request exactly one kind of wallpaper");
701 }
702
703 if (sGlobals.mService == null) {
704 Log.w(TAG, "WallpaperService not running");
705 return null;
706 } else {
707 try {
708 Bundle outParams = new Bundle();
709 return sGlobals.mService.getWallpaper(null, which, outParams, userId);
710 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -0700711 throw e.rethrowFromSystemServer();
Christopher Tatebe132e62016-02-10 12:59:49 -0800712 }
713 }
714 }
715
716 /**
Dianne Hackbornba398392011-08-01 16:11:57 -0700717 * Remove all internal references to the last loaded wallpaper. Useful
718 * for apps that want to reduce memory usage when they only temporarily
719 * need to have the wallpaper. After calling, the next request for the
720 * wallpaper will require reloading it again from disk.
721 */
722 public void forgetLoadedWallpaper() {
Christopher Tatebe132e62016-02-10 12:59:49 -0800723 sGlobals.forgetLoadedWallpaper();
Dianne Hackbornba398392011-08-01 16:11:57 -0700724 }
725
726 /**
Dianne Hackborneb034652009-09-07 00:49:58 -0700727 * If the current wallpaper is a live wallpaper component, return the
728 * information about that wallpaper. Otherwise, if it is a static image,
729 * simply return null.
Dianne Hackborneb034652009-09-07 00:49:58 -0700730 */
731 public WallpaperInfo getWallpaperInfo() {
732 try {
Mike Lockwoodc067c9c2011-10-31 12:50:12 -0400733 if (sGlobals.mService == null) {
734 Log.w(TAG, "WallpaperService not running");
735 return null;
736 } else {
737 return sGlobals.mService.getWallpaperInfo();
738 }
Dianne Hackborneb034652009-09-07 00:49:58 -0700739 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -0700740 throw e.rethrowFromSystemServer();
Dianne Hackborneb034652009-09-07 00:49:58 -0700741 }
742 }
Michael Jurkae8d1bf72013-09-09 15:58:54 +0200743
744 /**
Christopher Tatee409f0e2016-03-21 14:53:15 -0700745 * Get the ID of the current wallpaper of the given kind. If there is no
746 * such wallpaper configured, returns a negative number.
747 *
748 * @param which The wallpaper whose ID is to be returned. Must be a single
Christopher Tateedf7d042016-03-29 18:24:25 -0700749 * defined kind of wallpaper, either {@link #FLAG_SYSTEM} or
750 * {@link #FLAG_LOCK}.
Christopher Tatee409f0e2016-03-21 14:53:15 -0700751 * @return The positive numeric ID of the current wallpaper of the given kind,
752 * or a negative value if no such wallpaper is configured.
753 */
754 public int getWallpaperId(int which) {
755 return getWallpaperIdForUser(which, mContext.getUserId());
756 }
757
758 /**
759 * Get the ID of the given user's current wallpaper of the given kind. If there
760 * is no such wallpaper configured, returns a negative number.
761 * @hide
762 */
763 public int getWallpaperIdForUser(int which, int userId) {
764 try {
765 if (sGlobals.mService == null) {
766 Log.w(TAG, "WallpaperService not running");
767 return -1;
768 } else {
769 return sGlobals.mService.getWallpaperIdForUser(which, userId);
770 }
771 } catch (RemoteException e) {
772 throw e.rethrowFromSystemServer();
773 }
774 }
775
776 /**
Michael Jurkae8d1bf72013-09-09 15:58:54 +0200777 * Gets an Intent that will launch an activity that crops the given
778 * image and sets the device's wallpaper. If there is a default HOME activity
779 * that supports cropping wallpapers, it will be preferred as the default.
Ying Wang930d4e52013-09-14 11:57:17 -0700780 * Use this method instead of directly creating a {@link #ACTION_CROP_AND_SET_WALLPAPER}
Michael Jurkae8d1bf72013-09-09 15:58:54 +0200781 * intent.
Adam Lesinskibba72d12013-10-11 18:10:56 -0700782 *
783 * @param imageUri The image URI that will be set in the intent. The must be a content
784 * URI and its provider must resolve its type to "image/*"
785 *
786 * @throws IllegalArgumentException if the URI is not a content URI or its MIME type is
787 * not "image/*"
Michael Jurkae8d1bf72013-09-09 15:58:54 +0200788 */
789 public Intent getCropAndSetWallpaperIntent(Uri imageUri) {
Allen Shen1c0f21e2014-04-29 16:16:29 +0800790 if (imageUri == null) {
791 throw new IllegalArgumentException("Image URI must not be null");
792 }
793
Adam Lesinskibba72d12013-10-11 18:10:56 -0700794 if (!ContentResolver.SCHEME_CONTENT.equals(imageUri.getScheme())) {
795 throw new IllegalArgumentException("Image URI must be of the "
796 + ContentResolver.SCHEME_CONTENT + " scheme type");
797 }
798
Michael Jurkae8d1bf72013-09-09 15:58:54 +0200799 final PackageManager packageManager = mContext.getPackageManager();
800 Intent cropAndSetWallpaperIntent =
801 new Intent(ACTION_CROP_AND_SET_WALLPAPER, imageUri);
802 cropAndSetWallpaperIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
803
804 // Find out if the default HOME activity supports CROP_AND_SET_WALLPAPER
805 Intent homeIntent = new Intent(Intent.ACTION_MAIN).addCategory(Intent.CATEGORY_HOME);
806 ResolveInfo resolvedHome = packageManager.resolveActivity(homeIntent,
807 PackageManager.MATCH_DEFAULT_ONLY);
808 if (resolvedHome != null) {
809 cropAndSetWallpaperIntent.setPackage(resolvedHome.activityInfo.packageName);
810
811 List<ResolveInfo> cropAppList = packageManager.queryIntentActivities(
812 cropAndSetWallpaperIntent, 0);
813 if (cropAppList.size() > 0) {
814 return cropAndSetWallpaperIntent;
815 }
816 }
817
818 // fallback crop activity
Oren Blasberg60598ea0a2016-02-26 11:01:23 -0800819 final String cropperPackage = mContext.getString(
820 com.android.internal.R.string.config_wallpaperCropperPackage);
821 cropAndSetWallpaperIntent.setPackage(cropperPackage);
Adam Lesinskibba72d12013-10-11 18:10:56 -0700822 List<ResolveInfo> cropAppList = packageManager.queryIntentActivities(
823 cropAndSetWallpaperIntent, 0);
824 if (cropAppList.size() > 0) {
825 return cropAndSetWallpaperIntent;
826 }
827 // If the URI is not of the right type, or for some reason the system wallpaper
828 // cropper doesn't exist, return null
829 throw new IllegalArgumentException("Cannot use passed URI to set wallpaper; " +
830 "check that the type returned by ContentProvider matches image/*");
Michael Jurkae8d1bf72013-09-09 15:58:54 +0200831 }
832
Dianne Hackborneb034652009-09-07 00:49:58 -0700833 /**
Dianne Hackborn8cc6a502009-08-05 21:29:42 -0700834 * Change the current system wallpaper to the bitmap in the given resource.
835 * The resource is opened as a raw data stream and copied into the
836 * wallpaper; it must be a valid PNG or JPEG image. On success, the intent
837 * {@link Intent#ACTION_WALLPAPER_CHANGED} is broadcast.
838 *
Nicolas Falliere9530e3a2012-06-18 17:21:06 -0700839 * <p>This method requires the caller to hold the permission
840 * {@link android.Manifest.permission#SET_WALLPAPER}.
841 *
Christopher Tateedf7d042016-03-29 18:24:25 -0700842 * @param resid The resource ID of the bitmap to be used as the wallpaper image
Dianne Hackborn8cc6a502009-08-05 21:29:42 -0700843 *
Michael Jurkab668d0b2013-10-04 15:11:05 -0700844 * @throws IOException If an error occurs reverting to the built-in
Dianne Hackborn8cc6a502009-08-05 21:29:42 -0700845 * wallpaper.
846 */
Tor Norbye7b9c9122013-05-30 16:48:33 -0700847 public void setResource(@RawRes int resid) throws IOException {
Christopher Tateedf7d042016-03-29 18:24:25 -0700848 setResource(resid, FLAG_SYSTEM);
Christopher Tatead3c2592016-01-20 18:13:17 -0800849 }
850
851 /**
Christopher Tateedf7d042016-03-29 18:24:25 -0700852 * Version of {@link #setResource(int)} that allows the caller to specify which
853 * of the supported wallpaper categories to set.
Christopher Tatead3c2592016-01-20 18:13:17 -0800854 *
Christopher Tateedf7d042016-03-29 18:24:25 -0700855 * @param resid The resource ID of the bitmap to be used as the wallpaper image
856 * @param which Flags indicating which wallpaper(s) to configure with the new imagery
Christopher Tatead3c2592016-01-20 18:13:17 -0800857 *
Christopher Tateedf7d042016-03-29 18:24:25 -0700858 * @see #FLAG_LOCK
859 * @see #FLAG_SYSTEM
Christopher Tatead3c2592016-01-20 18:13:17 -0800860 *
861 * @return An integer ID assigned to the newly active wallpaper; or zero on failure.
862 *
863 * @throws IOException
864 */
865 public int setResource(@RawRes int resid, @SetWallpaperFlags int which)
866 throws IOException {
Mike Lockwoodc067c9c2011-10-31 12:50:12 -0400867 if (sGlobals.mService == null) {
868 Log.w(TAG, "WallpaperService not running");
Christopher Tatead3c2592016-01-20 18:13:17 -0800869 return 0;
Mike Lockwoodc067c9c2011-10-31 12:50:12 -0400870 }
Christopher Tatead3c2592016-01-20 18:13:17 -0800871 final Bundle result = new Bundle();
Christopher Tate1e1e2e02016-01-25 15:34:36 -0800872 final WallpaperSetCompletion completion = new WallpaperSetCompletion();
Dianne Hackborn8cc6a502009-08-05 21:29:42 -0700873 try {
874 Resources resources = mContext.getResources();
875 /* Set the wallpaper to the default values */
Dianne Hackborn840c3a22009-09-02 17:35:17 -0700876 ParcelFileDescriptor fd = sGlobals.mService.setWallpaper(
Christopher Tatead3c2592016-01-20 18:13:17 -0800877 "res:" + resources.getResourceName(resid),
Christopher Tate1e1e2e02016-01-25 15:34:36 -0800878 mContext.getOpPackageName(), null, result, which, completion);
Dianne Hackborn8cc6a502009-08-05 21:29:42 -0700879 if (fd != null) {
880 FileOutputStream fos = null;
Christopher Tate1e1e2e02016-01-25 15:34:36 -0800881 boolean ok = false;
Dianne Hackborn8cc6a502009-08-05 21:29:42 -0700882 try {
883 fos = new ParcelFileDescriptor.AutoCloseOutputStream(fd);
Christopher Tatead3c2592016-01-20 18:13:17 -0800884 copyStreamToWallpaperFile(resources.openRawResource(resid), fos);
Christopher Tate1e1e2e02016-01-25 15:34:36 -0800885 // The 'close()' is the trigger for any server-side image manipulation,
886 // so we must do that before waiting for completion.
887 fos.close();
888 completion.waitForCompletion();
Dianne Hackborn8cc6a502009-08-05 21:29:42 -0700889 } finally {
Christopher Tate1e1e2e02016-01-25 15:34:36 -0800890 // Might be redundant but completion shouldn't wait unless the write
891 // succeeded; this is a fallback if it threw past the close+wait.
Christopher Tateffa6a882015-12-11 15:08:46 -0800892 IoUtils.closeQuietly(fos);
Dianne Hackborn8cc6a502009-08-05 21:29:42 -0700893 }
894 }
895 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -0700896 throw e.rethrowFromSystemServer();
Dianne Hackborn8cc6a502009-08-05 21:29:42 -0700897 }
Christopher Tatead3c2592016-01-20 18:13:17 -0800898 return result.getInt(EXTRA_NEW_WALLPAPER_ID, 0);
Dianne Hackborn8cc6a502009-08-05 21:29:42 -0700899 }
Christopher Tateffa6a882015-12-11 15:08:46 -0800900
Dianne Hackborn8cc6a502009-08-05 21:29:42 -0700901 /**
902 * Change the current system wallpaper to a bitmap. The given bitmap is
903 * converted to a PNG and stored as the wallpaper. On success, the intent
904 * {@link Intent#ACTION_WALLPAPER_CHANGED} is broadcast.
905 *
Christopher Tateffa6a882015-12-11 15:08:46 -0800906 * <p>This method is equivalent to calling
907 * {@link #setBitmap(Bitmap, Rect, boolean)} and passing {@code null} for the
908 * {@code visibleCrop} rectangle and {@code true} for the {@code allowBackup}
909 * parameter.
910 *
Nicolas Falliere9530e3a2012-06-18 17:21:06 -0700911 * <p>This method requires the caller to hold the permission
912 * {@link android.Manifest.permission#SET_WALLPAPER}.
913 *
Christopher Tatead3c2592016-01-20 18:13:17 -0800914 * @param bitmap The bitmap to be used as the new system wallpaper.
Dianne Hackborn8cc6a502009-08-05 21:29:42 -0700915 *
Christopher Tateffa6a882015-12-11 15:08:46 -0800916 * @throws IOException If an error occurs when attempting to set the wallpaper
917 * to the provided image.
Dianne Hackborn8cc6a502009-08-05 21:29:42 -0700918 */
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700919 public void setBitmap(Bitmap bitmap) throws IOException {
Christopher Tateffa6a882015-12-11 15:08:46 -0800920 setBitmap(bitmap, null, true);
921 }
922
923 /**
924 * Change the current system wallpaper to a bitmap, specifying a hint about
925 * which subrectangle of the full image is to be visible. The OS will then
926 * try to best present the given portion of the full image as the static system
927 * wallpaper image. On success, the intent
928 * {@link Intent#ACTION_WALLPAPER_CHANGED} is broadcast.
929 *
930 * <p>Passing {@code null} as the {@code visibleHint} parameter is equivalent to
931 * passing (0, 0, {@code fullImage.getWidth()}, {@code fullImage.getHeight()}).
932 *
933 * <p>This method requires the caller to hold the permission
934 * {@link android.Manifest.permission#SET_WALLPAPER}.
935 *
Christopher Tatead3c2592016-01-20 18:13:17 -0800936 * @param fullImage A bitmap that will supply the wallpaper imagery.
Christopher Tateffa6a882015-12-11 15:08:46 -0800937 * @param visibleCropHint The rectangular subregion of {@code fullImage} that should be
938 * displayed as wallpaper. Passing {@code null} for this parameter means that
939 * the full image should be displayed if possible given the image's and device's
Christopher Tatead3c2592016-01-20 18:13:17 -0800940 * aspect ratios, etc.
Christopher Tateffa6a882015-12-11 15:08:46 -0800941 * @param allowBackup {@code true} if the OS is permitted to back up this wallpaper
942 * image for restore to a future device; {@code false} otherwise.
943 *
Christopher Tatead3c2592016-01-20 18:13:17 -0800944 * @return An integer ID assigned to the newly active wallpaper; or zero on failure.
945 *
Christopher Tateffa6a882015-12-11 15:08:46 -0800946 * @throws IOException If an error occurs when attempting to set the wallpaper
947 * to the provided image.
948 * @throws IllegalArgumentException If the {@code visibleCropHint} rectangle is
949 * empty or invalid.
950 */
Christopher Tatead3c2592016-01-20 18:13:17 -0800951 public int setBitmap(Bitmap fullImage, Rect visibleCropHint, boolean allowBackup)
952 throws IOException {
Christopher Tateedf7d042016-03-29 18:24:25 -0700953 return setBitmap(fullImage, visibleCropHint, allowBackup, FLAG_SYSTEM);
Christopher Tatead3c2592016-01-20 18:13:17 -0800954 }
955
956 /**
Christopher Tatead3c2592016-01-20 18:13:17 -0800957 * Version of {@link #setBitmap(Bitmap, Rect, boolean)} that allows the caller
958 * to specify which of the supported wallpaper categories to set.
959 *
960 * @param fullImage A bitmap that will supply the wallpaper imagery.
961 * @param visibleCropHint The rectangular subregion of {@code fullImage} that should be
962 * displayed as wallpaper. Passing {@code null} for this parameter means that
963 * the full image should be displayed if possible given the image's and device's
964 * aspect ratios, etc.
965 * @param allowBackup {@code true} if the OS is permitted to back up this wallpaper
966 * image for restore to a future device; {@code false} otherwise.
967 * @param which Flags indicating which wallpaper(s) to configure with the new imagery.
968 *
Christopher Tateedf7d042016-03-29 18:24:25 -0700969 * @see #FLAG_LOCK
970 * @see #FLAG_SYSTEM
Christopher Tatead3c2592016-01-20 18:13:17 -0800971 *
972 * @return An integer ID assigned to the newly active wallpaper; or zero on failure.
973 *
974 * @throws IOException
975 */
976 public int setBitmap(Bitmap fullImage, Rect visibleCropHint,
977 boolean allowBackup, @SetWallpaperFlags int which)
Christopher Tateffa6a882015-12-11 15:08:46 -0800978 throws IOException {
979 validateRect(visibleCropHint);
Mike Lockwoodc067c9c2011-10-31 12:50:12 -0400980 if (sGlobals.mService == null) {
981 Log.w(TAG, "WallpaperService not running");
Christopher Tatead3c2592016-01-20 18:13:17 -0800982 return 0;
Mike Lockwoodc067c9c2011-10-31 12:50:12 -0400983 }
Christopher Tatead3c2592016-01-20 18:13:17 -0800984 final Bundle result = new Bundle();
Christopher Tate1e1e2e02016-01-25 15:34:36 -0800985 final WallpaperSetCompletion completion = new WallpaperSetCompletion();
Dianne Hackborn8cc6a502009-08-05 21:29:42 -0700986 try {
Benjamin Franzf3ece362015-02-11 10:51:10 +0000987 ParcelFileDescriptor fd = sGlobals.mService.setWallpaper(null,
Christopher Tate1e1e2e02016-01-25 15:34:36 -0800988 mContext.getOpPackageName(), visibleCropHint, result, which, completion);
Christopher Tatead3c2592016-01-20 18:13:17 -0800989 if (fd != null) {
990 FileOutputStream fos = null;
991 try {
992 fos = new ParcelFileDescriptor.AutoCloseOutputStream(fd);
993 fullImage.compress(Bitmap.CompressFormat.PNG, 90, fos);
Christopher Tate1e1e2e02016-01-25 15:34:36 -0800994 fos.close();
995 completion.waitForCompletion();
Christopher Tatead3c2592016-01-20 18:13:17 -0800996 } finally {
997 IoUtils.closeQuietly(fos);
998 }
Dianne Hackborn8cc6a502009-08-05 21:29:42 -0700999 }
1000 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07001001 throw e.rethrowFromSystemServer();
Dianne Hackborn8cc6a502009-08-05 21:29:42 -07001002 }
Christopher Tatead3c2592016-01-20 18:13:17 -08001003 return result.getInt(EXTRA_NEW_WALLPAPER_ID, 0);
Dianne Hackborn8cc6a502009-08-05 21:29:42 -07001004 }
1005
Christopher Tateffa6a882015-12-11 15:08:46 -08001006 private final void validateRect(Rect rect) {
1007 if (rect != null && rect.isEmpty()) {
1008 throw new IllegalArgumentException("visibleCrop rectangle must be valid and non-empty");
1009 }
1010 }
1011
Dianne Hackborn8cc6a502009-08-05 21:29:42 -07001012 /**
1013 * Change the current system wallpaper to a specific byte stream. The
1014 * give InputStream is copied into persistent storage and will now be
1015 * used as the wallpaper. Currently it must be either a JPEG or PNG
1016 * image. On success, the intent {@link Intent#ACTION_WALLPAPER_CHANGED}
1017 * is broadcast.
1018 *
Christopher Tateffa6a882015-12-11 15:08:46 -08001019 * <p>This method is equivalent to calling
1020 * {@link #setStream(InputStream, Rect, boolean)} and passing {@code null} for the
1021 * {@code visibleCrop} rectangle and {@code true} for the {@code allowBackup}
1022 * parameter.
1023 *
Nicolas Falliere9530e3a2012-06-18 17:21:06 -07001024 * <p>This method requires the caller to hold the permission
1025 * {@link android.Manifest.permission#SET_WALLPAPER}.
1026 *
Christopher Tateffa6a882015-12-11 15:08:46 -08001027 * @param bitmapData A stream containing the raw data to install as a wallpaper.
Dianne Hackborn8cc6a502009-08-05 21:29:42 -07001028 *
Christopher Tateffa6a882015-12-11 15:08:46 -08001029 * @throws IOException If an error occurs when attempting to set the wallpaper
1030 * based on the provided image data.
Dianne Hackborn8cc6a502009-08-05 21:29:42 -07001031 */
Christopher Tateffa6a882015-12-11 15:08:46 -08001032 public void setStream(InputStream bitmapData) throws IOException {
1033 setStream(bitmapData, null, true);
1034 }
1035
Christopher Tatead3c2592016-01-20 18:13:17 -08001036 private void copyStreamToWallpaperFile(InputStream data, FileOutputStream fos)
Christopher Tateffa6a882015-12-11 15:08:46 -08001037 throws IOException {
1038 byte[] buffer = new byte[32768];
1039 int amt;
1040 while ((amt=data.read(buffer)) > 0) {
1041 fos.write(buffer, 0, amt);
1042 }
1043 }
1044
1045 /**
1046 * Change the current system wallpaper to a specific byte stream, specifying a
1047 * hint about which subrectangle of the full image is to be visible. The OS will
1048 * then try to best present the given portion of the full image as the static system
1049 * wallpaper image. The data from the given InputStream is copied into persistent
1050 * storage and will then be used as the system wallpaper. Currently the data must
1051 * be either a JPEG or PNG image. On success, the intent
1052 * {@link Intent#ACTION_WALLPAPER_CHANGED} is broadcast.
1053 *
1054 * <p>This method requires the caller to hold the permission
1055 * {@link android.Manifest.permission#SET_WALLPAPER}.
1056 *
1057 * @param bitmapData A stream containing the raw data to install as a wallpaper.
1058 * @param visibleCropHint The rectangular subregion of the streamed image that should be
1059 * displayed as wallpaper. Passing {@code null} for this parameter means that
1060 * the full image should be displayed if possible given the image's and device's
1061 * aspect ratios, etc.
1062 * @param allowBackup {@code true} if the OS is permitted to back up this wallpaper
1063 * image for restore to a future device; {@code false} otherwise.
1064 *
1065 * @throws IOException If an error occurs when attempting to set the wallpaper
1066 * based on the provided image data.
1067 * @throws IllegalArgumentException If the {@code visibleCropHint} rectangle is
1068 * empty or invalid.
1069 */
Christopher Tatead3c2592016-01-20 18:13:17 -08001070 public int setStream(InputStream bitmapData, Rect visibleCropHint, boolean allowBackup)
Christopher Tateffa6a882015-12-11 15:08:46 -08001071 throws IOException {
Christopher Tateedf7d042016-03-29 18:24:25 -07001072 return setStream(bitmapData, visibleCropHint, allowBackup, FLAG_SYSTEM);
Christopher Tatead3c2592016-01-20 18:13:17 -08001073 }
1074
1075 /**
1076 * Version of {@link #setStream(InputStream, Rect, boolean)} that allows the caller
1077 * to specify which of the supported wallpaper categories to set.
1078 *
1079 * @param bitmapData A stream containing the raw data to install as a wallpaper.
1080 * @param visibleCropHint The rectangular subregion of the streamed image that should be
1081 * displayed as wallpaper. Passing {@code null} for this parameter means that
1082 * the full image should be displayed if possible given the image's and device's
1083 * aspect ratios, etc.
1084 * @param allowBackup {@code true} if the OS is permitted to back up this wallpaper
1085 * image for restore to a future device; {@code false} otherwise.
1086 * @param which Flags indicating which wallpaper(s) to configure with the new imagery.
1087 *
Christopher Tateedf7d042016-03-29 18:24:25 -07001088 * @see #FLAG_LOCK
1089 * @see #FLAG_SYSTEM
Christopher Tatead3c2592016-01-20 18:13:17 -08001090 *
1091 * @throws IOException
1092 */
1093 public int setStream(InputStream bitmapData, Rect visibleCropHint,
1094 boolean allowBackup, @SetWallpaperFlags int which)
1095 throws IOException {
Christopher Tateffa6a882015-12-11 15:08:46 -08001096 validateRect(visibleCropHint);
Mike Lockwoodc067c9c2011-10-31 12:50:12 -04001097 if (sGlobals.mService == null) {
1098 Log.w(TAG, "WallpaperService not running");
Christopher Tatead3c2592016-01-20 18:13:17 -08001099 return 0;
Mike Lockwoodc067c9c2011-10-31 12:50:12 -04001100 }
Christopher Tatead3c2592016-01-20 18:13:17 -08001101 final Bundle result = new Bundle();
Christopher Tate1e1e2e02016-01-25 15:34:36 -08001102 final WallpaperSetCompletion completion = new WallpaperSetCompletion();
Dianne Hackborn8cc6a502009-08-05 21:29:42 -07001103 try {
Benjamin Franzf3ece362015-02-11 10:51:10 +00001104 ParcelFileDescriptor fd = sGlobals.mService.setWallpaper(null,
Christopher Tate1e1e2e02016-01-25 15:34:36 -08001105 mContext.getOpPackageName(), visibleCropHint, result, which, completion);
Christopher Tatead3c2592016-01-20 18:13:17 -08001106 if (fd != null) {
1107 FileOutputStream fos = null;
1108 try {
1109 fos = new ParcelFileDescriptor.AutoCloseOutputStream(fd);
1110 copyStreamToWallpaperFile(bitmapData, fos);
Christopher Tate1e1e2e02016-01-25 15:34:36 -08001111 fos.close();
1112 completion.waitForCompletion();
Christopher Tatead3c2592016-01-20 18:13:17 -08001113 } finally {
1114 IoUtils.closeQuietly(fos);
1115 }
Dianne Hackborn8cc6a502009-08-05 21:29:42 -07001116 }
1117 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07001118 throw e.rethrowFromSystemServer();
Dianne Hackborn8cc6a502009-08-05 21:29:42 -07001119 }
Christopher Tatead3c2592016-01-20 18:13:17 -08001120
1121 return result.getInt(EXTRA_NEW_WALLPAPER_ID, 0);
Dianne Hackborn8cc6a502009-08-05 21:29:42 -07001122 }
1123
Dianne Hackborn8cc6a502009-08-05 21:29:42 -07001124 /**
Dianne Hackborn5dc5a002012-09-15 19:33:48 -07001125 * Return whether any users are currently set to use the wallpaper
1126 * with the given resource ID. That is, their wallpaper has been
1127 * set through {@link #setResource(int)} with the same resource id.
1128 */
Tor Norbye7b9c9122013-05-30 16:48:33 -07001129 public boolean hasResourceWallpaper(@RawRes int resid) {
Dianne Hackborn5dc5a002012-09-15 19:33:48 -07001130 if (sGlobals.mService == null) {
1131 Log.w(TAG, "WallpaperService not running");
1132 return false;
1133 }
1134 try {
1135 Resources resources = mContext.getResources();
1136 String name = "res:" + resources.getResourceName(resid);
1137 return sGlobals.mService.hasNamedWallpaper(name);
1138 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07001139 throw e.rethrowFromSystemServer();
Dianne Hackborn5dc5a002012-09-15 19:33:48 -07001140 }
1141 }
1142
1143 /**
Dianne Hackborn8cc6a502009-08-05 21:29:42 -07001144 * Returns the desired minimum width for the wallpaper. Callers of
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001145 * {@link #setBitmap(android.graphics.Bitmap)} or
1146 * {@link #setStream(java.io.InputStream)} should check this value
Dianne Hackborn8cc6a502009-08-05 21:29:42 -07001147 * beforehand to make sure the supplied wallpaper respects the desired
1148 * minimum width.
1149 *
1150 * If the returned value is <= 0, the caller should use the width of
1151 * the default display instead.
1152 *
1153 * @return The desired minimum width for the wallpaper. This value should
1154 * be honored by applications that set the wallpaper but it is not
1155 * mandatory.
1156 */
1157 public int getDesiredMinimumWidth() {
Mike Lockwoodc067c9c2011-10-31 12:50:12 -04001158 if (sGlobals.mService == null) {
1159 Log.w(TAG, "WallpaperService not running");
1160 return 0;
1161 }
Dianne Hackborn8cc6a502009-08-05 21:29:42 -07001162 try {
Dianne Hackborn840c3a22009-09-02 17:35:17 -07001163 return sGlobals.mService.getWidthHint();
Dianne Hackborn8cc6a502009-08-05 21:29:42 -07001164 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07001165 throw e.rethrowFromSystemServer();
Dianne Hackborn8cc6a502009-08-05 21:29:42 -07001166 }
1167 }
1168
1169 /**
1170 * Returns the desired minimum height for the wallpaper. Callers of
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001171 * {@link #setBitmap(android.graphics.Bitmap)} or
1172 * {@link #setStream(java.io.InputStream)} should check this value
Dianne Hackborn8cc6a502009-08-05 21:29:42 -07001173 * beforehand to make sure the supplied wallpaper respects the desired
1174 * minimum height.
1175 *
1176 * If the returned value is <= 0, the caller should use the height of
1177 * the default display instead.
1178 *
1179 * @return The desired minimum height for the wallpaper. This value should
1180 * be honored by applications that set the wallpaper but it is not
1181 * mandatory.
1182 */
1183 public int getDesiredMinimumHeight() {
Mike Lockwoodc067c9c2011-10-31 12:50:12 -04001184 if (sGlobals.mService == null) {
1185 Log.w(TAG, "WallpaperService not running");
1186 return 0;
1187 }
Dianne Hackborn8cc6a502009-08-05 21:29:42 -07001188 try {
Dianne Hackborn840c3a22009-09-02 17:35:17 -07001189 return sGlobals.mService.getHeightHint();
Dianne Hackborn8cc6a502009-08-05 21:29:42 -07001190 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07001191 throw e.rethrowFromSystemServer();
Dianne Hackborn8cc6a502009-08-05 21:29:42 -07001192 }
1193 }
1194
1195 /**
1196 * For use only by the current home application, to specify the size of
1197 * wallpaper it would like to use. This allows such applications to have
1198 * a virtual wallpaper that is larger than the physical screen, matching
1199 * the size of their workspace.
Dianne Hackbornc5bf7582012-04-25 19:12:07 -07001200 *
1201 * <p>Note developers, who don't seem to be reading this. This is
Christopher Tateffa6a882015-12-11 15:08:46 -08001202 * for <em>home apps</em> to tell what size wallpaper they would like.
1203 * Nobody else should be calling this! Certainly not other non-home
Dianne Hackbornc5bf7582012-04-25 19:12:07 -07001204 * apps that change the wallpaper. Those apps are supposed to
1205 * <b>retrieve</b> the suggested size so they can construct a wallpaper
1206 * that matches it.
1207 *
Nicolas Falliere9530e3a2012-06-18 17:21:06 -07001208 * <p>This method requires the caller to hold the permission
1209 * {@link android.Manifest.permission#SET_WALLPAPER_HINTS}.
1210 *
Dianne Hackborn8cc6a502009-08-05 21:29:42 -07001211 * @param minimumWidth Desired minimum width
1212 * @param minimumHeight Desired minimum height
1213 */
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001214 public void suggestDesiredDimensions(int minimumWidth, int minimumHeight) {
Dianne Hackborn8cc6a502009-08-05 21:29:42 -07001215 try {
Donghan Ryu289c2732011-11-14 18:56:11 -08001216 /**
1217 * The framework makes no attempt to limit the window size
1218 * to the maximum texture size. Any window larger than this
1219 * cannot be composited.
1220 *
1221 * Read maximum texture size from system property and scale down
1222 * minimumWidth and minimumHeight accordingly.
1223 */
1224 int maximumTextureSize;
1225 try {
1226 maximumTextureSize = SystemProperties.getInt("sys.max_texture_size", 0);
1227 } catch (Exception e) {
1228 maximumTextureSize = 0;
1229 }
1230
1231 if (maximumTextureSize > 0) {
1232 if ((minimumWidth > maximumTextureSize) ||
1233 (minimumHeight > maximumTextureSize)) {
1234 float aspect = (float)minimumHeight / (float)minimumWidth;
1235 if (minimumWidth > minimumHeight) {
1236 minimumWidth = maximumTextureSize;
1237 minimumHeight = (int)((minimumWidth * aspect) + 0.5);
1238 } else {
1239 minimumHeight = maximumTextureSize;
1240 minimumWidth = (int)((minimumHeight / aspect) + 0.5);
1241 }
1242 }
1243 }
1244
Mike Lockwoodc067c9c2011-10-31 12:50:12 -04001245 if (sGlobals.mService == null) {
1246 Log.w(TAG, "WallpaperService not running");
1247 } else {
Benjamin Franzf3ece362015-02-11 10:51:10 +00001248 sGlobals.mService.setDimensionHints(minimumWidth, minimumHeight,
1249 mContext.getOpPackageName());
Mike Lockwoodc067c9c2011-10-31 12:50:12 -04001250 }
Dianne Hackborn8cc6a502009-08-05 21:29:42 -07001251 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07001252 throw e.rethrowFromSystemServer();
Dianne Hackborn8cc6a502009-08-05 21:29:42 -07001253 }
1254 }
Adam Cohen041a0ba2011-11-09 20:10:27 -08001255
Dianne Hackborn8cc6a502009-08-05 21:29:42 -07001256 /**
Dianne Hackborn067e5f62014-09-07 23:14:30 -07001257 * Specify extra padding that the wallpaper should have outside of the display.
1258 * That is, the given padding supplies additional pixels the wallpaper should extend
1259 * outside of the display itself.
1260 * @param padding The number of pixels the wallpaper should extend beyond the display,
1261 * on its left, top, right, and bottom sides.
1262 * @hide
1263 */
1264 @SystemApi
1265 public void setDisplayPadding(Rect padding) {
1266 try {
1267 if (sGlobals.mService == null) {
1268 Log.w(TAG, "WallpaperService not running");
1269 } else {
Benjamin Franzf3ece362015-02-11 10:51:10 +00001270 sGlobals.mService.setDisplayPadding(padding, mContext.getOpPackageName());
Dianne Hackborn067e5f62014-09-07 23:14:30 -07001271 }
1272 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07001273 throw e.rethrowFromSystemServer();
Dianne Hackborn067e5f62014-09-07 23:14:30 -07001274 }
1275 }
1276
1277 /**
1278 * Apply a raw offset to the wallpaper window. Should only be used in
1279 * combination with {@link #setDisplayPadding(android.graphics.Rect)} when you
1280 * have ensured that the wallpaper will extend outside of the display area so that
1281 * it can be moved without leaving part of the display uncovered.
1282 * @param x The offset, in pixels, to apply to the left edge.
1283 * @param y The offset, in pixels, to apply to the top edge.
1284 * @hide
1285 */
1286 @SystemApi
1287 public void setDisplayOffset(IBinder windowToken, int x, int y) {
1288 try {
1289 //Log.v(TAG, "Sending new wallpaper display offsets from app...");
1290 WindowManagerGlobal.getWindowSession().setWallpaperDisplayOffset(
1291 windowToken, x, y);
1292 //Log.v(TAG, "...app returning after sending display offset!");
1293 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07001294 throw e.rethrowFromSystemServer();
Dianne Hackborn067e5f62014-09-07 23:14:30 -07001295 }
1296 }
1297
1298 /**
Filip Gruszczynski9fedc722015-01-28 15:59:46 -08001299 * Clear the wallpaper.
1300 *
1301 * @hide
1302 */
1303 @SystemApi
1304 public void clearWallpaper() {
Christopher Tateedf7d042016-03-29 18:24:25 -07001305 clearWallpaper(FLAG_SYSTEM, mContext.getUserId());
Christopher Tatebe132e62016-02-10 12:59:49 -08001306 }
1307
1308 /**
1309 * Clear the wallpaper for a specific user. The caller must hold the
1310 * INTERACT_ACROSS_USERS_FULL permission to clear another user's
1311 * wallpaper.
1312 * @hide
1313 */
1314 @SystemApi
1315 public void clearWallpaper(int which, int userId) {
Filip Gruszczynski9fedc722015-01-28 15:59:46 -08001316 if (sGlobals.mService == null) {
1317 Log.w(TAG, "WallpaperService not running");
1318 return;
1319 }
1320 try {
Christopher Tatebe132e62016-02-10 12:59:49 -08001321 sGlobals.mService.clearWallpaper(mContext.getOpPackageName(), which, userId);
Filip Gruszczynski9fedc722015-01-28 15:59:46 -08001322 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07001323 throw e.rethrowFromSystemServer();
Filip Gruszczynski9fedc722015-01-28 15:59:46 -08001324 }
1325 }
1326
1327 /**
1328 * Set the live wallpaper.
1329 *
1330 * This can only be called by packages with android.permission.SET_WALLPAPER_COMPONENT
1331 * permission.
1332 *
1333 * @hide
1334 */
1335 @SystemApi
1336 public boolean setWallpaperComponent(ComponentName name) {
1337 if (sGlobals.mService == null) {
1338 Log.w(TAG, "WallpaperService not running");
1339 return false;
1340 }
1341 try {
Benjamin Franzf3ece362015-02-11 10:51:10 +00001342 sGlobals.mService.setWallpaperComponentChecked(name, mContext.getOpPackageName());
Filip Gruszczynski9fedc722015-01-28 15:59:46 -08001343 return true;
1344 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07001345 throw e.rethrowFromSystemServer();
Filip Gruszczynski9fedc722015-01-28 15:59:46 -08001346 }
Filip Gruszczynski9fedc722015-01-28 15:59:46 -08001347 }
1348
1349 /**
Christopher Tateffa6a882015-12-11 15:08:46 -08001350 * Set the display position of the current wallpaper within any larger space, when
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07001351 * that wallpaper is visible behind the given window. The X and Y offsets
1352 * are floating point numbers ranging from 0 to 1, representing where the
1353 * wallpaper should be positioned within the screen space. These only
Christopher Tateffa6a882015-12-11 15:08:46 -08001354 * make sense when the wallpaper is larger than the display.
Oleksandr Peletskyif2519812016-01-26 20:16:06 +01001355 *
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07001356 * @param windowToken The window who these offsets should be associated
Scott Main8b2e0002009-09-29 18:17:31 -07001357 * with, as returned by {@link android.view.View#getWindowToken()
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07001358 * View.getWindowToken()}.
Marco Nelissenbf6956b2009-11-09 15:21:13 -08001359 * @param xOffset The offset along the X dimension, from 0 to 1.
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07001360 * @param yOffset The offset along the Y dimension, from 0 to 1.
1361 */
1362 public void setWallpaperOffsets(IBinder windowToken, float xOffset, float yOffset) {
Adam Cohen791a6332012-01-12 14:38:38 -08001363 try {
1364 //Log.v(TAG, "Sending new wallpaper offsets from app...");
Jeff Brownf9e989d2013-04-04 23:04:03 -07001365 WindowManagerGlobal.getWindowSession().setWallpaperPosition(
Adam Cohen791a6332012-01-12 14:38:38 -08001366 windowToken, xOffset, yOffset, mWallpaperXStep, mWallpaperYStep);
1367 //Log.v(TAG, "...app returning after sending offsets!");
1368 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07001369 throw e.rethrowFromSystemServer();
Adam Cohen791a6332012-01-12 14:38:38 -08001370 }
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07001371 }
Adam Cohen041a0ba2011-11-09 20:10:27 -08001372
Dianne Hackbornc8a0a752009-08-10 23:05:49 -07001373 /**
Marco Nelissenbf6956b2009-11-09 15:21:13 -08001374 * For applications that use multiple virtual screens showing a wallpaper,
1375 * specify the step size between virtual screens. For example, if the
Dianne Hackborn23ef7b42009-11-18 18:20:39 -08001376 * launcher has 3 virtual screens, it would specify an xStep of 0.5,
Marco Nelissenbf6956b2009-11-09 15:21:13 -08001377 * since the X offset for those screens are 0.0, 0.5 and 1.0
Oleksandr Peletskyif2519812016-01-26 20:16:06 +01001378 * @param xStep The X offset delta from one screen to the next one
Marco Nelissenbf6956b2009-11-09 15:21:13 -08001379 * @param yStep The Y offset delta from one screen to the next one
1380 */
1381 public void setWallpaperOffsetSteps(float xStep, float yStep) {
1382 mWallpaperXStep = xStep;
1383 mWallpaperYStep = yStep;
1384 }
Oleksandr Peletskyif2519812016-01-26 20:16:06 +01001385
Marco Nelissenbf6956b2009-11-09 15:21:13 -08001386 /**
Dianne Hackborn75804932009-10-20 20:15:20 -07001387 * Send an arbitrary command to the current active wallpaper.
Oleksandr Peletskyif2519812016-01-26 20:16:06 +01001388 *
Dianne Hackborn75804932009-10-20 20:15:20 -07001389 * @param windowToken The window who these offsets should be associated
1390 * with, as returned by {@link android.view.View#getWindowToken()
1391 * View.getWindowToken()}.
1392 * @param action Name of the command to perform. This must be a scoped
1393 * name to avoid collisions, such as "com.mycompany.wallpaper.DOIT".
1394 * @param x Arbitrary integer argument based on command.
1395 * @param y Arbitrary integer argument based on command.
1396 * @param z Arbitrary integer argument based on command.
1397 * @param extras Optional additional information for the command, or null.
1398 */
1399 public void sendWallpaperCommand(IBinder windowToken, String action,
1400 int x, int y, int z, Bundle extras) {
1401 try {
1402 //Log.v(TAG, "Sending new wallpaper offsets from app...");
Jeff Brownf9e989d2013-04-04 23:04:03 -07001403 WindowManagerGlobal.getWindowSession().sendWallpaperCommand(
Dianne Hackborn75804932009-10-20 20:15:20 -07001404 windowToken, action, x, y, z, extras, false);
1405 //Log.v(TAG, "...app returning after sending offsets!");
1406 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07001407 throw e.rethrowFromSystemServer();
Dianne Hackborn75804932009-10-20 20:15:20 -07001408 }
1409 }
Benjamin Franzf3ece362015-02-11 10:51:10 +00001410
1411 /**
1412 * Returns whether wallpapers are supported for the calling user. If this function returns
Oleksandr Peletskyif2519812016-01-26 20:16:06 +01001413 * {@code false}, any attempts to changing the wallpaper will have no effect,
1414 * and any attempt to obtain of the wallpaper will return {@code null}.
Benjamin Franzf3ece362015-02-11 10:51:10 +00001415 */
1416 public boolean isWallpaperSupported() {
1417 if (sGlobals.mService == null) {
1418 Log.w(TAG, "WallpaperService not running");
1419 } else {
1420 try {
1421 return sGlobals.mService.isWallpaperSupported(mContext.getOpPackageName());
1422 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07001423 throw e.rethrowFromSystemServer();
Benjamin Franzf3ece362015-02-11 10:51:10 +00001424 }
1425 }
1426 return false;
1427 }
1428
Dianne Hackborn75804932009-10-20 20:15:20 -07001429 /**
Oleksandr Peletskyif2519812016-01-26 20:16:06 +01001430 * Returns whether the calling package is allowed to set the wallpaper for the calling user.
1431 * If this function returns {@code false}, any attempts to change the wallpaper will have
1432 * no effect. Always returns {@code true} for device owner and profile owner.
1433 *
1434 * @see android.os.UserManager#DISALLOW_SET_WALLPAPER
1435 */
1436 public boolean isWallpaperSettingAllowed() {
1437 if (sGlobals.mService == null) {
1438 Log.w(TAG, "WallpaperService not running");
1439 } else {
1440 try {
1441 return sGlobals.mService.isWallpaperSettingAllowed(mContext.getOpPackageName());
1442 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07001443 throw e.rethrowFromSystemServer();
Oleksandr Peletskyif2519812016-01-26 20:16:06 +01001444 }
1445 }
1446 return false;
1447 }
1448
1449 /**
Dianne Hackborn72c82ab2009-08-11 21:13:54 -07001450 * Clear the offsets previously associated with this window through
1451 * {@link #setWallpaperOffsets(IBinder, float, float)}. This reverts
1452 * the window to its default state, where it does not cause the wallpaper
1453 * to scroll from whatever its last offsets were.
Oleksandr Peletskyif2519812016-01-26 20:16:06 +01001454 *
Dianne Hackborn72c82ab2009-08-11 21:13:54 -07001455 * @param windowToken The window who these offsets should be associated
Scott Main8b2e0002009-09-29 18:17:31 -07001456 * with, as returned by {@link android.view.View#getWindowToken()
Dianne Hackborn72c82ab2009-08-11 21:13:54 -07001457 * View.getWindowToken()}.
1458 */
1459 public void clearWallpaperOffsets(IBinder windowToken) {
1460 try {
Jeff Brownf9e989d2013-04-04 23:04:03 -07001461 WindowManagerGlobal.getWindowSession().setWallpaperPosition(
Marco Nelissenbf6956b2009-11-09 15:21:13 -08001462 windowToken, -1, -1, -1, -1);
Dianne Hackborn72c82ab2009-08-11 21:13:54 -07001463 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07001464 throw e.rethrowFromSystemServer();
Dianne Hackborn72c82ab2009-08-11 21:13:54 -07001465 }
1466 }
Oleksandr Peletskyif2519812016-01-26 20:16:06 +01001467
Dianne Hackborn72c82ab2009-08-11 21:13:54 -07001468 /**
Christopher Tatebe132e62016-02-10 12:59:49 -08001469 * Remove any currently set system wallpaper, reverting to the system's built-in
Dianne Hackborn8cc6a502009-08-05 21:29:42 -07001470 * wallpaper. On success, the intent {@link Intent#ACTION_WALLPAPER_CHANGED}
1471 * is broadcast.
1472 *
Nicolas Falliere9530e3a2012-06-18 17:21:06 -07001473 * <p>This method requires the caller to hold the permission
1474 * {@link android.Manifest.permission#SET_WALLPAPER}.
1475 *
Michael Jurkab668d0b2013-10-04 15:11:05 -07001476 * @throws IOException If an error occurs reverting to the built-in
Dianne Hackborn8cc6a502009-08-05 21:29:42 -07001477 * wallpaper.
1478 */
1479 public void clear() throws IOException {
Jeff Sharkey28f08772014-04-16 09:41:58 -07001480 setStream(openDefaultWallpaper(mContext));
1481 }
1482
1483 /**
Christopher Tate79a24572016-03-02 14:42:44 -08001484 * Remove one or more currently set wallpapers, reverting to the system default
Christopher Tateedf7d042016-03-29 18:24:25 -07001485 * display for each one. If {@link #FLAG_SYSTEM} is set in the {@code which}
Christopher Tate79a24572016-03-02 14:42:44 -08001486 * parameter, the intent {@link Intent#ACTION_WALLPAPER_CHANGED} will be broadcast
1487 * upon success.
1488 *
Christopher Tateedf7d042016-03-29 18:24:25 -07001489 * @param which A bitwise combination of {@link #FLAG_SYSTEM} or
1490 * {@link #FLAG_LOCK}
Christopher Tate79a24572016-03-02 14:42:44 -08001491 * @throws IOException If an error occurs reverting to the built-in wallpaper.
1492 */
1493 public void clear(int which) throws IOException {
Christopher Tateedf7d042016-03-29 18:24:25 -07001494 if ((which & FLAG_SYSTEM) != 0) {
Christopher Tate79a24572016-03-02 14:42:44 -08001495 clear();
1496 }
Christopher Tateedf7d042016-03-29 18:24:25 -07001497 if ((which & FLAG_LOCK) != 0) {
1498 clearWallpaper(FLAG_LOCK, mContext.getUserId());
Christopher Tate79a24572016-03-02 14:42:44 -08001499 }
1500 }
1501
1502 /**
Jeff Sharkey28f08772014-04-16 09:41:58 -07001503 * Open stream representing the default static image wallpaper.
1504 *
1505 * @hide
1506 */
1507 public static InputStream openDefaultWallpaper(Context context) {
1508 final String path = SystemProperties.get(PROP_WALLPAPER);
1509 if (!TextUtils.isEmpty(path)) {
1510 final File file = new File(path);
1511 if (file.exists()) {
1512 try {
1513 return new FileInputStream(file);
1514 } catch (IOException e) {
1515 // Ignored, fall back to platform default below
1516 }
1517 }
1518 }
1519 return context.getResources().openRawResource(
1520 com.android.internal.R.drawable.default_wallpaper);
1521 }
1522
1523 /**
1524 * Return {@link ComponentName} of the default live wallpaper, or
1525 * {@code null} if none is defined.
1526 *
1527 * @hide
1528 */
1529 public static ComponentName getDefaultWallpaperComponent(Context context) {
1530 String flat = SystemProperties.get(PROP_WALLPAPER_COMPONENT);
1531 if (!TextUtils.isEmpty(flat)) {
1532 final ComponentName cn = ComponentName.unflattenFromString(flat);
1533 if (cn != null) {
1534 return cn;
1535 }
1536 }
1537
1538 flat = context.getString(com.android.internal.R.string.default_wallpaper_component);
1539 if (!TextUtils.isEmpty(flat)) {
1540 final ComponentName cn = ComponentName.unflattenFromString(flat);
1541 if (cn != null) {
1542 return cn;
1543 }
1544 }
1545
1546 return null;
Dianne Hackborn8cc6a502009-08-05 21:29:42 -07001547 }
Christopher Tate1e1e2e02016-01-25 15:34:36 -08001548
Christopher Tatebe132e62016-02-10 12:59:49 -08001549 /**
1550 * Register a callback for lock wallpaper observation. Only the OS may use this.
1551 *
1552 * @return true on success; false on error.
1553 * @hide
1554 */
1555 public boolean setLockWallpaperCallback(IWallpaperManagerCallback callback) {
1556 if (sGlobals.mService == null) {
1557 Log.w(TAG, "WallpaperService not running");
1558 return false;
1559 }
1560
1561 try {
1562 return sGlobals.mService.setLockWallpaperCallback(callback);
1563 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07001564 throw e.rethrowFromSystemServer();
Christopher Tatebe132e62016-02-10 12:59:49 -08001565 }
Christopher Tatebe132e62016-02-10 12:59:49 -08001566 }
1567
Christopher Tate1e1e2e02016-01-25 15:34:36 -08001568 // Private completion callback for setWallpaper() synchronization
1569 private class WallpaperSetCompletion extends IWallpaperManagerCallback.Stub {
1570 final CountDownLatch mLatch;
1571
1572 public WallpaperSetCompletion() {
1573 mLatch = new CountDownLatch(1);
1574 }
1575
1576 public void waitForCompletion() {
1577 try {
1578 mLatch.await(30, TimeUnit.SECONDS);
1579 } catch (InterruptedException e) {
1580 // This might be legit: the crop may take a very long time. Don't sweat
1581 // it in that case; we are okay with display lagging behind in order to
1582 // keep the caller from locking up indeterminately.
1583 }
1584 }
1585
1586 @Override
1587 public void onWallpaperChanged() throws RemoteException {
1588 mLatch.countDown();
1589 }
1590 }
Dianne Hackborn8cc6a502009-08-05 21:29:42 -07001591}