| 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); |
| Dianne Hackborn | c9421ba | 2010-03-11 22:23:46 -0800 | [diff] [blame] | 343 | } catch (IllegalArgumentException e) { |
| 344 | // This can happen if the default wallpaper component doesn't |
| 345 | // exist. This should be a system configuration problem, but |
| 346 | // let's not let it crash the system and just live with no |
| 347 | // wallpaper. |
| 348 | Slog.e(TAG, "Default wallpaper component not found!", e); |
| Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 349 | } finally { |
| 350 | Binder.restoreCallingIdentity(ident); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 351 | } |
| 352 | } |
| 353 | |
| 354 | public void setDimensionHints(int width, int height) throws RemoteException { |
| 355 | checkPermission(android.Manifest.permission.SET_WALLPAPER_HINTS); |
| 356 | |
| 357 | if (width <= 0 || height <= 0) { |
| 358 | throw new IllegalArgumentException("width and height must be > 0"); |
| 359 | } |
| 360 | |
| Joe Onorato | 9bb8fd7 | 2009-07-28 18:24:51 -0700 | [diff] [blame] | 361 | synchronized (mLock) { |
| 362 | if (width != mWidth || height != mHeight) { |
| 363 | mWidth = width; |
| 364 | mHeight = height; |
| 365 | saveSettingsLocked(); |
| Dianne Hackborn | 284ac93 | 2009-08-28 10:34:25 -0700 | [diff] [blame] | 366 | if (mWallpaperConnection != null) { |
| 367 | if (mWallpaperConnection.mEngine != null) { |
| 368 | try { |
| 369 | mWallpaperConnection.mEngine.setDesiredSize( |
| 370 | width, height); |
| 371 | } catch (RemoteException e) { |
| 372 | } |
| 373 | notifyCallbacksLocked(); |
| 374 | } |
| 375 | } |
| Joe Onorato | 9bb8fd7 | 2009-07-28 18:24:51 -0700 | [diff] [blame] | 376 | } |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 377 | } |
| 378 | } |
| 379 | |
| 380 | public int getWidthHint() throws RemoteException { |
| Joe Onorato | 9bb8fd7 | 2009-07-28 18:24:51 -0700 | [diff] [blame] | 381 | synchronized (mLock) { |
| 382 | return mWidth; |
| 383 | } |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 384 | } |
| 385 | |
| 386 | public int getHeightHint() throws RemoteException { |
| Joe Onorato | 9bb8fd7 | 2009-07-28 18:24:51 -0700 | [diff] [blame] | 387 | synchronized (mLock) { |
| 388 | return mHeight; |
| 389 | } |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 390 | } |
| 391 | |
| Dianne Hackborn | 284ac93 | 2009-08-28 10:34:25 -0700 | [diff] [blame] | 392 | public ParcelFileDescriptor getWallpaper(IWallpaperManagerCallback cb, |
| 393 | Bundle outParams) { |
| Joe Onorato | 9bb8fd7 | 2009-07-28 18:24:51 -0700 | [diff] [blame] | 394 | synchronized (mLock) { |
| 395 | try { |
| Dianne Hackborn | 284ac93 | 2009-08-28 10:34:25 -0700 | [diff] [blame] | 396 | if (outParams != null) { |
| 397 | outParams.putInt("width", mWidth); |
| 398 | outParams.putInt("height", mHeight); |
| 399 | } |
| Joe Onorato | 9bb8fd7 | 2009-07-28 18:24:51 -0700 | [diff] [blame] | 400 | mCallbacks.register(cb); |
| 401 | File f = WALLPAPER_FILE; |
| 402 | if (!f.exists()) { |
| 403 | return null; |
| 404 | } |
| 405 | return ParcelFileDescriptor.open(f, MODE_READ_ONLY); |
| 406 | } catch (FileNotFoundException e) { |
| 407 | /* Shouldn't happen as we check to see if the file exists */ |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 408 | Slog.w(TAG, "Error getting wallpaper", e); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 409 | } |
| Joe Onorato | 9bb8fd7 | 2009-07-28 18:24:51 -0700 | [diff] [blame] | 410 | return null; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 411 | } |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 412 | } |
| 413 | |
| Dianne Hackborn | eb03465 | 2009-09-07 00:49:58 -0700 | [diff] [blame] | 414 | public WallpaperInfo getWallpaperInfo() { |
| 415 | synchronized (mLock) { |
| 416 | if (mWallpaperConnection != null) { |
| 417 | return mWallpaperConnection.mInfo; |
| 418 | } |
| 419 | return null; |
| 420 | } |
| 421 | } |
| 422 | |
| Joe Onorato | 9bb8fd7 | 2009-07-28 18:24:51 -0700 | [diff] [blame] | 423 | public ParcelFileDescriptor setWallpaper(String name) { |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 424 | if (DEBUG) Slog.v(TAG, "setWallpaper"); |
| Mike Cleron | a428b2c | 2009-11-15 22:53:08 -0800 | [diff] [blame] | 425 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 426 | checkPermission(android.Manifest.permission.SET_WALLPAPER); |
| Joe Onorato | 9bb8fd7 | 2009-07-28 18:24:51 -0700 | [diff] [blame] | 427 | synchronized (mLock) { |
| Dianne Hackborn | 0cd4887 | 2009-08-13 18:51:59 -0700 | [diff] [blame] | 428 | final long ident = Binder.clearCallingIdentity(); |
| 429 | try { |
| 430 | ParcelFileDescriptor pfd = updateWallpaperBitmapLocked(name); |
| 431 | if (pfd != null) { |
| Mike Cleron | 322b6ee | 2009-11-12 07:45:47 -0800 | [diff] [blame] | 432 | // Bind the wallpaper to an ImageWallpaper |
| Mike Cleron | a428b2c | 2009-11-15 22:53:08 -0800 | [diff] [blame] | 433 | bindWallpaperComponentLocked(mImageWallpaperComponent); |
| Dianne Hackborn | 0cd4887 | 2009-08-13 18:51:59 -0700 | [diff] [blame] | 434 | saveSettingsLocked(); |
| 435 | } |
| 436 | return pfd; |
| 437 | } finally { |
| 438 | Binder.restoreCallingIdentity(ident); |
| Joe Onorato | 9bb8fd7 | 2009-07-28 18:24:51 -0700 | [diff] [blame] | 439 | } |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 440 | } |
| 441 | } |
| 442 | |
| Dianne Hackborn | 4c62fc0 | 2009-08-08 20:40:27 -0700 | [diff] [blame] | 443 | ParcelFileDescriptor updateWallpaperBitmapLocked(String name) { |
| 444 | if (name == null) name = ""; |
| 445 | try { |
| 446 | ParcelFileDescriptor fd = ParcelFileDescriptor.open(WALLPAPER_FILE, |
| 447 | MODE_CREATE|MODE_READ_WRITE); |
| 448 | mName = name; |
| 449 | return fd; |
| 450 | } catch (FileNotFoundException e) { |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 451 | Slog.w(TAG, "Error setting wallpaper", e); |
| Dianne Hackborn | 4c62fc0 | 2009-08-08 20:40:27 -0700 | [diff] [blame] | 452 | } |
| 453 | return null; |
| 454 | } |
| 455 | |
| 456 | public void setWallpaperComponent(ComponentName name) { |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 457 | if (DEBUG) Slog.v(TAG, "setWallpaperComponent name=" + name); |
| Dianne Hackborn | 4c62fc0 | 2009-08-08 20:40:27 -0700 | [diff] [blame] | 458 | checkPermission(android.Manifest.permission.SET_WALLPAPER_COMPONENT); |
| 459 | synchronized (mLock) { |
| 460 | final long ident = Binder.clearCallingIdentity(); |
| 461 | try { |
| Mike Cleron | a428b2c | 2009-11-15 22:53:08 -0800 | [diff] [blame] | 462 | bindWallpaperComponentLocked(name); |
| Dianne Hackborn | f21adf6 | 2009-08-13 10:20:21 -0700 | [diff] [blame] | 463 | } finally { |
| 464 | Binder.restoreCallingIdentity(ident); |
| 465 | } |
| 466 | } |
| 467 | } |
| 468 | |
| Mike Cleron | a428b2c | 2009-11-15 22:53:08 -0800 | [diff] [blame] | 469 | void bindWallpaperComponentLocked(ComponentName componentName) { |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 470 | if (DEBUG) Slog.v(TAG, "bindWallpaperComponentLocked: componentName=" + componentName); |
| Mike Cleron | a428b2c | 2009-11-15 22:53:08 -0800 | [diff] [blame] | 471 | |
| Dianne Hackborn | f21adf6 | 2009-08-13 10:20:21 -0700 | [diff] [blame] | 472 | // Has the component changed? |
| 473 | if (mWallpaperConnection != null) { |
| 474 | if (mWallpaperComponent == null) { |
| Mike Cleron | 322b6ee | 2009-11-12 07:45:47 -0800 | [diff] [blame] | 475 | if (componentName == null) { |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 476 | if (DEBUG) Slog.v(TAG, "bindWallpaperComponentLocked: still using default"); |
| Dianne Hackborn | f21adf6 | 2009-08-13 10:20:21 -0700 | [diff] [blame] | 477 | // Still using default wallpaper. |
| 478 | return; |
| Dianne Hackborn | 4c62fc0 | 2009-08-08 20:40:27 -0700 | [diff] [blame] | 479 | } |
| Mike Cleron | 322b6ee | 2009-11-12 07:45:47 -0800 | [diff] [blame] | 480 | } else if (mWallpaperComponent.equals(componentName)) { |
| Dianne Hackborn | f21adf6 | 2009-08-13 10:20:21 -0700 | [diff] [blame] | 481 | // Changing to same wallpaper. |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 482 | if (DEBUG) Slog.v(TAG, "same wallpaper"); |
| Dianne Hackborn | f21adf6 | 2009-08-13 10:20:21 -0700 | [diff] [blame] | 483 | return; |
| 484 | } |
| 485 | } |
| 486 | |
| 487 | try { |
| Mike Cleron | a428b2c | 2009-11-15 22:53:08 -0800 | [diff] [blame] | 488 | if (componentName == null) { |
| Mike Cleron | 322b6ee | 2009-11-12 07:45:47 -0800 | [diff] [blame] | 489 | String defaultComponent = |
| 490 | mContext.getString(com.android.internal.R.string.default_wallpaper_component); |
| Mike Cleron | a428b2c | 2009-11-15 22:53:08 -0800 | [diff] [blame] | 491 | if (defaultComponent != null) { |
| Mike Cleron | 322b6ee | 2009-11-12 07:45:47 -0800 | [diff] [blame] | 492 | // See if there is a default wallpaper component specified |
| Mike Cleron | a428b2c | 2009-11-15 22:53:08 -0800 | [diff] [blame] | 493 | componentName = ComponentName.unflattenFromString(defaultComponent); |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 494 | if (DEBUG) Slog.v(TAG, "Use default component wallpaper:" + componentName); |
| Mike Cleron | 322b6ee | 2009-11-12 07:45:47 -0800 | [diff] [blame] | 495 | } |
| Mike Cleron | a428b2c | 2009-11-15 22:53:08 -0800 | [diff] [blame] | 496 | if (componentName == null) { |
| Mike Cleron | 322b6ee | 2009-11-12 07:45:47 -0800 | [diff] [blame] | 497 | // Fall back to static image wallpaper |
| Mike Cleron | a428b2c | 2009-11-15 22:53:08 -0800 | [diff] [blame] | 498 | componentName = mImageWallpaperComponent; |
| Mike Cleron | 322b6ee | 2009-11-12 07:45:47 -0800 | [diff] [blame] | 499 | //clearWallpaperComponentLocked(); |
| 500 | //return; |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 501 | if (DEBUG) Slog.v(TAG, "Using image wallpaper"); |
| Mike Cleron | 322b6ee | 2009-11-12 07:45:47 -0800 | [diff] [blame] | 502 | } |
| Dianne Hackborn | f21adf6 | 2009-08-13 10:20:21 -0700 | [diff] [blame] | 503 | } |
| Mike Cleron | a428b2c | 2009-11-15 22:53:08 -0800 | [diff] [blame] | 504 | ServiceInfo si = mContext.getPackageManager().getServiceInfo(componentName, |
| Dianne Hackborn | f21adf6 | 2009-08-13 10:20:21 -0700 | [diff] [blame] | 505 | PackageManager.GET_META_DATA | PackageManager.GET_PERMISSIONS); |
| 506 | if (!android.Manifest.permission.BIND_WALLPAPER.equals(si.permission)) { |
| 507 | throw new SecurityException("Selected service does not require " |
| 508 | + android.Manifest.permission.BIND_WALLPAPER |
| Mike Cleron | a428b2c | 2009-11-15 22:53:08 -0800 | [diff] [blame] | 509 | + ": " + componentName); |
| Dianne Hackborn | f21adf6 | 2009-08-13 10:20:21 -0700 | [diff] [blame] | 510 | } |
| 511 | |
| Dianne Hackborn | eb03465 | 2009-09-07 00:49:58 -0700 | [diff] [blame] | 512 | WallpaperInfo wi = null; |
| 513 | |
| Dianne Hackborn | f21adf6 | 2009-08-13 10:20:21 -0700 | [diff] [blame] | 514 | Intent intent = new Intent(WallpaperService.SERVICE_INTERFACE); |
| Mike Cleron | a428b2c | 2009-11-15 22:53:08 -0800 | [diff] [blame] | 515 | if (componentName != null && !componentName.equals(mImageWallpaperComponent)) { |
| Dianne Hackborn | 4c62fc0 | 2009-08-08 20:40:27 -0700 | [diff] [blame] | 516 | // Make sure the selected service is actually a wallpaper service. |
| Dianne Hackborn | 4c62fc0 | 2009-08-08 20:40:27 -0700 | [diff] [blame] | 517 | List<ResolveInfo> ris = mContext.getPackageManager() |
| Dianne Hackborn | eb03465 | 2009-09-07 00:49:58 -0700 | [diff] [blame] | 518 | .queryIntentServices(intent, PackageManager.GET_META_DATA); |
| Dianne Hackborn | 4c62fc0 | 2009-08-08 20:40:27 -0700 | [diff] [blame] | 519 | for (int i=0; i<ris.size(); i++) { |
| 520 | ServiceInfo rsi = ris.get(i).serviceInfo; |
| 521 | if (rsi.name.equals(si.name) && |
| 522 | rsi.packageName.equals(si.packageName)) { |
| Dianne Hackborn | eb03465 | 2009-09-07 00:49:58 -0700 | [diff] [blame] | 523 | try { |
| 524 | wi = new WallpaperInfo(mContext, ris.get(i)); |
| 525 | } catch (XmlPullParserException e) { |
| 526 | throw new IllegalArgumentException(e); |
| 527 | } catch (IOException e) { |
| 528 | throw new IllegalArgumentException(e); |
| 529 | } |
| Dianne Hackborn | 4c62fc0 | 2009-08-08 20:40:27 -0700 | [diff] [blame] | 530 | break; |
| 531 | } |
| 532 | } |
| Dianne Hackborn | eb03465 | 2009-09-07 00:49:58 -0700 | [diff] [blame] | 533 | if (wi == null) { |
| Dianne Hackborn | 4c62fc0 | 2009-08-08 20:40:27 -0700 | [diff] [blame] | 534 | throw new SecurityException("Selected service is not a wallpaper: " |
| Mike Cleron | a428b2c | 2009-11-15 22:53:08 -0800 | [diff] [blame] | 535 | + componentName); |
| Dianne Hackborn | 4c62fc0 | 2009-08-08 20:40:27 -0700 | [diff] [blame] | 536 | } |
| Dianne Hackborn | 4c62fc0 | 2009-08-08 20:40:27 -0700 | [diff] [blame] | 537 | } |
| Dianne Hackborn | f21adf6 | 2009-08-13 10:20:21 -0700 | [diff] [blame] | 538 | |
| 539 | // Bind the service! |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 540 | if (DEBUG) Slog.v(TAG, "Binding to:" + componentName); |
| Dianne Hackborn | eb03465 | 2009-09-07 00:49:58 -0700 | [diff] [blame] | 541 | WallpaperConnection newConn = new WallpaperConnection(wi); |
| Mike Cleron | a428b2c | 2009-11-15 22:53:08 -0800 | [diff] [blame] | 542 | intent.setComponent(componentName); |
| Dianne Hackborn | dd9b82c | 2009-09-03 00:18:47 -0700 | [diff] [blame] | 543 | intent.putExtra(Intent.EXTRA_CLIENT_LABEL, |
| 544 | com.android.internal.R.string.wallpaper_binding_label); |
| 545 | intent.putExtra(Intent.EXTRA_CLIENT_INTENT, PendingIntent.getActivity( |
| Dianne Hackborn | eb03465 | 2009-09-07 00:49:58 -0700 | [diff] [blame] | 546 | mContext, 0, |
| 547 | Intent.createChooser(new Intent(Intent.ACTION_SET_WALLPAPER), |
| 548 | mContext.getText(com.android.internal.R.string.chooser_wallpaper)), |
| 549 | 0)); |
| Dianne Hackborn | f21adf6 | 2009-08-13 10:20:21 -0700 | [diff] [blame] | 550 | if (!mContext.bindService(intent, newConn, |
| 551 | Context.BIND_AUTO_CREATE)) { |
| 552 | throw new IllegalArgumentException("Unable to bind service: " |
| Mike Cleron | 322b6ee | 2009-11-12 07:45:47 -0800 | [diff] [blame] | 553 | + componentName); |
| Dianne Hackborn | f21adf6 | 2009-08-13 10:20:21 -0700 | [diff] [blame] | 554 | } |
| 555 | |
| 556 | clearWallpaperComponentLocked(); |
| Mike Cleron | 322b6ee | 2009-11-12 07:45:47 -0800 | [diff] [blame] | 557 | mWallpaperComponent = componentName; |
| Dianne Hackborn | f21adf6 | 2009-08-13 10:20:21 -0700 | [diff] [blame] | 558 | mWallpaperConnection = newConn; |
| Dianne Hackborn | 0cd4887 | 2009-08-13 18:51:59 -0700 | [diff] [blame] | 559 | mLastDiedTime = SystemClock.uptimeMillis(); |
| Dianne Hackborn | f21adf6 | 2009-08-13 10:20:21 -0700 | [diff] [blame] | 560 | try { |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 561 | if (DEBUG) Slog.v(TAG, "Adding window token: " + newConn.mToken); |
| Dianne Hackborn | f21adf6 | 2009-08-13 10:20:21 -0700 | [diff] [blame] | 562 | mIWindowManager.addWindowToken(newConn.mToken, |
| 563 | WindowManager.LayoutParams.TYPE_WALLPAPER); |
| 564 | } catch (RemoteException e) { |
| 565 | } |
| 566 | |
| 567 | } catch (PackageManager.NameNotFoundException e) { |
| Mike Cleron | 322b6ee | 2009-11-12 07:45:47 -0800 | [diff] [blame] | 568 | throw new IllegalArgumentException("Unknown component " + componentName); |
| Dianne Hackborn | 4c62fc0 | 2009-08-08 20:40:27 -0700 | [diff] [blame] | 569 | } |
| 570 | } |
| 571 | |
| 572 | void clearWallpaperComponentLocked() { |
| 573 | mWallpaperComponent = null; |
| 574 | if (mWallpaperConnection != null) { |
| 575 | if (mWallpaperConnection.mEngine != null) { |
| 576 | try { |
| 577 | mWallpaperConnection.mEngine.destroy(); |
| 578 | } catch (RemoteException e) { |
| 579 | } |
| 580 | } |
| 581 | mContext.unbindService(mWallpaperConnection); |
| Dianne Hackborn | e9e9bca | 2009-08-18 15:08:22 -0700 | [diff] [blame] | 582 | try { |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 583 | if (DEBUG) Slog.v(TAG, "Removing window token: " |
| Dianne Hackborn | e9e9bca | 2009-08-18 15:08:22 -0700 | [diff] [blame] | 584 | + mWallpaperConnection.mToken); |
| 585 | mIWindowManager.removeWindowToken(mWallpaperConnection.mToken); |
| 586 | } catch (RemoteException e) { |
| 587 | } |
| Dianne Hackborn | 4c62fc0 | 2009-08-08 20:40:27 -0700 | [diff] [blame] | 588 | mWallpaperConnection = null; |
| 589 | } |
| 590 | } |
| 591 | |
| 592 | void attachServiceLocked(WallpaperConnection conn) { |
| 593 | try { |
| Dianne Hackborn | 3be63c0 | 2009-08-20 19:31:38 -0700 | [diff] [blame] | 594 | conn.mService.attach(conn, conn.mToken, |
| 595 | WindowManager.LayoutParams.TYPE_WALLPAPER, false, |
| 596 | mWidth, mHeight); |
| Dianne Hackborn | 4c62fc0 | 2009-08-08 20:40:27 -0700 | [diff] [blame] | 597 | } catch (RemoteException e) { |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 598 | Slog.w(TAG, "Failed attaching wallpaper; clearing", e); |
| Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 599 | if (!mWallpaperUpdating) { |
| 600 | bindWallpaperComponentLocked(null); |
| 601 | } |
| Dianne Hackborn | 4c62fc0 | 2009-08-08 20:40:27 -0700 | [diff] [blame] | 602 | } |
| 603 | } |
| 604 | |
| Joe Onorato | 9bb8fd7 | 2009-07-28 18:24:51 -0700 | [diff] [blame] | 605 | private void notifyCallbacksLocked() { |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 606 | final int n = mCallbacks.beginBroadcast(); |
| 607 | for (int i = 0; i < n; i++) { |
| 608 | try { |
| 609 | mCallbacks.getBroadcastItem(i).onWallpaperChanged(); |
| 610 | } catch (RemoteException e) { |
| 611 | |
| 612 | // The RemoteCallbackList will take care of removing |
| 613 | // the dead object for us. |
| 614 | } |
| 615 | } |
| 616 | mCallbacks.finishBroadcast(); |
| 617 | final Intent intent = new Intent(Intent.ACTION_WALLPAPER_CHANGED); |
| 618 | mContext.sendBroadcast(intent); |
| 619 | } |
| 620 | |
| 621 | private void checkPermission(String permission) { |
| Joe Onorato | 9bb8fd7 | 2009-07-28 18:24:51 -0700 | [diff] [blame] | 622 | if (PackageManager.PERMISSION_GRANTED!= mContext.checkCallingOrSelfPermission(permission)) { |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 623 | throw new SecurityException("Access denied to process: " + Binder.getCallingPid() |
| 624 | + ", must have permission " + permission); |
| 625 | } |
| 626 | } |
| Joe Onorato | 9bb8fd7 | 2009-07-28 18:24:51 -0700 | [diff] [blame] | 627 | |
| 628 | private static JournaledFile makeJournaledFile() { |
| 629 | final String base = "/data/system/wallpaper_info.xml"; |
| 630 | return new JournaledFile(new File(base), new File(base + ".tmp")); |
| 631 | } |
| 632 | |
| 633 | private void saveSettingsLocked() { |
| 634 | JournaledFile journal = makeJournaledFile(); |
| 635 | FileOutputStream stream = null; |
| 636 | try { |
| 637 | stream = new FileOutputStream(journal.chooseForWrite(), false); |
| 638 | XmlSerializer out = new FastXmlSerializer(); |
| 639 | out.setOutput(stream, "utf-8"); |
| 640 | out.startDocument(null, true); |
| 641 | |
| 642 | out.startTag(null, "wp"); |
| 643 | out.attribute(null, "width", Integer.toString(mWidth)); |
| 644 | out.attribute(null, "height", Integer.toString(mHeight)); |
| 645 | out.attribute(null, "name", mName); |
| Dianne Hackborn | 4c62fc0 | 2009-08-08 20:40:27 -0700 | [diff] [blame] | 646 | if (mWallpaperComponent != null) { |
| 647 | out.attribute(null, "component", |
| 648 | mWallpaperComponent.flattenToShortString()); |
| 649 | } |
| Joe Onorato | 9bb8fd7 | 2009-07-28 18:24:51 -0700 | [diff] [blame] | 650 | out.endTag(null, "wp"); |
| 651 | |
| 652 | out.endDocument(); |
| 653 | stream.close(); |
| 654 | journal.commit(); |
| 655 | } catch (IOException e) { |
| 656 | try { |
| 657 | if (stream != null) { |
| 658 | stream.close(); |
| 659 | } |
| 660 | } catch (IOException ex) { |
| 661 | // Ignore |
| 662 | } |
| 663 | journal.rollback(); |
| 664 | } |
| 665 | } |
| 666 | |
| 667 | private void loadSettingsLocked() { |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 668 | if (DEBUG) Slog.v(TAG, "loadSettingsLocked"); |
| Mike Cleron | a428b2c | 2009-11-15 22:53:08 -0800 | [diff] [blame] | 669 | |
| Joe Onorato | 9bb8fd7 | 2009-07-28 18:24:51 -0700 | [diff] [blame] | 670 | JournaledFile journal = makeJournaledFile(); |
| 671 | FileInputStream stream = null; |
| 672 | File file = journal.chooseForRead(); |
| 673 | boolean success = false; |
| 674 | try { |
| 675 | stream = new FileInputStream(file); |
| 676 | XmlPullParser parser = Xml.newPullParser(); |
| 677 | parser.setInput(stream, null); |
| 678 | |
| 679 | int type; |
| Joe Onorato | 9bb8fd7 | 2009-07-28 18:24:51 -0700 | [diff] [blame] | 680 | do { |
| 681 | type = parser.next(); |
| 682 | if (type == XmlPullParser.START_TAG) { |
| 683 | String tag = parser.getName(); |
| 684 | if ("wp".equals(tag)) { |
| 685 | mWidth = Integer.parseInt(parser.getAttributeValue(null, "width")); |
| 686 | mHeight = Integer.parseInt(parser.getAttributeValue(null, "height")); |
| 687 | mName = parser.getAttributeValue(null, "name"); |
| Dianne Hackborn | 4c62fc0 | 2009-08-08 20:40:27 -0700 | [diff] [blame] | 688 | String comp = parser.getAttributeValue(null, "component"); |
| Mike Cleron | a428b2c | 2009-11-15 22:53:08 -0800 | [diff] [blame] | 689 | mNextWallpaperComponent = comp != null |
| Dianne Hackborn | 4c62fc0 | 2009-08-08 20:40:27 -0700 | [diff] [blame] | 690 | ? ComponentName.unflattenFromString(comp) |
| 691 | : null; |
| Mike Cleron | a428b2c | 2009-11-15 22:53:08 -0800 | [diff] [blame] | 692 | |
| 693 | if (DEBUG) { |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 694 | Slog.v(TAG, "mWidth:" + mWidth); |
| 695 | Slog.v(TAG, "mHeight:" + mHeight); |
| 696 | Slog.v(TAG, "mName:" + mName); |
| 697 | Slog.v(TAG, "mNextWallpaperComponent:" + mNextWallpaperComponent); |
| Mike Cleron | a428b2c | 2009-11-15 22:53:08 -0800 | [diff] [blame] | 698 | } |
| Joe Onorato | 9bb8fd7 | 2009-07-28 18:24:51 -0700 | [diff] [blame] | 699 | } |
| 700 | } |
| 701 | } while (type != XmlPullParser.END_DOCUMENT); |
| 702 | success = true; |
| 703 | } catch (NullPointerException 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 (NumberFormatException 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 | } catch (XmlPullParserException e) { |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 708 | Slog.w(TAG, "failed parsing " + file + " " + e); |
| Joe Onorato | 9bb8fd7 | 2009-07-28 18:24:51 -0700 | [diff] [blame] | 709 | } catch (IOException e) { |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 710 | Slog.w(TAG, "failed parsing " + file + " " + e); |
| Joe Onorato | 9bb8fd7 | 2009-07-28 18:24:51 -0700 | [diff] [blame] | 711 | } catch (IndexOutOfBoundsException e) { |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 712 | Slog.w(TAG, "failed parsing " + file + " " + e); |
| Joe Onorato | 9bb8fd7 | 2009-07-28 18:24:51 -0700 | [diff] [blame] | 713 | } |
| 714 | try { |
| 715 | if (stream != null) { |
| 716 | stream.close(); |
| 717 | } |
| 718 | } catch (IOException e) { |
| 719 | // Ignore |
| 720 | } |
| 721 | |
| 722 | if (!success) { |
| 723 | mWidth = -1; |
| 724 | mHeight = -1; |
| 725 | mName = ""; |
| 726 | } |
| 727 | } |
| 728 | |
| 729 | void settingsRestored() { |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 730 | if (DEBUG) Slog.v(TAG, "settingsRestored"); |
| Mike Cleron | a428b2c | 2009-11-15 22:53:08 -0800 | [diff] [blame] | 731 | |
| Joe Onorato | 9bb8fd7 | 2009-07-28 18:24:51 -0700 | [diff] [blame] | 732 | boolean success = false; |
| 733 | synchronized (mLock) { |
| 734 | loadSettingsLocked(); |
| Mike Cleron | a428b2c | 2009-11-15 22:53:08 -0800 | [diff] [blame] | 735 | if (mNextWallpaperComponent != null && |
| 736 | !mNextWallpaperComponent.equals(mImageWallpaperComponent)) { |
| Christopher Tate | e3ab4d0 | 2009-12-16 14:03:31 -0800 | [diff] [blame] | 737 | try { |
| 738 | bindWallpaperComponentLocked(mNextWallpaperComponent); |
| 739 | } catch (IllegalArgumentException e) { |
| 740 | // No such live wallpaper or other failure; fall back to the default |
| 741 | // live wallpaper (since the profile being restored indicated that the |
| 742 | // user had selected a live rather than static one). |
| 743 | bindWallpaperComponentLocked(null); |
| 744 | } |
| Joe Onorato | 9bb8fd7 | 2009-07-28 18:24:51 -0700 | [diff] [blame] | 745 | success = true; |
| 746 | } else { |
| Mike Cleron | a428b2c | 2009-11-15 22:53:08 -0800 | [diff] [blame] | 747 | // If there's a wallpaper name, we use that. If that can't be loaded, then we |
| 748 | // use the default. |
| 749 | if ("".equals(mName)) { |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 750 | if (DEBUG) Slog.v(TAG, "settingsRestored: name is empty"); |
| Mike Cleron | a428b2c | 2009-11-15 22:53:08 -0800 | [diff] [blame] | 751 | success = true; |
| 752 | } else { |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 753 | if (DEBUG) Slog.v(TAG, "settingsRestored: attempting to restore named resource"); |
| Mike Cleron | a428b2c | 2009-11-15 22:53:08 -0800 | [diff] [blame] | 754 | success = restoreNamedResourceLocked(); |
| 755 | } |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 756 | if (DEBUG) Slog.v(TAG, "settingsRestored: success=" + success); |
| Mike Cleron | a428b2c | 2009-11-15 22:53:08 -0800 | [diff] [blame] | 757 | if (success) { |
| 758 | bindWallpaperComponentLocked(mImageWallpaperComponent); |
| 759 | } |
| Joe Onorato | 9bb8fd7 | 2009-07-28 18:24:51 -0700 | [diff] [blame] | 760 | } |
| 761 | } |
| 762 | |
| 763 | if (!success) { |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 764 | Slog.e(TAG, "Failed to restore wallpaper: '" + mName + "'"); |
| Joe Onorato | 9bb8fd7 | 2009-07-28 18:24:51 -0700 | [diff] [blame] | 765 | mName = ""; |
| 766 | WALLPAPER_FILE.delete(); |
| 767 | } |
| 768 | saveSettingsLocked(); |
| 769 | } |
| 770 | |
| 771 | boolean restoreNamedResourceLocked() { |
| 772 | if (mName.length() > 4 && "res:".equals(mName.substring(0, 4))) { |
| 773 | String resName = mName.substring(4); |
| 774 | |
| 775 | String pkg = null; |
| 776 | int colon = resName.indexOf(':'); |
| 777 | if (colon > 0) { |
| 778 | pkg = resName.substring(0, colon); |
| 779 | } |
| 780 | |
| 781 | String ident = null; |
| 782 | int slash = resName.lastIndexOf('/'); |
| 783 | if (slash > 0) { |
| 784 | ident = resName.substring(slash+1); |
| 785 | } |
| 786 | |
| 787 | String type = null; |
| 788 | if (colon > 0 && slash > 0 && (slash-colon) > 1) { |
| 789 | type = resName.substring(colon+1, slash); |
| 790 | } |
| 791 | |
| 792 | if (pkg != null && ident != null && type != null) { |
| 793 | int resId = -1; |
| 794 | InputStream res = null; |
| 795 | FileOutputStream fos = null; |
| 796 | try { |
| 797 | Context c = mContext.createPackageContext(pkg, Context.CONTEXT_RESTRICTED); |
| 798 | Resources r = c.getResources(); |
| 799 | resId = r.getIdentifier(resName, null, null); |
| 800 | if (resId == 0) { |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 801 | Slog.e(TAG, "couldn't resolve identifier pkg=" + pkg + " type=" + type |
| Joe Onorato | 9bb8fd7 | 2009-07-28 18:24:51 -0700 | [diff] [blame] | 802 | + " ident=" + ident); |
| 803 | return false; |
| 804 | } |
| 805 | |
| 806 | res = r.openRawResource(resId); |
| 807 | fos = new FileOutputStream(WALLPAPER_FILE); |
| 808 | |
| 809 | byte[] buffer = new byte[32768]; |
| 810 | int amt; |
| 811 | while ((amt=res.read(buffer)) > 0) { |
| 812 | fos.write(buffer, 0, amt); |
| 813 | } |
| 814 | // mWallpaperObserver will notice the close and send the change broadcast |
| 815 | |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 816 | Slog.v(TAG, "Restored wallpaper: " + resName); |
| Joe Onorato | 9bb8fd7 | 2009-07-28 18:24:51 -0700 | [diff] [blame] | 817 | return true; |
| 818 | } catch (NameNotFoundException e) { |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 819 | Slog.e(TAG, "Package name " + pkg + " not found"); |
| Joe Onorato | 9bb8fd7 | 2009-07-28 18:24:51 -0700 | [diff] [blame] | 820 | } catch (Resources.NotFoundException e) { |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 821 | Slog.e(TAG, "Resource not found: " + resId); |
| Joe Onorato | 9bb8fd7 | 2009-07-28 18:24:51 -0700 | [diff] [blame] | 822 | } catch (IOException e) { |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 823 | Slog.e(TAG, "IOException while restoring wallpaper ", e); |
| Joe Onorato | 9bb8fd7 | 2009-07-28 18:24:51 -0700 | [diff] [blame] | 824 | } finally { |
| 825 | if (res != null) { |
| 826 | try { |
| 827 | res.close(); |
| 828 | } catch (IOException ex) {} |
| 829 | } |
| 830 | if (fos != null) { |
| 831 | try { |
| 832 | fos.close(); |
| 833 | } catch (IOException ex) {} |
| 834 | } |
| 835 | } |
| 836 | } |
| 837 | } |
| 838 | return false; |
| 839 | } |
| Dianne Hackborn | eb03465 | 2009-09-07 00:49:58 -0700 | [diff] [blame] | 840 | |
| 841 | @Override |
| 842 | protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) { |
| 843 | if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DUMP) |
| 844 | != PackageManager.PERMISSION_GRANTED) { |
| 845 | |
| 846 | pw.println("Permission Denial: can't dump wallpaper service from from pid=" |
| 847 | + Binder.getCallingPid() |
| 848 | + ", uid=" + Binder.getCallingUid()); |
| 849 | return; |
| 850 | } |
| 851 | |
| 852 | synchronized (mLock) { |
| 853 | pw.println("Current Wallpaper Service state:"); |
| 854 | pw.print(" mWidth="); pw.print(mWidth); |
| 855 | pw.print(" mHeight="); pw.println(mHeight); |
| 856 | pw.print(" mName="); pw.println(mName); |
| 857 | pw.print(" mWallpaperComponent="); pw.println(mWallpaperComponent); |
| 858 | if (mWallpaperConnection != null) { |
| 859 | WallpaperConnection conn = mWallpaperConnection; |
| 860 | pw.print(" Wallpaper connection "); |
| 861 | pw.print(conn); pw.println(":"); |
| 862 | pw.print(" mInfo.component="); pw.println(conn.mInfo.getComponent()); |
| 863 | pw.print(" mToken="); pw.println(conn.mToken); |
| 864 | pw.print(" mService="); pw.println(conn.mService); |
| 865 | pw.print(" mEngine="); pw.println(conn.mEngine); |
| 866 | pw.print(" mLastDiedTime="); |
| 867 | pw.println(mLastDiedTime - SystemClock.uptimeMillis()); |
| 868 | } |
| 869 | } |
| 870 | } |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 871 | } |