blob: 63952b086c1c8ac82ec99cf7a8c7b709691febd8 [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001/*
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 Yamasani09e9cdc2013-11-06 14:54:50 -080017package com.android.server.wallpaper;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080018
Santiago Etchebehere85ef2fd2020-03-20 15:05:44 -070019import static android.app.WallpaperManager.COMMAND_REAPPLY;
Christopher Tateedf7d042016-03-29 18:24:25 -070020import static android.app.WallpaperManager.FLAG_LOCK;
Jeff Sharkey1cab76a2016-04-12 18:23:31 -060021import static android.app.WallpaperManager.FLAG_SYSTEM;
wilsonshihde93f492018-11-01 21:23:40 +080022import static android.content.pm.PackageManager.MATCH_DIRECT_BOOT_AUTO;
Jeff Sharkey1cab76a2016-04-12 18:23:31 -060023import static android.os.ParcelFileDescriptor.MODE_CREATE;
24import static android.os.ParcelFileDescriptor.MODE_READ_ONLY;
25import static android.os.ParcelFileDescriptor.MODE_READ_WRITE;
26import static android.os.ParcelFileDescriptor.MODE_TRUNCATE;
Wale Ogunwaleac2561e2016-11-01 15:43:46 -070027import static android.view.Display.DEFAULT_DISPLAY;
28import static android.view.WindowManager.LayoutParams.TYPE_WALLPAPER;
Christopher Tate111bd4a2009-06-24 17:29:38 -070029
Lucas Dupin50ba9912017-07-14 11:55:05 -070030import android.annotation.NonNull;
Christopher Tatee409f0e2016-03-21 14:53:15 -070031import android.app.ActivityManager;
Amith Yamasani4e2820c2012-08-28 22:17:23 -070032import android.app.AppGlobals;
Benjamin Franzf3ece362015-02-11 10:51:10 +000033import android.app.AppOpsManager;
Dianne Hackborn8cc6a502009-08-05 21:29:42 -070034import android.app.IWallpaperManager;
35import android.app.IWallpaperManagerCallback;
Dianne Hackborndd9b82c2009-09-03 00:18:47 -070036import android.app.PendingIntent;
Sudheer Shanka2c4522c2016-08-27 20:53:28 -070037import android.app.UserSwitchObserver;
Lucas Dupin75ec3792017-06-29 14:07:18 -070038import android.app.WallpaperColors;
Dianne Hackborneb034652009-09-07 00:49:58 -070039import android.app.WallpaperInfo;
Jeff Sharkey28f08772014-04-16 09:41:58 -070040import android.app.WallpaperManager;
wilsonshih999ec102019-05-17 18:47:50 +080041import android.app.WallpaperManager.SetWallpaperFlags;
Oleksandr Peletskyif2519812016-01-26 20:16:06 +010042import android.app.admin.DevicePolicyManager;
Amith Yamasani37ce3a82012-02-06 12:04:42 -080043import android.app.backup.WallpaperBackupHelper;
Amith Yamasani13593602012-03-22 16:16:17 -070044import android.content.BroadcastReceiver;
Dianne Hackborn4c62fc02009-08-08 20:40:27 -070045import android.content.ComponentName;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080046import android.content.Context;
47import android.content.Intent;
Amith Yamasani13593602012-03-22 16:16:17 -070048import android.content.IntentFilter;
Dianne Hackborn4c62fc02009-08-08 20:40:27 -070049import android.content.ServiceConnection;
Amith Yamasani4e2820c2012-08-28 22:17:23 -070050import android.content.pm.IPackageManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080051import android.content.pm.PackageManager;
Jeff Sharkey1cab76a2016-04-12 18:23:31 -060052import android.content.pm.PackageManager.NameNotFoundException;
Dianne Hackborn4c62fc02009-08-08 20:40:27 -070053import android.content.pm.ResolveInfo;
54import android.content.pm.ServiceInfo;
Amith Yamasani6474c4c2012-10-04 14:55:42 -070055import android.content.pm.UserInfo;
Joe Onorato9bb8fd72009-07-28 18:24:51 -070056import android.content.res.Resources;
Christopher Tate1e1e2e02016-01-25 15:34:36 -080057import android.graphics.Bitmap;
58import android.graphics.BitmapFactory;
59import android.graphics.BitmapRegionDecoder;
Lucas Dupinea1fb1e2017-04-05 17:39:44 -070060import android.graphics.Color;
Dianne Hackborn067e5f62014-09-07 23:14:30 -070061import android.graphics.Rect;
wilsonshihde93f492018-11-01 21:23:40 +080062import android.hardware.display.DisplayManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080063import android.os.Binder;
Dianne Hackborn284ac932009-08-28 10:34:25 -070064import android.os.Bundle;
wilsonshiha282bf72018-11-30 12:48:05 +080065import android.os.Debug;
Amith Yamasani13593602012-03-22 16:16:17 -070066import android.os.Environment;
Jeff Sharkey1cab76a2016-04-12 18:23:31 -060067import android.os.FileObserver;
Dianne Hackborn8bdf5932010-10-15 12:54:40 -070068import android.os.FileUtils;
Christopher Tatec349e59f2017-05-05 17:37:43 -070069import android.os.Handler;
Dianne Hackborn4c62fc02009-08-08 20:40:27 -070070import android.os.IBinder;
Lucas Dupin50ba9912017-07-14 11:55:05 -070071import android.os.IInterface;
Dianne Hackborn5dc5a002012-09-15 19:33:48 -070072import android.os.IRemoteCallback;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080073import android.os.ParcelFileDescriptor;
Lucas Dupin75ec3792017-06-29 14:07:18 -070074import android.os.Process;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080075import android.os.RemoteCallbackList;
Jeff Sharkey1cab76a2016-04-12 18:23:31 -060076import android.os.RemoteException;
rpcraig554cb0c2012-07-05 06:41:43 -040077import android.os.SELinux;
Dianne Hackborn0cd48872009-08-13 18:51:59 -070078import android.os.SystemClock;
Dianne Hackbornf02b60a2012-08-16 10:48:27 -070079import android.os.UserHandle;
Amith Yamasani6474c4c2012-10-04 14:55:42 -070080import android.os.UserManager;
Bookatzcfc5d192019-11-05 17:25:53 -080081import android.os.UserManagerInternal;
Jeff Sharkey859856d2019-03-25 11:44:11 -060082import android.os.storage.StorageManager;
Dianne Hackborn4c62fc02009-08-08 20:40:27 -070083import android.service.wallpaper.IWallpaperConnection;
84import android.service.wallpaper.IWallpaperEngine;
85import android.service.wallpaper.IWallpaperService;
86import android.service.wallpaper.WallpaperService;
Christopher Tate8347b632016-04-29 18:59:18 -070087import android.system.ErrnoException;
88import android.system.Os;
Filip Gruszczynski5dcc3ac2014-10-13 15:51:39 -070089import android.util.EventLog;
Joe Onorato8a9b2202010-02-26 18:56:32 -080090import android.util.Slog;
Amith Yamasani37ce3a82012-02-06 12:04:42 -080091import android.util.SparseArray;
wilsonshiha282bf72018-11-30 12:48:05 +080092import android.util.SparseBooleanArray;
Joe Onorato9bb8fd72009-07-28 18:24:51 -070093import android.util.Xml;
Dianne Hackborn44bc17c2011-04-20 18:18:51 -070094import android.view.Display;
Valentin Iftime3047bb12018-12-28 17:02:19 +010095import android.view.DisplayInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080096
Jeff Sharkey1cab76a2016-04-12 18:23:31 -060097import com.android.internal.R;
wilsonshih999ec102019-05-17 18:47:50 +080098import com.android.internal.annotations.VisibleForTesting;
Jeff Sharkey1cab76a2016-04-12 18:23:31 -060099import com.android.internal.content.PackageMonitor;
Christopher Tate190e8532016-07-11 11:35:34 -0700100import com.android.internal.os.BackgroundThread;
Jeff Sharkeyfe9a53b2017-03-31 14:08:23 -0600101import com.android.internal.util.DumpUtils;
Jeff Sharkey1cab76a2016-04-12 18:23:31 -0600102import com.android.internal.util.FastXmlSerializer;
103import com.android.internal.util.JournaledFile;
104import com.android.server.EventLogTags;
Adrian Roosc3f915e2016-09-06 11:40:53 -0700105import com.android.server.FgThread;
Andrii Kuliandd989612019-02-21 12:13:28 -0800106import com.android.server.LocalServices;
Jeff Sharkey1cab76a2016-04-12 18:23:31 -0600107import com.android.server.SystemService;
Felipe Leme4e2cbb52019-12-02 16:46:49 -0800108import com.android.server.utils.TimingsTraceAndSlog;
Andrii Kuliandd989612019-02-21 12:13:28 -0800109import com.android.server.wm.WindowManagerInternal;
Jeff Sharkey1cab76a2016-04-12 18:23:31 -0600110
111import libcore.io.IoUtils;
Joe Onorato9bb8fd72009-07-28 18:24:51 -0700112
113import org.xmlpull.v1.XmlPullParser;
114import org.xmlpull.v1.XmlPullParserException;
115import org.xmlpull.v1.XmlSerializer;
116
Jeff Sharkey1cab76a2016-04-12 18:23:31 -0600117import java.io.BufferedOutputStream;
118import java.io.File;
119import java.io.FileDescriptor;
120import java.io.FileInputStream;
121import java.io.FileNotFoundException;
122import java.io.FileOutputStream;
123import java.io.IOException;
124import java.io.InputStream;
125import java.io.PrintWriter;
126import java.nio.charset.StandardCharsets;
Lucas Dupinfb3ab2a2017-08-08 14:17:30 -0400127import java.util.ArrayList;
Jeff Sharkey1cab76a2016-04-12 18:23:31 -0600128import java.util.Arrays;
129import java.util.List;
Christopher Tate2a6c55f2017-03-31 12:28:30 -0700130import java.util.Objects;
wilsonshihde93f492018-11-01 21:23:40 +0800131import java.util.function.Consumer;
wilsonshiha282bf72018-11-30 12:48:05 +0800132import java.util.function.Predicate;
Christopher Tatead3c2592016-01-20 18:13:17 -0800133
Daichi Hirono4bbf8522017-12-06 10:34:18 +0900134public class WallpaperManagerService extends IWallpaperManager.Stub
135 implements IWallpaperManagerService {
wilsonshiha282bf72018-11-30 12:48:05 +0800136 private static final String TAG = "WallpaperManagerService";
137 private static final boolean DEBUG = false;
138 private static final boolean DEBUG_LIVE = true;
Joe Onorato9bb8fd72009-07-28 18:24:51 -0700139
Ahan Wu32c6a7c2019-10-03 12:36:59 +0800140 // This 100MB limitation is defined in RecordingCanvas.
141 private static final int MAX_BITMAP_SIZE = 100 * 1024 * 1024;
142
Jeff Sharkey1cab76a2016-04-12 18:23:31 -0600143 public static class Lifecycle extends SystemService {
Daichi Hirono4bbf8522017-12-06 10:34:18 +0900144 private IWallpaperManagerService mService;
Jeff Sharkey1cab76a2016-04-12 18:23:31 -0600145
146 public Lifecycle(Context context) {
147 super(context);
148 }
149
150 @Override
151 public void onStart() {
Daichi Hirono4bbf8522017-12-06 10:34:18 +0900152 try {
153 final Class<? extends IWallpaperManagerService> klass =
154 (Class<? extends IWallpaperManagerService>)Class.forName(
155 getContext().getResources().getString(
156 R.string.config_wallpaperManagerServiceName));
157 mService = klass.getConstructor(Context.class).newInstance(getContext());
158 publishBinderService(Context.WALLPAPER_SERVICE, mService);
159 } catch (Exception exp) {
160 Slog.wtf(TAG, "Failed to instantiate WallpaperManagerService", exp);
161 }
Jeff Sharkey1cab76a2016-04-12 18:23:31 -0600162 }
163
164 @Override
165 public void onBootPhase(int phase) {
Daichi Hirono4bbf8522017-12-06 10:34:18 +0900166 if (mService != null) {
167 mService.onBootPhase(phase);
Jeff Sharkey1cab76a2016-04-12 18:23:31 -0600168 }
169 }
170
171 @Override
172 public void onUnlockUser(int userHandle) {
Daichi Hirono4bbf8522017-12-06 10:34:18 +0900173 if (mService != null) {
174 mService.onUnlockUser(userHandle);
175 }
Jeff Sharkey1cab76a2016-04-12 18:23:31 -0600176 }
177 }
178
wilsonshiha282bf72018-11-30 12:48:05 +0800179 private final Object mLock = new Object();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800180
Dianne Hackborn0cd48872009-08-13 18:51:59 -0700181 /**
182 * Minimum time between crashes of a wallpaper service for us to consider
183 * restarting it vs. just reverting to the static wallpaper.
184 */
wilsonshiha282bf72018-11-30 12:48:05 +0800185 private static final long MIN_WALLPAPER_CRASH_TIME = 10000;
186 private static final int MAX_WALLPAPER_COMPONENT_LOG_LENGTH = 128;
Christopher Tate1e1e2e02016-01-25 15:34:36 -0800187 static final String WALLPAPER = "wallpaper_orig";
188 static final String WALLPAPER_CROP = "wallpaper";
Christopher Tatebe132e62016-02-10 12:59:49 -0800189 static final String WALLPAPER_LOCK_ORIG = "wallpaper_lock_orig";
190 static final String WALLPAPER_LOCK_CROP = "wallpaper_lock";
Amith Yamasani37ce3a82012-02-06 12:04:42 -0800191 static final String WALLPAPER_INFO = "wallpaper_info.xml";
Dianne Hackbornbce0cbb2012-10-05 11:06:53 -0700192
Christopher Tatebe132e62016-02-10 12:59:49 -0800193 // All the various per-user state files we need to be aware of
wilsonshiha282bf72018-11-30 12:48:05 +0800194 private static final String[] sPerUserFiles = new String[] {
Christopher Tatebe132e62016-02-10 12:59:49 -0800195 WALLPAPER, WALLPAPER_CROP,
196 WALLPAPER_LOCK_ORIG, WALLPAPER_LOCK_CROP,
197 WALLPAPER_INFO
198 };
199
Dianne Hackbornbce0cbb2012-10-05 11:06:53 -0700200 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800201 * Observes the wallpaper for changes and notifies all IWallpaperServiceCallbacks
202 * that the wallpaper has changed. The CREATE is triggered when there is no
203 * wallpaper set and is created for the first time. The CLOSE_WRITE is triggered
Christopher Tate190e8532016-07-11 11:35:34 -0700204 * every time the wallpaper is changed.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800205 */
Amith Yamasani37ce3a82012-02-06 12:04:42 -0800206 private class WallpaperObserver extends FileObserver {
Joe Onorato9bb8fd72009-07-28 18:24:51 -0700207
Christopher Tatebe132e62016-02-10 12:59:49 -0800208 final int mUserId;
Amith Yamasani37ce3a82012-02-06 12:04:42 -0800209 final WallpaperData mWallpaper;
210 final File mWallpaperDir;
211 final File mWallpaperFile;
Christopher Tatebe132e62016-02-10 12:59:49 -0800212 final File mWallpaperLockFile;
Amith Yamasani37ce3a82012-02-06 12:04:42 -0800213
214 public WallpaperObserver(WallpaperData wallpaper) {
215 super(getWallpaperDir(wallpaper.userId).getAbsolutePath(),
Christopher Tateda058e22014-10-08 14:51:09 -0700216 CLOSE_WRITE | MOVED_TO | DELETE | DELETE_SELF);
Christopher Tatebe132e62016-02-10 12:59:49 -0800217 mUserId = wallpaper.userId;
Amith Yamasani37ce3a82012-02-06 12:04:42 -0800218 mWallpaperDir = getWallpaperDir(wallpaper.userId);
219 mWallpaper = wallpaper;
220 mWallpaperFile = new File(mWallpaperDir, WALLPAPER);
Christopher Tatebe132e62016-02-10 12:59:49 -0800221 mWallpaperLockFile = new File(mWallpaperDir, WALLPAPER_LOCK_ORIG);
Amith Yamasani37ce3a82012-02-06 12:04:42 -0800222 }
223
Christopher Tatebe132e62016-02-10 12:59:49 -0800224 private WallpaperData dataForEvent(boolean sysChanged, boolean lockChanged) {
225 WallpaperData wallpaper = null;
226 synchronized (mLock) {
227 if (lockChanged) {
228 wallpaper = mLockWallpaperMap.get(mUserId);
229 }
230 if (wallpaper == null) {
231 // no lock-specific wallpaper exists, or sys case, handled together
232 wallpaper = mWallpaperMap.get(mUserId);
233 }
234 }
235 return (wallpaper != null) ? wallpaper : mWallpaper;
236 }
237
Amith Yamasani37ce3a82012-02-06 12:04:42 -0800238 @Override
239 public void onEvent(int event, String path) {
240 if (path == null) {
241 return;
242 }
Christopher Tated7faf532016-02-25 12:43:38 -0800243 final boolean moved = (event == MOVED_TO);
244 final boolean written = (event == CLOSE_WRITE || moved);
Christopher Tate1e1e2e02016-01-25 15:34:36 -0800245 final File changedFile = new File(mWallpaperDir, path);
246
Christopher Tatebe132e62016-02-10 12:59:49 -0800247 // System and system+lock changes happen on the system wallpaper input file;
248 // lock-only changes happen on the dedicated lock wallpaper input file
249 final boolean sysWallpaperChanged = (mWallpaperFile.equals(changedFile));
250 final boolean lockWallpaperChanged = (mWallpaperLockFile.equals(changedFile));
Lucas Dupinea1fb1e2017-04-05 17:39:44 -0700251 int notifyColorsWhich = 0;
Christopher Tatebe132e62016-02-10 12:59:49 -0800252 WallpaperData wallpaper = dataForEvent(sysWallpaperChanged, lockWallpaperChanged);
253
254 if (DEBUG) {
255 Slog.v(TAG, "Wallpaper file change: evt=" + event
256 + " path=" + path
257 + " sys=" + sysWallpaperChanged
258 + " lock=" + lockWallpaperChanged
259 + " imagePending=" + wallpaper.imageWallpaperPending
260 + " whichPending=0x" + Integer.toHexString(wallpaper.whichPending)
261 + " written=" + written);
262 }
Christopher Tate8347b632016-04-29 18:59:18 -0700263
264 if (moved && lockWallpaperChanged) {
265 // We just migrated sys -> lock to preserve imagery for an impending
Christopher Tatefa7d97f2016-06-30 12:21:57 -0700266 // new system-only wallpaper. Tell keyguard about it and make sure it
267 // has the right SELinux label.
Christopher Tate8347b632016-04-29 18:59:18 -0700268 if (DEBUG) {
269 Slog.i(TAG, "Sys -> lock MOVED_TO");
270 }
Christopher Tatefa7d97f2016-06-30 12:21:57 -0700271 SELinux.restorecon(changedFile);
Christopher Tate8347b632016-04-29 18:59:18 -0700272 notifyLockWallpaperChanged();
Lucas Dupinea1fb1e2017-04-05 17:39:44 -0700273 notifyWallpaperColorsChanged(wallpaper, FLAG_LOCK);
Christopher Tate8347b632016-04-29 18:59:18 -0700274 return;
275 }
276
Amith Yamasani37ce3a82012-02-06 12:04:42 -0800277 synchronized (mLock) {
Christopher Tatebe132e62016-02-10 12:59:49 -0800278 if (sysWallpaperChanged || lockWallpaperChanged) {
279 notifyCallbacksLocked(wallpaper);
280 if (wallpaper.wallpaperComponent == null
Christopher Tateda058e22014-10-08 14:51:09 -0700281 || event != CLOSE_WRITE // includes the MOVED_TO case
Christopher Tatebe132e62016-02-10 12:59:49 -0800282 || wallpaper.imageWallpaperPending) {
Christopher Tateda058e22014-10-08 14:51:09 -0700283 if (written) {
Christopher Tate1e1e2e02016-01-25 15:34:36 -0800284 // The image source has finished writing the source image,
285 // so we now produce the crop rect (in the background), and
286 // only publish the new displayable (sub)image as a result
287 // of that work.
Christopher Tatebe132e62016-02-10 12:59:49 -0800288 if (DEBUG) {
289 Slog.v(TAG, "Wallpaper written; generating crop");
290 }
Christopher Tateebadfb12016-07-25 14:50:08 -0700291 SELinux.restorecon(changedFile);
Christopher Tated7faf532016-02-25 12:43:38 -0800292 if (moved) {
293 // This is a restore, so generate the crop using any just-restored new
294 // crop guidelines, making sure to preserve our local dimension hints.
Christopher Tatefa7d97f2016-06-30 12:21:57 -0700295 // We also make sure to reapply the correct SELinux label.
Christopher Tated7faf532016-02-25 12:43:38 -0800296 if (DEBUG) {
297 Slog.v(TAG, "moved-to, therefore restore; reloading metadata");
298 }
299 loadSettingsLocked(wallpaper.userId, true);
300 }
Christopher Tatebe132e62016-02-10 12:59:49 -0800301 generateCrop(wallpaper);
302 if (DEBUG) {
303 Slog.v(TAG, "Crop done; invoking completion callback");
304 }
305 wallpaper.imageWallpaperPending = false;
Christopher Tatebe132e62016-02-10 12:59:49 -0800306 if (sysWallpaperChanged) {
307 // If this was the system wallpaper, rebind...
308 bindWallpaperComponentLocked(mImageWallpaper, true,
309 false, wallpaper, null);
Lucas Dupinea1fb1e2017-04-05 17:39:44 -0700310 notifyColorsWhich |= FLAG_SYSTEM;
Christopher Tatebe132e62016-02-10 12:59:49 -0800311 }
312 if (lockWallpaperChanged
Christopher Tateedf7d042016-03-29 18:24:25 -0700313 || (wallpaper.whichPending & FLAG_LOCK) != 0) {
Christopher Tatebe132e62016-02-10 12:59:49 -0800314 if (DEBUG) {
Christopher Tatedb27b842016-02-25 14:39:17 -0800315 Slog.i(TAG, "Lock-relevant wallpaper changed");
Christopher Tatebe132e62016-02-10 12:59:49 -0800316 }
Christopher Tatedb27b842016-02-25 14:39:17 -0800317 // either a lock-only wallpaper commit or a system+lock event.
318 // if it's system-plus-lock we need to wipe the lock bookkeeping;
319 // we're falling back to displaying the system wallpaper there.
320 if (!lockWallpaperChanged) {
321 mLockWallpaperMap.remove(wallpaper.userId);
322 }
323 // and in any case, tell keyguard about it
Christopher Tate8347b632016-04-29 18:59:18 -0700324 notifyLockWallpaperChanged();
Lucas Dupinea1fb1e2017-04-05 17:39:44 -0700325 notifyColorsWhich |= FLAG_LOCK;
Christopher Tatebe132e62016-02-10 12:59:49 -0800326 }
Lucas Dupinea1fb1e2017-04-05 17:39:44 -0700327
Christopher Tatedb27b842016-02-25 14:39:17 -0800328 saveSettingsLocked(wallpaper.userId);
Christopher Tate8efbe0d2017-08-29 16:50:13 -0700329
330 // Publish completion *after* we've persisted the changes
331 if (wallpaper.setComplete != null) {
332 try {
333 wallpaper.setComplete.onWallpaperChanged();
334 } catch (RemoteException e) {
335 // if this fails we don't really care; the setting app may just
336 // have crashed and that sort of thing is a fact of life.
337 }
338 }
Joe Onorato9bb8fd72009-07-28 18:24:51 -0700339 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800340 }
341 }
Amith Yamasani37ce3a82012-02-06 12:04:42 -0800342 }
Lucas Dupinea1fb1e2017-04-05 17:39:44 -0700343
344 // Outside of the lock since it will synchronize itself
345 if (notifyColorsWhich != 0) {
346 notifyWallpaperColorsChanged(wallpaper, notifyColorsWhich);
347 }
Amith Yamasani37ce3a82012-02-06 12:04:42 -0800348 }
349 }
350
wilsonshiha282bf72018-11-30 12:48:05 +0800351 private void notifyLockWallpaperChanged() {
Christopher Tate8347b632016-04-29 18:59:18 -0700352 final IWallpaperManagerCallback cb = mKeyguardListener;
353 if (cb != null) {
354 try {
355 cb.onWallpaperChanged();
356 } catch (RemoteException e) {
357 // Oh well it went away; no big deal
358 }
359 }
360 }
361
Lucas Dupin50ba9912017-07-14 11:55:05 -0700362 private void notifyWallpaperColorsChanged(@NonNull WallpaperData wallpaper, int which) {
wilsonshih36597d42018-12-05 18:56:39 +0800363 if (wallpaper.connection != null) {
364 wallpaper.connection.forEachDisplayConnector(connector -> {
365 notifyWallpaperColorsChangedOnDisplay(wallpaper, which, connector.mDisplayId);
366 });
367 } else { // Lock wallpaper does not have WallpaperConnection.
368 notifyWallpaperColorsChangedOnDisplay(wallpaper, which, DEFAULT_DISPLAY);
369 }
370 }
371
372 private RemoteCallbackList<IWallpaperManagerCallback> getWallpaperCallbacks(int userId,
373 int displayId) {
374 RemoteCallbackList<IWallpaperManagerCallback> listeners = null;
375 final SparseArray<RemoteCallbackList<IWallpaperManagerCallback>> displayListeners =
376 mColorsChangedListeners.get(userId);
377 if (displayListeners != null) {
378 listeners = displayListeners.get(displayId);
379 }
380 return listeners;
381 }
382
383 private void notifyWallpaperColorsChangedOnDisplay(@NonNull WallpaperData wallpaper, int which,
384 int displayId) {
Lucas Dupinea1fb1e2017-04-05 17:39:44 -0700385 boolean needsExtraction;
386 synchronized (mLock) {
Lucas Dupin50ba9912017-07-14 11:55:05 -0700387 final RemoteCallbackList<IWallpaperManagerCallback> currentUserColorListeners =
wilsonshih36597d42018-12-05 18:56:39 +0800388 getWallpaperCallbacks(wallpaper.userId, displayId);
Lucas Dupin50ba9912017-07-14 11:55:05 -0700389 final RemoteCallbackList<IWallpaperManagerCallback> userAllColorListeners =
wilsonshih36597d42018-12-05 18:56:39 +0800390 getWallpaperCallbacks(UserHandle.USER_ALL, displayId);
Lucas Dupin50ba9912017-07-14 11:55:05 -0700391 // No-op until someone is listening to it.
392 if (emptyCallbackList(currentUserColorListeners) &&
393 emptyCallbackList(userAllColorListeners)) {
Lucas Dupinea1fb1e2017-04-05 17:39:44 -0700394 return;
Lucas Dupin50ba9912017-07-14 11:55:05 -0700395 }
Lucas Dupinea1fb1e2017-04-05 17:39:44 -0700396
397 if (DEBUG) {
wilsonshih36597d42018-12-05 18:56:39 +0800398 Slog.v(TAG, "notifyWallpaperColorsChangedOnDisplay " + which);
Lucas Dupinea1fb1e2017-04-05 17:39:44 -0700399 }
400
401 needsExtraction = wallpaper.primaryColors == null;
402 }
403
Lucas Dupin75ec3792017-06-29 14:07:18 -0700404 // Let's notify the current values, it's fine if it's null, it just means
405 // that we don't know yet.
wilsonshih36597d42018-12-05 18:56:39 +0800406 notifyColorListeners(wallpaper.primaryColors, which, wallpaper.userId, displayId);
Lucas Dupin75ec3792017-06-29 14:07:18 -0700407
Lucas Dupinea1fb1e2017-04-05 17:39:44 -0700408 if (needsExtraction) {
409 extractColors(wallpaper);
Lucas Dupin50ba9912017-07-14 11:55:05 -0700410 synchronized (mLock) {
411 // Don't need to notify if nothing changed.
412 if (wallpaper.primaryColors == null) {
413 return;
414 }
415 }
wilsonshih36597d42018-12-05 18:56:39 +0800416 notifyColorListeners(wallpaper.primaryColors, which, wallpaper.userId, displayId);
Lucas Dupinea1fb1e2017-04-05 17:39:44 -0700417 }
Lucas Dupin75ec3792017-06-29 14:07:18 -0700418 }
Lucas Dupinea1fb1e2017-04-05 17:39:44 -0700419
Lucas Dupin50ba9912017-07-14 11:55:05 -0700420 private static <T extends IInterface> boolean emptyCallbackList(RemoteCallbackList<T> list) {
421 return (list == null || list.getRegisteredCallbackCount() == 0);
422 }
423
424 private void notifyColorListeners(@NonNull WallpaperColors wallpaperColors, int which,
wilsonshih36597d42018-12-05 18:56:39 +0800425 int userId, int displayId) {
Lucas Dupin75ec3792017-06-29 14:07:18 -0700426 final IWallpaperManagerCallback keyguardListener;
Lucas Dupinfb3ab2a2017-08-08 14:17:30 -0400427 final ArrayList<IWallpaperManagerCallback> colorListeners = new ArrayList<>();
Lucas Dupinea1fb1e2017-04-05 17:39:44 -0700428 synchronized (mLock) {
Lucas Dupinfb3ab2a2017-08-08 14:17:30 -0400429 final RemoteCallbackList<IWallpaperManagerCallback> currentUserColorListeners =
wilsonshih36597d42018-12-05 18:56:39 +0800430 getWallpaperCallbacks(userId, displayId);
Lucas Dupinfb3ab2a2017-08-08 14:17:30 -0400431 final RemoteCallbackList<IWallpaperManagerCallback> userAllColorListeners =
wilsonshih36597d42018-12-05 18:56:39 +0800432 getWallpaperCallbacks(UserHandle.USER_ALL, displayId);
Lucas Dupin75ec3792017-06-29 14:07:18 -0700433 keyguardListener = mKeyguardListener;
Lucas Dupinfb3ab2a2017-08-08 14:17:30 -0400434
435 if (currentUserColorListeners != null) {
436 final int count = currentUserColorListeners.beginBroadcast();
437 for (int i = 0; i < count; i++) {
438 colorListeners.add(currentUserColorListeners.getBroadcastItem(i));
439 }
440 currentUserColorListeners.finishBroadcast();
441 }
442
443 if (userAllColorListeners != null) {
444 final int count = userAllColorListeners.beginBroadcast();
445 for (int i = 0; i < count; i++) {
446 colorListeners.add(userAllColorListeners.getBroadcastItem(i));
447 }
448 userAllColorListeners.finishBroadcast();
449 }
Lucas Dupin75ec3792017-06-29 14:07:18 -0700450 }
Lucas Dupinea1fb1e2017-04-05 17:39:44 -0700451
Lucas Dupinfb3ab2a2017-08-08 14:17:30 -0400452 final int count = colorListeners.size();
453 for (int i = 0; i < count; i++) {
454 try {
455 colorListeners.get(i).onWallpaperColorsChanged(wallpaperColors, which, userId);
456 } catch (RemoteException e) {
457 // Callback is gone, it's not necessary to unregister it since
458 // RemoteCallbackList#getBroadcastItem will take care of it.
Lucas Dupin75ec3792017-06-29 14:07:18 -0700459 }
460 }
461
wilsonshih36597d42018-12-05 18:56:39 +0800462 // Only shows Keyguard on default display
463 if (keyguardListener != null && displayId == DEFAULT_DISPLAY) {
Lucas Dupin75ec3792017-06-29 14:07:18 -0700464 try {
Lucas Dupin50ba9912017-07-14 11:55:05 -0700465 keyguardListener.onWallpaperColorsChanged(wallpaperColors, which, userId);
Lucas Dupin75ec3792017-06-29 14:07:18 -0700466 } catch (RemoteException e) {
467 // Oh well it went away; no big deal
Lucas Dupinea1fb1e2017-04-05 17:39:44 -0700468 }
469 }
470 }
471
Lucas Dupinbcae5852017-05-03 12:42:58 -0700472 /**
473 * We can easily extract colors from an ImageWallpaper since it's only a bitmap.
Lucas Dupin284836b2017-06-23 15:28:41 -0700474 * In this case, using the crop is more than enough. Live wallpapers are just ignored.
Lucas Dupinbcae5852017-05-03 12:42:58 -0700475 *
476 * @param wallpaper a wallpaper representation
477 */
Lucas Dupinea1fb1e2017-04-05 17:39:44 -0700478 private void extractColors(WallpaperData wallpaper) {
479 String cropFile = null;
wilsonshih31d70a12019-01-14 12:42:35 +0800480 boolean defaultImageWallpaper = false;
Lucas Dupin284836b2017-06-23 15:28:41 -0700481 int wallpaperId;
Lucas Dupinea1fb1e2017-04-05 17:39:44 -0700482
wilsonshih36597d42018-12-05 18:56:39 +0800483 if (wallpaper.equals(mFallbackWallpaper)) {
wilsonshihfa24e4f2019-02-22 10:29:19 +0800484 synchronized (mLock) {
485 if (mFallbackWallpaper.primaryColors != null) return;
486 }
487 final WallpaperColors colors = extractDefaultImageWallpaperColors();
488 synchronized (mLock) {
489 mFallbackWallpaper.primaryColors = colors;
490 }
wilsonshih36597d42018-12-05 18:56:39 +0800491 return;
492 }
493
Lucas Dupinbcae5852017-05-03 12:42:58 -0700494 synchronized (mLock) {
Lucas Dupin284836b2017-06-23 15:28:41 -0700495 // Not having a wallpaperComponent means it's a lock screen wallpaper.
496 final boolean imageWallpaper = mImageWallpaper.equals(wallpaper.wallpaperComponent)
Lucas Dupinbcae5852017-05-03 12:42:58 -0700497 || wallpaper.wallpaperComponent == null;
Lucas Dupin284836b2017-06-23 15:28:41 -0700498 if (imageWallpaper && wallpaper.cropFile != null && wallpaper.cropFile.exists()) {
499 cropFile = wallpaper.cropFile.getAbsolutePath();
wilsonshih31d70a12019-01-14 12:42:35 +0800500 } else if (imageWallpaper && !wallpaper.cropExists() && !wallpaper.sourceExists()) {
501 defaultImageWallpaper = true;
Lucas Dupinea1fb1e2017-04-05 17:39:44 -0700502 }
503 wallpaperId = wallpaper.wallpaperId;
504 }
505
Lucas Dupin84b89d92017-05-09 12:16:19 -0700506 WallpaperColors colors = null;
Lucas Dupinea1fb1e2017-04-05 17:39:44 -0700507 if (cropFile != null) {
Lucas Dupin84b89d92017-05-09 12:16:19 -0700508 Bitmap bitmap = BitmapFactory.decodeFile(cropFile);
Lucas Dupin284836b2017-06-23 15:28:41 -0700509 if (bitmap != null) {
Lucas Dupinb5e50532018-05-24 16:33:14 +0000510 colors = WallpaperColors.fromBitmap(bitmap);
Lucas Dupin284836b2017-06-23 15:28:41 -0700511 bitmap.recycle();
512 }
wilsonshih31d70a12019-01-14 12:42:35 +0800513 } else if (defaultImageWallpaper) {
514 // There is no crop and source file because this is default image wallpaper.
wilsonshihfa24e4f2019-02-22 10:29:19 +0800515 colors = extractDefaultImageWallpaperColors();
Lucas Dupinbcae5852017-05-03 12:42:58 -0700516 }
Lucas Dupinea1fb1e2017-04-05 17:39:44 -0700517
Lucas Dupin84b89d92017-05-09 12:16:19 -0700518 if (colors == null) {
Lucas Dupinbcae5852017-05-03 12:42:58 -0700519 Slog.w(TAG, "Cannot extract colors because wallpaper could not be read.");
520 return;
521 }
Lucas Dupinea1fb1e2017-04-05 17:39:44 -0700522
Lucas Dupinbcae5852017-05-03 12:42:58 -0700523 synchronized (mLock) {
524 if (wallpaper.wallpaperId == wallpaperId) {
Lucas Dupin84b89d92017-05-09 12:16:19 -0700525 wallpaper.primaryColors = colors;
Lucas Dupin75ec3792017-06-29 14:07:18 -0700526 // Now that we have the colors, let's save them into the xml
527 // to avoid having to run this again.
528 saveSettingsLocked(wallpaper.userId);
Lucas Dupinbcae5852017-05-03 12:42:58 -0700529 } else {
530 Slog.w(TAG, "Not setting primary colors since wallpaper changed");
Lucas Dupinea1fb1e2017-04-05 17:39:44 -0700531 }
532 }
533 }
534
wilsonshihfa24e4f2019-02-22 10:29:19 +0800535 private WallpaperColors extractDefaultImageWallpaperColors() {
536 if (DEBUG) Slog.d(TAG, "Extract default image wallpaper colors");
537
wilsonshih36597d42018-12-05 18:56:39 +0800538 synchronized (mLock) {
wilsonshihfa24e4f2019-02-22 10:29:19 +0800539 if (mCacheDefaultImageWallpaperColors != null) return mCacheDefaultImageWallpaperColors;
wilsonshih36597d42018-12-05 18:56:39 +0800540 }
541
wilsonshih36597d42018-12-05 18:56:39 +0800542 WallpaperColors colors = null;
wilsonshihfa24e4f2019-02-22 10:29:19 +0800543 try (InputStream is = WallpaperManager.openDefaultWallpaper(mContext, FLAG_SYSTEM)) {
544 if (is == null) {
545 Slog.w(TAG, "Can't open default wallpaper stream");
546 return null;
wilsonshih36597d42018-12-05 18:56:39 +0800547 }
wilsonshihfa24e4f2019-02-22 10:29:19 +0800548
549 final BitmapFactory.Options options = new BitmapFactory.Options();
550 final Bitmap bitmap = BitmapFactory.decodeStream(is, null, options);
551 if (bitmap != null) {
552 colors = WallpaperColors.fromBitmap(bitmap);
553 bitmap.recycle();
554 }
555 } catch (OutOfMemoryError e) {
556 Slog.w(TAG, "Can't decode default wallpaper stream", e);
557 } catch (IOException e) {
558 Slog.w(TAG, "Can't close default wallpaper stream", e);
wilsonshih36597d42018-12-05 18:56:39 +0800559 }
560
561 if (colors == null) {
562 Slog.e(TAG, "Extract default image wallpaper colors failed");
wilsonshihfa24e4f2019-02-22 10:29:19 +0800563 } else {
564 synchronized (mLock) {
565 mCacheDefaultImageWallpaperColors = colors;
566 }
wilsonshih36597d42018-12-05 18:56:39 +0800567 }
568
wilsonshihfa24e4f2019-02-22 10:29:19 +0800569 return colors;
wilsonshih36597d42018-12-05 18:56:39 +0800570 }
571
Christopher Tate1e1e2e02016-01-25 15:34:36 -0800572 /**
573 * Once a new wallpaper has been written via setWallpaper(...), it needs to be cropped
574 * for display.
575 */
576 private void generateCrop(WallpaperData wallpaper) {
577 boolean success = false;
Christopher Tate1a96b632016-03-22 15:25:42 -0700578
wilsonshih81e10a72018-11-15 10:54:21 +0800579 // Only generate crop for default display.
wilsonshiha282bf72018-11-30 12:48:05 +0800580 final DisplayData wpData = getDisplayDataOrCreate(DEFAULT_DISPLAY);
Ahan Wu32c6a7c2019-10-03 12:36:59 +0800581 final Rect cropHint = new Rect(wallpaper.cropHint);
582 final DisplayInfo displayInfo = new DisplayInfo();
583 mDisplayManager.getDisplay(DEFAULT_DISPLAY).getDisplayInfo(displayInfo);
Christopher Tatebe132e62016-02-10 12:59:49 -0800584
585 if (DEBUG) {
586 Slog.v(TAG, "Generating crop for new wallpaper(s): 0x"
587 + Integer.toHexString(wallpaper.whichPending)
Christopher Tate1a96b632016-03-22 15:25:42 -0700588 + " to " + wallpaper.cropFile.getName()
589 + " crop=(" + cropHint.width() + 'x' + cropHint.height()
wilsonshih81e10a72018-11-15 10:54:21 +0800590 + ") dim=(" + wpData.mWidth + 'x' + wpData.mHeight + ')');
Christopher Tatebe132e62016-02-10 12:59:49 -0800591 }
Christopher Tate1e1e2e02016-01-25 15:34:36 -0800592
593 // Analyse the source; needed in multiple cases
594 BitmapFactory.Options options = new BitmapFactory.Options();
595 options.inJustDecodeBounds = true;
596 BitmapFactory.decodeFile(wallpaper.wallpaperFile.getAbsolutePath(), options);
Christopher Tate1a96b632016-03-22 15:25:42 -0700597 if (options.outWidth <= 0 || options.outHeight <= 0) {
Joe LaPennac298b162016-05-02 15:25:50 -0700598 Slog.w(TAG, "Invalid wallpaper data");
Christopher Tate1a96b632016-03-22 15:25:42 -0700599 success = false;
Christopher Tate1e1e2e02016-01-25 15:34:36 -0800600 } else {
Christopher Tate1a96b632016-03-22 15:25:42 -0700601 boolean needCrop = false;
602 boolean needScale = false;
Christopher Tate1e1e2e02016-01-25 15:34:36 -0800603
Christopher Tate1a96b632016-03-22 15:25:42 -0700604 // Empty crop means use the full image
605 if (cropHint.isEmpty()) {
606 cropHint.left = cropHint.top = 0;
607 cropHint.right = options.outWidth;
608 cropHint.bottom = options.outHeight;
609 } else {
610 // force the crop rect to lie within the measured bounds
611 cropHint.offset(
612 (cropHint.right > options.outWidth ? options.outWidth - cropHint.right : 0),
613 (cropHint.bottom > options.outHeight ? options.outHeight - cropHint.bottom : 0));
614
Adrian Roos5c97ff22016-08-31 10:25:38 -0700615 // If the crop hint was larger than the image we just overshot. Patch things up.
616 if (cropHint.left < 0) {
617 cropHint.left = 0;
618 }
619 if (cropHint.top < 0) {
620 cropHint.top = 0;
621 }
622
Christopher Tate1a96b632016-03-22 15:25:42 -0700623 // Don't bother cropping if what we're left with is identity
Christopher Tateebadfb12016-07-25 14:50:08 -0700624 needCrop = (options.outHeight > cropHint.height()
Adrian Roos5c97ff22016-08-31 10:25:38 -0700625 || options.outWidth > cropHint.width());
Christopher Tate1a96b632016-03-22 15:25:42 -0700626 }
627
628 // scale if the crop height winds up not matching the recommended metrics
Ahan Wu32c6a7c2019-10-03 12:36:59 +0800629 needScale = wpData.mHeight != cropHint.height()
630 || cropHint.height() > GLHelper.getMaxTextureSize()
631 || cropHint.width() > GLHelper.getMaxTextureSize();
Christopher Tate1a96b632016-03-22 15:25:42 -0700632
Valentin Iftime3047bb12018-12-28 17:02:19 +0100633 //make sure screen aspect ratio is preserved if width is scaled under screen size
634 if (needScale) {
Valentin Iftime3047bb12018-12-28 17:02:19 +0100635 final float scaleByHeight = (float) wpData.mHeight / (float) cropHint.height();
636 final int newWidth = (int) (cropHint.width() * scaleByHeight);
637 if (newWidth < displayInfo.logicalWidth) {
638 final float screenAspectRatio =
639 (float) displayInfo.logicalHeight / (float) displayInfo.logicalWidth;
640 cropHint.bottom = (int) (cropHint.width() * screenAspectRatio);
641 needCrop = true;
642 }
643 }
644
Christopher Tate1a96b632016-03-22 15:25:42 -0700645 if (DEBUG) {
646 Slog.v(TAG, "crop: w=" + cropHint.width() + " h=" + cropHint.height());
wilsonshih81e10a72018-11-15 10:54:21 +0800647 Slog.v(TAG, "dims: w=" + wpData.mWidth + " h=" + wpData.mHeight);
Christopher Tate1a96b632016-03-22 15:25:42 -0700648 Slog.v(TAG, "meas: w=" + options.outWidth + " h=" + options.outHeight);
649 Slog.v(TAG, "crop?=" + needCrop + " scale?=" + needScale);
650 }
651
652 if (!needCrop && !needScale) {
653 // Simple case: the nominal crop fits what we want, so we take
654 // the whole thing and just copy the image file directly.
Ahan Wu32c6a7c2019-10-03 12:36:59 +0800655
656 // TODO: It is not accurate to estimate bitmap size without decoding it,
657 // may be we can try to remove this optimized way in the future,
658 // that means, we will always go into the 'else' block.
659
660 // This is just a quick estimation, may be smaller than it is.
661 long estimateSize = options.outWidth * options.outHeight * 4;
662
663 // A bitmap over than MAX_BITMAP_SIZE will make drawBitmap() fail.
664 // Please see: RecordingCanvas#throwIfCannotDraw.
665 if (estimateSize < MAX_BITMAP_SIZE) {
666 success = FileUtils.copyFile(wallpaper.wallpaperFile, wallpaper.cropFile);
Christopher Tate1e1e2e02016-01-25 15:34:36 -0800667 }
Ahan Wu32c6a7c2019-10-03 12:36:59 +0800668
Christopher Tate1a96b632016-03-22 15:25:42 -0700669 if (!success) {
670 wallpaper.cropFile.delete();
671 // TODO: fall back to default wallpaper in this case
672 }
Ahan Wu32c6a7c2019-10-03 12:36:59 +0800673
674 if (DEBUG) {
675 Slog.v(TAG, "Null crop of new wallpaper, estimate size="
676 + estimateSize + ", success=" + success);
677 }
Christopher Tate1a96b632016-03-22 15:25:42 -0700678 } else {
679 // Fancy case: crop and scale. First, we decode and scale down if appropriate.
680 FileOutputStream f = null;
681 BufferedOutputStream bos = null;
682 try {
683 BitmapRegionDecoder decoder = BitmapRegionDecoder.newInstance(
684 wallpaper.wallpaperFile.getAbsolutePath(), false);
685
686 // This actually downsamples only by powers of two, but that's okay; we do
687 // a proper scaling blit later. This is to minimize transient RAM use.
688 // We calculate the largest power-of-two under the actual ratio rather than
689 // just let the decode take care of it because we also want to remap where the
690 // cropHint rectangle lies in the decoded [super]rect.
wilsonshih81e10a72018-11-15 10:54:21 +0800691 final int actualScale = cropHint.height() / wpData.mHeight;
Christopher Tate1a96b632016-03-22 15:25:42 -0700692 int scale = 1;
Ahan Wu32c6a7c2019-10-03 12:36:59 +0800693 while (2 * scale <= actualScale) {
Christopher Tate1a96b632016-03-22 15:25:42 -0700694 scale *= 2;
695 }
Ahan Wu32c6a7c2019-10-03 12:36:59 +0800696 options.inSampleSize = scale;
697 options.inJustDecodeBounds = false;
698
699 final Rect estimateCrop = new Rect(cropHint);
700 estimateCrop.scale(1f / options.inSampleSize);
701 final float hRatio = (float) wpData.mHeight / estimateCrop.height();
702 final int destHeight = (int) (estimateCrop.height() * hRatio);
703 final int destWidth = (int) (estimateCrop.width() * hRatio);
704
705 // We estimated an invalid crop, try to adjust the cropHint to get a valid one.
706 if (destWidth > GLHelper.getMaxTextureSize()) {
707 int newHeight = (int) (wpData.mHeight / hRatio);
708 int newWidth = (int) (wpData.mWidth / hRatio);
709
Christopher Tate1a96b632016-03-22 15:25:42 -0700710 if (DEBUG) {
Ahan Wu32c6a7c2019-10-03 12:36:59 +0800711 Slog.v(TAG, "Invalid crop dimensions, trying to adjust.");
Christopher Tate1a96b632016-03-22 15:25:42 -0700712 }
Ahan Wu32c6a7c2019-10-03 12:36:59 +0800713
714 estimateCrop.set(cropHint);
715 estimateCrop.left += (cropHint.width() - newWidth) / 2;
716 estimateCrop.top += (cropHint.height() - newHeight) / 2;
717 estimateCrop.right = estimateCrop.left + newWidth;
718 estimateCrop.bottom = estimateCrop.top + newHeight;
719 cropHint.set(estimateCrop);
720 estimateCrop.scale(1f / options.inSampleSize);
Christopher Tate1a96b632016-03-22 15:25:42 -0700721 }
Ahan Wu32c6a7c2019-10-03 12:36:59 +0800722
723 // We've got the safe cropHint; now we want to scale it properly to
724 // the desired rectangle.
725 // That's a height-biased operation: make it fit the hinted height.
726 final int safeHeight = (int) (estimateCrop.height() * hRatio);
727 final int safeWidth = (int) (estimateCrop.width() * hRatio);
728
729 if (DEBUG) {
730 Slog.v(TAG, "Decode parameters:");
731 Slog.v(TAG, " cropHint=" + cropHint + ", estimateCrop=" + estimateCrop);
732 Slog.v(TAG, " down sampling=" + options.inSampleSize
733 + ", hRatio=" + hRatio);
734 Slog.v(TAG, " dest=" + destWidth + "x" + destHeight);
735 Slog.v(TAG, " safe=" + safeWidth + "x" + safeHeight);
736 Slog.v(TAG, " maxTextureSize=" + GLHelper.getMaxTextureSize());
737 }
738
739 Bitmap cropped = decoder.decodeRegion(cropHint, options);
Christopher Tate1a96b632016-03-22 15:25:42 -0700740 decoder.recycle();
741
742 if (cropped == null) {
743 Slog.e(TAG, "Could not decode new wallpaper");
744 } else {
Ahan Wu32c6a7c2019-10-03 12:36:59 +0800745 // We are safe to create final crop with safe dimensions now.
Christopher Tate1a96b632016-03-22 15:25:42 -0700746 final Bitmap finalCrop = Bitmap.createScaledBitmap(cropped,
Ahan Wu32c6a7c2019-10-03 12:36:59 +0800747 safeWidth, safeHeight, true);
Christopher Tate1a96b632016-03-22 15:25:42 -0700748 if (DEBUG) {
749 Slog.v(TAG, "Final extract:");
wilsonshih81e10a72018-11-15 10:54:21 +0800750 Slog.v(TAG, " dims: w=" + wpData.mWidth
751 + " h=" + wpData.mHeight);
Ahan Wu32c6a7c2019-10-03 12:36:59 +0800752 Slog.v(TAG, " out: w=" + finalCrop.getWidth()
Christopher Tate1a96b632016-03-22 15:25:42 -0700753 + " h=" + finalCrop.getHeight());
754 }
755
Ahan Wu32c6a7c2019-10-03 12:36:59 +0800756 // A bitmap over than MAX_BITMAP_SIZE will make drawBitmap() fail.
757 // Please see: RecordingCanvas#throwIfCannotDraw.
758 if (finalCrop.getByteCount() > MAX_BITMAP_SIZE) {
759 throw new RuntimeException(
760 "Too large bitmap, limit=" + MAX_BITMAP_SIZE);
761 }
762
Christopher Tate1a96b632016-03-22 15:25:42 -0700763 f = new FileOutputStream(wallpaper.cropFile);
764 bos = new BufferedOutputStream(f, 32*1024);
Christopher Tatec484f542016-05-11 14:31:34 -0700765 finalCrop.compress(Bitmap.CompressFormat.JPEG, 100, bos);
Christopher Tate1a96b632016-03-22 15:25:42 -0700766 bos.flush(); // don't rely on the implicit flush-at-close when noting success
767 success = true;
768 }
769 } catch (Exception e) {
770 if (DEBUG) {
771 Slog.e(TAG, "Error decoding crop", e);
772 }
773 } finally {
774 IoUtils.closeQuietly(bos);
775 IoUtils.closeQuietly(f);
776 }
Christopher Tate1e1e2e02016-01-25 15:34:36 -0800777 }
778 }
779
780 if (!success) {
781 Slog.e(TAG, "Unable to apply new wallpaper");
782 wallpaper.cropFile.delete();
783 }
784
785 if (wallpaper.cropFile.exists()) {
786 boolean didRestorecon = SELinux.restorecon(wallpaper.cropFile.getAbsoluteFile());
787 if (DEBUG) {
788 Slog.v(TAG, "restorecon() of crop file returned " + didRestorecon);
789 }
790 }
791 }
792
wilsonshiha282bf72018-11-30 12:48:05 +0800793 private final Context mContext;
Andrii Kuliandd989612019-02-21 12:13:28 -0800794 private final WindowManagerInternal mWindowManagerInternal;
wilsonshiha282bf72018-11-30 12:48:05 +0800795 private final IPackageManager mIPackageManager;
796 private final MyPackageMonitor mMonitor;
797 private final AppOpsManager mAppOpsManager;
wilsonshihde93f492018-11-01 21:23:40 +0800798
799 private final DisplayManager mDisplayManager;
800 private final DisplayManager.DisplayListener mDisplayListener =
801 new DisplayManager.DisplayListener() {
802
803 @Override
804 public void onDisplayAdded(int displayId) {
wilsonshihde93f492018-11-01 21:23:40 +0800805 }
806
807 @Override
808 public void onDisplayRemoved(int displayId) {
809 synchronized (mLock) {
810 if (mLastWallpaper != null) {
wilsonshiha282bf72018-11-30 12:48:05 +0800811 WallpaperData targetWallpaper = null;
812 if (mLastWallpaper.connection.containsDisplay(displayId)) {
813 targetWallpaper = mLastWallpaper;
814 } else if (mFallbackWallpaper.connection.containsDisplay(displayId)) {
815 targetWallpaper = mFallbackWallpaper;
816 }
817 if (targetWallpaper == null) return;
818 WallpaperConnection.DisplayConnector connector =
819 targetWallpaper.connection.getDisplayConnectorOrCreate(displayId);
wilsonshihde93f492018-11-01 21:23:40 +0800820 if (connector == null) return;
821 connector.disconnectLocked();
wilsonshiha282bf72018-11-30 12:48:05 +0800822 targetWallpaper.connection.removeDisplayConnector(displayId);
823 removeDisplayData(displayId);
wilsonshihde93f492018-11-01 21:23:40 +0800824 }
wilsonshih36597d42018-12-05 18:56:39 +0800825 for (int i = mColorsChangedListeners.size() - 1; i >= 0; i--) {
826 final SparseArray<RemoteCallbackList<IWallpaperManagerCallback>> callbacks =
827 mColorsChangedListeners.valueAt(i);
828 callbacks.delete(displayId);
829 }
wilsonshihde93f492018-11-01 21:23:40 +0800830 }
831 }
832
833 @Override
834 public void onDisplayChanged(int displayId) {
wilsonshihde93f492018-11-01 21:23:40 +0800835 }
836 };
837
Lucas Dupin50ba9912017-07-14 11:55:05 -0700838 /**
839 * Map of color listeners per user id.
wilsonshih36597d42018-12-05 18:56:39 +0800840 * The first key will be the id of a user or UserHandle.USER_ALL - for wildcard listeners.
841 * The secondary key will be the display id, which means which display the listener is
842 * interested in.
Lucas Dupin50ba9912017-07-14 11:55:05 -0700843 */
wilsonshih36597d42018-12-05 18:56:39 +0800844 private final SparseArray<SparseArray<RemoteCallbackList<IWallpaperManagerCallback>>>
wilsonshiha282bf72018-11-30 12:48:05 +0800845 mColorsChangedListeners;
wilsonshih999ec102019-05-17 18:47:50 +0800846 protected WallpaperData mLastWallpaper;
wilsonshiha282bf72018-11-30 12:48:05 +0800847 private IWallpaperManagerCallback mKeyguardListener;
848 private boolean mWaitingForUnlock;
849 private boolean mShuttingDown;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800850
Justin Koh29c30162014-09-05 17:10:10 -0700851 /**
Christopher Tatead3c2592016-01-20 18:13:17 -0800852 * ID of the current wallpaper, changed every time anything sets a wallpaper.
853 * This is used for external detection of wallpaper update activity.
854 */
wilsonshiha282bf72018-11-30 12:48:05 +0800855 private int mWallpaperId;
Christopher Tatead3c2592016-01-20 18:13:17 -0800856
857 /**
Justin Koh29c30162014-09-05 17:10:10 -0700858 * Name of the component used to display bitmap wallpapers from either the gallery or
859 * built-in wallpapers.
860 */
wilsonshiha282bf72018-11-30 12:48:05 +0800861 private final ComponentName mImageWallpaper;
Justin Koh29c30162014-09-05 17:10:10 -0700862
Christopher Tate2a6c55f2017-03-31 12:28:30 -0700863 /**
wilsonshihfa24e4f2019-02-22 10:29:19 +0800864 * Default image wallpaper shall never changed after system service started, caching it when we
865 * first read the image file.
866 */
867 private WallpaperColors mCacheDefaultImageWallpaperColors;
868
869 /**
Christopher Tate2a6c55f2017-03-31 12:28:30 -0700870 * Name of the default wallpaper component; might be different from mImageWallpaper
871 */
wilsonshiha282bf72018-11-30 12:48:05 +0800872 private final ComponentName mDefaultWallpaperComponent;
Christopher Tate2a6c55f2017-03-31 12:28:30 -0700873
wilsonshiha282bf72018-11-30 12:48:05 +0800874 private final SparseArray<WallpaperData> mWallpaperMap = new SparseArray<WallpaperData>();
875 private final SparseArray<WallpaperData> mLockWallpaperMap = new SparseArray<WallpaperData>();
Dianne Hackborn07213e62011-08-24 20:05:39 -0700876
wilsonshiha282bf72018-11-30 12:48:05 +0800877 private SparseArray<DisplayData> mDisplayDatas = new SparseArray<>();
878
wilsonshih999ec102019-05-17 18:47:50 +0800879 protected WallpaperData mFallbackWallpaper;
wilsonshiha282bf72018-11-30 12:48:05 +0800880
881 private final SparseBooleanArray mUserRestorecon = new SparseBooleanArray();
882 private int mCurrentUserId = UserHandle.USER_NULL;
883 private boolean mInAmbientMode;
Dianne Hackborn07213e62011-08-24 20:05:39 -0700884
Amith Yamasani37ce3a82012-02-06 12:04:42 -0800885 static class WallpaperData {
886
887 int userId;
888
Christopher Tatebe132e62016-02-10 12:59:49 -0800889 final File wallpaperFile; // source image
890 final File cropFile; // eventual destination
Amith Yamasani37ce3a82012-02-06 12:04:42 -0800891
892 /**
Christopher Tatebe132e62016-02-10 12:59:49 -0800893 * True while the client is writing a new wallpaper
Amith Yamasani37ce3a82012-02-06 12:04:42 -0800894 */
895 boolean imageWallpaperPending;
896
897 /**
Christopher Tatebe132e62016-02-10 12:59:49 -0800898 * Which new wallpapers are being written; mirrors the 'which'
899 * selector bit field to setWallpaper().
900 */
901 int whichPending;
902
903 /**
Christopher Tate1e1e2e02016-01-25 15:34:36 -0800904 * Callback once the set + crop is finished
905 */
906 IWallpaperManagerCallback setComplete;
907
908 /**
Christopher Tated7faf532016-02-25 12:43:38 -0800909 * Is the OS allowed to back up this wallpaper imagery?
910 */
911 boolean allowBackup;
912
913 /**
Amith Yamasani37ce3a82012-02-06 12:04:42 -0800914 * Resource name if using a picture from the wallpaper gallery
915 */
916 String name = "";
917
918 /**
919 * The component name of the currently set live wallpaper.
920 */
921 ComponentName wallpaperComponent;
922
923 /**
924 * The component name of the wallpaper that should be set next.
925 */
926 ComponentName nextWallpaperComponent;
927
Christopher Tatead3c2592016-01-20 18:13:17 -0800928 /**
929 * The ID of this wallpaper
930 */
931 int wallpaperId;
932
Lucas Dupinea1fb1e2017-04-05 17:39:44 -0700933 /**
934 * Primary colors histogram
935 */
936 WallpaperColors primaryColors;
937
Amith Yamasani37ce3a82012-02-06 12:04:42 -0800938 WallpaperConnection connection;
939 long lastDiedTime;
940 boolean wallpaperUpdating;
941 WallpaperObserver wallpaperObserver;
942
943 /**
944 * List of callbacks registered they should each be notified when the wallpaper is changed.
945 */
946 private RemoteCallbackList<IWallpaperManagerCallback> callbacks
947 = new RemoteCallbackList<IWallpaperManagerCallback>();
948
Christopher Tate1e1e2e02016-01-25 15:34:36 -0800949 /**
950 * The crop hint supplied for displaying a subset of the source image
951 */
952 final Rect cropHint = new Rect(0, 0, 0, 0);
953
wilsonshih999ec102019-05-17 18:47:50 +0800954 WallpaperData(int userId, File wallpaperDir, String inputFileName, String cropFileName) {
Amith Yamasani37ce3a82012-02-06 12:04:42 -0800955 this.userId = userId;
Christopher Tatebe132e62016-02-10 12:59:49 -0800956 wallpaperFile = new File(wallpaperDir, inputFileName);
957 cropFile = new File(wallpaperDir, cropFileName);
Christopher Tate1e1e2e02016-01-25 15:34:36 -0800958 }
959
Christopher Tatebe132e62016-02-10 12:59:49 -0800960 // Called during initialization of a given user's wallpaper bookkeeping
Christopher Tate41297ff2016-03-10 16:46:15 -0800961 boolean cropExists() {
962 return cropFile.exists();
Amith Yamasani37ce3a82012-02-06 12:04:42 -0800963 }
Christopher Tate2a6c55f2017-03-31 12:28:30 -0700964
965 boolean sourceExists() {
966 return wallpaperFile.exists();
967 }
wilsonshiha282bf72018-11-30 12:48:05 +0800968 }
wilsonshih81e10a72018-11-15 10:54:21 +0800969
wilsonshih999ec102019-05-17 18:47:50 +0800970 @VisibleForTesting
971 static final class DisplayData {
wilsonshiha282bf72018-11-30 12:48:05 +0800972 int mWidth = -1;
973 int mHeight = -1;
974 final Rect mPadding = new Rect(0, 0, 0, 0);
975 final int mDisplayId;
976
977 DisplayData(int displayId) {
978 mDisplayId = displayId;
wilsonshih81e10a72018-11-15 10:54:21 +0800979 }
980 }
981
wilsonshiha282bf72018-11-30 12:48:05 +0800982 private void removeDisplayData(int displayId) {
983 mDisplayDatas.remove(displayId);
984 }
985
986 private DisplayData getDisplayDataOrCreate(int displayId) {
987 DisplayData wpdData = mDisplayDatas.get(displayId);
wilsonshih81e10a72018-11-15 10:54:21 +0800988 if (wpdData == null) {
wilsonshiha282bf72018-11-30 12:48:05 +0800989 wpdData = new DisplayData(displayId);
wilsonshih81e10a72018-11-15 10:54:21 +0800990 ensureSaneWallpaperDisplaySize(wpdData, displayId);
wilsonshiha282bf72018-11-30 12:48:05 +0800991 mDisplayDatas.append(displayId, wpdData);
wilsonshih81e10a72018-11-15 10:54:21 +0800992 }
993 return wpdData;
994 }
995
wilsonshiha282bf72018-11-30 12:48:05 +0800996 private void ensureSaneWallpaperDisplaySize(DisplayData wpdData, int displayId) {
wilsonshih81e10a72018-11-15 10:54:21 +0800997 // We always want to have some reasonable width hint.
998 final int baseSize = getMaximumSizeDimension(displayId);
999 if (wpdData.mWidth < baseSize) {
1000 wpdData.mWidth = baseSize;
1001 }
1002 if (wpdData.mHeight < baseSize) {
1003 wpdData.mHeight = baseSize;
1004 }
1005 }
1006
1007 private int getMaximumSizeDimension(int displayId) {
1008 Display display = mDisplayManager.getDisplay(displayId);
wilsonshiha282bf72018-11-30 12:48:05 +08001009 if (display == null) {
1010 Slog.w(TAG, "Invalid displayId=" + displayId + " " + Debug.getCallers(4));
1011 display = mDisplayManager.getDisplay(DEFAULT_DISPLAY);
1012 }
wilsonshih81e10a72018-11-15 10:54:21 +08001013 return display.getMaximumSizeDimension();
1014 }
1015
wilsonshiha282bf72018-11-30 12:48:05 +08001016 void forEachDisplayData(Consumer<DisplayData> action) {
1017 for (int i = mDisplayDatas.size() - 1; i >= 0; i--) {
1018 final DisplayData wpdData = mDisplayDatas.valueAt(i);
wilsonshih81e10a72018-11-15 10:54:21 +08001019 action.accept(wpdData);
1020 }
Amith Yamasani37ce3a82012-02-06 12:04:42 -08001021 }
1022
Christopher Tatead3c2592016-01-20 18:13:17 -08001023 int makeWallpaperIdLocked() {
1024 do {
1025 ++mWallpaperId;
1026 } while (mWallpaperId == 0);
1027 return mWallpaperId;
1028 }
1029
wilsonshiha282bf72018-11-30 12:48:05 +08001030 private boolean supportsMultiDisplay(WallpaperConnection connection) {
1031 if (connection != null) {
1032 return connection.mInfo == null // This is image wallpaper
1033 || connection.mInfo.supportsMultipleDisplays();
1034 }
1035 return false;
1036 }
1037
1038 private void updateFallbackConnection() {
1039 if (mLastWallpaper == null || mFallbackWallpaper == null) return;
1040 final WallpaperConnection systemConnection = mLastWallpaper.connection;
1041 final WallpaperConnection fallbackConnection = mFallbackWallpaper.connection;
wilsonshih78268c32018-12-18 20:48:28 +08001042 if (fallbackConnection == null) {
1043 Slog.w(TAG, "Fallback wallpaper connection has not been created yet!!");
1044 return;
1045 }
wilsonshih507ada52019-01-19 11:22:09 +08001046 if (supportsMultiDisplay(systemConnection)) {
1047 if (fallbackConnection.mDisplayConnector.size() != 0) {
1048 fallbackConnection.forEachDisplayConnector(connector -> {
1049 if (connector.mEngine != null) {
1050 connector.disconnectLocked();
1051 }
1052 });
1053 fallbackConnection.mDisplayConnector.clear();
1054 }
wilsonshiha282bf72018-11-30 12:48:05 +08001055 } else {
1056 fallbackConnection.appendConnectorWithCondition(display ->
1057 fallbackConnection.isUsableDisplay(display)
1058 && display.getDisplayId() != DEFAULT_DISPLAY
1059 && !fallbackConnection.containsDisplay(display.getDisplayId()));
1060 fallbackConnection.forEachDisplayConnector(connector -> {
1061 if (connector.mEngine == null) {
1062 connector.connectLocked(fallbackConnection, mFallbackWallpaper);
1063 }
1064 });
1065 }
1066 }
1067
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001068 class WallpaperConnection extends IWallpaperConnection.Stub
1069 implements ServiceConnection {
Adrian Roosc3f915e2016-09-06 11:40:53 -07001070
wilsonshihde93f492018-11-01 21:23:40 +08001071 /**
1072 * Collect needed info for a display.
1073 */
Santiago Etchebehere85ef2fd2020-03-20 15:05:44 -07001074 @VisibleForTesting
1075 final class DisplayConnector {
wilsonshihde93f492018-11-01 21:23:40 +08001076 final int mDisplayId;
1077 final Binder mToken = new Binder();
1078 IWallpaperEngine mEngine;
1079 boolean mDimensionsChanged;
1080 boolean mPaddingChanged;
1081
1082 DisplayConnector(int displayId) {
1083 mDisplayId = displayId;
1084 }
1085
1086 void ensureStatusHandled() {
wilsonshiha282bf72018-11-30 12:48:05 +08001087 final DisplayData wpdData = getDisplayDataOrCreate(mDisplayId);
wilsonshihde93f492018-11-01 21:23:40 +08001088 if (mDimensionsChanged) {
1089 try {
wilsonshih81e10a72018-11-15 10:54:21 +08001090 mEngine.setDesiredSize(wpdData.mWidth, wpdData.mHeight);
wilsonshihde93f492018-11-01 21:23:40 +08001091 } catch (RemoteException e) {
1092 Slog.w(TAG, "Failed to set wallpaper dimensions", e);
1093 }
1094 mDimensionsChanged = false;
1095 }
1096 if (mPaddingChanged) {
1097 try {
wilsonshih81e10a72018-11-15 10:54:21 +08001098 mEngine.setDisplayPadding(wpdData.mPadding);
wilsonshihde93f492018-11-01 21:23:40 +08001099 } catch (RemoteException e) {
1100 Slog.w(TAG, "Failed to set wallpaper padding", e);
1101 }
1102 mPaddingChanged = false;
1103 }
1104 }
1105
1106 void connectLocked(WallpaperConnection connection, WallpaperData wallpaper) {
wilsonshih674a4a02018-12-19 11:47:25 +08001107 if (connection.mService == null) {
1108 Slog.w(TAG, "WallpaperService is not connected yet");
1109 return;
1110 }
wilsonshihde93f492018-11-01 21:23:40 +08001111 if (DEBUG) Slog.v(TAG, "Adding window token: " + mToken);
wilsonshih999ec102019-05-17 18:47:50 +08001112 mWindowManagerInternal.addWindowToken(mToken, TYPE_WALLPAPER, mDisplayId);
wilsonshiha282bf72018-11-30 12:48:05 +08001113 final DisplayData wpdData = getDisplayDataOrCreate(mDisplayId);
wilsonshihde93f492018-11-01 21:23:40 +08001114 try {
wilsonshihde93f492018-11-01 21:23:40 +08001115 connection.mService.attach(connection, mToken, TYPE_WALLPAPER, false,
wilsonshih81e10a72018-11-15 10:54:21 +08001116 wpdData.mWidth, wpdData.mHeight,
1117 wpdData.mPadding, mDisplayId);
wilsonshihde93f492018-11-01 21:23:40 +08001118 } catch (RemoteException e) {
1119 Slog.w(TAG, "Failed attaching wallpaper on display", e);
wilsonshih674a4a02018-12-19 11:47:25 +08001120 if (wallpaper != null && !wallpaper.wallpaperUpdating
wilsonshih81e10a72018-11-15 10:54:21 +08001121 && connection.getConnectedEngineSize() == 0) {
wilsonshihde93f492018-11-01 21:23:40 +08001122 bindWallpaperComponentLocked(null /* componentName */, false /* force */,
1123 false /* fromUser */, wallpaper, null /* reply */);
1124 }
1125 }
1126 }
1127
1128 void disconnectLocked() {
1129 if (DEBUG) Slog.v(TAG, "Removing window token: " + mToken);
wilsonshih999ec102019-05-17 18:47:50 +08001130 mWindowManagerInternal.removeWindowToken(mToken, false/* removeWindows */,
1131 mDisplayId);
wilsonshihde93f492018-11-01 21:23:40 +08001132 try {
1133 if (mEngine != null) {
1134 mEngine.destroy();
1135 }
1136 } catch (RemoteException e) {
1137 }
1138 mEngine = null;
1139 }
1140 }
1141
1142 /**
1143 * A map for each display.
1144 * Use {@link #getDisplayConnectorOrCreate(int displayId)} to ensure the display is usable.
1145 */
1146 private SparseArray<DisplayConnector> mDisplayConnector = new SparseArray<>();
1147
Adrian Roosc3f915e2016-09-06 11:40:53 -07001148 /** Time in milliseconds until we expect the wallpaper to reconnect (unless we're in the
1149 * middle of an update). If exceeded, the wallpaper gets reset to the system default. */
Christopher Tate2a6c55f2017-03-31 12:28:30 -07001150 private static final long WALLPAPER_RECONNECT_TIMEOUT_MS = 10000;
Adrian Roosc3f915e2016-09-06 11:40:53 -07001151
Dianne Hackborneb034652009-09-07 00:49:58 -07001152 final WallpaperInfo mInfo;
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001153 IWallpaperService mService;
Amith Yamasani37ce3a82012-02-06 12:04:42 -08001154 WallpaperData mWallpaper;
wilsonshihde93f492018-11-01 21:23:40 +08001155 final int mClientUid;
Dianne Hackborn5dc5a002012-09-15 19:33:48 -07001156 IRemoteCallback mReply;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001157
Adrian Roosc3f915e2016-09-06 11:40:53 -07001158 private Runnable mResetRunnable = () -> {
1159 synchronized (mLock) {
Christopher Tate762dfd12016-10-10 17:44:48 -07001160 if (mShuttingDown) {
1161 // Don't expect wallpaper services to relaunch during shutdown
Christopher Tate2a6c55f2017-03-31 12:28:30 -07001162 if (DEBUG_LIVE) {
Christopher Tate762dfd12016-10-10 17:44:48 -07001163 Slog.i(TAG, "Ignoring relaunch timeout during shutdown");
1164 }
1165 return;
1166 }
1167
Adrian Roosc3f915e2016-09-06 11:40:53 -07001168 if (!mWallpaper.wallpaperUpdating
1169 && mWallpaper.userId == mCurrentUserId) {
Christopher Tate2a6c55f2017-03-31 12:28:30 -07001170 Slog.w(TAG, "Wallpaper reconnect timed out for " + mWallpaper.wallpaperComponent
1171 + ", reverting to built-in wallpaper!");
Adrian Roosc3f915e2016-09-06 11:40:53 -07001172 clearWallpaperLocked(true, FLAG_SYSTEM, mWallpaper.userId,
1173 null);
1174 }
1175 }
1176 };
1177
wilsonshihd1551332020-02-24 11:51:43 +08001178 private Runnable mTryToRebindRunnable = () -> {
1179 tryToRebind();
1180 };
1181
wilsonshihde93f492018-11-01 21:23:40 +08001182 WallpaperConnection(WallpaperInfo info, WallpaperData wallpaper, int clientUid) {
Dianne Hackborneb034652009-09-07 00:49:58 -07001183 mInfo = info;
Amith Yamasani37ce3a82012-02-06 12:04:42 -08001184 mWallpaper = wallpaper;
wilsonshihde93f492018-11-01 21:23:40 +08001185 mClientUid = clientUid;
1186 initDisplayState();
1187 }
1188
1189 private void initDisplayState() {
wilsonshiha282bf72018-11-30 12:48:05 +08001190 // Do not initialize fallback wallpaper
1191 if (!mWallpaper.equals(mFallbackWallpaper)) {
1192 if (supportsMultiDisplay(this)) {
1193 // The system wallpaper is image wallpaper or it can supports multiple displays.
1194 appendConnectorWithCondition(this::isUsableDisplay);
1195 } else {
1196 // The system wallpaper does not support multiple displays, so just attach it on
1197 // default display.
1198 mDisplayConnector.append(DEFAULT_DISPLAY,
1199 new DisplayConnector(DEFAULT_DISPLAY));
wilsonshihde93f492018-11-01 21:23:40 +08001200 }
1201 }
1202 }
1203
wilsonshiha282bf72018-11-30 12:48:05 +08001204 private void appendConnectorWithCondition(Predicate<Display> tester) {
1205 final Display[] displays = mDisplayManager.getDisplays();
1206 for (Display display : displays) {
1207 if (tester.test(display)) {
1208 final int displayId = display.getDisplayId();
wilsonshih674a4a02018-12-19 11:47:25 +08001209 final DisplayConnector connector = mDisplayConnector.get(displayId);
1210 if (connector == null) {
1211 mDisplayConnector.append(displayId,
1212 new DisplayConnector(displayId));
1213 }
wilsonshiha282bf72018-11-30 12:48:05 +08001214 }
1215 }
1216 }
1217
Santiago Etchebehere85ef2fd2020-03-20 15:05:44 -07001218 @VisibleForTesting
1219 boolean isUsableDisplay(Display display) {
Andrii Kuliandd989612019-02-21 12:13:28 -08001220 if (display == null || !display.hasAccess(mClientUid)) {
1221 return false;
1222 }
1223 final int displayId = display.getDisplayId();
wilsonshih0d5792e2019-07-04 11:23:54 +08001224 if (displayId == DEFAULT_DISPLAY) {
1225 return true;
1226 }
1227
1228 final long ident = Binder.clearCallingIdentity();
1229 try {
1230 return mWindowManagerInternal.shouldShowSystemDecorOnDisplay(displayId);
1231 } finally {
1232 Binder.restoreCallingIdentity(ident);
1233 }
wilsonshihde93f492018-11-01 21:23:40 +08001234 }
1235
1236 void forEachDisplayConnector(Consumer<DisplayConnector> action) {
1237 for (int i = mDisplayConnector.size() - 1; i >= 0; i--) {
wilsonshih81e10a72018-11-15 10:54:21 +08001238 final DisplayConnector connector = mDisplayConnector.valueAt(i);
wilsonshihde93f492018-11-01 21:23:40 +08001239 action.accept(connector);
1240 }
1241 }
1242
wilsonshih81e10a72018-11-15 10:54:21 +08001243 int getConnectedEngineSize() {
1244 int engineSize = 0;
1245 for (int i = mDisplayConnector.size() - 1; i >= 0; i--) {
1246 final DisplayConnector connector = mDisplayConnector.valueAt(i);
1247 if (connector.mEngine != null) engineSize++;
1248 }
1249 return engineSize;
1250 }
1251
wilsonshihde93f492018-11-01 21:23:40 +08001252 DisplayConnector getDisplayConnectorOrCreate(int displayId) {
1253 DisplayConnector connector = mDisplayConnector.get(displayId);
1254 if (connector == null) {
1255 final Display display = mDisplayManager.getDisplay(displayId);
1256 if (isUsableDisplay(display)) {
1257 connector = new DisplayConnector(displayId);
1258 mDisplayConnector.append(displayId, connector);
1259 }
1260 }
1261 return connector;
1262 }
1263
wilsonshiha282bf72018-11-30 12:48:05 +08001264 boolean containsDisplay(int displayId) {
1265 return mDisplayConnector.get(displayId) != null;
1266 }
1267
wilsonshihde93f492018-11-01 21:23:40 +08001268 void removeDisplayConnector(int displayId) {
1269 final DisplayConnector connector = mDisplayConnector.get(displayId);
1270 if (connector != null) {
1271 mDisplayConnector.remove(displayId);
1272 }
Dianne Hackborneb034652009-09-07 00:49:58 -07001273 }
Dianne Hackborn5dc5a002012-09-15 19:33:48 -07001274
1275 @Override
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001276 public void onServiceConnected(ComponentName name, IBinder service) {
1277 synchronized (mLock) {
Amith Yamasani37ce3a82012-02-06 12:04:42 -08001278 if (mWallpaper.connection == this) {
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001279 mService = IWallpaperService.Stub.asInterface(service);
Amith Yamasani37ce3a82012-02-06 12:04:42 -08001280 attachServiceLocked(this, mWallpaper);
Dianne Hackborneb034652009-09-07 00:49:58 -07001281 // XXX should probably do saveSettingsLocked() later
1282 // when we have an engine, but I'm not sure about
1283 // locking there and anyway we always need to be able to
1284 // recover if there is something wrong.
wilsonshiha282bf72018-11-30 12:48:05 +08001285 if (!mWallpaper.equals(mFallbackWallpaper)) {
1286 saveSettingsLocked(mWallpaper.userId);
1287 }
Adrian Roosc3f915e2016-09-06 11:40:53 -07001288 FgThread.getHandler().removeCallbacks(mResetRunnable);
wilsonshihd1551332020-02-24 11:51:43 +08001289 mContext.getMainThreadHandler().removeCallbacks(mTryToRebindRunnable);
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001290 }
1291 }
1292 }
1293
Dianne Hackborn5dc5a002012-09-15 19:33:48 -07001294 @Override
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001295 public void onServiceDisconnected(ComponentName name) {
1296 synchronized (mLock) {
Christopher Tatec349e59f2017-05-05 17:37:43 -07001297 Slog.w(TAG, "Wallpaper service gone: " + name);
1298 if (!Objects.equals(name, mWallpaper.wallpaperComponent)) {
1299 Slog.e(TAG, "Does not match expected wallpaper component "
1300 + mWallpaper.wallpaperComponent);
1301 }
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001302 mService = null;
wilsonshihde93f492018-11-01 21:23:40 +08001303 forEachDisplayConnector(connector -> connector.mEngine = null);
Amith Yamasani37ce3a82012-02-06 12:04:42 -08001304 if (mWallpaper.connection == this) {
Christopher Tatec349e59f2017-05-05 17:37:43 -07001305 // There is an inherent ordering race between this callback and the
1306 // package monitor that receives notice that a package is being updated,
1307 // so we cannot quite trust at this moment that we know for sure that
1308 // this is not an update. If we think this is a genuine non-update
1309 // wallpaper outage, we do our "wait for reset" work as a continuation,
1310 // a short time in the future, specifically to allow any pending package
1311 // update message on this same looper thread to be processed.
1312 if (!mWallpaper.wallpaperUpdating) {
1313 mContext.getMainThreadHandler().postDelayed(() -> processDisconnect(this),
1314 1000);
1315 }
1316 }
1317 }
1318 }
1319
Tetsutoki Shiozawa06919212018-02-02 14:18:43 +09001320 public void scheduleTimeoutLocked() {
1321 // If we didn't reset it right away, do so after we couldn't connect to
1322 // it for an extended amount of time to avoid having a black wallpaper.
1323 final Handler fgHandler = FgThread.getHandler();
1324 fgHandler.removeCallbacks(mResetRunnable);
1325 fgHandler.postDelayed(mResetRunnable, WALLPAPER_RECONNECT_TIMEOUT_MS);
1326 if (DEBUG_LIVE) {
wilsonshihde93f492018-11-01 21:23:40 +08001327 Slog.i(TAG,
1328 "Started wallpaper reconnect timeout for " + mWallpaper.wallpaperComponent);
Tetsutoki Shiozawa06919212018-02-02 14:18:43 +09001329 }
1330 }
1331
wilsonshihb8753022019-10-09 20:50:44 +08001332 private void tryToRebind() {
1333 synchronized (mLock) {
1334 if (mWallpaper.wallpaperUpdating) {
1335 return;
1336 }
1337 final ComponentName wpService = mWallpaper.wallpaperComponent;
1338 // The broadcast of package update could be delayed after service disconnected. Try
1339 // to re-bind the service for 10 seconds.
1340 if (bindWallpaperComponentLocked(
1341 wpService, true, false, mWallpaper, null)) {
1342 mWallpaper.connection.scheduleTimeoutLocked();
1343 } else if (SystemClock.uptimeMillis() - mWallpaper.lastDiedTime
1344 < WALLPAPER_RECONNECT_TIMEOUT_MS) {
1345 // Bind fail without timeout, schedule rebind
1346 Slog.w(TAG, "Rebind fail! Try again later");
wilsonshihd1551332020-02-24 11:51:43 +08001347 mContext.getMainThreadHandler().postDelayed(mTryToRebindRunnable, 1000);
wilsonshihb8753022019-10-09 20:50:44 +08001348 } else {
1349 // Timeout
1350 Slog.w(TAG, "Reverting to built-in wallpaper!");
1351 clearWallpaperLocked(true, FLAG_SYSTEM, mWallpaper.userId, null);
1352 final String flattened = wpService.flattenToString();
1353 EventLog.writeEvent(EventLogTags.WP_WALLPAPER_CRASHED,
1354 flattened.substring(0, Math.min(flattened.length(),
1355 MAX_WALLPAPER_COMPONENT_LOG_LENGTH)));
1356 }
1357 }
1358 }
1359
Christopher Tatec349e59f2017-05-05 17:37:43 -07001360 private void processDisconnect(final ServiceConnection connection) {
1361 synchronized (mLock) {
1362 // The wallpaper disappeared. If this isn't a system-default one, track
1363 // crashes and fall back to default if it continues to misbehave.
1364 if (connection == mWallpaper.connection) {
Christopher Tate2a6c55f2017-03-31 12:28:30 -07001365 final ComponentName wpService = mWallpaper.wallpaperComponent;
Amith Yamasani37ce3a82012-02-06 12:04:42 -08001366 if (!mWallpaper.wallpaperUpdating
Christopher Tate2a6c55f2017-03-31 12:28:30 -07001367 && mWallpaper.userId == mCurrentUserId
1368 && !Objects.equals(mDefaultWallpaperComponent, wpService)
1369 && !Objects.equals(mImageWallpaper, wpService)) {
Selim Cinekebebadb2014-03-05 22:17:26 +01001370 // There is a race condition which causes
1371 // {@link #mWallpaper.wallpaperUpdating} to be false even if it is
1372 // currently updating since the broadcast notifying us is async.
1373 // This race is overcome by the general rule that we only reset the
1374 // wallpaper if its service was shut down twice
1375 // during {@link #MIN_WALLPAPER_CRASH_TIME} millis.
1376 if (mWallpaper.lastDiedTime != 0
1377 && mWallpaper.lastDiedTime + MIN_WALLPAPER_CRASH_TIME
Christopher Tatec349e59f2017-05-05 17:37:43 -07001378 > SystemClock.uptimeMillis()) {
Selim Cinekebebadb2014-03-05 22:17:26 +01001379 Slog.w(TAG, "Reverting to built-in wallpaper!");
Christopher Tateedf7d042016-03-29 18:24:25 -07001380 clearWallpaperLocked(true, FLAG_SYSTEM, mWallpaper.userId, null);
Selim Cinekebebadb2014-03-05 22:17:26 +01001381 } else {
1382 mWallpaper.lastDiedTime = SystemClock.uptimeMillis();
wilsonshihb8753022019-10-09 20:50:44 +08001383 tryToRebind();
Selim Cinekebebadb2014-03-05 22:17:26 +01001384 }
Dianne Hackborn0cd48872009-08-13 18:51:59 -07001385 }
Christopher Tatec349e59f2017-05-05 17:37:43 -07001386 } else {
1387 if (DEBUG_LIVE) {
1388 Slog.i(TAG, "Wallpaper changed during disconnect tracking; ignoring");
1389 }
Dianne Hackborn0cd48872009-08-13 18:51:59 -07001390 }
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001391 }
1392 }
Amith Yamasani37ce3a82012-02-06 12:04:42 -08001393
Lucas Dupinea1fb1e2017-04-05 17:39:44 -07001394 /**
1395 * Called by a live wallpaper if its colors have changed.
1396 * @param primaryColors representation of wallpaper primary colors
wilsonshih36597d42018-12-05 18:56:39 +08001397 * @param displayId for which display
Lucas Dupinea1fb1e2017-04-05 17:39:44 -07001398 */
1399 @Override
wilsonshih36597d42018-12-05 18:56:39 +08001400 public void onWallpaperColorsChanged(WallpaperColors primaryColors, int displayId) {
Lucas Dupinea1fb1e2017-04-05 17:39:44 -07001401 int which;
1402 synchronized (mLock) {
1403 // Do not broadcast changes on ImageWallpaper since it's handled
1404 // internally by this class.
1405 if (mImageWallpaper.equals(mWallpaper.wallpaperComponent)) {
1406 return;
1407 }
1408
1409 mWallpaper.primaryColors = primaryColors;
1410
1411 // Live wallpapers always are system wallpapers.
1412 which = FLAG_SYSTEM;
wilsonshih36597d42018-12-05 18:56:39 +08001413 // It's also the lock screen wallpaper when we don't have a bitmap in there.
1414 if (displayId == DEFAULT_DISPLAY) {
1415 final WallpaperData lockedWallpaper = mLockWallpaperMap.get(mWallpaper.userId);
1416 if (lockedWallpaper == null) {
1417 which |= FLAG_LOCK;
1418 }
Lucas Dupinea1fb1e2017-04-05 17:39:44 -07001419 }
1420 }
1421 if (which != 0) {
wilsonshih36597d42018-12-05 18:56:39 +08001422 notifyWallpaperColorsChangedOnDisplay(mWallpaper, which, displayId);
Lucas Dupinea1fb1e2017-04-05 17:39:44 -07001423 }
1424 }
1425
Dianne Hackborn5dc5a002012-09-15 19:33:48 -07001426 @Override
wilsonshihde93f492018-11-01 21:23:40 +08001427 public void attachEngine(IWallpaperEngine engine, int displayId) {
Dianne Hackborn5dc5a002012-09-15 19:33:48 -07001428 synchronized (mLock) {
wilsonshihde93f492018-11-01 21:23:40 +08001429 final DisplayConnector connector = getDisplayConnectorOrCreate(displayId);
1430 if (connector == null) {
Michael Wright5203a8b2013-10-03 14:16:42 -07001431 try {
wilsonshihde93f492018-11-01 21:23:40 +08001432 engine.destroy();
Michael Wright5203a8b2013-10-03 14:16:42 -07001433 } catch (RemoteException e) {
wilsonshihde93f492018-11-01 21:23:40 +08001434 Slog.w(TAG, "Failed to destroy engine", e);
Michael Wright5203a8b2013-10-03 14:16:42 -07001435 }
wilsonshihde93f492018-11-01 21:23:40 +08001436 return;
Michael Wright5203a8b2013-10-03 14:16:42 -07001437 }
wilsonshihde93f492018-11-01 21:23:40 +08001438 connector.mEngine = engine;
1439 connector.ensureStatusHandled();
1440
1441 // TODO(multi-display) TBD.
1442 if (mInfo != null && mInfo.supportsAmbientMode() && displayId == DEFAULT_DISPLAY) {
Dianne Hackborn067e5f62014-09-07 23:14:30 -07001443 try {
Lucas Dupin4c8c3272018-11-06 17:47:48 -08001444 connector.mEngine.setInAmbientMode(mInAmbientMode, 0L /* duration */);
Lucas Dupin7517b5d2017-08-22 12:51:25 -07001445 } catch (RemoteException e) {
1446 Slog.w(TAG, "Failed to set ambient mode state", e);
1447 }
1448 }
wilsonshih36597d42018-12-05 18:56:39 +08001449 try {
1450 // This will trigger onComputeColors in the wallpaper engine.
1451 // It's fine to be locked in here since the binder is oneway.
1452 connector.mEngine.requestWallpaperColors();
1453 } catch (RemoteException e) {
1454 Slog.w(TAG, "Failed to request wallpaper colors", e);
Lucas Dupin50ba9912017-07-14 11:55:05 -07001455 }
Dianne Hackborn5dc5a002012-09-15 19:33:48 -07001456 }
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001457 }
Amith Yamasani37ce3a82012-02-06 12:04:42 -08001458
Dianne Hackborn5dc5a002012-09-15 19:33:48 -07001459 @Override
1460 public void engineShown(IWallpaperEngine engine) {
1461 synchronized (mLock) {
1462 if (mReply != null) {
1463 long ident = Binder.clearCallingIdentity();
1464 try {
1465 mReply.sendResult(null);
1466 } catch (RemoteException e) {
1467 Binder.restoreCallingIdentity(ident);
1468 }
1469 mReply = null;
1470 }
1471 }
1472 }
1473
1474 @Override
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001475 public ParcelFileDescriptor setWallpaper(String name) {
1476 synchronized (mLock) {
Amith Yamasani37ce3a82012-02-06 12:04:42 -08001477 if (mWallpaper.connection == this) {
Christopher Tatead3c2592016-01-20 18:13:17 -08001478 return updateWallpaperBitmapLocked(name, mWallpaper, null);
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001479 }
1480 return null;
1481 }
1482 }
1483 }
Amith Yamasani37ce3a82012-02-06 12:04:42 -08001484
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001485 class MyPackageMonitor extends PackageMonitor {
1486 @Override
1487 public void onPackageUpdateFinished(String packageName, int uid) {
1488 synchronized (mLock) {
Dianne Hackbornc72fc672012-09-20 13:12:03 -07001489 if (mCurrentUserId != getChangingUserId()) {
1490 return;
1491 }
1492 WallpaperData wallpaper = mWallpaperMap.get(mCurrentUserId);
1493 if (wallpaper != null) {
Christopher Tate2a6c55f2017-03-31 12:28:30 -07001494 final ComponentName wpService = wallpaper.wallpaperComponent;
1495 if (wpService != null && wpService.getPackageName().equals(packageName)) {
1496 if (DEBUG_LIVE) {
1497 Slog.i(TAG, "Wallpaper " + wpService + " update has finished");
1498 }
Amith Yamasani37ce3a82012-02-06 12:04:42 -08001499 wallpaper.wallpaperUpdating = false;
Amith Yamasani37ce3a82012-02-06 12:04:42 -08001500 clearWallpaperComponentLocked(wallpaper);
Christopher Tate2a6c55f2017-03-31 12:28:30 -07001501 if (!bindWallpaperComponentLocked(wpService, false, false,
Dianne Hackbornc72fc672012-09-20 13:12:03 -07001502 wallpaper, null)) {
Christopher Tate2a6c55f2017-03-31 12:28:30 -07001503 Slog.w(TAG, "Wallpaper " + wpService
1504 + " no longer available; reverting to default");
Christopher Tateedf7d042016-03-29 18:24:25 -07001505 clearWallpaperLocked(false, FLAG_SYSTEM, wallpaper.userId, null);
Amith Yamasani37ce3a82012-02-06 12:04:42 -08001506 }
Dianne Hackborn80b902f2011-09-15 15:15:27 -07001507 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001508 }
1509 }
1510 }
1511
1512 @Override
Dianne Hackborn80b902f2011-09-15 15:15:27 -07001513 public void onPackageModified(String packageName) {
1514 synchronized (mLock) {
Dianne Hackbornc72fc672012-09-20 13:12:03 -07001515 if (mCurrentUserId != getChangingUserId()) {
1516 return;
1517 }
1518 WallpaperData wallpaper = mWallpaperMap.get(mCurrentUserId);
1519 if (wallpaper != null) {
Amith Yamasani37ce3a82012-02-06 12:04:42 -08001520 if (wallpaper.wallpaperComponent == null
1521 || !wallpaper.wallpaperComponent.getPackageName().equals(packageName)) {
Dianne Hackbornc72fc672012-09-20 13:12:03 -07001522 return;
Amith Yamasani37ce3a82012-02-06 12:04:42 -08001523 }
Dianne Hackbornd0d75032012-04-19 23:12:09 -07001524 doPackagesChangedLocked(true, wallpaper);
Dianne Hackborn80b902f2011-09-15 15:15:27 -07001525 }
1526 }
Dianne Hackborn80b902f2011-09-15 15:15:27 -07001527 }
1528
1529 @Override
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001530 public void onPackageUpdateStarted(String packageName, int uid) {
1531 synchronized (mLock) {
Dianne Hackbornc72fc672012-09-20 13:12:03 -07001532 if (mCurrentUserId != getChangingUserId()) {
1533 return;
1534 }
1535 WallpaperData wallpaper = mWallpaperMap.get(mCurrentUserId);
1536 if (wallpaper != null) {
Amith Yamasani37ce3a82012-02-06 12:04:42 -08001537 if (wallpaper.wallpaperComponent != null
1538 && wallpaper.wallpaperComponent.getPackageName().equals(packageName)) {
Christopher Tate2a6c55f2017-03-31 12:28:30 -07001539 if (DEBUG_LIVE) {
1540 Slog.i(TAG, "Wallpaper service " + wallpaper.wallpaperComponent
1541 + " is updating");
1542 }
Amith Yamasani37ce3a82012-02-06 12:04:42 -08001543 wallpaper.wallpaperUpdating = true;
Adrian Roosc3f915e2016-09-06 11:40:53 -07001544 if (wallpaper.connection != null) {
1545 FgThread.getHandler().removeCallbacks(
1546 wallpaper.connection.mResetRunnable);
1547 }
Amith Yamasani37ce3a82012-02-06 12:04:42 -08001548 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001549 }
1550 }
1551 }
1552
1553 @Override
1554 public boolean onHandleForceStop(Intent intent, String[] packages, int uid, boolean doit) {
Dianne Hackbornd0d75032012-04-19 23:12:09 -07001555 synchronized (mLock) {
1556 boolean changed = false;
Dianne Hackbornc72fc672012-09-20 13:12:03 -07001557 if (mCurrentUserId != getChangingUserId()) {
1558 return false;
1559 }
1560 WallpaperData wallpaper = mWallpaperMap.get(mCurrentUserId);
1561 if (wallpaper != null) {
Dianne Hackbornd0d75032012-04-19 23:12:09 -07001562 boolean res = doPackagesChangedLocked(doit, wallpaper);
1563 changed |= res;
1564 }
1565 return changed;
Amith Yamasani37ce3a82012-02-06 12:04:42 -08001566 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001567 }
1568
1569 @Override
1570 public void onSomePackagesChanged() {
Dianne Hackbornd0d75032012-04-19 23:12:09 -07001571 synchronized (mLock) {
Dianne Hackbornc72fc672012-09-20 13:12:03 -07001572 if (mCurrentUserId != getChangingUserId()) {
1573 return;
1574 }
1575 WallpaperData wallpaper = mWallpaperMap.get(mCurrentUserId);
1576 if (wallpaper != null) {
Dianne Hackbornd0d75032012-04-19 23:12:09 -07001577 doPackagesChangedLocked(true, wallpaper);
1578 }
Amith Yamasani37ce3a82012-02-06 12:04:42 -08001579 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001580 }
Amith Yamasani37ce3a82012-02-06 12:04:42 -08001581
Dianne Hackbornd0d75032012-04-19 23:12:09 -07001582 boolean doPackagesChangedLocked(boolean doit, WallpaperData wallpaper) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001583 boolean changed = false;
Dianne Hackbornd0d75032012-04-19 23:12:09 -07001584 if (wallpaper.wallpaperComponent != null) {
1585 int change = isPackageDisappearing(wallpaper.wallpaperComponent
1586 .getPackageName());
1587 if (change == PACKAGE_PERMANENT_CHANGE
1588 || change == PACKAGE_TEMPORARY_CHANGE) {
1589 changed = true;
1590 if (doit) {
1591 Slog.w(TAG, "Wallpaper uninstalled, removing: "
Amith Yamasani37ce3a82012-02-06 12:04:42 -08001592 + wallpaper.wallpaperComponent);
Christopher Tateedf7d042016-03-29 18:24:25 -07001593 clearWallpaperLocked(false, FLAG_SYSTEM, wallpaper.userId, null);
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001594 }
1595 }
Dianne Hackbornd0d75032012-04-19 23:12:09 -07001596 }
1597 if (wallpaper.nextWallpaperComponent != null) {
1598 int change = isPackageDisappearing(wallpaper.nextWallpaperComponent
1599 .getPackageName());
1600 if (change == PACKAGE_PERMANENT_CHANGE
1601 || change == PACKAGE_TEMPORARY_CHANGE) {
1602 wallpaper.nextWallpaperComponent = null;
1603 }
1604 }
1605 if (wallpaper.wallpaperComponent != null
1606 && isPackageModified(wallpaper.wallpaperComponent.getPackageName())) {
1607 try {
Jeff Sharkey1cab76a2016-04-12 18:23:31 -06001608 mContext.getPackageManager().getServiceInfo(wallpaper.wallpaperComponent,
1609 PackageManager.MATCH_DIRECT_BOOT_AWARE
1610 | PackageManager.MATCH_DIRECT_BOOT_UNAWARE);
Dianne Hackbornd0d75032012-04-19 23:12:09 -07001611 } catch (NameNotFoundException e) {
1612 Slog.w(TAG, "Wallpaper component gone, removing: "
1613 + wallpaper.wallpaperComponent);
Christopher Tateedf7d042016-03-29 18:24:25 -07001614 clearWallpaperLocked(false, FLAG_SYSTEM, wallpaper.userId, null);
Dianne Hackbornd0d75032012-04-19 23:12:09 -07001615 }
1616 }
1617 if (wallpaper.nextWallpaperComponent != null
1618 && isPackageModified(wallpaper.nextWallpaperComponent.getPackageName())) {
1619 try {
Jeff Sharkey1cab76a2016-04-12 18:23:31 -06001620 mContext.getPackageManager().getServiceInfo(wallpaper.nextWallpaperComponent,
1621 PackageManager.MATCH_DIRECT_BOOT_AWARE
1622 | PackageManager.MATCH_DIRECT_BOOT_UNAWARE);
Dianne Hackbornd0d75032012-04-19 23:12:09 -07001623 } catch (NameNotFoundException e) {
1624 wallpaper.nextWallpaperComponent = null;
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001625 }
1626 }
1627 return changed;
1628 }
1629 }
Oleksandr Peletskyif2519812016-01-26 20:16:06 +01001630
wilsonshih999ec102019-05-17 18:47:50 +08001631 @VisibleForTesting
1632 WallpaperData getCurrentWallpaperData(@SetWallpaperFlags int which, int userId) {
1633 synchronized (mLock) {
1634 final SparseArray<WallpaperData> wallpaperDataMap =
1635 which == FLAG_SYSTEM ? mWallpaperMap : mLockWallpaperMap;
1636 return wallpaperDataMap.get(userId);
1637 }
1638 }
1639
Dianne Hackborn8cc6a502009-08-05 21:29:42 -07001640 public WallpaperManagerService(Context context) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001641 if (DEBUG) Slog.v(TAG, "WallpaperService startup");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001642 mContext = context;
Christopher Tate762dfd12016-10-10 17:44:48 -07001643 mShuttingDown = false;
Justin Koh29c30162014-09-05 17:10:10 -07001644 mImageWallpaper = ComponentName.unflattenFromString(
1645 context.getResources().getString(R.string.image_wallpaper_component));
Christopher Tate2a6c55f2017-03-31 12:28:30 -07001646 mDefaultWallpaperComponent = WallpaperManager.getDefaultWallpaperComponent(context);
Andrii Kuliandd989612019-02-21 12:13:28 -08001647 mWindowManagerInternal = LocalServices.getService(WindowManagerInternal.class);
Amith Yamasani4e2820c2012-08-28 22:17:23 -07001648 mIPackageManager = AppGlobals.getPackageManager();
Benjamin Franzf3ece362015-02-11 10:51:10 +00001649 mAppOpsManager = (AppOpsManager) mContext.getSystemService(Context.APP_OPS_SERVICE);
wilsonshihde93f492018-11-01 21:23:40 +08001650 mDisplayManager = mContext.getSystemService(DisplayManager.class);
1651 mDisplayManager.registerDisplayListener(mDisplayListener, null /* handler */);
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001652 mMonitor = new MyPackageMonitor();
Jaekyun Seokad7d90f2018-04-04 01:57:18 +09001653 mColorsChangedListeners = new SparseArray<>();
wilsonshih643bf132019-02-27 12:49:19 +08001654
1655 LocalServices.addService(WallpaperManagerInternal.class, new LocalService());
1656 }
1657
1658 private final class LocalService extends WallpaperManagerInternal {
1659 @Override
1660 public void onDisplayReady(int displayId) {
1661 onDisplayReadyInternal(displayId);
1662 }
Jaekyun Seokad7d90f2018-04-04 01:57:18 +09001663 }
1664
1665 void initialize() {
1666 mMonitor.register(mContext, null, UserHandle.ALL, true);
Xiaohui Chen233d94c2015-07-30 15:08:00 -07001667 getWallpaperDir(UserHandle.USER_SYSTEM).mkdirs();
Christopher Tatef717b932017-09-11 15:52:54 -07001668
1669 // Initialize state from the persistent store, then guarantee that the
1670 // WallpaperData for the system imagery is instantiated & active, creating
1671 // it from defaults if necessary.
Christopher Tated7faf532016-02-25 12:43:38 -08001672 loadSettingsLocked(UserHandle.USER_SYSTEM, false);
Christopher Tatef717b932017-09-11 15:52:54 -07001673 getWallpaperSafeLocked(UserHandle.USER_SYSTEM, FLAG_SYSTEM);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001674 }
Oleksandr Peletskyif2519812016-01-26 20:16:06 +01001675
wilsonshih999ec102019-05-17 18:47:50 +08001676 File getWallpaperDir(int userId) {
Amith Yamasani61f57372012-08-31 12:12:28 -07001677 return Environment.getUserSystemDirectory(userId);
Amith Yamasani37ce3a82012-02-06 12:04:42 -08001678 }
1679
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001680 @Override
1681 protected void finalize() throws Throwable {
1682 super.finalize();
Amith Yamasani37ce3a82012-02-06 12:04:42 -08001683 for (int i = 0; i < mWallpaperMap.size(); i++) {
1684 WallpaperData wallpaper = mWallpaperMap.valueAt(i);
1685 wallpaper.wallpaperObserver.stopWatching();
1686 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001687 }
Amith Yamasani13593602012-03-22 16:16:17 -07001688
Jeff Sharkey1cab76a2016-04-12 18:23:31 -06001689 void systemReady() {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001690 if (DEBUG) Slog.v(TAG, "systemReady");
Jaekyun Seokad7d90f2018-04-04 01:57:18 +09001691 initialize();
1692
Xiaohui Chen233d94c2015-07-30 15:08:00 -07001693 WallpaperData wallpaper = mWallpaperMap.get(UserHandle.USER_SYSTEM);
Christopher Tate2cdd3f22016-03-14 17:36:16 -07001694 // If we think we're going to be using the system image wallpaper imagery, make
1695 // sure we have something to render
1696 if (mImageWallpaper.equals(wallpaper.nextWallpaperComponent)) {
1697 // No crop file? Make sure we've finished the processing sequence if necessary
1698 if (!wallpaper.cropExists()) {
1699 if (DEBUG) {
1700 Slog.i(TAG, "No crop; regenerating from source");
1701 }
1702 generateCrop(wallpaper);
1703 }
1704 // Still nothing? Fall back to default.
1705 if (!wallpaper.cropExists()) {
1706 if (DEBUG) {
1707 Slog.i(TAG, "Unable to regenerate crop; resetting");
1708 }
Christopher Tateedf7d042016-03-29 18:24:25 -07001709 clearWallpaperLocked(false, FLAG_SYSTEM, UserHandle.USER_SYSTEM, null);
Christopher Tate2cdd3f22016-03-14 17:36:16 -07001710 }
1711 } else {
1712 if (DEBUG) {
1713 Slog.i(TAG, "Nondefault wallpaper component; gracefully ignoring");
1714 }
Christopher Tate1e1e2e02016-01-25 15:34:36 -08001715 }
Amith Yamasani37ce3a82012-02-06 12:04:42 -08001716
Amith Yamasani13593602012-03-22 16:16:17 -07001717 IntentFilter userFilter = new IntentFilter();
Amith Yamasani13593602012-03-22 16:16:17 -07001718 userFilter.addAction(Intent.ACTION_USER_REMOVED);
1719 mContext.registerReceiver(new BroadcastReceiver() {
Amith Yamasani37ce3a82012-02-06 12:04:42 -08001720 @Override
Amith Yamasani13593602012-03-22 16:16:17 -07001721 public void onReceive(Context context, Intent intent) {
Jeff Sharkey1cab76a2016-04-12 18:23:31 -06001722 final String action = intent.getAction();
Dianne Hackborn5dc5a002012-09-15 19:33:48 -07001723 if (Intent.ACTION_USER_REMOVED.equals(action)) {
Amith Yamasani756901d2012-10-12 12:30:07 -07001724 onRemoveUser(intent.getIntExtra(Intent.EXTRA_USER_HANDLE,
1725 UserHandle.USER_NULL));
Amith Yamasani13593602012-03-22 16:16:17 -07001726 }
Amith Yamasani37ce3a82012-02-06 12:04:42 -08001727 }
Amith Yamasani13593602012-03-22 16:16:17 -07001728 }, userFilter);
Amith Yamasani756901d2012-10-12 12:30:07 -07001729
Christopher Tate762dfd12016-10-10 17:44:48 -07001730 final IntentFilter shutdownFilter = new IntentFilter(Intent.ACTION_SHUTDOWN);
1731 mContext.registerReceiver(new BroadcastReceiver() {
1732 @Override
1733 public void onReceive(Context context, Intent intent) {
1734 if (Intent.ACTION_SHUTDOWN.equals(intent.getAction())) {
1735 if (DEBUG) {
1736 Slog.i(TAG, "Shutting down");
1737 }
1738 synchronized (mLock) {
1739 mShuttingDown = true;
1740 }
1741 }
1742 }
1743 }, shutdownFilter);
1744
Dianne Hackborn5dc5a002012-09-15 19:33:48 -07001745 try {
Sudheer Shankadc589ac2016-11-10 15:30:17 -08001746 ActivityManager.getService().registerUserSwitchObserver(
Sudheer Shanka2c4522c2016-08-27 20:53:28 -07001747 new UserSwitchObserver() {
Dianne Hackborn5dc5a002012-09-15 19:33:48 -07001748 @Override
1749 public void onUserSwitching(int newUserId, IRemoteCallback reply) {
1750 switchUser(newUserId, reply);
1751 }
Fyodor Kupolov0b77ef92016-06-20 17:16:52 -07001752 }, TAG);
Dianne Hackborn5dc5a002012-09-15 19:33:48 -07001753 } catch (RemoteException e) {
Fyodor Kupolov0b77ef92016-06-20 17:16:52 -07001754 e.rethrowAsRuntimeException();
Dianne Hackborn5dc5a002012-09-15 19:33:48 -07001755 }
Amith Yamasani37ce3a82012-02-06 12:04:42 -08001756 }
1757
Amith Yamasani09e9cdc2013-11-06 14:54:50 -08001758 /** Called by SystemBackupAgent */
1759 public String getName() {
1760 // Verify caller is the system
1761 if (Binder.getCallingUid() != android.os.Process.SYSTEM_UID) {
1762 throw new RuntimeException("getName() can only be called from the system process");
1763 }
Dianne Hackborn5dc5a002012-09-15 19:33:48 -07001764 synchronized (mLock) {
1765 return mWallpaperMap.get(0).name;
1766 }
Amith Yamasani37ce3a82012-02-06 12:04:42 -08001767 }
1768
Christopher Tatebe132e62016-02-10 12:59:49 -08001769 void stopObserver(WallpaperData wallpaper) {
1770 if (wallpaper != null) {
1771 if (wallpaper.wallpaperObserver != null) {
1772 wallpaper.wallpaperObserver.stopWatching();
1773 wallpaper.wallpaperObserver = null;
Amith Yamasani13593602012-03-22 16:16:17 -07001774 }
Amith Yamasani756901d2012-10-12 12:30:07 -07001775 }
1776 }
1777
Christopher Tatebe132e62016-02-10 12:59:49 -08001778 void stopObserversLocked(int userId) {
1779 stopObserver(mWallpaperMap.get(userId));
1780 stopObserver(mLockWallpaperMap.get(userId));
1781 mWallpaperMap.remove(userId);
1782 mLockWallpaperMap.remove(userId);
1783 }
1784
Daichi Hirono4bbf8522017-12-06 10:34:18 +09001785 @Override
1786 public void onBootPhase(int phase) {
1787 if (phase == SystemService.PHASE_ACTIVITY_MANAGER_READY) {
1788 systemReady();
1789 } else if (phase == SystemService.PHASE_THIRD_PARTY_APPS_CAN_START) {
1790 switchUser(UserHandle.USER_SYSTEM, null);
1791 }
1792 }
1793
1794 @Override
1795 public void onUnlockUser(final int userId) {
Felipe Leme4e2cbb52019-12-02 16:46:49 -08001796 TimingsTraceAndSlog t = new TimingsTraceAndSlog(TAG);
1797 t.traceBegin("on-unlock-user-" + userId);
1798 try {
1799 synchronized (mLock) {
1800 if (mCurrentUserId == userId) {
1801 if (mWaitingForUnlock) {
1802 // the desired wallpaper is not direct-boot aware, load it now
1803 final WallpaperData systemWallpaper =
1804 getWallpaperSafeLocked(userId, FLAG_SYSTEM);
1805 switchWallpaper(systemWallpaper, null);
1806 notifyCallbacksLocked(systemWallpaper);
1807 }
Christopher Tate190e8532016-07-11 11:35:34 -07001808
Felipe Leme4e2cbb52019-12-02 16:46:49 -08001809 // Make sure that the SELinux labeling of all the relevant files is correct.
1810 // This corrects for mislabeling bugs that might have arisen from move-to
1811 // operations involving the wallpaper files. This isn't timing-critical,
1812 // so we do it in the background to avoid holding up the user unlock operation.
1813 if (!mUserRestorecon.get(userId)) {
1814 mUserRestorecon.put(userId, true);
1815 Runnable relabeler = () -> {
Christopher Tate38a5dc32016-07-20 15:10:18 -07001816 final File wallpaperDir = getWallpaperDir(userId);
1817 for (String filename : sPerUserFiles) {
1818 File f = new File(wallpaperDir, filename);
1819 if (f.exists()) {
1820 SELinux.restorecon(f);
1821 }
Christopher Tate190e8532016-07-11 11:35:34 -07001822 }
Felipe Leme4e2cbb52019-12-02 16:46:49 -08001823 };
1824 BackgroundThread.getHandler().post(relabeler);
1825 }
Christopher Tate38a5dc32016-07-20 15:10:18 -07001826 }
Jeff Sharkey1cab76a2016-04-12 18:23:31 -06001827 }
Felipe Leme4e2cbb52019-12-02 16:46:49 -08001828 } finally {
1829 t.traceEnd();
Jeff Sharkey1cab76a2016-04-12 18:23:31 -06001830 }
1831 }
1832
Amith Yamasani756901d2012-10-12 12:30:07 -07001833 void onRemoveUser(int userId) {
1834 if (userId < 1) return;
Christopher Tatebe132e62016-02-10 12:59:49 -08001835
1836 final File wallpaperDir = getWallpaperDir(userId);
Amith Yamasani756901d2012-10-12 12:30:07 -07001837 synchronized (mLock) {
Christopher Tatebe132e62016-02-10 12:59:49 -08001838 stopObserversLocked(userId);
1839 for (String filename : sPerUserFiles) {
1840 new File(wallpaperDir, filename).delete();
1841 }
wilsonshiha282bf72018-11-30 12:48:05 +08001842 mUserRestorecon.delete(userId);
Amith Yamasani13593602012-03-22 16:16:17 -07001843 }
1844 }
1845
Dianne Hackborn5dc5a002012-09-15 19:33:48 -07001846 void switchUser(int userId, IRemoteCallback reply) {
Felipe Leme4e2cbb52019-12-02 16:46:49 -08001847 TimingsTraceAndSlog t = new TimingsTraceAndSlog(TAG);
1848 t.traceBegin("switch-user-" + userId);
1849 try {
1850 final WallpaperData systemWallpaper;
1851 final WallpaperData lockWallpaper;
1852 synchronized (mLock) {
1853 if (mCurrentUserId == userId) {
1854 return;
1855 }
1856 mCurrentUserId = userId;
1857 systemWallpaper = getWallpaperSafeLocked(userId, FLAG_SYSTEM);
1858 final WallpaperData tmpLockWallpaper = mLockWallpaperMap.get(userId);
1859 lockWallpaper = tmpLockWallpaper == null ? systemWallpaper : tmpLockWallpaper;
1860 // Not started watching yet, in case wallpaper data was loaded for other reasons.
1861 if (systemWallpaper.wallpaperObserver == null) {
1862 systemWallpaper.wallpaperObserver = new WallpaperObserver(systemWallpaper);
1863 systemWallpaper.wallpaperObserver.startWatching();
1864 }
1865 switchWallpaper(systemWallpaper, reply);
Jaekyun Seokad7d90f2018-04-04 01:57:18 +09001866 }
Lucas Dupin9272d452017-09-14 14:15:42 -07001867
Felipe Leme4e2cbb52019-12-02 16:46:49 -08001868 // Offload color extraction to another thread since switchUser will be called
1869 // from the main thread.
1870 FgThread.getHandler().post(() -> {
1871 notifyWallpaperColorsChanged(systemWallpaper, FLAG_SYSTEM);
1872 notifyWallpaperColorsChanged(lockWallpaper, FLAG_LOCK);
1873 notifyWallpaperColorsChanged(mFallbackWallpaper, FLAG_SYSTEM);
1874 });
1875 } finally {
1876 t.traceEnd();
1877 }
Amith Yamasani37ce3a82012-02-06 12:04:42 -08001878 }
1879
Dianne Hackborn5dc5a002012-09-15 19:33:48 -07001880 void switchWallpaper(WallpaperData wallpaper, IRemoteCallback reply) {
Dianne Hackbornf21adf62009-08-13 10:20:21 -07001881 synchronized (mLock) {
Jeff Sharkey1cab76a2016-04-12 18:23:31 -06001882 mWaitingForUnlock = false;
1883 final ComponentName cname = wallpaper.wallpaperComponent != null ?
1884 wallpaper.wallpaperComponent : wallpaper.nextWallpaperComponent;
1885 if (!bindWallpaperComponentLocked(cname, true, false, wallpaper, reply)) {
1886 // We failed to bind the desired wallpaper, but that might
1887 // happen if the wallpaper isn't direct-boot aware
1888 ServiceInfo si = null;
1889 try {
1890 si = mIPackageManager.getServiceInfo(cname,
1891 PackageManager.MATCH_DIRECT_BOOT_UNAWARE, wallpaper.userId);
1892 } catch (RemoteException ignored) {
Dianne Hackbornf21adf62009-08-13 10:20:21 -07001893 }
Jeff Sharkey1cab76a2016-04-12 18:23:31 -06001894
1895 if (si == null) {
1896 Slog.w(TAG, "Failure starting previous wallpaper; clearing");
1897 clearWallpaperLocked(false, FLAG_SYSTEM, wallpaper.userId, reply);
1898 } else {
1899 Slog.w(TAG, "Wallpaper isn't direct boot aware; using fallback until unlocked");
1900 // We might end up persisting the current wallpaper data
1901 // while locked, so pretend like the component was actually
1902 // bound into place
1903 wallpaper.wallpaperComponent = wallpaper.nextWallpaperComponent;
wilsonshih999ec102019-05-17 18:47:50 +08001904 final WallpaperData fallback =
1905 new WallpaperData(wallpaper.userId, getWallpaperDir(wallpaper.userId),
Jeff Sharkey1cab76a2016-04-12 18:23:31 -06001906 WALLPAPER_LOCK_ORIG, WALLPAPER_LOCK_CROP);
wilsonshih594497e2020-03-31 10:49:37 +08001907 ensureSaneWallpaperData(fallback);
Jeff Sharkey1cab76a2016-04-12 18:23:31 -06001908 bindWallpaperComponentLocked(mImageWallpaper, true, false, fallback, reply);
1909 mWaitingForUnlock = true;
1910 }
Dianne Hackbornf21adf62009-08-13 10:20:21 -07001911 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001912 }
1913 }
1914
Christopher Tatebe132e62016-02-10 12:59:49 -08001915 @Override
1916 public void clearWallpaper(String callingPackage, int which, int userId) {
Amith Yamasani37ce3a82012-02-06 12:04:42 -08001917 if (DEBUG) Slog.v(TAG, "clearWallpaper");
Benjamin Franzf3ece362015-02-11 10:51:10 +00001918 checkPermission(android.Manifest.permission.SET_WALLPAPER);
Christopher Tate98d609c2016-05-18 17:31:58 -07001919 if (!isWallpaperSupported(callingPackage) || !isSetWallpaperAllowed(callingPackage)) {
Benjamin Franzf3ece362015-02-11 10:51:10 +00001920 return;
1921 }
Christopher Tatee409f0e2016-03-21 14:53:15 -07001922 userId = ActivityManager.handleIncomingUser(Binder.getCallingPid(),
1923 Binder.getCallingUid(), userId, false, true, "clearWallpaper", null);
Christopher Tatebe132e62016-02-10 12:59:49 -08001924
Lucas Dupin41f69422017-05-03 15:26:22 -07001925 WallpaperData data = null;
Amith Yamasani37ce3a82012-02-06 12:04:42 -08001926 synchronized (mLock) {
Christopher Tatebe132e62016-02-10 12:59:49 -08001927 clearWallpaperLocked(false, which, userId, null);
Lucas Dupin41f69422017-05-03 15:26:22 -07001928
1929 if (which == FLAG_LOCK) {
1930 data = mLockWallpaperMap.get(userId);
1931 }
1932 if (which == FLAG_SYSTEM || data == null) {
1933 data = mWallpaperMap.get(userId);
1934 }
Amith Yamasani37ce3a82012-02-06 12:04:42 -08001935 }
Lucas Dupinea1fb1e2017-04-05 17:39:44 -07001936
1937 // When clearing a wallpaper, broadcast new valid colors
Lucas Dupin41f69422017-05-03 15:26:22 -07001938 if (data != null) {
1939 notifyWallpaperColorsChanged(data, which);
wilsonshih36597d42018-12-05 18:56:39 +08001940 notifyWallpaperColorsChanged(mFallbackWallpaper, FLAG_SYSTEM);
Lucas Dupin41f69422017-05-03 15:26:22 -07001941 }
Amith Yamasani37ce3a82012-02-06 12:04:42 -08001942 }
1943
Christopher Tatebe132e62016-02-10 12:59:49 -08001944 void clearWallpaperLocked(boolean defaultFailed, int which, int userId, IRemoteCallback reply) {
Christopher Tateedf7d042016-03-29 18:24:25 -07001945 if (which != FLAG_SYSTEM && which != FLAG_LOCK) {
Christopher Tate105540d2018-03-21 13:03:09 -07001946 throw new IllegalArgumentException("Must specify exactly one kind of wallpaper to clear");
Christopher Tatebe132e62016-02-10 12:59:49 -08001947 }
1948
1949 WallpaperData wallpaper = null;
Christopher Tateedf7d042016-03-29 18:24:25 -07001950 if (which == FLAG_LOCK) {
Christopher Tatebe132e62016-02-10 12:59:49 -08001951 wallpaper = mLockWallpaperMap.get(userId);
1952 if (wallpaper == null) {
1953 // It's already gone; we're done.
Christopher Tate79a24572016-03-02 14:42:44 -08001954 if (DEBUG) {
1955 Slog.i(TAG, "Lock wallpaper already cleared");
1956 }
Christopher Tatebe132e62016-02-10 12:59:49 -08001957 return;
1958 }
1959 } else {
1960 wallpaper = mWallpaperMap.get(userId);
1961 if (wallpaper == null) {
1962 // Might need to bring it in the first time to establish our rewrite
Christopher Tated7faf532016-02-25 12:43:38 -08001963 loadSettingsLocked(userId, false);
Christopher Tatebe132e62016-02-10 12:59:49 -08001964 wallpaper = mWallpaperMap.get(userId);
1965 }
1966 }
Benjamin Franzf3ece362015-02-11 10:51:10 +00001967 if (wallpaper == null) {
1968 return;
1969 }
Christopher Tatebe132e62016-02-10 12:59:49 -08001970
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001971 final long ident = Binder.clearCallingIdentity();
1972 try {
Christopher Tatebe132e62016-02-10 12:59:49 -08001973 if (wallpaper.wallpaperFile.exists()) {
1974 wallpaper.wallpaperFile.delete();
1975 wallpaper.cropFile.delete();
Christopher Tateedf7d042016-03-29 18:24:25 -07001976 if (which == FLAG_LOCK) {
Christopher Tate79a24572016-03-02 14:42:44 -08001977 mLockWallpaperMap.remove(userId);
Christopher Tatebe132e62016-02-10 12:59:49 -08001978 final IWallpaperManagerCallback cb = mKeyguardListener;
1979 if (cb != null) {
Christopher Tate79a24572016-03-02 14:42:44 -08001980 if (DEBUG) {
1981 Slog.i(TAG, "Notifying keyguard of lock wallpaper clear");
1982 }
Christopher Tatebe132e62016-02-10 12:59:49 -08001983 try {
1984 cb.onWallpaperChanged();
1985 } catch (RemoteException e) {
1986 // Oh well it went away; no big deal
1987 }
1988 }
Christopher Tate79a24572016-03-02 14:42:44 -08001989 saveSettingsLocked(userId);
Christopher Tatebe132e62016-02-10 12:59:49 -08001990 return;
1991 }
1992 }
1993
Christopher Tateecd827a2014-09-05 17:42:34 -07001994 RuntimeException e = null;
1995 try {
Lucas Dupin75ec3792017-06-29 14:07:18 -07001996 wallpaper.primaryColors = null;
Christopher Tateecd827a2014-09-05 17:42:34 -07001997 wallpaper.imageWallpaperPending = false;
1998 if (userId != mCurrentUserId) return;
1999 if (bindWallpaperComponentLocked(defaultFailed
2000 ? mImageWallpaper
2001 : null, true, false, wallpaper, reply)) {
2002 return;
2003 }
2004 } catch (IllegalArgumentException e1) {
2005 e = e1;
Dianne Hackborn80b902f2011-09-15 15:15:27 -07002006 }
Christopher Tateecd827a2014-09-05 17:42:34 -07002007
2008 // This can happen if the default wallpaper component doesn't
2009 // exist. This should be a system configuration problem, but
2010 // let's not let it crash the system and just live with no
2011 // wallpaper.
2012 Slog.e(TAG, "Default wallpaper component not found!", e);
2013 clearWallpaperComponentLocked(wallpaper);
2014 if (reply != null) {
2015 try {
2016 reply.sendResult(null);
2017 } catch (RemoteException e1) {
2018 }
2019 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002020 } finally {
2021 Binder.restoreCallingIdentity(ident);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002022 }
Dianne Hackborn5dc5a002012-09-15 19:33:48 -07002023 }
2024
2025 public boolean hasNamedWallpaper(String name) {
2026 synchronized (mLock) {
Amith Yamasani6474c4c2012-10-04 14:55:42 -07002027 List<UserInfo> users;
2028 long ident = Binder.clearCallingIdentity();
2029 try {
2030 users = ((UserManager) mContext.getSystemService(Context.USER_SERVICE)).getUsers();
2031 } finally {
2032 Binder.restoreCallingIdentity(ident);
2033 }
2034 for (UserInfo user: users) {
Benjamin Franzf3ece362015-02-11 10:51:10 +00002035 // ignore managed profiles
2036 if (user.isManagedProfile()) {
2037 continue;
2038 }
Amith Yamasani6474c4c2012-10-04 14:55:42 -07002039 WallpaperData wd = mWallpaperMap.get(user.id);
2040 if (wd == null) {
2041 // User hasn't started yet, so load her settings to peek at the wallpaper
Christopher Tated7faf532016-02-25 12:43:38 -08002042 loadSettingsLocked(user.id, false);
Amith Yamasani6474c4c2012-10-04 14:55:42 -07002043 wd = mWallpaperMap.get(user.id);
2044 }
2045 if (wd != null && name.equals(wd.name)) {
Dianne Hackborn5dc5a002012-09-15 19:33:48 -07002046 return true;
2047 }
2048 }
2049 }
2050 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002051 }
2052
wilsonshih81e10a72018-11-15 10:54:21 +08002053 private boolean isValidDisplay(int displayId) {
2054 return mDisplayManager.getDisplay(displayId) != null;
2055 }
2056
2057 /**
2058 * Sets the dimension hint for the wallpaper. These hints indicate the desired
2059 * minimum width and height for the wallpaper in a particular display.
2060 */
2061 public void setDimensionHints(int width, int height, String callingPackage, int displayId)
Benjamin Franzf3ece362015-02-11 10:51:10 +00002062 throws RemoteException {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002063 checkPermission(android.Manifest.permission.SET_WALLPAPER_HINTS);
Benjamin Franzf3ece362015-02-11 10:51:10 +00002064 if (!isWallpaperSupported(callingPackage)) {
2065 return;
2066 }
Ahan Wu32c6a7c2019-10-03 12:36:59 +08002067
2068 // Make sure both width and height are not larger than max texture size.
2069 width = Math.min(width, GLHelper.getMaxTextureSize());
2070 height = Math.min(height, GLHelper.getMaxTextureSize());
2071
Joe Onorato9bb8fd72009-07-28 18:24:51 -07002072 synchronized (mLock) {
Dianne Hackborn5dc5a002012-09-15 19:33:48 -07002073 int userId = UserHandle.getCallingUserId();
Christopher Tateedf7d042016-03-29 18:24:25 -07002074 WallpaperData wallpaper = getWallpaperSafeLocked(userId, FLAG_SYSTEM);
Dianne Hackborn5dc5a002012-09-15 19:33:48 -07002075 if (width <= 0 || height <= 0) {
2076 throw new IllegalArgumentException("width and height must be > 0");
2077 }
2078
wilsonshih81e10a72018-11-15 10:54:21 +08002079 if (!isValidDisplay(displayId)) {
2080 throw new IllegalArgumentException("Cannot find display with id=" + displayId);
2081 }
2082
wilsonshiha282bf72018-11-30 12:48:05 +08002083 final DisplayData wpdData = getDisplayDataOrCreate(displayId);
wilsonshih81e10a72018-11-15 10:54:21 +08002084 if (width != wpdData.mWidth || height != wpdData.mHeight) {
2085 wpdData.mWidth = width;
2086 wpdData.mHeight = height;
2087 if (displayId == DEFAULT_DISPLAY) saveSettingsLocked(userId);
Amith Yamasani37ce3a82012-02-06 12:04:42 -08002088 if (mCurrentUserId != userId) return; // Don't change the properties now
2089 if (wallpaper.connection != null) {
wilsonshih81e10a72018-11-15 10:54:21 +08002090 final WallpaperConnection.DisplayConnector connector = wallpaper.connection
2091 .getDisplayConnectorOrCreate(displayId);
2092 final IWallpaperEngine engine = connector != null ? connector.mEngine : null;
wilsonshihde93f492018-11-01 21:23:40 +08002093 if (engine != null) {
Dianne Hackborn284ac932009-08-28 10:34:25 -07002094 try {
wilsonshihde93f492018-11-01 21:23:40 +08002095 engine.setDesiredSize(width, height);
Dianne Hackborn284ac932009-08-28 10:34:25 -07002096 } catch (RemoteException e) {
2097 }
Amith Yamasani37ce3a82012-02-06 12:04:42 -08002098 notifyCallbacksLocked(wallpaper);
wilsonshih81e10a72018-11-15 10:54:21 +08002099 } else if (wallpaper.connection.mService != null && connector != null) {
Michael Wright5203a8b2013-10-03 14:16:42 -07002100 // We've attached to the service but the engine hasn't attached back to us
2101 // yet. This means it will be created with the previous dimensions, so we
2102 // need to update it to the new dimensions once it attaches.
wilsonshih81e10a72018-11-15 10:54:21 +08002103 connector.mDimensionsChanged = true;
Dianne Hackborn284ac932009-08-28 10:34:25 -07002104 }
2105 }
Joe Onorato9bb8fd72009-07-28 18:24:51 -07002106 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002107 }
2108 }
2109
wilsonshih81e10a72018-11-15 10:54:21 +08002110 /**
2111 * Returns the desired minimum width for the wallpaper in a particular display.
2112 */
2113 public int getWidthHint(int displayId) throws RemoteException {
Joe Onorato9bb8fd72009-07-28 18:24:51 -07002114 synchronized (mLock) {
wilsonshih81e10a72018-11-15 10:54:21 +08002115 if (!isValidDisplay(displayId)) {
2116 throw new IllegalArgumentException("Cannot find display with id=" + displayId);
2117 }
Dianne Hackbornf02b60a2012-08-16 10:48:27 -07002118 WallpaperData wallpaper = mWallpaperMap.get(UserHandle.getCallingUserId());
Benjamin Franzf3ece362015-02-11 10:51:10 +00002119 if (wallpaper != null) {
wilsonshiha282bf72018-11-30 12:48:05 +08002120 final DisplayData wpdData = getDisplayDataOrCreate(displayId);
wilsonshih81e10a72018-11-15 10:54:21 +08002121 return wpdData.mWidth;
Benjamin Franzf3ece362015-02-11 10:51:10 +00002122 } else {
2123 return 0;
2124 }
Joe Onorato9bb8fd72009-07-28 18:24:51 -07002125 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002126 }
2127
wilsonshih81e10a72018-11-15 10:54:21 +08002128 /**
2129 * Returns the desired minimum height for the wallpaper in a particular display.
2130 */
2131 public int getHeightHint(int displayId) throws RemoteException {
Joe Onorato9bb8fd72009-07-28 18:24:51 -07002132 synchronized (mLock) {
wilsonshih81e10a72018-11-15 10:54:21 +08002133 if (!isValidDisplay(displayId)) {
2134 throw new IllegalArgumentException("Cannot find display with id=" + displayId);
2135 }
Dianne Hackbornf02b60a2012-08-16 10:48:27 -07002136 WallpaperData wallpaper = mWallpaperMap.get(UserHandle.getCallingUserId());
Benjamin Franzf3ece362015-02-11 10:51:10 +00002137 if (wallpaper != null) {
wilsonshiha282bf72018-11-30 12:48:05 +08002138 final DisplayData wpdData = getDisplayDataOrCreate(displayId);
wilsonshih81e10a72018-11-15 10:54:21 +08002139 return wpdData.mHeight;
Benjamin Franzf3ece362015-02-11 10:51:10 +00002140 } else {
2141 return 0;
2142 }
Joe Onorato9bb8fd72009-07-28 18:24:51 -07002143 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002144 }
2145
wilsonshih81e10a72018-11-15 10:54:21 +08002146 /**
2147 * Sets extra padding that we would like the wallpaper to have outside of the display.
2148 */
2149 public void setDisplayPadding(Rect padding, String callingPackage, int displayId) {
Dianne Hackborn067e5f62014-09-07 23:14:30 -07002150 checkPermission(android.Manifest.permission.SET_WALLPAPER_HINTS);
Benjamin Franzf3ece362015-02-11 10:51:10 +00002151 if (!isWallpaperSupported(callingPackage)) {
2152 return;
2153 }
Dianne Hackborn067e5f62014-09-07 23:14:30 -07002154 synchronized (mLock) {
wilsonshih81e10a72018-11-15 10:54:21 +08002155 if (!isValidDisplay(displayId)) {
2156 throw new IllegalArgumentException("Cannot find display with id=" + displayId);
2157 }
Dianne Hackborn067e5f62014-09-07 23:14:30 -07002158 int userId = UserHandle.getCallingUserId();
Christopher Tateedf7d042016-03-29 18:24:25 -07002159 WallpaperData wallpaper = getWallpaperSafeLocked(userId, FLAG_SYSTEM);
Dianne Hackborn067e5f62014-09-07 23:14:30 -07002160 if (padding.left < 0 || padding.top < 0 || padding.right < 0 || padding.bottom < 0) {
2161 throw new IllegalArgumentException("padding must be positive: " + padding);
2162 }
2163
wilsonshiha282bf72018-11-30 12:48:05 +08002164 final DisplayData wpdData = getDisplayDataOrCreate(displayId);
wilsonshih81e10a72018-11-15 10:54:21 +08002165 if (!padding.equals(wpdData.mPadding)) {
2166 wpdData.mPadding.set(padding);
2167 if (displayId == DEFAULT_DISPLAY) saveSettingsLocked(userId);
Dianne Hackborn067e5f62014-09-07 23:14:30 -07002168 if (mCurrentUserId != userId) return; // Don't change the properties now
2169 if (wallpaper.connection != null) {
wilsonshih81e10a72018-11-15 10:54:21 +08002170 final WallpaperConnection.DisplayConnector connector = wallpaper.connection
2171 .getDisplayConnectorOrCreate(displayId);
2172 final IWallpaperEngine engine = connector != null ? connector.mEngine : null;
wilsonshihde93f492018-11-01 21:23:40 +08002173 if (engine != null) {
Dianne Hackborn067e5f62014-09-07 23:14:30 -07002174 try {
wilsonshihde93f492018-11-01 21:23:40 +08002175 engine.setDisplayPadding(padding);
Dianne Hackborn067e5f62014-09-07 23:14:30 -07002176 } catch (RemoteException e) {
2177 }
2178 notifyCallbacksLocked(wallpaper);
wilsonshih81e10a72018-11-15 10:54:21 +08002179 } else if (wallpaper.connection.mService != null && connector != null) {
Dianne Hackborn067e5f62014-09-07 23:14:30 -07002180 // We've attached to the service but the engine hasn't attached back to us
2181 // yet. This means it will be created with the previous dimensions, so we
2182 // need to update it to the new dimensions once it attaches.
wilsonshih81e10a72018-11-15 10:54:21 +08002183 connector.mPaddingChanged = true;
Dianne Hackborn067e5f62014-09-07 23:14:30 -07002184 }
2185 }
2186 }
2187 }
2188 }
2189
Philip P. Moltmann41ff0f62019-11-15 10:55:52 -08002190 @Deprecated
Yorke Leedcd93cc2016-01-08 14:12:55 -08002191 @Override
Christopher Tate93252de2017-06-15 14:48:41 -07002192 public ParcelFileDescriptor getWallpaper(String callingPkg, IWallpaperManagerCallback cb,
2193 final int which, Bundle outParams, int wallpaperUserId) {
Philip P. Moltmann41ff0f62019-11-15 10:55:52 -08002194 return getWallpaperWithFeature(callingPkg, null, cb, which, outParams, wallpaperUserId);
2195 }
2196
2197 @Override
2198 public ParcelFileDescriptor getWallpaperWithFeature(String callingPkg, String callingFeatureId,
2199 IWallpaperManagerCallback cb, final int which, Bundle outParams, int wallpaperUserId) {
Christopher Tate8a71c482017-08-14 16:45:03 -07002200 final int hasPrivilege = mContext.checkCallingOrSelfPermission(
2201 android.Manifest.permission.READ_WALLPAPER_INTERNAL);
2202 if (hasPrivilege != PackageManager.PERMISSION_GRANTED) {
Jeff Sharkey859856d2019-03-25 11:44:11 -06002203 mContext.getSystemService(StorageManager.class).checkPermissionReadImages(true,
Philip P. Moltmann41ff0f62019-11-15 10:55:52 -08002204 Binder.getCallingPid(), Binder.getCallingUid(), callingPkg, callingFeatureId);
Christopher Tate8a71c482017-08-14 16:45:03 -07002205 }
Christopher Tate93252de2017-06-15 14:48:41 -07002206
Christopher Tatee409f0e2016-03-21 14:53:15 -07002207 wallpaperUserId = ActivityManager.handleIncomingUser(Binder.getCallingPid(),
2208 Binder.getCallingUid(), wallpaperUserId, false, true, "getWallpaper", null);
Christopher Tatebe132e62016-02-10 12:59:49 -08002209
Christopher Tateedf7d042016-03-29 18:24:25 -07002210 if (which != FLAG_SYSTEM && which != FLAG_LOCK) {
Christopher Tatebe132e62016-02-10 12:59:49 -08002211 throw new IllegalArgumentException("Must specify exactly one kind of wallpaper to read");
2212 }
2213
Joe Onorato9bb8fd72009-07-28 18:24:51 -07002214 synchronized (mLock) {
Vadim Tryshev8cde0792016-02-19 17:02:15 -08002215 final SparseArray<WallpaperData> whichSet =
Christopher Tateedf7d042016-03-29 18:24:25 -07002216 (which == FLAG_LOCK) ? mLockWallpaperMap : mWallpaperMap;
Vadim Tryshev8cde0792016-02-19 17:02:15 -08002217 WallpaperData wallpaper = whichSet.get(wallpaperUserId);
2218 if (wallpaper == null) {
Christopher Tatef717b932017-09-11 15:52:54 -07002219 // There is no established wallpaper imagery of this type (expected
2220 // only for lock wallpapers; a system WallpaperData is established at
2221 // user switch)
2222 return null;
Benjamin Franzf3ece362015-02-11 10:51:10 +00002223 }
wilsonshih81e10a72018-11-15 10:54:21 +08002224 // Only for default display.
wilsonshiha282bf72018-11-30 12:48:05 +08002225 final DisplayData wpdData = getDisplayDataOrCreate(DEFAULT_DISPLAY);
Joe Onorato9bb8fd72009-07-28 18:24:51 -07002226 try {
Dianne Hackborn284ac932009-08-28 10:34:25 -07002227 if (outParams != null) {
wilsonshih81e10a72018-11-15 10:54:21 +08002228 outParams.putInt("width", wpdData.mWidth);
2229 outParams.putInt("height", wpdData.mHeight);
Dianne Hackborn284ac932009-08-28 10:34:25 -07002230 }
Christopher Tateea6724a2016-02-18 18:39:19 -08002231 if (cb != null) {
2232 wallpaper.callbacks.register(cb);
2233 }
Christopher Tate1e1e2e02016-01-25 15:34:36 -08002234 if (!wallpaper.cropFile.exists()) {
Joe Onorato9bb8fd72009-07-28 18:24:51 -07002235 return null;
2236 }
Christopher Tate1e1e2e02016-01-25 15:34:36 -08002237 return ParcelFileDescriptor.open(wallpaper.cropFile, MODE_READ_ONLY);
Joe Onorato9bb8fd72009-07-28 18:24:51 -07002238 } catch (FileNotFoundException e) {
2239 /* Shouldn't happen as we check to see if the file exists */
Joe Onorato8a9b2202010-02-26 18:56:32 -08002240 Slog.w(TAG, "Error getting wallpaper", e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002241 }
Joe Onorato9bb8fd72009-07-28 18:24:51 -07002242 return null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002243 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002244 }
2245
Christopher Tatee409f0e2016-03-21 14:53:15 -07002246 @Override
Jorim Jaggie31f6b82016-07-01 16:15:09 -07002247 public WallpaperInfo getWallpaperInfo(int userId) {
2248 userId = ActivityManager.handleIncomingUser(Binder.getCallingPid(),
Christopher Tatef717b932017-09-11 15:52:54 -07002249 Binder.getCallingUid(), userId, false, true, "getWallpaperInfo", null);
Dianne Hackborneb034652009-09-07 00:49:58 -07002250 synchronized (mLock) {
Amith Yamasani37ce3a82012-02-06 12:04:42 -08002251 WallpaperData wallpaper = mWallpaperMap.get(userId);
Benjamin Franzf3ece362015-02-11 10:51:10 +00002252 if (wallpaper != null && wallpaper.connection != null) {
Amith Yamasani37ce3a82012-02-06 12:04:42 -08002253 return wallpaper.connection.mInfo;
Dianne Hackborneb034652009-09-07 00:49:58 -07002254 }
2255 return null;
2256 }
2257 }
Amith Yamasani37ce3a82012-02-06 12:04:42 -08002258
Christopher Tatead3c2592016-01-20 18:13:17 -08002259 @Override
Christopher Tatee409f0e2016-03-21 14:53:15 -07002260 public int getWallpaperIdForUser(int which, int userId) {
2261 userId = ActivityManager.handleIncomingUser(Binder.getCallingPid(),
2262 Binder.getCallingUid(), userId, false, true, "getWallpaperIdForUser", null);
2263
Christopher Tateedf7d042016-03-29 18:24:25 -07002264 if (which != FLAG_SYSTEM && which != FLAG_LOCK) {
Christopher Tatee409f0e2016-03-21 14:53:15 -07002265 throw new IllegalArgumentException("Must specify exactly one kind of wallpaper");
2266 }
2267
2268 final SparseArray<WallpaperData> map =
Christopher Tateedf7d042016-03-29 18:24:25 -07002269 (which == FLAG_LOCK) ? mLockWallpaperMap : mWallpaperMap;
Christopher Tatee409f0e2016-03-21 14:53:15 -07002270 synchronized (mLock) {
2271 WallpaperData wallpaper = map.get(userId);
2272 if (wallpaper != null) {
2273 return wallpaper.wallpaperId;
2274 }
2275 }
2276 return -1;
2277 }
2278
2279 @Override
wilsonshih36597d42018-12-05 18:56:39 +08002280 public void registerWallpaperColorsCallback(IWallpaperManagerCallback cb, int userId,
2281 int displayId) {
Lucas Dupin50ba9912017-07-14 11:55:05 -07002282 userId = ActivityManager.handleIncomingUser(Binder.getCallingPid(), Binder.getCallingUid(),
2283 userId, true, true, "registerWallpaperColorsCallback", null);
Lucas Dupinea1fb1e2017-04-05 17:39:44 -07002284 synchronized (mLock) {
wilsonshih36597d42018-12-05 18:56:39 +08002285 SparseArray<RemoteCallbackList<IWallpaperManagerCallback>>
2286 userDisplayColorsChangedListeners = mColorsChangedListeners.get(userId);
2287 if (userDisplayColorsChangedListeners == null) {
2288 userDisplayColorsChangedListeners = new SparseArray<>();
2289 mColorsChangedListeners.put(userId, userDisplayColorsChangedListeners);
Lucas Dupin50ba9912017-07-14 11:55:05 -07002290 }
wilsonshih36597d42018-12-05 18:56:39 +08002291 RemoteCallbackList<IWallpaperManagerCallback> displayChangedListeners =
2292 userDisplayColorsChangedListeners.get(displayId);
2293 if (displayChangedListeners == null) {
2294 displayChangedListeners = new RemoteCallbackList<>();
2295 userDisplayColorsChangedListeners.put(displayId, displayChangedListeners);
2296 }
2297 displayChangedListeners.register(cb);
Lucas Dupinea1fb1e2017-04-05 17:39:44 -07002298 }
2299 }
2300
2301 @Override
wilsonshih36597d42018-12-05 18:56:39 +08002302 public void unregisterWallpaperColorsCallback(IWallpaperManagerCallback cb, int userId,
2303 int displayId) {
Lucas Dupin50ba9912017-07-14 11:55:05 -07002304 userId = ActivityManager.handleIncomingUser(Binder.getCallingPid(), Binder.getCallingUid(),
2305 userId, true, true, "unregisterWallpaperColorsCallback", null);
Lucas Dupinea1fb1e2017-04-05 17:39:44 -07002306 synchronized (mLock) {
wilsonshih36597d42018-12-05 18:56:39 +08002307 SparseArray<RemoteCallbackList<IWallpaperManagerCallback>>
2308 userDisplayColorsChangedListeners = mColorsChangedListeners.get(userId);
2309 if (userDisplayColorsChangedListeners != null) {
2310 RemoteCallbackList<IWallpaperManagerCallback> displayChangedListeners =
2311 userDisplayColorsChangedListeners.get(displayId);
2312 if (displayChangedListeners != null) {
2313 displayChangedListeners.unregister(cb);
2314 }
Lucas Dupin50ba9912017-07-14 11:55:05 -07002315 }
Lucas Dupinea1fb1e2017-04-05 17:39:44 -07002316 }
2317 }
2318
Lucas Dupin4c8c3272018-11-06 17:47:48 -08002319 /**
wilsonshih36597d42018-12-05 18:56:39 +08002320 * TODO(multi-display) Extends this method with specific display.
Lucas Dupin4c8c3272018-11-06 17:47:48 -08002321 * Propagate ambient state to wallpaper engine.
2322 *
2323 * @param inAmbientMode {@code true} when in ambient mode, {@code false} otherwise.
2324 * @param animationDuration Duration of the animation, or 0 when immediate.
2325 */
2326 public void setInAmbientMode(boolean inAmbientMode, long animationDuration) {
Lucas Dupin7517b5d2017-08-22 12:51:25 -07002327 final IWallpaperEngine engine;
2328 synchronized (mLock) {
Lucas Dupin4c8c3272018-11-06 17:47:48 -08002329 mInAmbientMode = inAmbientMode;
Lucas Dupin7517b5d2017-08-22 12:51:25 -07002330 final WallpaperData data = mWallpaperMap.get(mCurrentUserId);
Ahan Wu723a80e2018-11-07 20:39:32 +08002331 // The wallpaper info is null for image wallpaper, also use the engine in this case.
Ahan Wu67e7f102019-01-14 20:38:14 +08002332 if (data != null && data.connection != null && (data.connection.mInfo == null
2333 || data.connection.mInfo.supportsAmbientMode())) {
wilsonshih36597d42018-12-05 18:56:39 +08002334 // TODO(multi-display) Extends this method with specific display.
wilsonshihde93f492018-11-01 21:23:40 +08002335 engine = data.connection.getDisplayConnectorOrCreate(DEFAULT_DISPLAY).mEngine;
Lucas Dupin7517b5d2017-08-22 12:51:25 -07002336 } else {
2337 engine = null;
2338 }
2339 }
2340
2341 if (engine != null) {
2342 try {
Lucas Dupin4c8c3272018-11-06 17:47:48 -08002343 engine.setInAmbientMode(inAmbientMode, animationDuration);
Lucas Dupin7517b5d2017-08-22 12:51:25 -07002344 } catch (RemoteException e) {
2345 // Cannot talk to wallpaper engine.
2346 }
2347 }
2348 }
2349
Lucas Dupinea1fb1e2017-04-05 17:39:44 -07002350 @Override
Christopher Tatebe132e62016-02-10 12:59:49 -08002351 public boolean setLockWallpaperCallback(IWallpaperManagerCallback cb) {
2352 checkPermission(android.Manifest.permission.INTERNAL_SYSTEM_WINDOW);
2353 synchronized (mLock) {
2354 mKeyguardListener = cb;
2355 }
2356 return true;
2357 }
2358
2359 @Override
wilsonshih36597d42018-12-05 18:56:39 +08002360 public WallpaperColors getWallpaperColors(int which, int userId, int displayId)
2361 throws RemoteException {
Lucas Dupinea1fb1e2017-04-05 17:39:44 -07002362 if (which != FLAG_LOCK && which != FLAG_SYSTEM) {
2363 throw new IllegalArgumentException("which should be either FLAG_LOCK or FLAG_SYSTEM");
2364 }
Lucas Dupin50ba9912017-07-14 11:55:05 -07002365 userId = ActivityManager.handleIncomingUser(Binder.getCallingPid(), Binder.getCallingUid(),
2366 userId, false, true, "getWallpaperColors", null);
Lucas Dupinea1fb1e2017-04-05 17:39:44 -07002367
2368 WallpaperData wallpaperData = null;
2369 boolean shouldExtract;
2370
2371 synchronized (mLock) {
2372 if (which == FLAG_LOCK) {
Lucas Dupin50ba9912017-07-14 11:55:05 -07002373 wallpaperData = mLockWallpaperMap.get(userId);
Lucas Dupinea1fb1e2017-04-05 17:39:44 -07002374 }
2375
2376 // Try to get the system wallpaper anyway since it might
2377 // also be the lock screen wallpaper
2378 if (wallpaperData == null) {
wilsonshih36597d42018-12-05 18:56:39 +08002379 wallpaperData = findWallpaperAtDisplay(userId, displayId);
Lucas Dupinea1fb1e2017-04-05 17:39:44 -07002380 }
2381
2382 if (wallpaperData == null) {
2383 return null;
2384 }
2385 shouldExtract = wallpaperData.primaryColors == null;
2386 }
2387
2388 if (shouldExtract) {
2389 extractColors(wallpaperData);
2390 }
2391
2392 synchronized (mLock) {
Lucas Dupin4b4c5302018-06-24 18:22:10 -07002393 return wallpaperData.primaryColors;
Lucas Dupinea1fb1e2017-04-05 17:39:44 -07002394 }
2395 }
2396
wilsonshih36597d42018-12-05 18:56:39 +08002397 private WallpaperData findWallpaperAtDisplay(int userId, int displayId) {
2398 if (mFallbackWallpaper != null && mFallbackWallpaper.connection != null
2399 && mFallbackWallpaper.connection.containsDisplay(displayId)) {
2400 return mFallbackWallpaper;
2401 } else {
2402 return mWallpaperMap.get(userId);
2403 }
2404 }
2405
Lucas Dupinea1fb1e2017-04-05 17:39:44 -07002406 @Override
Christopher Tate1e1e2e02016-01-25 15:34:36 -08002407 public ParcelFileDescriptor setWallpaper(String name, String callingPackage,
Christopher Tated7faf532016-02-25 12:43:38 -08002408 Rect cropHint, boolean allowBackup, Bundle extras, int which,
Jorim Jaggi6c902d02016-08-18 10:44:54 -07002409 IWallpaperManagerCallback completion, int userId) {
2410 userId = ActivityManager.handleIncomingUser(getCallingPid(), getCallingUid(), userId,
2411 false /* all */, true /* full */, "changing wallpaper", null /* pkg */);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002412 checkPermission(android.Manifest.permission.SET_WALLPAPER);
Christopher Tatead3c2592016-01-20 18:13:17 -08002413
Christopher Tateedf7d042016-03-29 18:24:25 -07002414 if ((which & (FLAG_LOCK|FLAG_SYSTEM)) == 0) {
Christopher Tate98d609c2016-05-18 17:31:58 -07002415 final String msg = "Must specify a valid wallpaper category to set";
2416 Slog.e(TAG, msg);
2417 throw new IllegalArgumentException(msg);
Christopher Tatead3c2592016-01-20 18:13:17 -08002418 }
2419
Christopher Tate98d609c2016-05-18 17:31:58 -07002420 if (!isWallpaperSupported(callingPackage) || !isSetWallpaperAllowed(callingPackage)) {
Benjamin Franzf3ece362015-02-11 10:51:10 +00002421 return null;
2422 }
Christopher Tatead3c2592016-01-20 18:13:17 -08002423
Christopher Tate1e1e2e02016-01-25 15:34:36 -08002424 // "null" means the no-op crop, preserving the full input image
2425 if (cropHint == null) {
2426 cropHint = new Rect(0, 0, 0, 0);
2427 } else {
wilsonshih594497e2020-03-31 10:49:37 +08002428 if (cropHint.width() < 0 || cropHint.height() < 0
Christopher Tate1e1e2e02016-01-25 15:34:36 -08002429 || cropHint.left < 0
2430 || cropHint.top < 0) {
Christopher Tate98d609c2016-05-18 17:31:58 -07002431 throw new IllegalArgumentException("Invalid crop rect supplied: " + cropHint);
Christopher Tate1e1e2e02016-01-25 15:34:36 -08002432 }
2433 }
2434
Joe Onorato9bb8fd72009-07-28 18:24:51 -07002435 synchronized (mLock) {
Christopher Tatebe132e62016-02-10 12:59:49 -08002436 if (DEBUG) Slog.v(TAG, "setWallpaper which=0x" + Integer.toHexString(which));
2437 WallpaperData wallpaper;
2438
Christopher Tate8347b632016-04-29 18:59:18 -07002439 /* If we're setting system but not lock, and lock is currently sharing the system
2440 * wallpaper, we need to migrate that image over to being lock-only before
2441 * the caller here writes new bitmap data.
2442 */
2443 if (which == FLAG_SYSTEM && mLockWallpaperMap.get(userId) == null) {
Ruslan Tkhakokhov3929da02020-03-25 11:35:30 +00002444 Slog.i(TAG, "Migrating current wallpaper to be lock-only before"
2445 + "updating system wallpaper");
Christopher Tate8347b632016-04-29 18:59:18 -07002446 migrateSystemToLockWallpaperLocked(userId);
2447 }
2448
Christopher Tatebe132e62016-02-10 12:59:49 -08002449 wallpaper = getWallpaperSafeLocked(userId, which);
Dianne Hackborn0cd48872009-08-13 18:51:59 -07002450 final long ident = Binder.clearCallingIdentity();
2451 try {
Christopher Tatead3c2592016-01-20 18:13:17 -08002452 ParcelFileDescriptor pfd = updateWallpaperBitmapLocked(name, wallpaper, extras);
Dianne Hackborn0cd48872009-08-13 18:51:59 -07002453 if (pfd != null) {
Amith Yamasani37ce3a82012-02-06 12:04:42 -08002454 wallpaper.imageWallpaperPending = true;
Christopher Tatebe132e62016-02-10 12:59:49 -08002455 wallpaper.whichPending = which;
Christopher Tate1e1e2e02016-01-25 15:34:36 -08002456 wallpaper.setComplete = completion;
2457 wallpaper.cropHint.set(cropHint);
Christopher Tatec613c632016-08-12 14:13:02 -07002458 wallpaper.allowBackup = allowBackup;
Dianne Hackborn0cd48872009-08-13 18:51:59 -07002459 }
2460 return pfd;
2461 } finally {
2462 Binder.restoreCallingIdentity(ident);
Joe Onorato9bb8fd72009-07-28 18:24:51 -07002463 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002464 }
2465 }
2466
Christopher Tate8347b632016-04-29 18:59:18 -07002467 private void migrateSystemToLockWallpaperLocked(int userId) {
2468 WallpaperData sysWP = mWallpaperMap.get(userId);
2469 if (sysWP == null) {
2470 if (DEBUG) {
2471 Slog.i(TAG, "No system wallpaper? Not tracking for lock-only");
2472 }
2473 return;
2474 }
2475
2476 // We know a-priori that there is no lock-only wallpaper currently
wilsonshih999ec102019-05-17 18:47:50 +08002477 WallpaperData lockWP = new WallpaperData(userId, getWallpaperDir(userId),
Christopher Tate8347b632016-04-29 18:59:18 -07002478 WALLPAPER_LOCK_ORIG, WALLPAPER_LOCK_CROP);
2479 lockWP.wallpaperId = sysWP.wallpaperId;
2480 lockWP.cropHint.set(sysWP.cropHint);
Christopher Tateedd8dc82016-10-12 15:17:58 -07002481 lockWP.allowBackup = sysWP.allowBackup;
Lucas Dupinea1fb1e2017-04-05 17:39:44 -07002482 lockWP.primaryColors = sysWP.primaryColors;
Christopher Tate8347b632016-04-29 18:59:18 -07002483
2484 // Migrate the bitmap files outright; no need to copy
2485 try {
2486 Os.rename(sysWP.wallpaperFile.getAbsolutePath(), lockWP.wallpaperFile.getAbsolutePath());
2487 Os.rename(sysWP.cropFile.getAbsolutePath(), lockWP.cropFile.getAbsolutePath());
2488 } catch (ErrnoException e) {
2489 Slog.e(TAG, "Can't migrate system wallpaper: " + e.getMessage());
2490 lockWP.wallpaperFile.delete();
2491 lockWP.cropFile.delete();
2492 return;
2493 }
2494
2495 mLockWallpaperMap.put(userId, lockWP);
2496 }
2497
Christopher Tatead3c2592016-01-20 18:13:17 -08002498 ParcelFileDescriptor updateWallpaperBitmapLocked(String name, WallpaperData wallpaper,
2499 Bundle extras) {
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07002500 if (name == null) name = "";
2501 try {
Amith Yamasani37ce3a82012-02-06 12:04:42 -08002502 File dir = getWallpaperDir(wallpaper.userId);
2503 if (!dir.exists()) {
2504 dir.mkdir();
Dianne Hackbornebac48c2011-11-29 18:01:50 -08002505 FileUtils.setPermissions(
Amith Yamasani37ce3a82012-02-06 12:04:42 -08002506 dir.getPath(),
Dianne Hackbornebac48c2011-11-29 18:01:50 -08002507 FileUtils.S_IRWXU|FileUtils.S_IRWXG|FileUtils.S_IXOTH,
2508 -1, -1);
2509 }
Christopher Tatebe132e62016-02-10 12:59:49 -08002510 ParcelFileDescriptor fd = ParcelFileDescriptor.open(wallpaper.wallpaperFile,
Christopher Tate90f86ba2014-09-11 12:37:19 -07002511 MODE_CREATE|MODE_READ_WRITE|MODE_TRUNCATE);
Christopher Tatebe132e62016-02-10 12:59:49 -08002512 if (!SELinux.restorecon(wallpaper.wallpaperFile)) {
Ruslan Tkhakokhov3929da02020-03-25 11:35:30 +00002513 Slog.w(TAG, "restorecon failed for wallpaper file: " +
2514 wallpaper.wallpaperFile.getPath());
rpcraig554cb0c2012-07-05 06:41:43 -04002515 return null;
2516 }
Amith Yamasani37ce3a82012-02-06 12:04:42 -08002517 wallpaper.name = name;
Christopher Tatead3c2592016-01-20 18:13:17 -08002518 wallpaper.wallpaperId = makeWallpaperIdLocked();
2519 if (extras != null) {
2520 extras.putInt(WallpaperManager.EXTRA_NEW_WALLPAPER_ID, wallpaper.wallpaperId);
2521 }
Lucas Dupinea1fb1e2017-04-05 17:39:44 -07002522 // Nullify field to require new computation
2523 wallpaper.primaryColors = null;
Ruslan Tkhakokhov3929da02020-03-25 11:35:30 +00002524 Slog.v(TAG, "updateWallpaperBitmapLocked() : id=" + wallpaper.wallpaperId
2525 + " name=" + name + " file=" + wallpaper.wallpaperFile.getName());
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07002526 return fd;
2527 } catch (FileNotFoundException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002528 Slog.w(TAG, "Error setting wallpaper", e);
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07002529 }
2530 return null;
2531 }
2532
Christopher Tated57d17c2016-03-25 13:41:46 -07002533 @Override
Adrian Roos40ea0832016-07-14 14:19:55 -07002534 public void setWallpaperComponentChecked(ComponentName name, String callingPackage,
2535 int userId) {
2536
Christopher Tate98d609c2016-05-18 17:31:58 -07002537 if (isWallpaperSupported(callingPackage) && isSetWallpaperAllowed(callingPackage)) {
Adrian Roos40ea0832016-07-14 14:19:55 -07002538 setWallpaperComponent(name, userId);
Benjamin Franzf3ece362015-02-11 10:51:10 +00002539 }
2540 }
2541
2542 // ToDo: Remove this version of the function
Christopher Tated57d17c2016-03-25 13:41:46 -07002543 @Override
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07002544 public void setWallpaperComponent(ComponentName name) {
Adrian Roos40ea0832016-07-14 14:19:55 -07002545 setWallpaperComponent(name, UserHandle.getCallingUserId());
2546 }
2547
2548 private void setWallpaperComponent(ComponentName name, int userId) {
2549 userId = ActivityManager.handleIncomingUser(getCallingPid(), getCallingUid(), userId,
2550 false /* all */, true /* full */, "changing live wallpaper", null /* pkg */);
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07002551 checkPermission(android.Manifest.permission.SET_WALLPAPER_COMPONENT);
Adrian Roos40ea0832016-07-14 14:19:55 -07002552
Lucas Dupinea1fb1e2017-04-05 17:39:44 -07002553 int which = FLAG_SYSTEM;
2554 boolean shouldNotifyColors = false;
2555 WallpaperData wallpaper;
2556
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07002557 synchronized (mLock) {
Ruslan Tkhakokhov3929da02020-03-25 11:35:30 +00002558 Slog.v(TAG, "setWallpaperComponent name=" + name);
Lucas Dupinea1fb1e2017-04-05 17:39:44 -07002559 wallpaper = mWallpaperMap.get(userId);
Dianne Hackborn5dc5a002012-09-15 19:33:48 -07002560 if (wallpaper == null) {
2561 throw new IllegalStateException("Wallpaper not yet initialized for user " + userId);
2562 }
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07002563 final long ident = Binder.clearCallingIdentity();
Christopher Tate7cd00102016-12-19 14:38:44 -08002564
2565 // Live wallpapers can't be specified for keyguard. If we're using a static
2566 // system+lock image currently, migrate the system wallpaper to be a lock-only
2567 // image as part of making a different live component active as the system
2568 // wallpaper.
2569 if (mImageWallpaper.equals(wallpaper.wallpaperComponent)) {
2570 if (mLockWallpaperMap.get(userId) == null) {
2571 // We're using the static imagery and there is no lock-specific image in place,
2572 // therefore it's a shared system+lock image that we need to migrate.
Ruslan Tkhakokhov3929da02020-03-25 11:35:30 +00002573 Slog.i(TAG, "Migrating current wallpaper to be lock-only before"
2574 + "updating system wallpaper");
Christopher Tate7cd00102016-12-19 14:38:44 -08002575 migrateSystemToLockWallpaperLocked(userId);
2576 }
2577 }
2578
Lucas Dupinea1fb1e2017-04-05 17:39:44 -07002579 // New live wallpaper is also a lock wallpaper if nothing is set
2580 if (mLockWallpaperMap.get(userId) == null) {
2581 which |= FLAG_LOCK;
2582 }
2583
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07002584 try {
Amith Yamasani37ce3a82012-02-06 12:04:42 -08002585 wallpaper.imageWallpaperPending = false;
Lucas Dupin50ba9912017-07-14 11:55:05 -07002586 boolean same = changingToSame(name, wallpaper);
Christopher Tated57d17c2016-03-25 13:41:46 -07002587 if (bindWallpaperComponentLocked(name, false, true, wallpaper, null)) {
Lucas Dupin50ba9912017-07-14 11:55:05 -07002588 if (!same) {
2589 wallpaper.primaryColors = null;
Santiago Etchebehere85ef2fd2020-03-20 15:05:44 -07002590 } else {
2591 if (wallpaper.connection != null) {
2592 wallpaper.connection.forEachDisplayConnector(displayConnector -> {
2593 try {
2594 if (displayConnector.mEngine != null) {
2595 displayConnector.mEngine.dispatchWallpaperCommand(
2596 COMMAND_REAPPLY, 0, 0, 0, null);
2597 }
2598 } catch (RemoteException e) {
2599 Slog.w(TAG, "Error sending apply message to wallpaper", e);
2600 }
2601 });
2602 }
Lucas Dupin50ba9912017-07-14 11:55:05 -07002603 }
Christopher Tated57d17c2016-03-25 13:41:46 -07002604 wallpaper.wallpaperId = makeWallpaperIdLocked();
Sunny Goyal0572e182016-03-31 11:05:51 -07002605 notifyCallbacksLocked(wallpaper);
Lucas Dupinea1fb1e2017-04-05 17:39:44 -07002606 shouldNotifyColors = true;
Christopher Tated57d17c2016-03-25 13:41:46 -07002607 }
Dianne Hackbornf21adf62009-08-13 10:20:21 -07002608 } finally {
2609 Binder.restoreCallingIdentity(ident);
2610 }
2611 }
Lucas Dupinea1fb1e2017-04-05 17:39:44 -07002612
2613 if (shouldNotifyColors) {
2614 notifyWallpaperColorsChanged(wallpaper, which);
wilsonshih36597d42018-12-05 18:56:39 +08002615 notifyWallpaperColorsChanged(mFallbackWallpaper, FLAG_SYSTEM);
Lucas Dupinea1fb1e2017-04-05 17:39:44 -07002616 }
Dianne Hackbornf21adf62009-08-13 10:20:21 -07002617 }
Oleksandr Peletskyif2519812016-01-26 20:16:06 +01002618
Lucas Dupin50ba9912017-07-14 11:55:05 -07002619 private boolean changingToSame(ComponentName componentName, WallpaperData wallpaper) {
2620 if (wallpaper.connection != null) {
2621 if (wallpaper.wallpaperComponent == null) {
2622 if (componentName == null) {
2623 if (DEBUG) Slog.v(TAG, "changingToSame: still using default");
2624 // Still using default wallpaper.
2625 return true;
2626 }
2627 } else if (wallpaper.wallpaperComponent.equals(componentName)) {
2628 // Changing to same wallpaper.
2629 if (DEBUG) Slog.v(TAG, "same wallpaper");
2630 return true;
2631 }
2632 }
2633 return false;
2634 }
2635
wilsonshiha282bf72018-11-30 12:48:05 +08002636 private boolean bindWallpaperComponentLocked(ComponentName componentName, boolean force,
Dianne Hackborn5dc5a002012-09-15 19:33:48 -07002637 boolean fromUser, WallpaperData wallpaper, IRemoteCallback reply) {
Christopher Tate2a6c55f2017-03-31 12:28:30 -07002638 if (DEBUG_LIVE) {
2639 Slog.v(TAG, "bindWallpaperComponentLocked: componentName=" + componentName);
2640 }
Dianne Hackbornf21adf62009-08-13 10:20:21 -07002641 // Has the component changed?
Lucas Dupin50ba9912017-07-14 11:55:05 -07002642 if (!force && changingToSame(componentName, wallpaper)) {
2643 return true;
Dianne Hackbornf21adf62009-08-13 10:20:21 -07002644 }
Oleksandr Peletskyif2519812016-01-26 20:16:06 +01002645
Dianne Hackbornf21adf62009-08-13 10:20:21 -07002646 try {
Mike Clerona428b2c2009-11-15 22:53:08 -08002647 if (componentName == null) {
Christopher Tate2a6c55f2017-03-31 12:28:30 -07002648 componentName = mDefaultWallpaperComponent;
Mike Clerona428b2c2009-11-15 22:53:08 -08002649 if (componentName == null) {
Mike Cleron322b6ee2009-11-12 07:45:47 -08002650 // Fall back to static image wallpaper
Justin Koh29c30162014-09-05 17:10:10 -07002651 componentName = mImageWallpaper;
Mike Cleron322b6ee2009-11-12 07:45:47 -08002652 //clearWallpaperComponentLocked();
2653 //return;
Christopher Tate2a6c55f2017-03-31 12:28:30 -07002654 if (DEBUG_LIVE) Slog.v(TAG, "No default component; using image wallpaper");
Mike Cleron322b6ee2009-11-12 07:45:47 -08002655 }
Dianne Hackbornf21adf62009-08-13 10:20:21 -07002656 }
Amith Yamasani4e2820c2012-08-28 22:17:23 -07002657 int serviceUserId = wallpaper.userId;
2658 ServiceInfo si = mIPackageManager.getServiceInfo(componentName,
2659 PackageManager.GET_META_DATA | PackageManager.GET_PERMISSIONS, serviceUserId);
Christopher Tate90952202013-09-08 13:01:28 -07002660 if (si == null) {
2661 // The wallpaper component we're trying to use doesn't exist
2662 Slog.w(TAG, "Attempted wallpaper " + componentName + " is unavailable");
2663 return false;
2664 }
Dianne Hackbornf21adf62009-08-13 10:20:21 -07002665 if (!android.Manifest.permission.BIND_WALLPAPER.equals(si.permission)) {
Lucas Dupin4c8c3272018-11-06 17:47:48 -08002666 String msg = "Selected service does not have "
Dianne Hackbornf21adf62009-08-13 10:20:21 -07002667 + android.Manifest.permission.BIND_WALLPAPER
Dianne Hackborn80b902f2011-09-15 15:15:27 -07002668 + ": " + componentName;
2669 if (fromUser) {
2670 throw new SecurityException(msg);
2671 }
2672 Slog.w(TAG, msg);
2673 return false;
Dianne Hackbornf21adf62009-08-13 10:20:21 -07002674 }
Oleksandr Peletskyif2519812016-01-26 20:16:06 +01002675
Dianne Hackborneb034652009-09-07 00:49:58 -07002676 WallpaperInfo wi = null;
Oleksandr Peletskyif2519812016-01-26 20:16:06 +01002677
Dianne Hackbornf21adf62009-08-13 10:20:21 -07002678 Intent intent = new Intent(WallpaperService.SERVICE_INTERFACE);
Justin Koh29c30162014-09-05 17:10:10 -07002679 if (componentName != null && !componentName.equals(mImageWallpaper)) {
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07002680 // Make sure the selected service is actually a wallpaper service.
Amith Yamasani4e2820c2012-08-28 22:17:23 -07002681 List<ResolveInfo> ris =
2682 mIPackageManager.queryIntentServices(intent,
2683 intent.resolveTypeIfNeeded(mContext.getContentResolver()),
Jeff Sharkeyd5896632016-03-04 16:16:00 -07002684 PackageManager.GET_META_DATA, serviceUserId).getList();
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07002685 for (int i=0; i<ris.size(); i++) {
2686 ServiceInfo rsi = ris.get(i).serviceInfo;
2687 if (rsi.name.equals(si.name) &&
2688 rsi.packageName.equals(si.packageName)) {
Dianne Hackborneb034652009-09-07 00:49:58 -07002689 try {
2690 wi = new WallpaperInfo(mContext, ris.get(i));
2691 } catch (XmlPullParserException e) {
Dianne Hackborn80b902f2011-09-15 15:15:27 -07002692 if (fromUser) {
2693 throw new IllegalArgumentException(e);
2694 }
2695 Slog.w(TAG, e);
2696 return false;
Dianne Hackborneb034652009-09-07 00:49:58 -07002697 } catch (IOException e) {
Dianne Hackborn80b902f2011-09-15 15:15:27 -07002698 if (fromUser) {
2699 throw new IllegalArgumentException(e);
2700 }
2701 Slog.w(TAG, e);
2702 return false;
Dianne Hackborneb034652009-09-07 00:49:58 -07002703 }
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07002704 break;
2705 }
2706 }
Dianne Hackborneb034652009-09-07 00:49:58 -07002707 if (wi == null) {
Dianne Hackborn80b902f2011-09-15 15:15:27 -07002708 String msg = "Selected service is not a wallpaper: "
2709 + componentName;
2710 if (fromUser) {
2711 throw new SecurityException(msg);
2712 }
2713 Slog.w(TAG, msg);
2714 return false;
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07002715 }
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07002716 }
Oleksandr Peletskyif2519812016-01-26 20:16:06 +01002717
Lucas Dupin4c8c3272018-11-06 17:47:48 -08002718 if (wi != null && wi.supportsAmbientMode()) {
2719 final int hasPrivilege = mIPackageManager.checkPermission(
2720 android.Manifest.permission.AMBIENT_WALLPAPER, wi.getPackageName(),
2721 serviceUserId);
2722 if (hasPrivilege != PackageManager.PERMISSION_GRANTED) {
2723 String msg = "Selected service does not have "
2724 + android.Manifest.permission.AMBIENT_WALLPAPER
2725 + ": " + componentName;
2726 if (fromUser) {
2727 throw new SecurityException(msg);
2728 }
2729 Slog.w(TAG, msg);
2730 return false;
2731 }
2732 }
2733
Dianne Hackbornf21adf62009-08-13 10:20:21 -07002734 // Bind the service!
Joe Onorato8a9b2202010-02-26 18:56:32 -08002735 if (DEBUG) Slog.v(TAG, "Binding to:" + componentName);
wilsonshihde93f492018-11-01 21:23:40 +08002736 final int componentUid = mIPackageManager.getPackageUid(componentName.getPackageName(),
2737 MATCH_DIRECT_BOOT_AUTO, wallpaper.userId);
2738 WallpaperConnection newConn = new WallpaperConnection(wi, wallpaper, componentUid);
Mike Clerona428b2c2009-11-15 22:53:08 -08002739 intent.setComponent(componentName);
Dianne Hackborndd9b82c2009-09-03 00:18:47 -07002740 intent.putExtra(Intent.EXTRA_CLIENT_LABEL,
2741 com.android.internal.R.string.wallpaper_binding_label);
Dianne Hackborn41203752012-08-31 14:05:51 -07002742 intent.putExtra(Intent.EXTRA_CLIENT_INTENT, PendingIntent.getActivityAsUser(
Dianne Hackborneb034652009-09-07 00:49:58 -07002743 mContext, 0,
2744 Intent.createChooser(new Intent(Intent.ACTION_SET_WALLPAPER),
2745 mContext.getText(com.android.internal.R.string.chooser_wallpaper)),
Dianne Hackborn41203752012-08-31 14:05:51 -07002746 0, null, new UserHandle(serviceUserId)));
Dianne Hackbornc8230512013-07-13 21:32:12 -07002747 if (!mContext.bindServiceAsUser(intent, newConn,
Dianne Hackbornd69e4c12015-04-24 09:54:54 -07002748 Context.BIND_AUTO_CREATE | Context.BIND_SHOWING_UI
Amith Yamasanic45a9902019-04-05 16:29:30 -07002749 | Context.BIND_FOREGROUND_SERVICE_WHILE_AWAKE
2750 | Context.BIND_INCLUDE_CAPABILITIES,
Amith Yamasani27b89e62013-01-16 12:30:11 -08002751 new UserHandle(serviceUserId))) {
Dianne Hackborn80b902f2011-09-15 15:15:27 -07002752 String msg = "Unable to bind service: "
2753 + componentName;
2754 if (fromUser) {
2755 throw new IllegalArgumentException(msg);
2756 }
2757 Slog.w(TAG, msg);
2758 return false;
Dianne Hackbornf21adf62009-08-13 10:20:21 -07002759 }
wilsonshiha282bf72018-11-30 12:48:05 +08002760 if (wallpaper.userId == mCurrentUserId && mLastWallpaper != null
2761 && !wallpaper.equals(mFallbackWallpaper)) {
Amith Yamasani37ce3a82012-02-06 12:04:42 -08002762 detachWallpaperLocked(mLastWallpaper);
2763 }
2764 wallpaper.wallpaperComponent = componentName;
2765 wallpaper.connection = newConn;
Dianne Hackborn5dc5a002012-09-15 19:33:48 -07002766 newConn.mReply = reply;
wilsonshiha282bf72018-11-30 12:48:05 +08002767 if (wallpaper.userId == mCurrentUserId && !wallpaper.equals(mFallbackWallpaper)) {
wilsonshihde93f492018-11-01 21:23:40 +08002768 mLastWallpaper = wallpaper;
Dianne Hackbornf21adf62009-08-13 10:20:21 -07002769 }
wilsonshiha282bf72018-11-30 12:48:05 +08002770 updateFallbackConnection();
Amith Yamasani4e2820c2012-08-28 22:17:23 -07002771 } catch (RemoteException e) {
2772 String msg = "Remote exception for " + componentName + "\n" + e;
Dianne Hackborn80b902f2011-09-15 15:15:27 -07002773 if (fromUser) {
2774 throw new IllegalArgumentException(msg);
2775 }
2776 Slog.w(TAG, msg);
2777 return false;
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07002778 }
Dianne Hackborn80b902f2011-09-15 15:15:27 -07002779 return true;
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07002780 }
Amith Yamasani37ce3a82012-02-06 12:04:42 -08002781
wilsonshiha282bf72018-11-30 12:48:05 +08002782 private void detachWallpaperLocked(WallpaperData wallpaper) {
Amith Yamasani37ce3a82012-02-06 12:04:42 -08002783 if (wallpaper.connection != null) {
Dianne Hackborn5dc5a002012-09-15 19:33:48 -07002784 if (wallpaper.connection.mReply != null) {
2785 try {
2786 wallpaper.connection.mReply.sendResult(null);
2787 } catch (RemoteException e) {
2788 }
2789 wallpaper.connection.mReply = null;
2790 }
wilsonshihb72ff9c2019-03-21 17:27:02 +08002791 try {
Keun young Parke3d2fea2019-03-29 17:42:32 -07002792 // It can be null if user switching happens before service connection.
2793 if (wallpaper.connection.mService != null) {
2794 wallpaper.connection.mService.detach();
2795 }
wilsonshihb72ff9c2019-03-21 17:27:02 +08002796 } catch (RemoteException e) {
2797 Slog.w(TAG, "Failed detaching wallpaper service ", e);
2798 }
Amith Yamasani37ce3a82012-02-06 12:04:42 -08002799 mContext.unbindService(wallpaper.connection);
wilsonshiha282bf72018-11-30 12:48:05 +08002800 wallpaper.connection.forEachDisplayConnector(
2801 WallpaperConnection.DisplayConnector::disconnectLocked);
Amith Yamasani37ce3a82012-02-06 12:04:42 -08002802 wallpaper.connection.mService = null;
wilsonshihde93f492018-11-01 21:23:40 +08002803 wallpaper.connection.mDisplayConnector.clear();
Amith Yamasani37ce3a82012-02-06 12:04:42 -08002804 wallpaper.connection = null;
wilsonshihde93f492018-11-01 21:23:40 +08002805 if (wallpaper == mLastWallpaper) mLastWallpaper = null;
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07002806 }
2807 }
Amith Yamasani37ce3a82012-02-06 12:04:42 -08002808
wilsonshiha282bf72018-11-30 12:48:05 +08002809 private void clearWallpaperComponentLocked(WallpaperData wallpaper) {
Amith Yamasani37ce3a82012-02-06 12:04:42 -08002810 wallpaper.wallpaperComponent = null;
2811 detachWallpaperLocked(wallpaper);
2812 }
2813
wilsonshiha282bf72018-11-30 12:48:05 +08002814 private void attachServiceLocked(WallpaperConnection conn, WallpaperData wallpaper) {
wilsonshihde93f492018-11-01 21:23:40 +08002815 conn.forEachDisplayConnector(connector-> connector.connectLocked(conn, wallpaper));
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07002816 }
Amith Yamasani37ce3a82012-02-06 12:04:42 -08002817
2818 private void notifyCallbacksLocked(WallpaperData wallpaper) {
2819 final int n = wallpaper.callbacks.beginBroadcast();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002820 for (int i = 0; i < n; i++) {
2821 try {
Amith Yamasani37ce3a82012-02-06 12:04:42 -08002822 wallpaper.callbacks.getBroadcastItem(i).onWallpaperChanged();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002823 } catch (RemoteException e) {
2824
2825 // The RemoteCallbackList will take care of removing
2826 // the dead object for us.
2827 }
2828 }
Amith Yamasani37ce3a82012-02-06 12:04:42 -08002829 wallpaper.callbacks.finishBroadcast();
Lucas Dupinea1fb1e2017-04-05 17:39:44 -07002830
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002831 final Intent intent = new Intent(Intent.ACTION_WALLPAPER_CHANGED);
Dianne Hackborn5ac72a22012-08-29 18:32:08 -07002832 mContext.sendBroadcastAsUser(intent, new UserHandle(mCurrentUserId));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002833 }
2834
2835 private void checkPermission(String permission) {
Joe Onorato9bb8fd72009-07-28 18:24:51 -07002836 if (PackageManager.PERMISSION_GRANTED!= mContext.checkCallingOrSelfPermission(permission)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002837 throw new SecurityException("Access denied to process: " + Binder.getCallingPid()
2838 + ", must have permission " + permission);
2839 }
2840 }
Joe Onorato9bb8fd72009-07-28 18:24:51 -07002841
Benjamin Franzf3ece362015-02-11 10:51:10 +00002842 /**
2843 * Certain user types do not support wallpapers (e.g. managed profiles). The check is
2844 * implemented through through the OP_WRITE_WALLPAPER AppOp.
2845 */
2846 public boolean isWallpaperSupported(String callingPackage) {
2847 return mAppOpsManager.checkOpNoThrow(AppOpsManager.OP_WRITE_WALLPAPER, Binder.getCallingUid(),
2848 callingPackage) == AppOpsManager.MODE_ALLOWED;
2849 }
2850
Oleksandr Peletskyif2519812016-01-26 20:16:06 +01002851 @Override
Christopher Tate98d609c2016-05-18 17:31:58 -07002852 public boolean isSetWallpaperAllowed(String callingPackage) {
Oleksandr Peletskyif2519812016-01-26 20:16:06 +01002853 final PackageManager pm = mContext.getPackageManager();
2854 String[] uidPackages = pm.getPackagesForUid(Binder.getCallingUid());
2855 boolean uidMatchPackage = Arrays.asList(uidPackages).contains(callingPackage);
2856 if (!uidMatchPackage) {
2857 return false; // callingPackage was faked.
2858 }
2859
Bookatzcfc5d192019-11-05 17:25:53 -08002860 // TODO(b/144048540): DPM needs to take into account the userId, not just the package.
Oleksandr Peletskyif2519812016-01-26 20:16:06 +01002861 final DevicePolicyManager dpm = mContext.getSystemService(DevicePolicyManager.class);
2862 if (dpm.isDeviceOwnerApp(callingPackage) || dpm.isProfileOwnerApp(callingPackage)) {
2863 return true;
2864 }
Bookatzcfc5d192019-11-05 17:25:53 -08002865 final int callingUserId = UserHandle.getCallingUserId();
2866 final long ident = Binder.clearCallingIdentity();
2867 try {
2868 UserManagerInternal umi = LocalServices.getService(UserManagerInternal.class);
2869 return !umi.hasUserRestriction(UserManager.DISALLOW_SET_WALLPAPER, callingUserId);
2870 } finally {
2871 Binder.restoreCallingIdentity(ident);
2872 }
Oleksandr Peletskyif2519812016-01-26 20:16:06 +01002873 }
2874
Christopher Tated7faf532016-02-25 12:43:38 -08002875 @Override
Christopher Tate61722662016-08-10 16:13:14 -07002876 public boolean isWallpaperBackupEligible(int which, int userId) {
Christopher Tated7faf532016-02-25 12:43:38 -08002877 if (Binder.getCallingUid() != Process.SYSTEM_UID) {
2878 throw new SecurityException("Only the system may call isWallpaperBackupEligible");
2879 }
2880
Christopher Tate61722662016-08-10 16:13:14 -07002881 WallpaperData wallpaper = (which == FLAG_LOCK)
Christopher Tatef7d1b5d2016-08-19 11:21:07 -07002882 ? mLockWallpaperMap.get(userId)
2883 : mWallpaperMap.get(userId);
Christopher Tated7faf532016-02-25 12:43:38 -08002884 return (wallpaper != null) ? wallpaper.allowBackup : false;
2885 }
2886
wilsonshih643bf132019-02-27 12:49:19 +08002887 private void onDisplayReadyInternal(int displayId) {
2888 synchronized (mLock) {
2889 if (mLastWallpaper == null) {
2890 return;
2891 }
2892 if (supportsMultiDisplay(mLastWallpaper.connection)) {
2893 final WallpaperConnection.DisplayConnector connector =
2894 mLastWallpaper.connection.getDisplayConnectorOrCreate(displayId);
2895 if (connector == null) return;
2896 connector.connectLocked(mLastWallpaper.connection, mLastWallpaper);
2897 return;
2898 }
2899 // System wallpaper does not support multiple displays, attach this display to
2900 // the fallback wallpaper.
2901 if (mFallbackWallpaper != null) {
2902 final WallpaperConnection.DisplayConnector connector = mFallbackWallpaper
2903 .connection.getDisplayConnectorOrCreate(displayId);
2904 if (connector == null) return;
2905 connector.connectLocked(mFallbackWallpaper.connection, mFallbackWallpaper);
2906 } else {
2907 Slog.w(TAG, "No wallpaper can be added to the new display");
2908 }
2909 }
2910 }
2911
wilsonshih999ec102019-05-17 18:47:50 +08002912 private JournaledFile makeJournaledFile(int userId) {
Amith Yamasani61f57372012-08-31 12:12:28 -07002913 final String base = new File(getWallpaperDir(userId), WALLPAPER_INFO).getAbsolutePath();
Joe Onorato9bb8fd72009-07-28 18:24:51 -07002914 return new JournaledFile(new File(base), new File(base + ".tmp"));
2915 }
2916
Christopher Tatedb27b842016-02-25 14:39:17 -08002917 private void saveSettingsLocked(int userId) {
2918 JournaledFile journal = makeJournaledFile(userId);
2919 FileOutputStream fstream = null;
2920 BufferedOutputStream stream = null;
Joe Onorato9bb8fd72009-07-28 18:24:51 -07002921 try {
Joe Onorato9bb8fd72009-07-28 18:24:51 -07002922 XmlSerializer out = new FastXmlSerializer();
Christopher Tatedb27b842016-02-25 14:39:17 -08002923 fstream = new FileOutputStream(journal.chooseForWrite(), false);
2924 stream = new BufferedOutputStream(fstream);
Wojciech Staszkiewicz9e9e2e72015-05-08 14:58:46 +01002925 out.setOutput(stream, StandardCharsets.UTF_8.name());
Joe Onorato9bb8fd72009-07-28 18:24:51 -07002926 out.startDocument(null, true);
2927
Christopher Tatedb27b842016-02-25 14:39:17 -08002928 WallpaperData wallpaper;
Christopher Tate1e1e2e02016-01-25 15:34:36 -08002929
Christopher Tatedb27b842016-02-25 14:39:17 -08002930 wallpaper = mWallpaperMap.get(userId);
2931 if (wallpaper != null) {
2932 writeWallpaperAttributes(out, "wp", wallpaper);
Dianne Hackborn067e5f62014-09-07 23:14:30 -07002933 }
Christopher Tatedb27b842016-02-25 14:39:17 -08002934 wallpaper = mLockWallpaperMap.get(userId);
2935 if (wallpaper != null) {
2936 writeWallpaperAttributes(out, "kwp", wallpaper);
Dianne Hackborn067e5f62014-09-07 23:14:30 -07002937 }
Joe Onorato9bb8fd72009-07-28 18:24:51 -07002938
2939 out.endDocument();
Christopher Tatedb27b842016-02-25 14:39:17 -08002940
2941 stream.flush(); // also flushes fstream
2942 FileUtils.sync(fstream);
2943 stream.close(); // also closes fstream
Joe Onorato9bb8fd72009-07-28 18:24:51 -07002944 journal.commit();
2945 } catch (IOException e) {
Christopher Tatead3c2592016-01-20 18:13:17 -08002946 IoUtils.closeQuietly(stream);
Joe Onorato9bb8fd72009-07-28 18:24:51 -07002947 journal.rollback();
2948 }
2949 }
2950
Christopher Tatedb27b842016-02-25 14:39:17 -08002951 private void writeWallpaperAttributes(XmlSerializer out, String tag, WallpaperData wallpaper)
2952 throws IllegalArgumentException, IllegalStateException, IOException {
Lucas Dupinea1fb1e2017-04-05 17:39:44 -07002953 if (DEBUG) {
Christopher Tatef717b932017-09-11 15:52:54 -07002954 Slog.v(TAG, "writeWallpaperAttributes id=" + wallpaper.wallpaperId);
Lucas Dupinea1fb1e2017-04-05 17:39:44 -07002955 }
wilsonshiha282bf72018-11-30 12:48:05 +08002956 final DisplayData wpdData = getDisplayDataOrCreate(DEFAULT_DISPLAY);
Christopher Tatedb27b842016-02-25 14:39:17 -08002957 out.startTag(null, tag);
2958 out.attribute(null, "id", Integer.toString(wallpaper.wallpaperId));
wilsonshih81e10a72018-11-15 10:54:21 +08002959 out.attribute(null, "width", Integer.toString(wpdData.mWidth));
2960 out.attribute(null, "height", Integer.toString(wpdData.mHeight));
Christopher Tatedb27b842016-02-25 14:39:17 -08002961
2962 out.attribute(null, "cropLeft", Integer.toString(wallpaper.cropHint.left));
2963 out.attribute(null, "cropTop", Integer.toString(wallpaper.cropHint.top));
2964 out.attribute(null, "cropRight", Integer.toString(wallpaper.cropHint.right));
2965 out.attribute(null, "cropBottom", Integer.toString(wallpaper.cropHint.bottom));
2966
wilsonshih81e10a72018-11-15 10:54:21 +08002967 if (wpdData.mPadding.left != 0) {
2968 out.attribute(null, "paddingLeft", Integer.toString(wpdData.mPadding.left));
Christopher Tatedb27b842016-02-25 14:39:17 -08002969 }
wilsonshih81e10a72018-11-15 10:54:21 +08002970 if (wpdData.mPadding.top != 0) {
2971 out.attribute(null, "paddingTop", Integer.toString(wpdData.mPadding.top));
Christopher Tatedb27b842016-02-25 14:39:17 -08002972 }
wilsonshih81e10a72018-11-15 10:54:21 +08002973 if (wpdData.mPadding.right != 0) {
2974 out.attribute(null, "paddingRight", Integer.toString(wpdData.mPadding.right));
Christopher Tatedb27b842016-02-25 14:39:17 -08002975 }
wilsonshih81e10a72018-11-15 10:54:21 +08002976 if (wpdData.mPadding.bottom != 0) {
2977 out.attribute(null, "paddingBottom", Integer.toString(wpdData.mPadding.bottom));
Christopher Tatedb27b842016-02-25 14:39:17 -08002978 }
2979
Lucas Dupinea1fb1e2017-04-05 17:39:44 -07002980 if (wallpaper.primaryColors != null) {
Lucas Dupin84b89d92017-05-09 12:16:19 -07002981 int colorsCount = wallpaper.primaryColors.getMainColors().size();
Lucas Dupinea1fb1e2017-04-05 17:39:44 -07002982 out.attribute(null, "colorsCount", Integer.toString(colorsCount));
2983 if (colorsCount > 0) {
2984 for (int i = 0; i < colorsCount; i++) {
Lucas Dupin84b89d92017-05-09 12:16:19 -07002985 final Color wc = wallpaper.primaryColors.getMainColors().get(i);
2986 out.attribute(null, "colorValue"+i, Integer.toString(wc.toArgb()));
Lucas Dupinea1fb1e2017-04-05 17:39:44 -07002987 }
2988 }
Lucas Dupin75ec3792017-06-29 14:07:18 -07002989 out.attribute(null, "colorHints",
Lucas Dupin84b89d92017-05-09 12:16:19 -07002990 Integer.toString(wallpaper.primaryColors.getColorHints()));
Lucas Dupinea1fb1e2017-04-05 17:39:44 -07002991 }
2992
Christopher Tatedb27b842016-02-25 14:39:17 -08002993 out.attribute(null, "name", wallpaper.name);
2994 if (wallpaper.wallpaperComponent != null
2995 && !wallpaper.wallpaperComponent.equals(mImageWallpaper)) {
2996 out.attribute(null, "component",
2997 wallpaper.wallpaperComponent.flattenToShortString());
2998 }
Christopher Tated7faf532016-02-25 12:43:38 -08002999
3000 if (wallpaper.allowBackup) {
3001 out.attribute(null, "backup", "true");
3002 }
3003
Christopher Tatedb27b842016-02-25 14:39:17 -08003004 out.endTag(null, tag);
3005 }
3006
Amith Yamasani37ce3a82012-02-06 12:04:42 -08003007 private void migrateFromOld() {
Christopher Tate9f224432017-08-01 16:32:49 -07003008 // Pre-N, what existed is the one we're now using as the display crop
3009 File preNWallpaper = new File(getWallpaperDir(0), WALLPAPER_CROP);
3010 // In the very-long-ago, imagery lived with the settings app
3011 File originalWallpaper = new File(WallpaperBackupHelper.WALLPAPER_IMAGE_KEY);
3012 File newWallpaper = new File(getWallpaperDir(0), WALLPAPER);
3013
3014 // Migrations from earlier wallpaper image storage schemas
3015 if (preNWallpaper.exists()) {
3016 if (!newWallpaper.exists()) {
3017 // we've got the 'wallpaper' crop file but not the nominal source image,
3018 // so do the simple "just take everything" straight copy of legacy data
3019 if (DEBUG) {
3020 Slog.i(TAG, "Migrating wallpaper schema");
3021 }
3022 FileUtils.copyFile(preNWallpaper, newWallpaper);
3023 } // else we're in the usual modern case: both source & crop exist
3024 } else if (originalWallpaper.exists()) {
3025 // VERY old schema; make sure things exist and are in the right place
3026 if (DEBUG) {
3027 Slog.i(TAG, "Migrating antique wallpaper schema");
3028 }
3029 File oldInfo = new File(WallpaperBackupHelper.WALLPAPER_INFO_KEY);
3030 if (oldInfo.exists()) {
3031 File newInfo = new File(getWallpaperDir(0), WALLPAPER_INFO);
3032 oldInfo.renameTo(newInfo);
3033 }
3034
3035 FileUtils.copyFile(originalWallpaper, preNWallpaper);
3036 originalWallpaper.renameTo(newWallpaper);
Amith Yamasani37ce3a82012-02-06 12:04:42 -08003037 }
3038 }
3039
Dianne Hackborn067e5f62014-09-07 23:14:30 -07003040 private int getAttributeInt(XmlPullParser parser, String name, int defValue) {
3041 String value = parser.getAttributeValue(null, name);
3042 if (value == null) {
3043 return defValue;
3044 }
3045 return Integer.parseInt(value);
3046 }
3047
Xiaohui Chenac531942015-05-13 13:20:52 -07003048 /**
3049 * Sometimes it is expected the wallpaper map may not have a user's data. E.g. This could
3050 * happen during user switch. The async user switch observer may not have received
3051 * the event yet. We use this safe method when we don't care about this ordering and just
3052 * want to update the data. The data is going to be applied when the user switch observer
3053 * is eventually executed.
Christopher Tatef717b932017-09-11 15:52:54 -07003054 *
3055 * Important: this method loads settings to initialize the given user's wallpaper data if
3056 * there is no current in-memory state.
Xiaohui Chenac531942015-05-13 13:20:52 -07003057 */
Christopher Tatebe132e62016-02-10 12:59:49 -08003058 private WallpaperData getWallpaperSafeLocked(int userId, int which) {
3059 // We're setting either just system (work with the system wallpaper),
3060 // both (also work with the system wallpaper), or just the lock
3061 // wallpaper (update against the existing lock wallpaper if any).
3062 // Combined or just-system operations use the 'system' WallpaperData
3063 // for this use; lock-only operations use the dedicated one.
3064 final SparseArray<WallpaperData> whichSet =
Christopher Tateedf7d042016-03-29 18:24:25 -07003065 (which == FLAG_LOCK) ? mLockWallpaperMap : mWallpaperMap;
Christopher Tatebe132e62016-02-10 12:59:49 -08003066 WallpaperData wallpaper = whichSet.get(userId);
Xiaohui Chenac531942015-05-13 13:20:52 -07003067 if (wallpaper == null) {
Christopher Tatebe132e62016-02-10 12:59:49 -08003068 // common case, this is the first lookup post-boot of the system or
3069 // unified lock, so we bring up the saved state lazily now and recheck.
Christopher Tated7faf532016-02-25 12:43:38 -08003070 loadSettingsLocked(userId, false);
Christopher Tatebe132e62016-02-10 12:59:49 -08003071 wallpaper = whichSet.get(userId);
3072 // if it's still null here, this is a lock-only operation and there is not
3073 // yet a lock-only wallpaper set for this user, so we need to establish
3074 // it now.
3075 if (wallpaper == null) {
Christopher Tateedf7d042016-03-29 18:24:25 -07003076 if (which == FLAG_LOCK) {
wilsonshih999ec102019-05-17 18:47:50 +08003077 wallpaper = new WallpaperData(userId, getWallpaperDir(userId),
Christopher Tatebe132e62016-02-10 12:59:49 -08003078 WALLPAPER_LOCK_ORIG, WALLPAPER_LOCK_CROP);
3079 mLockWallpaperMap.put(userId, wallpaper);
wilsonshih594497e2020-03-31 10:49:37 +08003080 ensureSaneWallpaperData(wallpaper);
Christopher Tatebe132e62016-02-10 12:59:49 -08003081 } else {
3082 // sanity fallback: we're in bad shape, but establishing a known
3083 // valid system+lock WallpaperData will keep us from dying.
3084 Slog.wtf(TAG, "Didn't find wallpaper in non-lock case!");
wilsonshih999ec102019-05-17 18:47:50 +08003085 wallpaper = new WallpaperData(userId, getWallpaperDir(userId),
3086 WALLPAPER, WALLPAPER_CROP);
Christopher Tatebe132e62016-02-10 12:59:49 -08003087 mWallpaperMap.put(userId, wallpaper);
wilsonshih594497e2020-03-31 10:49:37 +08003088 ensureSaneWallpaperData(wallpaper);
Christopher Tatebe132e62016-02-10 12:59:49 -08003089 }
3090 }
Xiaohui Chenac531942015-05-13 13:20:52 -07003091 }
3092 return wallpaper;
3093 }
3094
Christopher Tated7faf532016-02-25 12:43:38 -08003095 private void loadSettingsLocked(int userId, boolean keepDimensionHints) {
Amith Yamasani37ce3a82012-02-06 12:04:42 -08003096 JournaledFile journal = makeJournaledFile(userId);
Joe Onorato9bb8fd72009-07-28 18:24:51 -07003097 FileInputStream stream = null;
3098 File file = journal.chooseForRead();
Christopher Tate9f224432017-08-01 16:32:49 -07003099
Amith Yamasani37ce3a82012-02-06 12:04:42 -08003100 WallpaperData wallpaper = mWallpaperMap.get(userId);
3101 if (wallpaper == null) {
Christopher Tate9f224432017-08-01 16:32:49 -07003102 // Do this once per boot
3103 migrateFromOld();
3104
wilsonshih999ec102019-05-17 18:47:50 +08003105 wallpaper = new WallpaperData(userId, getWallpaperDir(userId),
3106 WALLPAPER, WALLPAPER_CROP);
Christopher Tated7faf532016-02-25 12:43:38 -08003107 wallpaper.allowBackup = true;
Amith Yamasani37ce3a82012-02-06 12:04:42 -08003108 mWallpaperMap.put(userId, wallpaper);
Christopher Tate41297ff2016-03-10 16:46:15 -08003109 if (!wallpaper.cropExists()) {
Christopher Tate2a6c55f2017-03-31 12:28:30 -07003110 if (wallpaper.sourceExists()) {
3111 generateCrop(wallpaper);
3112 } else {
3113 Slog.i(TAG, "No static wallpaper imagery; defaults will be shown");
3114 }
Christopher Tate41297ff2016-03-10 16:46:15 -08003115 }
wilsonshiha282bf72018-11-30 12:48:05 +08003116 initializeFallbackWallpaper();
Amith Yamasani37ce3a82012-02-06 12:04:42 -08003117 }
Joe Onorato9bb8fd72009-07-28 18:24:51 -07003118 boolean success = false;
wilsonshiha282bf72018-11-30 12:48:05 +08003119 final DisplayData wpdData = getDisplayDataOrCreate(DEFAULT_DISPLAY);
Joe Onorato9bb8fd72009-07-28 18:24:51 -07003120 try {
3121 stream = new FileInputStream(file);
3122 XmlPullParser parser = Xml.newPullParser();
Wojciech Staszkiewicz9e9e2e72015-05-08 14:58:46 +01003123 parser.setInput(stream, StandardCharsets.UTF_8.name());
Joe Onorato9bb8fd72009-07-28 18:24:51 -07003124
3125 int type;
Joe Onorato9bb8fd72009-07-28 18:24:51 -07003126 do {
3127 type = parser.next();
3128 if (type == XmlPullParser.START_TAG) {
3129 String tag = parser.getName();
3130 if ("wp".equals(tag)) {
Christopher Tatebe132e62016-02-10 12:59:49 -08003131 // Common to system + lock wallpapers
Christopher Tated7faf532016-02-25 12:43:38 -08003132 parseWallpaperAttributes(parser, wallpaper, keepDimensionHints);
Christopher Tatead3c2592016-01-20 18:13:17 -08003133
Christopher Tatebe132e62016-02-10 12:59:49 -08003134 // A system wallpaper might also be a live wallpaper
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07003135 String comp = parser.getAttributeValue(null, "component");
Amith Yamasani37ce3a82012-02-06 12:04:42 -08003136 wallpaper.nextWallpaperComponent = comp != null
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07003137 ? ComponentName.unflattenFromString(comp)
3138 : null;
Amith Yamasani37ce3a82012-02-06 12:04:42 -08003139 if (wallpaper.nextWallpaperComponent == null
3140 || "android".equals(wallpaper.nextWallpaperComponent
3141 .getPackageName())) {
Justin Koh29c30162014-09-05 17:10:10 -07003142 wallpaper.nextWallpaperComponent = mImageWallpaper;
Dianne Hackborn9ea31632011-08-05 14:43:50 -07003143 }
Oleksandr Peletskyif2519812016-01-26 20:16:06 +01003144
Mike Clerona428b2c2009-11-15 22:53:08 -08003145 if (DEBUG) {
wilsonshih81e10a72018-11-15 10:54:21 +08003146 Slog.v(TAG, "mWidth:" + wpdData.mWidth);
3147 Slog.v(TAG, "mHeight:" + wpdData.mHeight);
Christopher Tate1e1e2e02016-01-25 15:34:36 -08003148 Slog.v(TAG, "cropRect:" + wallpaper.cropHint);
Lucas Dupinea1fb1e2017-04-05 17:39:44 -07003149 Slog.v(TAG, "primaryColors:" + wallpaper.primaryColors);
Amith Yamasani37ce3a82012-02-06 12:04:42 -08003150 Slog.v(TAG, "mName:" + wallpaper.name);
3151 Slog.v(TAG, "mNextWallpaperComponent:"
3152 + wallpaper.nextWallpaperComponent);
Mike Clerona428b2c2009-11-15 22:53:08 -08003153 }
Christopher Tatebe132e62016-02-10 12:59:49 -08003154 } else if ("kwp".equals(tag)) {
3155 // keyguard-specific wallpaper for this user
3156 WallpaperData lockWallpaper = mLockWallpaperMap.get(userId);
3157 if (lockWallpaper == null) {
wilsonshih999ec102019-05-17 18:47:50 +08003158 lockWallpaper = new WallpaperData(userId, getWallpaperDir(userId),
Christopher Tatebe132e62016-02-10 12:59:49 -08003159 WALLPAPER_LOCK_ORIG, WALLPAPER_LOCK_CROP);
3160 mLockWallpaperMap.put(userId, lockWallpaper);
3161 }
Christopher Tated7faf532016-02-25 12:43:38 -08003162 parseWallpaperAttributes(parser, lockWallpaper, false);
Joe Onorato9bb8fd72009-07-28 18:24:51 -07003163 }
3164 }
3165 } while (type != XmlPullParser.END_DOCUMENT);
3166 success = true;
Dianne Hackborn13579ed2012-11-28 18:05:36 -08003167 } catch (FileNotFoundException e) {
3168 Slog.w(TAG, "no current wallpaper -- first boot?");
Joe Onorato9bb8fd72009-07-28 18:24:51 -07003169 } catch (NullPointerException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08003170 Slog.w(TAG, "failed parsing " + file + " " + e);
Joe Onorato9bb8fd72009-07-28 18:24:51 -07003171 } catch (NumberFormatException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08003172 Slog.w(TAG, "failed parsing " + file + " " + e);
Joe Onorato9bb8fd72009-07-28 18:24:51 -07003173 } catch (XmlPullParserException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08003174 Slog.w(TAG, "failed parsing " + file + " " + e);
Joe Onorato9bb8fd72009-07-28 18:24:51 -07003175 } catch (IOException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08003176 Slog.w(TAG, "failed parsing " + file + " " + e);
Joe Onorato9bb8fd72009-07-28 18:24:51 -07003177 } catch (IndexOutOfBoundsException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08003178 Slog.w(TAG, "failed parsing " + file + " " + e);
Joe Onorato9bb8fd72009-07-28 18:24:51 -07003179 }
Christopher Tatead3c2592016-01-20 18:13:17 -08003180 IoUtils.closeQuietly(stream);
Joe Onorato9bb8fd72009-07-28 18:24:51 -07003181
3182 if (!success) {
Christopher Tate1e1e2e02016-01-25 15:34:36 -08003183 wallpaper.cropHint.set(0, 0, 0, 0);
wilsonshih81e10a72018-11-15 10:54:21 +08003184 wpdData.mPadding.set(0, 0, 0, 0);
Amith Yamasani37ce3a82012-02-06 12:04:42 -08003185 wallpaper.name = "";
Adrian Roosc28e3a92016-04-14 10:47:52 -07003186
3187 mLockWallpaperMap.remove(userId);
Christopher Tatead3c2592016-01-20 18:13:17 -08003188 } else {
3189 if (wallpaper.wallpaperId <= 0) {
3190 wallpaper.wallpaperId = makeWallpaperIdLocked();
3191 if (DEBUG) {
3192 Slog.w(TAG, "Didn't set wallpaper id in loadSettingsLocked(" + userId
3193 + "); now " + wallpaper.wallpaperId);
3194 }
3195 }
Joe Onorato9bb8fd72009-07-28 18:24:51 -07003196 }
Dianne Hackborn44bc17c2011-04-20 18:18:51 -07003197
wilsonshihd9173df2018-11-29 11:52:15 +08003198 ensureSaneWallpaperDisplaySize(wpdData, DEFAULT_DISPLAY);
wilsonshih594497e2020-03-31 10:49:37 +08003199 ensureSaneWallpaperData(wallpaper);
Adrian Roosc28e3a92016-04-14 10:47:52 -07003200 WallpaperData lockWallpaper = mLockWallpaperMap.get(userId);
3201 if (lockWallpaper != null) {
wilsonshih594497e2020-03-31 10:49:37 +08003202 ensureSaneWallpaperData(lockWallpaper);
Adrian Roosc28e3a92016-04-14 10:47:52 -07003203 }
3204 }
3205
wilsonshiha282bf72018-11-30 12:48:05 +08003206 private void initializeFallbackWallpaper() {
3207 if (mFallbackWallpaper == null) {
3208 if (DEBUG) Slog.d(TAG, "Initialize fallback wallpaper");
wilsonshih999ec102019-05-17 18:47:50 +08003209 final int systemUserId = UserHandle.USER_SYSTEM;
3210 mFallbackWallpaper = new WallpaperData(systemUserId, getWallpaperDir(systemUserId),
3211 WALLPAPER, WALLPAPER_CROP);
wilsonshiha282bf72018-11-30 12:48:05 +08003212 mFallbackWallpaper.allowBackup = false;
3213 mFallbackWallpaper.wallpaperId = makeWallpaperIdLocked();
3214 bindWallpaperComponentLocked(mImageWallpaper, true, false, mFallbackWallpaper, null);
3215 }
3216 }
3217
wilsonshih594497e2020-03-31 10:49:37 +08003218 private void ensureSaneWallpaperData(WallpaperData wallpaper) {
3219 // Only overwrite cropHint if the rectangle is invalid.
3220 if (wallpaper.cropHint.width() < 0
3221 || wallpaper.cropHint.height() < 0) {
3222 wallpaper.cropHint.set(0, 0, 0, 0);
Christopher Tate1e1e2e02016-01-25 15:34:36 -08003223 }
Joe Onorato9bb8fd72009-07-28 18:24:51 -07003224 }
3225
Christopher Tated7faf532016-02-25 12:43:38 -08003226 private void parseWallpaperAttributes(XmlPullParser parser, WallpaperData wallpaper,
3227 boolean keepDimensionHints) {
Christopher Tatebe132e62016-02-10 12:59:49 -08003228 final String idString = parser.getAttributeValue(null, "id");
3229 if (idString != null) {
3230 final int id = wallpaper.wallpaperId = Integer.parseInt(idString);
3231 if (id > mWallpaperId) {
3232 mWallpaperId = id;
3233 }
3234 } else {
3235 wallpaper.wallpaperId = makeWallpaperIdLocked();
3236 }
3237
wilsonshiha282bf72018-11-30 12:48:05 +08003238 final DisplayData wpData = getDisplayDataOrCreate(DEFAULT_DISPLAY);
wilsonshih81e10a72018-11-15 10:54:21 +08003239
Christopher Tated7faf532016-02-25 12:43:38 -08003240 if (!keepDimensionHints) {
wilsonshih81e10a72018-11-15 10:54:21 +08003241 wpData.mWidth = Integer.parseInt(parser.getAttributeValue(null, "width"));
3242 wpData.mHeight = Integer.parseInt(parser.getAttributeValue(null, "height"));
Christopher Tated7faf532016-02-25 12:43:38 -08003243 }
Christopher Tatebe132e62016-02-10 12:59:49 -08003244 wallpaper.cropHint.left = getAttributeInt(parser, "cropLeft", 0);
3245 wallpaper.cropHint.top = getAttributeInt(parser, "cropTop", 0);
3246 wallpaper.cropHint.right = getAttributeInt(parser, "cropRight", 0);
3247 wallpaper.cropHint.bottom = getAttributeInt(parser, "cropBottom", 0);
wilsonshih81e10a72018-11-15 10:54:21 +08003248 wpData.mPadding.left = getAttributeInt(parser, "paddingLeft", 0);
3249 wpData.mPadding.top = getAttributeInt(parser, "paddingTop", 0);
3250 wpData.mPadding.right = getAttributeInt(parser, "paddingRight", 0);
3251 wpData.mPadding.bottom = getAttributeInt(parser, "paddingBottom", 0);
Lucas Dupinea1fb1e2017-04-05 17:39:44 -07003252 int colorsCount = getAttributeInt(parser, "colorsCount", 0);
3253 if (colorsCount > 0) {
Lucas Dupin84b89d92017-05-09 12:16:19 -07003254 Color primary = null, secondary = null, tertiary = null;
Lucas Dupinea1fb1e2017-04-05 17:39:44 -07003255 for (int i = 0; i < colorsCount; i++) {
Lucas Dupin84b89d92017-05-09 12:16:19 -07003256 Color color = Color.valueOf(getAttributeInt(parser, "colorValue" + i, 0));
3257 if (i == 0) {
3258 primary = color;
3259 } else if (i == 1) {
3260 secondary = color;
3261 } else if (i == 2) {
3262 tertiary = color;
3263 } else {
3264 break;
3265 }
Lucas Dupinea1fb1e2017-04-05 17:39:44 -07003266 }
Lucas Dupin84b89d92017-05-09 12:16:19 -07003267 int colorHints = getAttributeInt(parser, "colorHints", 0);
3268 wallpaper.primaryColors = new WallpaperColors(primary, secondary, tertiary, colorHints);
Lucas Dupinea1fb1e2017-04-05 17:39:44 -07003269 }
Christopher Tatebe132e62016-02-10 12:59:49 -08003270 wallpaper.name = parser.getAttributeValue(null, "name");
Christopher Tated7faf532016-02-25 12:43:38 -08003271 wallpaper.allowBackup = "true".equals(parser.getAttributeValue(null, "backup"));
Christopher Tatebe132e62016-02-10 12:59:49 -08003272 }
3273
Brad Fitzpatrick194b19a2010-09-14 11:30:29 -07003274 // Called by SystemBackupAgent after files are restored to disk.
Amith Yamasani09e9cdc2013-11-06 14:54:50 -08003275 public void settingsRestored() {
3276 // Verify caller is the system
3277 if (Binder.getCallingUid() != android.os.Process.SYSTEM_UID) {
3278 throw new RuntimeException("settingsRestored() can only be called from the system process");
3279 }
Amith Yamasani37ce3a82012-02-06 12:04:42 -08003280 // TODO: If necessary, make it work for secondary users as well. This currently assumes
3281 // restores only to the primary user
Joe Onorato8a9b2202010-02-26 18:56:32 -08003282 if (DEBUG) Slog.v(TAG, "settingsRestored");
Amith Yamasani37ce3a82012-02-06 12:04:42 -08003283 WallpaperData wallpaper = null;
Joe Onorato9bb8fd72009-07-28 18:24:51 -07003284 boolean success = false;
3285 synchronized (mLock) {
Christopher Tated7faf532016-02-25 12:43:38 -08003286 loadSettingsLocked(UserHandle.USER_SYSTEM, false);
Christopher Tatedb27b842016-02-25 14:39:17 -08003287 wallpaper = mWallpaperMap.get(UserHandle.USER_SYSTEM);
Christopher Tatead3c2592016-01-20 18:13:17 -08003288 wallpaper.wallpaperId = makeWallpaperIdLocked(); // always bump id at restore
Christopher Tated7faf532016-02-25 12:43:38 -08003289 wallpaper.allowBackup = true; // by definition if it was restored
Amith Yamasani37ce3a82012-02-06 12:04:42 -08003290 if (wallpaper.nextWallpaperComponent != null
Justin Koh29c30162014-09-05 17:10:10 -07003291 && !wallpaper.nextWallpaperComponent.equals(mImageWallpaper)) {
Amith Yamasani37ce3a82012-02-06 12:04:42 -08003292 if (!bindWallpaperComponentLocked(wallpaper.nextWallpaperComponent, false, false,
Dianne Hackborn5dc5a002012-09-15 19:33:48 -07003293 wallpaper, null)) {
Christopher Tatee3ab4d02009-12-16 14:03:31 -08003294 // No such live wallpaper or other failure; fall back to the default
3295 // live wallpaper (since the profile being restored indicated that the
3296 // user had selected a live rather than static one).
Dianne Hackborn5dc5a002012-09-15 19:33:48 -07003297 bindWallpaperComponentLocked(null, false, false, wallpaper, null);
Christopher Tatee3ab4d02009-12-16 14:03:31 -08003298 }
Joe Onorato9bb8fd72009-07-28 18:24:51 -07003299 success = true;
3300 } else {
Mike Clerona428b2c2009-11-15 22:53:08 -08003301 // If there's a wallpaper name, we use that. If that can't be loaded, then we
3302 // use the default.
Amith Yamasani37ce3a82012-02-06 12:04:42 -08003303 if ("".equals(wallpaper.name)) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08003304 if (DEBUG) Slog.v(TAG, "settingsRestored: name is empty");
Mike Clerona428b2c2009-11-15 22:53:08 -08003305 success = true;
3306 } else {
Joe Onorato8a9b2202010-02-26 18:56:32 -08003307 if (DEBUG) Slog.v(TAG, "settingsRestored: attempting to restore named resource");
Amith Yamasani37ce3a82012-02-06 12:04:42 -08003308 success = restoreNamedResourceLocked(wallpaper);
Mike Clerona428b2c2009-11-15 22:53:08 -08003309 }
Christopher Tatead3c2592016-01-20 18:13:17 -08003310 if (DEBUG) Slog.v(TAG, "settingsRestored: success=" + success
3311 + " id=" + wallpaper.wallpaperId);
Mike Clerona428b2c2009-11-15 22:53:08 -08003312 if (success) {
wilsonshih81e10a72018-11-15 10:54:21 +08003313 generateCrop(wallpaper); // based on the new image + metadata
Christopher Tate41297ff2016-03-10 16:46:15 -08003314 bindWallpaperComponentLocked(wallpaper.nextWallpaperComponent, true, false,
Dianne Hackborn5dc5a002012-09-15 19:33:48 -07003315 wallpaper, null);
Mike Clerona428b2c2009-11-15 22:53:08 -08003316 }
Joe Onorato9bb8fd72009-07-28 18:24:51 -07003317 }
3318 }
3319
3320 if (!success) {
Amith Yamasani37ce3a82012-02-06 12:04:42 -08003321 Slog.e(TAG, "Failed to restore wallpaper: '" + wallpaper.name + "'");
3322 wallpaper.name = "";
Christopher Tatedb27b842016-02-25 14:39:17 -08003323 getWallpaperDir(UserHandle.USER_SYSTEM).delete();
Joe Onorato9bb8fd72009-07-28 18:24:51 -07003324 }
Brad Fitzpatrick194b19a2010-09-14 11:30:29 -07003325
3326 synchronized (mLock) {
Christopher Tatedb27b842016-02-25 14:39:17 -08003327 saveSettingsLocked(UserHandle.USER_SYSTEM);
Brad Fitzpatrick194b19a2010-09-14 11:30:29 -07003328 }
Joe Onorato9bb8fd72009-07-28 18:24:51 -07003329 }
3330
Christopher Tate1e1e2e02016-01-25 15:34:36 -08003331 // Restore the named resource bitmap to both source + crop files
wilsonshiha282bf72018-11-30 12:48:05 +08003332 private boolean restoreNamedResourceLocked(WallpaperData wallpaper) {
Amith Yamasani37ce3a82012-02-06 12:04:42 -08003333 if (wallpaper.name.length() > 4 && "res:".equals(wallpaper.name.substring(0, 4))) {
3334 String resName = wallpaper.name.substring(4);
Joe Onorato9bb8fd72009-07-28 18:24:51 -07003335
3336 String pkg = null;
3337 int colon = resName.indexOf(':');
3338 if (colon > 0) {
3339 pkg = resName.substring(0, colon);
3340 }
3341
3342 String ident = null;
3343 int slash = resName.lastIndexOf('/');
3344 if (slash > 0) {
3345 ident = resName.substring(slash+1);
3346 }
3347
3348 String type = null;
3349 if (colon > 0 && slash > 0 && (slash-colon) > 1) {
3350 type = resName.substring(colon+1, slash);
3351 }
3352
3353 if (pkg != null && ident != null && type != null) {
3354 int resId = -1;
3355 InputStream res = null;
3356 FileOutputStream fos = null;
Christopher Tate1e1e2e02016-01-25 15:34:36 -08003357 FileOutputStream cos = null;
Joe Onorato9bb8fd72009-07-28 18:24:51 -07003358 try {
3359 Context c = mContext.createPackageContext(pkg, Context.CONTEXT_RESTRICTED);
3360 Resources r = c.getResources();
3361 resId = r.getIdentifier(resName, null, null);
3362 if (resId == 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08003363 Slog.e(TAG, "couldn't resolve identifier pkg=" + pkg + " type=" + type
Joe Onorato9bb8fd72009-07-28 18:24:51 -07003364 + " ident=" + ident);
3365 return false;
3366 }
3367
3368 res = r.openRawResource(resId);
Amith Yamasani37ce3a82012-02-06 12:04:42 -08003369 if (wallpaper.wallpaperFile.exists()) {
3370 wallpaper.wallpaperFile.delete();
Christopher Tate1e1e2e02016-01-25 15:34:36 -08003371 wallpaper.cropFile.delete();
Dianne Hackborn1afd1c92010-03-18 22:47:17 -07003372 }
Amith Yamasani37ce3a82012-02-06 12:04:42 -08003373 fos = new FileOutputStream(wallpaper.wallpaperFile);
Christopher Tate1e1e2e02016-01-25 15:34:36 -08003374 cos = new FileOutputStream(wallpaper.cropFile);
Joe Onorato9bb8fd72009-07-28 18:24:51 -07003375
3376 byte[] buffer = new byte[32768];
3377 int amt;
3378 while ((amt=res.read(buffer)) > 0) {
3379 fos.write(buffer, 0, amt);
Christopher Tate1e1e2e02016-01-25 15:34:36 -08003380 cos.write(buffer, 0, amt);
Joe Onorato9bb8fd72009-07-28 18:24:51 -07003381 }
3382 // mWallpaperObserver will notice the close and send the change broadcast
3383
Joe Onorato8a9b2202010-02-26 18:56:32 -08003384 Slog.v(TAG, "Restored wallpaper: " + resName);
Joe Onorato9bb8fd72009-07-28 18:24:51 -07003385 return true;
3386 } catch (NameNotFoundException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08003387 Slog.e(TAG, "Package name " + pkg + " not found");
Joe Onorato9bb8fd72009-07-28 18:24:51 -07003388 } catch (Resources.NotFoundException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08003389 Slog.e(TAG, "Resource not found: " + resId);
Joe Onorato9bb8fd72009-07-28 18:24:51 -07003390 } catch (IOException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08003391 Slog.e(TAG, "IOException while restoring wallpaper ", e);
Joe Onorato9bb8fd72009-07-28 18:24:51 -07003392 } finally {
Christopher Tatead3c2592016-01-20 18:13:17 -08003393 IoUtils.closeQuietly(res);
Joe Onorato9bb8fd72009-07-28 18:24:51 -07003394 if (fos != null) {
Dianne Hackborn8bdf5932010-10-15 12:54:40 -07003395 FileUtils.sync(fos);
Joe Onorato9bb8fd72009-07-28 18:24:51 -07003396 }
Christopher Tate1e1e2e02016-01-25 15:34:36 -08003397 if (cos != null) {
3398 FileUtils.sync(cos);
3399 }
3400 IoUtils.closeQuietly(fos);
3401 IoUtils.closeQuietly(cos);
Joe Onorato9bb8fd72009-07-28 18:24:51 -07003402 }
3403 }
3404 }
3405 return false;
3406 }
Amith Yamasani37ce3a82012-02-06 12:04:42 -08003407
Dianne Hackborneb034652009-09-07 00:49:58 -07003408 @Override
3409 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
Jeff Sharkeyfe9a53b2017-03-31 14:08:23 -06003410 if (!DumpUtils.checkDumpPermission(mContext, TAG, pw)) return;
Dianne Hackborneb034652009-09-07 00:49:58 -07003411
Felipe Leme9584cb6c2020-01-17 13:30:36 -08003412 pw.print("mDefaultWallpaperComponent="); pw.println(mDefaultWallpaperComponent);
3413 pw.print("mImageWallpaper="); pw.println(mImageWallpaper);
3414
Dianne Hackborneb034652009-09-07 00:49:58 -07003415 synchronized (mLock) {
Adrian Roosc28e3a92016-04-14 10:47:52 -07003416 pw.println("System wallpaper state:");
Amith Yamasani37ce3a82012-02-06 12:04:42 -08003417 for (int i = 0; i < mWallpaperMap.size(); i++) {
3418 WallpaperData wallpaper = mWallpaperMap.valueAt(i);
Christopher Tatead3c2592016-01-20 18:13:17 -08003419 pw.print(" User "); pw.print(wallpaper.userId);
wilsonshiha282bf72018-11-30 12:48:05 +08003420 pw.print(": id="); pw.println(wallpaper.wallpaperId);
3421 pw.println(" Display state:");
3422 forEachDisplayData(wpSize -> {
wilsonshih81e10a72018-11-15 10:54:21 +08003423 pw.print(" displayId=");
3424 pw.println(wpSize.mDisplayId);
3425 pw.print(" mWidth=");
3426 pw.print(wpSize.mWidth);
3427 pw.print(" mHeight=");
3428 pw.println(wpSize.mHeight);
3429 pw.print(" mPadding="); pw.println(wpSize.mPadding);
3430 });
Christopher Tate1e1e2e02016-01-25 15:34:36 -08003431 pw.print(" mCropHint="); pw.println(wallpaper.cropHint);
Dianne Hackborn067e5f62014-09-07 23:14:30 -07003432 pw.print(" mName="); pw.println(wallpaper.name);
Bryan Mawhinney0fa54f42017-07-06 17:09:37 +01003433 pw.print(" mAllowBackup="); pw.println(wallpaper.allowBackup);
Dianne Hackborn067e5f62014-09-07 23:14:30 -07003434 pw.print(" mWallpaperComponent="); pw.println(wallpaper.wallpaperComponent);
Amith Yamasani37ce3a82012-02-06 12:04:42 -08003435 if (wallpaper.connection != null) {
3436 WallpaperConnection conn = wallpaper.connection;
3437 pw.print(" Wallpaper connection ");
3438 pw.print(conn);
3439 pw.println(":");
3440 if (conn.mInfo != null) {
3441 pw.print(" mInfo.component=");
3442 pw.println(conn.mInfo.getComponent());
3443 }
wilsonshihde93f492018-11-01 21:23:40 +08003444 conn.forEachDisplayConnector(connector -> {
wilsonshiha282bf72018-11-30 12:48:05 +08003445 pw.print(" mDisplayId=");
wilsonshihde93f492018-11-01 21:23:40 +08003446 pw.println(connector.mDisplayId);
wilsonshiha282bf72018-11-30 12:48:05 +08003447 pw.print(" mToken=");
wilsonshihde93f492018-11-01 21:23:40 +08003448 pw.println(connector.mToken);
wilsonshiha282bf72018-11-30 12:48:05 +08003449 pw.print(" mEngine=");
wilsonshihde93f492018-11-01 21:23:40 +08003450 pw.println(connector.mEngine);
3451 });
Amith Yamasani37ce3a82012-02-06 12:04:42 -08003452 pw.print(" mService=");
3453 pw.println(conn.mService);
Amith Yamasani37ce3a82012-02-06 12:04:42 -08003454 pw.print(" mLastDiedTime=");
3455 pw.println(wallpaper.lastDiedTime - SystemClock.uptimeMillis());
3456 }
Dianne Hackborneb034652009-09-07 00:49:58 -07003457 }
Adrian Roosc28e3a92016-04-14 10:47:52 -07003458 pw.println("Lock wallpaper state:");
3459 for (int i = 0; i < mLockWallpaperMap.size(); i++) {
3460 WallpaperData wallpaper = mLockWallpaperMap.valueAt(i);
3461 pw.print(" User "); pw.print(wallpaper.userId);
wilsonshih81e10a72018-11-15 10:54:21 +08003462 pw.print(": id="); pw.println(wallpaper.wallpaperId);
Adrian Roosc28e3a92016-04-14 10:47:52 -07003463 pw.print(" mCropHint="); pw.println(wallpaper.cropHint);
Adrian Roosc28e3a92016-04-14 10:47:52 -07003464 pw.print(" mName="); pw.println(wallpaper.name);
Bryan Mawhinney0fa54f42017-07-06 17:09:37 +01003465 pw.print(" mAllowBackup="); pw.println(wallpaper.allowBackup);
Adrian Roosc28e3a92016-04-14 10:47:52 -07003466 }
wilsonshiha282bf72018-11-30 12:48:05 +08003467 pw.println("Fallback wallpaper state:");
3468 pw.print(" User "); pw.print(mFallbackWallpaper.userId);
3469 pw.print(": id="); pw.println(mFallbackWallpaper.wallpaperId);
3470 pw.print(" mCropHint="); pw.println(mFallbackWallpaper.cropHint);
3471 pw.print(" mName="); pw.println(mFallbackWallpaper.name);
3472 pw.print(" mAllowBackup="); pw.println(mFallbackWallpaper.allowBackup);
3473 if (mFallbackWallpaper.connection != null) {
3474 WallpaperConnection conn = mFallbackWallpaper.connection;
3475 pw.print(" Fallback Wallpaper connection ");
3476 pw.print(conn);
3477 pw.println(":");
3478 if (conn.mInfo != null) {
3479 pw.print(" mInfo.component=");
3480 pw.println(conn.mInfo.getComponent());
3481 }
3482 conn.forEachDisplayConnector(connector -> {
3483 pw.print(" mDisplayId=");
3484 pw.println(connector.mDisplayId);
3485 pw.print(" mToken=");
3486 pw.println(connector.mToken);
3487 pw.print(" mEngine=");
3488 pw.println(connector.mEngine);
3489 });
3490 pw.print(" mService=");
3491 pw.println(conn.mService);
3492 pw.print(" mLastDiedTime=");
3493 pw.println(mFallbackWallpaper.lastDiedTime - SystemClock.uptimeMillis());
3494 }
Dianne Hackborneb034652009-09-07 00:49:58 -07003495 }
3496 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003497}