Dianne Hackborn | 4c62fc0 | 2009-08-08 20:40:27 -0700 | [diff] [blame] | 1 | /* |
Christopher Tate | be132e6 | 2016-02-10 12:59:49 -0800 | [diff] [blame] | 2 | h * 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; |
Tor Norbye | 7b9c912 | 2013-05-30 16:48:33 -0700 | [diff] [blame] | 20 | import android.annotation.RawRes; |
Dianne Hackborn | 067e5f6 | 2014-09-07 23:14:30 -0700 | [diff] [blame] | 21 | import android.annotation.SystemApi; |
Jeff Sharkey | 28f0877 | 2014-04-16 09:41:58 -0700 | [diff] [blame] | 22 | import android.content.ComponentName; |
Adam Lesinski | bba72d1 | 2013-10-11 18:10:56 -0700 | [diff] [blame] | 23 | import android.content.ContentResolver; |
Dianne Hackborn | 8cc6a50 | 2009-08-05 21:29:42 -0700 | [diff] [blame] | 24 | import android.content.Context; |
| 25 | import android.content.Intent; |
Michael Jurka | e8d1bf7 | 2013-09-09 15:58:54 +0200 | [diff] [blame] | 26 | import android.content.pm.PackageManager; |
| 27 | import android.content.pm.ResolveInfo; |
Dianne Hackborn | 8cc6a50 | 2009-08-05 21:29:42 -0700 | [diff] [blame] | 28 | import android.content.res.Resources; |
| 29 | import android.graphics.Bitmap; |
| 30 | import android.graphics.BitmapFactory; |
Michael Jurka | b668d0b | 2013-10-04 15:11:05 -0700 | [diff] [blame] | 31 | import android.graphics.BitmapRegionDecoder; |
Dianne Hackborn | 284ac93 | 2009-08-28 10:34:25 -0700 | [diff] [blame] | 32 | import android.graphics.Canvas; |
Dianne Hackborn | 19382ac | 2009-09-11 21:13:37 -0700 | [diff] [blame] | 33 | import android.graphics.ColorFilter; |
Michael Jurka | b668d0b | 2013-10-04 15:11:05 -0700 | [diff] [blame] | 34 | import android.graphics.Matrix; |
Dianne Hackborn | 284ac93 | 2009-08-28 10:34:25 -0700 | [diff] [blame] | 35 | import android.graphics.Paint; |
Dianne Hackborn | 19382ac | 2009-09-11 21:13:37 -0700 | [diff] [blame] | 36 | import android.graphics.PixelFormat; |
Jeff Brown | 2457237 | 2011-06-09 19:05:15 -0700 | [diff] [blame] | 37 | import android.graphics.PorterDuff; |
| 38 | import android.graphics.PorterDuffXfermode; |
Dianne Hackborn | 284ac93 | 2009-08-28 10:34:25 -0700 | [diff] [blame] | 39 | import android.graphics.Rect; |
Michael Jurka | b668d0b | 2013-10-04 15:11:05 -0700 | [diff] [blame] | 40 | import android.graphics.RectF; |
Dianne Hackborn | 8cc6a50 | 2009-08-05 21:29:42 -0700 | [diff] [blame] | 41 | import android.graphics.drawable.BitmapDrawable; |
| 42 | import android.graphics.drawable.Drawable; |
Michael Jurka | e8d1bf7 | 2013-09-09 15:58:54 +0200 | [diff] [blame] | 43 | import android.net.Uri; |
Dianne Hackborn | 284ac93 | 2009-08-28 10:34:25 -0700 | [diff] [blame] | 44 | import android.os.Bundle; |
Dianne Hackborn | 8cc6a50 | 2009-08-05 21:29:42 -0700 | [diff] [blame] | 45 | import android.os.Handler; |
| 46 | import android.os.IBinder; |
Dianne Hackborn | 840c3a2 | 2009-09-02 17:35:17 -0700 | [diff] [blame] | 47 | import android.os.Looper; |
Dianne Hackborn | 8cc6a50 | 2009-08-05 21:29:42 -0700 | [diff] [blame] | 48 | import android.os.ParcelFileDescriptor; |
| 49 | import android.os.RemoteException; |
| 50 | import android.os.ServiceManager; |
Jeff Sharkey | 28f0877 | 2014-04-16 09:41:58 -0700 | [diff] [blame] | 51 | import android.os.SystemProperties; |
Christopher Tate | be132e6 | 2016-02-10 12:59:49 -0800 | [diff] [blame] | 52 | import android.os.UserHandle; |
Jeff Sharkey | 28f0877 | 2014-04-16 09:41:58 -0700 | [diff] [blame] | 53 | import android.text.TextUtils; |
Dianne Hackborn | 7341d7a | 2009-08-14 11:37:52 -0700 | [diff] [blame] | 54 | import android.util.Log; |
Jeff Brown | 98365d7 | 2012-08-19 20:30:52 -0700 | [diff] [blame] | 55 | import android.view.WindowManagerGlobal; |
Dianne Hackborn | 8cc6a50 | 2009-08-05 21:29:42 -0700 | [diff] [blame] | 56 | |
Christopher Tate | ffa6a88 | 2015-12-11 15:08:46 -0800 | [diff] [blame] | 57 | import libcore.io.IoUtils; |
| 58 | |
Michael Jurka | b668d0b | 2013-10-04 15:11:05 -0700 | [diff] [blame] | 59 | import java.io.BufferedInputStream; |
Jeff Sharkey | 28f0877 | 2014-04-16 09:41:58 -0700 | [diff] [blame] | 60 | import java.io.File; |
| 61 | import java.io.FileInputStream; |
Dianne Hackborn | 8cc6a50 | 2009-08-05 21:29:42 -0700 | [diff] [blame] | 62 | import java.io.FileOutputStream; |
| 63 | import java.io.IOException; |
| 64 | import java.io.InputStream; |
Christopher Tate | ad3c259 | 2016-01-20 18:13:17 -0800 | [diff] [blame] | 65 | import java.lang.annotation.Retention; |
| 66 | import java.lang.annotation.RetentionPolicy; |
Michael Jurka | e8d1bf7 | 2013-09-09 15:58:54 +0200 | [diff] [blame] | 67 | import java.util.List; |
Christopher Tate | 1e1e2e0 | 2016-01-25 15:34:36 -0800 | [diff] [blame] | 68 | import java.util.concurrent.CountDownLatch; |
| 69 | import java.util.concurrent.TimeUnit; |
Dianne Hackborn | 8cc6a50 | 2009-08-05 21:29:42 -0700 | [diff] [blame] | 70 | |
Scott Main | 8b2e000 | 2009-09-29 18:17:31 -0700 | [diff] [blame] | 71 | /** |
| 72 | * Provides access to the system wallpaper. With WallpaperManager, you can |
| 73 | * get the current wallpaper, get the desired dimensions for the wallpaper, set |
| 74 | * the wallpaper, and more. Get an instance of WallpaperManager with |
Benjamin Franz | f3ece36 | 2015-02-11 10:51:10 +0000 | [diff] [blame] | 75 | * {@link #getInstance(android.content.Context) getInstance()}. |
| 76 | * |
| 77 | * <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] | 78 | * {@link #isWallpaperSupported()}, and whether setting of wallpapers is allowed, by calling |
| 79 | * {@link #isWallpaperSettingAllowed()}. |
Scott Main | 8b2e000 | 2009-09-29 18:17:31 -0700 | [diff] [blame] | 80 | */ |
Dianne Hackborn | 8cc6a50 | 2009-08-05 21:29:42 -0700 | [diff] [blame] | 81 | public class WallpaperManager { |
| 82 | private static String TAG = "WallpaperManager"; |
| 83 | private static boolean DEBUG = false; |
Marco Nelissen | bf6956b | 2009-11-09 15:21:13 -0800 | [diff] [blame] | 84 | private float mWallpaperXStep = -1; |
| 85 | private float mWallpaperYStep = -1; |
Dianne Hackborn | 8cc6a50 | 2009-08-05 21:29:42 -0700 | [diff] [blame] | 86 | |
Jeff Sharkey | 28f0877 | 2014-04-16 09:41:58 -0700 | [diff] [blame] | 87 | /** {@hide} */ |
| 88 | private static final String PROP_WALLPAPER = "ro.config.wallpaper"; |
| 89 | /** {@hide} */ |
| 90 | private static final String PROP_WALLPAPER_COMPONENT = "ro.config.wallpaper_component"; |
| 91 | |
Dianne Hackborn | 4c62fc0 | 2009-08-08 20:40:27 -0700 | [diff] [blame] | 92 | /** |
Michael Jurka | e8d1bf7 | 2013-09-09 15:58:54 +0200 | [diff] [blame] | 93 | * Activity Action: Show settings for choosing wallpaper. Do not use directly to construct |
| 94 | * an intent; instead, use {@link #getCropAndSetWallpaperIntent}. |
| 95 | * <p>Input: {@link Intent#getData} is the URI of the image to crop and set as wallpaper. |
| 96 | * <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] | 97 | * Activities that support this intent should specify a MIME filter of "image/*" |
Michael Jurka | e8d1bf7 | 2013-09-09 15:58:54 +0200 | [diff] [blame] | 98 | */ |
| 99 | public static final String ACTION_CROP_AND_SET_WALLPAPER = |
| 100 | "android.service.wallpaper.CROP_AND_SET_WALLPAPER"; |
| 101 | |
| 102 | /** |
Dianne Hackborn | 4c62fc0 | 2009-08-08 20:40:27 -0700 | [diff] [blame] | 103 | * Launch an activity for the user to pick the current global live |
| 104 | * wallpaper. |
Dianne Hackborn | 4c62fc0 | 2009-08-08 20:40:27 -0700 | [diff] [blame] | 105 | */ |
| 106 | public static final String ACTION_LIVE_WALLPAPER_CHOOSER |
| 107 | = "android.service.wallpaper.LIVE_WALLPAPER_CHOOSER"; |
Adam Cohen | 5a242ec | 2010-12-07 21:07:07 -0800 | [diff] [blame] | 108 | |
| 109 | /** |
Dianne Hackborn | 7df7d20 | 2012-04-19 18:00:04 -0700 | [diff] [blame] | 110 | * Directly launch live wallpaper preview, allowing the user to immediately |
| 111 | * confirm to switch to a specific live wallpaper. You must specify |
| 112 | * {@link #EXTRA_LIVE_WALLPAPER_COMPONENT} with the ComponentName of |
| 113 | * a live wallpaper component that is to be shown. |
| 114 | */ |
| 115 | public static final String ACTION_CHANGE_LIVE_WALLPAPER |
| 116 | = "android.service.wallpaper.CHANGE_LIVE_WALLPAPER"; |
| 117 | |
| 118 | /** |
| 119 | * Extra in {@link #ACTION_CHANGE_LIVE_WALLPAPER} that specifies the |
| 120 | * ComponentName of a live wallpaper that should be shown as a preview, |
| 121 | * for the user to confirm. |
| 122 | */ |
| 123 | public static final String EXTRA_LIVE_WALLPAPER_COMPONENT |
| 124 | = "android.service.wallpaper.extra.LIVE_WALLPAPER_COMPONENT"; |
| 125 | |
| 126 | /** |
Adam Cohen | 5a242ec | 2010-12-07 21:07:07 -0800 | [diff] [blame] | 127 | * Manifest entry for activities that respond to {@link Intent#ACTION_SET_WALLPAPER} |
| 128 | * which allows them to provide a custom large icon associated with this action. |
| 129 | */ |
| 130 | public static final String WALLPAPER_PREVIEW_META_DATA = "android.wallpaper.preview"; |
| 131 | |
Dianne Hackborn | 23ef7b4 | 2009-11-18 18:20:39 -0800 | [diff] [blame] | 132 | /** |
| 133 | * Command for {@link #sendWallpaperCommand}: reported by the wallpaper |
| 134 | * host when the user taps on an empty area (not performing an action |
| 135 | * in the host). The x and y arguments are the location of the tap in |
| 136 | * screen coordinates. |
| 137 | */ |
| 138 | public static final String COMMAND_TAP = "android.wallpaper.tap"; |
Oleksandr Peletskyi | f251981 | 2016-01-26 20:16:06 +0100 | [diff] [blame] | 139 | |
Dianne Hackborn | 23ef7b4 | 2009-11-18 18:20:39 -0800 | [diff] [blame] | 140 | /** |
| 141 | * Command for {@link #sendWallpaperCommand}: reported by the wallpaper |
Jeff Brown | 9f3bdfe | 2010-10-13 06:01:27 -0700 | [diff] [blame] | 142 | * host when the user releases a secondary pointer on an empty area |
| 143 | * (not performing an action in the host). The x and y arguments are |
| 144 | * the location of the secondary tap in screen coordinates. |
| 145 | */ |
| 146 | public static final String COMMAND_SECONDARY_TAP = "android.wallpaper.secondaryTap"; |
| 147 | |
| 148 | /** |
| 149 | * Command for {@link #sendWallpaperCommand}: reported by the wallpaper |
Dianne Hackborn | 23ef7b4 | 2009-11-18 18:20:39 -0800 | [diff] [blame] | 150 | * host when the user drops an object into an area of the host. The x |
| 151 | * and y arguments are the location of the drop. |
| 152 | */ |
| 153 | public static final String COMMAND_DROP = "android.home.drop"; |
Christopher Tate | ad3c259 | 2016-01-20 18:13:17 -0800 | [diff] [blame] | 154 | |
| 155 | /** |
| 156 | * Extra passed back from setWallpaper() giving the new wallpaper's assigned ID. |
| 157 | * @hide |
| 158 | */ |
| 159 | public static final String EXTRA_NEW_WALLPAPER_ID = "android.service.wallpaper.extra.ID"; |
| 160 | |
| 161 | // flags for which kind of wallpaper to set |
| 162 | |
| 163 | /** @hide */ |
| 164 | @IntDef(flag = true, value = { |
| 165 | FLAG_SET_SYSTEM, |
| 166 | FLAG_SET_LOCK |
| 167 | }) |
| 168 | @Retention(RetentionPolicy.SOURCE) |
| 169 | public @interface SetWallpaperFlags {} |
| 170 | |
| 171 | /** |
| 172 | * Flag: use the supplied imagery as the general system wallpaper. |
| 173 | */ |
| 174 | public static final int FLAG_SET_SYSTEM = 1 << 0; |
| 175 | |
| 176 | /** |
| 177 | * Flag: use the supplied imagery as the lock-screen wallpaper. |
| 178 | */ |
| 179 | public static final int FLAG_SET_LOCK = 1 << 1; |
| 180 | |
Dianne Hackborn | 8cc6a50 | 2009-08-05 21:29:42 -0700 | [diff] [blame] | 181 | private final Context mContext; |
Oleksandr Peletskyi | f251981 | 2016-01-26 20:16:06 +0100 | [diff] [blame] | 182 | |
Dianne Hackborn | 19382ac | 2009-09-11 21:13:37 -0700 | [diff] [blame] | 183 | /** |
| 184 | * Special drawable that draws a wallpaper as fast as possible. Assumes |
| 185 | * no scaling or placement off (0,0) of the wallpaper (this should be done |
| 186 | * at the time the bitmap is loaded). |
| 187 | */ |
| 188 | static class FastBitmapDrawable extends Drawable { |
| 189 | private final Bitmap mBitmap; |
| 190 | private final int mWidth; |
| 191 | private final int mHeight; |
| 192 | private int mDrawLeft; |
| 193 | private int mDrawTop; |
Romain Guy | 407ec78 | 2011-08-24 17:06:58 -0700 | [diff] [blame] | 194 | private final Paint mPaint; |
Dianne Hackborn | 19382ac | 2009-09-11 21:13:37 -0700 | [diff] [blame] | 195 | |
| 196 | private FastBitmapDrawable(Bitmap bitmap) { |
| 197 | mBitmap = bitmap; |
| 198 | mWidth = bitmap.getWidth(); |
| 199 | mHeight = bitmap.getHeight(); |
Romain Guy | 407ec78 | 2011-08-24 17:06:58 -0700 | [diff] [blame] | 200 | |
Dianne Hackborn | 19382ac | 2009-09-11 21:13:37 -0700 | [diff] [blame] | 201 | setBounds(0, 0, mWidth, mHeight); |
Romain Guy | 407ec78 | 2011-08-24 17:06:58 -0700 | [diff] [blame] | 202 | |
| 203 | mPaint = new Paint(); |
| 204 | mPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC)); |
Dianne Hackborn | 19382ac | 2009-09-11 21:13:37 -0700 | [diff] [blame] | 205 | } |
| 206 | |
| 207 | @Override |
| 208 | public void draw(Canvas canvas) { |
Romain Guy | 407ec78 | 2011-08-24 17:06:58 -0700 | [diff] [blame] | 209 | canvas.drawBitmap(mBitmap, mDrawLeft, mDrawTop, mPaint); |
Dianne Hackborn | 19382ac | 2009-09-11 21:13:37 -0700 | [diff] [blame] | 210 | } |
| 211 | |
| 212 | @Override |
| 213 | public int getOpacity() { |
| 214 | return PixelFormat.OPAQUE; |
| 215 | } |
| 216 | |
| 217 | @Override |
| 218 | public void setBounds(int left, int top, int right, int bottom) { |
| 219 | mDrawLeft = left + (right-left - mWidth) / 2; |
| 220 | mDrawTop = top + (bottom-top - mHeight) / 2; |
| 221 | } |
| 222 | |
| 223 | @Override |
Dianne Hackborn | 19382ac | 2009-09-11 21:13:37 -0700 | [diff] [blame] | 224 | public void setAlpha(int alpha) { |
Romain Guy | 407ec78 | 2011-08-24 17:06:58 -0700 | [diff] [blame] | 225 | throw new UnsupportedOperationException("Not supported with this drawable"); |
Dianne Hackborn | 19382ac | 2009-09-11 21:13:37 -0700 | [diff] [blame] | 226 | } |
| 227 | |
| 228 | @Override |
Chris Craik | bd3bfc5 | 2015-03-02 10:43:29 -0800 | [diff] [blame] | 229 | public void setColorFilter(ColorFilter colorFilter) { |
Romain Guy | 407ec78 | 2011-08-24 17:06:58 -0700 | [diff] [blame] | 230 | throw new UnsupportedOperationException("Not supported with this drawable"); |
Dianne Hackborn | 19382ac | 2009-09-11 21:13:37 -0700 | [diff] [blame] | 231 | } |
| 232 | |
| 233 | @Override |
| 234 | public void setDither(boolean dither) { |
Romain Guy | 407ec78 | 2011-08-24 17:06:58 -0700 | [diff] [blame] | 235 | throw new UnsupportedOperationException("Not supported with this drawable"); |
Dianne Hackborn | 19382ac | 2009-09-11 21:13:37 -0700 | [diff] [blame] | 236 | } |
| 237 | |
| 238 | @Override |
| 239 | public void setFilterBitmap(boolean filter) { |
Romain Guy | 407ec78 | 2011-08-24 17:06:58 -0700 | [diff] [blame] | 240 | throw new UnsupportedOperationException("Not supported with this drawable"); |
Dianne Hackborn | 19382ac | 2009-09-11 21:13:37 -0700 | [diff] [blame] | 241 | } |
| 242 | |
| 243 | @Override |
| 244 | public int getIntrinsicWidth() { |
| 245 | return mWidth; |
| 246 | } |
| 247 | |
| 248 | @Override |
| 249 | public int getIntrinsicHeight() { |
| 250 | return mHeight; |
| 251 | } |
| 252 | |
| 253 | @Override |
| 254 | public int getMinimumWidth() { |
| 255 | return mWidth; |
| 256 | } |
| 257 | |
| 258 | @Override |
| 259 | public int getMinimumHeight() { |
| 260 | return mHeight; |
| 261 | } |
| 262 | } |
Oleksandr Peletskyi | f251981 | 2016-01-26 20:16:06 +0100 | [diff] [blame] | 263 | |
Dianne Hackborn | 8cc6a50 | 2009-08-05 21:29:42 -0700 | [diff] [blame] | 264 | static class Globals extends IWallpaperManagerCallback.Stub { |
| 265 | private IWallpaperManager mService; |
Dianne Hackborn | 284ac93 | 2009-08-28 10:34:25 -0700 | [diff] [blame] | 266 | private Bitmap mWallpaper; |
Dianne Hackborn | 19382ac | 2009-09-11 21:13:37 -0700 | [diff] [blame] | 267 | private Bitmap mDefaultWallpaper; |
Oleksandr Peletskyi | f251981 | 2016-01-26 20:16:06 +0100 | [diff] [blame] | 268 | |
Dianne Hackborn | 5e802fb | 2009-09-02 21:50:50 -0700 | [diff] [blame] | 269 | Globals(Looper looper) { |
Dianne Hackborn | 8cc6a50 | 2009-08-05 21:29:42 -0700 | [diff] [blame] | 270 | IBinder b = ServiceManager.getService(Context.WALLPAPER_SERVICE); |
| 271 | mService = IWallpaperManager.Stub.asInterface(b); |
Christopher Tate | be132e6 | 2016-02-10 12:59:49 -0800 | [diff] [blame] | 272 | forgetLoadedWallpaper(); |
Dianne Hackborn | 8cc6a50 | 2009-08-05 21:29:42 -0700 | [diff] [blame] | 273 | } |
Oleksandr Peletskyi | f251981 | 2016-01-26 20:16:06 +0100 | [diff] [blame] | 274 | |
Dianne Hackborn | 8cc6a50 | 2009-08-05 21:29:42 -0700 | [diff] [blame] | 275 | public void onWallpaperChanged() { |
| 276 | /* The wallpaper has changed but we shouldn't eagerly load the |
| 277 | * wallpaper as that would be inefficient. Reset the cached wallpaper |
| 278 | * to null so if the user requests the wallpaper again then we'll |
| 279 | * fetch it. |
| 280 | */ |
Christopher Tate | be132e6 | 2016-02-10 12:59:49 -0800 | [diff] [blame] | 281 | forgetLoadedWallpaper(); |
Dianne Hackborn | 8cc6a50 | 2009-08-05 21:29:42 -0700 | [diff] [blame] | 282 | } |
Adam Cohen | 041a0ba | 2011-11-09 20:10:27 -0800 | [diff] [blame] | 283 | |
Dianne Hackborn | 19382ac | 2009-09-11 21:13:37 -0700 | [diff] [blame] | 284 | public Bitmap peekWallpaperBitmap(Context context, boolean returnDefault) { |
Yorke Lee | dcd93cc | 2016-01-08 14:12:55 -0800 | [diff] [blame] | 285 | return peekWallpaperBitmap(context, returnDefault, context.getUserId()); |
| 286 | } |
| 287 | |
| 288 | public Bitmap peekWallpaperBitmap(Context context, boolean returnDefault, int userId) { |
Dianne Hackborn | 8cc6a50 | 2009-08-05 21:29:42 -0700 | [diff] [blame] | 289 | synchronized (this) { |
Benjamin Franz | f3ece36 | 2015-02-11 10:51:10 +0000 | [diff] [blame] | 290 | if (mService != null) { |
| 291 | try { |
| 292 | if (!mService.isWallpaperSupported(context.getOpPackageName())) { |
| 293 | return null; |
| 294 | } |
| 295 | } catch (RemoteException e) { |
| 296 | // Ignore |
| 297 | } |
| 298 | } |
Dianne Hackborn | 8cc6a50 | 2009-08-05 21:29:42 -0700 | [diff] [blame] | 299 | if (mWallpaper != null) { |
| 300 | return mWallpaper; |
| 301 | } |
Dianne Hackborn | 19382ac | 2009-09-11 21:13:37 -0700 | [diff] [blame] | 302 | if (mDefaultWallpaper != null) { |
| 303 | return mDefaultWallpaper; |
| 304 | } |
Dianne Hackborn | c9dbbe2 | 2009-11-11 22:50:37 -0800 | [diff] [blame] | 305 | mWallpaper = null; |
| 306 | try { |
Yorke Lee | dcd93cc | 2016-01-08 14:12:55 -0800 | [diff] [blame] | 307 | mWallpaper = getCurrentWallpaperLocked(userId); |
Dianne Hackborn | c9dbbe2 | 2009-11-11 22:50:37 -0800 | [diff] [blame] | 308 | } catch (OutOfMemoryError e) { |
| 309 | Log.w(TAG, "No memory load current wallpaper", e); |
| 310 | } |
Dianne Hackborn | ba39839 | 2011-08-01 16:11:57 -0700 | [diff] [blame] | 311 | if (returnDefault) { |
| 312 | if (mWallpaper == null) { |
| 313 | mDefaultWallpaper = getDefaultWallpaperLocked(context); |
| 314 | return mDefaultWallpaper; |
| 315 | } else { |
| 316 | mDefaultWallpaper = null; |
| 317 | } |
Dianne Hackborn | 19382ac | 2009-09-11 21:13:37 -0700 | [diff] [blame] | 318 | } |
Dianne Hackborn | 8cc6a50 | 2009-08-05 21:29:42 -0700 | [diff] [blame] | 319 | return mWallpaper; |
| 320 | } |
| 321 | } |
Dianne Hackborn | ba39839 | 2011-08-01 16:11:57 -0700 | [diff] [blame] | 322 | |
| 323 | public void forgetLoadedWallpaper() { |
| 324 | synchronized (this) { |
| 325 | mWallpaper = null; |
| 326 | mDefaultWallpaper = null; |
| 327 | } |
| 328 | } |
| 329 | |
Yorke Lee | dcd93cc | 2016-01-08 14:12:55 -0800 | [diff] [blame] | 330 | private Bitmap getCurrentWallpaperLocked(int userId) { |
Adam Lesinski | 2c8d67c | 2014-04-23 13:46:21 -0700 | [diff] [blame] | 331 | if (mService == null) { |
| 332 | Log.w(TAG, "WallpaperService not running"); |
| 333 | return null; |
| 334 | } |
| 335 | |
Dianne Hackborn | 8cc6a50 | 2009-08-05 21:29:42 -0700 | [diff] [blame] | 336 | try { |
Dianne Hackborn | 284ac93 | 2009-08-28 10:34:25 -0700 | [diff] [blame] | 337 | Bundle params = new Bundle(); |
Christopher Tate | be132e6 | 2016-02-10 12:59:49 -0800 | [diff] [blame] | 338 | ParcelFileDescriptor fd = mService.getWallpaper(this, FLAG_SET_SYSTEM, |
Yorke Lee | dcd93cc | 2016-01-08 14:12:55 -0800 | [diff] [blame] | 339 | params, userId); |
Dianne Hackborn | 8cc6a50 | 2009-08-05 21:29:42 -0700 | [diff] [blame] | 340 | if (fd != null) { |
Jeff Brown | 2457237 | 2011-06-09 19:05:15 -0700 | [diff] [blame] | 341 | try { |
| 342 | BitmapFactory.Options options = new BitmapFactory.Options(); |
Michael Jurka | 824a4b5 | 2013-12-18 17:10:16 +0100 | [diff] [blame] | 343 | return BitmapFactory.decodeFileDescriptor( |
Jeff Brown | 2457237 | 2011-06-09 19:05:15 -0700 | [diff] [blame] | 344 | fd.getFileDescriptor(), null, options); |
Jeff Brown | 2457237 | 2011-06-09 19:05:15 -0700 | [diff] [blame] | 345 | } catch (OutOfMemoryError e) { |
| 346 | Log.w(TAG, "Can't decode file", e); |
| 347 | } finally { |
Christopher Tate | ffa6a88 | 2015-12-11 15:08:46 -0800 | [diff] [blame] | 348 | IoUtils.closeQuietly(fd); |
Dianne Hackborn | 8cc6a50 | 2009-08-05 21:29:42 -0700 | [diff] [blame] | 349 | } |
Dianne Hackborn | 19382ac | 2009-09-11 21:13:37 -0700 | [diff] [blame] | 350 | } |
| 351 | } catch (RemoteException e) { |
Romain Guy | 407ec78 | 2011-08-24 17:06:58 -0700 | [diff] [blame] | 352 | // Ignore |
Dianne Hackborn | 19382ac | 2009-09-11 21:13:37 -0700 | [diff] [blame] | 353 | } |
| 354 | return null; |
| 355 | } |
Christopher Tate | ffa6a88 | 2015-12-11 15:08:46 -0800 | [diff] [blame] | 356 | |
Dianne Hackborn | 19382ac | 2009-09-11 21:13:37 -0700 | [diff] [blame] | 357 | private Bitmap getDefaultWallpaperLocked(Context context) { |
Adam Lesinski | e929bee | 2014-04-28 14:15:06 -0700 | [diff] [blame] | 358 | InputStream is = openDefaultWallpaper(context); |
Adam Lesinski | 2c8d67c | 2014-04-23 13:46:21 -0700 | [diff] [blame] | 359 | if (is != null) { |
| 360 | try { |
| 361 | BitmapFactory.Options options = new BitmapFactory.Options(); |
| 362 | return BitmapFactory.decodeStream(is, null, options); |
| 363 | } catch (OutOfMemoryError e) { |
| 364 | Log.w(TAG, "Can't decode stream", e); |
| 365 | } finally { |
Christopher Tate | ffa6a88 | 2015-12-11 15:08:46 -0800 | [diff] [blame] | 366 | IoUtils.closeQuietly(is); |
Dianne Hackborn | 8cc6a50 | 2009-08-05 21:29:42 -0700 | [diff] [blame] | 367 | } |
Dianne Hackborn | 8cc6a50 | 2009-08-05 21:29:42 -0700 | [diff] [blame] | 368 | } |
| 369 | return null; |
| 370 | } |
| 371 | } |
Oleksandr Peletskyi | f251981 | 2016-01-26 20:16:06 +0100 | [diff] [blame] | 372 | |
Romain Guy | 407ec78 | 2011-08-24 17:06:58 -0700 | [diff] [blame] | 373 | private static final Object sSync = new Object[0]; |
Dianne Hackborn | 8cc6a50 | 2009-08-05 21:29:42 -0700 | [diff] [blame] | 374 | private static Globals sGlobals; |
| 375 | |
Dianne Hackborn | 840c3a2 | 2009-09-02 17:35:17 -0700 | [diff] [blame] | 376 | static void initGlobals(Looper looper) { |
Romain Guy | 407ec78 | 2011-08-24 17:06:58 -0700 | [diff] [blame] | 377 | synchronized (sSync) { |
Dianne Hackborn | 8cc6a50 | 2009-08-05 21:29:42 -0700 | [diff] [blame] | 378 | if (sGlobals == null) { |
Dianne Hackborn | 5e802fb | 2009-09-02 21:50:50 -0700 | [diff] [blame] | 379 | sGlobals = new Globals(looper); |
Dianne Hackborn | 8cc6a50 | 2009-08-05 21:29:42 -0700 | [diff] [blame] | 380 | } |
Dianne Hackborn | 8cc6a50 | 2009-08-05 21:29:42 -0700 | [diff] [blame] | 381 | } |
| 382 | } |
Oleksandr Peletskyi | f251981 | 2016-01-26 20:16:06 +0100 | [diff] [blame] | 383 | |
Dianne Hackborn | 8cc6a50 | 2009-08-05 21:29:42 -0700 | [diff] [blame] | 384 | /*package*/ WallpaperManager(Context context, Handler handler) { |
| 385 | mContext = context; |
Dianne Hackborn | 840c3a2 | 2009-09-02 17:35:17 -0700 | [diff] [blame] | 386 | initGlobals(context.getMainLooper()); |
Dianne Hackborn | 8cc6a50 | 2009-08-05 21:29:42 -0700 | [diff] [blame] | 387 | } |
| 388 | |
| 389 | /** |
Dianne Hackborn | 4c62fc0 | 2009-08-08 20:40:27 -0700 | [diff] [blame] | 390 | * Retrieve a WallpaperManager associated with the given Context. |
| 391 | */ |
| 392 | public static WallpaperManager getInstance(Context context) { |
| 393 | return (WallpaperManager)context.getSystemService( |
| 394 | Context.WALLPAPER_SERVICE); |
| 395 | } |
Oleksandr Peletskyi | f251981 | 2016-01-26 20:16:06 +0100 | [diff] [blame] | 396 | |
Dianne Hackborn | 4c62fc0 | 2009-08-08 20:40:27 -0700 | [diff] [blame] | 397 | /** @hide */ |
| 398 | public IWallpaperManager getIWallpaperManager() { |
Dianne Hackborn | 840c3a2 | 2009-09-02 17:35:17 -0700 | [diff] [blame] | 399 | return sGlobals.mService; |
Dianne Hackborn | 4c62fc0 | 2009-08-08 20:40:27 -0700 | [diff] [blame] | 400 | } |
Oleksandr Peletskyi | f251981 | 2016-01-26 20:16:06 +0100 | [diff] [blame] | 401 | |
Dianne Hackborn | 4c62fc0 | 2009-08-08 20:40:27 -0700 | [diff] [blame] | 402 | /** |
Scott Main | 8b2e000 | 2009-09-29 18:17:31 -0700 | [diff] [blame] | 403 | * Retrieve the current system wallpaper; if |
Michael Jurka | b668d0b | 2013-10-04 15:11:05 -0700 | [diff] [blame] | 404 | * no wallpaper is set, the system built-in static wallpaper is returned. |
Scott Main | 8b2e000 | 2009-09-29 18:17:31 -0700 | [diff] [blame] | 405 | * This is returned as an |
| 406 | * abstract Drawable that you can install in a View to display whatever |
Oleksandr Peletskyi | f251981 | 2016-01-26 20:16:06 +0100 | [diff] [blame] | 407 | * wallpaper the user has currently set. |
Dianne Hackborn | 8cc6a50 | 2009-08-05 21:29:42 -0700 | [diff] [blame] | 408 | * |
| 409 | * @return Returns a Drawable object that will draw the wallpaper. |
| 410 | */ |
Dianne Hackborn | 4c62fc0 | 2009-08-08 20:40:27 -0700 | [diff] [blame] | 411 | public Drawable getDrawable() { |
Dianne Hackborn | 19382ac | 2009-09-11 21:13:37 -0700 | [diff] [blame] | 412 | Bitmap bm = sGlobals.peekWallpaperBitmap(mContext, true); |
| 413 | if (bm != null) { |
| 414 | Drawable dr = new BitmapDrawable(mContext.getResources(), bm); |
| 415 | dr.setDither(false); |
| 416 | return dr; |
| 417 | } |
| 418 | return null; |
Dianne Hackborn | 8cc6a50 | 2009-08-05 21:29:42 -0700 | [diff] [blame] | 419 | } |
| 420 | |
| 421 | /** |
Michael Jurka | b668d0b | 2013-10-04 15:11:05 -0700 | [diff] [blame] | 422 | * Returns a drawable for the system built-in static wallpaper . |
| 423 | * |
| 424 | */ |
| 425 | public Drawable getBuiltInDrawable() { |
| 426 | return getBuiltInDrawable(0, 0, false, 0, 0); |
| 427 | } |
| 428 | |
| 429 | /** |
| 430 | * Returns a drawable for the system built-in static wallpaper. Based on the parameters, the |
| 431 | * drawable can be cropped and scaled |
| 432 | * |
| 433 | * @param outWidth The width of the returned drawable |
| 434 | * @param outWidth The height of the returned drawable |
| 435 | * @param scaleToFit If true, scale the wallpaper down rather than just cropping it |
| 436 | * @param horizontalAlignment A float value between 0 and 1 specifying where to crop the image; |
| 437 | * 0 for left-aligned, 0.5 for horizontal center-aligned, and 1 for right-aligned |
| 438 | * @param verticalAlignment A float value between 0 and 1 specifying where to crop the image; |
| 439 | * 0 for top-aligned, 0.5 for vertical center-aligned, and 1 for bottom-aligned |
| 440 | * |
| 441 | */ |
| 442 | public Drawable getBuiltInDrawable(int outWidth, int outHeight, |
| 443 | boolean scaleToFit, float horizontalAlignment, float verticalAlignment) { |
| 444 | if (sGlobals.mService == null) { |
| 445 | Log.w(TAG, "WallpaperService not running"); |
| 446 | return null; |
| 447 | } |
| 448 | Resources resources = mContext.getResources(); |
| 449 | horizontalAlignment = Math.max(0, Math.min(1, horizontalAlignment)); |
| 450 | verticalAlignment = Math.max(0, Math.min(1, verticalAlignment)); |
| 451 | |
Jeff Sharkey | 28f0877 | 2014-04-16 09:41:58 -0700 | [diff] [blame] | 452 | InputStream is = new BufferedInputStream(openDefaultWallpaper(mContext)); |
Michael Jurka | b668d0b | 2013-10-04 15:11:05 -0700 | [diff] [blame] | 453 | |
| 454 | if (is == null) { |
| 455 | Log.e(TAG, "default wallpaper input stream is null"); |
| 456 | return null; |
| 457 | } else { |
| 458 | if (outWidth <= 0 || outHeight <= 0) { |
| 459 | Bitmap fullSize = BitmapFactory.decodeStream(is, null, null); |
| 460 | return new BitmapDrawable(resources, fullSize); |
| 461 | } else { |
| 462 | int inWidth; |
| 463 | int inHeight; |
| 464 | { |
| 465 | BitmapFactory.Options options = new BitmapFactory.Options(); |
| 466 | options.inJustDecodeBounds = true; |
| 467 | BitmapFactory.decodeStream(is, null, options); |
| 468 | if (options.outWidth != 0 && options.outHeight != 0) { |
| 469 | inWidth = options.outWidth; |
| 470 | inHeight = options.outHeight; |
| 471 | } else { |
| 472 | Log.e(TAG, "default wallpaper dimensions are 0"); |
| 473 | return null; |
| 474 | } |
| 475 | } |
| 476 | |
Jeff Sharkey | 28f0877 | 2014-04-16 09:41:58 -0700 | [diff] [blame] | 477 | is = new BufferedInputStream(openDefaultWallpaper(mContext)); |
Michael Jurka | b668d0b | 2013-10-04 15:11:05 -0700 | [diff] [blame] | 478 | |
| 479 | RectF cropRectF; |
| 480 | |
| 481 | outWidth = Math.min(inWidth, outWidth); |
| 482 | outHeight = Math.min(inHeight, outHeight); |
| 483 | if (scaleToFit) { |
| 484 | cropRectF = getMaxCropRect(inWidth, inHeight, outWidth, outHeight, |
| 485 | horizontalAlignment, verticalAlignment); |
| 486 | } else { |
| 487 | float left = (inWidth - outWidth) * horizontalAlignment; |
| 488 | float right = left + outWidth; |
| 489 | float top = (inHeight - outHeight) * verticalAlignment; |
| 490 | float bottom = top + outHeight; |
Michael Jurka | 0539153 | 2013-10-14 20:44:42 -0700 | [diff] [blame] | 491 | cropRectF = new RectF(left, top, right, bottom); |
Michael Jurka | b668d0b | 2013-10-04 15:11:05 -0700 | [diff] [blame] | 492 | } |
| 493 | Rect roundedTrueCrop = new Rect(); |
| 494 | cropRectF.roundOut(roundedTrueCrop); |
| 495 | |
| 496 | if (roundedTrueCrop.width() <= 0 || roundedTrueCrop.height() <= 0) { |
| 497 | Log.w(TAG, "crop has bad values for full size image"); |
| 498 | return null; |
| 499 | } |
| 500 | |
| 501 | // See how much we're reducing the size of the image |
| 502 | int scaleDownSampleSize = Math.min(roundedTrueCrop.width() / outWidth, |
| 503 | roundedTrueCrop.height() / outHeight); |
| 504 | |
| 505 | // Attempt to open a region decoder |
| 506 | BitmapRegionDecoder decoder = null; |
| 507 | try { |
| 508 | decoder = BitmapRegionDecoder.newInstance(is, true); |
| 509 | } catch (IOException e) { |
| 510 | Log.w(TAG, "cannot open region decoder for default wallpaper"); |
| 511 | } |
| 512 | |
| 513 | Bitmap crop = null; |
| 514 | if (decoder != null) { |
| 515 | // Do region decoding to get crop bitmap |
| 516 | BitmapFactory.Options options = new BitmapFactory.Options(); |
| 517 | if (scaleDownSampleSize > 1) { |
| 518 | options.inSampleSize = scaleDownSampleSize; |
| 519 | } |
| 520 | crop = decoder.decodeRegion(roundedTrueCrop, options); |
| 521 | decoder.recycle(); |
| 522 | } |
| 523 | |
| 524 | if (crop == null) { |
| 525 | // BitmapRegionDecoder has failed, try to crop in-memory |
Jeff Sharkey | 28f0877 | 2014-04-16 09:41:58 -0700 | [diff] [blame] | 526 | is = new BufferedInputStream(openDefaultWallpaper(mContext)); |
Michael Jurka | b668d0b | 2013-10-04 15:11:05 -0700 | [diff] [blame] | 527 | Bitmap fullSize = null; |
| 528 | if (is != null) { |
| 529 | BitmapFactory.Options options = new BitmapFactory.Options(); |
| 530 | if (scaleDownSampleSize > 1) { |
| 531 | options.inSampleSize = scaleDownSampleSize; |
| 532 | } |
| 533 | fullSize = BitmapFactory.decodeStream(is, null, options); |
| 534 | } |
| 535 | if (fullSize != null) { |
| 536 | crop = Bitmap.createBitmap(fullSize, roundedTrueCrop.left, |
| 537 | roundedTrueCrop.top, roundedTrueCrop.width(), |
| 538 | roundedTrueCrop.height()); |
| 539 | } |
| 540 | } |
| 541 | |
| 542 | if (crop == null) { |
| 543 | Log.w(TAG, "cannot decode default wallpaper"); |
| 544 | return null; |
| 545 | } |
| 546 | |
| 547 | // Scale down if necessary |
| 548 | if (outWidth > 0 && outHeight > 0 && |
| 549 | (crop.getWidth() != outWidth || crop.getHeight() != outHeight)) { |
| 550 | Matrix m = new Matrix(); |
| 551 | RectF cropRect = new RectF(0, 0, crop.getWidth(), crop.getHeight()); |
| 552 | RectF returnRect = new RectF(0, 0, outWidth, outHeight); |
| 553 | m.setRectToRect(cropRect, returnRect, Matrix.ScaleToFit.FILL); |
| 554 | Bitmap tmp = Bitmap.createBitmap((int) returnRect.width(), |
| 555 | (int) returnRect.height(), Bitmap.Config.ARGB_8888); |
| 556 | if (tmp != null) { |
| 557 | Canvas c = new Canvas(tmp); |
| 558 | Paint p = new Paint(); |
| 559 | p.setFilterBitmap(true); |
| 560 | c.drawBitmap(crop, m, p); |
| 561 | crop = tmp; |
| 562 | } |
| 563 | } |
| 564 | |
| 565 | return new BitmapDrawable(resources, crop); |
| 566 | } |
| 567 | } |
| 568 | } |
| 569 | |
| 570 | private static RectF getMaxCropRect(int inWidth, int inHeight, int outWidth, int outHeight, |
| 571 | float horizontalAlignment, float verticalAlignment) { |
| 572 | RectF cropRect = new RectF(); |
| 573 | // Get a crop rect that will fit this |
| 574 | if (inWidth / (float) inHeight > outWidth / (float) outHeight) { |
| 575 | cropRect.top = 0; |
| 576 | cropRect.bottom = inHeight; |
| 577 | float cropWidth = outWidth * (inHeight / (float) outHeight); |
| 578 | cropRect.left = (inWidth - cropWidth) * horizontalAlignment; |
| 579 | cropRect.right = cropRect.left + cropWidth; |
| 580 | } else { |
| 581 | cropRect.left = 0; |
| 582 | cropRect.right = inWidth; |
| 583 | float cropHeight = outHeight * (inWidth / (float) outWidth); |
| 584 | cropRect.top = (inHeight - cropHeight) * verticalAlignment; |
| 585 | cropRect.bottom = cropRect.top + cropHeight; |
| 586 | } |
| 587 | return cropRect; |
| 588 | } |
| 589 | |
| 590 | /** |
Scott Main | 8b2e000 | 2009-09-29 18:17:31 -0700 | [diff] [blame] | 591 | * Retrieve the current system wallpaper; if there is no wallpaper set, |
| 592 | * a null pointer is returned. This is returned as an |
Dianne Hackborn | 8cc6a50 | 2009-08-05 21:29:42 -0700 | [diff] [blame] | 593 | * abstract Drawable that you can install in a View to display whatever |
Oleksandr Peletskyi | f251981 | 2016-01-26 20:16:06 +0100 | [diff] [blame] | 594 | * wallpaper the user has currently set. |
Dianne Hackborn | 8cc6a50 | 2009-08-05 21:29:42 -0700 | [diff] [blame] | 595 | * |
| 596 | * @return Returns a Drawable object that will draw the wallpaper or a |
| 597 | * null pointer if these is none. |
| 598 | */ |
Dianne Hackborn | 4c62fc0 | 2009-08-08 20:40:27 -0700 | [diff] [blame] | 599 | public Drawable peekDrawable() { |
Dianne Hackborn | 19382ac | 2009-09-11 21:13:37 -0700 | [diff] [blame] | 600 | Bitmap bm = sGlobals.peekWallpaperBitmap(mContext, false); |
| 601 | if (bm != null) { |
| 602 | Drawable dr = new BitmapDrawable(mContext.getResources(), bm); |
| 603 | dr.setDither(false); |
| 604 | return dr; |
| 605 | } |
| 606 | return null; |
| 607 | } |
| 608 | |
| 609 | /** |
Scott Main | 8b2e000 | 2009-09-29 18:17:31 -0700 | [diff] [blame] | 610 | * Like {@link #getDrawable()}, but the returned Drawable has a number |
| 611 | * of limitations to reduce its overhead as much as possible. It will |
| 612 | * never scale the wallpaper (only centering it if the requested bounds |
| 613 | * do match the bitmap bounds, which should not be typical), doesn't |
| 614 | * allow setting an alpha, color filter, or other attributes, etc. The |
| 615 | * bounds of the returned drawable will be initialized to the same bounds |
| 616 | * as the wallpaper, so normally you will not need to touch it. The |
| 617 | * drawable also assumes that it will be used in a context running in |
| 618 | * the same density as the screen (not in density compatibility mode). |
Dianne Hackborn | 19382ac | 2009-09-11 21:13:37 -0700 | [diff] [blame] | 619 | * |
| 620 | * @return Returns a Drawable object that will draw the wallpaper. |
| 621 | */ |
| 622 | public Drawable getFastDrawable() { |
| 623 | Bitmap bm = sGlobals.peekWallpaperBitmap(mContext, true); |
| 624 | if (bm != null) { |
Romain Guy | 407ec78 | 2011-08-24 17:06:58 -0700 | [diff] [blame] | 625 | return new FastBitmapDrawable(bm); |
Dianne Hackborn | 19382ac | 2009-09-11 21:13:37 -0700 | [diff] [blame] | 626 | } |
| 627 | return null; |
| 628 | } |
| 629 | |
| 630 | /** |
Scott Main | 8b2e000 | 2009-09-29 18:17:31 -0700 | [diff] [blame] | 631 | * Like {@link #getFastDrawable()}, but if there is no wallpaper set, |
| 632 | * a null pointer is returned. |
Dianne Hackborn | 19382ac | 2009-09-11 21:13:37 -0700 | [diff] [blame] | 633 | * |
| 634 | * @return Returns an optimized Drawable object that will draw the |
| 635 | * wallpaper or a null pointer if these is none. |
| 636 | */ |
| 637 | public Drawable peekFastDrawable() { |
Christopher Tate | be132e6 | 2016-02-10 12:59:49 -0800 | [diff] [blame] | 638 | Bitmap bm = sGlobals.peekWallpaperBitmap(mContext, false); |
Dianne Hackborn | 19382ac | 2009-09-11 21:13:37 -0700 | [diff] [blame] | 639 | if (bm != null) { |
Romain Guy | 407ec78 | 2011-08-24 17:06:58 -0700 | [diff] [blame] | 640 | return new FastBitmapDrawable(bm); |
Dianne Hackborn | 19382ac | 2009-09-11 21:13:37 -0700 | [diff] [blame] | 641 | } |
| 642 | return null; |
Dianne Hackborn | 8cc6a50 | 2009-08-05 21:29:42 -0700 | [diff] [blame] | 643 | } |
| 644 | |
| 645 | /** |
Romain Guy | 407ec78 | 2011-08-24 17:06:58 -0700 | [diff] [blame] | 646 | * Like {@link #getDrawable()} but returns a Bitmap. |
Oleksandr Peletskyi | f251981 | 2016-01-26 20:16:06 +0100 | [diff] [blame] | 647 | * |
Romain Guy | 407ec78 | 2011-08-24 17:06:58 -0700 | [diff] [blame] | 648 | * @hide |
| 649 | */ |
| 650 | public Bitmap getBitmap() { |
Yorke Lee | dcd93cc | 2016-01-08 14:12:55 -0800 | [diff] [blame] | 651 | return getBitmapAsUser(mContext.getUserId()); |
| 652 | } |
| 653 | |
| 654 | /** |
| 655 | * Like {@link #getDrawable()} but returns a Bitmap for the provided user. |
| 656 | * |
| 657 | * @hide |
| 658 | */ |
| 659 | public Bitmap getBitmapAsUser(int userId) { |
| 660 | return sGlobals.peekWallpaperBitmap(mContext, true, userId); |
Romain Guy | 407ec78 | 2011-08-24 17:06:58 -0700 | [diff] [blame] | 661 | } |
| 662 | |
| 663 | /** |
Christopher Tate | be132e6 | 2016-02-10 12:59:49 -0800 | [diff] [blame] | 664 | * Get an open, readable file descriptor to the given wallpaper image file. |
| 665 | * The callee is resopnsible for closing the fd when done ingesting the file. |
| 666 | * |
| 667 | * <p>If no lock-specific wallpaper has been configured for the given user, then |
| 668 | * this method will return {@code null} when requesting {@link #FLAG_SET_LOCK} rather than |
| 669 | * returning the system wallpaper's image file. |
| 670 | */ |
| 671 | public ParcelFileDescriptor getWallpaperFile(int which) { |
| 672 | return getWallpaperFile(which, mContext.getUserId()); |
| 673 | } |
| 674 | |
| 675 | /** |
| 676 | * Version of {@link #getWallpaperFile(int)} that can access the wallpaper data |
| 677 | * for a given user. The caller must hold the INTERACT_ACROSS_USERS_FULL |
| 678 | * permission to access another user's wallpaper data. |
| 679 | * @hide |
| 680 | */ |
| 681 | public ParcelFileDescriptor getWallpaperFile(int which, int userId) { |
| 682 | if (which != FLAG_SET_SYSTEM && which != FLAG_SET_LOCK) { |
| 683 | throw new IllegalArgumentException("Must request exactly one kind of wallpaper"); |
| 684 | } |
| 685 | |
| 686 | if (sGlobals.mService == null) { |
| 687 | Log.w(TAG, "WallpaperService not running"); |
| 688 | return null; |
| 689 | } else { |
| 690 | try { |
| 691 | Bundle outParams = new Bundle(); |
| 692 | return sGlobals.mService.getWallpaper(null, which, outParams, userId); |
| 693 | } catch (RemoteException e) { |
| 694 | return null; |
| 695 | } |
| 696 | } |
| 697 | } |
| 698 | |
| 699 | /** |
Dianne Hackborn | ba39839 | 2011-08-01 16:11:57 -0700 | [diff] [blame] | 700 | * Remove all internal references to the last loaded wallpaper. Useful |
| 701 | * for apps that want to reduce memory usage when they only temporarily |
| 702 | * need to have the wallpaper. After calling, the next request for the |
| 703 | * wallpaper will require reloading it again from disk. |
| 704 | */ |
| 705 | public void forgetLoadedWallpaper() { |
Christopher Tate | be132e6 | 2016-02-10 12:59:49 -0800 | [diff] [blame] | 706 | sGlobals.forgetLoadedWallpaper(); |
Dianne Hackborn | ba39839 | 2011-08-01 16:11:57 -0700 | [diff] [blame] | 707 | } |
| 708 | |
| 709 | /** |
Dianne Hackborn | eb03465 | 2009-09-07 00:49:58 -0700 | [diff] [blame] | 710 | * If the current wallpaper is a live wallpaper component, return the |
| 711 | * information about that wallpaper. Otherwise, if it is a static image, |
| 712 | * simply return null. |
Dianne Hackborn | eb03465 | 2009-09-07 00:49:58 -0700 | [diff] [blame] | 713 | */ |
| 714 | public WallpaperInfo getWallpaperInfo() { |
| 715 | try { |
Mike Lockwood | c067c9c | 2011-10-31 12:50:12 -0400 | [diff] [blame] | 716 | if (sGlobals.mService == null) { |
| 717 | Log.w(TAG, "WallpaperService not running"); |
| 718 | return null; |
| 719 | } else { |
| 720 | return sGlobals.mService.getWallpaperInfo(); |
| 721 | } |
Dianne Hackborn | eb03465 | 2009-09-07 00:49:58 -0700 | [diff] [blame] | 722 | } catch (RemoteException e) { |
| 723 | return null; |
| 724 | } |
| 725 | } |
Michael Jurka | e8d1bf7 | 2013-09-09 15:58:54 +0200 | [diff] [blame] | 726 | |
| 727 | /** |
| 728 | * Gets an Intent that will launch an activity that crops the given |
| 729 | * image and sets the device's wallpaper. If there is a default HOME activity |
| 730 | * that supports cropping wallpapers, it will be preferred as the default. |
Ying Wang | 930d4e5 | 2013-09-14 11:57:17 -0700 | [diff] [blame] | 731 | * 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] | 732 | * intent. |
Adam Lesinski | bba72d1 | 2013-10-11 18:10:56 -0700 | [diff] [blame] | 733 | * |
| 734 | * @param imageUri The image URI that will be set in the intent. The must be a content |
| 735 | * URI and its provider must resolve its type to "image/*" |
| 736 | * |
| 737 | * @throws IllegalArgumentException if the URI is not a content URI or its MIME type is |
| 738 | * not "image/*" |
Michael Jurka | e8d1bf7 | 2013-09-09 15:58:54 +0200 | [diff] [blame] | 739 | */ |
| 740 | public Intent getCropAndSetWallpaperIntent(Uri imageUri) { |
Allen Shen | 1c0f21e | 2014-04-29 16:16:29 +0800 | [diff] [blame] | 741 | if (imageUri == null) { |
| 742 | throw new IllegalArgumentException("Image URI must not be null"); |
| 743 | } |
| 744 | |
Adam Lesinski | bba72d1 | 2013-10-11 18:10:56 -0700 | [diff] [blame] | 745 | if (!ContentResolver.SCHEME_CONTENT.equals(imageUri.getScheme())) { |
| 746 | throw new IllegalArgumentException("Image URI must be of the " |
| 747 | + ContentResolver.SCHEME_CONTENT + " scheme type"); |
| 748 | } |
| 749 | |
Michael Jurka | e8d1bf7 | 2013-09-09 15:58:54 +0200 | [diff] [blame] | 750 | final PackageManager packageManager = mContext.getPackageManager(); |
| 751 | Intent cropAndSetWallpaperIntent = |
| 752 | new Intent(ACTION_CROP_AND_SET_WALLPAPER, imageUri); |
| 753 | cropAndSetWallpaperIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); |
| 754 | |
| 755 | // Find out if the default HOME activity supports CROP_AND_SET_WALLPAPER |
| 756 | Intent homeIntent = new Intent(Intent.ACTION_MAIN).addCategory(Intent.CATEGORY_HOME); |
| 757 | ResolveInfo resolvedHome = packageManager.resolveActivity(homeIntent, |
| 758 | PackageManager.MATCH_DEFAULT_ONLY); |
| 759 | if (resolvedHome != null) { |
| 760 | cropAndSetWallpaperIntent.setPackage(resolvedHome.activityInfo.packageName); |
| 761 | |
| 762 | List<ResolveInfo> cropAppList = packageManager.queryIntentActivities( |
| 763 | cropAndSetWallpaperIntent, 0); |
| 764 | if (cropAppList.size() > 0) { |
| 765 | return cropAndSetWallpaperIntent; |
| 766 | } |
| 767 | } |
| 768 | |
| 769 | // fallback crop activity |
| 770 | cropAndSetWallpaperIntent.setPackage("com.android.wallpapercropper"); |
Adam Lesinski | bba72d1 | 2013-10-11 18:10:56 -0700 | [diff] [blame] | 771 | List<ResolveInfo> cropAppList = packageManager.queryIntentActivities( |
| 772 | cropAndSetWallpaperIntent, 0); |
| 773 | if (cropAppList.size() > 0) { |
| 774 | return cropAndSetWallpaperIntent; |
| 775 | } |
| 776 | // If the URI is not of the right type, or for some reason the system wallpaper |
| 777 | // cropper doesn't exist, return null |
| 778 | throw new IllegalArgumentException("Cannot use passed URI to set wallpaper; " + |
| 779 | "check that the type returned by ContentProvider matches image/*"); |
Michael Jurka | e8d1bf7 | 2013-09-09 15:58:54 +0200 | [diff] [blame] | 780 | } |
| 781 | |
Dianne Hackborn | eb03465 | 2009-09-07 00:49:58 -0700 | [diff] [blame] | 782 | /** |
Dianne Hackborn | 8cc6a50 | 2009-08-05 21:29:42 -0700 | [diff] [blame] | 783 | * Change the current system wallpaper to the bitmap in the given resource. |
| 784 | * The resource is opened as a raw data stream and copied into the |
| 785 | * wallpaper; it must be a valid PNG or JPEG image. On success, the intent |
| 786 | * {@link Intent#ACTION_WALLPAPER_CHANGED} is broadcast. |
| 787 | * |
Nicolas Falliere | 9530e3a | 2012-06-18 17:21:06 -0700 | [diff] [blame] | 788 | * <p>This method requires the caller to hold the permission |
| 789 | * {@link android.Manifest.permission#SET_WALLPAPER}. |
| 790 | * |
Dianne Hackborn | 8cc6a50 | 2009-08-05 21:29:42 -0700 | [diff] [blame] | 791 | * @param resid The bitmap to save. |
| 792 | * |
Michael Jurka | b668d0b | 2013-10-04 15:11:05 -0700 | [diff] [blame] | 793 | * @throws IOException If an error occurs reverting to the built-in |
Dianne Hackborn | 8cc6a50 | 2009-08-05 21:29:42 -0700 | [diff] [blame] | 794 | * wallpaper. |
| 795 | */ |
Tor Norbye | 7b9c912 | 2013-05-30 16:48:33 -0700 | [diff] [blame] | 796 | public void setResource(@RawRes int resid) throws IOException { |
Christopher Tate | ad3c259 | 2016-01-20 18:13:17 -0800 | [diff] [blame] | 797 | setResource(resid, FLAG_SET_SYSTEM); |
| 798 | } |
| 799 | |
| 800 | /** |
| 801 | * Version of {@link #setResource(int)} that takes an optional Bundle for returning |
| 802 | * metadata about the operation to the caller. |
| 803 | * |
| 804 | * @param resid |
| 805 | * @param which Flags indicating which wallpaper(s) to configure with the new imagery. |
| 806 | * |
| 807 | * @see #FLAG_SET_LOCK |
| 808 | * @see #FLAG_SET_SYSTEM |
| 809 | * |
| 810 | * @return An integer ID assigned to the newly active wallpaper; or zero on failure. |
| 811 | * |
| 812 | * @throws IOException |
| 813 | */ |
| 814 | public int setResource(@RawRes int resid, @SetWallpaperFlags int which) |
| 815 | throws IOException { |
Mike Lockwood | c067c9c | 2011-10-31 12:50:12 -0400 | [diff] [blame] | 816 | if (sGlobals.mService == null) { |
| 817 | Log.w(TAG, "WallpaperService not running"); |
Christopher Tate | ad3c259 | 2016-01-20 18:13:17 -0800 | [diff] [blame] | 818 | return 0; |
Mike Lockwood | c067c9c | 2011-10-31 12:50:12 -0400 | [diff] [blame] | 819 | } |
Christopher Tate | ad3c259 | 2016-01-20 18:13:17 -0800 | [diff] [blame] | 820 | final Bundle result = new Bundle(); |
Christopher Tate | 1e1e2e0 | 2016-01-25 15:34:36 -0800 | [diff] [blame] | 821 | final WallpaperSetCompletion completion = new WallpaperSetCompletion(); |
Dianne Hackborn | 8cc6a50 | 2009-08-05 21:29:42 -0700 | [diff] [blame] | 822 | try { |
| 823 | Resources resources = mContext.getResources(); |
| 824 | /* Set the wallpaper to the default values */ |
Dianne Hackborn | 840c3a2 | 2009-09-02 17:35:17 -0700 | [diff] [blame] | 825 | ParcelFileDescriptor fd = sGlobals.mService.setWallpaper( |
Christopher Tate | ad3c259 | 2016-01-20 18:13:17 -0800 | [diff] [blame] | 826 | "res:" + resources.getResourceName(resid), |
Christopher Tate | 1e1e2e0 | 2016-01-25 15:34:36 -0800 | [diff] [blame] | 827 | mContext.getOpPackageName(), null, result, which, completion); |
Dianne Hackborn | 8cc6a50 | 2009-08-05 21:29:42 -0700 | [diff] [blame] | 828 | if (fd != null) { |
| 829 | FileOutputStream fos = null; |
Christopher Tate | 1e1e2e0 | 2016-01-25 15:34:36 -0800 | [diff] [blame] | 830 | boolean ok = false; |
Dianne Hackborn | 8cc6a50 | 2009-08-05 21:29:42 -0700 | [diff] [blame] | 831 | try { |
| 832 | fos = new ParcelFileDescriptor.AutoCloseOutputStream(fd); |
Christopher Tate | ad3c259 | 2016-01-20 18:13:17 -0800 | [diff] [blame] | 833 | copyStreamToWallpaperFile(resources.openRawResource(resid), fos); |
Christopher Tate | 1e1e2e0 | 2016-01-25 15:34:36 -0800 | [diff] [blame] | 834 | // The 'close()' is the trigger for any server-side image manipulation, |
| 835 | // so we must do that before waiting for completion. |
| 836 | fos.close(); |
| 837 | completion.waitForCompletion(); |
Dianne Hackborn | 8cc6a50 | 2009-08-05 21:29:42 -0700 | [diff] [blame] | 838 | } finally { |
Christopher Tate | 1e1e2e0 | 2016-01-25 15:34:36 -0800 | [diff] [blame] | 839 | // Might be redundant but completion shouldn't wait unless the write |
| 840 | // succeeded; this is a fallback if it threw past the close+wait. |
Christopher Tate | ffa6a88 | 2015-12-11 15:08:46 -0800 | [diff] [blame] | 841 | IoUtils.closeQuietly(fos); |
Dianne Hackborn | 8cc6a50 | 2009-08-05 21:29:42 -0700 | [diff] [blame] | 842 | } |
| 843 | } |
| 844 | } catch (RemoteException e) { |
Romain Guy | 407ec78 | 2011-08-24 17:06:58 -0700 | [diff] [blame] | 845 | // Ignore |
Dianne Hackborn | 8cc6a50 | 2009-08-05 21:29:42 -0700 | [diff] [blame] | 846 | } |
Christopher Tate | ad3c259 | 2016-01-20 18:13:17 -0800 | [diff] [blame] | 847 | return result.getInt(EXTRA_NEW_WALLPAPER_ID, 0); |
Dianne Hackborn | 8cc6a50 | 2009-08-05 21:29:42 -0700 | [diff] [blame] | 848 | } |
Christopher Tate | ffa6a88 | 2015-12-11 15:08:46 -0800 | [diff] [blame] | 849 | |
Dianne Hackborn | 8cc6a50 | 2009-08-05 21:29:42 -0700 | [diff] [blame] | 850 | /** |
| 851 | * Change the current system wallpaper to a bitmap. The given bitmap is |
| 852 | * converted to a PNG and stored as the wallpaper. On success, the intent |
| 853 | * {@link Intent#ACTION_WALLPAPER_CHANGED} is broadcast. |
| 854 | * |
Christopher Tate | ffa6a88 | 2015-12-11 15:08:46 -0800 | [diff] [blame] | 855 | * <p>This method is equivalent to calling |
| 856 | * {@link #setBitmap(Bitmap, Rect, boolean)} and passing {@code null} for the |
| 857 | * {@code visibleCrop} rectangle and {@code true} for the {@code allowBackup} |
| 858 | * parameter. |
| 859 | * |
Nicolas Falliere | 9530e3a | 2012-06-18 17:21:06 -0700 | [diff] [blame] | 860 | * <p>This method requires the caller to hold the permission |
| 861 | * {@link android.Manifest.permission#SET_WALLPAPER}. |
| 862 | * |
Christopher Tate | ad3c259 | 2016-01-20 18:13:17 -0800 | [diff] [blame] | 863 | * @param bitmap The bitmap to be used as the new system wallpaper. |
Dianne Hackborn | 8cc6a50 | 2009-08-05 21:29:42 -0700 | [diff] [blame] | 864 | * |
Christopher Tate | ffa6a88 | 2015-12-11 15:08:46 -0800 | [diff] [blame] | 865 | * @throws IOException If an error occurs when attempting to set the wallpaper |
| 866 | * to the provided image. |
Dianne Hackborn | 8cc6a50 | 2009-08-05 21:29:42 -0700 | [diff] [blame] | 867 | */ |
Dianne Hackborn | 4c62fc0 | 2009-08-08 20:40:27 -0700 | [diff] [blame] | 868 | public void setBitmap(Bitmap bitmap) throws IOException { |
Christopher Tate | ffa6a88 | 2015-12-11 15:08:46 -0800 | [diff] [blame] | 869 | setBitmap(bitmap, null, true); |
| 870 | } |
| 871 | |
| 872 | /** |
| 873 | * Change the current system wallpaper to a bitmap, specifying a hint about |
| 874 | * which subrectangle of the full image is to be visible. The OS will then |
| 875 | * try to best present the given portion of the full image as the static system |
| 876 | * wallpaper image. On success, the intent |
| 877 | * {@link Intent#ACTION_WALLPAPER_CHANGED} is broadcast. |
| 878 | * |
| 879 | * <p>Passing {@code null} as the {@code visibleHint} parameter is equivalent to |
| 880 | * passing (0, 0, {@code fullImage.getWidth()}, {@code fullImage.getHeight()}). |
| 881 | * |
| 882 | * <p>This method requires the caller to hold the permission |
| 883 | * {@link android.Manifest.permission#SET_WALLPAPER}. |
| 884 | * |
Christopher Tate | ad3c259 | 2016-01-20 18:13:17 -0800 | [diff] [blame] | 885 | * @param fullImage A bitmap that will supply the wallpaper imagery. |
Christopher Tate | ffa6a88 | 2015-12-11 15:08:46 -0800 | [diff] [blame] | 886 | * @param visibleCropHint The rectangular subregion of {@code fullImage} that should be |
| 887 | * displayed as wallpaper. Passing {@code null} for this parameter means that |
| 888 | * 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] | 889 | * aspect ratios, etc. |
Christopher Tate | ffa6a88 | 2015-12-11 15:08:46 -0800 | [diff] [blame] | 890 | * @param allowBackup {@code true} if the OS is permitted to back up this wallpaper |
| 891 | * image for restore to a future device; {@code false} otherwise. |
| 892 | * |
Christopher Tate | ad3c259 | 2016-01-20 18:13:17 -0800 | [diff] [blame] | 893 | * @return An integer ID assigned to the newly active wallpaper; or zero on failure. |
| 894 | * |
Christopher Tate | ffa6a88 | 2015-12-11 15:08:46 -0800 | [diff] [blame] | 895 | * @throws IOException If an error occurs when attempting to set the wallpaper |
| 896 | * to the provided image. |
| 897 | * @throws IllegalArgumentException If the {@code visibleCropHint} rectangle is |
| 898 | * empty or invalid. |
| 899 | */ |
Christopher Tate | ad3c259 | 2016-01-20 18:13:17 -0800 | [diff] [blame] | 900 | public int setBitmap(Bitmap fullImage, Rect visibleCropHint, boolean allowBackup) |
| 901 | throws IOException { |
| 902 | return setBitmap(fullImage, visibleCropHint, allowBackup, FLAG_SET_SYSTEM); |
| 903 | } |
| 904 | |
| 905 | /** |
| 906 | /** |
| 907 | * Version of {@link #setBitmap(Bitmap, Rect, boolean)} that allows the caller |
| 908 | * to specify which of the supported wallpaper categories to set. |
| 909 | * |
| 910 | * @param fullImage A bitmap that will supply the wallpaper imagery. |
| 911 | * @param visibleCropHint The rectangular subregion of {@code fullImage} that should be |
| 912 | * displayed as wallpaper. Passing {@code null} for this parameter means that |
| 913 | * the full image should be displayed if possible given the image's and device's |
| 914 | * aspect ratios, etc. |
| 915 | * @param allowBackup {@code true} if the OS is permitted to back up this wallpaper |
| 916 | * image for restore to a future device; {@code false} otherwise. |
| 917 | * @param which Flags indicating which wallpaper(s) to configure with the new imagery. |
| 918 | * |
| 919 | * @see #FLAG_SET_LOCK_WALLPAPER |
| 920 | * @see #FLAG_SET_SYSTEM_WALLPAPER |
| 921 | * |
| 922 | * @return An integer ID assigned to the newly active wallpaper; or zero on failure. |
| 923 | * |
| 924 | * @throws IOException |
| 925 | */ |
| 926 | public int setBitmap(Bitmap fullImage, Rect visibleCropHint, |
| 927 | boolean allowBackup, @SetWallpaperFlags int which) |
Christopher Tate | ffa6a88 | 2015-12-11 15:08:46 -0800 | [diff] [blame] | 928 | throws IOException { |
| 929 | validateRect(visibleCropHint); |
Mike Lockwood | c067c9c | 2011-10-31 12:50:12 -0400 | [diff] [blame] | 930 | if (sGlobals.mService == null) { |
| 931 | Log.w(TAG, "WallpaperService not running"); |
Christopher Tate | ad3c259 | 2016-01-20 18:13:17 -0800 | [diff] [blame] | 932 | return 0; |
Mike Lockwood | c067c9c | 2011-10-31 12:50:12 -0400 | [diff] [blame] | 933 | } |
Christopher Tate | ad3c259 | 2016-01-20 18:13:17 -0800 | [diff] [blame] | 934 | final Bundle result = new Bundle(); |
Christopher Tate | 1e1e2e0 | 2016-01-25 15:34:36 -0800 | [diff] [blame] | 935 | final WallpaperSetCompletion completion = new WallpaperSetCompletion(); |
Dianne Hackborn | 8cc6a50 | 2009-08-05 21:29:42 -0700 | [diff] [blame] | 936 | try { |
Benjamin Franz | f3ece36 | 2015-02-11 10:51:10 +0000 | [diff] [blame] | 937 | ParcelFileDescriptor fd = sGlobals.mService.setWallpaper(null, |
Christopher Tate | 1e1e2e0 | 2016-01-25 15:34:36 -0800 | [diff] [blame] | 938 | mContext.getOpPackageName(), visibleCropHint, result, which, completion); |
Christopher Tate | ad3c259 | 2016-01-20 18:13:17 -0800 | [diff] [blame] | 939 | if (fd != null) { |
| 940 | FileOutputStream fos = null; |
| 941 | try { |
| 942 | fos = new ParcelFileDescriptor.AutoCloseOutputStream(fd); |
| 943 | fullImage.compress(Bitmap.CompressFormat.PNG, 90, fos); |
Christopher Tate | 1e1e2e0 | 2016-01-25 15:34:36 -0800 | [diff] [blame] | 944 | fos.close(); |
| 945 | completion.waitForCompletion(); |
Christopher Tate | ad3c259 | 2016-01-20 18:13:17 -0800 | [diff] [blame] | 946 | } finally { |
| 947 | IoUtils.closeQuietly(fos); |
| 948 | } |
Dianne Hackborn | 8cc6a50 | 2009-08-05 21:29:42 -0700 | [diff] [blame] | 949 | } |
| 950 | } catch (RemoteException e) { |
Romain Guy | 407ec78 | 2011-08-24 17:06:58 -0700 | [diff] [blame] | 951 | // Ignore |
Dianne Hackborn | 8cc6a50 | 2009-08-05 21:29:42 -0700 | [diff] [blame] | 952 | } |
Christopher Tate | ad3c259 | 2016-01-20 18:13:17 -0800 | [diff] [blame] | 953 | return result.getInt(EXTRA_NEW_WALLPAPER_ID, 0); |
Dianne Hackborn | 8cc6a50 | 2009-08-05 21:29:42 -0700 | [diff] [blame] | 954 | } |
| 955 | |
Christopher Tate | ffa6a88 | 2015-12-11 15:08:46 -0800 | [diff] [blame] | 956 | private final void validateRect(Rect rect) { |
| 957 | if (rect != null && rect.isEmpty()) { |
| 958 | throw new IllegalArgumentException("visibleCrop rectangle must be valid and non-empty"); |
| 959 | } |
| 960 | } |
| 961 | |
Dianne Hackborn | 8cc6a50 | 2009-08-05 21:29:42 -0700 | [diff] [blame] | 962 | /** |
| 963 | * Change the current system wallpaper to a specific byte stream. The |
| 964 | * give InputStream is copied into persistent storage and will now be |
| 965 | * used as the wallpaper. Currently it must be either a JPEG or PNG |
| 966 | * image. On success, the intent {@link Intent#ACTION_WALLPAPER_CHANGED} |
| 967 | * is broadcast. |
| 968 | * |
Christopher Tate | ffa6a88 | 2015-12-11 15:08:46 -0800 | [diff] [blame] | 969 | * <p>This method is equivalent to calling |
| 970 | * {@link #setStream(InputStream, Rect, boolean)} and passing {@code null} for the |
| 971 | * {@code visibleCrop} rectangle and {@code true} for the {@code allowBackup} |
| 972 | * parameter. |
| 973 | * |
Nicolas Falliere | 9530e3a | 2012-06-18 17:21:06 -0700 | [diff] [blame] | 974 | * <p>This method requires the caller to hold the permission |
| 975 | * {@link android.Manifest.permission#SET_WALLPAPER}. |
| 976 | * |
Christopher Tate | ffa6a88 | 2015-12-11 15:08:46 -0800 | [diff] [blame] | 977 | * @param bitmapData A stream containing the raw data to install as a wallpaper. |
Dianne Hackborn | 8cc6a50 | 2009-08-05 21:29:42 -0700 | [diff] [blame] | 978 | * |
Christopher Tate | ffa6a88 | 2015-12-11 15:08:46 -0800 | [diff] [blame] | 979 | * @throws IOException If an error occurs when attempting to set the wallpaper |
| 980 | * based on the provided image data. |
Dianne Hackborn | 8cc6a50 | 2009-08-05 21:29:42 -0700 | [diff] [blame] | 981 | */ |
Christopher Tate | ffa6a88 | 2015-12-11 15:08:46 -0800 | [diff] [blame] | 982 | public void setStream(InputStream bitmapData) throws IOException { |
| 983 | setStream(bitmapData, null, true); |
| 984 | } |
| 985 | |
Christopher Tate | ad3c259 | 2016-01-20 18:13:17 -0800 | [diff] [blame] | 986 | private void copyStreamToWallpaperFile(InputStream data, FileOutputStream fos) |
Christopher Tate | ffa6a88 | 2015-12-11 15:08:46 -0800 | [diff] [blame] | 987 | throws IOException { |
| 988 | byte[] buffer = new byte[32768]; |
| 989 | int amt; |
| 990 | while ((amt=data.read(buffer)) > 0) { |
| 991 | fos.write(buffer, 0, amt); |
| 992 | } |
| 993 | } |
| 994 | |
| 995 | /** |
| 996 | * Change the current system wallpaper to a specific byte stream, specifying a |
| 997 | * hint about which subrectangle of the full image is to be visible. The OS will |
| 998 | * then try to best present the given portion of the full image as the static system |
| 999 | * wallpaper image. The data from the given InputStream is copied into persistent |
| 1000 | * storage and will then be used as the system wallpaper. Currently the data must |
| 1001 | * be either a JPEG or PNG image. On success, the intent |
| 1002 | * {@link Intent#ACTION_WALLPAPER_CHANGED} is broadcast. |
| 1003 | * |
| 1004 | * <p>This method requires the caller to hold the permission |
| 1005 | * {@link android.Manifest.permission#SET_WALLPAPER}. |
| 1006 | * |
| 1007 | * @param bitmapData A stream containing the raw data to install as a wallpaper. |
| 1008 | * @param visibleCropHint The rectangular subregion of the streamed image that should be |
| 1009 | * displayed as wallpaper. Passing {@code null} for this parameter means that |
| 1010 | * the full image should be displayed if possible given the image's and device's |
| 1011 | * aspect ratios, etc. |
| 1012 | * @param allowBackup {@code true} if the OS is permitted to back up this wallpaper |
| 1013 | * image for restore to a future device; {@code false} otherwise. |
| 1014 | * |
| 1015 | * @throws IOException If an error occurs when attempting to set the wallpaper |
| 1016 | * based on the provided image data. |
| 1017 | * @throws IllegalArgumentException If the {@code visibleCropHint} rectangle is |
| 1018 | * empty or invalid. |
| 1019 | */ |
Christopher Tate | ad3c259 | 2016-01-20 18:13:17 -0800 | [diff] [blame] | 1020 | public int setStream(InputStream bitmapData, Rect visibleCropHint, boolean allowBackup) |
Christopher Tate | ffa6a88 | 2015-12-11 15:08:46 -0800 | [diff] [blame] | 1021 | throws IOException { |
Christopher Tate | ad3c259 | 2016-01-20 18:13:17 -0800 | [diff] [blame] | 1022 | return setStream(bitmapData, visibleCropHint, allowBackup, FLAG_SET_SYSTEM); |
| 1023 | } |
| 1024 | |
| 1025 | /** |
| 1026 | * Version of {@link #setStream(InputStream, Rect, boolean)} that allows the caller |
| 1027 | * to specify which of the supported wallpaper categories to set. |
| 1028 | * |
| 1029 | * @param bitmapData A stream containing the raw data to install as a wallpaper. |
| 1030 | * @param visibleCropHint The rectangular subregion of the streamed image that should be |
| 1031 | * displayed as wallpaper. Passing {@code null} for this parameter means that |
| 1032 | * the full image should be displayed if possible given the image's and device's |
| 1033 | * aspect ratios, etc. |
| 1034 | * @param allowBackup {@code true} if the OS is permitted to back up this wallpaper |
| 1035 | * image for restore to a future device; {@code false} otherwise. |
| 1036 | * @param which Flags indicating which wallpaper(s) to configure with the new imagery. |
| 1037 | * |
| 1038 | * @see #FLAG_SET_LOCK_WALLPAPER |
| 1039 | * @see #FLAG_SET_SYSTEM_WALLPAPER |
| 1040 | * |
| 1041 | * @throws IOException |
| 1042 | */ |
| 1043 | public int setStream(InputStream bitmapData, Rect visibleCropHint, |
| 1044 | boolean allowBackup, @SetWallpaperFlags int which) |
| 1045 | throws IOException { |
Christopher Tate | ffa6a88 | 2015-12-11 15:08:46 -0800 | [diff] [blame] | 1046 | validateRect(visibleCropHint); |
Mike Lockwood | c067c9c | 2011-10-31 12:50:12 -0400 | [diff] [blame] | 1047 | if (sGlobals.mService == null) { |
| 1048 | Log.w(TAG, "WallpaperService not running"); |
Christopher Tate | ad3c259 | 2016-01-20 18:13:17 -0800 | [diff] [blame] | 1049 | return 0; |
Mike Lockwood | c067c9c | 2011-10-31 12:50:12 -0400 | [diff] [blame] | 1050 | } |
Christopher Tate | ad3c259 | 2016-01-20 18:13:17 -0800 | [diff] [blame] | 1051 | final Bundle result = new Bundle(); |
Christopher Tate | 1e1e2e0 | 2016-01-25 15:34:36 -0800 | [diff] [blame] | 1052 | final WallpaperSetCompletion completion = new WallpaperSetCompletion(); |
Dianne Hackborn | 8cc6a50 | 2009-08-05 21:29:42 -0700 | [diff] [blame] | 1053 | try { |
Benjamin Franz | f3ece36 | 2015-02-11 10:51:10 +0000 | [diff] [blame] | 1054 | ParcelFileDescriptor fd = sGlobals.mService.setWallpaper(null, |
Christopher Tate | 1e1e2e0 | 2016-01-25 15:34:36 -0800 | [diff] [blame] | 1055 | mContext.getOpPackageName(), visibleCropHint, result, which, completion); |
Christopher Tate | ad3c259 | 2016-01-20 18:13:17 -0800 | [diff] [blame] | 1056 | if (fd != null) { |
| 1057 | FileOutputStream fos = null; |
| 1058 | try { |
| 1059 | fos = new ParcelFileDescriptor.AutoCloseOutputStream(fd); |
| 1060 | copyStreamToWallpaperFile(bitmapData, fos); |
Christopher Tate | 1e1e2e0 | 2016-01-25 15:34:36 -0800 | [diff] [blame] | 1061 | fos.close(); |
| 1062 | completion.waitForCompletion(); |
Christopher Tate | ad3c259 | 2016-01-20 18:13:17 -0800 | [diff] [blame] | 1063 | } finally { |
| 1064 | IoUtils.closeQuietly(fos); |
| 1065 | } |
Dianne Hackborn | 8cc6a50 | 2009-08-05 21:29:42 -0700 | [diff] [blame] | 1066 | } |
| 1067 | } catch (RemoteException e) { |
Romain Guy | 407ec78 | 2011-08-24 17:06:58 -0700 | [diff] [blame] | 1068 | // Ignore |
Dianne Hackborn | 8cc6a50 | 2009-08-05 21:29:42 -0700 | [diff] [blame] | 1069 | } |
Christopher Tate | ad3c259 | 2016-01-20 18:13:17 -0800 | [diff] [blame] | 1070 | |
| 1071 | return result.getInt(EXTRA_NEW_WALLPAPER_ID, 0); |
Dianne Hackborn | 8cc6a50 | 2009-08-05 21:29:42 -0700 | [diff] [blame] | 1072 | } |
| 1073 | |
Dianne Hackborn | 8cc6a50 | 2009-08-05 21:29:42 -0700 | [diff] [blame] | 1074 | /** |
Dianne Hackborn | 5dc5a00 | 2012-09-15 19:33:48 -0700 | [diff] [blame] | 1075 | * Return whether any users are currently set to use the wallpaper |
| 1076 | * with the given resource ID. That is, their wallpaper has been |
| 1077 | * set through {@link #setResource(int)} with the same resource id. |
| 1078 | */ |
Tor Norbye | 7b9c912 | 2013-05-30 16:48:33 -0700 | [diff] [blame] | 1079 | public boolean hasResourceWallpaper(@RawRes int resid) { |
Dianne Hackborn | 5dc5a00 | 2012-09-15 19:33:48 -0700 | [diff] [blame] | 1080 | if (sGlobals.mService == null) { |
| 1081 | Log.w(TAG, "WallpaperService not running"); |
| 1082 | return false; |
| 1083 | } |
| 1084 | try { |
| 1085 | Resources resources = mContext.getResources(); |
| 1086 | String name = "res:" + resources.getResourceName(resid); |
| 1087 | return sGlobals.mService.hasNamedWallpaper(name); |
| 1088 | } catch (RemoteException e) { |
| 1089 | return false; |
| 1090 | } |
| 1091 | } |
| 1092 | |
| 1093 | /** |
Dianne Hackborn | 8cc6a50 | 2009-08-05 21:29:42 -0700 | [diff] [blame] | 1094 | * Returns the desired minimum width for the wallpaper. Callers of |
Dianne Hackborn | 4c62fc0 | 2009-08-08 20:40:27 -0700 | [diff] [blame] | 1095 | * {@link #setBitmap(android.graphics.Bitmap)} or |
| 1096 | * {@link #setStream(java.io.InputStream)} should check this value |
Dianne Hackborn | 8cc6a50 | 2009-08-05 21:29:42 -0700 | [diff] [blame] | 1097 | * beforehand to make sure the supplied wallpaper respects the desired |
| 1098 | * minimum width. |
| 1099 | * |
| 1100 | * If the returned value is <= 0, the caller should use the width of |
| 1101 | * the default display instead. |
| 1102 | * |
| 1103 | * @return The desired minimum width for the wallpaper. This value should |
| 1104 | * be honored by applications that set the wallpaper but it is not |
| 1105 | * mandatory. |
| 1106 | */ |
| 1107 | public int getDesiredMinimumWidth() { |
Mike Lockwood | c067c9c | 2011-10-31 12:50:12 -0400 | [diff] [blame] | 1108 | if (sGlobals.mService == null) { |
| 1109 | Log.w(TAG, "WallpaperService not running"); |
| 1110 | return 0; |
| 1111 | } |
Dianne Hackborn | 8cc6a50 | 2009-08-05 21:29:42 -0700 | [diff] [blame] | 1112 | try { |
Dianne Hackborn | 840c3a2 | 2009-09-02 17:35:17 -0700 | [diff] [blame] | 1113 | return sGlobals.mService.getWidthHint(); |
Dianne Hackborn | 8cc6a50 | 2009-08-05 21:29:42 -0700 | [diff] [blame] | 1114 | } catch (RemoteException e) { |
| 1115 | // Shouldn't happen! |
| 1116 | return 0; |
| 1117 | } |
| 1118 | } |
| 1119 | |
| 1120 | /** |
| 1121 | * Returns the desired minimum height for the wallpaper. Callers of |
Dianne Hackborn | 4c62fc0 | 2009-08-08 20:40:27 -0700 | [diff] [blame] | 1122 | * {@link #setBitmap(android.graphics.Bitmap)} or |
| 1123 | * {@link #setStream(java.io.InputStream)} should check this value |
Dianne Hackborn | 8cc6a50 | 2009-08-05 21:29:42 -0700 | [diff] [blame] | 1124 | * beforehand to make sure the supplied wallpaper respects the desired |
| 1125 | * minimum height. |
| 1126 | * |
| 1127 | * If the returned value is <= 0, the caller should use the height of |
| 1128 | * the default display instead. |
| 1129 | * |
| 1130 | * @return The desired minimum height for the wallpaper. This value should |
| 1131 | * be honored by applications that set the wallpaper but it is not |
| 1132 | * mandatory. |
| 1133 | */ |
| 1134 | public int getDesiredMinimumHeight() { |
Mike Lockwood | c067c9c | 2011-10-31 12:50:12 -0400 | [diff] [blame] | 1135 | if (sGlobals.mService == null) { |
| 1136 | Log.w(TAG, "WallpaperService not running"); |
| 1137 | return 0; |
| 1138 | } |
Dianne Hackborn | 8cc6a50 | 2009-08-05 21:29:42 -0700 | [diff] [blame] | 1139 | try { |
Dianne Hackborn | 840c3a2 | 2009-09-02 17:35:17 -0700 | [diff] [blame] | 1140 | return sGlobals.mService.getHeightHint(); |
Dianne Hackborn | 8cc6a50 | 2009-08-05 21:29:42 -0700 | [diff] [blame] | 1141 | } catch (RemoteException e) { |
| 1142 | // Shouldn't happen! |
| 1143 | return 0; |
| 1144 | } |
| 1145 | } |
| 1146 | |
| 1147 | /** |
| 1148 | * For use only by the current home application, to specify the size of |
| 1149 | * wallpaper it would like to use. This allows such applications to have |
| 1150 | * a virtual wallpaper that is larger than the physical screen, matching |
| 1151 | * the size of their workspace. |
Dianne Hackborn | c5bf758 | 2012-04-25 19:12:07 -0700 | [diff] [blame] | 1152 | * |
| 1153 | * <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] | 1154 | * for <em>home apps</em> to tell what size wallpaper they would like. |
| 1155 | * Nobody else should be calling this! Certainly not other non-home |
Dianne Hackborn | c5bf758 | 2012-04-25 19:12:07 -0700 | [diff] [blame] | 1156 | * apps that change the wallpaper. Those apps are supposed to |
| 1157 | * <b>retrieve</b> the suggested size so they can construct a wallpaper |
| 1158 | * that matches it. |
| 1159 | * |
Nicolas Falliere | 9530e3a | 2012-06-18 17:21:06 -0700 | [diff] [blame] | 1160 | * <p>This method requires the caller to hold the permission |
| 1161 | * {@link android.Manifest.permission#SET_WALLPAPER_HINTS}. |
| 1162 | * |
Dianne Hackborn | 8cc6a50 | 2009-08-05 21:29:42 -0700 | [diff] [blame] | 1163 | * @param minimumWidth Desired minimum width |
| 1164 | * @param minimumHeight Desired minimum height |
| 1165 | */ |
Dianne Hackborn | 4c62fc0 | 2009-08-08 20:40:27 -0700 | [diff] [blame] | 1166 | public void suggestDesiredDimensions(int minimumWidth, int minimumHeight) { |
Dianne Hackborn | 8cc6a50 | 2009-08-05 21:29:42 -0700 | [diff] [blame] | 1167 | try { |
Donghan Ryu | 289c273 | 2011-11-14 18:56:11 -0800 | [diff] [blame] | 1168 | /** |
| 1169 | * The framework makes no attempt to limit the window size |
| 1170 | * to the maximum texture size. Any window larger than this |
| 1171 | * cannot be composited. |
| 1172 | * |
| 1173 | * Read maximum texture size from system property and scale down |
| 1174 | * minimumWidth and minimumHeight accordingly. |
| 1175 | */ |
| 1176 | int maximumTextureSize; |
| 1177 | try { |
| 1178 | maximumTextureSize = SystemProperties.getInt("sys.max_texture_size", 0); |
| 1179 | } catch (Exception e) { |
| 1180 | maximumTextureSize = 0; |
| 1181 | } |
| 1182 | |
| 1183 | if (maximumTextureSize > 0) { |
| 1184 | if ((minimumWidth > maximumTextureSize) || |
| 1185 | (minimumHeight > maximumTextureSize)) { |
| 1186 | float aspect = (float)minimumHeight / (float)minimumWidth; |
| 1187 | if (minimumWidth > minimumHeight) { |
| 1188 | minimumWidth = maximumTextureSize; |
| 1189 | minimumHeight = (int)((minimumWidth * aspect) + 0.5); |
| 1190 | } else { |
| 1191 | minimumHeight = maximumTextureSize; |
| 1192 | minimumWidth = (int)((minimumHeight / aspect) + 0.5); |
| 1193 | } |
| 1194 | } |
| 1195 | } |
| 1196 | |
Mike Lockwood | c067c9c | 2011-10-31 12:50:12 -0400 | [diff] [blame] | 1197 | if (sGlobals.mService == null) { |
| 1198 | Log.w(TAG, "WallpaperService not running"); |
| 1199 | } else { |
Benjamin Franz | f3ece36 | 2015-02-11 10:51:10 +0000 | [diff] [blame] | 1200 | sGlobals.mService.setDimensionHints(minimumWidth, minimumHeight, |
| 1201 | mContext.getOpPackageName()); |
Mike Lockwood | c067c9c | 2011-10-31 12:50:12 -0400 | [diff] [blame] | 1202 | } |
Dianne Hackborn | 8cc6a50 | 2009-08-05 21:29:42 -0700 | [diff] [blame] | 1203 | } catch (RemoteException e) { |
Romain Guy | 407ec78 | 2011-08-24 17:06:58 -0700 | [diff] [blame] | 1204 | // Ignore |
Dianne Hackborn | 8cc6a50 | 2009-08-05 21:29:42 -0700 | [diff] [blame] | 1205 | } |
| 1206 | } |
Adam Cohen | 041a0ba | 2011-11-09 20:10:27 -0800 | [diff] [blame] | 1207 | |
Dianne Hackborn | 8cc6a50 | 2009-08-05 21:29:42 -0700 | [diff] [blame] | 1208 | /** |
Dianne Hackborn | 067e5f6 | 2014-09-07 23:14:30 -0700 | [diff] [blame] | 1209 | * Specify extra padding that the wallpaper should have outside of the display. |
| 1210 | * That is, the given padding supplies additional pixels the wallpaper should extend |
| 1211 | * outside of the display itself. |
| 1212 | * @param padding The number of pixels the wallpaper should extend beyond the display, |
| 1213 | * on its left, top, right, and bottom sides. |
| 1214 | * @hide |
| 1215 | */ |
| 1216 | @SystemApi |
| 1217 | public void setDisplayPadding(Rect padding) { |
| 1218 | try { |
| 1219 | if (sGlobals.mService == null) { |
| 1220 | Log.w(TAG, "WallpaperService not running"); |
| 1221 | } else { |
Benjamin Franz | f3ece36 | 2015-02-11 10:51:10 +0000 | [diff] [blame] | 1222 | sGlobals.mService.setDisplayPadding(padding, mContext.getOpPackageName()); |
Dianne Hackborn | 067e5f6 | 2014-09-07 23:14:30 -0700 | [diff] [blame] | 1223 | } |
| 1224 | } catch (RemoteException e) { |
| 1225 | // Ignore |
| 1226 | } |
| 1227 | } |
| 1228 | |
| 1229 | /** |
| 1230 | * Apply a raw offset to the wallpaper window. Should only be used in |
| 1231 | * combination with {@link #setDisplayPadding(android.graphics.Rect)} when you |
| 1232 | * have ensured that the wallpaper will extend outside of the display area so that |
| 1233 | * it can be moved without leaving part of the display uncovered. |
| 1234 | * @param x The offset, in pixels, to apply to the left edge. |
| 1235 | * @param y The offset, in pixels, to apply to the top edge. |
| 1236 | * @hide |
| 1237 | */ |
| 1238 | @SystemApi |
| 1239 | public void setDisplayOffset(IBinder windowToken, int x, int y) { |
| 1240 | try { |
| 1241 | //Log.v(TAG, "Sending new wallpaper display offsets from app..."); |
| 1242 | WindowManagerGlobal.getWindowSession().setWallpaperDisplayOffset( |
| 1243 | windowToken, x, y); |
| 1244 | //Log.v(TAG, "...app returning after sending display offset!"); |
| 1245 | } catch (RemoteException e) { |
| 1246 | // Ignore. |
| 1247 | } |
| 1248 | } |
| 1249 | |
| 1250 | /** |
Filip Gruszczynski | 9fedc72 | 2015-01-28 15:59:46 -0800 | [diff] [blame] | 1251 | * Clear the wallpaper. |
| 1252 | * |
| 1253 | * @hide |
| 1254 | */ |
| 1255 | @SystemApi |
| 1256 | public void clearWallpaper() { |
Christopher Tate | be132e6 | 2016-02-10 12:59:49 -0800 | [diff] [blame] | 1257 | clearWallpaper(FLAG_SET_SYSTEM, mContext.getUserId()); |
| 1258 | } |
| 1259 | |
| 1260 | /** |
| 1261 | * Clear the wallpaper for a specific user. The caller must hold the |
| 1262 | * INTERACT_ACROSS_USERS_FULL permission to clear another user's |
| 1263 | * wallpaper. |
| 1264 | * @hide |
| 1265 | */ |
| 1266 | @SystemApi |
| 1267 | public void clearWallpaper(int which, int userId) { |
Filip Gruszczynski | 9fedc72 | 2015-01-28 15:59:46 -0800 | [diff] [blame] | 1268 | if (sGlobals.mService == null) { |
| 1269 | Log.w(TAG, "WallpaperService not running"); |
| 1270 | return; |
| 1271 | } |
| 1272 | try { |
Christopher Tate | be132e6 | 2016-02-10 12:59:49 -0800 | [diff] [blame] | 1273 | sGlobals.mService.clearWallpaper(mContext.getOpPackageName(), which, userId); |
Filip Gruszczynski | 9fedc72 | 2015-01-28 15:59:46 -0800 | [diff] [blame] | 1274 | } catch (RemoteException e) { |
| 1275 | // Ignore |
| 1276 | } |
| 1277 | } |
| 1278 | |
| 1279 | /** |
| 1280 | * Set the live wallpaper. |
| 1281 | * |
| 1282 | * This can only be called by packages with android.permission.SET_WALLPAPER_COMPONENT |
| 1283 | * permission. |
| 1284 | * |
| 1285 | * @hide |
| 1286 | */ |
| 1287 | @SystemApi |
| 1288 | public boolean setWallpaperComponent(ComponentName name) { |
| 1289 | if (sGlobals.mService == null) { |
| 1290 | Log.w(TAG, "WallpaperService not running"); |
| 1291 | return false; |
| 1292 | } |
| 1293 | try { |
Benjamin Franz | f3ece36 | 2015-02-11 10:51:10 +0000 | [diff] [blame] | 1294 | sGlobals.mService.setWallpaperComponentChecked(name, mContext.getOpPackageName()); |
Filip Gruszczynski | 9fedc72 | 2015-01-28 15:59:46 -0800 | [diff] [blame] | 1295 | return true; |
| 1296 | } catch (RemoteException e) { |
| 1297 | // Ignore |
| 1298 | } |
| 1299 | return false; |
| 1300 | } |
| 1301 | |
| 1302 | /** |
Christopher Tate | ffa6a88 | 2015-12-11 15:08:46 -0800 | [diff] [blame] | 1303 | * 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] | 1304 | * that wallpaper is visible behind the given window. The X and Y offsets |
| 1305 | * are floating point numbers ranging from 0 to 1, representing where the |
| 1306 | * wallpaper should be positioned within the screen space. These only |
Christopher Tate | ffa6a88 | 2015-12-11 15:08:46 -0800 | [diff] [blame] | 1307 | * make sense when the wallpaper is larger than the display. |
Oleksandr Peletskyi | f251981 | 2016-01-26 20:16:06 +0100 | [diff] [blame] | 1308 | * |
Dianne Hackborn | c8a0a75 | 2009-08-10 23:05:49 -0700 | [diff] [blame] | 1309 | * @param windowToken The window who these offsets should be associated |
Scott Main | 8b2e000 | 2009-09-29 18:17:31 -0700 | [diff] [blame] | 1310 | * with, as returned by {@link android.view.View#getWindowToken() |
Dianne Hackborn | c8a0a75 | 2009-08-10 23:05:49 -0700 | [diff] [blame] | 1311 | * View.getWindowToken()}. |
Marco Nelissen | bf6956b | 2009-11-09 15:21:13 -0800 | [diff] [blame] | 1312 | * @param xOffset The offset along the X dimension, from 0 to 1. |
Dianne Hackborn | c8a0a75 | 2009-08-10 23:05:49 -0700 | [diff] [blame] | 1313 | * @param yOffset The offset along the Y dimension, from 0 to 1. |
| 1314 | */ |
| 1315 | public void setWallpaperOffsets(IBinder windowToken, float xOffset, float yOffset) { |
Adam Cohen | 791a633 | 2012-01-12 14:38:38 -0800 | [diff] [blame] | 1316 | try { |
| 1317 | //Log.v(TAG, "Sending new wallpaper offsets from app..."); |
Jeff Brown | f9e989d | 2013-04-04 23:04:03 -0700 | [diff] [blame] | 1318 | WindowManagerGlobal.getWindowSession().setWallpaperPosition( |
Adam Cohen | 791a633 | 2012-01-12 14:38:38 -0800 | [diff] [blame] | 1319 | windowToken, xOffset, yOffset, mWallpaperXStep, mWallpaperYStep); |
| 1320 | //Log.v(TAG, "...app returning after sending offsets!"); |
| 1321 | } catch (RemoteException e) { |
| 1322 | // Ignore. |
| 1323 | } |
Dianne Hackborn | c8a0a75 | 2009-08-10 23:05:49 -0700 | [diff] [blame] | 1324 | } |
Adam Cohen | 041a0ba | 2011-11-09 20:10:27 -0800 | [diff] [blame] | 1325 | |
Dianne Hackborn | c8a0a75 | 2009-08-10 23:05:49 -0700 | [diff] [blame] | 1326 | /** |
Marco Nelissen | bf6956b | 2009-11-09 15:21:13 -0800 | [diff] [blame] | 1327 | * For applications that use multiple virtual screens showing a wallpaper, |
| 1328 | * specify the step size between virtual screens. For example, if the |
Dianne Hackborn | 23ef7b4 | 2009-11-18 18:20:39 -0800 | [diff] [blame] | 1329 | * 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] | 1330 | * 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] | 1331 | * @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] | 1332 | * @param yStep The Y offset delta from one screen to the next one |
| 1333 | */ |
| 1334 | public void setWallpaperOffsetSteps(float xStep, float yStep) { |
| 1335 | mWallpaperXStep = xStep; |
| 1336 | mWallpaperYStep = yStep; |
| 1337 | } |
Oleksandr Peletskyi | f251981 | 2016-01-26 20:16:06 +0100 | [diff] [blame] | 1338 | |
Marco Nelissen | bf6956b | 2009-11-09 15:21:13 -0800 | [diff] [blame] | 1339 | /** |
Dianne Hackborn | 7580493 | 2009-10-20 20:15:20 -0700 | [diff] [blame] | 1340 | * Send an arbitrary command to the current active wallpaper. |
Oleksandr Peletskyi | f251981 | 2016-01-26 20:16:06 +0100 | [diff] [blame] | 1341 | * |
Dianne Hackborn | 7580493 | 2009-10-20 20:15:20 -0700 | [diff] [blame] | 1342 | * @param windowToken The window who these offsets should be associated |
| 1343 | * with, as returned by {@link android.view.View#getWindowToken() |
| 1344 | * View.getWindowToken()}. |
| 1345 | * @param action Name of the command to perform. This must be a scoped |
| 1346 | * name to avoid collisions, such as "com.mycompany.wallpaper.DOIT". |
| 1347 | * @param x Arbitrary integer argument based on command. |
| 1348 | * @param y Arbitrary integer argument based on command. |
| 1349 | * @param z Arbitrary integer argument based on command. |
| 1350 | * @param extras Optional additional information for the command, or null. |
| 1351 | */ |
| 1352 | public void sendWallpaperCommand(IBinder windowToken, String action, |
| 1353 | int x, int y, int z, Bundle extras) { |
| 1354 | try { |
| 1355 | //Log.v(TAG, "Sending new wallpaper offsets from app..."); |
Jeff Brown | f9e989d | 2013-04-04 23:04:03 -0700 | [diff] [blame] | 1356 | WindowManagerGlobal.getWindowSession().sendWallpaperCommand( |
Dianne Hackborn | 7580493 | 2009-10-20 20:15:20 -0700 | [diff] [blame] | 1357 | windowToken, action, x, y, z, extras, false); |
| 1358 | //Log.v(TAG, "...app returning after sending offsets!"); |
| 1359 | } catch (RemoteException e) { |
| 1360 | // Ignore. |
| 1361 | } |
| 1362 | } |
Benjamin Franz | f3ece36 | 2015-02-11 10:51:10 +0000 | [diff] [blame] | 1363 | |
| 1364 | /** |
| 1365 | * 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] | 1366 | * {@code false}, any attempts to changing the wallpaper will have no effect, |
| 1367 | * and any attempt to obtain of the wallpaper will return {@code null}. |
Benjamin Franz | f3ece36 | 2015-02-11 10:51:10 +0000 | [diff] [blame] | 1368 | */ |
| 1369 | public boolean isWallpaperSupported() { |
| 1370 | if (sGlobals.mService == null) { |
| 1371 | Log.w(TAG, "WallpaperService not running"); |
| 1372 | } else { |
| 1373 | try { |
| 1374 | return sGlobals.mService.isWallpaperSupported(mContext.getOpPackageName()); |
| 1375 | } catch (RemoteException e) { |
| 1376 | // Ignore |
| 1377 | } |
| 1378 | } |
| 1379 | return false; |
| 1380 | } |
| 1381 | |
Dianne Hackborn | 7580493 | 2009-10-20 20:15:20 -0700 | [diff] [blame] | 1382 | /** |
Oleksandr Peletskyi | f251981 | 2016-01-26 20:16:06 +0100 | [diff] [blame] | 1383 | * Returns whether the calling package is allowed to set the wallpaper for the calling user. |
| 1384 | * If this function returns {@code false}, any attempts to change the wallpaper will have |
| 1385 | * no effect. Always returns {@code true} for device owner and profile owner. |
| 1386 | * |
| 1387 | * @see android.os.UserManager#DISALLOW_SET_WALLPAPER |
| 1388 | */ |
| 1389 | public boolean isWallpaperSettingAllowed() { |
| 1390 | if (sGlobals.mService == null) { |
| 1391 | Log.w(TAG, "WallpaperService not running"); |
| 1392 | } else { |
| 1393 | try { |
| 1394 | return sGlobals.mService.isWallpaperSettingAllowed(mContext.getOpPackageName()); |
| 1395 | } catch (RemoteException e) { |
| 1396 | // Ignore |
| 1397 | } |
| 1398 | } |
| 1399 | return false; |
| 1400 | } |
| 1401 | |
| 1402 | /** |
Dianne Hackborn | 72c82ab | 2009-08-11 21:13:54 -0700 | [diff] [blame] | 1403 | * Clear the offsets previously associated with this window through |
| 1404 | * {@link #setWallpaperOffsets(IBinder, float, float)}. This reverts |
| 1405 | * the window to its default state, where it does not cause the wallpaper |
| 1406 | * to scroll from whatever its last offsets were. |
Oleksandr Peletskyi | f251981 | 2016-01-26 20:16:06 +0100 | [diff] [blame] | 1407 | * |
Dianne Hackborn | 72c82ab | 2009-08-11 21:13:54 -0700 | [diff] [blame] | 1408 | * @param windowToken The window who these offsets should be associated |
Scott Main | 8b2e000 | 2009-09-29 18:17:31 -0700 | [diff] [blame] | 1409 | * with, as returned by {@link android.view.View#getWindowToken() |
Dianne Hackborn | 72c82ab | 2009-08-11 21:13:54 -0700 | [diff] [blame] | 1410 | * View.getWindowToken()}. |
| 1411 | */ |
| 1412 | public void clearWallpaperOffsets(IBinder windowToken) { |
| 1413 | try { |
Jeff Brown | f9e989d | 2013-04-04 23:04:03 -0700 | [diff] [blame] | 1414 | WindowManagerGlobal.getWindowSession().setWallpaperPosition( |
Marco Nelissen | bf6956b | 2009-11-09 15:21:13 -0800 | [diff] [blame] | 1415 | windowToken, -1, -1, -1, -1); |
Dianne Hackborn | 72c82ab | 2009-08-11 21:13:54 -0700 | [diff] [blame] | 1416 | } catch (RemoteException e) { |
| 1417 | // Ignore. |
| 1418 | } |
| 1419 | } |
Oleksandr Peletskyi | f251981 | 2016-01-26 20:16:06 +0100 | [diff] [blame] | 1420 | |
Dianne Hackborn | 72c82ab | 2009-08-11 21:13:54 -0700 | [diff] [blame] | 1421 | /** |
Christopher Tate | be132e6 | 2016-02-10 12:59:49 -0800 | [diff] [blame] | 1422 | * 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] | 1423 | * wallpaper. On success, the intent {@link Intent#ACTION_WALLPAPER_CHANGED} |
| 1424 | * is broadcast. |
| 1425 | * |
Nicolas Falliere | 9530e3a | 2012-06-18 17:21:06 -0700 | [diff] [blame] | 1426 | * <p>This method requires the caller to hold the permission |
| 1427 | * {@link android.Manifest.permission#SET_WALLPAPER}. |
| 1428 | * |
Michael Jurka | b668d0b | 2013-10-04 15:11:05 -0700 | [diff] [blame] | 1429 | * @throws IOException If an error occurs reverting to the built-in |
Dianne Hackborn | 8cc6a50 | 2009-08-05 21:29:42 -0700 | [diff] [blame] | 1430 | * wallpaper. |
| 1431 | */ |
| 1432 | public void clear() throws IOException { |
Jeff Sharkey | 28f0877 | 2014-04-16 09:41:58 -0700 | [diff] [blame] | 1433 | setStream(openDefaultWallpaper(mContext)); |
| 1434 | } |
| 1435 | |
| 1436 | /** |
| 1437 | * Open stream representing the default static image wallpaper. |
| 1438 | * |
| 1439 | * @hide |
| 1440 | */ |
| 1441 | public static InputStream openDefaultWallpaper(Context context) { |
| 1442 | final String path = SystemProperties.get(PROP_WALLPAPER); |
| 1443 | if (!TextUtils.isEmpty(path)) { |
| 1444 | final File file = new File(path); |
| 1445 | if (file.exists()) { |
| 1446 | try { |
| 1447 | return new FileInputStream(file); |
| 1448 | } catch (IOException e) { |
| 1449 | // Ignored, fall back to platform default below |
| 1450 | } |
| 1451 | } |
| 1452 | } |
| 1453 | return context.getResources().openRawResource( |
| 1454 | com.android.internal.R.drawable.default_wallpaper); |
| 1455 | } |
| 1456 | |
| 1457 | /** |
| 1458 | * Return {@link ComponentName} of the default live wallpaper, or |
| 1459 | * {@code null} if none is defined. |
| 1460 | * |
| 1461 | * @hide |
| 1462 | */ |
| 1463 | public static ComponentName getDefaultWallpaperComponent(Context context) { |
| 1464 | String flat = SystemProperties.get(PROP_WALLPAPER_COMPONENT); |
| 1465 | if (!TextUtils.isEmpty(flat)) { |
| 1466 | final ComponentName cn = ComponentName.unflattenFromString(flat); |
| 1467 | if (cn != null) { |
| 1468 | return cn; |
| 1469 | } |
| 1470 | } |
| 1471 | |
| 1472 | flat = context.getString(com.android.internal.R.string.default_wallpaper_component); |
| 1473 | if (!TextUtils.isEmpty(flat)) { |
| 1474 | final ComponentName cn = ComponentName.unflattenFromString(flat); |
| 1475 | if (cn != null) { |
| 1476 | return cn; |
| 1477 | } |
| 1478 | } |
| 1479 | |
| 1480 | return null; |
Dianne Hackborn | 8cc6a50 | 2009-08-05 21:29:42 -0700 | [diff] [blame] | 1481 | } |
Christopher Tate | 1e1e2e0 | 2016-01-25 15:34:36 -0800 | [diff] [blame] | 1482 | |
Christopher Tate | be132e6 | 2016-02-10 12:59:49 -0800 | [diff] [blame] | 1483 | /** |
| 1484 | * Register a callback for lock wallpaper observation. Only the OS may use this. |
| 1485 | * |
| 1486 | * @return true on success; false on error. |
| 1487 | * @hide |
| 1488 | */ |
| 1489 | public boolean setLockWallpaperCallback(IWallpaperManagerCallback callback) { |
| 1490 | if (sGlobals.mService == null) { |
| 1491 | Log.w(TAG, "WallpaperService not running"); |
| 1492 | return false; |
| 1493 | } |
| 1494 | |
| 1495 | try { |
| 1496 | return sGlobals.mService.setLockWallpaperCallback(callback); |
| 1497 | } catch (RemoteException e) { |
| 1498 | Log.e(TAG, "Unable to contact wallpaper service"); |
| 1499 | } |
| 1500 | return false; |
| 1501 | } |
| 1502 | |
Christopher Tate | 1e1e2e0 | 2016-01-25 15:34:36 -0800 | [diff] [blame] | 1503 | // Private completion callback for setWallpaper() synchronization |
| 1504 | private class WallpaperSetCompletion extends IWallpaperManagerCallback.Stub { |
| 1505 | final CountDownLatch mLatch; |
| 1506 | |
| 1507 | public WallpaperSetCompletion() { |
| 1508 | mLatch = new CountDownLatch(1); |
| 1509 | } |
| 1510 | |
| 1511 | public void waitForCompletion() { |
| 1512 | try { |
| 1513 | mLatch.await(30, TimeUnit.SECONDS); |
| 1514 | } catch (InterruptedException e) { |
| 1515 | // This might be legit: the crop may take a very long time. Don't sweat |
| 1516 | // it in that case; we are okay with display lagging behind in order to |
| 1517 | // keep the caller from locking up indeterminately. |
| 1518 | } |
| 1519 | } |
| 1520 | |
| 1521 | @Override |
| 1522 | public void onWallpaperChanged() throws RemoteException { |
| 1523 | mLatch.countDown(); |
| 1524 | } |
| 1525 | } |
Dianne Hackborn | 8cc6a50 | 2009-08-05 21:29:42 -0700 | [diff] [blame] | 1526 | } |