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