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