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