The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2008 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
Amith Yamasani | 09e9cdc | 2013-11-06 14:54:50 -0800 | [diff] [blame] | 17 | package com.android.server.wallpaper; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 18 | |
Christopher Tate | be132e6 | 2016-02-10 12:59:49 -0800 | [diff] [blame] | 19 | import static android.app.WallpaperManager.FLAG_SET_SYSTEM; |
| 20 | import static android.app.WallpaperManager.FLAG_SET_LOCK; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 21 | import static android.os.ParcelFileDescriptor.*; |
Christopher Tate | 111bd4a | 2009-06-24 17:29:38 -0700 | [diff] [blame] | 22 | |
Christopher Tate | e409f0e | 2016-03-21 14:53:15 -0700 | [diff] [blame] | 23 | import android.app.ActivityManager; |
Dianne Hackborn | 5dc5a00 | 2012-09-15 19:33:48 -0700 | [diff] [blame] | 24 | import android.app.ActivityManagerNative; |
Amith Yamasani | 4e2820c | 2012-08-28 22:17:23 -0700 | [diff] [blame] | 25 | import android.app.AppGlobals; |
Benjamin Franz | f3ece36 | 2015-02-11 10:51:10 +0000 | [diff] [blame] | 26 | import android.app.AppOpsManager; |
Dianne Hackborn | 5dc5a00 | 2012-09-15 19:33:48 -0700 | [diff] [blame] | 27 | import android.app.IUserSwitchObserver; |
Dianne Hackborn | 8cc6a50 | 2009-08-05 21:29:42 -0700 | [diff] [blame] | 28 | import android.app.IWallpaperManager; |
| 29 | import android.app.IWallpaperManagerCallback; |
Dianne Hackborn | dd9b82c | 2009-09-03 00:18:47 -0700 | [diff] [blame] | 30 | import android.app.PendingIntent; |
Dianne Hackborn | eb03465 | 2009-09-07 00:49:58 -0700 | [diff] [blame] | 31 | import android.app.WallpaperInfo; |
Jeff Sharkey | 28f0877 | 2014-04-16 09:41:58 -0700 | [diff] [blame] | 32 | import android.app.WallpaperManager; |
Oleksandr Peletskyi | f251981 | 2016-01-26 20:16:06 +0100 | [diff] [blame] | 33 | import android.app.admin.DevicePolicyManager; |
Christopher Tate | 4528186 | 2010-03-05 15:46:30 -0800 | [diff] [blame] | 34 | import android.app.backup.BackupManager; |
Amith Yamasani | 37ce3a8 | 2012-02-06 12:04:42 -0800 | [diff] [blame] | 35 | import android.app.backup.WallpaperBackupHelper; |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 36 | import android.content.BroadcastReceiver; |
Dianne Hackborn | 4c62fc0 | 2009-08-08 20:40:27 -0700 | [diff] [blame] | 37 | import android.content.ComponentName; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 38 | import android.content.Context; |
| 39 | import android.content.Intent; |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 40 | import android.content.IntentFilter; |
Dianne Hackborn | 4c62fc0 | 2009-08-08 20:40:27 -0700 | [diff] [blame] | 41 | import android.content.ServiceConnection; |
Amith Yamasani | 4e2820c | 2012-08-28 22:17:23 -0700 | [diff] [blame] | 42 | import android.content.pm.IPackageManager; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 43 | import android.content.pm.PackageManager; |
Dianne Hackborn | 4c62fc0 | 2009-08-08 20:40:27 -0700 | [diff] [blame] | 44 | import android.content.pm.ResolveInfo; |
| 45 | import android.content.pm.ServiceInfo; |
Joe Onorato | 9bb8fd7 | 2009-07-28 18:24:51 -0700 | [diff] [blame] | 46 | import android.content.pm.PackageManager.NameNotFoundException; |
Amith Yamasani | 6474c4c | 2012-10-04 14:55:42 -0700 | [diff] [blame] | 47 | import android.content.pm.UserInfo; |
Joe Onorato | 9bb8fd7 | 2009-07-28 18:24:51 -0700 | [diff] [blame] | 48 | import android.content.res.Resources; |
Christopher Tate | 1e1e2e0 | 2016-01-25 15:34:36 -0800 | [diff] [blame] | 49 | import android.graphics.Bitmap; |
| 50 | import android.graphics.BitmapFactory; |
| 51 | import android.graphics.BitmapRegionDecoder; |
John Spurlock | 41f6464 | 2013-11-04 13:48:38 -0500 | [diff] [blame] | 52 | import android.graphics.Point; |
Dianne Hackborn | 067e5f6 | 2014-09-07 23:14:30 -0700 | [diff] [blame] | 53 | import android.graphics.Rect; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 54 | import android.os.Binder; |
Dianne Hackborn | 284ac93 | 2009-08-28 10:34:25 -0700 | [diff] [blame] | 55 | import android.os.Bundle; |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 56 | import android.os.Environment; |
Dianne Hackborn | 8bdf593 | 2010-10-15 12:54:40 -0700 | [diff] [blame] | 57 | import android.os.FileUtils; |
Dianne Hackborn | 4c62fc0 | 2009-08-08 20:40:27 -0700 | [diff] [blame] | 58 | import android.os.IBinder; |
Dianne Hackborn | 5dc5a00 | 2012-09-15 19:33:48 -0700 | [diff] [blame] | 59 | import android.os.IRemoteCallback; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 60 | import android.os.RemoteException; |
| 61 | import android.os.FileObserver; |
| 62 | import android.os.ParcelFileDescriptor; |
| 63 | import android.os.RemoteCallbackList; |
rpcraig | 554cb0c | 2012-07-05 06:41:43 -0400 | [diff] [blame] | 64 | import android.os.SELinux; |
Dianne Hackborn | 4c62fc0 | 2009-08-08 20:40:27 -0700 | [diff] [blame] | 65 | import android.os.ServiceManager; |
Dianne Hackborn | 0cd4887 | 2009-08-13 18:51:59 -0700 | [diff] [blame] | 66 | import android.os.SystemClock; |
Dianne Hackborn | f02b60a | 2012-08-16 10:48:27 -0700 | [diff] [blame] | 67 | import android.os.UserHandle; |
Amith Yamasani | 6474c4c | 2012-10-04 14:55:42 -0700 | [diff] [blame] | 68 | import android.os.UserManager; |
Dianne Hackborn | 4c62fc0 | 2009-08-08 20:40:27 -0700 | [diff] [blame] | 69 | import android.service.wallpaper.IWallpaperConnection; |
| 70 | import android.service.wallpaper.IWallpaperEngine; |
| 71 | import android.service.wallpaper.IWallpaperService; |
| 72 | import android.service.wallpaper.WallpaperService; |
Filip Gruszczynski | 5dcc3ac | 2014-10-13 15:51:39 -0700 | [diff] [blame] | 73 | import android.util.EventLog; |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 74 | import android.util.Slog; |
Amith Yamasani | 37ce3a8 | 2012-02-06 12:04:42 -0800 | [diff] [blame] | 75 | import android.util.SparseArray; |
Joe Onorato | 9bb8fd7 | 2009-07-28 18:24:51 -0700 | [diff] [blame] | 76 | import android.util.Xml; |
Dianne Hackborn | 44bc17c | 2011-04-20 18:18:51 -0700 | [diff] [blame] | 77 | import android.view.Display; |
Dianne Hackborn | 4c62fc0 | 2009-08-08 20:40:27 -0700 | [diff] [blame] | 78 | import android.view.IWindowManager; |
| 79 | import android.view.WindowManager; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 80 | |
Christopher Tate | 1e1e2e0 | 2016-01-25 15:34:36 -0800 | [diff] [blame] | 81 | import java.io.BufferedOutputStream; |
Dianne Hackborn | eb03465 | 2009-09-07 00:49:58 -0700 | [diff] [blame] | 82 | import java.io.FileDescriptor; |
Joe Onorato | 9bb8fd7 | 2009-07-28 18:24:51 -0700 | [diff] [blame] | 83 | import java.io.IOException; |
| 84 | import java.io.InputStream; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 85 | import java.io.File; |
| 86 | import java.io.FileNotFoundException; |
Joe Onorato | 9bb8fd7 | 2009-07-28 18:24:51 -0700 | [diff] [blame] | 87 | import java.io.FileInputStream; |
| 88 | import java.io.FileOutputStream; |
Dianne Hackborn | eb03465 | 2009-09-07 00:49:58 -0700 | [diff] [blame] | 89 | import java.io.PrintWriter; |
Wojciech Staszkiewicz | 9e9e2e7 | 2015-05-08 14:58:46 +0100 | [diff] [blame] | 90 | import java.nio.charset.StandardCharsets; |
Oleksandr Peletskyi | f251981 | 2016-01-26 20:16:06 +0100 | [diff] [blame] | 91 | import java.util.Arrays; |
Dianne Hackborn | 4c62fc0 | 2009-08-08 20:40:27 -0700 | [diff] [blame] | 92 | import java.util.List; |
Joe Onorato | 9bb8fd7 | 2009-07-28 18:24:51 -0700 | [diff] [blame] | 93 | |
| 94 | import org.xmlpull.v1.XmlPullParser; |
| 95 | import org.xmlpull.v1.XmlPullParserException; |
| 96 | import org.xmlpull.v1.XmlSerializer; |
| 97 | |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 98 | import com.android.internal.content.PackageMonitor; |
Dianne Hackborn | 2269d157 | 2010-02-24 19:54:22 -0800 | [diff] [blame] | 99 | import com.android.internal.util.FastXmlSerializer; |
Dianne Hackborn | 1afd1c9 | 2010-03-18 22:47:17 -0700 | [diff] [blame] | 100 | import com.android.internal.util.JournaledFile; |
Justin Koh | 29c3016 | 2014-09-05 17:10:10 -0700 | [diff] [blame] | 101 | import com.android.internal.R; |
Filip Gruszczynski | 5dcc3ac | 2014-10-13 15:51:39 -0700 | [diff] [blame] | 102 | import com.android.server.EventLogTags; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 103 | |
Christopher Tate | ad3c259 | 2016-01-20 18:13:17 -0800 | [diff] [blame] | 104 | import libcore.io.IoUtils; |
| 105 | |
Amith Yamasani | 09e9cdc | 2013-11-06 14:54:50 -0800 | [diff] [blame] | 106 | public class WallpaperManagerService extends IWallpaperManager.Stub { |
| 107 | static final String TAG = "WallpaperManagerService"; |
Joe Onorato | 7e1693a | 2016-02-01 17:45:03 -0800 | [diff] [blame] | 108 | static final boolean DEBUG = false; |
Joe Onorato | 9bb8fd7 | 2009-07-28 18:24:51 -0700 | [diff] [blame] | 109 | |
Christopher Tate | be132e6 | 2016-02-10 12:59:49 -0800 | [diff] [blame] | 110 | final Object mLock = new Object(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 111 | |
Dianne Hackborn | 0cd4887 | 2009-08-13 18:51:59 -0700 | [diff] [blame] | 112 | /** |
| 113 | * Minimum time between crashes of a wallpaper service for us to consider |
| 114 | * restarting it vs. just reverting to the static wallpaper. |
| 115 | */ |
| 116 | static final long MIN_WALLPAPER_CRASH_TIME = 10000; |
Filip Gruszczynski | 5dcc3ac | 2014-10-13 15:51:39 -0700 | [diff] [blame] | 117 | static final int MAX_WALLPAPER_COMPONENT_LOG_LENGTH = 128; |
Christopher Tate | 1e1e2e0 | 2016-01-25 15:34:36 -0800 | [diff] [blame] | 118 | static final String WALLPAPER = "wallpaper_orig"; |
| 119 | static final String WALLPAPER_CROP = "wallpaper"; |
Christopher Tate | be132e6 | 2016-02-10 12:59:49 -0800 | [diff] [blame] | 120 | static final String WALLPAPER_LOCK_ORIG = "wallpaper_lock_orig"; |
| 121 | static final String WALLPAPER_LOCK_CROP = "wallpaper_lock"; |
Amith Yamasani | 37ce3a8 | 2012-02-06 12:04:42 -0800 | [diff] [blame] | 122 | static final String WALLPAPER_INFO = "wallpaper_info.xml"; |
Dianne Hackborn | bce0cbb | 2012-10-05 11:06:53 -0700 | [diff] [blame] | 123 | |
Christopher Tate | be132e6 | 2016-02-10 12:59:49 -0800 | [diff] [blame] | 124 | // All the various per-user state files we need to be aware of |
| 125 | static final String[] sPerUserFiles = new String[] { |
| 126 | WALLPAPER, WALLPAPER_CROP, |
| 127 | WALLPAPER_LOCK_ORIG, WALLPAPER_LOCK_CROP, |
| 128 | WALLPAPER_INFO |
| 129 | }; |
| 130 | |
Dianne Hackborn | bce0cbb | 2012-10-05 11:06:53 -0700 | [diff] [blame] | 131 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 132 | * Observes the wallpaper for changes and notifies all IWallpaperServiceCallbacks |
| 133 | * that the wallpaper has changed. The CREATE is triggered when there is no |
| 134 | * wallpaper set and is created for the first time. The CLOSE_WRITE is triggered |
| 135 | * everytime the wallpaper is changed. |
| 136 | */ |
Amith Yamasani | 37ce3a8 | 2012-02-06 12:04:42 -0800 | [diff] [blame] | 137 | private class WallpaperObserver extends FileObserver { |
Joe Onorato | 9bb8fd7 | 2009-07-28 18:24:51 -0700 | [diff] [blame] | 138 | |
Christopher Tate | be132e6 | 2016-02-10 12:59:49 -0800 | [diff] [blame] | 139 | final int mUserId; |
Amith Yamasani | 37ce3a8 | 2012-02-06 12:04:42 -0800 | [diff] [blame] | 140 | final WallpaperData mWallpaper; |
| 141 | final File mWallpaperDir; |
| 142 | final File mWallpaperFile; |
Christopher Tate | be132e6 | 2016-02-10 12:59:49 -0800 | [diff] [blame] | 143 | final File mWallpaperLockFile; |
Amith Yamasani | 52d750c | 2014-12-10 14:09:05 -0800 | [diff] [blame] | 144 | final File mWallpaperInfoFile; |
Amith Yamasani | 37ce3a8 | 2012-02-06 12:04:42 -0800 | [diff] [blame] | 145 | |
| 146 | public WallpaperObserver(WallpaperData wallpaper) { |
| 147 | super(getWallpaperDir(wallpaper.userId).getAbsolutePath(), |
Christopher Tate | da058e2 | 2014-10-08 14:51:09 -0700 | [diff] [blame] | 148 | CLOSE_WRITE | MOVED_TO | DELETE | DELETE_SELF); |
Christopher Tate | be132e6 | 2016-02-10 12:59:49 -0800 | [diff] [blame] | 149 | mUserId = wallpaper.userId; |
Amith Yamasani | 37ce3a8 | 2012-02-06 12:04:42 -0800 | [diff] [blame] | 150 | mWallpaperDir = getWallpaperDir(wallpaper.userId); |
| 151 | mWallpaper = wallpaper; |
| 152 | mWallpaperFile = new File(mWallpaperDir, WALLPAPER); |
Christopher Tate | be132e6 | 2016-02-10 12:59:49 -0800 | [diff] [blame] | 153 | mWallpaperLockFile = new File(mWallpaperDir, WALLPAPER_LOCK_ORIG); |
Amith Yamasani | 52d750c | 2014-12-10 14:09:05 -0800 | [diff] [blame] | 154 | mWallpaperInfoFile = new File(mWallpaperDir, WALLPAPER_INFO); |
Amith Yamasani | 37ce3a8 | 2012-02-06 12:04:42 -0800 | [diff] [blame] | 155 | } |
| 156 | |
Christopher Tate | be132e6 | 2016-02-10 12:59:49 -0800 | [diff] [blame] | 157 | private WallpaperData dataForEvent(boolean sysChanged, boolean lockChanged) { |
| 158 | WallpaperData wallpaper = null; |
| 159 | synchronized (mLock) { |
| 160 | if (lockChanged) { |
| 161 | wallpaper = mLockWallpaperMap.get(mUserId); |
| 162 | } |
| 163 | if (wallpaper == null) { |
| 164 | // no lock-specific wallpaper exists, or sys case, handled together |
| 165 | wallpaper = mWallpaperMap.get(mUserId); |
| 166 | } |
| 167 | } |
| 168 | return (wallpaper != null) ? wallpaper : mWallpaper; |
| 169 | } |
| 170 | |
Amith Yamasani | 37ce3a8 | 2012-02-06 12:04:42 -0800 | [diff] [blame] | 171 | @Override |
| 172 | public void onEvent(int event, String path) { |
| 173 | if (path == null) { |
| 174 | return; |
| 175 | } |
Christopher Tate | 1e1e2e0 | 2016-01-25 15:34:36 -0800 | [diff] [blame] | 176 | final boolean written = (event == CLOSE_WRITE || event == MOVED_TO); |
| 177 | final File changedFile = new File(mWallpaperDir, path); |
| 178 | |
Christopher Tate | be132e6 | 2016-02-10 12:59:49 -0800 | [diff] [blame] | 179 | // System and system+lock changes happen on the system wallpaper input file; |
| 180 | // lock-only changes happen on the dedicated lock wallpaper input file |
| 181 | final boolean sysWallpaperChanged = (mWallpaperFile.equals(changedFile)); |
| 182 | final boolean lockWallpaperChanged = (mWallpaperLockFile.equals(changedFile)); |
| 183 | WallpaperData wallpaper = dataForEvent(sysWallpaperChanged, lockWallpaperChanged); |
| 184 | |
| 185 | if (DEBUG) { |
| 186 | Slog.v(TAG, "Wallpaper file change: evt=" + event |
| 187 | + " path=" + path |
| 188 | + " sys=" + sysWallpaperChanged |
| 189 | + " lock=" + lockWallpaperChanged |
| 190 | + " imagePending=" + wallpaper.imageWallpaperPending |
| 191 | + " whichPending=0x" + Integer.toHexString(wallpaper.whichPending) |
| 192 | + " written=" + written); |
| 193 | } |
Amith Yamasani | 37ce3a8 | 2012-02-06 12:04:42 -0800 | [diff] [blame] | 194 | synchronized (mLock) { |
Christopher Tate | be132e6 | 2016-02-10 12:59:49 -0800 | [diff] [blame] | 195 | if (sysWallpaperChanged || mWallpaperInfoFile.equals(changedFile)) { |
Amith Yamasani | 52d750c | 2014-12-10 14:09:05 -0800 | [diff] [blame] | 196 | // changing the wallpaper means we'll need to back up the new one |
| 197 | long origId = Binder.clearCallingIdentity(); |
| 198 | BackupManager bm = new BackupManager(mContext); |
| 199 | bm.dataChanged(); |
| 200 | Binder.restoreCallingIdentity(origId); |
| 201 | } |
Christopher Tate | be132e6 | 2016-02-10 12:59:49 -0800 | [diff] [blame] | 202 | if (sysWallpaperChanged || lockWallpaperChanged) { |
| 203 | notifyCallbacksLocked(wallpaper); |
| 204 | if (wallpaper.wallpaperComponent == null |
Christopher Tate | da058e2 | 2014-10-08 14:51:09 -0700 | [diff] [blame] | 205 | || event != CLOSE_WRITE // includes the MOVED_TO case |
Christopher Tate | be132e6 | 2016-02-10 12:59:49 -0800 | [diff] [blame] | 206 | || wallpaper.imageWallpaperPending) { |
Christopher Tate | da058e2 | 2014-10-08 14:51:09 -0700 | [diff] [blame] | 207 | if (written) { |
Christopher Tate | 1e1e2e0 | 2016-01-25 15:34:36 -0800 | [diff] [blame] | 208 | // The image source has finished writing the source image, |
| 209 | // so we now produce the crop rect (in the background), and |
| 210 | // only publish the new displayable (sub)image as a result |
| 211 | // of that work. |
Christopher Tate | be132e6 | 2016-02-10 12:59:49 -0800 | [diff] [blame] | 212 | if (DEBUG) { |
| 213 | Slog.v(TAG, "Wallpaper written; generating crop"); |
| 214 | } |
| 215 | generateCrop(wallpaper); |
| 216 | if (DEBUG) { |
| 217 | Slog.v(TAG, "Crop done; invoking completion callback"); |
| 218 | } |
| 219 | wallpaper.imageWallpaperPending = false; |
| 220 | if (wallpaper.setComplete != null) { |
Christopher Tate | 1e1e2e0 | 2016-01-25 15:34:36 -0800 | [diff] [blame] | 221 | try { |
Christopher Tate | be132e6 | 2016-02-10 12:59:49 -0800 | [diff] [blame] | 222 | wallpaper.setComplete.onWallpaperChanged(); |
Christopher Tate | 1e1e2e0 | 2016-01-25 15:34:36 -0800 | [diff] [blame] | 223 | } catch (RemoteException e) { |
| 224 | // if this fails we don't really care; the setting app may just |
| 225 | // have crashed and that sort of thing is a fact of life. |
| 226 | } |
| 227 | } |
Christopher Tate | be132e6 | 2016-02-10 12:59:49 -0800 | [diff] [blame] | 228 | if (sysWallpaperChanged) { |
| 229 | // If this was the system wallpaper, rebind... |
| 230 | bindWallpaperComponentLocked(mImageWallpaper, true, |
| 231 | false, wallpaper, null); |
| 232 | } |
| 233 | if (lockWallpaperChanged |
| 234 | || (wallpaper.whichPending & FLAG_SET_LOCK) != 0) { |
Christopher Tate | be132e6 | 2016-02-10 12:59:49 -0800 | [diff] [blame] | 235 | if (DEBUG) { |
Christopher Tate | db27b84 | 2016-02-25 14:39:17 -0800 | [diff] [blame] | 236 | Slog.i(TAG, "Lock-relevant wallpaper changed"); |
Christopher Tate | be132e6 | 2016-02-10 12:59:49 -0800 | [diff] [blame] | 237 | } |
Christopher Tate | db27b84 | 2016-02-25 14:39:17 -0800 | [diff] [blame] | 238 | // either a lock-only wallpaper commit or a system+lock event. |
| 239 | // if it's system-plus-lock we need to wipe the lock bookkeeping; |
| 240 | // we're falling back to displaying the system wallpaper there. |
| 241 | if (!lockWallpaperChanged) { |
| 242 | mLockWallpaperMap.remove(wallpaper.userId); |
| 243 | } |
| 244 | // and in any case, tell keyguard about it |
Christopher Tate | be132e6 | 2016-02-10 12:59:49 -0800 | [diff] [blame] | 245 | final IWallpaperManagerCallback cb = mKeyguardListener; |
| 246 | if (cb != null) { |
| 247 | try { |
| 248 | cb.onWallpaperChanged(); |
| 249 | } catch (RemoteException e) { |
| 250 | // Oh well it went away; no big deal |
| 251 | } |
| 252 | } |
| 253 | } |
Christopher Tate | db27b84 | 2016-02-25 14:39:17 -0800 | [diff] [blame] | 254 | saveSettingsLocked(wallpaper.userId); |
Joe Onorato | 9bb8fd7 | 2009-07-28 18:24:51 -0700 | [diff] [blame] | 255 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 256 | } |
| 257 | } |
Amith Yamasani | 37ce3a8 | 2012-02-06 12:04:42 -0800 | [diff] [blame] | 258 | } |
| 259 | } |
| 260 | } |
| 261 | |
Christopher Tate | 1e1e2e0 | 2016-01-25 15:34:36 -0800 | [diff] [blame] | 262 | /** |
| 263 | * Once a new wallpaper has been written via setWallpaper(...), it needs to be cropped |
| 264 | * for display. |
| 265 | */ |
| 266 | private void generateCrop(WallpaperData wallpaper) { |
| 267 | boolean success = false; |
| 268 | boolean needCrop = false; |
Christopher Tate | be132e6 | 2016-02-10 12:59:49 -0800 | [diff] [blame] | 269 | boolean needScale = false; |
| 270 | |
| 271 | if (DEBUG) { |
| 272 | Slog.v(TAG, "Generating crop for new wallpaper(s): 0x" |
| 273 | + Integer.toHexString(wallpaper.whichPending) |
| 274 | + " to " + wallpaper.cropFile.getName()); |
| 275 | } |
Christopher Tate | 1e1e2e0 | 2016-01-25 15:34:36 -0800 | [diff] [blame] | 276 | |
| 277 | // Analyse the source; needed in multiple cases |
| 278 | BitmapFactory.Options options = new BitmapFactory.Options(); |
| 279 | options.inJustDecodeBounds = true; |
| 280 | BitmapFactory.decodeFile(wallpaper.wallpaperFile.getAbsolutePath(), options); |
| 281 | |
Christopher Tate | be132e6 | 2016-02-10 12:59:49 -0800 | [diff] [blame] | 282 | // We'll need to scale if the crop is sufficiently bigger than the display |
| 283 | |
Christopher Tate | 1e1e2e0 | 2016-01-25 15:34:36 -0800 | [diff] [blame] | 284 | // Legacy case uses an empty crop rect here, so we just preserve the |
| 285 | // source image verbatim |
| 286 | if (!wallpaper.cropHint.isEmpty()) { |
| 287 | // ...clamp the crop rect to the measured bounds... |
| 288 | wallpaper.cropHint.right = Math.min(wallpaper.cropHint.right, options.outWidth); |
| 289 | wallpaper.cropHint.bottom = Math.min(wallpaper.cropHint.bottom, options.outHeight); |
| 290 | // ...and don't bother cropping if what we're left with is identity |
| 291 | needCrop = (options.outHeight >= wallpaper.cropHint.height() |
| 292 | && options.outWidth >= wallpaper.cropHint.width()); |
| 293 | } |
| 294 | |
Christopher Tate | be132e6 | 2016-02-10 12:59:49 -0800 | [diff] [blame] | 295 | if (!needCrop && !needScale) { |
Christopher Tate | 1e1e2e0 | 2016-01-25 15:34:36 -0800 | [diff] [blame] | 296 | // Simple case: the nominal crop is at least as big as the source image, |
| 297 | // so we take the whole thing and just copy the image file directly. |
| 298 | if (DEBUG) { |
| 299 | Slog.v(TAG, "Null crop of new wallpaper; copying"); |
| 300 | } |
| 301 | success = FileUtils.copyFile(wallpaper.wallpaperFile, wallpaper.cropFile); |
| 302 | if (!success) { |
| 303 | wallpaper.cropFile.delete(); |
| 304 | // TODO: fall back to default wallpaper in this case |
| 305 | } |
| 306 | } else { |
Christopher Tate | be132e6 | 2016-02-10 12:59:49 -0800 | [diff] [blame] | 307 | // Fancy case: crop and/or scale |
Christopher Tate | 1e1e2e0 | 2016-01-25 15:34:36 -0800 | [diff] [blame] | 308 | FileOutputStream f = null; |
| 309 | BufferedOutputStream bos = null; |
| 310 | try { |
| 311 | BitmapRegionDecoder decoder = BitmapRegionDecoder.newInstance( |
| 312 | wallpaper.wallpaperFile.getAbsolutePath(), false); |
| 313 | Bitmap cropped = decoder.decodeRegion(wallpaper.cropHint, null); |
| 314 | decoder.recycle(); |
| 315 | |
| 316 | if (cropped == null) { |
| 317 | Slog.e(TAG, "Could not decode new wallpaper"); |
| 318 | } else { |
| 319 | f = new FileOutputStream(wallpaper.cropFile); |
| 320 | bos = new BufferedOutputStream(f, 32*1024); |
| 321 | cropped.compress(Bitmap.CompressFormat.PNG, 90, bos); |
| 322 | bos.flush(); // don't rely on the implicit flush-at-close when noting success |
| 323 | success = true; |
| 324 | } |
| 325 | } catch (IOException e) { |
| 326 | if (DEBUG) { |
| 327 | Slog.e(TAG, "I/O error decoding crop: " + e.getMessage()); |
| 328 | } |
| 329 | } finally { |
| 330 | IoUtils.closeQuietly(bos); |
| 331 | IoUtils.closeQuietly(f); |
| 332 | } |
| 333 | } |
| 334 | |
| 335 | if (!success) { |
| 336 | Slog.e(TAG, "Unable to apply new wallpaper"); |
| 337 | wallpaper.cropFile.delete(); |
| 338 | } |
| 339 | |
| 340 | if (wallpaper.cropFile.exists()) { |
| 341 | boolean didRestorecon = SELinux.restorecon(wallpaper.cropFile.getAbsoluteFile()); |
| 342 | if (DEBUG) { |
| 343 | Slog.v(TAG, "restorecon() of crop file returned " + didRestorecon); |
| 344 | } |
| 345 | } |
| 346 | } |
| 347 | |
Dianne Hackborn | 4c62fc0 | 2009-08-08 20:40:27 -0700 | [diff] [blame] | 348 | final Context mContext; |
| 349 | final IWindowManager mIWindowManager; |
Amith Yamasani | 4e2820c | 2012-08-28 22:17:23 -0700 | [diff] [blame] | 350 | final IPackageManager mIPackageManager; |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 351 | final MyPackageMonitor mMonitor; |
Benjamin Franz | f3ece36 | 2015-02-11 10:51:10 +0000 | [diff] [blame] | 352 | final AppOpsManager mAppOpsManager; |
Amith Yamasani | 37ce3a8 | 2012-02-06 12:04:42 -0800 | [diff] [blame] | 353 | WallpaperData mLastWallpaper; |
Christopher Tate | be132e6 | 2016-02-10 12:59:49 -0800 | [diff] [blame] | 354 | IWallpaperManagerCallback mKeyguardListener; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 355 | |
Justin Koh | 29c3016 | 2014-09-05 17:10:10 -0700 | [diff] [blame] | 356 | /** |
Christopher Tate | ad3c259 | 2016-01-20 18:13:17 -0800 | [diff] [blame] | 357 | * ID of the current wallpaper, changed every time anything sets a wallpaper. |
| 358 | * This is used for external detection of wallpaper update activity. |
| 359 | */ |
| 360 | int mWallpaperId; |
| 361 | |
| 362 | /** |
Justin Koh | 29c3016 | 2014-09-05 17:10:10 -0700 | [diff] [blame] | 363 | * Name of the component used to display bitmap wallpapers from either the gallery or |
| 364 | * built-in wallpapers. |
| 365 | */ |
| 366 | final ComponentName mImageWallpaper; |
| 367 | |
Christopher Tate | be132e6 | 2016-02-10 12:59:49 -0800 | [diff] [blame] | 368 | final SparseArray<WallpaperData> mWallpaperMap = new SparseArray<WallpaperData>(); |
| 369 | final SparseArray<WallpaperData> mLockWallpaperMap = new SparseArray<WallpaperData>(); |
Dianne Hackborn | 07213e6 | 2011-08-24 20:05:39 -0700 | [diff] [blame] | 370 | |
Amith Yamasani | 37ce3a8 | 2012-02-06 12:04:42 -0800 | [diff] [blame] | 371 | int mCurrentUserId; |
Dianne Hackborn | 07213e6 | 2011-08-24 20:05:39 -0700 | [diff] [blame] | 372 | |
Amith Yamasani | 37ce3a8 | 2012-02-06 12:04:42 -0800 | [diff] [blame] | 373 | static class WallpaperData { |
| 374 | |
| 375 | int userId; |
| 376 | |
Christopher Tate | be132e6 | 2016-02-10 12:59:49 -0800 | [diff] [blame] | 377 | final File wallpaperFile; // source image |
| 378 | final File cropFile; // eventual destination |
Amith Yamasani | 37ce3a8 | 2012-02-06 12:04:42 -0800 | [diff] [blame] | 379 | |
| 380 | /** |
Christopher Tate | be132e6 | 2016-02-10 12:59:49 -0800 | [diff] [blame] | 381 | * True while the client is writing a new wallpaper |
Amith Yamasani | 37ce3a8 | 2012-02-06 12:04:42 -0800 | [diff] [blame] | 382 | */ |
| 383 | boolean imageWallpaperPending; |
| 384 | |
| 385 | /** |
Christopher Tate | be132e6 | 2016-02-10 12:59:49 -0800 | [diff] [blame] | 386 | * Which new wallpapers are being written; mirrors the 'which' |
| 387 | * selector bit field to setWallpaper(). |
| 388 | */ |
| 389 | int whichPending; |
| 390 | |
| 391 | /** |
Christopher Tate | 1e1e2e0 | 2016-01-25 15:34:36 -0800 | [diff] [blame] | 392 | * Callback once the set + crop is finished |
| 393 | */ |
| 394 | IWallpaperManagerCallback setComplete; |
| 395 | |
| 396 | /** |
Amith Yamasani | 37ce3a8 | 2012-02-06 12:04:42 -0800 | [diff] [blame] | 397 | * Resource name if using a picture from the wallpaper gallery |
| 398 | */ |
| 399 | String name = ""; |
| 400 | |
| 401 | /** |
| 402 | * The component name of the currently set live wallpaper. |
| 403 | */ |
| 404 | ComponentName wallpaperComponent; |
| 405 | |
| 406 | /** |
| 407 | * The component name of the wallpaper that should be set next. |
| 408 | */ |
| 409 | ComponentName nextWallpaperComponent; |
| 410 | |
Christopher Tate | ad3c259 | 2016-01-20 18:13:17 -0800 | [diff] [blame] | 411 | /** |
| 412 | * The ID of this wallpaper |
| 413 | */ |
| 414 | int wallpaperId; |
| 415 | |
Amith Yamasani | 37ce3a8 | 2012-02-06 12:04:42 -0800 | [diff] [blame] | 416 | WallpaperConnection connection; |
| 417 | long lastDiedTime; |
| 418 | boolean wallpaperUpdating; |
| 419 | WallpaperObserver wallpaperObserver; |
| 420 | |
| 421 | /** |
| 422 | * List of callbacks registered they should each be notified when the wallpaper is changed. |
| 423 | */ |
| 424 | private RemoteCallbackList<IWallpaperManagerCallback> callbacks |
| 425 | = new RemoteCallbackList<IWallpaperManagerCallback>(); |
| 426 | |
| 427 | int width = -1; |
| 428 | int height = -1; |
| 429 | |
Christopher Tate | 1e1e2e0 | 2016-01-25 15:34:36 -0800 | [diff] [blame] | 430 | /** |
| 431 | * The crop hint supplied for displaying a subset of the source image |
| 432 | */ |
| 433 | final Rect cropHint = new Rect(0, 0, 0, 0); |
| 434 | |
Dianne Hackborn | 067e5f6 | 2014-09-07 23:14:30 -0700 | [diff] [blame] | 435 | final Rect padding = new Rect(0, 0, 0, 0); |
| 436 | |
Christopher Tate | be132e6 | 2016-02-10 12:59:49 -0800 | [diff] [blame] | 437 | WallpaperData(int userId, String inputFileName, String cropFileName) { |
Amith Yamasani | 37ce3a8 | 2012-02-06 12:04:42 -0800 | [diff] [blame] | 438 | this.userId = userId; |
Christopher Tate | be132e6 | 2016-02-10 12:59:49 -0800 | [diff] [blame] | 439 | final File wallpaperDir = getWallpaperDir(userId); |
| 440 | wallpaperFile = new File(wallpaperDir, inputFileName); |
| 441 | cropFile = new File(wallpaperDir, cropFileName); |
Christopher Tate | 1e1e2e0 | 2016-01-25 15:34:36 -0800 | [diff] [blame] | 442 | } |
| 443 | |
Christopher Tate | be132e6 | 2016-02-10 12:59:49 -0800 | [diff] [blame] | 444 | // Called during initialization of a given user's wallpaper bookkeeping |
Christopher Tate | 41297ff | 2016-03-10 16:46:15 -0800 | [diff] [blame] | 445 | boolean cropExists() { |
| 446 | return cropFile.exists(); |
Amith Yamasani | 37ce3a8 | 2012-02-06 12:04:42 -0800 | [diff] [blame] | 447 | } |
| 448 | } |
| 449 | |
Christopher Tate | ad3c259 | 2016-01-20 18:13:17 -0800 | [diff] [blame] | 450 | int makeWallpaperIdLocked() { |
| 451 | do { |
| 452 | ++mWallpaperId; |
| 453 | } while (mWallpaperId == 0); |
| 454 | return mWallpaperId; |
| 455 | } |
| 456 | |
Dianne Hackborn | 4c62fc0 | 2009-08-08 20:40:27 -0700 | [diff] [blame] | 457 | class WallpaperConnection extends IWallpaperConnection.Stub |
| 458 | implements ServiceConnection { |
Dianne Hackborn | eb03465 | 2009-09-07 00:49:58 -0700 | [diff] [blame] | 459 | final WallpaperInfo mInfo; |
Dianne Hackborn | 4c62fc0 | 2009-08-08 20:40:27 -0700 | [diff] [blame] | 460 | final Binder mToken = new Binder(); |
| 461 | IWallpaperService mService; |
| 462 | IWallpaperEngine mEngine; |
Amith Yamasani | 37ce3a8 | 2012-02-06 12:04:42 -0800 | [diff] [blame] | 463 | WallpaperData mWallpaper; |
Dianne Hackborn | 5dc5a00 | 2012-09-15 19:33:48 -0700 | [diff] [blame] | 464 | IRemoteCallback mReply; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 465 | |
Michael Wright | 5203a8b | 2013-10-03 14:16:42 -0700 | [diff] [blame] | 466 | boolean mDimensionsChanged = false; |
Dianne Hackborn | 067e5f6 | 2014-09-07 23:14:30 -0700 | [diff] [blame] | 467 | boolean mPaddingChanged = false; |
Michael Wright | 5203a8b | 2013-10-03 14:16:42 -0700 | [diff] [blame] | 468 | |
Amith Yamasani | 37ce3a8 | 2012-02-06 12:04:42 -0800 | [diff] [blame] | 469 | public WallpaperConnection(WallpaperInfo info, WallpaperData wallpaper) { |
Dianne Hackborn | eb03465 | 2009-09-07 00:49:58 -0700 | [diff] [blame] | 470 | mInfo = info; |
Amith Yamasani | 37ce3a8 | 2012-02-06 12:04:42 -0800 | [diff] [blame] | 471 | mWallpaper = wallpaper; |
Dianne Hackborn | eb03465 | 2009-09-07 00:49:58 -0700 | [diff] [blame] | 472 | } |
Dianne Hackborn | 5dc5a00 | 2012-09-15 19:33:48 -0700 | [diff] [blame] | 473 | |
| 474 | @Override |
Dianne Hackborn | 4c62fc0 | 2009-08-08 20:40:27 -0700 | [diff] [blame] | 475 | public void onServiceConnected(ComponentName name, IBinder service) { |
| 476 | synchronized (mLock) { |
Amith Yamasani | 37ce3a8 | 2012-02-06 12:04:42 -0800 | [diff] [blame] | 477 | if (mWallpaper.connection == this) { |
Dianne Hackborn | 4c62fc0 | 2009-08-08 20:40:27 -0700 | [diff] [blame] | 478 | mService = IWallpaperService.Stub.asInterface(service); |
Amith Yamasani | 37ce3a8 | 2012-02-06 12:04:42 -0800 | [diff] [blame] | 479 | attachServiceLocked(this, mWallpaper); |
Dianne Hackborn | eb03465 | 2009-09-07 00:49:58 -0700 | [diff] [blame] | 480 | // XXX should probably do saveSettingsLocked() later |
| 481 | // when we have an engine, but I'm not sure about |
| 482 | // locking there and anyway we always need to be able to |
| 483 | // recover if there is something wrong. |
Christopher Tate | db27b84 | 2016-02-25 14:39:17 -0800 | [diff] [blame] | 484 | saveSettingsLocked(mWallpaper.userId); |
Dianne Hackborn | 4c62fc0 | 2009-08-08 20:40:27 -0700 | [diff] [blame] | 485 | } |
| 486 | } |
| 487 | } |
| 488 | |
Dianne Hackborn | 5dc5a00 | 2012-09-15 19:33:48 -0700 | [diff] [blame] | 489 | @Override |
Dianne Hackborn | 4c62fc0 | 2009-08-08 20:40:27 -0700 | [diff] [blame] | 490 | public void onServiceDisconnected(ComponentName name) { |
| 491 | synchronized (mLock) { |
| 492 | mService = null; |
| 493 | mEngine = null; |
Amith Yamasani | 37ce3a8 | 2012-02-06 12:04:42 -0800 | [diff] [blame] | 494 | if (mWallpaper.connection == this) { |
| 495 | Slog.w(TAG, "Wallpaper service gone: " + mWallpaper.wallpaperComponent); |
| 496 | if (!mWallpaper.wallpaperUpdating |
Amith Yamasani | 37ce3a8 | 2012-02-06 12:04:42 -0800 | [diff] [blame] | 497 | && mWallpaper.userId == mCurrentUserId) { |
Selim Cinek | ebebadb | 2014-03-05 22:17:26 +0100 | [diff] [blame] | 498 | // There is a race condition which causes |
| 499 | // {@link #mWallpaper.wallpaperUpdating} to be false even if it is |
| 500 | // currently updating since the broadcast notifying us is async. |
| 501 | // This race is overcome by the general rule that we only reset the |
| 502 | // wallpaper if its service was shut down twice |
| 503 | // during {@link #MIN_WALLPAPER_CRASH_TIME} millis. |
| 504 | if (mWallpaper.lastDiedTime != 0 |
| 505 | && mWallpaper.lastDiedTime + MIN_WALLPAPER_CRASH_TIME |
| 506 | > SystemClock.uptimeMillis()) { |
| 507 | Slog.w(TAG, "Reverting to built-in wallpaper!"); |
Christopher Tate | be132e6 | 2016-02-10 12:59:49 -0800 | [diff] [blame] | 508 | clearWallpaperLocked(true, FLAG_SET_SYSTEM, mWallpaper.userId, null); |
Selim Cinek | ebebadb | 2014-03-05 22:17:26 +0100 | [diff] [blame] | 509 | } else { |
| 510 | mWallpaper.lastDiedTime = SystemClock.uptimeMillis(); |
| 511 | } |
Filip Gruszczynski | 5a58943 | 2014-10-14 12:06:06 -0700 | [diff] [blame] | 512 | final String flattened = name.flattenToString(); |
Filip Gruszczynski | 5dcc3ac | 2014-10-13 15:51:39 -0700 | [diff] [blame] | 513 | EventLog.writeEvent(EventLogTags.WP_WALLPAPER_CRASHED, |
Filip Gruszczynski | 5a58943 | 2014-10-14 12:06:06 -0700 | [diff] [blame] | 514 | flattened.substring(0, Math.min(flattened.length(), |
| 515 | MAX_WALLPAPER_COMPONENT_LOG_LENGTH))); |
Dianne Hackborn | 0cd4887 | 2009-08-13 18:51:59 -0700 | [diff] [blame] | 516 | } |
| 517 | } |
Dianne Hackborn | 4c62fc0 | 2009-08-08 20:40:27 -0700 | [diff] [blame] | 518 | } |
| 519 | } |
Amith Yamasani | 37ce3a8 | 2012-02-06 12:04:42 -0800 | [diff] [blame] | 520 | |
Dianne Hackborn | 5dc5a00 | 2012-09-15 19:33:48 -0700 | [diff] [blame] | 521 | @Override |
Dianne Hackborn | 4c62fc0 | 2009-08-08 20:40:27 -0700 | [diff] [blame] | 522 | public void attachEngine(IWallpaperEngine engine) { |
Dianne Hackborn | 5dc5a00 | 2012-09-15 19:33:48 -0700 | [diff] [blame] | 523 | synchronized (mLock) { |
| 524 | mEngine = engine; |
Michael Wright | 5203a8b | 2013-10-03 14:16:42 -0700 | [diff] [blame] | 525 | if (mDimensionsChanged) { |
| 526 | try { |
| 527 | mEngine.setDesiredSize(mWallpaper.width, mWallpaper.height); |
| 528 | } catch (RemoteException e) { |
| 529 | Slog.w(TAG, "Failed to set wallpaper dimensions", e); |
| 530 | } |
| 531 | mDimensionsChanged = false; |
| 532 | } |
Dianne Hackborn | 067e5f6 | 2014-09-07 23:14:30 -0700 | [diff] [blame] | 533 | if (mPaddingChanged) { |
| 534 | try { |
| 535 | mEngine.setDisplayPadding(mWallpaper.padding); |
| 536 | } catch (RemoteException e) { |
| 537 | Slog.w(TAG, "Failed to set wallpaper padding", e); |
| 538 | } |
| 539 | mPaddingChanged = false; |
| 540 | } |
Dianne Hackborn | 5dc5a00 | 2012-09-15 19:33:48 -0700 | [diff] [blame] | 541 | } |
Dianne Hackborn | 4c62fc0 | 2009-08-08 20:40:27 -0700 | [diff] [blame] | 542 | } |
Amith Yamasani | 37ce3a8 | 2012-02-06 12:04:42 -0800 | [diff] [blame] | 543 | |
Dianne Hackborn | 5dc5a00 | 2012-09-15 19:33:48 -0700 | [diff] [blame] | 544 | @Override |
| 545 | public void engineShown(IWallpaperEngine engine) { |
| 546 | synchronized (mLock) { |
| 547 | if (mReply != null) { |
| 548 | long ident = Binder.clearCallingIdentity(); |
| 549 | try { |
| 550 | mReply.sendResult(null); |
| 551 | } catch (RemoteException e) { |
| 552 | Binder.restoreCallingIdentity(ident); |
| 553 | } |
| 554 | mReply = null; |
| 555 | } |
| 556 | } |
| 557 | } |
| 558 | |
| 559 | @Override |
Dianne Hackborn | 4c62fc0 | 2009-08-08 20:40:27 -0700 | [diff] [blame] | 560 | public ParcelFileDescriptor setWallpaper(String name) { |
| 561 | synchronized (mLock) { |
Amith Yamasani | 37ce3a8 | 2012-02-06 12:04:42 -0800 | [diff] [blame] | 562 | if (mWallpaper.connection == this) { |
Christopher Tate | ad3c259 | 2016-01-20 18:13:17 -0800 | [diff] [blame] | 563 | return updateWallpaperBitmapLocked(name, mWallpaper, null); |
Dianne Hackborn | 4c62fc0 | 2009-08-08 20:40:27 -0700 | [diff] [blame] | 564 | } |
| 565 | return null; |
| 566 | } |
| 567 | } |
| 568 | } |
Amith Yamasani | 37ce3a8 | 2012-02-06 12:04:42 -0800 | [diff] [blame] | 569 | |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 570 | class MyPackageMonitor extends PackageMonitor { |
| 571 | @Override |
| 572 | public void onPackageUpdateFinished(String packageName, int uid) { |
| 573 | synchronized (mLock) { |
Dianne Hackborn | c72fc67 | 2012-09-20 13:12:03 -0700 | [diff] [blame] | 574 | if (mCurrentUserId != getChangingUserId()) { |
| 575 | return; |
| 576 | } |
| 577 | WallpaperData wallpaper = mWallpaperMap.get(mCurrentUserId); |
| 578 | if (wallpaper != null) { |
Amith Yamasani | 37ce3a8 | 2012-02-06 12:04:42 -0800 | [diff] [blame] | 579 | if (wallpaper.wallpaperComponent != null |
| 580 | && wallpaper.wallpaperComponent.getPackageName().equals(packageName)) { |
| 581 | wallpaper.wallpaperUpdating = false; |
| 582 | ComponentName comp = wallpaper.wallpaperComponent; |
| 583 | clearWallpaperComponentLocked(wallpaper); |
Dianne Hackborn | c72fc67 | 2012-09-20 13:12:03 -0700 | [diff] [blame] | 584 | if (!bindWallpaperComponentLocked(comp, false, false, |
| 585 | wallpaper, null)) { |
Amith Yamasani | 37ce3a8 | 2012-02-06 12:04:42 -0800 | [diff] [blame] | 586 | Slog.w(TAG, "Wallpaper no longer available; reverting to default"); |
Christopher Tate | be132e6 | 2016-02-10 12:59:49 -0800 | [diff] [blame] | 587 | clearWallpaperLocked(false, FLAG_SET_SYSTEM, wallpaper.userId, null); |
Amith Yamasani | 37ce3a8 | 2012-02-06 12:04:42 -0800 | [diff] [blame] | 588 | } |
Dianne Hackborn | 80b902f | 2011-09-15 15:15:27 -0700 | [diff] [blame] | 589 | } |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 590 | } |
| 591 | } |
| 592 | } |
| 593 | |
| 594 | @Override |
Dianne Hackborn | 80b902f | 2011-09-15 15:15:27 -0700 | [diff] [blame] | 595 | public void onPackageModified(String packageName) { |
| 596 | synchronized (mLock) { |
Dianne Hackborn | c72fc67 | 2012-09-20 13:12:03 -0700 | [diff] [blame] | 597 | if (mCurrentUserId != getChangingUserId()) { |
| 598 | return; |
| 599 | } |
| 600 | WallpaperData wallpaper = mWallpaperMap.get(mCurrentUserId); |
| 601 | if (wallpaper != null) { |
Amith Yamasani | 37ce3a8 | 2012-02-06 12:04:42 -0800 | [diff] [blame] | 602 | if (wallpaper.wallpaperComponent == null |
| 603 | || !wallpaper.wallpaperComponent.getPackageName().equals(packageName)) { |
Dianne Hackborn | c72fc67 | 2012-09-20 13:12:03 -0700 | [diff] [blame] | 604 | return; |
Amith Yamasani | 37ce3a8 | 2012-02-06 12:04:42 -0800 | [diff] [blame] | 605 | } |
Dianne Hackborn | d0d7503 | 2012-04-19 23:12:09 -0700 | [diff] [blame] | 606 | doPackagesChangedLocked(true, wallpaper); |
Dianne Hackborn | 80b902f | 2011-09-15 15:15:27 -0700 | [diff] [blame] | 607 | } |
| 608 | } |
Dianne Hackborn | 80b902f | 2011-09-15 15:15:27 -0700 | [diff] [blame] | 609 | } |
| 610 | |
| 611 | @Override |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 612 | public void onPackageUpdateStarted(String packageName, int uid) { |
| 613 | synchronized (mLock) { |
Dianne Hackborn | c72fc67 | 2012-09-20 13:12:03 -0700 | [diff] [blame] | 614 | if (mCurrentUserId != getChangingUserId()) { |
| 615 | return; |
| 616 | } |
| 617 | WallpaperData wallpaper = mWallpaperMap.get(mCurrentUserId); |
| 618 | if (wallpaper != null) { |
Amith Yamasani | 37ce3a8 | 2012-02-06 12:04:42 -0800 | [diff] [blame] | 619 | if (wallpaper.wallpaperComponent != null |
| 620 | && wallpaper.wallpaperComponent.getPackageName().equals(packageName)) { |
| 621 | wallpaper.wallpaperUpdating = true; |
| 622 | } |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 623 | } |
| 624 | } |
| 625 | } |
| 626 | |
| 627 | @Override |
| 628 | public boolean onHandleForceStop(Intent intent, String[] packages, int uid, boolean doit) { |
Dianne Hackborn | d0d7503 | 2012-04-19 23:12:09 -0700 | [diff] [blame] | 629 | synchronized (mLock) { |
| 630 | boolean changed = false; |
Dianne Hackborn | c72fc67 | 2012-09-20 13:12:03 -0700 | [diff] [blame] | 631 | if (mCurrentUserId != getChangingUserId()) { |
| 632 | return false; |
| 633 | } |
| 634 | WallpaperData wallpaper = mWallpaperMap.get(mCurrentUserId); |
| 635 | if (wallpaper != null) { |
Dianne Hackborn | d0d7503 | 2012-04-19 23:12:09 -0700 | [diff] [blame] | 636 | boolean res = doPackagesChangedLocked(doit, wallpaper); |
| 637 | changed |= res; |
| 638 | } |
| 639 | return changed; |
Amith Yamasani | 37ce3a8 | 2012-02-06 12:04:42 -0800 | [diff] [blame] | 640 | } |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 641 | } |
| 642 | |
| 643 | @Override |
| 644 | public void onSomePackagesChanged() { |
Dianne Hackborn | d0d7503 | 2012-04-19 23:12:09 -0700 | [diff] [blame] | 645 | synchronized (mLock) { |
Dianne Hackborn | c72fc67 | 2012-09-20 13:12:03 -0700 | [diff] [blame] | 646 | if (mCurrentUserId != getChangingUserId()) { |
| 647 | return; |
| 648 | } |
| 649 | WallpaperData wallpaper = mWallpaperMap.get(mCurrentUserId); |
| 650 | if (wallpaper != null) { |
Dianne Hackborn | d0d7503 | 2012-04-19 23:12:09 -0700 | [diff] [blame] | 651 | doPackagesChangedLocked(true, wallpaper); |
| 652 | } |
Amith Yamasani | 37ce3a8 | 2012-02-06 12:04:42 -0800 | [diff] [blame] | 653 | } |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 654 | } |
Amith Yamasani | 37ce3a8 | 2012-02-06 12:04:42 -0800 | [diff] [blame] | 655 | |
Dianne Hackborn | d0d7503 | 2012-04-19 23:12:09 -0700 | [diff] [blame] | 656 | boolean doPackagesChangedLocked(boolean doit, WallpaperData wallpaper) { |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 657 | boolean changed = false; |
Dianne Hackborn | d0d7503 | 2012-04-19 23:12:09 -0700 | [diff] [blame] | 658 | if (wallpaper.wallpaperComponent != null) { |
| 659 | int change = isPackageDisappearing(wallpaper.wallpaperComponent |
| 660 | .getPackageName()); |
| 661 | if (change == PACKAGE_PERMANENT_CHANGE |
| 662 | || change == PACKAGE_TEMPORARY_CHANGE) { |
| 663 | changed = true; |
| 664 | if (doit) { |
| 665 | Slog.w(TAG, "Wallpaper uninstalled, removing: " |
Amith Yamasani | 37ce3a8 | 2012-02-06 12:04:42 -0800 | [diff] [blame] | 666 | + wallpaper.wallpaperComponent); |
Christopher Tate | be132e6 | 2016-02-10 12:59:49 -0800 | [diff] [blame] | 667 | clearWallpaperLocked(false, FLAG_SET_SYSTEM, wallpaper.userId, null); |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 668 | } |
| 669 | } |
Dianne Hackborn | d0d7503 | 2012-04-19 23:12:09 -0700 | [diff] [blame] | 670 | } |
| 671 | if (wallpaper.nextWallpaperComponent != null) { |
| 672 | int change = isPackageDisappearing(wallpaper.nextWallpaperComponent |
| 673 | .getPackageName()); |
| 674 | if (change == PACKAGE_PERMANENT_CHANGE |
| 675 | || change == PACKAGE_TEMPORARY_CHANGE) { |
| 676 | wallpaper.nextWallpaperComponent = null; |
| 677 | } |
| 678 | } |
| 679 | if (wallpaper.wallpaperComponent != null |
| 680 | && isPackageModified(wallpaper.wallpaperComponent.getPackageName())) { |
| 681 | try { |
| 682 | mContext.getPackageManager().getServiceInfo( |
| 683 | wallpaper.wallpaperComponent, 0); |
| 684 | } catch (NameNotFoundException e) { |
| 685 | Slog.w(TAG, "Wallpaper component gone, removing: " |
| 686 | + wallpaper.wallpaperComponent); |
Christopher Tate | be132e6 | 2016-02-10 12:59:49 -0800 | [diff] [blame] | 687 | clearWallpaperLocked(false, FLAG_SET_SYSTEM, wallpaper.userId, null); |
Dianne Hackborn | d0d7503 | 2012-04-19 23:12:09 -0700 | [diff] [blame] | 688 | } |
| 689 | } |
| 690 | if (wallpaper.nextWallpaperComponent != null |
| 691 | && isPackageModified(wallpaper.nextWallpaperComponent.getPackageName())) { |
| 692 | try { |
| 693 | mContext.getPackageManager().getServiceInfo( |
| 694 | wallpaper.nextWallpaperComponent, 0); |
| 695 | } catch (NameNotFoundException e) { |
| 696 | wallpaper.nextWallpaperComponent = null; |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 697 | } |
| 698 | } |
| 699 | return changed; |
| 700 | } |
| 701 | } |
Oleksandr Peletskyi | f251981 | 2016-01-26 20:16:06 +0100 | [diff] [blame] | 702 | |
Dianne Hackborn | 8cc6a50 | 2009-08-05 21:29:42 -0700 | [diff] [blame] | 703 | public WallpaperManagerService(Context context) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 704 | if (DEBUG) Slog.v(TAG, "WallpaperService startup"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 705 | mContext = context; |
Justin Koh | 29c3016 | 2014-09-05 17:10:10 -0700 | [diff] [blame] | 706 | mImageWallpaper = ComponentName.unflattenFromString( |
| 707 | context.getResources().getString(R.string.image_wallpaper_component)); |
Dianne Hackborn | 4c62fc0 | 2009-08-08 20:40:27 -0700 | [diff] [blame] | 708 | mIWindowManager = IWindowManager.Stub.asInterface( |
| 709 | ServiceManager.getService(Context.WINDOW_SERVICE)); |
Amith Yamasani | 4e2820c | 2012-08-28 22:17:23 -0700 | [diff] [blame] | 710 | mIPackageManager = AppGlobals.getPackageManager(); |
Benjamin Franz | f3ece36 | 2015-02-11 10:51:10 +0000 | [diff] [blame] | 711 | mAppOpsManager = (AppOpsManager) mContext.getSystemService(Context.APP_OPS_SERVICE); |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 712 | mMonitor = new MyPackageMonitor(); |
Dianne Hackborn | c72fc67 | 2012-09-20 13:12:03 -0700 | [diff] [blame] | 713 | mMonitor.register(context, null, UserHandle.ALL, true); |
Xiaohui Chen | 233d94c | 2015-07-30 15:08:00 -0700 | [diff] [blame] | 714 | getWallpaperDir(UserHandle.USER_SYSTEM).mkdirs(); |
| 715 | loadSettingsLocked(UserHandle.USER_SYSTEM); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 716 | } |
Oleksandr Peletskyi | f251981 | 2016-01-26 20:16:06 +0100 | [diff] [blame] | 717 | |
Amith Yamasani | 37ce3a8 | 2012-02-06 12:04:42 -0800 | [diff] [blame] | 718 | private static File getWallpaperDir(int userId) { |
Amith Yamasani | 61f5737 | 2012-08-31 12:12:28 -0700 | [diff] [blame] | 719 | return Environment.getUserSystemDirectory(userId); |
Amith Yamasani | 37ce3a8 | 2012-02-06 12:04:42 -0800 | [diff] [blame] | 720 | } |
| 721 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 722 | @Override |
| 723 | protected void finalize() throws Throwable { |
| 724 | super.finalize(); |
Amith Yamasani | 37ce3a8 | 2012-02-06 12:04:42 -0800 | [diff] [blame] | 725 | for (int i = 0; i < mWallpaperMap.size(); i++) { |
| 726 | WallpaperData wallpaper = mWallpaperMap.valueAt(i); |
| 727 | wallpaper.wallpaperObserver.stopWatching(); |
| 728 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 729 | } |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 730 | |
Svetoslav Ganov | a002715 | 2013-06-25 14:59:53 -0700 | [diff] [blame] | 731 | public void systemRunning() { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 732 | if (DEBUG) Slog.v(TAG, "systemReady"); |
Xiaohui Chen | 233d94c | 2015-07-30 15:08:00 -0700 | [diff] [blame] | 733 | WallpaperData wallpaper = mWallpaperMap.get(UserHandle.USER_SYSTEM); |
Christopher Tate | 2cdd3f2 | 2016-03-14 17:36:16 -0700 | [diff] [blame] | 734 | // If we think we're going to be using the system image wallpaper imagery, make |
| 735 | // sure we have something to render |
| 736 | if (mImageWallpaper.equals(wallpaper.nextWallpaperComponent)) { |
| 737 | // No crop file? Make sure we've finished the processing sequence if necessary |
| 738 | if (!wallpaper.cropExists()) { |
| 739 | if (DEBUG) { |
| 740 | Slog.i(TAG, "No crop; regenerating from source"); |
| 741 | } |
| 742 | generateCrop(wallpaper); |
| 743 | } |
| 744 | // Still nothing? Fall back to default. |
| 745 | if (!wallpaper.cropExists()) { |
| 746 | if (DEBUG) { |
| 747 | Slog.i(TAG, "Unable to regenerate crop; resetting"); |
| 748 | } |
| 749 | clearWallpaperLocked(false, FLAG_SET_SYSTEM, UserHandle.USER_SYSTEM, null); |
| 750 | } |
| 751 | } else { |
| 752 | if (DEBUG) { |
| 753 | Slog.i(TAG, "Nondefault wallpaper component; gracefully ignoring"); |
| 754 | } |
Christopher Tate | 1e1e2e0 | 2016-01-25 15:34:36 -0800 | [diff] [blame] | 755 | } |
Dianne Hackborn | 5dc5a00 | 2012-09-15 19:33:48 -0700 | [diff] [blame] | 756 | switchWallpaper(wallpaper, null); |
Amith Yamasani | 37ce3a8 | 2012-02-06 12:04:42 -0800 | [diff] [blame] | 757 | wallpaper.wallpaperObserver = new WallpaperObserver(wallpaper); |
| 758 | wallpaper.wallpaperObserver.startWatching(); |
Amith Yamasani | 37ce3a8 | 2012-02-06 12:04:42 -0800 | [diff] [blame] | 759 | |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 760 | IntentFilter userFilter = new IntentFilter(); |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 761 | userFilter.addAction(Intent.ACTION_USER_REMOVED); |
Amith Yamasani | 756901d | 2012-10-12 12:30:07 -0700 | [diff] [blame] | 762 | userFilter.addAction(Intent.ACTION_USER_STOPPING); |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 763 | mContext.registerReceiver(new BroadcastReceiver() { |
Amith Yamasani | 37ce3a8 | 2012-02-06 12:04:42 -0800 | [diff] [blame] | 764 | @Override |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 765 | public void onReceive(Context context, Intent intent) { |
| 766 | String action = intent.getAction(); |
Dianne Hackborn | 5dc5a00 | 2012-09-15 19:33:48 -0700 | [diff] [blame] | 767 | if (Intent.ACTION_USER_REMOVED.equals(action)) { |
Amith Yamasani | 756901d | 2012-10-12 12:30:07 -0700 | [diff] [blame] | 768 | onRemoveUser(intent.getIntExtra(Intent.EXTRA_USER_HANDLE, |
| 769 | UserHandle.USER_NULL)); |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 770 | } |
Amith Yamasani | 0c29371 | 2012-10-30 12:23:52 -0700 | [diff] [blame] | 771 | // TODO: Race condition causing problems when cleaning up on stopping a user. |
| 772 | // Comment this out for now. |
| 773 | // else if (Intent.ACTION_USER_STOPPING.equals(action)) { |
| 774 | // onStoppingUser(intent.getIntExtra(Intent.EXTRA_USER_HANDLE, |
| 775 | // UserHandle.USER_NULL)); |
| 776 | // } |
Amith Yamasani | 37ce3a8 | 2012-02-06 12:04:42 -0800 | [diff] [blame] | 777 | } |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 778 | }, userFilter); |
Amith Yamasani | 756901d | 2012-10-12 12:30:07 -0700 | [diff] [blame] | 779 | |
Dianne Hackborn | 5dc5a00 | 2012-09-15 19:33:48 -0700 | [diff] [blame] | 780 | try { |
| 781 | ActivityManagerNative.getDefault().registerUserSwitchObserver( |
| 782 | new IUserSwitchObserver.Stub() { |
| 783 | @Override |
| 784 | public void onUserSwitching(int newUserId, IRemoteCallback reply) { |
| 785 | switchUser(newUserId, reply); |
| 786 | } |
| 787 | |
| 788 | @Override |
| 789 | public void onUserSwitchComplete(int newUserId) throws RemoteException { |
| 790 | } |
Kenny Guy | 4297962 | 2015-04-13 18:03:05 +0000 | [diff] [blame] | 791 | |
| 792 | @Override |
| 793 | public void onForegroundProfileSwitch(int newProfileId) { |
| 794 | // Ignore. |
| 795 | } |
Dianne Hackborn | 5dc5a00 | 2012-09-15 19:33:48 -0700 | [diff] [blame] | 796 | }); |
| 797 | } catch (RemoteException e) { |
| 798 | // TODO Auto-generated catch block |
| 799 | e.printStackTrace(); |
| 800 | } |
Amith Yamasani | 37ce3a8 | 2012-02-06 12:04:42 -0800 | [diff] [blame] | 801 | } |
| 802 | |
Amith Yamasani | 09e9cdc | 2013-11-06 14:54:50 -0800 | [diff] [blame] | 803 | /** Called by SystemBackupAgent */ |
| 804 | public String getName() { |
| 805 | // Verify caller is the system |
| 806 | if (Binder.getCallingUid() != android.os.Process.SYSTEM_UID) { |
| 807 | throw new RuntimeException("getName() can only be called from the system process"); |
| 808 | } |
Dianne Hackborn | 5dc5a00 | 2012-09-15 19:33:48 -0700 | [diff] [blame] | 809 | synchronized (mLock) { |
| 810 | return mWallpaperMap.get(0).name; |
| 811 | } |
Amith Yamasani | 37ce3a8 | 2012-02-06 12:04:42 -0800 | [diff] [blame] | 812 | } |
| 813 | |
Christopher Tate | be132e6 | 2016-02-10 12:59:49 -0800 | [diff] [blame] | 814 | void stopObserver(WallpaperData wallpaper) { |
| 815 | if (wallpaper != null) { |
| 816 | if (wallpaper.wallpaperObserver != null) { |
| 817 | wallpaper.wallpaperObserver.stopWatching(); |
| 818 | wallpaper.wallpaperObserver = null; |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 819 | } |
Amith Yamasani | 756901d | 2012-10-12 12:30:07 -0700 | [diff] [blame] | 820 | } |
| 821 | } |
| 822 | |
Christopher Tate | be132e6 | 2016-02-10 12:59:49 -0800 | [diff] [blame] | 823 | void stopObserversLocked(int userId) { |
| 824 | stopObserver(mWallpaperMap.get(userId)); |
| 825 | stopObserver(mLockWallpaperMap.get(userId)); |
| 826 | mWallpaperMap.remove(userId); |
| 827 | mLockWallpaperMap.remove(userId); |
| 828 | } |
| 829 | |
Amith Yamasani | 756901d | 2012-10-12 12:30:07 -0700 | [diff] [blame] | 830 | void onRemoveUser(int userId) { |
| 831 | if (userId < 1) return; |
Christopher Tate | be132e6 | 2016-02-10 12:59:49 -0800 | [diff] [blame] | 832 | |
| 833 | final File wallpaperDir = getWallpaperDir(userId); |
Amith Yamasani | 756901d | 2012-10-12 12:30:07 -0700 | [diff] [blame] | 834 | synchronized (mLock) { |
Christopher Tate | be132e6 | 2016-02-10 12:59:49 -0800 | [diff] [blame] | 835 | stopObserversLocked(userId); |
| 836 | for (String filename : sPerUserFiles) { |
| 837 | new File(wallpaperDir, filename).delete(); |
| 838 | } |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 839 | } |
| 840 | } |
| 841 | |
Dianne Hackborn | 5dc5a00 | 2012-09-15 19:33:48 -0700 | [diff] [blame] | 842 | void switchUser(int userId, IRemoteCallback reply) { |
Amith Yamasani | 37ce3a8 | 2012-02-06 12:04:42 -0800 | [diff] [blame] | 843 | synchronized (mLock) { |
| 844 | mCurrentUserId = userId; |
Christopher Tate | be132e6 | 2016-02-10 12:59:49 -0800 | [diff] [blame] | 845 | WallpaperData wallpaper = getWallpaperSafeLocked(userId, FLAG_SET_SYSTEM); |
Amith Yamasani | 6474c4c | 2012-10-04 14:55:42 -0700 | [diff] [blame] | 846 | // Not started watching yet, in case wallpaper data was loaded for other reasons. |
| 847 | if (wallpaper.wallpaperObserver == null) { |
Amith Yamasani | 37ce3a8 | 2012-02-06 12:04:42 -0800 | [diff] [blame] | 848 | wallpaper.wallpaperObserver = new WallpaperObserver(wallpaper); |
| 849 | wallpaper.wallpaperObserver.startWatching(); |
| 850 | } |
Dianne Hackborn | 5dc5a00 | 2012-09-15 19:33:48 -0700 | [diff] [blame] | 851 | switchWallpaper(wallpaper, reply); |
Amith Yamasani | 37ce3a8 | 2012-02-06 12:04:42 -0800 | [diff] [blame] | 852 | } |
| 853 | } |
| 854 | |
Dianne Hackborn | 5dc5a00 | 2012-09-15 19:33:48 -0700 | [diff] [blame] | 855 | void switchWallpaper(WallpaperData wallpaper, IRemoteCallback reply) { |
Dianne Hackborn | f21adf6 | 2009-08-13 10:20:21 -0700 | [diff] [blame] | 856 | synchronized (mLock) { |
Dianne Hackborn | 80b902f | 2011-09-15 15:15:27 -0700 | [diff] [blame] | 857 | RuntimeException e = null; |
Dianne Hackborn | f21adf6 | 2009-08-13 10:20:21 -0700 | [diff] [blame] | 858 | try { |
Amith Yamasani | 37ce3a8 | 2012-02-06 12:04:42 -0800 | [diff] [blame] | 859 | ComponentName cname = wallpaper.wallpaperComponent != null ? |
| 860 | wallpaper.wallpaperComponent : wallpaper.nextWallpaperComponent; |
Dianne Hackborn | 5dc5a00 | 2012-09-15 19:33:48 -0700 | [diff] [blame] | 861 | if (bindWallpaperComponentLocked(cname, true, false, wallpaper, reply)) { |
Dianne Hackborn | 80b902f | 2011-09-15 15:15:27 -0700 | [diff] [blame] | 862 | return; |
Dianne Hackborn | f21adf6 | 2009-08-13 10:20:21 -0700 | [diff] [blame] | 863 | } |
Dianne Hackborn | 80b902f | 2011-09-15 15:15:27 -0700 | [diff] [blame] | 864 | } catch (RuntimeException e1) { |
| 865 | e = e1; |
Dianne Hackborn | f21adf6 | 2009-08-13 10:20:21 -0700 | [diff] [blame] | 866 | } |
Dianne Hackborn | 80b902f | 2011-09-15 15:15:27 -0700 | [diff] [blame] | 867 | Slog.w(TAG, "Failure starting previous wallpaper", e); |
Christopher Tate | be132e6 | 2016-02-10 12:59:49 -0800 | [diff] [blame] | 868 | clearWallpaperLocked(false, FLAG_SET_SYSTEM, wallpaper.userId, reply); |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 869 | } |
| 870 | } |
| 871 | |
Christopher Tate | be132e6 | 2016-02-10 12:59:49 -0800 | [diff] [blame] | 872 | @Override |
| 873 | public void clearWallpaper(String callingPackage, int which, int userId) { |
Amith Yamasani | 37ce3a8 | 2012-02-06 12:04:42 -0800 | [diff] [blame] | 874 | if (DEBUG) Slog.v(TAG, "clearWallpaper"); |
Benjamin Franz | f3ece36 | 2015-02-11 10:51:10 +0000 | [diff] [blame] | 875 | checkPermission(android.Manifest.permission.SET_WALLPAPER); |
Oleksandr Peletskyi | f251981 | 2016-01-26 20:16:06 +0100 | [diff] [blame] | 876 | if (!isWallpaperSupported(callingPackage) || !isWallpaperSettingAllowed(callingPackage)) { |
Benjamin Franz | f3ece36 | 2015-02-11 10:51:10 +0000 | [diff] [blame] | 877 | return; |
| 878 | } |
Christopher Tate | e409f0e | 2016-03-21 14:53:15 -0700 | [diff] [blame] | 879 | userId = ActivityManager.handleIncomingUser(Binder.getCallingPid(), |
| 880 | Binder.getCallingUid(), userId, false, true, "clearWallpaper", null); |
Christopher Tate | be132e6 | 2016-02-10 12:59:49 -0800 | [diff] [blame] | 881 | |
Amith Yamasani | 37ce3a8 | 2012-02-06 12:04:42 -0800 | [diff] [blame] | 882 | synchronized (mLock) { |
Christopher Tate | be132e6 | 2016-02-10 12:59:49 -0800 | [diff] [blame] | 883 | clearWallpaperLocked(false, which, userId, null); |
Amith Yamasani | 37ce3a8 | 2012-02-06 12:04:42 -0800 | [diff] [blame] | 884 | } |
| 885 | } |
| 886 | |
Christopher Tate | be132e6 | 2016-02-10 12:59:49 -0800 | [diff] [blame] | 887 | void clearWallpaperLocked(boolean defaultFailed, int which, int userId, IRemoteCallback reply) { |
| 888 | if (which != FLAG_SET_SYSTEM && which != FLAG_SET_LOCK) { |
| 889 | throw new IllegalArgumentException("Must specify exactly one kind of wallpaper to read"); |
| 890 | } |
| 891 | |
| 892 | WallpaperData wallpaper = null; |
| 893 | if (which == FLAG_SET_LOCK) { |
| 894 | wallpaper = mLockWallpaperMap.get(userId); |
| 895 | if (wallpaper == null) { |
| 896 | // It's already gone; we're done. |
Christopher Tate | 79a2457 | 2016-03-02 14:42:44 -0800 | [diff] [blame] | 897 | if (DEBUG) { |
| 898 | Slog.i(TAG, "Lock wallpaper already cleared"); |
| 899 | } |
Christopher Tate | be132e6 | 2016-02-10 12:59:49 -0800 | [diff] [blame] | 900 | return; |
| 901 | } |
| 902 | } else { |
| 903 | wallpaper = mWallpaperMap.get(userId); |
| 904 | if (wallpaper == null) { |
| 905 | // Might need to bring it in the first time to establish our rewrite |
| 906 | loadSettingsLocked(userId); |
| 907 | wallpaper = mWallpaperMap.get(userId); |
| 908 | } |
| 909 | } |
Benjamin Franz | f3ece36 | 2015-02-11 10:51:10 +0000 | [diff] [blame] | 910 | if (wallpaper == null) { |
| 911 | return; |
| 912 | } |
Christopher Tate | be132e6 | 2016-02-10 12:59:49 -0800 | [diff] [blame] | 913 | |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 914 | final long ident = Binder.clearCallingIdentity(); |
| 915 | try { |
Christopher Tate | be132e6 | 2016-02-10 12:59:49 -0800 | [diff] [blame] | 916 | if (wallpaper.wallpaperFile.exists()) { |
| 917 | wallpaper.wallpaperFile.delete(); |
| 918 | wallpaper.cropFile.delete(); |
| 919 | if (which == FLAG_SET_LOCK) { |
Christopher Tate | 79a2457 | 2016-03-02 14:42:44 -0800 | [diff] [blame] | 920 | mLockWallpaperMap.remove(userId); |
Christopher Tate | be132e6 | 2016-02-10 12:59:49 -0800 | [diff] [blame] | 921 | final IWallpaperManagerCallback cb = mKeyguardListener; |
| 922 | if (cb != null) { |
Christopher Tate | 79a2457 | 2016-03-02 14:42:44 -0800 | [diff] [blame] | 923 | if (DEBUG) { |
| 924 | Slog.i(TAG, "Notifying keyguard of lock wallpaper clear"); |
| 925 | } |
Christopher Tate | be132e6 | 2016-02-10 12:59:49 -0800 | [diff] [blame] | 926 | try { |
| 927 | cb.onWallpaperChanged(); |
| 928 | } catch (RemoteException e) { |
| 929 | // Oh well it went away; no big deal |
| 930 | } |
| 931 | } |
Christopher Tate | 79a2457 | 2016-03-02 14:42:44 -0800 | [diff] [blame] | 932 | saveSettingsLocked(userId); |
Christopher Tate | be132e6 | 2016-02-10 12:59:49 -0800 | [diff] [blame] | 933 | return; |
| 934 | } |
| 935 | } |
| 936 | |
Christopher Tate | ecd827a | 2014-09-05 17:42:34 -0700 | [diff] [blame] | 937 | RuntimeException e = null; |
| 938 | try { |
| 939 | wallpaper.imageWallpaperPending = false; |
| 940 | if (userId != mCurrentUserId) return; |
| 941 | if (bindWallpaperComponentLocked(defaultFailed |
| 942 | ? mImageWallpaper |
| 943 | : null, true, false, wallpaper, reply)) { |
| 944 | return; |
| 945 | } |
| 946 | } catch (IllegalArgumentException e1) { |
| 947 | e = e1; |
Dianne Hackborn | 80b902f | 2011-09-15 15:15:27 -0700 | [diff] [blame] | 948 | } |
Christopher Tate | ecd827a | 2014-09-05 17:42:34 -0700 | [diff] [blame] | 949 | |
| 950 | // This can happen if the default wallpaper component doesn't |
| 951 | // exist. This should be a system configuration problem, but |
| 952 | // let's not let it crash the system and just live with no |
| 953 | // wallpaper. |
| 954 | Slog.e(TAG, "Default wallpaper component not found!", e); |
| 955 | clearWallpaperComponentLocked(wallpaper); |
| 956 | if (reply != null) { |
| 957 | try { |
| 958 | reply.sendResult(null); |
| 959 | } catch (RemoteException e1) { |
| 960 | } |
| 961 | } |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 962 | } finally { |
| 963 | Binder.restoreCallingIdentity(ident); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 964 | } |
Dianne Hackborn | 5dc5a00 | 2012-09-15 19:33:48 -0700 | [diff] [blame] | 965 | } |
| 966 | |
| 967 | public boolean hasNamedWallpaper(String name) { |
| 968 | synchronized (mLock) { |
Amith Yamasani | 6474c4c | 2012-10-04 14:55:42 -0700 | [diff] [blame] | 969 | List<UserInfo> users; |
| 970 | long ident = Binder.clearCallingIdentity(); |
| 971 | try { |
| 972 | users = ((UserManager) mContext.getSystemService(Context.USER_SERVICE)).getUsers(); |
| 973 | } finally { |
| 974 | Binder.restoreCallingIdentity(ident); |
| 975 | } |
| 976 | for (UserInfo user: users) { |
Benjamin Franz | f3ece36 | 2015-02-11 10:51:10 +0000 | [diff] [blame] | 977 | // ignore managed profiles |
| 978 | if (user.isManagedProfile()) { |
| 979 | continue; |
| 980 | } |
Amith Yamasani | 6474c4c | 2012-10-04 14:55:42 -0700 | [diff] [blame] | 981 | WallpaperData wd = mWallpaperMap.get(user.id); |
| 982 | if (wd == null) { |
| 983 | // User hasn't started yet, so load her settings to peek at the wallpaper |
| 984 | loadSettingsLocked(user.id); |
| 985 | wd = mWallpaperMap.get(user.id); |
| 986 | } |
| 987 | if (wd != null && name.equals(wd.name)) { |
Dianne Hackborn | 5dc5a00 | 2012-09-15 19:33:48 -0700 | [diff] [blame] | 988 | return true; |
| 989 | } |
| 990 | } |
| 991 | } |
| 992 | return false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 993 | } |
| 994 | |
John Spurlock | 41f6464 | 2013-11-04 13:48:38 -0500 | [diff] [blame] | 995 | private Point getDefaultDisplaySize() { |
| 996 | Point p = new Point(); |
John Spurlock | d6e836c | 2013-11-18 14:14:49 -0500 | [diff] [blame] | 997 | WindowManager wm = (WindowManager)mContext.getSystemService(Context.WINDOW_SERVICE); |
| 998 | Display d = wm.getDefaultDisplay(); |
| 999 | d.getRealSize(p); |
John Spurlock | 41f6464 | 2013-11-04 13:48:38 -0500 | [diff] [blame] | 1000 | return p; |
| 1001 | } |
| 1002 | |
Benjamin Franz | f3ece36 | 2015-02-11 10:51:10 +0000 | [diff] [blame] | 1003 | public void setDimensionHints(int width, int height, String callingPackage) |
| 1004 | throws RemoteException { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1005 | checkPermission(android.Manifest.permission.SET_WALLPAPER_HINTS); |
Benjamin Franz | f3ece36 | 2015-02-11 10:51:10 +0000 | [diff] [blame] | 1006 | if (!isWallpaperSupported(callingPackage)) { |
| 1007 | return; |
| 1008 | } |
Joe Onorato | 9bb8fd7 | 2009-07-28 18:24:51 -0700 | [diff] [blame] | 1009 | synchronized (mLock) { |
Dianne Hackborn | 5dc5a00 | 2012-09-15 19:33:48 -0700 | [diff] [blame] | 1010 | int userId = UserHandle.getCallingUserId(); |
Christopher Tate | be132e6 | 2016-02-10 12:59:49 -0800 | [diff] [blame] | 1011 | WallpaperData wallpaper = getWallpaperSafeLocked(userId, FLAG_SET_SYSTEM); |
Dianne Hackborn | 5dc5a00 | 2012-09-15 19:33:48 -0700 | [diff] [blame] | 1012 | if (width <= 0 || height <= 0) { |
| 1013 | throw new IllegalArgumentException("width and height must be > 0"); |
| 1014 | } |
John Spurlock | 41f6464 | 2013-11-04 13:48:38 -0500 | [diff] [blame] | 1015 | // Make sure it is at least as large as the display. |
| 1016 | Point displaySize = getDefaultDisplaySize(); |
| 1017 | width = Math.max(width, displaySize.x); |
| 1018 | height = Math.max(height, displaySize.y); |
Dianne Hackborn | 5dc5a00 | 2012-09-15 19:33:48 -0700 | [diff] [blame] | 1019 | |
Amith Yamasani | 37ce3a8 | 2012-02-06 12:04:42 -0800 | [diff] [blame] | 1020 | if (width != wallpaper.width || height != wallpaper.height) { |
| 1021 | wallpaper.width = width; |
| 1022 | wallpaper.height = height; |
Christopher Tate | db27b84 | 2016-02-25 14:39:17 -0800 | [diff] [blame] | 1023 | saveSettingsLocked(userId); |
Amith Yamasani | 37ce3a8 | 2012-02-06 12:04:42 -0800 | [diff] [blame] | 1024 | if (mCurrentUserId != userId) return; // Don't change the properties now |
| 1025 | if (wallpaper.connection != null) { |
| 1026 | if (wallpaper.connection.mEngine != null) { |
Dianne Hackborn | 284ac93 | 2009-08-28 10:34:25 -0700 | [diff] [blame] | 1027 | try { |
Amith Yamasani | 37ce3a8 | 2012-02-06 12:04:42 -0800 | [diff] [blame] | 1028 | wallpaper.connection.mEngine.setDesiredSize( |
Dianne Hackborn | 284ac93 | 2009-08-28 10:34:25 -0700 | [diff] [blame] | 1029 | width, height); |
| 1030 | } catch (RemoteException e) { |
| 1031 | } |
Amith Yamasani | 37ce3a8 | 2012-02-06 12:04:42 -0800 | [diff] [blame] | 1032 | notifyCallbacksLocked(wallpaper); |
Michael Wright | 5203a8b | 2013-10-03 14:16:42 -0700 | [diff] [blame] | 1033 | } else if (wallpaper.connection.mService != null) { |
| 1034 | // We've attached to the service but the engine hasn't attached back to us |
| 1035 | // yet. This means it will be created with the previous dimensions, so we |
| 1036 | // need to update it to the new dimensions once it attaches. |
| 1037 | wallpaper.connection.mDimensionsChanged = true; |
Dianne Hackborn | 284ac93 | 2009-08-28 10:34:25 -0700 | [diff] [blame] | 1038 | } |
| 1039 | } |
Joe Onorato | 9bb8fd7 | 2009-07-28 18:24:51 -0700 | [diff] [blame] | 1040 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1041 | } |
| 1042 | } |
| 1043 | |
| 1044 | public int getWidthHint() throws RemoteException { |
Joe Onorato | 9bb8fd7 | 2009-07-28 18:24:51 -0700 | [diff] [blame] | 1045 | synchronized (mLock) { |
Dianne Hackborn | f02b60a | 2012-08-16 10:48:27 -0700 | [diff] [blame] | 1046 | WallpaperData wallpaper = mWallpaperMap.get(UserHandle.getCallingUserId()); |
Benjamin Franz | f3ece36 | 2015-02-11 10:51:10 +0000 | [diff] [blame] | 1047 | if (wallpaper != null) { |
| 1048 | return wallpaper.width; |
| 1049 | } else { |
| 1050 | return 0; |
| 1051 | } |
Joe Onorato | 9bb8fd7 | 2009-07-28 18:24:51 -0700 | [diff] [blame] | 1052 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1053 | } |
| 1054 | |
| 1055 | public int getHeightHint() throws RemoteException { |
Joe Onorato | 9bb8fd7 | 2009-07-28 18:24:51 -0700 | [diff] [blame] | 1056 | synchronized (mLock) { |
Dianne Hackborn | f02b60a | 2012-08-16 10:48:27 -0700 | [diff] [blame] | 1057 | WallpaperData wallpaper = mWallpaperMap.get(UserHandle.getCallingUserId()); |
Benjamin Franz | f3ece36 | 2015-02-11 10:51:10 +0000 | [diff] [blame] | 1058 | if (wallpaper != null) { |
| 1059 | return wallpaper.height; |
| 1060 | } else { |
| 1061 | return 0; |
| 1062 | } |
Joe Onorato | 9bb8fd7 | 2009-07-28 18:24:51 -0700 | [diff] [blame] | 1063 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1064 | } |
| 1065 | |
Benjamin Franz | f3ece36 | 2015-02-11 10:51:10 +0000 | [diff] [blame] | 1066 | public void setDisplayPadding(Rect padding, String callingPackage) { |
Dianne Hackborn | 067e5f6 | 2014-09-07 23:14:30 -0700 | [diff] [blame] | 1067 | checkPermission(android.Manifest.permission.SET_WALLPAPER_HINTS); |
Benjamin Franz | f3ece36 | 2015-02-11 10:51:10 +0000 | [diff] [blame] | 1068 | if (!isWallpaperSupported(callingPackage)) { |
| 1069 | return; |
| 1070 | } |
Dianne Hackborn | 067e5f6 | 2014-09-07 23:14:30 -0700 | [diff] [blame] | 1071 | synchronized (mLock) { |
| 1072 | int userId = UserHandle.getCallingUserId(); |
Christopher Tate | be132e6 | 2016-02-10 12:59:49 -0800 | [diff] [blame] | 1073 | WallpaperData wallpaper = getWallpaperSafeLocked(userId, FLAG_SET_SYSTEM); |
Dianne Hackborn | 067e5f6 | 2014-09-07 23:14:30 -0700 | [diff] [blame] | 1074 | if (padding.left < 0 || padding.top < 0 || padding.right < 0 || padding.bottom < 0) { |
| 1075 | throw new IllegalArgumentException("padding must be positive: " + padding); |
| 1076 | } |
| 1077 | |
| 1078 | if (!padding.equals(wallpaper.padding)) { |
| 1079 | wallpaper.padding.set(padding); |
Christopher Tate | db27b84 | 2016-02-25 14:39:17 -0800 | [diff] [blame] | 1080 | saveSettingsLocked(userId); |
Dianne Hackborn | 067e5f6 | 2014-09-07 23:14:30 -0700 | [diff] [blame] | 1081 | if (mCurrentUserId != userId) return; // Don't change the properties now |
| 1082 | if (wallpaper.connection != null) { |
| 1083 | if (wallpaper.connection.mEngine != null) { |
| 1084 | try { |
| 1085 | wallpaper.connection.mEngine.setDisplayPadding(padding); |
| 1086 | } catch (RemoteException e) { |
| 1087 | } |
| 1088 | notifyCallbacksLocked(wallpaper); |
| 1089 | } else if (wallpaper.connection.mService != null) { |
| 1090 | // We've attached to the service but the engine hasn't attached back to us |
| 1091 | // yet. This means it will be created with the previous dimensions, so we |
| 1092 | // need to update it to the new dimensions once it attaches. |
| 1093 | wallpaper.connection.mPaddingChanged = true; |
| 1094 | } |
| 1095 | } |
| 1096 | } |
| 1097 | } |
| 1098 | } |
| 1099 | |
Yorke Lee | dcd93cc | 2016-01-08 14:12:55 -0800 | [diff] [blame] | 1100 | @Override |
Christopher Tate | be132e6 | 2016-02-10 12:59:49 -0800 | [diff] [blame] | 1101 | public ParcelFileDescriptor getWallpaper(IWallpaperManagerCallback cb, final int which, |
| 1102 | Bundle outParams, int wallpaperUserId) { |
Christopher Tate | e409f0e | 2016-03-21 14:53:15 -0700 | [diff] [blame] | 1103 | wallpaperUserId = ActivityManager.handleIncomingUser(Binder.getCallingPid(), |
| 1104 | Binder.getCallingUid(), wallpaperUserId, false, true, "getWallpaper", null); |
Christopher Tate | be132e6 | 2016-02-10 12:59:49 -0800 | [diff] [blame] | 1105 | |
| 1106 | if (which != FLAG_SET_SYSTEM && which != FLAG_SET_LOCK) { |
| 1107 | throw new IllegalArgumentException("Must specify exactly one kind of wallpaper to read"); |
| 1108 | } |
| 1109 | |
Joe Onorato | 9bb8fd7 | 2009-07-28 18:24:51 -0700 | [diff] [blame] | 1110 | synchronized (mLock) { |
Vadim Tryshev | 8cde079 | 2016-02-19 17:02:15 -0800 | [diff] [blame] | 1111 | final SparseArray<WallpaperData> whichSet = |
| 1112 | (which == FLAG_SET_LOCK) ? mLockWallpaperMap : mWallpaperMap; |
| 1113 | WallpaperData wallpaper = whichSet.get(wallpaperUserId); |
| 1114 | if (wallpaper == null) { |
| 1115 | // common case, this is the first lookup post-boot of the system or |
| 1116 | // unified lock, so we bring up the saved state lazily now and recheck. |
| 1117 | loadSettingsLocked(wallpaperUserId); |
| 1118 | wallpaper = whichSet.get(wallpaperUserId); |
Christopher Tate | be132e6 | 2016-02-10 12:59:49 -0800 | [diff] [blame] | 1119 | if (wallpaper == null) { |
Christopher Tate | be132e6 | 2016-02-10 12:59:49 -0800 | [diff] [blame] | 1120 | return null; |
| 1121 | } |
Benjamin Franz | f3ece36 | 2015-02-11 10:51:10 +0000 | [diff] [blame] | 1122 | } |
Joe Onorato | 9bb8fd7 | 2009-07-28 18:24:51 -0700 | [diff] [blame] | 1123 | try { |
Dianne Hackborn | 284ac93 | 2009-08-28 10:34:25 -0700 | [diff] [blame] | 1124 | if (outParams != null) { |
Amith Yamasani | 37ce3a8 | 2012-02-06 12:04:42 -0800 | [diff] [blame] | 1125 | outParams.putInt("width", wallpaper.width); |
| 1126 | outParams.putInt("height", wallpaper.height); |
Dianne Hackborn | 284ac93 | 2009-08-28 10:34:25 -0700 | [diff] [blame] | 1127 | } |
Christopher Tate | ea6724a | 2016-02-18 18:39:19 -0800 | [diff] [blame] | 1128 | if (cb != null) { |
| 1129 | wallpaper.callbacks.register(cb); |
| 1130 | } |
Christopher Tate | 1e1e2e0 | 2016-01-25 15:34:36 -0800 | [diff] [blame] | 1131 | if (!wallpaper.cropFile.exists()) { |
Joe Onorato | 9bb8fd7 | 2009-07-28 18:24:51 -0700 | [diff] [blame] | 1132 | return null; |
| 1133 | } |
Christopher Tate | 1e1e2e0 | 2016-01-25 15:34:36 -0800 | [diff] [blame] | 1134 | return ParcelFileDescriptor.open(wallpaper.cropFile, MODE_READ_ONLY); |
Joe Onorato | 9bb8fd7 | 2009-07-28 18:24:51 -0700 | [diff] [blame] | 1135 | } catch (FileNotFoundException e) { |
| 1136 | /* Shouldn't happen as we check to see if the file exists */ |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1137 | Slog.w(TAG, "Error getting wallpaper", e); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1138 | } |
Joe Onorato | 9bb8fd7 | 2009-07-28 18:24:51 -0700 | [diff] [blame] | 1139 | return null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1140 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1141 | } |
| 1142 | |
Christopher Tate | e409f0e | 2016-03-21 14:53:15 -0700 | [diff] [blame] | 1143 | @Override |
Dianne Hackborn | eb03465 | 2009-09-07 00:49:58 -0700 | [diff] [blame] | 1144 | public WallpaperInfo getWallpaperInfo() { |
Dianne Hackborn | f02b60a | 2012-08-16 10:48:27 -0700 | [diff] [blame] | 1145 | int userId = UserHandle.getCallingUserId(); |
Dianne Hackborn | eb03465 | 2009-09-07 00:49:58 -0700 | [diff] [blame] | 1146 | synchronized (mLock) { |
Amith Yamasani | 37ce3a8 | 2012-02-06 12:04:42 -0800 | [diff] [blame] | 1147 | WallpaperData wallpaper = mWallpaperMap.get(userId); |
Benjamin Franz | f3ece36 | 2015-02-11 10:51:10 +0000 | [diff] [blame] | 1148 | if (wallpaper != null && wallpaper.connection != null) { |
Amith Yamasani | 37ce3a8 | 2012-02-06 12:04:42 -0800 | [diff] [blame] | 1149 | return wallpaper.connection.mInfo; |
Dianne Hackborn | eb03465 | 2009-09-07 00:49:58 -0700 | [diff] [blame] | 1150 | } |
| 1151 | return null; |
| 1152 | } |
| 1153 | } |
Amith Yamasani | 37ce3a8 | 2012-02-06 12:04:42 -0800 | [diff] [blame] | 1154 | |
Christopher Tate | ad3c259 | 2016-01-20 18:13:17 -0800 | [diff] [blame] | 1155 | @Override |
Christopher Tate | e409f0e | 2016-03-21 14:53:15 -0700 | [diff] [blame] | 1156 | public int getWallpaperIdForUser(int which, int userId) { |
| 1157 | userId = ActivityManager.handleIncomingUser(Binder.getCallingPid(), |
| 1158 | Binder.getCallingUid(), userId, false, true, "getWallpaperIdForUser", null); |
| 1159 | |
| 1160 | if (which != FLAG_SET_SYSTEM && which != FLAG_SET_LOCK) { |
| 1161 | throw new IllegalArgumentException("Must specify exactly one kind of wallpaper"); |
| 1162 | } |
| 1163 | |
| 1164 | final SparseArray<WallpaperData> map = |
| 1165 | (which == FLAG_SET_LOCK) ? mLockWallpaperMap : mWallpaperMap; |
| 1166 | synchronized (mLock) { |
| 1167 | WallpaperData wallpaper = map.get(userId); |
| 1168 | if (wallpaper != null) { |
| 1169 | return wallpaper.wallpaperId; |
| 1170 | } |
| 1171 | } |
| 1172 | return -1; |
| 1173 | } |
| 1174 | |
| 1175 | @Override |
Christopher Tate | be132e6 | 2016-02-10 12:59:49 -0800 | [diff] [blame] | 1176 | public boolean setLockWallpaperCallback(IWallpaperManagerCallback cb) { |
| 1177 | checkPermission(android.Manifest.permission.INTERNAL_SYSTEM_WINDOW); |
| 1178 | synchronized (mLock) { |
| 1179 | mKeyguardListener = cb; |
| 1180 | } |
| 1181 | return true; |
| 1182 | } |
| 1183 | |
| 1184 | @Override |
Christopher Tate | 1e1e2e0 | 2016-01-25 15:34:36 -0800 | [diff] [blame] | 1185 | public ParcelFileDescriptor setWallpaper(String name, String callingPackage, |
| 1186 | Rect cropHint, Bundle extras, int which, IWallpaperManagerCallback completion) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1187 | checkPermission(android.Manifest.permission.SET_WALLPAPER); |
Christopher Tate | ad3c259 | 2016-01-20 18:13:17 -0800 | [diff] [blame] | 1188 | |
Christopher Tate | be132e6 | 2016-02-10 12:59:49 -0800 | [diff] [blame] | 1189 | if ((which & (FLAG_SET_LOCK|FLAG_SET_SYSTEM)) == 0) { |
| 1190 | Slog.e(TAG, "Must specify a valid wallpaper category to set"); |
Christopher Tate | ad3c259 | 2016-01-20 18:13:17 -0800 | [diff] [blame] | 1191 | return null; |
| 1192 | } |
| 1193 | |
Oleksandr Peletskyi | f251981 | 2016-01-26 20:16:06 +0100 | [diff] [blame] | 1194 | if (!isWallpaperSupported(callingPackage) || !isWallpaperSettingAllowed(callingPackage)) { |
Benjamin Franz | f3ece36 | 2015-02-11 10:51:10 +0000 | [diff] [blame] | 1195 | return null; |
| 1196 | } |
Christopher Tate | ad3c259 | 2016-01-20 18:13:17 -0800 | [diff] [blame] | 1197 | |
Christopher Tate | 1e1e2e0 | 2016-01-25 15:34:36 -0800 | [diff] [blame] | 1198 | // "null" means the no-op crop, preserving the full input image |
| 1199 | if (cropHint == null) { |
| 1200 | cropHint = new Rect(0, 0, 0, 0); |
| 1201 | } else { |
| 1202 | if (cropHint.isEmpty() |
| 1203 | || cropHint.left < 0 |
| 1204 | || cropHint.top < 0) { |
| 1205 | return null; |
| 1206 | } |
| 1207 | } |
| 1208 | |
Christopher Tate | be132e6 | 2016-02-10 12:59:49 -0800 | [diff] [blame] | 1209 | final int userId = UserHandle.getCallingUserId(); |
| 1210 | |
Joe Onorato | 9bb8fd7 | 2009-07-28 18:24:51 -0700 | [diff] [blame] | 1211 | synchronized (mLock) { |
Christopher Tate | be132e6 | 2016-02-10 12:59:49 -0800 | [diff] [blame] | 1212 | if (DEBUG) Slog.v(TAG, "setWallpaper which=0x" + Integer.toHexString(which)); |
| 1213 | WallpaperData wallpaper; |
| 1214 | |
| 1215 | wallpaper = getWallpaperSafeLocked(userId, which); |
Dianne Hackborn | 0cd4887 | 2009-08-13 18:51:59 -0700 | [diff] [blame] | 1216 | final long ident = Binder.clearCallingIdentity(); |
| 1217 | try { |
Christopher Tate | ad3c259 | 2016-01-20 18:13:17 -0800 | [diff] [blame] | 1218 | ParcelFileDescriptor pfd = updateWallpaperBitmapLocked(name, wallpaper, extras); |
Dianne Hackborn | 0cd4887 | 2009-08-13 18:51:59 -0700 | [diff] [blame] | 1219 | if (pfd != null) { |
Amith Yamasani | 37ce3a8 | 2012-02-06 12:04:42 -0800 | [diff] [blame] | 1220 | wallpaper.imageWallpaperPending = true; |
Christopher Tate | be132e6 | 2016-02-10 12:59:49 -0800 | [diff] [blame] | 1221 | wallpaper.whichPending = which; |
Christopher Tate | 1e1e2e0 | 2016-01-25 15:34:36 -0800 | [diff] [blame] | 1222 | wallpaper.setComplete = completion; |
| 1223 | wallpaper.cropHint.set(cropHint); |
Dianne Hackborn | 0cd4887 | 2009-08-13 18:51:59 -0700 | [diff] [blame] | 1224 | } |
| 1225 | return pfd; |
| 1226 | } finally { |
| 1227 | Binder.restoreCallingIdentity(ident); |
Joe Onorato | 9bb8fd7 | 2009-07-28 18:24:51 -0700 | [diff] [blame] | 1228 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1229 | } |
| 1230 | } |
| 1231 | |
Christopher Tate | ad3c259 | 2016-01-20 18:13:17 -0800 | [diff] [blame] | 1232 | ParcelFileDescriptor updateWallpaperBitmapLocked(String name, WallpaperData wallpaper, |
| 1233 | Bundle extras) { |
Dianne Hackborn | 4c62fc0 | 2009-08-08 20:40:27 -0700 | [diff] [blame] | 1234 | if (name == null) name = ""; |
| 1235 | try { |
Amith Yamasani | 37ce3a8 | 2012-02-06 12:04:42 -0800 | [diff] [blame] | 1236 | File dir = getWallpaperDir(wallpaper.userId); |
| 1237 | if (!dir.exists()) { |
| 1238 | dir.mkdir(); |
Dianne Hackborn | ebac48c | 2011-11-29 18:01:50 -0800 | [diff] [blame] | 1239 | FileUtils.setPermissions( |
Amith Yamasani | 37ce3a8 | 2012-02-06 12:04:42 -0800 | [diff] [blame] | 1240 | dir.getPath(), |
Dianne Hackborn | ebac48c | 2011-11-29 18:01:50 -0800 | [diff] [blame] | 1241 | FileUtils.S_IRWXU|FileUtils.S_IRWXG|FileUtils.S_IXOTH, |
| 1242 | -1, -1); |
| 1243 | } |
Christopher Tate | be132e6 | 2016-02-10 12:59:49 -0800 | [diff] [blame] | 1244 | ParcelFileDescriptor fd = ParcelFileDescriptor.open(wallpaper.wallpaperFile, |
Christopher Tate | 90f86ba | 2014-09-11 12:37:19 -0700 | [diff] [blame] | 1245 | MODE_CREATE|MODE_READ_WRITE|MODE_TRUNCATE); |
Christopher Tate | be132e6 | 2016-02-10 12:59:49 -0800 | [diff] [blame] | 1246 | if (!SELinux.restorecon(wallpaper.wallpaperFile)) { |
rpcraig | 554cb0c | 2012-07-05 06:41:43 -0400 | [diff] [blame] | 1247 | return null; |
| 1248 | } |
Amith Yamasani | 37ce3a8 | 2012-02-06 12:04:42 -0800 | [diff] [blame] | 1249 | wallpaper.name = name; |
Christopher Tate | ad3c259 | 2016-01-20 18:13:17 -0800 | [diff] [blame] | 1250 | wallpaper.wallpaperId = makeWallpaperIdLocked(); |
| 1251 | if (extras != null) { |
| 1252 | extras.putInt(WallpaperManager.EXTRA_NEW_WALLPAPER_ID, wallpaper.wallpaperId); |
| 1253 | } |
| 1254 | if (DEBUG) { |
| 1255 | Slog.v(TAG, "updateWallpaperBitmapLocked() : id=" + wallpaper.wallpaperId |
Christopher Tate | be132e6 | 2016-02-10 12:59:49 -0800 | [diff] [blame] | 1256 | + " name=" + name + " file=" + wallpaper.wallpaperFile.getName()); |
Christopher Tate | ad3c259 | 2016-01-20 18:13:17 -0800 | [diff] [blame] | 1257 | } |
Dianne Hackborn | 4c62fc0 | 2009-08-08 20:40:27 -0700 | [diff] [blame] | 1258 | return fd; |
| 1259 | } catch (FileNotFoundException e) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1260 | Slog.w(TAG, "Error setting wallpaper", e); |
Dianne Hackborn | 4c62fc0 | 2009-08-08 20:40:27 -0700 | [diff] [blame] | 1261 | } |
| 1262 | return null; |
| 1263 | } |
| 1264 | |
Benjamin Franz | f3ece36 | 2015-02-11 10:51:10 +0000 | [diff] [blame] | 1265 | public void setWallpaperComponentChecked(ComponentName name, String callingPackage) { |
Oleksandr Peletskyi | f251981 | 2016-01-26 20:16:06 +0100 | [diff] [blame] | 1266 | if (isWallpaperSupported(callingPackage) && isWallpaperSettingAllowed(callingPackage)) { |
Benjamin Franz | f3ece36 | 2015-02-11 10:51:10 +0000 | [diff] [blame] | 1267 | setWallpaperComponent(name); |
| 1268 | } |
| 1269 | } |
| 1270 | |
| 1271 | // ToDo: Remove this version of the function |
Dianne Hackborn | 4c62fc0 | 2009-08-08 20:40:27 -0700 | [diff] [blame] | 1272 | public void setWallpaperComponent(ComponentName name) { |
| 1273 | checkPermission(android.Manifest.permission.SET_WALLPAPER_COMPONENT); |
| 1274 | synchronized (mLock) { |
Dianne Hackborn | 5dc5a00 | 2012-09-15 19:33:48 -0700 | [diff] [blame] | 1275 | if (DEBUG) Slog.v(TAG, "setWallpaperComponent name=" + name); |
| 1276 | int userId = UserHandle.getCallingUserId(); |
| 1277 | WallpaperData wallpaper = mWallpaperMap.get(userId); |
| 1278 | if (wallpaper == null) { |
| 1279 | throw new IllegalStateException("Wallpaper not yet initialized for user " + userId); |
| 1280 | } |
Dianne Hackborn | 4c62fc0 | 2009-08-08 20:40:27 -0700 | [diff] [blame] | 1281 | final long ident = Binder.clearCallingIdentity(); |
| 1282 | try { |
Amith Yamasani | 37ce3a8 | 2012-02-06 12:04:42 -0800 | [diff] [blame] | 1283 | wallpaper.imageWallpaperPending = false; |
Dianne Hackborn | 5dc5a00 | 2012-09-15 19:33:48 -0700 | [diff] [blame] | 1284 | bindWallpaperComponentLocked(name, false, true, wallpaper, null); |
Dianne Hackborn | f21adf6 | 2009-08-13 10:20:21 -0700 | [diff] [blame] | 1285 | } finally { |
| 1286 | Binder.restoreCallingIdentity(ident); |
| 1287 | } |
| 1288 | } |
| 1289 | } |
Oleksandr Peletskyi | f251981 | 2016-01-26 20:16:06 +0100 | [diff] [blame] | 1290 | |
Amith Yamasani | 37ce3a8 | 2012-02-06 12:04:42 -0800 | [diff] [blame] | 1291 | boolean bindWallpaperComponentLocked(ComponentName componentName, boolean force, |
Dianne Hackborn | 5dc5a00 | 2012-09-15 19:33:48 -0700 | [diff] [blame] | 1292 | boolean fromUser, WallpaperData wallpaper, IRemoteCallback reply) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1293 | if (DEBUG) Slog.v(TAG, "bindWallpaperComponentLocked: componentName=" + componentName); |
Dianne Hackborn | f21adf6 | 2009-08-13 10:20:21 -0700 | [diff] [blame] | 1294 | // Has the component changed? |
Dianne Hackborn | 9ea3163 | 2011-08-05 14:43:50 -0700 | [diff] [blame] | 1295 | if (!force) { |
Amith Yamasani | 37ce3a8 | 2012-02-06 12:04:42 -0800 | [diff] [blame] | 1296 | if (wallpaper.connection != null) { |
| 1297 | if (wallpaper.wallpaperComponent == null) { |
Dianne Hackborn | 9ea3163 | 2011-08-05 14:43:50 -0700 | [diff] [blame] | 1298 | if (componentName == null) { |
| 1299 | if (DEBUG) Slog.v(TAG, "bindWallpaperComponentLocked: still using default"); |
| 1300 | // Still using default wallpaper. |
Dianne Hackborn | 80b902f | 2011-09-15 15:15:27 -0700 | [diff] [blame] | 1301 | return true; |
Dianne Hackborn | 9ea3163 | 2011-08-05 14:43:50 -0700 | [diff] [blame] | 1302 | } |
Amith Yamasani | 37ce3a8 | 2012-02-06 12:04:42 -0800 | [diff] [blame] | 1303 | } else if (wallpaper.wallpaperComponent.equals(componentName)) { |
Dianne Hackborn | 9ea3163 | 2011-08-05 14:43:50 -0700 | [diff] [blame] | 1304 | // Changing to same wallpaper. |
| 1305 | if (DEBUG) Slog.v(TAG, "same wallpaper"); |
Dianne Hackborn | 80b902f | 2011-09-15 15:15:27 -0700 | [diff] [blame] | 1306 | return true; |
Dianne Hackborn | 4c62fc0 | 2009-08-08 20:40:27 -0700 | [diff] [blame] | 1307 | } |
Dianne Hackborn | f21adf6 | 2009-08-13 10:20:21 -0700 | [diff] [blame] | 1308 | } |
| 1309 | } |
Oleksandr Peletskyi | f251981 | 2016-01-26 20:16:06 +0100 | [diff] [blame] | 1310 | |
Dianne Hackborn | f21adf6 | 2009-08-13 10:20:21 -0700 | [diff] [blame] | 1311 | try { |
Mike Cleron | a428b2c | 2009-11-15 22:53:08 -0800 | [diff] [blame] | 1312 | if (componentName == null) { |
Jeff Sharkey | 28f0877 | 2014-04-16 09:41:58 -0700 | [diff] [blame] | 1313 | componentName = WallpaperManager.getDefaultWallpaperComponent(mContext); |
Mike Cleron | a428b2c | 2009-11-15 22:53:08 -0800 | [diff] [blame] | 1314 | if (componentName == null) { |
Mike Cleron | 322b6ee | 2009-11-12 07:45:47 -0800 | [diff] [blame] | 1315 | // Fall back to static image wallpaper |
Justin Koh | 29c3016 | 2014-09-05 17:10:10 -0700 | [diff] [blame] | 1316 | componentName = mImageWallpaper; |
Mike Cleron | 322b6ee | 2009-11-12 07:45:47 -0800 | [diff] [blame] | 1317 | //clearWallpaperComponentLocked(); |
| 1318 | //return; |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1319 | if (DEBUG) Slog.v(TAG, "Using image wallpaper"); |
Mike Cleron | 322b6ee | 2009-11-12 07:45:47 -0800 | [diff] [blame] | 1320 | } |
Dianne Hackborn | f21adf6 | 2009-08-13 10:20:21 -0700 | [diff] [blame] | 1321 | } |
Amith Yamasani | 4e2820c | 2012-08-28 22:17:23 -0700 | [diff] [blame] | 1322 | int serviceUserId = wallpaper.userId; |
| 1323 | ServiceInfo si = mIPackageManager.getServiceInfo(componentName, |
| 1324 | PackageManager.GET_META_DATA | PackageManager.GET_PERMISSIONS, serviceUserId); |
Christopher Tate | 9095220 | 2013-09-08 13:01:28 -0700 | [diff] [blame] | 1325 | if (si == null) { |
| 1326 | // The wallpaper component we're trying to use doesn't exist |
| 1327 | Slog.w(TAG, "Attempted wallpaper " + componentName + " is unavailable"); |
| 1328 | return false; |
| 1329 | } |
Dianne Hackborn | f21adf6 | 2009-08-13 10:20:21 -0700 | [diff] [blame] | 1330 | if (!android.Manifest.permission.BIND_WALLPAPER.equals(si.permission)) { |
Dianne Hackborn | 80b902f | 2011-09-15 15:15:27 -0700 | [diff] [blame] | 1331 | String msg = "Selected service does not require " |
Dianne Hackborn | f21adf6 | 2009-08-13 10:20:21 -0700 | [diff] [blame] | 1332 | + android.Manifest.permission.BIND_WALLPAPER |
Dianne Hackborn | 80b902f | 2011-09-15 15:15:27 -0700 | [diff] [blame] | 1333 | + ": " + componentName; |
| 1334 | if (fromUser) { |
| 1335 | throw new SecurityException(msg); |
| 1336 | } |
| 1337 | Slog.w(TAG, msg); |
| 1338 | return false; |
Dianne Hackborn | f21adf6 | 2009-08-13 10:20:21 -0700 | [diff] [blame] | 1339 | } |
Oleksandr Peletskyi | f251981 | 2016-01-26 20:16:06 +0100 | [diff] [blame] | 1340 | |
Dianne Hackborn | eb03465 | 2009-09-07 00:49:58 -0700 | [diff] [blame] | 1341 | WallpaperInfo wi = null; |
Oleksandr Peletskyi | f251981 | 2016-01-26 20:16:06 +0100 | [diff] [blame] | 1342 | |
Dianne Hackborn | f21adf6 | 2009-08-13 10:20:21 -0700 | [diff] [blame] | 1343 | Intent intent = new Intent(WallpaperService.SERVICE_INTERFACE); |
Justin Koh | 29c3016 | 2014-09-05 17:10:10 -0700 | [diff] [blame] | 1344 | if (componentName != null && !componentName.equals(mImageWallpaper)) { |
Dianne Hackborn | 4c62fc0 | 2009-08-08 20:40:27 -0700 | [diff] [blame] | 1345 | // Make sure the selected service is actually a wallpaper service. |
Amith Yamasani | 4e2820c | 2012-08-28 22:17:23 -0700 | [diff] [blame] | 1346 | List<ResolveInfo> ris = |
| 1347 | mIPackageManager.queryIntentServices(intent, |
| 1348 | intent.resolveTypeIfNeeded(mContext.getContentResolver()), |
Jeff Sharkey | d589663 | 2016-03-04 16:16:00 -0700 | [diff] [blame] | 1349 | PackageManager.GET_META_DATA, serviceUserId).getList(); |
Dianne Hackborn | 4c62fc0 | 2009-08-08 20:40:27 -0700 | [diff] [blame] | 1350 | for (int i=0; i<ris.size(); i++) { |
| 1351 | ServiceInfo rsi = ris.get(i).serviceInfo; |
| 1352 | if (rsi.name.equals(si.name) && |
| 1353 | rsi.packageName.equals(si.packageName)) { |
Dianne Hackborn | eb03465 | 2009-09-07 00:49:58 -0700 | [diff] [blame] | 1354 | try { |
| 1355 | wi = new WallpaperInfo(mContext, ris.get(i)); |
| 1356 | } catch (XmlPullParserException e) { |
Dianne Hackborn | 80b902f | 2011-09-15 15:15:27 -0700 | [diff] [blame] | 1357 | if (fromUser) { |
| 1358 | throw new IllegalArgumentException(e); |
| 1359 | } |
| 1360 | Slog.w(TAG, e); |
| 1361 | return false; |
Dianne Hackborn | eb03465 | 2009-09-07 00:49:58 -0700 | [diff] [blame] | 1362 | } catch (IOException e) { |
Dianne Hackborn | 80b902f | 2011-09-15 15:15:27 -0700 | [diff] [blame] | 1363 | if (fromUser) { |
| 1364 | throw new IllegalArgumentException(e); |
| 1365 | } |
| 1366 | Slog.w(TAG, e); |
| 1367 | return false; |
Dianne Hackborn | eb03465 | 2009-09-07 00:49:58 -0700 | [diff] [blame] | 1368 | } |
Dianne Hackborn | 4c62fc0 | 2009-08-08 20:40:27 -0700 | [diff] [blame] | 1369 | break; |
| 1370 | } |
| 1371 | } |
Dianne Hackborn | eb03465 | 2009-09-07 00:49:58 -0700 | [diff] [blame] | 1372 | if (wi == null) { |
Dianne Hackborn | 80b902f | 2011-09-15 15:15:27 -0700 | [diff] [blame] | 1373 | String msg = "Selected service is not a wallpaper: " |
| 1374 | + componentName; |
| 1375 | if (fromUser) { |
| 1376 | throw new SecurityException(msg); |
| 1377 | } |
| 1378 | Slog.w(TAG, msg); |
| 1379 | return false; |
Dianne Hackborn | 4c62fc0 | 2009-08-08 20:40:27 -0700 | [diff] [blame] | 1380 | } |
Dianne Hackborn | 4c62fc0 | 2009-08-08 20:40:27 -0700 | [diff] [blame] | 1381 | } |
Oleksandr Peletskyi | f251981 | 2016-01-26 20:16:06 +0100 | [diff] [blame] | 1382 | |
Dianne Hackborn | f21adf6 | 2009-08-13 10:20:21 -0700 | [diff] [blame] | 1383 | // Bind the service! |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1384 | if (DEBUG) Slog.v(TAG, "Binding to:" + componentName); |
Amith Yamasani | 37ce3a8 | 2012-02-06 12:04:42 -0800 | [diff] [blame] | 1385 | WallpaperConnection newConn = new WallpaperConnection(wi, wallpaper); |
Mike Cleron | a428b2c | 2009-11-15 22:53:08 -0800 | [diff] [blame] | 1386 | intent.setComponent(componentName); |
Dianne Hackborn | dd9b82c | 2009-09-03 00:18:47 -0700 | [diff] [blame] | 1387 | intent.putExtra(Intent.EXTRA_CLIENT_LABEL, |
| 1388 | com.android.internal.R.string.wallpaper_binding_label); |
Dianne Hackborn | 4120375 | 2012-08-31 14:05:51 -0700 | [diff] [blame] | 1389 | intent.putExtra(Intent.EXTRA_CLIENT_INTENT, PendingIntent.getActivityAsUser( |
Dianne Hackborn | eb03465 | 2009-09-07 00:49:58 -0700 | [diff] [blame] | 1390 | mContext, 0, |
| 1391 | Intent.createChooser(new Intent(Intent.ACTION_SET_WALLPAPER), |
| 1392 | mContext.getText(com.android.internal.R.string.chooser_wallpaper)), |
Dianne Hackborn | 4120375 | 2012-08-31 14:05:51 -0700 | [diff] [blame] | 1393 | 0, null, new UserHandle(serviceUserId))); |
Dianne Hackborn | c823051 | 2013-07-13 21:32:12 -0700 | [diff] [blame] | 1394 | if (!mContext.bindServiceAsUser(intent, newConn, |
Dianne Hackborn | d69e4c1 | 2015-04-24 09:54:54 -0700 | [diff] [blame] | 1395 | Context.BIND_AUTO_CREATE | Context.BIND_SHOWING_UI |
| 1396 | | Context.BIND_FOREGROUND_SERVICE_WHILE_AWAKE, |
Amith Yamasani | 27b89e6 | 2013-01-16 12:30:11 -0800 | [diff] [blame] | 1397 | new UserHandle(serviceUserId))) { |
Dianne Hackborn | 80b902f | 2011-09-15 15:15:27 -0700 | [diff] [blame] | 1398 | String msg = "Unable to bind service: " |
| 1399 | + componentName; |
| 1400 | if (fromUser) { |
| 1401 | throw new IllegalArgumentException(msg); |
| 1402 | } |
| 1403 | Slog.w(TAG, msg); |
| 1404 | return false; |
Dianne Hackborn | f21adf6 | 2009-08-13 10:20:21 -0700 | [diff] [blame] | 1405 | } |
Amith Yamasani | 37ce3a8 | 2012-02-06 12:04:42 -0800 | [diff] [blame] | 1406 | if (wallpaper.userId == mCurrentUserId && mLastWallpaper != null) { |
| 1407 | detachWallpaperLocked(mLastWallpaper); |
| 1408 | } |
| 1409 | wallpaper.wallpaperComponent = componentName; |
| 1410 | wallpaper.connection = newConn; |
Dianne Hackborn | 5dc5a00 | 2012-09-15 19:33:48 -0700 | [diff] [blame] | 1411 | newConn.mReply = reply; |
Dianne Hackborn | f21adf6 | 2009-08-13 10:20:21 -0700 | [diff] [blame] | 1412 | try { |
Amith Yamasani | 37ce3a8 | 2012-02-06 12:04:42 -0800 | [diff] [blame] | 1413 | if (wallpaper.userId == mCurrentUserId) { |
| 1414 | if (DEBUG) |
| 1415 | Slog.v(TAG, "Adding window token: " + newConn.mToken); |
| 1416 | mIWindowManager.addWindowToken(newConn.mToken, |
| 1417 | WindowManager.LayoutParams.TYPE_WALLPAPER); |
| 1418 | mLastWallpaper = wallpaper; |
| 1419 | } |
Dianne Hackborn | f21adf6 | 2009-08-13 10:20:21 -0700 | [diff] [blame] | 1420 | } catch (RemoteException e) { |
| 1421 | } |
Amith Yamasani | 4e2820c | 2012-08-28 22:17:23 -0700 | [diff] [blame] | 1422 | } catch (RemoteException e) { |
| 1423 | String msg = "Remote exception for " + componentName + "\n" + e; |
Dianne Hackborn | 80b902f | 2011-09-15 15:15:27 -0700 | [diff] [blame] | 1424 | if (fromUser) { |
| 1425 | throw new IllegalArgumentException(msg); |
| 1426 | } |
| 1427 | Slog.w(TAG, msg); |
| 1428 | return false; |
Dianne Hackborn | 4c62fc0 | 2009-08-08 20:40:27 -0700 | [diff] [blame] | 1429 | } |
Dianne Hackborn | 80b902f | 2011-09-15 15:15:27 -0700 | [diff] [blame] | 1430 | return true; |
Dianne Hackborn | 4c62fc0 | 2009-08-08 20:40:27 -0700 | [diff] [blame] | 1431 | } |
Amith Yamasani | 37ce3a8 | 2012-02-06 12:04:42 -0800 | [diff] [blame] | 1432 | |
| 1433 | void detachWallpaperLocked(WallpaperData wallpaper) { |
| 1434 | if (wallpaper.connection != null) { |
Dianne Hackborn | 5dc5a00 | 2012-09-15 19:33:48 -0700 | [diff] [blame] | 1435 | if (wallpaper.connection.mReply != null) { |
| 1436 | try { |
| 1437 | wallpaper.connection.mReply.sendResult(null); |
| 1438 | } catch (RemoteException e) { |
| 1439 | } |
| 1440 | wallpaper.connection.mReply = null; |
| 1441 | } |
Amith Yamasani | 37ce3a8 | 2012-02-06 12:04:42 -0800 | [diff] [blame] | 1442 | if (wallpaper.connection.mEngine != null) { |
Dianne Hackborn | 4c62fc0 | 2009-08-08 20:40:27 -0700 | [diff] [blame] | 1443 | try { |
Amith Yamasani | 37ce3a8 | 2012-02-06 12:04:42 -0800 | [diff] [blame] | 1444 | wallpaper.connection.mEngine.destroy(); |
Dianne Hackborn | 4c62fc0 | 2009-08-08 20:40:27 -0700 | [diff] [blame] | 1445 | } catch (RemoteException e) { |
| 1446 | } |
| 1447 | } |
Amith Yamasani | 37ce3a8 | 2012-02-06 12:04:42 -0800 | [diff] [blame] | 1448 | mContext.unbindService(wallpaper.connection); |
Dianne Hackborn | e9e9bca | 2009-08-18 15:08:22 -0700 | [diff] [blame] | 1449 | try { |
Amith Yamasani | 37ce3a8 | 2012-02-06 12:04:42 -0800 | [diff] [blame] | 1450 | if (DEBUG) |
| 1451 | Slog.v(TAG, "Removing window token: " + wallpaper.connection.mToken); |
| 1452 | mIWindowManager.removeWindowToken(wallpaper.connection.mToken); |
Dianne Hackborn | e9e9bca | 2009-08-18 15:08:22 -0700 | [diff] [blame] | 1453 | } catch (RemoteException e) { |
| 1454 | } |
Amith Yamasani | 37ce3a8 | 2012-02-06 12:04:42 -0800 | [diff] [blame] | 1455 | wallpaper.connection.mService = null; |
| 1456 | wallpaper.connection.mEngine = null; |
| 1457 | wallpaper.connection = null; |
Dianne Hackborn | 4c62fc0 | 2009-08-08 20:40:27 -0700 | [diff] [blame] | 1458 | } |
| 1459 | } |
Amith Yamasani | 37ce3a8 | 2012-02-06 12:04:42 -0800 | [diff] [blame] | 1460 | |
| 1461 | void clearWallpaperComponentLocked(WallpaperData wallpaper) { |
| 1462 | wallpaper.wallpaperComponent = null; |
| 1463 | detachWallpaperLocked(wallpaper); |
| 1464 | } |
| 1465 | |
| 1466 | void attachServiceLocked(WallpaperConnection conn, WallpaperData wallpaper) { |
Dianne Hackborn | 4c62fc0 | 2009-08-08 20:40:27 -0700 | [diff] [blame] | 1467 | try { |
Dianne Hackborn | 3be63c0 | 2009-08-20 19:31:38 -0700 | [diff] [blame] | 1468 | conn.mService.attach(conn, conn.mToken, |
| 1469 | WindowManager.LayoutParams.TYPE_WALLPAPER, false, |
Dianne Hackborn | 067e5f6 | 2014-09-07 23:14:30 -0700 | [diff] [blame] | 1470 | wallpaper.width, wallpaper.height, wallpaper.padding); |
Dianne Hackborn | 4c62fc0 | 2009-08-08 20:40:27 -0700 | [diff] [blame] | 1471 | } catch (RemoteException e) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1472 | Slog.w(TAG, "Failed attaching wallpaper; clearing", e); |
Amith Yamasani | 37ce3a8 | 2012-02-06 12:04:42 -0800 | [diff] [blame] | 1473 | if (!wallpaper.wallpaperUpdating) { |
Dianne Hackborn | 5dc5a00 | 2012-09-15 19:33:48 -0700 | [diff] [blame] | 1474 | bindWallpaperComponentLocked(null, false, false, wallpaper, null); |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 1475 | } |
Dianne Hackborn | 4c62fc0 | 2009-08-08 20:40:27 -0700 | [diff] [blame] | 1476 | } |
| 1477 | } |
Amith Yamasani | 37ce3a8 | 2012-02-06 12:04:42 -0800 | [diff] [blame] | 1478 | |
| 1479 | private void notifyCallbacksLocked(WallpaperData wallpaper) { |
| 1480 | final int n = wallpaper.callbacks.beginBroadcast(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1481 | for (int i = 0; i < n; i++) { |
| 1482 | try { |
Amith Yamasani | 37ce3a8 | 2012-02-06 12:04:42 -0800 | [diff] [blame] | 1483 | wallpaper.callbacks.getBroadcastItem(i).onWallpaperChanged(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1484 | } catch (RemoteException e) { |
| 1485 | |
| 1486 | // The RemoteCallbackList will take care of removing |
| 1487 | // the dead object for us. |
| 1488 | } |
| 1489 | } |
Amith Yamasani | 37ce3a8 | 2012-02-06 12:04:42 -0800 | [diff] [blame] | 1490 | wallpaper.callbacks.finishBroadcast(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1491 | final Intent intent = new Intent(Intent.ACTION_WALLPAPER_CHANGED); |
Dianne Hackborn | 5ac72a2 | 2012-08-29 18:32:08 -0700 | [diff] [blame] | 1492 | mContext.sendBroadcastAsUser(intent, new UserHandle(mCurrentUserId)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1493 | } |
| 1494 | |
| 1495 | private void checkPermission(String permission) { |
Joe Onorato | 9bb8fd7 | 2009-07-28 18:24:51 -0700 | [diff] [blame] | 1496 | if (PackageManager.PERMISSION_GRANTED!= mContext.checkCallingOrSelfPermission(permission)) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1497 | throw new SecurityException("Access denied to process: " + Binder.getCallingPid() |
| 1498 | + ", must have permission " + permission); |
| 1499 | } |
| 1500 | } |
Joe Onorato | 9bb8fd7 | 2009-07-28 18:24:51 -0700 | [diff] [blame] | 1501 | |
Benjamin Franz | f3ece36 | 2015-02-11 10:51:10 +0000 | [diff] [blame] | 1502 | /** |
| 1503 | * Certain user types do not support wallpapers (e.g. managed profiles). The check is |
| 1504 | * implemented through through the OP_WRITE_WALLPAPER AppOp. |
| 1505 | */ |
| 1506 | public boolean isWallpaperSupported(String callingPackage) { |
| 1507 | return mAppOpsManager.checkOpNoThrow(AppOpsManager.OP_WRITE_WALLPAPER, Binder.getCallingUid(), |
| 1508 | callingPackage) == AppOpsManager.MODE_ALLOWED; |
| 1509 | } |
| 1510 | |
Oleksandr Peletskyi | f251981 | 2016-01-26 20:16:06 +0100 | [diff] [blame] | 1511 | @Override |
| 1512 | public boolean isWallpaperSettingAllowed(String callingPackage) { |
| 1513 | final PackageManager pm = mContext.getPackageManager(); |
| 1514 | String[] uidPackages = pm.getPackagesForUid(Binder.getCallingUid()); |
| 1515 | boolean uidMatchPackage = Arrays.asList(uidPackages).contains(callingPackage); |
| 1516 | if (!uidMatchPackage) { |
| 1517 | return false; // callingPackage was faked. |
| 1518 | } |
| 1519 | |
| 1520 | final DevicePolicyManager dpm = mContext.getSystemService(DevicePolicyManager.class); |
| 1521 | if (dpm.isDeviceOwnerApp(callingPackage) || dpm.isProfileOwnerApp(callingPackage)) { |
| 1522 | return true; |
| 1523 | } |
| 1524 | final UserManager um = (UserManager) mContext.getSystemService(Context.USER_SERVICE); |
| 1525 | return !um.hasUserRestriction(UserManager.DISALLOW_SET_WALLPAPER); |
| 1526 | } |
| 1527 | |
Amith Yamasani | 37ce3a8 | 2012-02-06 12:04:42 -0800 | [diff] [blame] | 1528 | private static JournaledFile makeJournaledFile(int userId) { |
Amith Yamasani | 61f5737 | 2012-08-31 12:12:28 -0700 | [diff] [blame] | 1529 | final String base = new File(getWallpaperDir(userId), WALLPAPER_INFO).getAbsolutePath(); |
Joe Onorato | 9bb8fd7 | 2009-07-28 18:24:51 -0700 | [diff] [blame] | 1530 | return new JournaledFile(new File(base), new File(base + ".tmp")); |
| 1531 | } |
| 1532 | |
Christopher Tate | db27b84 | 2016-02-25 14:39:17 -0800 | [diff] [blame] | 1533 | private void saveSettingsLocked(int userId) { |
| 1534 | JournaledFile journal = makeJournaledFile(userId); |
| 1535 | FileOutputStream fstream = null; |
| 1536 | BufferedOutputStream stream = null; |
Joe Onorato | 9bb8fd7 | 2009-07-28 18:24:51 -0700 | [diff] [blame] | 1537 | try { |
Joe Onorato | 9bb8fd7 | 2009-07-28 18:24:51 -0700 | [diff] [blame] | 1538 | XmlSerializer out = new FastXmlSerializer(); |
Christopher Tate | db27b84 | 2016-02-25 14:39:17 -0800 | [diff] [blame] | 1539 | fstream = new FileOutputStream(journal.chooseForWrite(), false); |
| 1540 | stream = new BufferedOutputStream(fstream); |
Wojciech Staszkiewicz | 9e9e2e7 | 2015-05-08 14:58:46 +0100 | [diff] [blame] | 1541 | out.setOutput(stream, StandardCharsets.UTF_8.name()); |
Joe Onorato | 9bb8fd7 | 2009-07-28 18:24:51 -0700 | [diff] [blame] | 1542 | out.startDocument(null, true); |
| 1543 | |
Christopher Tate | db27b84 | 2016-02-25 14:39:17 -0800 | [diff] [blame] | 1544 | WallpaperData wallpaper; |
Christopher Tate | 1e1e2e0 | 2016-01-25 15:34:36 -0800 | [diff] [blame] | 1545 | |
Christopher Tate | db27b84 | 2016-02-25 14:39:17 -0800 | [diff] [blame] | 1546 | wallpaper = mWallpaperMap.get(userId); |
| 1547 | if (wallpaper != null) { |
| 1548 | writeWallpaperAttributes(out, "wp", wallpaper); |
Dianne Hackborn | 067e5f6 | 2014-09-07 23:14:30 -0700 | [diff] [blame] | 1549 | } |
Christopher Tate | db27b84 | 2016-02-25 14:39:17 -0800 | [diff] [blame] | 1550 | wallpaper = mLockWallpaperMap.get(userId); |
| 1551 | if (wallpaper != null) { |
| 1552 | writeWallpaperAttributes(out, "kwp", wallpaper); |
Dianne Hackborn | 067e5f6 | 2014-09-07 23:14:30 -0700 | [diff] [blame] | 1553 | } |
Joe Onorato | 9bb8fd7 | 2009-07-28 18:24:51 -0700 | [diff] [blame] | 1554 | |
| 1555 | out.endDocument(); |
Christopher Tate | db27b84 | 2016-02-25 14:39:17 -0800 | [diff] [blame] | 1556 | |
| 1557 | stream.flush(); // also flushes fstream |
| 1558 | FileUtils.sync(fstream); |
| 1559 | stream.close(); // also closes fstream |
Joe Onorato | 9bb8fd7 | 2009-07-28 18:24:51 -0700 | [diff] [blame] | 1560 | journal.commit(); |
| 1561 | } catch (IOException e) { |
Christopher Tate | ad3c259 | 2016-01-20 18:13:17 -0800 | [diff] [blame] | 1562 | IoUtils.closeQuietly(stream); |
Joe Onorato | 9bb8fd7 | 2009-07-28 18:24:51 -0700 | [diff] [blame] | 1563 | journal.rollback(); |
| 1564 | } |
| 1565 | } |
| 1566 | |
Christopher Tate | db27b84 | 2016-02-25 14:39:17 -0800 | [diff] [blame] | 1567 | private void writeWallpaperAttributes(XmlSerializer out, String tag, WallpaperData wallpaper) |
| 1568 | throws IllegalArgumentException, IllegalStateException, IOException { |
| 1569 | out.startTag(null, tag); |
| 1570 | out.attribute(null, "id", Integer.toString(wallpaper.wallpaperId)); |
| 1571 | out.attribute(null, "width", Integer.toString(wallpaper.width)); |
| 1572 | out.attribute(null, "height", Integer.toString(wallpaper.height)); |
| 1573 | |
| 1574 | out.attribute(null, "cropLeft", Integer.toString(wallpaper.cropHint.left)); |
| 1575 | out.attribute(null, "cropTop", Integer.toString(wallpaper.cropHint.top)); |
| 1576 | out.attribute(null, "cropRight", Integer.toString(wallpaper.cropHint.right)); |
| 1577 | out.attribute(null, "cropBottom", Integer.toString(wallpaper.cropHint.bottom)); |
| 1578 | |
| 1579 | if (wallpaper.padding.left != 0) { |
| 1580 | out.attribute(null, "paddingLeft", Integer.toString(wallpaper.padding.left)); |
| 1581 | } |
| 1582 | if (wallpaper.padding.top != 0) { |
| 1583 | out.attribute(null, "paddingTop", Integer.toString(wallpaper.padding.top)); |
| 1584 | } |
| 1585 | if (wallpaper.padding.right != 0) { |
| 1586 | out.attribute(null, "paddingRight", Integer.toString(wallpaper.padding.right)); |
| 1587 | } |
| 1588 | if (wallpaper.padding.bottom != 0) { |
| 1589 | out.attribute(null, "paddingBottom", Integer.toString(wallpaper.padding.bottom)); |
| 1590 | } |
| 1591 | |
| 1592 | out.attribute(null, "name", wallpaper.name); |
| 1593 | if (wallpaper.wallpaperComponent != null |
| 1594 | && !wallpaper.wallpaperComponent.equals(mImageWallpaper)) { |
| 1595 | out.attribute(null, "component", |
| 1596 | wallpaper.wallpaperComponent.flattenToShortString()); |
| 1597 | } |
| 1598 | out.endTag(null, tag); |
| 1599 | } |
| 1600 | |
Amith Yamasani | 37ce3a8 | 2012-02-06 12:04:42 -0800 | [diff] [blame] | 1601 | private void migrateFromOld() { |
| 1602 | File oldWallpaper = new File(WallpaperBackupHelper.WALLPAPER_IMAGE_KEY); |
| 1603 | File oldInfo = new File(WallpaperBackupHelper.WALLPAPER_INFO_KEY); |
| 1604 | if (oldWallpaper.exists()) { |
| 1605 | File newWallpaper = new File(getWallpaperDir(0), WALLPAPER); |
| 1606 | oldWallpaper.renameTo(newWallpaper); |
| 1607 | } |
| 1608 | if (oldInfo.exists()) { |
| 1609 | File newInfo = new File(getWallpaperDir(0), WALLPAPER_INFO); |
| 1610 | oldInfo.renameTo(newInfo); |
| 1611 | } |
| 1612 | } |
| 1613 | |
Dianne Hackborn | 067e5f6 | 2014-09-07 23:14:30 -0700 | [diff] [blame] | 1614 | private int getAttributeInt(XmlPullParser parser, String name, int defValue) { |
| 1615 | String value = parser.getAttributeValue(null, name); |
| 1616 | if (value == null) { |
| 1617 | return defValue; |
| 1618 | } |
| 1619 | return Integer.parseInt(value); |
| 1620 | } |
| 1621 | |
Xiaohui Chen | ac53194 | 2015-05-13 13:20:52 -0700 | [diff] [blame] | 1622 | /** |
| 1623 | * Sometimes it is expected the wallpaper map may not have a user's data. E.g. This could |
| 1624 | * happen during user switch. The async user switch observer may not have received |
| 1625 | * the event yet. We use this safe method when we don't care about this ordering and just |
| 1626 | * want to update the data. The data is going to be applied when the user switch observer |
| 1627 | * is eventually executed. |
| 1628 | */ |
Christopher Tate | be132e6 | 2016-02-10 12:59:49 -0800 | [diff] [blame] | 1629 | private WallpaperData getWallpaperSafeLocked(int userId, int which) { |
| 1630 | // We're setting either just system (work with the system wallpaper), |
| 1631 | // both (also work with the system wallpaper), or just the lock |
| 1632 | // wallpaper (update against the existing lock wallpaper if any). |
| 1633 | // Combined or just-system operations use the 'system' WallpaperData |
| 1634 | // for this use; lock-only operations use the dedicated one. |
| 1635 | final SparseArray<WallpaperData> whichSet = |
| 1636 | (which == FLAG_SET_LOCK) ? mLockWallpaperMap : mWallpaperMap; |
| 1637 | WallpaperData wallpaper = whichSet.get(userId); |
Xiaohui Chen | ac53194 | 2015-05-13 13:20:52 -0700 | [diff] [blame] | 1638 | if (wallpaper == null) { |
Christopher Tate | be132e6 | 2016-02-10 12:59:49 -0800 | [diff] [blame] | 1639 | // common case, this is the first lookup post-boot of the system or |
| 1640 | // unified lock, so we bring up the saved state lazily now and recheck. |
Xiaohui Chen | ac53194 | 2015-05-13 13:20:52 -0700 | [diff] [blame] | 1641 | loadSettingsLocked(userId); |
Christopher Tate | be132e6 | 2016-02-10 12:59:49 -0800 | [diff] [blame] | 1642 | wallpaper = whichSet.get(userId); |
| 1643 | // if it's still null here, this is a lock-only operation and there is not |
| 1644 | // yet a lock-only wallpaper set for this user, so we need to establish |
| 1645 | // it now. |
| 1646 | if (wallpaper == null) { |
| 1647 | if (which == FLAG_SET_LOCK) { |
| 1648 | wallpaper = new WallpaperData(userId, |
| 1649 | WALLPAPER_LOCK_ORIG, WALLPAPER_LOCK_CROP); |
| 1650 | mLockWallpaperMap.put(userId, wallpaper); |
| 1651 | } else { |
| 1652 | // sanity fallback: we're in bad shape, but establishing a known |
| 1653 | // valid system+lock WallpaperData will keep us from dying. |
| 1654 | Slog.wtf(TAG, "Didn't find wallpaper in non-lock case!"); |
| 1655 | wallpaper = new WallpaperData(userId, WALLPAPER, WALLPAPER_CROP); |
| 1656 | mWallpaperMap.put(userId, wallpaper); |
| 1657 | } |
| 1658 | } |
Xiaohui Chen | ac53194 | 2015-05-13 13:20:52 -0700 | [diff] [blame] | 1659 | } |
| 1660 | return wallpaper; |
| 1661 | } |
| 1662 | |
Amith Yamasani | 37ce3a8 | 2012-02-06 12:04:42 -0800 | [diff] [blame] | 1663 | private void loadSettingsLocked(int userId) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1664 | if (DEBUG) Slog.v(TAG, "loadSettingsLocked"); |
Benjamin Franz | f3ece36 | 2015-02-11 10:51:10 +0000 | [diff] [blame] | 1665 | |
Amith Yamasani | 37ce3a8 | 2012-02-06 12:04:42 -0800 | [diff] [blame] | 1666 | JournaledFile journal = makeJournaledFile(userId); |
Joe Onorato | 9bb8fd7 | 2009-07-28 18:24:51 -0700 | [diff] [blame] | 1667 | FileInputStream stream = null; |
| 1668 | File file = journal.chooseForRead(); |
Amith Yamasani | 37ce3a8 | 2012-02-06 12:04:42 -0800 | [diff] [blame] | 1669 | if (!file.exists()) { |
| 1670 | // This should only happen one time, when upgrading from a legacy system |
| 1671 | migrateFromOld(); |
| 1672 | } |
| 1673 | WallpaperData wallpaper = mWallpaperMap.get(userId); |
| 1674 | if (wallpaper == null) { |
Christopher Tate | be132e6 | 2016-02-10 12:59:49 -0800 | [diff] [blame] | 1675 | wallpaper = new WallpaperData(userId, WALLPAPER, WALLPAPER_CROP); |
Amith Yamasani | 37ce3a8 | 2012-02-06 12:04:42 -0800 | [diff] [blame] | 1676 | mWallpaperMap.put(userId, wallpaper); |
Christopher Tate | 41297ff | 2016-03-10 16:46:15 -0800 | [diff] [blame] | 1677 | if (!wallpaper.cropExists()) { |
| 1678 | generateCrop(wallpaper); |
| 1679 | } |
Amith Yamasani | 37ce3a8 | 2012-02-06 12:04:42 -0800 | [diff] [blame] | 1680 | } |
Joe Onorato | 9bb8fd7 | 2009-07-28 18:24:51 -0700 | [diff] [blame] | 1681 | boolean success = false; |
| 1682 | try { |
| 1683 | stream = new FileInputStream(file); |
| 1684 | XmlPullParser parser = Xml.newPullParser(); |
Wojciech Staszkiewicz | 9e9e2e7 | 2015-05-08 14:58:46 +0100 | [diff] [blame] | 1685 | parser.setInput(stream, StandardCharsets.UTF_8.name()); |
Joe Onorato | 9bb8fd7 | 2009-07-28 18:24:51 -0700 | [diff] [blame] | 1686 | |
| 1687 | int type; |
Joe Onorato | 9bb8fd7 | 2009-07-28 18:24:51 -0700 | [diff] [blame] | 1688 | do { |
| 1689 | type = parser.next(); |
| 1690 | if (type == XmlPullParser.START_TAG) { |
| 1691 | String tag = parser.getName(); |
| 1692 | if ("wp".equals(tag)) { |
Christopher Tate | be132e6 | 2016-02-10 12:59:49 -0800 | [diff] [blame] | 1693 | // Common to system + lock wallpapers |
| 1694 | parseWallpaperAttributes(parser, wallpaper); |
Christopher Tate | ad3c259 | 2016-01-20 18:13:17 -0800 | [diff] [blame] | 1695 | |
Christopher Tate | be132e6 | 2016-02-10 12:59:49 -0800 | [diff] [blame] | 1696 | // A system wallpaper might also be a live wallpaper |
Dianne Hackborn | 4c62fc0 | 2009-08-08 20:40:27 -0700 | [diff] [blame] | 1697 | String comp = parser.getAttributeValue(null, "component"); |
Amith Yamasani | 37ce3a8 | 2012-02-06 12:04:42 -0800 | [diff] [blame] | 1698 | wallpaper.nextWallpaperComponent = comp != null |
Dianne Hackborn | 4c62fc0 | 2009-08-08 20:40:27 -0700 | [diff] [blame] | 1699 | ? ComponentName.unflattenFromString(comp) |
| 1700 | : null; |
Amith Yamasani | 37ce3a8 | 2012-02-06 12:04:42 -0800 | [diff] [blame] | 1701 | if (wallpaper.nextWallpaperComponent == null |
| 1702 | || "android".equals(wallpaper.nextWallpaperComponent |
| 1703 | .getPackageName())) { |
Justin Koh | 29c3016 | 2014-09-05 17:10:10 -0700 | [diff] [blame] | 1704 | wallpaper.nextWallpaperComponent = mImageWallpaper; |
Dianne Hackborn | 9ea3163 | 2011-08-05 14:43:50 -0700 | [diff] [blame] | 1705 | } |
Oleksandr Peletskyi | f251981 | 2016-01-26 20:16:06 +0100 | [diff] [blame] | 1706 | |
Mike Cleron | a428b2c | 2009-11-15 22:53:08 -0800 | [diff] [blame] | 1707 | if (DEBUG) { |
Amith Yamasani | 37ce3a8 | 2012-02-06 12:04:42 -0800 | [diff] [blame] | 1708 | Slog.v(TAG, "mWidth:" + wallpaper.width); |
| 1709 | Slog.v(TAG, "mHeight:" + wallpaper.height); |
Christopher Tate | 1e1e2e0 | 2016-01-25 15:34:36 -0800 | [diff] [blame] | 1710 | Slog.v(TAG, "cropRect:" + wallpaper.cropHint); |
Amith Yamasani | 37ce3a8 | 2012-02-06 12:04:42 -0800 | [diff] [blame] | 1711 | Slog.v(TAG, "mName:" + wallpaper.name); |
| 1712 | Slog.v(TAG, "mNextWallpaperComponent:" |
| 1713 | + wallpaper.nextWallpaperComponent); |
Mike Cleron | a428b2c | 2009-11-15 22:53:08 -0800 | [diff] [blame] | 1714 | } |
Christopher Tate | be132e6 | 2016-02-10 12:59:49 -0800 | [diff] [blame] | 1715 | } else if ("kwp".equals(tag)) { |
| 1716 | // keyguard-specific wallpaper for this user |
| 1717 | WallpaperData lockWallpaper = mLockWallpaperMap.get(userId); |
| 1718 | if (lockWallpaper == null) { |
| 1719 | lockWallpaper = new WallpaperData(userId, |
| 1720 | WALLPAPER_LOCK_ORIG, WALLPAPER_LOCK_CROP); |
| 1721 | mLockWallpaperMap.put(userId, lockWallpaper); |
| 1722 | } |
| 1723 | parseWallpaperAttributes(parser, lockWallpaper); |
Joe Onorato | 9bb8fd7 | 2009-07-28 18:24:51 -0700 | [diff] [blame] | 1724 | } |
| 1725 | } |
| 1726 | } while (type != XmlPullParser.END_DOCUMENT); |
| 1727 | success = true; |
Dianne Hackborn | 13579ed | 2012-11-28 18:05:36 -0800 | [diff] [blame] | 1728 | } catch (FileNotFoundException e) { |
| 1729 | Slog.w(TAG, "no current wallpaper -- first boot?"); |
Joe Onorato | 9bb8fd7 | 2009-07-28 18:24:51 -0700 | [diff] [blame] | 1730 | } catch (NullPointerException e) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1731 | Slog.w(TAG, "failed parsing " + file + " " + e); |
Joe Onorato | 9bb8fd7 | 2009-07-28 18:24:51 -0700 | [diff] [blame] | 1732 | } catch (NumberFormatException e) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1733 | Slog.w(TAG, "failed parsing " + file + " " + e); |
Joe Onorato | 9bb8fd7 | 2009-07-28 18:24:51 -0700 | [diff] [blame] | 1734 | } catch (XmlPullParserException e) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1735 | Slog.w(TAG, "failed parsing " + file + " " + e); |
Joe Onorato | 9bb8fd7 | 2009-07-28 18:24:51 -0700 | [diff] [blame] | 1736 | } catch (IOException e) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1737 | Slog.w(TAG, "failed parsing " + file + " " + e); |
Joe Onorato | 9bb8fd7 | 2009-07-28 18:24:51 -0700 | [diff] [blame] | 1738 | } catch (IndexOutOfBoundsException e) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1739 | Slog.w(TAG, "failed parsing " + file + " " + e); |
Joe Onorato | 9bb8fd7 | 2009-07-28 18:24:51 -0700 | [diff] [blame] | 1740 | } |
Christopher Tate | ad3c259 | 2016-01-20 18:13:17 -0800 | [diff] [blame] | 1741 | IoUtils.closeQuietly(stream); |
Joe Onorato | 9bb8fd7 | 2009-07-28 18:24:51 -0700 | [diff] [blame] | 1742 | |
| 1743 | if (!success) { |
Amith Yamasani | 37ce3a8 | 2012-02-06 12:04:42 -0800 | [diff] [blame] | 1744 | wallpaper.width = -1; |
| 1745 | wallpaper.height = -1; |
Christopher Tate | 1e1e2e0 | 2016-01-25 15:34:36 -0800 | [diff] [blame] | 1746 | wallpaper.cropHint.set(0, 0, 0, 0); |
Dianne Hackborn | 067e5f6 | 2014-09-07 23:14:30 -0700 | [diff] [blame] | 1747 | wallpaper.padding.set(0, 0, 0, 0); |
Amith Yamasani | 37ce3a8 | 2012-02-06 12:04:42 -0800 | [diff] [blame] | 1748 | wallpaper.name = ""; |
Christopher Tate | ad3c259 | 2016-01-20 18:13:17 -0800 | [diff] [blame] | 1749 | } else { |
| 1750 | if (wallpaper.wallpaperId <= 0) { |
| 1751 | wallpaper.wallpaperId = makeWallpaperIdLocked(); |
| 1752 | if (DEBUG) { |
| 1753 | Slog.w(TAG, "Didn't set wallpaper id in loadSettingsLocked(" + userId |
| 1754 | + "); now " + wallpaper.wallpaperId); |
| 1755 | } |
| 1756 | } |
Joe Onorato | 9bb8fd7 | 2009-07-28 18:24:51 -0700 | [diff] [blame] | 1757 | } |
Dianne Hackborn | 44bc17c | 2011-04-20 18:18:51 -0700 | [diff] [blame] | 1758 | |
| 1759 | // We always want to have some reasonable width hint. |
John Spurlock | 7ea91ec | 2013-11-04 13:48:38 -0500 | [diff] [blame] | 1760 | int baseSize = getMaximumSizeDimension(); |
Amith Yamasani | 37ce3a8 | 2012-02-06 12:04:42 -0800 | [diff] [blame] | 1761 | if (wallpaper.width < baseSize) { |
| 1762 | wallpaper.width = baseSize; |
Dianne Hackborn | 44bc17c | 2011-04-20 18:18:51 -0700 | [diff] [blame] | 1763 | } |
Amith Yamasani | 37ce3a8 | 2012-02-06 12:04:42 -0800 | [diff] [blame] | 1764 | if (wallpaper.height < baseSize) { |
| 1765 | wallpaper.height = baseSize; |
Dianne Hackborn | 44bc17c | 2011-04-20 18:18:51 -0700 | [diff] [blame] | 1766 | } |
Christopher Tate | 1e1e2e0 | 2016-01-25 15:34:36 -0800 | [diff] [blame] | 1767 | // and crop, if not previously specified |
| 1768 | if (wallpaper.cropHint.width() <= 0 |
| 1769 | || wallpaper.cropHint.height() <= 0) { |
| 1770 | wallpaper.cropHint.set(0, 0, wallpaper.width, wallpaper.height); |
| 1771 | } |
Joe Onorato | 9bb8fd7 | 2009-07-28 18:24:51 -0700 | [diff] [blame] | 1772 | } |
| 1773 | |
Christopher Tate | be132e6 | 2016-02-10 12:59:49 -0800 | [diff] [blame] | 1774 | private void parseWallpaperAttributes(XmlPullParser parser, WallpaperData wallpaper) { |
| 1775 | final String idString = parser.getAttributeValue(null, "id"); |
| 1776 | if (idString != null) { |
| 1777 | final int id = wallpaper.wallpaperId = Integer.parseInt(idString); |
| 1778 | if (id > mWallpaperId) { |
| 1779 | mWallpaperId = id; |
| 1780 | } |
| 1781 | } else { |
| 1782 | wallpaper.wallpaperId = makeWallpaperIdLocked(); |
| 1783 | } |
| 1784 | |
| 1785 | wallpaper.width = Integer.parseInt(parser.getAttributeValue(null, "width")); |
| 1786 | wallpaper.height = Integer.parseInt(parser |
| 1787 | .getAttributeValue(null, "height")); |
| 1788 | wallpaper.cropHint.left = getAttributeInt(parser, "cropLeft", 0); |
| 1789 | wallpaper.cropHint.top = getAttributeInt(parser, "cropTop", 0); |
| 1790 | wallpaper.cropHint.right = getAttributeInt(parser, "cropRight", 0); |
| 1791 | wallpaper.cropHint.bottom = getAttributeInt(parser, "cropBottom", 0); |
| 1792 | wallpaper.padding.left = getAttributeInt(parser, "paddingLeft", 0); |
| 1793 | wallpaper.padding.top = getAttributeInt(parser, "paddingTop", 0); |
| 1794 | wallpaper.padding.right = getAttributeInt(parser, "paddingRight", 0); |
| 1795 | wallpaper.padding.bottom = getAttributeInt(parser, "paddingBottom", 0); |
| 1796 | wallpaper.name = parser.getAttributeValue(null, "name"); |
| 1797 | } |
| 1798 | |
John Spurlock | 7ea91ec | 2013-11-04 13:48:38 -0500 | [diff] [blame] | 1799 | private int getMaximumSizeDimension() { |
| 1800 | WindowManager wm = (WindowManager)mContext.getSystemService(Context.WINDOW_SERVICE); |
| 1801 | Display d = wm.getDefaultDisplay(); |
| 1802 | return d.getMaximumSizeDimension(); |
| 1803 | } |
| 1804 | |
Brad Fitzpatrick | 194b19a | 2010-09-14 11:30:29 -0700 | [diff] [blame] | 1805 | // Called by SystemBackupAgent after files are restored to disk. |
Amith Yamasani | 09e9cdc | 2013-11-06 14:54:50 -0800 | [diff] [blame] | 1806 | public void settingsRestored() { |
| 1807 | // Verify caller is the system |
| 1808 | if (Binder.getCallingUid() != android.os.Process.SYSTEM_UID) { |
| 1809 | throw new RuntimeException("settingsRestored() can only be called from the system process"); |
| 1810 | } |
Amith Yamasani | 37ce3a8 | 2012-02-06 12:04:42 -0800 | [diff] [blame] | 1811 | // TODO: If necessary, make it work for secondary users as well. This currently assumes |
| 1812 | // restores only to the primary user |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1813 | if (DEBUG) Slog.v(TAG, "settingsRestored"); |
Amith Yamasani | 37ce3a8 | 2012-02-06 12:04:42 -0800 | [diff] [blame] | 1814 | WallpaperData wallpaper = null; |
Joe Onorato | 9bb8fd7 | 2009-07-28 18:24:51 -0700 | [diff] [blame] | 1815 | boolean success = false; |
| 1816 | synchronized (mLock) { |
Christopher Tate | db27b84 | 2016-02-25 14:39:17 -0800 | [diff] [blame] | 1817 | loadSettingsLocked(UserHandle.USER_SYSTEM); |
| 1818 | wallpaper = mWallpaperMap.get(UserHandle.USER_SYSTEM); |
Christopher Tate | ad3c259 | 2016-01-20 18:13:17 -0800 | [diff] [blame] | 1819 | wallpaper.wallpaperId = makeWallpaperIdLocked(); // always bump id at restore |
Amith Yamasani | 37ce3a8 | 2012-02-06 12:04:42 -0800 | [diff] [blame] | 1820 | if (wallpaper.nextWallpaperComponent != null |
Justin Koh | 29c3016 | 2014-09-05 17:10:10 -0700 | [diff] [blame] | 1821 | && !wallpaper.nextWallpaperComponent.equals(mImageWallpaper)) { |
Amith Yamasani | 37ce3a8 | 2012-02-06 12:04:42 -0800 | [diff] [blame] | 1822 | if (!bindWallpaperComponentLocked(wallpaper.nextWallpaperComponent, false, false, |
Dianne Hackborn | 5dc5a00 | 2012-09-15 19:33:48 -0700 | [diff] [blame] | 1823 | wallpaper, null)) { |
Christopher Tate | e3ab4d0 | 2009-12-16 14:03:31 -0800 | [diff] [blame] | 1824 | // No such live wallpaper or other failure; fall back to the default |
| 1825 | // live wallpaper (since the profile being restored indicated that the |
| 1826 | // user had selected a live rather than static one). |
Dianne Hackborn | 5dc5a00 | 2012-09-15 19:33:48 -0700 | [diff] [blame] | 1827 | bindWallpaperComponentLocked(null, false, false, wallpaper, null); |
Christopher Tate | e3ab4d0 | 2009-12-16 14:03:31 -0800 | [diff] [blame] | 1828 | } |
Joe Onorato | 9bb8fd7 | 2009-07-28 18:24:51 -0700 | [diff] [blame] | 1829 | success = true; |
| 1830 | } else { |
Mike Cleron | a428b2c | 2009-11-15 22:53:08 -0800 | [diff] [blame] | 1831 | // If there's a wallpaper name, we use that. If that can't be loaded, then we |
| 1832 | // use the default. |
Amith Yamasani | 37ce3a8 | 2012-02-06 12:04:42 -0800 | [diff] [blame] | 1833 | if ("".equals(wallpaper.name)) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1834 | if (DEBUG) Slog.v(TAG, "settingsRestored: name is empty"); |
Mike Cleron | a428b2c | 2009-11-15 22:53:08 -0800 | [diff] [blame] | 1835 | success = true; |
| 1836 | } else { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1837 | if (DEBUG) Slog.v(TAG, "settingsRestored: attempting to restore named resource"); |
Amith Yamasani | 37ce3a8 | 2012-02-06 12:04:42 -0800 | [diff] [blame] | 1838 | success = restoreNamedResourceLocked(wallpaper); |
Mike Cleron | a428b2c | 2009-11-15 22:53:08 -0800 | [diff] [blame] | 1839 | } |
Christopher Tate | ad3c259 | 2016-01-20 18:13:17 -0800 | [diff] [blame] | 1840 | if (DEBUG) Slog.v(TAG, "settingsRestored: success=" + success |
| 1841 | + " id=" + wallpaper.wallpaperId); |
Mike Cleron | a428b2c | 2009-11-15 22:53:08 -0800 | [diff] [blame] | 1842 | if (success) { |
Christopher Tate | 41297ff | 2016-03-10 16:46:15 -0800 | [diff] [blame] | 1843 | generateCrop(wallpaper); // based on the new image + metadata |
| 1844 | bindWallpaperComponentLocked(wallpaper.nextWallpaperComponent, true, false, |
Dianne Hackborn | 5dc5a00 | 2012-09-15 19:33:48 -0700 | [diff] [blame] | 1845 | wallpaper, null); |
Mike Cleron | a428b2c | 2009-11-15 22:53:08 -0800 | [diff] [blame] | 1846 | } |
Joe Onorato | 9bb8fd7 | 2009-07-28 18:24:51 -0700 | [diff] [blame] | 1847 | } |
| 1848 | } |
| 1849 | |
| 1850 | if (!success) { |
Amith Yamasani | 37ce3a8 | 2012-02-06 12:04:42 -0800 | [diff] [blame] | 1851 | Slog.e(TAG, "Failed to restore wallpaper: '" + wallpaper.name + "'"); |
| 1852 | wallpaper.name = ""; |
Christopher Tate | db27b84 | 2016-02-25 14:39:17 -0800 | [diff] [blame] | 1853 | getWallpaperDir(UserHandle.USER_SYSTEM).delete(); |
Joe Onorato | 9bb8fd7 | 2009-07-28 18:24:51 -0700 | [diff] [blame] | 1854 | } |
Brad Fitzpatrick | 194b19a | 2010-09-14 11:30:29 -0700 | [diff] [blame] | 1855 | |
| 1856 | synchronized (mLock) { |
Christopher Tate | db27b84 | 2016-02-25 14:39:17 -0800 | [diff] [blame] | 1857 | saveSettingsLocked(UserHandle.USER_SYSTEM); |
Brad Fitzpatrick | 194b19a | 2010-09-14 11:30:29 -0700 | [diff] [blame] | 1858 | } |
Joe Onorato | 9bb8fd7 | 2009-07-28 18:24:51 -0700 | [diff] [blame] | 1859 | } |
| 1860 | |
Christopher Tate | 1e1e2e0 | 2016-01-25 15:34:36 -0800 | [diff] [blame] | 1861 | // Restore the named resource bitmap to both source + crop files |
Amith Yamasani | 37ce3a8 | 2012-02-06 12:04:42 -0800 | [diff] [blame] | 1862 | boolean restoreNamedResourceLocked(WallpaperData wallpaper) { |
| 1863 | if (wallpaper.name.length() > 4 && "res:".equals(wallpaper.name.substring(0, 4))) { |
| 1864 | String resName = wallpaper.name.substring(4); |
Joe Onorato | 9bb8fd7 | 2009-07-28 18:24:51 -0700 | [diff] [blame] | 1865 | |
| 1866 | String pkg = null; |
| 1867 | int colon = resName.indexOf(':'); |
| 1868 | if (colon > 0) { |
| 1869 | pkg = resName.substring(0, colon); |
| 1870 | } |
| 1871 | |
| 1872 | String ident = null; |
| 1873 | int slash = resName.lastIndexOf('/'); |
| 1874 | if (slash > 0) { |
| 1875 | ident = resName.substring(slash+1); |
| 1876 | } |
| 1877 | |
| 1878 | String type = null; |
| 1879 | if (colon > 0 && slash > 0 && (slash-colon) > 1) { |
| 1880 | type = resName.substring(colon+1, slash); |
| 1881 | } |
| 1882 | |
| 1883 | if (pkg != null && ident != null && type != null) { |
| 1884 | int resId = -1; |
| 1885 | InputStream res = null; |
| 1886 | FileOutputStream fos = null; |
Christopher Tate | 1e1e2e0 | 2016-01-25 15:34:36 -0800 | [diff] [blame] | 1887 | FileOutputStream cos = null; |
Joe Onorato | 9bb8fd7 | 2009-07-28 18:24:51 -0700 | [diff] [blame] | 1888 | try { |
| 1889 | Context c = mContext.createPackageContext(pkg, Context.CONTEXT_RESTRICTED); |
| 1890 | Resources r = c.getResources(); |
| 1891 | resId = r.getIdentifier(resName, null, null); |
| 1892 | if (resId == 0) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1893 | Slog.e(TAG, "couldn't resolve identifier pkg=" + pkg + " type=" + type |
Joe Onorato | 9bb8fd7 | 2009-07-28 18:24:51 -0700 | [diff] [blame] | 1894 | + " ident=" + ident); |
| 1895 | return false; |
| 1896 | } |
| 1897 | |
| 1898 | res = r.openRawResource(resId); |
Amith Yamasani | 37ce3a8 | 2012-02-06 12:04:42 -0800 | [diff] [blame] | 1899 | if (wallpaper.wallpaperFile.exists()) { |
| 1900 | wallpaper.wallpaperFile.delete(); |
Christopher Tate | 1e1e2e0 | 2016-01-25 15:34:36 -0800 | [diff] [blame] | 1901 | wallpaper.cropFile.delete(); |
Dianne Hackborn | 1afd1c9 | 2010-03-18 22:47:17 -0700 | [diff] [blame] | 1902 | } |
Amith Yamasani | 37ce3a8 | 2012-02-06 12:04:42 -0800 | [diff] [blame] | 1903 | fos = new FileOutputStream(wallpaper.wallpaperFile); |
Christopher Tate | 1e1e2e0 | 2016-01-25 15:34:36 -0800 | [diff] [blame] | 1904 | cos = new FileOutputStream(wallpaper.cropFile); |
Joe Onorato | 9bb8fd7 | 2009-07-28 18:24:51 -0700 | [diff] [blame] | 1905 | |
| 1906 | byte[] buffer = new byte[32768]; |
| 1907 | int amt; |
| 1908 | while ((amt=res.read(buffer)) > 0) { |
| 1909 | fos.write(buffer, 0, amt); |
Christopher Tate | 1e1e2e0 | 2016-01-25 15:34:36 -0800 | [diff] [blame] | 1910 | cos.write(buffer, 0, amt); |
Joe Onorato | 9bb8fd7 | 2009-07-28 18:24:51 -0700 | [diff] [blame] | 1911 | } |
| 1912 | // mWallpaperObserver will notice the close and send the change broadcast |
| 1913 | |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1914 | Slog.v(TAG, "Restored wallpaper: " + resName); |
Joe Onorato | 9bb8fd7 | 2009-07-28 18:24:51 -0700 | [diff] [blame] | 1915 | return true; |
| 1916 | } catch (NameNotFoundException e) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1917 | Slog.e(TAG, "Package name " + pkg + " not found"); |
Joe Onorato | 9bb8fd7 | 2009-07-28 18:24:51 -0700 | [diff] [blame] | 1918 | } catch (Resources.NotFoundException e) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1919 | Slog.e(TAG, "Resource not found: " + resId); |
Joe Onorato | 9bb8fd7 | 2009-07-28 18:24:51 -0700 | [diff] [blame] | 1920 | } catch (IOException e) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1921 | Slog.e(TAG, "IOException while restoring wallpaper ", e); |
Joe Onorato | 9bb8fd7 | 2009-07-28 18:24:51 -0700 | [diff] [blame] | 1922 | } finally { |
Christopher Tate | ad3c259 | 2016-01-20 18:13:17 -0800 | [diff] [blame] | 1923 | IoUtils.closeQuietly(res); |
Joe Onorato | 9bb8fd7 | 2009-07-28 18:24:51 -0700 | [diff] [blame] | 1924 | if (fos != null) { |
Dianne Hackborn | 8bdf593 | 2010-10-15 12:54:40 -0700 | [diff] [blame] | 1925 | FileUtils.sync(fos); |
Joe Onorato | 9bb8fd7 | 2009-07-28 18:24:51 -0700 | [diff] [blame] | 1926 | } |
Christopher Tate | 1e1e2e0 | 2016-01-25 15:34:36 -0800 | [diff] [blame] | 1927 | if (cos != null) { |
| 1928 | FileUtils.sync(cos); |
| 1929 | } |
| 1930 | IoUtils.closeQuietly(fos); |
| 1931 | IoUtils.closeQuietly(cos); |
Joe Onorato | 9bb8fd7 | 2009-07-28 18:24:51 -0700 | [diff] [blame] | 1932 | } |
| 1933 | } |
| 1934 | } |
| 1935 | return false; |
| 1936 | } |
Amith Yamasani | 37ce3a8 | 2012-02-06 12:04:42 -0800 | [diff] [blame] | 1937 | |
Dianne Hackborn | eb03465 | 2009-09-07 00:49:58 -0700 | [diff] [blame] | 1938 | @Override |
| 1939 | protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) { |
| 1940 | if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DUMP) |
| 1941 | != PackageManager.PERMISSION_GRANTED) { |
Oleksandr Peletskyi | f251981 | 2016-01-26 20:16:06 +0100 | [diff] [blame] | 1942 | |
Dianne Hackborn | eb03465 | 2009-09-07 00:49:58 -0700 | [diff] [blame] | 1943 | pw.println("Permission Denial: can't dump wallpaper service from from pid=" |
| 1944 | + Binder.getCallingPid() |
| 1945 | + ", uid=" + Binder.getCallingUid()); |
| 1946 | return; |
| 1947 | } |
| 1948 | |
| 1949 | synchronized (mLock) { |
| 1950 | pw.println("Current Wallpaper Service state:"); |
Amith Yamasani | 37ce3a8 | 2012-02-06 12:04:42 -0800 | [diff] [blame] | 1951 | for (int i = 0; i < mWallpaperMap.size(); i++) { |
| 1952 | WallpaperData wallpaper = mWallpaperMap.valueAt(i); |
Christopher Tate | ad3c259 | 2016-01-20 18:13:17 -0800 | [diff] [blame] | 1953 | pw.print(" User "); pw.print(wallpaper.userId); |
| 1954 | pw.print(": id="); pw.println(wallpaper.wallpaperId); |
Amith Yamasani | 37ce3a8 | 2012-02-06 12:04:42 -0800 | [diff] [blame] | 1955 | pw.print(" mWidth="); |
Dianne Hackborn | 067e5f6 | 2014-09-07 23:14:30 -0700 | [diff] [blame] | 1956 | pw.print(wallpaper.width); |
| 1957 | pw.print(" mHeight="); |
| 1958 | pw.println(wallpaper.height); |
Christopher Tate | 1e1e2e0 | 2016-01-25 15:34:36 -0800 | [diff] [blame] | 1959 | pw.print(" mCropHint="); pw.println(wallpaper.cropHint); |
Dianne Hackborn | 067e5f6 | 2014-09-07 23:14:30 -0700 | [diff] [blame] | 1960 | pw.print(" mPadding="); pw.println(wallpaper.padding); |
| 1961 | pw.print(" mName="); pw.println(wallpaper.name); |
| 1962 | pw.print(" mWallpaperComponent="); pw.println(wallpaper.wallpaperComponent); |
Amith Yamasani | 37ce3a8 | 2012-02-06 12:04:42 -0800 | [diff] [blame] | 1963 | if (wallpaper.connection != null) { |
| 1964 | WallpaperConnection conn = wallpaper.connection; |
| 1965 | pw.print(" Wallpaper connection "); |
| 1966 | pw.print(conn); |
| 1967 | pw.println(":"); |
| 1968 | if (conn.mInfo != null) { |
| 1969 | pw.print(" mInfo.component="); |
| 1970 | pw.println(conn.mInfo.getComponent()); |
| 1971 | } |
| 1972 | pw.print(" mToken="); |
| 1973 | pw.println(conn.mToken); |
| 1974 | pw.print(" mService="); |
| 1975 | pw.println(conn.mService); |
| 1976 | pw.print(" mEngine="); |
| 1977 | pw.println(conn.mEngine); |
| 1978 | pw.print(" mLastDiedTime="); |
| 1979 | pw.println(wallpaper.lastDiedTime - SystemClock.uptimeMillis()); |
| 1980 | } |
Dianne Hackborn | eb03465 | 2009-09-07 00:49:58 -0700 | [diff] [blame] | 1981 | } |
| 1982 | } |
| 1983 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1984 | } |