| 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 | |
| Dianne Hackborn | 8cc6a50 | 2009-08-05 21:29:42 -0700 | [diff] [blame] | 22 | import android.app.IWallpaperManager; |
| 23 | import android.app.IWallpaperManagerCallback; |
| Christopher Tate | 111bd4a | 2009-06-24 17:29:38 -0700 | [diff] [blame] | 24 | import android.backup.BackupManager; |
| Dianne Hackborn | 4c62fc0 | 2009-08-08 20:40:27 -0700 | [diff] [blame] | 25 | import android.content.ComponentName; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 26 | import android.content.Context; |
| 27 | import android.content.Intent; |
| Dianne Hackborn | 4c62fc0 | 2009-08-08 20:40:27 -0700 | [diff] [blame] | 28 | import android.content.ServiceConnection; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 29 | import android.content.pm.PackageManager; |
| Dianne Hackborn | 4c62fc0 | 2009-08-08 20:40:27 -0700 | [diff] [blame] | 30 | import android.content.pm.ResolveInfo; |
| 31 | import android.content.pm.ServiceInfo; |
| Joe Onorato | 9bb8fd7 | 2009-07-28 18:24:51 -0700 | [diff] [blame] | 32 | import android.content.pm.PackageManager.NameNotFoundException; |
| 33 | import android.content.res.Resources; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 34 | import android.os.Binder; |
| Dianne Hackborn | 4c62fc0 | 2009-08-08 20:40:27 -0700 | [diff] [blame] | 35 | import android.os.IBinder; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 36 | import android.os.RemoteException; |
| 37 | import android.os.FileObserver; |
| 38 | import android.os.ParcelFileDescriptor; |
| 39 | import android.os.RemoteCallbackList; |
| Dianne Hackborn | 4c62fc0 | 2009-08-08 20:40:27 -0700 | [diff] [blame] | 40 | import android.os.ServiceManager; |
| Dianne Hackborn | 0cd4887 | 2009-08-13 18:51:59 -0700 | [diff] [blame] | 41 | import android.os.SystemClock; |
| Dianne Hackborn | 4c62fc0 | 2009-08-08 20:40:27 -0700 | [diff] [blame] | 42 | import android.service.wallpaper.IWallpaperConnection; |
| 43 | import android.service.wallpaper.IWallpaperEngine; |
| 44 | import android.service.wallpaper.IWallpaperService; |
| 45 | import android.service.wallpaper.WallpaperService; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 46 | import android.util.Log; |
| Joe Onorato | 9bb8fd7 | 2009-07-28 18:24:51 -0700 | [diff] [blame] | 47 | import android.util.Xml; |
| Dianne Hackborn | 4c62fc0 | 2009-08-08 20:40:27 -0700 | [diff] [blame] | 48 | import android.view.IWindowManager; |
| 49 | import android.view.WindowManager; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 50 | |
| Joe Onorato | 9bb8fd7 | 2009-07-28 18:24:51 -0700 | [diff] [blame] | 51 | import java.io.IOException; |
| 52 | import java.io.InputStream; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 53 | import java.io.File; |
| 54 | import java.io.FileNotFoundException; |
| Joe Onorato | 9bb8fd7 | 2009-07-28 18:24:51 -0700 | [diff] [blame] | 55 | import java.io.FileInputStream; |
| 56 | import java.io.FileOutputStream; |
| Dianne Hackborn | 4c62fc0 | 2009-08-08 20:40:27 -0700 | [diff] [blame] | 57 | import java.util.List; |
| Joe Onorato | 9bb8fd7 | 2009-07-28 18:24:51 -0700 | [diff] [blame] | 58 | |
| 59 | import org.xmlpull.v1.XmlPullParser; |
| 60 | import org.xmlpull.v1.XmlPullParserException; |
| 61 | import org.xmlpull.v1.XmlSerializer; |
| 62 | |
| Dianne Hackborn | f21adf6 | 2009-08-13 10:20:21 -0700 | [diff] [blame] | 63 | import com.android.internal.service.wallpaper.ImageWallpaper; |
| Joe Onorato | 9bb8fd7 | 2009-07-28 18:24:51 -0700 | [diff] [blame] | 64 | import com.android.internal.util.FastXmlSerializer; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 65 | |
| Dianne Hackborn | 8cc6a50 | 2009-08-05 21:29:42 -0700 | [diff] [blame] | 66 | class WallpaperManagerService extends IWallpaperManager.Stub { |
| Dianne Hackborn | 4c62fc0 | 2009-08-08 20:40:27 -0700 | [diff] [blame] | 67 | static final String TAG = "WallpaperService"; |
| 68 | static final boolean DEBUG = true; |
| Joe Onorato | 9bb8fd7 | 2009-07-28 18:24:51 -0700 | [diff] [blame] | 69 | |
| Dianne Hackborn | 4c62fc0 | 2009-08-08 20:40:27 -0700 | [diff] [blame] | 70 | Object mLock = new Object(); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 71 | |
| Dianne Hackborn | 0cd4887 | 2009-08-13 18:51:59 -0700 | [diff] [blame] | 72 | /** |
| 73 | * Minimum time between crashes of a wallpaper service for us to consider |
| 74 | * restarting it vs. just reverting to the static wallpaper. |
| 75 | */ |
| 76 | static final long MIN_WALLPAPER_CRASH_TIME = 10000; |
| 77 | |
| 78 | static final File WALLPAPER_DIR = new File( |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 79 | "/data/data/com.android.settings/files"); |
| Dianne Hackborn | 0cd4887 | 2009-08-13 18:51:59 -0700 | [diff] [blame] | 80 | static final String WALLPAPER = "wallpaper"; |
| 81 | static final File WALLPAPER_FILE = new File(WALLPAPER_DIR, WALLPAPER); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 82 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 83 | /** |
| 84 | * List of callbacks registered they should each be notified |
| 85 | * when the wallpaper is changed. |
| 86 | */ |
| Dianne Hackborn | 8cc6a50 | 2009-08-05 21:29:42 -0700 | [diff] [blame] | 87 | private final RemoteCallbackList<IWallpaperManagerCallback> mCallbacks |
| 88 | = new RemoteCallbackList<IWallpaperManagerCallback>(); |
| Joe Onorato | 9bb8fd7 | 2009-07-28 18:24:51 -0700 | [diff] [blame] | 89 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 90 | /** |
| 91 | * Observes the wallpaper for changes and notifies all IWallpaperServiceCallbacks |
| 92 | * that the wallpaper has changed. The CREATE is triggered when there is no |
| 93 | * wallpaper set and is created for the first time. The CLOSE_WRITE is triggered |
| 94 | * everytime the wallpaper is changed. |
| 95 | */ |
| 96 | private final FileObserver mWallpaperObserver = new FileObserver( |
| Joe Onorato | 9bb8fd7 | 2009-07-28 18:24:51 -0700 | [diff] [blame] | 97 | WALLPAPER_DIR.getAbsolutePath(), CREATE | CLOSE_WRITE | DELETE | DELETE_SELF) { |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 98 | @Override |
| 99 | public void onEvent(int event, String path) { |
| Joe Onorato | e712ee3 | 2009-07-29 16:23:58 -0700 | [diff] [blame] | 100 | if (path == null) { |
| 101 | return; |
| 102 | } |
| Joe Onorato | 9bb8fd7 | 2009-07-28 18:24:51 -0700 | [diff] [blame] | 103 | synchronized (mLock) { |
| Joe Onorato | 9bb8fd7 | 2009-07-28 18:24:51 -0700 | [diff] [blame] | 104 | // changing the wallpaper means we'll need to back up the new one |
| 105 | long origId = Binder.clearCallingIdentity(); |
| 106 | BackupManager bm = new BackupManager(mContext); |
| 107 | bm.dataChanged(); |
| 108 | Binder.restoreCallingIdentity(origId); |
| 109 | |
| 110 | File changedFile = new File(WALLPAPER_DIR, path); |
| 111 | if (WALLPAPER_FILE.equals(changedFile)) { |
| 112 | notifyCallbacksLocked(); |
| 113 | } |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 114 | } |
| 115 | } |
| 116 | }; |
| 117 | |
| Dianne Hackborn | 4c62fc0 | 2009-08-08 20:40:27 -0700 | [diff] [blame] | 118 | final Context mContext; |
| 119 | final IWindowManager mIWindowManager; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 120 | |
| Dianne Hackborn | 4c62fc0 | 2009-08-08 20:40:27 -0700 | [diff] [blame] | 121 | int mWidth = -1; |
| 122 | int mHeight = -1; |
| 123 | String mName = ""; |
| 124 | ComponentName mWallpaperComponent; |
| 125 | WallpaperConnection mWallpaperConnection; |
| Dianne Hackborn | 0cd4887 | 2009-08-13 18:51:59 -0700 | [diff] [blame] | 126 | long mLastDiedTime; |
| Dianne Hackborn | 4c62fc0 | 2009-08-08 20:40:27 -0700 | [diff] [blame] | 127 | |
| 128 | class WallpaperConnection extends IWallpaperConnection.Stub |
| 129 | implements ServiceConnection { |
| 130 | final Binder mToken = new Binder(); |
| 131 | IWallpaperService mService; |
| 132 | IWallpaperEngine mEngine; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 133 | |
| Dianne Hackborn | 4c62fc0 | 2009-08-08 20:40:27 -0700 | [diff] [blame] | 134 | public void onServiceConnected(ComponentName name, IBinder service) { |
| 135 | synchronized (mLock) { |
| 136 | if (mWallpaperConnection == this) { |
| 137 | mService = IWallpaperService.Stub.asInterface(service); |
| 138 | attachServiceLocked(this); |
| 139 | } |
| 140 | } |
| 141 | } |
| 142 | |
| 143 | public void onServiceDisconnected(ComponentName name) { |
| 144 | synchronized (mLock) { |
| 145 | mService = null; |
| 146 | mEngine = null; |
| Dianne Hackborn | 0cd4887 | 2009-08-13 18:51:59 -0700 | [diff] [blame] | 147 | if (mWallpaperConnection == this) { |
| 148 | Log.w(TAG, "Wallpaper service gone: " + mWallpaperComponent); |
| 149 | if ((mLastDiedTime+MIN_WALLPAPER_CRASH_TIME) |
| 150 | < SystemClock.uptimeMillis()) { |
| 151 | Log.w(TAG, "Reverting to built-in wallpaper!"); |
| 152 | bindWallpaperComponentLocked(null); |
| 153 | } |
| 154 | } |
| Dianne Hackborn | 4c62fc0 | 2009-08-08 20:40:27 -0700 | [diff] [blame] | 155 | } |
| 156 | } |
| 157 | |
| 158 | public void attachEngine(IWallpaperEngine engine) { |
| 159 | mEngine = engine; |
| 160 | } |
| 161 | |
| 162 | public ParcelFileDescriptor setWallpaper(String name) { |
| 163 | synchronized (mLock) { |
| 164 | if (mWallpaperConnection == this) { |
| 165 | ParcelFileDescriptor pfd = updateWallpaperBitmapLocked(name); |
| 166 | if (pfd != null) { |
| 167 | saveSettingsLocked(); |
| 168 | } |
| 169 | return pfd; |
| 170 | } |
| 171 | return null; |
| 172 | } |
| 173 | } |
| 174 | } |
| 175 | |
| Dianne Hackborn | 8cc6a50 | 2009-08-05 21:29:42 -0700 | [diff] [blame] | 176 | public WallpaperManagerService(Context context) { |
| Dianne Hackborn | 4c62fc0 | 2009-08-08 20:40:27 -0700 | [diff] [blame] | 177 | if (DEBUG) Log.d(TAG, "WallpaperService startup"); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 178 | mContext = context; |
| Dianne Hackborn | 4c62fc0 | 2009-08-08 20:40:27 -0700 | [diff] [blame] | 179 | mIWindowManager = IWindowManager.Stub.asInterface( |
| 180 | ServiceManager.getService(Context.WINDOW_SERVICE)); |
| Joe Onorato | e712ee3 | 2009-07-29 16:23:58 -0700 | [diff] [blame] | 181 | WALLPAPER_DIR.mkdirs(); |
| Joe Onorato | 9bb8fd7 | 2009-07-28 18:24:51 -0700 | [diff] [blame] | 182 | loadSettingsLocked(); |
| Joe Onorato | e712ee3 | 2009-07-29 16:23:58 -0700 | [diff] [blame] | 183 | mWallpaperObserver.startWatching(); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 184 | } |
| 185 | |
| 186 | @Override |
| 187 | protected void finalize() throws Throwable { |
| 188 | super.finalize(); |
| 189 | mWallpaperObserver.stopWatching(); |
| 190 | } |
| 191 | |
| Dianne Hackborn | f21adf6 | 2009-08-13 10:20:21 -0700 | [diff] [blame] | 192 | public void systemReady() { |
| 193 | synchronized (mLock) { |
| 194 | try { |
| 195 | bindWallpaperComponentLocked(mWallpaperComponent); |
| 196 | } catch (RuntimeException e) { |
| 197 | Log.w(TAG, "Failure starting previous wallpaper", e); |
| 198 | try { |
| 199 | bindWallpaperComponentLocked(null); |
| 200 | } catch (RuntimeException e2) { |
| 201 | Log.w(TAG, "Failure starting default wallpaper", e2); |
| 202 | clearWallpaperComponentLocked(); |
| 203 | } |
| 204 | } |
| 205 | } |
| 206 | } |
| 207 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 208 | public void clearWallpaper() { |
| Joe Onorato | 9bb8fd7 | 2009-07-28 18:24:51 -0700 | [diff] [blame] | 209 | synchronized (mLock) { |
| 210 | File f = WALLPAPER_FILE; |
| 211 | if (f.exists()) { |
| 212 | f.delete(); |
| 213 | } |
| Dianne Hackborn | 0cd4887 | 2009-08-13 18:51:59 -0700 | [diff] [blame] | 214 | final long ident = Binder.clearCallingIdentity(); |
| 215 | try { |
| 216 | bindWallpaperComponentLocked(null); |
| 217 | } finally { |
| 218 | Binder.restoreCallingIdentity(ident); |
| 219 | } |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 220 | } |
| 221 | } |
| 222 | |
| 223 | public void setDimensionHints(int width, int height) throws RemoteException { |
| 224 | checkPermission(android.Manifest.permission.SET_WALLPAPER_HINTS); |
| 225 | |
| 226 | if (width <= 0 || height <= 0) { |
| 227 | throw new IllegalArgumentException("width and height must be > 0"); |
| 228 | } |
| 229 | |
| Joe Onorato | 9bb8fd7 | 2009-07-28 18:24:51 -0700 | [diff] [blame] | 230 | synchronized (mLock) { |
| 231 | if (width != mWidth || height != mHeight) { |
| 232 | mWidth = width; |
| 233 | mHeight = height; |
| 234 | saveSettingsLocked(); |
| 235 | } |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 236 | } |
| 237 | } |
| 238 | |
| 239 | public int getWidthHint() throws RemoteException { |
| Joe Onorato | 9bb8fd7 | 2009-07-28 18:24:51 -0700 | [diff] [blame] | 240 | synchronized (mLock) { |
| 241 | return mWidth; |
| 242 | } |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 243 | } |
| 244 | |
| 245 | public int getHeightHint() throws RemoteException { |
| Joe Onorato | 9bb8fd7 | 2009-07-28 18:24:51 -0700 | [diff] [blame] | 246 | synchronized (mLock) { |
| 247 | return mHeight; |
| 248 | } |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 249 | } |
| 250 | |
| Dianne Hackborn | 8cc6a50 | 2009-08-05 21:29:42 -0700 | [diff] [blame] | 251 | public ParcelFileDescriptor getWallpaper(IWallpaperManagerCallback cb) { |
| Joe Onorato | 9bb8fd7 | 2009-07-28 18:24:51 -0700 | [diff] [blame] | 252 | synchronized (mLock) { |
| 253 | try { |
| 254 | mCallbacks.register(cb); |
| 255 | File f = WALLPAPER_FILE; |
| 256 | if (!f.exists()) { |
| 257 | return null; |
| 258 | } |
| 259 | return ParcelFileDescriptor.open(f, MODE_READ_ONLY); |
| 260 | } catch (FileNotFoundException e) { |
| 261 | /* Shouldn't happen as we check to see if the file exists */ |
| Dianne Hackborn | 4c62fc0 | 2009-08-08 20:40:27 -0700 | [diff] [blame] | 262 | Log.w(TAG, "Error getting wallpaper", e); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 263 | } |
| Joe Onorato | 9bb8fd7 | 2009-07-28 18:24:51 -0700 | [diff] [blame] | 264 | return null; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 265 | } |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 266 | } |
| 267 | |
| Joe Onorato | 9bb8fd7 | 2009-07-28 18:24:51 -0700 | [diff] [blame] | 268 | public ParcelFileDescriptor setWallpaper(String name) { |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 269 | checkPermission(android.Manifest.permission.SET_WALLPAPER); |
| Joe Onorato | 9bb8fd7 | 2009-07-28 18:24:51 -0700 | [diff] [blame] | 270 | synchronized (mLock) { |
| Dianne Hackborn | 0cd4887 | 2009-08-13 18:51:59 -0700 | [diff] [blame] | 271 | final long ident = Binder.clearCallingIdentity(); |
| 272 | try { |
| 273 | ParcelFileDescriptor pfd = updateWallpaperBitmapLocked(name); |
| 274 | if (pfd != null) { |
| 275 | bindWallpaperComponentLocked(null); |
| 276 | saveSettingsLocked(); |
| 277 | } |
| 278 | return pfd; |
| 279 | } finally { |
| 280 | Binder.restoreCallingIdentity(ident); |
| Joe Onorato | 9bb8fd7 | 2009-07-28 18:24:51 -0700 | [diff] [blame] | 281 | } |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 282 | } |
| 283 | } |
| 284 | |
| Dianne Hackborn | 4c62fc0 | 2009-08-08 20:40:27 -0700 | [diff] [blame] | 285 | ParcelFileDescriptor updateWallpaperBitmapLocked(String name) { |
| 286 | if (name == null) name = ""; |
| 287 | try { |
| 288 | ParcelFileDescriptor fd = ParcelFileDescriptor.open(WALLPAPER_FILE, |
| 289 | MODE_CREATE|MODE_READ_WRITE); |
| 290 | mName = name; |
| 291 | return fd; |
| 292 | } catch (FileNotFoundException e) { |
| 293 | Log.w(TAG, "Error setting wallpaper", e); |
| 294 | } |
| 295 | return null; |
| 296 | } |
| 297 | |
| 298 | public void setWallpaperComponent(ComponentName name) { |
| 299 | checkPermission(android.Manifest.permission.SET_WALLPAPER_COMPONENT); |
| 300 | synchronized (mLock) { |
| 301 | final long ident = Binder.clearCallingIdentity(); |
| 302 | try { |
| Dianne Hackborn | f21adf6 | 2009-08-13 10:20:21 -0700 | [diff] [blame] | 303 | bindWallpaperComponentLocked(name); |
| 304 | } finally { |
| 305 | Binder.restoreCallingIdentity(ident); |
| 306 | } |
| 307 | } |
| 308 | } |
| 309 | |
| 310 | void bindWallpaperComponentLocked(ComponentName name) { |
| 311 | // Has the component changed? |
| 312 | if (mWallpaperConnection != null) { |
| 313 | if (mWallpaperComponent == null) { |
| 314 | if (name == null) { |
| 315 | // Still using default wallpaper. |
| 316 | return; |
| Dianne Hackborn | 4c62fc0 | 2009-08-08 20:40:27 -0700 | [diff] [blame] | 317 | } |
| Dianne Hackborn | f21adf6 | 2009-08-13 10:20:21 -0700 | [diff] [blame] | 318 | } else if (mWallpaperComponent.equals(name)) { |
| 319 | // Changing to same wallpaper. |
| 320 | return; |
| 321 | } |
| 322 | } |
| 323 | |
| 324 | try { |
| 325 | ComponentName realName = name; |
| 326 | if (realName == null) { |
| 327 | // The default component is our static image wallpaper. |
| Dianne Hackborn | b1ac1a8 | 2009-08-14 12:12:31 -0700 | [diff] [blame] | 328 | //realName = new ComponentName("android", |
| 329 | // ImageWallpaper.class.getName()); |
| 330 | clearWallpaperComponentLocked(); |
| 331 | return; |
| Dianne Hackborn | f21adf6 | 2009-08-13 10:20:21 -0700 | [diff] [blame] | 332 | } |
| 333 | ServiceInfo si = mContext.getPackageManager().getServiceInfo(realName, |
| 334 | PackageManager.GET_META_DATA | PackageManager.GET_PERMISSIONS); |
| 335 | if (!android.Manifest.permission.BIND_WALLPAPER.equals(si.permission)) { |
| 336 | throw new SecurityException("Selected service does not require " |
| 337 | + android.Manifest.permission.BIND_WALLPAPER |
| 338 | + ": " + realName); |
| 339 | } |
| 340 | |
| 341 | Intent intent = new Intent(WallpaperService.SERVICE_INTERFACE); |
| 342 | if (name != null) { |
| Dianne Hackborn | 4c62fc0 | 2009-08-08 20:40:27 -0700 | [diff] [blame] | 343 | // Make sure the selected service is actually a wallpaper service. |
| Dianne Hackborn | 4c62fc0 | 2009-08-08 20:40:27 -0700 | [diff] [blame] | 344 | List<ResolveInfo> ris = mContext.getPackageManager() |
| 345 | .queryIntentServices(intent, 0); |
| 346 | for (int i=0; i<ris.size(); i++) { |
| 347 | ServiceInfo rsi = ris.get(i).serviceInfo; |
| 348 | if (rsi.name.equals(si.name) && |
| 349 | rsi.packageName.equals(si.packageName)) { |
| 350 | ris = null; |
| 351 | break; |
| 352 | } |
| 353 | } |
| 354 | if (ris != null) { |
| 355 | throw new SecurityException("Selected service is not a wallpaper: " |
| Dianne Hackborn | f21adf6 | 2009-08-13 10:20:21 -0700 | [diff] [blame] | 356 | + realName); |
| Dianne Hackborn | 4c62fc0 | 2009-08-08 20:40:27 -0700 | [diff] [blame] | 357 | } |
| Dianne Hackborn | 4c62fc0 | 2009-08-08 20:40:27 -0700 | [diff] [blame] | 358 | } |
| Dianne Hackborn | f21adf6 | 2009-08-13 10:20:21 -0700 | [diff] [blame] | 359 | |
| 360 | // Bind the service! |
| 361 | WallpaperConnection newConn = new WallpaperConnection(); |
| 362 | intent.setComponent(realName); |
| 363 | if (!mContext.bindService(intent, newConn, |
| 364 | Context.BIND_AUTO_CREATE)) { |
| 365 | throw new IllegalArgumentException("Unable to bind service: " |
| 366 | + name); |
| 367 | } |
| 368 | |
| 369 | clearWallpaperComponentLocked(); |
| 370 | mWallpaperComponent = name; |
| 371 | mWallpaperConnection = newConn; |
| Dianne Hackborn | 0cd4887 | 2009-08-13 18:51:59 -0700 | [diff] [blame] | 372 | mLastDiedTime = SystemClock.uptimeMillis(); |
| Dianne Hackborn | f21adf6 | 2009-08-13 10:20:21 -0700 | [diff] [blame] | 373 | try { |
| 374 | if (DEBUG) Log.v(TAG, "Adding window token: " + newConn.mToken); |
| 375 | mIWindowManager.addWindowToken(newConn.mToken, |
| 376 | WindowManager.LayoutParams.TYPE_WALLPAPER); |
| 377 | } catch (RemoteException e) { |
| 378 | } |
| 379 | |
| 380 | } catch (PackageManager.NameNotFoundException e) { |
| 381 | throw new IllegalArgumentException("Unknown component " + name); |
| Dianne Hackborn | 4c62fc0 | 2009-08-08 20:40:27 -0700 | [diff] [blame] | 382 | } |
| 383 | } |
| 384 | |
| 385 | void clearWallpaperComponentLocked() { |
| 386 | mWallpaperComponent = null; |
| 387 | if (mWallpaperConnection != null) { |
| 388 | if (mWallpaperConnection.mEngine != null) { |
| 389 | try { |
| 390 | mWallpaperConnection.mEngine.destroy(); |
| 391 | } catch (RemoteException e) { |
| 392 | } |
| 393 | } |
| 394 | mContext.unbindService(mWallpaperConnection); |
| 395 | mWallpaperConnection = null; |
| 396 | } |
| 397 | } |
| 398 | |
| 399 | void attachServiceLocked(WallpaperConnection conn) { |
| 400 | try { |
| 401 | conn.mService.attach(conn, conn.mToken, mWidth, mHeight); |
| 402 | } catch (RemoteException e) { |
| 403 | Log.w(TAG, "Failed attaching wallpaper; clearing", e); |
| Dianne Hackborn | f21adf6 | 2009-08-13 10:20:21 -0700 | [diff] [blame] | 404 | bindWallpaperComponentLocked(null); |
| Dianne Hackborn | 4c62fc0 | 2009-08-08 20:40:27 -0700 | [diff] [blame] | 405 | } |
| 406 | } |
| 407 | |
| Joe Onorato | 9bb8fd7 | 2009-07-28 18:24:51 -0700 | [diff] [blame] | 408 | private void notifyCallbacksLocked() { |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 409 | final int n = mCallbacks.beginBroadcast(); |
| 410 | for (int i = 0; i < n; i++) { |
| 411 | try { |
| 412 | mCallbacks.getBroadcastItem(i).onWallpaperChanged(); |
| 413 | } catch (RemoteException e) { |
| 414 | |
| 415 | // The RemoteCallbackList will take care of removing |
| 416 | // the dead object for us. |
| 417 | } |
| 418 | } |
| 419 | mCallbacks.finishBroadcast(); |
| 420 | final Intent intent = new Intent(Intent.ACTION_WALLPAPER_CHANGED); |
| 421 | mContext.sendBroadcast(intent); |
| 422 | } |
| 423 | |
| 424 | private void checkPermission(String permission) { |
| Joe Onorato | 9bb8fd7 | 2009-07-28 18:24:51 -0700 | [diff] [blame] | 425 | if (PackageManager.PERMISSION_GRANTED!= mContext.checkCallingOrSelfPermission(permission)) { |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 426 | throw new SecurityException("Access denied to process: " + Binder.getCallingPid() |
| 427 | + ", must have permission " + permission); |
| 428 | } |
| 429 | } |
| Joe Onorato | 9bb8fd7 | 2009-07-28 18:24:51 -0700 | [diff] [blame] | 430 | |
| 431 | private static JournaledFile makeJournaledFile() { |
| 432 | final String base = "/data/system/wallpaper_info.xml"; |
| 433 | return new JournaledFile(new File(base), new File(base + ".tmp")); |
| 434 | } |
| 435 | |
| 436 | private void saveSettingsLocked() { |
| 437 | JournaledFile journal = makeJournaledFile(); |
| 438 | FileOutputStream stream = null; |
| 439 | try { |
| 440 | stream = new FileOutputStream(journal.chooseForWrite(), false); |
| 441 | XmlSerializer out = new FastXmlSerializer(); |
| 442 | out.setOutput(stream, "utf-8"); |
| 443 | out.startDocument(null, true); |
| 444 | |
| 445 | out.startTag(null, "wp"); |
| 446 | out.attribute(null, "width", Integer.toString(mWidth)); |
| 447 | out.attribute(null, "height", Integer.toString(mHeight)); |
| 448 | out.attribute(null, "name", mName); |
| Dianne Hackborn | 4c62fc0 | 2009-08-08 20:40:27 -0700 | [diff] [blame] | 449 | if (mWallpaperComponent != null) { |
| 450 | out.attribute(null, "component", |
| 451 | mWallpaperComponent.flattenToShortString()); |
| 452 | } |
| Joe Onorato | 9bb8fd7 | 2009-07-28 18:24:51 -0700 | [diff] [blame] | 453 | out.endTag(null, "wp"); |
| 454 | |
| 455 | out.endDocument(); |
| 456 | stream.close(); |
| 457 | journal.commit(); |
| 458 | } catch (IOException e) { |
| 459 | try { |
| 460 | if (stream != null) { |
| 461 | stream.close(); |
| 462 | } |
| 463 | } catch (IOException ex) { |
| 464 | // Ignore |
| 465 | } |
| 466 | journal.rollback(); |
| 467 | } |
| 468 | } |
| 469 | |
| 470 | private void loadSettingsLocked() { |
| 471 | JournaledFile journal = makeJournaledFile(); |
| 472 | FileInputStream stream = null; |
| 473 | File file = journal.chooseForRead(); |
| 474 | boolean success = false; |
| 475 | try { |
| 476 | stream = new FileInputStream(file); |
| 477 | XmlPullParser parser = Xml.newPullParser(); |
| 478 | parser.setInput(stream, null); |
| 479 | |
| 480 | int type; |
| Joe Onorato | 9bb8fd7 | 2009-07-28 18:24:51 -0700 | [diff] [blame] | 481 | do { |
| 482 | type = parser.next(); |
| 483 | if (type == XmlPullParser.START_TAG) { |
| 484 | String tag = parser.getName(); |
| 485 | if ("wp".equals(tag)) { |
| 486 | mWidth = Integer.parseInt(parser.getAttributeValue(null, "width")); |
| 487 | mHeight = Integer.parseInt(parser.getAttributeValue(null, "height")); |
| 488 | mName = parser.getAttributeValue(null, "name"); |
| Dianne Hackborn | 4c62fc0 | 2009-08-08 20:40:27 -0700 | [diff] [blame] | 489 | String comp = parser.getAttributeValue(null, "component"); |
| 490 | mWallpaperComponent = comp != null |
| 491 | ? ComponentName.unflattenFromString(comp) |
| 492 | : null; |
| Joe Onorato | 9bb8fd7 | 2009-07-28 18:24:51 -0700 | [diff] [blame] | 493 | } |
| 494 | } |
| 495 | } while (type != XmlPullParser.END_DOCUMENT); |
| 496 | success = true; |
| 497 | } catch (NullPointerException e) { |
| Joe Onorato | 2d9c9e3 | 2009-07-29 16:43:06 -0700 | [diff] [blame] | 498 | Log.w(TAG, "failed parsing " + file + " " + e); |
| Joe Onorato | 9bb8fd7 | 2009-07-28 18:24:51 -0700 | [diff] [blame] | 499 | } catch (NumberFormatException e) { |
| Joe Onorato | 2d9c9e3 | 2009-07-29 16:43:06 -0700 | [diff] [blame] | 500 | Log.w(TAG, "failed parsing " + file + " " + e); |
| Joe Onorato | 9bb8fd7 | 2009-07-28 18:24:51 -0700 | [diff] [blame] | 501 | } catch (XmlPullParserException e) { |
| Joe Onorato | 2d9c9e3 | 2009-07-29 16:43:06 -0700 | [diff] [blame] | 502 | Log.w(TAG, "failed parsing " + file + " " + e); |
| Joe Onorato | 9bb8fd7 | 2009-07-28 18:24:51 -0700 | [diff] [blame] | 503 | } catch (IOException e) { |
| Joe Onorato | 2d9c9e3 | 2009-07-29 16:43:06 -0700 | [diff] [blame] | 504 | Log.w(TAG, "failed parsing " + file + " " + e); |
| Joe Onorato | 9bb8fd7 | 2009-07-28 18:24:51 -0700 | [diff] [blame] | 505 | } catch (IndexOutOfBoundsException e) { |
| Joe Onorato | 2d9c9e3 | 2009-07-29 16:43:06 -0700 | [diff] [blame] | 506 | Log.w(TAG, "failed parsing " + file + " " + e); |
| Joe Onorato | 9bb8fd7 | 2009-07-28 18:24:51 -0700 | [diff] [blame] | 507 | } |
| 508 | try { |
| 509 | if (stream != null) { |
| 510 | stream.close(); |
| 511 | } |
| 512 | } catch (IOException e) { |
| 513 | // Ignore |
| 514 | } |
| 515 | |
| 516 | if (!success) { |
| 517 | mWidth = -1; |
| 518 | mHeight = -1; |
| 519 | mName = ""; |
| 520 | } |
| 521 | } |
| 522 | |
| 523 | void settingsRestored() { |
| 524 | boolean success = false; |
| 525 | synchronized (mLock) { |
| 526 | loadSettingsLocked(); |
| 527 | // If there's a wallpaper name, we use that. If that can't be loaded, then we |
| 528 | // use the default. |
| 529 | if ("".equals(mName)) { |
| 530 | success = true; |
| 531 | } else { |
| 532 | success = restoreNamedResourceLocked(); |
| 533 | } |
| 534 | } |
| 535 | |
| 536 | if (!success) { |
| 537 | Log.e(TAG, "Failed to restore wallpaper: '" + mName + "'"); |
| 538 | mName = ""; |
| 539 | WALLPAPER_FILE.delete(); |
| 540 | } |
| 541 | saveSettingsLocked(); |
| 542 | } |
| 543 | |
| 544 | boolean restoreNamedResourceLocked() { |
| 545 | if (mName.length() > 4 && "res:".equals(mName.substring(0, 4))) { |
| 546 | String resName = mName.substring(4); |
| 547 | |
| 548 | String pkg = null; |
| 549 | int colon = resName.indexOf(':'); |
| 550 | if (colon > 0) { |
| 551 | pkg = resName.substring(0, colon); |
| 552 | } |
| 553 | |
| 554 | String ident = null; |
| 555 | int slash = resName.lastIndexOf('/'); |
| 556 | if (slash > 0) { |
| 557 | ident = resName.substring(slash+1); |
| 558 | } |
| 559 | |
| 560 | String type = null; |
| 561 | if (colon > 0 && slash > 0 && (slash-colon) > 1) { |
| 562 | type = resName.substring(colon+1, slash); |
| 563 | } |
| 564 | |
| 565 | if (pkg != null && ident != null && type != null) { |
| 566 | int resId = -1; |
| 567 | InputStream res = null; |
| 568 | FileOutputStream fos = null; |
| 569 | try { |
| 570 | Context c = mContext.createPackageContext(pkg, Context.CONTEXT_RESTRICTED); |
| 571 | Resources r = c.getResources(); |
| 572 | resId = r.getIdentifier(resName, null, null); |
| 573 | if (resId == 0) { |
| 574 | Log.e(TAG, "couldn't resolve identifier pkg=" + pkg + " type=" + type |
| 575 | + " ident=" + ident); |
| 576 | return false; |
| 577 | } |
| 578 | |
| 579 | res = r.openRawResource(resId); |
| 580 | fos = new FileOutputStream(WALLPAPER_FILE); |
| 581 | |
| 582 | byte[] buffer = new byte[32768]; |
| 583 | int amt; |
| 584 | while ((amt=res.read(buffer)) > 0) { |
| 585 | fos.write(buffer, 0, amt); |
| 586 | } |
| 587 | // mWallpaperObserver will notice the close and send the change broadcast |
| 588 | |
| 589 | Log.d(TAG, "Restored wallpaper: " + resName); |
| 590 | return true; |
| 591 | } catch (NameNotFoundException e) { |
| 592 | Log.e(TAG, "Package name " + pkg + " not found"); |
| 593 | } catch (Resources.NotFoundException e) { |
| 594 | Log.e(TAG, "Resource not found: " + resId); |
| 595 | } catch (IOException e) { |
| 596 | Log.e(TAG, "IOException while restoring wallpaper ", e); |
| 597 | } finally { |
| 598 | if (res != null) { |
| 599 | try { |
| 600 | res.close(); |
| 601 | } catch (IOException ex) {} |
| 602 | } |
| 603 | if (fos != null) { |
| 604 | try { |
| 605 | fos.close(); |
| 606 | } catch (IOException ex) {} |
| 607 | } |
| 608 | } |
| 609 | } |
| 610 | } |
| 611 | return false; |
| 612 | } |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 613 | } |