blob: cc977b09808e6b7822f03fcee75b98d148d7b3f7 [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;
Christopher Tate111bd4a2009-06-24 17:29:38 -070025import android.backup.BackupManager;
Dianne Hackborn4c62fc02009-08-08 20:40:27 -070026import android.content.ComponentName;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080027import android.content.Context;
28import android.content.Intent;
Dianne Hackborn4c62fc02009-08-08 20:40:27 -070029import android.content.ServiceConnection;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080030import android.content.pm.PackageManager;
Dianne Hackborn4c62fc02009-08-08 20:40:27 -070031import android.content.pm.ResolveInfo;
32import android.content.pm.ServiceInfo;
Joe Onorato9bb8fd72009-07-28 18:24:51 -070033import android.content.pm.PackageManager.NameNotFoundException;
34import android.content.res.Resources;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080035import android.os.Binder;
Dianne Hackborn284ac932009-08-28 10:34:25 -070036import android.os.Bundle;
Dianne Hackborn4c62fc02009-08-08 20:40:27 -070037import android.os.IBinder;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080038import android.os.RemoteException;
39import android.os.FileObserver;
40import android.os.ParcelFileDescriptor;
41import android.os.RemoteCallbackList;
Dianne Hackborn4c62fc02009-08-08 20:40:27 -070042import android.os.ServiceManager;
Dianne Hackborn0cd48872009-08-13 18:51:59 -070043import android.os.SystemClock;
Dianne Hackborndd9b82c2009-09-03 00:18:47 -070044import android.provider.Settings;
Dianne Hackborn4c62fc02009-08-08 20:40:27 -070045import android.service.wallpaper.IWallpaperConnection;
46import android.service.wallpaper.IWallpaperEngine;
47import android.service.wallpaper.IWallpaperService;
48import android.service.wallpaper.WallpaperService;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080049import android.util.Log;
Joe Onorato9bb8fd72009-07-28 18:24:51 -070050import android.util.Xml;
Dianne Hackborn4c62fc02009-08-08 20:40:27 -070051import android.view.IWindowManager;
52import android.view.WindowManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080053
Joe Onorato9bb8fd72009-07-28 18:24:51 -070054import java.io.IOException;
55import java.io.InputStream;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080056import java.io.File;
57import java.io.FileNotFoundException;
Joe Onorato9bb8fd72009-07-28 18:24:51 -070058import java.io.FileInputStream;
59import java.io.FileOutputStream;
Dianne Hackborn4c62fc02009-08-08 20:40:27 -070060import java.util.List;
Joe Onorato9bb8fd72009-07-28 18:24:51 -070061
62import org.xmlpull.v1.XmlPullParser;
63import org.xmlpull.v1.XmlPullParserException;
64import org.xmlpull.v1.XmlSerializer;
65
Dianne Hackbornf21adf62009-08-13 10:20:21 -070066import com.android.internal.service.wallpaper.ImageWallpaper;
Joe Onorato9bb8fd72009-07-28 18:24:51 -070067import com.android.internal.util.FastXmlSerializer;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080068
Dianne Hackborn8cc6a502009-08-05 21:29:42 -070069class WallpaperManagerService extends IWallpaperManager.Stub {
Dianne Hackborn4c62fc02009-08-08 20:40:27 -070070 static final String TAG = "WallpaperService";
Dianne Hackborncbf15042009-08-18 18:29:09 -070071 static final boolean DEBUG = false;
Joe Onorato9bb8fd72009-07-28 18:24:51 -070072
Dianne Hackborn4c62fc02009-08-08 20:40:27 -070073 Object mLock = new Object();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080074
Dianne Hackborn0cd48872009-08-13 18:51:59 -070075 /**
76 * Minimum time between crashes of a wallpaper service for us to consider
77 * restarting it vs. just reverting to the static wallpaper.
78 */
79 static final long MIN_WALLPAPER_CRASH_TIME = 10000;
80
81 static final File WALLPAPER_DIR = new File(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080082 "/data/data/com.android.settings/files");
Dianne Hackborn0cd48872009-08-13 18:51:59 -070083 static final String WALLPAPER = "wallpaper";
84 static final File WALLPAPER_FILE = new File(WALLPAPER_DIR, WALLPAPER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080085
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080086 /**
87 * List of callbacks registered they should each be notified
88 * when the wallpaper is changed.
89 */
Dianne Hackborn8cc6a502009-08-05 21:29:42 -070090 private final RemoteCallbackList<IWallpaperManagerCallback> mCallbacks
91 = new RemoteCallbackList<IWallpaperManagerCallback>();
Joe Onorato9bb8fd72009-07-28 18:24:51 -070092
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080093 /**
94 * Observes the wallpaper for changes and notifies all IWallpaperServiceCallbacks
95 * that the wallpaper has changed. The CREATE is triggered when there is no
96 * wallpaper set and is created for the first time. The CLOSE_WRITE is triggered
97 * everytime the wallpaper is changed.
98 */
99 private final FileObserver mWallpaperObserver = new FileObserver(
Joe Onorato9bb8fd72009-07-28 18:24:51 -0700100 WALLPAPER_DIR.getAbsolutePath(), CREATE | CLOSE_WRITE | DELETE | DELETE_SELF) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800101 @Override
102 public void onEvent(int event, String path) {
Joe Onoratoe712ee32009-07-29 16:23:58 -0700103 if (path == null) {
104 return;
105 }
Joe Onorato9bb8fd72009-07-28 18:24:51 -0700106 synchronized (mLock) {
Joe Onorato9bb8fd72009-07-28 18:24:51 -0700107 // changing the wallpaper means we'll need to back up the new one
108 long origId = Binder.clearCallingIdentity();
109 BackupManager bm = new BackupManager(mContext);
110 bm.dataChanged();
111 Binder.restoreCallingIdentity(origId);
112
113 File changedFile = new File(WALLPAPER_DIR, path);
114 if (WALLPAPER_FILE.equals(changedFile)) {
115 notifyCallbacksLocked();
116 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800117 }
118 }
119 };
120
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700121 final Context mContext;
122 final IWindowManager mIWindowManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800123
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700124 int mWidth = -1;
125 int mHeight = -1;
126 String mName = "";
127 ComponentName mWallpaperComponent;
128 WallpaperConnection mWallpaperConnection;
Dianne Hackborn0cd48872009-08-13 18:51:59 -0700129 long mLastDiedTime;
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700130
131 class WallpaperConnection extends IWallpaperConnection.Stub
132 implements ServiceConnection {
133 final Binder mToken = new Binder();
134 IWallpaperService mService;
135 IWallpaperEngine mEngine;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800136
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700137 public void onServiceConnected(ComponentName name, IBinder service) {
138 synchronized (mLock) {
139 if (mWallpaperConnection == this) {
140 mService = IWallpaperService.Stub.asInterface(service);
141 attachServiceLocked(this);
142 }
143 }
144 }
145
146 public void onServiceDisconnected(ComponentName name) {
147 synchronized (mLock) {
148 mService = null;
149 mEngine = null;
Dianne Hackborn0cd48872009-08-13 18:51:59 -0700150 if (mWallpaperConnection == this) {
151 Log.w(TAG, "Wallpaper service gone: " + mWallpaperComponent);
152 if ((mLastDiedTime+MIN_WALLPAPER_CRASH_TIME)
153 < SystemClock.uptimeMillis()) {
154 Log.w(TAG, "Reverting to built-in wallpaper!");
155 bindWallpaperComponentLocked(null);
156 }
157 }
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700158 }
159 }
160
161 public void attachEngine(IWallpaperEngine engine) {
162 mEngine = engine;
163 }
164
165 public ParcelFileDescriptor setWallpaper(String name) {
166 synchronized (mLock) {
167 if (mWallpaperConnection == this) {
168 ParcelFileDescriptor pfd = updateWallpaperBitmapLocked(name);
169 if (pfd != null) {
170 saveSettingsLocked();
171 }
172 return pfd;
173 }
174 return null;
175 }
176 }
177 }
178
Dianne Hackborn8cc6a502009-08-05 21:29:42 -0700179 public WallpaperManagerService(Context context) {
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700180 if (DEBUG) Log.d(TAG, "WallpaperService startup");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800181 mContext = context;
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700182 mIWindowManager = IWindowManager.Stub.asInterface(
183 ServiceManager.getService(Context.WINDOW_SERVICE));
Joe Onoratoe712ee32009-07-29 16:23:58 -0700184 WALLPAPER_DIR.mkdirs();
Joe Onorato9bb8fd72009-07-28 18:24:51 -0700185 loadSettingsLocked();
Joe Onoratoe712ee32009-07-29 16:23:58 -0700186 mWallpaperObserver.startWatching();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800187 }
188
189 @Override
190 protected void finalize() throws Throwable {
191 super.finalize();
192 mWallpaperObserver.stopWatching();
193 }
194
Dianne Hackbornf21adf62009-08-13 10:20:21 -0700195 public void systemReady() {
196 synchronized (mLock) {
197 try {
198 bindWallpaperComponentLocked(mWallpaperComponent);
199 } catch (RuntimeException e) {
200 Log.w(TAG, "Failure starting previous wallpaper", e);
201 try {
202 bindWallpaperComponentLocked(null);
203 } catch (RuntimeException e2) {
204 Log.w(TAG, "Failure starting default wallpaper", e2);
205 clearWallpaperComponentLocked();
206 }
207 }
208 }
209 }
210
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800211 public void clearWallpaper() {
Joe Onorato9bb8fd72009-07-28 18:24:51 -0700212 synchronized (mLock) {
213 File f = WALLPAPER_FILE;
214 if (f.exists()) {
215 f.delete();
216 }
Dianne Hackborn0cd48872009-08-13 18:51:59 -0700217 final long ident = Binder.clearCallingIdentity();
218 try {
219 bindWallpaperComponentLocked(null);
220 } finally {
221 Binder.restoreCallingIdentity(ident);
222 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800223 }
224 }
225
226 public void setDimensionHints(int width, int height) throws RemoteException {
227 checkPermission(android.Manifest.permission.SET_WALLPAPER_HINTS);
228
229 if (width <= 0 || height <= 0) {
230 throw new IllegalArgumentException("width and height must be > 0");
231 }
232
Joe Onorato9bb8fd72009-07-28 18:24:51 -0700233 synchronized (mLock) {
234 if (width != mWidth || height != mHeight) {
235 mWidth = width;
236 mHeight = height;
237 saveSettingsLocked();
Dianne Hackborn284ac932009-08-28 10:34:25 -0700238 if (mWallpaperConnection != null) {
239 if (mWallpaperConnection.mEngine != null) {
240 try {
241 mWallpaperConnection.mEngine.setDesiredSize(
242 width, height);
243 } catch (RemoteException e) {
244 }
245 notifyCallbacksLocked();
246 }
247 }
Joe Onorato9bb8fd72009-07-28 18:24:51 -0700248 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800249 }
250 }
251
252 public int getWidthHint() throws RemoteException {
Joe Onorato9bb8fd72009-07-28 18:24:51 -0700253 synchronized (mLock) {
254 return mWidth;
255 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800256 }
257
258 public int getHeightHint() throws RemoteException {
Joe Onorato9bb8fd72009-07-28 18:24:51 -0700259 synchronized (mLock) {
260 return mHeight;
261 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800262 }
263
Dianne Hackborn284ac932009-08-28 10:34:25 -0700264 public ParcelFileDescriptor getWallpaper(IWallpaperManagerCallback cb,
265 Bundle outParams) {
Joe Onorato9bb8fd72009-07-28 18:24:51 -0700266 synchronized (mLock) {
267 try {
Dianne Hackborn284ac932009-08-28 10:34:25 -0700268 if (outParams != null) {
269 outParams.putInt("width", mWidth);
270 outParams.putInt("height", mHeight);
271 }
Joe Onorato9bb8fd72009-07-28 18:24:51 -0700272 mCallbacks.register(cb);
273 File f = WALLPAPER_FILE;
274 if (!f.exists()) {
275 return null;
276 }
277 return ParcelFileDescriptor.open(f, MODE_READ_ONLY);
278 } catch (FileNotFoundException e) {
279 /* Shouldn't happen as we check to see if the file exists */
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700280 Log.w(TAG, "Error getting wallpaper", e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800281 }
Joe Onorato9bb8fd72009-07-28 18:24:51 -0700282 return null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800283 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800284 }
285
Joe Onorato9bb8fd72009-07-28 18:24:51 -0700286 public ParcelFileDescriptor setWallpaper(String name) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800287 checkPermission(android.Manifest.permission.SET_WALLPAPER);
Joe Onorato9bb8fd72009-07-28 18:24:51 -0700288 synchronized (mLock) {
Dianne Hackborn0cd48872009-08-13 18:51:59 -0700289 final long ident = Binder.clearCallingIdentity();
290 try {
291 ParcelFileDescriptor pfd = updateWallpaperBitmapLocked(name);
292 if (pfd != null) {
293 bindWallpaperComponentLocked(null);
294 saveSettingsLocked();
295 }
296 return pfd;
297 } finally {
298 Binder.restoreCallingIdentity(ident);
Joe Onorato9bb8fd72009-07-28 18:24:51 -0700299 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800300 }
301 }
302
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700303 ParcelFileDescriptor updateWallpaperBitmapLocked(String name) {
304 if (name == null) name = "";
305 try {
306 ParcelFileDescriptor fd = ParcelFileDescriptor.open(WALLPAPER_FILE,
307 MODE_CREATE|MODE_READ_WRITE);
308 mName = name;
309 return fd;
310 } catch (FileNotFoundException e) {
311 Log.w(TAG, "Error setting wallpaper", e);
312 }
313 return null;
314 }
315
316 public void setWallpaperComponent(ComponentName name) {
317 checkPermission(android.Manifest.permission.SET_WALLPAPER_COMPONENT);
318 synchronized (mLock) {
319 final long ident = Binder.clearCallingIdentity();
320 try {
Dianne Hackbornf21adf62009-08-13 10:20:21 -0700321 bindWallpaperComponentLocked(name);
322 } finally {
323 Binder.restoreCallingIdentity(ident);
324 }
325 }
326 }
327
328 void bindWallpaperComponentLocked(ComponentName name) {
329 // Has the component changed?
330 if (mWallpaperConnection != null) {
331 if (mWallpaperComponent == null) {
332 if (name == null) {
333 // Still using default wallpaper.
334 return;
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700335 }
Dianne Hackbornf21adf62009-08-13 10:20:21 -0700336 } else if (mWallpaperComponent.equals(name)) {
337 // Changing to same wallpaper.
338 return;
339 }
340 }
341
342 try {
343 ComponentName realName = name;
344 if (realName == null) {
345 // The default component is our static image wallpaper.
Dianne Hackborn7341d7a2009-08-14 11:37:52 -0700346 realName = new ComponentName("android",
347 ImageWallpaper.class.getName());
348 //clearWallpaperComponentLocked();
349 //return;
Dianne Hackbornf21adf62009-08-13 10:20:21 -0700350 }
351 ServiceInfo si = mContext.getPackageManager().getServiceInfo(realName,
352 PackageManager.GET_META_DATA | PackageManager.GET_PERMISSIONS);
353 if (!android.Manifest.permission.BIND_WALLPAPER.equals(si.permission)) {
354 throw new SecurityException("Selected service does not require "
355 + android.Manifest.permission.BIND_WALLPAPER
356 + ": " + realName);
357 }
358
359 Intent intent = new Intent(WallpaperService.SERVICE_INTERFACE);
360 if (name != null) {
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700361 // Make sure the selected service is actually a wallpaper service.
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700362 List<ResolveInfo> ris = mContext.getPackageManager()
363 .queryIntentServices(intent, 0);
364 for (int i=0; i<ris.size(); i++) {
365 ServiceInfo rsi = ris.get(i).serviceInfo;
366 if (rsi.name.equals(si.name) &&
367 rsi.packageName.equals(si.packageName)) {
368 ris = null;
369 break;
370 }
371 }
372 if (ris != null) {
373 throw new SecurityException("Selected service is not a wallpaper: "
Dianne Hackbornf21adf62009-08-13 10:20:21 -0700374 + realName);
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700375 }
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700376 }
Dianne Hackbornf21adf62009-08-13 10:20:21 -0700377
378 // Bind the service!
379 WallpaperConnection newConn = new WallpaperConnection();
380 intent.setComponent(realName);
Dianne Hackborndd9b82c2009-09-03 00:18:47 -0700381 intent.putExtra(Intent.EXTRA_CLIENT_LABEL,
382 com.android.internal.R.string.wallpaper_binding_label);
383 intent.putExtra(Intent.EXTRA_CLIENT_INTENT, PendingIntent.getActivity(
384 mContext, 0, new Intent(Intent.ACTION_SET_WALLPAPER), 0));
Dianne Hackbornf21adf62009-08-13 10:20:21 -0700385 if (!mContext.bindService(intent, newConn,
386 Context.BIND_AUTO_CREATE)) {
387 throw new IllegalArgumentException("Unable to bind service: "
388 + name);
389 }
390
391 clearWallpaperComponentLocked();
392 mWallpaperComponent = name;
393 mWallpaperConnection = newConn;
Dianne Hackborn0cd48872009-08-13 18:51:59 -0700394 mLastDiedTime = SystemClock.uptimeMillis();
Dianne Hackbornf21adf62009-08-13 10:20:21 -0700395 try {
396 if (DEBUG) Log.v(TAG, "Adding window token: " + newConn.mToken);
397 mIWindowManager.addWindowToken(newConn.mToken,
398 WindowManager.LayoutParams.TYPE_WALLPAPER);
399 } catch (RemoteException e) {
400 }
401
402 } catch (PackageManager.NameNotFoundException e) {
403 throw new IllegalArgumentException("Unknown component " + name);
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700404 }
405 }
406
407 void clearWallpaperComponentLocked() {
408 mWallpaperComponent = null;
409 if (mWallpaperConnection != null) {
410 if (mWallpaperConnection.mEngine != null) {
411 try {
412 mWallpaperConnection.mEngine.destroy();
413 } catch (RemoteException e) {
414 }
415 }
416 mContext.unbindService(mWallpaperConnection);
Dianne Hackborne9e9bca2009-08-18 15:08:22 -0700417 try {
418 if (DEBUG) Log.v(TAG, "Removing window token: "
419 + mWallpaperConnection.mToken);
420 mIWindowManager.removeWindowToken(mWallpaperConnection.mToken);
421 } catch (RemoteException e) {
422 }
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700423 mWallpaperConnection = null;
424 }
425 }
426
427 void attachServiceLocked(WallpaperConnection conn) {
428 try {
Dianne Hackborn3be63c02009-08-20 19:31:38 -0700429 conn.mService.attach(conn, conn.mToken,
430 WindowManager.LayoutParams.TYPE_WALLPAPER, false,
431 mWidth, mHeight);
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700432 } catch (RemoteException e) {
433 Log.w(TAG, "Failed attaching wallpaper; clearing", e);
Dianne Hackbornf21adf62009-08-13 10:20:21 -0700434 bindWallpaperComponentLocked(null);
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700435 }
436 }
437
Joe Onorato9bb8fd72009-07-28 18:24:51 -0700438 private void notifyCallbacksLocked() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800439 final int n = mCallbacks.beginBroadcast();
440 for (int i = 0; i < n; i++) {
441 try {
442 mCallbacks.getBroadcastItem(i).onWallpaperChanged();
443 } catch (RemoteException e) {
444
445 // The RemoteCallbackList will take care of removing
446 // the dead object for us.
447 }
448 }
449 mCallbacks.finishBroadcast();
450 final Intent intent = new Intent(Intent.ACTION_WALLPAPER_CHANGED);
451 mContext.sendBroadcast(intent);
452 }
453
454 private void checkPermission(String permission) {
Joe Onorato9bb8fd72009-07-28 18:24:51 -0700455 if (PackageManager.PERMISSION_GRANTED!= mContext.checkCallingOrSelfPermission(permission)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800456 throw new SecurityException("Access denied to process: " + Binder.getCallingPid()
457 + ", must have permission " + permission);
458 }
459 }
Joe Onorato9bb8fd72009-07-28 18:24:51 -0700460
461 private static JournaledFile makeJournaledFile() {
462 final String base = "/data/system/wallpaper_info.xml";
463 return new JournaledFile(new File(base), new File(base + ".tmp"));
464 }
465
466 private void saveSettingsLocked() {
467 JournaledFile journal = makeJournaledFile();
468 FileOutputStream stream = null;
469 try {
470 stream = new FileOutputStream(journal.chooseForWrite(), false);
471 XmlSerializer out = new FastXmlSerializer();
472 out.setOutput(stream, "utf-8");
473 out.startDocument(null, true);
474
475 out.startTag(null, "wp");
476 out.attribute(null, "width", Integer.toString(mWidth));
477 out.attribute(null, "height", Integer.toString(mHeight));
478 out.attribute(null, "name", mName);
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700479 if (mWallpaperComponent != null) {
480 out.attribute(null, "component",
481 mWallpaperComponent.flattenToShortString());
482 }
Joe Onorato9bb8fd72009-07-28 18:24:51 -0700483 out.endTag(null, "wp");
484
485 out.endDocument();
486 stream.close();
487 journal.commit();
488 } catch (IOException e) {
489 try {
490 if (stream != null) {
491 stream.close();
492 }
493 } catch (IOException ex) {
494 // Ignore
495 }
496 journal.rollback();
497 }
498 }
499
500 private void loadSettingsLocked() {
501 JournaledFile journal = makeJournaledFile();
502 FileInputStream stream = null;
503 File file = journal.chooseForRead();
504 boolean success = false;
505 try {
506 stream = new FileInputStream(file);
507 XmlPullParser parser = Xml.newPullParser();
508 parser.setInput(stream, null);
509
510 int type;
Joe Onorato9bb8fd72009-07-28 18:24:51 -0700511 do {
512 type = parser.next();
513 if (type == XmlPullParser.START_TAG) {
514 String tag = parser.getName();
515 if ("wp".equals(tag)) {
516 mWidth = Integer.parseInt(parser.getAttributeValue(null, "width"));
517 mHeight = Integer.parseInt(parser.getAttributeValue(null, "height"));
518 mName = parser.getAttributeValue(null, "name");
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700519 String comp = parser.getAttributeValue(null, "component");
520 mWallpaperComponent = comp != null
521 ? ComponentName.unflattenFromString(comp)
522 : null;
Joe Onorato9bb8fd72009-07-28 18:24:51 -0700523 }
524 }
525 } while (type != XmlPullParser.END_DOCUMENT);
526 success = true;
527 } catch (NullPointerException 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 (NumberFormatException e) {
Joe Onorato2d9c9e32009-07-29 16:43:06 -0700530 Log.w(TAG, "failed parsing " + file + " " + e);
Joe Onorato9bb8fd72009-07-28 18:24:51 -0700531 } catch (XmlPullParserException e) {
Joe Onorato2d9c9e32009-07-29 16:43:06 -0700532 Log.w(TAG, "failed parsing " + file + " " + e);
Joe Onorato9bb8fd72009-07-28 18:24:51 -0700533 } catch (IOException e) {
Joe Onorato2d9c9e32009-07-29 16:43:06 -0700534 Log.w(TAG, "failed parsing " + file + " " + e);
Joe Onorato9bb8fd72009-07-28 18:24:51 -0700535 } catch (IndexOutOfBoundsException e) {
Joe Onorato2d9c9e32009-07-29 16:43:06 -0700536 Log.w(TAG, "failed parsing " + file + " " + e);
Joe Onorato9bb8fd72009-07-28 18:24:51 -0700537 }
538 try {
539 if (stream != null) {
540 stream.close();
541 }
542 } catch (IOException e) {
543 // Ignore
544 }
545
546 if (!success) {
547 mWidth = -1;
548 mHeight = -1;
549 mName = "";
550 }
551 }
552
553 void settingsRestored() {
554 boolean success = false;
555 synchronized (mLock) {
556 loadSettingsLocked();
557 // If there's a wallpaper name, we use that. If that can't be loaded, then we
558 // use the default.
559 if ("".equals(mName)) {
560 success = true;
561 } else {
562 success = restoreNamedResourceLocked();
563 }
564 }
565
566 if (!success) {
567 Log.e(TAG, "Failed to restore wallpaper: '" + mName + "'");
568 mName = "";
569 WALLPAPER_FILE.delete();
570 }
571 saveSettingsLocked();
572 }
573
574 boolean restoreNamedResourceLocked() {
575 if (mName.length() > 4 && "res:".equals(mName.substring(0, 4))) {
576 String resName = mName.substring(4);
577
578 String pkg = null;
579 int colon = resName.indexOf(':');
580 if (colon > 0) {
581 pkg = resName.substring(0, colon);
582 }
583
584 String ident = null;
585 int slash = resName.lastIndexOf('/');
586 if (slash > 0) {
587 ident = resName.substring(slash+1);
588 }
589
590 String type = null;
591 if (colon > 0 && slash > 0 && (slash-colon) > 1) {
592 type = resName.substring(colon+1, slash);
593 }
594
595 if (pkg != null && ident != null && type != null) {
596 int resId = -1;
597 InputStream res = null;
598 FileOutputStream fos = null;
599 try {
600 Context c = mContext.createPackageContext(pkg, Context.CONTEXT_RESTRICTED);
601 Resources r = c.getResources();
602 resId = r.getIdentifier(resName, null, null);
603 if (resId == 0) {
604 Log.e(TAG, "couldn't resolve identifier pkg=" + pkg + " type=" + type
605 + " ident=" + ident);
606 return false;
607 }
608
609 res = r.openRawResource(resId);
610 fos = new FileOutputStream(WALLPAPER_FILE);
611
612 byte[] buffer = new byte[32768];
613 int amt;
614 while ((amt=res.read(buffer)) > 0) {
615 fos.write(buffer, 0, amt);
616 }
617 // mWallpaperObserver will notice the close and send the change broadcast
618
619 Log.d(TAG, "Restored wallpaper: " + resName);
620 return true;
621 } catch (NameNotFoundException e) {
622 Log.e(TAG, "Package name " + pkg + " not found");
623 } catch (Resources.NotFoundException e) {
624 Log.e(TAG, "Resource not found: " + resId);
625 } catch (IOException e) {
626 Log.e(TAG, "IOException while restoring wallpaper ", e);
627 } finally {
628 if (res != null) {
629 try {
630 res.close();
631 } catch (IOException ex) {}
632 }
633 if (fos != null) {
634 try {
635 fos.close();
636 } catch (IOException ex) {}
637 }
638 }
639 }
640 }
641 return false;
642 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800643}