blob: 4925a4e46e511374f55d6103e14a54ea7eb9b901 [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001/*
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
17package com.android.server;
18
19import static android.os.FileObserver.*;
20import static android.os.ParcelFileDescriptor.*;
Christopher Tate111bd4a2009-06-24 17:29:38 -070021
Dianne Hackborn8cc6a502009-08-05 21:29:42 -070022import android.app.IWallpaperManager;
23import android.app.IWallpaperManagerCallback;
Dianne Hackborndd9b82c2009-09-03 00:18:47 -070024import android.app.PendingIntent;
Dianne Hackborneb034652009-09-07 00:49:58 -070025import android.app.WallpaperInfo;
Christopher Tate45281862010-03-05 15:46:30 -080026import android.app.backup.BackupManager;
Dianne Hackborn4c62fc02009-08-08 20:40:27 -070027import android.content.ComponentName;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080028import android.content.Context;
29import android.content.Intent;
Dianne Hackborn4c62fc02009-08-08 20:40:27 -070030import android.content.ServiceConnection;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080031import android.content.pm.PackageManager;
Dianne Hackborn4c62fc02009-08-08 20:40:27 -070032import android.content.pm.ResolveInfo;
33import android.content.pm.ServiceInfo;
Joe Onorato9bb8fd72009-07-28 18:24:51 -070034import android.content.pm.PackageManager.NameNotFoundException;
35import android.content.res.Resources;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080036import android.os.Binder;
Dianne Hackborn284ac932009-08-28 10:34:25 -070037import android.os.Bundle;
Dianne Hackborn8bdf5932010-10-15 12:54:40 -070038import android.os.FileUtils;
Dianne Hackborn4c62fc02009-08-08 20:40:27 -070039import android.os.IBinder;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080040import android.os.RemoteException;
41import android.os.FileObserver;
42import android.os.ParcelFileDescriptor;
43import android.os.RemoteCallbackList;
Dianne Hackborn4c62fc02009-08-08 20:40:27 -070044import android.os.ServiceManager;
Dianne Hackborn0cd48872009-08-13 18:51:59 -070045import android.os.SystemClock;
Dianne Hackborn4c62fc02009-08-08 20:40:27 -070046import android.service.wallpaper.IWallpaperConnection;
47import android.service.wallpaper.IWallpaperEngine;
48import android.service.wallpaper.IWallpaperService;
49import android.service.wallpaper.WallpaperService;
Joe Onorato8a9b2202010-02-26 18:56:32 -080050import android.util.Slog;
Joe Onorato9bb8fd72009-07-28 18:24:51 -070051import android.util.Xml;
Dianne Hackborn44bc17c2011-04-20 18:18:51 -070052import android.view.Display;
Dianne Hackborn4c62fc02009-08-08 20:40:27 -070053import android.view.IWindowManager;
54import android.view.WindowManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080055
Dianne Hackborneb034652009-09-07 00:49:58 -070056import java.io.FileDescriptor;
Joe Onorato9bb8fd72009-07-28 18:24:51 -070057import java.io.IOException;
58import java.io.InputStream;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080059import java.io.File;
60import java.io.FileNotFoundException;
Joe Onorato9bb8fd72009-07-28 18:24:51 -070061import java.io.FileInputStream;
62import java.io.FileOutputStream;
Dianne Hackborneb034652009-09-07 00:49:58 -070063import java.io.PrintWriter;
Dianne Hackborn4c62fc02009-08-08 20:40:27 -070064import java.util.List;
Joe Onorato9bb8fd72009-07-28 18:24:51 -070065
66import org.xmlpull.v1.XmlPullParser;
67import org.xmlpull.v1.XmlPullParserException;
68import org.xmlpull.v1.XmlSerializer;
69
Dianne Hackborn21f1bd12010-02-19 17:02:21 -080070import com.android.internal.content.PackageMonitor;
Dianne Hackborn2269d1572010-02-24 19:54:22 -080071import com.android.internal.util.FastXmlSerializer;
Dianne Hackborn1afd1c92010-03-18 22:47:17 -070072import com.android.internal.util.JournaledFile;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080073
Dianne Hackborn8cc6a502009-08-05 21:29:42 -070074class WallpaperManagerService extends IWallpaperManager.Stub {
Dianne Hackborn4c62fc02009-08-08 20:40:27 -070075 static final String TAG = "WallpaperService";
Dianne Hackborncbf15042009-08-18 18:29:09 -070076 static final boolean DEBUG = false;
Joe Onorato9bb8fd72009-07-28 18:24:51 -070077
Romain Guy407ec782011-08-24 17:06:58 -070078 final Object mLock = new Object[0];
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080079
Dianne Hackborn0cd48872009-08-13 18:51:59 -070080 /**
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 Project9066cfe2009-03-03 19:31:44 -080087 "/data/data/com.android.settings/files");
Dianne Hackborn0cd48872009-08-13 18:51:59 -070088 static final String WALLPAPER = "wallpaper";
89 static final File WALLPAPER_FILE = new File(WALLPAPER_DIR, WALLPAPER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080090
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080091 /**
92 * List of callbacks registered they should each be notified
93 * when the wallpaper is changed.
94 */
Dianne Hackborn8cc6a502009-08-05 21:29:42 -070095 private final RemoteCallbackList<IWallpaperManagerCallback> mCallbacks
96 = new RemoteCallbackList<IWallpaperManagerCallback>();
Joe Onorato9bb8fd72009-07-28 18:24:51 -070097
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080098 /**
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(
Dianne Hackborn3da31822011-10-21 12:29:10 -0700105 WALLPAPER_DIR.getAbsolutePath(), CLOSE_WRITE | DELETE | DELETE_SELF) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800106 @Override
107 public void onEvent(int event, String path) {
Joe Onoratoe712ee32009-07-29 16:23:58 -0700108 if (path == null) {
109 return;
110 }
Joe Onorato9bb8fd72009-07-28 18:24:51 -0700111 synchronized (mLock) {
Joe Onorato9bb8fd72009-07-28 18:24:51 -0700112 // 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();
Dianne Hackborn3da31822011-10-21 12:29:10 -0700121 if (mWallpaperComponent == null || event != CLOSE_WRITE
122 || mImageWallpaperPending) {
123 if (event == CLOSE_WRITE) {
124 mImageWallpaperPending = false;
125 }
Dianne Hackborn80b902f2011-09-15 15:15:27 -0700126 bindWallpaperComponentLocked(mImageWallpaperComponent,
127 true, false);
Dianne Hackborn07213e62011-08-24 20:05:39 -0700128 saveSettingsLocked();
Dianne Hackborn9ea31632011-08-05 14:43:50 -0700129 }
Joe Onorato9bb8fd72009-07-28 18:24:51 -0700130 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800131 }
132 }
133 };
134
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700135 final Context mContext;
136 final IWindowManager mIWindowManager;
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800137 final MyPackageMonitor mMonitor;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800138
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700139 int mWidth = -1;
140 int mHeight = -1;
Dianne Hackborn07213e62011-08-24 20:05:39 -0700141
142 /**
143 * Client is currently writing a new image wallpaper.
144 */
145 boolean mImageWallpaperPending;
146
Mike Clerona428b2c2009-11-15 22:53:08 -0800147 /**
148 * Resource name if using a picture from the wallpaper gallery
149 */
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700150 String mName = "";
Mike Clerond6c0b842009-11-13 16:37:27 -0800151
152 /**
Mike Clerona428b2c2009-11-15 22:53:08 -0800153 * The component name of the currently set live wallpaper.
Mike Clerond6c0b842009-11-13 16:37:27 -0800154 */
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700155 ComponentName mWallpaperComponent;
Mike Clerona428b2c2009-11-15 22:53:08 -0800156
157 /**
158 * The component name of the wallpaper that should be set next.
159 */
160 ComponentName mNextWallpaperComponent;
161
162 /**
163 * Name of the component used to display bitmap wallpapers from either the gallery or
164 * built-in wallpapers.
165 */
Dianne Hackbornba398392011-08-01 16:11:57 -0700166 ComponentName mImageWallpaperComponent = new ComponentName("com.android.systemui",
167 "com.android.systemui.ImageWallpaper");
Mike Clerona428b2c2009-11-15 22:53:08 -0800168
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700169 WallpaperConnection mWallpaperConnection;
Dianne Hackborn0cd48872009-08-13 18:51:59 -0700170 long mLastDiedTime;
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800171 boolean mWallpaperUpdating;
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700172
173 class WallpaperConnection extends IWallpaperConnection.Stub
174 implements ServiceConnection {
Dianne Hackborneb034652009-09-07 00:49:58 -0700175 final WallpaperInfo mInfo;
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700176 final Binder mToken = new Binder();
177 IWallpaperService mService;
178 IWallpaperEngine mEngine;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800179
Dianne Hackborneb034652009-09-07 00:49:58 -0700180 public WallpaperConnection(WallpaperInfo info) {
181 mInfo = info;
182 }
183
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700184 public void onServiceConnected(ComponentName name, IBinder service) {
185 synchronized (mLock) {
186 if (mWallpaperConnection == this) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800187 mLastDiedTime = SystemClock.uptimeMillis();
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700188 mService = IWallpaperService.Stub.asInterface(service);
189 attachServiceLocked(this);
Dianne Hackborneb034652009-09-07 00:49:58 -0700190 // XXX should probably do saveSettingsLocked() later
191 // when we have an engine, but I'm not sure about
192 // locking there and anyway we always need to be able to
193 // recover if there is something wrong.
194 saveSettingsLocked();
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700195 }
196 }
197 }
198
199 public void onServiceDisconnected(ComponentName name) {
200 synchronized (mLock) {
201 mService = null;
202 mEngine = null;
Dianne Hackborn0cd48872009-08-13 18:51:59 -0700203 if (mWallpaperConnection == this) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800204 Slog.w(TAG, "Wallpaper service gone: " + mWallpaperComponent);
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800205 if (!mWallpaperUpdating && (mLastDiedTime+MIN_WALLPAPER_CRASH_TIME)
206 > SystemClock.uptimeMillis()) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800207 Slog.w(TAG, "Reverting to built-in wallpaper!");
Dianne Hackborn80b902f2011-09-15 15:15:27 -0700208 clearWallpaperLocked(true);
Dianne Hackborn0cd48872009-08-13 18:51:59 -0700209 }
210 }
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700211 }
212 }
213
214 public void attachEngine(IWallpaperEngine engine) {
215 mEngine = engine;
216 }
217
218 public ParcelFileDescriptor setWallpaper(String name) {
219 synchronized (mLock) {
220 if (mWallpaperConnection == this) {
Dianne Hackborneb034652009-09-07 00:49:58 -0700221 return updateWallpaperBitmapLocked(name);
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700222 }
223 return null;
224 }
225 }
226 }
227
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800228 class MyPackageMonitor extends PackageMonitor {
229 @Override
230 public void onPackageUpdateFinished(String packageName, int uid) {
231 synchronized (mLock) {
232 if (mWallpaperComponent != null &&
233 mWallpaperComponent.getPackageName().equals(packageName)) {
234 mWallpaperUpdating = false;
235 ComponentName comp = mWallpaperComponent;
236 clearWallpaperComponentLocked();
Dianne Hackborn80b902f2011-09-15 15:15:27 -0700237 if (!bindWallpaperComponentLocked(comp, false, false)) {
238 Slog.w(TAG, "Wallpaper no longer available; reverting to default");
239 clearWallpaperLocked(false);
240 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800241 }
242 }
243 }
244
245 @Override
Dianne Hackborn80b902f2011-09-15 15:15:27 -0700246 public void onPackageModified(String packageName) {
247 synchronized (mLock) {
248 if (mWallpaperComponent == null ||
249 !mWallpaperComponent.getPackageName().equals(packageName)) {
250 return;
251 }
252 }
253 doPackagesChanged(true);
254 }
255
256 @Override
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800257 public void onPackageUpdateStarted(String packageName, int uid) {
258 synchronized (mLock) {
259 if (mWallpaperComponent != null &&
260 mWallpaperComponent.getPackageName().equals(packageName)) {
261 mWallpaperUpdating = true;
262 }
263 }
264 }
265
266 @Override
267 public boolean onHandleForceStop(Intent intent, String[] packages, int uid, boolean doit) {
268 return doPackagesChanged(doit);
269 }
270
271 @Override
272 public void onSomePackagesChanged() {
273 doPackagesChanged(true);
274 }
275
276 boolean doPackagesChanged(boolean doit) {
277 boolean changed = false;
278 synchronized (mLock) {
279 if (mWallpaperComponent != null) {
280 int change = isPackageDisappearing(mWallpaperComponent.getPackageName());
281 if (change == PACKAGE_PERMANENT_CHANGE
282 || change == PACKAGE_TEMPORARY_CHANGE) {
283 changed = true;
284 if (doit) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800285 Slog.w(TAG, "Wallpaper uninstalled, removing: " + mWallpaperComponent);
Dianne Hackborn80b902f2011-09-15 15:15:27 -0700286 clearWallpaperLocked(false);
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800287 }
288 }
289 }
290 if (mNextWallpaperComponent != null) {
291 int change = isPackageDisappearing(mNextWallpaperComponent.getPackageName());
292 if (change == PACKAGE_PERMANENT_CHANGE
293 || change == PACKAGE_TEMPORARY_CHANGE) {
294 mNextWallpaperComponent = null;
295 }
296 }
297 if (mWallpaperComponent != null
298 && isPackageModified(mWallpaperComponent.getPackageName())) {
299 try {
300 mContext.getPackageManager().getServiceInfo(
301 mWallpaperComponent, 0);
302 } catch (NameNotFoundException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800303 Slog.w(TAG, "Wallpaper component gone, removing: " + mWallpaperComponent);
Dianne Hackborn80b902f2011-09-15 15:15:27 -0700304 clearWallpaperLocked(false);
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800305 }
306 }
307 if (mNextWallpaperComponent != null
308 && isPackageModified(mNextWallpaperComponent.getPackageName())) {
309 try {
310 mContext.getPackageManager().getServiceInfo(
311 mNextWallpaperComponent, 0);
312 } catch (NameNotFoundException e) {
313 mNextWallpaperComponent = null;
314 }
315 }
316 }
317 return changed;
318 }
319 }
320
Dianne Hackborn8cc6a502009-08-05 21:29:42 -0700321 public WallpaperManagerService(Context context) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800322 if (DEBUG) Slog.v(TAG, "WallpaperService startup");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800323 mContext = context;
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700324 mIWindowManager = IWindowManager.Stub.asInterface(
325 ServiceManager.getService(Context.WINDOW_SERVICE));
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800326 mMonitor = new MyPackageMonitor();
327 mMonitor.register(context, true);
Joe Onoratoe712ee32009-07-29 16:23:58 -0700328 WALLPAPER_DIR.mkdirs();
Joe Onorato9bb8fd72009-07-28 18:24:51 -0700329 loadSettingsLocked();
Joe Onoratoe712ee32009-07-29 16:23:58 -0700330 mWallpaperObserver.startWatching();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800331 }
332
333 @Override
334 protected void finalize() throws Throwable {
335 super.finalize();
336 mWallpaperObserver.stopWatching();
337 }
338
Dianne Hackbornf21adf62009-08-13 10:20:21 -0700339 public void systemReady() {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800340 if (DEBUG) Slog.v(TAG, "systemReady");
Dianne Hackbornf21adf62009-08-13 10:20:21 -0700341 synchronized (mLock) {
Dianne Hackborn80b902f2011-09-15 15:15:27 -0700342 RuntimeException e = null;
Dianne Hackbornf21adf62009-08-13 10:20:21 -0700343 try {
Dianne Hackborn80b902f2011-09-15 15:15:27 -0700344 if (bindWallpaperComponentLocked(mNextWallpaperComponent, false, false)) {
345 return;
Dianne Hackbornf21adf62009-08-13 10:20:21 -0700346 }
Dianne Hackborn80b902f2011-09-15 15:15:27 -0700347 } catch (RuntimeException e1) {
348 e = e1;
Dianne Hackbornf21adf62009-08-13 10:20:21 -0700349 }
Dianne Hackborn80b902f2011-09-15 15:15:27 -0700350 Slog.w(TAG, "Failure starting previous wallpaper", e);
351 clearWallpaperLocked(false);
Dianne Hackbornf21adf62009-08-13 10:20:21 -0700352 }
353 }
354
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800355 public void clearWallpaper() {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800356 if (DEBUG) Slog.v(TAG, "clearWallpaper");
Joe Onorato9bb8fd72009-07-28 18:24:51 -0700357 synchronized (mLock) {
Dianne Hackborn80b902f2011-09-15 15:15:27 -0700358 clearWallpaperLocked(false);
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800359 }
360 }
361
Dianne Hackborn80b902f2011-09-15 15:15:27 -0700362 public void clearWallpaperLocked(boolean defaultFailed) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800363 File f = WALLPAPER_FILE;
364 if (f.exists()) {
365 f.delete();
366 }
367 final long ident = Binder.clearCallingIdentity();
Dianne Hackborn80b902f2011-09-15 15:15:27 -0700368 RuntimeException e = null;
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800369 try {
Dianne Hackborn07213e62011-08-24 20:05:39 -0700370 mImageWallpaperPending = false;
Dianne Hackborn80b902f2011-09-15 15:15:27 -0700371 if (bindWallpaperComponentLocked(defaultFailed
372 ? mImageWallpaperComponent : null, true, false)) {
373 return;
374 }
375 } catch (IllegalArgumentException e1) {
376 e = e1;
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800377 } finally {
378 Binder.restoreCallingIdentity(ident);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800379 }
Dianne Hackborn80b902f2011-09-15 15:15:27 -0700380
381 // This can happen if the default wallpaper component doesn't
382 // exist. This should be a system configuration problem, but
383 // let's not let it crash the system and just live with no
384 // wallpaper.
385 Slog.e(TAG, "Default wallpaper component not found!", e);
386 clearWallpaperComponentLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800387 }
388
389 public void setDimensionHints(int width, int height) throws RemoteException {
390 checkPermission(android.Manifest.permission.SET_WALLPAPER_HINTS);
391
392 if (width <= 0 || height <= 0) {
393 throw new IllegalArgumentException("width and height must be > 0");
394 }
395
Joe Onorato9bb8fd72009-07-28 18:24:51 -0700396 synchronized (mLock) {
397 if (width != mWidth || height != mHeight) {
398 mWidth = width;
399 mHeight = height;
400 saveSettingsLocked();
Dianne Hackborn284ac932009-08-28 10:34:25 -0700401 if (mWallpaperConnection != null) {
402 if (mWallpaperConnection.mEngine != null) {
403 try {
404 mWallpaperConnection.mEngine.setDesiredSize(
405 width, height);
406 } catch (RemoteException e) {
407 }
408 notifyCallbacksLocked();
409 }
410 }
Joe Onorato9bb8fd72009-07-28 18:24:51 -0700411 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800412 }
413 }
414
415 public int getWidthHint() throws RemoteException {
Joe Onorato9bb8fd72009-07-28 18:24:51 -0700416 synchronized (mLock) {
417 return mWidth;
418 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800419 }
420
421 public int getHeightHint() throws RemoteException {
Joe Onorato9bb8fd72009-07-28 18:24:51 -0700422 synchronized (mLock) {
423 return mHeight;
424 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800425 }
426
Dianne Hackborn284ac932009-08-28 10:34:25 -0700427 public ParcelFileDescriptor getWallpaper(IWallpaperManagerCallback cb,
428 Bundle outParams) {
Joe Onorato9bb8fd72009-07-28 18:24:51 -0700429 synchronized (mLock) {
430 try {
Dianne Hackborn284ac932009-08-28 10:34:25 -0700431 if (outParams != null) {
432 outParams.putInt("width", mWidth);
433 outParams.putInt("height", mHeight);
434 }
Joe Onorato9bb8fd72009-07-28 18:24:51 -0700435 mCallbacks.register(cb);
436 File f = WALLPAPER_FILE;
437 if (!f.exists()) {
438 return null;
439 }
440 return ParcelFileDescriptor.open(f, MODE_READ_ONLY);
441 } catch (FileNotFoundException e) {
442 /* Shouldn't happen as we check to see if the file exists */
Joe Onorato8a9b2202010-02-26 18:56:32 -0800443 Slog.w(TAG, "Error getting wallpaper", e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800444 }
Joe Onorato9bb8fd72009-07-28 18:24:51 -0700445 return null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800446 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800447 }
448
Dianne Hackborneb034652009-09-07 00:49:58 -0700449 public WallpaperInfo getWallpaperInfo() {
450 synchronized (mLock) {
451 if (mWallpaperConnection != null) {
452 return mWallpaperConnection.mInfo;
453 }
454 return null;
455 }
456 }
457
Joe Onorato9bb8fd72009-07-28 18:24:51 -0700458 public ParcelFileDescriptor setWallpaper(String name) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800459 if (DEBUG) Slog.v(TAG, "setWallpaper");
Mike Clerona428b2c2009-11-15 22:53:08 -0800460
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800461 checkPermission(android.Manifest.permission.SET_WALLPAPER);
Joe Onorato9bb8fd72009-07-28 18:24:51 -0700462 synchronized (mLock) {
Dianne Hackborn0cd48872009-08-13 18:51:59 -0700463 final long ident = Binder.clearCallingIdentity();
464 try {
465 ParcelFileDescriptor pfd = updateWallpaperBitmapLocked(name);
466 if (pfd != null) {
Dianne Hackborn07213e62011-08-24 20:05:39 -0700467 mImageWallpaperPending = true;
Dianne Hackborn0cd48872009-08-13 18:51:59 -0700468 }
469 return pfd;
470 } finally {
471 Binder.restoreCallingIdentity(ident);
Joe Onorato9bb8fd72009-07-28 18:24:51 -0700472 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800473 }
474 }
475
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700476 ParcelFileDescriptor updateWallpaperBitmapLocked(String name) {
477 if (name == null) name = "";
478 try {
Dianne Hackbornebac48c2011-11-29 18:01:50 -0800479 if (!WALLPAPER_DIR.exists()) {
480 WALLPAPER_DIR.mkdir();
481 FileUtils.setPermissions(
482 WALLPAPER_DIR.getPath(),
483 FileUtils.S_IRWXU|FileUtils.S_IRWXG|FileUtils.S_IXOTH,
484 -1, -1);
485 }
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700486 ParcelFileDescriptor fd = ParcelFileDescriptor.open(WALLPAPER_FILE,
487 MODE_CREATE|MODE_READ_WRITE);
488 mName = name;
489 return fd;
490 } catch (FileNotFoundException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800491 Slog.w(TAG, "Error setting wallpaper", e);
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700492 }
493 return null;
494 }
495
496 public void setWallpaperComponent(ComponentName name) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800497 if (DEBUG) Slog.v(TAG, "setWallpaperComponent name=" + name);
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700498 checkPermission(android.Manifest.permission.SET_WALLPAPER_COMPONENT);
499 synchronized (mLock) {
500 final long ident = Binder.clearCallingIdentity();
501 try {
Dianne Hackborn07213e62011-08-24 20:05:39 -0700502 mImageWallpaperPending = false;
Dianne Hackborn80b902f2011-09-15 15:15:27 -0700503 bindWallpaperComponentLocked(name, false, true);
Dianne Hackbornf21adf62009-08-13 10:20:21 -0700504 } finally {
505 Binder.restoreCallingIdentity(ident);
506 }
507 }
508 }
509
Dianne Hackborn80b902f2011-09-15 15:15:27 -0700510 boolean bindWallpaperComponentLocked(ComponentName componentName, boolean force, boolean fromUser) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800511 if (DEBUG) Slog.v(TAG, "bindWallpaperComponentLocked: componentName=" + componentName);
Mike Clerona428b2c2009-11-15 22:53:08 -0800512
Dianne Hackbornf21adf62009-08-13 10:20:21 -0700513 // Has the component changed?
Dianne Hackborn9ea31632011-08-05 14:43:50 -0700514 if (!force) {
515 if (mWallpaperConnection != null) {
516 if (mWallpaperComponent == null) {
517 if (componentName == null) {
518 if (DEBUG) Slog.v(TAG, "bindWallpaperComponentLocked: still using default");
519 // Still using default wallpaper.
Dianne Hackborn80b902f2011-09-15 15:15:27 -0700520 return true;
Dianne Hackborn9ea31632011-08-05 14:43:50 -0700521 }
522 } else if (mWallpaperComponent.equals(componentName)) {
523 // Changing to same wallpaper.
524 if (DEBUG) Slog.v(TAG, "same wallpaper");
Dianne Hackborn80b902f2011-09-15 15:15:27 -0700525 return true;
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700526 }
Dianne Hackbornf21adf62009-08-13 10:20:21 -0700527 }
528 }
529
530 try {
Mike Clerona428b2c2009-11-15 22:53:08 -0800531 if (componentName == null) {
Mike Cleron322b6ee2009-11-12 07:45:47 -0800532 String defaultComponent =
533 mContext.getString(com.android.internal.R.string.default_wallpaper_component);
Mike Clerona428b2c2009-11-15 22:53:08 -0800534 if (defaultComponent != null) {
Mike Cleron322b6ee2009-11-12 07:45:47 -0800535 // See if there is a default wallpaper component specified
Mike Clerona428b2c2009-11-15 22:53:08 -0800536 componentName = ComponentName.unflattenFromString(defaultComponent);
Joe Onorato8a9b2202010-02-26 18:56:32 -0800537 if (DEBUG) Slog.v(TAG, "Use default component wallpaper:" + componentName);
Mike Cleron322b6ee2009-11-12 07:45:47 -0800538 }
Mike Clerona428b2c2009-11-15 22:53:08 -0800539 if (componentName == null) {
Mike Cleron322b6ee2009-11-12 07:45:47 -0800540 // Fall back to static image wallpaper
Mike Clerona428b2c2009-11-15 22:53:08 -0800541 componentName = mImageWallpaperComponent;
Mike Cleron322b6ee2009-11-12 07:45:47 -0800542 //clearWallpaperComponentLocked();
543 //return;
Joe Onorato8a9b2202010-02-26 18:56:32 -0800544 if (DEBUG) Slog.v(TAG, "Using image wallpaper");
Mike Cleron322b6ee2009-11-12 07:45:47 -0800545 }
Dianne Hackbornf21adf62009-08-13 10:20:21 -0700546 }
Mike Clerona428b2c2009-11-15 22:53:08 -0800547 ServiceInfo si = mContext.getPackageManager().getServiceInfo(componentName,
Dianne Hackbornf21adf62009-08-13 10:20:21 -0700548 PackageManager.GET_META_DATA | PackageManager.GET_PERMISSIONS);
549 if (!android.Manifest.permission.BIND_WALLPAPER.equals(si.permission)) {
Dianne Hackborn80b902f2011-09-15 15:15:27 -0700550 String msg = "Selected service does not require "
Dianne Hackbornf21adf62009-08-13 10:20:21 -0700551 + android.Manifest.permission.BIND_WALLPAPER
Dianne Hackborn80b902f2011-09-15 15:15:27 -0700552 + ": " + componentName;
553 if (fromUser) {
554 throw new SecurityException(msg);
555 }
556 Slog.w(TAG, msg);
557 return false;
Dianne Hackbornf21adf62009-08-13 10:20:21 -0700558 }
559
Dianne Hackborneb034652009-09-07 00:49:58 -0700560 WallpaperInfo wi = null;
561
Dianne Hackbornf21adf62009-08-13 10:20:21 -0700562 Intent intent = new Intent(WallpaperService.SERVICE_INTERFACE);
Mike Clerona428b2c2009-11-15 22:53:08 -0800563 if (componentName != null && !componentName.equals(mImageWallpaperComponent)) {
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700564 // Make sure the selected service is actually a wallpaper service.
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700565 List<ResolveInfo> ris = mContext.getPackageManager()
Dianne Hackborneb034652009-09-07 00:49:58 -0700566 .queryIntentServices(intent, PackageManager.GET_META_DATA);
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700567 for (int i=0; i<ris.size(); i++) {
568 ServiceInfo rsi = ris.get(i).serviceInfo;
569 if (rsi.name.equals(si.name) &&
570 rsi.packageName.equals(si.packageName)) {
Dianne Hackborneb034652009-09-07 00:49:58 -0700571 try {
572 wi = new WallpaperInfo(mContext, ris.get(i));
573 } catch (XmlPullParserException e) {
Dianne Hackborn80b902f2011-09-15 15:15:27 -0700574 if (fromUser) {
575 throw new IllegalArgumentException(e);
576 }
577 Slog.w(TAG, e);
578 return false;
Dianne Hackborneb034652009-09-07 00:49:58 -0700579 } catch (IOException e) {
Dianne Hackborn80b902f2011-09-15 15:15:27 -0700580 if (fromUser) {
581 throw new IllegalArgumentException(e);
582 }
583 Slog.w(TAG, e);
584 return false;
Dianne Hackborneb034652009-09-07 00:49:58 -0700585 }
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700586 break;
587 }
588 }
Dianne Hackborneb034652009-09-07 00:49:58 -0700589 if (wi == null) {
Dianne Hackborn80b902f2011-09-15 15:15:27 -0700590 String msg = "Selected service is not a wallpaper: "
591 + componentName;
592 if (fromUser) {
593 throw new SecurityException(msg);
594 }
595 Slog.w(TAG, msg);
596 return false;
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700597 }
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700598 }
Dianne Hackbornf21adf62009-08-13 10:20:21 -0700599
600 // Bind the service!
Joe Onorato8a9b2202010-02-26 18:56:32 -0800601 if (DEBUG) Slog.v(TAG, "Binding to:" + componentName);
Dianne Hackborneb034652009-09-07 00:49:58 -0700602 WallpaperConnection newConn = new WallpaperConnection(wi);
Mike Clerona428b2c2009-11-15 22:53:08 -0800603 intent.setComponent(componentName);
Dianne Hackborndd9b82c2009-09-03 00:18:47 -0700604 intent.putExtra(Intent.EXTRA_CLIENT_LABEL,
605 com.android.internal.R.string.wallpaper_binding_label);
606 intent.putExtra(Intent.EXTRA_CLIENT_INTENT, PendingIntent.getActivity(
Dianne Hackborneb034652009-09-07 00:49:58 -0700607 mContext, 0,
608 Intent.createChooser(new Intent(Intent.ACTION_SET_WALLPAPER),
609 mContext.getText(com.android.internal.R.string.chooser_wallpaper)),
610 0));
Dianne Hackbornf21adf62009-08-13 10:20:21 -0700611 if (!mContext.bindService(intent, newConn,
612 Context.BIND_AUTO_CREATE)) {
Dianne Hackborn80b902f2011-09-15 15:15:27 -0700613 String msg = "Unable to bind service: "
614 + componentName;
615 if (fromUser) {
616 throw new IllegalArgumentException(msg);
617 }
618 Slog.w(TAG, msg);
619 return false;
Dianne Hackbornf21adf62009-08-13 10:20:21 -0700620 }
621
622 clearWallpaperComponentLocked();
Mike Cleron322b6ee2009-11-12 07:45:47 -0800623 mWallpaperComponent = componentName;
Dianne Hackbornf21adf62009-08-13 10:20:21 -0700624 mWallpaperConnection = newConn;
Dianne Hackborn0cd48872009-08-13 18:51:59 -0700625 mLastDiedTime = SystemClock.uptimeMillis();
Dianne Hackbornf21adf62009-08-13 10:20:21 -0700626 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800627 if (DEBUG) Slog.v(TAG, "Adding window token: " + newConn.mToken);
Dianne Hackbornf21adf62009-08-13 10:20:21 -0700628 mIWindowManager.addWindowToken(newConn.mToken,
629 WindowManager.LayoutParams.TYPE_WALLPAPER);
630 } catch (RemoteException e) {
631 }
632
633 } catch (PackageManager.NameNotFoundException e) {
Dianne Hackborn80b902f2011-09-15 15:15:27 -0700634 String msg = "Unknown component " + componentName;
635 if (fromUser) {
636 throw new IllegalArgumentException(msg);
637 }
638 Slog.w(TAG, msg);
639 return false;
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700640 }
Dianne Hackborn80b902f2011-09-15 15:15:27 -0700641 return true;
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700642 }
643
644 void clearWallpaperComponentLocked() {
645 mWallpaperComponent = null;
646 if (mWallpaperConnection != null) {
647 if (mWallpaperConnection.mEngine != null) {
648 try {
649 mWallpaperConnection.mEngine.destroy();
650 } catch (RemoteException e) {
651 }
652 }
653 mContext.unbindService(mWallpaperConnection);
Dianne Hackborne9e9bca2009-08-18 15:08:22 -0700654 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800655 if (DEBUG) Slog.v(TAG, "Removing window token: "
Dianne Hackborne9e9bca2009-08-18 15:08:22 -0700656 + mWallpaperConnection.mToken);
657 mIWindowManager.removeWindowToken(mWallpaperConnection.mToken);
658 } catch (RemoteException e) {
659 }
Vairavan Srinivasanfdfc1b22010-12-23 14:05:44 -0800660 mWallpaperConnection.mService = null;
661 mWallpaperConnection.mEngine = null;
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700662 mWallpaperConnection = null;
663 }
664 }
665
666 void attachServiceLocked(WallpaperConnection conn) {
667 try {
Dianne Hackborn3be63c02009-08-20 19:31:38 -0700668 conn.mService.attach(conn, conn.mToken,
669 WindowManager.LayoutParams.TYPE_WALLPAPER, false,
670 mWidth, mHeight);
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700671 } catch (RemoteException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800672 Slog.w(TAG, "Failed attaching wallpaper; clearing", e);
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800673 if (!mWallpaperUpdating) {
Dianne Hackborn80b902f2011-09-15 15:15:27 -0700674 bindWallpaperComponentLocked(null, false, false);
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800675 }
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700676 }
677 }
678
Joe Onorato9bb8fd72009-07-28 18:24:51 -0700679 private void notifyCallbacksLocked() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800680 final int n = mCallbacks.beginBroadcast();
681 for (int i = 0; i < n; i++) {
682 try {
683 mCallbacks.getBroadcastItem(i).onWallpaperChanged();
684 } catch (RemoteException e) {
685
686 // The RemoteCallbackList will take care of removing
687 // the dead object for us.
688 }
689 }
690 mCallbacks.finishBroadcast();
691 final Intent intent = new Intent(Intent.ACTION_WALLPAPER_CHANGED);
692 mContext.sendBroadcast(intent);
693 }
694
695 private void checkPermission(String permission) {
Joe Onorato9bb8fd72009-07-28 18:24:51 -0700696 if (PackageManager.PERMISSION_GRANTED!= mContext.checkCallingOrSelfPermission(permission)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800697 throw new SecurityException("Access denied to process: " + Binder.getCallingPid()
698 + ", must have permission " + permission);
699 }
700 }
Joe Onorato9bb8fd72009-07-28 18:24:51 -0700701
702 private static JournaledFile makeJournaledFile() {
703 final String base = "/data/system/wallpaper_info.xml";
704 return new JournaledFile(new File(base), new File(base + ".tmp"));
705 }
706
707 private void saveSettingsLocked() {
708 JournaledFile journal = makeJournaledFile();
709 FileOutputStream stream = null;
710 try {
711 stream = new FileOutputStream(journal.chooseForWrite(), false);
712 XmlSerializer out = new FastXmlSerializer();
713 out.setOutput(stream, "utf-8");
714 out.startDocument(null, true);
715
716 out.startTag(null, "wp");
717 out.attribute(null, "width", Integer.toString(mWidth));
718 out.attribute(null, "height", Integer.toString(mHeight));
719 out.attribute(null, "name", mName);
Dianne Hackborn9ea31632011-08-05 14:43:50 -0700720 if (mWallpaperComponent != null &&
721 !mWallpaperComponent.equals(mImageWallpaperComponent)) {
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700722 out.attribute(null, "component",
723 mWallpaperComponent.flattenToShortString());
724 }
Joe Onorato9bb8fd72009-07-28 18:24:51 -0700725 out.endTag(null, "wp");
726
727 out.endDocument();
728 stream.close();
729 journal.commit();
730 } catch (IOException e) {
731 try {
732 if (stream != null) {
733 stream.close();
734 }
735 } catch (IOException ex) {
736 // Ignore
737 }
738 journal.rollback();
739 }
740 }
741
742 private void loadSettingsLocked() {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800743 if (DEBUG) Slog.v(TAG, "loadSettingsLocked");
Mike Clerona428b2c2009-11-15 22:53:08 -0800744
Joe Onorato9bb8fd72009-07-28 18:24:51 -0700745 JournaledFile journal = makeJournaledFile();
746 FileInputStream stream = null;
747 File file = journal.chooseForRead();
748 boolean success = false;
749 try {
750 stream = new FileInputStream(file);
751 XmlPullParser parser = Xml.newPullParser();
752 parser.setInput(stream, null);
753
754 int type;
Joe Onorato9bb8fd72009-07-28 18:24:51 -0700755 do {
756 type = parser.next();
757 if (type == XmlPullParser.START_TAG) {
758 String tag = parser.getName();
759 if ("wp".equals(tag)) {
760 mWidth = Integer.parseInt(parser.getAttributeValue(null, "width"));
761 mHeight = Integer.parseInt(parser.getAttributeValue(null, "height"));
762 mName = parser.getAttributeValue(null, "name");
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700763 String comp = parser.getAttributeValue(null, "component");
Mike Clerona428b2c2009-11-15 22:53:08 -0800764 mNextWallpaperComponent = comp != null
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700765 ? ComponentName.unflattenFromString(comp)
766 : null;
Dianne Hackborn9ea31632011-08-05 14:43:50 -0700767 if (mNextWallpaperComponent == null ||
768 "android".equals(mNextWallpaperComponent.getPackageName())) {
769 mNextWallpaperComponent = mImageWallpaperComponent;
770 }
Mike Clerona428b2c2009-11-15 22:53:08 -0800771
772 if (DEBUG) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800773 Slog.v(TAG, "mWidth:" + mWidth);
774 Slog.v(TAG, "mHeight:" + mHeight);
775 Slog.v(TAG, "mName:" + mName);
776 Slog.v(TAG, "mNextWallpaperComponent:" + mNextWallpaperComponent);
Mike Clerona428b2c2009-11-15 22:53:08 -0800777 }
Joe Onorato9bb8fd72009-07-28 18:24:51 -0700778 }
779 }
780 } while (type != XmlPullParser.END_DOCUMENT);
781 success = true;
782 } catch (NullPointerException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800783 Slog.w(TAG, "failed parsing " + file + " " + e);
Joe Onorato9bb8fd72009-07-28 18:24:51 -0700784 } catch (NumberFormatException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800785 Slog.w(TAG, "failed parsing " + file + " " + e);
Joe Onorato9bb8fd72009-07-28 18:24:51 -0700786 } catch (XmlPullParserException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800787 Slog.w(TAG, "failed parsing " + file + " " + e);
Joe Onorato9bb8fd72009-07-28 18:24:51 -0700788 } catch (IOException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800789 Slog.w(TAG, "failed parsing " + file + " " + e);
Joe Onorato9bb8fd72009-07-28 18:24:51 -0700790 } catch (IndexOutOfBoundsException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800791 Slog.w(TAG, "failed parsing " + file + " " + e);
Joe Onorato9bb8fd72009-07-28 18:24:51 -0700792 }
793 try {
794 if (stream != null) {
795 stream.close();
796 }
797 } catch (IOException e) {
798 // Ignore
799 }
800
801 if (!success) {
802 mWidth = -1;
803 mHeight = -1;
804 mName = "";
805 }
Dianne Hackborn44bc17c2011-04-20 18:18:51 -0700806
807 // We always want to have some reasonable width hint.
808 WindowManager wm = (WindowManager)mContext.getSystemService(Context.WINDOW_SERVICE);
809 Display d = wm.getDefaultDisplay();
810 int baseSize = d.getMaximumSizeDimension();
811 if (mWidth < baseSize) {
812 mWidth = baseSize;
813 }
814 if (mHeight < baseSize) {
815 mHeight = baseSize;
816 }
Joe Onorato9bb8fd72009-07-28 18:24:51 -0700817 }
818
Brad Fitzpatrick194b19a2010-09-14 11:30:29 -0700819 // Called by SystemBackupAgent after files are restored to disk.
Joe Onorato9bb8fd72009-07-28 18:24:51 -0700820 void settingsRestored() {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800821 if (DEBUG) Slog.v(TAG, "settingsRestored");
Brad Fitzpatrick194b19a2010-09-14 11:30:29 -0700822
Joe Onorato9bb8fd72009-07-28 18:24:51 -0700823 boolean success = false;
824 synchronized (mLock) {
825 loadSettingsLocked();
Mike Clerona428b2c2009-11-15 22:53:08 -0800826 if (mNextWallpaperComponent != null &&
827 !mNextWallpaperComponent.equals(mImageWallpaperComponent)) {
Dianne Hackborn80b902f2011-09-15 15:15:27 -0700828 if (!bindWallpaperComponentLocked(mNextWallpaperComponent, false, false)) {
Christopher Tatee3ab4d02009-12-16 14:03:31 -0800829 // No such live wallpaper or other failure; fall back to the default
830 // live wallpaper (since the profile being restored indicated that the
831 // user had selected a live rather than static one).
Dianne Hackborn80b902f2011-09-15 15:15:27 -0700832 bindWallpaperComponentLocked(null, false, false);
Christopher Tatee3ab4d02009-12-16 14:03:31 -0800833 }
Joe Onorato9bb8fd72009-07-28 18:24:51 -0700834 success = true;
835 } else {
Mike Clerona428b2c2009-11-15 22:53:08 -0800836 // If there's a wallpaper name, we use that. If that can't be loaded, then we
837 // use the default.
838 if ("".equals(mName)) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800839 if (DEBUG) Slog.v(TAG, "settingsRestored: name is empty");
Mike Clerona428b2c2009-11-15 22:53:08 -0800840 success = true;
841 } else {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800842 if (DEBUG) Slog.v(TAG, "settingsRestored: attempting to restore named resource");
Mike Clerona428b2c2009-11-15 22:53:08 -0800843 success = restoreNamedResourceLocked();
844 }
Joe Onorato8a9b2202010-02-26 18:56:32 -0800845 if (DEBUG) Slog.v(TAG, "settingsRestored: success=" + success);
Mike Clerona428b2c2009-11-15 22:53:08 -0800846 if (success) {
Christopher Tatebf6ee4f2011-10-07 12:37:42 -0700847 bindWallpaperComponentLocked(mNextWallpaperComponent, false, false);
Mike Clerona428b2c2009-11-15 22:53:08 -0800848 }
Joe Onorato9bb8fd72009-07-28 18:24:51 -0700849 }
850 }
851
852 if (!success) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800853 Slog.e(TAG, "Failed to restore wallpaper: '" + mName + "'");
Joe Onorato9bb8fd72009-07-28 18:24:51 -0700854 mName = "";
855 WALLPAPER_FILE.delete();
856 }
Brad Fitzpatrick194b19a2010-09-14 11:30:29 -0700857
858 synchronized (mLock) {
859 saveSettingsLocked();
860 }
Joe Onorato9bb8fd72009-07-28 18:24:51 -0700861 }
862
863 boolean restoreNamedResourceLocked() {
864 if (mName.length() > 4 && "res:".equals(mName.substring(0, 4))) {
865 String resName = mName.substring(4);
866
867 String pkg = null;
868 int colon = resName.indexOf(':');
869 if (colon > 0) {
870 pkg = resName.substring(0, colon);
871 }
872
873 String ident = null;
874 int slash = resName.lastIndexOf('/');
875 if (slash > 0) {
876 ident = resName.substring(slash+1);
877 }
878
879 String type = null;
880 if (colon > 0 && slash > 0 && (slash-colon) > 1) {
881 type = resName.substring(colon+1, slash);
882 }
883
884 if (pkg != null && ident != null && type != null) {
885 int resId = -1;
886 InputStream res = null;
887 FileOutputStream fos = null;
888 try {
889 Context c = mContext.createPackageContext(pkg, Context.CONTEXT_RESTRICTED);
890 Resources r = c.getResources();
891 resId = r.getIdentifier(resName, null, null);
892 if (resId == 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800893 Slog.e(TAG, "couldn't resolve identifier pkg=" + pkg + " type=" + type
Joe Onorato9bb8fd72009-07-28 18:24:51 -0700894 + " ident=" + ident);
895 return false;
896 }
897
898 res = r.openRawResource(resId);
Dianne Hackborn1afd1c92010-03-18 22:47:17 -0700899 if (WALLPAPER_FILE.exists()) {
900 WALLPAPER_FILE.delete();
901 }
Joe Onorato9bb8fd72009-07-28 18:24:51 -0700902 fos = new FileOutputStream(WALLPAPER_FILE);
903
904 byte[] buffer = new byte[32768];
905 int amt;
906 while ((amt=res.read(buffer)) > 0) {
907 fos.write(buffer, 0, amt);
908 }
909 // mWallpaperObserver will notice the close and send the change broadcast
910
Joe Onorato8a9b2202010-02-26 18:56:32 -0800911 Slog.v(TAG, "Restored wallpaper: " + resName);
Joe Onorato9bb8fd72009-07-28 18:24:51 -0700912 return true;
913 } catch (NameNotFoundException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800914 Slog.e(TAG, "Package name " + pkg + " not found");
Joe Onorato9bb8fd72009-07-28 18:24:51 -0700915 } catch (Resources.NotFoundException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800916 Slog.e(TAG, "Resource not found: " + resId);
Joe Onorato9bb8fd72009-07-28 18:24:51 -0700917 } catch (IOException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800918 Slog.e(TAG, "IOException while restoring wallpaper ", e);
Joe Onorato9bb8fd72009-07-28 18:24:51 -0700919 } finally {
920 if (res != null) {
921 try {
922 res.close();
923 } catch (IOException ex) {}
924 }
925 if (fos != null) {
Dianne Hackborn8bdf5932010-10-15 12:54:40 -0700926 FileUtils.sync(fos);
Joe Onorato9bb8fd72009-07-28 18:24:51 -0700927 try {
928 fos.close();
929 } catch (IOException ex) {}
930 }
931 }
932 }
933 }
934 return false;
935 }
Dianne Hackborneb034652009-09-07 00:49:58 -0700936
937 @Override
938 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
939 if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DUMP)
940 != PackageManager.PERMISSION_GRANTED) {
941
942 pw.println("Permission Denial: can't dump wallpaper service from from pid="
943 + Binder.getCallingPid()
944 + ", uid=" + Binder.getCallingUid());
945 return;
946 }
947
948 synchronized (mLock) {
949 pw.println("Current Wallpaper Service state:");
950 pw.print(" mWidth="); pw.print(mWidth);
951 pw.print(" mHeight="); pw.println(mHeight);
952 pw.print(" mName="); pw.println(mName);
953 pw.print(" mWallpaperComponent="); pw.println(mWallpaperComponent);
954 if (mWallpaperConnection != null) {
955 WallpaperConnection conn = mWallpaperConnection;
956 pw.print(" Wallpaper connection ");
957 pw.print(conn); pw.println(":");
958 pw.print(" mInfo.component="); pw.println(conn.mInfo.getComponent());
959 pw.print(" mToken="); pw.println(conn.mToken);
960 pw.print(" mService="); pw.println(conn.mService);
961 pw.print(" mEngine="); pw.println(conn.mEngine);
962 pw.print(" mLastDiedTime=");
963 pw.println(mLastDiedTime - SystemClock.uptimeMillis());
964 }
965 }
966 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800967}