blob: 9a293a97b89a572382d40daf669e7b1794b6ff87 [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;
Christopher Tate111bd4a2009-06-24 17:29:38 -070024import android.backup.BackupManager;
Dianne Hackborn4c62fc02009-08-08 20:40:27 -070025import android.content.ComponentName;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080026import android.content.Context;
27import android.content.Intent;
Dianne Hackborn4c62fc02009-08-08 20:40:27 -070028import android.content.ServiceConnection;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080029import android.content.pm.PackageManager;
Dianne Hackborn4c62fc02009-08-08 20:40:27 -070030import android.content.pm.ResolveInfo;
31import android.content.pm.ServiceInfo;
Joe Onorato9bb8fd72009-07-28 18:24:51 -070032import android.content.pm.PackageManager.NameNotFoundException;
33import android.content.res.Resources;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080034import android.os.Binder;
Dianne Hackborn4c62fc02009-08-08 20:40:27 -070035import android.os.IBinder;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080036import android.os.RemoteException;
37import android.os.FileObserver;
38import android.os.ParcelFileDescriptor;
39import android.os.RemoteCallbackList;
Dianne Hackborn4c62fc02009-08-08 20:40:27 -070040import android.os.ServiceManager;
Dianne Hackborn0cd48872009-08-13 18:51:59 -070041import android.os.SystemClock;
Dianne Hackborn4c62fc02009-08-08 20:40:27 -070042import android.service.wallpaper.IWallpaperConnection;
43import android.service.wallpaper.IWallpaperEngine;
44import android.service.wallpaper.IWallpaperService;
45import android.service.wallpaper.WallpaperService;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080046import android.util.Log;
Joe Onorato9bb8fd72009-07-28 18:24:51 -070047import android.util.Xml;
Dianne Hackborn4c62fc02009-08-08 20:40:27 -070048import android.view.IWindowManager;
49import android.view.WindowManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080050
Joe Onorato9bb8fd72009-07-28 18:24:51 -070051import java.io.IOException;
52import java.io.InputStream;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080053import java.io.File;
54import java.io.FileNotFoundException;
Joe Onorato9bb8fd72009-07-28 18:24:51 -070055import java.io.FileInputStream;
56import java.io.FileOutputStream;
Dianne Hackborn4c62fc02009-08-08 20:40:27 -070057import java.util.List;
Joe Onorato9bb8fd72009-07-28 18:24:51 -070058
59import org.xmlpull.v1.XmlPullParser;
60import org.xmlpull.v1.XmlPullParserException;
61import org.xmlpull.v1.XmlSerializer;
62
Dianne Hackbornf21adf62009-08-13 10:20:21 -070063import com.android.internal.service.wallpaper.ImageWallpaper;
Joe Onorato9bb8fd72009-07-28 18:24:51 -070064import com.android.internal.util.FastXmlSerializer;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080065
Dianne Hackborn8cc6a502009-08-05 21:29:42 -070066class WallpaperManagerService extends IWallpaperManager.Stub {
Dianne Hackborn4c62fc02009-08-08 20:40:27 -070067 static final String TAG = "WallpaperService";
68 static final boolean DEBUG = true;
Joe Onorato9bb8fd72009-07-28 18:24:51 -070069
Dianne Hackborn4c62fc02009-08-08 20:40:27 -070070 Object mLock = new Object();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080071
Dianne Hackborn0cd48872009-08-13 18:51:59 -070072 /**
73 * Minimum time between crashes of a wallpaper service for us to consider
74 * restarting it vs. just reverting to the static wallpaper.
75 */
76 static final long MIN_WALLPAPER_CRASH_TIME = 10000;
77
78 static final File WALLPAPER_DIR = new File(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080079 "/data/data/com.android.settings/files");
Dianne Hackborn0cd48872009-08-13 18:51:59 -070080 static final String WALLPAPER = "wallpaper";
81 static final File WALLPAPER_FILE = new File(WALLPAPER_DIR, WALLPAPER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080082
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080083 /**
84 * List of callbacks registered they should each be notified
85 * when the wallpaper is changed.
86 */
Dianne Hackborn8cc6a502009-08-05 21:29:42 -070087 private final RemoteCallbackList<IWallpaperManagerCallback> mCallbacks
88 = new RemoteCallbackList<IWallpaperManagerCallback>();
Joe Onorato9bb8fd72009-07-28 18:24:51 -070089
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080090 /**
91 * Observes the wallpaper for changes and notifies all IWallpaperServiceCallbacks
92 * that the wallpaper has changed. The CREATE is triggered when there is no
93 * wallpaper set and is created for the first time. The CLOSE_WRITE is triggered
94 * everytime the wallpaper is changed.
95 */
96 private final FileObserver mWallpaperObserver = new FileObserver(
Joe Onorato9bb8fd72009-07-28 18:24:51 -070097 WALLPAPER_DIR.getAbsolutePath(), CREATE | CLOSE_WRITE | DELETE | DELETE_SELF) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080098 @Override
99 public void onEvent(int event, String path) {
Joe Onoratoe712ee32009-07-29 16:23:58 -0700100 if (path == null) {
101 return;
102 }
Joe Onorato9bb8fd72009-07-28 18:24:51 -0700103 synchronized (mLock) {
Joe Onorato9bb8fd72009-07-28 18:24:51 -0700104 // changing the wallpaper means we'll need to back up the new one
105 long origId = Binder.clearCallingIdentity();
106 BackupManager bm = new BackupManager(mContext);
107 bm.dataChanged();
108 Binder.restoreCallingIdentity(origId);
109
110 File changedFile = new File(WALLPAPER_DIR, path);
111 if (WALLPAPER_FILE.equals(changedFile)) {
112 notifyCallbacksLocked();
113 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800114 }
115 }
116 };
117
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700118 final Context mContext;
119 final IWindowManager mIWindowManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800120
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700121 int mWidth = -1;
122 int mHeight = -1;
123 String mName = "";
124 ComponentName mWallpaperComponent;
125 WallpaperConnection mWallpaperConnection;
Dianne Hackborn0cd48872009-08-13 18:51:59 -0700126 long mLastDiedTime;
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700127
128 class WallpaperConnection extends IWallpaperConnection.Stub
129 implements ServiceConnection {
130 final Binder mToken = new Binder();
131 IWallpaperService mService;
132 IWallpaperEngine mEngine;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800133
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700134 public void onServiceConnected(ComponentName name, IBinder service) {
135 synchronized (mLock) {
136 if (mWallpaperConnection == this) {
137 mService = IWallpaperService.Stub.asInterface(service);
138 attachServiceLocked(this);
139 }
140 }
141 }
142
143 public void onServiceDisconnected(ComponentName name) {
144 synchronized (mLock) {
145 mService = null;
146 mEngine = null;
Dianne Hackborn0cd48872009-08-13 18:51:59 -0700147 if (mWallpaperConnection == this) {
148 Log.w(TAG, "Wallpaper service gone: " + mWallpaperComponent);
149 if ((mLastDiedTime+MIN_WALLPAPER_CRASH_TIME)
150 < SystemClock.uptimeMillis()) {
151 Log.w(TAG, "Reverting to built-in wallpaper!");
152 bindWallpaperComponentLocked(null);
153 }
154 }
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700155 }
156 }
157
158 public void attachEngine(IWallpaperEngine engine) {
159 mEngine = engine;
160 }
161
162 public ParcelFileDescriptor setWallpaper(String name) {
163 synchronized (mLock) {
164 if (mWallpaperConnection == this) {
165 ParcelFileDescriptor pfd = updateWallpaperBitmapLocked(name);
166 if (pfd != null) {
167 saveSettingsLocked();
168 }
169 return pfd;
170 }
171 return null;
172 }
173 }
174 }
175
Dianne Hackborn8cc6a502009-08-05 21:29:42 -0700176 public WallpaperManagerService(Context context) {
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700177 if (DEBUG) Log.d(TAG, "WallpaperService startup");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800178 mContext = context;
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700179 mIWindowManager = IWindowManager.Stub.asInterface(
180 ServiceManager.getService(Context.WINDOW_SERVICE));
Joe Onoratoe712ee32009-07-29 16:23:58 -0700181 WALLPAPER_DIR.mkdirs();
Joe Onorato9bb8fd72009-07-28 18:24:51 -0700182 loadSettingsLocked();
Joe Onoratoe712ee32009-07-29 16:23:58 -0700183 mWallpaperObserver.startWatching();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800184 }
185
186 @Override
187 protected void finalize() throws Throwable {
188 super.finalize();
189 mWallpaperObserver.stopWatching();
190 }
191
Dianne Hackbornf21adf62009-08-13 10:20:21 -0700192 public void systemReady() {
193 synchronized (mLock) {
194 try {
195 bindWallpaperComponentLocked(mWallpaperComponent);
196 } catch (RuntimeException e) {
197 Log.w(TAG, "Failure starting previous wallpaper", e);
198 try {
199 bindWallpaperComponentLocked(null);
200 } catch (RuntimeException e2) {
201 Log.w(TAG, "Failure starting default wallpaper", e2);
202 clearWallpaperComponentLocked();
203 }
204 }
205 }
206 }
207
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800208 public void clearWallpaper() {
Joe Onorato9bb8fd72009-07-28 18:24:51 -0700209 synchronized (mLock) {
210 File f = WALLPAPER_FILE;
211 if (f.exists()) {
212 f.delete();
213 }
Dianne Hackborn0cd48872009-08-13 18:51:59 -0700214 final long ident = Binder.clearCallingIdentity();
215 try {
216 bindWallpaperComponentLocked(null);
217 } finally {
218 Binder.restoreCallingIdentity(ident);
219 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800220 }
221 }
222
223 public void setDimensionHints(int width, int height) throws RemoteException {
224 checkPermission(android.Manifest.permission.SET_WALLPAPER_HINTS);
225
226 if (width <= 0 || height <= 0) {
227 throw new IllegalArgumentException("width and height must be > 0");
228 }
229
Joe Onorato9bb8fd72009-07-28 18:24:51 -0700230 synchronized (mLock) {
231 if (width != mWidth || height != mHeight) {
232 mWidth = width;
233 mHeight = height;
234 saveSettingsLocked();
235 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800236 }
237 }
238
239 public int getWidthHint() throws RemoteException {
Joe Onorato9bb8fd72009-07-28 18:24:51 -0700240 synchronized (mLock) {
241 return mWidth;
242 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800243 }
244
245 public int getHeightHint() throws RemoteException {
Joe Onorato9bb8fd72009-07-28 18:24:51 -0700246 synchronized (mLock) {
247 return mHeight;
248 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800249 }
250
Dianne Hackborn8cc6a502009-08-05 21:29:42 -0700251 public ParcelFileDescriptor getWallpaper(IWallpaperManagerCallback cb) {
Joe Onorato9bb8fd72009-07-28 18:24:51 -0700252 synchronized (mLock) {
253 try {
254 mCallbacks.register(cb);
255 File f = WALLPAPER_FILE;
256 if (!f.exists()) {
257 return null;
258 }
259 return ParcelFileDescriptor.open(f, MODE_READ_ONLY);
260 } catch (FileNotFoundException e) {
261 /* Shouldn't happen as we check to see if the file exists */
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700262 Log.w(TAG, "Error getting wallpaper", e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800263 }
Joe Onorato9bb8fd72009-07-28 18:24:51 -0700264 return null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800265 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800266 }
267
Joe Onorato9bb8fd72009-07-28 18:24:51 -0700268 public ParcelFileDescriptor setWallpaper(String name) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800269 checkPermission(android.Manifest.permission.SET_WALLPAPER);
Joe Onorato9bb8fd72009-07-28 18:24:51 -0700270 synchronized (mLock) {
Dianne Hackborn0cd48872009-08-13 18:51:59 -0700271 final long ident = Binder.clearCallingIdentity();
272 try {
273 ParcelFileDescriptor pfd = updateWallpaperBitmapLocked(name);
274 if (pfd != null) {
275 bindWallpaperComponentLocked(null);
276 saveSettingsLocked();
277 }
278 return pfd;
279 } finally {
280 Binder.restoreCallingIdentity(ident);
Joe Onorato9bb8fd72009-07-28 18:24:51 -0700281 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800282 }
283 }
284
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700285 ParcelFileDescriptor updateWallpaperBitmapLocked(String name) {
286 if (name == null) name = "";
287 try {
288 ParcelFileDescriptor fd = ParcelFileDescriptor.open(WALLPAPER_FILE,
289 MODE_CREATE|MODE_READ_WRITE);
290 mName = name;
291 return fd;
292 } catch (FileNotFoundException e) {
293 Log.w(TAG, "Error setting wallpaper", e);
294 }
295 return null;
296 }
297
298 public void setWallpaperComponent(ComponentName name) {
299 checkPermission(android.Manifest.permission.SET_WALLPAPER_COMPONENT);
300 synchronized (mLock) {
301 final long ident = Binder.clearCallingIdentity();
302 try {
Dianne Hackbornf21adf62009-08-13 10:20:21 -0700303 bindWallpaperComponentLocked(name);
304 } finally {
305 Binder.restoreCallingIdentity(ident);
306 }
307 }
308 }
309
310 void bindWallpaperComponentLocked(ComponentName name) {
311 // Has the component changed?
312 if (mWallpaperConnection != null) {
313 if (mWallpaperComponent == null) {
314 if (name == null) {
315 // Still using default wallpaper.
316 return;
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700317 }
Dianne Hackbornf21adf62009-08-13 10:20:21 -0700318 } else if (mWallpaperComponent.equals(name)) {
319 // Changing to same wallpaper.
320 return;
321 }
322 }
323
324 try {
325 ComponentName realName = name;
326 if (realName == null) {
327 // The default component is our static image wallpaper.
Dianne Hackbornb1ac1a82009-08-14 12:12:31 -0700328 //realName = new ComponentName("android",
329 // ImageWallpaper.class.getName());
330 clearWallpaperComponentLocked();
331 return;
Dianne Hackbornf21adf62009-08-13 10:20:21 -0700332 }
333 ServiceInfo si = mContext.getPackageManager().getServiceInfo(realName,
334 PackageManager.GET_META_DATA | PackageManager.GET_PERMISSIONS);
335 if (!android.Manifest.permission.BIND_WALLPAPER.equals(si.permission)) {
336 throw new SecurityException("Selected service does not require "
337 + android.Manifest.permission.BIND_WALLPAPER
338 + ": " + realName);
339 }
340
341 Intent intent = new Intent(WallpaperService.SERVICE_INTERFACE);
342 if (name != null) {
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700343 // Make sure the selected service is actually a wallpaper service.
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700344 List<ResolveInfo> ris = mContext.getPackageManager()
345 .queryIntentServices(intent, 0);
346 for (int i=0; i<ris.size(); i++) {
347 ServiceInfo rsi = ris.get(i).serviceInfo;
348 if (rsi.name.equals(si.name) &&
349 rsi.packageName.equals(si.packageName)) {
350 ris = null;
351 break;
352 }
353 }
354 if (ris != null) {
355 throw new SecurityException("Selected service is not a wallpaper: "
Dianne Hackbornf21adf62009-08-13 10:20:21 -0700356 + realName);
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700357 }
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700358 }
Dianne Hackbornf21adf62009-08-13 10:20:21 -0700359
360 // Bind the service!
361 WallpaperConnection newConn = new WallpaperConnection();
362 intent.setComponent(realName);
363 if (!mContext.bindService(intent, newConn,
364 Context.BIND_AUTO_CREATE)) {
365 throw new IllegalArgumentException("Unable to bind service: "
366 + name);
367 }
368
369 clearWallpaperComponentLocked();
370 mWallpaperComponent = name;
371 mWallpaperConnection = newConn;
Dianne Hackborn0cd48872009-08-13 18:51:59 -0700372 mLastDiedTime = SystemClock.uptimeMillis();
Dianne Hackbornf21adf62009-08-13 10:20:21 -0700373 try {
374 if (DEBUG) Log.v(TAG, "Adding window token: " + newConn.mToken);
375 mIWindowManager.addWindowToken(newConn.mToken,
376 WindowManager.LayoutParams.TYPE_WALLPAPER);
377 } catch (RemoteException e) {
378 }
379
380 } catch (PackageManager.NameNotFoundException e) {
381 throw new IllegalArgumentException("Unknown component " + name);
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700382 }
383 }
384
385 void clearWallpaperComponentLocked() {
386 mWallpaperComponent = null;
387 if (mWallpaperConnection != null) {
388 if (mWallpaperConnection.mEngine != null) {
389 try {
390 mWallpaperConnection.mEngine.destroy();
391 } catch (RemoteException e) {
392 }
393 }
394 mContext.unbindService(mWallpaperConnection);
395 mWallpaperConnection = null;
396 }
397 }
398
399 void attachServiceLocked(WallpaperConnection conn) {
400 try {
401 conn.mService.attach(conn, conn.mToken, mWidth, mHeight);
402 } catch (RemoteException e) {
403 Log.w(TAG, "Failed attaching wallpaper; clearing", e);
Dianne Hackbornf21adf62009-08-13 10:20:21 -0700404 bindWallpaperComponentLocked(null);
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700405 }
406 }
407
Joe Onorato9bb8fd72009-07-28 18:24:51 -0700408 private void notifyCallbacksLocked() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800409 final int n = mCallbacks.beginBroadcast();
410 for (int i = 0; i < n; i++) {
411 try {
412 mCallbacks.getBroadcastItem(i).onWallpaperChanged();
413 } catch (RemoteException e) {
414
415 // The RemoteCallbackList will take care of removing
416 // the dead object for us.
417 }
418 }
419 mCallbacks.finishBroadcast();
420 final Intent intent = new Intent(Intent.ACTION_WALLPAPER_CHANGED);
421 mContext.sendBroadcast(intent);
422 }
423
424 private void checkPermission(String permission) {
Joe Onorato9bb8fd72009-07-28 18:24:51 -0700425 if (PackageManager.PERMISSION_GRANTED!= mContext.checkCallingOrSelfPermission(permission)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800426 throw new SecurityException("Access denied to process: " + Binder.getCallingPid()
427 + ", must have permission " + permission);
428 }
429 }
Joe Onorato9bb8fd72009-07-28 18:24:51 -0700430
431 private static JournaledFile makeJournaledFile() {
432 final String base = "/data/system/wallpaper_info.xml";
433 return new JournaledFile(new File(base), new File(base + ".tmp"));
434 }
435
436 private void saveSettingsLocked() {
437 JournaledFile journal = makeJournaledFile();
438 FileOutputStream stream = null;
439 try {
440 stream = new FileOutputStream(journal.chooseForWrite(), false);
441 XmlSerializer out = new FastXmlSerializer();
442 out.setOutput(stream, "utf-8");
443 out.startDocument(null, true);
444
445 out.startTag(null, "wp");
446 out.attribute(null, "width", Integer.toString(mWidth));
447 out.attribute(null, "height", Integer.toString(mHeight));
448 out.attribute(null, "name", mName);
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700449 if (mWallpaperComponent != null) {
450 out.attribute(null, "component",
451 mWallpaperComponent.flattenToShortString());
452 }
Joe Onorato9bb8fd72009-07-28 18:24:51 -0700453 out.endTag(null, "wp");
454
455 out.endDocument();
456 stream.close();
457 journal.commit();
458 } catch (IOException e) {
459 try {
460 if (stream != null) {
461 stream.close();
462 }
463 } catch (IOException ex) {
464 // Ignore
465 }
466 journal.rollback();
467 }
468 }
469
470 private void loadSettingsLocked() {
471 JournaledFile journal = makeJournaledFile();
472 FileInputStream stream = null;
473 File file = journal.chooseForRead();
474 boolean success = false;
475 try {
476 stream = new FileInputStream(file);
477 XmlPullParser parser = Xml.newPullParser();
478 parser.setInput(stream, null);
479
480 int type;
Joe Onorato9bb8fd72009-07-28 18:24:51 -0700481 do {
482 type = parser.next();
483 if (type == XmlPullParser.START_TAG) {
484 String tag = parser.getName();
485 if ("wp".equals(tag)) {
486 mWidth = Integer.parseInt(parser.getAttributeValue(null, "width"));
487 mHeight = Integer.parseInt(parser.getAttributeValue(null, "height"));
488 mName = parser.getAttributeValue(null, "name");
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700489 String comp = parser.getAttributeValue(null, "component");
490 mWallpaperComponent = comp != null
491 ? ComponentName.unflattenFromString(comp)
492 : null;
Joe Onorato9bb8fd72009-07-28 18:24:51 -0700493 }
494 }
495 } while (type != XmlPullParser.END_DOCUMENT);
496 success = true;
497 } catch (NullPointerException e) {
Joe Onorato2d9c9e32009-07-29 16:43:06 -0700498 Log.w(TAG, "failed parsing " + file + " " + e);
Joe Onorato9bb8fd72009-07-28 18:24:51 -0700499 } catch (NumberFormatException e) {
Joe Onorato2d9c9e32009-07-29 16:43:06 -0700500 Log.w(TAG, "failed parsing " + file + " " + e);
Joe Onorato9bb8fd72009-07-28 18:24:51 -0700501 } catch (XmlPullParserException e) {
Joe Onorato2d9c9e32009-07-29 16:43:06 -0700502 Log.w(TAG, "failed parsing " + file + " " + e);
Joe Onorato9bb8fd72009-07-28 18:24:51 -0700503 } catch (IOException e) {
Joe Onorato2d9c9e32009-07-29 16:43:06 -0700504 Log.w(TAG, "failed parsing " + file + " " + e);
Joe Onorato9bb8fd72009-07-28 18:24:51 -0700505 } catch (IndexOutOfBoundsException e) {
Joe Onorato2d9c9e32009-07-29 16:43:06 -0700506 Log.w(TAG, "failed parsing " + file + " " + e);
Joe Onorato9bb8fd72009-07-28 18:24:51 -0700507 }
508 try {
509 if (stream != null) {
510 stream.close();
511 }
512 } catch (IOException e) {
513 // Ignore
514 }
515
516 if (!success) {
517 mWidth = -1;
518 mHeight = -1;
519 mName = "";
520 }
521 }
522
523 void settingsRestored() {
524 boolean success = false;
525 synchronized (mLock) {
526 loadSettingsLocked();
527 // If there's a wallpaper name, we use that. If that can't be loaded, then we
528 // use the default.
529 if ("".equals(mName)) {
530 success = true;
531 } else {
532 success = restoreNamedResourceLocked();
533 }
534 }
535
536 if (!success) {
537 Log.e(TAG, "Failed to restore wallpaper: '" + mName + "'");
538 mName = "";
539 WALLPAPER_FILE.delete();
540 }
541 saveSettingsLocked();
542 }
543
544 boolean restoreNamedResourceLocked() {
545 if (mName.length() > 4 && "res:".equals(mName.substring(0, 4))) {
546 String resName = mName.substring(4);
547
548 String pkg = null;
549 int colon = resName.indexOf(':');
550 if (colon > 0) {
551 pkg = resName.substring(0, colon);
552 }
553
554 String ident = null;
555 int slash = resName.lastIndexOf('/');
556 if (slash > 0) {
557 ident = resName.substring(slash+1);
558 }
559
560 String type = null;
561 if (colon > 0 && slash > 0 && (slash-colon) > 1) {
562 type = resName.substring(colon+1, slash);
563 }
564
565 if (pkg != null && ident != null && type != null) {
566 int resId = -1;
567 InputStream res = null;
568 FileOutputStream fos = null;
569 try {
570 Context c = mContext.createPackageContext(pkg, Context.CONTEXT_RESTRICTED);
571 Resources r = c.getResources();
572 resId = r.getIdentifier(resName, null, null);
573 if (resId == 0) {
574 Log.e(TAG, "couldn't resolve identifier pkg=" + pkg + " type=" + type
575 + " ident=" + ident);
576 return false;
577 }
578
579 res = r.openRawResource(resId);
580 fos = new FileOutputStream(WALLPAPER_FILE);
581
582 byte[] buffer = new byte[32768];
583 int amt;
584 while ((amt=res.read(buffer)) > 0) {
585 fos.write(buffer, 0, amt);
586 }
587 // mWallpaperObserver will notice the close and send the change broadcast
588
589 Log.d(TAG, "Restored wallpaper: " + resName);
590 return true;
591 } catch (NameNotFoundException e) {
592 Log.e(TAG, "Package name " + pkg + " not found");
593 } catch (Resources.NotFoundException e) {
594 Log.e(TAG, "Resource not found: " + resId);
595 } catch (IOException e) {
596 Log.e(TAG, "IOException while restoring wallpaper ", e);
597 } finally {
598 if (res != null) {
599 try {
600 res.close();
601 } catch (IOException ex) {}
602 }
603 if (fos != null) {
604 try {
605 fos.close();
606 } catch (IOException ex) {}
607 }
608 }
609 }
610 }
611 return false;
612 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800613}