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