The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2008 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
Amith Yamasani | 09e9cdc | 2013-11-06 14:54:50 -0800 | [diff] [blame] | 17 | package com.android.server.wallpaper; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 18 | |
Christopher Tate | edf7d04 | 2016-03-29 18:24:25 -0700 | [diff] [blame] | 19 | import static android.app.WallpaperManager.FLAG_LOCK; |
Jeff Sharkey | 1cab76a | 2016-04-12 18:23:31 -0600 | [diff] [blame] | 20 | import static android.app.WallpaperManager.FLAG_SYSTEM; |
| 21 | import static android.os.ParcelFileDescriptor.MODE_CREATE; |
| 22 | import static android.os.ParcelFileDescriptor.MODE_READ_ONLY; |
| 23 | import static android.os.ParcelFileDescriptor.MODE_READ_WRITE; |
| 24 | import static android.os.ParcelFileDescriptor.MODE_TRUNCATE; |
Wale Ogunwale | ac2561e | 2016-11-01 15:43:46 -0700 | [diff] [blame] | 25 | import static android.view.Display.DEFAULT_DISPLAY; |
| 26 | import static android.view.WindowManager.LayoutParams.TYPE_WALLPAPER; |
Christopher Tate | 111bd4a | 2009-06-24 17:29:38 -0700 | [diff] [blame] | 27 | |
Lucas Dupin | 50ba991 | 2017-07-14 11:55:05 -0700 | [diff] [blame] | 28 | import android.annotation.NonNull; |
Christopher Tate | e409f0e | 2016-03-21 14:53:15 -0700 | [diff] [blame] | 29 | import android.app.ActivityManager; |
Amith Yamasani | 4e2820c | 2012-08-28 22:17:23 -0700 | [diff] [blame] | 30 | import android.app.AppGlobals; |
Benjamin Franz | f3ece36 | 2015-02-11 10:51:10 +0000 | [diff] [blame] | 31 | import android.app.AppOpsManager; |
Dianne Hackborn | 8cc6a50 | 2009-08-05 21:29:42 -0700 | [diff] [blame] | 32 | import android.app.IWallpaperManager; |
| 33 | import android.app.IWallpaperManagerCallback; |
Dianne Hackborn | dd9b82c | 2009-09-03 00:18:47 -0700 | [diff] [blame] | 34 | import android.app.PendingIntent; |
Sudheer Shanka | 2c4522c | 2016-08-27 20:53:28 -0700 | [diff] [blame] | 35 | import android.app.UserSwitchObserver; |
Lucas Dupin | 75ec379 | 2017-06-29 14:07:18 -0700 | [diff] [blame] | 36 | import android.app.WallpaperColors; |
Dianne Hackborn | eb03465 | 2009-09-07 00:49:58 -0700 | [diff] [blame] | 37 | import android.app.WallpaperInfo; |
Jeff Sharkey | 28f0877 | 2014-04-16 09:41:58 -0700 | [diff] [blame] | 38 | import android.app.WallpaperManager; |
Oleksandr Peletskyi | f251981 | 2016-01-26 20:16:06 +0100 | [diff] [blame] | 39 | import android.app.admin.DevicePolicyManager; |
Amith Yamasani | 37ce3a8 | 2012-02-06 12:04:42 -0800 | [diff] [blame] | 40 | import android.app.backup.WallpaperBackupHelper; |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 41 | import android.content.BroadcastReceiver; |
Dianne Hackborn | 4c62fc0 | 2009-08-08 20:40:27 -0700 | [diff] [blame] | 42 | import android.content.ComponentName; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 43 | import android.content.Context; |
| 44 | import android.content.Intent; |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 45 | import android.content.IntentFilter; |
Dianne Hackborn | 4c62fc0 | 2009-08-08 20:40:27 -0700 | [diff] [blame] | 46 | import android.content.ServiceConnection; |
Amith Yamasani | 4e2820c | 2012-08-28 22:17:23 -0700 | [diff] [blame] | 47 | import android.content.pm.IPackageManager; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 48 | import android.content.pm.PackageManager; |
Jeff Sharkey | 1cab76a | 2016-04-12 18:23:31 -0600 | [diff] [blame] | 49 | import android.content.pm.PackageManager.NameNotFoundException; |
Dianne Hackborn | 4c62fc0 | 2009-08-08 20:40:27 -0700 | [diff] [blame] | 50 | import android.content.pm.ResolveInfo; |
| 51 | import android.content.pm.ServiceInfo; |
Amith Yamasani | 6474c4c | 2012-10-04 14:55:42 -0700 | [diff] [blame] | 52 | import android.content.pm.UserInfo; |
Joe Onorato | 9bb8fd7 | 2009-07-28 18:24:51 -0700 | [diff] [blame] | 53 | import android.content.res.Resources; |
Christopher Tate | 1e1e2e0 | 2016-01-25 15:34:36 -0800 | [diff] [blame] | 54 | import android.graphics.Bitmap; |
| 55 | import android.graphics.BitmapFactory; |
| 56 | import android.graphics.BitmapRegionDecoder; |
Lucas Dupin | ea1fb1e | 2017-04-05 17:39:44 -0700 | [diff] [blame] | 57 | import android.graphics.Color; |
John Spurlock | 41f6464 | 2013-11-04 13:48:38 -0500 | [diff] [blame] | 58 | import android.graphics.Point; |
Dianne Hackborn | 067e5f6 | 2014-09-07 23:14:30 -0700 | [diff] [blame] | 59 | import android.graphics.Rect; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 60 | import android.os.Binder; |
Dianne Hackborn | 284ac93 | 2009-08-28 10:34:25 -0700 | [diff] [blame] | 61 | import android.os.Bundle; |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 62 | import android.os.Environment; |
Jeff Sharkey | 1cab76a | 2016-04-12 18:23:31 -0600 | [diff] [blame] | 63 | import android.os.FileObserver; |
Dianne Hackborn | 8bdf593 | 2010-10-15 12:54:40 -0700 | [diff] [blame] | 64 | import android.os.FileUtils; |
Christopher Tate | c349e59f | 2017-05-05 17:37:43 -0700 | [diff] [blame] | 65 | import android.os.Handler; |
Dianne Hackborn | 4c62fc0 | 2009-08-08 20:40:27 -0700 | [diff] [blame] | 66 | import android.os.IBinder; |
Lucas Dupin | 50ba991 | 2017-07-14 11:55:05 -0700 | [diff] [blame] | 67 | import android.os.IInterface; |
Dianne Hackborn | 5dc5a00 | 2012-09-15 19:33:48 -0700 | [diff] [blame] | 68 | import android.os.IRemoteCallback; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 69 | import android.os.ParcelFileDescriptor; |
Lucas Dupin | 75ec379 | 2017-06-29 14:07:18 -0700 | [diff] [blame] | 70 | import android.os.Process; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 71 | import android.os.RemoteCallbackList; |
Jeff Sharkey | 1cab76a | 2016-04-12 18:23:31 -0600 | [diff] [blame] | 72 | import android.os.RemoteException; |
rpcraig | 554cb0c | 2012-07-05 06:41:43 -0400 | [diff] [blame] | 73 | import android.os.SELinux; |
Dianne Hackborn | 4c62fc0 | 2009-08-08 20:40:27 -0700 | [diff] [blame] | 74 | import android.os.ServiceManager; |
Dianne Hackborn | 0cd4887 | 2009-08-13 18:51:59 -0700 | [diff] [blame] | 75 | import android.os.SystemClock; |
Dianne Hackborn | f02b60a | 2012-08-16 10:48:27 -0700 | [diff] [blame] | 76 | import android.os.UserHandle; |
Amith Yamasani | 6474c4c | 2012-10-04 14:55:42 -0700 | [diff] [blame] | 77 | import android.os.UserManager; |
Dianne Hackborn | 4c62fc0 | 2009-08-08 20:40:27 -0700 | [diff] [blame] | 78 | import android.service.wallpaper.IWallpaperConnection; |
| 79 | import android.service.wallpaper.IWallpaperEngine; |
| 80 | import android.service.wallpaper.IWallpaperService; |
| 81 | import android.service.wallpaper.WallpaperService; |
Christopher Tate | 8347b63 | 2016-04-29 18:59:18 -0700 | [diff] [blame] | 82 | import android.system.ErrnoException; |
| 83 | import android.system.Os; |
Filip Gruszczynski | 5dcc3ac | 2014-10-13 15:51:39 -0700 | [diff] [blame] | 84 | import android.util.EventLog; |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 85 | import android.util.Slog; |
Amith Yamasani | 37ce3a8 | 2012-02-06 12:04:42 -0800 | [diff] [blame] | 86 | import android.util.SparseArray; |
Joe Onorato | 9bb8fd7 | 2009-07-28 18:24:51 -0700 | [diff] [blame] | 87 | import android.util.Xml; |
Dianne Hackborn | 44bc17c | 2011-04-20 18:18:51 -0700 | [diff] [blame] | 88 | import android.view.Display; |
Dianne Hackborn | 4c62fc0 | 2009-08-08 20:40:27 -0700 | [diff] [blame] | 89 | import android.view.IWindowManager; |
| 90 | import android.view.WindowManager; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 91 | |
Jeff Sharkey | 1cab76a | 2016-04-12 18:23:31 -0600 | [diff] [blame] | 92 | import com.android.internal.R; |
| 93 | import com.android.internal.content.PackageMonitor; |
Christopher Tate | 190e853 | 2016-07-11 11:35:34 -0700 | [diff] [blame] | 94 | import com.android.internal.os.BackgroundThread; |
Jeff Sharkey | fe9a53b | 2017-03-31 14:08:23 -0600 | [diff] [blame] | 95 | import com.android.internal.util.DumpUtils; |
Jeff Sharkey | 1cab76a | 2016-04-12 18:23:31 -0600 | [diff] [blame] | 96 | import com.android.internal.util.FastXmlSerializer; |
| 97 | import com.android.internal.util.JournaledFile; |
| 98 | import com.android.server.EventLogTags; |
Adrian Roos | c3f915e | 2016-09-06 11:40:53 -0700 | [diff] [blame] | 99 | import com.android.server.FgThread; |
Jeff Sharkey | 1cab76a | 2016-04-12 18:23:31 -0600 | [diff] [blame] | 100 | import com.android.server.SystemService; |
| 101 | |
Daichi Hirono | 4bbf852 | 2017-12-06 10:34:18 +0900 | [diff] [blame] | 102 | import java.lang.reflect.InvocationTargetException; |
Jeff Sharkey | 1cab76a | 2016-04-12 18:23:31 -0600 | [diff] [blame] | 103 | import libcore.io.IoUtils; |
Joe Onorato | 9bb8fd7 | 2009-07-28 18:24:51 -0700 | [diff] [blame] | 104 | |
| 105 | import org.xmlpull.v1.XmlPullParser; |
| 106 | import org.xmlpull.v1.XmlPullParserException; |
| 107 | import org.xmlpull.v1.XmlSerializer; |
| 108 | |
Jeff Sharkey | 1cab76a | 2016-04-12 18:23:31 -0600 | [diff] [blame] | 109 | import java.io.BufferedOutputStream; |
| 110 | import java.io.File; |
| 111 | import java.io.FileDescriptor; |
| 112 | import java.io.FileInputStream; |
| 113 | import java.io.FileNotFoundException; |
| 114 | import java.io.FileOutputStream; |
| 115 | import java.io.IOException; |
| 116 | import java.io.InputStream; |
| 117 | import java.io.PrintWriter; |
| 118 | import java.nio.charset.StandardCharsets; |
Lucas Dupin | fb3ab2a | 2017-08-08 14:17:30 -0400 | [diff] [blame] | 119 | import java.util.ArrayList; |
Jeff Sharkey | 1cab76a | 2016-04-12 18:23:31 -0600 | [diff] [blame] | 120 | import java.util.Arrays; |
| 121 | import java.util.List; |
Christopher Tate | 2a6c55f | 2017-03-31 12:28:30 -0700 | [diff] [blame] | 122 | import java.util.Objects; |
Daichi Hirono | 4bbf852 | 2017-12-06 10:34:18 +0900 | [diff] [blame] | 123 | import com.android.internal.R; |
Christopher Tate | ad3c259 | 2016-01-20 18:13:17 -0800 | [diff] [blame] | 124 | |
Daichi Hirono | 4bbf852 | 2017-12-06 10:34:18 +0900 | [diff] [blame] | 125 | public class WallpaperManagerService extends IWallpaperManager.Stub |
| 126 | implements IWallpaperManagerService { |
Amith Yamasani | 09e9cdc | 2013-11-06 14:54:50 -0800 | [diff] [blame] | 127 | static final String TAG = "WallpaperManagerService"; |
Joe Onorato | 7e1693a | 2016-02-01 17:45:03 -0800 | [diff] [blame] | 128 | static final boolean DEBUG = false; |
Christopher Tate | 2a6c55f | 2017-03-31 12:28:30 -0700 | [diff] [blame] | 129 | static final boolean DEBUG_LIVE = DEBUG || true; |
Joe Onorato | 9bb8fd7 | 2009-07-28 18:24:51 -0700 | [diff] [blame] | 130 | |
Jeff Sharkey | 1cab76a | 2016-04-12 18:23:31 -0600 | [diff] [blame] | 131 | public static class Lifecycle extends SystemService { |
Daichi Hirono | 4bbf852 | 2017-12-06 10:34:18 +0900 | [diff] [blame] | 132 | private IWallpaperManagerService mService; |
Jeff Sharkey | 1cab76a | 2016-04-12 18:23:31 -0600 | [diff] [blame] | 133 | |
| 134 | public Lifecycle(Context context) { |
| 135 | super(context); |
| 136 | } |
| 137 | |
| 138 | @Override |
| 139 | public void onStart() { |
Daichi Hirono | 4bbf852 | 2017-12-06 10:34:18 +0900 | [diff] [blame] | 140 | try { |
| 141 | final Class<? extends IWallpaperManagerService> klass = |
| 142 | (Class<? extends IWallpaperManagerService>)Class.forName( |
| 143 | getContext().getResources().getString( |
| 144 | R.string.config_wallpaperManagerServiceName)); |
| 145 | mService = klass.getConstructor(Context.class).newInstance(getContext()); |
| 146 | publishBinderService(Context.WALLPAPER_SERVICE, mService); |
| 147 | } catch (Exception exp) { |
| 148 | Slog.wtf(TAG, "Failed to instantiate WallpaperManagerService", exp); |
| 149 | } |
Jeff Sharkey | 1cab76a | 2016-04-12 18:23:31 -0600 | [diff] [blame] | 150 | } |
| 151 | |
| 152 | @Override |
| 153 | public void onBootPhase(int phase) { |
Daichi Hirono | 4bbf852 | 2017-12-06 10:34:18 +0900 | [diff] [blame] | 154 | if (mService != null) { |
| 155 | mService.onBootPhase(phase); |
Jeff Sharkey | 1cab76a | 2016-04-12 18:23:31 -0600 | [diff] [blame] | 156 | } |
| 157 | } |
| 158 | |
| 159 | @Override |
| 160 | public void onUnlockUser(int userHandle) { |
Daichi Hirono | 4bbf852 | 2017-12-06 10:34:18 +0900 | [diff] [blame] | 161 | if (mService != null) { |
| 162 | mService.onUnlockUser(userHandle); |
| 163 | } |
Jeff Sharkey | 1cab76a | 2016-04-12 18:23:31 -0600 | [diff] [blame] | 164 | } |
| 165 | } |
| 166 | |
Christopher Tate | be132e6 | 2016-02-10 12:59:49 -0800 | [diff] [blame] | 167 | final Object mLock = new Object(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 168 | |
Dianne Hackborn | 0cd4887 | 2009-08-13 18:51:59 -0700 | [diff] [blame] | 169 | /** |
| 170 | * Minimum time between crashes of a wallpaper service for us to consider |
| 171 | * restarting it vs. just reverting to the static wallpaper. |
| 172 | */ |
| 173 | static final long MIN_WALLPAPER_CRASH_TIME = 10000; |
Filip Gruszczynski | 5dcc3ac | 2014-10-13 15:51:39 -0700 | [diff] [blame] | 174 | static final int MAX_WALLPAPER_COMPONENT_LOG_LENGTH = 128; |
Christopher Tate | 1e1e2e0 | 2016-01-25 15:34:36 -0800 | [diff] [blame] | 175 | static final String WALLPAPER = "wallpaper_orig"; |
| 176 | static final String WALLPAPER_CROP = "wallpaper"; |
Christopher Tate | be132e6 | 2016-02-10 12:59:49 -0800 | [diff] [blame] | 177 | static final String WALLPAPER_LOCK_ORIG = "wallpaper_lock_orig"; |
| 178 | static final String WALLPAPER_LOCK_CROP = "wallpaper_lock"; |
Amith Yamasani | 37ce3a8 | 2012-02-06 12:04:42 -0800 | [diff] [blame] | 179 | static final String WALLPAPER_INFO = "wallpaper_info.xml"; |
Dianne Hackborn | bce0cbb | 2012-10-05 11:06:53 -0700 | [diff] [blame] | 180 | |
Christopher Tate | be132e6 | 2016-02-10 12:59:49 -0800 | [diff] [blame] | 181 | // All the various per-user state files we need to be aware of |
| 182 | static final String[] sPerUserFiles = new String[] { |
| 183 | WALLPAPER, WALLPAPER_CROP, |
| 184 | WALLPAPER_LOCK_ORIG, WALLPAPER_LOCK_CROP, |
| 185 | WALLPAPER_INFO |
| 186 | }; |
| 187 | |
Dianne Hackborn | bce0cbb | 2012-10-05 11:06:53 -0700 | [diff] [blame] | 188 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 189 | * Observes the wallpaper for changes and notifies all IWallpaperServiceCallbacks |
| 190 | * that the wallpaper has changed. The CREATE is triggered when there is no |
| 191 | * wallpaper set and is created for the first time. The CLOSE_WRITE is triggered |
Christopher Tate | 190e853 | 2016-07-11 11:35:34 -0700 | [diff] [blame] | 192 | * every time the wallpaper is changed. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 193 | */ |
Amith Yamasani | 37ce3a8 | 2012-02-06 12:04:42 -0800 | [diff] [blame] | 194 | private class WallpaperObserver extends FileObserver { |
Joe Onorato | 9bb8fd7 | 2009-07-28 18:24:51 -0700 | [diff] [blame] | 195 | |
Christopher Tate | be132e6 | 2016-02-10 12:59:49 -0800 | [diff] [blame] | 196 | final int mUserId; |
Amith Yamasani | 37ce3a8 | 2012-02-06 12:04:42 -0800 | [diff] [blame] | 197 | final WallpaperData mWallpaper; |
| 198 | final File mWallpaperDir; |
| 199 | final File mWallpaperFile; |
Christopher Tate | be132e6 | 2016-02-10 12:59:49 -0800 | [diff] [blame] | 200 | final File mWallpaperLockFile; |
Amith Yamasani | 37ce3a8 | 2012-02-06 12:04:42 -0800 | [diff] [blame] | 201 | |
| 202 | public WallpaperObserver(WallpaperData wallpaper) { |
| 203 | super(getWallpaperDir(wallpaper.userId).getAbsolutePath(), |
Christopher Tate | da058e2 | 2014-10-08 14:51:09 -0700 | [diff] [blame] | 204 | CLOSE_WRITE | MOVED_TO | DELETE | DELETE_SELF); |
Christopher Tate | be132e6 | 2016-02-10 12:59:49 -0800 | [diff] [blame] | 205 | mUserId = wallpaper.userId; |
Amith Yamasani | 37ce3a8 | 2012-02-06 12:04:42 -0800 | [diff] [blame] | 206 | mWallpaperDir = getWallpaperDir(wallpaper.userId); |
| 207 | mWallpaper = wallpaper; |
| 208 | mWallpaperFile = new File(mWallpaperDir, WALLPAPER); |
Christopher Tate | be132e6 | 2016-02-10 12:59:49 -0800 | [diff] [blame] | 209 | mWallpaperLockFile = new File(mWallpaperDir, WALLPAPER_LOCK_ORIG); |
Amith Yamasani | 37ce3a8 | 2012-02-06 12:04:42 -0800 | [diff] [blame] | 210 | } |
| 211 | |
Christopher Tate | be132e6 | 2016-02-10 12:59:49 -0800 | [diff] [blame] | 212 | private WallpaperData dataForEvent(boolean sysChanged, boolean lockChanged) { |
| 213 | WallpaperData wallpaper = null; |
| 214 | synchronized (mLock) { |
| 215 | if (lockChanged) { |
| 216 | wallpaper = mLockWallpaperMap.get(mUserId); |
| 217 | } |
| 218 | if (wallpaper == null) { |
| 219 | // no lock-specific wallpaper exists, or sys case, handled together |
| 220 | wallpaper = mWallpaperMap.get(mUserId); |
| 221 | } |
| 222 | } |
| 223 | return (wallpaper != null) ? wallpaper : mWallpaper; |
| 224 | } |
| 225 | |
Amith Yamasani | 37ce3a8 | 2012-02-06 12:04:42 -0800 | [diff] [blame] | 226 | @Override |
| 227 | public void onEvent(int event, String path) { |
| 228 | if (path == null) { |
| 229 | return; |
| 230 | } |
Christopher Tate | d7faf53 | 2016-02-25 12:43:38 -0800 | [diff] [blame] | 231 | final boolean moved = (event == MOVED_TO); |
| 232 | final boolean written = (event == CLOSE_WRITE || moved); |
Christopher Tate | 1e1e2e0 | 2016-01-25 15:34:36 -0800 | [diff] [blame] | 233 | final File changedFile = new File(mWallpaperDir, path); |
| 234 | |
Christopher Tate | be132e6 | 2016-02-10 12:59:49 -0800 | [diff] [blame] | 235 | // System and system+lock changes happen on the system wallpaper input file; |
| 236 | // lock-only changes happen on the dedicated lock wallpaper input file |
| 237 | final boolean sysWallpaperChanged = (mWallpaperFile.equals(changedFile)); |
| 238 | final boolean lockWallpaperChanged = (mWallpaperLockFile.equals(changedFile)); |
Lucas Dupin | ea1fb1e | 2017-04-05 17:39:44 -0700 | [diff] [blame] | 239 | int notifyColorsWhich = 0; |
Christopher Tate | be132e6 | 2016-02-10 12:59:49 -0800 | [diff] [blame] | 240 | WallpaperData wallpaper = dataForEvent(sysWallpaperChanged, lockWallpaperChanged); |
| 241 | |
| 242 | if (DEBUG) { |
| 243 | Slog.v(TAG, "Wallpaper file change: evt=" + event |
| 244 | + " path=" + path |
| 245 | + " sys=" + sysWallpaperChanged |
| 246 | + " lock=" + lockWallpaperChanged |
| 247 | + " imagePending=" + wallpaper.imageWallpaperPending |
| 248 | + " whichPending=0x" + Integer.toHexString(wallpaper.whichPending) |
| 249 | + " written=" + written); |
| 250 | } |
Christopher Tate | 8347b63 | 2016-04-29 18:59:18 -0700 | [diff] [blame] | 251 | |
| 252 | if (moved && lockWallpaperChanged) { |
| 253 | // We just migrated sys -> lock to preserve imagery for an impending |
Christopher Tate | fa7d97f | 2016-06-30 12:21:57 -0700 | [diff] [blame] | 254 | // new system-only wallpaper. Tell keyguard about it and make sure it |
| 255 | // has the right SELinux label. |
Christopher Tate | 8347b63 | 2016-04-29 18:59:18 -0700 | [diff] [blame] | 256 | if (DEBUG) { |
| 257 | Slog.i(TAG, "Sys -> lock MOVED_TO"); |
| 258 | } |
Christopher Tate | fa7d97f | 2016-06-30 12:21:57 -0700 | [diff] [blame] | 259 | SELinux.restorecon(changedFile); |
Christopher Tate | 8347b63 | 2016-04-29 18:59:18 -0700 | [diff] [blame] | 260 | notifyLockWallpaperChanged(); |
Lucas Dupin | ea1fb1e | 2017-04-05 17:39:44 -0700 | [diff] [blame] | 261 | notifyWallpaperColorsChanged(wallpaper, FLAG_LOCK); |
Christopher Tate | 8347b63 | 2016-04-29 18:59:18 -0700 | [diff] [blame] | 262 | return; |
| 263 | } |
| 264 | |
Amith Yamasani | 37ce3a8 | 2012-02-06 12:04:42 -0800 | [diff] [blame] | 265 | synchronized (mLock) { |
Christopher Tate | be132e6 | 2016-02-10 12:59:49 -0800 | [diff] [blame] | 266 | if (sysWallpaperChanged || lockWallpaperChanged) { |
| 267 | notifyCallbacksLocked(wallpaper); |
| 268 | if (wallpaper.wallpaperComponent == null |
Christopher Tate | da058e2 | 2014-10-08 14:51:09 -0700 | [diff] [blame] | 269 | || event != CLOSE_WRITE // includes the MOVED_TO case |
Christopher Tate | be132e6 | 2016-02-10 12:59:49 -0800 | [diff] [blame] | 270 | || wallpaper.imageWallpaperPending) { |
Christopher Tate | da058e2 | 2014-10-08 14:51:09 -0700 | [diff] [blame] | 271 | if (written) { |
Christopher Tate | 1e1e2e0 | 2016-01-25 15:34:36 -0800 | [diff] [blame] | 272 | // The image source has finished writing the source image, |
| 273 | // so we now produce the crop rect (in the background), and |
| 274 | // only publish the new displayable (sub)image as a result |
| 275 | // of that work. |
Christopher Tate | be132e6 | 2016-02-10 12:59:49 -0800 | [diff] [blame] | 276 | if (DEBUG) { |
| 277 | Slog.v(TAG, "Wallpaper written; generating crop"); |
| 278 | } |
Christopher Tate | ebadfb1 | 2016-07-25 14:50:08 -0700 | [diff] [blame] | 279 | SELinux.restorecon(changedFile); |
Christopher Tate | d7faf53 | 2016-02-25 12:43:38 -0800 | [diff] [blame] | 280 | if (moved) { |
| 281 | // This is a restore, so generate the crop using any just-restored new |
| 282 | // crop guidelines, making sure to preserve our local dimension hints. |
Christopher Tate | fa7d97f | 2016-06-30 12:21:57 -0700 | [diff] [blame] | 283 | // We also make sure to reapply the correct SELinux label. |
Christopher Tate | d7faf53 | 2016-02-25 12:43:38 -0800 | [diff] [blame] | 284 | if (DEBUG) { |
| 285 | Slog.v(TAG, "moved-to, therefore restore; reloading metadata"); |
| 286 | } |
| 287 | loadSettingsLocked(wallpaper.userId, true); |
| 288 | } |
Christopher Tate | be132e6 | 2016-02-10 12:59:49 -0800 | [diff] [blame] | 289 | generateCrop(wallpaper); |
| 290 | if (DEBUG) { |
| 291 | Slog.v(TAG, "Crop done; invoking completion callback"); |
| 292 | } |
| 293 | wallpaper.imageWallpaperPending = false; |
Christopher Tate | be132e6 | 2016-02-10 12:59:49 -0800 | [diff] [blame] | 294 | if (sysWallpaperChanged) { |
| 295 | // If this was the system wallpaper, rebind... |
| 296 | bindWallpaperComponentLocked(mImageWallpaper, true, |
| 297 | false, wallpaper, null); |
Lucas Dupin | ea1fb1e | 2017-04-05 17:39:44 -0700 | [diff] [blame] | 298 | notifyColorsWhich |= FLAG_SYSTEM; |
Christopher Tate | be132e6 | 2016-02-10 12:59:49 -0800 | [diff] [blame] | 299 | } |
| 300 | if (lockWallpaperChanged |
Christopher Tate | edf7d04 | 2016-03-29 18:24:25 -0700 | [diff] [blame] | 301 | || (wallpaper.whichPending & FLAG_LOCK) != 0) { |
Christopher Tate | be132e6 | 2016-02-10 12:59:49 -0800 | [diff] [blame] | 302 | if (DEBUG) { |
Christopher Tate | db27b84 | 2016-02-25 14:39:17 -0800 | [diff] [blame] | 303 | Slog.i(TAG, "Lock-relevant wallpaper changed"); |
Christopher Tate | be132e6 | 2016-02-10 12:59:49 -0800 | [diff] [blame] | 304 | } |
Christopher Tate | db27b84 | 2016-02-25 14:39:17 -0800 | [diff] [blame] | 305 | // either a lock-only wallpaper commit or a system+lock event. |
| 306 | // if it's system-plus-lock we need to wipe the lock bookkeeping; |
| 307 | // we're falling back to displaying the system wallpaper there. |
| 308 | if (!lockWallpaperChanged) { |
| 309 | mLockWallpaperMap.remove(wallpaper.userId); |
| 310 | } |
| 311 | // and in any case, tell keyguard about it |
Christopher Tate | 8347b63 | 2016-04-29 18:59:18 -0700 | [diff] [blame] | 312 | notifyLockWallpaperChanged(); |
Lucas Dupin | ea1fb1e | 2017-04-05 17:39:44 -0700 | [diff] [blame] | 313 | notifyColorsWhich |= FLAG_LOCK; |
Christopher Tate | be132e6 | 2016-02-10 12:59:49 -0800 | [diff] [blame] | 314 | } |
Lucas Dupin | ea1fb1e | 2017-04-05 17:39:44 -0700 | [diff] [blame] | 315 | |
Christopher Tate | db27b84 | 2016-02-25 14:39:17 -0800 | [diff] [blame] | 316 | saveSettingsLocked(wallpaper.userId); |
Christopher Tate | 8efbe0d | 2017-08-29 16:50:13 -0700 | [diff] [blame] | 317 | |
| 318 | // Publish completion *after* we've persisted the changes |
| 319 | if (wallpaper.setComplete != null) { |
| 320 | try { |
| 321 | wallpaper.setComplete.onWallpaperChanged(); |
| 322 | } catch (RemoteException e) { |
| 323 | // if this fails we don't really care; the setting app may just |
| 324 | // have crashed and that sort of thing is a fact of life. |
| 325 | } |
| 326 | } |
Joe Onorato | 9bb8fd7 | 2009-07-28 18:24:51 -0700 | [diff] [blame] | 327 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 328 | } |
| 329 | } |
Amith Yamasani | 37ce3a8 | 2012-02-06 12:04:42 -0800 | [diff] [blame] | 330 | } |
Lucas Dupin | ea1fb1e | 2017-04-05 17:39:44 -0700 | [diff] [blame] | 331 | |
| 332 | // Outside of the lock since it will synchronize itself |
| 333 | if (notifyColorsWhich != 0) { |
| 334 | notifyWallpaperColorsChanged(wallpaper, notifyColorsWhich); |
| 335 | } |
Amith Yamasani | 37ce3a8 | 2012-02-06 12:04:42 -0800 | [diff] [blame] | 336 | } |
| 337 | } |
| 338 | |
Christopher Tate | 8347b63 | 2016-04-29 18:59:18 -0700 | [diff] [blame] | 339 | void notifyLockWallpaperChanged() { |
| 340 | final IWallpaperManagerCallback cb = mKeyguardListener; |
| 341 | if (cb != null) { |
| 342 | try { |
| 343 | cb.onWallpaperChanged(); |
| 344 | } catch (RemoteException e) { |
| 345 | // Oh well it went away; no big deal |
| 346 | } |
| 347 | } |
| 348 | } |
| 349 | |
Lucas Dupin | 50ba991 | 2017-07-14 11:55:05 -0700 | [diff] [blame] | 350 | private void notifyWallpaperColorsChanged(@NonNull WallpaperData wallpaper, int which) { |
Lucas Dupin | ea1fb1e | 2017-04-05 17:39:44 -0700 | [diff] [blame] | 351 | boolean needsExtraction; |
| 352 | synchronized (mLock) { |
Lucas Dupin | 50ba991 | 2017-07-14 11:55:05 -0700 | [diff] [blame] | 353 | final RemoteCallbackList<IWallpaperManagerCallback> currentUserColorListeners = |
| 354 | mColorsChangedListeners.get(wallpaper.userId); |
| 355 | final RemoteCallbackList<IWallpaperManagerCallback> userAllColorListeners = |
| 356 | mColorsChangedListeners.get(UserHandle.USER_ALL); |
| 357 | // No-op until someone is listening to it. |
| 358 | if (emptyCallbackList(currentUserColorListeners) && |
| 359 | emptyCallbackList(userAllColorListeners)) { |
Lucas Dupin | ea1fb1e | 2017-04-05 17:39:44 -0700 | [diff] [blame] | 360 | return; |
Lucas Dupin | 50ba991 | 2017-07-14 11:55:05 -0700 | [diff] [blame] | 361 | } |
Lucas Dupin | ea1fb1e | 2017-04-05 17:39:44 -0700 | [diff] [blame] | 362 | |
| 363 | if (DEBUG) { |
| 364 | Slog.v(TAG, "notifyWallpaperColorsChanged " + which); |
| 365 | } |
| 366 | |
| 367 | needsExtraction = wallpaper.primaryColors == null; |
| 368 | } |
| 369 | |
Lucas Dupin | 75ec379 | 2017-06-29 14:07:18 -0700 | [diff] [blame] | 370 | // Let's notify the current values, it's fine if it's null, it just means |
| 371 | // that we don't know yet. |
Lucas Dupin | 50ba991 | 2017-07-14 11:55:05 -0700 | [diff] [blame] | 372 | notifyColorListeners(wallpaper.primaryColors, which, wallpaper.userId); |
Lucas Dupin | 75ec379 | 2017-06-29 14:07:18 -0700 | [diff] [blame] | 373 | |
Lucas Dupin | ea1fb1e | 2017-04-05 17:39:44 -0700 | [diff] [blame] | 374 | if (needsExtraction) { |
| 375 | extractColors(wallpaper); |
Lucas Dupin | 50ba991 | 2017-07-14 11:55:05 -0700 | [diff] [blame] | 376 | synchronized (mLock) { |
| 377 | // Don't need to notify if nothing changed. |
| 378 | if (wallpaper.primaryColors == null) { |
| 379 | return; |
| 380 | } |
| 381 | } |
| 382 | notifyColorListeners(wallpaper.primaryColors, which, wallpaper.userId); |
Lucas Dupin | ea1fb1e | 2017-04-05 17:39:44 -0700 | [diff] [blame] | 383 | } |
Lucas Dupin | 75ec379 | 2017-06-29 14:07:18 -0700 | [diff] [blame] | 384 | } |
Lucas Dupin | ea1fb1e | 2017-04-05 17:39:44 -0700 | [diff] [blame] | 385 | |
Lucas Dupin | 50ba991 | 2017-07-14 11:55:05 -0700 | [diff] [blame] | 386 | private static <T extends IInterface> boolean emptyCallbackList(RemoteCallbackList<T> list) { |
| 387 | return (list == null || list.getRegisteredCallbackCount() == 0); |
| 388 | } |
| 389 | |
| 390 | private void notifyColorListeners(@NonNull WallpaperColors wallpaperColors, int which, |
| 391 | int userId) { |
Lucas Dupin | 75ec379 | 2017-06-29 14:07:18 -0700 | [diff] [blame] | 392 | final IWallpaperManagerCallback keyguardListener; |
Lucas Dupin | fb3ab2a | 2017-08-08 14:17:30 -0400 | [diff] [blame] | 393 | final ArrayList<IWallpaperManagerCallback> colorListeners = new ArrayList<>(); |
Lucas Dupin | ea1fb1e | 2017-04-05 17:39:44 -0700 | [diff] [blame] | 394 | synchronized (mLock) { |
Lucas Dupin | fb3ab2a | 2017-08-08 14:17:30 -0400 | [diff] [blame] | 395 | final RemoteCallbackList<IWallpaperManagerCallback> currentUserColorListeners = |
| 396 | mColorsChangedListeners.get(userId); |
| 397 | final RemoteCallbackList<IWallpaperManagerCallback> userAllColorListeners = |
| 398 | mColorsChangedListeners.get(UserHandle.USER_ALL); |
Lucas Dupin | 75ec379 | 2017-06-29 14:07:18 -0700 | [diff] [blame] | 399 | keyguardListener = mKeyguardListener; |
Lucas Dupin | fb3ab2a | 2017-08-08 14:17:30 -0400 | [diff] [blame] | 400 | |
| 401 | if (currentUserColorListeners != null) { |
| 402 | final int count = currentUserColorListeners.beginBroadcast(); |
| 403 | for (int i = 0; i < count; i++) { |
| 404 | colorListeners.add(currentUserColorListeners.getBroadcastItem(i)); |
| 405 | } |
| 406 | currentUserColorListeners.finishBroadcast(); |
| 407 | } |
| 408 | |
| 409 | if (userAllColorListeners != null) { |
| 410 | final int count = userAllColorListeners.beginBroadcast(); |
| 411 | for (int i = 0; i < count; i++) { |
| 412 | colorListeners.add(userAllColorListeners.getBroadcastItem(i)); |
| 413 | } |
| 414 | userAllColorListeners.finishBroadcast(); |
| 415 | } |
Lucas Dupin | 75ec379 | 2017-06-29 14:07:18 -0700 | [diff] [blame] | 416 | } |
Lucas Dupin | ea1fb1e | 2017-04-05 17:39:44 -0700 | [diff] [blame] | 417 | |
Lucas Dupin | fb3ab2a | 2017-08-08 14:17:30 -0400 | [diff] [blame] | 418 | final int count = colorListeners.size(); |
| 419 | for (int i = 0; i < count; i++) { |
| 420 | try { |
| 421 | colorListeners.get(i).onWallpaperColorsChanged(wallpaperColors, which, userId); |
| 422 | } catch (RemoteException e) { |
| 423 | // Callback is gone, it's not necessary to unregister it since |
| 424 | // RemoteCallbackList#getBroadcastItem will take care of it. |
Lucas Dupin | 75ec379 | 2017-06-29 14:07:18 -0700 | [diff] [blame] | 425 | } |
| 426 | } |
| 427 | |
| 428 | if (keyguardListener != null) { |
| 429 | try { |
Lucas Dupin | 50ba991 | 2017-07-14 11:55:05 -0700 | [diff] [blame] | 430 | keyguardListener.onWallpaperColorsChanged(wallpaperColors, which, userId); |
Lucas Dupin | 75ec379 | 2017-06-29 14:07:18 -0700 | [diff] [blame] | 431 | } catch (RemoteException e) { |
| 432 | // Oh well it went away; no big deal |
Lucas Dupin | ea1fb1e | 2017-04-05 17:39:44 -0700 | [diff] [blame] | 433 | } |
| 434 | } |
| 435 | } |
| 436 | |
Lucas Dupin | bcae585 | 2017-05-03 12:42:58 -0700 | [diff] [blame] | 437 | /** |
| 438 | * We can easily extract colors from an ImageWallpaper since it's only a bitmap. |
Lucas Dupin | 284836b | 2017-06-23 15:28:41 -0700 | [diff] [blame] | 439 | * In this case, using the crop is more than enough. Live wallpapers are just ignored. |
Lucas Dupin | bcae585 | 2017-05-03 12:42:58 -0700 | [diff] [blame] | 440 | * |
| 441 | * @param wallpaper a wallpaper representation |
| 442 | */ |
Lucas Dupin | ea1fb1e | 2017-04-05 17:39:44 -0700 | [diff] [blame] | 443 | private void extractColors(WallpaperData wallpaper) { |
| 444 | String cropFile = null; |
Lucas Dupin | 284836b | 2017-06-23 15:28:41 -0700 | [diff] [blame] | 445 | int wallpaperId; |
Lucas Dupin | ea1fb1e | 2017-04-05 17:39:44 -0700 | [diff] [blame] | 446 | |
Lucas Dupin | bcae585 | 2017-05-03 12:42:58 -0700 | [diff] [blame] | 447 | synchronized (mLock) { |
Lucas Dupin | 284836b | 2017-06-23 15:28:41 -0700 | [diff] [blame] | 448 | // Not having a wallpaperComponent means it's a lock screen wallpaper. |
| 449 | final boolean imageWallpaper = mImageWallpaper.equals(wallpaper.wallpaperComponent) |
Lucas Dupin | bcae585 | 2017-05-03 12:42:58 -0700 | [diff] [blame] | 450 | || wallpaper.wallpaperComponent == null; |
Lucas Dupin | 284836b | 2017-06-23 15:28:41 -0700 | [diff] [blame] | 451 | if (imageWallpaper && wallpaper.cropFile != null && wallpaper.cropFile.exists()) { |
| 452 | cropFile = wallpaper.cropFile.getAbsolutePath(); |
Lucas Dupin | ea1fb1e | 2017-04-05 17:39:44 -0700 | [diff] [blame] | 453 | } |
| 454 | wallpaperId = wallpaper.wallpaperId; |
| 455 | } |
| 456 | |
Lucas Dupin | 84b89d9 | 2017-05-09 12:16:19 -0700 | [diff] [blame] | 457 | WallpaperColors colors = null; |
Lucas Dupin | ea1fb1e | 2017-04-05 17:39:44 -0700 | [diff] [blame] | 458 | if (cropFile != null) { |
Lucas Dupin | 84b89d9 | 2017-05-09 12:16:19 -0700 | [diff] [blame] | 459 | Bitmap bitmap = BitmapFactory.decodeFile(cropFile); |
Lucas Dupin | 284836b | 2017-06-23 15:28:41 -0700 | [diff] [blame] | 460 | if (bitmap != null) { |
| 461 | colors = WallpaperColors.fromBitmap(bitmap); |
| 462 | bitmap.recycle(); |
| 463 | } |
Lucas Dupin | bcae585 | 2017-05-03 12:42:58 -0700 | [diff] [blame] | 464 | } |
Lucas Dupin | ea1fb1e | 2017-04-05 17:39:44 -0700 | [diff] [blame] | 465 | |
Lucas Dupin | 84b89d9 | 2017-05-09 12:16:19 -0700 | [diff] [blame] | 466 | if (colors == null) { |
Lucas Dupin | bcae585 | 2017-05-03 12:42:58 -0700 | [diff] [blame] | 467 | Slog.w(TAG, "Cannot extract colors because wallpaper could not be read."); |
| 468 | return; |
| 469 | } |
Lucas Dupin | ea1fb1e | 2017-04-05 17:39:44 -0700 | [diff] [blame] | 470 | |
Lucas Dupin | bcae585 | 2017-05-03 12:42:58 -0700 | [diff] [blame] | 471 | synchronized (mLock) { |
| 472 | if (wallpaper.wallpaperId == wallpaperId) { |
Lucas Dupin | 84b89d9 | 2017-05-09 12:16:19 -0700 | [diff] [blame] | 473 | wallpaper.primaryColors = colors; |
Lucas Dupin | 75ec379 | 2017-06-29 14:07:18 -0700 | [diff] [blame] | 474 | // Now that we have the colors, let's save them into the xml |
| 475 | // to avoid having to run this again. |
| 476 | saveSettingsLocked(wallpaper.userId); |
Lucas Dupin | bcae585 | 2017-05-03 12:42:58 -0700 | [diff] [blame] | 477 | } else { |
| 478 | Slog.w(TAG, "Not setting primary colors since wallpaper changed"); |
Lucas Dupin | ea1fb1e | 2017-04-05 17:39:44 -0700 | [diff] [blame] | 479 | } |
| 480 | } |
| 481 | } |
| 482 | |
Christopher Tate | 1e1e2e0 | 2016-01-25 15:34:36 -0800 | [diff] [blame] | 483 | /** |
| 484 | * Once a new wallpaper has been written via setWallpaper(...), it needs to be cropped |
| 485 | * for display. |
| 486 | */ |
| 487 | private void generateCrop(WallpaperData wallpaper) { |
| 488 | boolean success = false; |
Christopher Tate | 1a96b63 | 2016-03-22 15:25:42 -0700 | [diff] [blame] | 489 | |
| 490 | Rect cropHint = new Rect(wallpaper.cropHint); |
Christopher Tate | be132e6 | 2016-02-10 12:59:49 -0800 | [diff] [blame] | 491 | |
| 492 | if (DEBUG) { |
| 493 | Slog.v(TAG, "Generating crop for new wallpaper(s): 0x" |
| 494 | + Integer.toHexString(wallpaper.whichPending) |
Christopher Tate | 1a96b63 | 2016-03-22 15:25:42 -0700 | [diff] [blame] | 495 | + " to " + wallpaper.cropFile.getName() |
| 496 | + " crop=(" + cropHint.width() + 'x' + cropHint.height() |
| 497 | + ") dim=(" + wallpaper.width + 'x' + wallpaper.height + ')'); |
Christopher Tate | be132e6 | 2016-02-10 12:59:49 -0800 | [diff] [blame] | 498 | } |
Christopher Tate | 1e1e2e0 | 2016-01-25 15:34:36 -0800 | [diff] [blame] | 499 | |
| 500 | // Analyse the source; needed in multiple cases |
| 501 | BitmapFactory.Options options = new BitmapFactory.Options(); |
| 502 | options.inJustDecodeBounds = true; |
| 503 | BitmapFactory.decodeFile(wallpaper.wallpaperFile.getAbsolutePath(), options); |
Christopher Tate | 1a96b63 | 2016-03-22 15:25:42 -0700 | [diff] [blame] | 504 | if (options.outWidth <= 0 || options.outHeight <= 0) { |
Joe LaPenna | c298b16 | 2016-05-02 15:25:50 -0700 | [diff] [blame] | 505 | Slog.w(TAG, "Invalid wallpaper data"); |
Christopher Tate | 1a96b63 | 2016-03-22 15:25:42 -0700 | [diff] [blame] | 506 | success = false; |
Christopher Tate | 1e1e2e0 | 2016-01-25 15:34:36 -0800 | [diff] [blame] | 507 | } else { |
Christopher Tate | 1a96b63 | 2016-03-22 15:25:42 -0700 | [diff] [blame] | 508 | boolean needCrop = false; |
| 509 | boolean needScale = false; |
Christopher Tate | 1e1e2e0 | 2016-01-25 15:34:36 -0800 | [diff] [blame] | 510 | |
Christopher Tate | 1a96b63 | 2016-03-22 15:25:42 -0700 | [diff] [blame] | 511 | // Empty crop means use the full image |
| 512 | if (cropHint.isEmpty()) { |
| 513 | cropHint.left = cropHint.top = 0; |
| 514 | cropHint.right = options.outWidth; |
| 515 | cropHint.bottom = options.outHeight; |
| 516 | } else { |
| 517 | // force the crop rect to lie within the measured bounds |
| 518 | cropHint.offset( |
| 519 | (cropHint.right > options.outWidth ? options.outWidth - cropHint.right : 0), |
| 520 | (cropHint.bottom > options.outHeight ? options.outHeight - cropHint.bottom : 0)); |
| 521 | |
Adrian Roos | 5c97ff2 | 2016-08-31 10:25:38 -0700 | [diff] [blame] | 522 | // If the crop hint was larger than the image we just overshot. Patch things up. |
| 523 | if (cropHint.left < 0) { |
| 524 | cropHint.left = 0; |
| 525 | } |
| 526 | if (cropHint.top < 0) { |
| 527 | cropHint.top = 0; |
| 528 | } |
| 529 | |
Christopher Tate | 1a96b63 | 2016-03-22 15:25:42 -0700 | [diff] [blame] | 530 | // Don't bother cropping if what we're left with is identity |
Christopher Tate | ebadfb1 | 2016-07-25 14:50:08 -0700 | [diff] [blame] | 531 | needCrop = (options.outHeight > cropHint.height() |
Adrian Roos | 5c97ff2 | 2016-08-31 10:25:38 -0700 | [diff] [blame] | 532 | || options.outWidth > cropHint.width()); |
Christopher Tate | 1a96b63 | 2016-03-22 15:25:42 -0700 | [diff] [blame] | 533 | } |
| 534 | |
| 535 | // scale if the crop height winds up not matching the recommended metrics |
| 536 | needScale = (wallpaper.height != cropHint.height()); |
| 537 | |
| 538 | if (DEBUG) { |
| 539 | Slog.v(TAG, "crop: w=" + cropHint.width() + " h=" + cropHint.height()); |
| 540 | Slog.v(TAG, "dims: w=" + wallpaper.width + " h=" + wallpaper.height); |
| 541 | Slog.v(TAG, "meas: w=" + options.outWidth + " h=" + options.outHeight); |
| 542 | Slog.v(TAG, "crop?=" + needCrop + " scale?=" + needScale); |
| 543 | } |
| 544 | |
| 545 | if (!needCrop && !needScale) { |
| 546 | // Simple case: the nominal crop fits what we want, so we take |
| 547 | // the whole thing and just copy the image file directly. |
Christopher Tate | 1e1e2e0 | 2016-01-25 15:34:36 -0800 | [diff] [blame] | 548 | if (DEBUG) { |
Christopher Tate | 1a96b63 | 2016-03-22 15:25:42 -0700 | [diff] [blame] | 549 | Slog.v(TAG, "Null crop of new wallpaper; copying"); |
Christopher Tate | 1e1e2e0 | 2016-01-25 15:34:36 -0800 | [diff] [blame] | 550 | } |
Christopher Tate | 1a96b63 | 2016-03-22 15:25:42 -0700 | [diff] [blame] | 551 | success = FileUtils.copyFile(wallpaper.wallpaperFile, wallpaper.cropFile); |
| 552 | if (!success) { |
| 553 | wallpaper.cropFile.delete(); |
| 554 | // TODO: fall back to default wallpaper in this case |
| 555 | } |
| 556 | } else { |
| 557 | // Fancy case: crop and scale. First, we decode and scale down if appropriate. |
| 558 | FileOutputStream f = null; |
| 559 | BufferedOutputStream bos = null; |
| 560 | try { |
| 561 | BitmapRegionDecoder decoder = BitmapRegionDecoder.newInstance( |
| 562 | wallpaper.wallpaperFile.getAbsolutePath(), false); |
| 563 | |
| 564 | // This actually downsamples only by powers of two, but that's okay; we do |
| 565 | // a proper scaling blit later. This is to minimize transient RAM use. |
| 566 | // We calculate the largest power-of-two under the actual ratio rather than |
| 567 | // just let the decode take care of it because we also want to remap where the |
| 568 | // cropHint rectangle lies in the decoded [super]rect. |
| 569 | final BitmapFactory.Options scaler; |
| 570 | final int actualScale = cropHint.height() / wallpaper.height; |
| 571 | int scale = 1; |
| 572 | while (2*scale < actualScale) { |
| 573 | scale *= 2; |
| 574 | } |
| 575 | if (scale > 1) { |
| 576 | scaler = new BitmapFactory.Options(); |
| 577 | scaler.inSampleSize = scale; |
| 578 | if (DEBUG) { |
| 579 | Slog.v(TAG, "Downsampling cropped rect with scale " + scale); |
| 580 | } |
| 581 | } else { |
| 582 | scaler = null; |
| 583 | } |
| 584 | Bitmap cropped = decoder.decodeRegion(cropHint, scaler); |
| 585 | decoder.recycle(); |
| 586 | |
| 587 | if (cropped == null) { |
| 588 | Slog.e(TAG, "Could not decode new wallpaper"); |
| 589 | } else { |
| 590 | // We've got the extracted crop; now we want to scale it properly to |
| 591 | // the desired rectangle. That's a height-biased operation: make it |
| 592 | // fit the hinted height, and accept whatever width we end up with. |
| 593 | cropHint.offsetTo(0, 0); |
| 594 | cropHint.right /= scale; // adjust by downsampling factor |
| 595 | cropHint.bottom /= scale; |
| 596 | final float heightR = ((float)wallpaper.height) / ((float)cropHint.height()); |
| 597 | if (DEBUG) { |
| 598 | Slog.v(TAG, "scale " + heightR + ", extracting " + cropHint); |
| 599 | } |
| 600 | final int destWidth = (int)(cropHint.width() * heightR); |
| 601 | final Bitmap finalCrop = Bitmap.createScaledBitmap(cropped, |
| 602 | destWidth, wallpaper.height, true); |
| 603 | if (DEBUG) { |
| 604 | Slog.v(TAG, "Final extract:"); |
| 605 | Slog.v(TAG, " dims: w=" + wallpaper.width |
| 606 | + " h=" + wallpaper.height); |
| 607 | Slog.v(TAG, " out: w=" + finalCrop.getWidth() |
| 608 | + " h=" + finalCrop.getHeight()); |
| 609 | } |
| 610 | |
| 611 | f = new FileOutputStream(wallpaper.cropFile); |
| 612 | bos = new BufferedOutputStream(f, 32*1024); |
Christopher Tate | c484f54 | 2016-05-11 14:31:34 -0700 | [diff] [blame] | 613 | finalCrop.compress(Bitmap.CompressFormat.JPEG, 100, bos); |
Christopher Tate | 1a96b63 | 2016-03-22 15:25:42 -0700 | [diff] [blame] | 614 | bos.flush(); // don't rely on the implicit flush-at-close when noting success |
| 615 | success = true; |
| 616 | } |
| 617 | } catch (Exception e) { |
| 618 | if (DEBUG) { |
| 619 | Slog.e(TAG, "Error decoding crop", e); |
| 620 | } |
| 621 | } finally { |
| 622 | IoUtils.closeQuietly(bos); |
| 623 | IoUtils.closeQuietly(f); |
| 624 | } |
Christopher Tate | 1e1e2e0 | 2016-01-25 15:34:36 -0800 | [diff] [blame] | 625 | } |
| 626 | } |
| 627 | |
| 628 | if (!success) { |
| 629 | Slog.e(TAG, "Unable to apply new wallpaper"); |
| 630 | wallpaper.cropFile.delete(); |
| 631 | } |
| 632 | |
| 633 | if (wallpaper.cropFile.exists()) { |
| 634 | boolean didRestorecon = SELinux.restorecon(wallpaper.cropFile.getAbsoluteFile()); |
| 635 | if (DEBUG) { |
| 636 | Slog.v(TAG, "restorecon() of crop file returned " + didRestorecon); |
| 637 | } |
| 638 | } |
| 639 | } |
| 640 | |
Dianne Hackborn | 4c62fc0 | 2009-08-08 20:40:27 -0700 | [diff] [blame] | 641 | final Context mContext; |
| 642 | final IWindowManager mIWindowManager; |
Amith Yamasani | 4e2820c | 2012-08-28 22:17:23 -0700 | [diff] [blame] | 643 | final IPackageManager mIPackageManager; |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 644 | final MyPackageMonitor mMonitor; |
Benjamin Franz | f3ece36 | 2015-02-11 10:51:10 +0000 | [diff] [blame] | 645 | final AppOpsManager mAppOpsManager; |
Lucas Dupin | 50ba991 | 2017-07-14 11:55:05 -0700 | [diff] [blame] | 646 | /** |
| 647 | * Map of color listeners per user id. |
| 648 | * The key will be the id of a user or UserHandle.USER_ALL - for wildcard listeners. |
| 649 | */ |
| 650 | final SparseArray<RemoteCallbackList<IWallpaperManagerCallback>> mColorsChangedListeners; |
Amith Yamasani | 37ce3a8 | 2012-02-06 12:04:42 -0800 | [diff] [blame] | 651 | WallpaperData mLastWallpaper; |
Christopher Tate | be132e6 | 2016-02-10 12:59:49 -0800 | [diff] [blame] | 652 | IWallpaperManagerCallback mKeyguardListener; |
Jeff Sharkey | 1cab76a | 2016-04-12 18:23:31 -0600 | [diff] [blame] | 653 | boolean mWaitingForUnlock; |
Christopher Tate | 762dfd1 | 2016-10-10 17:44:48 -0700 | [diff] [blame] | 654 | boolean mShuttingDown; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 655 | |
Justin Koh | 29c3016 | 2014-09-05 17:10:10 -0700 | [diff] [blame] | 656 | /** |
Christopher Tate | ad3c259 | 2016-01-20 18:13:17 -0800 | [diff] [blame] | 657 | * ID of the current wallpaper, changed every time anything sets a wallpaper. |
| 658 | * This is used for external detection of wallpaper update activity. |
| 659 | */ |
| 660 | int mWallpaperId; |
| 661 | |
| 662 | /** |
Justin Koh | 29c3016 | 2014-09-05 17:10:10 -0700 | [diff] [blame] | 663 | * Name of the component used to display bitmap wallpapers from either the gallery or |
| 664 | * built-in wallpapers. |
| 665 | */ |
| 666 | final ComponentName mImageWallpaper; |
| 667 | |
Christopher Tate | 2a6c55f | 2017-03-31 12:28:30 -0700 | [diff] [blame] | 668 | /** |
| 669 | * Name of the default wallpaper component; might be different from mImageWallpaper |
| 670 | */ |
| 671 | final ComponentName mDefaultWallpaperComponent; |
| 672 | |
Christopher Tate | be132e6 | 2016-02-10 12:59:49 -0800 | [diff] [blame] | 673 | final SparseArray<WallpaperData> mWallpaperMap = new SparseArray<WallpaperData>(); |
| 674 | final SparseArray<WallpaperData> mLockWallpaperMap = new SparseArray<WallpaperData>(); |
Dianne Hackborn | 07213e6 | 2011-08-24 20:05:39 -0700 | [diff] [blame] | 675 | |
Christopher Tate | 38a5dc3 | 2016-07-20 15:10:18 -0700 | [diff] [blame] | 676 | final SparseArray<Boolean> mUserRestorecon = new SparseArray<Boolean>(); |
Amith Yamasani | 37ce3a8 | 2012-02-06 12:04:42 -0800 | [diff] [blame] | 677 | int mCurrentUserId; |
Lucas Dupin | 7517b5d | 2017-08-22 12:51:25 -0700 | [diff] [blame] | 678 | boolean mInAmbientMode; |
Dianne Hackborn | 07213e6 | 2011-08-24 20:05:39 -0700 | [diff] [blame] | 679 | |
Amith Yamasani | 37ce3a8 | 2012-02-06 12:04:42 -0800 | [diff] [blame] | 680 | static class WallpaperData { |
| 681 | |
| 682 | int userId; |
| 683 | |
Christopher Tate | be132e6 | 2016-02-10 12:59:49 -0800 | [diff] [blame] | 684 | final File wallpaperFile; // source image |
| 685 | final File cropFile; // eventual destination |
Amith Yamasani | 37ce3a8 | 2012-02-06 12:04:42 -0800 | [diff] [blame] | 686 | |
| 687 | /** |
Christopher Tate | be132e6 | 2016-02-10 12:59:49 -0800 | [diff] [blame] | 688 | * True while the client is writing a new wallpaper |
Amith Yamasani | 37ce3a8 | 2012-02-06 12:04:42 -0800 | [diff] [blame] | 689 | */ |
| 690 | boolean imageWallpaperPending; |
| 691 | |
| 692 | /** |
Christopher Tate | be132e6 | 2016-02-10 12:59:49 -0800 | [diff] [blame] | 693 | * Which new wallpapers are being written; mirrors the 'which' |
| 694 | * selector bit field to setWallpaper(). |
| 695 | */ |
| 696 | int whichPending; |
| 697 | |
| 698 | /** |
Christopher Tate | 1e1e2e0 | 2016-01-25 15:34:36 -0800 | [diff] [blame] | 699 | * Callback once the set + crop is finished |
| 700 | */ |
| 701 | IWallpaperManagerCallback setComplete; |
| 702 | |
| 703 | /** |
Christopher Tate | d7faf53 | 2016-02-25 12:43:38 -0800 | [diff] [blame] | 704 | * Is the OS allowed to back up this wallpaper imagery? |
| 705 | */ |
| 706 | boolean allowBackup; |
| 707 | |
| 708 | /** |
Amith Yamasani | 37ce3a8 | 2012-02-06 12:04:42 -0800 | [diff] [blame] | 709 | * Resource name if using a picture from the wallpaper gallery |
| 710 | */ |
| 711 | String name = ""; |
| 712 | |
| 713 | /** |
| 714 | * The component name of the currently set live wallpaper. |
| 715 | */ |
| 716 | ComponentName wallpaperComponent; |
| 717 | |
| 718 | /** |
| 719 | * The component name of the wallpaper that should be set next. |
| 720 | */ |
| 721 | ComponentName nextWallpaperComponent; |
| 722 | |
Christopher Tate | ad3c259 | 2016-01-20 18:13:17 -0800 | [diff] [blame] | 723 | /** |
| 724 | * The ID of this wallpaper |
| 725 | */ |
| 726 | int wallpaperId; |
| 727 | |
Lucas Dupin | ea1fb1e | 2017-04-05 17:39:44 -0700 | [diff] [blame] | 728 | /** |
| 729 | * Primary colors histogram |
| 730 | */ |
| 731 | WallpaperColors primaryColors; |
| 732 | |
Amith Yamasani | 37ce3a8 | 2012-02-06 12:04:42 -0800 | [diff] [blame] | 733 | WallpaperConnection connection; |
| 734 | long lastDiedTime; |
| 735 | boolean wallpaperUpdating; |
| 736 | WallpaperObserver wallpaperObserver; |
| 737 | |
| 738 | /** |
| 739 | * List of callbacks registered they should each be notified when the wallpaper is changed. |
| 740 | */ |
| 741 | private RemoteCallbackList<IWallpaperManagerCallback> callbacks |
| 742 | = new RemoteCallbackList<IWallpaperManagerCallback>(); |
| 743 | |
| 744 | int width = -1; |
| 745 | int height = -1; |
| 746 | |
Christopher Tate | 1e1e2e0 | 2016-01-25 15:34:36 -0800 | [diff] [blame] | 747 | /** |
| 748 | * The crop hint supplied for displaying a subset of the source image |
| 749 | */ |
| 750 | final Rect cropHint = new Rect(0, 0, 0, 0); |
| 751 | |
Dianne Hackborn | 067e5f6 | 2014-09-07 23:14:30 -0700 | [diff] [blame] | 752 | final Rect padding = new Rect(0, 0, 0, 0); |
| 753 | |
Christopher Tate | be132e6 | 2016-02-10 12:59:49 -0800 | [diff] [blame] | 754 | WallpaperData(int userId, String inputFileName, String cropFileName) { |
Amith Yamasani | 37ce3a8 | 2012-02-06 12:04:42 -0800 | [diff] [blame] | 755 | this.userId = userId; |
Christopher Tate | be132e6 | 2016-02-10 12:59:49 -0800 | [diff] [blame] | 756 | final File wallpaperDir = getWallpaperDir(userId); |
| 757 | wallpaperFile = new File(wallpaperDir, inputFileName); |
| 758 | cropFile = new File(wallpaperDir, cropFileName); |
Christopher Tate | 1e1e2e0 | 2016-01-25 15:34:36 -0800 | [diff] [blame] | 759 | } |
| 760 | |
Christopher Tate | be132e6 | 2016-02-10 12:59:49 -0800 | [diff] [blame] | 761 | // Called during initialization of a given user's wallpaper bookkeeping |
Christopher Tate | 41297ff | 2016-03-10 16:46:15 -0800 | [diff] [blame] | 762 | boolean cropExists() { |
| 763 | return cropFile.exists(); |
Amith Yamasani | 37ce3a8 | 2012-02-06 12:04:42 -0800 | [diff] [blame] | 764 | } |
Christopher Tate | 2a6c55f | 2017-03-31 12:28:30 -0700 | [diff] [blame] | 765 | |
| 766 | boolean sourceExists() { |
| 767 | return wallpaperFile.exists(); |
| 768 | } |
Amith Yamasani | 37ce3a8 | 2012-02-06 12:04:42 -0800 | [diff] [blame] | 769 | } |
| 770 | |
Christopher Tate | ad3c259 | 2016-01-20 18:13:17 -0800 | [diff] [blame] | 771 | int makeWallpaperIdLocked() { |
| 772 | do { |
| 773 | ++mWallpaperId; |
| 774 | } while (mWallpaperId == 0); |
| 775 | return mWallpaperId; |
| 776 | } |
| 777 | |
Dianne Hackborn | 4c62fc0 | 2009-08-08 20:40:27 -0700 | [diff] [blame] | 778 | class WallpaperConnection extends IWallpaperConnection.Stub |
| 779 | implements ServiceConnection { |
Adrian Roos | c3f915e | 2016-09-06 11:40:53 -0700 | [diff] [blame] | 780 | |
| 781 | /** Time in milliseconds until we expect the wallpaper to reconnect (unless we're in the |
| 782 | * middle of an update). If exceeded, the wallpaper gets reset to the system default. */ |
Christopher Tate | 2a6c55f | 2017-03-31 12:28:30 -0700 | [diff] [blame] | 783 | private static final long WALLPAPER_RECONNECT_TIMEOUT_MS = 10000; |
Adrian Roos | c3f915e | 2016-09-06 11:40:53 -0700 | [diff] [blame] | 784 | |
Dianne Hackborn | eb03465 | 2009-09-07 00:49:58 -0700 | [diff] [blame] | 785 | final WallpaperInfo mInfo; |
Dianne Hackborn | 4c62fc0 | 2009-08-08 20:40:27 -0700 | [diff] [blame] | 786 | final Binder mToken = new Binder(); |
| 787 | IWallpaperService mService; |
| 788 | IWallpaperEngine mEngine; |
Amith Yamasani | 37ce3a8 | 2012-02-06 12:04:42 -0800 | [diff] [blame] | 789 | WallpaperData mWallpaper; |
Dianne Hackborn | 5dc5a00 | 2012-09-15 19:33:48 -0700 | [diff] [blame] | 790 | IRemoteCallback mReply; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 791 | |
Michael Wright | 5203a8b | 2013-10-03 14:16:42 -0700 | [diff] [blame] | 792 | boolean mDimensionsChanged = false; |
Dianne Hackborn | 067e5f6 | 2014-09-07 23:14:30 -0700 | [diff] [blame] | 793 | boolean mPaddingChanged = false; |
Michael Wright | 5203a8b | 2013-10-03 14:16:42 -0700 | [diff] [blame] | 794 | |
Adrian Roos | c3f915e | 2016-09-06 11:40:53 -0700 | [diff] [blame] | 795 | private Runnable mResetRunnable = () -> { |
| 796 | synchronized (mLock) { |
Christopher Tate | 762dfd1 | 2016-10-10 17:44:48 -0700 | [diff] [blame] | 797 | if (mShuttingDown) { |
| 798 | // Don't expect wallpaper services to relaunch during shutdown |
Christopher Tate | 2a6c55f | 2017-03-31 12:28:30 -0700 | [diff] [blame] | 799 | if (DEBUG_LIVE) { |
Christopher Tate | 762dfd1 | 2016-10-10 17:44:48 -0700 | [diff] [blame] | 800 | Slog.i(TAG, "Ignoring relaunch timeout during shutdown"); |
| 801 | } |
| 802 | return; |
| 803 | } |
| 804 | |
Adrian Roos | c3f915e | 2016-09-06 11:40:53 -0700 | [diff] [blame] | 805 | if (!mWallpaper.wallpaperUpdating |
| 806 | && mWallpaper.userId == mCurrentUserId) { |
Christopher Tate | 2a6c55f | 2017-03-31 12:28:30 -0700 | [diff] [blame] | 807 | Slog.w(TAG, "Wallpaper reconnect timed out for " + mWallpaper.wallpaperComponent |
| 808 | + ", reverting to built-in wallpaper!"); |
Adrian Roos | c3f915e | 2016-09-06 11:40:53 -0700 | [diff] [blame] | 809 | clearWallpaperLocked(true, FLAG_SYSTEM, mWallpaper.userId, |
| 810 | null); |
| 811 | } |
| 812 | } |
| 813 | }; |
| 814 | |
Amith Yamasani | 37ce3a8 | 2012-02-06 12:04:42 -0800 | [diff] [blame] | 815 | public WallpaperConnection(WallpaperInfo info, WallpaperData wallpaper) { |
Dianne Hackborn | eb03465 | 2009-09-07 00:49:58 -0700 | [diff] [blame] | 816 | mInfo = info; |
Amith Yamasani | 37ce3a8 | 2012-02-06 12:04:42 -0800 | [diff] [blame] | 817 | mWallpaper = wallpaper; |
Dianne Hackborn | eb03465 | 2009-09-07 00:49:58 -0700 | [diff] [blame] | 818 | } |
Dianne Hackborn | 5dc5a00 | 2012-09-15 19:33:48 -0700 | [diff] [blame] | 819 | |
| 820 | @Override |
Dianne Hackborn | 4c62fc0 | 2009-08-08 20:40:27 -0700 | [diff] [blame] | 821 | public void onServiceConnected(ComponentName name, IBinder service) { |
| 822 | synchronized (mLock) { |
Amith Yamasani | 37ce3a8 | 2012-02-06 12:04:42 -0800 | [diff] [blame] | 823 | if (mWallpaper.connection == this) { |
Dianne Hackborn | 4c62fc0 | 2009-08-08 20:40:27 -0700 | [diff] [blame] | 824 | mService = IWallpaperService.Stub.asInterface(service); |
Amith Yamasani | 37ce3a8 | 2012-02-06 12:04:42 -0800 | [diff] [blame] | 825 | attachServiceLocked(this, mWallpaper); |
Dianne Hackborn | eb03465 | 2009-09-07 00:49:58 -0700 | [diff] [blame] | 826 | // XXX should probably do saveSettingsLocked() later |
| 827 | // when we have an engine, but I'm not sure about |
| 828 | // locking there and anyway we always need to be able to |
| 829 | // recover if there is something wrong. |
Christopher Tate | db27b84 | 2016-02-25 14:39:17 -0800 | [diff] [blame] | 830 | saveSettingsLocked(mWallpaper.userId); |
Adrian Roos | c3f915e | 2016-09-06 11:40:53 -0700 | [diff] [blame] | 831 | FgThread.getHandler().removeCallbacks(mResetRunnable); |
Dianne Hackborn | 4c62fc0 | 2009-08-08 20:40:27 -0700 | [diff] [blame] | 832 | } |
| 833 | } |
| 834 | } |
| 835 | |
Dianne Hackborn | 5dc5a00 | 2012-09-15 19:33:48 -0700 | [diff] [blame] | 836 | @Override |
Dianne Hackborn | 4c62fc0 | 2009-08-08 20:40:27 -0700 | [diff] [blame] | 837 | public void onServiceDisconnected(ComponentName name) { |
| 838 | synchronized (mLock) { |
Christopher Tate | c349e59f | 2017-05-05 17:37:43 -0700 | [diff] [blame] | 839 | Slog.w(TAG, "Wallpaper service gone: " + name); |
| 840 | if (!Objects.equals(name, mWallpaper.wallpaperComponent)) { |
| 841 | Slog.e(TAG, "Does not match expected wallpaper component " |
| 842 | + mWallpaper.wallpaperComponent); |
| 843 | } |
Dianne Hackborn | 4c62fc0 | 2009-08-08 20:40:27 -0700 | [diff] [blame] | 844 | mService = null; |
| 845 | mEngine = null; |
Amith Yamasani | 37ce3a8 | 2012-02-06 12:04:42 -0800 | [diff] [blame] | 846 | if (mWallpaper.connection == this) { |
Christopher Tate | c349e59f | 2017-05-05 17:37:43 -0700 | [diff] [blame] | 847 | // There is an inherent ordering race between this callback and the |
| 848 | // package monitor that receives notice that a package is being updated, |
| 849 | // so we cannot quite trust at this moment that we know for sure that |
| 850 | // this is not an update. If we think this is a genuine non-update |
| 851 | // wallpaper outage, we do our "wait for reset" work as a continuation, |
| 852 | // a short time in the future, specifically to allow any pending package |
| 853 | // update message on this same looper thread to be processed. |
| 854 | if (!mWallpaper.wallpaperUpdating) { |
| 855 | mContext.getMainThreadHandler().postDelayed(() -> processDisconnect(this), |
| 856 | 1000); |
| 857 | } |
| 858 | } |
| 859 | } |
| 860 | } |
| 861 | |
Tetsutoki Shiozawa | 0691921 | 2018-02-02 14:18:43 +0900 | [diff] [blame] | 862 | public void scheduleTimeoutLocked() { |
| 863 | // If we didn't reset it right away, do so after we couldn't connect to |
| 864 | // it for an extended amount of time to avoid having a black wallpaper. |
| 865 | final Handler fgHandler = FgThread.getHandler(); |
| 866 | fgHandler.removeCallbacks(mResetRunnable); |
| 867 | fgHandler.postDelayed(mResetRunnable, WALLPAPER_RECONNECT_TIMEOUT_MS); |
| 868 | if (DEBUG_LIVE) { |
| 869 | Slog.i(TAG, "Started wallpaper reconnect timeout for " + mWallpaper.wallpaperComponent); |
| 870 | } |
| 871 | } |
| 872 | |
Christopher Tate | c349e59f | 2017-05-05 17:37:43 -0700 | [diff] [blame] | 873 | private void processDisconnect(final ServiceConnection connection) { |
| 874 | synchronized (mLock) { |
| 875 | // The wallpaper disappeared. If this isn't a system-default one, track |
| 876 | // crashes and fall back to default if it continues to misbehave. |
| 877 | if (connection == mWallpaper.connection) { |
Christopher Tate | 2a6c55f | 2017-03-31 12:28:30 -0700 | [diff] [blame] | 878 | final ComponentName wpService = mWallpaper.wallpaperComponent; |
Amith Yamasani | 37ce3a8 | 2012-02-06 12:04:42 -0800 | [diff] [blame] | 879 | if (!mWallpaper.wallpaperUpdating |
Christopher Tate | 2a6c55f | 2017-03-31 12:28:30 -0700 | [diff] [blame] | 880 | && mWallpaper.userId == mCurrentUserId |
| 881 | && !Objects.equals(mDefaultWallpaperComponent, wpService) |
| 882 | && !Objects.equals(mImageWallpaper, wpService)) { |
Selim Cinek | ebebadb | 2014-03-05 22:17:26 +0100 | [diff] [blame] | 883 | // There is a race condition which causes |
| 884 | // {@link #mWallpaper.wallpaperUpdating} to be false even if it is |
| 885 | // currently updating since the broadcast notifying us is async. |
| 886 | // This race is overcome by the general rule that we only reset the |
| 887 | // wallpaper if its service was shut down twice |
| 888 | // during {@link #MIN_WALLPAPER_CRASH_TIME} millis. |
| 889 | if (mWallpaper.lastDiedTime != 0 |
| 890 | && mWallpaper.lastDiedTime + MIN_WALLPAPER_CRASH_TIME |
Christopher Tate | c349e59f | 2017-05-05 17:37:43 -0700 | [diff] [blame] | 891 | > SystemClock.uptimeMillis()) { |
Selim Cinek | ebebadb | 2014-03-05 22:17:26 +0100 | [diff] [blame] | 892 | Slog.w(TAG, "Reverting to built-in wallpaper!"); |
Christopher Tate | edf7d04 | 2016-03-29 18:24:25 -0700 | [diff] [blame] | 893 | clearWallpaperLocked(true, FLAG_SYSTEM, mWallpaper.userId, null); |
Selim Cinek | ebebadb | 2014-03-05 22:17:26 +0100 | [diff] [blame] | 894 | } else { |
| 895 | mWallpaper.lastDiedTime = SystemClock.uptimeMillis(); |
Adrian Roos | c3f915e | 2016-09-06 11:40:53 -0700 | [diff] [blame] | 896 | |
Tetsutoki Shiozawa | 0691921 | 2018-02-02 14:18:43 +0900 | [diff] [blame] | 897 | clearWallpaperComponentLocked(mWallpaper); |
| 898 | if (bindWallpaperComponentLocked( |
| 899 | wpService, false, false, mWallpaper, null)) { |
| 900 | mWallpaper.connection.scheduleTimeoutLocked(); |
| 901 | } else { |
| 902 | Slog.w(TAG, "Reverting to built-in wallpaper!"); |
| 903 | clearWallpaperLocked(true, FLAG_SYSTEM, mWallpaper.userId, null); |
Christopher Tate | 2a6c55f | 2017-03-31 12:28:30 -0700 | [diff] [blame] | 904 | } |
Selim Cinek | ebebadb | 2014-03-05 22:17:26 +0100 | [diff] [blame] | 905 | } |
Christopher Tate | c349e59f | 2017-05-05 17:37:43 -0700 | [diff] [blame] | 906 | final String flattened = wpService.flattenToString(); |
Filip Gruszczynski | 5dcc3ac | 2014-10-13 15:51:39 -0700 | [diff] [blame] | 907 | EventLog.writeEvent(EventLogTags.WP_WALLPAPER_CRASHED, |
Filip Gruszczynski | 5a58943 | 2014-10-14 12:06:06 -0700 | [diff] [blame] | 908 | flattened.substring(0, Math.min(flattened.length(), |
| 909 | MAX_WALLPAPER_COMPONENT_LOG_LENGTH))); |
Dianne Hackborn | 0cd4887 | 2009-08-13 18:51:59 -0700 | [diff] [blame] | 910 | } |
Christopher Tate | c349e59f | 2017-05-05 17:37:43 -0700 | [diff] [blame] | 911 | } else { |
| 912 | if (DEBUG_LIVE) { |
| 913 | Slog.i(TAG, "Wallpaper changed during disconnect tracking; ignoring"); |
| 914 | } |
Dianne Hackborn | 0cd4887 | 2009-08-13 18:51:59 -0700 | [diff] [blame] | 915 | } |
Dianne Hackborn | 4c62fc0 | 2009-08-08 20:40:27 -0700 | [diff] [blame] | 916 | } |
| 917 | } |
Amith Yamasani | 37ce3a8 | 2012-02-06 12:04:42 -0800 | [diff] [blame] | 918 | |
Lucas Dupin | ea1fb1e | 2017-04-05 17:39:44 -0700 | [diff] [blame] | 919 | /** |
| 920 | * Called by a live wallpaper if its colors have changed. |
| 921 | * @param primaryColors representation of wallpaper primary colors |
| 922 | */ |
| 923 | @Override |
| 924 | public void onWallpaperColorsChanged(WallpaperColors primaryColors) { |
| 925 | int which; |
| 926 | synchronized (mLock) { |
| 927 | // Do not broadcast changes on ImageWallpaper since it's handled |
| 928 | // internally by this class. |
| 929 | if (mImageWallpaper.equals(mWallpaper.wallpaperComponent)) { |
| 930 | return; |
| 931 | } |
| 932 | |
| 933 | mWallpaper.primaryColors = primaryColors; |
| 934 | |
| 935 | // Live wallpapers always are system wallpapers. |
| 936 | which = FLAG_SYSTEM; |
| 937 | // It's also the lock screen wallpaper when we don't have a bitmap in there |
Lucas Dupin | 50ba991 | 2017-07-14 11:55:05 -0700 | [diff] [blame] | 938 | WallpaperData lockedWallpaper = mLockWallpaperMap.get(mWallpaper.userId); |
Lucas Dupin | ea1fb1e | 2017-04-05 17:39:44 -0700 | [diff] [blame] | 939 | if (lockedWallpaper == null) { |
| 940 | which |= FLAG_LOCK; |
| 941 | } |
| 942 | } |
| 943 | if (which != 0) { |
| 944 | notifyWallpaperColorsChanged(mWallpaper, which); |
| 945 | } |
| 946 | } |
| 947 | |
Dianne Hackborn | 5dc5a00 | 2012-09-15 19:33:48 -0700 | [diff] [blame] | 948 | @Override |
Dianne Hackborn | 4c62fc0 | 2009-08-08 20:40:27 -0700 | [diff] [blame] | 949 | public void attachEngine(IWallpaperEngine engine) { |
Dianne Hackborn | 5dc5a00 | 2012-09-15 19:33:48 -0700 | [diff] [blame] | 950 | synchronized (mLock) { |
| 951 | mEngine = engine; |
Michael Wright | 5203a8b | 2013-10-03 14:16:42 -0700 | [diff] [blame] | 952 | if (mDimensionsChanged) { |
| 953 | try { |
| 954 | mEngine.setDesiredSize(mWallpaper.width, mWallpaper.height); |
| 955 | } catch (RemoteException e) { |
| 956 | Slog.w(TAG, "Failed to set wallpaper dimensions", e); |
| 957 | } |
| 958 | mDimensionsChanged = false; |
| 959 | } |
Dianne Hackborn | 067e5f6 | 2014-09-07 23:14:30 -0700 | [diff] [blame] | 960 | if (mPaddingChanged) { |
| 961 | try { |
| 962 | mEngine.setDisplayPadding(mWallpaper.padding); |
| 963 | } catch (RemoteException e) { |
| 964 | Slog.w(TAG, "Failed to set wallpaper padding", e); |
| 965 | } |
| 966 | mPaddingChanged = false; |
| 967 | } |
Lucas Dupin | 7517b5d | 2017-08-22 12:51:25 -0700 | [diff] [blame] | 968 | if (mInfo != null && mInfo.getSupportsAmbientMode()) { |
| 969 | try { |
Lucas Dupin | 660d573 | 2017-12-19 10:05:19 -0800 | [diff] [blame] | 970 | mEngine.setInAmbientMode(mInAmbientMode, false /* animated */); |
Lucas Dupin | 7517b5d | 2017-08-22 12:51:25 -0700 | [diff] [blame] | 971 | } catch (RemoteException e) { |
| 972 | Slog.w(TAG, "Failed to set ambient mode state", e); |
| 973 | } |
| 974 | } |
Lucas Dupin | 50ba991 | 2017-07-14 11:55:05 -0700 | [diff] [blame] | 975 | try { |
| 976 | // This will trigger onComputeColors in the wallpaper engine. |
| 977 | // It's fine to be locked in here since the binder is oneway. |
| 978 | mEngine.requestWallpaperColors(); |
| 979 | } catch (RemoteException e) { |
| 980 | Slog.w(TAG, "Failed to request wallpaper colors", e); |
| 981 | } |
Dianne Hackborn | 5dc5a00 | 2012-09-15 19:33:48 -0700 | [diff] [blame] | 982 | } |
Dianne Hackborn | 4c62fc0 | 2009-08-08 20:40:27 -0700 | [diff] [blame] | 983 | } |
Amith Yamasani | 37ce3a8 | 2012-02-06 12:04:42 -0800 | [diff] [blame] | 984 | |
Dianne Hackborn | 5dc5a00 | 2012-09-15 19:33:48 -0700 | [diff] [blame] | 985 | @Override |
| 986 | public void engineShown(IWallpaperEngine engine) { |
| 987 | synchronized (mLock) { |
| 988 | if (mReply != null) { |
| 989 | long ident = Binder.clearCallingIdentity(); |
| 990 | try { |
| 991 | mReply.sendResult(null); |
| 992 | } catch (RemoteException e) { |
| 993 | Binder.restoreCallingIdentity(ident); |
| 994 | } |
| 995 | mReply = null; |
| 996 | } |
| 997 | } |
| 998 | } |
| 999 | |
| 1000 | @Override |
Dianne Hackborn | 4c62fc0 | 2009-08-08 20:40:27 -0700 | [diff] [blame] | 1001 | public ParcelFileDescriptor setWallpaper(String name) { |
| 1002 | synchronized (mLock) { |
Amith Yamasani | 37ce3a8 | 2012-02-06 12:04:42 -0800 | [diff] [blame] | 1003 | if (mWallpaper.connection == this) { |
Christopher Tate | ad3c259 | 2016-01-20 18:13:17 -0800 | [diff] [blame] | 1004 | return updateWallpaperBitmapLocked(name, mWallpaper, null); |
Dianne Hackborn | 4c62fc0 | 2009-08-08 20:40:27 -0700 | [diff] [blame] | 1005 | } |
| 1006 | return null; |
| 1007 | } |
| 1008 | } |
| 1009 | } |
Amith Yamasani | 37ce3a8 | 2012-02-06 12:04:42 -0800 | [diff] [blame] | 1010 | |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 1011 | class MyPackageMonitor extends PackageMonitor { |
| 1012 | @Override |
| 1013 | public void onPackageUpdateFinished(String packageName, int uid) { |
| 1014 | synchronized (mLock) { |
Dianne Hackborn | c72fc67 | 2012-09-20 13:12:03 -0700 | [diff] [blame] | 1015 | if (mCurrentUserId != getChangingUserId()) { |
| 1016 | return; |
| 1017 | } |
| 1018 | WallpaperData wallpaper = mWallpaperMap.get(mCurrentUserId); |
| 1019 | if (wallpaper != null) { |
Christopher Tate | 2a6c55f | 2017-03-31 12:28:30 -0700 | [diff] [blame] | 1020 | final ComponentName wpService = wallpaper.wallpaperComponent; |
| 1021 | if (wpService != null && wpService.getPackageName().equals(packageName)) { |
| 1022 | if (DEBUG_LIVE) { |
| 1023 | Slog.i(TAG, "Wallpaper " + wpService + " update has finished"); |
| 1024 | } |
Amith Yamasani | 37ce3a8 | 2012-02-06 12:04:42 -0800 | [diff] [blame] | 1025 | wallpaper.wallpaperUpdating = false; |
Amith Yamasani | 37ce3a8 | 2012-02-06 12:04:42 -0800 | [diff] [blame] | 1026 | clearWallpaperComponentLocked(wallpaper); |
Christopher Tate | 2a6c55f | 2017-03-31 12:28:30 -0700 | [diff] [blame] | 1027 | if (!bindWallpaperComponentLocked(wpService, false, false, |
Dianne Hackborn | c72fc67 | 2012-09-20 13:12:03 -0700 | [diff] [blame] | 1028 | wallpaper, null)) { |
Christopher Tate | 2a6c55f | 2017-03-31 12:28:30 -0700 | [diff] [blame] | 1029 | Slog.w(TAG, "Wallpaper " + wpService |
| 1030 | + " no longer available; reverting to default"); |
Christopher Tate | edf7d04 | 2016-03-29 18:24:25 -0700 | [diff] [blame] | 1031 | clearWallpaperLocked(false, FLAG_SYSTEM, wallpaper.userId, null); |
Amith Yamasani | 37ce3a8 | 2012-02-06 12:04:42 -0800 | [diff] [blame] | 1032 | } |
Dianne Hackborn | 80b902f | 2011-09-15 15:15:27 -0700 | [diff] [blame] | 1033 | } |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 1034 | } |
| 1035 | } |
| 1036 | } |
| 1037 | |
| 1038 | @Override |
Dianne Hackborn | 80b902f | 2011-09-15 15:15:27 -0700 | [diff] [blame] | 1039 | public void onPackageModified(String packageName) { |
| 1040 | synchronized (mLock) { |
Dianne Hackborn | c72fc67 | 2012-09-20 13:12:03 -0700 | [diff] [blame] | 1041 | if (mCurrentUserId != getChangingUserId()) { |
| 1042 | return; |
| 1043 | } |
| 1044 | WallpaperData wallpaper = mWallpaperMap.get(mCurrentUserId); |
| 1045 | if (wallpaper != null) { |
Amith Yamasani | 37ce3a8 | 2012-02-06 12:04:42 -0800 | [diff] [blame] | 1046 | if (wallpaper.wallpaperComponent == null |
| 1047 | || !wallpaper.wallpaperComponent.getPackageName().equals(packageName)) { |
Dianne Hackborn | c72fc67 | 2012-09-20 13:12:03 -0700 | [diff] [blame] | 1048 | return; |
Amith Yamasani | 37ce3a8 | 2012-02-06 12:04:42 -0800 | [diff] [blame] | 1049 | } |
Dianne Hackborn | d0d7503 | 2012-04-19 23:12:09 -0700 | [diff] [blame] | 1050 | doPackagesChangedLocked(true, wallpaper); |
Dianne Hackborn | 80b902f | 2011-09-15 15:15:27 -0700 | [diff] [blame] | 1051 | } |
| 1052 | } |
Dianne Hackborn | 80b902f | 2011-09-15 15:15:27 -0700 | [diff] [blame] | 1053 | } |
| 1054 | |
| 1055 | @Override |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 1056 | public void onPackageUpdateStarted(String packageName, int uid) { |
| 1057 | synchronized (mLock) { |
Dianne Hackborn | c72fc67 | 2012-09-20 13:12:03 -0700 | [diff] [blame] | 1058 | if (mCurrentUserId != getChangingUserId()) { |
| 1059 | return; |
| 1060 | } |
| 1061 | WallpaperData wallpaper = mWallpaperMap.get(mCurrentUserId); |
| 1062 | if (wallpaper != null) { |
Amith Yamasani | 37ce3a8 | 2012-02-06 12:04:42 -0800 | [diff] [blame] | 1063 | if (wallpaper.wallpaperComponent != null |
| 1064 | && wallpaper.wallpaperComponent.getPackageName().equals(packageName)) { |
Christopher Tate | 2a6c55f | 2017-03-31 12:28:30 -0700 | [diff] [blame] | 1065 | if (DEBUG_LIVE) { |
| 1066 | Slog.i(TAG, "Wallpaper service " + wallpaper.wallpaperComponent |
| 1067 | + " is updating"); |
| 1068 | } |
Amith Yamasani | 37ce3a8 | 2012-02-06 12:04:42 -0800 | [diff] [blame] | 1069 | wallpaper.wallpaperUpdating = true; |
Adrian Roos | c3f915e | 2016-09-06 11:40:53 -0700 | [diff] [blame] | 1070 | if (wallpaper.connection != null) { |
| 1071 | FgThread.getHandler().removeCallbacks( |
| 1072 | wallpaper.connection.mResetRunnable); |
| 1073 | } |
Amith Yamasani | 37ce3a8 | 2012-02-06 12:04:42 -0800 | [diff] [blame] | 1074 | } |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 1075 | } |
| 1076 | } |
| 1077 | } |
| 1078 | |
| 1079 | @Override |
| 1080 | public boolean onHandleForceStop(Intent intent, String[] packages, int uid, boolean doit) { |
Dianne Hackborn | d0d7503 | 2012-04-19 23:12:09 -0700 | [diff] [blame] | 1081 | synchronized (mLock) { |
| 1082 | boolean changed = false; |
Dianne Hackborn | c72fc67 | 2012-09-20 13:12:03 -0700 | [diff] [blame] | 1083 | if (mCurrentUserId != getChangingUserId()) { |
| 1084 | return false; |
| 1085 | } |
| 1086 | WallpaperData wallpaper = mWallpaperMap.get(mCurrentUserId); |
| 1087 | if (wallpaper != null) { |
Dianne Hackborn | d0d7503 | 2012-04-19 23:12:09 -0700 | [diff] [blame] | 1088 | boolean res = doPackagesChangedLocked(doit, wallpaper); |
| 1089 | changed |= res; |
| 1090 | } |
| 1091 | return changed; |
Amith Yamasani | 37ce3a8 | 2012-02-06 12:04:42 -0800 | [diff] [blame] | 1092 | } |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 1093 | } |
| 1094 | |
| 1095 | @Override |
| 1096 | public void onSomePackagesChanged() { |
Dianne Hackborn | d0d7503 | 2012-04-19 23:12:09 -0700 | [diff] [blame] | 1097 | synchronized (mLock) { |
Dianne Hackborn | c72fc67 | 2012-09-20 13:12:03 -0700 | [diff] [blame] | 1098 | if (mCurrentUserId != getChangingUserId()) { |
| 1099 | return; |
| 1100 | } |
| 1101 | WallpaperData wallpaper = mWallpaperMap.get(mCurrentUserId); |
| 1102 | if (wallpaper != null) { |
Dianne Hackborn | d0d7503 | 2012-04-19 23:12:09 -0700 | [diff] [blame] | 1103 | doPackagesChangedLocked(true, wallpaper); |
| 1104 | } |
Amith Yamasani | 37ce3a8 | 2012-02-06 12:04:42 -0800 | [diff] [blame] | 1105 | } |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 1106 | } |
Amith Yamasani | 37ce3a8 | 2012-02-06 12:04:42 -0800 | [diff] [blame] | 1107 | |
Dianne Hackborn | d0d7503 | 2012-04-19 23:12:09 -0700 | [diff] [blame] | 1108 | boolean doPackagesChangedLocked(boolean doit, WallpaperData wallpaper) { |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 1109 | boolean changed = false; |
Dianne Hackborn | d0d7503 | 2012-04-19 23:12:09 -0700 | [diff] [blame] | 1110 | if (wallpaper.wallpaperComponent != null) { |
| 1111 | int change = isPackageDisappearing(wallpaper.wallpaperComponent |
| 1112 | .getPackageName()); |
| 1113 | if (change == PACKAGE_PERMANENT_CHANGE |
| 1114 | || change == PACKAGE_TEMPORARY_CHANGE) { |
| 1115 | changed = true; |
| 1116 | if (doit) { |
| 1117 | Slog.w(TAG, "Wallpaper uninstalled, removing: " |
Amith Yamasani | 37ce3a8 | 2012-02-06 12:04:42 -0800 | [diff] [blame] | 1118 | + wallpaper.wallpaperComponent); |
Christopher Tate | edf7d04 | 2016-03-29 18:24:25 -0700 | [diff] [blame] | 1119 | clearWallpaperLocked(false, FLAG_SYSTEM, wallpaper.userId, null); |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 1120 | } |
| 1121 | } |
Dianne Hackborn | d0d7503 | 2012-04-19 23:12:09 -0700 | [diff] [blame] | 1122 | } |
| 1123 | if (wallpaper.nextWallpaperComponent != null) { |
| 1124 | int change = isPackageDisappearing(wallpaper.nextWallpaperComponent |
| 1125 | .getPackageName()); |
| 1126 | if (change == PACKAGE_PERMANENT_CHANGE |
| 1127 | || change == PACKAGE_TEMPORARY_CHANGE) { |
| 1128 | wallpaper.nextWallpaperComponent = null; |
| 1129 | } |
| 1130 | } |
| 1131 | if (wallpaper.wallpaperComponent != null |
| 1132 | && isPackageModified(wallpaper.wallpaperComponent.getPackageName())) { |
| 1133 | try { |
Jeff Sharkey | 1cab76a | 2016-04-12 18:23:31 -0600 | [diff] [blame] | 1134 | mContext.getPackageManager().getServiceInfo(wallpaper.wallpaperComponent, |
| 1135 | PackageManager.MATCH_DIRECT_BOOT_AWARE |
| 1136 | | PackageManager.MATCH_DIRECT_BOOT_UNAWARE); |
Dianne Hackborn | d0d7503 | 2012-04-19 23:12:09 -0700 | [diff] [blame] | 1137 | } catch (NameNotFoundException e) { |
| 1138 | Slog.w(TAG, "Wallpaper component gone, removing: " |
| 1139 | + wallpaper.wallpaperComponent); |
Christopher Tate | edf7d04 | 2016-03-29 18:24:25 -0700 | [diff] [blame] | 1140 | clearWallpaperLocked(false, FLAG_SYSTEM, wallpaper.userId, null); |
Dianne Hackborn | d0d7503 | 2012-04-19 23:12:09 -0700 | [diff] [blame] | 1141 | } |
| 1142 | } |
| 1143 | if (wallpaper.nextWallpaperComponent != null |
| 1144 | && isPackageModified(wallpaper.nextWallpaperComponent.getPackageName())) { |
| 1145 | try { |
Jeff Sharkey | 1cab76a | 2016-04-12 18:23:31 -0600 | [diff] [blame] | 1146 | mContext.getPackageManager().getServiceInfo(wallpaper.nextWallpaperComponent, |
| 1147 | PackageManager.MATCH_DIRECT_BOOT_AWARE |
| 1148 | | PackageManager.MATCH_DIRECT_BOOT_UNAWARE); |
Dianne Hackborn | d0d7503 | 2012-04-19 23:12:09 -0700 | [diff] [blame] | 1149 | } catch (NameNotFoundException e) { |
| 1150 | wallpaper.nextWallpaperComponent = null; |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 1151 | } |
| 1152 | } |
| 1153 | return changed; |
| 1154 | } |
| 1155 | } |
Oleksandr Peletskyi | f251981 | 2016-01-26 20:16:06 +0100 | [diff] [blame] | 1156 | |
Dianne Hackborn | 8cc6a50 | 2009-08-05 21:29:42 -0700 | [diff] [blame] | 1157 | public WallpaperManagerService(Context context) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1158 | if (DEBUG) Slog.v(TAG, "WallpaperService startup"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1159 | mContext = context; |
Christopher Tate | 762dfd1 | 2016-10-10 17:44:48 -0700 | [diff] [blame] | 1160 | mShuttingDown = false; |
Justin Koh | 29c3016 | 2014-09-05 17:10:10 -0700 | [diff] [blame] | 1161 | mImageWallpaper = ComponentName.unflattenFromString( |
| 1162 | context.getResources().getString(R.string.image_wallpaper_component)); |
Christopher Tate | 2a6c55f | 2017-03-31 12:28:30 -0700 | [diff] [blame] | 1163 | mDefaultWallpaperComponent = WallpaperManager.getDefaultWallpaperComponent(context); |
Dianne Hackborn | 4c62fc0 | 2009-08-08 20:40:27 -0700 | [diff] [blame] | 1164 | mIWindowManager = IWindowManager.Stub.asInterface( |
| 1165 | ServiceManager.getService(Context.WINDOW_SERVICE)); |
Amith Yamasani | 4e2820c | 2012-08-28 22:17:23 -0700 | [diff] [blame] | 1166 | mIPackageManager = AppGlobals.getPackageManager(); |
Benjamin Franz | f3ece36 | 2015-02-11 10:51:10 +0000 | [diff] [blame] | 1167 | mAppOpsManager = (AppOpsManager) mContext.getSystemService(Context.APP_OPS_SERVICE); |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 1168 | mMonitor = new MyPackageMonitor(); |
Dianne Hackborn | c72fc67 | 2012-09-20 13:12:03 -0700 | [diff] [blame] | 1169 | mMonitor.register(context, null, UserHandle.ALL, true); |
Xiaohui Chen | 233d94c | 2015-07-30 15:08:00 -0700 | [diff] [blame] | 1170 | getWallpaperDir(UserHandle.USER_SYSTEM).mkdirs(); |
Christopher Tate | f717b93 | 2017-09-11 15:52:54 -0700 | [diff] [blame] | 1171 | |
| 1172 | // Initialize state from the persistent store, then guarantee that the |
| 1173 | // WallpaperData for the system imagery is instantiated & active, creating |
| 1174 | // it from defaults if necessary. |
Christopher Tate | d7faf53 | 2016-02-25 12:43:38 -0800 | [diff] [blame] | 1175 | loadSettingsLocked(UserHandle.USER_SYSTEM, false); |
Christopher Tate | f717b93 | 2017-09-11 15:52:54 -0700 | [diff] [blame] | 1176 | getWallpaperSafeLocked(UserHandle.USER_SYSTEM, FLAG_SYSTEM); |
| 1177 | |
Lucas Dupin | 50ba991 | 2017-07-14 11:55:05 -0700 | [diff] [blame] | 1178 | mColorsChangedListeners = new SparseArray<>(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1179 | } |
Oleksandr Peletskyi | f251981 | 2016-01-26 20:16:06 +0100 | [diff] [blame] | 1180 | |
Amith Yamasani | 37ce3a8 | 2012-02-06 12:04:42 -0800 | [diff] [blame] | 1181 | private static File getWallpaperDir(int userId) { |
Amith Yamasani | 61f5737 | 2012-08-31 12:12:28 -0700 | [diff] [blame] | 1182 | return Environment.getUserSystemDirectory(userId); |
Amith Yamasani | 37ce3a8 | 2012-02-06 12:04:42 -0800 | [diff] [blame] | 1183 | } |
| 1184 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1185 | @Override |
| 1186 | protected void finalize() throws Throwable { |
| 1187 | super.finalize(); |
Amith Yamasani | 37ce3a8 | 2012-02-06 12:04:42 -0800 | [diff] [blame] | 1188 | for (int i = 0; i < mWallpaperMap.size(); i++) { |
| 1189 | WallpaperData wallpaper = mWallpaperMap.valueAt(i); |
| 1190 | wallpaper.wallpaperObserver.stopWatching(); |
| 1191 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1192 | } |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 1193 | |
Jeff Sharkey | 1cab76a | 2016-04-12 18:23:31 -0600 | [diff] [blame] | 1194 | void systemReady() { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1195 | if (DEBUG) Slog.v(TAG, "systemReady"); |
Xiaohui Chen | 233d94c | 2015-07-30 15:08:00 -0700 | [diff] [blame] | 1196 | WallpaperData wallpaper = mWallpaperMap.get(UserHandle.USER_SYSTEM); |
Christopher Tate | 2cdd3f2 | 2016-03-14 17:36:16 -0700 | [diff] [blame] | 1197 | // If we think we're going to be using the system image wallpaper imagery, make |
| 1198 | // sure we have something to render |
| 1199 | if (mImageWallpaper.equals(wallpaper.nextWallpaperComponent)) { |
| 1200 | // No crop file? Make sure we've finished the processing sequence if necessary |
| 1201 | if (!wallpaper.cropExists()) { |
| 1202 | if (DEBUG) { |
| 1203 | Slog.i(TAG, "No crop; regenerating from source"); |
| 1204 | } |
| 1205 | generateCrop(wallpaper); |
| 1206 | } |
| 1207 | // Still nothing? Fall back to default. |
| 1208 | if (!wallpaper.cropExists()) { |
| 1209 | if (DEBUG) { |
| 1210 | Slog.i(TAG, "Unable to regenerate crop; resetting"); |
| 1211 | } |
Christopher Tate | edf7d04 | 2016-03-29 18:24:25 -0700 | [diff] [blame] | 1212 | clearWallpaperLocked(false, FLAG_SYSTEM, UserHandle.USER_SYSTEM, null); |
Christopher Tate | 2cdd3f2 | 2016-03-14 17:36:16 -0700 | [diff] [blame] | 1213 | } |
| 1214 | } else { |
| 1215 | if (DEBUG) { |
| 1216 | Slog.i(TAG, "Nondefault wallpaper component; gracefully ignoring"); |
| 1217 | } |
Christopher Tate | 1e1e2e0 | 2016-01-25 15:34:36 -0800 | [diff] [blame] | 1218 | } |
Amith Yamasani | 37ce3a8 | 2012-02-06 12:04:42 -0800 | [diff] [blame] | 1219 | |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 1220 | IntentFilter userFilter = new IntentFilter(); |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 1221 | userFilter.addAction(Intent.ACTION_USER_REMOVED); |
| 1222 | mContext.registerReceiver(new BroadcastReceiver() { |
Amith Yamasani | 37ce3a8 | 2012-02-06 12:04:42 -0800 | [diff] [blame] | 1223 | @Override |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 1224 | public void onReceive(Context context, Intent intent) { |
Jeff Sharkey | 1cab76a | 2016-04-12 18:23:31 -0600 | [diff] [blame] | 1225 | final String action = intent.getAction(); |
Dianne Hackborn | 5dc5a00 | 2012-09-15 19:33:48 -0700 | [diff] [blame] | 1226 | if (Intent.ACTION_USER_REMOVED.equals(action)) { |
Amith Yamasani | 756901d | 2012-10-12 12:30:07 -0700 | [diff] [blame] | 1227 | onRemoveUser(intent.getIntExtra(Intent.EXTRA_USER_HANDLE, |
| 1228 | UserHandle.USER_NULL)); |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 1229 | } |
Amith Yamasani | 37ce3a8 | 2012-02-06 12:04:42 -0800 | [diff] [blame] | 1230 | } |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 1231 | }, userFilter); |
Amith Yamasani | 756901d | 2012-10-12 12:30:07 -0700 | [diff] [blame] | 1232 | |
Christopher Tate | 762dfd1 | 2016-10-10 17:44:48 -0700 | [diff] [blame] | 1233 | final IntentFilter shutdownFilter = new IntentFilter(Intent.ACTION_SHUTDOWN); |
| 1234 | mContext.registerReceiver(new BroadcastReceiver() { |
| 1235 | @Override |
| 1236 | public void onReceive(Context context, Intent intent) { |
| 1237 | if (Intent.ACTION_SHUTDOWN.equals(intent.getAction())) { |
| 1238 | if (DEBUG) { |
| 1239 | Slog.i(TAG, "Shutting down"); |
| 1240 | } |
| 1241 | synchronized (mLock) { |
| 1242 | mShuttingDown = true; |
| 1243 | } |
| 1244 | } |
| 1245 | } |
| 1246 | }, shutdownFilter); |
| 1247 | |
Dianne Hackborn | 5dc5a00 | 2012-09-15 19:33:48 -0700 | [diff] [blame] | 1248 | try { |
Sudheer Shanka | dc589ac | 2016-11-10 15:30:17 -0800 | [diff] [blame] | 1249 | ActivityManager.getService().registerUserSwitchObserver( |
Sudheer Shanka | 2c4522c | 2016-08-27 20:53:28 -0700 | [diff] [blame] | 1250 | new UserSwitchObserver() { |
Dianne Hackborn | 5dc5a00 | 2012-09-15 19:33:48 -0700 | [diff] [blame] | 1251 | @Override |
| 1252 | public void onUserSwitching(int newUserId, IRemoteCallback reply) { |
| 1253 | switchUser(newUserId, reply); |
| 1254 | } |
Fyodor Kupolov | 0b77ef9 | 2016-06-20 17:16:52 -0700 | [diff] [blame] | 1255 | }, TAG); |
Dianne Hackborn | 5dc5a00 | 2012-09-15 19:33:48 -0700 | [diff] [blame] | 1256 | } catch (RemoteException e) { |
Fyodor Kupolov | 0b77ef9 | 2016-06-20 17:16:52 -0700 | [diff] [blame] | 1257 | e.rethrowAsRuntimeException(); |
Dianne Hackborn | 5dc5a00 | 2012-09-15 19:33:48 -0700 | [diff] [blame] | 1258 | } |
Amith Yamasani | 37ce3a8 | 2012-02-06 12:04:42 -0800 | [diff] [blame] | 1259 | } |
| 1260 | |
Amith Yamasani | 09e9cdc | 2013-11-06 14:54:50 -0800 | [diff] [blame] | 1261 | /** Called by SystemBackupAgent */ |
| 1262 | public String getName() { |
| 1263 | // Verify caller is the system |
| 1264 | if (Binder.getCallingUid() != android.os.Process.SYSTEM_UID) { |
| 1265 | throw new RuntimeException("getName() can only be called from the system process"); |
| 1266 | } |
Dianne Hackborn | 5dc5a00 | 2012-09-15 19:33:48 -0700 | [diff] [blame] | 1267 | synchronized (mLock) { |
| 1268 | return mWallpaperMap.get(0).name; |
| 1269 | } |
Amith Yamasani | 37ce3a8 | 2012-02-06 12:04:42 -0800 | [diff] [blame] | 1270 | } |
| 1271 | |
Christopher Tate | be132e6 | 2016-02-10 12:59:49 -0800 | [diff] [blame] | 1272 | void stopObserver(WallpaperData wallpaper) { |
| 1273 | if (wallpaper != null) { |
| 1274 | if (wallpaper.wallpaperObserver != null) { |
| 1275 | wallpaper.wallpaperObserver.stopWatching(); |
| 1276 | wallpaper.wallpaperObserver = null; |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 1277 | } |
Amith Yamasani | 756901d | 2012-10-12 12:30:07 -0700 | [diff] [blame] | 1278 | } |
| 1279 | } |
| 1280 | |
Christopher Tate | be132e6 | 2016-02-10 12:59:49 -0800 | [diff] [blame] | 1281 | void stopObserversLocked(int userId) { |
| 1282 | stopObserver(mWallpaperMap.get(userId)); |
| 1283 | stopObserver(mLockWallpaperMap.get(userId)); |
| 1284 | mWallpaperMap.remove(userId); |
| 1285 | mLockWallpaperMap.remove(userId); |
| 1286 | } |
| 1287 | |
Daichi Hirono | 4bbf852 | 2017-12-06 10:34:18 +0900 | [diff] [blame] | 1288 | @Override |
| 1289 | public void onBootPhase(int phase) { |
| 1290 | if (phase == SystemService.PHASE_ACTIVITY_MANAGER_READY) { |
| 1291 | systemReady(); |
| 1292 | } else if (phase == SystemService.PHASE_THIRD_PARTY_APPS_CAN_START) { |
| 1293 | switchUser(UserHandle.USER_SYSTEM, null); |
| 1294 | } |
| 1295 | } |
| 1296 | |
| 1297 | @Override |
| 1298 | public void onUnlockUser(final int userId) { |
Jeff Sharkey | 1cab76a | 2016-04-12 18:23:31 -0600 | [diff] [blame] | 1299 | synchronized (mLock) { |
Christopher Tate | 38a5dc3 | 2016-07-20 15:10:18 -0700 | [diff] [blame] | 1300 | if (mCurrentUserId == userId) { |
| 1301 | if (mWaitingForUnlock) { |
| 1302 | // If we're switching users, now is when we transition the wallpaper |
| 1303 | switchUser(userId, null); |
| 1304 | } |
Christopher Tate | 190e853 | 2016-07-11 11:35:34 -0700 | [diff] [blame] | 1305 | |
| 1306 | // Make sure that the SELinux labeling of all the relevant files is correct. |
| 1307 | // This corrects for mislabeling bugs that might have arisen from move-to |
| 1308 | // operations involving the wallpaper files. This isn't timing-critical, |
| 1309 | // so we do it in the background to avoid holding up the user unlock operation. |
Christopher Tate | 38a5dc3 | 2016-07-20 15:10:18 -0700 | [diff] [blame] | 1310 | if (mUserRestorecon.get(userId) != Boolean.TRUE) { |
| 1311 | mUserRestorecon.put(userId, Boolean.TRUE); |
| 1312 | Runnable relabeler = new Runnable() { |
| 1313 | @Override |
| 1314 | public void run() { |
| 1315 | final File wallpaperDir = getWallpaperDir(userId); |
| 1316 | for (String filename : sPerUserFiles) { |
| 1317 | File f = new File(wallpaperDir, filename); |
| 1318 | if (f.exists()) { |
| 1319 | SELinux.restorecon(f); |
| 1320 | } |
Christopher Tate | 190e853 | 2016-07-11 11:35:34 -0700 | [diff] [blame] | 1321 | } |
| 1322 | } |
Christopher Tate | 38a5dc3 | 2016-07-20 15:10:18 -0700 | [diff] [blame] | 1323 | }; |
| 1324 | BackgroundThread.getHandler().post(relabeler); |
| 1325 | } |
Jeff Sharkey | 1cab76a | 2016-04-12 18:23:31 -0600 | [diff] [blame] | 1326 | } |
| 1327 | } |
| 1328 | } |
| 1329 | |
Amith Yamasani | 756901d | 2012-10-12 12:30:07 -0700 | [diff] [blame] | 1330 | void onRemoveUser(int userId) { |
| 1331 | if (userId < 1) return; |
Christopher Tate | be132e6 | 2016-02-10 12:59:49 -0800 | [diff] [blame] | 1332 | |
| 1333 | final File wallpaperDir = getWallpaperDir(userId); |
Amith Yamasani | 756901d | 2012-10-12 12:30:07 -0700 | [diff] [blame] | 1334 | synchronized (mLock) { |
Christopher Tate | be132e6 | 2016-02-10 12:59:49 -0800 | [diff] [blame] | 1335 | stopObserversLocked(userId); |
| 1336 | for (String filename : sPerUserFiles) { |
| 1337 | new File(wallpaperDir, filename).delete(); |
| 1338 | } |
Sudheer Shanka | 69f0cb5 | 2016-10-06 17:33:20 -0700 | [diff] [blame] | 1339 | mUserRestorecon.remove(userId); |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 1340 | } |
| 1341 | } |
| 1342 | |
Dianne Hackborn | 5dc5a00 | 2012-09-15 19:33:48 -0700 | [diff] [blame] | 1343 | void switchUser(int userId, IRemoteCallback reply) { |
Lucas Dupin | 9272d45 | 2017-09-14 14:15:42 -0700 | [diff] [blame] | 1344 | final WallpaperData systemWallpaper; |
| 1345 | final WallpaperData lockWallpaper; |
Amith Yamasani | 37ce3a8 | 2012-02-06 12:04:42 -0800 | [diff] [blame] | 1346 | synchronized (mLock) { |
| 1347 | mCurrentUserId = userId; |
Lucas Dupin | 50ba991 | 2017-07-14 11:55:05 -0700 | [diff] [blame] | 1348 | systemWallpaper = getWallpaperSafeLocked(userId, FLAG_SYSTEM); |
Lucas Dupin | 9272d45 | 2017-09-14 14:15:42 -0700 | [diff] [blame] | 1349 | final WallpaperData tmpLockWallpaper = mLockWallpaperMap.get(userId); |
| 1350 | lockWallpaper = tmpLockWallpaper == null ? systemWallpaper : tmpLockWallpaper; |
Lucas Dupin | 50ba991 | 2017-07-14 11:55:05 -0700 | [diff] [blame] | 1351 | // Not started watching yet, in case wallpaper data was loaded for other reasons. |
| 1352 | if (systemWallpaper.wallpaperObserver == null) { |
| 1353 | systemWallpaper.wallpaperObserver = new WallpaperObserver(systemWallpaper); |
| 1354 | systemWallpaper.wallpaperObserver.startWatching(); |
| 1355 | } |
| 1356 | switchWallpaper(systemWallpaper, reply); |
Amith Yamasani | 37ce3a8 | 2012-02-06 12:04:42 -0800 | [diff] [blame] | 1357 | } |
Lucas Dupin | 9272d45 | 2017-09-14 14:15:42 -0700 | [diff] [blame] | 1358 | |
| 1359 | // Offload color extraction to another thread since switchUser will be called |
| 1360 | // from the main thread. |
| 1361 | FgThread.getHandler().post(() -> { |
| 1362 | notifyWallpaperColorsChanged(systemWallpaper, FLAG_SYSTEM); |
| 1363 | notifyWallpaperColorsChanged(lockWallpaper, FLAG_LOCK); |
| 1364 | }); |
Amith Yamasani | 37ce3a8 | 2012-02-06 12:04:42 -0800 | [diff] [blame] | 1365 | } |
| 1366 | |
Dianne Hackborn | 5dc5a00 | 2012-09-15 19:33:48 -0700 | [diff] [blame] | 1367 | void switchWallpaper(WallpaperData wallpaper, IRemoteCallback reply) { |
Dianne Hackborn | f21adf6 | 2009-08-13 10:20:21 -0700 | [diff] [blame] | 1368 | synchronized (mLock) { |
Jeff Sharkey | 1cab76a | 2016-04-12 18:23:31 -0600 | [diff] [blame] | 1369 | mWaitingForUnlock = false; |
| 1370 | final ComponentName cname = wallpaper.wallpaperComponent != null ? |
| 1371 | wallpaper.wallpaperComponent : wallpaper.nextWallpaperComponent; |
| 1372 | if (!bindWallpaperComponentLocked(cname, true, false, wallpaper, reply)) { |
| 1373 | // We failed to bind the desired wallpaper, but that might |
| 1374 | // happen if the wallpaper isn't direct-boot aware |
| 1375 | ServiceInfo si = null; |
| 1376 | try { |
| 1377 | si = mIPackageManager.getServiceInfo(cname, |
| 1378 | PackageManager.MATCH_DIRECT_BOOT_UNAWARE, wallpaper.userId); |
| 1379 | } catch (RemoteException ignored) { |
Dianne Hackborn | f21adf6 | 2009-08-13 10:20:21 -0700 | [diff] [blame] | 1380 | } |
Jeff Sharkey | 1cab76a | 2016-04-12 18:23:31 -0600 | [diff] [blame] | 1381 | |
| 1382 | if (si == null) { |
| 1383 | Slog.w(TAG, "Failure starting previous wallpaper; clearing"); |
| 1384 | clearWallpaperLocked(false, FLAG_SYSTEM, wallpaper.userId, reply); |
| 1385 | } else { |
| 1386 | Slog.w(TAG, "Wallpaper isn't direct boot aware; using fallback until unlocked"); |
| 1387 | // We might end up persisting the current wallpaper data |
| 1388 | // while locked, so pretend like the component was actually |
| 1389 | // bound into place |
| 1390 | wallpaper.wallpaperComponent = wallpaper.nextWallpaperComponent; |
| 1391 | final WallpaperData fallback = new WallpaperData(wallpaper.userId, |
| 1392 | WALLPAPER_LOCK_ORIG, WALLPAPER_LOCK_CROP); |
Adrian Roos | c28e3a9 | 2016-04-14 10:47:52 -0700 | [diff] [blame] | 1393 | ensureSaneWallpaperData(fallback); |
Jeff Sharkey | 1cab76a | 2016-04-12 18:23:31 -0600 | [diff] [blame] | 1394 | bindWallpaperComponentLocked(mImageWallpaper, true, false, fallback, reply); |
| 1395 | mWaitingForUnlock = true; |
| 1396 | } |
Dianne Hackborn | f21adf6 | 2009-08-13 10:20:21 -0700 | [diff] [blame] | 1397 | } |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 1398 | } |
| 1399 | } |
| 1400 | |
Christopher Tate | be132e6 | 2016-02-10 12:59:49 -0800 | [diff] [blame] | 1401 | @Override |
| 1402 | public void clearWallpaper(String callingPackage, int which, int userId) { |
Amith Yamasani | 37ce3a8 | 2012-02-06 12:04:42 -0800 | [diff] [blame] | 1403 | if (DEBUG) Slog.v(TAG, "clearWallpaper"); |
Benjamin Franz | f3ece36 | 2015-02-11 10:51:10 +0000 | [diff] [blame] | 1404 | checkPermission(android.Manifest.permission.SET_WALLPAPER); |
Christopher Tate | 98d609c | 2016-05-18 17:31:58 -0700 | [diff] [blame] | 1405 | if (!isWallpaperSupported(callingPackage) || !isSetWallpaperAllowed(callingPackage)) { |
Benjamin Franz | f3ece36 | 2015-02-11 10:51:10 +0000 | [diff] [blame] | 1406 | return; |
| 1407 | } |
Christopher Tate | e409f0e | 2016-03-21 14:53:15 -0700 | [diff] [blame] | 1408 | userId = ActivityManager.handleIncomingUser(Binder.getCallingPid(), |
| 1409 | Binder.getCallingUid(), userId, false, true, "clearWallpaper", null); |
Christopher Tate | be132e6 | 2016-02-10 12:59:49 -0800 | [diff] [blame] | 1410 | |
Lucas Dupin | 41f6942 | 2017-05-03 15:26:22 -0700 | [diff] [blame] | 1411 | WallpaperData data = null; |
Amith Yamasani | 37ce3a8 | 2012-02-06 12:04:42 -0800 | [diff] [blame] | 1412 | synchronized (mLock) { |
Christopher Tate | be132e6 | 2016-02-10 12:59:49 -0800 | [diff] [blame] | 1413 | clearWallpaperLocked(false, which, userId, null); |
Lucas Dupin | 41f6942 | 2017-05-03 15:26:22 -0700 | [diff] [blame] | 1414 | |
| 1415 | if (which == FLAG_LOCK) { |
| 1416 | data = mLockWallpaperMap.get(userId); |
| 1417 | } |
| 1418 | if (which == FLAG_SYSTEM || data == null) { |
| 1419 | data = mWallpaperMap.get(userId); |
| 1420 | } |
Amith Yamasani | 37ce3a8 | 2012-02-06 12:04:42 -0800 | [diff] [blame] | 1421 | } |
Lucas Dupin | ea1fb1e | 2017-04-05 17:39:44 -0700 | [diff] [blame] | 1422 | |
| 1423 | // When clearing a wallpaper, broadcast new valid colors |
Lucas Dupin | 41f6942 | 2017-05-03 15:26:22 -0700 | [diff] [blame] | 1424 | if (data != null) { |
| 1425 | notifyWallpaperColorsChanged(data, which); |
| 1426 | } |
Amith Yamasani | 37ce3a8 | 2012-02-06 12:04:42 -0800 | [diff] [blame] | 1427 | } |
| 1428 | |
Christopher Tate | be132e6 | 2016-02-10 12:59:49 -0800 | [diff] [blame] | 1429 | void clearWallpaperLocked(boolean defaultFailed, int which, int userId, IRemoteCallback reply) { |
Christopher Tate | edf7d04 | 2016-03-29 18:24:25 -0700 | [diff] [blame] | 1430 | if (which != FLAG_SYSTEM && which != FLAG_LOCK) { |
Christopher Tate | be132e6 | 2016-02-10 12:59:49 -0800 | [diff] [blame] | 1431 | throw new IllegalArgumentException("Must specify exactly one kind of wallpaper to read"); |
| 1432 | } |
| 1433 | |
| 1434 | WallpaperData wallpaper = null; |
Christopher Tate | edf7d04 | 2016-03-29 18:24:25 -0700 | [diff] [blame] | 1435 | if (which == FLAG_LOCK) { |
Christopher Tate | be132e6 | 2016-02-10 12:59:49 -0800 | [diff] [blame] | 1436 | wallpaper = mLockWallpaperMap.get(userId); |
| 1437 | if (wallpaper == null) { |
| 1438 | // It's already gone; we're done. |
Christopher Tate | 79a2457 | 2016-03-02 14:42:44 -0800 | [diff] [blame] | 1439 | if (DEBUG) { |
| 1440 | Slog.i(TAG, "Lock wallpaper already cleared"); |
| 1441 | } |
Christopher Tate | be132e6 | 2016-02-10 12:59:49 -0800 | [diff] [blame] | 1442 | return; |
| 1443 | } |
| 1444 | } else { |
| 1445 | wallpaper = mWallpaperMap.get(userId); |
| 1446 | if (wallpaper == null) { |
| 1447 | // Might need to bring it in the first time to establish our rewrite |
Christopher Tate | d7faf53 | 2016-02-25 12:43:38 -0800 | [diff] [blame] | 1448 | loadSettingsLocked(userId, false); |
Christopher Tate | be132e6 | 2016-02-10 12:59:49 -0800 | [diff] [blame] | 1449 | wallpaper = mWallpaperMap.get(userId); |
| 1450 | } |
| 1451 | } |
Benjamin Franz | f3ece36 | 2015-02-11 10:51:10 +0000 | [diff] [blame] | 1452 | if (wallpaper == null) { |
| 1453 | return; |
| 1454 | } |
Christopher Tate | be132e6 | 2016-02-10 12:59:49 -0800 | [diff] [blame] | 1455 | |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 1456 | final long ident = Binder.clearCallingIdentity(); |
| 1457 | try { |
Christopher Tate | be132e6 | 2016-02-10 12:59:49 -0800 | [diff] [blame] | 1458 | if (wallpaper.wallpaperFile.exists()) { |
| 1459 | wallpaper.wallpaperFile.delete(); |
| 1460 | wallpaper.cropFile.delete(); |
Christopher Tate | edf7d04 | 2016-03-29 18:24:25 -0700 | [diff] [blame] | 1461 | if (which == FLAG_LOCK) { |
Christopher Tate | 79a2457 | 2016-03-02 14:42:44 -0800 | [diff] [blame] | 1462 | mLockWallpaperMap.remove(userId); |
Christopher Tate | be132e6 | 2016-02-10 12:59:49 -0800 | [diff] [blame] | 1463 | final IWallpaperManagerCallback cb = mKeyguardListener; |
| 1464 | if (cb != null) { |
Christopher Tate | 79a2457 | 2016-03-02 14:42:44 -0800 | [diff] [blame] | 1465 | if (DEBUG) { |
| 1466 | Slog.i(TAG, "Notifying keyguard of lock wallpaper clear"); |
| 1467 | } |
Christopher Tate | be132e6 | 2016-02-10 12:59:49 -0800 | [diff] [blame] | 1468 | try { |
| 1469 | cb.onWallpaperChanged(); |
| 1470 | } catch (RemoteException e) { |
| 1471 | // Oh well it went away; no big deal |
| 1472 | } |
| 1473 | } |
Christopher Tate | 79a2457 | 2016-03-02 14:42:44 -0800 | [diff] [blame] | 1474 | saveSettingsLocked(userId); |
Christopher Tate | be132e6 | 2016-02-10 12:59:49 -0800 | [diff] [blame] | 1475 | return; |
| 1476 | } |
| 1477 | } |
| 1478 | |
Christopher Tate | ecd827a | 2014-09-05 17:42:34 -0700 | [diff] [blame] | 1479 | RuntimeException e = null; |
| 1480 | try { |
Lucas Dupin | 75ec379 | 2017-06-29 14:07:18 -0700 | [diff] [blame] | 1481 | wallpaper.primaryColors = null; |
Christopher Tate | ecd827a | 2014-09-05 17:42:34 -0700 | [diff] [blame] | 1482 | wallpaper.imageWallpaperPending = false; |
| 1483 | if (userId != mCurrentUserId) return; |
| 1484 | if (bindWallpaperComponentLocked(defaultFailed |
| 1485 | ? mImageWallpaper |
| 1486 | : null, true, false, wallpaper, reply)) { |
| 1487 | return; |
| 1488 | } |
| 1489 | } catch (IllegalArgumentException e1) { |
| 1490 | e = e1; |
Dianne Hackborn | 80b902f | 2011-09-15 15:15:27 -0700 | [diff] [blame] | 1491 | } |
Christopher Tate | ecd827a | 2014-09-05 17:42:34 -0700 | [diff] [blame] | 1492 | |
| 1493 | // This can happen if the default wallpaper component doesn't |
| 1494 | // exist. This should be a system configuration problem, but |
| 1495 | // let's not let it crash the system and just live with no |
| 1496 | // wallpaper. |
| 1497 | Slog.e(TAG, "Default wallpaper component not found!", e); |
| 1498 | clearWallpaperComponentLocked(wallpaper); |
| 1499 | if (reply != null) { |
| 1500 | try { |
| 1501 | reply.sendResult(null); |
| 1502 | } catch (RemoteException e1) { |
| 1503 | } |
| 1504 | } |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 1505 | } finally { |
| 1506 | Binder.restoreCallingIdentity(ident); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1507 | } |
Dianne Hackborn | 5dc5a00 | 2012-09-15 19:33:48 -0700 | [diff] [blame] | 1508 | } |
| 1509 | |
| 1510 | public boolean hasNamedWallpaper(String name) { |
| 1511 | synchronized (mLock) { |
Amith Yamasani | 6474c4c | 2012-10-04 14:55:42 -0700 | [diff] [blame] | 1512 | List<UserInfo> users; |
| 1513 | long ident = Binder.clearCallingIdentity(); |
| 1514 | try { |
| 1515 | users = ((UserManager) mContext.getSystemService(Context.USER_SERVICE)).getUsers(); |
| 1516 | } finally { |
| 1517 | Binder.restoreCallingIdentity(ident); |
| 1518 | } |
| 1519 | for (UserInfo user: users) { |
Benjamin Franz | f3ece36 | 2015-02-11 10:51:10 +0000 | [diff] [blame] | 1520 | // ignore managed profiles |
| 1521 | if (user.isManagedProfile()) { |
| 1522 | continue; |
| 1523 | } |
Amith Yamasani | 6474c4c | 2012-10-04 14:55:42 -0700 | [diff] [blame] | 1524 | WallpaperData wd = mWallpaperMap.get(user.id); |
| 1525 | if (wd == null) { |
| 1526 | // User hasn't started yet, so load her settings to peek at the wallpaper |
Christopher Tate | d7faf53 | 2016-02-25 12:43:38 -0800 | [diff] [blame] | 1527 | loadSettingsLocked(user.id, false); |
Amith Yamasani | 6474c4c | 2012-10-04 14:55:42 -0700 | [diff] [blame] | 1528 | wd = mWallpaperMap.get(user.id); |
| 1529 | } |
| 1530 | if (wd != null && name.equals(wd.name)) { |
Dianne Hackborn | 5dc5a00 | 2012-09-15 19:33:48 -0700 | [diff] [blame] | 1531 | return true; |
| 1532 | } |
| 1533 | } |
| 1534 | } |
| 1535 | return false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1536 | } |
| 1537 | |
John Spurlock | 41f6464 | 2013-11-04 13:48:38 -0500 | [diff] [blame] | 1538 | private Point getDefaultDisplaySize() { |
| 1539 | Point p = new Point(); |
John Spurlock | d6e836c | 2013-11-18 14:14:49 -0500 | [diff] [blame] | 1540 | WindowManager wm = (WindowManager)mContext.getSystemService(Context.WINDOW_SERVICE); |
| 1541 | Display d = wm.getDefaultDisplay(); |
| 1542 | d.getRealSize(p); |
John Spurlock | 41f6464 | 2013-11-04 13:48:38 -0500 | [diff] [blame] | 1543 | return p; |
| 1544 | } |
| 1545 | |
Benjamin Franz | f3ece36 | 2015-02-11 10:51:10 +0000 | [diff] [blame] | 1546 | public void setDimensionHints(int width, int height, String callingPackage) |
| 1547 | throws RemoteException { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1548 | checkPermission(android.Manifest.permission.SET_WALLPAPER_HINTS); |
Benjamin Franz | f3ece36 | 2015-02-11 10:51:10 +0000 | [diff] [blame] | 1549 | if (!isWallpaperSupported(callingPackage)) { |
| 1550 | return; |
| 1551 | } |
Joe Onorato | 9bb8fd7 | 2009-07-28 18:24:51 -0700 | [diff] [blame] | 1552 | synchronized (mLock) { |
Dianne Hackborn | 5dc5a00 | 2012-09-15 19:33:48 -0700 | [diff] [blame] | 1553 | int userId = UserHandle.getCallingUserId(); |
Christopher Tate | edf7d04 | 2016-03-29 18:24:25 -0700 | [diff] [blame] | 1554 | WallpaperData wallpaper = getWallpaperSafeLocked(userId, FLAG_SYSTEM); |
Dianne Hackborn | 5dc5a00 | 2012-09-15 19:33:48 -0700 | [diff] [blame] | 1555 | if (width <= 0 || height <= 0) { |
| 1556 | throw new IllegalArgumentException("width and height must be > 0"); |
| 1557 | } |
John Spurlock | 41f6464 | 2013-11-04 13:48:38 -0500 | [diff] [blame] | 1558 | // Make sure it is at least as large as the display. |
| 1559 | Point displaySize = getDefaultDisplaySize(); |
| 1560 | width = Math.max(width, displaySize.x); |
| 1561 | height = Math.max(height, displaySize.y); |
Dianne Hackborn | 5dc5a00 | 2012-09-15 19:33:48 -0700 | [diff] [blame] | 1562 | |
Amith Yamasani | 37ce3a8 | 2012-02-06 12:04:42 -0800 | [diff] [blame] | 1563 | if (width != wallpaper.width || height != wallpaper.height) { |
| 1564 | wallpaper.width = width; |
| 1565 | wallpaper.height = height; |
Christopher Tate | db27b84 | 2016-02-25 14:39:17 -0800 | [diff] [blame] | 1566 | saveSettingsLocked(userId); |
Amith Yamasani | 37ce3a8 | 2012-02-06 12:04:42 -0800 | [diff] [blame] | 1567 | if (mCurrentUserId != userId) return; // Don't change the properties now |
| 1568 | if (wallpaper.connection != null) { |
| 1569 | if (wallpaper.connection.mEngine != null) { |
Dianne Hackborn | 284ac93 | 2009-08-28 10:34:25 -0700 | [diff] [blame] | 1570 | try { |
Amith Yamasani | 37ce3a8 | 2012-02-06 12:04:42 -0800 | [diff] [blame] | 1571 | wallpaper.connection.mEngine.setDesiredSize( |
Dianne Hackborn | 284ac93 | 2009-08-28 10:34:25 -0700 | [diff] [blame] | 1572 | width, height); |
| 1573 | } catch (RemoteException e) { |
| 1574 | } |
Amith Yamasani | 37ce3a8 | 2012-02-06 12:04:42 -0800 | [diff] [blame] | 1575 | notifyCallbacksLocked(wallpaper); |
Michael Wright | 5203a8b | 2013-10-03 14:16:42 -0700 | [diff] [blame] | 1576 | } else if (wallpaper.connection.mService != null) { |
| 1577 | // We've attached to the service but the engine hasn't attached back to us |
| 1578 | // yet. This means it will be created with the previous dimensions, so we |
| 1579 | // need to update it to the new dimensions once it attaches. |
| 1580 | wallpaper.connection.mDimensionsChanged = true; |
Dianne Hackborn | 284ac93 | 2009-08-28 10:34:25 -0700 | [diff] [blame] | 1581 | } |
| 1582 | } |
Joe Onorato | 9bb8fd7 | 2009-07-28 18:24:51 -0700 | [diff] [blame] | 1583 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1584 | } |
| 1585 | } |
| 1586 | |
| 1587 | public int getWidthHint() throws RemoteException { |
Joe Onorato | 9bb8fd7 | 2009-07-28 18:24:51 -0700 | [diff] [blame] | 1588 | synchronized (mLock) { |
Dianne Hackborn | f02b60a | 2012-08-16 10:48:27 -0700 | [diff] [blame] | 1589 | WallpaperData wallpaper = mWallpaperMap.get(UserHandle.getCallingUserId()); |
Benjamin Franz | f3ece36 | 2015-02-11 10:51:10 +0000 | [diff] [blame] | 1590 | if (wallpaper != null) { |
| 1591 | return wallpaper.width; |
| 1592 | } else { |
| 1593 | return 0; |
| 1594 | } |
Joe Onorato | 9bb8fd7 | 2009-07-28 18:24:51 -0700 | [diff] [blame] | 1595 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1596 | } |
| 1597 | |
| 1598 | public int getHeightHint() throws RemoteException { |
Joe Onorato | 9bb8fd7 | 2009-07-28 18:24:51 -0700 | [diff] [blame] | 1599 | synchronized (mLock) { |
Dianne Hackborn | f02b60a | 2012-08-16 10:48:27 -0700 | [diff] [blame] | 1600 | WallpaperData wallpaper = mWallpaperMap.get(UserHandle.getCallingUserId()); |
Benjamin Franz | f3ece36 | 2015-02-11 10:51:10 +0000 | [diff] [blame] | 1601 | if (wallpaper != null) { |
| 1602 | return wallpaper.height; |
| 1603 | } else { |
| 1604 | return 0; |
| 1605 | } |
Joe Onorato | 9bb8fd7 | 2009-07-28 18:24:51 -0700 | [diff] [blame] | 1606 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1607 | } |
| 1608 | |
Benjamin Franz | f3ece36 | 2015-02-11 10:51:10 +0000 | [diff] [blame] | 1609 | public void setDisplayPadding(Rect padding, String callingPackage) { |
Dianne Hackborn | 067e5f6 | 2014-09-07 23:14:30 -0700 | [diff] [blame] | 1610 | checkPermission(android.Manifest.permission.SET_WALLPAPER_HINTS); |
Benjamin Franz | f3ece36 | 2015-02-11 10:51:10 +0000 | [diff] [blame] | 1611 | if (!isWallpaperSupported(callingPackage)) { |
| 1612 | return; |
| 1613 | } |
Dianne Hackborn | 067e5f6 | 2014-09-07 23:14:30 -0700 | [diff] [blame] | 1614 | synchronized (mLock) { |
| 1615 | int userId = UserHandle.getCallingUserId(); |
Christopher Tate | edf7d04 | 2016-03-29 18:24:25 -0700 | [diff] [blame] | 1616 | WallpaperData wallpaper = getWallpaperSafeLocked(userId, FLAG_SYSTEM); |
Dianne Hackborn | 067e5f6 | 2014-09-07 23:14:30 -0700 | [diff] [blame] | 1617 | if (padding.left < 0 || padding.top < 0 || padding.right < 0 || padding.bottom < 0) { |
| 1618 | throw new IllegalArgumentException("padding must be positive: " + padding); |
| 1619 | } |
| 1620 | |
| 1621 | if (!padding.equals(wallpaper.padding)) { |
| 1622 | wallpaper.padding.set(padding); |
Christopher Tate | db27b84 | 2016-02-25 14:39:17 -0800 | [diff] [blame] | 1623 | saveSettingsLocked(userId); |
Dianne Hackborn | 067e5f6 | 2014-09-07 23:14:30 -0700 | [diff] [blame] | 1624 | if (mCurrentUserId != userId) return; // Don't change the properties now |
| 1625 | if (wallpaper.connection != null) { |
| 1626 | if (wallpaper.connection.mEngine != null) { |
| 1627 | try { |
| 1628 | wallpaper.connection.mEngine.setDisplayPadding(padding); |
| 1629 | } catch (RemoteException e) { |
| 1630 | } |
| 1631 | notifyCallbacksLocked(wallpaper); |
| 1632 | } else if (wallpaper.connection.mService != null) { |
| 1633 | // We've attached to the service but the engine hasn't attached back to us |
| 1634 | // yet. This means it will be created with the previous dimensions, so we |
| 1635 | // need to update it to the new dimensions once it attaches. |
| 1636 | wallpaper.connection.mPaddingChanged = true; |
| 1637 | } |
| 1638 | } |
| 1639 | } |
| 1640 | } |
| 1641 | } |
| 1642 | |
Christopher Tate | 93252de | 2017-06-15 14:48:41 -0700 | [diff] [blame] | 1643 | private void enforceCallingOrSelfPermissionAndAppOp(String permission, final String callingPkg, |
| 1644 | final int callingUid, String message) { |
| 1645 | mContext.enforceCallingOrSelfPermission(permission, message); |
| 1646 | |
| 1647 | final String opName = AppOpsManager.permissionToOp(permission); |
| 1648 | if (opName != null) { |
| 1649 | final int appOpMode = mAppOpsManager.noteOp(opName, callingUid, callingPkg); |
| 1650 | if (appOpMode != AppOpsManager.MODE_ALLOWED) { |
| 1651 | throw new SecurityException( |
| 1652 | message + ": " + callingPkg + " is not allowed to " + permission); |
| 1653 | } |
| 1654 | } |
| 1655 | } |
| 1656 | |
Yorke Lee | dcd93cc | 2016-01-08 14:12:55 -0800 | [diff] [blame] | 1657 | @Override |
Christopher Tate | 93252de | 2017-06-15 14:48:41 -0700 | [diff] [blame] | 1658 | public ParcelFileDescriptor getWallpaper(String callingPkg, IWallpaperManagerCallback cb, |
| 1659 | final int which, Bundle outParams, int wallpaperUserId) { |
Christopher Tate | 8a71c48 | 2017-08-14 16:45:03 -0700 | [diff] [blame] | 1660 | final int hasPrivilege = mContext.checkCallingOrSelfPermission( |
| 1661 | android.Manifest.permission.READ_WALLPAPER_INTERNAL); |
| 1662 | if (hasPrivilege != PackageManager.PERMISSION_GRANTED) { |
| 1663 | enforceCallingOrSelfPermissionAndAppOp(android.Manifest.permission.READ_EXTERNAL_STORAGE, |
| 1664 | callingPkg, Binder.getCallingUid(), "read wallpaper"); |
| 1665 | } |
Christopher Tate | 93252de | 2017-06-15 14:48:41 -0700 | [diff] [blame] | 1666 | |
Christopher Tate | e409f0e | 2016-03-21 14:53:15 -0700 | [diff] [blame] | 1667 | wallpaperUserId = ActivityManager.handleIncomingUser(Binder.getCallingPid(), |
| 1668 | Binder.getCallingUid(), wallpaperUserId, false, true, "getWallpaper", null); |
Christopher Tate | be132e6 | 2016-02-10 12:59:49 -0800 | [diff] [blame] | 1669 | |
Christopher Tate | edf7d04 | 2016-03-29 18:24:25 -0700 | [diff] [blame] | 1670 | if (which != FLAG_SYSTEM && which != FLAG_LOCK) { |
Christopher Tate | be132e6 | 2016-02-10 12:59:49 -0800 | [diff] [blame] | 1671 | throw new IllegalArgumentException("Must specify exactly one kind of wallpaper to read"); |
| 1672 | } |
| 1673 | |
Joe Onorato | 9bb8fd7 | 2009-07-28 18:24:51 -0700 | [diff] [blame] | 1674 | synchronized (mLock) { |
Vadim Tryshev | 8cde079 | 2016-02-19 17:02:15 -0800 | [diff] [blame] | 1675 | final SparseArray<WallpaperData> whichSet = |
Christopher Tate | edf7d04 | 2016-03-29 18:24:25 -0700 | [diff] [blame] | 1676 | (which == FLAG_LOCK) ? mLockWallpaperMap : mWallpaperMap; |
Vadim Tryshev | 8cde079 | 2016-02-19 17:02:15 -0800 | [diff] [blame] | 1677 | WallpaperData wallpaper = whichSet.get(wallpaperUserId); |
| 1678 | if (wallpaper == null) { |
Christopher Tate | f717b93 | 2017-09-11 15:52:54 -0700 | [diff] [blame] | 1679 | // There is no established wallpaper imagery of this type (expected |
| 1680 | // only for lock wallpapers; a system WallpaperData is established at |
| 1681 | // user switch) |
| 1682 | return null; |
Benjamin Franz | f3ece36 | 2015-02-11 10:51:10 +0000 | [diff] [blame] | 1683 | } |
Joe Onorato | 9bb8fd7 | 2009-07-28 18:24:51 -0700 | [diff] [blame] | 1684 | try { |
Dianne Hackborn | 284ac93 | 2009-08-28 10:34:25 -0700 | [diff] [blame] | 1685 | if (outParams != null) { |
Amith Yamasani | 37ce3a8 | 2012-02-06 12:04:42 -0800 | [diff] [blame] | 1686 | outParams.putInt("width", wallpaper.width); |
| 1687 | outParams.putInt("height", wallpaper.height); |
Dianne Hackborn | 284ac93 | 2009-08-28 10:34:25 -0700 | [diff] [blame] | 1688 | } |
Christopher Tate | ea6724a | 2016-02-18 18:39:19 -0800 | [diff] [blame] | 1689 | if (cb != null) { |
| 1690 | wallpaper.callbacks.register(cb); |
| 1691 | } |
Christopher Tate | 1e1e2e0 | 2016-01-25 15:34:36 -0800 | [diff] [blame] | 1692 | if (!wallpaper.cropFile.exists()) { |
Joe Onorato | 9bb8fd7 | 2009-07-28 18:24:51 -0700 | [diff] [blame] | 1693 | return null; |
| 1694 | } |
Christopher Tate | 1e1e2e0 | 2016-01-25 15:34:36 -0800 | [diff] [blame] | 1695 | return ParcelFileDescriptor.open(wallpaper.cropFile, MODE_READ_ONLY); |
Joe Onorato | 9bb8fd7 | 2009-07-28 18:24:51 -0700 | [diff] [blame] | 1696 | } catch (FileNotFoundException e) { |
| 1697 | /* Shouldn't happen as we check to see if the file exists */ |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1698 | Slog.w(TAG, "Error getting wallpaper", e); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1699 | } |
Joe Onorato | 9bb8fd7 | 2009-07-28 18:24:51 -0700 | [diff] [blame] | 1700 | return null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1701 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1702 | } |
| 1703 | |
Christopher Tate | e409f0e | 2016-03-21 14:53:15 -0700 | [diff] [blame] | 1704 | @Override |
Jorim Jaggi | e31f6b8 | 2016-07-01 16:15:09 -0700 | [diff] [blame] | 1705 | public WallpaperInfo getWallpaperInfo(int userId) { |
| 1706 | userId = ActivityManager.handleIncomingUser(Binder.getCallingPid(), |
Christopher Tate | f717b93 | 2017-09-11 15:52:54 -0700 | [diff] [blame] | 1707 | Binder.getCallingUid(), userId, false, true, "getWallpaperInfo", null); |
Dianne Hackborn | eb03465 | 2009-09-07 00:49:58 -0700 | [diff] [blame] | 1708 | synchronized (mLock) { |
Amith Yamasani | 37ce3a8 | 2012-02-06 12:04:42 -0800 | [diff] [blame] | 1709 | WallpaperData wallpaper = mWallpaperMap.get(userId); |
Benjamin Franz | f3ece36 | 2015-02-11 10:51:10 +0000 | [diff] [blame] | 1710 | if (wallpaper != null && wallpaper.connection != null) { |
Amith Yamasani | 37ce3a8 | 2012-02-06 12:04:42 -0800 | [diff] [blame] | 1711 | return wallpaper.connection.mInfo; |
Dianne Hackborn | eb03465 | 2009-09-07 00:49:58 -0700 | [diff] [blame] | 1712 | } |
| 1713 | return null; |
| 1714 | } |
| 1715 | } |
Amith Yamasani | 37ce3a8 | 2012-02-06 12:04:42 -0800 | [diff] [blame] | 1716 | |
Christopher Tate | ad3c259 | 2016-01-20 18:13:17 -0800 | [diff] [blame] | 1717 | @Override |
Christopher Tate | e409f0e | 2016-03-21 14:53:15 -0700 | [diff] [blame] | 1718 | public int getWallpaperIdForUser(int which, int userId) { |
| 1719 | userId = ActivityManager.handleIncomingUser(Binder.getCallingPid(), |
| 1720 | Binder.getCallingUid(), userId, false, true, "getWallpaperIdForUser", null); |
| 1721 | |
Christopher Tate | edf7d04 | 2016-03-29 18:24:25 -0700 | [diff] [blame] | 1722 | if (which != FLAG_SYSTEM && which != FLAG_LOCK) { |
Christopher Tate | e409f0e | 2016-03-21 14:53:15 -0700 | [diff] [blame] | 1723 | throw new IllegalArgumentException("Must specify exactly one kind of wallpaper"); |
| 1724 | } |
| 1725 | |
| 1726 | final SparseArray<WallpaperData> map = |
Christopher Tate | edf7d04 | 2016-03-29 18:24:25 -0700 | [diff] [blame] | 1727 | (which == FLAG_LOCK) ? mLockWallpaperMap : mWallpaperMap; |
Christopher Tate | e409f0e | 2016-03-21 14:53:15 -0700 | [diff] [blame] | 1728 | synchronized (mLock) { |
| 1729 | WallpaperData wallpaper = map.get(userId); |
| 1730 | if (wallpaper != null) { |
| 1731 | return wallpaper.wallpaperId; |
| 1732 | } |
| 1733 | } |
| 1734 | return -1; |
| 1735 | } |
| 1736 | |
| 1737 | @Override |
Lucas Dupin | 50ba991 | 2017-07-14 11:55:05 -0700 | [diff] [blame] | 1738 | public void registerWallpaperColorsCallback(IWallpaperManagerCallback cb, int userId) { |
| 1739 | userId = ActivityManager.handleIncomingUser(Binder.getCallingPid(), Binder.getCallingUid(), |
| 1740 | userId, true, true, "registerWallpaperColorsCallback", null); |
Lucas Dupin | ea1fb1e | 2017-04-05 17:39:44 -0700 | [diff] [blame] | 1741 | synchronized (mLock) { |
Lucas Dupin | 50ba991 | 2017-07-14 11:55:05 -0700 | [diff] [blame] | 1742 | RemoteCallbackList<IWallpaperManagerCallback> userColorsChangedListeners = |
| 1743 | mColorsChangedListeners.get(userId); |
| 1744 | if (userColorsChangedListeners == null) { |
| 1745 | userColorsChangedListeners = new RemoteCallbackList<>(); |
| 1746 | mColorsChangedListeners.put(userId, userColorsChangedListeners); |
| 1747 | } |
| 1748 | userColorsChangedListeners.register(cb); |
Lucas Dupin | ea1fb1e | 2017-04-05 17:39:44 -0700 | [diff] [blame] | 1749 | } |
| 1750 | } |
| 1751 | |
| 1752 | @Override |
Lucas Dupin | 50ba991 | 2017-07-14 11:55:05 -0700 | [diff] [blame] | 1753 | public void unregisterWallpaperColorsCallback(IWallpaperManagerCallback cb, int userId) { |
| 1754 | userId = ActivityManager.handleIncomingUser(Binder.getCallingPid(), Binder.getCallingUid(), |
| 1755 | userId, true, true, "unregisterWallpaperColorsCallback", null); |
Lucas Dupin | ea1fb1e | 2017-04-05 17:39:44 -0700 | [diff] [blame] | 1756 | synchronized (mLock) { |
Lucas Dupin | 50ba991 | 2017-07-14 11:55:05 -0700 | [diff] [blame] | 1757 | final RemoteCallbackList<IWallpaperManagerCallback> userColorsChangedListeners = |
| 1758 | mColorsChangedListeners.get(userId); |
| 1759 | if (userColorsChangedListeners != null) { |
| 1760 | userColorsChangedListeners.unregister(cb); |
| 1761 | } |
Lucas Dupin | ea1fb1e | 2017-04-05 17:39:44 -0700 | [diff] [blame] | 1762 | } |
| 1763 | } |
| 1764 | |
Lucas Dupin | 660d573 | 2017-12-19 10:05:19 -0800 | [diff] [blame] | 1765 | public void setInAmbientMode(boolean inAmbienMode, boolean animated) { |
Lucas Dupin | 7517b5d | 2017-08-22 12:51:25 -0700 | [diff] [blame] | 1766 | final IWallpaperEngine engine; |
| 1767 | synchronized (mLock) { |
| 1768 | mInAmbientMode = inAmbienMode; |
| 1769 | final WallpaperData data = mWallpaperMap.get(mCurrentUserId); |
| 1770 | if (data != null && data.connection != null && data.connection.mInfo != null |
| 1771 | && data.connection.mInfo.getSupportsAmbientMode()) { |
| 1772 | engine = data.connection.mEngine; |
| 1773 | } else { |
| 1774 | engine = null; |
| 1775 | } |
| 1776 | } |
| 1777 | |
| 1778 | if (engine != null) { |
| 1779 | try { |
Lucas Dupin | 660d573 | 2017-12-19 10:05:19 -0800 | [diff] [blame] | 1780 | engine.setInAmbientMode(inAmbienMode, animated); |
Lucas Dupin | 7517b5d | 2017-08-22 12:51:25 -0700 | [diff] [blame] | 1781 | } catch (RemoteException e) { |
| 1782 | // Cannot talk to wallpaper engine. |
| 1783 | } |
| 1784 | } |
| 1785 | } |
| 1786 | |
Lucas Dupin | ea1fb1e | 2017-04-05 17:39:44 -0700 | [diff] [blame] | 1787 | @Override |
Christopher Tate | be132e6 | 2016-02-10 12:59:49 -0800 | [diff] [blame] | 1788 | public boolean setLockWallpaperCallback(IWallpaperManagerCallback cb) { |
| 1789 | checkPermission(android.Manifest.permission.INTERNAL_SYSTEM_WINDOW); |
| 1790 | synchronized (mLock) { |
| 1791 | mKeyguardListener = cb; |
| 1792 | } |
| 1793 | return true; |
| 1794 | } |
| 1795 | |
| 1796 | @Override |
Lucas Dupin | 50ba991 | 2017-07-14 11:55:05 -0700 | [diff] [blame] | 1797 | public WallpaperColors getWallpaperColors(int which, int userId) throws RemoteException { |
Lucas Dupin | ea1fb1e | 2017-04-05 17:39:44 -0700 | [diff] [blame] | 1798 | if (which != FLAG_LOCK && which != FLAG_SYSTEM) { |
| 1799 | throw new IllegalArgumentException("which should be either FLAG_LOCK or FLAG_SYSTEM"); |
| 1800 | } |
Lucas Dupin | 50ba991 | 2017-07-14 11:55:05 -0700 | [diff] [blame] | 1801 | userId = ActivityManager.handleIncomingUser(Binder.getCallingPid(), Binder.getCallingUid(), |
| 1802 | userId, false, true, "getWallpaperColors", null); |
Lucas Dupin | ea1fb1e | 2017-04-05 17:39:44 -0700 | [diff] [blame] | 1803 | |
| 1804 | WallpaperData wallpaperData = null; |
| 1805 | boolean shouldExtract; |
| 1806 | |
| 1807 | synchronized (mLock) { |
| 1808 | if (which == FLAG_LOCK) { |
Lucas Dupin | 50ba991 | 2017-07-14 11:55:05 -0700 | [diff] [blame] | 1809 | wallpaperData = mLockWallpaperMap.get(userId); |
Lucas Dupin | ea1fb1e | 2017-04-05 17:39:44 -0700 | [diff] [blame] | 1810 | } |
| 1811 | |
| 1812 | // Try to get the system wallpaper anyway since it might |
| 1813 | // also be the lock screen wallpaper |
| 1814 | if (wallpaperData == null) { |
Lucas Dupin | 50ba991 | 2017-07-14 11:55:05 -0700 | [diff] [blame] | 1815 | wallpaperData = mWallpaperMap.get(userId); |
Lucas Dupin | ea1fb1e | 2017-04-05 17:39:44 -0700 | [diff] [blame] | 1816 | } |
| 1817 | |
| 1818 | if (wallpaperData == null) { |
| 1819 | return null; |
| 1820 | } |
| 1821 | shouldExtract = wallpaperData.primaryColors == null; |
| 1822 | } |
| 1823 | |
| 1824 | if (shouldExtract) { |
| 1825 | extractColors(wallpaperData); |
| 1826 | } |
| 1827 | |
| 1828 | synchronized (mLock) { |
| 1829 | return wallpaperData.primaryColors; |
| 1830 | } |
| 1831 | } |
| 1832 | |
| 1833 | @Override |
Christopher Tate | 1e1e2e0 | 2016-01-25 15:34:36 -0800 | [diff] [blame] | 1834 | public ParcelFileDescriptor setWallpaper(String name, String callingPackage, |
Christopher Tate | d7faf53 | 2016-02-25 12:43:38 -0800 | [diff] [blame] | 1835 | Rect cropHint, boolean allowBackup, Bundle extras, int which, |
Jorim Jaggi | 6c902d0 | 2016-08-18 10:44:54 -0700 | [diff] [blame] | 1836 | IWallpaperManagerCallback completion, int userId) { |
| 1837 | userId = ActivityManager.handleIncomingUser(getCallingPid(), getCallingUid(), userId, |
| 1838 | false /* all */, true /* full */, "changing wallpaper", null /* pkg */); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1839 | checkPermission(android.Manifest.permission.SET_WALLPAPER); |
Christopher Tate | ad3c259 | 2016-01-20 18:13:17 -0800 | [diff] [blame] | 1840 | |
Christopher Tate | edf7d04 | 2016-03-29 18:24:25 -0700 | [diff] [blame] | 1841 | if ((which & (FLAG_LOCK|FLAG_SYSTEM)) == 0) { |
Christopher Tate | 98d609c | 2016-05-18 17:31:58 -0700 | [diff] [blame] | 1842 | final String msg = "Must specify a valid wallpaper category to set"; |
| 1843 | Slog.e(TAG, msg); |
| 1844 | throw new IllegalArgumentException(msg); |
Christopher Tate | ad3c259 | 2016-01-20 18:13:17 -0800 | [diff] [blame] | 1845 | } |
| 1846 | |
Christopher Tate | 98d609c | 2016-05-18 17:31:58 -0700 | [diff] [blame] | 1847 | if (!isWallpaperSupported(callingPackage) || !isSetWallpaperAllowed(callingPackage)) { |
Benjamin Franz | f3ece36 | 2015-02-11 10:51:10 +0000 | [diff] [blame] | 1848 | return null; |
| 1849 | } |
Christopher Tate | ad3c259 | 2016-01-20 18:13:17 -0800 | [diff] [blame] | 1850 | |
Christopher Tate | 1e1e2e0 | 2016-01-25 15:34:36 -0800 | [diff] [blame] | 1851 | // "null" means the no-op crop, preserving the full input image |
| 1852 | if (cropHint == null) { |
| 1853 | cropHint = new Rect(0, 0, 0, 0); |
| 1854 | } else { |
| 1855 | if (cropHint.isEmpty() |
| 1856 | || cropHint.left < 0 |
| 1857 | || cropHint.top < 0) { |
Christopher Tate | 98d609c | 2016-05-18 17:31:58 -0700 | [diff] [blame] | 1858 | throw new IllegalArgumentException("Invalid crop rect supplied: " + cropHint); |
Christopher Tate | 1e1e2e0 | 2016-01-25 15:34:36 -0800 | [diff] [blame] | 1859 | } |
| 1860 | } |
| 1861 | |
Joe Onorato | 9bb8fd7 | 2009-07-28 18:24:51 -0700 | [diff] [blame] | 1862 | synchronized (mLock) { |
Christopher Tate | be132e6 | 2016-02-10 12:59:49 -0800 | [diff] [blame] | 1863 | if (DEBUG) Slog.v(TAG, "setWallpaper which=0x" + Integer.toHexString(which)); |
| 1864 | WallpaperData wallpaper; |
| 1865 | |
Christopher Tate | 8347b63 | 2016-04-29 18:59:18 -0700 | [diff] [blame] | 1866 | /* If we're setting system but not lock, and lock is currently sharing the system |
| 1867 | * wallpaper, we need to migrate that image over to being lock-only before |
| 1868 | * the caller here writes new bitmap data. |
| 1869 | */ |
| 1870 | if (which == FLAG_SYSTEM && mLockWallpaperMap.get(userId) == null) { |
| 1871 | if (DEBUG) { |
| 1872 | Slog.i(TAG, "Migrating system->lock to preserve"); |
| 1873 | } |
| 1874 | migrateSystemToLockWallpaperLocked(userId); |
| 1875 | } |
| 1876 | |
Christopher Tate | be132e6 | 2016-02-10 12:59:49 -0800 | [diff] [blame] | 1877 | wallpaper = getWallpaperSafeLocked(userId, which); |
Dianne Hackborn | 0cd4887 | 2009-08-13 18:51:59 -0700 | [diff] [blame] | 1878 | final long ident = Binder.clearCallingIdentity(); |
| 1879 | try { |
Christopher Tate | ad3c259 | 2016-01-20 18:13:17 -0800 | [diff] [blame] | 1880 | ParcelFileDescriptor pfd = updateWallpaperBitmapLocked(name, wallpaper, extras); |
Dianne Hackborn | 0cd4887 | 2009-08-13 18:51:59 -0700 | [diff] [blame] | 1881 | if (pfd != null) { |
Amith Yamasani | 37ce3a8 | 2012-02-06 12:04:42 -0800 | [diff] [blame] | 1882 | wallpaper.imageWallpaperPending = true; |
Christopher Tate | be132e6 | 2016-02-10 12:59:49 -0800 | [diff] [blame] | 1883 | wallpaper.whichPending = which; |
Christopher Tate | 1e1e2e0 | 2016-01-25 15:34:36 -0800 | [diff] [blame] | 1884 | wallpaper.setComplete = completion; |
| 1885 | wallpaper.cropHint.set(cropHint); |
Christopher Tate | c613c63 | 2016-08-12 14:13:02 -0700 | [diff] [blame] | 1886 | wallpaper.allowBackup = allowBackup; |
Dianne Hackborn | 0cd4887 | 2009-08-13 18:51:59 -0700 | [diff] [blame] | 1887 | } |
| 1888 | return pfd; |
| 1889 | } finally { |
| 1890 | Binder.restoreCallingIdentity(ident); |
Joe Onorato | 9bb8fd7 | 2009-07-28 18:24:51 -0700 | [diff] [blame] | 1891 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1892 | } |
| 1893 | } |
| 1894 | |
Christopher Tate | 8347b63 | 2016-04-29 18:59:18 -0700 | [diff] [blame] | 1895 | private void migrateSystemToLockWallpaperLocked(int userId) { |
| 1896 | WallpaperData sysWP = mWallpaperMap.get(userId); |
| 1897 | if (sysWP == null) { |
| 1898 | if (DEBUG) { |
| 1899 | Slog.i(TAG, "No system wallpaper? Not tracking for lock-only"); |
| 1900 | } |
| 1901 | return; |
| 1902 | } |
| 1903 | |
| 1904 | // We know a-priori that there is no lock-only wallpaper currently |
| 1905 | WallpaperData lockWP = new WallpaperData(userId, |
| 1906 | WALLPAPER_LOCK_ORIG, WALLPAPER_LOCK_CROP); |
| 1907 | lockWP.wallpaperId = sysWP.wallpaperId; |
| 1908 | lockWP.cropHint.set(sysWP.cropHint); |
| 1909 | lockWP.width = sysWP.width; |
| 1910 | lockWP.height = sysWP.height; |
Christopher Tate | edd8dc8 | 2016-10-12 15:17:58 -0700 | [diff] [blame] | 1911 | lockWP.allowBackup = sysWP.allowBackup; |
Lucas Dupin | ea1fb1e | 2017-04-05 17:39:44 -0700 | [diff] [blame] | 1912 | lockWP.primaryColors = sysWP.primaryColors; |
Christopher Tate | 8347b63 | 2016-04-29 18:59:18 -0700 | [diff] [blame] | 1913 | |
| 1914 | // Migrate the bitmap files outright; no need to copy |
| 1915 | try { |
| 1916 | Os.rename(sysWP.wallpaperFile.getAbsolutePath(), lockWP.wallpaperFile.getAbsolutePath()); |
| 1917 | Os.rename(sysWP.cropFile.getAbsolutePath(), lockWP.cropFile.getAbsolutePath()); |
| 1918 | } catch (ErrnoException e) { |
| 1919 | Slog.e(TAG, "Can't migrate system wallpaper: " + e.getMessage()); |
| 1920 | lockWP.wallpaperFile.delete(); |
| 1921 | lockWP.cropFile.delete(); |
| 1922 | return; |
| 1923 | } |
| 1924 | |
| 1925 | mLockWallpaperMap.put(userId, lockWP); |
| 1926 | } |
| 1927 | |
Christopher Tate | ad3c259 | 2016-01-20 18:13:17 -0800 | [diff] [blame] | 1928 | ParcelFileDescriptor updateWallpaperBitmapLocked(String name, WallpaperData wallpaper, |
| 1929 | Bundle extras) { |
Dianne Hackborn | 4c62fc0 | 2009-08-08 20:40:27 -0700 | [diff] [blame] | 1930 | if (name == null) name = ""; |
| 1931 | try { |
Amith Yamasani | 37ce3a8 | 2012-02-06 12:04:42 -0800 | [diff] [blame] | 1932 | File dir = getWallpaperDir(wallpaper.userId); |
| 1933 | if (!dir.exists()) { |
| 1934 | dir.mkdir(); |
Dianne Hackborn | ebac48c | 2011-11-29 18:01:50 -0800 | [diff] [blame] | 1935 | FileUtils.setPermissions( |
Amith Yamasani | 37ce3a8 | 2012-02-06 12:04:42 -0800 | [diff] [blame] | 1936 | dir.getPath(), |
Dianne Hackborn | ebac48c | 2011-11-29 18:01:50 -0800 | [diff] [blame] | 1937 | FileUtils.S_IRWXU|FileUtils.S_IRWXG|FileUtils.S_IXOTH, |
| 1938 | -1, -1); |
| 1939 | } |
Christopher Tate | be132e6 | 2016-02-10 12:59:49 -0800 | [diff] [blame] | 1940 | ParcelFileDescriptor fd = ParcelFileDescriptor.open(wallpaper.wallpaperFile, |
Christopher Tate | 90f86ba | 2014-09-11 12:37:19 -0700 | [diff] [blame] | 1941 | MODE_CREATE|MODE_READ_WRITE|MODE_TRUNCATE); |
Christopher Tate | be132e6 | 2016-02-10 12:59:49 -0800 | [diff] [blame] | 1942 | if (!SELinux.restorecon(wallpaper.wallpaperFile)) { |
rpcraig | 554cb0c | 2012-07-05 06:41:43 -0400 | [diff] [blame] | 1943 | return null; |
| 1944 | } |
Amith Yamasani | 37ce3a8 | 2012-02-06 12:04:42 -0800 | [diff] [blame] | 1945 | wallpaper.name = name; |
Christopher Tate | ad3c259 | 2016-01-20 18:13:17 -0800 | [diff] [blame] | 1946 | wallpaper.wallpaperId = makeWallpaperIdLocked(); |
| 1947 | if (extras != null) { |
| 1948 | extras.putInt(WallpaperManager.EXTRA_NEW_WALLPAPER_ID, wallpaper.wallpaperId); |
| 1949 | } |
Lucas Dupin | ea1fb1e | 2017-04-05 17:39:44 -0700 | [diff] [blame] | 1950 | // Nullify field to require new computation |
| 1951 | wallpaper.primaryColors = null; |
Christopher Tate | ad3c259 | 2016-01-20 18:13:17 -0800 | [diff] [blame] | 1952 | if (DEBUG) { |
| 1953 | Slog.v(TAG, "updateWallpaperBitmapLocked() : id=" + wallpaper.wallpaperId |
Christopher Tate | be132e6 | 2016-02-10 12:59:49 -0800 | [diff] [blame] | 1954 | + " name=" + name + " file=" + wallpaper.wallpaperFile.getName()); |
Christopher Tate | ad3c259 | 2016-01-20 18:13:17 -0800 | [diff] [blame] | 1955 | } |
Dianne Hackborn | 4c62fc0 | 2009-08-08 20:40:27 -0700 | [diff] [blame] | 1956 | return fd; |
| 1957 | } catch (FileNotFoundException e) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1958 | Slog.w(TAG, "Error setting wallpaper", e); |
Dianne Hackborn | 4c62fc0 | 2009-08-08 20:40:27 -0700 | [diff] [blame] | 1959 | } |
| 1960 | return null; |
| 1961 | } |
| 1962 | |
Christopher Tate | d57d17c | 2016-03-25 13:41:46 -0700 | [diff] [blame] | 1963 | @Override |
Adrian Roos | 40ea083 | 2016-07-14 14:19:55 -0700 | [diff] [blame] | 1964 | public void setWallpaperComponentChecked(ComponentName name, String callingPackage, |
| 1965 | int userId) { |
| 1966 | |
Christopher Tate | 98d609c | 2016-05-18 17:31:58 -0700 | [diff] [blame] | 1967 | if (isWallpaperSupported(callingPackage) && isSetWallpaperAllowed(callingPackage)) { |
Adrian Roos | 40ea083 | 2016-07-14 14:19:55 -0700 | [diff] [blame] | 1968 | setWallpaperComponent(name, userId); |
Benjamin Franz | f3ece36 | 2015-02-11 10:51:10 +0000 | [diff] [blame] | 1969 | } |
| 1970 | } |
| 1971 | |
| 1972 | // ToDo: Remove this version of the function |
Christopher Tate | d57d17c | 2016-03-25 13:41:46 -0700 | [diff] [blame] | 1973 | @Override |
Dianne Hackborn | 4c62fc0 | 2009-08-08 20:40:27 -0700 | [diff] [blame] | 1974 | public void setWallpaperComponent(ComponentName name) { |
Adrian Roos | 40ea083 | 2016-07-14 14:19:55 -0700 | [diff] [blame] | 1975 | setWallpaperComponent(name, UserHandle.getCallingUserId()); |
| 1976 | } |
| 1977 | |
| 1978 | private void setWallpaperComponent(ComponentName name, int userId) { |
| 1979 | userId = ActivityManager.handleIncomingUser(getCallingPid(), getCallingUid(), userId, |
| 1980 | false /* all */, true /* full */, "changing live wallpaper", null /* pkg */); |
Dianne Hackborn | 4c62fc0 | 2009-08-08 20:40:27 -0700 | [diff] [blame] | 1981 | checkPermission(android.Manifest.permission.SET_WALLPAPER_COMPONENT); |
Adrian Roos | 40ea083 | 2016-07-14 14:19:55 -0700 | [diff] [blame] | 1982 | |
Lucas Dupin | ea1fb1e | 2017-04-05 17:39:44 -0700 | [diff] [blame] | 1983 | int which = FLAG_SYSTEM; |
| 1984 | boolean shouldNotifyColors = false; |
| 1985 | WallpaperData wallpaper; |
| 1986 | |
Dianne Hackborn | 4c62fc0 | 2009-08-08 20:40:27 -0700 | [diff] [blame] | 1987 | synchronized (mLock) { |
Dianne Hackborn | 5dc5a00 | 2012-09-15 19:33:48 -0700 | [diff] [blame] | 1988 | if (DEBUG) Slog.v(TAG, "setWallpaperComponent name=" + name); |
Lucas Dupin | ea1fb1e | 2017-04-05 17:39:44 -0700 | [diff] [blame] | 1989 | wallpaper = mWallpaperMap.get(userId); |
Dianne Hackborn | 5dc5a00 | 2012-09-15 19:33:48 -0700 | [diff] [blame] | 1990 | if (wallpaper == null) { |
| 1991 | throw new IllegalStateException("Wallpaper not yet initialized for user " + userId); |
| 1992 | } |
Dianne Hackborn | 4c62fc0 | 2009-08-08 20:40:27 -0700 | [diff] [blame] | 1993 | final long ident = Binder.clearCallingIdentity(); |
Christopher Tate | 7cd0010 | 2016-12-19 14:38:44 -0800 | [diff] [blame] | 1994 | |
| 1995 | // Live wallpapers can't be specified for keyguard. If we're using a static |
| 1996 | // system+lock image currently, migrate the system wallpaper to be a lock-only |
| 1997 | // image as part of making a different live component active as the system |
| 1998 | // wallpaper. |
| 1999 | if (mImageWallpaper.equals(wallpaper.wallpaperComponent)) { |
| 2000 | if (mLockWallpaperMap.get(userId) == null) { |
| 2001 | // We're using the static imagery and there is no lock-specific image in place, |
| 2002 | // therefore it's a shared system+lock image that we need to migrate. |
| 2003 | migrateSystemToLockWallpaperLocked(userId); |
| 2004 | } |
| 2005 | } |
| 2006 | |
Lucas Dupin | ea1fb1e | 2017-04-05 17:39:44 -0700 | [diff] [blame] | 2007 | // New live wallpaper is also a lock wallpaper if nothing is set |
| 2008 | if (mLockWallpaperMap.get(userId) == null) { |
| 2009 | which |= FLAG_LOCK; |
| 2010 | } |
| 2011 | |
Dianne Hackborn | 4c62fc0 | 2009-08-08 20:40:27 -0700 | [diff] [blame] | 2012 | try { |
Amith Yamasani | 37ce3a8 | 2012-02-06 12:04:42 -0800 | [diff] [blame] | 2013 | wallpaper.imageWallpaperPending = false; |
Lucas Dupin | 50ba991 | 2017-07-14 11:55:05 -0700 | [diff] [blame] | 2014 | boolean same = changingToSame(name, wallpaper); |
Christopher Tate | d57d17c | 2016-03-25 13:41:46 -0700 | [diff] [blame] | 2015 | if (bindWallpaperComponentLocked(name, false, true, wallpaper, null)) { |
Lucas Dupin | 50ba991 | 2017-07-14 11:55:05 -0700 | [diff] [blame] | 2016 | if (!same) { |
| 2017 | wallpaper.primaryColors = null; |
| 2018 | } |
Christopher Tate | d57d17c | 2016-03-25 13:41:46 -0700 | [diff] [blame] | 2019 | wallpaper.wallpaperId = makeWallpaperIdLocked(); |
Sunny Goyal | 0572e18 | 2016-03-31 11:05:51 -0700 | [diff] [blame] | 2020 | notifyCallbacksLocked(wallpaper); |
Lucas Dupin | ea1fb1e | 2017-04-05 17:39:44 -0700 | [diff] [blame] | 2021 | shouldNotifyColors = true; |
Christopher Tate | d57d17c | 2016-03-25 13:41:46 -0700 | [diff] [blame] | 2022 | } |
Dianne Hackborn | f21adf6 | 2009-08-13 10:20:21 -0700 | [diff] [blame] | 2023 | } finally { |
| 2024 | Binder.restoreCallingIdentity(ident); |
| 2025 | } |
| 2026 | } |
Lucas Dupin | ea1fb1e | 2017-04-05 17:39:44 -0700 | [diff] [blame] | 2027 | |
| 2028 | if (shouldNotifyColors) { |
| 2029 | notifyWallpaperColorsChanged(wallpaper, which); |
| 2030 | } |
Dianne Hackborn | f21adf6 | 2009-08-13 10:20:21 -0700 | [diff] [blame] | 2031 | } |
Oleksandr Peletskyi | f251981 | 2016-01-26 20:16:06 +0100 | [diff] [blame] | 2032 | |
Lucas Dupin | 50ba991 | 2017-07-14 11:55:05 -0700 | [diff] [blame] | 2033 | private boolean changingToSame(ComponentName componentName, WallpaperData wallpaper) { |
| 2034 | if (wallpaper.connection != null) { |
| 2035 | if (wallpaper.wallpaperComponent == null) { |
| 2036 | if (componentName == null) { |
| 2037 | if (DEBUG) Slog.v(TAG, "changingToSame: still using default"); |
| 2038 | // Still using default wallpaper. |
| 2039 | return true; |
| 2040 | } |
| 2041 | } else if (wallpaper.wallpaperComponent.equals(componentName)) { |
| 2042 | // Changing to same wallpaper. |
| 2043 | if (DEBUG) Slog.v(TAG, "same wallpaper"); |
| 2044 | return true; |
| 2045 | } |
| 2046 | } |
| 2047 | return false; |
| 2048 | } |
| 2049 | |
Amith Yamasani | 37ce3a8 | 2012-02-06 12:04:42 -0800 | [diff] [blame] | 2050 | boolean bindWallpaperComponentLocked(ComponentName componentName, boolean force, |
Dianne Hackborn | 5dc5a00 | 2012-09-15 19:33:48 -0700 | [diff] [blame] | 2051 | boolean fromUser, WallpaperData wallpaper, IRemoteCallback reply) { |
Christopher Tate | 2a6c55f | 2017-03-31 12:28:30 -0700 | [diff] [blame] | 2052 | if (DEBUG_LIVE) { |
| 2053 | Slog.v(TAG, "bindWallpaperComponentLocked: componentName=" + componentName); |
| 2054 | } |
Dianne Hackborn | f21adf6 | 2009-08-13 10:20:21 -0700 | [diff] [blame] | 2055 | // Has the component changed? |
Lucas Dupin | 50ba991 | 2017-07-14 11:55:05 -0700 | [diff] [blame] | 2056 | if (!force && changingToSame(componentName, wallpaper)) { |
| 2057 | return true; |
Dianne Hackborn | f21adf6 | 2009-08-13 10:20:21 -0700 | [diff] [blame] | 2058 | } |
Oleksandr Peletskyi | f251981 | 2016-01-26 20:16:06 +0100 | [diff] [blame] | 2059 | |
Dianne Hackborn | f21adf6 | 2009-08-13 10:20:21 -0700 | [diff] [blame] | 2060 | try { |
Mike Cleron | a428b2c | 2009-11-15 22:53:08 -0800 | [diff] [blame] | 2061 | if (componentName == null) { |
Christopher Tate | 2a6c55f | 2017-03-31 12:28:30 -0700 | [diff] [blame] | 2062 | componentName = mDefaultWallpaperComponent; |
Mike Cleron | a428b2c | 2009-11-15 22:53:08 -0800 | [diff] [blame] | 2063 | if (componentName == null) { |
Mike Cleron | 322b6ee | 2009-11-12 07:45:47 -0800 | [diff] [blame] | 2064 | // Fall back to static image wallpaper |
Justin Koh | 29c3016 | 2014-09-05 17:10:10 -0700 | [diff] [blame] | 2065 | componentName = mImageWallpaper; |
Mike Cleron | 322b6ee | 2009-11-12 07:45:47 -0800 | [diff] [blame] | 2066 | //clearWallpaperComponentLocked(); |
| 2067 | //return; |
Christopher Tate | 2a6c55f | 2017-03-31 12:28:30 -0700 | [diff] [blame] | 2068 | if (DEBUG_LIVE) Slog.v(TAG, "No default component; using image wallpaper"); |
Mike Cleron | 322b6ee | 2009-11-12 07:45:47 -0800 | [diff] [blame] | 2069 | } |
Dianne Hackborn | f21adf6 | 2009-08-13 10:20:21 -0700 | [diff] [blame] | 2070 | } |
Amith Yamasani | 4e2820c | 2012-08-28 22:17:23 -0700 | [diff] [blame] | 2071 | int serviceUserId = wallpaper.userId; |
| 2072 | ServiceInfo si = mIPackageManager.getServiceInfo(componentName, |
| 2073 | PackageManager.GET_META_DATA | PackageManager.GET_PERMISSIONS, serviceUserId); |
Christopher Tate | 9095220 | 2013-09-08 13:01:28 -0700 | [diff] [blame] | 2074 | if (si == null) { |
| 2075 | // The wallpaper component we're trying to use doesn't exist |
| 2076 | Slog.w(TAG, "Attempted wallpaper " + componentName + " is unavailable"); |
| 2077 | return false; |
| 2078 | } |
Dianne Hackborn | f21adf6 | 2009-08-13 10:20:21 -0700 | [diff] [blame] | 2079 | if (!android.Manifest.permission.BIND_WALLPAPER.equals(si.permission)) { |
Dianne Hackborn | 80b902f | 2011-09-15 15:15:27 -0700 | [diff] [blame] | 2080 | String msg = "Selected service does not require " |
Dianne Hackborn | f21adf6 | 2009-08-13 10:20:21 -0700 | [diff] [blame] | 2081 | + android.Manifest.permission.BIND_WALLPAPER |
Dianne Hackborn | 80b902f | 2011-09-15 15:15:27 -0700 | [diff] [blame] | 2082 | + ": " + componentName; |
| 2083 | if (fromUser) { |
| 2084 | throw new SecurityException(msg); |
| 2085 | } |
| 2086 | Slog.w(TAG, msg); |
| 2087 | return false; |
Dianne Hackborn | f21adf6 | 2009-08-13 10:20:21 -0700 | [diff] [blame] | 2088 | } |
Oleksandr Peletskyi | f251981 | 2016-01-26 20:16:06 +0100 | [diff] [blame] | 2089 | |
Dianne Hackborn | eb03465 | 2009-09-07 00:49:58 -0700 | [diff] [blame] | 2090 | WallpaperInfo wi = null; |
Oleksandr Peletskyi | f251981 | 2016-01-26 20:16:06 +0100 | [diff] [blame] | 2091 | |
Dianne Hackborn | f21adf6 | 2009-08-13 10:20:21 -0700 | [diff] [blame] | 2092 | Intent intent = new Intent(WallpaperService.SERVICE_INTERFACE); |
Justin Koh | 29c3016 | 2014-09-05 17:10:10 -0700 | [diff] [blame] | 2093 | if (componentName != null && !componentName.equals(mImageWallpaper)) { |
Dianne Hackborn | 4c62fc0 | 2009-08-08 20:40:27 -0700 | [diff] [blame] | 2094 | // Make sure the selected service is actually a wallpaper service. |
Amith Yamasani | 4e2820c | 2012-08-28 22:17:23 -0700 | [diff] [blame] | 2095 | List<ResolveInfo> ris = |
| 2096 | mIPackageManager.queryIntentServices(intent, |
| 2097 | intent.resolveTypeIfNeeded(mContext.getContentResolver()), |
Jeff Sharkey | d589663 | 2016-03-04 16:16:00 -0700 | [diff] [blame] | 2098 | PackageManager.GET_META_DATA, serviceUserId).getList(); |
Dianne Hackborn | 4c62fc0 | 2009-08-08 20:40:27 -0700 | [diff] [blame] | 2099 | for (int i=0; i<ris.size(); i++) { |
| 2100 | ServiceInfo rsi = ris.get(i).serviceInfo; |
| 2101 | if (rsi.name.equals(si.name) && |
| 2102 | rsi.packageName.equals(si.packageName)) { |
Dianne Hackborn | eb03465 | 2009-09-07 00:49:58 -0700 | [diff] [blame] | 2103 | try { |
| 2104 | wi = new WallpaperInfo(mContext, ris.get(i)); |
| 2105 | } catch (XmlPullParserException e) { |
Dianne Hackborn | 80b902f | 2011-09-15 15:15:27 -0700 | [diff] [blame] | 2106 | if (fromUser) { |
| 2107 | throw new IllegalArgumentException(e); |
| 2108 | } |
| 2109 | Slog.w(TAG, e); |
| 2110 | return false; |
Dianne Hackborn | eb03465 | 2009-09-07 00:49:58 -0700 | [diff] [blame] | 2111 | } catch (IOException e) { |
Dianne Hackborn | 80b902f | 2011-09-15 15:15:27 -0700 | [diff] [blame] | 2112 | if (fromUser) { |
| 2113 | throw new IllegalArgumentException(e); |
| 2114 | } |
| 2115 | Slog.w(TAG, e); |
| 2116 | return false; |
Dianne Hackborn | eb03465 | 2009-09-07 00:49:58 -0700 | [diff] [blame] | 2117 | } |
Dianne Hackborn | 4c62fc0 | 2009-08-08 20:40:27 -0700 | [diff] [blame] | 2118 | break; |
| 2119 | } |
| 2120 | } |
Dianne Hackborn | eb03465 | 2009-09-07 00:49:58 -0700 | [diff] [blame] | 2121 | if (wi == null) { |
Dianne Hackborn | 80b902f | 2011-09-15 15:15:27 -0700 | [diff] [blame] | 2122 | String msg = "Selected service is not a wallpaper: " |
| 2123 | + componentName; |
| 2124 | if (fromUser) { |
| 2125 | throw new SecurityException(msg); |
| 2126 | } |
| 2127 | Slog.w(TAG, msg); |
| 2128 | return false; |
Dianne Hackborn | 4c62fc0 | 2009-08-08 20:40:27 -0700 | [diff] [blame] | 2129 | } |
Dianne Hackborn | 4c62fc0 | 2009-08-08 20:40:27 -0700 | [diff] [blame] | 2130 | } |
Oleksandr Peletskyi | f251981 | 2016-01-26 20:16:06 +0100 | [diff] [blame] | 2131 | |
Dianne Hackborn | f21adf6 | 2009-08-13 10:20:21 -0700 | [diff] [blame] | 2132 | // Bind the service! |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 2133 | if (DEBUG) Slog.v(TAG, "Binding to:" + componentName); |
Amith Yamasani | 37ce3a8 | 2012-02-06 12:04:42 -0800 | [diff] [blame] | 2134 | WallpaperConnection newConn = new WallpaperConnection(wi, wallpaper); |
Mike Cleron | a428b2c | 2009-11-15 22:53:08 -0800 | [diff] [blame] | 2135 | intent.setComponent(componentName); |
Dianne Hackborn | dd9b82c | 2009-09-03 00:18:47 -0700 | [diff] [blame] | 2136 | intent.putExtra(Intent.EXTRA_CLIENT_LABEL, |
| 2137 | com.android.internal.R.string.wallpaper_binding_label); |
Dianne Hackborn | 4120375 | 2012-08-31 14:05:51 -0700 | [diff] [blame] | 2138 | intent.putExtra(Intent.EXTRA_CLIENT_INTENT, PendingIntent.getActivityAsUser( |
Dianne Hackborn | eb03465 | 2009-09-07 00:49:58 -0700 | [diff] [blame] | 2139 | mContext, 0, |
| 2140 | Intent.createChooser(new Intent(Intent.ACTION_SET_WALLPAPER), |
| 2141 | mContext.getText(com.android.internal.R.string.chooser_wallpaper)), |
Dianne Hackborn | 4120375 | 2012-08-31 14:05:51 -0700 | [diff] [blame] | 2142 | 0, null, new UserHandle(serviceUserId))); |
Dianne Hackborn | c823051 | 2013-07-13 21:32:12 -0700 | [diff] [blame] | 2143 | if (!mContext.bindServiceAsUser(intent, newConn, |
Dianne Hackborn | d69e4c1 | 2015-04-24 09:54:54 -0700 | [diff] [blame] | 2144 | Context.BIND_AUTO_CREATE | Context.BIND_SHOWING_UI |
| 2145 | | Context.BIND_FOREGROUND_SERVICE_WHILE_AWAKE, |
Amith Yamasani | 27b89e6 | 2013-01-16 12:30:11 -0800 | [diff] [blame] | 2146 | new UserHandle(serviceUserId))) { |
Dianne Hackborn | 80b902f | 2011-09-15 15:15:27 -0700 | [diff] [blame] | 2147 | String msg = "Unable to bind service: " |
| 2148 | + componentName; |
| 2149 | if (fromUser) { |
| 2150 | throw new IllegalArgumentException(msg); |
| 2151 | } |
| 2152 | Slog.w(TAG, msg); |
| 2153 | return false; |
Dianne Hackborn | f21adf6 | 2009-08-13 10:20:21 -0700 | [diff] [blame] | 2154 | } |
Amith Yamasani | 37ce3a8 | 2012-02-06 12:04:42 -0800 | [diff] [blame] | 2155 | if (wallpaper.userId == mCurrentUserId && mLastWallpaper != null) { |
| 2156 | detachWallpaperLocked(mLastWallpaper); |
| 2157 | } |
| 2158 | wallpaper.wallpaperComponent = componentName; |
| 2159 | wallpaper.connection = newConn; |
Dianne Hackborn | 5dc5a00 | 2012-09-15 19:33:48 -0700 | [diff] [blame] | 2160 | newConn.mReply = reply; |
Dianne Hackborn | f21adf6 | 2009-08-13 10:20:21 -0700 | [diff] [blame] | 2161 | try { |
Amith Yamasani | 37ce3a8 | 2012-02-06 12:04:42 -0800 | [diff] [blame] | 2162 | if (wallpaper.userId == mCurrentUserId) { |
| 2163 | if (DEBUG) |
| 2164 | Slog.v(TAG, "Adding window token: " + newConn.mToken); |
Wale Ogunwale | ac2561e | 2016-11-01 15:43:46 -0700 | [diff] [blame] | 2165 | mIWindowManager.addWindowToken(newConn.mToken, TYPE_WALLPAPER, DEFAULT_DISPLAY); |
Amith Yamasani | 37ce3a8 | 2012-02-06 12:04:42 -0800 | [diff] [blame] | 2166 | mLastWallpaper = wallpaper; |
| 2167 | } |
Dianne Hackborn | f21adf6 | 2009-08-13 10:20:21 -0700 | [diff] [blame] | 2168 | } catch (RemoteException e) { |
| 2169 | } |
Amith Yamasani | 4e2820c | 2012-08-28 22:17:23 -0700 | [diff] [blame] | 2170 | } catch (RemoteException e) { |
| 2171 | String msg = "Remote exception for " + componentName + "\n" + e; |
Dianne Hackborn | 80b902f | 2011-09-15 15:15:27 -0700 | [diff] [blame] | 2172 | if (fromUser) { |
| 2173 | throw new IllegalArgumentException(msg); |
| 2174 | } |
| 2175 | Slog.w(TAG, msg); |
| 2176 | return false; |
Dianne Hackborn | 4c62fc0 | 2009-08-08 20:40:27 -0700 | [diff] [blame] | 2177 | } |
Dianne Hackborn | 80b902f | 2011-09-15 15:15:27 -0700 | [diff] [blame] | 2178 | return true; |
Dianne Hackborn | 4c62fc0 | 2009-08-08 20:40:27 -0700 | [diff] [blame] | 2179 | } |
Amith Yamasani | 37ce3a8 | 2012-02-06 12:04:42 -0800 | [diff] [blame] | 2180 | |
| 2181 | void detachWallpaperLocked(WallpaperData wallpaper) { |
| 2182 | if (wallpaper.connection != null) { |
Dianne Hackborn | 5dc5a00 | 2012-09-15 19:33:48 -0700 | [diff] [blame] | 2183 | if (wallpaper.connection.mReply != null) { |
| 2184 | try { |
| 2185 | wallpaper.connection.mReply.sendResult(null); |
| 2186 | } catch (RemoteException e) { |
| 2187 | } |
| 2188 | wallpaper.connection.mReply = null; |
| 2189 | } |
Amith Yamasani | 37ce3a8 | 2012-02-06 12:04:42 -0800 | [diff] [blame] | 2190 | if (wallpaper.connection.mEngine != null) { |
Dianne Hackborn | 4c62fc0 | 2009-08-08 20:40:27 -0700 | [diff] [blame] | 2191 | try { |
Amith Yamasani | 37ce3a8 | 2012-02-06 12:04:42 -0800 | [diff] [blame] | 2192 | wallpaper.connection.mEngine.destroy(); |
Dianne Hackborn | 4c62fc0 | 2009-08-08 20:40:27 -0700 | [diff] [blame] | 2193 | } catch (RemoteException e) { |
| 2194 | } |
| 2195 | } |
Amith Yamasani | 37ce3a8 | 2012-02-06 12:04:42 -0800 | [diff] [blame] | 2196 | mContext.unbindService(wallpaper.connection); |
Dianne Hackborn | e9e9bca | 2009-08-18 15:08:22 -0700 | [diff] [blame] | 2197 | try { |
Amith Yamasani | 37ce3a8 | 2012-02-06 12:04:42 -0800 | [diff] [blame] | 2198 | if (DEBUG) |
| 2199 | Slog.v(TAG, "Removing window token: " + wallpaper.connection.mToken); |
Wale Ogunwale | ac2561e | 2016-11-01 15:43:46 -0700 | [diff] [blame] | 2200 | mIWindowManager.removeWindowToken(wallpaper.connection.mToken, DEFAULT_DISPLAY); |
Dianne Hackborn | e9e9bca | 2009-08-18 15:08:22 -0700 | [diff] [blame] | 2201 | } catch (RemoteException e) { |
| 2202 | } |
Amith Yamasani | 37ce3a8 | 2012-02-06 12:04:42 -0800 | [diff] [blame] | 2203 | wallpaper.connection.mService = null; |
| 2204 | wallpaper.connection.mEngine = null; |
| 2205 | wallpaper.connection = null; |
Dianne Hackborn | 4c62fc0 | 2009-08-08 20:40:27 -0700 | [diff] [blame] | 2206 | } |
| 2207 | } |
Amith Yamasani | 37ce3a8 | 2012-02-06 12:04:42 -0800 | [diff] [blame] | 2208 | |
| 2209 | void clearWallpaperComponentLocked(WallpaperData wallpaper) { |
| 2210 | wallpaper.wallpaperComponent = null; |
| 2211 | detachWallpaperLocked(wallpaper); |
| 2212 | } |
| 2213 | |
| 2214 | void attachServiceLocked(WallpaperConnection conn, WallpaperData wallpaper) { |
Dianne Hackborn | 4c62fc0 | 2009-08-08 20:40:27 -0700 | [diff] [blame] | 2215 | try { |
Dianne Hackborn | 3be63c0 | 2009-08-20 19:31:38 -0700 | [diff] [blame] | 2216 | conn.mService.attach(conn, conn.mToken, |
Wale Ogunwale | ac2561e | 2016-11-01 15:43:46 -0700 | [diff] [blame] | 2217 | TYPE_WALLPAPER, false, |
Dianne Hackborn | 067e5f6 | 2014-09-07 23:14:30 -0700 | [diff] [blame] | 2218 | wallpaper.width, wallpaper.height, wallpaper.padding); |
Dianne Hackborn | 4c62fc0 | 2009-08-08 20:40:27 -0700 | [diff] [blame] | 2219 | } catch (RemoteException e) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 2220 | Slog.w(TAG, "Failed attaching wallpaper; clearing", e); |
Amith Yamasani | 37ce3a8 | 2012-02-06 12:04:42 -0800 | [diff] [blame] | 2221 | if (!wallpaper.wallpaperUpdating) { |
Dianne Hackborn | 5dc5a00 | 2012-09-15 19:33:48 -0700 | [diff] [blame] | 2222 | bindWallpaperComponentLocked(null, false, false, wallpaper, null); |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 2223 | } |
Dianne Hackborn | 4c62fc0 | 2009-08-08 20:40:27 -0700 | [diff] [blame] | 2224 | } |
| 2225 | } |
Amith Yamasani | 37ce3a8 | 2012-02-06 12:04:42 -0800 | [diff] [blame] | 2226 | |
| 2227 | private void notifyCallbacksLocked(WallpaperData wallpaper) { |
| 2228 | final int n = wallpaper.callbacks.beginBroadcast(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2229 | for (int i = 0; i < n; i++) { |
| 2230 | try { |
Amith Yamasani | 37ce3a8 | 2012-02-06 12:04:42 -0800 | [diff] [blame] | 2231 | wallpaper.callbacks.getBroadcastItem(i).onWallpaperChanged(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2232 | } catch (RemoteException e) { |
| 2233 | |
| 2234 | // The RemoteCallbackList will take care of removing |
| 2235 | // the dead object for us. |
| 2236 | } |
| 2237 | } |
Amith Yamasani | 37ce3a8 | 2012-02-06 12:04:42 -0800 | [diff] [blame] | 2238 | wallpaper.callbacks.finishBroadcast(); |
Lucas Dupin | ea1fb1e | 2017-04-05 17:39:44 -0700 | [diff] [blame] | 2239 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2240 | final Intent intent = new Intent(Intent.ACTION_WALLPAPER_CHANGED); |
Dianne Hackborn | 5ac72a2 | 2012-08-29 18:32:08 -0700 | [diff] [blame] | 2241 | mContext.sendBroadcastAsUser(intent, new UserHandle(mCurrentUserId)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2242 | } |
| 2243 | |
| 2244 | private void checkPermission(String permission) { |
Joe Onorato | 9bb8fd7 | 2009-07-28 18:24:51 -0700 | [diff] [blame] | 2245 | if (PackageManager.PERMISSION_GRANTED!= mContext.checkCallingOrSelfPermission(permission)) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2246 | throw new SecurityException("Access denied to process: " + Binder.getCallingPid() |
| 2247 | + ", must have permission " + permission); |
| 2248 | } |
| 2249 | } |
Joe Onorato | 9bb8fd7 | 2009-07-28 18:24:51 -0700 | [diff] [blame] | 2250 | |
Benjamin Franz | f3ece36 | 2015-02-11 10:51:10 +0000 | [diff] [blame] | 2251 | /** |
| 2252 | * Certain user types do not support wallpapers (e.g. managed profiles). The check is |
| 2253 | * implemented through through the OP_WRITE_WALLPAPER AppOp. |
| 2254 | */ |
| 2255 | public boolean isWallpaperSupported(String callingPackage) { |
| 2256 | return mAppOpsManager.checkOpNoThrow(AppOpsManager.OP_WRITE_WALLPAPER, Binder.getCallingUid(), |
| 2257 | callingPackage) == AppOpsManager.MODE_ALLOWED; |
| 2258 | } |
| 2259 | |
Oleksandr Peletskyi | f251981 | 2016-01-26 20:16:06 +0100 | [diff] [blame] | 2260 | @Override |
Christopher Tate | 98d609c | 2016-05-18 17:31:58 -0700 | [diff] [blame] | 2261 | public boolean isSetWallpaperAllowed(String callingPackage) { |
Oleksandr Peletskyi | f251981 | 2016-01-26 20:16:06 +0100 | [diff] [blame] | 2262 | final PackageManager pm = mContext.getPackageManager(); |
| 2263 | String[] uidPackages = pm.getPackagesForUid(Binder.getCallingUid()); |
| 2264 | boolean uidMatchPackage = Arrays.asList(uidPackages).contains(callingPackage); |
| 2265 | if (!uidMatchPackage) { |
| 2266 | return false; // callingPackage was faked. |
| 2267 | } |
| 2268 | |
| 2269 | final DevicePolicyManager dpm = mContext.getSystemService(DevicePolicyManager.class); |
| 2270 | if (dpm.isDeviceOwnerApp(callingPackage) || dpm.isProfileOwnerApp(callingPackage)) { |
| 2271 | return true; |
| 2272 | } |
| 2273 | final UserManager um = (UserManager) mContext.getSystemService(Context.USER_SERVICE); |
| 2274 | return !um.hasUserRestriction(UserManager.DISALLOW_SET_WALLPAPER); |
| 2275 | } |
| 2276 | |
Christopher Tate | d7faf53 | 2016-02-25 12:43:38 -0800 | [diff] [blame] | 2277 | @Override |
Christopher Tate | 6172266 | 2016-08-10 16:13:14 -0700 | [diff] [blame] | 2278 | public boolean isWallpaperBackupEligible(int which, int userId) { |
Christopher Tate | d7faf53 | 2016-02-25 12:43:38 -0800 | [diff] [blame] | 2279 | if (Binder.getCallingUid() != Process.SYSTEM_UID) { |
| 2280 | throw new SecurityException("Only the system may call isWallpaperBackupEligible"); |
| 2281 | } |
| 2282 | |
Christopher Tate | 6172266 | 2016-08-10 16:13:14 -0700 | [diff] [blame] | 2283 | WallpaperData wallpaper = (which == FLAG_LOCK) |
Christopher Tate | f7d1b5d | 2016-08-19 11:21:07 -0700 | [diff] [blame] | 2284 | ? mLockWallpaperMap.get(userId) |
| 2285 | : mWallpaperMap.get(userId); |
Christopher Tate | d7faf53 | 2016-02-25 12:43:38 -0800 | [diff] [blame] | 2286 | return (wallpaper != null) ? wallpaper.allowBackup : false; |
| 2287 | } |
| 2288 | |
Amith Yamasani | 37ce3a8 | 2012-02-06 12:04:42 -0800 | [diff] [blame] | 2289 | private static JournaledFile makeJournaledFile(int userId) { |
Amith Yamasani | 61f5737 | 2012-08-31 12:12:28 -0700 | [diff] [blame] | 2290 | final String base = new File(getWallpaperDir(userId), WALLPAPER_INFO).getAbsolutePath(); |
Joe Onorato | 9bb8fd7 | 2009-07-28 18:24:51 -0700 | [diff] [blame] | 2291 | return new JournaledFile(new File(base), new File(base + ".tmp")); |
| 2292 | } |
| 2293 | |
Christopher Tate | db27b84 | 2016-02-25 14:39:17 -0800 | [diff] [blame] | 2294 | private void saveSettingsLocked(int userId) { |
| 2295 | JournaledFile journal = makeJournaledFile(userId); |
| 2296 | FileOutputStream fstream = null; |
| 2297 | BufferedOutputStream stream = null; |
Joe Onorato | 9bb8fd7 | 2009-07-28 18:24:51 -0700 | [diff] [blame] | 2298 | try { |
Joe Onorato | 9bb8fd7 | 2009-07-28 18:24:51 -0700 | [diff] [blame] | 2299 | XmlSerializer out = new FastXmlSerializer(); |
Christopher Tate | db27b84 | 2016-02-25 14:39:17 -0800 | [diff] [blame] | 2300 | fstream = new FileOutputStream(journal.chooseForWrite(), false); |
| 2301 | stream = new BufferedOutputStream(fstream); |
Wojciech Staszkiewicz | 9e9e2e7 | 2015-05-08 14:58:46 +0100 | [diff] [blame] | 2302 | out.setOutput(stream, StandardCharsets.UTF_8.name()); |
Joe Onorato | 9bb8fd7 | 2009-07-28 18:24:51 -0700 | [diff] [blame] | 2303 | out.startDocument(null, true); |
| 2304 | |
Christopher Tate | db27b84 | 2016-02-25 14:39:17 -0800 | [diff] [blame] | 2305 | WallpaperData wallpaper; |
Christopher Tate | 1e1e2e0 | 2016-01-25 15:34:36 -0800 | [diff] [blame] | 2306 | |
Christopher Tate | db27b84 | 2016-02-25 14:39:17 -0800 | [diff] [blame] | 2307 | wallpaper = mWallpaperMap.get(userId); |
| 2308 | if (wallpaper != null) { |
| 2309 | writeWallpaperAttributes(out, "wp", wallpaper); |
Dianne Hackborn | 067e5f6 | 2014-09-07 23:14:30 -0700 | [diff] [blame] | 2310 | } |
Christopher Tate | db27b84 | 2016-02-25 14:39:17 -0800 | [diff] [blame] | 2311 | wallpaper = mLockWallpaperMap.get(userId); |
| 2312 | if (wallpaper != null) { |
| 2313 | writeWallpaperAttributes(out, "kwp", wallpaper); |
Dianne Hackborn | 067e5f6 | 2014-09-07 23:14:30 -0700 | [diff] [blame] | 2314 | } |
Joe Onorato | 9bb8fd7 | 2009-07-28 18:24:51 -0700 | [diff] [blame] | 2315 | |
| 2316 | out.endDocument(); |
Christopher Tate | db27b84 | 2016-02-25 14:39:17 -0800 | [diff] [blame] | 2317 | |
| 2318 | stream.flush(); // also flushes fstream |
| 2319 | FileUtils.sync(fstream); |
| 2320 | stream.close(); // also closes fstream |
Joe Onorato | 9bb8fd7 | 2009-07-28 18:24:51 -0700 | [diff] [blame] | 2321 | journal.commit(); |
| 2322 | } catch (IOException e) { |
Christopher Tate | ad3c259 | 2016-01-20 18:13:17 -0800 | [diff] [blame] | 2323 | IoUtils.closeQuietly(stream); |
Joe Onorato | 9bb8fd7 | 2009-07-28 18:24:51 -0700 | [diff] [blame] | 2324 | journal.rollback(); |
| 2325 | } |
| 2326 | } |
| 2327 | |
Christopher Tate | db27b84 | 2016-02-25 14:39:17 -0800 | [diff] [blame] | 2328 | private void writeWallpaperAttributes(XmlSerializer out, String tag, WallpaperData wallpaper) |
| 2329 | throws IllegalArgumentException, IllegalStateException, IOException { |
Lucas Dupin | ea1fb1e | 2017-04-05 17:39:44 -0700 | [diff] [blame] | 2330 | if (DEBUG) { |
Christopher Tate | f717b93 | 2017-09-11 15:52:54 -0700 | [diff] [blame] | 2331 | Slog.v(TAG, "writeWallpaperAttributes id=" + wallpaper.wallpaperId); |
Lucas Dupin | ea1fb1e | 2017-04-05 17:39:44 -0700 | [diff] [blame] | 2332 | } |
Christopher Tate | db27b84 | 2016-02-25 14:39:17 -0800 | [diff] [blame] | 2333 | out.startTag(null, tag); |
| 2334 | out.attribute(null, "id", Integer.toString(wallpaper.wallpaperId)); |
| 2335 | out.attribute(null, "width", Integer.toString(wallpaper.width)); |
| 2336 | out.attribute(null, "height", Integer.toString(wallpaper.height)); |
| 2337 | |
| 2338 | out.attribute(null, "cropLeft", Integer.toString(wallpaper.cropHint.left)); |
| 2339 | out.attribute(null, "cropTop", Integer.toString(wallpaper.cropHint.top)); |
| 2340 | out.attribute(null, "cropRight", Integer.toString(wallpaper.cropHint.right)); |
| 2341 | out.attribute(null, "cropBottom", Integer.toString(wallpaper.cropHint.bottom)); |
| 2342 | |
| 2343 | if (wallpaper.padding.left != 0) { |
| 2344 | out.attribute(null, "paddingLeft", Integer.toString(wallpaper.padding.left)); |
| 2345 | } |
| 2346 | if (wallpaper.padding.top != 0) { |
| 2347 | out.attribute(null, "paddingTop", Integer.toString(wallpaper.padding.top)); |
| 2348 | } |
| 2349 | if (wallpaper.padding.right != 0) { |
| 2350 | out.attribute(null, "paddingRight", Integer.toString(wallpaper.padding.right)); |
| 2351 | } |
| 2352 | if (wallpaper.padding.bottom != 0) { |
| 2353 | out.attribute(null, "paddingBottom", Integer.toString(wallpaper.padding.bottom)); |
| 2354 | } |
| 2355 | |
Lucas Dupin | ea1fb1e | 2017-04-05 17:39:44 -0700 | [diff] [blame] | 2356 | if (wallpaper.primaryColors != null) { |
Lucas Dupin | 84b89d9 | 2017-05-09 12:16:19 -0700 | [diff] [blame] | 2357 | int colorsCount = wallpaper.primaryColors.getMainColors().size(); |
Lucas Dupin | ea1fb1e | 2017-04-05 17:39:44 -0700 | [diff] [blame] | 2358 | out.attribute(null, "colorsCount", Integer.toString(colorsCount)); |
| 2359 | if (colorsCount > 0) { |
| 2360 | for (int i = 0; i < colorsCount; i++) { |
Lucas Dupin | 84b89d9 | 2017-05-09 12:16:19 -0700 | [diff] [blame] | 2361 | final Color wc = wallpaper.primaryColors.getMainColors().get(i); |
| 2362 | out.attribute(null, "colorValue"+i, Integer.toString(wc.toArgb())); |
Lucas Dupin | ea1fb1e | 2017-04-05 17:39:44 -0700 | [diff] [blame] | 2363 | } |
| 2364 | } |
Lucas Dupin | 75ec379 | 2017-06-29 14:07:18 -0700 | [diff] [blame] | 2365 | out.attribute(null, "colorHints", |
Lucas Dupin | 84b89d9 | 2017-05-09 12:16:19 -0700 | [diff] [blame] | 2366 | Integer.toString(wallpaper.primaryColors.getColorHints())); |
Lucas Dupin | ea1fb1e | 2017-04-05 17:39:44 -0700 | [diff] [blame] | 2367 | } |
| 2368 | |
Christopher Tate | db27b84 | 2016-02-25 14:39:17 -0800 | [diff] [blame] | 2369 | out.attribute(null, "name", wallpaper.name); |
| 2370 | if (wallpaper.wallpaperComponent != null |
| 2371 | && !wallpaper.wallpaperComponent.equals(mImageWallpaper)) { |
| 2372 | out.attribute(null, "component", |
| 2373 | wallpaper.wallpaperComponent.flattenToShortString()); |
| 2374 | } |
Christopher Tate | d7faf53 | 2016-02-25 12:43:38 -0800 | [diff] [blame] | 2375 | |
| 2376 | if (wallpaper.allowBackup) { |
| 2377 | out.attribute(null, "backup", "true"); |
| 2378 | } |
| 2379 | |
Christopher Tate | db27b84 | 2016-02-25 14:39:17 -0800 | [diff] [blame] | 2380 | out.endTag(null, tag); |
| 2381 | } |
| 2382 | |
Amith Yamasani | 37ce3a8 | 2012-02-06 12:04:42 -0800 | [diff] [blame] | 2383 | private void migrateFromOld() { |
Christopher Tate | 9f22443 | 2017-08-01 16:32:49 -0700 | [diff] [blame] | 2384 | // Pre-N, what existed is the one we're now using as the display crop |
| 2385 | File preNWallpaper = new File(getWallpaperDir(0), WALLPAPER_CROP); |
| 2386 | // In the very-long-ago, imagery lived with the settings app |
| 2387 | File originalWallpaper = new File(WallpaperBackupHelper.WALLPAPER_IMAGE_KEY); |
| 2388 | File newWallpaper = new File(getWallpaperDir(0), WALLPAPER); |
| 2389 | |
| 2390 | // Migrations from earlier wallpaper image storage schemas |
| 2391 | if (preNWallpaper.exists()) { |
| 2392 | if (!newWallpaper.exists()) { |
| 2393 | // we've got the 'wallpaper' crop file but not the nominal source image, |
| 2394 | // so do the simple "just take everything" straight copy of legacy data |
| 2395 | if (DEBUG) { |
| 2396 | Slog.i(TAG, "Migrating wallpaper schema"); |
| 2397 | } |
| 2398 | FileUtils.copyFile(preNWallpaper, newWallpaper); |
| 2399 | } // else we're in the usual modern case: both source & crop exist |
| 2400 | } else if (originalWallpaper.exists()) { |
| 2401 | // VERY old schema; make sure things exist and are in the right place |
| 2402 | if (DEBUG) { |
| 2403 | Slog.i(TAG, "Migrating antique wallpaper schema"); |
| 2404 | } |
| 2405 | File oldInfo = new File(WallpaperBackupHelper.WALLPAPER_INFO_KEY); |
| 2406 | if (oldInfo.exists()) { |
| 2407 | File newInfo = new File(getWallpaperDir(0), WALLPAPER_INFO); |
| 2408 | oldInfo.renameTo(newInfo); |
| 2409 | } |
| 2410 | |
| 2411 | FileUtils.copyFile(originalWallpaper, preNWallpaper); |
| 2412 | originalWallpaper.renameTo(newWallpaper); |
Amith Yamasani | 37ce3a8 | 2012-02-06 12:04:42 -0800 | [diff] [blame] | 2413 | } |
| 2414 | } |
| 2415 | |
Dianne Hackborn | 067e5f6 | 2014-09-07 23:14:30 -0700 | [diff] [blame] | 2416 | private int getAttributeInt(XmlPullParser parser, String name, int defValue) { |
| 2417 | String value = parser.getAttributeValue(null, name); |
| 2418 | if (value == null) { |
| 2419 | return defValue; |
| 2420 | } |
| 2421 | return Integer.parseInt(value); |
| 2422 | } |
| 2423 | |
Xiaohui Chen | ac53194 | 2015-05-13 13:20:52 -0700 | [diff] [blame] | 2424 | /** |
| 2425 | * Sometimes it is expected the wallpaper map may not have a user's data. E.g. This could |
| 2426 | * happen during user switch. The async user switch observer may not have received |
| 2427 | * the event yet. We use this safe method when we don't care about this ordering and just |
| 2428 | * want to update the data. The data is going to be applied when the user switch observer |
| 2429 | * is eventually executed. |
Christopher Tate | f717b93 | 2017-09-11 15:52:54 -0700 | [diff] [blame] | 2430 | * |
| 2431 | * Important: this method loads settings to initialize the given user's wallpaper data if |
| 2432 | * there is no current in-memory state. |
Xiaohui Chen | ac53194 | 2015-05-13 13:20:52 -0700 | [diff] [blame] | 2433 | */ |
Christopher Tate | be132e6 | 2016-02-10 12:59:49 -0800 | [diff] [blame] | 2434 | private WallpaperData getWallpaperSafeLocked(int userId, int which) { |
| 2435 | // We're setting either just system (work with the system wallpaper), |
| 2436 | // both (also work with the system wallpaper), or just the lock |
| 2437 | // wallpaper (update against the existing lock wallpaper if any). |
| 2438 | // Combined or just-system operations use the 'system' WallpaperData |
| 2439 | // for this use; lock-only operations use the dedicated one. |
| 2440 | final SparseArray<WallpaperData> whichSet = |
Christopher Tate | edf7d04 | 2016-03-29 18:24:25 -0700 | [diff] [blame] | 2441 | (which == FLAG_LOCK) ? mLockWallpaperMap : mWallpaperMap; |
Christopher Tate | be132e6 | 2016-02-10 12:59:49 -0800 | [diff] [blame] | 2442 | WallpaperData wallpaper = whichSet.get(userId); |
Xiaohui Chen | ac53194 | 2015-05-13 13:20:52 -0700 | [diff] [blame] | 2443 | if (wallpaper == null) { |
Christopher Tate | be132e6 | 2016-02-10 12:59:49 -0800 | [diff] [blame] | 2444 | // common case, this is the first lookup post-boot of the system or |
| 2445 | // unified lock, so we bring up the saved state lazily now and recheck. |
Christopher Tate | d7faf53 | 2016-02-25 12:43:38 -0800 | [diff] [blame] | 2446 | loadSettingsLocked(userId, false); |
Christopher Tate | be132e6 | 2016-02-10 12:59:49 -0800 | [diff] [blame] | 2447 | wallpaper = whichSet.get(userId); |
| 2448 | // if it's still null here, this is a lock-only operation and there is not |
| 2449 | // yet a lock-only wallpaper set for this user, so we need to establish |
| 2450 | // it now. |
| 2451 | if (wallpaper == null) { |
Christopher Tate | edf7d04 | 2016-03-29 18:24:25 -0700 | [diff] [blame] | 2452 | if (which == FLAG_LOCK) { |
Christopher Tate | be132e6 | 2016-02-10 12:59:49 -0800 | [diff] [blame] | 2453 | wallpaper = new WallpaperData(userId, |
| 2454 | WALLPAPER_LOCK_ORIG, WALLPAPER_LOCK_CROP); |
| 2455 | mLockWallpaperMap.put(userId, wallpaper); |
Adrian Roos | c28e3a9 | 2016-04-14 10:47:52 -0700 | [diff] [blame] | 2456 | ensureSaneWallpaperData(wallpaper); |
Christopher Tate | be132e6 | 2016-02-10 12:59:49 -0800 | [diff] [blame] | 2457 | } else { |
| 2458 | // sanity fallback: we're in bad shape, but establishing a known |
| 2459 | // valid system+lock WallpaperData will keep us from dying. |
| 2460 | Slog.wtf(TAG, "Didn't find wallpaper in non-lock case!"); |
| 2461 | wallpaper = new WallpaperData(userId, WALLPAPER, WALLPAPER_CROP); |
| 2462 | mWallpaperMap.put(userId, wallpaper); |
Adrian Roos | c28e3a9 | 2016-04-14 10:47:52 -0700 | [diff] [blame] | 2463 | ensureSaneWallpaperData(wallpaper); |
Christopher Tate | be132e6 | 2016-02-10 12:59:49 -0800 | [diff] [blame] | 2464 | } |
| 2465 | } |
Xiaohui Chen | ac53194 | 2015-05-13 13:20:52 -0700 | [diff] [blame] | 2466 | } |
| 2467 | return wallpaper; |
| 2468 | } |
| 2469 | |
Christopher Tate | d7faf53 | 2016-02-25 12:43:38 -0800 | [diff] [blame] | 2470 | private void loadSettingsLocked(int userId, boolean keepDimensionHints) { |
Amith Yamasani | 37ce3a8 | 2012-02-06 12:04:42 -0800 | [diff] [blame] | 2471 | JournaledFile journal = makeJournaledFile(userId); |
Joe Onorato | 9bb8fd7 | 2009-07-28 18:24:51 -0700 | [diff] [blame] | 2472 | FileInputStream stream = null; |
| 2473 | File file = journal.chooseForRead(); |
Christopher Tate | 9f22443 | 2017-08-01 16:32:49 -0700 | [diff] [blame] | 2474 | |
Amith Yamasani | 37ce3a8 | 2012-02-06 12:04:42 -0800 | [diff] [blame] | 2475 | WallpaperData wallpaper = mWallpaperMap.get(userId); |
| 2476 | if (wallpaper == null) { |
Christopher Tate | 9f22443 | 2017-08-01 16:32:49 -0700 | [diff] [blame] | 2477 | // Do this once per boot |
| 2478 | migrateFromOld(); |
| 2479 | |
Christopher Tate | be132e6 | 2016-02-10 12:59:49 -0800 | [diff] [blame] | 2480 | wallpaper = new WallpaperData(userId, WALLPAPER, WALLPAPER_CROP); |
Christopher Tate | d7faf53 | 2016-02-25 12:43:38 -0800 | [diff] [blame] | 2481 | wallpaper.allowBackup = true; |
Amith Yamasani | 37ce3a8 | 2012-02-06 12:04:42 -0800 | [diff] [blame] | 2482 | mWallpaperMap.put(userId, wallpaper); |
Christopher Tate | 41297ff | 2016-03-10 16:46:15 -0800 | [diff] [blame] | 2483 | if (!wallpaper.cropExists()) { |
Christopher Tate | 2a6c55f | 2017-03-31 12:28:30 -0700 | [diff] [blame] | 2484 | if (wallpaper.sourceExists()) { |
| 2485 | generateCrop(wallpaper); |
| 2486 | } else { |
| 2487 | Slog.i(TAG, "No static wallpaper imagery; defaults will be shown"); |
| 2488 | } |
Christopher Tate | 41297ff | 2016-03-10 16:46:15 -0800 | [diff] [blame] | 2489 | } |
Amith Yamasani | 37ce3a8 | 2012-02-06 12:04:42 -0800 | [diff] [blame] | 2490 | } |
Joe Onorato | 9bb8fd7 | 2009-07-28 18:24:51 -0700 | [diff] [blame] | 2491 | boolean success = false; |
| 2492 | try { |
| 2493 | stream = new FileInputStream(file); |
| 2494 | XmlPullParser parser = Xml.newPullParser(); |
Wojciech Staszkiewicz | 9e9e2e7 | 2015-05-08 14:58:46 +0100 | [diff] [blame] | 2495 | parser.setInput(stream, StandardCharsets.UTF_8.name()); |
Joe Onorato | 9bb8fd7 | 2009-07-28 18:24:51 -0700 | [diff] [blame] | 2496 | |
| 2497 | int type; |
Joe Onorato | 9bb8fd7 | 2009-07-28 18:24:51 -0700 | [diff] [blame] | 2498 | do { |
| 2499 | type = parser.next(); |
| 2500 | if (type == XmlPullParser.START_TAG) { |
| 2501 | String tag = parser.getName(); |
| 2502 | if ("wp".equals(tag)) { |
Christopher Tate | be132e6 | 2016-02-10 12:59:49 -0800 | [diff] [blame] | 2503 | // Common to system + lock wallpapers |
Christopher Tate | d7faf53 | 2016-02-25 12:43:38 -0800 | [diff] [blame] | 2504 | parseWallpaperAttributes(parser, wallpaper, keepDimensionHints); |
Christopher Tate | ad3c259 | 2016-01-20 18:13:17 -0800 | [diff] [blame] | 2505 | |
Christopher Tate | be132e6 | 2016-02-10 12:59:49 -0800 | [diff] [blame] | 2506 | // A system wallpaper might also be a live wallpaper |
Dianne Hackborn | 4c62fc0 | 2009-08-08 20:40:27 -0700 | [diff] [blame] | 2507 | String comp = parser.getAttributeValue(null, "component"); |
Amith Yamasani | 37ce3a8 | 2012-02-06 12:04:42 -0800 | [diff] [blame] | 2508 | wallpaper.nextWallpaperComponent = comp != null |
Dianne Hackborn | 4c62fc0 | 2009-08-08 20:40:27 -0700 | [diff] [blame] | 2509 | ? ComponentName.unflattenFromString(comp) |
| 2510 | : null; |
Amith Yamasani | 37ce3a8 | 2012-02-06 12:04:42 -0800 | [diff] [blame] | 2511 | if (wallpaper.nextWallpaperComponent == null |
| 2512 | || "android".equals(wallpaper.nextWallpaperComponent |
| 2513 | .getPackageName())) { |
Justin Koh | 29c3016 | 2014-09-05 17:10:10 -0700 | [diff] [blame] | 2514 | wallpaper.nextWallpaperComponent = mImageWallpaper; |
Dianne Hackborn | 9ea3163 | 2011-08-05 14:43:50 -0700 | [diff] [blame] | 2515 | } |
Oleksandr Peletskyi | f251981 | 2016-01-26 20:16:06 +0100 | [diff] [blame] | 2516 | |
Mike Cleron | a428b2c | 2009-11-15 22:53:08 -0800 | [diff] [blame] | 2517 | if (DEBUG) { |
Amith Yamasani | 37ce3a8 | 2012-02-06 12:04:42 -0800 | [diff] [blame] | 2518 | Slog.v(TAG, "mWidth:" + wallpaper.width); |
| 2519 | Slog.v(TAG, "mHeight:" + wallpaper.height); |
Christopher Tate | 1e1e2e0 | 2016-01-25 15:34:36 -0800 | [diff] [blame] | 2520 | Slog.v(TAG, "cropRect:" + wallpaper.cropHint); |
Lucas Dupin | ea1fb1e | 2017-04-05 17:39:44 -0700 | [diff] [blame] | 2521 | Slog.v(TAG, "primaryColors:" + wallpaper.primaryColors); |
Amith Yamasani | 37ce3a8 | 2012-02-06 12:04:42 -0800 | [diff] [blame] | 2522 | Slog.v(TAG, "mName:" + wallpaper.name); |
| 2523 | Slog.v(TAG, "mNextWallpaperComponent:" |
| 2524 | + wallpaper.nextWallpaperComponent); |
Mike Cleron | a428b2c | 2009-11-15 22:53:08 -0800 | [diff] [blame] | 2525 | } |
Christopher Tate | be132e6 | 2016-02-10 12:59:49 -0800 | [diff] [blame] | 2526 | } else if ("kwp".equals(tag)) { |
| 2527 | // keyguard-specific wallpaper for this user |
| 2528 | WallpaperData lockWallpaper = mLockWallpaperMap.get(userId); |
| 2529 | if (lockWallpaper == null) { |
| 2530 | lockWallpaper = new WallpaperData(userId, |
| 2531 | WALLPAPER_LOCK_ORIG, WALLPAPER_LOCK_CROP); |
| 2532 | mLockWallpaperMap.put(userId, lockWallpaper); |
| 2533 | } |
Christopher Tate | d7faf53 | 2016-02-25 12:43:38 -0800 | [diff] [blame] | 2534 | parseWallpaperAttributes(parser, lockWallpaper, false); |
Joe Onorato | 9bb8fd7 | 2009-07-28 18:24:51 -0700 | [diff] [blame] | 2535 | } |
| 2536 | } |
| 2537 | } while (type != XmlPullParser.END_DOCUMENT); |
| 2538 | success = true; |
Dianne Hackborn | 13579ed | 2012-11-28 18:05:36 -0800 | [diff] [blame] | 2539 | } catch (FileNotFoundException e) { |
| 2540 | Slog.w(TAG, "no current wallpaper -- first boot?"); |
Joe Onorato | 9bb8fd7 | 2009-07-28 18:24:51 -0700 | [diff] [blame] | 2541 | } catch (NullPointerException e) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 2542 | Slog.w(TAG, "failed parsing " + file + " " + e); |
Joe Onorato | 9bb8fd7 | 2009-07-28 18:24:51 -0700 | [diff] [blame] | 2543 | } catch (NumberFormatException e) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 2544 | Slog.w(TAG, "failed parsing " + file + " " + e); |
Joe Onorato | 9bb8fd7 | 2009-07-28 18:24:51 -0700 | [diff] [blame] | 2545 | } catch (XmlPullParserException e) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 2546 | Slog.w(TAG, "failed parsing " + file + " " + e); |
Joe Onorato | 9bb8fd7 | 2009-07-28 18:24:51 -0700 | [diff] [blame] | 2547 | } catch (IOException e) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 2548 | Slog.w(TAG, "failed parsing " + file + " " + e); |
Joe Onorato | 9bb8fd7 | 2009-07-28 18:24:51 -0700 | [diff] [blame] | 2549 | } catch (IndexOutOfBoundsException e) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 2550 | Slog.w(TAG, "failed parsing " + file + " " + e); |
Joe Onorato | 9bb8fd7 | 2009-07-28 18:24:51 -0700 | [diff] [blame] | 2551 | } |
Christopher Tate | ad3c259 | 2016-01-20 18:13:17 -0800 | [diff] [blame] | 2552 | IoUtils.closeQuietly(stream); |
Joe Onorato | 9bb8fd7 | 2009-07-28 18:24:51 -0700 | [diff] [blame] | 2553 | |
| 2554 | if (!success) { |
Amith Yamasani | 37ce3a8 | 2012-02-06 12:04:42 -0800 | [diff] [blame] | 2555 | wallpaper.width = -1; |
| 2556 | wallpaper.height = -1; |
Christopher Tate | 1e1e2e0 | 2016-01-25 15:34:36 -0800 | [diff] [blame] | 2557 | wallpaper.cropHint.set(0, 0, 0, 0); |
Dianne Hackborn | 067e5f6 | 2014-09-07 23:14:30 -0700 | [diff] [blame] | 2558 | wallpaper.padding.set(0, 0, 0, 0); |
Amith Yamasani | 37ce3a8 | 2012-02-06 12:04:42 -0800 | [diff] [blame] | 2559 | wallpaper.name = ""; |
Adrian Roos | c28e3a9 | 2016-04-14 10:47:52 -0700 | [diff] [blame] | 2560 | |
| 2561 | mLockWallpaperMap.remove(userId); |
Christopher Tate | ad3c259 | 2016-01-20 18:13:17 -0800 | [diff] [blame] | 2562 | } else { |
| 2563 | if (wallpaper.wallpaperId <= 0) { |
| 2564 | wallpaper.wallpaperId = makeWallpaperIdLocked(); |
| 2565 | if (DEBUG) { |
| 2566 | Slog.w(TAG, "Didn't set wallpaper id in loadSettingsLocked(" + userId |
| 2567 | + "); now " + wallpaper.wallpaperId); |
| 2568 | } |
| 2569 | } |
Joe Onorato | 9bb8fd7 | 2009-07-28 18:24:51 -0700 | [diff] [blame] | 2570 | } |
Dianne Hackborn | 44bc17c | 2011-04-20 18:18:51 -0700 | [diff] [blame] | 2571 | |
Adrian Roos | c28e3a9 | 2016-04-14 10:47:52 -0700 | [diff] [blame] | 2572 | ensureSaneWallpaperData(wallpaper); |
| 2573 | WallpaperData lockWallpaper = mLockWallpaperMap.get(userId); |
| 2574 | if (lockWallpaper != null) { |
| 2575 | ensureSaneWallpaperData(lockWallpaper); |
| 2576 | } |
| 2577 | } |
| 2578 | |
| 2579 | private void ensureSaneWallpaperData(WallpaperData wallpaper) { |
Dianne Hackborn | 44bc17c | 2011-04-20 18:18:51 -0700 | [diff] [blame] | 2580 | // We always want to have some reasonable width hint. |
John Spurlock | 7ea91ec | 2013-11-04 13:48:38 -0500 | [diff] [blame] | 2581 | int baseSize = getMaximumSizeDimension(); |
Amith Yamasani | 37ce3a8 | 2012-02-06 12:04:42 -0800 | [diff] [blame] | 2582 | if (wallpaper.width < baseSize) { |
| 2583 | wallpaper.width = baseSize; |
Dianne Hackborn | 44bc17c | 2011-04-20 18:18:51 -0700 | [diff] [blame] | 2584 | } |
Amith Yamasani | 37ce3a8 | 2012-02-06 12:04:42 -0800 | [diff] [blame] | 2585 | if (wallpaper.height < baseSize) { |
| 2586 | wallpaper.height = baseSize; |
Dianne Hackborn | 44bc17c | 2011-04-20 18:18:51 -0700 | [diff] [blame] | 2587 | } |
Christopher Tate | 1e1e2e0 | 2016-01-25 15:34:36 -0800 | [diff] [blame] | 2588 | // and crop, if not previously specified |
| 2589 | if (wallpaper.cropHint.width() <= 0 |
| 2590 | || wallpaper.cropHint.height() <= 0) { |
| 2591 | wallpaper.cropHint.set(0, 0, wallpaper.width, wallpaper.height); |
| 2592 | } |
Joe Onorato | 9bb8fd7 | 2009-07-28 18:24:51 -0700 | [diff] [blame] | 2593 | } |
| 2594 | |
Christopher Tate | d7faf53 | 2016-02-25 12:43:38 -0800 | [diff] [blame] | 2595 | private void parseWallpaperAttributes(XmlPullParser parser, WallpaperData wallpaper, |
| 2596 | boolean keepDimensionHints) { |
Christopher Tate | be132e6 | 2016-02-10 12:59:49 -0800 | [diff] [blame] | 2597 | final String idString = parser.getAttributeValue(null, "id"); |
| 2598 | if (idString != null) { |
| 2599 | final int id = wallpaper.wallpaperId = Integer.parseInt(idString); |
| 2600 | if (id > mWallpaperId) { |
| 2601 | mWallpaperId = id; |
| 2602 | } |
| 2603 | } else { |
| 2604 | wallpaper.wallpaperId = makeWallpaperIdLocked(); |
| 2605 | } |
| 2606 | |
Christopher Tate | d7faf53 | 2016-02-25 12:43:38 -0800 | [diff] [blame] | 2607 | if (!keepDimensionHints) { |
| 2608 | wallpaper.width = Integer.parseInt(parser.getAttributeValue(null, "width")); |
| 2609 | wallpaper.height = Integer.parseInt(parser |
| 2610 | .getAttributeValue(null, "height")); |
| 2611 | } |
Christopher Tate | be132e6 | 2016-02-10 12:59:49 -0800 | [diff] [blame] | 2612 | wallpaper.cropHint.left = getAttributeInt(parser, "cropLeft", 0); |
| 2613 | wallpaper.cropHint.top = getAttributeInt(parser, "cropTop", 0); |
| 2614 | wallpaper.cropHint.right = getAttributeInt(parser, "cropRight", 0); |
| 2615 | wallpaper.cropHint.bottom = getAttributeInt(parser, "cropBottom", 0); |
| 2616 | wallpaper.padding.left = getAttributeInt(parser, "paddingLeft", 0); |
| 2617 | wallpaper.padding.top = getAttributeInt(parser, "paddingTop", 0); |
| 2618 | wallpaper.padding.right = getAttributeInt(parser, "paddingRight", 0); |
| 2619 | wallpaper.padding.bottom = getAttributeInt(parser, "paddingBottom", 0); |
Lucas Dupin | ea1fb1e | 2017-04-05 17:39:44 -0700 | [diff] [blame] | 2620 | int colorsCount = getAttributeInt(parser, "colorsCount", 0); |
| 2621 | if (colorsCount > 0) { |
Lucas Dupin | 84b89d9 | 2017-05-09 12:16:19 -0700 | [diff] [blame] | 2622 | Color primary = null, secondary = null, tertiary = null; |
Lucas Dupin | ea1fb1e | 2017-04-05 17:39:44 -0700 | [diff] [blame] | 2623 | for (int i = 0; i < colorsCount; i++) { |
Lucas Dupin | 84b89d9 | 2017-05-09 12:16:19 -0700 | [diff] [blame] | 2624 | Color color = Color.valueOf(getAttributeInt(parser, "colorValue" + i, 0)); |
| 2625 | if (i == 0) { |
| 2626 | primary = color; |
| 2627 | } else if (i == 1) { |
| 2628 | secondary = color; |
| 2629 | } else if (i == 2) { |
| 2630 | tertiary = color; |
| 2631 | } else { |
| 2632 | break; |
| 2633 | } |
Lucas Dupin | ea1fb1e | 2017-04-05 17:39:44 -0700 | [diff] [blame] | 2634 | } |
Lucas Dupin | 84b89d9 | 2017-05-09 12:16:19 -0700 | [diff] [blame] | 2635 | int colorHints = getAttributeInt(parser, "colorHints", 0); |
| 2636 | wallpaper.primaryColors = new WallpaperColors(primary, secondary, tertiary, colorHints); |
Lucas Dupin | ea1fb1e | 2017-04-05 17:39:44 -0700 | [diff] [blame] | 2637 | } |
Christopher Tate | be132e6 | 2016-02-10 12:59:49 -0800 | [diff] [blame] | 2638 | wallpaper.name = parser.getAttributeValue(null, "name"); |
Christopher Tate | d7faf53 | 2016-02-25 12:43:38 -0800 | [diff] [blame] | 2639 | wallpaper.allowBackup = "true".equals(parser.getAttributeValue(null, "backup")); |
Christopher Tate | be132e6 | 2016-02-10 12:59:49 -0800 | [diff] [blame] | 2640 | } |
| 2641 | |
John Spurlock | 7ea91ec | 2013-11-04 13:48:38 -0500 | [diff] [blame] | 2642 | private int getMaximumSizeDimension() { |
| 2643 | WindowManager wm = (WindowManager)mContext.getSystemService(Context.WINDOW_SERVICE); |
| 2644 | Display d = wm.getDefaultDisplay(); |
| 2645 | return d.getMaximumSizeDimension(); |
| 2646 | } |
| 2647 | |
Brad Fitzpatrick | 194b19a | 2010-09-14 11:30:29 -0700 | [diff] [blame] | 2648 | // Called by SystemBackupAgent after files are restored to disk. |
Amith Yamasani | 09e9cdc | 2013-11-06 14:54:50 -0800 | [diff] [blame] | 2649 | public void settingsRestored() { |
| 2650 | // Verify caller is the system |
| 2651 | if (Binder.getCallingUid() != android.os.Process.SYSTEM_UID) { |
| 2652 | throw new RuntimeException("settingsRestored() can only be called from the system process"); |
| 2653 | } |
Amith Yamasani | 37ce3a8 | 2012-02-06 12:04:42 -0800 | [diff] [blame] | 2654 | // TODO: If necessary, make it work for secondary users as well. This currently assumes |
| 2655 | // restores only to the primary user |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 2656 | if (DEBUG) Slog.v(TAG, "settingsRestored"); |
Amith Yamasani | 37ce3a8 | 2012-02-06 12:04:42 -0800 | [diff] [blame] | 2657 | WallpaperData wallpaper = null; |
Joe Onorato | 9bb8fd7 | 2009-07-28 18:24:51 -0700 | [diff] [blame] | 2658 | boolean success = false; |
| 2659 | synchronized (mLock) { |
Christopher Tate | d7faf53 | 2016-02-25 12:43:38 -0800 | [diff] [blame] | 2660 | loadSettingsLocked(UserHandle.USER_SYSTEM, false); |
Christopher Tate | db27b84 | 2016-02-25 14:39:17 -0800 | [diff] [blame] | 2661 | wallpaper = mWallpaperMap.get(UserHandle.USER_SYSTEM); |
Christopher Tate | ad3c259 | 2016-01-20 18:13:17 -0800 | [diff] [blame] | 2662 | wallpaper.wallpaperId = makeWallpaperIdLocked(); // always bump id at restore |
Christopher Tate | d7faf53 | 2016-02-25 12:43:38 -0800 | [diff] [blame] | 2663 | wallpaper.allowBackup = true; // by definition if it was restored |
Amith Yamasani | 37ce3a8 | 2012-02-06 12:04:42 -0800 | [diff] [blame] | 2664 | if (wallpaper.nextWallpaperComponent != null |
Justin Koh | 29c3016 | 2014-09-05 17:10:10 -0700 | [diff] [blame] | 2665 | && !wallpaper.nextWallpaperComponent.equals(mImageWallpaper)) { |
Amith Yamasani | 37ce3a8 | 2012-02-06 12:04:42 -0800 | [diff] [blame] | 2666 | if (!bindWallpaperComponentLocked(wallpaper.nextWallpaperComponent, false, false, |
Dianne Hackborn | 5dc5a00 | 2012-09-15 19:33:48 -0700 | [diff] [blame] | 2667 | wallpaper, null)) { |
Christopher Tate | e3ab4d0 | 2009-12-16 14:03:31 -0800 | [diff] [blame] | 2668 | // No such live wallpaper or other failure; fall back to the default |
| 2669 | // live wallpaper (since the profile being restored indicated that the |
| 2670 | // user had selected a live rather than static one). |
Dianne Hackborn | 5dc5a00 | 2012-09-15 19:33:48 -0700 | [diff] [blame] | 2671 | bindWallpaperComponentLocked(null, false, false, wallpaper, null); |
Christopher Tate | e3ab4d0 | 2009-12-16 14:03:31 -0800 | [diff] [blame] | 2672 | } |
Joe Onorato | 9bb8fd7 | 2009-07-28 18:24:51 -0700 | [diff] [blame] | 2673 | success = true; |
| 2674 | } else { |
Mike Cleron | a428b2c | 2009-11-15 22:53:08 -0800 | [diff] [blame] | 2675 | // If there's a wallpaper name, we use that. If that can't be loaded, then we |
| 2676 | // use the default. |
Amith Yamasani | 37ce3a8 | 2012-02-06 12:04:42 -0800 | [diff] [blame] | 2677 | if ("".equals(wallpaper.name)) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 2678 | if (DEBUG) Slog.v(TAG, "settingsRestored: name is empty"); |
Mike Cleron | a428b2c | 2009-11-15 22:53:08 -0800 | [diff] [blame] | 2679 | success = true; |
| 2680 | } else { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 2681 | if (DEBUG) Slog.v(TAG, "settingsRestored: attempting to restore named resource"); |
Amith Yamasani | 37ce3a8 | 2012-02-06 12:04:42 -0800 | [diff] [blame] | 2682 | success = restoreNamedResourceLocked(wallpaper); |
Mike Cleron | a428b2c | 2009-11-15 22:53:08 -0800 | [diff] [blame] | 2683 | } |
Christopher Tate | ad3c259 | 2016-01-20 18:13:17 -0800 | [diff] [blame] | 2684 | if (DEBUG) Slog.v(TAG, "settingsRestored: success=" + success |
| 2685 | + " id=" + wallpaper.wallpaperId); |
Mike Cleron | a428b2c | 2009-11-15 22:53:08 -0800 | [diff] [blame] | 2686 | if (success) { |
Christopher Tate | 41297ff | 2016-03-10 16:46:15 -0800 | [diff] [blame] | 2687 | generateCrop(wallpaper); // based on the new image + metadata |
| 2688 | bindWallpaperComponentLocked(wallpaper.nextWallpaperComponent, true, false, |
Dianne Hackborn | 5dc5a00 | 2012-09-15 19:33:48 -0700 | [diff] [blame] | 2689 | wallpaper, null); |
Mike Cleron | a428b2c | 2009-11-15 22:53:08 -0800 | [diff] [blame] | 2690 | } |
Joe Onorato | 9bb8fd7 | 2009-07-28 18:24:51 -0700 | [diff] [blame] | 2691 | } |
| 2692 | } |
| 2693 | |
| 2694 | if (!success) { |
Amith Yamasani | 37ce3a8 | 2012-02-06 12:04:42 -0800 | [diff] [blame] | 2695 | Slog.e(TAG, "Failed to restore wallpaper: '" + wallpaper.name + "'"); |
| 2696 | wallpaper.name = ""; |
Christopher Tate | db27b84 | 2016-02-25 14:39:17 -0800 | [diff] [blame] | 2697 | getWallpaperDir(UserHandle.USER_SYSTEM).delete(); |
Joe Onorato | 9bb8fd7 | 2009-07-28 18:24:51 -0700 | [diff] [blame] | 2698 | } |
Brad Fitzpatrick | 194b19a | 2010-09-14 11:30:29 -0700 | [diff] [blame] | 2699 | |
| 2700 | synchronized (mLock) { |
Christopher Tate | db27b84 | 2016-02-25 14:39:17 -0800 | [diff] [blame] | 2701 | saveSettingsLocked(UserHandle.USER_SYSTEM); |
Brad Fitzpatrick | 194b19a | 2010-09-14 11:30:29 -0700 | [diff] [blame] | 2702 | } |
Joe Onorato | 9bb8fd7 | 2009-07-28 18:24:51 -0700 | [diff] [blame] | 2703 | } |
| 2704 | |
Christopher Tate | 1e1e2e0 | 2016-01-25 15:34:36 -0800 | [diff] [blame] | 2705 | // Restore the named resource bitmap to both source + crop files |
Amith Yamasani | 37ce3a8 | 2012-02-06 12:04:42 -0800 | [diff] [blame] | 2706 | boolean restoreNamedResourceLocked(WallpaperData wallpaper) { |
| 2707 | if (wallpaper.name.length() > 4 && "res:".equals(wallpaper.name.substring(0, 4))) { |
| 2708 | String resName = wallpaper.name.substring(4); |
Joe Onorato | 9bb8fd7 | 2009-07-28 18:24:51 -0700 | [diff] [blame] | 2709 | |
| 2710 | String pkg = null; |
| 2711 | int colon = resName.indexOf(':'); |
| 2712 | if (colon > 0) { |
| 2713 | pkg = resName.substring(0, colon); |
| 2714 | } |
| 2715 | |
| 2716 | String ident = null; |
| 2717 | int slash = resName.lastIndexOf('/'); |
| 2718 | if (slash > 0) { |
| 2719 | ident = resName.substring(slash+1); |
| 2720 | } |
| 2721 | |
| 2722 | String type = null; |
| 2723 | if (colon > 0 && slash > 0 && (slash-colon) > 1) { |
| 2724 | type = resName.substring(colon+1, slash); |
| 2725 | } |
| 2726 | |
| 2727 | if (pkg != null && ident != null && type != null) { |
| 2728 | int resId = -1; |
| 2729 | InputStream res = null; |
| 2730 | FileOutputStream fos = null; |
Christopher Tate | 1e1e2e0 | 2016-01-25 15:34:36 -0800 | [diff] [blame] | 2731 | FileOutputStream cos = null; |
Joe Onorato | 9bb8fd7 | 2009-07-28 18:24:51 -0700 | [diff] [blame] | 2732 | try { |
| 2733 | Context c = mContext.createPackageContext(pkg, Context.CONTEXT_RESTRICTED); |
| 2734 | Resources r = c.getResources(); |
| 2735 | resId = r.getIdentifier(resName, null, null); |
| 2736 | if (resId == 0) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 2737 | Slog.e(TAG, "couldn't resolve identifier pkg=" + pkg + " type=" + type |
Joe Onorato | 9bb8fd7 | 2009-07-28 18:24:51 -0700 | [diff] [blame] | 2738 | + " ident=" + ident); |
| 2739 | return false; |
| 2740 | } |
| 2741 | |
| 2742 | res = r.openRawResource(resId); |
Amith Yamasani | 37ce3a8 | 2012-02-06 12:04:42 -0800 | [diff] [blame] | 2743 | if (wallpaper.wallpaperFile.exists()) { |
| 2744 | wallpaper.wallpaperFile.delete(); |
Christopher Tate | 1e1e2e0 | 2016-01-25 15:34:36 -0800 | [diff] [blame] | 2745 | wallpaper.cropFile.delete(); |
Dianne Hackborn | 1afd1c9 | 2010-03-18 22:47:17 -0700 | [diff] [blame] | 2746 | } |
Amith Yamasani | 37ce3a8 | 2012-02-06 12:04:42 -0800 | [diff] [blame] | 2747 | fos = new FileOutputStream(wallpaper.wallpaperFile); |
Christopher Tate | 1e1e2e0 | 2016-01-25 15:34:36 -0800 | [diff] [blame] | 2748 | cos = new FileOutputStream(wallpaper.cropFile); |
Joe Onorato | 9bb8fd7 | 2009-07-28 18:24:51 -0700 | [diff] [blame] | 2749 | |
| 2750 | byte[] buffer = new byte[32768]; |
| 2751 | int amt; |
| 2752 | while ((amt=res.read(buffer)) > 0) { |
| 2753 | fos.write(buffer, 0, amt); |
Christopher Tate | 1e1e2e0 | 2016-01-25 15:34:36 -0800 | [diff] [blame] | 2754 | cos.write(buffer, 0, amt); |
Joe Onorato | 9bb8fd7 | 2009-07-28 18:24:51 -0700 | [diff] [blame] | 2755 | } |
| 2756 | // mWallpaperObserver will notice the close and send the change broadcast |
| 2757 | |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 2758 | Slog.v(TAG, "Restored wallpaper: " + resName); |
Joe Onorato | 9bb8fd7 | 2009-07-28 18:24:51 -0700 | [diff] [blame] | 2759 | return true; |
| 2760 | } catch (NameNotFoundException e) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 2761 | Slog.e(TAG, "Package name " + pkg + " not found"); |
Joe Onorato | 9bb8fd7 | 2009-07-28 18:24:51 -0700 | [diff] [blame] | 2762 | } catch (Resources.NotFoundException e) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 2763 | Slog.e(TAG, "Resource not found: " + resId); |
Joe Onorato | 9bb8fd7 | 2009-07-28 18:24:51 -0700 | [diff] [blame] | 2764 | } catch (IOException e) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 2765 | Slog.e(TAG, "IOException while restoring wallpaper ", e); |
Joe Onorato | 9bb8fd7 | 2009-07-28 18:24:51 -0700 | [diff] [blame] | 2766 | } finally { |
Christopher Tate | ad3c259 | 2016-01-20 18:13:17 -0800 | [diff] [blame] | 2767 | IoUtils.closeQuietly(res); |
Joe Onorato | 9bb8fd7 | 2009-07-28 18:24:51 -0700 | [diff] [blame] | 2768 | if (fos != null) { |
Dianne Hackborn | 8bdf593 | 2010-10-15 12:54:40 -0700 | [diff] [blame] | 2769 | FileUtils.sync(fos); |
Joe Onorato | 9bb8fd7 | 2009-07-28 18:24:51 -0700 | [diff] [blame] | 2770 | } |
Christopher Tate | 1e1e2e0 | 2016-01-25 15:34:36 -0800 | [diff] [blame] | 2771 | if (cos != null) { |
| 2772 | FileUtils.sync(cos); |
| 2773 | } |
| 2774 | IoUtils.closeQuietly(fos); |
| 2775 | IoUtils.closeQuietly(cos); |
Joe Onorato | 9bb8fd7 | 2009-07-28 18:24:51 -0700 | [diff] [blame] | 2776 | } |
| 2777 | } |
| 2778 | } |
| 2779 | return false; |
| 2780 | } |
Amith Yamasani | 37ce3a8 | 2012-02-06 12:04:42 -0800 | [diff] [blame] | 2781 | |
Dianne Hackborn | eb03465 | 2009-09-07 00:49:58 -0700 | [diff] [blame] | 2782 | @Override |
| 2783 | protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) { |
Jeff Sharkey | fe9a53b | 2017-03-31 14:08:23 -0600 | [diff] [blame] | 2784 | if (!DumpUtils.checkDumpPermission(mContext, TAG, pw)) return; |
Dianne Hackborn | eb03465 | 2009-09-07 00:49:58 -0700 | [diff] [blame] | 2785 | |
| 2786 | synchronized (mLock) { |
Adrian Roos | c28e3a9 | 2016-04-14 10:47:52 -0700 | [diff] [blame] | 2787 | pw.println("System wallpaper state:"); |
Amith Yamasani | 37ce3a8 | 2012-02-06 12:04:42 -0800 | [diff] [blame] | 2788 | for (int i = 0; i < mWallpaperMap.size(); i++) { |
| 2789 | WallpaperData wallpaper = mWallpaperMap.valueAt(i); |
Christopher Tate | ad3c259 | 2016-01-20 18:13:17 -0800 | [diff] [blame] | 2790 | pw.print(" User "); pw.print(wallpaper.userId); |
Adrian Roos | c28e3a9 | 2016-04-14 10:47:52 -0700 | [diff] [blame] | 2791 | pw.print(": id="); pw.println(wallpaper.wallpaperId); |
Amith Yamasani | 37ce3a8 | 2012-02-06 12:04:42 -0800 | [diff] [blame] | 2792 | pw.print(" mWidth="); |
Dianne Hackborn | 067e5f6 | 2014-09-07 23:14:30 -0700 | [diff] [blame] | 2793 | pw.print(wallpaper.width); |
| 2794 | pw.print(" mHeight="); |
| 2795 | pw.println(wallpaper.height); |
Christopher Tate | 1e1e2e0 | 2016-01-25 15:34:36 -0800 | [diff] [blame] | 2796 | pw.print(" mCropHint="); pw.println(wallpaper.cropHint); |
Dianne Hackborn | 067e5f6 | 2014-09-07 23:14:30 -0700 | [diff] [blame] | 2797 | pw.print(" mPadding="); pw.println(wallpaper.padding); |
| 2798 | pw.print(" mName="); pw.println(wallpaper.name); |
Bryan Mawhinney | 0fa54f4 | 2017-07-06 17:09:37 +0100 | [diff] [blame] | 2799 | pw.print(" mAllowBackup="); pw.println(wallpaper.allowBackup); |
Dianne Hackborn | 067e5f6 | 2014-09-07 23:14:30 -0700 | [diff] [blame] | 2800 | pw.print(" mWallpaperComponent="); pw.println(wallpaper.wallpaperComponent); |
Amith Yamasani | 37ce3a8 | 2012-02-06 12:04:42 -0800 | [diff] [blame] | 2801 | if (wallpaper.connection != null) { |
| 2802 | WallpaperConnection conn = wallpaper.connection; |
| 2803 | pw.print(" Wallpaper connection "); |
| 2804 | pw.print(conn); |
| 2805 | pw.println(":"); |
| 2806 | if (conn.mInfo != null) { |
| 2807 | pw.print(" mInfo.component="); |
| 2808 | pw.println(conn.mInfo.getComponent()); |
| 2809 | } |
| 2810 | pw.print(" mToken="); |
| 2811 | pw.println(conn.mToken); |
| 2812 | pw.print(" mService="); |
| 2813 | pw.println(conn.mService); |
| 2814 | pw.print(" mEngine="); |
| 2815 | pw.println(conn.mEngine); |
| 2816 | pw.print(" mLastDiedTime="); |
| 2817 | pw.println(wallpaper.lastDiedTime - SystemClock.uptimeMillis()); |
| 2818 | } |
Dianne Hackborn | eb03465 | 2009-09-07 00:49:58 -0700 | [diff] [blame] | 2819 | } |
Adrian Roos | c28e3a9 | 2016-04-14 10:47:52 -0700 | [diff] [blame] | 2820 | pw.println("Lock wallpaper state:"); |
| 2821 | for (int i = 0; i < mLockWallpaperMap.size(); i++) { |
| 2822 | WallpaperData wallpaper = mLockWallpaperMap.valueAt(i); |
| 2823 | pw.print(" User "); pw.print(wallpaper.userId); |
| 2824 | pw.print(": id="); pw.println(wallpaper.wallpaperId); |
| 2825 | pw.print(" mWidth="); pw.print(wallpaper.width); |
| 2826 | pw.print(" mHeight="); pw.println(wallpaper.height); |
| 2827 | pw.print(" mCropHint="); pw.println(wallpaper.cropHint); |
| 2828 | pw.print(" mPadding="); pw.println(wallpaper.padding); |
| 2829 | pw.print(" mName="); pw.println(wallpaper.name); |
Bryan Mawhinney | 0fa54f4 | 2017-07-06 17:09:37 +0100 | [diff] [blame] | 2830 | pw.print(" mAllowBackup="); pw.println(wallpaper.allowBackup); |
Adrian Roos | c28e3a9 | 2016-04-14 10:47:52 -0700 | [diff] [blame] | 2831 | } |
| 2832 | |
Dianne Hackborn | eb03465 | 2009-09-07 00:49:58 -0700 | [diff] [blame] | 2833 | } |
| 2834 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2835 | } |