blob: 9765f2a0088716f9a414f3651b2cd81f90f86f94 [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
Dianne Hackborn4c62fc02009-08-08 20:40:27 -070078 Object mLock = new Object();
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(
Joe Onorato9bb8fd72009-07-28 18:24:51 -0700105 WALLPAPER_DIR.getAbsolutePath(), CREATE | 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 Hackborn9ea31632011-08-05 14:43:50 -0700121 if (mWallpaperComponent == null ||
122 mWallpaperComponent.equals(mImageWallpaperComponent)) {
123 bindWallpaperComponentLocked(mWallpaperComponent, true);
124 }
Joe Onorato9bb8fd72009-07-28 18:24:51 -0700125 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800126 }
127 }
128 };
129
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700130 final Context mContext;
131 final IWindowManager mIWindowManager;
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800132 final MyPackageMonitor mMonitor;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800133
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700134 int mWidth = -1;
135 int mHeight = -1;
Mike Clerona428b2c2009-11-15 22:53:08 -0800136
137 /**
138 * Resource name if using a picture from the wallpaper gallery
139 */
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700140 String mName = "";
Mike Clerond6c0b842009-11-13 16:37:27 -0800141
142 /**
Mike Clerona428b2c2009-11-15 22:53:08 -0800143 * The component name of the currently set live wallpaper.
Mike Clerond6c0b842009-11-13 16:37:27 -0800144 */
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700145 ComponentName mWallpaperComponent;
Mike Clerona428b2c2009-11-15 22:53:08 -0800146
147 /**
148 * The component name of the wallpaper that should be set next.
149 */
150 ComponentName mNextWallpaperComponent;
151
152 /**
153 * Name of the component used to display bitmap wallpapers from either the gallery or
154 * built-in wallpapers.
155 */
Dianne Hackbornba398392011-08-01 16:11:57 -0700156 ComponentName mImageWallpaperComponent = new ComponentName("com.android.systemui",
157 "com.android.systemui.ImageWallpaper");
Mike Clerona428b2c2009-11-15 22:53:08 -0800158
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700159 WallpaperConnection mWallpaperConnection;
Dianne Hackborn0cd48872009-08-13 18:51:59 -0700160 long mLastDiedTime;
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800161 boolean mWallpaperUpdating;
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700162
163 class WallpaperConnection extends IWallpaperConnection.Stub
164 implements ServiceConnection {
Dianne Hackborneb034652009-09-07 00:49:58 -0700165 final WallpaperInfo mInfo;
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700166 final Binder mToken = new Binder();
167 IWallpaperService mService;
168 IWallpaperEngine mEngine;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800169
Dianne Hackborneb034652009-09-07 00:49:58 -0700170 public WallpaperConnection(WallpaperInfo info) {
171 mInfo = info;
172 }
173
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700174 public void onServiceConnected(ComponentName name, IBinder service) {
175 synchronized (mLock) {
176 if (mWallpaperConnection == this) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800177 mLastDiedTime = SystemClock.uptimeMillis();
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700178 mService = IWallpaperService.Stub.asInterface(service);
179 attachServiceLocked(this);
Dianne Hackborneb034652009-09-07 00:49:58 -0700180 // XXX should probably do saveSettingsLocked() later
181 // when we have an engine, but I'm not sure about
182 // locking there and anyway we always need to be able to
183 // recover if there is something wrong.
184 saveSettingsLocked();
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700185 }
186 }
187 }
188
189 public void onServiceDisconnected(ComponentName name) {
190 synchronized (mLock) {
191 mService = null;
192 mEngine = null;
Dianne Hackborn0cd48872009-08-13 18:51:59 -0700193 if (mWallpaperConnection == this) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800194 Slog.w(TAG, "Wallpaper service gone: " + mWallpaperComponent);
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800195 if (!mWallpaperUpdating && (mLastDiedTime+MIN_WALLPAPER_CRASH_TIME)
196 > SystemClock.uptimeMillis()) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800197 Slog.w(TAG, "Reverting to built-in wallpaper!");
Dianne Hackborn9ea31632011-08-05 14:43:50 -0700198 bindWallpaperComponentLocked(null, true);
Dianne Hackborn0cd48872009-08-13 18:51:59 -0700199 }
200 }
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700201 }
202 }
203
204 public void attachEngine(IWallpaperEngine engine) {
205 mEngine = engine;
206 }
207
208 public ParcelFileDescriptor setWallpaper(String name) {
209 synchronized (mLock) {
210 if (mWallpaperConnection == this) {
Dianne Hackborneb034652009-09-07 00:49:58 -0700211 return updateWallpaperBitmapLocked(name);
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700212 }
213 return null;
214 }
215 }
216 }
217
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800218 class MyPackageMonitor extends PackageMonitor {
219 @Override
220 public void onPackageUpdateFinished(String packageName, int uid) {
221 synchronized (mLock) {
222 if (mWallpaperComponent != null &&
223 mWallpaperComponent.getPackageName().equals(packageName)) {
224 mWallpaperUpdating = false;
225 ComponentName comp = mWallpaperComponent;
226 clearWallpaperComponentLocked();
Dianne Hackborn9ea31632011-08-05 14:43:50 -0700227 bindWallpaperComponentLocked(comp, false);
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800228 }
229 }
230 }
231
232 @Override
233 public void onPackageUpdateStarted(String packageName, int uid) {
234 synchronized (mLock) {
235 if (mWallpaperComponent != null &&
236 mWallpaperComponent.getPackageName().equals(packageName)) {
237 mWallpaperUpdating = true;
238 }
239 }
240 }
241
242 @Override
243 public boolean onHandleForceStop(Intent intent, String[] packages, int uid, boolean doit) {
244 return doPackagesChanged(doit);
245 }
246
247 @Override
248 public void onSomePackagesChanged() {
249 doPackagesChanged(true);
250 }
251
252 boolean doPackagesChanged(boolean doit) {
253 boolean changed = false;
254 synchronized (mLock) {
255 if (mWallpaperComponent != null) {
256 int change = isPackageDisappearing(mWallpaperComponent.getPackageName());
257 if (change == PACKAGE_PERMANENT_CHANGE
258 || change == PACKAGE_TEMPORARY_CHANGE) {
259 changed = true;
260 if (doit) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800261 Slog.w(TAG, "Wallpaper uninstalled, removing: " + mWallpaperComponent);
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800262 clearWallpaperLocked();
263 }
264 }
265 }
266 if (mNextWallpaperComponent != null) {
267 int change = isPackageDisappearing(mNextWallpaperComponent.getPackageName());
268 if (change == PACKAGE_PERMANENT_CHANGE
269 || change == PACKAGE_TEMPORARY_CHANGE) {
270 mNextWallpaperComponent = null;
271 }
272 }
273 if (mWallpaperComponent != null
274 && isPackageModified(mWallpaperComponent.getPackageName())) {
275 try {
276 mContext.getPackageManager().getServiceInfo(
277 mWallpaperComponent, 0);
278 } catch (NameNotFoundException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800279 Slog.w(TAG, "Wallpaper component gone, removing: " + mWallpaperComponent);
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800280 clearWallpaperLocked();
281 }
282 }
283 if (mNextWallpaperComponent != null
284 && isPackageModified(mNextWallpaperComponent.getPackageName())) {
285 try {
286 mContext.getPackageManager().getServiceInfo(
287 mNextWallpaperComponent, 0);
288 } catch (NameNotFoundException e) {
289 mNextWallpaperComponent = null;
290 }
291 }
292 }
293 return changed;
294 }
295 }
296
Dianne Hackborn8cc6a502009-08-05 21:29:42 -0700297 public WallpaperManagerService(Context context) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800298 if (DEBUG) Slog.v(TAG, "WallpaperService startup");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800299 mContext = context;
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700300 mIWindowManager = IWindowManager.Stub.asInterface(
301 ServiceManager.getService(Context.WINDOW_SERVICE));
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800302 mMonitor = new MyPackageMonitor();
303 mMonitor.register(context, true);
Joe Onoratoe712ee32009-07-29 16:23:58 -0700304 WALLPAPER_DIR.mkdirs();
Joe Onorato9bb8fd72009-07-28 18:24:51 -0700305 loadSettingsLocked();
Joe Onoratoe712ee32009-07-29 16:23:58 -0700306 mWallpaperObserver.startWatching();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800307 }
308
309 @Override
310 protected void finalize() throws Throwable {
311 super.finalize();
312 mWallpaperObserver.stopWatching();
313 }
314
Dianne Hackbornf21adf62009-08-13 10:20:21 -0700315 public void systemReady() {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800316 if (DEBUG) Slog.v(TAG, "systemReady");
Dianne Hackbornf21adf62009-08-13 10:20:21 -0700317 synchronized (mLock) {
318 try {
Dianne Hackborn9ea31632011-08-05 14:43:50 -0700319 bindWallpaperComponentLocked(mNextWallpaperComponent, false);
Dianne Hackbornf21adf62009-08-13 10:20:21 -0700320 } catch (RuntimeException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800321 Slog.w(TAG, "Failure starting previous wallpaper", e);
Dianne Hackbornf21adf62009-08-13 10:20:21 -0700322 try {
Dianne Hackborn9ea31632011-08-05 14:43:50 -0700323 bindWallpaperComponentLocked(null, false);
Dianne Hackbornf21adf62009-08-13 10:20:21 -0700324 } catch (RuntimeException e2) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800325 Slog.w(TAG, "Failure starting default wallpaper", e2);
Dianne Hackbornf21adf62009-08-13 10:20:21 -0700326 clearWallpaperComponentLocked();
327 }
328 }
329 }
330 }
331
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800332 public void clearWallpaper() {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800333 if (DEBUG) Slog.v(TAG, "clearWallpaper");
Joe Onorato9bb8fd72009-07-28 18:24:51 -0700334 synchronized (mLock) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800335 clearWallpaperLocked();
336 }
337 }
338
339 public void clearWallpaperLocked() {
340 File f = WALLPAPER_FILE;
341 if (f.exists()) {
342 f.delete();
343 }
344 final long ident = Binder.clearCallingIdentity();
345 try {
Dianne Hackborn9ea31632011-08-05 14:43:50 -0700346 bindWallpaperComponentLocked(null, false);
Dianne Hackbornc9421ba2010-03-11 22:23:46 -0800347 } catch (IllegalArgumentException e) {
348 // This can happen if the default wallpaper component doesn't
349 // exist. This should be a system configuration problem, but
350 // let's not let it crash the system and just live with no
351 // wallpaper.
352 Slog.e(TAG, "Default wallpaper component not found!", e);
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800353 } finally {
354 Binder.restoreCallingIdentity(ident);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800355 }
356 }
357
358 public void setDimensionHints(int width, int height) throws RemoteException {
359 checkPermission(android.Manifest.permission.SET_WALLPAPER_HINTS);
360
361 if (width <= 0 || height <= 0) {
362 throw new IllegalArgumentException("width and height must be > 0");
363 }
364
Joe Onorato9bb8fd72009-07-28 18:24:51 -0700365 synchronized (mLock) {
366 if (width != mWidth || height != mHeight) {
367 mWidth = width;
368 mHeight = height;
369 saveSettingsLocked();
Dianne Hackborn284ac932009-08-28 10:34:25 -0700370 if (mWallpaperConnection != null) {
371 if (mWallpaperConnection.mEngine != null) {
372 try {
373 mWallpaperConnection.mEngine.setDesiredSize(
374 width, height);
375 } catch (RemoteException e) {
376 }
377 notifyCallbacksLocked();
378 }
379 }
Joe Onorato9bb8fd72009-07-28 18:24:51 -0700380 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800381 }
382 }
383
384 public int getWidthHint() throws RemoteException {
Joe Onorato9bb8fd72009-07-28 18:24:51 -0700385 synchronized (mLock) {
386 return mWidth;
387 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800388 }
389
390 public int getHeightHint() throws RemoteException {
Joe Onorato9bb8fd72009-07-28 18:24:51 -0700391 synchronized (mLock) {
392 return mHeight;
393 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800394 }
395
Dianne Hackborn284ac932009-08-28 10:34:25 -0700396 public ParcelFileDescriptor getWallpaper(IWallpaperManagerCallback cb,
397 Bundle outParams) {
Joe Onorato9bb8fd72009-07-28 18:24:51 -0700398 synchronized (mLock) {
399 try {
Dianne Hackborn284ac932009-08-28 10:34:25 -0700400 if (outParams != null) {
401 outParams.putInt("width", mWidth);
402 outParams.putInt("height", mHeight);
403 }
Joe Onorato9bb8fd72009-07-28 18:24:51 -0700404 mCallbacks.register(cb);
405 File f = WALLPAPER_FILE;
406 if (!f.exists()) {
407 return null;
408 }
409 return ParcelFileDescriptor.open(f, MODE_READ_ONLY);
410 } catch (FileNotFoundException e) {
411 /* Shouldn't happen as we check to see if the file exists */
Joe Onorato8a9b2202010-02-26 18:56:32 -0800412 Slog.w(TAG, "Error getting wallpaper", e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800413 }
Joe Onorato9bb8fd72009-07-28 18:24:51 -0700414 return null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800415 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800416 }
417
Dianne Hackborneb034652009-09-07 00:49:58 -0700418 public WallpaperInfo getWallpaperInfo() {
419 synchronized (mLock) {
420 if (mWallpaperConnection != null) {
421 return mWallpaperConnection.mInfo;
422 }
423 return null;
424 }
425 }
426
Joe Onorato9bb8fd72009-07-28 18:24:51 -0700427 public ParcelFileDescriptor setWallpaper(String name) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800428 if (DEBUG) Slog.v(TAG, "setWallpaper");
Mike Clerona428b2c2009-11-15 22:53:08 -0800429
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800430 checkPermission(android.Manifest.permission.SET_WALLPAPER);
Joe Onorato9bb8fd72009-07-28 18:24:51 -0700431 synchronized (mLock) {
Dianne Hackborn0cd48872009-08-13 18:51:59 -0700432 final long ident = Binder.clearCallingIdentity();
433 try {
434 ParcelFileDescriptor pfd = updateWallpaperBitmapLocked(name);
435 if (pfd != null) {
Mike Cleron322b6ee2009-11-12 07:45:47 -0800436 // Bind the wallpaper to an ImageWallpaper
Dianne Hackborn9ea31632011-08-05 14:43:50 -0700437 bindWallpaperComponentLocked(mImageWallpaperComponent, false);
Dianne Hackborn0cd48872009-08-13 18:51:59 -0700438 saveSettingsLocked();
439 }
440 return pfd;
441 } finally {
442 Binder.restoreCallingIdentity(ident);
Joe Onorato9bb8fd72009-07-28 18:24:51 -0700443 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800444 }
445 }
446
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700447 ParcelFileDescriptor updateWallpaperBitmapLocked(String name) {
448 if (name == null) name = "";
449 try {
450 ParcelFileDescriptor fd = ParcelFileDescriptor.open(WALLPAPER_FILE,
451 MODE_CREATE|MODE_READ_WRITE);
452 mName = name;
453 return fd;
454 } catch (FileNotFoundException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800455 Slog.w(TAG, "Error setting wallpaper", e);
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700456 }
457 return null;
458 }
459
460 public void setWallpaperComponent(ComponentName name) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800461 if (DEBUG) Slog.v(TAG, "setWallpaperComponent name=" + name);
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700462 checkPermission(android.Manifest.permission.SET_WALLPAPER_COMPONENT);
463 synchronized (mLock) {
464 final long ident = Binder.clearCallingIdentity();
465 try {
Dianne Hackborn9ea31632011-08-05 14:43:50 -0700466 bindWallpaperComponentLocked(name, false);
Dianne Hackbornf21adf62009-08-13 10:20:21 -0700467 } finally {
468 Binder.restoreCallingIdentity(ident);
469 }
470 }
471 }
472
Dianne Hackborn9ea31632011-08-05 14:43:50 -0700473 void bindWallpaperComponentLocked(ComponentName componentName, boolean force) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800474 if (DEBUG) Slog.v(TAG, "bindWallpaperComponentLocked: componentName=" + componentName);
Mike Clerona428b2c2009-11-15 22:53:08 -0800475
Dianne Hackbornf21adf62009-08-13 10:20:21 -0700476 // Has the component changed?
Dianne Hackborn9ea31632011-08-05 14:43:50 -0700477 if (!force) {
478 if (mWallpaperConnection != null) {
479 if (mWallpaperComponent == null) {
480 if (componentName == null) {
481 if (DEBUG) Slog.v(TAG, "bindWallpaperComponentLocked: still using default");
482 // Still using default wallpaper.
483 return;
484 }
485 } else if (mWallpaperComponent.equals(componentName)) {
486 // Changing to same wallpaper.
487 if (DEBUG) Slog.v(TAG, "same wallpaper");
Dianne Hackbornf21adf62009-08-13 10:20:21 -0700488 return;
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700489 }
Dianne Hackbornf21adf62009-08-13 10:20:21 -0700490 }
491 }
492
493 try {
Mike Clerona428b2c2009-11-15 22:53:08 -0800494 if (componentName == null) {
Mike Cleron322b6ee2009-11-12 07:45:47 -0800495 String defaultComponent =
496 mContext.getString(com.android.internal.R.string.default_wallpaper_component);
Mike Clerona428b2c2009-11-15 22:53:08 -0800497 if (defaultComponent != null) {
Mike Cleron322b6ee2009-11-12 07:45:47 -0800498 // See if there is a default wallpaper component specified
Mike Clerona428b2c2009-11-15 22:53:08 -0800499 componentName = ComponentName.unflattenFromString(defaultComponent);
Joe Onorato8a9b2202010-02-26 18:56:32 -0800500 if (DEBUG) Slog.v(TAG, "Use default component wallpaper:" + componentName);
Mike Cleron322b6ee2009-11-12 07:45:47 -0800501 }
Mike Clerona428b2c2009-11-15 22:53:08 -0800502 if (componentName == null) {
Mike Cleron322b6ee2009-11-12 07:45:47 -0800503 // Fall back to static image wallpaper
Mike Clerona428b2c2009-11-15 22:53:08 -0800504 componentName = mImageWallpaperComponent;
Mike Cleron322b6ee2009-11-12 07:45:47 -0800505 //clearWallpaperComponentLocked();
506 //return;
Joe Onorato8a9b2202010-02-26 18:56:32 -0800507 if (DEBUG) Slog.v(TAG, "Using image wallpaper");
Mike Cleron322b6ee2009-11-12 07:45:47 -0800508 }
Dianne Hackbornf21adf62009-08-13 10:20:21 -0700509 }
Mike Clerona428b2c2009-11-15 22:53:08 -0800510 ServiceInfo si = mContext.getPackageManager().getServiceInfo(componentName,
Dianne Hackbornf21adf62009-08-13 10:20:21 -0700511 PackageManager.GET_META_DATA | PackageManager.GET_PERMISSIONS);
512 if (!android.Manifest.permission.BIND_WALLPAPER.equals(si.permission)) {
513 throw new SecurityException("Selected service does not require "
514 + android.Manifest.permission.BIND_WALLPAPER
Mike Clerona428b2c2009-11-15 22:53:08 -0800515 + ": " + componentName);
Dianne Hackbornf21adf62009-08-13 10:20:21 -0700516 }
517
Dianne Hackborneb034652009-09-07 00:49:58 -0700518 WallpaperInfo wi = null;
519
Dianne Hackbornf21adf62009-08-13 10:20:21 -0700520 Intent intent = new Intent(WallpaperService.SERVICE_INTERFACE);
Mike Clerona428b2c2009-11-15 22:53:08 -0800521 if (componentName != null && !componentName.equals(mImageWallpaperComponent)) {
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700522 // Make sure the selected service is actually a wallpaper service.
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700523 List<ResolveInfo> ris = mContext.getPackageManager()
Dianne Hackborneb034652009-09-07 00:49:58 -0700524 .queryIntentServices(intent, PackageManager.GET_META_DATA);
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700525 for (int i=0; i<ris.size(); i++) {
526 ServiceInfo rsi = ris.get(i).serviceInfo;
527 if (rsi.name.equals(si.name) &&
528 rsi.packageName.equals(si.packageName)) {
Dianne Hackborneb034652009-09-07 00:49:58 -0700529 try {
530 wi = new WallpaperInfo(mContext, ris.get(i));
531 } catch (XmlPullParserException e) {
532 throw new IllegalArgumentException(e);
533 } catch (IOException e) {
534 throw new IllegalArgumentException(e);
535 }
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700536 break;
537 }
538 }
Dianne Hackborneb034652009-09-07 00:49:58 -0700539 if (wi == null) {
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700540 throw new SecurityException("Selected service is not a wallpaper: "
Mike Clerona428b2c2009-11-15 22:53:08 -0800541 + componentName);
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700542 }
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700543 }
Dianne Hackbornf21adf62009-08-13 10:20:21 -0700544
545 // Bind the service!
Joe Onorato8a9b2202010-02-26 18:56:32 -0800546 if (DEBUG) Slog.v(TAG, "Binding to:" + componentName);
Dianne Hackborneb034652009-09-07 00:49:58 -0700547 WallpaperConnection newConn = new WallpaperConnection(wi);
Mike Clerona428b2c2009-11-15 22:53:08 -0800548 intent.setComponent(componentName);
Dianne Hackborndd9b82c2009-09-03 00:18:47 -0700549 intent.putExtra(Intent.EXTRA_CLIENT_LABEL,
550 com.android.internal.R.string.wallpaper_binding_label);
551 intent.putExtra(Intent.EXTRA_CLIENT_INTENT, PendingIntent.getActivity(
Dianne Hackborneb034652009-09-07 00:49:58 -0700552 mContext, 0,
553 Intent.createChooser(new Intent(Intent.ACTION_SET_WALLPAPER),
554 mContext.getText(com.android.internal.R.string.chooser_wallpaper)),
555 0));
Dianne Hackbornf21adf62009-08-13 10:20:21 -0700556 if (!mContext.bindService(intent, newConn,
557 Context.BIND_AUTO_CREATE)) {
558 throw new IllegalArgumentException("Unable to bind service: "
Mike Cleron322b6ee2009-11-12 07:45:47 -0800559 + componentName);
Dianne Hackbornf21adf62009-08-13 10:20:21 -0700560 }
561
562 clearWallpaperComponentLocked();
Mike Cleron322b6ee2009-11-12 07:45:47 -0800563 mWallpaperComponent = componentName;
Dianne Hackbornf21adf62009-08-13 10:20:21 -0700564 mWallpaperConnection = newConn;
Dianne Hackborn0cd48872009-08-13 18:51:59 -0700565 mLastDiedTime = SystemClock.uptimeMillis();
Dianne Hackbornf21adf62009-08-13 10:20:21 -0700566 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800567 if (DEBUG) Slog.v(TAG, "Adding window token: " + newConn.mToken);
Dianne Hackbornf21adf62009-08-13 10:20:21 -0700568 mIWindowManager.addWindowToken(newConn.mToken,
569 WindowManager.LayoutParams.TYPE_WALLPAPER);
570 } catch (RemoteException e) {
571 }
572
573 } catch (PackageManager.NameNotFoundException e) {
Mike Cleron322b6ee2009-11-12 07:45:47 -0800574 throw new IllegalArgumentException("Unknown component " + componentName);
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700575 }
576 }
577
578 void clearWallpaperComponentLocked() {
579 mWallpaperComponent = null;
580 if (mWallpaperConnection != null) {
581 if (mWallpaperConnection.mEngine != null) {
582 try {
583 mWallpaperConnection.mEngine.destroy();
584 } catch (RemoteException e) {
585 }
586 }
587 mContext.unbindService(mWallpaperConnection);
Dianne Hackborne9e9bca2009-08-18 15:08:22 -0700588 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800589 if (DEBUG) Slog.v(TAG, "Removing window token: "
Dianne Hackborne9e9bca2009-08-18 15:08:22 -0700590 + mWallpaperConnection.mToken);
591 mIWindowManager.removeWindowToken(mWallpaperConnection.mToken);
592 } catch (RemoteException e) {
593 }
Vairavan Srinivasanfdfc1b22010-12-23 14:05:44 -0800594 mWallpaperConnection.mService = null;
595 mWallpaperConnection.mEngine = null;
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700596 mWallpaperConnection = null;
597 }
598 }
599
600 void attachServiceLocked(WallpaperConnection conn) {
601 try {
Dianne Hackborn3be63c02009-08-20 19:31:38 -0700602 conn.mService.attach(conn, conn.mToken,
603 WindowManager.LayoutParams.TYPE_WALLPAPER, false,
604 mWidth, mHeight);
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700605 } catch (RemoteException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800606 Slog.w(TAG, "Failed attaching wallpaper; clearing", e);
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800607 if (!mWallpaperUpdating) {
Dianne Hackborn9ea31632011-08-05 14:43:50 -0700608 bindWallpaperComponentLocked(null, false);
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800609 }
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700610 }
611 }
612
Joe Onorato9bb8fd72009-07-28 18:24:51 -0700613 private void notifyCallbacksLocked() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800614 final int n = mCallbacks.beginBroadcast();
615 for (int i = 0; i < n; i++) {
616 try {
617 mCallbacks.getBroadcastItem(i).onWallpaperChanged();
618 } catch (RemoteException e) {
619
620 // The RemoteCallbackList will take care of removing
621 // the dead object for us.
622 }
623 }
624 mCallbacks.finishBroadcast();
625 final Intent intent = new Intent(Intent.ACTION_WALLPAPER_CHANGED);
626 mContext.sendBroadcast(intent);
627 }
628
629 private void checkPermission(String permission) {
Joe Onorato9bb8fd72009-07-28 18:24:51 -0700630 if (PackageManager.PERMISSION_GRANTED!= mContext.checkCallingOrSelfPermission(permission)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800631 throw new SecurityException("Access denied to process: " + Binder.getCallingPid()
632 + ", must have permission " + permission);
633 }
634 }
Joe Onorato9bb8fd72009-07-28 18:24:51 -0700635
636 private static JournaledFile makeJournaledFile() {
637 final String base = "/data/system/wallpaper_info.xml";
638 return new JournaledFile(new File(base), new File(base + ".tmp"));
639 }
640
641 private void saveSettingsLocked() {
642 JournaledFile journal = makeJournaledFile();
643 FileOutputStream stream = null;
644 try {
645 stream = new FileOutputStream(journal.chooseForWrite(), false);
646 XmlSerializer out = new FastXmlSerializer();
647 out.setOutput(stream, "utf-8");
648 out.startDocument(null, true);
649
650 out.startTag(null, "wp");
651 out.attribute(null, "width", Integer.toString(mWidth));
652 out.attribute(null, "height", Integer.toString(mHeight));
653 out.attribute(null, "name", mName);
Dianne Hackborn9ea31632011-08-05 14:43:50 -0700654 if (mWallpaperComponent != null &&
655 !mWallpaperComponent.equals(mImageWallpaperComponent)) {
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700656 out.attribute(null, "component",
657 mWallpaperComponent.flattenToShortString());
658 }
Joe Onorato9bb8fd72009-07-28 18:24:51 -0700659 out.endTag(null, "wp");
660
661 out.endDocument();
662 stream.close();
663 journal.commit();
664 } catch (IOException e) {
665 try {
666 if (stream != null) {
667 stream.close();
668 }
669 } catch (IOException ex) {
670 // Ignore
671 }
672 journal.rollback();
673 }
674 }
675
676 private void loadSettingsLocked() {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800677 if (DEBUG) Slog.v(TAG, "loadSettingsLocked");
Mike Clerona428b2c2009-11-15 22:53:08 -0800678
Joe Onorato9bb8fd72009-07-28 18:24:51 -0700679 JournaledFile journal = makeJournaledFile();
680 FileInputStream stream = null;
681 File file = journal.chooseForRead();
682 boolean success = false;
683 try {
684 stream = new FileInputStream(file);
685 XmlPullParser parser = Xml.newPullParser();
686 parser.setInput(stream, null);
687
688 int type;
Joe Onorato9bb8fd72009-07-28 18:24:51 -0700689 do {
690 type = parser.next();
691 if (type == XmlPullParser.START_TAG) {
692 String tag = parser.getName();
693 if ("wp".equals(tag)) {
694 mWidth = Integer.parseInt(parser.getAttributeValue(null, "width"));
695 mHeight = Integer.parseInt(parser.getAttributeValue(null, "height"));
696 mName = parser.getAttributeValue(null, "name");
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700697 String comp = parser.getAttributeValue(null, "component");
Mike Clerona428b2c2009-11-15 22:53:08 -0800698 mNextWallpaperComponent = comp != null
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700699 ? ComponentName.unflattenFromString(comp)
700 : null;
Dianne Hackborn9ea31632011-08-05 14:43:50 -0700701 if (mNextWallpaperComponent == null ||
702 "android".equals(mNextWallpaperComponent.getPackageName())) {
703 mNextWallpaperComponent = mImageWallpaperComponent;
704 }
Mike Clerona428b2c2009-11-15 22:53:08 -0800705
706 if (DEBUG) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800707 Slog.v(TAG, "mWidth:" + mWidth);
708 Slog.v(TAG, "mHeight:" + mHeight);
709 Slog.v(TAG, "mName:" + mName);
710 Slog.v(TAG, "mNextWallpaperComponent:" + mNextWallpaperComponent);
Mike Clerona428b2c2009-11-15 22:53:08 -0800711 }
Joe Onorato9bb8fd72009-07-28 18:24:51 -0700712 }
713 }
714 } while (type != XmlPullParser.END_DOCUMENT);
715 success = true;
716 } catch (NullPointerException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800717 Slog.w(TAG, "failed parsing " + file + " " + e);
Joe Onorato9bb8fd72009-07-28 18:24:51 -0700718 } catch (NumberFormatException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800719 Slog.w(TAG, "failed parsing " + file + " " + e);
Joe Onorato9bb8fd72009-07-28 18:24:51 -0700720 } catch (XmlPullParserException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800721 Slog.w(TAG, "failed parsing " + file + " " + e);
Joe Onorato9bb8fd72009-07-28 18:24:51 -0700722 } catch (IOException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800723 Slog.w(TAG, "failed parsing " + file + " " + e);
Joe Onorato9bb8fd72009-07-28 18:24:51 -0700724 } catch (IndexOutOfBoundsException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800725 Slog.w(TAG, "failed parsing " + file + " " + e);
Joe Onorato9bb8fd72009-07-28 18:24:51 -0700726 }
727 try {
728 if (stream != null) {
729 stream.close();
730 }
731 } catch (IOException e) {
732 // Ignore
733 }
734
735 if (!success) {
736 mWidth = -1;
737 mHeight = -1;
738 mName = "";
739 }
Dianne Hackborn44bc17c2011-04-20 18:18:51 -0700740
741 // We always want to have some reasonable width hint.
742 WindowManager wm = (WindowManager)mContext.getSystemService(Context.WINDOW_SERVICE);
743 Display d = wm.getDefaultDisplay();
744 int baseSize = d.getMaximumSizeDimension();
745 if (mWidth < baseSize) {
746 mWidth = baseSize;
747 }
748 if (mHeight < baseSize) {
749 mHeight = baseSize;
750 }
Joe Onorato9bb8fd72009-07-28 18:24:51 -0700751 }
752
Brad Fitzpatrick194b19a2010-09-14 11:30:29 -0700753 // Called by SystemBackupAgent after files are restored to disk.
Joe Onorato9bb8fd72009-07-28 18:24:51 -0700754 void settingsRestored() {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800755 if (DEBUG) Slog.v(TAG, "settingsRestored");
Brad Fitzpatrick194b19a2010-09-14 11:30:29 -0700756
Joe Onorato9bb8fd72009-07-28 18:24:51 -0700757 boolean success = false;
758 synchronized (mLock) {
759 loadSettingsLocked();
Mike Clerona428b2c2009-11-15 22:53:08 -0800760 if (mNextWallpaperComponent != null &&
761 !mNextWallpaperComponent.equals(mImageWallpaperComponent)) {
Christopher Tatee3ab4d02009-12-16 14:03:31 -0800762 try {
Dianne Hackborn9ea31632011-08-05 14:43:50 -0700763 bindWallpaperComponentLocked(mNextWallpaperComponent, false);
Christopher Tatee3ab4d02009-12-16 14:03:31 -0800764 } catch (IllegalArgumentException e) {
765 // No such live wallpaper or other failure; fall back to the default
766 // live wallpaper (since the profile being restored indicated that the
767 // user had selected a live rather than static one).
Dianne Hackborn9ea31632011-08-05 14:43:50 -0700768 bindWallpaperComponentLocked(null, false);
Christopher Tatee3ab4d02009-12-16 14:03:31 -0800769 }
Joe Onorato9bb8fd72009-07-28 18:24:51 -0700770 success = true;
771 } else {
Mike Clerona428b2c2009-11-15 22:53:08 -0800772 // If there's a wallpaper name, we use that. If that can't be loaded, then we
773 // use the default.
774 if ("".equals(mName)) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800775 if (DEBUG) Slog.v(TAG, "settingsRestored: name is empty");
Mike Clerona428b2c2009-11-15 22:53:08 -0800776 success = true;
777 } else {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800778 if (DEBUG) Slog.v(TAG, "settingsRestored: attempting to restore named resource");
Mike Clerona428b2c2009-11-15 22:53:08 -0800779 success = restoreNamedResourceLocked();
780 }
Joe Onorato8a9b2202010-02-26 18:56:32 -0800781 if (DEBUG) Slog.v(TAG, "settingsRestored: success=" + success);
Mike Clerona428b2c2009-11-15 22:53:08 -0800782 if (success) {
Dianne Hackborn9ea31632011-08-05 14:43:50 -0700783 bindWallpaperComponentLocked(null, false);
Mike Clerona428b2c2009-11-15 22:53:08 -0800784 }
Joe Onorato9bb8fd72009-07-28 18:24:51 -0700785 }
786 }
787
788 if (!success) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800789 Slog.e(TAG, "Failed to restore wallpaper: '" + mName + "'");
Joe Onorato9bb8fd72009-07-28 18:24:51 -0700790 mName = "";
791 WALLPAPER_FILE.delete();
792 }
Brad Fitzpatrick194b19a2010-09-14 11:30:29 -0700793
794 synchronized (mLock) {
795 saveSettingsLocked();
796 }
Joe Onorato9bb8fd72009-07-28 18:24:51 -0700797 }
798
799 boolean restoreNamedResourceLocked() {
800 if (mName.length() > 4 && "res:".equals(mName.substring(0, 4))) {
801 String resName = mName.substring(4);
802
803 String pkg = null;
804 int colon = resName.indexOf(':');
805 if (colon > 0) {
806 pkg = resName.substring(0, colon);
807 }
808
809 String ident = null;
810 int slash = resName.lastIndexOf('/');
811 if (slash > 0) {
812 ident = resName.substring(slash+1);
813 }
814
815 String type = null;
816 if (colon > 0 && slash > 0 && (slash-colon) > 1) {
817 type = resName.substring(colon+1, slash);
818 }
819
820 if (pkg != null && ident != null && type != null) {
821 int resId = -1;
822 InputStream res = null;
823 FileOutputStream fos = null;
824 try {
825 Context c = mContext.createPackageContext(pkg, Context.CONTEXT_RESTRICTED);
826 Resources r = c.getResources();
827 resId = r.getIdentifier(resName, null, null);
828 if (resId == 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800829 Slog.e(TAG, "couldn't resolve identifier pkg=" + pkg + " type=" + type
Joe Onorato9bb8fd72009-07-28 18:24:51 -0700830 + " ident=" + ident);
831 return false;
832 }
833
834 res = r.openRawResource(resId);
Dianne Hackborn1afd1c92010-03-18 22:47:17 -0700835 if (WALLPAPER_FILE.exists()) {
836 WALLPAPER_FILE.delete();
837 }
Joe Onorato9bb8fd72009-07-28 18:24:51 -0700838 fos = new FileOutputStream(WALLPAPER_FILE);
839
840 byte[] buffer = new byte[32768];
841 int amt;
842 while ((amt=res.read(buffer)) > 0) {
843 fos.write(buffer, 0, amt);
844 }
845 // mWallpaperObserver will notice the close and send the change broadcast
846
Joe Onorato8a9b2202010-02-26 18:56:32 -0800847 Slog.v(TAG, "Restored wallpaper: " + resName);
Joe Onorato9bb8fd72009-07-28 18:24:51 -0700848 return true;
849 } catch (NameNotFoundException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800850 Slog.e(TAG, "Package name " + pkg + " not found");
Joe Onorato9bb8fd72009-07-28 18:24:51 -0700851 } catch (Resources.NotFoundException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800852 Slog.e(TAG, "Resource not found: " + resId);
Joe Onorato9bb8fd72009-07-28 18:24:51 -0700853 } catch (IOException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800854 Slog.e(TAG, "IOException while restoring wallpaper ", e);
Joe Onorato9bb8fd72009-07-28 18:24:51 -0700855 } finally {
856 if (res != null) {
857 try {
858 res.close();
859 } catch (IOException ex) {}
860 }
861 if (fos != null) {
Dianne Hackborn8bdf5932010-10-15 12:54:40 -0700862 FileUtils.sync(fos);
Joe Onorato9bb8fd72009-07-28 18:24:51 -0700863 try {
864 fos.close();
865 } catch (IOException ex) {}
866 }
867 }
868 }
869 }
870 return false;
871 }
Dianne Hackborneb034652009-09-07 00:49:58 -0700872
873 @Override
874 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
875 if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DUMP)
876 != PackageManager.PERMISSION_GRANTED) {
877
878 pw.println("Permission Denial: can't dump wallpaper service from from pid="
879 + Binder.getCallingPid()
880 + ", uid=" + Binder.getCallingUid());
881 return;
882 }
883
884 synchronized (mLock) {
885 pw.println("Current Wallpaper Service state:");
886 pw.print(" mWidth="); pw.print(mWidth);
887 pw.print(" mHeight="); pw.println(mHeight);
888 pw.print(" mName="); pw.println(mName);
889 pw.print(" mWallpaperComponent="); pw.println(mWallpaperComponent);
890 if (mWallpaperConnection != null) {
891 WallpaperConnection conn = mWallpaperConnection;
892 pw.print(" Wallpaper connection ");
893 pw.print(conn); pw.println(":");
894 pw.print(" mInfo.component="); pw.println(conn.mInfo.getComponent());
895 pw.print(" mToken="); pw.println(conn.mToken);
896 pw.print(" mService="); pw.println(conn.mService);
897 pw.print(" mEngine="); pw.println(conn.mEngine);
898 pw.print(" mLastDiedTime=");
899 pw.println(mLastDiedTime - SystemClock.uptimeMillis());
900 }
901 }
902 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800903}