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