blob: c101463b2a3016f391e51542595684c1f62abf4a [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 Hackborn284ac932009-08-28 10:34:25 -070035import android.os.Bundle;
Dianne Hackborn4c62fc02009-08-08 20:40:27 -070036import android.os.IBinder;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080037import android.os.RemoteException;
38import android.os.FileObserver;
39import android.os.ParcelFileDescriptor;
40import android.os.RemoteCallbackList;
Dianne Hackborn4c62fc02009-08-08 20:40:27 -070041import android.os.ServiceManager;
Dianne Hackborn0cd48872009-08-13 18:51:59 -070042import android.os.SystemClock;
Dianne Hackborn4c62fc02009-08-08 20:40:27 -070043import android.service.wallpaper.IWallpaperConnection;
44import android.service.wallpaper.IWallpaperEngine;
45import android.service.wallpaper.IWallpaperService;
46import android.service.wallpaper.WallpaperService;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080047import android.util.Log;
Joe Onorato9bb8fd72009-07-28 18:24:51 -070048import android.util.Xml;
Dianne Hackborn4c62fc02009-08-08 20:40:27 -070049import android.view.IWindowManager;
50import android.view.WindowManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080051
Joe Onorato9bb8fd72009-07-28 18:24:51 -070052import java.io.IOException;
53import java.io.InputStream;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080054import java.io.File;
55import java.io.FileNotFoundException;
Joe Onorato9bb8fd72009-07-28 18:24:51 -070056import java.io.FileInputStream;
57import java.io.FileOutputStream;
Dianne Hackborn4c62fc02009-08-08 20:40:27 -070058import java.util.List;
Joe Onorato9bb8fd72009-07-28 18:24:51 -070059
60import org.xmlpull.v1.XmlPullParser;
61import org.xmlpull.v1.XmlPullParserException;
62import org.xmlpull.v1.XmlSerializer;
63
Dianne Hackbornf21adf62009-08-13 10:20:21 -070064import com.android.internal.service.wallpaper.ImageWallpaper;
Joe Onorato9bb8fd72009-07-28 18:24:51 -070065import com.android.internal.util.FastXmlSerializer;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080066
Dianne Hackborn8cc6a502009-08-05 21:29:42 -070067class WallpaperManagerService extends IWallpaperManager.Stub {
Dianne Hackborn4c62fc02009-08-08 20:40:27 -070068 static final String TAG = "WallpaperService";
Dianne Hackborncbf15042009-08-18 18:29:09 -070069 static final boolean DEBUG = false;
Joe Onorato9bb8fd72009-07-28 18:24:51 -070070
Dianne Hackborn4c62fc02009-08-08 20:40:27 -070071 Object mLock = new Object();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080072
Dianne Hackborn0cd48872009-08-13 18:51:59 -070073 /**
74 * Minimum time between crashes of a wallpaper service for us to consider
75 * restarting it vs. just reverting to the static wallpaper.
76 */
77 static final long MIN_WALLPAPER_CRASH_TIME = 10000;
78
79 static final File WALLPAPER_DIR = new File(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080080 "/data/data/com.android.settings/files");
Dianne Hackborn0cd48872009-08-13 18:51:59 -070081 static final String WALLPAPER = "wallpaper";
82 static final File WALLPAPER_FILE = new File(WALLPAPER_DIR, WALLPAPER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080083
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080084 /**
85 * List of callbacks registered they should each be notified
86 * when the wallpaper is changed.
87 */
Dianne Hackborn8cc6a502009-08-05 21:29:42 -070088 private final RemoteCallbackList<IWallpaperManagerCallback> mCallbacks
89 = new RemoteCallbackList<IWallpaperManagerCallback>();
Joe Onorato9bb8fd72009-07-28 18:24:51 -070090
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080091 /**
92 * Observes the wallpaper for changes and notifies all IWallpaperServiceCallbacks
93 * that the wallpaper has changed. The CREATE is triggered when there is no
94 * wallpaper set and is created for the first time. The CLOSE_WRITE is triggered
95 * everytime the wallpaper is changed.
96 */
97 private final FileObserver mWallpaperObserver = new FileObserver(
Joe Onorato9bb8fd72009-07-28 18:24:51 -070098 WALLPAPER_DIR.getAbsolutePath(), CREATE | CLOSE_WRITE | DELETE | DELETE_SELF) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080099 @Override
100 public void onEvent(int event, String path) {
Joe Onoratoe712ee32009-07-29 16:23:58 -0700101 if (path == null) {
102 return;
103 }
Joe Onorato9bb8fd72009-07-28 18:24:51 -0700104 synchronized (mLock) {
Joe Onorato9bb8fd72009-07-28 18:24:51 -0700105 // changing the wallpaper means we'll need to back up the new one
106 long origId = Binder.clearCallingIdentity();
107 BackupManager bm = new BackupManager(mContext);
108 bm.dataChanged();
109 Binder.restoreCallingIdentity(origId);
110
111 File changedFile = new File(WALLPAPER_DIR, path);
112 if (WALLPAPER_FILE.equals(changedFile)) {
113 notifyCallbacksLocked();
114 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800115 }
116 }
117 };
118
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700119 final Context mContext;
120 final IWindowManager mIWindowManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800121
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700122 int mWidth = -1;
123 int mHeight = -1;
124 String mName = "";
125 ComponentName mWallpaperComponent;
126 WallpaperConnection mWallpaperConnection;
Dianne Hackborn0cd48872009-08-13 18:51:59 -0700127 long mLastDiedTime;
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700128
129 class WallpaperConnection extends IWallpaperConnection.Stub
130 implements ServiceConnection {
131 final Binder mToken = new Binder();
132 IWallpaperService mService;
133 IWallpaperEngine mEngine;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800134
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700135 public void onServiceConnected(ComponentName name, IBinder service) {
136 synchronized (mLock) {
137 if (mWallpaperConnection == this) {
138 mService = IWallpaperService.Stub.asInterface(service);
139 attachServiceLocked(this);
140 }
141 }
142 }
143
144 public void onServiceDisconnected(ComponentName name) {
145 synchronized (mLock) {
146 mService = null;
147 mEngine = null;
Dianne Hackborn0cd48872009-08-13 18:51:59 -0700148 if (mWallpaperConnection == this) {
149 Log.w(TAG, "Wallpaper service gone: " + mWallpaperComponent);
150 if ((mLastDiedTime+MIN_WALLPAPER_CRASH_TIME)
151 < SystemClock.uptimeMillis()) {
152 Log.w(TAG, "Reverting to built-in wallpaper!");
153 bindWallpaperComponentLocked(null);
154 }
155 }
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700156 }
157 }
158
159 public void attachEngine(IWallpaperEngine engine) {
160 mEngine = engine;
161 }
162
163 public ParcelFileDescriptor setWallpaper(String name) {
164 synchronized (mLock) {
165 if (mWallpaperConnection == this) {
166 ParcelFileDescriptor pfd = updateWallpaperBitmapLocked(name);
167 if (pfd != null) {
168 saveSettingsLocked();
169 }
170 return pfd;
171 }
172 return null;
173 }
174 }
175 }
176
Dianne Hackborn8cc6a502009-08-05 21:29:42 -0700177 public WallpaperManagerService(Context context) {
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700178 if (DEBUG) Log.d(TAG, "WallpaperService startup");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800179 mContext = context;
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700180 mIWindowManager = IWindowManager.Stub.asInterface(
181 ServiceManager.getService(Context.WINDOW_SERVICE));
Joe Onoratoe712ee32009-07-29 16:23:58 -0700182 WALLPAPER_DIR.mkdirs();
Joe Onorato9bb8fd72009-07-28 18:24:51 -0700183 loadSettingsLocked();
Joe Onoratoe712ee32009-07-29 16:23:58 -0700184 mWallpaperObserver.startWatching();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800185 }
186
187 @Override
188 protected void finalize() throws Throwable {
189 super.finalize();
190 mWallpaperObserver.stopWatching();
191 }
192
Dianne Hackbornf21adf62009-08-13 10:20:21 -0700193 public void systemReady() {
194 synchronized (mLock) {
195 try {
196 bindWallpaperComponentLocked(mWallpaperComponent);
197 } catch (RuntimeException e) {
198 Log.w(TAG, "Failure starting previous wallpaper", e);
199 try {
200 bindWallpaperComponentLocked(null);
201 } catch (RuntimeException e2) {
202 Log.w(TAG, "Failure starting default wallpaper", e2);
203 clearWallpaperComponentLocked();
204 }
205 }
206 }
207 }
208
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800209 public void clearWallpaper() {
Joe Onorato9bb8fd72009-07-28 18:24:51 -0700210 synchronized (mLock) {
211 File f = WALLPAPER_FILE;
212 if (f.exists()) {
213 f.delete();
214 }
Dianne Hackborn0cd48872009-08-13 18:51:59 -0700215 final long ident = Binder.clearCallingIdentity();
216 try {
217 bindWallpaperComponentLocked(null);
218 } finally {
219 Binder.restoreCallingIdentity(ident);
220 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800221 }
222 }
223
224 public void setDimensionHints(int width, int height) throws RemoteException {
225 checkPermission(android.Manifest.permission.SET_WALLPAPER_HINTS);
226
227 if (width <= 0 || height <= 0) {
228 throw new IllegalArgumentException("width and height must be > 0");
229 }
230
Joe Onorato9bb8fd72009-07-28 18:24:51 -0700231 synchronized (mLock) {
232 if (width != mWidth || height != mHeight) {
233 mWidth = width;
234 mHeight = height;
235 saveSettingsLocked();
Dianne Hackborn284ac932009-08-28 10:34:25 -0700236 if (mWallpaperConnection != null) {
237 if (mWallpaperConnection.mEngine != null) {
238 try {
239 mWallpaperConnection.mEngine.setDesiredSize(
240 width, height);
241 } catch (RemoteException e) {
242 }
243 notifyCallbacksLocked();
244 }
245 }
Joe Onorato9bb8fd72009-07-28 18:24:51 -0700246 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800247 }
248 }
249
250 public int getWidthHint() throws RemoteException {
Joe Onorato9bb8fd72009-07-28 18:24:51 -0700251 synchronized (mLock) {
252 return mWidth;
253 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800254 }
255
256 public int getHeightHint() throws RemoteException {
Joe Onorato9bb8fd72009-07-28 18:24:51 -0700257 synchronized (mLock) {
258 return mHeight;
259 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800260 }
261
Dianne Hackborn284ac932009-08-28 10:34:25 -0700262 public ParcelFileDescriptor getWallpaper(IWallpaperManagerCallback cb,
263 Bundle outParams) {
Joe Onorato9bb8fd72009-07-28 18:24:51 -0700264 synchronized (mLock) {
265 try {
Dianne Hackborn284ac932009-08-28 10:34:25 -0700266 if (outParams != null) {
267 outParams.putInt("width", mWidth);
268 outParams.putInt("height", mHeight);
269 }
Joe Onorato9bb8fd72009-07-28 18:24:51 -0700270 mCallbacks.register(cb);
271 File f = WALLPAPER_FILE;
272 if (!f.exists()) {
273 return null;
274 }
275 return ParcelFileDescriptor.open(f, MODE_READ_ONLY);
276 } catch (FileNotFoundException e) {
277 /* Shouldn't happen as we check to see if the file exists */
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700278 Log.w(TAG, "Error getting wallpaper", e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800279 }
Joe Onorato9bb8fd72009-07-28 18:24:51 -0700280 return null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800281 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800282 }
283
Joe Onorato9bb8fd72009-07-28 18:24:51 -0700284 public ParcelFileDescriptor setWallpaper(String name) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800285 checkPermission(android.Manifest.permission.SET_WALLPAPER);
Joe Onorato9bb8fd72009-07-28 18:24:51 -0700286 synchronized (mLock) {
Dianne Hackborn0cd48872009-08-13 18:51:59 -0700287 final long ident = Binder.clearCallingIdentity();
288 try {
289 ParcelFileDescriptor pfd = updateWallpaperBitmapLocked(name);
290 if (pfd != null) {
291 bindWallpaperComponentLocked(null);
292 saveSettingsLocked();
293 }
294 return pfd;
295 } finally {
296 Binder.restoreCallingIdentity(ident);
Joe Onorato9bb8fd72009-07-28 18:24:51 -0700297 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800298 }
299 }
300
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700301 ParcelFileDescriptor updateWallpaperBitmapLocked(String name) {
302 if (name == null) name = "";
303 try {
304 ParcelFileDescriptor fd = ParcelFileDescriptor.open(WALLPAPER_FILE,
305 MODE_CREATE|MODE_READ_WRITE);
306 mName = name;
307 return fd;
308 } catch (FileNotFoundException e) {
309 Log.w(TAG, "Error setting wallpaper", e);
310 }
311 return null;
312 }
313
314 public void setWallpaperComponent(ComponentName name) {
315 checkPermission(android.Manifest.permission.SET_WALLPAPER_COMPONENT);
316 synchronized (mLock) {
317 final long ident = Binder.clearCallingIdentity();
318 try {
Dianne Hackbornf21adf62009-08-13 10:20:21 -0700319 bindWallpaperComponentLocked(name);
320 } finally {
321 Binder.restoreCallingIdentity(ident);
322 }
323 }
324 }
325
326 void bindWallpaperComponentLocked(ComponentName name) {
327 // Has the component changed?
328 if (mWallpaperConnection != null) {
329 if (mWallpaperComponent == null) {
330 if (name == null) {
331 // Still using default wallpaper.
332 return;
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700333 }
Dianne Hackbornf21adf62009-08-13 10:20:21 -0700334 } else if (mWallpaperComponent.equals(name)) {
335 // Changing to same wallpaper.
336 return;
337 }
338 }
339
340 try {
341 ComponentName realName = name;
342 if (realName == null) {
343 // The default component is our static image wallpaper.
Dianne Hackborn7341d7a2009-08-14 11:37:52 -0700344 realName = new ComponentName("android",
345 ImageWallpaper.class.getName());
346 //clearWallpaperComponentLocked();
347 //return;
Dianne Hackbornf21adf62009-08-13 10:20:21 -0700348 }
349 ServiceInfo si = mContext.getPackageManager().getServiceInfo(realName,
350 PackageManager.GET_META_DATA | PackageManager.GET_PERMISSIONS);
351 if (!android.Manifest.permission.BIND_WALLPAPER.equals(si.permission)) {
352 throw new SecurityException("Selected service does not require "
353 + android.Manifest.permission.BIND_WALLPAPER
354 + ": " + realName);
355 }
356
357 Intent intent = new Intent(WallpaperService.SERVICE_INTERFACE);
358 if (name != null) {
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700359 // Make sure the selected service is actually a wallpaper service.
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700360 List<ResolveInfo> ris = mContext.getPackageManager()
361 .queryIntentServices(intent, 0);
362 for (int i=0; i<ris.size(); i++) {
363 ServiceInfo rsi = ris.get(i).serviceInfo;
364 if (rsi.name.equals(si.name) &&
365 rsi.packageName.equals(si.packageName)) {
366 ris = null;
367 break;
368 }
369 }
370 if (ris != null) {
371 throw new SecurityException("Selected service is not a wallpaper: "
Dianne Hackbornf21adf62009-08-13 10:20:21 -0700372 + realName);
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700373 }
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700374 }
Dianne Hackbornf21adf62009-08-13 10:20:21 -0700375
376 // Bind the service!
377 WallpaperConnection newConn = new WallpaperConnection();
378 intent.setComponent(realName);
379 if (!mContext.bindService(intent, newConn,
380 Context.BIND_AUTO_CREATE)) {
381 throw new IllegalArgumentException("Unable to bind service: "
382 + name);
383 }
384
385 clearWallpaperComponentLocked();
386 mWallpaperComponent = name;
387 mWallpaperConnection = newConn;
Dianne Hackborn0cd48872009-08-13 18:51:59 -0700388 mLastDiedTime = SystemClock.uptimeMillis();
Dianne Hackbornf21adf62009-08-13 10:20:21 -0700389 try {
390 if (DEBUG) Log.v(TAG, "Adding window token: " + newConn.mToken);
391 mIWindowManager.addWindowToken(newConn.mToken,
392 WindowManager.LayoutParams.TYPE_WALLPAPER);
393 } catch (RemoteException e) {
394 }
395
396 } catch (PackageManager.NameNotFoundException e) {
397 throw new IllegalArgumentException("Unknown component " + name);
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700398 }
399 }
400
401 void clearWallpaperComponentLocked() {
402 mWallpaperComponent = null;
403 if (mWallpaperConnection != null) {
404 if (mWallpaperConnection.mEngine != null) {
405 try {
406 mWallpaperConnection.mEngine.destroy();
407 } catch (RemoteException e) {
408 }
409 }
410 mContext.unbindService(mWallpaperConnection);
Dianne Hackborne9e9bca2009-08-18 15:08:22 -0700411 try {
412 if (DEBUG) Log.v(TAG, "Removing window token: "
413 + mWallpaperConnection.mToken);
414 mIWindowManager.removeWindowToken(mWallpaperConnection.mToken);
415 } catch (RemoteException e) {
416 }
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700417 mWallpaperConnection = null;
418 }
419 }
420
421 void attachServiceLocked(WallpaperConnection conn) {
422 try {
Dianne Hackborn3be63c02009-08-20 19:31:38 -0700423 conn.mService.attach(conn, conn.mToken,
424 WindowManager.LayoutParams.TYPE_WALLPAPER, false,
425 mWidth, mHeight);
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700426 } catch (RemoteException e) {
427 Log.w(TAG, "Failed attaching wallpaper; clearing", e);
Dianne Hackbornf21adf62009-08-13 10:20:21 -0700428 bindWallpaperComponentLocked(null);
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700429 }
430 }
431
Joe Onorato9bb8fd72009-07-28 18:24:51 -0700432 private void notifyCallbacksLocked() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800433 final int n = mCallbacks.beginBroadcast();
434 for (int i = 0; i < n; i++) {
435 try {
436 mCallbacks.getBroadcastItem(i).onWallpaperChanged();
437 } catch (RemoteException e) {
438
439 // The RemoteCallbackList will take care of removing
440 // the dead object for us.
441 }
442 }
443 mCallbacks.finishBroadcast();
444 final Intent intent = new Intent(Intent.ACTION_WALLPAPER_CHANGED);
445 mContext.sendBroadcast(intent);
446 }
447
448 private void checkPermission(String permission) {
Joe Onorato9bb8fd72009-07-28 18:24:51 -0700449 if (PackageManager.PERMISSION_GRANTED!= mContext.checkCallingOrSelfPermission(permission)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800450 throw new SecurityException("Access denied to process: " + Binder.getCallingPid()
451 + ", must have permission " + permission);
452 }
453 }
Joe Onorato9bb8fd72009-07-28 18:24:51 -0700454
455 private static JournaledFile makeJournaledFile() {
456 final String base = "/data/system/wallpaper_info.xml";
457 return new JournaledFile(new File(base), new File(base + ".tmp"));
458 }
459
460 private void saveSettingsLocked() {
461 JournaledFile journal = makeJournaledFile();
462 FileOutputStream stream = null;
463 try {
464 stream = new FileOutputStream(journal.chooseForWrite(), false);
465 XmlSerializer out = new FastXmlSerializer();
466 out.setOutput(stream, "utf-8");
467 out.startDocument(null, true);
468
469 out.startTag(null, "wp");
470 out.attribute(null, "width", Integer.toString(mWidth));
471 out.attribute(null, "height", Integer.toString(mHeight));
472 out.attribute(null, "name", mName);
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700473 if (mWallpaperComponent != null) {
474 out.attribute(null, "component",
475 mWallpaperComponent.flattenToShortString());
476 }
Joe Onorato9bb8fd72009-07-28 18:24:51 -0700477 out.endTag(null, "wp");
478
479 out.endDocument();
480 stream.close();
481 journal.commit();
482 } catch (IOException e) {
483 try {
484 if (stream != null) {
485 stream.close();
486 }
487 } catch (IOException ex) {
488 // Ignore
489 }
490 journal.rollback();
491 }
492 }
493
494 private void loadSettingsLocked() {
495 JournaledFile journal = makeJournaledFile();
496 FileInputStream stream = null;
497 File file = journal.chooseForRead();
498 boolean success = false;
499 try {
500 stream = new FileInputStream(file);
501 XmlPullParser parser = Xml.newPullParser();
502 parser.setInput(stream, null);
503
504 int type;
Joe Onorato9bb8fd72009-07-28 18:24:51 -0700505 do {
506 type = parser.next();
507 if (type == XmlPullParser.START_TAG) {
508 String tag = parser.getName();
509 if ("wp".equals(tag)) {
510 mWidth = Integer.parseInt(parser.getAttributeValue(null, "width"));
511 mHeight = Integer.parseInt(parser.getAttributeValue(null, "height"));
512 mName = parser.getAttributeValue(null, "name");
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700513 String comp = parser.getAttributeValue(null, "component");
514 mWallpaperComponent = comp != null
515 ? ComponentName.unflattenFromString(comp)
516 : null;
Joe Onorato9bb8fd72009-07-28 18:24:51 -0700517 }
518 }
519 } while (type != XmlPullParser.END_DOCUMENT);
520 success = true;
521 } catch (NullPointerException e) {
Joe Onorato2d9c9e32009-07-29 16:43:06 -0700522 Log.w(TAG, "failed parsing " + file + " " + e);
Joe Onorato9bb8fd72009-07-28 18:24:51 -0700523 } catch (NumberFormatException e) {
Joe Onorato2d9c9e32009-07-29 16:43:06 -0700524 Log.w(TAG, "failed parsing " + file + " " + e);
Joe Onorato9bb8fd72009-07-28 18:24:51 -0700525 } catch (XmlPullParserException e) {
Joe Onorato2d9c9e32009-07-29 16:43:06 -0700526 Log.w(TAG, "failed parsing " + file + " " + e);
Joe Onorato9bb8fd72009-07-28 18:24:51 -0700527 } catch (IOException e) {
Joe Onorato2d9c9e32009-07-29 16:43:06 -0700528 Log.w(TAG, "failed parsing " + file + " " + e);
Joe Onorato9bb8fd72009-07-28 18:24:51 -0700529 } catch (IndexOutOfBoundsException e) {
Joe Onorato2d9c9e32009-07-29 16:43:06 -0700530 Log.w(TAG, "failed parsing " + file + " " + e);
Joe Onorato9bb8fd72009-07-28 18:24:51 -0700531 }
532 try {
533 if (stream != null) {
534 stream.close();
535 }
536 } catch (IOException e) {
537 // Ignore
538 }
539
540 if (!success) {
541 mWidth = -1;
542 mHeight = -1;
543 mName = "";
544 }
545 }
546
547 void settingsRestored() {
548 boolean success = false;
549 synchronized (mLock) {
550 loadSettingsLocked();
551 // If there's a wallpaper name, we use that. If that can't be loaded, then we
552 // use the default.
553 if ("".equals(mName)) {
554 success = true;
555 } else {
556 success = restoreNamedResourceLocked();
557 }
558 }
559
560 if (!success) {
561 Log.e(TAG, "Failed to restore wallpaper: '" + mName + "'");
562 mName = "";
563 WALLPAPER_FILE.delete();
564 }
565 saveSettingsLocked();
566 }
567
568 boolean restoreNamedResourceLocked() {
569 if (mName.length() > 4 && "res:".equals(mName.substring(0, 4))) {
570 String resName = mName.substring(4);
571
572 String pkg = null;
573 int colon = resName.indexOf(':');
574 if (colon > 0) {
575 pkg = resName.substring(0, colon);
576 }
577
578 String ident = null;
579 int slash = resName.lastIndexOf('/');
580 if (slash > 0) {
581 ident = resName.substring(slash+1);
582 }
583
584 String type = null;
585 if (colon > 0 && slash > 0 && (slash-colon) > 1) {
586 type = resName.substring(colon+1, slash);
587 }
588
589 if (pkg != null && ident != null && type != null) {
590 int resId = -1;
591 InputStream res = null;
592 FileOutputStream fos = null;
593 try {
594 Context c = mContext.createPackageContext(pkg, Context.CONTEXT_RESTRICTED);
595 Resources r = c.getResources();
596 resId = r.getIdentifier(resName, null, null);
597 if (resId == 0) {
598 Log.e(TAG, "couldn't resolve identifier pkg=" + pkg + " type=" + type
599 + " ident=" + ident);
600 return false;
601 }
602
603 res = r.openRawResource(resId);
604 fos = new FileOutputStream(WALLPAPER_FILE);
605
606 byte[] buffer = new byte[32768];
607 int amt;
608 while ((amt=res.read(buffer)) > 0) {
609 fos.write(buffer, 0, amt);
610 }
611 // mWallpaperObserver will notice the close and send the change broadcast
612
613 Log.d(TAG, "Restored wallpaper: " + resName);
614 return true;
615 } catch (NameNotFoundException e) {
616 Log.e(TAG, "Package name " + pkg + " not found");
617 } catch (Resources.NotFoundException e) {
618 Log.e(TAG, "Resource not found: " + resId);
619 } catch (IOException e) {
620 Log.e(TAG, "IOException while restoring wallpaper ", e);
621 } finally {
622 if (res != null) {
623 try {
624 res.close();
625 } catch (IOException ex) {}
626 }
627 if (fos != null) {
628 try {
629 fos.close();
630 } catch (IOException ex) {}
631 }
632 }
633 }
634 }
635 return false;
636 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800637}