1 /*
2  * Copyright (C) 2009 The Android Open Source Project
3  *
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 
17 package android.app;
18 
19 import android.annotation.IntDef;
20 import android.annotation.NonNull;
21 import android.annotation.Nullable;
22 import android.annotation.RawRes;
23 import android.annotation.RequiresPermission;
24 import android.annotation.SdkConstant;
25 import android.annotation.SdkConstant.SdkConstantType;
26 import android.annotation.SystemApi;
27 import android.annotation.SystemService;
28 import android.annotation.TestApi;
29 import android.compat.annotation.UnsupportedAppUsage;
30 import android.content.ComponentName;
31 import android.content.ContentResolver;
32 import android.content.Context;
33 import android.content.Intent;
34 import android.content.pm.PackageManager;
35 import android.content.pm.ResolveInfo;
36 import android.content.res.Configuration;
37 import android.content.res.Resources;
38 import android.content.res.Resources.NotFoundException;
39 import android.graphics.Bitmap;
40 import android.graphics.BitmapFactory;
41 import android.graphics.BitmapRegionDecoder;
42 import android.graphics.Canvas;
43 import android.graphics.ColorFilter;
44 import android.graphics.ColorSpace;
45 import android.graphics.ImageDecoder;
46 import android.graphics.Matrix;
47 import android.graphics.Paint;
48 import android.graphics.PixelFormat;
49 import android.graphics.PorterDuff;
50 import android.graphics.PorterDuffXfermode;
51 import android.graphics.Rect;
52 import android.graphics.RectF;
53 import android.graphics.drawable.BitmapDrawable;
54 import android.graphics.drawable.Drawable;
55 import android.net.Uri;
56 import android.os.Build;
57 import android.os.Bundle;
58 import android.os.DeadSystemException;
59 import android.os.FileUtils;
60 import android.os.Handler;
61 import android.os.IBinder;
62 import android.os.Looper;
63 import android.os.ParcelFileDescriptor;
64 import android.os.RemoteException;
65 import android.os.SystemProperties;
66 import android.text.TextUtils;
67 import android.util.Log;
68 import android.util.Pair;
69 import android.view.Display;
70 import android.view.WindowManagerGlobal;
71 
72 import com.android.internal.R;
73 
74 import libcore.io.IoUtils;
75 
76 import java.io.BufferedInputStream;
77 import java.io.ByteArrayOutputStream;
78 import java.io.File;
79 import java.io.FileInputStream;
80 import java.io.FileOutputStream;
81 import java.io.IOException;
82 import java.io.InputStream;
83 import java.lang.annotation.Retention;
84 import java.lang.annotation.RetentionPolicy;
85 import java.util.ArrayList;
86 import java.util.Arrays;
87 import java.util.HashSet;
88 import java.util.List;
89 import java.util.Set;
90 import java.util.concurrent.CountDownLatch;
91 import java.util.concurrent.TimeUnit;
92 
93 /**
94  * Provides access to the system wallpaper. With WallpaperManager, you can
95  * get the current wallpaper, get the desired dimensions for the wallpaper, set
96  * the wallpaper, and more.
97  *
98  * <p> An app can check whether wallpapers are supported for the current user, by calling
99  * {@link #isWallpaperSupported()}, and whether setting of wallpapers is allowed, by calling
100  * {@link #isSetWallpaperAllowed()}.
101  */
102 @SystemService(Context.WALLPAPER_SERVICE)
103 public class WallpaperManager {
104     private static String TAG = "WallpaperManager";
105     private static boolean DEBUG = false;
106     private float mWallpaperXStep = -1;
107     private float mWallpaperYStep = -1;
108 
109     /** {@hide} */
110     private static final String PROP_WALLPAPER = "ro.config.wallpaper";
111     /** {@hide} */
112     private static final String PROP_LOCK_WALLPAPER = "ro.config.lock_wallpaper";
113     /** {@hide} */
114     private static final String PROP_WALLPAPER_COMPONENT = "ro.config.wallpaper_component";
115 
116     /**
117      * Activity Action: Show settings for choosing wallpaper. Do not use directly to construct
118      * an intent; instead, use {@link #getCropAndSetWallpaperIntent}.
119      * <p>Input:  {@link Intent#getData} is the URI of the image to crop and set as wallpaper.
120      * <p>Output: RESULT_OK if user decided to crop/set the wallpaper, RESULT_CANCEL otherwise
121      * Activities that support this intent should specify a MIME filter of "image/*"
122      */
123     @SdkConstant(SdkConstantType.ACTIVITY_INTENT_ACTION)
124     public static final String ACTION_CROP_AND_SET_WALLPAPER =
125             "android.service.wallpaper.CROP_AND_SET_WALLPAPER";
126 
127     /**
128      * Launch an activity for the user to pick the current global live
129      * wallpaper.
130      */
131     public static final String ACTION_LIVE_WALLPAPER_CHOOSER
132             = "android.service.wallpaper.LIVE_WALLPAPER_CHOOSER";
133 
134     /**
135      * Directly launch live wallpaper preview, allowing the user to immediately
136      * confirm to switch to a specific live wallpaper.  You must specify
137      * {@link #EXTRA_LIVE_WALLPAPER_COMPONENT} with the ComponentName of
138      * a live wallpaper component that is to be shown.
139      */
140     public static final String ACTION_CHANGE_LIVE_WALLPAPER
141             = "android.service.wallpaper.CHANGE_LIVE_WALLPAPER";
142 
143     /**
144      * Extra in {@link #ACTION_CHANGE_LIVE_WALLPAPER} that specifies the
145      * ComponentName of a live wallpaper that should be shown as a preview,
146      * for the user to confirm.
147      */
148     public static final String EXTRA_LIVE_WALLPAPER_COMPONENT
149             = "android.service.wallpaper.extra.LIVE_WALLPAPER_COMPONENT";
150 
151     /**
152      * Manifest entry for activities that respond to {@link Intent#ACTION_SET_WALLPAPER}
153      * which allows them to provide a custom large icon associated with this action.
154      */
155     public static final String WALLPAPER_PREVIEW_META_DATA = "android.wallpaper.preview";
156 
157     /**
158      * Command for {@link #sendWallpaperCommand}: reported by the wallpaper
159      * host when the user taps on an empty area (not performing an action
160      * in the host).  The x and y arguments are the location of the tap in
161      * screen coordinates.
162      */
163     public static final String COMMAND_TAP = "android.wallpaper.tap";
164 
165     /**
166      * Command for {@link #sendWallpaperCommand}: reported by the wallpaper
167      * host when the user releases a secondary pointer on an empty area
168      * (not performing an action in the host).  The x and y arguments are
169      * the location of the secondary tap in screen coordinates.
170      */
171     public static final String COMMAND_SECONDARY_TAP = "android.wallpaper.secondaryTap";
172 
173     /**
174      * Command for {@link #sendWallpaperCommand}: reported by the wallpaper
175      * host when the user drops an object into an area of the host.  The x
176      * and y arguments are the location of the drop.
177      */
178     public static final String COMMAND_DROP = "android.home.drop";
179 
180     /**
181      * Command for {@link #sendWallpaperCommand}: reported when the wallpaper that was already
182      * set is re-applied by the user.
183      * @hide
184      */
185     public static final String COMMAND_REAPPLY = "android.wallpaper.reapply";
186 
187     /**
188      * Extra passed back from setWallpaper() giving the new wallpaper's assigned ID.
189      * @hide
190      */
191     public static final String EXTRA_NEW_WALLPAPER_ID = "android.service.wallpaper.extra.ID";
192 
193     // flags for which kind of wallpaper to act on
194 
195     /** @hide */
196     @IntDef(flag = true, prefix = { "FLAG_" }, value = {
197             FLAG_SYSTEM,
198             FLAG_LOCK
199     })
200     @Retention(RetentionPolicy.SOURCE)
201     public @interface SetWallpaperFlags {}
202 
203     /**
204      * Flag: set or retrieve the general system wallpaper.
205      */
206     public static final int FLAG_SYSTEM = 1 << 0;
207 
208     /**
209      * Flag: set or retrieve the lock-screen-specific wallpaper.
210      */
211     public static final int FLAG_LOCK = 1 << 1;
212 
213     private static final Object sSync = new Object[0];
214     @UnsupportedAppUsage
215     private static Globals sGlobals;
216 
217     private final Context mContext;
218     private final boolean mWcgEnabled;
219     private final ColorManagementProxy mCmProxy;
220 
221     /**
222      * Special drawable that draws a wallpaper as fast as possible.  Assumes
223      * no scaling or placement off (0,0) of the wallpaper (this should be done
224      * at the time the bitmap is loaded).
225      */
226     static class FastBitmapDrawable extends Drawable {
227         private final Bitmap mBitmap;
228         private final int mWidth;
229         private final int mHeight;
230         private int mDrawLeft;
231         private int mDrawTop;
232         private final Paint mPaint;
233 
FastBitmapDrawable(Bitmap bitmap)234         private FastBitmapDrawable(Bitmap bitmap) {
235             mBitmap = bitmap;
236             mWidth = bitmap.getWidth();
237             mHeight = bitmap.getHeight();
238 
239             setBounds(0, 0, mWidth, mHeight);
240 
241             mPaint = new Paint();
242             mPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC));
243         }
244 
245         @Override
draw(Canvas canvas)246         public void draw(Canvas canvas) {
247             canvas.drawBitmap(mBitmap, mDrawLeft, mDrawTop, mPaint);
248         }
249 
250         @Override
getOpacity()251         public int getOpacity() {
252             return PixelFormat.OPAQUE;
253         }
254 
255         @Override
setBounds(int left, int top, int right, int bottom)256         public void setBounds(int left, int top, int right, int bottom) {
257             mDrawLeft = left + (right-left - mWidth) / 2;
258             mDrawTop = top + (bottom-top - mHeight) / 2;
259         }
260 
261         @Override
setAlpha(int alpha)262         public void setAlpha(int alpha) {
263             throw new UnsupportedOperationException("Not supported with this drawable");
264         }
265 
266         @Override
setColorFilter(ColorFilter colorFilter)267         public void setColorFilter(ColorFilter colorFilter) {
268             throw new UnsupportedOperationException("Not supported with this drawable");
269         }
270 
271         @Override
setDither(boolean dither)272         public void setDither(boolean dither) {
273             throw new UnsupportedOperationException("Not supported with this drawable");
274         }
275 
276         @Override
setFilterBitmap(boolean filter)277         public void setFilterBitmap(boolean filter) {
278             throw new UnsupportedOperationException("Not supported with this drawable");
279         }
280 
281         @Override
getIntrinsicWidth()282         public int getIntrinsicWidth() {
283             return mWidth;
284         }
285 
286         @Override
getIntrinsicHeight()287         public int getIntrinsicHeight() {
288             return mHeight;
289         }
290 
291         @Override
getMinimumWidth()292         public int getMinimumWidth() {
293             return mWidth;
294         }
295 
296         @Override
getMinimumHeight()297         public int getMinimumHeight() {
298             return mHeight;
299         }
300     }
301 
302     private static class Globals extends IWallpaperManagerCallback.Stub {
303         private final IWallpaperManager mService;
304         private boolean mColorCallbackRegistered;
305         private final ArrayList<Pair<OnColorsChangedListener, Handler>> mColorListeners =
306                 new ArrayList<>();
307         private Bitmap mCachedWallpaper;
308         private int mCachedWallpaperUserId;
309         private Bitmap mDefaultWallpaper;
310         private Handler mMainLooperHandler;
311 
Globals(IWallpaperManager service, Looper looper)312         Globals(IWallpaperManager service, Looper looper) {
313             mService = service;
314             mMainLooperHandler = new Handler(looper);
315             forgetLoadedWallpaper();
316         }
317 
onWallpaperChanged()318         public void onWallpaperChanged() {
319             /* The wallpaper has changed but we shouldn't eagerly load the
320              * wallpaper as that would be inefficient. Reset the cached wallpaper
321              * to null so if the user requests the wallpaper again then we'll
322              * fetch it.
323              */
324             forgetLoadedWallpaper();
325         }
326 
327         /**
328          * Start listening to wallpaper color events.
329          * Will be called whenever someone changes their wallpaper or if a live wallpaper
330          * changes its colors.
331          * @param callback Listener
332          * @param handler Thread to call it from. Main thread if null.
333          * @param userId Owner of the wallpaper or UserHandle.USER_ALL
334          * @param displayId Caller comes from which display
335          */
addOnColorsChangedListener(@onNull OnColorsChangedListener callback, @Nullable Handler handler, int userId, int displayId)336         public void addOnColorsChangedListener(@NonNull OnColorsChangedListener callback,
337                 @Nullable Handler handler, int userId, int displayId) {
338             synchronized (this) {
339                 if (!mColorCallbackRegistered) {
340                     try {
341                         mService.registerWallpaperColorsCallback(this, userId, displayId);
342                         mColorCallbackRegistered = true;
343                     } catch (RemoteException e) {
344                         // Failed, service is gone
345                         Log.w(TAG, "Can't register for color updates", e);
346                     }
347                 }
348                 mColorListeners.add(new Pair<>(callback, handler));
349             }
350         }
351 
352         /**
353          * Stop listening to wallpaper color events.
354          *
355          * @param callback listener
356          * @param userId Owner of the wallpaper or UserHandle.USER_ALL
357          * @param displayId Which display is interested
358          */
removeOnColorsChangedListener(@onNull OnColorsChangedListener callback, int userId, int displayId)359         public void removeOnColorsChangedListener(@NonNull OnColorsChangedListener callback,
360                 int userId, int displayId) {
361             synchronized (this) {
362                 mColorListeners.removeIf(pair -> pair.first == callback);
363 
364                 if (mColorListeners.size() == 0 && mColorCallbackRegistered) {
365                     mColorCallbackRegistered = false;
366                     try {
367                         mService.unregisterWallpaperColorsCallback(this, userId, displayId);
368                     } catch (RemoteException e) {
369                         // Failed, service is gone
370                         Log.w(TAG, "Can't unregister color updates", e);
371                     }
372                 }
373             }
374         }
375 
376         @Override
onWallpaperColorsChanged(WallpaperColors colors, int which, int userId)377         public void onWallpaperColorsChanged(WallpaperColors colors, int which, int userId) {
378             synchronized (this) {
379                 for (Pair<OnColorsChangedListener, Handler> listener : mColorListeners) {
380                     Handler handler = listener.second;
381                     if (listener.second == null) {
382                         handler = mMainLooperHandler;
383                     }
384                     handler.post(() -> {
385                         // Dealing with race conditions between posting a callback and
386                         // removeOnColorsChangedListener being called.
387                         boolean stillExists;
388                         synchronized (sGlobals) {
389                             stillExists = mColorListeners.contains(listener);
390                         }
391                         if (stillExists) {
392                             listener.first.onColorsChanged(colors, which, userId);
393                         }
394                     });
395                 }
396             }
397         }
398 
getWallpaperColors(int which, int userId, int displayId)399         WallpaperColors getWallpaperColors(int which, int userId, int displayId) {
400             if (which != FLAG_LOCK && which != FLAG_SYSTEM) {
401                 throw new IllegalArgumentException(
402                         "Must request colors for exactly one kind of wallpaper");
403             }
404 
405             try {
406                 return mService.getWallpaperColors(which, userId, displayId);
407             } catch (RemoteException e) {
408                 // Can't get colors, connection lost.
409             }
410             return null;
411         }
412 
peekWallpaperBitmap(Context context, boolean returnDefault, @SetWallpaperFlags int which, ColorManagementProxy cmProxy)413         public Bitmap peekWallpaperBitmap(Context context, boolean returnDefault,
414                 @SetWallpaperFlags int which, ColorManagementProxy cmProxy) {
415             return peekWallpaperBitmap(context, returnDefault, which, context.getUserId(),
416                     false /* hardware */, cmProxy);
417         }
418 
peekWallpaperBitmap(Context context, boolean returnDefault, @SetWallpaperFlags int which, int userId, boolean hardware, ColorManagementProxy cmProxy)419         public Bitmap peekWallpaperBitmap(Context context, boolean returnDefault,
420                 @SetWallpaperFlags int which, int userId, boolean hardware,
421                 ColorManagementProxy cmProxy) {
422             if (mService != null) {
423                 try {
424                     if (!mService.isWallpaperSupported(context.getOpPackageName())) {
425                         return null;
426                     }
427                 } catch (RemoteException e) {
428                     throw e.rethrowFromSystemServer();
429                 }
430             }
431             synchronized (this) {
432                 if (mCachedWallpaper != null && mCachedWallpaperUserId == userId
433                         && !mCachedWallpaper.isRecycled()) {
434                     return mCachedWallpaper;
435                 }
436                 mCachedWallpaper = null;
437                 mCachedWallpaperUserId = 0;
438                 try {
439                     mCachedWallpaper = getCurrentWallpaperLocked(
440                             context, userId, hardware, cmProxy);
441                     mCachedWallpaperUserId = userId;
442                 } catch (OutOfMemoryError e) {
443                     Log.w(TAG, "Out of memory loading the current wallpaper: " + e);
444                 } catch (SecurityException e) {
445                     if (context.getApplicationInfo().targetSdkVersion < Build.VERSION_CODES.O_MR1) {
446                         Log.w(TAG, "No permission to access wallpaper, suppressing"
447                                 + " exception to avoid crashing legacy app.");
448                     } else {
449                         // Post-O apps really most sincerely need the permission.
450                         throw e;
451                     }
452                 }
453                 if (mCachedWallpaper != null) {
454                     return mCachedWallpaper;
455                 }
456             }
457             if (returnDefault) {
458                 Bitmap defaultWallpaper = mDefaultWallpaper;
459                 if (defaultWallpaper == null) {
460                     defaultWallpaper = getDefaultWallpaper(context, which);
461                     synchronized (this) {
462                         mDefaultWallpaper = defaultWallpaper;
463                     }
464                 }
465                 return defaultWallpaper;
466             }
467             return null;
468         }
469 
forgetLoadedWallpaper()470         void forgetLoadedWallpaper() {
471             synchronized (this) {
472                 mCachedWallpaper = null;
473                 mCachedWallpaperUserId = 0;
474                 mDefaultWallpaper = null;
475             }
476         }
477 
getCurrentWallpaperLocked(Context context, int userId, boolean hardware, ColorManagementProxy cmProxy)478         private Bitmap getCurrentWallpaperLocked(Context context, int userId, boolean hardware,
479                 ColorManagementProxy cmProxy) {
480             if (mService == null) {
481                 Log.w(TAG, "WallpaperService not running");
482                 return null;
483             }
484 
485             try {
486                 Bundle params = new Bundle();
487                 ParcelFileDescriptor pfd = mService.getWallpaperWithFeature(
488                         context.getOpPackageName(), context.getAttributionTag(), this, FLAG_SYSTEM,
489                         params, userId);
490 
491                 if (pfd != null) {
492                     try (BufferedInputStream bis = new BufferedInputStream(
493                             new ParcelFileDescriptor.AutoCloseInputStream(pfd))) {
494                         final ByteArrayOutputStream baos = new ByteArrayOutputStream();
495                         int data;
496                         while ((data = bis.read()) != -1) {
497                             baos.write(data);
498                         }
499                         ImageDecoder.Source src = ImageDecoder.createSource(baos.toByteArray());
500                         return ImageDecoder.decodeBitmap(src, ((decoder, info, source) -> {
501                             // Mutable and hardware config can't be set at the same time.
502                             decoder.setMutableRequired(!hardware);
503                             // Let's do color management
504                             if (cmProxy != null) {
505                                 cmProxy.doColorManagement(decoder, info);
506                             }
507                         }));
508                     } catch (OutOfMemoryError | IOException e) {
509                         Log.w(TAG, "Can't decode file", e);
510                     }
511                 }
512             } catch (RemoteException e) {
513                 throw e.rethrowFromSystemServer();
514             }
515             return null;
516         }
517 
getDefaultWallpaper(Context context, @SetWallpaperFlags int which)518         private Bitmap getDefaultWallpaper(Context context, @SetWallpaperFlags int which) {
519             InputStream is = openDefaultWallpaper(context, which);
520             if (is != null) {
521                 try {
522                     BitmapFactory.Options options = new BitmapFactory.Options();
523                     return BitmapFactory.decodeStream(is, null, options);
524                 } catch (OutOfMemoryError e) {
525                     Log.w(TAG, "Can't decode stream", e);
526                 } finally {
527                     IoUtils.closeQuietly(is);
528                 }
529             }
530             return null;
531         }
532     }
533 
initGlobals(IWallpaperManager service, Looper looper)534     static void initGlobals(IWallpaperManager service, Looper looper) {
535         synchronized (sSync) {
536             if (sGlobals == null) {
537                 sGlobals = new Globals(service, looper);
538             }
539         }
540     }
541 
WallpaperManager(IWallpaperManager service, Context context, Handler handler)542     /*package*/ WallpaperManager(IWallpaperManager service, Context context, Handler handler) {
543         mContext = context;
544         if (service != null) {
545             initGlobals(service, context.getMainLooper());
546         }
547         // Check if supports mixed color spaces composition in hardware.
548         mWcgEnabled = context.getResources().getConfiguration().isScreenWideColorGamut()
549                 && context.getResources().getBoolean(R.bool.config_enableWcgMode);
550         mCmProxy = new ColorManagementProxy(context);
551     }
552 
553     // no-op constructor called just by DisabledWallpaperManager
WallpaperManager()554     /*package*/ WallpaperManager() {
555         mContext = null;
556         mCmProxy = null;
557         mWcgEnabled = false;
558     }
559 
560     /**
561      * Retrieve a WallpaperManager associated with the given Context.
562      */
getInstance(Context context)563     public static WallpaperManager getInstance(Context context) {
564         return (WallpaperManager)context.getSystemService(
565                 Context.WALLPAPER_SERVICE);
566     }
567 
568     /** @hide */
569     @UnsupportedAppUsage
getIWallpaperManager()570     public IWallpaperManager getIWallpaperManager() {
571         return sGlobals.mService;
572     }
573 
574     /**
575      * Indicate whether wcg (Wide Color Gamut) should be enabled.
576      * <p>
577      * Some devices lack of capability of mixed color spaces composition,
578      * enable wcg on such devices might cause memory or battery concern.
579      * <p>
580      * Therefore, in addition to {@link Configuration#isScreenWideColorGamut()},
581      * we also take mixed color spaces composition (config_enableWcgMode) into account.
582      *
583      * @see Configuration#isScreenWideColorGamut()
584      * @return True if wcg should be enabled for this device.
585      * @hide
586      */
587     @TestApi
shouldEnableWideColorGamut()588     public boolean shouldEnableWideColorGamut() {
589         return mWcgEnabled;
590     }
591 
592     /**
593      * Retrieve the current system wallpaper; if
594      * no wallpaper is set, the system built-in static wallpaper is returned.
595      * This is returned as an
596      * abstract Drawable that you can install in a View to display whatever
597      * wallpaper the user has currently set.
598      * <p>
599      * This method can return null if there is no system wallpaper available, if
600      * wallpapers are not supported in the current user, or if the calling app is not
601      * permitted to access the system wallpaper.
602      *
603      * @return Returns a Drawable object that will draw the system wallpaper,
604      *     or {@code null} if no system wallpaper exists or if the calling application
605      *     is not able to access the wallpaper.
606      */
getDrawable()607     public Drawable getDrawable() {
608         final ColorManagementProxy cmProxy = getColorManagementProxy();
609         Bitmap bm = sGlobals.peekWallpaperBitmap(mContext, true, FLAG_SYSTEM, cmProxy);
610         if (bm != null) {
611             Drawable dr = new BitmapDrawable(mContext.getResources(), bm);
612             dr.setDither(false);
613             return dr;
614         }
615         return null;
616     }
617 
618     /**
619      * Obtain a drawable for the built-in static system wallpaper.
620      */
getBuiltInDrawable()621     public Drawable getBuiltInDrawable() {
622         return getBuiltInDrawable(0, 0, false, 0, 0, FLAG_SYSTEM);
623     }
624 
625     /**
626      * Obtain a drawable for the specified built-in static system wallpaper.
627      *
628      * @param which The {@code FLAG_*} identifier of a valid wallpaper type.  Throws
629      *     IllegalArgumentException if an invalid wallpaper is requested.
630      * @return A Drawable presenting the specified wallpaper image, or {@code null}
631      *     if no built-in default image for that wallpaper type exists.
632      */
getBuiltInDrawable(@etWallpaperFlags int which)633     public Drawable getBuiltInDrawable(@SetWallpaperFlags int which) {
634         return getBuiltInDrawable(0, 0, false, 0, 0, which);
635     }
636 
637     /**
638      * Returns a drawable for the system built-in static wallpaper. Based on the parameters, the
639      * drawable can be cropped and scaled
640      *
641      * @param outWidth The width of the returned drawable
642      * @param outWidth The height of the returned drawable
643      * @param scaleToFit If true, scale the wallpaper down rather than just cropping it
644      * @param horizontalAlignment A float value between 0 and 1 specifying where to crop the image;
645      *        0 for left-aligned, 0.5 for horizontal center-aligned, and 1 for right-aligned
646      * @param verticalAlignment A float value between 0 and 1 specifying where to crop the image;
647      *        0 for top-aligned, 0.5 for vertical center-aligned, and 1 for bottom-aligned
648      * @return A Drawable presenting the built-in default system wallpaper image,
649      *        or {@code null} if no such default image is defined on this device.
650      */
getBuiltInDrawable(int outWidth, int outHeight, boolean scaleToFit, float horizontalAlignment, float verticalAlignment)651     public Drawable getBuiltInDrawable(int outWidth, int outHeight,
652             boolean scaleToFit, float horizontalAlignment, float verticalAlignment) {
653         return getBuiltInDrawable(outWidth, outHeight, scaleToFit,
654                 horizontalAlignment, verticalAlignment, FLAG_SYSTEM);
655     }
656 
657     /**
658      * Returns a drawable for the built-in static wallpaper of the specified type.  Based on the
659      * parameters, the drawable can be cropped and scaled.
660      *
661      * @param outWidth The width of the returned drawable
662      * @param outWidth The height of the returned drawable
663      * @param scaleToFit If true, scale the wallpaper down rather than just cropping it
664      * @param horizontalAlignment A float value between 0 and 1 specifying where to crop the image;
665      *        0 for left-aligned, 0.5 for horizontal center-aligned, and 1 for right-aligned
666      * @param verticalAlignment A float value between 0 and 1 specifying where to crop the image;
667      *        0 for top-aligned, 0.5 for vertical center-aligned, and 1 for bottom-aligned
668      * @param which The {@code FLAG_*} identifier of a valid wallpaper type.  Throws
669      *     IllegalArgumentException if an invalid wallpaper is requested.
670      * @return A Drawable presenting the built-in default wallpaper image of the given type,
671      *        or {@code null} if no default image of that type is defined on this device.
672      */
getBuiltInDrawable(int outWidth, int outHeight, boolean scaleToFit, float horizontalAlignment, float verticalAlignment, @SetWallpaperFlags int which)673     public Drawable getBuiltInDrawable(int outWidth, int outHeight, boolean scaleToFit,
674             float horizontalAlignment, float verticalAlignment, @SetWallpaperFlags int which) {
675         if (sGlobals.mService == null) {
676             Log.w(TAG, "WallpaperService not running");
677             throw new RuntimeException(new DeadSystemException());
678         }
679 
680         if (which != FLAG_SYSTEM && which != FLAG_LOCK) {
681             throw new IllegalArgumentException("Must request exactly one kind of wallpaper");
682         }
683 
684         Resources resources = mContext.getResources();
685         horizontalAlignment = Math.max(0, Math.min(1, horizontalAlignment));
686         verticalAlignment = Math.max(0, Math.min(1, verticalAlignment));
687 
688         InputStream wpStream = openDefaultWallpaper(mContext, which);
689         if (wpStream == null) {
690             if (DEBUG) {
691                 Log.w(TAG, "default wallpaper stream " + which + " is null");
692             }
693             return null;
694         } else {
695             InputStream is = new BufferedInputStream(wpStream);
696             if (outWidth <= 0 || outHeight <= 0) {
697                 Bitmap fullSize = BitmapFactory.decodeStream(is, null, null);
698                 return new BitmapDrawable(resources, fullSize);
699             } else {
700                 int inWidth;
701                 int inHeight;
702                 // Just measure this time through...
703                 {
704                     BitmapFactory.Options options = new BitmapFactory.Options();
705                     options.inJustDecodeBounds = true;
706                     BitmapFactory.decodeStream(is, null, options);
707                     if (options.outWidth != 0 && options.outHeight != 0) {
708                         inWidth = options.outWidth;
709                         inHeight = options.outHeight;
710                     } else {
711                         Log.e(TAG, "default wallpaper dimensions are 0");
712                         return null;
713                     }
714                 }
715 
716                 // Reopen the stream to do the full decode.  We know at this point
717                 // that openDefaultWallpaper() will return non-null.
718                 is = new BufferedInputStream(openDefaultWallpaper(mContext, which));
719 
720                 RectF cropRectF;
721 
722                 outWidth = Math.min(inWidth, outWidth);
723                 outHeight = Math.min(inHeight, outHeight);
724                 if (scaleToFit) {
725                     cropRectF = getMaxCropRect(inWidth, inHeight, outWidth, outHeight,
726                         horizontalAlignment, verticalAlignment);
727                 } else {
728                     float left = (inWidth - outWidth) * horizontalAlignment;
729                     float right = left + outWidth;
730                     float top = (inHeight - outHeight) * verticalAlignment;
731                     float bottom = top + outHeight;
732                     cropRectF = new RectF(left, top, right, bottom);
733                 }
734                 Rect roundedTrueCrop = new Rect();
735                 cropRectF.roundOut(roundedTrueCrop);
736 
737                 if (roundedTrueCrop.width() <= 0 || roundedTrueCrop.height() <= 0) {
738                     Log.w(TAG, "crop has bad values for full size image");
739                     return null;
740                 }
741 
742                 // See how much we're reducing the size of the image
743                 int scaleDownSampleSize = Math.min(roundedTrueCrop.width() / outWidth,
744                         roundedTrueCrop.height() / outHeight);
745 
746                 // Attempt to open a region decoder
747                 BitmapRegionDecoder decoder = null;
748                 try {
749                     decoder = BitmapRegionDecoder.newInstance(is, true);
750                 } catch (IOException e) {
751                     Log.w(TAG, "cannot open region decoder for default wallpaper");
752                 }
753 
754                 Bitmap crop = null;
755                 if (decoder != null) {
756                     // Do region decoding to get crop bitmap
757                     BitmapFactory.Options options = new BitmapFactory.Options();
758                     if (scaleDownSampleSize > 1) {
759                         options.inSampleSize = scaleDownSampleSize;
760                     }
761                     crop = decoder.decodeRegion(roundedTrueCrop, options);
762                     decoder.recycle();
763                 }
764 
765                 if (crop == null) {
766                     // BitmapRegionDecoder has failed, try to crop in-memory. We know at
767                     // this point that openDefaultWallpaper() will return non-null.
768                     is = new BufferedInputStream(openDefaultWallpaper(mContext, which));
769                     Bitmap fullSize = null;
770                     BitmapFactory.Options options = new BitmapFactory.Options();
771                     if (scaleDownSampleSize > 1) {
772                         options.inSampleSize = scaleDownSampleSize;
773                     }
774                     fullSize = BitmapFactory.decodeStream(is, null, options);
775                     if (fullSize != null) {
776                         crop = Bitmap.createBitmap(fullSize, roundedTrueCrop.left,
777                                 roundedTrueCrop.top, roundedTrueCrop.width(),
778                                 roundedTrueCrop.height());
779                     }
780                 }
781 
782                 if (crop == null) {
783                     Log.w(TAG, "cannot decode default wallpaper");
784                     return null;
785                 }
786 
787                 // Scale down if necessary
788                 if (outWidth > 0 && outHeight > 0 &&
789                         (crop.getWidth() != outWidth || crop.getHeight() != outHeight)) {
790                     Matrix m = new Matrix();
791                     RectF cropRect = new RectF(0, 0, crop.getWidth(), crop.getHeight());
792                     RectF returnRect = new RectF(0, 0, outWidth, outHeight);
793                     m.setRectToRect(cropRect, returnRect, Matrix.ScaleToFit.FILL);
794                     Bitmap tmp = Bitmap.createBitmap((int) returnRect.width(),
795                             (int) returnRect.height(), Bitmap.Config.ARGB_8888);
796                     if (tmp != null) {
797                         Canvas c = new Canvas(tmp);
798                         Paint p = new Paint();
799                         p.setFilterBitmap(true);
800                         c.drawBitmap(crop, m, p);
801                         crop = tmp;
802                     }
803                 }
804 
805                 return new BitmapDrawable(resources, crop);
806             }
807         }
808     }
809 
getMaxCropRect(int inWidth, int inHeight, int outWidth, int outHeight, float horizontalAlignment, float verticalAlignment)810     private static RectF getMaxCropRect(int inWidth, int inHeight, int outWidth, int outHeight,
811                 float horizontalAlignment, float verticalAlignment) {
812         RectF cropRect = new RectF();
813         // Get a crop rect that will fit this
814         if (inWidth / (float) inHeight > outWidth / (float) outHeight) {
815              cropRect.top = 0;
816              cropRect.bottom = inHeight;
817              float cropWidth = outWidth * (inHeight / (float) outHeight);
818              cropRect.left = (inWidth - cropWidth) * horizontalAlignment;
819              cropRect.right = cropRect.left + cropWidth;
820         } else {
821             cropRect.left = 0;
822             cropRect.right = inWidth;
823             float cropHeight = outHeight * (inWidth / (float) outWidth);
824             cropRect.top = (inHeight - cropHeight) * verticalAlignment;
825             cropRect.bottom = cropRect.top + cropHeight;
826         }
827         return cropRect;
828     }
829 
830     /**
831      * Retrieve the current system wallpaper; if there is no wallpaper set,
832      * a null pointer is returned. This is returned as an
833      * abstract Drawable that you can install in a View to display whatever
834      * wallpaper the user has currently set.
835      *
836      * @return Returns a Drawable object that will draw the wallpaper or a
837      * null pointer if these is none.
838      */
peekDrawable()839     public Drawable peekDrawable() {
840         final ColorManagementProxy cmProxy = getColorManagementProxy();
841         Bitmap bm = sGlobals.peekWallpaperBitmap(mContext, false, FLAG_SYSTEM, cmProxy);
842         if (bm != null) {
843             Drawable dr = new BitmapDrawable(mContext.getResources(), bm);
844             dr.setDither(false);
845             return dr;
846         }
847         return null;
848     }
849 
850     /**
851      * Like {@link #getDrawable()}, but the returned Drawable has a number
852      * of limitations to reduce its overhead as much as possible. It will
853      * never scale the wallpaper (only centering it if the requested bounds
854      * do match the bitmap bounds, which should not be typical), doesn't
855      * allow setting an alpha, color filter, or other attributes, etc.  The
856      * bounds of the returned drawable will be initialized to the same bounds
857      * as the wallpaper, so normally you will not need to touch it.  The
858      * drawable also assumes that it will be used in a context running in
859      * the same density as the screen (not in density compatibility mode).
860      *
861      * @return Returns a Drawable object that will draw the wallpaper.
862      */
863     @RequiresPermission(android.Manifest.permission.READ_EXTERNAL_STORAGE)
getFastDrawable()864     public Drawable getFastDrawable() {
865         final ColorManagementProxy cmProxy = getColorManagementProxy();
866         Bitmap bm = sGlobals.peekWallpaperBitmap(mContext, true, FLAG_SYSTEM, cmProxy);
867         if (bm != null) {
868             return new FastBitmapDrawable(bm);
869         }
870         return null;
871     }
872 
873     /**
874      * Like {@link #getFastDrawable()}, but if there is no wallpaper set,
875      * a null pointer is returned.
876      *
877      * @return Returns an optimized Drawable object that will draw the
878      * wallpaper or a null pointer if these is none.
879      */
880     @RequiresPermission(android.Manifest.permission.READ_EXTERNAL_STORAGE)
peekFastDrawable()881     public Drawable peekFastDrawable() {
882         final ColorManagementProxy cmProxy = getColorManagementProxy();
883         Bitmap bm = sGlobals.peekWallpaperBitmap(mContext, false, FLAG_SYSTEM, cmProxy);
884         if (bm != null) {
885             return new FastBitmapDrawable(bm);
886         }
887         return null;
888     }
889 
890     /**
891      * Whether the wallpaper supports Wide Color Gamut or not.
892      * @param which The wallpaper whose image file is to be retrieved. Must be a single
893      *     defined kind of wallpaper, either {@link #FLAG_SYSTEM} or {@link #FLAG_LOCK}.
894      * @return true when supported.
895      *
896      * @see #FLAG_LOCK
897      * @see #FLAG_SYSTEM
898      * @hide
899      */
900     @TestApi
901     @RequiresPermission(android.Manifest.permission.READ_EXTERNAL_STORAGE)
wallpaperSupportsWcg(int which)902     public boolean wallpaperSupportsWcg(int which) {
903         if (!shouldEnableWideColorGamut()) {
904             return false;
905         }
906         final ColorManagementProxy cmProxy = getColorManagementProxy();
907         Bitmap bitmap = sGlobals.peekWallpaperBitmap(mContext, false, which, cmProxy);
908         return bitmap != null && bitmap.getColorSpace() != null
909                 && bitmap.getColorSpace() != ColorSpace.get(ColorSpace.Named.SRGB)
910                 && cmProxy.isSupportedColorSpace(bitmap.getColorSpace());
911     }
912 
913     /**
914      * Like {@link #getDrawable()} but returns a Bitmap with default {@link Bitmap.Config}.
915      *
916      * @hide
917      */
918     @TestApi
919     @Nullable
920     @UnsupportedAppUsage
getBitmap()921     public Bitmap getBitmap() {
922         return getBitmap(false);
923     }
924 
925     /**
926      * Like {@link #getDrawable()} but returns a Bitmap.
927      *
928      * @param hardware Asks for a hardware backed bitmap.
929      * @see Bitmap.Config#HARDWARE
930      * @hide
931      */
932     @UnsupportedAppUsage
getBitmap(boolean hardware)933     public Bitmap getBitmap(boolean hardware) {
934         return getBitmapAsUser(mContext.getUserId(), hardware);
935     }
936 
937     /**
938      * Like {@link #getDrawable()} but returns a Bitmap for the provided user.
939      *
940      * @hide
941      */
getBitmapAsUser(int userId, boolean hardware)942     public Bitmap getBitmapAsUser(int userId, boolean hardware) {
943         final ColorManagementProxy cmProxy = getColorManagementProxy();
944         return sGlobals.peekWallpaperBitmap(mContext, true, FLAG_SYSTEM, userId, hardware, cmProxy);
945     }
946 
947     /**
948      * Get an open, readable file descriptor to the given wallpaper image file.
949      * The caller is responsible for closing the file descriptor when done ingesting the file.
950      *
951      * <p>If no lock-specific wallpaper has been configured for the given user, then
952      * this method will return {@code null} when requesting {@link #FLAG_LOCK} rather than
953      * returning the system wallpaper's image file.
954      *
955      * @param which The wallpaper whose image file is to be retrieved.  Must be a single
956      *     defined kind of wallpaper, either {@link #FLAG_SYSTEM} or
957      *     {@link #FLAG_LOCK}.
958      * @return An open, readable file desriptor to the requested wallpaper image file;
959      *     or {@code null} if no such wallpaper is configured or if the calling app does
960      *     not have permission to read the current wallpaper.
961      *
962      * @see #FLAG_LOCK
963      * @see #FLAG_SYSTEM
964      */
965     @RequiresPermission(android.Manifest.permission.READ_EXTERNAL_STORAGE)
getWallpaperFile(@etWallpaperFlags int which)966     public ParcelFileDescriptor getWallpaperFile(@SetWallpaperFlags int which) {
967         return getWallpaperFile(which, mContext.getUserId());
968     }
969 
970     /**
971      * Registers a listener to get notified when the wallpaper colors change.
972      * @param listener A listener to register
973      * @param handler Where to call it from. Will be called from the main thread
974      *                if null.
975      */
addOnColorsChangedListener(@onNull OnColorsChangedListener listener, @NonNull Handler handler)976     public void addOnColorsChangedListener(@NonNull OnColorsChangedListener listener,
977             @NonNull Handler handler) {
978         addOnColorsChangedListener(listener, handler, mContext.getUserId());
979     }
980 
981     /**
982      * Registers a listener to get notified when the wallpaper colors change
983      * @param listener A listener to register
984      * @param handler Where to call it from. Will be called from the main thread
985      *                if null.
986      * @param userId Owner of the wallpaper or UserHandle.USER_ALL.
987      * @hide
988      */
989     @UnsupportedAppUsage
addOnColorsChangedListener(@onNull OnColorsChangedListener listener, @NonNull Handler handler, int userId)990     public void addOnColorsChangedListener(@NonNull OnColorsChangedListener listener,
991             @NonNull Handler handler, int userId) {
992         sGlobals.addOnColorsChangedListener(listener, handler, userId, mContext.getDisplayId());
993     }
994 
995     /**
996      * Stop listening to color updates.
997      * @param callback A callback to unsubscribe.
998      */
removeOnColorsChangedListener(@onNull OnColorsChangedListener callback)999     public void removeOnColorsChangedListener(@NonNull OnColorsChangedListener callback) {
1000         removeOnColorsChangedListener(callback, mContext.getUserId());
1001     }
1002 
1003     /**
1004      * Stop listening to color updates.
1005      * @param callback A callback to unsubscribe.
1006      * @param userId Owner of the wallpaper or UserHandle.USER_ALL.
1007      * @hide
1008      */
removeOnColorsChangedListener(@onNull OnColorsChangedListener callback, int userId)1009     public void removeOnColorsChangedListener(@NonNull OnColorsChangedListener callback,
1010             int userId) {
1011         sGlobals.removeOnColorsChangedListener(callback, userId, mContext.getDisplayId());
1012     }
1013 
1014     /**
1015      * Get the primary colors of a wallpaper.
1016      *
1017      * <p>This method can return {@code null} when:
1018      * <ul>
1019      * <li>Colors are still being processed by the system.</li>
1020      * <li>The user has chosen to use a live wallpaper:  live wallpapers might not
1021      * implement
1022      * {@link android.service.wallpaper.WallpaperService.Engine#onComputeColors()
1023      *     WallpaperService.Engine#onComputeColors()}.</li>
1024      * </ul>
1025      * <p>Please note that this API will go through IPC and may take some time to
1026      * calculate the wallpaper color, which could block the caller thread, so it is
1027      * not recommended to call this in the UI thread.</p>
1028      *
1029      * @param which Wallpaper type. Must be either {@link #FLAG_SYSTEM} or
1030      *     {@link #FLAG_LOCK}.
1031      * @return Current {@link WallpaperColors} or null if colors are unknown.
1032      * @see #addOnColorsChangedListener(OnColorsChangedListener, Handler)
1033      */
getWallpaperColors(int which)1034     public @Nullable WallpaperColors getWallpaperColors(int which) {
1035         return getWallpaperColors(which, mContext.getUserId());
1036     }
1037 
1038     /**
1039      * Get the primary colors of the wallpaper configured in the given user.
1040      * @param which wallpaper type. Must be either {@link #FLAG_SYSTEM} or
1041      *     {@link #FLAG_LOCK}
1042      * @param userId Owner of the wallpaper.
1043      * @return {@link WallpaperColors} or null if colors are unknown.
1044      * @hide
1045      */
1046     @UnsupportedAppUsage
getWallpaperColors(int which, int userId)1047     public @Nullable WallpaperColors getWallpaperColors(int which, int userId) {
1048         return sGlobals.getWallpaperColors(which, userId, mContext.getDisplayId());
1049     }
1050 
1051     /**
1052      * Version of {@link #getWallpaperFile(int)} that can access the wallpaper data
1053      * for a given user.  The caller must hold the INTERACT_ACROSS_USERS_FULL
1054      * permission to access another user's wallpaper data.
1055      *
1056      * @param which The wallpaper whose image file is to be retrieved.  Must be a single
1057      *     defined kind of wallpaper, either {@link #FLAG_SYSTEM} or
1058      *     {@link #FLAG_LOCK}.
1059      * @param userId The user or profile whose imagery is to be retrieved
1060      *
1061      * @see #FLAG_LOCK
1062      * @see #FLAG_SYSTEM
1063      *
1064      * @hide
1065      */
1066     @UnsupportedAppUsage
getWallpaperFile(@etWallpaperFlags int which, int userId)1067     public ParcelFileDescriptor getWallpaperFile(@SetWallpaperFlags int which, int userId) {
1068         if (which != FLAG_SYSTEM && which != FLAG_LOCK) {
1069             throw new IllegalArgumentException("Must request exactly one kind of wallpaper");
1070         }
1071 
1072         if (sGlobals.mService == null) {
1073             Log.w(TAG, "WallpaperService not running");
1074             throw new RuntimeException(new DeadSystemException());
1075         } else {
1076             try {
1077                 Bundle outParams = new Bundle();
1078                 return sGlobals.mService.getWallpaperWithFeature(mContext.getOpPackageName(),
1079                         mContext.getAttributionTag(), null, which, outParams, userId);
1080             } catch (RemoteException e) {
1081                 throw e.rethrowFromSystemServer();
1082             } catch (SecurityException e) {
1083                 if (mContext.getApplicationInfo().targetSdkVersion < Build.VERSION_CODES.O_MR1) {
1084                     Log.w(TAG, "No permission to access wallpaper, suppressing"
1085                             + " exception to avoid crashing legacy app.");
1086                     return null;
1087                 } else {
1088                     throw e;
1089                 }
1090             }
1091         }
1092     }
1093 
1094     /**
1095      * Remove all internal references to the last loaded wallpaper.  Useful
1096      * for apps that want to reduce memory usage when they only temporarily
1097      * need to have the wallpaper.  After calling, the next request for the
1098      * wallpaper will require reloading it again from disk.
1099      */
forgetLoadedWallpaper()1100     public void forgetLoadedWallpaper() {
1101         sGlobals.forgetLoadedWallpaper();
1102     }
1103 
1104     /**
1105      * Returns the information about the wallpaper if the current wallpaper is
1106      * a live wallpaper component. Otherwise, if the wallpaper is a static image,
1107      * this returns null.
1108      */
getWallpaperInfo()1109     public WallpaperInfo getWallpaperInfo() {
1110         return getWallpaperInfo(mContext.getUserId());
1111     }
1112 
1113     /**
1114      * Returns the information about the wallpaper if the current wallpaper is
1115      * a live wallpaper component. Otherwise, if the wallpaper is a static image,
1116      * this returns null.
1117      *
1118      * @param userId Owner of the wallpaper.
1119      * @hide
1120      */
getWallpaperInfo(int userId)1121     public WallpaperInfo getWallpaperInfo(int userId) {
1122         try {
1123             if (sGlobals.mService == null) {
1124                 Log.w(TAG, "WallpaperService not running");
1125                 throw new RuntimeException(new DeadSystemException());
1126             } else {
1127                 return sGlobals.mService.getWallpaperInfo(userId);
1128             }
1129         } catch (RemoteException e) {
1130             throw e.rethrowFromSystemServer();
1131         }
1132     }
1133 
1134     /**
1135      * Get the ID of the current wallpaper of the given kind.  If there is no
1136      * such wallpaper configured, returns a negative number.
1137      *
1138      * <p>Every time the wallpaper image is set, a new ID is assigned to it.
1139      * This method allows the caller to determine whether the wallpaper imagery
1140      * has changed, regardless of how that change happened.
1141      *
1142      * @param which The wallpaper whose ID is to be returned.  Must be a single
1143      *     defined kind of wallpaper, either {@link #FLAG_SYSTEM} or
1144      *     {@link #FLAG_LOCK}.
1145      * @return The positive numeric ID of the current wallpaper of the given kind,
1146      *     or a negative value if no such wallpaper is configured.
1147      */
getWallpaperId(@etWallpaperFlags int which)1148     public int getWallpaperId(@SetWallpaperFlags int which) {
1149         return getWallpaperIdForUser(which, mContext.getUserId());
1150     }
1151 
1152     /**
1153      * Get the ID of the given user's current wallpaper of the given kind.  If there
1154      * is no such wallpaper configured, returns a negative number.
1155      * @hide
1156      */
getWallpaperIdForUser(@etWallpaperFlags int which, int userId)1157     public int getWallpaperIdForUser(@SetWallpaperFlags int which, int userId) {
1158         try {
1159             if (sGlobals.mService == null) {
1160                 Log.w(TAG, "WallpaperService not running");
1161                 throw new RuntimeException(new DeadSystemException());
1162             } else {
1163                 return sGlobals.mService.getWallpaperIdForUser(which, userId);
1164             }
1165         } catch (RemoteException e) {
1166             throw e.rethrowFromSystemServer();
1167         }
1168     }
1169 
1170     /**
1171      * Gets an Intent that will launch an activity that crops the given
1172      * image and sets the device's wallpaper. If there is a default HOME activity
1173      * that supports cropping wallpapers, it will be preferred as the default.
1174      * Use this method instead of directly creating a {@link #ACTION_CROP_AND_SET_WALLPAPER}
1175      * intent.
1176      *
1177      * @param imageUri The image URI that will be set in the intent. The must be a content
1178      *                 URI and its provider must resolve its type to "image/*"
1179      *
1180      * @throws IllegalArgumentException if the URI is not a content URI or its MIME type is
1181      *         not "image/*"
1182      */
getCropAndSetWallpaperIntent(Uri imageUri)1183     public Intent getCropAndSetWallpaperIntent(Uri imageUri) {
1184         if (imageUri == null) {
1185             throw new IllegalArgumentException("Image URI must not be null");
1186         }
1187 
1188         if (!ContentResolver.SCHEME_CONTENT.equals(imageUri.getScheme())) {
1189             throw new IllegalArgumentException("Image URI must be of the "
1190                     + ContentResolver.SCHEME_CONTENT + " scheme type");
1191         }
1192 
1193         final PackageManager packageManager = mContext.getPackageManager();
1194         Intent cropAndSetWallpaperIntent =
1195                 new Intent(ACTION_CROP_AND_SET_WALLPAPER, imageUri);
1196         cropAndSetWallpaperIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
1197 
1198         // Find out if the default HOME activity supports CROP_AND_SET_WALLPAPER
1199         Intent homeIntent = new Intent(Intent.ACTION_MAIN).addCategory(Intent.CATEGORY_HOME);
1200         ResolveInfo resolvedHome = packageManager.resolveActivity(homeIntent,
1201                 PackageManager.MATCH_DEFAULT_ONLY);
1202         if (resolvedHome != null) {
1203             cropAndSetWallpaperIntent.setPackage(resolvedHome.activityInfo.packageName);
1204 
1205             List<ResolveInfo> cropAppList = packageManager.queryIntentActivities(
1206                     cropAndSetWallpaperIntent, 0);
1207             if (cropAppList.size() > 0) {
1208                 return cropAndSetWallpaperIntent;
1209             }
1210         }
1211 
1212         // fallback crop activity
1213         final String cropperPackage = mContext.getString(
1214                 com.android.internal.R.string.config_wallpaperCropperPackage);
1215         cropAndSetWallpaperIntent.setPackage(cropperPackage);
1216         List<ResolveInfo> cropAppList = packageManager.queryIntentActivities(
1217                 cropAndSetWallpaperIntent, 0);
1218         if (cropAppList.size() > 0) {
1219             return cropAndSetWallpaperIntent;
1220         }
1221         // If the URI is not of the right type, or for some reason the system wallpaper
1222         // cropper doesn't exist, return null
1223         throw new IllegalArgumentException("Cannot use passed URI to set wallpaper; " +
1224             "check that the type returned by ContentProvider matches image/*");
1225     }
1226 
1227     /**
1228      * Change the current system wallpaper to the bitmap in the given resource.
1229      * The resource is opened as a raw data stream and copied into the
1230      * wallpaper; it must be a valid PNG or JPEG image.  On success, the intent
1231      * {@link Intent#ACTION_WALLPAPER_CHANGED} is broadcast.
1232      *
1233      * <p>This method requires the caller to hold the permission
1234      * {@link android.Manifest.permission#SET_WALLPAPER}.
1235      *
1236      * @param resid The resource ID of the bitmap to be used as the wallpaper image
1237      *
1238      * @throws IOException If an error occurs reverting to the built-in
1239      * wallpaper.
1240      */
1241     @RequiresPermission(android.Manifest.permission.SET_WALLPAPER)
setResource(@awRes int resid)1242     public void setResource(@RawRes int resid) throws IOException {
1243         setResource(resid, FLAG_SYSTEM | FLAG_LOCK);
1244     }
1245 
1246     /**
1247      * Version of {@link #setResource(int)} that allows the caller to specify which
1248      * of the supported wallpaper categories to set.
1249      *
1250      * @param resid The resource ID of the bitmap to be used as the wallpaper image
1251      * @param which Flags indicating which wallpaper(s) to configure with the new imagery
1252      *
1253      * @see #FLAG_LOCK
1254      * @see #FLAG_SYSTEM
1255      *
1256      * @return An integer ID assigned to the newly active wallpaper; or zero on failure.
1257      *
1258      * @throws IOException
1259      */
1260     @RequiresPermission(android.Manifest.permission.SET_WALLPAPER)
setResource(@awRes int resid, @SetWallpaperFlags int which)1261     public int setResource(@RawRes int resid, @SetWallpaperFlags int which)
1262             throws IOException {
1263         if (sGlobals.mService == null) {
1264             Log.w(TAG, "WallpaperService not running");
1265             throw new RuntimeException(new DeadSystemException());
1266         }
1267         final Bundle result = new Bundle();
1268         final WallpaperSetCompletion completion = new WallpaperSetCompletion();
1269         try {
1270             Resources resources = mContext.getResources();
1271             /* Set the wallpaper to the default values */
1272             ParcelFileDescriptor fd = sGlobals.mService.setWallpaper(
1273                     "res:" + resources.getResourceName(resid),
1274                     mContext.getOpPackageName(), null, false, result, which, completion,
1275                     mContext.getUserId());
1276             if (fd != null) {
1277                 FileOutputStream fos = null;
1278                 boolean ok = false;
1279                 try {
1280                     fos = new ParcelFileDescriptor.AutoCloseOutputStream(fd);
1281                     copyStreamToWallpaperFile(resources.openRawResource(resid), fos);
1282                     // The 'close()' is the trigger for any server-side image manipulation,
1283                     // so we must do that before waiting for completion.
1284                     fos.close();
1285                     completion.waitForCompletion();
1286                 } finally {
1287                     // Might be redundant but completion shouldn't wait unless the write
1288                     // succeeded; this is a fallback if it threw past the close+wait.
1289                     IoUtils.closeQuietly(fos);
1290                 }
1291             }
1292         } catch (RemoteException e) {
1293             throw e.rethrowFromSystemServer();
1294         }
1295         return result.getInt(EXTRA_NEW_WALLPAPER_ID, 0);
1296     }
1297 
1298     /**
1299      * Change the current system wallpaper to a bitmap.  The given bitmap is
1300      * converted to a PNG and stored as the wallpaper.  On success, the intent
1301      * {@link Intent#ACTION_WALLPAPER_CHANGED} is broadcast.
1302      *
1303      * <p>This method is equivalent to calling
1304      * {@link #setBitmap(Bitmap, Rect, boolean)} and passing {@code null} for the
1305      * {@code visibleCrop} rectangle and {@code true} for the {@code allowBackup}
1306      * parameter.
1307      *
1308      * <p>This method requires the caller to hold the permission
1309      * {@link android.Manifest.permission#SET_WALLPAPER}.
1310      *
1311      * @param bitmap The bitmap to be used as the new system wallpaper.
1312      *
1313      * @throws IOException If an error occurs when attempting to set the wallpaper
1314      *     to the provided image.
1315      */
1316     @RequiresPermission(android.Manifest.permission.SET_WALLPAPER)
setBitmap(Bitmap bitmap)1317     public void setBitmap(Bitmap bitmap) throws IOException {
1318         setBitmap(bitmap, null, true);
1319     }
1320 
1321     /**
1322      * Change the current system wallpaper to a bitmap, specifying a hint about
1323      * which subrectangle of the full image is to be visible.  The OS will then
1324      * try to best present the given portion of the full image as the static system
1325      * wallpaper image.  On success, the intent
1326      * {@link Intent#ACTION_WALLPAPER_CHANGED} is broadcast.
1327      *
1328      * <p>Passing {@code null} as the {@code visibleHint} parameter is equivalent to
1329      * passing (0, 0, {@code fullImage.getWidth()}, {@code fullImage.getHeight()}).
1330      *
1331      * <p>This method requires the caller to hold the permission
1332      * {@link android.Manifest.permission#SET_WALLPAPER}.
1333      *
1334      * @param fullImage A bitmap that will supply the wallpaper imagery.
1335      * @param visibleCropHint The rectangular subregion of {@code fullImage} that should be
1336      *     displayed as wallpaper.  Passing {@code null} for this parameter means that
1337      *     the full image should be displayed if possible given the image's and device's
1338      *     aspect ratios, etc.
1339      * @param allowBackup {@code true} if the OS is permitted to back up this wallpaper
1340      *     image for restore to a future device; {@code false} otherwise.
1341      *
1342      * @return An integer ID assigned to the newly active wallpaper; or zero on failure.
1343      *
1344      * @throws IOException If an error occurs when attempting to set the wallpaper
1345      *     to the provided image.
1346      * @throws IllegalArgumentException If the {@code visibleCropHint} rectangle is
1347      *     empty or invalid.
1348      */
1349     @RequiresPermission(android.Manifest.permission.SET_WALLPAPER)
setBitmap(Bitmap fullImage, Rect visibleCropHint, boolean allowBackup)1350     public int setBitmap(Bitmap fullImage, Rect visibleCropHint, boolean allowBackup)
1351             throws IOException {
1352         return setBitmap(fullImage, visibleCropHint, allowBackup, FLAG_SYSTEM | FLAG_LOCK);
1353     }
1354 
1355     /**
1356      * Version of {@link #setBitmap(Bitmap, Rect, boolean)} that allows the caller
1357      * to specify which of the supported wallpaper categories to set.
1358      *
1359      * @param fullImage A bitmap that will supply the wallpaper imagery.
1360      * @param visibleCropHint The rectangular subregion of {@code fullImage} that should be
1361      *     displayed as wallpaper.  Passing {@code null} for this parameter means that
1362      *     the full image should be displayed if possible given the image's and device's
1363      *     aspect ratios, etc.
1364      * @param allowBackup {@code true} if the OS is permitted to back up this wallpaper
1365      *     image for restore to a future device; {@code false} otherwise.
1366      * @param which Flags indicating which wallpaper(s) to configure with the new imagery.
1367      *
1368      * @see #FLAG_LOCK
1369      * @see #FLAG_SYSTEM
1370      *
1371      * @return An integer ID assigned to the newly active wallpaper; or zero on failure.
1372      *
1373      * @throws IOException
1374      */
1375     @RequiresPermission(android.Manifest.permission.SET_WALLPAPER)
setBitmap(Bitmap fullImage, Rect visibleCropHint, boolean allowBackup, @SetWallpaperFlags int which)1376     public int setBitmap(Bitmap fullImage, Rect visibleCropHint,
1377             boolean allowBackup, @SetWallpaperFlags int which)
1378             throws IOException {
1379         return setBitmap(fullImage, visibleCropHint, allowBackup, which,
1380                 mContext.getUserId());
1381     }
1382 
1383     /**
1384      * Like {@link #setBitmap(Bitmap, Rect, boolean, int)}, but allows to pass in an explicit user
1385      * id. If the user id doesn't match the user id the process is running under, calling this
1386      * requires permission {@link android.Manifest.permission#INTERACT_ACROSS_USERS_FULL}.
1387      * @hide
1388      */
1389     @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P, trackingBug = 115609023)
setBitmap(Bitmap fullImage, Rect visibleCropHint, boolean allowBackup, @SetWallpaperFlags int which, int userId)1390     public int setBitmap(Bitmap fullImage, Rect visibleCropHint,
1391             boolean allowBackup, @SetWallpaperFlags int which, int userId)
1392             throws IOException {
1393         validateRect(visibleCropHint);
1394         if (sGlobals.mService == null) {
1395             Log.w(TAG, "WallpaperService not running");
1396             throw new RuntimeException(new DeadSystemException());
1397         }
1398         final Bundle result = new Bundle();
1399         final WallpaperSetCompletion completion = new WallpaperSetCompletion();
1400         try {
1401             ParcelFileDescriptor fd = sGlobals.mService.setWallpaper(null,
1402                     mContext.getOpPackageName(), visibleCropHint, allowBackup,
1403                     result, which, completion, userId);
1404             if (fd != null) {
1405                 FileOutputStream fos = null;
1406                 try {
1407                     fos = new ParcelFileDescriptor.AutoCloseOutputStream(fd);
1408                     fullImage.compress(Bitmap.CompressFormat.PNG, 90, fos);
1409                     fos.close();
1410                     completion.waitForCompletion();
1411                 } finally {
1412                     IoUtils.closeQuietly(fos);
1413                 }
1414             }
1415         } catch (RemoteException e) {
1416             throw e.rethrowFromSystemServer();
1417         }
1418         return result.getInt(EXTRA_NEW_WALLPAPER_ID, 0);
1419     }
1420 
validateRect(Rect rect)1421     private final void validateRect(Rect rect) {
1422         if (rect != null && rect.isEmpty()) {
1423             throw new IllegalArgumentException("visibleCrop rectangle must be valid and non-empty");
1424         }
1425     }
1426 
1427     /**
1428      * Change the current system wallpaper to a specific byte stream.  The
1429      * give InputStream is copied into persistent storage and will now be
1430      * used as the wallpaper.  Currently it must be either a JPEG or PNG
1431      * image.  On success, the intent {@link Intent#ACTION_WALLPAPER_CHANGED}
1432      * is broadcast.
1433      *
1434      * <p>This method is equivalent to calling
1435      * {@link #setStream(InputStream, Rect, boolean)} and passing {@code null} for the
1436      * {@code visibleCrop} rectangle and {@code true} for the {@code allowBackup}
1437      * parameter.
1438      *
1439      * <p>This method requires the caller to hold the permission
1440      * {@link android.Manifest.permission#SET_WALLPAPER}.
1441      *
1442      * @param bitmapData A stream containing the raw data to install as a wallpaper.  This
1443      *     data can be in any format handled by {@link BitmapRegionDecoder}.
1444      *
1445      * @throws IOException If an error occurs when attempting to set the wallpaper
1446      *     based on the provided image data.
1447      */
1448     @RequiresPermission(android.Manifest.permission.SET_WALLPAPER)
setStream(InputStream bitmapData)1449     public void setStream(InputStream bitmapData) throws IOException {
1450         setStream(bitmapData, null, true);
1451     }
1452 
copyStreamToWallpaperFile(InputStream data, FileOutputStream fos)1453     private void copyStreamToWallpaperFile(InputStream data, FileOutputStream fos)
1454             throws IOException {
1455         FileUtils.copy(data, fos);
1456     }
1457 
1458     /**
1459      * Change the current system wallpaper to a specific byte stream, specifying a
1460      * hint about which subrectangle of the full image is to be visible.  The OS will
1461      * then try to best present the given portion of the full image as the static system
1462      * wallpaper image.  The data from the given InputStream is copied into persistent
1463      * storage and will then be used as the system wallpaper.  Currently the data must
1464      * be either a JPEG or PNG image.  On success, the intent
1465      * {@link Intent#ACTION_WALLPAPER_CHANGED} is broadcast.
1466      *
1467      * <p>This method requires the caller to hold the permission
1468      * {@link android.Manifest.permission#SET_WALLPAPER}.
1469      *
1470      * @param bitmapData A stream containing the raw data to install as a wallpaper.  This
1471      *     data can be in any format handled by {@link BitmapRegionDecoder}.
1472      * @param visibleCropHint The rectangular subregion of the streamed image that should be
1473      *     displayed as wallpaper.  Passing {@code null} for this parameter means that
1474      *     the full image should be displayed if possible given the image's and device's
1475      *     aspect ratios, etc.
1476      * @param allowBackup {@code true} if the OS is permitted to back up this wallpaper
1477      *     image for restore to a future device; {@code false} otherwise.
1478      * @return An integer ID assigned to the newly active wallpaper; or zero on failure.
1479      *
1480      * @see #getWallpaperId(int)
1481      *
1482      * @throws IOException If an error occurs when attempting to set the wallpaper
1483      *     based on the provided image data.
1484      * @throws IllegalArgumentException If the {@code visibleCropHint} rectangle is
1485      *     empty or invalid.
1486      */
1487     @RequiresPermission(android.Manifest.permission.SET_WALLPAPER)
setStream(InputStream bitmapData, Rect visibleCropHint, boolean allowBackup)1488     public int setStream(InputStream bitmapData, Rect visibleCropHint, boolean allowBackup)
1489             throws IOException {
1490         return setStream(bitmapData, visibleCropHint, allowBackup, FLAG_SYSTEM | FLAG_LOCK);
1491     }
1492 
1493     /**
1494      * Version of {@link #setStream(InputStream, Rect, boolean)} that allows the caller
1495      * to specify which of the supported wallpaper categories to set.
1496      *
1497      * @param bitmapData A stream containing the raw data to install as a wallpaper.  This
1498      *     data can be in any format handled by {@link BitmapRegionDecoder}.
1499      * @param visibleCropHint The rectangular subregion of the streamed image that should be
1500      *     displayed as wallpaper.  Passing {@code null} for this parameter means that
1501      *     the full image should be displayed if possible given the image's and device's
1502      *     aspect ratios, etc.
1503      * @param allowBackup {@code true} if the OS is permitted to back up this wallpaper
1504      *     image for restore to a future device; {@code false} otherwise.
1505      * @param which Flags indicating which wallpaper(s) to configure with the new imagery.
1506      * @return An integer ID assigned to the newly active wallpaper; or zero on failure.
1507      *
1508      * @see #getWallpaperId(int)
1509      * @see #FLAG_LOCK
1510      * @see #FLAG_SYSTEM
1511      *
1512      * @throws IOException
1513      */
1514     @RequiresPermission(android.Manifest.permission.SET_WALLPAPER)
setStream(InputStream bitmapData, Rect visibleCropHint, boolean allowBackup, @SetWallpaperFlags int which)1515     public int setStream(InputStream bitmapData, Rect visibleCropHint,
1516             boolean allowBackup, @SetWallpaperFlags int which)
1517                     throws IOException {
1518         validateRect(visibleCropHint);
1519         if (sGlobals.mService == null) {
1520             Log.w(TAG, "WallpaperService not running");
1521             throw new RuntimeException(new DeadSystemException());
1522         }
1523         final Bundle result = new Bundle();
1524         final WallpaperSetCompletion completion = new WallpaperSetCompletion();
1525         try {
1526             ParcelFileDescriptor fd = sGlobals.mService.setWallpaper(null,
1527                     mContext.getOpPackageName(), visibleCropHint, allowBackup,
1528                     result, which, completion, mContext.getUserId());
1529             if (fd != null) {
1530                 FileOutputStream fos = null;
1531                 try {
1532                     fos = new ParcelFileDescriptor.AutoCloseOutputStream(fd);
1533                     copyStreamToWallpaperFile(bitmapData, fos);
1534                     fos.close();
1535                     completion.waitForCompletion();
1536                 } finally {
1537                     IoUtils.closeQuietly(fos);
1538                 }
1539             }
1540         } catch (RemoteException e) {
1541             throw e.rethrowFromSystemServer();
1542         }
1543 
1544         return result.getInt(EXTRA_NEW_WALLPAPER_ID, 0);
1545     }
1546 
1547     /**
1548      * Return whether any users are currently set to use the wallpaper
1549      * with the given resource ID.  That is, their wallpaper has been
1550      * set through {@link #setResource(int)} with the same resource id.
1551      */
hasResourceWallpaper(@awRes int resid)1552     public boolean hasResourceWallpaper(@RawRes int resid) {
1553         if (sGlobals.mService == null) {
1554             Log.w(TAG, "WallpaperService not running");
1555             throw new RuntimeException(new DeadSystemException());
1556         }
1557         try {
1558             Resources resources = mContext.getResources();
1559             String name = "res:" + resources.getResourceName(resid);
1560             return sGlobals.mService.hasNamedWallpaper(name);
1561         } catch (RemoteException e) {
1562             throw e.rethrowFromSystemServer();
1563         }
1564     }
1565 
1566     /**
1567      * Returns the desired minimum width for the wallpaper. Callers of
1568      * {@link #setBitmap(android.graphics.Bitmap)} or
1569      * {@link #setStream(java.io.InputStream)} should check this value
1570      * beforehand to make sure the supplied wallpaper respects the desired
1571      * minimum width.
1572      *
1573      * If the returned value is <= 0, the caller should use the width of
1574      * the default display instead.
1575      *
1576      * @return The desired minimum width for the wallpaper. This value should
1577      * be honored by applications that set the wallpaper but it is not
1578      * mandatory.
1579      *
1580      * @see #getDesiredMinimumHeight()
1581      */
getDesiredMinimumWidth()1582     public int getDesiredMinimumWidth() {
1583         if (sGlobals.mService == null) {
1584             Log.w(TAG, "WallpaperService not running");
1585             throw new RuntimeException(new DeadSystemException());
1586         }
1587         try {
1588             return sGlobals.mService.getWidthHint(mContext.getDisplayId());
1589         } catch (RemoteException e) {
1590             throw e.rethrowFromSystemServer();
1591         }
1592     }
1593 
1594     /**
1595      * Returns the desired minimum height for the wallpaper. Callers of
1596      * {@link #setBitmap(android.graphics.Bitmap)} or
1597      * {@link #setStream(java.io.InputStream)} should check this value
1598      * beforehand to make sure the supplied wallpaper respects the desired
1599      * minimum height.
1600      *
1601      * If the returned value is <= 0, the caller should use the height of
1602      * the default display instead.
1603      *
1604      * @return The desired minimum height for the wallpaper. This value should
1605      * be honored by applications that set the wallpaper but it is not
1606      * mandatory.
1607      *
1608      * @see #getDesiredMinimumWidth()
1609      */
getDesiredMinimumHeight()1610     public int getDesiredMinimumHeight() {
1611         if (sGlobals.mService == null) {
1612             Log.w(TAG, "WallpaperService not running");
1613             throw new RuntimeException(new DeadSystemException());
1614         }
1615         try {
1616             return sGlobals.mService.getHeightHint(mContext.getDisplayId());
1617         } catch (RemoteException e) {
1618             throw e.rethrowFromSystemServer();
1619         }
1620     }
1621 
1622     /**
1623      * For use only by the current home application, to specify the size of
1624      * wallpaper it would like to use.  This allows such applications to have
1625      * a virtual wallpaper that is larger than the physical screen, matching
1626      * the size of their workspace.
1627      *
1628      * <p class="note">Calling this method from apps other than the active
1629      * home app is not guaranteed to work properly.  Other apps that supply
1630      * wallpaper imagery should use {@link #getDesiredMinimumWidth()} and
1631      * {@link #getDesiredMinimumHeight()} and construct a wallpaper that
1632      * matches those dimensions.
1633      *
1634      * <p>This method requires the caller to hold the permission
1635      * {@link android.Manifest.permission#SET_WALLPAPER_HINTS}.
1636      *
1637      * @param minimumWidth Desired minimum width
1638      * @param minimumHeight Desired minimum height
1639      */
suggestDesiredDimensions(int minimumWidth, int minimumHeight)1640     public void suggestDesiredDimensions(int minimumWidth, int minimumHeight) {
1641         try {
1642             /**
1643              * The framework makes no attempt to limit the window size
1644              * to the maximum texture size. Any window larger than this
1645              * cannot be composited.
1646              *
1647              * Read maximum texture size from system property and scale down
1648              * minimumWidth and minimumHeight accordingly.
1649              */
1650             int maximumTextureSize;
1651             try {
1652                 maximumTextureSize = SystemProperties.getInt("sys.max_texture_size", 0);
1653             } catch (Exception e) {
1654                 maximumTextureSize = 0;
1655             }
1656 
1657             if (maximumTextureSize > 0) {
1658                 if ((minimumWidth > maximumTextureSize) ||
1659                     (minimumHeight > maximumTextureSize)) {
1660                     float aspect = (float)minimumHeight / (float)minimumWidth;
1661                     if (minimumWidth > minimumHeight) {
1662                         minimumWidth = maximumTextureSize;
1663                         minimumHeight = (int)((minimumWidth * aspect) + 0.5);
1664                     } else {
1665                         minimumHeight = maximumTextureSize;
1666                         minimumWidth = (int)((minimumHeight / aspect) + 0.5);
1667                     }
1668                 }
1669             }
1670 
1671             if (sGlobals.mService == null) {
1672                 Log.w(TAG, "WallpaperService not running");
1673                 throw new RuntimeException(new DeadSystemException());
1674             } else {
1675                 sGlobals.mService.setDimensionHints(minimumWidth, minimumHeight,
1676                         mContext.getOpPackageName(), mContext.getDisplayId());
1677             }
1678         } catch (RemoteException e) {
1679             throw e.rethrowFromSystemServer();
1680         }
1681     }
1682 
1683     /**
1684      * Specify extra padding that the wallpaper should have outside of the display.
1685      * That is, the given padding supplies additional pixels the wallpaper should extend
1686      * outside of the display itself.
1687      *
1688      * <p>This method requires the caller to hold the permission
1689      * {@link android.Manifest.permission#SET_WALLPAPER_HINTS}.
1690      *
1691      * @param padding The number of pixels the wallpaper should extend beyond the display,
1692      * on its left, top, right, and bottom sides.
1693      */
1694     @RequiresPermission(android.Manifest.permission.SET_WALLPAPER_HINTS)
setDisplayPadding(Rect padding)1695     public void setDisplayPadding(Rect padding) {
1696         try {
1697             if (sGlobals.mService == null) {
1698                 Log.w(TAG, "WallpaperService not running");
1699                 throw new RuntimeException(new DeadSystemException());
1700             } else {
1701                 sGlobals.mService.setDisplayPadding(padding, mContext.getOpPackageName(),
1702                         mContext.getDisplayId());
1703             }
1704         } catch (RemoteException e) {
1705             throw e.rethrowFromSystemServer();
1706         }
1707     }
1708 
1709     /**
1710      * Apply a raw offset to the wallpaper window.  Should only be used in
1711      * combination with {@link #setDisplayPadding(android.graphics.Rect)} when you
1712      * have ensured that the wallpaper will extend outside of the display area so that
1713      * it can be moved without leaving part of the display uncovered.
1714      * @param x The offset, in pixels, to apply to the left edge.
1715      * @param y The offset, in pixels, to apply to the top edge.
1716      * @hide
1717      */
1718     @SystemApi
setDisplayOffset(IBinder windowToken, int x, int y)1719     public void setDisplayOffset(IBinder windowToken, int x, int y) {
1720         try {
1721             //Log.v(TAG, "Sending new wallpaper display offsets from app...");
1722             WindowManagerGlobal.getWindowSession().setWallpaperDisplayOffset(
1723                     windowToken, x, y);
1724             //Log.v(TAG, "...app returning after sending display offset!");
1725         } catch (RemoteException e) {
1726             throw e.rethrowFromSystemServer();
1727         }
1728     }
1729 
1730     /**
1731      * Reset all wallpaper to the factory default.
1732      *
1733      * <p>This method requires the caller to hold the permission
1734      * {@link android.Manifest.permission#SET_WALLPAPER}.
1735      */
1736     @RequiresPermission(android.Manifest.permission.SET_WALLPAPER)
clearWallpaper()1737     public void clearWallpaper() {
1738         clearWallpaper(FLAG_LOCK, mContext.getUserId());
1739         clearWallpaper(FLAG_SYSTEM, mContext.getUserId());
1740     }
1741 
1742     /**
1743      * Clear the wallpaper for a specific user.  The caller must hold the
1744      * INTERACT_ACROSS_USERS_FULL permission to clear another user's
1745      * wallpaper, and must hold the SET_WALLPAPER permission in all
1746      * circumstances.
1747      * @hide
1748      */
1749     @SystemApi
1750     @RequiresPermission(android.Manifest.permission.INTERACT_ACROSS_USERS_FULL)
clearWallpaper(@etWallpaperFlags int which, int userId)1751     public void clearWallpaper(@SetWallpaperFlags int which, int userId) {
1752         if (sGlobals.mService == null) {
1753             Log.w(TAG, "WallpaperService not running");
1754             throw new RuntimeException(new DeadSystemException());
1755         }
1756         try {
1757             sGlobals.mService.clearWallpaper(mContext.getOpPackageName(), which, userId);
1758         } catch (RemoteException e) {
1759             throw e.rethrowFromSystemServer();
1760         }
1761     }
1762 
1763     /**
1764      * Set the live wallpaper.
1765      *
1766      * @hide
1767      */
1768     @TestApi
1769     @SystemApi
1770     @RequiresPermission(android.Manifest.permission.SET_WALLPAPER_COMPONENT)
setWallpaperComponent(ComponentName name)1771     public boolean setWallpaperComponent(ComponentName name) {
1772         return setWallpaperComponent(name, mContext.getUserId());
1773     }
1774 
1775     /**
1776      * Set the live wallpaper.
1777      *
1778      * This can only be called by packages with android.permission.SET_WALLPAPER_COMPONENT
1779      * permission. The caller must hold the INTERACT_ACROSS_USERS_FULL permission to change
1780      * another user's wallpaper.
1781      *
1782      * @hide
1783      */
1784     @RequiresPermission(android.Manifest.permission.SET_WALLPAPER_COMPONENT)
1785     @UnsupportedAppUsage
setWallpaperComponent(ComponentName name, int userId)1786     public boolean setWallpaperComponent(ComponentName name, int userId) {
1787         if (sGlobals.mService == null) {
1788             Log.w(TAG, "WallpaperService not running");
1789             throw new RuntimeException(new DeadSystemException());
1790         }
1791         try {
1792             sGlobals.mService.setWallpaperComponentChecked(name, mContext.getOpPackageName(),
1793                     userId);
1794             return true;
1795         } catch (RemoteException e) {
1796             throw e.rethrowFromSystemServer();
1797         }
1798     }
1799 
1800     /**
1801      * Set the display position of the current wallpaper within any larger space, when
1802      * that wallpaper is visible behind the given window.  The X and Y offsets
1803      * are floating point numbers ranging from 0 to 1, representing where the
1804      * wallpaper should be positioned within the screen space.  These only
1805      * make sense when the wallpaper is larger than the display.
1806      *
1807      * @param windowToken The window who these offsets should be associated
1808      * with, as returned by {@link android.view.View#getWindowToken()
1809      * View.getWindowToken()}.
1810      * @param xOffset The offset along the X dimension, from 0 to 1.
1811      * @param yOffset The offset along the Y dimension, from 0 to 1.
1812      */
setWallpaperOffsets(IBinder windowToken, float xOffset, float yOffset)1813     public void setWallpaperOffsets(IBinder windowToken, float xOffset, float yOffset) {
1814         try {
1815             //Log.v(TAG, "Sending new wallpaper offsets from app...");
1816             WindowManagerGlobal.getWindowSession().setWallpaperPosition(
1817                     windowToken, xOffset, yOffset, mWallpaperXStep, mWallpaperYStep);
1818             //Log.v(TAG, "...app returning after sending offsets!");
1819         } catch (RemoteException e) {
1820             throw e.rethrowFromSystemServer();
1821         }
1822     }
1823 
1824     /**
1825      * For applications that use multiple virtual screens showing a wallpaper,
1826      * specify the step size between virtual screens. For example, if the
1827      * launcher has 3 virtual screens, it would specify an xStep of 0.5,
1828      * since the X offset for those screens are 0.0, 0.5 and 1.0
1829      * @param xStep The X offset delta from one screen to the next one
1830      * @param yStep The Y offset delta from one screen to the next one
1831      */
setWallpaperOffsetSteps(float xStep, float yStep)1832     public void setWallpaperOffsetSteps(float xStep, float yStep) {
1833         mWallpaperXStep = xStep;
1834         mWallpaperYStep = yStep;
1835     }
1836 
1837     /**
1838      * Send an arbitrary command to the current active wallpaper.
1839      *
1840      * @param windowToken The window who these offsets should be associated
1841      * with, as returned by {@link android.view.View#getWindowToken()
1842      * View.getWindowToken()}.
1843      * @param action Name of the command to perform.  This must be a scoped
1844      * name to avoid collisions, such as "com.mycompany.wallpaper.DOIT".
1845      * @param x Arbitrary integer argument based on command.
1846      * @param y Arbitrary integer argument based on command.
1847      * @param z Arbitrary integer argument based on command.
1848      * @param extras Optional additional information for the command, or null.
1849      */
sendWallpaperCommand(IBinder windowToken, String action, int x, int y, int z, Bundle extras)1850     public void sendWallpaperCommand(IBinder windowToken, String action,
1851             int x, int y, int z, Bundle extras) {
1852         try {
1853             //Log.v(TAG, "Sending new wallpaper offsets from app...");
1854             WindowManagerGlobal.getWindowSession().sendWallpaperCommand(
1855                     windowToken, action, x, y, z, extras, false);
1856             //Log.v(TAG, "...app returning after sending offsets!");
1857         } catch (RemoteException e) {
1858             throw e.rethrowFromSystemServer();
1859         }
1860     }
1861 
1862     /**
1863      * Set the current zoom out level of the wallpaper
1864      * @param zoom from 0 to 1 (inclusive) where 1 means fully zoomed out, 0 means fully zoomed in
1865      *
1866      * @hide
1867      */
setWallpaperZoomOut(IBinder windowToken, float zoom)1868     public void setWallpaperZoomOut(IBinder windowToken, float zoom) {
1869         if (zoom < 0 || zoom > 1f) {
1870             throw new IllegalArgumentException("zoom must be between 0 and one: " + zoom);
1871         }
1872         try {
1873             WindowManagerGlobal.getWindowSession().setWallpaperZoomOut(windowToken, zoom);
1874         } catch (RemoteException e) {
1875             throw e.rethrowFromSystemServer();
1876         }
1877     }
1878 
1879     /**
1880      * Returns whether wallpapers are supported for the calling user. If this function returns
1881      * {@code false}, any attempts to changing the wallpaper will have no effect,
1882      * and any attempt to obtain of the wallpaper will return {@code null}.
1883      */
isWallpaperSupported()1884     public boolean isWallpaperSupported() {
1885         if (sGlobals.mService == null) {
1886             Log.w(TAG, "WallpaperService not running");
1887             throw new RuntimeException(new DeadSystemException());
1888         } else {
1889             try {
1890                 return sGlobals.mService.isWallpaperSupported(mContext.getOpPackageName());
1891             } catch (RemoteException e) {
1892                 throw e.rethrowFromSystemServer();
1893             }
1894         }
1895     }
1896 
1897     /**
1898      * Returns whether the calling package is allowed to set the wallpaper for the calling user.
1899      * If this function returns {@code false}, any attempts to change the wallpaper will have
1900      * no effect. Always returns {@code true} for device owner and profile owner.
1901      *
1902      * @see android.os.UserManager#DISALLOW_SET_WALLPAPER
1903      */
isSetWallpaperAllowed()1904     public boolean isSetWallpaperAllowed() {
1905         if (sGlobals.mService == null) {
1906             Log.w(TAG, "WallpaperService not running");
1907             throw new RuntimeException(new DeadSystemException());
1908         } else {
1909             try {
1910                 return sGlobals.mService.isSetWallpaperAllowed(mContext.getOpPackageName());
1911             } catch (RemoteException e) {
1912                 throw e.rethrowFromSystemServer();
1913             }
1914         }
1915     }
1916 
1917     /**
1918      * Clear the offsets previously associated with this window through
1919      * {@link #setWallpaperOffsets(IBinder, float, float)}.  This reverts
1920      * the window to its default state, where it does not cause the wallpaper
1921      * to scroll from whatever its last offsets were.
1922      *
1923      * @param windowToken The window who these offsets should be associated
1924      * with, as returned by {@link android.view.View#getWindowToken()
1925      * View.getWindowToken()}.
1926      */
clearWallpaperOffsets(IBinder windowToken)1927     public void clearWallpaperOffsets(IBinder windowToken) {
1928         try {
1929             WindowManagerGlobal.getWindowSession().setWallpaperPosition(
1930                     windowToken, -1, -1, -1, -1);
1931         } catch (RemoteException e) {
1932             throw e.rethrowFromSystemServer();
1933         }
1934     }
1935 
1936     /**
1937      * Remove any currently set system wallpaper, reverting to the system's built-in
1938      * wallpaper. On success, the intent {@link Intent#ACTION_WALLPAPER_CHANGED}
1939      * is broadcast.
1940      *
1941      * <p>This method requires the caller to hold the permission
1942      * {@link android.Manifest.permission#SET_WALLPAPER}.
1943      *
1944      * @throws IOException If an error occurs reverting to the built-in
1945      * wallpaper.
1946      */
1947     @RequiresPermission(android.Manifest.permission.SET_WALLPAPER)
clear()1948     public void clear() throws IOException {
1949         setStream(openDefaultWallpaper(mContext, FLAG_SYSTEM), null, false);
1950     }
1951 
1952     /**
1953      * Remove one or more currently set wallpapers, reverting to the system default
1954      * display for each one.  If {@link #FLAG_SYSTEM} is set in the {@code which}
1955      * parameter, the intent {@link Intent#ACTION_WALLPAPER_CHANGED} will be broadcast
1956      * upon success.
1957      *
1958      * @param which A bitwise combination of {@link #FLAG_SYSTEM} or
1959      *   {@link #FLAG_LOCK}
1960      * @throws IOException If an error occurs reverting to the built-in wallpaper.
1961      */
1962     @RequiresPermission(android.Manifest.permission.SET_WALLPAPER)
clear(@etWallpaperFlags int which)1963     public void clear(@SetWallpaperFlags int which) throws IOException {
1964         if ((which & FLAG_SYSTEM) != 0) {
1965             clear();
1966         }
1967         if ((which & FLAG_LOCK) != 0) {
1968             clearWallpaper(FLAG_LOCK, mContext.getUserId());
1969         }
1970     }
1971 
1972     /**
1973      * Open stream representing the default static image wallpaper.
1974      *
1975      * If the device defines no default wallpaper of the requested kind,
1976      * {@code null} is returned.
1977      *
1978      * @hide
1979      */
1980     @UnsupportedAppUsage
openDefaultWallpaper(Context context, @SetWallpaperFlags int which)1981     public static InputStream openDefaultWallpaper(Context context, @SetWallpaperFlags int which) {
1982         final String whichProp;
1983         final int defaultResId;
1984         if (which == FLAG_LOCK) {
1985             /* Factory-default lock wallpapers are not yet supported
1986             whichProp = PROP_LOCK_WALLPAPER;
1987             defaultResId = com.android.internal.R.drawable.default_lock_wallpaper;
1988             */
1989             return null;
1990         } else {
1991             whichProp = PROP_WALLPAPER;
1992             defaultResId = com.android.internal.R.drawable.default_wallpaper;
1993         }
1994         final String path = SystemProperties.get(whichProp);
1995         if (!TextUtils.isEmpty(path)) {
1996             final File file = new File(path);
1997             if (file.exists()) {
1998                 try {
1999                     return new FileInputStream(file);
2000                 } catch (IOException e) {
2001                     // Ignored, fall back to platform default below
2002                 }
2003             }
2004         }
2005         try {
2006             return context.getResources().openRawResource(defaultResId);
2007         } catch (NotFoundException e) {
2008             // no default defined for this device; this is not a failure
2009         }
2010         return null;
2011     }
2012 
2013     /**
2014      * Return {@link ComponentName} of the default live wallpaper, or
2015      * {@code null} if none is defined.
2016      *
2017      * @hide
2018      */
getDefaultWallpaperComponent(Context context)2019     public static ComponentName getDefaultWallpaperComponent(Context context) {
2020         ComponentName cn = null;
2021 
2022         String flat = SystemProperties.get(PROP_WALLPAPER_COMPONENT);
2023         if (!TextUtils.isEmpty(flat)) {
2024             cn = ComponentName.unflattenFromString(flat);
2025         }
2026 
2027         if (cn == null) {
2028             flat = context.getString(com.android.internal.R.string.default_wallpaper_component);
2029             if (!TextUtils.isEmpty(flat)) {
2030                 cn = ComponentName.unflattenFromString(flat);
2031             }
2032         }
2033 
2034         // Check if the package exists
2035         if (cn != null) {
2036             try {
2037                 final PackageManager packageManager = context.getPackageManager();
2038                 packageManager.getPackageInfo(cn.getPackageName(),
2039                         PackageManager.MATCH_DIRECT_BOOT_AWARE
2040                                 | PackageManager.MATCH_DIRECT_BOOT_UNAWARE);
2041             } catch (PackageManager.NameNotFoundException e) {
2042                 cn = null;
2043             }
2044         }
2045 
2046         return cn;
2047     }
2048 
2049     /**
2050      * Register a callback for lock wallpaper observation. Only the OS may use this.
2051      *
2052      * @return true on success; false on error.
2053      * @hide
2054      */
setLockWallpaperCallback(IWallpaperManagerCallback callback)2055     public boolean setLockWallpaperCallback(IWallpaperManagerCallback callback) {
2056         if (sGlobals.mService == null) {
2057             Log.w(TAG, "WallpaperService not running");
2058             throw new RuntimeException(new DeadSystemException());
2059         }
2060 
2061         try {
2062             return sGlobals.mService.setLockWallpaperCallback(callback);
2063         } catch (RemoteException e) {
2064             throw e.rethrowFromSystemServer();
2065         }
2066     }
2067 
2068     /**
2069      * Is the current system wallpaper eligible for backup?
2070      *
2071      * Only the OS itself may use this method.
2072      * @hide
2073      */
isWallpaperBackupEligible(int which)2074     public boolean isWallpaperBackupEligible(int which) {
2075         if (sGlobals.mService == null) {
2076             Log.w(TAG, "WallpaperService not running");
2077             throw new RuntimeException(new DeadSystemException());
2078         }
2079         try {
2080             return sGlobals.mService.isWallpaperBackupEligible(which, mContext.getUserId());
2081         } catch (RemoteException e) {
2082             Log.e(TAG, "Exception querying wallpaper backup eligibility: " + e.getMessage());
2083         }
2084         return false;
2085     }
2086 
2087     /**
2088      * Get the instance of {@link ColorManagementProxy}.
2089      *
2090      * @return instance of {@link ColorManagementProxy}.
2091      * @hide
2092      */
getColorManagementProxy()2093     public ColorManagementProxy getColorManagementProxy() {
2094         return mCmProxy;
2095     }
2096 
2097     /**
2098      * A hidden class to help {@link Globals#getCurrentWallpaperLocked} handle color management.
2099      * @hide
2100      */
2101     public static class ColorManagementProxy {
2102         private final Set<ColorSpace> mSupportedColorSpaces = new HashSet<>();
2103 
ColorManagementProxy(@onNull Context context)2104         public ColorManagementProxy(@NonNull Context context) {
2105             // Get a list of supported wide gamut color spaces.
2106             Display display = context.getDisplayNoVerify();
2107             if (display != null) {
2108                 mSupportedColorSpaces.addAll(Arrays.asList(display.getSupportedWideColorGamut()));
2109             }
2110         }
2111 
2112         @NonNull
getSupportedColorSpaces()2113         public Set<ColorSpace> getSupportedColorSpaces() {
2114             return mSupportedColorSpaces;
2115         }
2116 
isSupportedColorSpace(ColorSpace colorSpace)2117         boolean isSupportedColorSpace(ColorSpace colorSpace) {
2118             return colorSpace != null && (colorSpace == ColorSpace.get(ColorSpace.Named.SRGB)
2119                     || getSupportedColorSpaces().contains(colorSpace));
2120         }
2121 
doColorManagement(ImageDecoder decoder, ImageDecoder.ImageInfo info)2122         void doColorManagement(ImageDecoder decoder, ImageDecoder.ImageInfo info) {
2123             if (!isSupportedColorSpace(info.getColorSpace())) {
2124                 decoder.setTargetColorSpace(ColorSpace.get(ColorSpace.Named.SRGB));
2125                 Log.w(TAG, "Not supported color space: " + info.getColorSpace());
2126             }
2127         }
2128     }
2129 
2130     // Private completion callback for setWallpaper() synchronization
2131     private class WallpaperSetCompletion extends IWallpaperManagerCallback.Stub {
2132         final CountDownLatch mLatch;
2133 
WallpaperSetCompletion()2134         public WallpaperSetCompletion() {
2135             mLatch = new CountDownLatch(1);
2136         }
2137 
waitForCompletion()2138         public void waitForCompletion() {
2139             try {
2140                 mLatch.await(30, TimeUnit.SECONDS);
2141             } catch (InterruptedException e) {
2142                 // This might be legit: the crop may take a very long time. Don't sweat
2143                 // it in that case; we are okay with display lagging behind in order to
2144                 // keep the caller from locking up indeterminately.
2145             }
2146         }
2147 
2148         @Override
onWallpaperChanged()2149         public void onWallpaperChanged() throws RemoteException {
2150             mLatch.countDown();
2151         }
2152 
2153         @Override
onWallpaperColorsChanged(WallpaperColors colors, int which, int userId)2154         public void onWallpaperColorsChanged(WallpaperColors colors, int which, int userId)
2155             throws RemoteException {
2156             sGlobals.onWallpaperColorsChanged(colors, which, userId);
2157         }
2158     }
2159 
2160     /**
2161      * Interface definition for a callback to be invoked when colors change on a wallpaper.
2162      */
2163     public interface OnColorsChangedListener {
2164         /**
2165          * Called when colors change.
2166          * A {@link android.app.WallpaperColors} object containing a simplified
2167          * color histogram will be given.
2168          *
2169          * @param colors Wallpaper color info
2170          * @param which A combination of {@link #FLAG_LOCK} and {@link #FLAG_SYSTEM}
2171          */
onColorsChanged(WallpaperColors colors, int which)2172         void onColorsChanged(WallpaperColors colors, int which);
2173 
2174         /**
2175          * Called when colors change.
2176          * A {@link android.app.WallpaperColors} object containing a simplified
2177          * color histogram will be given.
2178          *
2179          * @param colors Wallpaper color info
2180          * @param which A combination of {@link #FLAG_LOCK} and {@link #FLAG_SYSTEM}
2181          * @param userId Owner of the wallpaper
2182          * @hide
2183          */
onColorsChanged(WallpaperColors colors, int which, int userId)2184         default void onColorsChanged(WallpaperColors colors, int which, int userId) {
2185             onColorsChanged(colors, which);
2186         }
2187     }
2188 }
2189