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