blob: 4b6049f03e1cfc4e66a6343125e03ff1ece7fbe1 [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001/*
2 * Copyright (C) 2008 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.server;
18
19import static android.os.FileObserver.*;
20import static android.os.ParcelFileDescriptor.*;
Christopher Tate111bd4a2009-06-24 17:29:38 -070021
Dianne Hackborn8cc6a502009-08-05 21:29:42 -070022import android.app.IWallpaperManager;
23import android.app.IWallpaperManagerCallback;
Dianne Hackborndd9b82c2009-09-03 00:18:47 -070024import android.app.PendingIntent;
Dianne Hackborneb034652009-09-07 00:49:58 -070025import android.app.WallpaperInfo;
Christopher Tate111bd4a2009-06-24 17:29:38 -070026import android.backup.BackupManager;
Dianne Hackborn4c62fc02009-08-08 20:40:27 -070027import android.content.ComponentName;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080028import android.content.Context;
29import android.content.Intent;
Dianne Hackborn4c62fc02009-08-08 20:40:27 -070030import android.content.ServiceConnection;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080031import android.content.pm.PackageManager;
Dianne Hackborn4c62fc02009-08-08 20:40:27 -070032import android.content.pm.ResolveInfo;
33import android.content.pm.ServiceInfo;
Joe Onorato9bb8fd72009-07-28 18:24:51 -070034import android.content.pm.PackageManager.NameNotFoundException;
35import android.content.res.Resources;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080036import android.os.Binder;
Dianne Hackborn284ac932009-08-28 10:34:25 -070037import android.os.Bundle;
Dianne Hackborn4c62fc02009-08-08 20:40:27 -070038import android.os.IBinder;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080039import android.os.RemoteException;
40import android.os.FileObserver;
41import android.os.ParcelFileDescriptor;
42import android.os.RemoteCallbackList;
Dianne Hackborn4c62fc02009-08-08 20:40:27 -070043import android.os.ServiceManager;
Dianne Hackborn0cd48872009-08-13 18:51:59 -070044import android.os.SystemClock;
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
Dianne Hackborneb034652009-09-07 00:49:58 -070054import java.io.FileDescriptor;
Joe Onorato9bb8fd72009-07-28 18:24:51 -070055import java.io.IOException;
56import java.io.InputStream;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080057import java.io.File;
58import java.io.FileNotFoundException;
Joe Onorato9bb8fd72009-07-28 18:24:51 -070059import java.io.FileInputStream;
60import java.io.FileOutputStream;
Dianne Hackborneb034652009-09-07 00:49:58 -070061import java.io.PrintWriter;
Dianne Hackborn4c62fc02009-08-08 20:40:27 -070062import java.util.List;
Joe Onorato9bb8fd72009-07-28 18:24:51 -070063
64import org.xmlpull.v1.XmlPullParser;
65import org.xmlpull.v1.XmlPullParserException;
66import org.xmlpull.v1.XmlSerializer;
67
Dianne Hackbornf21adf62009-08-13 10:20:21 -070068import com.android.internal.service.wallpaper.ImageWallpaper;
Joe Onorato9bb8fd72009-07-28 18:24:51 -070069import com.android.internal.util.FastXmlSerializer;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080070
Dianne Hackborn8cc6a502009-08-05 21:29:42 -070071class WallpaperManagerService extends IWallpaperManager.Stub {
Dianne Hackborn4c62fc02009-08-08 20:40:27 -070072 static final String TAG = "WallpaperService";
Dianne Hackborncbf15042009-08-18 18:29:09 -070073 static final boolean DEBUG = false;
Joe Onorato9bb8fd72009-07-28 18:24:51 -070074
Dianne Hackborn4c62fc02009-08-08 20:40:27 -070075 Object mLock = new Object();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080076
Dianne Hackborn0cd48872009-08-13 18:51:59 -070077 /**
78 * Minimum time between crashes of a wallpaper service for us to consider
79 * restarting it vs. just reverting to the static wallpaper.
80 */
81 static final long MIN_WALLPAPER_CRASH_TIME = 10000;
82
83 static final File WALLPAPER_DIR = new File(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080084 "/data/data/com.android.settings/files");
Dianne Hackborn0cd48872009-08-13 18:51:59 -070085 static final String WALLPAPER = "wallpaper";
86 static final File WALLPAPER_FILE = new File(WALLPAPER_DIR, WALLPAPER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080087
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080088 /**
89 * List of callbacks registered they should each be notified
90 * when the wallpaper is changed.
91 */
Dianne Hackborn8cc6a502009-08-05 21:29:42 -070092 private final RemoteCallbackList<IWallpaperManagerCallback> mCallbacks
93 = new RemoteCallbackList<IWallpaperManagerCallback>();
Joe Onorato9bb8fd72009-07-28 18:24:51 -070094
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080095 /**
96 * Observes the wallpaper for changes and notifies all IWallpaperServiceCallbacks
97 * that the wallpaper has changed. The CREATE is triggered when there is no
98 * wallpaper set and is created for the first time. The CLOSE_WRITE is triggered
99 * everytime the wallpaper is changed.
100 */
101 private final FileObserver mWallpaperObserver = new FileObserver(
Joe Onorato9bb8fd72009-07-28 18:24:51 -0700102 WALLPAPER_DIR.getAbsolutePath(), CREATE | CLOSE_WRITE | DELETE | DELETE_SELF) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800103 @Override
104 public void onEvent(int event, String path) {
Joe Onoratoe712ee32009-07-29 16:23:58 -0700105 if (path == null) {
106 return;
107 }
Joe Onorato9bb8fd72009-07-28 18:24:51 -0700108 synchronized (mLock) {
Joe Onorato9bb8fd72009-07-28 18:24:51 -0700109 // changing the wallpaper means we'll need to back up the new one
110 long origId = Binder.clearCallingIdentity();
111 BackupManager bm = new BackupManager(mContext);
112 bm.dataChanged();
113 Binder.restoreCallingIdentity(origId);
114
115 File changedFile = new File(WALLPAPER_DIR, path);
116 if (WALLPAPER_FILE.equals(changedFile)) {
117 notifyCallbacksLocked();
118 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800119 }
120 }
121 };
122
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700123 final Context mContext;
124 final IWindowManager mIWindowManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800125
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700126 int mWidth = -1;
127 int mHeight = -1;
128 String mName = "";
129 ComponentName mWallpaperComponent;
130 WallpaperConnection mWallpaperConnection;
Dianne Hackborn0cd48872009-08-13 18:51:59 -0700131 long mLastDiedTime;
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700132
133 class WallpaperConnection extends IWallpaperConnection.Stub
134 implements ServiceConnection {
Dianne Hackborneb034652009-09-07 00:49:58 -0700135 final WallpaperInfo mInfo;
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700136 final Binder mToken = new Binder();
137 IWallpaperService mService;
138 IWallpaperEngine mEngine;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800139
Dianne Hackborneb034652009-09-07 00:49:58 -0700140 public WallpaperConnection(WallpaperInfo info) {
141 mInfo = info;
142 }
143
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700144 public void onServiceConnected(ComponentName name, IBinder service) {
145 synchronized (mLock) {
146 if (mWallpaperConnection == this) {
147 mService = IWallpaperService.Stub.asInterface(service);
148 attachServiceLocked(this);
Dianne Hackborneb034652009-09-07 00:49:58 -0700149 // XXX should probably do saveSettingsLocked() later
150 // when we have an engine, but I'm not sure about
151 // locking there and anyway we always need to be able to
152 // recover if there is something wrong.
153 saveSettingsLocked();
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700154 }
155 }
156 }
157
158 public void onServiceDisconnected(ComponentName name) {
159 synchronized (mLock) {
160 mService = null;
161 mEngine = null;
Dianne Hackborn0cd48872009-08-13 18:51:59 -0700162 if (mWallpaperConnection == this) {
163 Log.w(TAG, "Wallpaper service gone: " + mWallpaperComponent);
164 if ((mLastDiedTime+MIN_WALLPAPER_CRASH_TIME)
165 < SystemClock.uptimeMillis()) {
166 Log.w(TAG, "Reverting to built-in wallpaper!");
167 bindWallpaperComponentLocked(null);
168 }
169 }
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700170 }
171 }
172
173 public void attachEngine(IWallpaperEngine engine) {
174 mEngine = engine;
175 }
176
177 public ParcelFileDescriptor setWallpaper(String name) {
178 synchronized (mLock) {
179 if (mWallpaperConnection == this) {
Dianne Hackborneb034652009-09-07 00:49:58 -0700180 return updateWallpaperBitmapLocked(name);
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700181 }
182 return null;
183 }
184 }
185 }
186
Dianne Hackborn8cc6a502009-08-05 21:29:42 -0700187 public WallpaperManagerService(Context context) {
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700188 if (DEBUG) Log.d(TAG, "WallpaperService startup");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800189 mContext = context;
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700190 mIWindowManager = IWindowManager.Stub.asInterface(
191 ServiceManager.getService(Context.WINDOW_SERVICE));
Joe Onoratoe712ee32009-07-29 16:23:58 -0700192 WALLPAPER_DIR.mkdirs();
Joe Onorato9bb8fd72009-07-28 18:24:51 -0700193 loadSettingsLocked();
Joe Onoratoe712ee32009-07-29 16:23:58 -0700194 mWallpaperObserver.startWatching();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800195 }
196
197 @Override
198 protected void finalize() throws Throwable {
199 super.finalize();
200 mWallpaperObserver.stopWatching();
201 }
202
Dianne Hackbornf21adf62009-08-13 10:20:21 -0700203 public void systemReady() {
204 synchronized (mLock) {
205 try {
206 bindWallpaperComponentLocked(mWallpaperComponent);
207 } catch (RuntimeException e) {
208 Log.w(TAG, "Failure starting previous wallpaper", e);
209 try {
210 bindWallpaperComponentLocked(null);
211 } catch (RuntimeException e2) {
212 Log.w(TAG, "Failure starting default wallpaper", e2);
213 clearWallpaperComponentLocked();
214 }
215 }
216 }
217 }
218
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800219 public void clearWallpaper() {
Joe Onorato9bb8fd72009-07-28 18:24:51 -0700220 synchronized (mLock) {
221 File f = WALLPAPER_FILE;
222 if (f.exists()) {
223 f.delete();
224 }
Dianne Hackborn0cd48872009-08-13 18:51:59 -0700225 final long ident = Binder.clearCallingIdentity();
226 try {
227 bindWallpaperComponentLocked(null);
228 } finally {
229 Binder.restoreCallingIdentity(ident);
230 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800231 }
232 }
233
234 public void setDimensionHints(int width, int height) throws RemoteException {
235 checkPermission(android.Manifest.permission.SET_WALLPAPER_HINTS);
236
237 if (width <= 0 || height <= 0) {
238 throw new IllegalArgumentException("width and height must be > 0");
239 }
240
Joe Onorato9bb8fd72009-07-28 18:24:51 -0700241 synchronized (mLock) {
242 if (width != mWidth || height != mHeight) {
243 mWidth = width;
244 mHeight = height;
245 saveSettingsLocked();
Dianne Hackborn284ac932009-08-28 10:34:25 -0700246 if (mWallpaperConnection != null) {
247 if (mWallpaperConnection.mEngine != null) {
248 try {
249 mWallpaperConnection.mEngine.setDesiredSize(
250 width, height);
251 } catch (RemoteException e) {
252 }
253 notifyCallbacksLocked();
254 }
255 }
Joe Onorato9bb8fd72009-07-28 18:24:51 -0700256 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800257 }
258 }
259
260 public int getWidthHint() throws RemoteException {
Joe Onorato9bb8fd72009-07-28 18:24:51 -0700261 synchronized (mLock) {
262 return mWidth;
263 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800264 }
265
266 public int getHeightHint() throws RemoteException {
Joe Onorato9bb8fd72009-07-28 18:24:51 -0700267 synchronized (mLock) {
268 return mHeight;
269 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800270 }
271
Dianne Hackborn284ac932009-08-28 10:34:25 -0700272 public ParcelFileDescriptor getWallpaper(IWallpaperManagerCallback cb,
273 Bundle outParams) {
Joe Onorato9bb8fd72009-07-28 18:24:51 -0700274 synchronized (mLock) {
275 try {
Dianne Hackborn284ac932009-08-28 10:34:25 -0700276 if (outParams != null) {
277 outParams.putInt("width", mWidth);
278 outParams.putInt("height", mHeight);
279 }
Joe Onorato9bb8fd72009-07-28 18:24:51 -0700280 mCallbacks.register(cb);
281 File f = WALLPAPER_FILE;
282 if (!f.exists()) {
283 return null;
284 }
285 return ParcelFileDescriptor.open(f, MODE_READ_ONLY);
286 } catch (FileNotFoundException e) {
287 /* Shouldn't happen as we check to see if the file exists */
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700288 Log.w(TAG, "Error getting wallpaper", e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800289 }
Joe Onorato9bb8fd72009-07-28 18:24:51 -0700290 return null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800291 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800292 }
293
Dianne Hackborneb034652009-09-07 00:49:58 -0700294 public WallpaperInfo getWallpaperInfo() {
295 synchronized (mLock) {
296 if (mWallpaperConnection != null) {
297 return mWallpaperConnection.mInfo;
298 }
299 return null;
300 }
301 }
302
Joe Onorato9bb8fd72009-07-28 18:24:51 -0700303 public ParcelFileDescriptor setWallpaper(String name) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800304 checkPermission(android.Manifest.permission.SET_WALLPAPER);
Joe Onorato9bb8fd72009-07-28 18:24:51 -0700305 synchronized (mLock) {
Dianne Hackborn0cd48872009-08-13 18:51:59 -0700306 final long ident = Binder.clearCallingIdentity();
307 try {
308 ParcelFileDescriptor pfd = updateWallpaperBitmapLocked(name);
309 if (pfd != null) {
310 bindWallpaperComponentLocked(null);
311 saveSettingsLocked();
312 }
313 return pfd;
314 } finally {
315 Binder.restoreCallingIdentity(ident);
Joe Onorato9bb8fd72009-07-28 18:24:51 -0700316 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800317 }
318 }
319
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700320 ParcelFileDescriptor updateWallpaperBitmapLocked(String name) {
321 if (name == null) name = "";
322 try {
323 ParcelFileDescriptor fd = ParcelFileDescriptor.open(WALLPAPER_FILE,
324 MODE_CREATE|MODE_READ_WRITE);
325 mName = name;
326 return fd;
327 } catch (FileNotFoundException e) {
328 Log.w(TAG, "Error setting wallpaper", e);
329 }
330 return null;
331 }
332
333 public void setWallpaperComponent(ComponentName name) {
334 checkPermission(android.Manifest.permission.SET_WALLPAPER_COMPONENT);
335 synchronized (mLock) {
336 final long ident = Binder.clearCallingIdentity();
337 try {
Dianne Hackbornf21adf62009-08-13 10:20:21 -0700338 bindWallpaperComponentLocked(name);
339 } finally {
340 Binder.restoreCallingIdentity(ident);
341 }
342 }
343 }
344
345 void bindWallpaperComponentLocked(ComponentName name) {
346 // Has the component changed?
347 if (mWallpaperConnection != null) {
348 if (mWallpaperComponent == null) {
349 if (name == null) {
350 // Still using default wallpaper.
351 return;
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700352 }
Dianne Hackbornf21adf62009-08-13 10:20:21 -0700353 } else if (mWallpaperComponent.equals(name)) {
354 // Changing to same wallpaper.
355 return;
356 }
357 }
358
359 try {
360 ComponentName realName = name;
361 if (realName == null) {
362 // The default component is our static image wallpaper.
Dianne Hackborn7341d7a2009-08-14 11:37:52 -0700363 realName = new ComponentName("android",
364 ImageWallpaper.class.getName());
365 //clearWallpaperComponentLocked();
366 //return;
Dianne Hackbornf21adf62009-08-13 10:20:21 -0700367 }
368 ServiceInfo si = mContext.getPackageManager().getServiceInfo(realName,
369 PackageManager.GET_META_DATA | PackageManager.GET_PERMISSIONS);
370 if (!android.Manifest.permission.BIND_WALLPAPER.equals(si.permission)) {
371 throw new SecurityException("Selected service does not require "
372 + android.Manifest.permission.BIND_WALLPAPER
373 + ": " + realName);
374 }
375
Dianne Hackborneb034652009-09-07 00:49:58 -0700376 WallpaperInfo wi = null;
377
Dianne Hackbornf21adf62009-08-13 10:20:21 -0700378 Intent intent = new Intent(WallpaperService.SERVICE_INTERFACE);
379 if (name != null) {
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700380 // Make sure the selected service is actually a wallpaper service.
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700381 List<ResolveInfo> ris = mContext.getPackageManager()
Dianne Hackborneb034652009-09-07 00:49:58 -0700382 .queryIntentServices(intent, PackageManager.GET_META_DATA);
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700383 for (int i=0; i<ris.size(); i++) {
384 ServiceInfo rsi = ris.get(i).serviceInfo;
385 if (rsi.name.equals(si.name) &&
386 rsi.packageName.equals(si.packageName)) {
Dianne Hackborneb034652009-09-07 00:49:58 -0700387 try {
388 wi = new WallpaperInfo(mContext, ris.get(i));
389 } catch (XmlPullParserException e) {
390 throw new IllegalArgumentException(e);
391 } catch (IOException e) {
392 throw new IllegalArgumentException(e);
393 }
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700394 break;
395 }
396 }
Dianne Hackborneb034652009-09-07 00:49:58 -0700397 if (wi == null) {
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700398 throw new SecurityException("Selected service is not a wallpaper: "
Dianne Hackbornf21adf62009-08-13 10:20:21 -0700399 + realName);
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700400 }
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700401 }
Dianne Hackbornf21adf62009-08-13 10:20:21 -0700402
403 // Bind the service!
Dianne Hackborneb034652009-09-07 00:49:58 -0700404 WallpaperConnection newConn = new WallpaperConnection(wi);
Dianne Hackbornf21adf62009-08-13 10:20:21 -0700405 intent.setComponent(realName);
Dianne Hackborndd9b82c2009-09-03 00:18:47 -0700406 intent.putExtra(Intent.EXTRA_CLIENT_LABEL,
407 com.android.internal.R.string.wallpaper_binding_label);
408 intent.putExtra(Intent.EXTRA_CLIENT_INTENT, PendingIntent.getActivity(
Dianne Hackborneb034652009-09-07 00:49:58 -0700409 mContext, 0,
410 Intent.createChooser(new Intent(Intent.ACTION_SET_WALLPAPER),
411 mContext.getText(com.android.internal.R.string.chooser_wallpaper)),
412 0));
Dianne Hackbornf21adf62009-08-13 10:20:21 -0700413 if (!mContext.bindService(intent, newConn,
414 Context.BIND_AUTO_CREATE)) {
415 throw new IllegalArgumentException("Unable to bind service: "
416 + name);
417 }
418
419 clearWallpaperComponentLocked();
420 mWallpaperComponent = name;
421 mWallpaperConnection = newConn;
Dianne Hackborn0cd48872009-08-13 18:51:59 -0700422 mLastDiedTime = SystemClock.uptimeMillis();
Dianne Hackbornf21adf62009-08-13 10:20:21 -0700423 try {
424 if (DEBUG) Log.v(TAG, "Adding window token: " + newConn.mToken);
425 mIWindowManager.addWindowToken(newConn.mToken,
426 WindowManager.LayoutParams.TYPE_WALLPAPER);
427 } catch (RemoteException e) {
428 }
429
430 } catch (PackageManager.NameNotFoundException e) {
431 throw new IllegalArgumentException("Unknown component " + name);
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700432 }
433 }
434
435 void clearWallpaperComponentLocked() {
436 mWallpaperComponent = null;
437 if (mWallpaperConnection != null) {
438 if (mWallpaperConnection.mEngine != null) {
439 try {
440 mWallpaperConnection.mEngine.destroy();
441 } catch (RemoteException e) {
442 }
443 }
444 mContext.unbindService(mWallpaperConnection);
Dianne Hackborne9e9bca2009-08-18 15:08:22 -0700445 try {
446 if (DEBUG) Log.v(TAG, "Removing window token: "
447 + mWallpaperConnection.mToken);
448 mIWindowManager.removeWindowToken(mWallpaperConnection.mToken);
449 } catch (RemoteException e) {
450 }
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700451 mWallpaperConnection = null;
452 }
453 }
454
455 void attachServiceLocked(WallpaperConnection conn) {
456 try {
Dianne Hackborn3be63c02009-08-20 19:31:38 -0700457 conn.mService.attach(conn, conn.mToken,
458 WindowManager.LayoutParams.TYPE_WALLPAPER, false,
459 mWidth, mHeight);
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700460 } catch (RemoteException e) {
461 Log.w(TAG, "Failed attaching wallpaper; clearing", e);
Dianne Hackbornf21adf62009-08-13 10:20:21 -0700462 bindWallpaperComponentLocked(null);
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700463 }
464 }
465
Joe Onorato9bb8fd72009-07-28 18:24:51 -0700466 private void notifyCallbacksLocked() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800467 final int n = mCallbacks.beginBroadcast();
468 for (int i = 0; i < n; i++) {
469 try {
470 mCallbacks.getBroadcastItem(i).onWallpaperChanged();
471 } catch (RemoteException e) {
472
473 // The RemoteCallbackList will take care of removing
474 // the dead object for us.
475 }
476 }
477 mCallbacks.finishBroadcast();
478 final Intent intent = new Intent(Intent.ACTION_WALLPAPER_CHANGED);
479 mContext.sendBroadcast(intent);
480 }
481
482 private void checkPermission(String permission) {
Joe Onorato9bb8fd72009-07-28 18:24:51 -0700483 if (PackageManager.PERMISSION_GRANTED!= mContext.checkCallingOrSelfPermission(permission)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800484 throw new SecurityException("Access denied to process: " + Binder.getCallingPid()
485 + ", must have permission " + permission);
486 }
487 }
Joe Onorato9bb8fd72009-07-28 18:24:51 -0700488
489 private static JournaledFile makeJournaledFile() {
490 final String base = "/data/system/wallpaper_info.xml";
491 return new JournaledFile(new File(base), new File(base + ".tmp"));
492 }
493
494 private void saveSettingsLocked() {
495 JournaledFile journal = makeJournaledFile();
496 FileOutputStream stream = null;
497 try {
498 stream = new FileOutputStream(journal.chooseForWrite(), false);
499 XmlSerializer out = new FastXmlSerializer();
500 out.setOutput(stream, "utf-8");
501 out.startDocument(null, true);
502
503 out.startTag(null, "wp");
504 out.attribute(null, "width", Integer.toString(mWidth));
505 out.attribute(null, "height", Integer.toString(mHeight));
506 out.attribute(null, "name", mName);
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700507 if (mWallpaperComponent != null) {
508 out.attribute(null, "component",
509 mWallpaperComponent.flattenToShortString());
510 }
Joe Onorato9bb8fd72009-07-28 18:24:51 -0700511 out.endTag(null, "wp");
512
513 out.endDocument();
514 stream.close();
515 journal.commit();
516 } catch (IOException e) {
517 try {
518 if (stream != null) {
519 stream.close();
520 }
521 } catch (IOException ex) {
522 // Ignore
523 }
524 journal.rollback();
525 }
526 }
527
528 private void loadSettingsLocked() {
529 JournaledFile journal = makeJournaledFile();
530 FileInputStream stream = null;
531 File file = journal.chooseForRead();
532 boolean success = false;
533 try {
534 stream = new FileInputStream(file);
535 XmlPullParser parser = Xml.newPullParser();
536 parser.setInput(stream, null);
537
538 int type;
Joe Onorato9bb8fd72009-07-28 18:24:51 -0700539 do {
540 type = parser.next();
541 if (type == XmlPullParser.START_TAG) {
542 String tag = parser.getName();
543 if ("wp".equals(tag)) {
544 mWidth = Integer.parseInt(parser.getAttributeValue(null, "width"));
545 mHeight = Integer.parseInt(parser.getAttributeValue(null, "height"));
546 mName = parser.getAttributeValue(null, "name");
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700547 String comp = parser.getAttributeValue(null, "component");
548 mWallpaperComponent = comp != null
549 ? ComponentName.unflattenFromString(comp)
550 : null;
Joe Onorato9bb8fd72009-07-28 18:24:51 -0700551 }
552 }
553 } while (type != XmlPullParser.END_DOCUMENT);
554 success = true;
555 } catch (NullPointerException e) {
Joe Onorato2d9c9e32009-07-29 16:43:06 -0700556 Log.w(TAG, "failed parsing " + file + " " + e);
Joe Onorato9bb8fd72009-07-28 18:24:51 -0700557 } catch (NumberFormatException e) {
Joe Onorato2d9c9e32009-07-29 16:43:06 -0700558 Log.w(TAG, "failed parsing " + file + " " + e);
Joe Onorato9bb8fd72009-07-28 18:24:51 -0700559 } catch (XmlPullParserException e) {
Joe Onorato2d9c9e32009-07-29 16:43:06 -0700560 Log.w(TAG, "failed parsing " + file + " " + e);
Joe Onorato9bb8fd72009-07-28 18:24:51 -0700561 } catch (IOException e) {
Joe Onorato2d9c9e32009-07-29 16:43:06 -0700562 Log.w(TAG, "failed parsing " + file + " " + e);
Joe Onorato9bb8fd72009-07-28 18:24:51 -0700563 } catch (IndexOutOfBoundsException e) {
Joe Onorato2d9c9e32009-07-29 16:43:06 -0700564 Log.w(TAG, "failed parsing " + file + " " + e);
Joe Onorato9bb8fd72009-07-28 18:24:51 -0700565 }
566 try {
567 if (stream != null) {
568 stream.close();
569 }
570 } catch (IOException e) {
571 // Ignore
572 }
573
574 if (!success) {
575 mWidth = -1;
576 mHeight = -1;
577 mName = "";
578 }
579 }
580
581 void settingsRestored() {
582 boolean success = false;
583 synchronized (mLock) {
584 loadSettingsLocked();
585 // If there's a wallpaper name, we use that. If that can't be loaded, then we
586 // use the default.
587 if ("".equals(mName)) {
588 success = true;
589 } else {
590 success = restoreNamedResourceLocked();
591 }
592 }
593
594 if (!success) {
595 Log.e(TAG, "Failed to restore wallpaper: '" + mName + "'");
596 mName = "";
597 WALLPAPER_FILE.delete();
598 }
599 saveSettingsLocked();
600 }
601
602 boolean restoreNamedResourceLocked() {
603 if (mName.length() > 4 && "res:".equals(mName.substring(0, 4))) {
604 String resName = mName.substring(4);
605
606 String pkg = null;
607 int colon = resName.indexOf(':');
608 if (colon > 0) {
609 pkg = resName.substring(0, colon);
610 }
611
612 String ident = null;
613 int slash = resName.lastIndexOf('/');
614 if (slash > 0) {
615 ident = resName.substring(slash+1);
616 }
617
618 String type = null;
619 if (colon > 0 && slash > 0 && (slash-colon) > 1) {
620 type = resName.substring(colon+1, slash);
621 }
622
623 if (pkg != null && ident != null && type != null) {
624 int resId = -1;
625 InputStream res = null;
626 FileOutputStream fos = null;
627 try {
628 Context c = mContext.createPackageContext(pkg, Context.CONTEXT_RESTRICTED);
629 Resources r = c.getResources();
630 resId = r.getIdentifier(resName, null, null);
631 if (resId == 0) {
632 Log.e(TAG, "couldn't resolve identifier pkg=" + pkg + " type=" + type
633 + " ident=" + ident);
634 return false;
635 }
636
637 res = r.openRawResource(resId);
638 fos = new FileOutputStream(WALLPAPER_FILE);
639
640 byte[] buffer = new byte[32768];
641 int amt;
642 while ((amt=res.read(buffer)) > 0) {
643 fos.write(buffer, 0, amt);
644 }
645 // mWallpaperObserver will notice the close and send the change broadcast
646
647 Log.d(TAG, "Restored wallpaper: " + resName);
648 return true;
649 } catch (NameNotFoundException e) {
650 Log.e(TAG, "Package name " + pkg + " not found");
651 } catch (Resources.NotFoundException e) {
652 Log.e(TAG, "Resource not found: " + resId);
653 } catch (IOException e) {
654 Log.e(TAG, "IOException while restoring wallpaper ", e);
655 } finally {
656 if (res != null) {
657 try {
658 res.close();
659 } catch (IOException ex) {}
660 }
661 if (fos != null) {
662 try {
663 fos.close();
664 } catch (IOException ex) {}
665 }
666 }
667 }
668 }
669 return false;
670 }
Dianne Hackborneb034652009-09-07 00:49:58 -0700671
672 @Override
673 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
674 if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DUMP)
675 != PackageManager.PERMISSION_GRANTED) {
676
677 pw.println("Permission Denial: can't dump wallpaper service from from pid="
678 + Binder.getCallingPid()
679 + ", uid=" + Binder.getCallingUid());
680 return;
681 }
682
683 synchronized (mLock) {
684 pw.println("Current Wallpaper Service state:");
685 pw.print(" mWidth="); pw.print(mWidth);
686 pw.print(" mHeight="); pw.println(mHeight);
687 pw.print(" mName="); pw.println(mName);
688 pw.print(" mWallpaperComponent="); pw.println(mWallpaperComponent);
689 if (mWallpaperConnection != null) {
690 WallpaperConnection conn = mWallpaperConnection;
691 pw.print(" Wallpaper connection ");
692 pw.print(conn); pw.println(":");
693 pw.print(" mInfo.component="); pw.println(conn.mInfo.getComponent());
694 pw.print(" mToken="); pw.println(conn.mToken);
695 pw.print(" mService="); pw.println(conn.mService);
696 pw.print(" mEngine="); pw.println(conn.mEngine);
697 pw.print(" mLastDiedTime=");
698 pw.println(mLastDiedTime - SystemClock.uptimeMillis());
699 }
700 }
701 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800702}