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