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