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