blob: 3c62aa0177db60e632edc64486346dac95a58640 [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";
Dianne Hackborncbf15042009-08-18 18:29:09 -070068 static final boolean DEBUG = false;
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 Hackborn7341d7a2009-08-14 11:37:52 -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);
Dianne Hackborne9e9bca2009-08-18 15:08:22 -0700395 try {
396 if (DEBUG) Log.v(TAG, "Removing window token: "
397 + mWallpaperConnection.mToken);
398 mIWindowManager.removeWindowToken(mWallpaperConnection.mToken);
399 } catch (RemoteException e) {
400 }
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700401 mWallpaperConnection = null;
402 }
403 }
404
405 void attachServiceLocked(WallpaperConnection conn) {
406 try {
Dianne Hackborn3be63c02009-08-20 19:31:38 -0700407 conn.mService.attach(conn, conn.mToken,
408 WindowManager.LayoutParams.TYPE_WALLPAPER, false,
409 mWidth, mHeight);
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700410 } catch (RemoteException e) {
411 Log.w(TAG, "Failed attaching wallpaper; clearing", e);
Dianne Hackbornf21adf62009-08-13 10:20:21 -0700412 bindWallpaperComponentLocked(null);
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700413 }
414 }
415
Joe Onorato9bb8fd72009-07-28 18:24:51 -0700416 private void notifyCallbacksLocked() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800417 final int n = mCallbacks.beginBroadcast();
418 for (int i = 0; i < n; i++) {
419 try {
420 mCallbacks.getBroadcastItem(i).onWallpaperChanged();
421 } catch (RemoteException e) {
422
423 // The RemoteCallbackList will take care of removing
424 // the dead object for us.
425 }
426 }
427 mCallbacks.finishBroadcast();
428 final Intent intent = new Intent(Intent.ACTION_WALLPAPER_CHANGED);
429 mContext.sendBroadcast(intent);
430 }
431
432 private void checkPermission(String permission) {
Joe Onorato9bb8fd72009-07-28 18:24:51 -0700433 if (PackageManager.PERMISSION_GRANTED!= mContext.checkCallingOrSelfPermission(permission)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800434 throw new SecurityException("Access denied to process: " + Binder.getCallingPid()
435 + ", must have permission " + permission);
436 }
437 }
Joe Onorato9bb8fd72009-07-28 18:24:51 -0700438
439 private static JournaledFile makeJournaledFile() {
440 final String base = "/data/system/wallpaper_info.xml";
441 return new JournaledFile(new File(base), new File(base + ".tmp"));
442 }
443
444 private void saveSettingsLocked() {
445 JournaledFile journal = makeJournaledFile();
446 FileOutputStream stream = null;
447 try {
448 stream = new FileOutputStream(journal.chooseForWrite(), false);
449 XmlSerializer out = new FastXmlSerializer();
450 out.setOutput(stream, "utf-8");
451 out.startDocument(null, true);
452
453 out.startTag(null, "wp");
454 out.attribute(null, "width", Integer.toString(mWidth));
455 out.attribute(null, "height", Integer.toString(mHeight));
456 out.attribute(null, "name", mName);
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700457 if (mWallpaperComponent != null) {
458 out.attribute(null, "component",
459 mWallpaperComponent.flattenToShortString());
460 }
Joe Onorato9bb8fd72009-07-28 18:24:51 -0700461 out.endTag(null, "wp");
462
463 out.endDocument();
464 stream.close();
465 journal.commit();
466 } catch (IOException e) {
467 try {
468 if (stream != null) {
469 stream.close();
470 }
471 } catch (IOException ex) {
472 // Ignore
473 }
474 journal.rollback();
475 }
476 }
477
478 private void loadSettingsLocked() {
479 JournaledFile journal = makeJournaledFile();
480 FileInputStream stream = null;
481 File file = journal.chooseForRead();
482 boolean success = false;
483 try {
484 stream = new FileInputStream(file);
485 XmlPullParser parser = Xml.newPullParser();
486 parser.setInput(stream, null);
487
488 int type;
Joe Onorato9bb8fd72009-07-28 18:24:51 -0700489 do {
490 type = parser.next();
491 if (type == XmlPullParser.START_TAG) {
492 String tag = parser.getName();
493 if ("wp".equals(tag)) {
494 mWidth = Integer.parseInt(parser.getAttributeValue(null, "width"));
495 mHeight = Integer.parseInt(parser.getAttributeValue(null, "height"));
496 mName = parser.getAttributeValue(null, "name");
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700497 String comp = parser.getAttributeValue(null, "component");
498 mWallpaperComponent = comp != null
499 ? ComponentName.unflattenFromString(comp)
500 : null;
Joe Onorato9bb8fd72009-07-28 18:24:51 -0700501 }
502 }
503 } while (type != XmlPullParser.END_DOCUMENT);
504 success = true;
505 } catch (NullPointerException e) {
Joe Onorato2d9c9e32009-07-29 16:43:06 -0700506 Log.w(TAG, "failed parsing " + file + " " + e);
Joe Onorato9bb8fd72009-07-28 18:24:51 -0700507 } catch (NumberFormatException e) {
Joe Onorato2d9c9e32009-07-29 16:43:06 -0700508 Log.w(TAG, "failed parsing " + file + " " + e);
Joe Onorato9bb8fd72009-07-28 18:24:51 -0700509 } catch (XmlPullParserException e) {
Joe Onorato2d9c9e32009-07-29 16:43:06 -0700510 Log.w(TAG, "failed parsing " + file + " " + e);
Joe Onorato9bb8fd72009-07-28 18:24:51 -0700511 } catch (IOException e) {
Joe Onorato2d9c9e32009-07-29 16:43:06 -0700512 Log.w(TAG, "failed parsing " + file + " " + e);
Joe Onorato9bb8fd72009-07-28 18:24:51 -0700513 } catch (IndexOutOfBoundsException e) {
Joe Onorato2d9c9e32009-07-29 16:43:06 -0700514 Log.w(TAG, "failed parsing " + file + " " + e);
Joe Onorato9bb8fd72009-07-28 18:24:51 -0700515 }
516 try {
517 if (stream != null) {
518 stream.close();
519 }
520 } catch (IOException e) {
521 // Ignore
522 }
523
524 if (!success) {
525 mWidth = -1;
526 mHeight = -1;
527 mName = "";
528 }
529 }
530
531 void settingsRestored() {
532 boolean success = false;
533 synchronized (mLock) {
534 loadSettingsLocked();
535 // If there's a wallpaper name, we use that. If that can't be loaded, then we
536 // use the default.
537 if ("".equals(mName)) {
538 success = true;
539 } else {
540 success = restoreNamedResourceLocked();
541 }
542 }
543
544 if (!success) {
545 Log.e(TAG, "Failed to restore wallpaper: '" + mName + "'");
546 mName = "";
547 WALLPAPER_FILE.delete();
548 }
549 saveSettingsLocked();
550 }
551
552 boolean restoreNamedResourceLocked() {
553 if (mName.length() > 4 && "res:".equals(mName.substring(0, 4))) {
554 String resName = mName.substring(4);
555
556 String pkg = null;
557 int colon = resName.indexOf(':');
558 if (colon > 0) {
559 pkg = resName.substring(0, colon);
560 }
561
562 String ident = null;
563 int slash = resName.lastIndexOf('/');
564 if (slash > 0) {
565 ident = resName.substring(slash+1);
566 }
567
568 String type = null;
569 if (colon > 0 && slash > 0 && (slash-colon) > 1) {
570 type = resName.substring(colon+1, slash);
571 }
572
573 if (pkg != null && ident != null && type != null) {
574 int resId = -1;
575 InputStream res = null;
576 FileOutputStream fos = null;
577 try {
578 Context c = mContext.createPackageContext(pkg, Context.CONTEXT_RESTRICTED);
579 Resources r = c.getResources();
580 resId = r.getIdentifier(resName, null, null);
581 if (resId == 0) {
582 Log.e(TAG, "couldn't resolve identifier pkg=" + pkg + " type=" + type
583 + " ident=" + ident);
584 return false;
585 }
586
587 res = r.openRawResource(resId);
588 fos = new FileOutputStream(WALLPAPER_FILE);
589
590 byte[] buffer = new byte[32768];
591 int amt;
592 while ((amt=res.read(buffer)) > 0) {
593 fos.write(buffer, 0, amt);
594 }
595 // mWallpaperObserver will notice the close and send the change broadcast
596
597 Log.d(TAG, "Restored wallpaper: " + resName);
598 return true;
599 } catch (NameNotFoundException e) {
600 Log.e(TAG, "Package name " + pkg + " not found");
601 } catch (Resources.NotFoundException e) {
602 Log.e(TAG, "Resource not found: " + resId);
603 } catch (IOException e) {
604 Log.e(TAG, "IOException while restoring wallpaper ", e);
605 } finally {
606 if (res != null) {
607 try {
608 res.close();
609 } catch (IOException ex) {}
610 }
611 if (fos != null) {
612 try {
613 fos.close();
614 } catch (IOException ex) {}
615 }
616 }
617 }
618 }
619 return false;
620 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800621}