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