blob: b027c1f76a766df44dbe137009cc59ce2b69e9d6 [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
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080019import static android.os.ParcelFileDescriptor.*;
Christopher Tate111bd4a2009-06-24 17:29:38 -070020
Dianne Hackborn5dc5a002012-09-15 19:33:48 -070021import android.app.ActivityManagerNative;
Amith Yamasani4e2820c2012-08-28 22:17:23 -070022import android.app.AppGlobals;
Dianne Hackborn5dc5a002012-09-15 19:33:48 -070023import android.app.IUserSwitchObserver;
Dianne Hackborn8cc6a502009-08-05 21:29:42 -070024import android.app.IWallpaperManager;
25import android.app.IWallpaperManagerCallback;
Dianne Hackborndd9b82c2009-09-03 00:18:47 -070026import android.app.PendingIntent;
Dianne Hackborneb034652009-09-07 00:49:58 -070027import android.app.WallpaperInfo;
Christopher Tate45281862010-03-05 15:46:30 -080028import android.app.backup.BackupManager;
Amith Yamasani37ce3a82012-02-06 12:04:42 -080029import android.app.backup.WallpaperBackupHelper;
Amith Yamasani13593602012-03-22 16:16:17 -070030import android.content.BroadcastReceiver;
Dianne Hackborn4c62fc02009-08-08 20:40:27 -070031import android.content.ComponentName;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080032import android.content.Context;
33import android.content.Intent;
Amith Yamasani13593602012-03-22 16:16:17 -070034import android.content.IntentFilter;
Dianne Hackborn4c62fc02009-08-08 20:40:27 -070035import android.content.ServiceConnection;
Amith Yamasani4e2820c2012-08-28 22:17:23 -070036import android.content.pm.IPackageManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080037import android.content.pm.PackageManager;
Dianne Hackborn4c62fc02009-08-08 20:40:27 -070038import android.content.pm.ResolveInfo;
39import android.content.pm.ServiceInfo;
Joe Onorato9bb8fd72009-07-28 18:24:51 -070040import android.content.pm.PackageManager.NameNotFoundException;
41import android.content.res.Resources;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080042import android.os.Binder;
Dianne Hackborn284ac932009-08-28 10:34:25 -070043import android.os.Bundle;
Amith Yamasani13593602012-03-22 16:16:17 -070044import android.os.Environment;
Dianne Hackborn8bdf5932010-10-15 12:54:40 -070045import android.os.FileUtils;
Dianne Hackborn4c62fc02009-08-08 20:40:27 -070046import android.os.IBinder;
Dianne Hackborn5dc5a002012-09-15 19:33:48 -070047import android.os.IRemoteCallback;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080048import android.os.RemoteException;
49import android.os.FileObserver;
50import android.os.ParcelFileDescriptor;
51import android.os.RemoteCallbackList;
rpcraig554cb0c2012-07-05 06:41:43 -040052import android.os.SELinux;
Dianne Hackborn4c62fc02009-08-08 20:40:27 -070053import android.os.ServiceManager;
Dianne Hackborn0cd48872009-08-13 18:51:59 -070054import android.os.SystemClock;
Dianne Hackbornf02b60a2012-08-16 10:48:27 -070055import android.os.UserHandle;
Dianne Hackborn4c62fc02009-08-08 20:40:27 -070056import android.service.wallpaper.IWallpaperConnection;
57import android.service.wallpaper.IWallpaperEngine;
58import android.service.wallpaper.IWallpaperService;
59import android.service.wallpaper.WallpaperService;
Joe Onorato8a9b2202010-02-26 18:56:32 -080060import android.util.Slog;
Amith Yamasani37ce3a82012-02-06 12:04:42 -080061import android.util.SparseArray;
Joe Onorato9bb8fd72009-07-28 18:24:51 -070062import android.util.Xml;
Dianne Hackborn44bc17c2011-04-20 18:18:51 -070063import android.view.Display;
Dianne Hackborn4c62fc02009-08-08 20:40:27 -070064import android.view.IWindowManager;
65import android.view.WindowManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080066
Dianne Hackborneb034652009-09-07 00:49:58 -070067import java.io.FileDescriptor;
Joe Onorato9bb8fd72009-07-28 18:24:51 -070068import java.io.IOException;
69import java.io.InputStream;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080070import java.io.File;
71import java.io.FileNotFoundException;
Joe Onorato9bb8fd72009-07-28 18:24:51 -070072import java.io.FileInputStream;
73import java.io.FileOutputStream;
Dianne Hackborneb034652009-09-07 00:49:58 -070074import java.io.PrintWriter;
Dianne Hackborn4c62fc02009-08-08 20:40:27 -070075import java.util.List;
Joe Onorato9bb8fd72009-07-28 18:24:51 -070076
77import org.xmlpull.v1.XmlPullParser;
78import org.xmlpull.v1.XmlPullParserException;
79import org.xmlpull.v1.XmlSerializer;
80
Dianne Hackborn21f1bd12010-02-19 17:02:21 -080081import com.android.internal.content.PackageMonitor;
Dianne Hackborn2269d1572010-02-24 19:54:22 -080082import com.android.internal.util.FastXmlSerializer;
Dianne Hackborn1afd1c92010-03-18 22:47:17 -070083import com.android.internal.util.JournaledFile;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080084
Dianne Hackborn8cc6a502009-08-05 21:29:42 -070085class WallpaperManagerService extends IWallpaperManager.Stub {
Dianne Hackborn4c62fc02009-08-08 20:40:27 -070086 static final String TAG = "WallpaperService";
Dianne Hackborncbf15042009-08-18 18:29:09 -070087 static final boolean DEBUG = false;
Joe Onorato9bb8fd72009-07-28 18:24:51 -070088
Romain Guy407ec782011-08-24 17:06:58 -070089 final Object mLock = new Object[0];
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080090
Dianne Hackborn0cd48872009-08-13 18:51:59 -070091 /**
92 * Minimum time between crashes of a wallpaper service for us to consider
93 * restarting it vs. just reverting to the static wallpaper.
94 */
95 static final long MIN_WALLPAPER_CRASH_TIME = 10000;
Dianne Hackborn0cd48872009-08-13 18:51:59 -070096 static final String WALLPAPER = "wallpaper";
Amith Yamasani37ce3a82012-02-06 12:04:42 -080097 static final String WALLPAPER_INFO = "wallpaper_info.xml";
Joe Onorato9bb8fd72009-07-28 18:24:51 -070098
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080099 /**
100 * Observes the wallpaper for changes and notifies all IWallpaperServiceCallbacks
101 * that the wallpaper has changed. The CREATE is triggered when there is no
102 * wallpaper set and is created for the first time. The CLOSE_WRITE is triggered
103 * everytime the wallpaper is changed.
104 */
Amith Yamasani37ce3a82012-02-06 12:04:42 -0800105 private class WallpaperObserver extends FileObserver {
Joe Onorato9bb8fd72009-07-28 18:24:51 -0700106
Amith Yamasani37ce3a82012-02-06 12:04:42 -0800107 final WallpaperData mWallpaper;
108 final File mWallpaperDir;
109 final File mWallpaperFile;
110
111 public WallpaperObserver(WallpaperData wallpaper) {
112 super(getWallpaperDir(wallpaper.userId).getAbsolutePath(),
113 CLOSE_WRITE | DELETE | DELETE_SELF);
114 mWallpaperDir = getWallpaperDir(wallpaper.userId);
115 mWallpaper = wallpaper;
116 mWallpaperFile = new File(mWallpaperDir, WALLPAPER);
117 }
118
119 @Override
120 public void onEvent(int event, String path) {
121 if (path == null) {
122 return;
123 }
124 synchronized (mLock) {
125 // changing the wallpaper means we'll need to back up the new one
126 long origId = Binder.clearCallingIdentity();
127 BackupManager bm = new BackupManager(mContext);
128 bm.dataChanged();
129 Binder.restoreCallingIdentity(origId);
130
131 File changedFile = new File(mWallpaperDir, path);
132 if (mWallpaperFile.equals(changedFile)) {
133 notifyCallbacksLocked(mWallpaper);
134 if (mWallpaper.wallpaperComponent == null || event != CLOSE_WRITE
135 || mWallpaper.imageWallpaperPending) {
136 if (event == CLOSE_WRITE) {
137 mWallpaper.imageWallpaperPending = false;
Joe Onorato9bb8fd72009-07-28 18:24:51 -0700138 }
Amith Yamasani37ce3a82012-02-06 12:04:42 -0800139 bindWallpaperComponentLocked(mWallpaper.imageWallpaperComponent, true,
Dianne Hackborn5dc5a002012-09-15 19:33:48 -0700140 false, mWallpaper, null);
Amith Yamasani37ce3a82012-02-06 12:04:42 -0800141 saveSettingsLocked(mWallpaper);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800142 }
143 }
Amith Yamasani37ce3a82012-02-06 12:04:42 -0800144 }
145 }
146 }
147
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700148 final Context mContext;
149 final IWindowManager mIWindowManager;
Amith Yamasani4e2820c2012-08-28 22:17:23 -0700150 final IPackageManager mIPackageManager;
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800151 final MyPackageMonitor mMonitor;
Amith Yamasani37ce3a82012-02-06 12:04:42 -0800152 WallpaperData mLastWallpaper;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800153
Amith Yamasani37ce3a82012-02-06 12:04:42 -0800154 SparseArray<WallpaperData> mWallpaperMap = new SparseArray<WallpaperData>();
Dianne Hackborn07213e62011-08-24 20:05:39 -0700155
Amith Yamasani37ce3a82012-02-06 12:04:42 -0800156 int mCurrentUserId;
Dianne Hackborn07213e62011-08-24 20:05:39 -0700157
Amith Yamasani37ce3a82012-02-06 12:04:42 -0800158 static class WallpaperData {
159
160 int userId;
161
162 File wallpaperFile;
163
164 /**
165 * Client is currently writing a new image wallpaper.
166 */
167 boolean imageWallpaperPending;
168
169 /**
170 * Resource name if using a picture from the wallpaper gallery
171 */
172 String name = "";
173
174 /**
175 * The component name of the currently set live wallpaper.
176 */
177 ComponentName wallpaperComponent;
178
179 /**
180 * The component name of the wallpaper that should be set next.
181 */
182 ComponentName nextWallpaperComponent;
183
184 /**
185 * Name of the component used to display bitmap wallpapers from either the gallery or
186 * built-in wallpapers.
187 */
188 ComponentName imageWallpaperComponent = new ComponentName("com.android.systemui",
189 "com.android.systemui.ImageWallpaper");
190
191 WallpaperConnection connection;
192 long lastDiedTime;
193 boolean wallpaperUpdating;
194 WallpaperObserver wallpaperObserver;
195
196 /**
197 * List of callbacks registered they should each be notified when the wallpaper is changed.
198 */
199 private RemoteCallbackList<IWallpaperManagerCallback> callbacks
200 = new RemoteCallbackList<IWallpaperManagerCallback>();
201
202 int width = -1;
203 int height = -1;
204
205 WallpaperData(int userId) {
206 this.userId = userId;
207 wallpaperFile = new File(getWallpaperDir(userId), WALLPAPER);
208 }
209 }
210
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700211 class WallpaperConnection extends IWallpaperConnection.Stub
212 implements ServiceConnection {
Dianne Hackborneb034652009-09-07 00:49:58 -0700213 final WallpaperInfo mInfo;
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700214 final Binder mToken = new Binder();
215 IWallpaperService mService;
216 IWallpaperEngine mEngine;
Amith Yamasani37ce3a82012-02-06 12:04:42 -0800217 WallpaperData mWallpaper;
Dianne Hackborn5dc5a002012-09-15 19:33:48 -0700218 IRemoteCallback mReply;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800219
Amith Yamasani37ce3a82012-02-06 12:04:42 -0800220 public WallpaperConnection(WallpaperInfo info, WallpaperData wallpaper) {
Dianne Hackborneb034652009-09-07 00:49:58 -0700221 mInfo = info;
Amith Yamasani37ce3a82012-02-06 12:04:42 -0800222 mWallpaper = wallpaper;
Dianne Hackborneb034652009-09-07 00:49:58 -0700223 }
Dianne Hackborn5dc5a002012-09-15 19:33:48 -0700224
225 @Override
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700226 public void onServiceConnected(ComponentName name, IBinder service) {
227 synchronized (mLock) {
Amith Yamasani37ce3a82012-02-06 12:04:42 -0800228 if (mWallpaper.connection == this) {
229 mWallpaper.lastDiedTime = SystemClock.uptimeMillis();
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700230 mService = IWallpaperService.Stub.asInterface(service);
Amith Yamasani37ce3a82012-02-06 12:04:42 -0800231 attachServiceLocked(this, mWallpaper);
Dianne Hackborneb034652009-09-07 00:49:58 -0700232 // XXX should probably do saveSettingsLocked() later
233 // when we have an engine, but I'm not sure about
234 // locking there and anyway we always need to be able to
235 // recover if there is something wrong.
Amith Yamasani37ce3a82012-02-06 12:04:42 -0800236 saveSettingsLocked(mWallpaper);
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700237 }
238 }
239 }
240
Dianne Hackborn5dc5a002012-09-15 19:33:48 -0700241 @Override
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700242 public void onServiceDisconnected(ComponentName name) {
243 synchronized (mLock) {
244 mService = null;
245 mEngine = null;
Amith Yamasani37ce3a82012-02-06 12:04:42 -0800246 if (mWallpaper.connection == this) {
247 Slog.w(TAG, "Wallpaper service gone: " + mWallpaper.wallpaperComponent);
248 if (!mWallpaper.wallpaperUpdating
249 && (mWallpaper.lastDiedTime + MIN_WALLPAPER_CRASH_TIME)
250 > SystemClock.uptimeMillis()
251 && mWallpaper.userId == mCurrentUserId) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800252 Slog.w(TAG, "Reverting to built-in wallpaper!");
Dianne Hackborn5dc5a002012-09-15 19:33:48 -0700253 clearWallpaperLocked(true, mWallpaper.userId, null);
Dianne Hackborn0cd48872009-08-13 18:51:59 -0700254 }
255 }
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700256 }
257 }
Amith Yamasani37ce3a82012-02-06 12:04:42 -0800258
Dianne Hackborn5dc5a002012-09-15 19:33:48 -0700259 @Override
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700260 public void attachEngine(IWallpaperEngine engine) {
Dianne Hackborn5dc5a002012-09-15 19:33:48 -0700261 synchronized (mLock) {
262 mEngine = engine;
263 }
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700264 }
Amith Yamasani37ce3a82012-02-06 12:04:42 -0800265
Dianne Hackborn5dc5a002012-09-15 19:33:48 -0700266 @Override
267 public void engineShown(IWallpaperEngine engine) {
268 synchronized (mLock) {
269 if (mReply != null) {
270 long ident = Binder.clearCallingIdentity();
271 try {
272 mReply.sendResult(null);
273 } catch (RemoteException e) {
274 Binder.restoreCallingIdentity(ident);
275 }
276 mReply = null;
277 }
278 }
279 }
280
281 @Override
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700282 public ParcelFileDescriptor setWallpaper(String name) {
283 synchronized (mLock) {
Amith Yamasani37ce3a82012-02-06 12:04:42 -0800284 if (mWallpaper.connection == this) {
285 return updateWallpaperBitmapLocked(name, mWallpaper);
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700286 }
287 return null;
288 }
289 }
290 }
Amith Yamasani37ce3a82012-02-06 12:04:42 -0800291
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800292 class MyPackageMonitor extends PackageMonitor {
293 @Override
294 public void onPackageUpdateFinished(String packageName, int uid) {
295 synchronized (mLock) {
Amith Yamasani37ce3a82012-02-06 12:04:42 -0800296 for (int i = 0; i < mWallpaperMap.size(); i++) {
297 WallpaperData wallpaper = mWallpaperMap.valueAt(i);
298 if (wallpaper.wallpaperComponent != null
299 && wallpaper.wallpaperComponent.getPackageName().equals(packageName)) {
300 wallpaper.wallpaperUpdating = false;
301 ComponentName comp = wallpaper.wallpaperComponent;
302 clearWallpaperComponentLocked(wallpaper);
303 // Do this only for the current user's wallpaper
304 if (wallpaper.userId == mCurrentUserId
Dianne Hackborn5dc5a002012-09-15 19:33:48 -0700305 && !bindWallpaperComponentLocked(comp, false, false,
306 wallpaper, null)) {
Amith Yamasani37ce3a82012-02-06 12:04:42 -0800307 Slog.w(TAG, "Wallpaper no longer available; reverting to default");
Dianne Hackborn5dc5a002012-09-15 19:33:48 -0700308 clearWallpaperLocked(false, wallpaper.userId, null);
Amith Yamasani37ce3a82012-02-06 12:04:42 -0800309 }
Dianne Hackborn80b902f2011-09-15 15:15:27 -0700310 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800311 }
312 }
313 }
314
315 @Override
Dianne Hackborn80b902f2011-09-15 15:15:27 -0700316 public void onPackageModified(String packageName) {
317 synchronized (mLock) {
Amith Yamasani37ce3a82012-02-06 12:04:42 -0800318 for (int i = 0; i < mWallpaperMap.size(); i++) {
319 WallpaperData wallpaper = mWallpaperMap.valueAt(i);
320 if (wallpaper.wallpaperComponent == null
321 || !wallpaper.wallpaperComponent.getPackageName().equals(packageName)) {
322 continue;
323 }
Dianne Hackbornd0d75032012-04-19 23:12:09 -0700324 doPackagesChangedLocked(true, wallpaper);
Dianne Hackborn80b902f2011-09-15 15:15:27 -0700325 }
326 }
Dianne Hackborn80b902f2011-09-15 15:15:27 -0700327 }
328
329 @Override
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800330 public void onPackageUpdateStarted(String packageName, int uid) {
331 synchronized (mLock) {
Amith Yamasani37ce3a82012-02-06 12:04:42 -0800332 for (int i = 0; i < mWallpaperMap.size(); i++) {
333 WallpaperData wallpaper = mWallpaperMap.valueAt(i);
334 if (wallpaper.wallpaperComponent != null
335 && wallpaper.wallpaperComponent.getPackageName().equals(packageName)) {
336 wallpaper.wallpaperUpdating = true;
337 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800338 }
339 }
340 }
341
342 @Override
343 public boolean onHandleForceStop(Intent intent, String[] packages, int uid, boolean doit) {
Dianne Hackbornd0d75032012-04-19 23:12:09 -0700344 synchronized (mLock) {
345 boolean changed = false;
346 for (int i = 0; i < mWallpaperMap.size(); i++) {
347 WallpaperData wallpaper = mWallpaperMap.valueAt(i);
348 boolean res = doPackagesChangedLocked(doit, wallpaper);
349 changed |= res;
350 }
351 return changed;
Amith Yamasani37ce3a82012-02-06 12:04:42 -0800352 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800353 }
354
355 @Override
356 public void onSomePackagesChanged() {
Dianne Hackbornd0d75032012-04-19 23:12:09 -0700357 synchronized (mLock) {
358 for (int i = 0; i < mWallpaperMap.size(); i++) {
359 WallpaperData wallpaper = mWallpaperMap.valueAt(i);
360 doPackagesChangedLocked(true, wallpaper);
361 }
Amith Yamasani37ce3a82012-02-06 12:04:42 -0800362 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800363 }
Amith Yamasani37ce3a82012-02-06 12:04:42 -0800364
Dianne Hackbornd0d75032012-04-19 23:12:09 -0700365 boolean doPackagesChangedLocked(boolean doit, WallpaperData wallpaper) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800366 boolean changed = false;
Dianne Hackbornd0d75032012-04-19 23:12:09 -0700367 if (wallpaper.wallpaperComponent != null) {
368 int change = isPackageDisappearing(wallpaper.wallpaperComponent
369 .getPackageName());
370 if (change == PACKAGE_PERMANENT_CHANGE
371 || change == PACKAGE_TEMPORARY_CHANGE) {
372 changed = true;
373 if (doit) {
374 Slog.w(TAG, "Wallpaper uninstalled, removing: "
Amith Yamasani37ce3a82012-02-06 12:04:42 -0800375 + wallpaper.wallpaperComponent);
Dianne Hackborn5dc5a002012-09-15 19:33:48 -0700376 clearWallpaperLocked(false, wallpaper.userId, null);
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800377 }
378 }
Dianne Hackbornd0d75032012-04-19 23:12:09 -0700379 }
380 if (wallpaper.nextWallpaperComponent != null) {
381 int change = isPackageDisappearing(wallpaper.nextWallpaperComponent
382 .getPackageName());
383 if (change == PACKAGE_PERMANENT_CHANGE
384 || change == PACKAGE_TEMPORARY_CHANGE) {
385 wallpaper.nextWallpaperComponent = null;
386 }
387 }
388 if (wallpaper.wallpaperComponent != null
389 && isPackageModified(wallpaper.wallpaperComponent.getPackageName())) {
390 try {
391 mContext.getPackageManager().getServiceInfo(
392 wallpaper.wallpaperComponent, 0);
393 } catch (NameNotFoundException e) {
394 Slog.w(TAG, "Wallpaper component gone, removing: "
395 + wallpaper.wallpaperComponent);
Dianne Hackborn5dc5a002012-09-15 19:33:48 -0700396 clearWallpaperLocked(false, wallpaper.userId, null);
Dianne Hackbornd0d75032012-04-19 23:12:09 -0700397 }
398 }
399 if (wallpaper.nextWallpaperComponent != null
400 && isPackageModified(wallpaper.nextWallpaperComponent.getPackageName())) {
401 try {
402 mContext.getPackageManager().getServiceInfo(
403 wallpaper.nextWallpaperComponent, 0);
404 } catch (NameNotFoundException e) {
405 wallpaper.nextWallpaperComponent = null;
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800406 }
407 }
408 return changed;
409 }
410 }
411
Dianne Hackborn8cc6a502009-08-05 21:29:42 -0700412 public WallpaperManagerService(Context context) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800413 if (DEBUG) Slog.v(TAG, "WallpaperService startup");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800414 mContext = context;
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700415 mIWindowManager = IWindowManager.Stub.asInterface(
416 ServiceManager.getService(Context.WINDOW_SERVICE));
Amith Yamasani4e2820c2012-08-28 22:17:23 -0700417 mIPackageManager = AppGlobals.getPackageManager();
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800418 mMonitor = new MyPackageMonitor();
Dianne Hackbornd0d75032012-04-19 23:12:09 -0700419 mMonitor.register(context, null, true);
Amith Yamasani61f57372012-08-31 12:12:28 -0700420 getWallpaperDir(UserHandle.USER_OWNER).mkdirs();
421 loadSettingsLocked(UserHandle.USER_OWNER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800422 }
423
Amith Yamasani37ce3a82012-02-06 12:04:42 -0800424 private static File getWallpaperDir(int userId) {
Amith Yamasani61f57372012-08-31 12:12:28 -0700425 return Environment.getUserSystemDirectory(userId);
Amith Yamasani37ce3a82012-02-06 12:04:42 -0800426 }
427
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800428 @Override
429 protected void finalize() throws Throwable {
430 super.finalize();
Amith Yamasani37ce3a82012-02-06 12:04:42 -0800431 for (int i = 0; i < mWallpaperMap.size(); i++) {
432 WallpaperData wallpaper = mWallpaperMap.valueAt(i);
433 wallpaper.wallpaperObserver.stopWatching();
434 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800435 }
Amith Yamasani13593602012-03-22 16:16:17 -0700436
Dianne Hackbornf21adf62009-08-13 10:20:21 -0700437 public void systemReady() {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800438 if (DEBUG) Slog.v(TAG, "systemReady");
Amith Yamasani61f57372012-08-31 12:12:28 -0700439 WallpaperData wallpaper = mWallpaperMap.get(UserHandle.USER_OWNER);
Dianne Hackborn5dc5a002012-09-15 19:33:48 -0700440 switchWallpaper(wallpaper, null);
Amith Yamasani37ce3a82012-02-06 12:04:42 -0800441 wallpaper.wallpaperObserver = new WallpaperObserver(wallpaper);
442 wallpaper.wallpaperObserver.startWatching();
Amith Yamasani37ce3a82012-02-06 12:04:42 -0800443
Amith Yamasani13593602012-03-22 16:16:17 -0700444 IntentFilter userFilter = new IntentFilter();
Amith Yamasani13593602012-03-22 16:16:17 -0700445 userFilter.addAction(Intent.ACTION_USER_REMOVED);
446 mContext.registerReceiver(new BroadcastReceiver() {
Amith Yamasani37ce3a82012-02-06 12:04:42 -0800447 @Override
Amith Yamasani13593602012-03-22 16:16:17 -0700448 public void onReceive(Context context, Intent intent) {
449 String action = intent.getAction();
Dianne Hackborn5dc5a002012-09-15 19:33:48 -0700450 if (Intent.ACTION_USER_REMOVED.equals(action)) {
Amith Yamasani2a003292012-08-14 18:25:45 -0700451 removeUser(intent.getIntExtra(Intent.EXTRA_USER_HANDLE, 0));
Amith Yamasani13593602012-03-22 16:16:17 -0700452 }
Amith Yamasani37ce3a82012-02-06 12:04:42 -0800453 }
Amith Yamasani13593602012-03-22 16:16:17 -0700454 }, userFilter);
Dianne Hackborn5dc5a002012-09-15 19:33:48 -0700455 try {
456 ActivityManagerNative.getDefault().registerUserSwitchObserver(
457 new IUserSwitchObserver.Stub() {
458 @Override
459 public void onUserSwitching(int newUserId, IRemoteCallback reply) {
460 switchUser(newUserId, reply);
461 }
462
463 @Override
464 public void onUserSwitchComplete(int newUserId) throws RemoteException {
465 }
466 });
467 } catch (RemoteException e) {
468 // TODO Auto-generated catch block
469 e.printStackTrace();
470 }
Amith Yamasani37ce3a82012-02-06 12:04:42 -0800471 }
472
473 String getName() {
Dianne Hackborn5dc5a002012-09-15 19:33:48 -0700474 synchronized (mLock) {
475 return mWallpaperMap.get(0).name;
476 }
Amith Yamasani37ce3a82012-02-06 12:04:42 -0800477 }
478
Amith Yamasani13593602012-03-22 16:16:17 -0700479 void removeUser(int userId) {
480 synchronized (mLock) {
481 WallpaperData wallpaper = mWallpaperMap.get(userId);
482 if (wallpaper != null) {
483 wallpaper.wallpaperObserver.stopWatching();
484 mWallpaperMap.remove(userId);
485 }
486 File wallpaperFile = new File(getWallpaperDir(userId), WALLPAPER);
487 wallpaperFile.delete();
488 File wallpaperInfoFile = new File(getWallpaperDir(userId), WALLPAPER_INFO);
489 wallpaperInfoFile.delete();
490 }
491 }
492
Dianne Hackborn5dc5a002012-09-15 19:33:48 -0700493 void switchUser(int userId, IRemoteCallback reply) {
Amith Yamasani37ce3a82012-02-06 12:04:42 -0800494 synchronized (mLock) {
495 mCurrentUserId = userId;
496 WallpaperData wallpaper = mWallpaperMap.get(userId);
497 if (wallpaper == null) {
498 wallpaper = new WallpaperData(userId);
499 mWallpaperMap.put(userId, wallpaper);
500 loadSettingsLocked(userId);
501 wallpaper.wallpaperObserver = new WallpaperObserver(wallpaper);
502 wallpaper.wallpaperObserver.startWatching();
503 }
Dianne Hackborn5dc5a002012-09-15 19:33:48 -0700504 switchWallpaper(wallpaper, reply);
Amith Yamasani37ce3a82012-02-06 12:04:42 -0800505 }
506 }
507
Dianne Hackborn5dc5a002012-09-15 19:33:48 -0700508 void switchWallpaper(WallpaperData wallpaper, IRemoteCallback reply) {
Dianne Hackbornf21adf62009-08-13 10:20:21 -0700509 synchronized (mLock) {
Dianne Hackborn80b902f2011-09-15 15:15:27 -0700510 RuntimeException e = null;
Dianne Hackbornf21adf62009-08-13 10:20:21 -0700511 try {
Amith Yamasani37ce3a82012-02-06 12:04:42 -0800512 ComponentName cname = wallpaper.wallpaperComponent != null ?
513 wallpaper.wallpaperComponent : wallpaper.nextWallpaperComponent;
Dianne Hackborn5dc5a002012-09-15 19:33:48 -0700514 if (bindWallpaperComponentLocked(cname, true, false, wallpaper, reply)) {
Dianne Hackborn80b902f2011-09-15 15:15:27 -0700515 return;
Dianne Hackbornf21adf62009-08-13 10:20:21 -0700516 }
Dianne Hackborn80b902f2011-09-15 15:15:27 -0700517 } catch (RuntimeException e1) {
518 e = e1;
Dianne Hackbornf21adf62009-08-13 10:20:21 -0700519 }
Dianne Hackborn80b902f2011-09-15 15:15:27 -0700520 Slog.w(TAG, "Failure starting previous wallpaper", e);
Dianne Hackborn5dc5a002012-09-15 19:33:48 -0700521 clearWallpaperLocked(false, wallpaper.userId, reply);
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800522 }
523 }
524
Amith Yamasani37ce3a82012-02-06 12:04:42 -0800525 public void clearWallpaper() {
526 if (DEBUG) Slog.v(TAG, "clearWallpaper");
527 synchronized (mLock) {
Dianne Hackborn5dc5a002012-09-15 19:33:48 -0700528 clearWallpaperLocked(false, UserHandle.getCallingUserId(), null);
Amith Yamasani37ce3a82012-02-06 12:04:42 -0800529 }
530 }
531
Dianne Hackborn5dc5a002012-09-15 19:33:48 -0700532 void clearWallpaperLocked(boolean defaultFailed, int userId, IRemoteCallback reply) {
Amith Yamasani37ce3a82012-02-06 12:04:42 -0800533 WallpaperData wallpaper = mWallpaperMap.get(userId);
534 File f = new File(getWallpaperDir(userId), WALLPAPER);
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800535 if (f.exists()) {
536 f.delete();
537 }
538 final long ident = Binder.clearCallingIdentity();
Dianne Hackborn80b902f2011-09-15 15:15:27 -0700539 RuntimeException e = null;
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800540 try {
Amith Yamasani37ce3a82012-02-06 12:04:42 -0800541 wallpaper.imageWallpaperPending = false;
542 if (userId != mCurrentUserId) return;
Dianne Hackborn80b902f2011-09-15 15:15:27 -0700543 if (bindWallpaperComponentLocked(defaultFailed
Amith Yamasani37ce3a82012-02-06 12:04:42 -0800544 ? wallpaper.imageWallpaperComponent
Dianne Hackborn5dc5a002012-09-15 19:33:48 -0700545 : null, true, false, wallpaper, reply)) {
Dianne Hackborn80b902f2011-09-15 15:15:27 -0700546 return;
547 }
548 } catch (IllegalArgumentException e1) {
549 e = e1;
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800550 } finally {
551 Binder.restoreCallingIdentity(ident);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800552 }
Dianne Hackborn80b902f2011-09-15 15:15:27 -0700553
554 // This can happen if the default wallpaper component doesn't
555 // exist. This should be a system configuration problem, but
556 // let's not let it crash the system and just live with no
557 // wallpaper.
558 Slog.e(TAG, "Default wallpaper component not found!", e);
Amith Yamasani37ce3a82012-02-06 12:04:42 -0800559 clearWallpaperComponentLocked(wallpaper);
Dianne Hackborn5dc5a002012-09-15 19:33:48 -0700560 if (reply != null) {
561 try {
562 reply.sendResult(null);
563 } catch (RemoteException e1) {
564 }
565 }
566 }
567
568 public boolean hasNamedWallpaper(String name) {
569 synchronized (mLock) {
570 for (int i=0; i<mWallpaperMap.size(); i++) {
571 WallpaperData wd = mWallpaperMap.valueAt(i);
572 if (name.equals(wd.name)) {
573 return true;
574 }
575 }
576 }
577 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800578 }
579
580 public void setDimensionHints(int width, int height) throws RemoteException {
581 checkPermission(android.Manifest.permission.SET_WALLPAPER_HINTS);
Joe Onorato9bb8fd72009-07-28 18:24:51 -0700582 synchronized (mLock) {
Dianne Hackborn5dc5a002012-09-15 19:33:48 -0700583 int userId = UserHandle.getCallingUserId();
584 WallpaperData wallpaper = mWallpaperMap.get(userId);
585 if (wallpaper == null) {
586 throw new IllegalStateException("Wallpaper not yet initialized for user " + userId);
587 }
588 if (width <= 0 || height <= 0) {
589 throw new IllegalArgumentException("width and height must be > 0");
590 }
591
Amith Yamasani37ce3a82012-02-06 12:04:42 -0800592 if (width != wallpaper.width || height != wallpaper.height) {
593 wallpaper.width = width;
594 wallpaper.height = height;
595 saveSettingsLocked(wallpaper);
596 if (mCurrentUserId != userId) return; // Don't change the properties now
597 if (wallpaper.connection != null) {
598 if (wallpaper.connection.mEngine != null) {
Dianne Hackborn284ac932009-08-28 10:34:25 -0700599 try {
Amith Yamasani37ce3a82012-02-06 12:04:42 -0800600 wallpaper.connection.mEngine.setDesiredSize(
Dianne Hackborn284ac932009-08-28 10:34:25 -0700601 width, height);
602 } catch (RemoteException e) {
603 }
Amith Yamasani37ce3a82012-02-06 12:04:42 -0800604 notifyCallbacksLocked(wallpaper);
Dianne Hackborn284ac932009-08-28 10:34:25 -0700605 }
606 }
Joe Onorato9bb8fd72009-07-28 18:24:51 -0700607 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800608 }
609 }
610
611 public int getWidthHint() throws RemoteException {
Joe Onorato9bb8fd72009-07-28 18:24:51 -0700612 synchronized (mLock) {
Dianne Hackbornf02b60a2012-08-16 10:48:27 -0700613 WallpaperData wallpaper = mWallpaperMap.get(UserHandle.getCallingUserId());
Amith Yamasani37ce3a82012-02-06 12:04:42 -0800614 return wallpaper.width;
Joe Onorato9bb8fd72009-07-28 18:24:51 -0700615 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800616 }
617
618 public int getHeightHint() throws RemoteException {
Joe Onorato9bb8fd72009-07-28 18:24:51 -0700619 synchronized (mLock) {
Dianne Hackbornf02b60a2012-08-16 10:48:27 -0700620 WallpaperData wallpaper = mWallpaperMap.get(UserHandle.getCallingUserId());
Amith Yamasani37ce3a82012-02-06 12:04:42 -0800621 return wallpaper.height;
Joe Onorato9bb8fd72009-07-28 18:24:51 -0700622 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800623 }
624
Dianne Hackborn284ac932009-08-28 10:34:25 -0700625 public ParcelFileDescriptor getWallpaper(IWallpaperManagerCallback cb,
626 Bundle outParams) {
Joe Onorato9bb8fd72009-07-28 18:24:51 -0700627 synchronized (mLock) {
Amith Yamasani37ce3a82012-02-06 12:04:42 -0800628 // This returns the current user's wallpaper, if called by a system service. Else it
629 // returns the wallpaper for the calling user.
630 int callingUid = Binder.getCallingUid();
631 int wallpaperUserId = 0;
632 if (callingUid == android.os.Process.SYSTEM_UID) {
633 wallpaperUserId = mCurrentUserId;
634 } else {
Dianne Hackbornf02b60a2012-08-16 10:48:27 -0700635 wallpaperUserId = UserHandle.getUserId(callingUid);
Amith Yamasani37ce3a82012-02-06 12:04:42 -0800636 }
637 WallpaperData wallpaper = mWallpaperMap.get(wallpaperUserId);
Joe Onorato9bb8fd72009-07-28 18:24:51 -0700638 try {
Dianne Hackborn284ac932009-08-28 10:34:25 -0700639 if (outParams != null) {
Amith Yamasani37ce3a82012-02-06 12:04:42 -0800640 outParams.putInt("width", wallpaper.width);
641 outParams.putInt("height", wallpaper.height);
Dianne Hackborn284ac932009-08-28 10:34:25 -0700642 }
Amith Yamasani37ce3a82012-02-06 12:04:42 -0800643 wallpaper.callbacks.register(cb);
644 File f = new File(getWallpaperDir(wallpaperUserId), WALLPAPER);
Joe Onorato9bb8fd72009-07-28 18:24:51 -0700645 if (!f.exists()) {
646 return null;
647 }
648 return ParcelFileDescriptor.open(f, MODE_READ_ONLY);
649 } catch (FileNotFoundException e) {
650 /* Shouldn't happen as we check to see if the file exists */
Joe Onorato8a9b2202010-02-26 18:56:32 -0800651 Slog.w(TAG, "Error getting wallpaper", e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800652 }
Joe Onorato9bb8fd72009-07-28 18:24:51 -0700653 return null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800654 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800655 }
656
Dianne Hackborneb034652009-09-07 00:49:58 -0700657 public WallpaperInfo getWallpaperInfo() {
Dianne Hackbornf02b60a2012-08-16 10:48:27 -0700658 int userId = UserHandle.getCallingUserId();
Dianne Hackborneb034652009-09-07 00:49:58 -0700659 synchronized (mLock) {
Amith Yamasani37ce3a82012-02-06 12:04:42 -0800660 WallpaperData wallpaper = mWallpaperMap.get(userId);
661 if (wallpaper.connection != null) {
662 return wallpaper.connection.mInfo;
Dianne Hackborneb034652009-09-07 00:49:58 -0700663 }
664 return null;
665 }
666 }
Amith Yamasani37ce3a82012-02-06 12:04:42 -0800667
Joe Onorato9bb8fd72009-07-28 18:24:51 -0700668 public ParcelFileDescriptor setWallpaper(String name) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800669 checkPermission(android.Manifest.permission.SET_WALLPAPER);
Joe Onorato9bb8fd72009-07-28 18:24:51 -0700670 synchronized (mLock) {
Dianne Hackborn5dc5a002012-09-15 19:33:48 -0700671 if (DEBUG) Slog.v(TAG, "setWallpaper");
672 int userId = UserHandle.getCallingUserId();
673 WallpaperData wallpaper = mWallpaperMap.get(userId);
674 if (wallpaper == null) {
675 throw new IllegalStateException("Wallpaper not yet initialized for user " + userId);
676 }
Dianne Hackborn0cd48872009-08-13 18:51:59 -0700677 final long ident = Binder.clearCallingIdentity();
678 try {
Amith Yamasani37ce3a82012-02-06 12:04:42 -0800679 ParcelFileDescriptor pfd = updateWallpaperBitmapLocked(name, wallpaper);
Dianne Hackborn0cd48872009-08-13 18:51:59 -0700680 if (pfd != null) {
Amith Yamasani37ce3a82012-02-06 12:04:42 -0800681 wallpaper.imageWallpaperPending = true;
Dianne Hackborn0cd48872009-08-13 18:51:59 -0700682 }
683 return pfd;
684 } finally {
685 Binder.restoreCallingIdentity(ident);
Joe Onorato9bb8fd72009-07-28 18:24:51 -0700686 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800687 }
688 }
689
Amith Yamasani37ce3a82012-02-06 12:04:42 -0800690 ParcelFileDescriptor updateWallpaperBitmapLocked(String name, WallpaperData wallpaper) {
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700691 if (name == null) name = "";
692 try {
Amith Yamasani37ce3a82012-02-06 12:04:42 -0800693 File dir = getWallpaperDir(wallpaper.userId);
694 if (!dir.exists()) {
695 dir.mkdir();
Dianne Hackbornebac48c2011-11-29 18:01:50 -0800696 FileUtils.setPermissions(
Amith Yamasani37ce3a82012-02-06 12:04:42 -0800697 dir.getPath(),
Dianne Hackbornebac48c2011-11-29 18:01:50 -0800698 FileUtils.S_IRWXU|FileUtils.S_IRWXG|FileUtils.S_IXOTH,
699 -1, -1);
700 }
rpcraig554cb0c2012-07-05 06:41:43 -0400701 File file = new File(dir, WALLPAPER);
702 ParcelFileDescriptor fd = ParcelFileDescriptor.open(file,
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700703 MODE_CREATE|MODE_READ_WRITE);
rpcraig554cb0c2012-07-05 06:41:43 -0400704 if (!SELinux.restorecon(file)) {
705 return null;
706 }
Amith Yamasani37ce3a82012-02-06 12:04:42 -0800707 wallpaper.name = name;
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700708 return fd;
709 } catch (FileNotFoundException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800710 Slog.w(TAG, "Error setting wallpaper", e);
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700711 }
712 return null;
713 }
714
715 public void setWallpaperComponent(ComponentName name) {
716 checkPermission(android.Manifest.permission.SET_WALLPAPER_COMPONENT);
717 synchronized (mLock) {
Dianne Hackborn5dc5a002012-09-15 19:33:48 -0700718 if (DEBUG) Slog.v(TAG, "setWallpaperComponent name=" + name);
719 int userId = UserHandle.getCallingUserId();
720 WallpaperData wallpaper = mWallpaperMap.get(userId);
721 if (wallpaper == null) {
722 throw new IllegalStateException("Wallpaper not yet initialized for user " + userId);
723 }
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700724 final long ident = Binder.clearCallingIdentity();
725 try {
Amith Yamasani37ce3a82012-02-06 12:04:42 -0800726 wallpaper.imageWallpaperPending = false;
Dianne Hackborn5dc5a002012-09-15 19:33:48 -0700727 bindWallpaperComponentLocked(name, false, true, wallpaper, null);
Dianne Hackbornf21adf62009-08-13 10:20:21 -0700728 } finally {
729 Binder.restoreCallingIdentity(ident);
730 }
731 }
732 }
733
Amith Yamasani37ce3a82012-02-06 12:04:42 -0800734 boolean bindWallpaperComponentLocked(ComponentName componentName, boolean force,
Dianne Hackborn5dc5a002012-09-15 19:33:48 -0700735 boolean fromUser, WallpaperData wallpaper, IRemoteCallback reply) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800736 if (DEBUG) Slog.v(TAG, "bindWallpaperComponentLocked: componentName=" + componentName);
Dianne Hackbornf21adf62009-08-13 10:20:21 -0700737 // Has the component changed?
Dianne Hackborn9ea31632011-08-05 14:43:50 -0700738 if (!force) {
Amith Yamasani37ce3a82012-02-06 12:04:42 -0800739 if (wallpaper.connection != null) {
740 if (wallpaper.wallpaperComponent == null) {
Dianne Hackborn9ea31632011-08-05 14:43:50 -0700741 if (componentName == null) {
742 if (DEBUG) Slog.v(TAG, "bindWallpaperComponentLocked: still using default");
743 // Still using default wallpaper.
Dianne Hackborn80b902f2011-09-15 15:15:27 -0700744 return true;
Dianne Hackborn9ea31632011-08-05 14:43:50 -0700745 }
Amith Yamasani37ce3a82012-02-06 12:04:42 -0800746 } else if (wallpaper.wallpaperComponent.equals(componentName)) {
Dianne Hackborn9ea31632011-08-05 14:43:50 -0700747 // Changing to same wallpaper.
748 if (DEBUG) Slog.v(TAG, "same wallpaper");
Dianne Hackborn80b902f2011-09-15 15:15:27 -0700749 return true;
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700750 }
Dianne Hackbornf21adf62009-08-13 10:20:21 -0700751 }
752 }
753
754 try {
Mike Clerona428b2c2009-11-15 22:53:08 -0800755 if (componentName == null) {
Mike Cleron322b6ee2009-11-12 07:45:47 -0800756 String defaultComponent =
757 mContext.getString(com.android.internal.R.string.default_wallpaper_component);
Mike Clerona428b2c2009-11-15 22:53:08 -0800758 if (defaultComponent != null) {
Mike Cleron322b6ee2009-11-12 07:45:47 -0800759 // See if there is a default wallpaper component specified
Mike Clerona428b2c2009-11-15 22:53:08 -0800760 componentName = ComponentName.unflattenFromString(defaultComponent);
Joe Onorato8a9b2202010-02-26 18:56:32 -0800761 if (DEBUG) Slog.v(TAG, "Use default component wallpaper:" + componentName);
Mike Cleron322b6ee2009-11-12 07:45:47 -0800762 }
Mike Clerona428b2c2009-11-15 22:53:08 -0800763 if (componentName == null) {
Mike Cleron322b6ee2009-11-12 07:45:47 -0800764 // Fall back to static image wallpaper
Amith Yamasani37ce3a82012-02-06 12:04:42 -0800765 componentName = wallpaper.imageWallpaperComponent;
Mike Cleron322b6ee2009-11-12 07:45:47 -0800766 //clearWallpaperComponentLocked();
767 //return;
Joe Onorato8a9b2202010-02-26 18:56:32 -0800768 if (DEBUG) Slog.v(TAG, "Using image wallpaper");
Mike Cleron322b6ee2009-11-12 07:45:47 -0800769 }
Dianne Hackbornf21adf62009-08-13 10:20:21 -0700770 }
Amith Yamasani4e2820c2012-08-28 22:17:23 -0700771 int serviceUserId = wallpaper.userId;
772 ServiceInfo si = mIPackageManager.getServiceInfo(componentName,
773 PackageManager.GET_META_DATA | PackageManager.GET_PERMISSIONS, serviceUserId);
Dianne Hackbornf21adf62009-08-13 10:20:21 -0700774 if (!android.Manifest.permission.BIND_WALLPAPER.equals(si.permission)) {
Dianne Hackborn80b902f2011-09-15 15:15:27 -0700775 String msg = "Selected service does not require "
Dianne Hackbornf21adf62009-08-13 10:20:21 -0700776 + android.Manifest.permission.BIND_WALLPAPER
Dianne Hackborn80b902f2011-09-15 15:15:27 -0700777 + ": " + componentName;
778 if (fromUser) {
779 throw new SecurityException(msg);
780 }
781 Slog.w(TAG, msg);
782 return false;
Dianne Hackbornf21adf62009-08-13 10:20:21 -0700783 }
784
Dianne Hackborneb034652009-09-07 00:49:58 -0700785 WallpaperInfo wi = null;
786
Dianne Hackbornf21adf62009-08-13 10:20:21 -0700787 Intent intent = new Intent(WallpaperService.SERVICE_INTERFACE);
Amith Yamasani37ce3a82012-02-06 12:04:42 -0800788 if (componentName != null && !componentName.equals(wallpaper.imageWallpaperComponent)) {
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700789 // Make sure the selected service is actually a wallpaper service.
Amith Yamasani4e2820c2012-08-28 22:17:23 -0700790 List<ResolveInfo> ris =
791 mIPackageManager.queryIntentServices(intent,
792 intent.resolveTypeIfNeeded(mContext.getContentResolver()),
793 PackageManager.GET_META_DATA, serviceUserId);
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700794 for (int i=0; i<ris.size(); i++) {
795 ServiceInfo rsi = ris.get(i).serviceInfo;
796 if (rsi.name.equals(si.name) &&
797 rsi.packageName.equals(si.packageName)) {
Dianne Hackborneb034652009-09-07 00:49:58 -0700798 try {
799 wi = new WallpaperInfo(mContext, ris.get(i));
800 } catch (XmlPullParserException e) {
Dianne Hackborn80b902f2011-09-15 15:15:27 -0700801 if (fromUser) {
802 throw new IllegalArgumentException(e);
803 }
804 Slog.w(TAG, e);
805 return false;
Dianne Hackborneb034652009-09-07 00:49:58 -0700806 } catch (IOException e) {
Dianne Hackborn80b902f2011-09-15 15:15:27 -0700807 if (fromUser) {
808 throw new IllegalArgumentException(e);
809 }
810 Slog.w(TAG, e);
811 return false;
Dianne Hackborneb034652009-09-07 00:49:58 -0700812 }
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700813 break;
814 }
815 }
Dianne Hackborneb034652009-09-07 00:49:58 -0700816 if (wi == null) {
Dianne Hackborn80b902f2011-09-15 15:15:27 -0700817 String msg = "Selected service is not a wallpaper: "
818 + componentName;
819 if (fromUser) {
820 throw new SecurityException(msg);
821 }
822 Slog.w(TAG, msg);
823 return false;
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700824 }
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700825 }
Dianne Hackbornf21adf62009-08-13 10:20:21 -0700826
827 // Bind the service!
Joe Onorato8a9b2202010-02-26 18:56:32 -0800828 if (DEBUG) Slog.v(TAG, "Binding to:" + componentName);
Amith Yamasani37ce3a82012-02-06 12:04:42 -0800829 WallpaperConnection newConn = new WallpaperConnection(wi, wallpaper);
Mike Clerona428b2c2009-11-15 22:53:08 -0800830 intent.setComponent(componentName);
Dianne Hackborndd9b82c2009-09-03 00:18:47 -0700831 intent.putExtra(Intent.EXTRA_CLIENT_LABEL,
832 com.android.internal.R.string.wallpaper_binding_label);
Dianne Hackborn41203752012-08-31 14:05:51 -0700833 intent.putExtra(Intent.EXTRA_CLIENT_INTENT, PendingIntent.getActivityAsUser(
Dianne Hackborneb034652009-09-07 00:49:58 -0700834 mContext, 0,
835 Intent.createChooser(new Intent(Intent.ACTION_SET_WALLPAPER),
836 mContext.getText(com.android.internal.R.string.chooser_wallpaper)),
Dianne Hackborn41203752012-08-31 14:05:51 -0700837 0, null, new UserHandle(serviceUserId)));
Amith Yamasani37ce3a82012-02-06 12:04:42 -0800838 if (!mContext.bindService(intent, newConn, Context.BIND_AUTO_CREATE, serviceUserId)) {
Dianne Hackborn80b902f2011-09-15 15:15:27 -0700839 String msg = "Unable to bind service: "
840 + componentName;
841 if (fromUser) {
842 throw new IllegalArgumentException(msg);
843 }
844 Slog.w(TAG, msg);
845 return false;
Dianne Hackbornf21adf62009-08-13 10:20:21 -0700846 }
Amith Yamasani37ce3a82012-02-06 12:04:42 -0800847 if (wallpaper.userId == mCurrentUserId && mLastWallpaper != null) {
848 detachWallpaperLocked(mLastWallpaper);
849 }
850 wallpaper.wallpaperComponent = componentName;
851 wallpaper.connection = newConn;
852 wallpaper.lastDiedTime = SystemClock.uptimeMillis();
Dianne Hackborn5dc5a002012-09-15 19:33:48 -0700853 newConn.mReply = reply;
Dianne Hackbornf21adf62009-08-13 10:20:21 -0700854 try {
Amith Yamasani37ce3a82012-02-06 12:04:42 -0800855 if (wallpaper.userId == mCurrentUserId) {
856 if (DEBUG)
857 Slog.v(TAG, "Adding window token: " + newConn.mToken);
858 mIWindowManager.addWindowToken(newConn.mToken,
859 WindowManager.LayoutParams.TYPE_WALLPAPER);
860 mLastWallpaper = wallpaper;
861 }
Dianne Hackbornf21adf62009-08-13 10:20:21 -0700862 } catch (RemoteException e) {
863 }
Amith Yamasani4e2820c2012-08-28 22:17:23 -0700864 } catch (RemoteException e) {
865 String msg = "Remote exception for " + componentName + "\n" + e;
Dianne Hackborn80b902f2011-09-15 15:15:27 -0700866 if (fromUser) {
867 throw new IllegalArgumentException(msg);
868 }
869 Slog.w(TAG, msg);
870 return false;
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700871 }
Dianne Hackborn80b902f2011-09-15 15:15:27 -0700872 return true;
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700873 }
Amith Yamasani37ce3a82012-02-06 12:04:42 -0800874
875 void detachWallpaperLocked(WallpaperData wallpaper) {
876 if (wallpaper.connection != null) {
Dianne Hackborn5dc5a002012-09-15 19:33:48 -0700877 if (wallpaper.connection.mReply != null) {
878 try {
879 wallpaper.connection.mReply.sendResult(null);
880 } catch (RemoteException e) {
881 }
882 wallpaper.connection.mReply = null;
883 }
Amith Yamasani37ce3a82012-02-06 12:04:42 -0800884 if (wallpaper.connection.mEngine != null) {
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700885 try {
Amith Yamasani37ce3a82012-02-06 12:04:42 -0800886 wallpaper.connection.mEngine.destroy();
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700887 } catch (RemoteException e) {
888 }
889 }
Amith Yamasani37ce3a82012-02-06 12:04:42 -0800890 mContext.unbindService(wallpaper.connection);
Dianne Hackborne9e9bca2009-08-18 15:08:22 -0700891 try {
Amith Yamasani37ce3a82012-02-06 12:04:42 -0800892 if (DEBUG)
893 Slog.v(TAG, "Removing window token: " + wallpaper.connection.mToken);
894 mIWindowManager.removeWindowToken(wallpaper.connection.mToken);
Dianne Hackborne9e9bca2009-08-18 15:08:22 -0700895 } catch (RemoteException e) {
896 }
Amith Yamasani37ce3a82012-02-06 12:04:42 -0800897 wallpaper.connection.mService = null;
898 wallpaper.connection.mEngine = null;
899 wallpaper.connection = null;
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700900 }
901 }
Amith Yamasani37ce3a82012-02-06 12:04:42 -0800902
903 void clearWallpaperComponentLocked(WallpaperData wallpaper) {
904 wallpaper.wallpaperComponent = null;
905 detachWallpaperLocked(wallpaper);
906 }
907
908 void attachServiceLocked(WallpaperConnection conn, WallpaperData wallpaper) {
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700909 try {
Dianne Hackborn3be63c02009-08-20 19:31:38 -0700910 conn.mService.attach(conn, conn.mToken,
911 WindowManager.LayoutParams.TYPE_WALLPAPER, false,
Amith Yamasani37ce3a82012-02-06 12:04:42 -0800912 wallpaper.width, wallpaper.height);
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700913 } catch (RemoteException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800914 Slog.w(TAG, "Failed attaching wallpaper; clearing", e);
Amith Yamasani37ce3a82012-02-06 12:04:42 -0800915 if (!wallpaper.wallpaperUpdating) {
Dianne Hackborn5dc5a002012-09-15 19:33:48 -0700916 bindWallpaperComponentLocked(null, false, false, wallpaper, null);
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800917 }
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700918 }
919 }
Amith Yamasani37ce3a82012-02-06 12:04:42 -0800920
921 private void notifyCallbacksLocked(WallpaperData wallpaper) {
922 final int n = wallpaper.callbacks.beginBroadcast();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800923 for (int i = 0; i < n; i++) {
924 try {
Amith Yamasani37ce3a82012-02-06 12:04:42 -0800925 wallpaper.callbacks.getBroadcastItem(i).onWallpaperChanged();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800926 } catch (RemoteException e) {
927
928 // The RemoteCallbackList will take care of removing
929 // the dead object for us.
930 }
931 }
Amith Yamasani37ce3a82012-02-06 12:04:42 -0800932 wallpaper.callbacks.finishBroadcast();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800933 final Intent intent = new Intent(Intent.ACTION_WALLPAPER_CHANGED);
Dianne Hackborn5ac72a22012-08-29 18:32:08 -0700934 mContext.sendBroadcastAsUser(intent, new UserHandle(mCurrentUserId));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800935 }
936
937 private void checkPermission(String permission) {
Joe Onorato9bb8fd72009-07-28 18:24:51 -0700938 if (PackageManager.PERMISSION_GRANTED!= mContext.checkCallingOrSelfPermission(permission)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800939 throw new SecurityException("Access denied to process: " + Binder.getCallingPid()
940 + ", must have permission " + permission);
941 }
942 }
Joe Onorato9bb8fd72009-07-28 18:24:51 -0700943
Amith Yamasani37ce3a82012-02-06 12:04:42 -0800944 private static JournaledFile makeJournaledFile(int userId) {
Amith Yamasani61f57372012-08-31 12:12:28 -0700945 final String base = new File(getWallpaperDir(userId), WALLPAPER_INFO).getAbsolutePath();
Joe Onorato9bb8fd72009-07-28 18:24:51 -0700946 return new JournaledFile(new File(base), new File(base + ".tmp"));
947 }
948
Amith Yamasani37ce3a82012-02-06 12:04:42 -0800949 private void saveSettingsLocked(WallpaperData wallpaper) {
950 JournaledFile journal = makeJournaledFile(wallpaper.userId);
Joe Onorato9bb8fd72009-07-28 18:24:51 -0700951 FileOutputStream stream = null;
952 try {
953 stream = new FileOutputStream(journal.chooseForWrite(), false);
954 XmlSerializer out = new FastXmlSerializer();
955 out.setOutput(stream, "utf-8");
956 out.startDocument(null, true);
957
958 out.startTag(null, "wp");
Amith Yamasani37ce3a82012-02-06 12:04:42 -0800959 out.attribute(null, "width", Integer.toString(wallpaper.width));
960 out.attribute(null, "height", Integer.toString(wallpaper.height));
961 out.attribute(null, "name", wallpaper.name);
962 if (wallpaper.wallpaperComponent != null
963 && !wallpaper.wallpaperComponent.equals(wallpaper.imageWallpaperComponent)) {
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700964 out.attribute(null, "component",
Amith Yamasani37ce3a82012-02-06 12:04:42 -0800965 wallpaper.wallpaperComponent.flattenToShortString());
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700966 }
Joe Onorato9bb8fd72009-07-28 18:24:51 -0700967 out.endTag(null, "wp");
968
969 out.endDocument();
970 stream.close();
971 journal.commit();
972 } catch (IOException e) {
973 try {
974 if (stream != null) {
975 stream.close();
976 }
977 } catch (IOException ex) {
978 // Ignore
979 }
980 journal.rollback();
981 }
982 }
983
Amith Yamasani37ce3a82012-02-06 12:04:42 -0800984 private void migrateFromOld() {
985 File oldWallpaper = new File(WallpaperBackupHelper.WALLPAPER_IMAGE_KEY);
986 File oldInfo = new File(WallpaperBackupHelper.WALLPAPER_INFO_KEY);
987 if (oldWallpaper.exists()) {
988 File newWallpaper = new File(getWallpaperDir(0), WALLPAPER);
989 oldWallpaper.renameTo(newWallpaper);
990 }
991 if (oldInfo.exists()) {
992 File newInfo = new File(getWallpaperDir(0), WALLPAPER_INFO);
993 oldInfo.renameTo(newInfo);
994 }
995 }
996
997 private void loadSettingsLocked(int userId) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800998 if (DEBUG) Slog.v(TAG, "loadSettingsLocked");
Mike Clerona428b2c2009-11-15 22:53:08 -0800999
Amith Yamasani37ce3a82012-02-06 12:04:42 -08001000 JournaledFile journal = makeJournaledFile(userId);
Joe Onorato9bb8fd72009-07-28 18:24:51 -07001001 FileInputStream stream = null;
1002 File file = journal.chooseForRead();
Amith Yamasani37ce3a82012-02-06 12:04:42 -08001003 if (!file.exists()) {
1004 // This should only happen one time, when upgrading from a legacy system
1005 migrateFromOld();
1006 }
1007 WallpaperData wallpaper = mWallpaperMap.get(userId);
1008 if (wallpaper == null) {
1009 wallpaper = new WallpaperData(userId);
1010 mWallpaperMap.put(userId, wallpaper);
1011 }
Joe Onorato9bb8fd72009-07-28 18:24:51 -07001012 boolean success = false;
1013 try {
1014 stream = new FileInputStream(file);
1015 XmlPullParser parser = Xml.newPullParser();
1016 parser.setInput(stream, null);
1017
1018 int type;
Joe Onorato9bb8fd72009-07-28 18:24:51 -07001019 do {
1020 type = parser.next();
1021 if (type == XmlPullParser.START_TAG) {
1022 String tag = parser.getName();
1023 if ("wp".equals(tag)) {
Amith Yamasani37ce3a82012-02-06 12:04:42 -08001024 wallpaper.width = Integer.parseInt(parser.getAttributeValue(null, "width"));
1025 wallpaper.height = Integer.parseInt(parser
1026 .getAttributeValue(null, "height"));
1027 wallpaper.name = parser.getAttributeValue(null, "name");
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001028 String comp = parser.getAttributeValue(null, "component");
Amith Yamasani37ce3a82012-02-06 12:04:42 -08001029 wallpaper.nextWallpaperComponent = comp != null
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001030 ? ComponentName.unflattenFromString(comp)
1031 : null;
Amith Yamasani37ce3a82012-02-06 12:04:42 -08001032 if (wallpaper.nextWallpaperComponent == null
1033 || "android".equals(wallpaper.nextWallpaperComponent
1034 .getPackageName())) {
1035 wallpaper.nextWallpaperComponent = wallpaper.imageWallpaperComponent;
Dianne Hackborn9ea31632011-08-05 14:43:50 -07001036 }
Mike Clerona428b2c2009-11-15 22:53:08 -08001037
1038 if (DEBUG) {
Amith Yamasani37ce3a82012-02-06 12:04:42 -08001039 Slog.v(TAG, "mWidth:" + wallpaper.width);
1040 Slog.v(TAG, "mHeight:" + wallpaper.height);
1041 Slog.v(TAG, "mName:" + wallpaper.name);
1042 Slog.v(TAG, "mNextWallpaperComponent:"
1043 + wallpaper.nextWallpaperComponent);
Mike Clerona428b2c2009-11-15 22:53:08 -08001044 }
Joe Onorato9bb8fd72009-07-28 18:24:51 -07001045 }
1046 }
1047 } while (type != XmlPullParser.END_DOCUMENT);
1048 success = true;
1049 } catch (NullPointerException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001050 Slog.w(TAG, "failed parsing " + file + " " + e);
Joe Onorato9bb8fd72009-07-28 18:24:51 -07001051 } catch (NumberFormatException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001052 Slog.w(TAG, "failed parsing " + file + " " + e);
Joe Onorato9bb8fd72009-07-28 18:24:51 -07001053 } catch (XmlPullParserException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001054 Slog.w(TAG, "failed parsing " + file + " " + e);
Joe Onorato9bb8fd72009-07-28 18:24:51 -07001055 } catch (IOException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001056 Slog.w(TAG, "failed parsing " + file + " " + e);
Joe Onorato9bb8fd72009-07-28 18:24:51 -07001057 } catch (IndexOutOfBoundsException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001058 Slog.w(TAG, "failed parsing " + file + " " + e);
Joe Onorato9bb8fd72009-07-28 18:24:51 -07001059 }
1060 try {
1061 if (stream != null) {
1062 stream.close();
1063 }
1064 } catch (IOException e) {
1065 // Ignore
1066 }
1067
1068 if (!success) {
Amith Yamasani37ce3a82012-02-06 12:04:42 -08001069 wallpaper.width = -1;
1070 wallpaper.height = -1;
1071 wallpaper.name = "";
Joe Onorato9bb8fd72009-07-28 18:24:51 -07001072 }
Dianne Hackborn44bc17c2011-04-20 18:18:51 -07001073
1074 // We always want to have some reasonable width hint.
1075 WindowManager wm = (WindowManager)mContext.getSystemService(Context.WINDOW_SERVICE);
1076 Display d = wm.getDefaultDisplay();
1077 int baseSize = d.getMaximumSizeDimension();
Amith Yamasani37ce3a82012-02-06 12:04:42 -08001078 if (wallpaper.width < baseSize) {
1079 wallpaper.width = baseSize;
Dianne Hackborn44bc17c2011-04-20 18:18:51 -07001080 }
Amith Yamasani37ce3a82012-02-06 12:04:42 -08001081 if (wallpaper.height < baseSize) {
1082 wallpaper.height = baseSize;
Dianne Hackborn44bc17c2011-04-20 18:18:51 -07001083 }
Joe Onorato9bb8fd72009-07-28 18:24:51 -07001084 }
1085
Brad Fitzpatrick194b19a2010-09-14 11:30:29 -07001086 // Called by SystemBackupAgent after files are restored to disk.
Joe Onorato9bb8fd72009-07-28 18:24:51 -07001087 void settingsRestored() {
Amith Yamasani37ce3a82012-02-06 12:04:42 -08001088 // TODO: If necessary, make it work for secondary users as well. This currently assumes
1089 // restores only to the primary user
Joe Onorato8a9b2202010-02-26 18:56:32 -08001090 if (DEBUG) Slog.v(TAG, "settingsRestored");
Amith Yamasani37ce3a82012-02-06 12:04:42 -08001091 WallpaperData wallpaper = null;
Joe Onorato9bb8fd72009-07-28 18:24:51 -07001092 boolean success = false;
1093 synchronized (mLock) {
Amith Yamasani37ce3a82012-02-06 12:04:42 -08001094 loadSettingsLocked(0);
1095 wallpaper = mWallpaperMap.get(0);
1096 if (wallpaper.nextWallpaperComponent != null
1097 && !wallpaper.nextWallpaperComponent.equals(wallpaper.imageWallpaperComponent)) {
1098 if (!bindWallpaperComponentLocked(wallpaper.nextWallpaperComponent, false, false,
Dianne Hackborn5dc5a002012-09-15 19:33:48 -07001099 wallpaper, null)) {
Christopher Tatee3ab4d02009-12-16 14:03:31 -08001100 // No such live wallpaper or other failure; fall back to the default
1101 // live wallpaper (since the profile being restored indicated that the
1102 // user had selected a live rather than static one).
Dianne Hackborn5dc5a002012-09-15 19:33:48 -07001103 bindWallpaperComponentLocked(null, false, false, wallpaper, null);
Christopher Tatee3ab4d02009-12-16 14:03:31 -08001104 }
Joe Onorato9bb8fd72009-07-28 18:24:51 -07001105 success = true;
1106 } else {
Mike Clerona428b2c2009-11-15 22:53:08 -08001107 // If there's a wallpaper name, we use that. If that can't be loaded, then we
1108 // use the default.
Amith Yamasani37ce3a82012-02-06 12:04:42 -08001109 if ("".equals(wallpaper.name)) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001110 if (DEBUG) Slog.v(TAG, "settingsRestored: name is empty");
Mike Clerona428b2c2009-11-15 22:53:08 -08001111 success = true;
1112 } else {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001113 if (DEBUG) Slog.v(TAG, "settingsRestored: attempting to restore named resource");
Amith Yamasani37ce3a82012-02-06 12:04:42 -08001114 success = restoreNamedResourceLocked(wallpaper);
Mike Clerona428b2c2009-11-15 22:53:08 -08001115 }
Joe Onorato8a9b2202010-02-26 18:56:32 -08001116 if (DEBUG) Slog.v(TAG, "settingsRestored: success=" + success);
Mike Clerona428b2c2009-11-15 22:53:08 -08001117 if (success) {
Amith Yamasani37ce3a82012-02-06 12:04:42 -08001118 bindWallpaperComponentLocked(wallpaper.nextWallpaperComponent, false, false,
Dianne Hackborn5dc5a002012-09-15 19:33:48 -07001119 wallpaper, null);
Mike Clerona428b2c2009-11-15 22:53:08 -08001120 }
Joe Onorato9bb8fd72009-07-28 18:24:51 -07001121 }
1122 }
1123
1124 if (!success) {
Amith Yamasani37ce3a82012-02-06 12:04:42 -08001125 Slog.e(TAG, "Failed to restore wallpaper: '" + wallpaper.name + "'");
1126 wallpaper.name = "";
1127 getWallpaperDir(0).delete();
Joe Onorato9bb8fd72009-07-28 18:24:51 -07001128 }
Brad Fitzpatrick194b19a2010-09-14 11:30:29 -07001129
1130 synchronized (mLock) {
Amith Yamasani37ce3a82012-02-06 12:04:42 -08001131 saveSettingsLocked(wallpaper);
Brad Fitzpatrick194b19a2010-09-14 11:30:29 -07001132 }
Joe Onorato9bb8fd72009-07-28 18:24:51 -07001133 }
1134
Amith Yamasani37ce3a82012-02-06 12:04:42 -08001135 boolean restoreNamedResourceLocked(WallpaperData wallpaper) {
1136 if (wallpaper.name.length() > 4 && "res:".equals(wallpaper.name.substring(0, 4))) {
1137 String resName = wallpaper.name.substring(4);
Joe Onorato9bb8fd72009-07-28 18:24:51 -07001138
1139 String pkg = null;
1140 int colon = resName.indexOf(':');
1141 if (colon > 0) {
1142 pkg = resName.substring(0, colon);
1143 }
1144
1145 String ident = null;
1146 int slash = resName.lastIndexOf('/');
1147 if (slash > 0) {
1148 ident = resName.substring(slash+1);
1149 }
1150
1151 String type = null;
1152 if (colon > 0 && slash > 0 && (slash-colon) > 1) {
1153 type = resName.substring(colon+1, slash);
1154 }
1155
1156 if (pkg != null && ident != null && type != null) {
1157 int resId = -1;
1158 InputStream res = null;
1159 FileOutputStream fos = null;
1160 try {
1161 Context c = mContext.createPackageContext(pkg, Context.CONTEXT_RESTRICTED);
1162 Resources r = c.getResources();
1163 resId = r.getIdentifier(resName, null, null);
1164 if (resId == 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001165 Slog.e(TAG, "couldn't resolve identifier pkg=" + pkg + " type=" + type
Joe Onorato9bb8fd72009-07-28 18:24:51 -07001166 + " ident=" + ident);
1167 return false;
1168 }
1169
1170 res = r.openRawResource(resId);
Amith Yamasani37ce3a82012-02-06 12:04:42 -08001171 if (wallpaper.wallpaperFile.exists()) {
1172 wallpaper.wallpaperFile.delete();
Dianne Hackborn1afd1c92010-03-18 22:47:17 -07001173 }
Amith Yamasani37ce3a82012-02-06 12:04:42 -08001174 fos = new FileOutputStream(wallpaper.wallpaperFile);
Joe Onorato9bb8fd72009-07-28 18:24:51 -07001175
1176 byte[] buffer = new byte[32768];
1177 int amt;
1178 while ((amt=res.read(buffer)) > 0) {
1179 fos.write(buffer, 0, amt);
1180 }
1181 // mWallpaperObserver will notice the close and send the change broadcast
1182
Joe Onorato8a9b2202010-02-26 18:56:32 -08001183 Slog.v(TAG, "Restored wallpaper: " + resName);
Joe Onorato9bb8fd72009-07-28 18:24:51 -07001184 return true;
1185 } catch (NameNotFoundException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001186 Slog.e(TAG, "Package name " + pkg + " not found");
Joe Onorato9bb8fd72009-07-28 18:24:51 -07001187 } catch (Resources.NotFoundException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001188 Slog.e(TAG, "Resource not found: " + resId);
Joe Onorato9bb8fd72009-07-28 18:24:51 -07001189 } catch (IOException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001190 Slog.e(TAG, "IOException while restoring wallpaper ", e);
Joe Onorato9bb8fd72009-07-28 18:24:51 -07001191 } finally {
1192 if (res != null) {
1193 try {
1194 res.close();
1195 } catch (IOException ex) {}
1196 }
1197 if (fos != null) {
Dianne Hackborn8bdf5932010-10-15 12:54:40 -07001198 FileUtils.sync(fos);
Joe Onorato9bb8fd72009-07-28 18:24:51 -07001199 try {
1200 fos.close();
1201 } catch (IOException ex) {}
1202 }
1203 }
1204 }
1205 }
1206 return false;
1207 }
Amith Yamasani37ce3a82012-02-06 12:04:42 -08001208
Dianne Hackborneb034652009-09-07 00:49:58 -07001209 @Override
1210 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
1211 if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DUMP)
1212 != PackageManager.PERMISSION_GRANTED) {
1213
1214 pw.println("Permission Denial: can't dump wallpaper service from from pid="
1215 + Binder.getCallingPid()
1216 + ", uid=" + Binder.getCallingUid());
1217 return;
1218 }
1219
1220 synchronized (mLock) {
1221 pw.println("Current Wallpaper Service state:");
Amith Yamasani37ce3a82012-02-06 12:04:42 -08001222 for (int i = 0; i < mWallpaperMap.size(); i++) {
1223 WallpaperData wallpaper = mWallpaperMap.valueAt(i);
1224 pw.println(" User " + wallpaper.userId + ":");
1225 pw.print(" mWidth=");
1226 pw.print(wallpaper.width);
1227 pw.print(" mHeight=");
1228 pw.println(wallpaper.height);
1229 pw.print(" mName=");
1230 pw.println(wallpaper.name);
1231 pw.print(" mWallpaperComponent=");
1232 pw.println(wallpaper.wallpaperComponent);
1233 if (wallpaper.connection != null) {
1234 WallpaperConnection conn = wallpaper.connection;
1235 pw.print(" Wallpaper connection ");
1236 pw.print(conn);
1237 pw.println(":");
1238 if (conn.mInfo != null) {
1239 pw.print(" mInfo.component=");
1240 pw.println(conn.mInfo.getComponent());
1241 }
1242 pw.print(" mToken=");
1243 pw.println(conn.mToken);
1244 pw.print(" mService=");
1245 pw.println(conn.mService);
1246 pw.print(" mEngine=");
1247 pw.println(conn.mEngine);
1248 pw.print(" mLastDiedTime=");
1249 pw.println(wallpaper.lastDiedTime - SystemClock.uptimeMillis());
1250 }
Dianne Hackborneb034652009-09-07 00:49:58 -07001251 }
1252 }
1253 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001254}