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 | |
| 17 | package com.android.server; |
| 18 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 19 | import static android.os.ParcelFileDescriptor.*; |
Christopher Tate | 111bd4a | 2009-06-24 17:29:38 -0700 | [diff] [blame] | 20 | |
Dianne Hackborn | 5dc5a00 | 2012-09-15 19:33:48 -0700 | [diff] [blame] | 21 | import android.app.ActivityManagerNative; |
Amith Yamasani | 4e2820c | 2012-08-28 22:17:23 -0700 | [diff] [blame] | 22 | import android.app.AppGlobals; |
Dianne Hackborn | 5dc5a00 | 2012-09-15 19:33:48 -0700 | [diff] [blame] | 23 | import android.app.IUserSwitchObserver; |
Dianne Hackborn | 8cc6a50 | 2009-08-05 21:29:42 -0700 | [diff] [blame] | 24 | import android.app.IWallpaperManager; |
| 25 | import android.app.IWallpaperManagerCallback; |
Dianne Hackborn | dd9b82c | 2009-09-03 00:18:47 -0700 | [diff] [blame] | 26 | import android.app.PendingIntent; |
Dianne Hackborn | eb03465 | 2009-09-07 00:49:58 -0700 | [diff] [blame] | 27 | import android.app.WallpaperInfo; |
Christopher Tate | 4528186 | 2010-03-05 15:46:30 -0800 | [diff] [blame] | 28 | import android.app.backup.BackupManager; |
Amith Yamasani | 37ce3a8 | 2012-02-06 12:04:42 -0800 | [diff] [blame] | 29 | import android.app.backup.WallpaperBackupHelper; |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 30 | import android.content.BroadcastReceiver; |
Dianne Hackborn | 4c62fc0 | 2009-08-08 20:40:27 -0700 | [diff] [blame] | 31 | import android.content.ComponentName; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 32 | import android.content.Context; |
| 33 | import android.content.Intent; |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 34 | import android.content.IntentFilter; |
Dianne Hackborn | 4c62fc0 | 2009-08-08 20:40:27 -0700 | [diff] [blame] | 35 | import android.content.ServiceConnection; |
Amith Yamasani | 4e2820c | 2012-08-28 22:17:23 -0700 | [diff] [blame] | 36 | import android.content.pm.IPackageManager; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 37 | import android.content.pm.PackageManager; |
Dianne Hackborn | 4c62fc0 | 2009-08-08 20:40:27 -0700 | [diff] [blame] | 38 | import android.content.pm.ResolveInfo; |
| 39 | import android.content.pm.ServiceInfo; |
Joe Onorato | 9bb8fd7 | 2009-07-28 18:24:51 -0700 | [diff] [blame] | 40 | import android.content.pm.PackageManager.NameNotFoundException; |
| 41 | import android.content.res.Resources; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 42 | import android.os.Binder; |
Dianne Hackborn | 284ac93 | 2009-08-28 10:34:25 -0700 | [diff] [blame] | 43 | import android.os.Bundle; |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 44 | import android.os.Environment; |
Dianne Hackborn | 8bdf593 | 2010-10-15 12:54:40 -0700 | [diff] [blame] | 45 | import android.os.FileUtils; |
Dianne Hackborn | 4c62fc0 | 2009-08-08 20:40:27 -0700 | [diff] [blame] | 46 | import android.os.IBinder; |
Dianne Hackborn | 5dc5a00 | 2012-09-15 19:33:48 -0700 | [diff] [blame] | 47 | import android.os.IRemoteCallback; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 48 | import android.os.RemoteException; |
| 49 | import android.os.FileObserver; |
| 50 | import android.os.ParcelFileDescriptor; |
| 51 | import android.os.RemoteCallbackList; |
rpcraig | 554cb0c | 2012-07-05 06:41:43 -0400 | [diff] [blame] | 52 | import android.os.SELinux; |
Dianne Hackborn | 4c62fc0 | 2009-08-08 20:40:27 -0700 | [diff] [blame] | 53 | import android.os.ServiceManager; |
Dianne Hackborn | 0cd4887 | 2009-08-13 18:51:59 -0700 | [diff] [blame] | 54 | import android.os.SystemClock; |
Dianne Hackborn | f02b60a | 2012-08-16 10:48:27 -0700 | [diff] [blame] | 55 | import android.os.UserHandle; |
Dianne Hackborn | 4c62fc0 | 2009-08-08 20:40:27 -0700 | [diff] [blame] | 56 | import android.service.wallpaper.IWallpaperConnection; |
| 57 | import android.service.wallpaper.IWallpaperEngine; |
| 58 | import android.service.wallpaper.IWallpaperService; |
| 59 | import android.service.wallpaper.WallpaperService; |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 60 | import android.util.Slog; |
Amith Yamasani | 37ce3a8 | 2012-02-06 12:04:42 -0800 | [diff] [blame] | 61 | import android.util.SparseArray; |
Joe Onorato | 9bb8fd7 | 2009-07-28 18:24:51 -0700 | [diff] [blame] | 62 | import android.util.Xml; |
Dianne Hackborn | 44bc17c | 2011-04-20 18:18:51 -0700 | [diff] [blame] | 63 | import android.view.Display; |
Dianne Hackborn | 4c62fc0 | 2009-08-08 20:40:27 -0700 | [diff] [blame] | 64 | import android.view.IWindowManager; |
| 65 | import android.view.WindowManager; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 66 | |
Dianne Hackborn | eb03465 | 2009-09-07 00:49:58 -0700 | [diff] [blame] | 67 | import java.io.FileDescriptor; |
Joe Onorato | 9bb8fd7 | 2009-07-28 18:24:51 -0700 | [diff] [blame] | 68 | import java.io.IOException; |
| 69 | import java.io.InputStream; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 70 | import java.io.File; |
| 71 | import java.io.FileNotFoundException; |
Joe Onorato | 9bb8fd7 | 2009-07-28 18:24:51 -0700 | [diff] [blame] | 72 | import java.io.FileInputStream; |
| 73 | import java.io.FileOutputStream; |
Dianne Hackborn | eb03465 | 2009-09-07 00:49:58 -0700 | [diff] [blame] | 74 | import java.io.PrintWriter; |
Dianne Hackborn | 4c62fc0 | 2009-08-08 20:40:27 -0700 | [diff] [blame] | 75 | import java.util.List; |
Joe Onorato | 9bb8fd7 | 2009-07-28 18:24:51 -0700 | [diff] [blame] | 76 | |
| 77 | import org.xmlpull.v1.XmlPullParser; |
| 78 | import org.xmlpull.v1.XmlPullParserException; |
| 79 | import org.xmlpull.v1.XmlSerializer; |
| 80 | |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 81 | import com.android.internal.content.PackageMonitor; |
Dianne Hackborn | 2269d157 | 2010-02-24 19:54:22 -0800 | [diff] [blame] | 82 | import com.android.internal.util.FastXmlSerializer; |
Dianne Hackborn | 1afd1c9 | 2010-03-18 22:47:17 -0700 | [diff] [blame] | 83 | import com.android.internal.util.JournaledFile; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 84 | |
Dianne Hackborn | 8cc6a50 | 2009-08-05 21:29:42 -0700 | [diff] [blame] | 85 | class WallpaperManagerService extends IWallpaperManager.Stub { |
Dianne Hackborn | 4c62fc0 | 2009-08-08 20:40:27 -0700 | [diff] [blame] | 86 | static final String TAG = "WallpaperService"; |
Dianne Hackborn | cbf1504 | 2009-08-18 18:29:09 -0700 | [diff] [blame] | 87 | static final boolean DEBUG = false; |
Joe Onorato | 9bb8fd7 | 2009-07-28 18:24:51 -0700 | [diff] [blame] | 88 | |
Romain Guy | 407ec78 | 2011-08-24 17:06:58 -0700 | [diff] [blame] | 89 | final Object mLock = new Object[0]; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 90 | |
Dianne Hackborn | 0cd4887 | 2009-08-13 18:51:59 -0700 | [diff] [blame] | 91 | /** |
| 92 | * Minimum time between crashes of a wallpaper service for us to consider |
| 93 | * restarting it vs. just reverting to the static wallpaper. |
| 94 | */ |
| 95 | static final long MIN_WALLPAPER_CRASH_TIME = 10000; |
Dianne Hackborn | 0cd4887 | 2009-08-13 18:51:59 -0700 | [diff] [blame] | 96 | static final String WALLPAPER = "wallpaper"; |
Amith Yamasani | 37ce3a8 | 2012-02-06 12:04:42 -0800 | [diff] [blame] | 97 | static final String WALLPAPER_INFO = "wallpaper_info.xml"; |
Joe Onorato | 9bb8fd7 | 2009-07-28 18:24:51 -0700 | [diff] [blame] | 98 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 99 | /** |
| 100 | * Observes the wallpaper for changes and notifies all IWallpaperServiceCallbacks |
| 101 | * that the wallpaper has changed. The CREATE is triggered when there is no |
| 102 | * wallpaper set and is created for the first time. The CLOSE_WRITE is triggered |
| 103 | * everytime the wallpaper is changed. |
| 104 | */ |
Amith Yamasani | 37ce3a8 | 2012-02-06 12:04:42 -0800 | [diff] [blame] | 105 | private class WallpaperObserver extends FileObserver { |
Joe Onorato | 9bb8fd7 | 2009-07-28 18:24:51 -0700 | [diff] [blame] | 106 | |
Amith Yamasani | 37ce3a8 | 2012-02-06 12:04:42 -0800 | [diff] [blame] | 107 | final WallpaperData mWallpaper; |
| 108 | final File mWallpaperDir; |
| 109 | final File mWallpaperFile; |
| 110 | |
| 111 | public WallpaperObserver(WallpaperData wallpaper) { |
| 112 | super(getWallpaperDir(wallpaper.userId).getAbsolutePath(), |
| 113 | CLOSE_WRITE | DELETE | DELETE_SELF); |
| 114 | mWallpaperDir = getWallpaperDir(wallpaper.userId); |
| 115 | mWallpaper = wallpaper; |
| 116 | mWallpaperFile = new File(mWallpaperDir, WALLPAPER); |
| 117 | } |
| 118 | |
| 119 | @Override |
| 120 | public void onEvent(int event, String path) { |
| 121 | if (path == null) { |
| 122 | return; |
| 123 | } |
| 124 | synchronized (mLock) { |
| 125 | // changing the wallpaper means we'll need to back up the new one |
| 126 | long origId = Binder.clearCallingIdentity(); |
| 127 | BackupManager bm = new BackupManager(mContext); |
| 128 | bm.dataChanged(); |
| 129 | Binder.restoreCallingIdentity(origId); |
| 130 | |
| 131 | File changedFile = new File(mWallpaperDir, path); |
| 132 | if (mWallpaperFile.equals(changedFile)) { |
| 133 | notifyCallbacksLocked(mWallpaper); |
| 134 | if (mWallpaper.wallpaperComponent == null || event != CLOSE_WRITE |
| 135 | || mWallpaper.imageWallpaperPending) { |
| 136 | if (event == CLOSE_WRITE) { |
| 137 | mWallpaper.imageWallpaperPending = false; |
Joe Onorato | 9bb8fd7 | 2009-07-28 18:24:51 -0700 | [diff] [blame] | 138 | } |
Amith Yamasani | 37ce3a8 | 2012-02-06 12:04:42 -0800 | [diff] [blame] | 139 | bindWallpaperComponentLocked(mWallpaper.imageWallpaperComponent, true, |
Dianne Hackborn | 5dc5a00 | 2012-09-15 19:33:48 -0700 | [diff] [blame] | 140 | false, mWallpaper, null); |
Amith Yamasani | 37ce3a8 | 2012-02-06 12:04:42 -0800 | [diff] [blame] | 141 | saveSettingsLocked(mWallpaper); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 142 | } |
| 143 | } |
Amith Yamasani | 37ce3a8 | 2012-02-06 12:04:42 -0800 | [diff] [blame] | 144 | } |
| 145 | } |
| 146 | } |
| 147 | |
Dianne Hackborn | 4c62fc0 | 2009-08-08 20:40:27 -0700 | [diff] [blame] | 148 | final Context mContext; |
| 149 | final IWindowManager mIWindowManager; |
Amith Yamasani | 4e2820c | 2012-08-28 22:17:23 -0700 | [diff] [blame] | 150 | final IPackageManager mIPackageManager; |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 151 | final MyPackageMonitor mMonitor; |
Amith Yamasani | 37ce3a8 | 2012-02-06 12:04:42 -0800 | [diff] [blame] | 152 | WallpaperData mLastWallpaper; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 153 | |
Amith Yamasani | 37ce3a8 | 2012-02-06 12:04:42 -0800 | [diff] [blame] | 154 | SparseArray<WallpaperData> mWallpaperMap = new SparseArray<WallpaperData>(); |
Dianne Hackborn | 07213e6 | 2011-08-24 20:05:39 -0700 | [diff] [blame] | 155 | |
Amith Yamasani | 37ce3a8 | 2012-02-06 12:04:42 -0800 | [diff] [blame] | 156 | int mCurrentUserId; |
Dianne Hackborn | 07213e6 | 2011-08-24 20:05:39 -0700 | [diff] [blame] | 157 | |
Amith Yamasani | 37ce3a8 | 2012-02-06 12:04:42 -0800 | [diff] [blame] | 158 | static class WallpaperData { |
| 159 | |
| 160 | int userId; |
| 161 | |
| 162 | File wallpaperFile; |
| 163 | |
| 164 | /** |
| 165 | * Client is currently writing a new image wallpaper. |
| 166 | */ |
| 167 | boolean imageWallpaperPending; |
| 168 | |
| 169 | /** |
| 170 | * Resource name if using a picture from the wallpaper gallery |
| 171 | */ |
| 172 | String name = ""; |
| 173 | |
| 174 | /** |
| 175 | * The component name of the currently set live wallpaper. |
| 176 | */ |
| 177 | ComponentName wallpaperComponent; |
| 178 | |
| 179 | /** |
| 180 | * The component name of the wallpaper that should be set next. |
| 181 | */ |
| 182 | ComponentName nextWallpaperComponent; |
| 183 | |
| 184 | /** |
| 185 | * Name of the component used to display bitmap wallpapers from either the gallery or |
| 186 | * built-in wallpapers. |
| 187 | */ |
| 188 | ComponentName imageWallpaperComponent = new ComponentName("com.android.systemui", |
| 189 | "com.android.systemui.ImageWallpaper"); |
| 190 | |
| 191 | WallpaperConnection connection; |
| 192 | long lastDiedTime; |
| 193 | boolean wallpaperUpdating; |
| 194 | WallpaperObserver wallpaperObserver; |
| 195 | |
| 196 | /** |
| 197 | * List of callbacks registered they should each be notified when the wallpaper is changed. |
| 198 | */ |
| 199 | private RemoteCallbackList<IWallpaperManagerCallback> callbacks |
| 200 | = new RemoteCallbackList<IWallpaperManagerCallback>(); |
| 201 | |
| 202 | int width = -1; |
| 203 | int height = -1; |
| 204 | |
| 205 | WallpaperData(int userId) { |
| 206 | this.userId = userId; |
| 207 | wallpaperFile = new File(getWallpaperDir(userId), WALLPAPER); |
| 208 | } |
| 209 | } |
| 210 | |
Dianne Hackborn | 4c62fc0 | 2009-08-08 20:40:27 -0700 | [diff] [blame] | 211 | class WallpaperConnection extends IWallpaperConnection.Stub |
| 212 | implements ServiceConnection { |
Dianne Hackborn | eb03465 | 2009-09-07 00:49:58 -0700 | [diff] [blame] | 213 | final WallpaperInfo mInfo; |
Dianne Hackborn | 4c62fc0 | 2009-08-08 20:40:27 -0700 | [diff] [blame] | 214 | final Binder mToken = new Binder(); |
| 215 | IWallpaperService mService; |
| 216 | IWallpaperEngine mEngine; |
Amith Yamasani | 37ce3a8 | 2012-02-06 12:04:42 -0800 | [diff] [blame] | 217 | WallpaperData mWallpaper; |
Dianne Hackborn | 5dc5a00 | 2012-09-15 19:33:48 -0700 | [diff] [blame] | 218 | IRemoteCallback mReply; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 219 | |
Amith Yamasani | 37ce3a8 | 2012-02-06 12:04:42 -0800 | [diff] [blame] | 220 | public WallpaperConnection(WallpaperInfo info, WallpaperData wallpaper) { |
Dianne Hackborn | eb03465 | 2009-09-07 00:49:58 -0700 | [diff] [blame] | 221 | mInfo = info; |
Amith Yamasani | 37ce3a8 | 2012-02-06 12:04:42 -0800 | [diff] [blame] | 222 | mWallpaper = wallpaper; |
Dianne Hackborn | eb03465 | 2009-09-07 00:49:58 -0700 | [diff] [blame] | 223 | } |
Dianne Hackborn | 5dc5a00 | 2012-09-15 19:33:48 -0700 | [diff] [blame] | 224 | |
| 225 | @Override |
Dianne Hackborn | 4c62fc0 | 2009-08-08 20:40:27 -0700 | [diff] [blame] | 226 | public void onServiceConnected(ComponentName name, IBinder service) { |
| 227 | synchronized (mLock) { |
Amith Yamasani | 37ce3a8 | 2012-02-06 12:04:42 -0800 | [diff] [blame] | 228 | if (mWallpaper.connection == this) { |
| 229 | mWallpaper.lastDiedTime = SystemClock.uptimeMillis(); |
Dianne Hackborn | 4c62fc0 | 2009-08-08 20:40:27 -0700 | [diff] [blame] | 230 | mService = IWallpaperService.Stub.asInterface(service); |
Amith Yamasani | 37ce3a8 | 2012-02-06 12:04:42 -0800 | [diff] [blame] | 231 | attachServiceLocked(this, mWallpaper); |
Dianne Hackborn | eb03465 | 2009-09-07 00:49:58 -0700 | [diff] [blame] | 232 | // XXX should probably do saveSettingsLocked() later |
| 233 | // when we have an engine, but I'm not sure about |
| 234 | // locking there and anyway we always need to be able to |
| 235 | // recover if there is something wrong. |
Amith Yamasani | 37ce3a8 | 2012-02-06 12:04:42 -0800 | [diff] [blame] | 236 | saveSettingsLocked(mWallpaper); |
Dianne Hackborn | 4c62fc0 | 2009-08-08 20:40:27 -0700 | [diff] [blame] | 237 | } |
| 238 | } |
| 239 | } |
| 240 | |
Dianne Hackborn | 5dc5a00 | 2012-09-15 19:33:48 -0700 | [diff] [blame] | 241 | @Override |
Dianne Hackborn | 4c62fc0 | 2009-08-08 20:40:27 -0700 | [diff] [blame] | 242 | public void onServiceDisconnected(ComponentName name) { |
| 243 | synchronized (mLock) { |
| 244 | mService = null; |
| 245 | mEngine = null; |
Amith Yamasani | 37ce3a8 | 2012-02-06 12:04:42 -0800 | [diff] [blame] | 246 | if (mWallpaper.connection == this) { |
| 247 | Slog.w(TAG, "Wallpaper service gone: " + mWallpaper.wallpaperComponent); |
| 248 | if (!mWallpaper.wallpaperUpdating |
| 249 | && (mWallpaper.lastDiedTime + MIN_WALLPAPER_CRASH_TIME) |
| 250 | > SystemClock.uptimeMillis() |
| 251 | && mWallpaper.userId == mCurrentUserId) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 252 | Slog.w(TAG, "Reverting to built-in wallpaper!"); |
Dianne Hackborn | 5dc5a00 | 2012-09-15 19:33:48 -0700 | [diff] [blame] | 253 | clearWallpaperLocked(true, mWallpaper.userId, null); |
Dianne Hackborn | 0cd4887 | 2009-08-13 18:51:59 -0700 | [diff] [blame] | 254 | } |
| 255 | } |
Dianne Hackborn | 4c62fc0 | 2009-08-08 20:40:27 -0700 | [diff] [blame] | 256 | } |
| 257 | } |
Amith Yamasani | 37ce3a8 | 2012-02-06 12:04:42 -0800 | [diff] [blame] | 258 | |
Dianne Hackborn | 5dc5a00 | 2012-09-15 19:33:48 -0700 | [diff] [blame] | 259 | @Override |
Dianne Hackborn | 4c62fc0 | 2009-08-08 20:40:27 -0700 | [diff] [blame] | 260 | public void attachEngine(IWallpaperEngine engine) { |
Dianne Hackborn | 5dc5a00 | 2012-09-15 19:33:48 -0700 | [diff] [blame] | 261 | synchronized (mLock) { |
| 262 | mEngine = engine; |
| 263 | } |
Dianne Hackborn | 4c62fc0 | 2009-08-08 20:40:27 -0700 | [diff] [blame] | 264 | } |
Amith Yamasani | 37ce3a8 | 2012-02-06 12:04:42 -0800 | [diff] [blame] | 265 | |
Dianne Hackborn | 5dc5a00 | 2012-09-15 19:33:48 -0700 | [diff] [blame] | 266 | @Override |
| 267 | public void engineShown(IWallpaperEngine engine) { |
| 268 | synchronized (mLock) { |
| 269 | if (mReply != null) { |
| 270 | long ident = Binder.clearCallingIdentity(); |
| 271 | try { |
| 272 | mReply.sendResult(null); |
| 273 | } catch (RemoteException e) { |
| 274 | Binder.restoreCallingIdentity(ident); |
| 275 | } |
| 276 | mReply = null; |
| 277 | } |
| 278 | } |
| 279 | } |
| 280 | |
| 281 | @Override |
Dianne Hackborn | 4c62fc0 | 2009-08-08 20:40:27 -0700 | [diff] [blame] | 282 | public ParcelFileDescriptor setWallpaper(String name) { |
| 283 | synchronized (mLock) { |
Amith Yamasani | 37ce3a8 | 2012-02-06 12:04:42 -0800 | [diff] [blame] | 284 | if (mWallpaper.connection == this) { |
| 285 | return updateWallpaperBitmapLocked(name, mWallpaper); |
Dianne Hackborn | 4c62fc0 | 2009-08-08 20:40:27 -0700 | [diff] [blame] | 286 | } |
| 287 | return null; |
| 288 | } |
| 289 | } |
| 290 | } |
Amith Yamasani | 37ce3a8 | 2012-02-06 12:04:42 -0800 | [diff] [blame] | 291 | |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 292 | class MyPackageMonitor extends PackageMonitor { |
| 293 | @Override |
| 294 | public void onPackageUpdateFinished(String packageName, int uid) { |
| 295 | synchronized (mLock) { |
Dianne Hackborn | c72fc67 | 2012-09-20 13:12:03 -0700 | [diff] [blame] | 296 | if (mCurrentUserId != getChangingUserId()) { |
| 297 | return; |
| 298 | } |
| 299 | WallpaperData wallpaper = mWallpaperMap.get(mCurrentUserId); |
| 300 | if (wallpaper != null) { |
Amith Yamasani | 37ce3a8 | 2012-02-06 12:04:42 -0800 | [diff] [blame] | 301 | if (wallpaper.wallpaperComponent != null |
| 302 | && wallpaper.wallpaperComponent.getPackageName().equals(packageName)) { |
| 303 | wallpaper.wallpaperUpdating = false; |
| 304 | ComponentName comp = wallpaper.wallpaperComponent; |
| 305 | clearWallpaperComponentLocked(wallpaper); |
Dianne Hackborn | c72fc67 | 2012-09-20 13:12:03 -0700 | [diff] [blame] | 306 | if (!bindWallpaperComponentLocked(comp, false, false, |
| 307 | wallpaper, null)) { |
Amith Yamasani | 37ce3a8 | 2012-02-06 12:04:42 -0800 | [diff] [blame] | 308 | Slog.w(TAG, "Wallpaper no longer available; reverting to default"); |
Dianne Hackborn | 5dc5a00 | 2012-09-15 19:33:48 -0700 | [diff] [blame] | 309 | clearWallpaperLocked(false, wallpaper.userId, null); |
Amith Yamasani | 37ce3a8 | 2012-02-06 12:04:42 -0800 | [diff] [blame] | 310 | } |
Dianne Hackborn | 80b902f | 2011-09-15 15:15:27 -0700 | [diff] [blame] | 311 | } |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 312 | } |
| 313 | } |
| 314 | } |
| 315 | |
| 316 | @Override |
Dianne Hackborn | 80b902f | 2011-09-15 15:15:27 -0700 | [diff] [blame] | 317 | public void onPackageModified(String packageName) { |
| 318 | synchronized (mLock) { |
Dianne Hackborn | c72fc67 | 2012-09-20 13:12:03 -0700 | [diff] [blame] | 319 | if (mCurrentUserId != getChangingUserId()) { |
| 320 | return; |
| 321 | } |
| 322 | WallpaperData wallpaper = mWallpaperMap.get(mCurrentUserId); |
| 323 | if (wallpaper != null) { |
Amith Yamasani | 37ce3a8 | 2012-02-06 12:04:42 -0800 | [diff] [blame] | 324 | if (wallpaper.wallpaperComponent == null |
| 325 | || !wallpaper.wallpaperComponent.getPackageName().equals(packageName)) { |
Dianne Hackborn | c72fc67 | 2012-09-20 13:12:03 -0700 | [diff] [blame] | 326 | return; |
Amith Yamasani | 37ce3a8 | 2012-02-06 12:04:42 -0800 | [diff] [blame] | 327 | } |
Dianne Hackborn | d0d7503 | 2012-04-19 23:12:09 -0700 | [diff] [blame] | 328 | doPackagesChangedLocked(true, wallpaper); |
Dianne Hackborn | 80b902f | 2011-09-15 15:15:27 -0700 | [diff] [blame] | 329 | } |
| 330 | } |
Dianne Hackborn | 80b902f | 2011-09-15 15:15:27 -0700 | [diff] [blame] | 331 | } |
| 332 | |
| 333 | @Override |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 334 | public void onPackageUpdateStarted(String packageName, int uid) { |
| 335 | synchronized (mLock) { |
Dianne Hackborn | c72fc67 | 2012-09-20 13:12:03 -0700 | [diff] [blame] | 336 | if (mCurrentUserId != getChangingUserId()) { |
| 337 | return; |
| 338 | } |
| 339 | WallpaperData wallpaper = mWallpaperMap.get(mCurrentUserId); |
| 340 | if (wallpaper != null) { |
Amith Yamasani | 37ce3a8 | 2012-02-06 12:04:42 -0800 | [diff] [blame] | 341 | if (wallpaper.wallpaperComponent != null |
| 342 | && wallpaper.wallpaperComponent.getPackageName().equals(packageName)) { |
| 343 | wallpaper.wallpaperUpdating = true; |
| 344 | } |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 345 | } |
| 346 | } |
| 347 | } |
| 348 | |
| 349 | @Override |
| 350 | public boolean onHandleForceStop(Intent intent, String[] packages, int uid, boolean doit) { |
Dianne Hackborn | d0d7503 | 2012-04-19 23:12:09 -0700 | [diff] [blame] | 351 | synchronized (mLock) { |
| 352 | boolean changed = false; |
Dianne Hackborn | c72fc67 | 2012-09-20 13:12:03 -0700 | [diff] [blame] | 353 | if (mCurrentUserId != getChangingUserId()) { |
| 354 | return false; |
| 355 | } |
| 356 | WallpaperData wallpaper = mWallpaperMap.get(mCurrentUserId); |
| 357 | if (wallpaper != null) { |
Dianne Hackborn | d0d7503 | 2012-04-19 23:12:09 -0700 | [diff] [blame] | 358 | boolean res = doPackagesChangedLocked(doit, wallpaper); |
| 359 | changed |= res; |
| 360 | } |
| 361 | return changed; |
Amith Yamasani | 37ce3a8 | 2012-02-06 12:04:42 -0800 | [diff] [blame] | 362 | } |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 363 | } |
| 364 | |
| 365 | @Override |
| 366 | public void onSomePackagesChanged() { |
Dianne Hackborn | d0d7503 | 2012-04-19 23:12:09 -0700 | [diff] [blame] | 367 | synchronized (mLock) { |
Dianne Hackborn | c72fc67 | 2012-09-20 13:12:03 -0700 | [diff] [blame] | 368 | if (mCurrentUserId != getChangingUserId()) { |
| 369 | return; |
| 370 | } |
| 371 | WallpaperData wallpaper = mWallpaperMap.get(mCurrentUserId); |
| 372 | if (wallpaper != null) { |
Dianne Hackborn | d0d7503 | 2012-04-19 23:12:09 -0700 | [diff] [blame] | 373 | doPackagesChangedLocked(true, wallpaper); |
| 374 | } |
Amith Yamasani | 37ce3a8 | 2012-02-06 12:04:42 -0800 | [diff] [blame] | 375 | } |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 376 | } |
Amith Yamasani | 37ce3a8 | 2012-02-06 12:04:42 -0800 | [diff] [blame] | 377 | |
Dianne Hackborn | d0d7503 | 2012-04-19 23:12:09 -0700 | [diff] [blame] | 378 | boolean doPackagesChangedLocked(boolean doit, WallpaperData wallpaper) { |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 379 | boolean changed = false; |
Dianne Hackborn | d0d7503 | 2012-04-19 23:12:09 -0700 | [diff] [blame] | 380 | if (wallpaper.wallpaperComponent != null) { |
| 381 | int change = isPackageDisappearing(wallpaper.wallpaperComponent |
| 382 | .getPackageName()); |
| 383 | if (change == PACKAGE_PERMANENT_CHANGE |
| 384 | || change == PACKAGE_TEMPORARY_CHANGE) { |
| 385 | changed = true; |
| 386 | if (doit) { |
| 387 | Slog.w(TAG, "Wallpaper uninstalled, removing: " |
Amith Yamasani | 37ce3a8 | 2012-02-06 12:04:42 -0800 | [diff] [blame] | 388 | + wallpaper.wallpaperComponent); |
Dianne Hackborn | 5dc5a00 | 2012-09-15 19:33:48 -0700 | [diff] [blame] | 389 | clearWallpaperLocked(false, wallpaper.userId, null); |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 390 | } |
| 391 | } |
Dianne Hackborn | d0d7503 | 2012-04-19 23:12:09 -0700 | [diff] [blame] | 392 | } |
| 393 | if (wallpaper.nextWallpaperComponent != null) { |
| 394 | int change = isPackageDisappearing(wallpaper.nextWallpaperComponent |
| 395 | .getPackageName()); |
| 396 | if (change == PACKAGE_PERMANENT_CHANGE |
| 397 | || change == PACKAGE_TEMPORARY_CHANGE) { |
| 398 | wallpaper.nextWallpaperComponent = null; |
| 399 | } |
| 400 | } |
| 401 | if (wallpaper.wallpaperComponent != null |
| 402 | && isPackageModified(wallpaper.wallpaperComponent.getPackageName())) { |
| 403 | try { |
| 404 | mContext.getPackageManager().getServiceInfo( |
| 405 | wallpaper.wallpaperComponent, 0); |
| 406 | } catch (NameNotFoundException e) { |
| 407 | Slog.w(TAG, "Wallpaper component gone, removing: " |
| 408 | + wallpaper.wallpaperComponent); |
Dianne Hackborn | 5dc5a00 | 2012-09-15 19:33:48 -0700 | [diff] [blame] | 409 | clearWallpaperLocked(false, wallpaper.userId, null); |
Dianne Hackborn | d0d7503 | 2012-04-19 23:12:09 -0700 | [diff] [blame] | 410 | } |
| 411 | } |
| 412 | if (wallpaper.nextWallpaperComponent != null |
| 413 | && isPackageModified(wallpaper.nextWallpaperComponent.getPackageName())) { |
| 414 | try { |
| 415 | mContext.getPackageManager().getServiceInfo( |
| 416 | wallpaper.nextWallpaperComponent, 0); |
| 417 | } catch (NameNotFoundException e) { |
| 418 | wallpaper.nextWallpaperComponent = null; |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 419 | } |
| 420 | } |
| 421 | return changed; |
| 422 | } |
| 423 | } |
| 424 | |
Dianne Hackborn | 8cc6a50 | 2009-08-05 21:29:42 -0700 | [diff] [blame] | 425 | public WallpaperManagerService(Context context) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 426 | if (DEBUG) Slog.v(TAG, "WallpaperService startup"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 427 | mContext = context; |
Dianne Hackborn | 4c62fc0 | 2009-08-08 20:40:27 -0700 | [diff] [blame] | 428 | mIWindowManager = IWindowManager.Stub.asInterface( |
| 429 | ServiceManager.getService(Context.WINDOW_SERVICE)); |
Amith Yamasani | 4e2820c | 2012-08-28 22:17:23 -0700 | [diff] [blame] | 430 | mIPackageManager = AppGlobals.getPackageManager(); |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 431 | mMonitor = new MyPackageMonitor(); |
Dianne Hackborn | c72fc67 | 2012-09-20 13:12:03 -0700 | [diff] [blame] | 432 | mMonitor.register(context, null, UserHandle.ALL, true); |
Amith Yamasani | 61f5737 | 2012-08-31 12:12:28 -0700 | [diff] [blame] | 433 | getWallpaperDir(UserHandle.USER_OWNER).mkdirs(); |
| 434 | loadSettingsLocked(UserHandle.USER_OWNER); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 435 | } |
| 436 | |
Amith Yamasani | 37ce3a8 | 2012-02-06 12:04:42 -0800 | [diff] [blame] | 437 | private static File getWallpaperDir(int userId) { |
Amith Yamasani | 61f5737 | 2012-08-31 12:12:28 -0700 | [diff] [blame] | 438 | return Environment.getUserSystemDirectory(userId); |
Amith Yamasani | 37ce3a8 | 2012-02-06 12:04:42 -0800 | [diff] [blame] | 439 | } |
| 440 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 441 | @Override |
| 442 | protected void finalize() throws Throwable { |
| 443 | super.finalize(); |
Amith Yamasani | 37ce3a8 | 2012-02-06 12:04:42 -0800 | [diff] [blame] | 444 | for (int i = 0; i < mWallpaperMap.size(); i++) { |
| 445 | WallpaperData wallpaper = mWallpaperMap.valueAt(i); |
| 446 | wallpaper.wallpaperObserver.stopWatching(); |
| 447 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 448 | } |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 449 | |
Dianne Hackborn | f21adf6 | 2009-08-13 10:20:21 -0700 | [diff] [blame] | 450 | public void systemReady() { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 451 | if (DEBUG) Slog.v(TAG, "systemReady"); |
Amith Yamasani | 61f5737 | 2012-08-31 12:12:28 -0700 | [diff] [blame] | 452 | WallpaperData wallpaper = mWallpaperMap.get(UserHandle.USER_OWNER); |
Dianne Hackborn | 5dc5a00 | 2012-09-15 19:33:48 -0700 | [diff] [blame] | 453 | switchWallpaper(wallpaper, null); |
Amith Yamasani | 37ce3a8 | 2012-02-06 12:04:42 -0800 | [diff] [blame] | 454 | wallpaper.wallpaperObserver = new WallpaperObserver(wallpaper); |
| 455 | wallpaper.wallpaperObserver.startWatching(); |
Amith Yamasani | 37ce3a8 | 2012-02-06 12:04:42 -0800 | [diff] [blame] | 456 | |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 457 | IntentFilter userFilter = new IntentFilter(); |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 458 | userFilter.addAction(Intent.ACTION_USER_REMOVED); |
| 459 | mContext.registerReceiver(new BroadcastReceiver() { |
Amith Yamasani | 37ce3a8 | 2012-02-06 12:04:42 -0800 | [diff] [blame] | 460 | @Override |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 461 | public void onReceive(Context context, Intent intent) { |
| 462 | String action = intent.getAction(); |
Dianne Hackborn | 5dc5a00 | 2012-09-15 19:33:48 -0700 | [diff] [blame] | 463 | if (Intent.ACTION_USER_REMOVED.equals(action)) { |
Amith Yamasani | 2a00329 | 2012-08-14 18:25:45 -0700 | [diff] [blame] | 464 | removeUser(intent.getIntExtra(Intent.EXTRA_USER_HANDLE, 0)); |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 465 | } |
Amith Yamasani | 37ce3a8 | 2012-02-06 12:04:42 -0800 | [diff] [blame] | 466 | } |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 467 | }, userFilter); |
Dianne Hackborn | 5dc5a00 | 2012-09-15 19:33:48 -0700 | [diff] [blame] | 468 | try { |
| 469 | ActivityManagerNative.getDefault().registerUserSwitchObserver( |
| 470 | new IUserSwitchObserver.Stub() { |
| 471 | @Override |
| 472 | public void onUserSwitching(int newUserId, IRemoteCallback reply) { |
| 473 | switchUser(newUserId, reply); |
| 474 | } |
| 475 | |
| 476 | @Override |
| 477 | public void onUserSwitchComplete(int newUserId) throws RemoteException { |
| 478 | } |
| 479 | }); |
| 480 | } catch (RemoteException e) { |
| 481 | // TODO Auto-generated catch block |
| 482 | e.printStackTrace(); |
| 483 | } |
Amith Yamasani | 37ce3a8 | 2012-02-06 12:04:42 -0800 | [diff] [blame] | 484 | } |
| 485 | |
| 486 | String getName() { |
Dianne Hackborn | 5dc5a00 | 2012-09-15 19:33:48 -0700 | [diff] [blame] | 487 | synchronized (mLock) { |
| 488 | return mWallpaperMap.get(0).name; |
| 489 | } |
Amith Yamasani | 37ce3a8 | 2012-02-06 12:04:42 -0800 | [diff] [blame] | 490 | } |
| 491 | |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 492 | void removeUser(int userId) { |
| 493 | synchronized (mLock) { |
| 494 | WallpaperData wallpaper = mWallpaperMap.get(userId); |
| 495 | if (wallpaper != null) { |
| 496 | wallpaper.wallpaperObserver.stopWatching(); |
| 497 | mWallpaperMap.remove(userId); |
| 498 | } |
| 499 | File wallpaperFile = new File(getWallpaperDir(userId), WALLPAPER); |
| 500 | wallpaperFile.delete(); |
| 501 | File wallpaperInfoFile = new File(getWallpaperDir(userId), WALLPAPER_INFO); |
| 502 | wallpaperInfoFile.delete(); |
| 503 | } |
| 504 | } |
| 505 | |
Dianne Hackborn | 5dc5a00 | 2012-09-15 19:33:48 -0700 | [diff] [blame] | 506 | void switchUser(int userId, IRemoteCallback reply) { |
Amith Yamasani | 37ce3a8 | 2012-02-06 12:04:42 -0800 | [diff] [blame] | 507 | synchronized (mLock) { |
| 508 | mCurrentUserId = userId; |
| 509 | WallpaperData wallpaper = mWallpaperMap.get(userId); |
| 510 | if (wallpaper == null) { |
| 511 | wallpaper = new WallpaperData(userId); |
| 512 | mWallpaperMap.put(userId, wallpaper); |
| 513 | loadSettingsLocked(userId); |
| 514 | wallpaper.wallpaperObserver = new WallpaperObserver(wallpaper); |
| 515 | wallpaper.wallpaperObserver.startWatching(); |
| 516 | } |
Dianne Hackborn | 5dc5a00 | 2012-09-15 19:33:48 -0700 | [diff] [blame] | 517 | switchWallpaper(wallpaper, reply); |
Amith Yamasani | 37ce3a8 | 2012-02-06 12:04:42 -0800 | [diff] [blame] | 518 | } |
| 519 | } |
| 520 | |
Dianne Hackborn | 5dc5a00 | 2012-09-15 19:33:48 -0700 | [diff] [blame] | 521 | void switchWallpaper(WallpaperData wallpaper, IRemoteCallback reply) { |
Dianne Hackborn | f21adf6 | 2009-08-13 10:20:21 -0700 | [diff] [blame] | 522 | synchronized (mLock) { |
Dianne Hackborn | 80b902f | 2011-09-15 15:15:27 -0700 | [diff] [blame] | 523 | RuntimeException e = null; |
Dianne Hackborn | f21adf6 | 2009-08-13 10:20:21 -0700 | [diff] [blame] | 524 | try { |
Amith Yamasani | 37ce3a8 | 2012-02-06 12:04:42 -0800 | [diff] [blame] | 525 | ComponentName cname = wallpaper.wallpaperComponent != null ? |
| 526 | wallpaper.wallpaperComponent : wallpaper.nextWallpaperComponent; |
Dianne Hackborn | 5dc5a00 | 2012-09-15 19:33:48 -0700 | [diff] [blame] | 527 | if (bindWallpaperComponentLocked(cname, true, false, wallpaper, reply)) { |
Dianne Hackborn | 80b902f | 2011-09-15 15:15:27 -0700 | [diff] [blame] | 528 | return; |
Dianne Hackborn | f21adf6 | 2009-08-13 10:20:21 -0700 | [diff] [blame] | 529 | } |
Dianne Hackborn | 80b902f | 2011-09-15 15:15:27 -0700 | [diff] [blame] | 530 | } catch (RuntimeException e1) { |
| 531 | e = e1; |
Dianne Hackborn | f21adf6 | 2009-08-13 10:20:21 -0700 | [diff] [blame] | 532 | } |
Dianne Hackborn | 80b902f | 2011-09-15 15:15:27 -0700 | [diff] [blame] | 533 | Slog.w(TAG, "Failure starting previous wallpaper", e); |
Dianne Hackborn | 5dc5a00 | 2012-09-15 19:33:48 -0700 | [diff] [blame] | 534 | clearWallpaperLocked(false, wallpaper.userId, reply); |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 535 | } |
| 536 | } |
| 537 | |
Amith Yamasani | 37ce3a8 | 2012-02-06 12:04:42 -0800 | [diff] [blame] | 538 | public void clearWallpaper() { |
| 539 | if (DEBUG) Slog.v(TAG, "clearWallpaper"); |
| 540 | synchronized (mLock) { |
Dianne Hackborn | 5dc5a00 | 2012-09-15 19:33:48 -0700 | [diff] [blame] | 541 | clearWallpaperLocked(false, UserHandle.getCallingUserId(), null); |
Amith Yamasani | 37ce3a8 | 2012-02-06 12:04:42 -0800 | [diff] [blame] | 542 | } |
| 543 | } |
| 544 | |
Dianne Hackborn | 5dc5a00 | 2012-09-15 19:33:48 -0700 | [diff] [blame] | 545 | void clearWallpaperLocked(boolean defaultFailed, int userId, IRemoteCallback reply) { |
Amith Yamasani | 37ce3a8 | 2012-02-06 12:04:42 -0800 | [diff] [blame] | 546 | WallpaperData wallpaper = mWallpaperMap.get(userId); |
| 547 | File f = new File(getWallpaperDir(userId), WALLPAPER); |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 548 | if (f.exists()) { |
| 549 | f.delete(); |
| 550 | } |
| 551 | final long ident = Binder.clearCallingIdentity(); |
Dianne Hackborn | 80b902f | 2011-09-15 15:15:27 -0700 | [diff] [blame] | 552 | RuntimeException e = null; |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 553 | try { |
Amith Yamasani | 37ce3a8 | 2012-02-06 12:04:42 -0800 | [diff] [blame] | 554 | wallpaper.imageWallpaperPending = false; |
| 555 | if (userId != mCurrentUserId) return; |
Dianne Hackborn | 80b902f | 2011-09-15 15:15:27 -0700 | [diff] [blame] | 556 | if (bindWallpaperComponentLocked(defaultFailed |
Amith Yamasani | 37ce3a8 | 2012-02-06 12:04:42 -0800 | [diff] [blame] | 557 | ? wallpaper.imageWallpaperComponent |
Dianne Hackborn | 5dc5a00 | 2012-09-15 19:33:48 -0700 | [diff] [blame] | 558 | : null, true, false, wallpaper, reply)) { |
Dianne Hackborn | 80b902f | 2011-09-15 15:15:27 -0700 | [diff] [blame] | 559 | return; |
| 560 | } |
| 561 | } catch (IllegalArgumentException e1) { |
| 562 | e = e1; |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 563 | } finally { |
| 564 | Binder.restoreCallingIdentity(ident); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 565 | } |
Dianne Hackborn | 80b902f | 2011-09-15 15:15:27 -0700 | [diff] [blame] | 566 | |
| 567 | // This can happen if the default wallpaper component doesn't |
| 568 | // exist. This should be a system configuration problem, but |
| 569 | // let's not let it crash the system and just live with no |
| 570 | // wallpaper. |
| 571 | Slog.e(TAG, "Default wallpaper component not found!", e); |
Amith Yamasani | 37ce3a8 | 2012-02-06 12:04:42 -0800 | [diff] [blame] | 572 | clearWallpaperComponentLocked(wallpaper); |
Dianne Hackborn | 5dc5a00 | 2012-09-15 19:33:48 -0700 | [diff] [blame] | 573 | if (reply != null) { |
| 574 | try { |
| 575 | reply.sendResult(null); |
| 576 | } catch (RemoteException e1) { |
| 577 | } |
| 578 | } |
| 579 | } |
| 580 | |
| 581 | public boolean hasNamedWallpaper(String name) { |
| 582 | synchronized (mLock) { |
| 583 | for (int i=0; i<mWallpaperMap.size(); i++) { |
| 584 | WallpaperData wd = mWallpaperMap.valueAt(i); |
| 585 | if (name.equals(wd.name)) { |
| 586 | return true; |
| 587 | } |
| 588 | } |
| 589 | } |
| 590 | return false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 591 | } |
| 592 | |
| 593 | public void setDimensionHints(int width, int height) throws RemoteException { |
| 594 | checkPermission(android.Manifest.permission.SET_WALLPAPER_HINTS); |
Joe Onorato | 9bb8fd7 | 2009-07-28 18:24:51 -0700 | [diff] [blame] | 595 | synchronized (mLock) { |
Dianne Hackborn | 5dc5a00 | 2012-09-15 19:33:48 -0700 | [diff] [blame] | 596 | int userId = UserHandle.getCallingUserId(); |
| 597 | WallpaperData wallpaper = mWallpaperMap.get(userId); |
| 598 | if (wallpaper == null) { |
| 599 | throw new IllegalStateException("Wallpaper not yet initialized for user " + userId); |
| 600 | } |
| 601 | if (width <= 0 || height <= 0) { |
| 602 | throw new IllegalArgumentException("width and height must be > 0"); |
| 603 | } |
| 604 | |
Amith Yamasani | 37ce3a8 | 2012-02-06 12:04:42 -0800 | [diff] [blame] | 605 | if (width != wallpaper.width || height != wallpaper.height) { |
| 606 | wallpaper.width = width; |
| 607 | wallpaper.height = height; |
| 608 | saveSettingsLocked(wallpaper); |
| 609 | if (mCurrentUserId != userId) return; // Don't change the properties now |
| 610 | if (wallpaper.connection != null) { |
| 611 | if (wallpaper.connection.mEngine != null) { |
Dianne Hackborn | 284ac93 | 2009-08-28 10:34:25 -0700 | [diff] [blame] | 612 | try { |
Amith Yamasani | 37ce3a8 | 2012-02-06 12:04:42 -0800 | [diff] [blame] | 613 | wallpaper.connection.mEngine.setDesiredSize( |
Dianne Hackborn | 284ac93 | 2009-08-28 10:34:25 -0700 | [diff] [blame] | 614 | width, height); |
| 615 | } catch (RemoteException e) { |
| 616 | } |
Amith Yamasani | 37ce3a8 | 2012-02-06 12:04:42 -0800 | [diff] [blame] | 617 | notifyCallbacksLocked(wallpaper); |
Dianne Hackborn | 284ac93 | 2009-08-28 10:34:25 -0700 | [diff] [blame] | 618 | } |
| 619 | } |
Joe Onorato | 9bb8fd7 | 2009-07-28 18:24:51 -0700 | [diff] [blame] | 620 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 621 | } |
| 622 | } |
| 623 | |
| 624 | public int getWidthHint() throws RemoteException { |
Joe Onorato | 9bb8fd7 | 2009-07-28 18:24:51 -0700 | [diff] [blame] | 625 | synchronized (mLock) { |
Dianne Hackborn | f02b60a | 2012-08-16 10:48:27 -0700 | [diff] [blame] | 626 | WallpaperData wallpaper = mWallpaperMap.get(UserHandle.getCallingUserId()); |
Amith Yamasani | 37ce3a8 | 2012-02-06 12:04:42 -0800 | [diff] [blame] | 627 | return wallpaper.width; |
Joe Onorato | 9bb8fd7 | 2009-07-28 18:24:51 -0700 | [diff] [blame] | 628 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 629 | } |
| 630 | |
| 631 | public int getHeightHint() throws RemoteException { |
Joe Onorato | 9bb8fd7 | 2009-07-28 18:24:51 -0700 | [diff] [blame] | 632 | synchronized (mLock) { |
Dianne Hackborn | f02b60a | 2012-08-16 10:48:27 -0700 | [diff] [blame] | 633 | WallpaperData wallpaper = mWallpaperMap.get(UserHandle.getCallingUserId()); |
Amith Yamasani | 37ce3a8 | 2012-02-06 12:04:42 -0800 | [diff] [blame] | 634 | return wallpaper.height; |
Joe Onorato | 9bb8fd7 | 2009-07-28 18:24:51 -0700 | [diff] [blame] | 635 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 636 | } |
| 637 | |
Dianne Hackborn | 284ac93 | 2009-08-28 10:34:25 -0700 | [diff] [blame] | 638 | public ParcelFileDescriptor getWallpaper(IWallpaperManagerCallback cb, |
| 639 | Bundle outParams) { |
Joe Onorato | 9bb8fd7 | 2009-07-28 18:24:51 -0700 | [diff] [blame] | 640 | synchronized (mLock) { |
Amith Yamasani | 37ce3a8 | 2012-02-06 12:04:42 -0800 | [diff] [blame] | 641 | // This returns the current user's wallpaper, if called by a system service. Else it |
| 642 | // returns the wallpaper for the calling user. |
| 643 | int callingUid = Binder.getCallingUid(); |
| 644 | int wallpaperUserId = 0; |
| 645 | if (callingUid == android.os.Process.SYSTEM_UID) { |
| 646 | wallpaperUserId = mCurrentUserId; |
| 647 | } else { |
Dianne Hackborn | f02b60a | 2012-08-16 10:48:27 -0700 | [diff] [blame] | 648 | wallpaperUserId = UserHandle.getUserId(callingUid); |
Amith Yamasani | 37ce3a8 | 2012-02-06 12:04:42 -0800 | [diff] [blame] | 649 | } |
| 650 | WallpaperData wallpaper = mWallpaperMap.get(wallpaperUserId); |
Joe Onorato | 9bb8fd7 | 2009-07-28 18:24:51 -0700 | [diff] [blame] | 651 | try { |
Dianne Hackborn | 284ac93 | 2009-08-28 10:34:25 -0700 | [diff] [blame] | 652 | if (outParams != null) { |
Amith Yamasani | 37ce3a8 | 2012-02-06 12:04:42 -0800 | [diff] [blame] | 653 | outParams.putInt("width", wallpaper.width); |
| 654 | outParams.putInt("height", wallpaper.height); |
Dianne Hackborn | 284ac93 | 2009-08-28 10:34:25 -0700 | [diff] [blame] | 655 | } |
Amith Yamasani | 37ce3a8 | 2012-02-06 12:04:42 -0800 | [diff] [blame] | 656 | wallpaper.callbacks.register(cb); |
| 657 | File f = new File(getWallpaperDir(wallpaperUserId), WALLPAPER); |
Joe Onorato | 9bb8fd7 | 2009-07-28 18:24:51 -0700 | [diff] [blame] | 658 | if (!f.exists()) { |
| 659 | return null; |
| 660 | } |
| 661 | return ParcelFileDescriptor.open(f, MODE_READ_ONLY); |
| 662 | } catch (FileNotFoundException e) { |
| 663 | /* Shouldn't happen as we check to see if the file exists */ |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 664 | Slog.w(TAG, "Error getting wallpaper", e); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 665 | } |
Joe Onorato | 9bb8fd7 | 2009-07-28 18:24:51 -0700 | [diff] [blame] | 666 | return null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 667 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 668 | } |
| 669 | |
Dianne Hackborn | eb03465 | 2009-09-07 00:49:58 -0700 | [diff] [blame] | 670 | public WallpaperInfo getWallpaperInfo() { |
Dianne Hackborn | f02b60a | 2012-08-16 10:48:27 -0700 | [diff] [blame] | 671 | int userId = UserHandle.getCallingUserId(); |
Dianne Hackborn | eb03465 | 2009-09-07 00:49:58 -0700 | [diff] [blame] | 672 | synchronized (mLock) { |
Amith Yamasani | 37ce3a8 | 2012-02-06 12:04:42 -0800 | [diff] [blame] | 673 | WallpaperData wallpaper = mWallpaperMap.get(userId); |
| 674 | if (wallpaper.connection != null) { |
| 675 | return wallpaper.connection.mInfo; |
Dianne Hackborn | eb03465 | 2009-09-07 00:49:58 -0700 | [diff] [blame] | 676 | } |
| 677 | return null; |
| 678 | } |
| 679 | } |
Amith Yamasani | 37ce3a8 | 2012-02-06 12:04:42 -0800 | [diff] [blame] | 680 | |
Joe Onorato | 9bb8fd7 | 2009-07-28 18:24:51 -0700 | [diff] [blame] | 681 | public ParcelFileDescriptor setWallpaper(String name) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 682 | checkPermission(android.Manifest.permission.SET_WALLPAPER); |
Joe Onorato | 9bb8fd7 | 2009-07-28 18:24:51 -0700 | [diff] [blame] | 683 | synchronized (mLock) { |
Dianne Hackborn | 5dc5a00 | 2012-09-15 19:33:48 -0700 | [diff] [blame] | 684 | if (DEBUG) Slog.v(TAG, "setWallpaper"); |
| 685 | int userId = UserHandle.getCallingUserId(); |
| 686 | WallpaperData wallpaper = mWallpaperMap.get(userId); |
| 687 | if (wallpaper == null) { |
| 688 | throw new IllegalStateException("Wallpaper not yet initialized for user " + userId); |
| 689 | } |
Dianne Hackborn | 0cd4887 | 2009-08-13 18:51:59 -0700 | [diff] [blame] | 690 | final long ident = Binder.clearCallingIdentity(); |
| 691 | try { |
Amith Yamasani | 37ce3a8 | 2012-02-06 12:04:42 -0800 | [diff] [blame] | 692 | ParcelFileDescriptor pfd = updateWallpaperBitmapLocked(name, wallpaper); |
Dianne Hackborn | 0cd4887 | 2009-08-13 18:51:59 -0700 | [diff] [blame] | 693 | if (pfd != null) { |
Amith Yamasani | 37ce3a8 | 2012-02-06 12:04:42 -0800 | [diff] [blame] | 694 | wallpaper.imageWallpaperPending = true; |
Dianne Hackborn | 0cd4887 | 2009-08-13 18:51:59 -0700 | [diff] [blame] | 695 | } |
| 696 | return pfd; |
| 697 | } finally { |
| 698 | Binder.restoreCallingIdentity(ident); |
Joe Onorato | 9bb8fd7 | 2009-07-28 18:24:51 -0700 | [diff] [blame] | 699 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 700 | } |
| 701 | } |
| 702 | |
Amith Yamasani | 37ce3a8 | 2012-02-06 12:04:42 -0800 | [diff] [blame] | 703 | ParcelFileDescriptor updateWallpaperBitmapLocked(String name, WallpaperData wallpaper) { |
Dianne Hackborn | 4c62fc0 | 2009-08-08 20:40:27 -0700 | [diff] [blame] | 704 | if (name == null) name = ""; |
| 705 | try { |
Amith Yamasani | 37ce3a8 | 2012-02-06 12:04:42 -0800 | [diff] [blame] | 706 | File dir = getWallpaperDir(wallpaper.userId); |
| 707 | if (!dir.exists()) { |
| 708 | dir.mkdir(); |
Dianne Hackborn | ebac48c | 2011-11-29 18:01:50 -0800 | [diff] [blame] | 709 | FileUtils.setPermissions( |
Amith Yamasani | 37ce3a8 | 2012-02-06 12:04:42 -0800 | [diff] [blame] | 710 | dir.getPath(), |
Dianne Hackborn | ebac48c | 2011-11-29 18:01:50 -0800 | [diff] [blame] | 711 | FileUtils.S_IRWXU|FileUtils.S_IRWXG|FileUtils.S_IXOTH, |
| 712 | -1, -1); |
| 713 | } |
rpcraig | 554cb0c | 2012-07-05 06:41:43 -0400 | [diff] [blame] | 714 | File file = new File(dir, WALLPAPER); |
| 715 | ParcelFileDescriptor fd = ParcelFileDescriptor.open(file, |
Dianne Hackborn | 4c62fc0 | 2009-08-08 20:40:27 -0700 | [diff] [blame] | 716 | MODE_CREATE|MODE_READ_WRITE); |
rpcraig | 554cb0c | 2012-07-05 06:41:43 -0400 | [diff] [blame] | 717 | if (!SELinux.restorecon(file)) { |
| 718 | return null; |
| 719 | } |
Amith Yamasani | 37ce3a8 | 2012-02-06 12:04:42 -0800 | [diff] [blame] | 720 | wallpaper.name = name; |
Dianne Hackborn | 4c62fc0 | 2009-08-08 20:40:27 -0700 | [diff] [blame] | 721 | return fd; |
| 722 | } catch (FileNotFoundException e) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 723 | Slog.w(TAG, "Error setting wallpaper", e); |
Dianne Hackborn | 4c62fc0 | 2009-08-08 20:40:27 -0700 | [diff] [blame] | 724 | } |
| 725 | return null; |
| 726 | } |
| 727 | |
| 728 | public void setWallpaperComponent(ComponentName name) { |
| 729 | checkPermission(android.Manifest.permission.SET_WALLPAPER_COMPONENT); |
| 730 | synchronized (mLock) { |
Dianne Hackborn | 5dc5a00 | 2012-09-15 19:33:48 -0700 | [diff] [blame] | 731 | if (DEBUG) Slog.v(TAG, "setWallpaperComponent name=" + name); |
| 732 | int userId = UserHandle.getCallingUserId(); |
| 733 | WallpaperData wallpaper = mWallpaperMap.get(userId); |
| 734 | if (wallpaper == null) { |
| 735 | throw new IllegalStateException("Wallpaper not yet initialized for user " + userId); |
| 736 | } |
Dianne Hackborn | 4c62fc0 | 2009-08-08 20:40:27 -0700 | [diff] [blame] | 737 | final long ident = Binder.clearCallingIdentity(); |
| 738 | try { |
Amith Yamasani | 37ce3a8 | 2012-02-06 12:04:42 -0800 | [diff] [blame] | 739 | wallpaper.imageWallpaperPending = false; |
Dianne Hackborn | 5dc5a00 | 2012-09-15 19:33:48 -0700 | [diff] [blame] | 740 | bindWallpaperComponentLocked(name, false, true, wallpaper, null); |
Dianne Hackborn | f21adf6 | 2009-08-13 10:20:21 -0700 | [diff] [blame] | 741 | } finally { |
| 742 | Binder.restoreCallingIdentity(ident); |
| 743 | } |
| 744 | } |
| 745 | } |
| 746 | |
Amith Yamasani | 37ce3a8 | 2012-02-06 12:04:42 -0800 | [diff] [blame] | 747 | boolean bindWallpaperComponentLocked(ComponentName componentName, boolean force, |
Dianne Hackborn | 5dc5a00 | 2012-09-15 19:33:48 -0700 | [diff] [blame] | 748 | boolean fromUser, WallpaperData wallpaper, IRemoteCallback reply) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 749 | if (DEBUG) Slog.v(TAG, "bindWallpaperComponentLocked: componentName=" + componentName); |
Dianne Hackborn | f21adf6 | 2009-08-13 10:20:21 -0700 | [diff] [blame] | 750 | // Has the component changed? |
Dianne Hackborn | 9ea3163 | 2011-08-05 14:43:50 -0700 | [diff] [blame] | 751 | if (!force) { |
Amith Yamasani | 37ce3a8 | 2012-02-06 12:04:42 -0800 | [diff] [blame] | 752 | if (wallpaper.connection != null) { |
| 753 | if (wallpaper.wallpaperComponent == null) { |
Dianne Hackborn | 9ea3163 | 2011-08-05 14:43:50 -0700 | [diff] [blame] | 754 | if (componentName == null) { |
| 755 | if (DEBUG) Slog.v(TAG, "bindWallpaperComponentLocked: still using default"); |
| 756 | // Still using default wallpaper. |
Dianne Hackborn | 80b902f | 2011-09-15 15:15:27 -0700 | [diff] [blame] | 757 | return true; |
Dianne Hackborn | 9ea3163 | 2011-08-05 14:43:50 -0700 | [diff] [blame] | 758 | } |
Amith Yamasani | 37ce3a8 | 2012-02-06 12:04:42 -0800 | [diff] [blame] | 759 | } else if (wallpaper.wallpaperComponent.equals(componentName)) { |
Dianne Hackborn | 9ea3163 | 2011-08-05 14:43:50 -0700 | [diff] [blame] | 760 | // Changing to same wallpaper. |
| 761 | if (DEBUG) Slog.v(TAG, "same wallpaper"); |
Dianne Hackborn | 80b902f | 2011-09-15 15:15:27 -0700 | [diff] [blame] | 762 | return true; |
Dianne Hackborn | 4c62fc0 | 2009-08-08 20:40:27 -0700 | [diff] [blame] | 763 | } |
Dianne Hackborn | f21adf6 | 2009-08-13 10:20:21 -0700 | [diff] [blame] | 764 | } |
| 765 | } |
| 766 | |
| 767 | try { |
Mike Cleron | a428b2c | 2009-11-15 22:53:08 -0800 | [diff] [blame] | 768 | if (componentName == null) { |
Mike Cleron | 322b6ee | 2009-11-12 07:45:47 -0800 | [diff] [blame] | 769 | String defaultComponent = |
| 770 | mContext.getString(com.android.internal.R.string.default_wallpaper_component); |
Mike Cleron | a428b2c | 2009-11-15 22:53:08 -0800 | [diff] [blame] | 771 | if (defaultComponent != null) { |
Mike Cleron | 322b6ee | 2009-11-12 07:45:47 -0800 | [diff] [blame] | 772 | // See if there is a default wallpaper component specified |
Mike Cleron | a428b2c | 2009-11-15 22:53:08 -0800 | [diff] [blame] | 773 | componentName = ComponentName.unflattenFromString(defaultComponent); |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 774 | if (DEBUG) Slog.v(TAG, "Use default component wallpaper:" + componentName); |
Mike Cleron | 322b6ee | 2009-11-12 07:45:47 -0800 | [diff] [blame] | 775 | } |
Mike Cleron | a428b2c | 2009-11-15 22:53:08 -0800 | [diff] [blame] | 776 | if (componentName == null) { |
Mike Cleron | 322b6ee | 2009-11-12 07:45:47 -0800 | [diff] [blame] | 777 | // Fall back to static image wallpaper |
Amith Yamasani | 37ce3a8 | 2012-02-06 12:04:42 -0800 | [diff] [blame] | 778 | componentName = wallpaper.imageWallpaperComponent; |
Mike Cleron | 322b6ee | 2009-11-12 07:45:47 -0800 | [diff] [blame] | 779 | //clearWallpaperComponentLocked(); |
| 780 | //return; |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 781 | if (DEBUG) Slog.v(TAG, "Using image wallpaper"); |
Mike Cleron | 322b6ee | 2009-11-12 07:45:47 -0800 | [diff] [blame] | 782 | } |
Dianne Hackborn | f21adf6 | 2009-08-13 10:20:21 -0700 | [diff] [blame] | 783 | } |
Amith Yamasani | 4e2820c | 2012-08-28 22:17:23 -0700 | [diff] [blame] | 784 | int serviceUserId = wallpaper.userId; |
| 785 | ServiceInfo si = mIPackageManager.getServiceInfo(componentName, |
| 786 | PackageManager.GET_META_DATA | PackageManager.GET_PERMISSIONS, serviceUserId); |
Dianne Hackborn | f21adf6 | 2009-08-13 10:20:21 -0700 | [diff] [blame] | 787 | if (!android.Manifest.permission.BIND_WALLPAPER.equals(si.permission)) { |
Dianne Hackborn | 80b902f | 2011-09-15 15:15:27 -0700 | [diff] [blame] | 788 | String msg = "Selected service does not require " |
Dianne Hackborn | f21adf6 | 2009-08-13 10:20:21 -0700 | [diff] [blame] | 789 | + android.Manifest.permission.BIND_WALLPAPER |
Dianne Hackborn | 80b902f | 2011-09-15 15:15:27 -0700 | [diff] [blame] | 790 | + ": " + componentName; |
| 791 | if (fromUser) { |
| 792 | throw new SecurityException(msg); |
| 793 | } |
| 794 | Slog.w(TAG, msg); |
| 795 | return false; |
Dianne Hackborn | f21adf6 | 2009-08-13 10:20:21 -0700 | [diff] [blame] | 796 | } |
| 797 | |
Dianne Hackborn | eb03465 | 2009-09-07 00:49:58 -0700 | [diff] [blame] | 798 | WallpaperInfo wi = null; |
| 799 | |
Dianne Hackborn | f21adf6 | 2009-08-13 10:20:21 -0700 | [diff] [blame] | 800 | Intent intent = new Intent(WallpaperService.SERVICE_INTERFACE); |
Amith Yamasani | 37ce3a8 | 2012-02-06 12:04:42 -0800 | [diff] [blame] | 801 | if (componentName != null && !componentName.equals(wallpaper.imageWallpaperComponent)) { |
Dianne Hackborn | 4c62fc0 | 2009-08-08 20:40:27 -0700 | [diff] [blame] | 802 | // Make sure the selected service is actually a wallpaper service. |
Amith Yamasani | 4e2820c | 2012-08-28 22:17:23 -0700 | [diff] [blame] | 803 | List<ResolveInfo> ris = |
| 804 | mIPackageManager.queryIntentServices(intent, |
| 805 | intent.resolveTypeIfNeeded(mContext.getContentResolver()), |
| 806 | PackageManager.GET_META_DATA, serviceUserId); |
Dianne Hackborn | 4c62fc0 | 2009-08-08 20:40:27 -0700 | [diff] [blame] | 807 | for (int i=0; i<ris.size(); i++) { |
| 808 | ServiceInfo rsi = ris.get(i).serviceInfo; |
| 809 | if (rsi.name.equals(si.name) && |
| 810 | rsi.packageName.equals(si.packageName)) { |
Dianne Hackborn | eb03465 | 2009-09-07 00:49:58 -0700 | [diff] [blame] | 811 | try { |
| 812 | wi = new WallpaperInfo(mContext, ris.get(i)); |
| 813 | } catch (XmlPullParserException e) { |
Dianne Hackborn | 80b902f | 2011-09-15 15:15:27 -0700 | [diff] [blame] | 814 | if (fromUser) { |
| 815 | throw new IllegalArgumentException(e); |
| 816 | } |
| 817 | Slog.w(TAG, e); |
| 818 | return false; |
Dianne Hackborn | eb03465 | 2009-09-07 00:49:58 -0700 | [diff] [blame] | 819 | } catch (IOException e) { |
Dianne Hackborn | 80b902f | 2011-09-15 15:15:27 -0700 | [diff] [blame] | 820 | if (fromUser) { |
| 821 | throw new IllegalArgumentException(e); |
| 822 | } |
| 823 | Slog.w(TAG, e); |
| 824 | return false; |
Dianne Hackborn | eb03465 | 2009-09-07 00:49:58 -0700 | [diff] [blame] | 825 | } |
Dianne Hackborn | 4c62fc0 | 2009-08-08 20:40:27 -0700 | [diff] [blame] | 826 | break; |
| 827 | } |
| 828 | } |
Dianne Hackborn | eb03465 | 2009-09-07 00:49:58 -0700 | [diff] [blame] | 829 | if (wi == null) { |
Dianne Hackborn | 80b902f | 2011-09-15 15:15:27 -0700 | [diff] [blame] | 830 | String msg = "Selected service is not a wallpaper: " |
| 831 | + componentName; |
| 832 | if (fromUser) { |
| 833 | throw new SecurityException(msg); |
| 834 | } |
| 835 | Slog.w(TAG, msg); |
| 836 | return false; |
Dianne Hackborn | 4c62fc0 | 2009-08-08 20:40:27 -0700 | [diff] [blame] | 837 | } |
Dianne Hackborn | 4c62fc0 | 2009-08-08 20:40:27 -0700 | [diff] [blame] | 838 | } |
Dianne Hackborn | f21adf6 | 2009-08-13 10:20:21 -0700 | [diff] [blame] | 839 | |
| 840 | // Bind the service! |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 841 | if (DEBUG) Slog.v(TAG, "Binding to:" + componentName); |
Amith Yamasani | 37ce3a8 | 2012-02-06 12:04:42 -0800 | [diff] [blame] | 842 | WallpaperConnection newConn = new WallpaperConnection(wi, wallpaper); |
Mike Cleron | a428b2c | 2009-11-15 22:53:08 -0800 | [diff] [blame] | 843 | intent.setComponent(componentName); |
Dianne Hackborn | dd9b82c | 2009-09-03 00:18:47 -0700 | [diff] [blame] | 844 | intent.putExtra(Intent.EXTRA_CLIENT_LABEL, |
| 845 | com.android.internal.R.string.wallpaper_binding_label); |
Dianne Hackborn | 4120375 | 2012-08-31 14:05:51 -0700 | [diff] [blame] | 846 | intent.putExtra(Intent.EXTRA_CLIENT_INTENT, PendingIntent.getActivityAsUser( |
Dianne Hackborn | eb03465 | 2009-09-07 00:49:58 -0700 | [diff] [blame] | 847 | mContext, 0, |
| 848 | Intent.createChooser(new Intent(Intent.ACTION_SET_WALLPAPER), |
| 849 | mContext.getText(com.android.internal.R.string.chooser_wallpaper)), |
Dianne Hackborn | 4120375 | 2012-08-31 14:05:51 -0700 | [diff] [blame] | 850 | 0, null, new UserHandle(serviceUserId))); |
Amith Yamasani | 37ce3a8 | 2012-02-06 12:04:42 -0800 | [diff] [blame] | 851 | if (!mContext.bindService(intent, newConn, Context.BIND_AUTO_CREATE, serviceUserId)) { |
Dianne Hackborn | 80b902f | 2011-09-15 15:15:27 -0700 | [diff] [blame] | 852 | String msg = "Unable to bind service: " |
| 853 | + componentName; |
| 854 | if (fromUser) { |
| 855 | throw new IllegalArgumentException(msg); |
| 856 | } |
| 857 | Slog.w(TAG, msg); |
| 858 | return false; |
Dianne Hackborn | f21adf6 | 2009-08-13 10:20:21 -0700 | [diff] [blame] | 859 | } |
Amith Yamasani | 37ce3a8 | 2012-02-06 12:04:42 -0800 | [diff] [blame] | 860 | if (wallpaper.userId == mCurrentUserId && mLastWallpaper != null) { |
| 861 | detachWallpaperLocked(mLastWallpaper); |
| 862 | } |
| 863 | wallpaper.wallpaperComponent = componentName; |
| 864 | wallpaper.connection = newConn; |
| 865 | wallpaper.lastDiedTime = SystemClock.uptimeMillis(); |
Dianne Hackborn | 5dc5a00 | 2012-09-15 19:33:48 -0700 | [diff] [blame] | 866 | newConn.mReply = reply; |
Dianne Hackborn | f21adf6 | 2009-08-13 10:20:21 -0700 | [diff] [blame] | 867 | try { |
Amith Yamasani | 37ce3a8 | 2012-02-06 12:04:42 -0800 | [diff] [blame] | 868 | if (wallpaper.userId == mCurrentUserId) { |
| 869 | if (DEBUG) |
| 870 | Slog.v(TAG, "Adding window token: " + newConn.mToken); |
| 871 | mIWindowManager.addWindowToken(newConn.mToken, |
| 872 | WindowManager.LayoutParams.TYPE_WALLPAPER); |
| 873 | mLastWallpaper = wallpaper; |
| 874 | } |
Dianne Hackborn | f21adf6 | 2009-08-13 10:20:21 -0700 | [diff] [blame] | 875 | } catch (RemoteException e) { |
| 876 | } |
Amith Yamasani | 4e2820c | 2012-08-28 22:17:23 -0700 | [diff] [blame] | 877 | } catch (RemoteException e) { |
| 878 | String msg = "Remote exception for " + componentName + "\n" + e; |
Dianne Hackborn | 80b902f | 2011-09-15 15:15:27 -0700 | [diff] [blame] | 879 | if (fromUser) { |
| 880 | throw new IllegalArgumentException(msg); |
| 881 | } |
| 882 | Slog.w(TAG, msg); |
| 883 | return false; |
Dianne Hackborn | 4c62fc0 | 2009-08-08 20:40:27 -0700 | [diff] [blame] | 884 | } |
Dianne Hackborn | 80b902f | 2011-09-15 15:15:27 -0700 | [diff] [blame] | 885 | return true; |
Dianne Hackborn | 4c62fc0 | 2009-08-08 20:40:27 -0700 | [diff] [blame] | 886 | } |
Amith Yamasani | 37ce3a8 | 2012-02-06 12:04:42 -0800 | [diff] [blame] | 887 | |
| 888 | void detachWallpaperLocked(WallpaperData wallpaper) { |
| 889 | if (wallpaper.connection != null) { |
Dianne Hackborn | 5dc5a00 | 2012-09-15 19:33:48 -0700 | [diff] [blame] | 890 | if (wallpaper.connection.mReply != null) { |
| 891 | try { |
| 892 | wallpaper.connection.mReply.sendResult(null); |
| 893 | } catch (RemoteException e) { |
| 894 | } |
| 895 | wallpaper.connection.mReply = null; |
| 896 | } |
Amith Yamasani | 37ce3a8 | 2012-02-06 12:04:42 -0800 | [diff] [blame] | 897 | if (wallpaper.connection.mEngine != null) { |
Dianne Hackborn | 4c62fc0 | 2009-08-08 20:40:27 -0700 | [diff] [blame] | 898 | try { |
Amith Yamasani | 37ce3a8 | 2012-02-06 12:04:42 -0800 | [diff] [blame] | 899 | wallpaper.connection.mEngine.destroy(); |
Dianne Hackborn | 4c62fc0 | 2009-08-08 20:40:27 -0700 | [diff] [blame] | 900 | } catch (RemoteException e) { |
| 901 | } |
| 902 | } |
Amith Yamasani | 37ce3a8 | 2012-02-06 12:04:42 -0800 | [diff] [blame] | 903 | mContext.unbindService(wallpaper.connection); |
Dianne Hackborn | e9e9bca | 2009-08-18 15:08:22 -0700 | [diff] [blame] | 904 | try { |
Amith Yamasani | 37ce3a8 | 2012-02-06 12:04:42 -0800 | [diff] [blame] | 905 | if (DEBUG) |
| 906 | Slog.v(TAG, "Removing window token: " + wallpaper.connection.mToken); |
| 907 | mIWindowManager.removeWindowToken(wallpaper.connection.mToken); |
Dianne Hackborn | e9e9bca | 2009-08-18 15:08:22 -0700 | [diff] [blame] | 908 | } catch (RemoteException e) { |
| 909 | } |
Amith Yamasani | 37ce3a8 | 2012-02-06 12:04:42 -0800 | [diff] [blame] | 910 | wallpaper.connection.mService = null; |
| 911 | wallpaper.connection.mEngine = null; |
| 912 | wallpaper.connection = null; |
Dianne Hackborn | 4c62fc0 | 2009-08-08 20:40:27 -0700 | [diff] [blame] | 913 | } |
| 914 | } |
Amith Yamasani | 37ce3a8 | 2012-02-06 12:04:42 -0800 | [diff] [blame] | 915 | |
| 916 | void clearWallpaperComponentLocked(WallpaperData wallpaper) { |
| 917 | wallpaper.wallpaperComponent = null; |
| 918 | detachWallpaperLocked(wallpaper); |
| 919 | } |
| 920 | |
| 921 | void attachServiceLocked(WallpaperConnection conn, WallpaperData wallpaper) { |
Dianne Hackborn | 4c62fc0 | 2009-08-08 20:40:27 -0700 | [diff] [blame] | 922 | try { |
Dianne Hackborn | 3be63c0 | 2009-08-20 19:31:38 -0700 | [diff] [blame] | 923 | conn.mService.attach(conn, conn.mToken, |
| 924 | WindowManager.LayoutParams.TYPE_WALLPAPER, false, |
Amith Yamasani | 37ce3a8 | 2012-02-06 12:04:42 -0800 | [diff] [blame] | 925 | wallpaper.width, wallpaper.height); |
Dianne Hackborn | 4c62fc0 | 2009-08-08 20:40:27 -0700 | [diff] [blame] | 926 | } catch (RemoteException e) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 927 | Slog.w(TAG, "Failed attaching wallpaper; clearing", e); |
Amith Yamasani | 37ce3a8 | 2012-02-06 12:04:42 -0800 | [diff] [blame] | 928 | if (!wallpaper.wallpaperUpdating) { |
Dianne Hackborn | 5dc5a00 | 2012-09-15 19:33:48 -0700 | [diff] [blame] | 929 | bindWallpaperComponentLocked(null, false, false, wallpaper, null); |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 930 | } |
Dianne Hackborn | 4c62fc0 | 2009-08-08 20:40:27 -0700 | [diff] [blame] | 931 | } |
| 932 | } |
Amith Yamasani | 37ce3a8 | 2012-02-06 12:04:42 -0800 | [diff] [blame] | 933 | |
| 934 | private void notifyCallbacksLocked(WallpaperData wallpaper) { |
| 935 | final int n = wallpaper.callbacks.beginBroadcast(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 936 | for (int i = 0; i < n; i++) { |
| 937 | try { |
Amith Yamasani | 37ce3a8 | 2012-02-06 12:04:42 -0800 | [diff] [blame] | 938 | wallpaper.callbacks.getBroadcastItem(i).onWallpaperChanged(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 939 | } catch (RemoteException e) { |
| 940 | |
| 941 | // The RemoteCallbackList will take care of removing |
| 942 | // the dead object for us. |
| 943 | } |
| 944 | } |
Amith Yamasani | 37ce3a8 | 2012-02-06 12:04:42 -0800 | [diff] [blame] | 945 | wallpaper.callbacks.finishBroadcast(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 946 | final Intent intent = new Intent(Intent.ACTION_WALLPAPER_CHANGED); |
Dianne Hackborn | 5ac72a2 | 2012-08-29 18:32:08 -0700 | [diff] [blame] | 947 | mContext.sendBroadcastAsUser(intent, new UserHandle(mCurrentUserId)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 948 | } |
| 949 | |
| 950 | private void checkPermission(String permission) { |
Joe Onorato | 9bb8fd7 | 2009-07-28 18:24:51 -0700 | [diff] [blame] | 951 | if (PackageManager.PERMISSION_GRANTED!= mContext.checkCallingOrSelfPermission(permission)) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 952 | throw new SecurityException("Access denied to process: " + Binder.getCallingPid() |
| 953 | + ", must have permission " + permission); |
| 954 | } |
| 955 | } |
Joe Onorato | 9bb8fd7 | 2009-07-28 18:24:51 -0700 | [diff] [blame] | 956 | |
Amith Yamasani | 37ce3a8 | 2012-02-06 12:04:42 -0800 | [diff] [blame] | 957 | private static JournaledFile makeJournaledFile(int userId) { |
Amith Yamasani | 61f5737 | 2012-08-31 12:12:28 -0700 | [diff] [blame] | 958 | final String base = new File(getWallpaperDir(userId), WALLPAPER_INFO).getAbsolutePath(); |
Joe Onorato | 9bb8fd7 | 2009-07-28 18:24:51 -0700 | [diff] [blame] | 959 | return new JournaledFile(new File(base), new File(base + ".tmp")); |
| 960 | } |
| 961 | |
Amith Yamasani | 37ce3a8 | 2012-02-06 12:04:42 -0800 | [diff] [blame] | 962 | private void saveSettingsLocked(WallpaperData wallpaper) { |
| 963 | JournaledFile journal = makeJournaledFile(wallpaper.userId); |
Joe Onorato | 9bb8fd7 | 2009-07-28 18:24:51 -0700 | [diff] [blame] | 964 | FileOutputStream stream = null; |
| 965 | try { |
| 966 | stream = new FileOutputStream(journal.chooseForWrite(), false); |
| 967 | XmlSerializer out = new FastXmlSerializer(); |
| 968 | out.setOutput(stream, "utf-8"); |
| 969 | out.startDocument(null, true); |
| 970 | |
| 971 | out.startTag(null, "wp"); |
Amith Yamasani | 37ce3a8 | 2012-02-06 12:04:42 -0800 | [diff] [blame] | 972 | out.attribute(null, "width", Integer.toString(wallpaper.width)); |
| 973 | out.attribute(null, "height", Integer.toString(wallpaper.height)); |
| 974 | out.attribute(null, "name", wallpaper.name); |
| 975 | if (wallpaper.wallpaperComponent != null |
| 976 | && !wallpaper.wallpaperComponent.equals(wallpaper.imageWallpaperComponent)) { |
Dianne Hackborn | 4c62fc0 | 2009-08-08 20:40:27 -0700 | [diff] [blame] | 977 | out.attribute(null, "component", |
Amith Yamasani | 37ce3a8 | 2012-02-06 12:04:42 -0800 | [diff] [blame] | 978 | wallpaper.wallpaperComponent.flattenToShortString()); |
Dianne Hackborn | 4c62fc0 | 2009-08-08 20:40:27 -0700 | [diff] [blame] | 979 | } |
Joe Onorato | 9bb8fd7 | 2009-07-28 18:24:51 -0700 | [diff] [blame] | 980 | out.endTag(null, "wp"); |
| 981 | |
| 982 | out.endDocument(); |
| 983 | stream.close(); |
| 984 | journal.commit(); |
| 985 | } catch (IOException e) { |
| 986 | try { |
| 987 | if (stream != null) { |
| 988 | stream.close(); |
| 989 | } |
| 990 | } catch (IOException ex) { |
| 991 | // Ignore |
| 992 | } |
| 993 | journal.rollback(); |
| 994 | } |
| 995 | } |
| 996 | |
Amith Yamasani | 37ce3a8 | 2012-02-06 12:04:42 -0800 | [diff] [blame] | 997 | private void migrateFromOld() { |
| 998 | File oldWallpaper = new File(WallpaperBackupHelper.WALLPAPER_IMAGE_KEY); |
| 999 | File oldInfo = new File(WallpaperBackupHelper.WALLPAPER_INFO_KEY); |
| 1000 | if (oldWallpaper.exists()) { |
| 1001 | File newWallpaper = new File(getWallpaperDir(0), WALLPAPER); |
| 1002 | oldWallpaper.renameTo(newWallpaper); |
| 1003 | } |
| 1004 | if (oldInfo.exists()) { |
| 1005 | File newInfo = new File(getWallpaperDir(0), WALLPAPER_INFO); |
| 1006 | oldInfo.renameTo(newInfo); |
| 1007 | } |
| 1008 | } |
| 1009 | |
| 1010 | private void loadSettingsLocked(int userId) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1011 | if (DEBUG) Slog.v(TAG, "loadSettingsLocked"); |
Mike Cleron | a428b2c | 2009-11-15 22:53:08 -0800 | [diff] [blame] | 1012 | |
Amith Yamasani | 37ce3a8 | 2012-02-06 12:04:42 -0800 | [diff] [blame] | 1013 | JournaledFile journal = makeJournaledFile(userId); |
Joe Onorato | 9bb8fd7 | 2009-07-28 18:24:51 -0700 | [diff] [blame] | 1014 | FileInputStream stream = null; |
| 1015 | File file = journal.chooseForRead(); |
Amith Yamasani | 37ce3a8 | 2012-02-06 12:04:42 -0800 | [diff] [blame] | 1016 | if (!file.exists()) { |
| 1017 | // This should only happen one time, when upgrading from a legacy system |
| 1018 | migrateFromOld(); |
| 1019 | } |
| 1020 | WallpaperData wallpaper = mWallpaperMap.get(userId); |
| 1021 | if (wallpaper == null) { |
| 1022 | wallpaper = new WallpaperData(userId); |
| 1023 | mWallpaperMap.put(userId, wallpaper); |
| 1024 | } |
Joe Onorato | 9bb8fd7 | 2009-07-28 18:24:51 -0700 | [diff] [blame] | 1025 | boolean success = false; |
| 1026 | try { |
| 1027 | stream = new FileInputStream(file); |
| 1028 | XmlPullParser parser = Xml.newPullParser(); |
| 1029 | parser.setInput(stream, null); |
| 1030 | |
| 1031 | int type; |
Joe Onorato | 9bb8fd7 | 2009-07-28 18:24:51 -0700 | [diff] [blame] | 1032 | do { |
| 1033 | type = parser.next(); |
| 1034 | if (type == XmlPullParser.START_TAG) { |
| 1035 | String tag = parser.getName(); |
| 1036 | if ("wp".equals(tag)) { |
Amith Yamasani | 37ce3a8 | 2012-02-06 12:04:42 -0800 | [diff] [blame] | 1037 | wallpaper.width = Integer.parseInt(parser.getAttributeValue(null, "width")); |
| 1038 | wallpaper.height = Integer.parseInt(parser |
| 1039 | .getAttributeValue(null, "height")); |
| 1040 | wallpaper.name = parser.getAttributeValue(null, "name"); |
Dianne Hackborn | 4c62fc0 | 2009-08-08 20:40:27 -0700 | [diff] [blame] | 1041 | String comp = parser.getAttributeValue(null, "component"); |
Amith Yamasani | 37ce3a8 | 2012-02-06 12:04:42 -0800 | [diff] [blame] | 1042 | wallpaper.nextWallpaperComponent = comp != null |
Dianne Hackborn | 4c62fc0 | 2009-08-08 20:40:27 -0700 | [diff] [blame] | 1043 | ? ComponentName.unflattenFromString(comp) |
| 1044 | : null; |
Amith Yamasani | 37ce3a8 | 2012-02-06 12:04:42 -0800 | [diff] [blame] | 1045 | if (wallpaper.nextWallpaperComponent == null |
| 1046 | || "android".equals(wallpaper.nextWallpaperComponent |
| 1047 | .getPackageName())) { |
| 1048 | wallpaper.nextWallpaperComponent = wallpaper.imageWallpaperComponent; |
Dianne Hackborn | 9ea3163 | 2011-08-05 14:43:50 -0700 | [diff] [blame] | 1049 | } |
Mike Cleron | a428b2c | 2009-11-15 22:53:08 -0800 | [diff] [blame] | 1050 | |
| 1051 | if (DEBUG) { |
Amith Yamasani | 37ce3a8 | 2012-02-06 12:04:42 -0800 | [diff] [blame] | 1052 | Slog.v(TAG, "mWidth:" + wallpaper.width); |
| 1053 | Slog.v(TAG, "mHeight:" + wallpaper.height); |
| 1054 | Slog.v(TAG, "mName:" + wallpaper.name); |
| 1055 | Slog.v(TAG, "mNextWallpaperComponent:" |
| 1056 | + wallpaper.nextWallpaperComponent); |
Mike Cleron | a428b2c | 2009-11-15 22:53:08 -0800 | [diff] [blame] | 1057 | } |
Joe Onorato | 9bb8fd7 | 2009-07-28 18:24:51 -0700 | [diff] [blame] | 1058 | } |
| 1059 | } |
| 1060 | } while (type != XmlPullParser.END_DOCUMENT); |
| 1061 | success = true; |
| 1062 | } catch (NullPointerException e) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1063 | Slog.w(TAG, "failed parsing " + file + " " + e); |
Joe Onorato | 9bb8fd7 | 2009-07-28 18:24:51 -0700 | [diff] [blame] | 1064 | } catch (NumberFormatException e) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1065 | Slog.w(TAG, "failed parsing " + file + " " + e); |
Joe Onorato | 9bb8fd7 | 2009-07-28 18:24:51 -0700 | [diff] [blame] | 1066 | } catch (XmlPullParserException e) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1067 | Slog.w(TAG, "failed parsing " + file + " " + e); |
Joe Onorato | 9bb8fd7 | 2009-07-28 18:24:51 -0700 | [diff] [blame] | 1068 | } catch (IOException e) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1069 | Slog.w(TAG, "failed parsing " + file + " " + e); |
Joe Onorato | 9bb8fd7 | 2009-07-28 18:24:51 -0700 | [diff] [blame] | 1070 | } catch (IndexOutOfBoundsException e) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1071 | Slog.w(TAG, "failed parsing " + file + " " + e); |
Joe Onorato | 9bb8fd7 | 2009-07-28 18:24:51 -0700 | [diff] [blame] | 1072 | } |
| 1073 | try { |
| 1074 | if (stream != null) { |
| 1075 | stream.close(); |
| 1076 | } |
| 1077 | } catch (IOException e) { |
| 1078 | // Ignore |
| 1079 | } |
| 1080 | |
| 1081 | if (!success) { |
Amith Yamasani | 37ce3a8 | 2012-02-06 12:04:42 -0800 | [diff] [blame] | 1082 | wallpaper.width = -1; |
| 1083 | wallpaper.height = -1; |
| 1084 | wallpaper.name = ""; |
Joe Onorato | 9bb8fd7 | 2009-07-28 18:24:51 -0700 | [diff] [blame] | 1085 | } |
Dianne Hackborn | 44bc17c | 2011-04-20 18:18:51 -0700 | [diff] [blame] | 1086 | |
| 1087 | // We always want to have some reasonable width hint. |
| 1088 | WindowManager wm = (WindowManager)mContext.getSystemService(Context.WINDOW_SERVICE); |
| 1089 | Display d = wm.getDefaultDisplay(); |
| 1090 | int baseSize = d.getMaximumSizeDimension(); |
Amith Yamasani | 37ce3a8 | 2012-02-06 12:04:42 -0800 | [diff] [blame] | 1091 | if (wallpaper.width < baseSize) { |
| 1092 | wallpaper.width = baseSize; |
Dianne Hackborn | 44bc17c | 2011-04-20 18:18:51 -0700 | [diff] [blame] | 1093 | } |
Amith Yamasani | 37ce3a8 | 2012-02-06 12:04:42 -0800 | [diff] [blame] | 1094 | if (wallpaper.height < baseSize) { |
| 1095 | wallpaper.height = baseSize; |
Dianne Hackborn | 44bc17c | 2011-04-20 18:18:51 -0700 | [diff] [blame] | 1096 | } |
Joe Onorato | 9bb8fd7 | 2009-07-28 18:24:51 -0700 | [diff] [blame] | 1097 | } |
| 1098 | |
Brad Fitzpatrick | 194b19a | 2010-09-14 11:30:29 -0700 | [diff] [blame] | 1099 | // Called by SystemBackupAgent after files are restored to disk. |
Joe Onorato | 9bb8fd7 | 2009-07-28 18:24:51 -0700 | [diff] [blame] | 1100 | void settingsRestored() { |
Amith Yamasani | 37ce3a8 | 2012-02-06 12:04:42 -0800 | [diff] [blame] | 1101 | // TODO: If necessary, make it work for secondary users as well. This currently assumes |
| 1102 | // restores only to the primary user |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1103 | if (DEBUG) Slog.v(TAG, "settingsRestored"); |
Amith Yamasani | 37ce3a8 | 2012-02-06 12:04:42 -0800 | [diff] [blame] | 1104 | WallpaperData wallpaper = null; |
Joe Onorato | 9bb8fd7 | 2009-07-28 18:24:51 -0700 | [diff] [blame] | 1105 | boolean success = false; |
| 1106 | synchronized (mLock) { |
Amith Yamasani | 37ce3a8 | 2012-02-06 12:04:42 -0800 | [diff] [blame] | 1107 | loadSettingsLocked(0); |
| 1108 | wallpaper = mWallpaperMap.get(0); |
| 1109 | if (wallpaper.nextWallpaperComponent != null |
| 1110 | && !wallpaper.nextWallpaperComponent.equals(wallpaper.imageWallpaperComponent)) { |
| 1111 | if (!bindWallpaperComponentLocked(wallpaper.nextWallpaperComponent, false, false, |
Dianne Hackborn | 5dc5a00 | 2012-09-15 19:33:48 -0700 | [diff] [blame] | 1112 | wallpaper, null)) { |
Christopher Tate | e3ab4d0 | 2009-12-16 14:03:31 -0800 | [diff] [blame] | 1113 | // No such live wallpaper or other failure; fall back to the default |
| 1114 | // live wallpaper (since the profile being restored indicated that the |
| 1115 | // user had selected a live rather than static one). |
Dianne Hackborn | 5dc5a00 | 2012-09-15 19:33:48 -0700 | [diff] [blame] | 1116 | bindWallpaperComponentLocked(null, false, false, wallpaper, null); |
Christopher Tate | e3ab4d0 | 2009-12-16 14:03:31 -0800 | [diff] [blame] | 1117 | } |
Joe Onorato | 9bb8fd7 | 2009-07-28 18:24:51 -0700 | [diff] [blame] | 1118 | success = true; |
| 1119 | } else { |
Mike Cleron | a428b2c | 2009-11-15 22:53:08 -0800 | [diff] [blame] | 1120 | // If there's a wallpaper name, we use that. If that can't be loaded, then we |
| 1121 | // use the default. |
Amith Yamasani | 37ce3a8 | 2012-02-06 12:04:42 -0800 | [diff] [blame] | 1122 | if ("".equals(wallpaper.name)) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1123 | if (DEBUG) Slog.v(TAG, "settingsRestored: name is empty"); |
Mike Cleron | a428b2c | 2009-11-15 22:53:08 -0800 | [diff] [blame] | 1124 | success = true; |
| 1125 | } else { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1126 | if (DEBUG) Slog.v(TAG, "settingsRestored: attempting to restore named resource"); |
Amith Yamasani | 37ce3a8 | 2012-02-06 12:04:42 -0800 | [diff] [blame] | 1127 | success = restoreNamedResourceLocked(wallpaper); |
Mike Cleron | a428b2c | 2009-11-15 22:53:08 -0800 | [diff] [blame] | 1128 | } |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1129 | if (DEBUG) Slog.v(TAG, "settingsRestored: success=" + success); |
Mike Cleron | a428b2c | 2009-11-15 22:53:08 -0800 | [diff] [blame] | 1130 | if (success) { |
Amith Yamasani | 37ce3a8 | 2012-02-06 12:04:42 -0800 | [diff] [blame] | 1131 | bindWallpaperComponentLocked(wallpaper.nextWallpaperComponent, false, false, |
Dianne Hackborn | 5dc5a00 | 2012-09-15 19:33:48 -0700 | [diff] [blame] | 1132 | wallpaper, null); |
Mike Cleron | a428b2c | 2009-11-15 22:53:08 -0800 | [diff] [blame] | 1133 | } |
Joe Onorato | 9bb8fd7 | 2009-07-28 18:24:51 -0700 | [diff] [blame] | 1134 | } |
| 1135 | } |
| 1136 | |
| 1137 | if (!success) { |
Amith Yamasani | 37ce3a8 | 2012-02-06 12:04:42 -0800 | [diff] [blame] | 1138 | Slog.e(TAG, "Failed to restore wallpaper: '" + wallpaper.name + "'"); |
| 1139 | wallpaper.name = ""; |
| 1140 | getWallpaperDir(0).delete(); |
Joe Onorato | 9bb8fd7 | 2009-07-28 18:24:51 -0700 | [diff] [blame] | 1141 | } |
Brad Fitzpatrick | 194b19a | 2010-09-14 11:30:29 -0700 | [diff] [blame] | 1142 | |
| 1143 | synchronized (mLock) { |
Amith Yamasani | 37ce3a8 | 2012-02-06 12:04:42 -0800 | [diff] [blame] | 1144 | saveSettingsLocked(wallpaper); |
Brad Fitzpatrick | 194b19a | 2010-09-14 11:30:29 -0700 | [diff] [blame] | 1145 | } |
Joe Onorato | 9bb8fd7 | 2009-07-28 18:24:51 -0700 | [diff] [blame] | 1146 | } |
| 1147 | |
Amith Yamasani | 37ce3a8 | 2012-02-06 12:04:42 -0800 | [diff] [blame] | 1148 | boolean restoreNamedResourceLocked(WallpaperData wallpaper) { |
| 1149 | if (wallpaper.name.length() > 4 && "res:".equals(wallpaper.name.substring(0, 4))) { |
| 1150 | String resName = wallpaper.name.substring(4); |
Joe Onorato | 9bb8fd7 | 2009-07-28 18:24:51 -0700 | [diff] [blame] | 1151 | |
| 1152 | String pkg = null; |
| 1153 | int colon = resName.indexOf(':'); |
| 1154 | if (colon > 0) { |
| 1155 | pkg = resName.substring(0, colon); |
| 1156 | } |
| 1157 | |
| 1158 | String ident = null; |
| 1159 | int slash = resName.lastIndexOf('/'); |
| 1160 | if (slash > 0) { |
| 1161 | ident = resName.substring(slash+1); |
| 1162 | } |
| 1163 | |
| 1164 | String type = null; |
| 1165 | if (colon > 0 && slash > 0 && (slash-colon) > 1) { |
| 1166 | type = resName.substring(colon+1, slash); |
| 1167 | } |
| 1168 | |
| 1169 | if (pkg != null && ident != null && type != null) { |
| 1170 | int resId = -1; |
| 1171 | InputStream res = null; |
| 1172 | FileOutputStream fos = null; |
| 1173 | try { |
| 1174 | Context c = mContext.createPackageContext(pkg, Context.CONTEXT_RESTRICTED); |
| 1175 | Resources r = c.getResources(); |
| 1176 | resId = r.getIdentifier(resName, null, null); |
| 1177 | if (resId == 0) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1178 | Slog.e(TAG, "couldn't resolve identifier pkg=" + pkg + " type=" + type |
Joe Onorato | 9bb8fd7 | 2009-07-28 18:24:51 -0700 | [diff] [blame] | 1179 | + " ident=" + ident); |
| 1180 | return false; |
| 1181 | } |
| 1182 | |
| 1183 | res = r.openRawResource(resId); |
Amith Yamasani | 37ce3a8 | 2012-02-06 12:04:42 -0800 | [diff] [blame] | 1184 | if (wallpaper.wallpaperFile.exists()) { |
| 1185 | wallpaper.wallpaperFile.delete(); |
Dianne Hackborn | 1afd1c9 | 2010-03-18 22:47:17 -0700 | [diff] [blame] | 1186 | } |
Amith Yamasani | 37ce3a8 | 2012-02-06 12:04:42 -0800 | [diff] [blame] | 1187 | fos = new FileOutputStream(wallpaper.wallpaperFile); |
Joe Onorato | 9bb8fd7 | 2009-07-28 18:24:51 -0700 | [diff] [blame] | 1188 | |
| 1189 | byte[] buffer = new byte[32768]; |
| 1190 | int amt; |
| 1191 | while ((amt=res.read(buffer)) > 0) { |
| 1192 | fos.write(buffer, 0, amt); |
| 1193 | } |
| 1194 | // mWallpaperObserver will notice the close and send the change broadcast |
| 1195 | |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1196 | Slog.v(TAG, "Restored wallpaper: " + resName); |
Joe Onorato | 9bb8fd7 | 2009-07-28 18:24:51 -0700 | [diff] [blame] | 1197 | return true; |
| 1198 | } catch (NameNotFoundException e) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1199 | Slog.e(TAG, "Package name " + pkg + " not found"); |
Joe Onorato | 9bb8fd7 | 2009-07-28 18:24:51 -0700 | [diff] [blame] | 1200 | } catch (Resources.NotFoundException e) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1201 | Slog.e(TAG, "Resource not found: " + resId); |
Joe Onorato | 9bb8fd7 | 2009-07-28 18:24:51 -0700 | [diff] [blame] | 1202 | } catch (IOException e) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1203 | Slog.e(TAG, "IOException while restoring wallpaper ", e); |
Joe Onorato | 9bb8fd7 | 2009-07-28 18:24:51 -0700 | [diff] [blame] | 1204 | } finally { |
| 1205 | if (res != null) { |
| 1206 | try { |
| 1207 | res.close(); |
| 1208 | } catch (IOException ex) {} |
| 1209 | } |
| 1210 | if (fos != null) { |
Dianne Hackborn | 8bdf593 | 2010-10-15 12:54:40 -0700 | [diff] [blame] | 1211 | FileUtils.sync(fos); |
Joe Onorato | 9bb8fd7 | 2009-07-28 18:24:51 -0700 | [diff] [blame] | 1212 | try { |
| 1213 | fos.close(); |
| 1214 | } catch (IOException ex) {} |
| 1215 | } |
| 1216 | } |
| 1217 | } |
| 1218 | } |
| 1219 | return false; |
| 1220 | } |
Amith Yamasani | 37ce3a8 | 2012-02-06 12:04:42 -0800 | [diff] [blame] | 1221 | |
Dianne Hackborn | eb03465 | 2009-09-07 00:49:58 -0700 | [diff] [blame] | 1222 | @Override |
| 1223 | protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) { |
| 1224 | if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DUMP) |
| 1225 | != PackageManager.PERMISSION_GRANTED) { |
| 1226 | |
| 1227 | pw.println("Permission Denial: can't dump wallpaper service from from pid=" |
| 1228 | + Binder.getCallingPid() |
| 1229 | + ", uid=" + Binder.getCallingUid()); |
| 1230 | return; |
| 1231 | } |
| 1232 | |
| 1233 | synchronized (mLock) { |
| 1234 | pw.println("Current Wallpaper Service state:"); |
Amith Yamasani | 37ce3a8 | 2012-02-06 12:04:42 -0800 | [diff] [blame] | 1235 | for (int i = 0; i < mWallpaperMap.size(); i++) { |
| 1236 | WallpaperData wallpaper = mWallpaperMap.valueAt(i); |
| 1237 | pw.println(" User " + wallpaper.userId + ":"); |
| 1238 | pw.print(" mWidth="); |
| 1239 | pw.print(wallpaper.width); |
| 1240 | pw.print(" mHeight="); |
| 1241 | pw.println(wallpaper.height); |
| 1242 | pw.print(" mName="); |
| 1243 | pw.println(wallpaper.name); |
| 1244 | pw.print(" mWallpaperComponent="); |
| 1245 | pw.println(wallpaper.wallpaperComponent); |
| 1246 | if (wallpaper.connection != null) { |
| 1247 | WallpaperConnection conn = wallpaper.connection; |
| 1248 | pw.print(" Wallpaper connection "); |
| 1249 | pw.print(conn); |
| 1250 | pw.println(":"); |
| 1251 | if (conn.mInfo != null) { |
| 1252 | pw.print(" mInfo.component="); |
| 1253 | pw.println(conn.mInfo.getComponent()); |
| 1254 | } |
| 1255 | pw.print(" mToken="); |
| 1256 | pw.println(conn.mToken); |
| 1257 | pw.print(" mService="); |
| 1258 | pw.println(conn.mService); |
| 1259 | pw.print(" mEngine="); |
| 1260 | pw.println(conn.mEngine); |
| 1261 | pw.print(" mLastDiedTime="); |
| 1262 | pw.println(wallpaper.lastDiedTime - SystemClock.uptimeMillis()); |
| 1263 | } |
Dianne Hackborn | eb03465 | 2009-09-07 00:49:58 -0700 | [diff] [blame] | 1264 | } |
| 1265 | } |
| 1266 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1267 | } |