blob: 5b8e11cd30ae4d6dc122745fd01c005ae215961d [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!");
Mike Cleron322b6ee2009-11-12 07:45:47 -0800167 bindWallpaperComponentLocked(null, false);
Dianne Hackborn0cd48872009-08-13 18:51:59 -0700168 }
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 {
Mike Cleron322b6ee2009-11-12 07:45:47 -0800206 bindWallpaperComponentLocked(mWallpaperComponent, false);
Dianne Hackbornf21adf62009-08-13 10:20:21 -0700207 } catch (RuntimeException e) {
208 Log.w(TAG, "Failure starting previous wallpaper", e);
209 try {
Mike Cleron322b6ee2009-11-12 07:45:47 -0800210 bindWallpaperComponentLocked(null, false);
Dianne Hackbornf21adf62009-08-13 10:20:21 -0700211 } 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 {
Mike Cleron322b6ee2009-11-12 07:45:47 -0800227 bindWallpaperComponentLocked(null, false);
Dianne Hackborn0cd48872009-08-13 18:51:59 -0700228 } 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) {
Mike Cleron322b6ee2009-11-12 07:45:47 -0800310 // Bind the wallpaper to an ImageWallpaper
311 bindWallpaperComponentLocked(null, true);
Dianne Hackborn0cd48872009-08-13 18:51:59 -0700312 saveSettingsLocked();
313 }
314 return pfd;
315 } finally {
316 Binder.restoreCallingIdentity(ident);
Joe Onorato9bb8fd72009-07-28 18:24:51 -0700317 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800318 }
319 }
320
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700321 ParcelFileDescriptor updateWallpaperBitmapLocked(String name) {
322 if (name == null) name = "";
323 try {
324 ParcelFileDescriptor fd = ParcelFileDescriptor.open(WALLPAPER_FILE,
325 MODE_CREATE|MODE_READ_WRITE);
326 mName = name;
327 return fd;
328 } catch (FileNotFoundException e) {
329 Log.w(TAG, "Error setting wallpaper", e);
330 }
331 return null;
332 }
333
334 public void setWallpaperComponent(ComponentName name) {
335 checkPermission(android.Manifest.permission.SET_WALLPAPER_COMPONENT);
336 synchronized (mLock) {
337 final long ident = Binder.clearCallingIdentity();
338 try {
Mike Cleron322b6ee2009-11-12 07:45:47 -0800339 bindWallpaperComponentLocked(name, false);
Dianne Hackbornf21adf62009-08-13 10:20:21 -0700340 } finally {
341 Binder.restoreCallingIdentity(ident);
342 }
343 }
344 }
345
Mike Cleron322b6ee2009-11-12 07:45:47 -0800346 void bindWallpaperComponentLocked(ComponentName componentName, boolean isBitmap) {
Dianne Hackbornf21adf62009-08-13 10:20:21 -0700347 // Has the component changed?
348 if (mWallpaperConnection != null) {
349 if (mWallpaperComponent == null) {
Mike Cleron322b6ee2009-11-12 07:45:47 -0800350 if (componentName == null) {
Dianne Hackbornf21adf62009-08-13 10:20:21 -0700351 // Still using default wallpaper.
352 return;
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700353 }
Mike Cleron322b6ee2009-11-12 07:45:47 -0800354 } else if (mWallpaperComponent.equals(componentName)) {
Dianne Hackbornf21adf62009-08-13 10:20:21 -0700355 // Changing to same wallpaper.
356 return;
357 }
358 }
359
360 try {
Mike Cleron322b6ee2009-11-12 07:45:47 -0800361 ComponentName realComponentName = componentName;
362 if (realComponentName == null) {
363 String defaultComponent =
364 mContext.getString(com.android.internal.R.string.default_wallpaper_component);
365 if (defaultComponent != null && !isBitmap) {
366 // See if there is a default wallpaper component specified
367 // Only look for this if the wallpaper is not being set to a bitmap
368 realComponentName = ComponentName.unflattenFromString(defaultComponent);
369 }
370 if (realComponentName == null) {
371 // Fall back to static image wallpaper
372 realComponentName = new ComponentName("android",
373 ImageWallpaper.class.getName());
374 //clearWallpaperComponentLocked();
375 //return;
376 }
Dianne Hackbornf21adf62009-08-13 10:20:21 -0700377 }
Mike Cleron322b6ee2009-11-12 07:45:47 -0800378 ServiceInfo si = mContext.getPackageManager().getServiceInfo(realComponentName,
Dianne Hackbornf21adf62009-08-13 10:20:21 -0700379 PackageManager.GET_META_DATA | PackageManager.GET_PERMISSIONS);
380 if (!android.Manifest.permission.BIND_WALLPAPER.equals(si.permission)) {
381 throw new SecurityException("Selected service does not require "
382 + android.Manifest.permission.BIND_WALLPAPER
Mike Cleron322b6ee2009-11-12 07:45:47 -0800383 + ": " + realComponentName);
Dianne Hackbornf21adf62009-08-13 10:20:21 -0700384 }
385
Dianne Hackborneb034652009-09-07 00:49:58 -0700386 WallpaperInfo wi = null;
387
Dianne Hackbornf21adf62009-08-13 10:20:21 -0700388 Intent intent = new Intent(WallpaperService.SERVICE_INTERFACE);
Mike Cleron322b6ee2009-11-12 07:45:47 -0800389 if (componentName != null) {
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700390 // Make sure the selected service is actually a wallpaper service.
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700391 List<ResolveInfo> ris = mContext.getPackageManager()
Dianne Hackborneb034652009-09-07 00:49:58 -0700392 .queryIntentServices(intent, PackageManager.GET_META_DATA);
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700393 for (int i=0; i<ris.size(); i++) {
394 ServiceInfo rsi = ris.get(i).serviceInfo;
395 if (rsi.name.equals(si.name) &&
396 rsi.packageName.equals(si.packageName)) {
Dianne Hackborneb034652009-09-07 00:49:58 -0700397 try {
398 wi = new WallpaperInfo(mContext, ris.get(i));
399 } catch (XmlPullParserException e) {
400 throw new IllegalArgumentException(e);
401 } catch (IOException e) {
402 throw new IllegalArgumentException(e);
403 }
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700404 break;
405 }
406 }
Dianne Hackborneb034652009-09-07 00:49:58 -0700407 if (wi == null) {
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700408 throw new SecurityException("Selected service is not a wallpaper: "
Mike Cleron322b6ee2009-11-12 07:45:47 -0800409 + realComponentName);
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700410 }
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700411 }
Dianne Hackbornf21adf62009-08-13 10:20:21 -0700412
413 // Bind the service!
Dianne Hackborneb034652009-09-07 00:49:58 -0700414 WallpaperConnection newConn = new WallpaperConnection(wi);
Mike Cleron322b6ee2009-11-12 07:45:47 -0800415 intent.setComponent(realComponentName);
Dianne Hackborndd9b82c2009-09-03 00:18:47 -0700416 intent.putExtra(Intent.EXTRA_CLIENT_LABEL,
417 com.android.internal.R.string.wallpaper_binding_label);
418 intent.putExtra(Intent.EXTRA_CLIENT_INTENT, PendingIntent.getActivity(
Dianne Hackborneb034652009-09-07 00:49:58 -0700419 mContext, 0,
420 Intent.createChooser(new Intent(Intent.ACTION_SET_WALLPAPER),
421 mContext.getText(com.android.internal.R.string.chooser_wallpaper)),
422 0));
Dianne Hackbornf21adf62009-08-13 10:20:21 -0700423 if (!mContext.bindService(intent, newConn,
424 Context.BIND_AUTO_CREATE)) {
425 throw new IllegalArgumentException("Unable to bind service: "
Mike Cleron322b6ee2009-11-12 07:45:47 -0800426 + componentName);
Dianne Hackbornf21adf62009-08-13 10:20:21 -0700427 }
428
429 clearWallpaperComponentLocked();
Mike Cleron322b6ee2009-11-12 07:45:47 -0800430 mWallpaperComponent = componentName;
Dianne Hackbornf21adf62009-08-13 10:20:21 -0700431 mWallpaperConnection = newConn;
Dianne Hackborn0cd48872009-08-13 18:51:59 -0700432 mLastDiedTime = SystemClock.uptimeMillis();
Dianne Hackbornf21adf62009-08-13 10:20:21 -0700433 try {
434 if (DEBUG) Log.v(TAG, "Adding window token: " + newConn.mToken);
435 mIWindowManager.addWindowToken(newConn.mToken,
436 WindowManager.LayoutParams.TYPE_WALLPAPER);
437 } catch (RemoteException e) {
438 }
439
440 } catch (PackageManager.NameNotFoundException e) {
Mike Cleron322b6ee2009-11-12 07:45:47 -0800441 throw new IllegalArgumentException("Unknown component " + componentName);
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700442 }
443 }
444
445 void clearWallpaperComponentLocked() {
446 mWallpaperComponent = null;
447 if (mWallpaperConnection != null) {
448 if (mWallpaperConnection.mEngine != null) {
449 try {
450 mWallpaperConnection.mEngine.destroy();
451 } catch (RemoteException e) {
452 }
453 }
454 mContext.unbindService(mWallpaperConnection);
Dianne Hackborne9e9bca2009-08-18 15:08:22 -0700455 try {
456 if (DEBUG) Log.v(TAG, "Removing window token: "
457 + mWallpaperConnection.mToken);
458 mIWindowManager.removeWindowToken(mWallpaperConnection.mToken);
459 } catch (RemoteException e) {
460 }
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700461 mWallpaperConnection = null;
462 }
463 }
464
465 void attachServiceLocked(WallpaperConnection conn) {
466 try {
Dianne Hackborn3be63c02009-08-20 19:31:38 -0700467 conn.mService.attach(conn, conn.mToken,
468 WindowManager.LayoutParams.TYPE_WALLPAPER, false,
469 mWidth, mHeight);
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700470 } catch (RemoteException e) {
471 Log.w(TAG, "Failed attaching wallpaper; clearing", e);
Mike Cleron322b6ee2009-11-12 07:45:47 -0800472 bindWallpaperComponentLocked(null, false);
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700473 }
474 }
475
Joe Onorato9bb8fd72009-07-28 18:24:51 -0700476 private void notifyCallbacksLocked() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800477 final int n = mCallbacks.beginBroadcast();
478 for (int i = 0; i < n; i++) {
479 try {
480 mCallbacks.getBroadcastItem(i).onWallpaperChanged();
481 } catch (RemoteException e) {
482
483 // The RemoteCallbackList will take care of removing
484 // the dead object for us.
485 }
486 }
487 mCallbacks.finishBroadcast();
488 final Intent intent = new Intent(Intent.ACTION_WALLPAPER_CHANGED);
489 mContext.sendBroadcast(intent);
490 }
491
492 private void checkPermission(String permission) {
Joe Onorato9bb8fd72009-07-28 18:24:51 -0700493 if (PackageManager.PERMISSION_GRANTED!= mContext.checkCallingOrSelfPermission(permission)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800494 throw new SecurityException("Access denied to process: " + Binder.getCallingPid()
495 + ", must have permission " + permission);
496 }
497 }
Joe Onorato9bb8fd72009-07-28 18:24:51 -0700498
499 private static JournaledFile makeJournaledFile() {
500 final String base = "/data/system/wallpaper_info.xml";
501 return new JournaledFile(new File(base), new File(base + ".tmp"));
502 }
503
504 private void saveSettingsLocked() {
505 JournaledFile journal = makeJournaledFile();
506 FileOutputStream stream = null;
507 try {
508 stream = new FileOutputStream(journal.chooseForWrite(), false);
509 XmlSerializer out = new FastXmlSerializer();
510 out.setOutput(stream, "utf-8");
511 out.startDocument(null, true);
512
513 out.startTag(null, "wp");
514 out.attribute(null, "width", Integer.toString(mWidth));
515 out.attribute(null, "height", Integer.toString(mHeight));
516 out.attribute(null, "name", mName);
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700517 if (mWallpaperComponent != null) {
518 out.attribute(null, "component",
519 mWallpaperComponent.flattenToShortString());
520 }
Joe Onorato9bb8fd72009-07-28 18:24:51 -0700521 out.endTag(null, "wp");
522
523 out.endDocument();
524 stream.close();
525 journal.commit();
526 } catch (IOException e) {
527 try {
528 if (stream != null) {
529 stream.close();
530 }
531 } catch (IOException ex) {
532 // Ignore
533 }
534 journal.rollback();
535 }
536 }
537
538 private void loadSettingsLocked() {
539 JournaledFile journal = makeJournaledFile();
540 FileInputStream stream = null;
541 File file = journal.chooseForRead();
542 boolean success = false;
543 try {
544 stream = new FileInputStream(file);
545 XmlPullParser parser = Xml.newPullParser();
546 parser.setInput(stream, null);
547
548 int type;
Joe Onorato9bb8fd72009-07-28 18:24:51 -0700549 do {
550 type = parser.next();
551 if (type == XmlPullParser.START_TAG) {
552 String tag = parser.getName();
553 if ("wp".equals(tag)) {
554 mWidth = Integer.parseInt(parser.getAttributeValue(null, "width"));
555 mHeight = Integer.parseInt(parser.getAttributeValue(null, "height"));
556 mName = parser.getAttributeValue(null, "name");
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700557 String comp = parser.getAttributeValue(null, "component");
558 mWallpaperComponent = comp != null
559 ? ComponentName.unflattenFromString(comp)
560 : null;
Joe Onorato9bb8fd72009-07-28 18:24:51 -0700561 }
562 }
563 } while (type != XmlPullParser.END_DOCUMENT);
564 success = true;
565 } catch (NullPointerException e) {
Joe Onorato2d9c9e32009-07-29 16:43:06 -0700566 Log.w(TAG, "failed parsing " + file + " " + e);
Joe Onorato9bb8fd72009-07-28 18:24:51 -0700567 } catch (NumberFormatException e) {
Joe Onorato2d9c9e32009-07-29 16:43:06 -0700568 Log.w(TAG, "failed parsing " + file + " " + e);
Joe Onorato9bb8fd72009-07-28 18:24:51 -0700569 } catch (XmlPullParserException e) {
Joe Onorato2d9c9e32009-07-29 16:43:06 -0700570 Log.w(TAG, "failed parsing " + file + " " + e);
Joe Onorato9bb8fd72009-07-28 18:24:51 -0700571 } catch (IOException e) {
Joe Onorato2d9c9e32009-07-29 16:43:06 -0700572 Log.w(TAG, "failed parsing " + file + " " + e);
Joe Onorato9bb8fd72009-07-28 18:24:51 -0700573 } catch (IndexOutOfBoundsException e) {
Joe Onorato2d9c9e32009-07-29 16:43:06 -0700574 Log.w(TAG, "failed parsing " + file + " " + e);
Joe Onorato9bb8fd72009-07-28 18:24:51 -0700575 }
576 try {
577 if (stream != null) {
578 stream.close();
579 }
580 } catch (IOException e) {
581 // Ignore
582 }
583
584 if (!success) {
585 mWidth = -1;
586 mHeight = -1;
587 mName = "";
588 }
589 }
590
591 void settingsRestored() {
592 boolean success = false;
593 synchronized (mLock) {
594 loadSettingsLocked();
595 // If there's a wallpaper name, we use that. If that can't be loaded, then we
596 // use the default.
597 if ("".equals(mName)) {
598 success = true;
599 } else {
600 success = restoreNamedResourceLocked();
601 }
602 }
603
604 if (!success) {
605 Log.e(TAG, "Failed to restore wallpaper: '" + mName + "'");
606 mName = "";
607 WALLPAPER_FILE.delete();
608 }
609 saveSettingsLocked();
610 }
611
612 boolean restoreNamedResourceLocked() {
613 if (mName.length() > 4 && "res:".equals(mName.substring(0, 4))) {
614 String resName = mName.substring(4);
615
616 String pkg = null;
617 int colon = resName.indexOf(':');
618 if (colon > 0) {
619 pkg = resName.substring(0, colon);
620 }
621
622 String ident = null;
623 int slash = resName.lastIndexOf('/');
624 if (slash > 0) {
625 ident = resName.substring(slash+1);
626 }
627
628 String type = null;
629 if (colon > 0 && slash > 0 && (slash-colon) > 1) {
630 type = resName.substring(colon+1, slash);
631 }
632
633 if (pkg != null && ident != null && type != null) {
634 int resId = -1;
635 InputStream res = null;
636 FileOutputStream fos = null;
637 try {
638 Context c = mContext.createPackageContext(pkg, Context.CONTEXT_RESTRICTED);
639 Resources r = c.getResources();
640 resId = r.getIdentifier(resName, null, null);
641 if (resId == 0) {
642 Log.e(TAG, "couldn't resolve identifier pkg=" + pkg + " type=" + type
643 + " ident=" + ident);
644 return false;
645 }
646
647 res = r.openRawResource(resId);
648 fos = new FileOutputStream(WALLPAPER_FILE);
649
650 byte[] buffer = new byte[32768];
651 int amt;
652 while ((amt=res.read(buffer)) > 0) {
653 fos.write(buffer, 0, amt);
654 }
655 // mWallpaperObserver will notice the close and send the change broadcast
656
657 Log.d(TAG, "Restored wallpaper: " + resName);
658 return true;
659 } catch (NameNotFoundException e) {
660 Log.e(TAG, "Package name " + pkg + " not found");
661 } catch (Resources.NotFoundException e) {
662 Log.e(TAG, "Resource not found: " + resId);
663 } catch (IOException e) {
664 Log.e(TAG, "IOException while restoring wallpaper ", e);
665 } finally {
666 if (res != null) {
667 try {
668 res.close();
669 } catch (IOException ex) {}
670 }
671 if (fos != null) {
672 try {
673 fos.close();
674 } catch (IOException ex) {}
675 }
676 }
677 }
678 }
679 return false;
680 }
Dianne Hackborneb034652009-09-07 00:49:58 -0700681
682 @Override
683 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
684 if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DUMP)
685 != PackageManager.PERMISSION_GRANTED) {
686
687 pw.println("Permission Denial: can't dump wallpaper service from from pid="
688 + Binder.getCallingPid()
689 + ", uid=" + Binder.getCallingUid());
690 return;
691 }
692
693 synchronized (mLock) {
694 pw.println("Current Wallpaper Service state:");
695 pw.print(" mWidth="); pw.print(mWidth);
696 pw.print(" mHeight="); pw.println(mHeight);
697 pw.print(" mName="); pw.println(mName);
698 pw.print(" mWallpaperComponent="); pw.println(mWallpaperComponent);
699 if (mWallpaperConnection != null) {
700 WallpaperConnection conn = mWallpaperConnection;
701 pw.print(" Wallpaper connection ");
702 pw.print(conn); pw.println(":");
703 pw.print(" mInfo.component="); pw.println(conn.mInfo.getComponent());
704 pw.print(" mToken="); pw.println(conn.mToken);
705 pw.print(" mService="); pw.println(conn.mService);
706 pw.print(" mEngine="); pw.println(conn.mEngine);
707 pw.print(" mLastDiedTime=");
708 pw.println(mLastDiedTime - SystemClock.uptimeMillis());
709 }
710 }
711 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800712}