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