blob: 481e6a43cfb15e9648cf31028c86420c4d9fda0d [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 Hackborn21f1bd12010-02-19 17:02:21 -080068import com.android.internal.content.PackageMonitor;
Dianne Hackbornf21adf62009-08-13 10:20:21 -070069import com.android.internal.service.wallpaper.ImageWallpaper;
Dianne Hackborn21f1bd12010-02-19 17:02:21 -080070import com.android.server.DevicePolicyManagerService.ActiveAdmin;
71import com.android.server.DevicePolicyManagerService.MyPackageMonitor;
Tom Taylord4a47292009-12-21 13:59:18 -080072import com.android.common.FastXmlSerializer;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080073
Dianne Hackborn8cc6a502009-08-05 21:29:42 -070074class WallpaperManagerService extends IWallpaperManager.Stub {
Dianne Hackborn4c62fc02009-08-08 20:40:27 -070075 static final String TAG = "WallpaperService";
Dianne Hackborncbf15042009-08-18 18:29:09 -070076 static final boolean DEBUG = false;
Joe Onorato9bb8fd72009-07-28 18:24:51 -070077
Dianne Hackborn4c62fc02009-08-08 20:40:27 -070078 Object mLock = new Object();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080079
Dianne Hackborn0cd48872009-08-13 18:51:59 -070080 /**
81 * Minimum time between crashes of a wallpaper service for us to consider
82 * restarting it vs. just reverting to the static wallpaper.
83 */
84 static final long MIN_WALLPAPER_CRASH_TIME = 10000;
85
86 static final File WALLPAPER_DIR = new File(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080087 "/data/data/com.android.settings/files");
Dianne Hackborn0cd48872009-08-13 18:51:59 -070088 static final String WALLPAPER = "wallpaper";
89 static final File WALLPAPER_FILE = new File(WALLPAPER_DIR, WALLPAPER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080090
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080091 /**
92 * List of callbacks registered they should each be notified
93 * when the wallpaper is changed.
94 */
Dianne Hackborn8cc6a502009-08-05 21:29:42 -070095 private final RemoteCallbackList<IWallpaperManagerCallback> mCallbacks
96 = new RemoteCallbackList<IWallpaperManagerCallback>();
Joe Onorato9bb8fd72009-07-28 18:24:51 -070097
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080098 /**
99 * Observes the wallpaper for changes and notifies all IWallpaperServiceCallbacks
100 * that the wallpaper has changed. The CREATE is triggered when there is no
101 * wallpaper set and is created for the first time. The CLOSE_WRITE is triggered
102 * everytime the wallpaper is changed.
103 */
104 private final FileObserver mWallpaperObserver = new FileObserver(
Joe Onorato9bb8fd72009-07-28 18:24:51 -0700105 WALLPAPER_DIR.getAbsolutePath(), CREATE | CLOSE_WRITE | DELETE | DELETE_SELF) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800106 @Override
107 public void onEvent(int event, String path) {
Joe Onoratoe712ee32009-07-29 16:23:58 -0700108 if (path == null) {
109 return;
110 }
Joe Onorato9bb8fd72009-07-28 18:24:51 -0700111 synchronized (mLock) {
Joe Onorato9bb8fd72009-07-28 18:24:51 -0700112 // changing the wallpaper means we'll need to back up the new one
113 long origId = Binder.clearCallingIdentity();
114 BackupManager bm = new BackupManager(mContext);
115 bm.dataChanged();
116 Binder.restoreCallingIdentity(origId);
117
118 File changedFile = new File(WALLPAPER_DIR, path);
119 if (WALLPAPER_FILE.equals(changedFile)) {
120 notifyCallbacksLocked();
121 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800122 }
123 }
124 };
125
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700126 final Context mContext;
127 final IWindowManager mIWindowManager;
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800128 final MyPackageMonitor mMonitor;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800129
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700130 int mWidth = -1;
131 int mHeight = -1;
Mike Clerona428b2c2009-11-15 22:53:08 -0800132
133 /**
134 * Resource name if using a picture from the wallpaper gallery
135 */
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700136 String mName = "";
Mike Clerond6c0b842009-11-13 16:37:27 -0800137
138 /**
Mike Clerona428b2c2009-11-15 22:53:08 -0800139 * The component name of the currently set live wallpaper.
Mike Clerond6c0b842009-11-13 16:37:27 -0800140 */
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700141 ComponentName mWallpaperComponent;
Mike Clerona428b2c2009-11-15 22:53:08 -0800142
143 /**
144 * The component name of the wallpaper that should be set next.
145 */
146 ComponentName mNextWallpaperComponent;
147
148 /**
149 * Name of the component used to display bitmap wallpapers from either the gallery or
150 * built-in wallpapers.
151 */
152 ComponentName mImageWallpaperComponent = new ComponentName("android",
153 ImageWallpaper.class.getName());
154
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700155 WallpaperConnection mWallpaperConnection;
Dianne Hackborn0cd48872009-08-13 18:51:59 -0700156 long mLastDiedTime;
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800157 boolean mWallpaperUpdating;
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700158
159 class WallpaperConnection extends IWallpaperConnection.Stub
160 implements ServiceConnection {
Dianne Hackborneb034652009-09-07 00:49:58 -0700161 final WallpaperInfo mInfo;
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700162 final Binder mToken = new Binder();
163 IWallpaperService mService;
164 IWallpaperEngine mEngine;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800165
Dianne Hackborneb034652009-09-07 00:49:58 -0700166 public WallpaperConnection(WallpaperInfo info) {
167 mInfo = info;
168 }
169
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700170 public void onServiceConnected(ComponentName name, IBinder service) {
171 synchronized (mLock) {
172 if (mWallpaperConnection == this) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800173 mLastDiedTime = SystemClock.uptimeMillis();
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700174 mService = IWallpaperService.Stub.asInterface(service);
175 attachServiceLocked(this);
Dianne Hackborneb034652009-09-07 00:49:58 -0700176 // XXX should probably do saveSettingsLocked() later
177 // when we have an engine, but I'm not sure about
178 // locking there and anyway we always need to be able to
179 // recover if there is something wrong.
180 saveSettingsLocked();
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700181 }
182 }
183 }
184
185 public void onServiceDisconnected(ComponentName name) {
186 synchronized (mLock) {
187 mService = null;
188 mEngine = null;
Dianne Hackborn0cd48872009-08-13 18:51:59 -0700189 if (mWallpaperConnection == this) {
190 Log.w(TAG, "Wallpaper service gone: " + mWallpaperComponent);
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800191 if (!mWallpaperUpdating && (mLastDiedTime+MIN_WALLPAPER_CRASH_TIME)
192 > SystemClock.uptimeMillis()) {
Dianne Hackborn0cd48872009-08-13 18:51:59 -0700193 Log.w(TAG, "Reverting to built-in wallpaper!");
Mike Clerona428b2c2009-11-15 22:53:08 -0800194 bindWallpaperComponentLocked(null);
Dianne Hackborn0cd48872009-08-13 18:51:59 -0700195 }
196 }
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700197 }
198 }
199
200 public void attachEngine(IWallpaperEngine engine) {
201 mEngine = engine;
202 }
203
204 public ParcelFileDescriptor setWallpaper(String name) {
205 synchronized (mLock) {
206 if (mWallpaperConnection == this) {
Dianne Hackborneb034652009-09-07 00:49:58 -0700207 return updateWallpaperBitmapLocked(name);
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700208 }
209 return null;
210 }
211 }
212 }
213
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800214 class MyPackageMonitor extends PackageMonitor {
215 @Override
216 public void onPackageUpdateFinished(String packageName, int uid) {
217 synchronized (mLock) {
218 if (mWallpaperComponent != null &&
219 mWallpaperComponent.getPackageName().equals(packageName)) {
220 mWallpaperUpdating = false;
221 ComponentName comp = mWallpaperComponent;
222 clearWallpaperComponentLocked();
223 bindWallpaperComponentLocked(comp);
224 }
225 }
226 }
227
228 @Override
229 public void onPackageUpdateStarted(String packageName, int uid) {
230 synchronized (mLock) {
231 if (mWallpaperComponent != null &&
232 mWallpaperComponent.getPackageName().equals(packageName)) {
233 mWallpaperUpdating = true;
234 }
235 }
236 }
237
238 @Override
239 public boolean onHandleForceStop(Intent intent, String[] packages, int uid, boolean doit) {
240 return doPackagesChanged(doit);
241 }
242
243 @Override
244 public void onSomePackagesChanged() {
245 doPackagesChanged(true);
246 }
247
248 boolean doPackagesChanged(boolean doit) {
249 boolean changed = false;
250 synchronized (mLock) {
251 if (mWallpaperComponent != null) {
252 int change = isPackageDisappearing(mWallpaperComponent.getPackageName());
253 if (change == PACKAGE_PERMANENT_CHANGE
254 || change == PACKAGE_TEMPORARY_CHANGE) {
255 changed = true;
256 if (doit) {
257 Log.w(TAG, "Wallpaper uninstalled, removing: " + mWallpaperComponent);
258 clearWallpaperLocked();
259 }
260 }
261 }
262 if (mNextWallpaperComponent != null) {
263 int change = isPackageDisappearing(mNextWallpaperComponent.getPackageName());
264 if (change == PACKAGE_PERMANENT_CHANGE
265 || change == PACKAGE_TEMPORARY_CHANGE) {
266 mNextWallpaperComponent = null;
267 }
268 }
269 if (mWallpaperComponent != null
270 && isPackageModified(mWallpaperComponent.getPackageName())) {
271 try {
272 mContext.getPackageManager().getServiceInfo(
273 mWallpaperComponent, 0);
274 } catch (NameNotFoundException e) {
275 Log.w(TAG, "Wallpaper component gone, removing: " + mWallpaperComponent);
276 clearWallpaperLocked();
277 }
278 }
279 if (mNextWallpaperComponent != null
280 && isPackageModified(mNextWallpaperComponent.getPackageName())) {
281 try {
282 mContext.getPackageManager().getServiceInfo(
283 mNextWallpaperComponent, 0);
284 } catch (NameNotFoundException e) {
285 mNextWallpaperComponent = null;
286 }
287 }
288 }
289 return changed;
290 }
291 }
292
Dianne Hackborn8cc6a502009-08-05 21:29:42 -0700293 public WallpaperManagerService(Context context) {
Mike Clerona428b2c2009-11-15 22:53:08 -0800294 if (DEBUG) Log.v(TAG, "WallpaperService startup");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800295 mContext = context;
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700296 mIWindowManager = IWindowManager.Stub.asInterface(
297 ServiceManager.getService(Context.WINDOW_SERVICE));
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800298 mMonitor = new MyPackageMonitor();
299 mMonitor.register(context, true);
Joe Onoratoe712ee32009-07-29 16:23:58 -0700300 WALLPAPER_DIR.mkdirs();
Joe Onorato9bb8fd72009-07-28 18:24:51 -0700301 loadSettingsLocked();
Joe Onoratoe712ee32009-07-29 16:23:58 -0700302 mWallpaperObserver.startWatching();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800303 }
304
305 @Override
306 protected void finalize() throws Throwable {
307 super.finalize();
308 mWallpaperObserver.stopWatching();
309 }
310
Dianne Hackbornf21adf62009-08-13 10:20:21 -0700311 public void systemReady() {
Mike Clerona428b2c2009-11-15 22:53:08 -0800312 if (DEBUG) Log.v(TAG, "systemReady");
Dianne Hackbornf21adf62009-08-13 10:20:21 -0700313 synchronized (mLock) {
314 try {
Mike Clerona428b2c2009-11-15 22:53:08 -0800315 bindWallpaperComponentLocked(mNextWallpaperComponent);
Dianne Hackbornf21adf62009-08-13 10:20:21 -0700316 } catch (RuntimeException e) {
317 Log.w(TAG, "Failure starting previous wallpaper", e);
318 try {
Mike Clerona428b2c2009-11-15 22:53:08 -0800319 bindWallpaperComponentLocked(null);
Dianne Hackbornf21adf62009-08-13 10:20:21 -0700320 } catch (RuntimeException e2) {
321 Log.w(TAG, "Failure starting default wallpaper", e2);
322 clearWallpaperComponentLocked();
323 }
324 }
325 }
326 }
327
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800328 public void clearWallpaper() {
Mike Clerona428b2c2009-11-15 22:53:08 -0800329 if (DEBUG) Log.v(TAG, "clearWallpaper");
Joe Onorato9bb8fd72009-07-28 18:24:51 -0700330 synchronized (mLock) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800331 clearWallpaperLocked();
332 }
333 }
334
335 public void clearWallpaperLocked() {
336 File f = WALLPAPER_FILE;
337 if (f.exists()) {
338 f.delete();
339 }
340 final long ident = Binder.clearCallingIdentity();
341 try {
342 bindWallpaperComponentLocked(null);
343 } finally {
344 Binder.restoreCallingIdentity(ident);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800345 }
346 }
347
348 public void setDimensionHints(int width, int height) throws RemoteException {
349 checkPermission(android.Manifest.permission.SET_WALLPAPER_HINTS);
350
351 if (width <= 0 || height <= 0) {
352 throw new IllegalArgumentException("width and height must be > 0");
353 }
354
Joe Onorato9bb8fd72009-07-28 18:24:51 -0700355 synchronized (mLock) {
356 if (width != mWidth || height != mHeight) {
357 mWidth = width;
358 mHeight = height;
359 saveSettingsLocked();
Dianne Hackborn284ac932009-08-28 10:34:25 -0700360 if (mWallpaperConnection != null) {
361 if (mWallpaperConnection.mEngine != null) {
362 try {
363 mWallpaperConnection.mEngine.setDesiredSize(
364 width, height);
365 } catch (RemoteException e) {
366 }
367 notifyCallbacksLocked();
368 }
369 }
Joe Onorato9bb8fd72009-07-28 18:24:51 -0700370 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800371 }
372 }
373
374 public int getWidthHint() throws RemoteException {
Joe Onorato9bb8fd72009-07-28 18:24:51 -0700375 synchronized (mLock) {
376 return mWidth;
377 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800378 }
379
380 public int getHeightHint() throws RemoteException {
Joe Onorato9bb8fd72009-07-28 18:24:51 -0700381 synchronized (mLock) {
382 return mHeight;
383 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800384 }
385
Dianne Hackborn284ac932009-08-28 10:34:25 -0700386 public ParcelFileDescriptor getWallpaper(IWallpaperManagerCallback cb,
387 Bundle outParams) {
Joe Onorato9bb8fd72009-07-28 18:24:51 -0700388 synchronized (mLock) {
389 try {
Dianne Hackborn284ac932009-08-28 10:34:25 -0700390 if (outParams != null) {
391 outParams.putInt("width", mWidth);
392 outParams.putInt("height", mHeight);
393 }
Joe Onorato9bb8fd72009-07-28 18:24:51 -0700394 mCallbacks.register(cb);
395 File f = WALLPAPER_FILE;
396 if (!f.exists()) {
397 return null;
398 }
399 return ParcelFileDescriptor.open(f, MODE_READ_ONLY);
400 } catch (FileNotFoundException e) {
401 /* Shouldn't happen as we check to see if the file exists */
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700402 Log.w(TAG, "Error getting wallpaper", e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800403 }
Joe Onorato9bb8fd72009-07-28 18:24:51 -0700404 return null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800405 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800406 }
407
Dianne Hackborneb034652009-09-07 00:49:58 -0700408 public WallpaperInfo getWallpaperInfo() {
409 synchronized (mLock) {
410 if (mWallpaperConnection != null) {
411 return mWallpaperConnection.mInfo;
412 }
413 return null;
414 }
415 }
416
Joe Onorato9bb8fd72009-07-28 18:24:51 -0700417 public ParcelFileDescriptor setWallpaper(String name) {
Mike Clerona428b2c2009-11-15 22:53:08 -0800418 if (DEBUG) Log.v(TAG, "setWallpaper");
419
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800420 checkPermission(android.Manifest.permission.SET_WALLPAPER);
Joe Onorato9bb8fd72009-07-28 18:24:51 -0700421 synchronized (mLock) {
Dianne Hackborn0cd48872009-08-13 18:51:59 -0700422 final long ident = Binder.clearCallingIdentity();
423 try {
424 ParcelFileDescriptor pfd = updateWallpaperBitmapLocked(name);
425 if (pfd != null) {
Mike Cleron322b6ee2009-11-12 07:45:47 -0800426 // Bind the wallpaper to an ImageWallpaper
Mike Clerona428b2c2009-11-15 22:53:08 -0800427 bindWallpaperComponentLocked(mImageWallpaperComponent);
Dianne Hackborn0cd48872009-08-13 18:51:59 -0700428 saveSettingsLocked();
429 }
430 return pfd;
431 } finally {
432 Binder.restoreCallingIdentity(ident);
Joe Onorato9bb8fd72009-07-28 18:24:51 -0700433 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800434 }
435 }
436
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700437 ParcelFileDescriptor updateWallpaperBitmapLocked(String name) {
438 if (name == null) name = "";
439 try {
440 ParcelFileDescriptor fd = ParcelFileDescriptor.open(WALLPAPER_FILE,
441 MODE_CREATE|MODE_READ_WRITE);
442 mName = name;
443 return fd;
444 } catch (FileNotFoundException e) {
445 Log.w(TAG, "Error setting wallpaper", e);
446 }
447 return null;
448 }
449
450 public void setWallpaperComponent(ComponentName name) {
Mike Clerona428b2c2009-11-15 22:53:08 -0800451 if (DEBUG) Log.v(TAG, "setWallpaperComponent name=" + name);
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700452 checkPermission(android.Manifest.permission.SET_WALLPAPER_COMPONENT);
453 synchronized (mLock) {
454 final long ident = Binder.clearCallingIdentity();
455 try {
Mike Clerona428b2c2009-11-15 22:53:08 -0800456 bindWallpaperComponentLocked(name);
Dianne Hackbornf21adf62009-08-13 10:20:21 -0700457 } finally {
458 Binder.restoreCallingIdentity(ident);
459 }
460 }
461 }
462
Mike Clerona428b2c2009-11-15 22:53:08 -0800463 void bindWallpaperComponentLocked(ComponentName componentName) {
464 if (DEBUG) Log.v(TAG, "bindWallpaperComponentLocked: componentName=" + componentName);
465
Dianne Hackbornf21adf62009-08-13 10:20:21 -0700466 // Has the component changed?
467 if (mWallpaperConnection != null) {
468 if (mWallpaperComponent == null) {
Mike Cleron322b6ee2009-11-12 07:45:47 -0800469 if (componentName == null) {
Mike Clerona428b2c2009-11-15 22:53:08 -0800470 if (DEBUG) Log.v(TAG, "bindWallpaperComponentLocked: still using default");
Dianne Hackbornf21adf62009-08-13 10:20:21 -0700471 // Still using default wallpaper.
472 return;
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700473 }
Mike Cleron322b6ee2009-11-12 07:45:47 -0800474 } else if (mWallpaperComponent.equals(componentName)) {
Dianne Hackbornf21adf62009-08-13 10:20:21 -0700475 // Changing to same wallpaper.
Mike Clerona428b2c2009-11-15 22:53:08 -0800476 if (DEBUG) Log.v(TAG, "same wallpaper");
Dianne Hackbornf21adf62009-08-13 10:20:21 -0700477 return;
478 }
479 }
480
481 try {
Mike Clerona428b2c2009-11-15 22:53:08 -0800482 if (componentName == null) {
Mike Cleron322b6ee2009-11-12 07:45:47 -0800483 String defaultComponent =
484 mContext.getString(com.android.internal.R.string.default_wallpaper_component);
Mike Clerona428b2c2009-11-15 22:53:08 -0800485 if (defaultComponent != null) {
Mike Cleron322b6ee2009-11-12 07:45:47 -0800486 // See if there is a default wallpaper component specified
Mike Clerona428b2c2009-11-15 22:53:08 -0800487 componentName = ComponentName.unflattenFromString(defaultComponent);
Christopher Tatee3ab4d02009-12-16 14:03:31 -0800488 if (DEBUG) Log.v(TAG, "Use default component wallpaper:" + componentName);
Mike Cleron322b6ee2009-11-12 07:45:47 -0800489 }
Mike Clerona428b2c2009-11-15 22:53:08 -0800490 if (componentName == null) {
Mike Cleron322b6ee2009-11-12 07:45:47 -0800491 // Fall back to static image wallpaper
Mike Clerona428b2c2009-11-15 22:53:08 -0800492 componentName = mImageWallpaperComponent;
Mike Cleron322b6ee2009-11-12 07:45:47 -0800493 //clearWallpaperComponentLocked();
494 //return;
Mike Clerona428b2c2009-11-15 22:53:08 -0800495 if (DEBUG) Log.v(TAG, "Using image wallpaper");
Mike Cleron322b6ee2009-11-12 07:45:47 -0800496 }
Dianne Hackbornf21adf62009-08-13 10:20:21 -0700497 }
Mike Clerona428b2c2009-11-15 22:53:08 -0800498 ServiceInfo si = mContext.getPackageManager().getServiceInfo(componentName,
Dianne Hackbornf21adf62009-08-13 10:20:21 -0700499 PackageManager.GET_META_DATA | PackageManager.GET_PERMISSIONS);
500 if (!android.Manifest.permission.BIND_WALLPAPER.equals(si.permission)) {
501 throw new SecurityException("Selected service does not require "
502 + android.Manifest.permission.BIND_WALLPAPER
Mike Clerona428b2c2009-11-15 22:53:08 -0800503 + ": " + componentName);
Dianne Hackbornf21adf62009-08-13 10:20:21 -0700504 }
505
Dianne Hackborneb034652009-09-07 00:49:58 -0700506 WallpaperInfo wi = null;
507
Dianne Hackbornf21adf62009-08-13 10:20:21 -0700508 Intent intent = new Intent(WallpaperService.SERVICE_INTERFACE);
Mike Clerona428b2c2009-11-15 22:53:08 -0800509 if (componentName != null && !componentName.equals(mImageWallpaperComponent)) {
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700510 // Make sure the selected service is actually a wallpaper service.
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700511 List<ResolveInfo> ris = mContext.getPackageManager()
Dianne Hackborneb034652009-09-07 00:49:58 -0700512 .queryIntentServices(intent, PackageManager.GET_META_DATA);
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700513 for (int i=0; i<ris.size(); i++) {
514 ServiceInfo rsi = ris.get(i).serviceInfo;
515 if (rsi.name.equals(si.name) &&
516 rsi.packageName.equals(si.packageName)) {
Dianne Hackborneb034652009-09-07 00:49:58 -0700517 try {
518 wi = new WallpaperInfo(mContext, ris.get(i));
519 } catch (XmlPullParserException e) {
520 throw new IllegalArgumentException(e);
521 } catch (IOException e) {
522 throw new IllegalArgumentException(e);
523 }
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700524 break;
525 }
526 }
Dianne Hackborneb034652009-09-07 00:49:58 -0700527 if (wi == null) {
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700528 throw new SecurityException("Selected service is not a wallpaper: "
Mike Clerona428b2c2009-11-15 22:53:08 -0800529 + componentName);
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700530 }
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700531 }
Dianne Hackbornf21adf62009-08-13 10:20:21 -0700532
533 // Bind the service!
Mike Clerona428b2c2009-11-15 22:53:08 -0800534 if (DEBUG) Log.v(TAG, "Binding to:" + componentName);
Dianne Hackborneb034652009-09-07 00:49:58 -0700535 WallpaperConnection newConn = new WallpaperConnection(wi);
Mike Clerona428b2c2009-11-15 22:53:08 -0800536 intent.setComponent(componentName);
Dianne Hackborndd9b82c2009-09-03 00:18:47 -0700537 intent.putExtra(Intent.EXTRA_CLIENT_LABEL,
538 com.android.internal.R.string.wallpaper_binding_label);
539 intent.putExtra(Intent.EXTRA_CLIENT_INTENT, PendingIntent.getActivity(
Dianne Hackborneb034652009-09-07 00:49:58 -0700540 mContext, 0,
541 Intent.createChooser(new Intent(Intent.ACTION_SET_WALLPAPER),
542 mContext.getText(com.android.internal.R.string.chooser_wallpaper)),
543 0));
Dianne Hackbornf21adf62009-08-13 10:20:21 -0700544 if (!mContext.bindService(intent, newConn,
545 Context.BIND_AUTO_CREATE)) {
546 throw new IllegalArgumentException("Unable to bind service: "
Mike Cleron322b6ee2009-11-12 07:45:47 -0800547 + componentName);
Dianne Hackbornf21adf62009-08-13 10:20:21 -0700548 }
549
550 clearWallpaperComponentLocked();
Mike Cleron322b6ee2009-11-12 07:45:47 -0800551 mWallpaperComponent = componentName;
Dianne Hackbornf21adf62009-08-13 10:20:21 -0700552 mWallpaperConnection = newConn;
Dianne Hackborn0cd48872009-08-13 18:51:59 -0700553 mLastDiedTime = SystemClock.uptimeMillis();
Dianne Hackbornf21adf62009-08-13 10:20:21 -0700554 try {
555 if (DEBUG) Log.v(TAG, "Adding window token: " + newConn.mToken);
556 mIWindowManager.addWindowToken(newConn.mToken,
557 WindowManager.LayoutParams.TYPE_WALLPAPER);
558 } catch (RemoteException e) {
559 }
560
561 } catch (PackageManager.NameNotFoundException e) {
Mike Cleron322b6ee2009-11-12 07:45:47 -0800562 throw new IllegalArgumentException("Unknown component " + componentName);
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700563 }
564 }
565
566 void clearWallpaperComponentLocked() {
567 mWallpaperComponent = null;
568 if (mWallpaperConnection != null) {
569 if (mWallpaperConnection.mEngine != null) {
570 try {
571 mWallpaperConnection.mEngine.destroy();
572 } catch (RemoteException e) {
573 }
574 }
575 mContext.unbindService(mWallpaperConnection);
Dianne Hackborne9e9bca2009-08-18 15:08:22 -0700576 try {
577 if (DEBUG) Log.v(TAG, "Removing window token: "
578 + mWallpaperConnection.mToken);
579 mIWindowManager.removeWindowToken(mWallpaperConnection.mToken);
580 } catch (RemoteException e) {
581 }
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700582 mWallpaperConnection = null;
583 }
584 }
585
586 void attachServiceLocked(WallpaperConnection conn) {
587 try {
Dianne Hackborn3be63c02009-08-20 19:31:38 -0700588 conn.mService.attach(conn, conn.mToken,
589 WindowManager.LayoutParams.TYPE_WALLPAPER, false,
590 mWidth, mHeight);
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700591 } catch (RemoteException e) {
592 Log.w(TAG, "Failed attaching wallpaper; clearing", e);
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800593 if (!mWallpaperUpdating) {
594 bindWallpaperComponentLocked(null);
595 }
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700596 }
597 }
598
Joe Onorato9bb8fd72009-07-28 18:24:51 -0700599 private void notifyCallbacksLocked() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800600 final int n = mCallbacks.beginBroadcast();
601 for (int i = 0; i < n; i++) {
602 try {
603 mCallbacks.getBroadcastItem(i).onWallpaperChanged();
604 } catch (RemoteException e) {
605
606 // The RemoteCallbackList will take care of removing
607 // the dead object for us.
608 }
609 }
610 mCallbacks.finishBroadcast();
611 final Intent intent = new Intent(Intent.ACTION_WALLPAPER_CHANGED);
612 mContext.sendBroadcast(intent);
613 }
614
615 private void checkPermission(String permission) {
Joe Onorato9bb8fd72009-07-28 18:24:51 -0700616 if (PackageManager.PERMISSION_GRANTED!= mContext.checkCallingOrSelfPermission(permission)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800617 throw new SecurityException("Access denied to process: " + Binder.getCallingPid()
618 + ", must have permission " + permission);
619 }
620 }
Joe Onorato9bb8fd72009-07-28 18:24:51 -0700621
622 private static JournaledFile makeJournaledFile() {
623 final String base = "/data/system/wallpaper_info.xml";
624 return new JournaledFile(new File(base), new File(base + ".tmp"));
625 }
626
627 private void saveSettingsLocked() {
628 JournaledFile journal = makeJournaledFile();
629 FileOutputStream stream = null;
630 try {
631 stream = new FileOutputStream(journal.chooseForWrite(), false);
632 XmlSerializer out = new FastXmlSerializer();
633 out.setOutput(stream, "utf-8");
634 out.startDocument(null, true);
635
636 out.startTag(null, "wp");
637 out.attribute(null, "width", Integer.toString(mWidth));
638 out.attribute(null, "height", Integer.toString(mHeight));
639 out.attribute(null, "name", mName);
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700640 if (mWallpaperComponent != null) {
641 out.attribute(null, "component",
642 mWallpaperComponent.flattenToShortString());
643 }
Joe Onorato9bb8fd72009-07-28 18:24:51 -0700644 out.endTag(null, "wp");
645
646 out.endDocument();
647 stream.close();
648 journal.commit();
649 } catch (IOException e) {
650 try {
651 if (stream != null) {
652 stream.close();
653 }
654 } catch (IOException ex) {
655 // Ignore
656 }
657 journal.rollback();
658 }
659 }
660
661 private void loadSettingsLocked() {
Mike Clerona428b2c2009-11-15 22:53:08 -0800662 if (DEBUG) Log.v(TAG, "loadSettingsLocked");
663
Joe Onorato9bb8fd72009-07-28 18:24:51 -0700664 JournaledFile journal = makeJournaledFile();
665 FileInputStream stream = null;
666 File file = journal.chooseForRead();
667 boolean success = false;
668 try {
669 stream = new FileInputStream(file);
670 XmlPullParser parser = Xml.newPullParser();
671 parser.setInput(stream, null);
672
673 int type;
Joe Onorato9bb8fd72009-07-28 18:24:51 -0700674 do {
675 type = parser.next();
676 if (type == XmlPullParser.START_TAG) {
677 String tag = parser.getName();
678 if ("wp".equals(tag)) {
679 mWidth = Integer.parseInt(parser.getAttributeValue(null, "width"));
680 mHeight = Integer.parseInt(parser.getAttributeValue(null, "height"));
681 mName = parser.getAttributeValue(null, "name");
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700682 String comp = parser.getAttributeValue(null, "component");
Mike Clerona428b2c2009-11-15 22:53:08 -0800683 mNextWallpaperComponent = comp != null
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700684 ? ComponentName.unflattenFromString(comp)
685 : null;
Mike Clerona428b2c2009-11-15 22:53:08 -0800686
687 if (DEBUG) {
688 Log.v(TAG, "mWidth:" + mWidth);
689 Log.v(TAG, "mHeight:" + mHeight);
690 Log.v(TAG, "mName:" + mName);
691 Log.v(TAG, "mNextWallpaperComponent:" + mNextWallpaperComponent);
692 }
Joe Onorato9bb8fd72009-07-28 18:24:51 -0700693 }
694 }
695 } while (type != XmlPullParser.END_DOCUMENT);
696 success = true;
697 } catch (NullPointerException e) {
Joe Onorato2d9c9e32009-07-29 16:43:06 -0700698 Log.w(TAG, "failed parsing " + file + " " + e);
Joe Onorato9bb8fd72009-07-28 18:24:51 -0700699 } catch (NumberFormatException e) {
Joe Onorato2d9c9e32009-07-29 16:43:06 -0700700 Log.w(TAG, "failed parsing " + file + " " + e);
Joe Onorato9bb8fd72009-07-28 18:24:51 -0700701 } catch (XmlPullParserException e) {
Joe Onorato2d9c9e32009-07-29 16:43:06 -0700702 Log.w(TAG, "failed parsing " + file + " " + e);
Joe Onorato9bb8fd72009-07-28 18:24:51 -0700703 } catch (IOException e) {
Joe Onorato2d9c9e32009-07-29 16:43:06 -0700704 Log.w(TAG, "failed parsing " + file + " " + e);
Joe Onorato9bb8fd72009-07-28 18:24:51 -0700705 } catch (IndexOutOfBoundsException e) {
Joe Onorato2d9c9e32009-07-29 16:43:06 -0700706 Log.w(TAG, "failed parsing " + file + " " + e);
Joe Onorato9bb8fd72009-07-28 18:24:51 -0700707 }
708 try {
709 if (stream != null) {
710 stream.close();
711 }
712 } catch (IOException e) {
713 // Ignore
714 }
715
716 if (!success) {
717 mWidth = -1;
718 mHeight = -1;
719 mName = "";
720 }
721 }
722
723 void settingsRestored() {
Mike Clerona428b2c2009-11-15 22:53:08 -0800724 if (DEBUG) Log.v(TAG, "settingsRestored");
725
Joe Onorato9bb8fd72009-07-28 18:24:51 -0700726 boolean success = false;
727 synchronized (mLock) {
728 loadSettingsLocked();
Mike Clerona428b2c2009-11-15 22:53:08 -0800729 if (mNextWallpaperComponent != null &&
730 !mNextWallpaperComponent.equals(mImageWallpaperComponent)) {
Christopher Tatee3ab4d02009-12-16 14:03:31 -0800731 try {
732 bindWallpaperComponentLocked(mNextWallpaperComponent);
733 } catch (IllegalArgumentException e) {
734 // No such live wallpaper or other failure; fall back to the default
735 // live wallpaper (since the profile being restored indicated that the
736 // user had selected a live rather than static one).
737 bindWallpaperComponentLocked(null);
738 }
Joe Onorato9bb8fd72009-07-28 18:24:51 -0700739 success = true;
740 } else {
Mike Clerona428b2c2009-11-15 22:53:08 -0800741 // If there's a wallpaper name, we use that. If that can't be loaded, then we
742 // use the default.
743 if ("".equals(mName)) {
744 if (DEBUG) Log.v(TAG, "settingsRestored: name is empty");
745 success = true;
746 } else {
747 if (DEBUG) Log.v(TAG, "settingsRestored: attempting to restore named resource");
748 success = restoreNamedResourceLocked();
749 }
750 if (DEBUG) Log.v(TAG, "settingsRestored: success=" + success);
751 if (success) {
752 bindWallpaperComponentLocked(mImageWallpaperComponent);
753 }
Joe Onorato9bb8fd72009-07-28 18:24:51 -0700754 }
755 }
756
757 if (!success) {
758 Log.e(TAG, "Failed to restore wallpaper: '" + mName + "'");
759 mName = "";
760 WALLPAPER_FILE.delete();
761 }
762 saveSettingsLocked();
763 }
764
765 boolean restoreNamedResourceLocked() {
766 if (mName.length() > 4 && "res:".equals(mName.substring(0, 4))) {
767 String resName = mName.substring(4);
768
769 String pkg = null;
770 int colon = resName.indexOf(':');
771 if (colon > 0) {
772 pkg = resName.substring(0, colon);
773 }
774
775 String ident = null;
776 int slash = resName.lastIndexOf('/');
777 if (slash > 0) {
778 ident = resName.substring(slash+1);
779 }
780
781 String type = null;
782 if (colon > 0 && slash > 0 && (slash-colon) > 1) {
783 type = resName.substring(colon+1, slash);
784 }
785
786 if (pkg != null && ident != null && type != null) {
787 int resId = -1;
788 InputStream res = null;
789 FileOutputStream fos = null;
790 try {
791 Context c = mContext.createPackageContext(pkg, Context.CONTEXT_RESTRICTED);
792 Resources r = c.getResources();
793 resId = r.getIdentifier(resName, null, null);
794 if (resId == 0) {
795 Log.e(TAG, "couldn't resolve identifier pkg=" + pkg + " type=" + type
796 + " ident=" + ident);
797 return false;
798 }
799
800 res = r.openRawResource(resId);
801 fos = new FileOutputStream(WALLPAPER_FILE);
802
803 byte[] buffer = new byte[32768];
804 int amt;
805 while ((amt=res.read(buffer)) > 0) {
806 fos.write(buffer, 0, amt);
807 }
808 // mWallpaperObserver will notice the close and send the change broadcast
809
Mike Clerona428b2c2009-11-15 22:53:08 -0800810 Log.v(TAG, "Restored wallpaper: " + resName);
Joe Onorato9bb8fd72009-07-28 18:24:51 -0700811 return true;
812 } catch (NameNotFoundException e) {
813 Log.e(TAG, "Package name " + pkg + " not found");
814 } catch (Resources.NotFoundException e) {
815 Log.e(TAG, "Resource not found: " + resId);
816 } catch (IOException e) {
817 Log.e(TAG, "IOException while restoring wallpaper ", e);
818 } finally {
819 if (res != null) {
820 try {
821 res.close();
822 } catch (IOException ex) {}
823 }
824 if (fos != null) {
825 try {
826 fos.close();
827 } catch (IOException ex) {}
828 }
829 }
830 }
831 }
832 return false;
833 }
Dianne Hackborneb034652009-09-07 00:49:58 -0700834
835 @Override
836 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
837 if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DUMP)
838 != PackageManager.PERMISSION_GRANTED) {
839
840 pw.println("Permission Denial: can't dump wallpaper service from from pid="
841 + Binder.getCallingPid()
842 + ", uid=" + Binder.getCallingUid());
843 return;
844 }
845
846 synchronized (mLock) {
847 pw.println("Current Wallpaper Service state:");
848 pw.print(" mWidth="); pw.print(mWidth);
849 pw.print(" mHeight="); pw.println(mHeight);
850 pw.print(" mName="); pw.println(mName);
851 pw.print(" mWallpaperComponent="); pw.println(mWallpaperComponent);
852 if (mWallpaperConnection != null) {
853 WallpaperConnection conn = mWallpaperConnection;
854 pw.print(" Wallpaper connection ");
855 pw.print(conn); pw.println(":");
856 pw.print(" mInfo.component="); pw.println(conn.mInfo.getComponent());
857 pw.print(" mToken="); pw.println(conn.mToken);
858 pw.print(" mService="); pw.println(conn.mService);
859 pw.print(" mEngine="); pw.println(conn.mEngine);
860 pw.print(" mLastDiedTime=");
861 pw.println(mLastDiedTime - SystemClock.uptimeMillis());
862 }
863 }
864 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800865}