blob: 162add425cf535a066cc1cc506744b782109363c [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;
Amith Yamasani6474c4c2012-10-04 14:55:42 -070041import android.content.pm.UserInfo;
Joe Onorato9bb8fd72009-07-28 18:24:51 -070042import android.content.res.Resources;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080043import android.os.Binder;
Dianne Hackborn284ac932009-08-28 10:34:25 -070044import android.os.Bundle;
Amith Yamasani13593602012-03-22 16:16:17 -070045import android.os.Environment;
Dianne Hackborn8bdf5932010-10-15 12:54:40 -070046import android.os.FileUtils;
Dianne Hackborn4c62fc02009-08-08 20:40:27 -070047import android.os.IBinder;
Dianne Hackborn5dc5a002012-09-15 19:33:48 -070048import android.os.IRemoteCallback;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080049import android.os.RemoteException;
50import android.os.FileObserver;
51import android.os.ParcelFileDescriptor;
52import android.os.RemoteCallbackList;
rpcraig554cb0c2012-07-05 06:41:43 -040053import android.os.SELinux;
Dianne Hackborn4c62fc02009-08-08 20:40:27 -070054import android.os.ServiceManager;
Dianne Hackborn0cd48872009-08-13 18:51:59 -070055import android.os.SystemClock;
Dianne Hackbornf02b60a2012-08-16 10:48:27 -070056import android.os.UserHandle;
Amith Yamasani6474c4c2012-10-04 14:55:42 -070057import android.os.UserManager;
Dianne Hackborn4c62fc02009-08-08 20:40:27 -070058import android.service.wallpaper.IWallpaperConnection;
59import android.service.wallpaper.IWallpaperEngine;
60import android.service.wallpaper.IWallpaperService;
61import android.service.wallpaper.WallpaperService;
Joe Onorato8a9b2202010-02-26 18:56:32 -080062import android.util.Slog;
Amith Yamasani37ce3a82012-02-06 12:04:42 -080063import android.util.SparseArray;
Joe Onorato9bb8fd72009-07-28 18:24:51 -070064import android.util.Xml;
Dianne Hackborn44bc17c2011-04-20 18:18:51 -070065import android.view.Display;
Dianne Hackborn4c62fc02009-08-08 20:40:27 -070066import android.view.IWindowManager;
67import android.view.WindowManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080068
Dianne Hackborneb034652009-09-07 00:49:58 -070069import java.io.FileDescriptor;
Joe Onorato9bb8fd72009-07-28 18:24:51 -070070import java.io.IOException;
71import java.io.InputStream;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080072import java.io.File;
73import java.io.FileNotFoundException;
Joe Onorato9bb8fd72009-07-28 18:24:51 -070074import java.io.FileInputStream;
75import java.io.FileOutputStream;
Dianne Hackborneb034652009-09-07 00:49:58 -070076import java.io.PrintWriter;
Dianne Hackborn4c62fc02009-08-08 20:40:27 -070077import java.util.List;
Joe Onorato9bb8fd72009-07-28 18:24:51 -070078
79import org.xmlpull.v1.XmlPullParser;
80import org.xmlpull.v1.XmlPullParserException;
81import org.xmlpull.v1.XmlSerializer;
82
Dianne Hackborn21f1bd12010-02-19 17:02:21 -080083import com.android.internal.content.PackageMonitor;
Dianne Hackborn2269d1572010-02-24 19:54:22 -080084import com.android.internal.util.FastXmlSerializer;
Dianne Hackborn1afd1c92010-03-18 22:47:17 -070085import com.android.internal.util.JournaledFile;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080086
Dianne Hackborn8cc6a502009-08-05 21:29:42 -070087class WallpaperManagerService extends IWallpaperManager.Stub {
Dianne Hackborn4c62fc02009-08-08 20:40:27 -070088 static final String TAG = "WallpaperService";
Dianne Hackborncbf15042009-08-18 18:29:09 -070089 static final boolean DEBUG = false;
Joe Onorato9bb8fd72009-07-28 18:24:51 -070090
Romain Guy407ec782011-08-24 17:06:58 -070091 final Object mLock = new Object[0];
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080092
Dianne Hackborn0cd48872009-08-13 18:51:59 -070093 /**
94 * Minimum time between crashes of a wallpaper service for us to consider
95 * restarting it vs. just reverting to the static wallpaper.
96 */
97 static final long MIN_WALLPAPER_CRASH_TIME = 10000;
Dianne Hackborn0cd48872009-08-13 18:51:59 -070098 static final String WALLPAPER = "wallpaper";
Amith Yamasani37ce3a82012-02-06 12:04:42 -080099 static final String WALLPAPER_INFO = "wallpaper_info.xml";
Joe Onorato9bb8fd72009-07-28 18:24:51 -0700100
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800101 /**
Dianne Hackbornbce0cbb2012-10-05 11:06:53 -0700102 * Name of the component used to display bitmap wallpapers from either the gallery or
103 * built-in wallpapers.
104 */
105 static final ComponentName IMAGE_WALLPAPER = new ComponentName("com.android.systemui",
106 "com.android.systemui.ImageWallpaper");
107
108 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800109 * Observes the wallpaper for changes and notifies all IWallpaperServiceCallbacks
110 * that the wallpaper has changed. The CREATE is triggered when there is no
111 * wallpaper set and is created for the first time. The CLOSE_WRITE is triggered
112 * everytime the wallpaper is changed.
113 */
Amith Yamasani37ce3a82012-02-06 12:04:42 -0800114 private class WallpaperObserver extends FileObserver {
Joe Onorato9bb8fd72009-07-28 18:24:51 -0700115
Amith Yamasani37ce3a82012-02-06 12:04:42 -0800116 final WallpaperData mWallpaper;
117 final File mWallpaperDir;
118 final File mWallpaperFile;
119
120 public WallpaperObserver(WallpaperData wallpaper) {
121 super(getWallpaperDir(wallpaper.userId).getAbsolutePath(),
122 CLOSE_WRITE | DELETE | DELETE_SELF);
123 mWallpaperDir = getWallpaperDir(wallpaper.userId);
124 mWallpaper = wallpaper;
125 mWallpaperFile = new File(mWallpaperDir, WALLPAPER);
126 }
127
128 @Override
129 public void onEvent(int event, String path) {
130 if (path == null) {
131 return;
132 }
133 synchronized (mLock) {
134 // changing the wallpaper means we'll need to back up the new one
135 long origId = Binder.clearCallingIdentity();
136 BackupManager bm = new BackupManager(mContext);
137 bm.dataChanged();
138 Binder.restoreCallingIdentity(origId);
139
140 File changedFile = new File(mWallpaperDir, path);
141 if (mWallpaperFile.equals(changedFile)) {
142 notifyCallbacksLocked(mWallpaper);
143 if (mWallpaper.wallpaperComponent == null || event != CLOSE_WRITE
144 || mWallpaper.imageWallpaperPending) {
145 if (event == CLOSE_WRITE) {
146 mWallpaper.imageWallpaperPending = false;
Joe Onorato9bb8fd72009-07-28 18:24:51 -0700147 }
Dianne Hackbornbce0cbb2012-10-05 11:06:53 -0700148 bindWallpaperComponentLocked(IMAGE_WALLPAPER, true,
Dianne Hackborn5dc5a002012-09-15 19:33:48 -0700149 false, mWallpaper, null);
Amith Yamasani37ce3a82012-02-06 12:04:42 -0800150 saveSettingsLocked(mWallpaper);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800151 }
152 }
Amith Yamasani37ce3a82012-02-06 12:04:42 -0800153 }
154 }
155 }
156
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700157 final Context mContext;
158 final IWindowManager mIWindowManager;
Amith Yamasani4e2820c2012-08-28 22:17:23 -0700159 final IPackageManager mIPackageManager;
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800160 final MyPackageMonitor mMonitor;
Amith Yamasani37ce3a82012-02-06 12:04:42 -0800161 WallpaperData mLastWallpaper;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800162
Amith Yamasani37ce3a82012-02-06 12:04:42 -0800163 SparseArray<WallpaperData> mWallpaperMap = new SparseArray<WallpaperData>();
Dianne Hackborn07213e62011-08-24 20:05:39 -0700164
Amith Yamasani37ce3a82012-02-06 12:04:42 -0800165 int mCurrentUserId;
Dianne Hackborn07213e62011-08-24 20:05:39 -0700166
Amith Yamasani37ce3a82012-02-06 12:04:42 -0800167 static class WallpaperData {
168
169 int userId;
170
171 File wallpaperFile;
172
173 /**
174 * Client is currently writing a new image wallpaper.
175 */
176 boolean imageWallpaperPending;
177
178 /**
179 * Resource name if using a picture from the wallpaper gallery
180 */
181 String name = "";
182
183 /**
184 * The component name of the currently set live wallpaper.
185 */
186 ComponentName wallpaperComponent;
187
188 /**
189 * The component name of the wallpaper that should be set next.
190 */
191 ComponentName nextWallpaperComponent;
192
Amith Yamasani37ce3a82012-02-06 12:04:42 -0800193 WallpaperConnection connection;
194 long lastDiedTime;
195 boolean wallpaperUpdating;
196 WallpaperObserver wallpaperObserver;
197
198 /**
199 * List of callbacks registered they should each be notified when the wallpaper is changed.
200 */
201 private RemoteCallbackList<IWallpaperManagerCallback> callbacks
202 = new RemoteCallbackList<IWallpaperManagerCallback>();
203
204 int width = -1;
205 int height = -1;
206
207 WallpaperData(int userId) {
208 this.userId = userId;
209 wallpaperFile = new File(getWallpaperDir(userId), WALLPAPER);
210 }
211 }
212
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700213 class WallpaperConnection extends IWallpaperConnection.Stub
214 implements ServiceConnection {
Dianne Hackborneb034652009-09-07 00:49:58 -0700215 final WallpaperInfo mInfo;
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700216 final Binder mToken = new Binder();
217 IWallpaperService mService;
218 IWallpaperEngine mEngine;
Amith Yamasani37ce3a82012-02-06 12:04:42 -0800219 WallpaperData mWallpaper;
Dianne Hackborn5dc5a002012-09-15 19:33:48 -0700220 IRemoteCallback mReply;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800221
Amith Yamasani37ce3a82012-02-06 12:04:42 -0800222 public WallpaperConnection(WallpaperInfo info, WallpaperData wallpaper) {
Dianne Hackborneb034652009-09-07 00:49:58 -0700223 mInfo = info;
Amith Yamasani37ce3a82012-02-06 12:04:42 -0800224 mWallpaper = wallpaper;
Dianne Hackborneb034652009-09-07 00:49:58 -0700225 }
Dianne Hackborn5dc5a002012-09-15 19:33:48 -0700226
227 @Override
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700228 public void onServiceConnected(ComponentName name, IBinder service) {
229 synchronized (mLock) {
Amith Yamasani37ce3a82012-02-06 12:04:42 -0800230 if (mWallpaper.connection == this) {
231 mWallpaper.lastDiedTime = SystemClock.uptimeMillis();
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700232 mService = IWallpaperService.Stub.asInterface(service);
Amith Yamasani37ce3a82012-02-06 12:04:42 -0800233 attachServiceLocked(this, mWallpaper);
Dianne Hackborneb034652009-09-07 00:49:58 -0700234 // XXX should probably do saveSettingsLocked() later
235 // when we have an engine, but I'm not sure about
236 // locking there and anyway we always need to be able to
237 // recover if there is something wrong.
Amith Yamasani37ce3a82012-02-06 12:04:42 -0800238 saveSettingsLocked(mWallpaper);
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700239 }
240 }
241 }
242
Dianne Hackborn5dc5a002012-09-15 19:33:48 -0700243 @Override
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700244 public void onServiceDisconnected(ComponentName name) {
245 synchronized (mLock) {
246 mService = null;
247 mEngine = null;
Amith Yamasani37ce3a82012-02-06 12:04:42 -0800248 if (mWallpaper.connection == this) {
249 Slog.w(TAG, "Wallpaper service gone: " + mWallpaper.wallpaperComponent);
250 if (!mWallpaper.wallpaperUpdating
251 && (mWallpaper.lastDiedTime + MIN_WALLPAPER_CRASH_TIME)
252 > SystemClock.uptimeMillis()
253 && mWallpaper.userId == mCurrentUserId) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800254 Slog.w(TAG, "Reverting to built-in wallpaper!");
Dianne Hackborn5dc5a002012-09-15 19:33:48 -0700255 clearWallpaperLocked(true, mWallpaper.userId, null);
Dianne Hackborn0cd48872009-08-13 18:51:59 -0700256 }
257 }
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700258 }
259 }
Amith Yamasani37ce3a82012-02-06 12:04:42 -0800260
Dianne Hackborn5dc5a002012-09-15 19:33:48 -0700261 @Override
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700262 public void attachEngine(IWallpaperEngine engine) {
Dianne Hackborn5dc5a002012-09-15 19:33:48 -0700263 synchronized (mLock) {
264 mEngine = engine;
265 }
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700266 }
Amith Yamasani37ce3a82012-02-06 12:04:42 -0800267
Dianne Hackborn5dc5a002012-09-15 19:33:48 -0700268 @Override
269 public void engineShown(IWallpaperEngine engine) {
270 synchronized (mLock) {
271 if (mReply != null) {
272 long ident = Binder.clearCallingIdentity();
273 try {
274 mReply.sendResult(null);
275 } catch (RemoteException e) {
276 Binder.restoreCallingIdentity(ident);
277 }
278 mReply = null;
279 }
280 }
281 }
282
283 @Override
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700284 public ParcelFileDescriptor setWallpaper(String name) {
285 synchronized (mLock) {
Amith Yamasani37ce3a82012-02-06 12:04:42 -0800286 if (mWallpaper.connection == this) {
287 return updateWallpaperBitmapLocked(name, mWallpaper);
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700288 }
289 return null;
290 }
291 }
292 }
Amith Yamasani37ce3a82012-02-06 12:04:42 -0800293
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800294 class MyPackageMonitor extends PackageMonitor {
295 @Override
296 public void onPackageUpdateFinished(String packageName, int uid) {
297 synchronized (mLock) {
Dianne Hackbornc72fc672012-09-20 13:12:03 -0700298 if (mCurrentUserId != getChangingUserId()) {
299 return;
300 }
301 WallpaperData wallpaper = mWallpaperMap.get(mCurrentUserId);
302 if (wallpaper != null) {
Amith Yamasani37ce3a82012-02-06 12:04:42 -0800303 if (wallpaper.wallpaperComponent != null
304 && wallpaper.wallpaperComponent.getPackageName().equals(packageName)) {
305 wallpaper.wallpaperUpdating = false;
306 ComponentName comp = wallpaper.wallpaperComponent;
307 clearWallpaperComponentLocked(wallpaper);
Dianne Hackbornc72fc672012-09-20 13:12:03 -0700308 if (!bindWallpaperComponentLocked(comp, false, false,
309 wallpaper, null)) {
Amith Yamasani37ce3a82012-02-06 12:04:42 -0800310 Slog.w(TAG, "Wallpaper no longer available; reverting to default");
Dianne Hackborn5dc5a002012-09-15 19:33:48 -0700311 clearWallpaperLocked(false, wallpaper.userId, null);
Amith Yamasani37ce3a82012-02-06 12:04:42 -0800312 }
Dianne Hackborn80b902f2011-09-15 15:15:27 -0700313 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800314 }
315 }
316 }
317
318 @Override
Dianne Hackborn80b902f2011-09-15 15:15:27 -0700319 public void onPackageModified(String packageName) {
320 synchronized (mLock) {
Dianne Hackbornc72fc672012-09-20 13:12:03 -0700321 if (mCurrentUserId != getChangingUserId()) {
322 return;
323 }
324 WallpaperData wallpaper = mWallpaperMap.get(mCurrentUserId);
325 if (wallpaper != null) {
Amith Yamasani37ce3a82012-02-06 12:04:42 -0800326 if (wallpaper.wallpaperComponent == null
327 || !wallpaper.wallpaperComponent.getPackageName().equals(packageName)) {
Dianne Hackbornc72fc672012-09-20 13:12:03 -0700328 return;
Amith Yamasani37ce3a82012-02-06 12:04:42 -0800329 }
Dianne Hackbornd0d75032012-04-19 23:12:09 -0700330 doPackagesChangedLocked(true, wallpaper);
Dianne Hackborn80b902f2011-09-15 15:15:27 -0700331 }
332 }
Dianne Hackborn80b902f2011-09-15 15:15:27 -0700333 }
334
335 @Override
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800336 public void onPackageUpdateStarted(String packageName, int uid) {
337 synchronized (mLock) {
Dianne Hackbornc72fc672012-09-20 13:12:03 -0700338 if (mCurrentUserId != getChangingUserId()) {
339 return;
340 }
341 WallpaperData wallpaper = mWallpaperMap.get(mCurrentUserId);
342 if (wallpaper != null) {
Amith Yamasani37ce3a82012-02-06 12:04:42 -0800343 if (wallpaper.wallpaperComponent != null
344 && wallpaper.wallpaperComponent.getPackageName().equals(packageName)) {
345 wallpaper.wallpaperUpdating = true;
346 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800347 }
348 }
349 }
350
351 @Override
352 public boolean onHandleForceStop(Intent intent, String[] packages, int uid, boolean doit) {
Dianne Hackbornd0d75032012-04-19 23:12:09 -0700353 synchronized (mLock) {
354 boolean changed = false;
Dianne Hackbornc72fc672012-09-20 13:12:03 -0700355 if (mCurrentUserId != getChangingUserId()) {
356 return false;
357 }
358 WallpaperData wallpaper = mWallpaperMap.get(mCurrentUserId);
359 if (wallpaper != null) {
Dianne Hackbornd0d75032012-04-19 23:12:09 -0700360 boolean res = doPackagesChangedLocked(doit, wallpaper);
361 changed |= res;
362 }
363 return changed;
Amith Yamasani37ce3a82012-02-06 12:04:42 -0800364 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800365 }
366
367 @Override
368 public void onSomePackagesChanged() {
Dianne Hackbornd0d75032012-04-19 23:12:09 -0700369 synchronized (mLock) {
Dianne Hackbornc72fc672012-09-20 13:12:03 -0700370 if (mCurrentUserId != getChangingUserId()) {
371 return;
372 }
373 WallpaperData wallpaper = mWallpaperMap.get(mCurrentUserId);
374 if (wallpaper != null) {
Dianne Hackbornd0d75032012-04-19 23:12:09 -0700375 doPackagesChangedLocked(true, wallpaper);
376 }
Amith Yamasani37ce3a82012-02-06 12:04:42 -0800377 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800378 }
Amith Yamasani37ce3a82012-02-06 12:04:42 -0800379
Dianne Hackbornd0d75032012-04-19 23:12:09 -0700380 boolean doPackagesChangedLocked(boolean doit, WallpaperData wallpaper) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800381 boolean changed = false;
Dianne Hackbornd0d75032012-04-19 23:12:09 -0700382 if (wallpaper.wallpaperComponent != null) {
383 int change = isPackageDisappearing(wallpaper.wallpaperComponent
384 .getPackageName());
385 if (change == PACKAGE_PERMANENT_CHANGE
386 || change == PACKAGE_TEMPORARY_CHANGE) {
387 changed = true;
388 if (doit) {
389 Slog.w(TAG, "Wallpaper uninstalled, removing: "
Amith Yamasani37ce3a82012-02-06 12:04:42 -0800390 + wallpaper.wallpaperComponent);
Dianne Hackborn5dc5a002012-09-15 19:33:48 -0700391 clearWallpaperLocked(false, wallpaper.userId, null);
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800392 }
393 }
Dianne Hackbornd0d75032012-04-19 23:12:09 -0700394 }
395 if (wallpaper.nextWallpaperComponent != null) {
396 int change = isPackageDisappearing(wallpaper.nextWallpaperComponent
397 .getPackageName());
398 if (change == PACKAGE_PERMANENT_CHANGE
399 || change == PACKAGE_TEMPORARY_CHANGE) {
400 wallpaper.nextWallpaperComponent = null;
401 }
402 }
403 if (wallpaper.wallpaperComponent != null
404 && isPackageModified(wallpaper.wallpaperComponent.getPackageName())) {
405 try {
406 mContext.getPackageManager().getServiceInfo(
407 wallpaper.wallpaperComponent, 0);
408 } catch (NameNotFoundException e) {
409 Slog.w(TAG, "Wallpaper component gone, removing: "
410 + wallpaper.wallpaperComponent);
Dianne Hackborn5dc5a002012-09-15 19:33:48 -0700411 clearWallpaperLocked(false, wallpaper.userId, null);
Dianne Hackbornd0d75032012-04-19 23:12:09 -0700412 }
413 }
414 if (wallpaper.nextWallpaperComponent != null
415 && isPackageModified(wallpaper.nextWallpaperComponent.getPackageName())) {
416 try {
417 mContext.getPackageManager().getServiceInfo(
418 wallpaper.nextWallpaperComponent, 0);
419 } catch (NameNotFoundException e) {
420 wallpaper.nextWallpaperComponent = null;
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800421 }
422 }
423 return changed;
424 }
425 }
426
Dianne Hackborn8cc6a502009-08-05 21:29:42 -0700427 public WallpaperManagerService(Context context) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800428 if (DEBUG) Slog.v(TAG, "WallpaperService startup");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800429 mContext = context;
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700430 mIWindowManager = IWindowManager.Stub.asInterface(
431 ServiceManager.getService(Context.WINDOW_SERVICE));
Amith Yamasani4e2820c2012-08-28 22:17:23 -0700432 mIPackageManager = AppGlobals.getPackageManager();
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800433 mMonitor = new MyPackageMonitor();
Dianne Hackbornc72fc672012-09-20 13:12:03 -0700434 mMonitor.register(context, null, UserHandle.ALL, true);
Amith Yamasani61f57372012-08-31 12:12:28 -0700435 getWallpaperDir(UserHandle.USER_OWNER).mkdirs();
436 loadSettingsLocked(UserHandle.USER_OWNER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800437 }
438
Amith Yamasani37ce3a82012-02-06 12:04:42 -0800439 private static File getWallpaperDir(int userId) {
Amith Yamasani61f57372012-08-31 12:12:28 -0700440 return Environment.getUserSystemDirectory(userId);
Amith Yamasani37ce3a82012-02-06 12:04:42 -0800441 }
442
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800443 @Override
444 protected void finalize() throws Throwable {
445 super.finalize();
Amith Yamasani37ce3a82012-02-06 12:04:42 -0800446 for (int i = 0; i < mWallpaperMap.size(); i++) {
447 WallpaperData wallpaper = mWallpaperMap.valueAt(i);
448 wallpaper.wallpaperObserver.stopWatching();
449 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800450 }
Amith Yamasani13593602012-03-22 16:16:17 -0700451
Svetoslav Ganova0027152013-06-25 14:59:53 -0700452 public void systemRunning() {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800453 if (DEBUG) Slog.v(TAG, "systemReady");
Amith Yamasani61f57372012-08-31 12:12:28 -0700454 WallpaperData wallpaper = mWallpaperMap.get(UserHandle.USER_OWNER);
Dianne Hackborn5dc5a002012-09-15 19:33:48 -0700455 switchWallpaper(wallpaper, null);
Amith Yamasani37ce3a82012-02-06 12:04:42 -0800456 wallpaper.wallpaperObserver = new WallpaperObserver(wallpaper);
457 wallpaper.wallpaperObserver.startWatching();
Amith Yamasani37ce3a82012-02-06 12:04:42 -0800458
Amith Yamasani13593602012-03-22 16:16:17 -0700459 IntentFilter userFilter = new IntentFilter();
Amith Yamasani13593602012-03-22 16:16:17 -0700460 userFilter.addAction(Intent.ACTION_USER_REMOVED);
Amith Yamasani756901d2012-10-12 12:30:07 -0700461 userFilter.addAction(Intent.ACTION_USER_STOPPING);
Amith Yamasani13593602012-03-22 16:16:17 -0700462 mContext.registerReceiver(new BroadcastReceiver() {
Amith Yamasani37ce3a82012-02-06 12:04:42 -0800463 @Override
Amith Yamasani13593602012-03-22 16:16:17 -0700464 public void onReceive(Context context, Intent intent) {
465 String action = intent.getAction();
Dianne Hackborn5dc5a002012-09-15 19:33:48 -0700466 if (Intent.ACTION_USER_REMOVED.equals(action)) {
Amith Yamasani756901d2012-10-12 12:30:07 -0700467 onRemoveUser(intent.getIntExtra(Intent.EXTRA_USER_HANDLE,
468 UserHandle.USER_NULL));
Amith Yamasani13593602012-03-22 16:16:17 -0700469 }
Amith Yamasani0c293712012-10-30 12:23:52 -0700470 // TODO: Race condition causing problems when cleaning up on stopping a user.
471 // Comment this out for now.
472 // else if (Intent.ACTION_USER_STOPPING.equals(action)) {
473 // onStoppingUser(intent.getIntExtra(Intent.EXTRA_USER_HANDLE,
474 // UserHandle.USER_NULL));
475 // }
Amith Yamasani37ce3a82012-02-06 12:04:42 -0800476 }
Amith Yamasani13593602012-03-22 16:16:17 -0700477 }, userFilter);
Amith Yamasani756901d2012-10-12 12:30:07 -0700478
Dianne Hackborn5dc5a002012-09-15 19:33:48 -0700479 try {
480 ActivityManagerNative.getDefault().registerUserSwitchObserver(
481 new IUserSwitchObserver.Stub() {
482 @Override
483 public void onUserSwitching(int newUserId, IRemoteCallback reply) {
484 switchUser(newUserId, reply);
485 }
486
487 @Override
488 public void onUserSwitchComplete(int newUserId) throws RemoteException {
489 }
490 });
491 } catch (RemoteException e) {
492 // TODO Auto-generated catch block
493 e.printStackTrace();
494 }
Amith Yamasani37ce3a82012-02-06 12:04:42 -0800495 }
496
497 String getName() {
Dianne Hackborn5dc5a002012-09-15 19:33:48 -0700498 synchronized (mLock) {
499 return mWallpaperMap.get(0).name;
500 }
Amith Yamasani37ce3a82012-02-06 12:04:42 -0800501 }
502
Amith Yamasani756901d2012-10-12 12:30:07 -0700503 void onStoppingUser(int userId) {
504 if (userId < 1) return;
Amith Yamasani13593602012-03-22 16:16:17 -0700505 synchronized (mLock) {
506 WallpaperData wallpaper = mWallpaperMap.get(userId);
507 if (wallpaper != null) {
Amith Yamasani756901d2012-10-12 12:30:07 -0700508 if (wallpaper.wallpaperObserver != null) {
509 wallpaper.wallpaperObserver.stopWatching();
510 wallpaper.wallpaperObserver = null;
511 }
Amith Yamasani13593602012-03-22 16:16:17 -0700512 mWallpaperMap.remove(userId);
513 }
Amith Yamasani756901d2012-10-12 12:30:07 -0700514 }
515 }
516
517 void onRemoveUser(int userId) {
518 if (userId < 1) return;
519 synchronized (mLock) {
520 onStoppingUser(userId);
Amith Yamasani13593602012-03-22 16:16:17 -0700521 File wallpaperFile = new File(getWallpaperDir(userId), WALLPAPER);
522 wallpaperFile.delete();
523 File wallpaperInfoFile = new File(getWallpaperDir(userId), WALLPAPER_INFO);
524 wallpaperInfoFile.delete();
525 }
526 }
527
Dianne Hackborn5dc5a002012-09-15 19:33:48 -0700528 void switchUser(int userId, IRemoteCallback reply) {
Amith Yamasani37ce3a82012-02-06 12:04:42 -0800529 synchronized (mLock) {
530 mCurrentUserId = userId;
531 WallpaperData wallpaper = mWallpaperMap.get(userId);
532 if (wallpaper == null) {
533 wallpaper = new WallpaperData(userId);
534 mWallpaperMap.put(userId, wallpaper);
535 loadSettingsLocked(userId);
Amith Yamasani6474c4c2012-10-04 14:55:42 -0700536 }
537 // Not started watching yet, in case wallpaper data was loaded for other reasons.
538 if (wallpaper.wallpaperObserver == null) {
Amith Yamasani37ce3a82012-02-06 12:04:42 -0800539 wallpaper.wallpaperObserver = new WallpaperObserver(wallpaper);
540 wallpaper.wallpaperObserver.startWatching();
541 }
Dianne Hackborn5dc5a002012-09-15 19:33:48 -0700542 switchWallpaper(wallpaper, reply);
Amith Yamasani37ce3a82012-02-06 12:04:42 -0800543 }
544 }
545
Dianne Hackborn5dc5a002012-09-15 19:33:48 -0700546 void switchWallpaper(WallpaperData wallpaper, IRemoteCallback reply) {
Dianne Hackbornf21adf62009-08-13 10:20:21 -0700547 synchronized (mLock) {
Dianne Hackborn80b902f2011-09-15 15:15:27 -0700548 RuntimeException e = null;
Dianne Hackbornf21adf62009-08-13 10:20:21 -0700549 try {
Amith Yamasani37ce3a82012-02-06 12:04:42 -0800550 ComponentName cname = wallpaper.wallpaperComponent != null ?
551 wallpaper.wallpaperComponent : wallpaper.nextWallpaperComponent;
Dianne Hackborn5dc5a002012-09-15 19:33:48 -0700552 if (bindWallpaperComponentLocked(cname, true, false, wallpaper, reply)) {
Dianne Hackborn80b902f2011-09-15 15:15:27 -0700553 return;
Dianne Hackbornf21adf62009-08-13 10:20:21 -0700554 }
Dianne Hackborn80b902f2011-09-15 15:15:27 -0700555 } catch (RuntimeException e1) {
556 e = e1;
Dianne Hackbornf21adf62009-08-13 10:20:21 -0700557 }
Dianne Hackborn80b902f2011-09-15 15:15:27 -0700558 Slog.w(TAG, "Failure starting previous wallpaper", e);
Dianne Hackborn5dc5a002012-09-15 19:33:48 -0700559 clearWallpaperLocked(false, wallpaper.userId, reply);
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800560 }
561 }
562
Amith Yamasani37ce3a82012-02-06 12:04:42 -0800563 public void clearWallpaper() {
564 if (DEBUG) Slog.v(TAG, "clearWallpaper");
565 synchronized (mLock) {
Dianne Hackborn5dc5a002012-09-15 19:33:48 -0700566 clearWallpaperLocked(false, UserHandle.getCallingUserId(), null);
Amith Yamasani37ce3a82012-02-06 12:04:42 -0800567 }
568 }
569
Dianne Hackborn5dc5a002012-09-15 19:33:48 -0700570 void clearWallpaperLocked(boolean defaultFailed, int userId, IRemoteCallback reply) {
Amith Yamasani37ce3a82012-02-06 12:04:42 -0800571 WallpaperData wallpaper = mWallpaperMap.get(userId);
572 File f = new File(getWallpaperDir(userId), WALLPAPER);
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800573 if (f.exists()) {
574 f.delete();
575 }
576 final long ident = Binder.clearCallingIdentity();
Dianne Hackborn80b902f2011-09-15 15:15:27 -0700577 RuntimeException e = null;
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800578 try {
Amith Yamasani37ce3a82012-02-06 12:04:42 -0800579 wallpaper.imageWallpaperPending = false;
580 if (userId != mCurrentUserId) return;
Dianne Hackborn80b902f2011-09-15 15:15:27 -0700581 if (bindWallpaperComponentLocked(defaultFailed
Dianne Hackbornbce0cbb2012-10-05 11:06:53 -0700582 ? IMAGE_WALLPAPER
Dianne Hackborn5dc5a002012-09-15 19:33:48 -0700583 : null, true, false, wallpaper, reply)) {
Dianne Hackborn80b902f2011-09-15 15:15:27 -0700584 return;
585 }
586 } catch (IllegalArgumentException e1) {
587 e = e1;
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800588 } finally {
589 Binder.restoreCallingIdentity(ident);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800590 }
Dianne Hackborn80b902f2011-09-15 15:15:27 -0700591
592 // This can happen if the default wallpaper component doesn't
593 // exist. This should be a system configuration problem, but
594 // let's not let it crash the system and just live with no
595 // wallpaper.
596 Slog.e(TAG, "Default wallpaper component not found!", e);
Amith Yamasani37ce3a82012-02-06 12:04:42 -0800597 clearWallpaperComponentLocked(wallpaper);
Dianne Hackborn5dc5a002012-09-15 19:33:48 -0700598 if (reply != null) {
599 try {
600 reply.sendResult(null);
601 } catch (RemoteException e1) {
602 }
603 }
604 }
605
606 public boolean hasNamedWallpaper(String name) {
607 synchronized (mLock) {
Amith Yamasani6474c4c2012-10-04 14:55:42 -0700608 List<UserInfo> users;
609 long ident = Binder.clearCallingIdentity();
610 try {
611 users = ((UserManager) mContext.getSystemService(Context.USER_SERVICE)).getUsers();
612 } finally {
613 Binder.restoreCallingIdentity(ident);
614 }
615 for (UserInfo user: users) {
616 WallpaperData wd = mWallpaperMap.get(user.id);
617 if (wd == null) {
618 // User hasn't started yet, so load her settings to peek at the wallpaper
619 loadSettingsLocked(user.id);
620 wd = mWallpaperMap.get(user.id);
621 }
622 if (wd != null && name.equals(wd.name)) {
Dianne Hackborn5dc5a002012-09-15 19:33:48 -0700623 return true;
624 }
625 }
626 }
627 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800628 }
629
630 public void setDimensionHints(int width, int height) throws RemoteException {
631 checkPermission(android.Manifest.permission.SET_WALLPAPER_HINTS);
Joe Onorato9bb8fd72009-07-28 18:24:51 -0700632 synchronized (mLock) {
Dianne Hackborn5dc5a002012-09-15 19:33:48 -0700633 int userId = UserHandle.getCallingUserId();
634 WallpaperData wallpaper = mWallpaperMap.get(userId);
635 if (wallpaper == null) {
636 throw new IllegalStateException("Wallpaper not yet initialized for user " + userId);
637 }
638 if (width <= 0 || height <= 0) {
639 throw new IllegalArgumentException("width and height must be > 0");
640 }
641
Amith Yamasani37ce3a82012-02-06 12:04:42 -0800642 if (width != wallpaper.width || height != wallpaper.height) {
643 wallpaper.width = width;
644 wallpaper.height = height;
645 saveSettingsLocked(wallpaper);
646 if (mCurrentUserId != userId) return; // Don't change the properties now
647 if (wallpaper.connection != null) {
648 if (wallpaper.connection.mEngine != null) {
Dianne Hackborn284ac932009-08-28 10:34:25 -0700649 try {
Amith Yamasani37ce3a82012-02-06 12:04:42 -0800650 wallpaper.connection.mEngine.setDesiredSize(
Dianne Hackborn284ac932009-08-28 10:34:25 -0700651 width, height);
652 } catch (RemoteException e) {
653 }
Amith Yamasani37ce3a82012-02-06 12:04:42 -0800654 notifyCallbacksLocked(wallpaper);
Dianne Hackborn284ac932009-08-28 10:34:25 -0700655 }
656 }
Joe Onorato9bb8fd72009-07-28 18:24:51 -0700657 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800658 }
659 }
660
661 public int getWidthHint() throws RemoteException {
Joe Onorato9bb8fd72009-07-28 18:24:51 -0700662 synchronized (mLock) {
Dianne Hackbornf02b60a2012-08-16 10:48:27 -0700663 WallpaperData wallpaper = mWallpaperMap.get(UserHandle.getCallingUserId());
Amith Yamasani37ce3a82012-02-06 12:04:42 -0800664 return wallpaper.width;
Joe Onorato9bb8fd72009-07-28 18:24:51 -0700665 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800666 }
667
668 public int getHeightHint() throws RemoteException {
Joe Onorato9bb8fd72009-07-28 18:24:51 -0700669 synchronized (mLock) {
Dianne Hackbornf02b60a2012-08-16 10:48:27 -0700670 WallpaperData wallpaper = mWallpaperMap.get(UserHandle.getCallingUserId());
Amith Yamasani37ce3a82012-02-06 12:04:42 -0800671 return wallpaper.height;
Joe Onorato9bb8fd72009-07-28 18:24:51 -0700672 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800673 }
674
Dianne Hackborn284ac932009-08-28 10:34:25 -0700675 public ParcelFileDescriptor getWallpaper(IWallpaperManagerCallback cb,
676 Bundle outParams) {
Joe Onorato9bb8fd72009-07-28 18:24:51 -0700677 synchronized (mLock) {
Amith Yamasani37ce3a82012-02-06 12:04:42 -0800678 // This returns the current user's wallpaper, if called by a system service. Else it
679 // returns the wallpaper for the calling user.
680 int callingUid = Binder.getCallingUid();
681 int wallpaperUserId = 0;
682 if (callingUid == android.os.Process.SYSTEM_UID) {
683 wallpaperUserId = mCurrentUserId;
684 } else {
Dianne Hackbornf02b60a2012-08-16 10:48:27 -0700685 wallpaperUserId = UserHandle.getUserId(callingUid);
Amith Yamasani37ce3a82012-02-06 12:04:42 -0800686 }
687 WallpaperData wallpaper = mWallpaperMap.get(wallpaperUserId);
Joe Onorato9bb8fd72009-07-28 18:24:51 -0700688 try {
Dianne Hackborn284ac932009-08-28 10:34:25 -0700689 if (outParams != null) {
Amith Yamasani37ce3a82012-02-06 12:04:42 -0800690 outParams.putInt("width", wallpaper.width);
691 outParams.putInt("height", wallpaper.height);
Dianne Hackborn284ac932009-08-28 10:34:25 -0700692 }
Amith Yamasani37ce3a82012-02-06 12:04:42 -0800693 wallpaper.callbacks.register(cb);
694 File f = new File(getWallpaperDir(wallpaperUserId), WALLPAPER);
Joe Onorato9bb8fd72009-07-28 18:24:51 -0700695 if (!f.exists()) {
696 return null;
697 }
698 return ParcelFileDescriptor.open(f, MODE_READ_ONLY);
699 } catch (FileNotFoundException e) {
700 /* Shouldn't happen as we check to see if the file exists */
Joe Onorato8a9b2202010-02-26 18:56:32 -0800701 Slog.w(TAG, "Error getting wallpaper", e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800702 }
Joe Onorato9bb8fd72009-07-28 18:24:51 -0700703 return null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800704 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800705 }
706
Dianne Hackborneb034652009-09-07 00:49:58 -0700707 public WallpaperInfo getWallpaperInfo() {
Dianne Hackbornf02b60a2012-08-16 10:48:27 -0700708 int userId = UserHandle.getCallingUserId();
Dianne Hackborneb034652009-09-07 00:49:58 -0700709 synchronized (mLock) {
Amith Yamasani37ce3a82012-02-06 12:04:42 -0800710 WallpaperData wallpaper = mWallpaperMap.get(userId);
711 if (wallpaper.connection != null) {
712 return wallpaper.connection.mInfo;
Dianne Hackborneb034652009-09-07 00:49:58 -0700713 }
714 return null;
715 }
716 }
Amith Yamasani37ce3a82012-02-06 12:04:42 -0800717
Joe Onorato9bb8fd72009-07-28 18:24:51 -0700718 public ParcelFileDescriptor setWallpaper(String name) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800719 checkPermission(android.Manifest.permission.SET_WALLPAPER);
Joe Onorato9bb8fd72009-07-28 18:24:51 -0700720 synchronized (mLock) {
Dianne Hackborn5dc5a002012-09-15 19:33:48 -0700721 if (DEBUG) Slog.v(TAG, "setWallpaper");
722 int userId = UserHandle.getCallingUserId();
723 WallpaperData wallpaper = mWallpaperMap.get(userId);
724 if (wallpaper == null) {
725 throw new IllegalStateException("Wallpaper not yet initialized for user " + userId);
726 }
Dianne Hackborn0cd48872009-08-13 18:51:59 -0700727 final long ident = Binder.clearCallingIdentity();
728 try {
Amith Yamasani37ce3a82012-02-06 12:04:42 -0800729 ParcelFileDescriptor pfd = updateWallpaperBitmapLocked(name, wallpaper);
Dianne Hackborn0cd48872009-08-13 18:51:59 -0700730 if (pfd != null) {
Amith Yamasani37ce3a82012-02-06 12:04:42 -0800731 wallpaper.imageWallpaperPending = true;
Dianne Hackborn0cd48872009-08-13 18:51:59 -0700732 }
733 return pfd;
734 } finally {
735 Binder.restoreCallingIdentity(ident);
Joe Onorato9bb8fd72009-07-28 18:24:51 -0700736 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800737 }
738 }
739
Amith Yamasani37ce3a82012-02-06 12:04:42 -0800740 ParcelFileDescriptor updateWallpaperBitmapLocked(String name, WallpaperData wallpaper) {
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700741 if (name == null) name = "";
742 try {
Amith Yamasani37ce3a82012-02-06 12:04:42 -0800743 File dir = getWallpaperDir(wallpaper.userId);
744 if (!dir.exists()) {
745 dir.mkdir();
Dianne Hackbornebac48c2011-11-29 18:01:50 -0800746 FileUtils.setPermissions(
Amith Yamasani37ce3a82012-02-06 12:04:42 -0800747 dir.getPath(),
Dianne Hackbornebac48c2011-11-29 18:01:50 -0800748 FileUtils.S_IRWXU|FileUtils.S_IRWXG|FileUtils.S_IXOTH,
749 -1, -1);
750 }
rpcraig554cb0c2012-07-05 06:41:43 -0400751 File file = new File(dir, WALLPAPER);
752 ParcelFileDescriptor fd = ParcelFileDescriptor.open(file,
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700753 MODE_CREATE|MODE_READ_WRITE);
rpcraig554cb0c2012-07-05 06:41:43 -0400754 if (!SELinux.restorecon(file)) {
755 return null;
756 }
Amith Yamasani37ce3a82012-02-06 12:04:42 -0800757 wallpaper.name = name;
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700758 return fd;
759 } catch (FileNotFoundException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800760 Slog.w(TAG, "Error setting wallpaper", e);
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700761 }
762 return null;
763 }
764
765 public void setWallpaperComponent(ComponentName name) {
766 checkPermission(android.Manifest.permission.SET_WALLPAPER_COMPONENT);
767 synchronized (mLock) {
Dianne Hackborn5dc5a002012-09-15 19:33:48 -0700768 if (DEBUG) Slog.v(TAG, "setWallpaperComponent name=" + name);
769 int userId = UserHandle.getCallingUserId();
770 WallpaperData wallpaper = mWallpaperMap.get(userId);
771 if (wallpaper == null) {
772 throw new IllegalStateException("Wallpaper not yet initialized for user " + userId);
773 }
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700774 final long ident = Binder.clearCallingIdentity();
775 try {
Amith Yamasani37ce3a82012-02-06 12:04:42 -0800776 wallpaper.imageWallpaperPending = false;
Dianne Hackborn5dc5a002012-09-15 19:33:48 -0700777 bindWallpaperComponentLocked(name, false, true, wallpaper, null);
Dianne Hackbornf21adf62009-08-13 10:20:21 -0700778 } finally {
779 Binder.restoreCallingIdentity(ident);
780 }
781 }
782 }
783
Amith Yamasani37ce3a82012-02-06 12:04:42 -0800784 boolean bindWallpaperComponentLocked(ComponentName componentName, boolean force,
Dianne Hackborn5dc5a002012-09-15 19:33:48 -0700785 boolean fromUser, WallpaperData wallpaper, IRemoteCallback reply) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800786 if (DEBUG) Slog.v(TAG, "bindWallpaperComponentLocked: componentName=" + componentName);
Dianne Hackbornf21adf62009-08-13 10:20:21 -0700787 // Has the component changed?
Dianne Hackborn9ea31632011-08-05 14:43:50 -0700788 if (!force) {
Amith Yamasani37ce3a82012-02-06 12:04:42 -0800789 if (wallpaper.connection != null) {
790 if (wallpaper.wallpaperComponent == null) {
Dianne Hackborn9ea31632011-08-05 14:43:50 -0700791 if (componentName == null) {
792 if (DEBUG) Slog.v(TAG, "bindWallpaperComponentLocked: still using default");
793 // Still using default wallpaper.
Dianne Hackborn80b902f2011-09-15 15:15:27 -0700794 return true;
Dianne Hackborn9ea31632011-08-05 14:43:50 -0700795 }
Amith Yamasani37ce3a82012-02-06 12:04:42 -0800796 } else if (wallpaper.wallpaperComponent.equals(componentName)) {
Dianne Hackborn9ea31632011-08-05 14:43:50 -0700797 // Changing to same wallpaper.
798 if (DEBUG) Slog.v(TAG, "same wallpaper");
Dianne Hackborn80b902f2011-09-15 15:15:27 -0700799 return true;
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700800 }
Dianne Hackbornf21adf62009-08-13 10:20:21 -0700801 }
802 }
803
804 try {
Mike Clerona428b2c2009-11-15 22:53:08 -0800805 if (componentName == null) {
Mike Cleron322b6ee2009-11-12 07:45:47 -0800806 String defaultComponent =
807 mContext.getString(com.android.internal.R.string.default_wallpaper_component);
Mike Clerona428b2c2009-11-15 22:53:08 -0800808 if (defaultComponent != null) {
Mike Cleron322b6ee2009-11-12 07:45:47 -0800809 // See if there is a default wallpaper component specified
Mike Clerona428b2c2009-11-15 22:53:08 -0800810 componentName = ComponentName.unflattenFromString(defaultComponent);
Joe Onorato8a9b2202010-02-26 18:56:32 -0800811 if (DEBUG) Slog.v(TAG, "Use default component wallpaper:" + componentName);
Mike Cleron322b6ee2009-11-12 07:45:47 -0800812 }
Mike Clerona428b2c2009-11-15 22:53:08 -0800813 if (componentName == null) {
Mike Cleron322b6ee2009-11-12 07:45:47 -0800814 // Fall back to static image wallpaper
Dianne Hackbornbce0cbb2012-10-05 11:06:53 -0700815 componentName = IMAGE_WALLPAPER;
Mike Cleron322b6ee2009-11-12 07:45:47 -0800816 //clearWallpaperComponentLocked();
817 //return;
Joe Onorato8a9b2202010-02-26 18:56:32 -0800818 if (DEBUG) Slog.v(TAG, "Using image wallpaper");
Mike Cleron322b6ee2009-11-12 07:45:47 -0800819 }
Dianne Hackbornf21adf62009-08-13 10:20:21 -0700820 }
Amith Yamasani4e2820c2012-08-28 22:17:23 -0700821 int serviceUserId = wallpaper.userId;
822 ServiceInfo si = mIPackageManager.getServiceInfo(componentName,
823 PackageManager.GET_META_DATA | PackageManager.GET_PERMISSIONS, serviceUserId);
Christopher Tate90952202013-09-08 13:01:28 -0700824 if (si == null) {
825 // The wallpaper component we're trying to use doesn't exist
826 Slog.w(TAG, "Attempted wallpaper " + componentName + " is unavailable");
827 return false;
828 }
Dianne Hackbornf21adf62009-08-13 10:20:21 -0700829 if (!android.Manifest.permission.BIND_WALLPAPER.equals(si.permission)) {
Dianne Hackborn80b902f2011-09-15 15:15:27 -0700830 String msg = "Selected service does not require "
Dianne Hackbornf21adf62009-08-13 10:20:21 -0700831 + android.Manifest.permission.BIND_WALLPAPER
Dianne Hackborn80b902f2011-09-15 15:15:27 -0700832 + ": " + componentName;
833 if (fromUser) {
834 throw new SecurityException(msg);
835 }
836 Slog.w(TAG, msg);
837 return false;
Dianne Hackbornf21adf62009-08-13 10:20:21 -0700838 }
839
Dianne Hackborneb034652009-09-07 00:49:58 -0700840 WallpaperInfo wi = null;
841
Dianne Hackbornf21adf62009-08-13 10:20:21 -0700842 Intent intent = new Intent(WallpaperService.SERVICE_INTERFACE);
Dianne Hackbornbce0cbb2012-10-05 11:06:53 -0700843 if (componentName != null && !componentName.equals(IMAGE_WALLPAPER)) {
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700844 // Make sure the selected service is actually a wallpaper service.
Amith Yamasani4e2820c2012-08-28 22:17:23 -0700845 List<ResolveInfo> ris =
846 mIPackageManager.queryIntentServices(intent,
847 intent.resolveTypeIfNeeded(mContext.getContentResolver()),
848 PackageManager.GET_META_DATA, serviceUserId);
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700849 for (int i=0; i<ris.size(); i++) {
850 ServiceInfo rsi = ris.get(i).serviceInfo;
851 if (rsi.name.equals(si.name) &&
852 rsi.packageName.equals(si.packageName)) {
Dianne Hackborneb034652009-09-07 00:49:58 -0700853 try {
854 wi = new WallpaperInfo(mContext, ris.get(i));
855 } catch (XmlPullParserException e) {
Dianne Hackborn80b902f2011-09-15 15:15:27 -0700856 if (fromUser) {
857 throw new IllegalArgumentException(e);
858 }
859 Slog.w(TAG, e);
860 return false;
Dianne Hackborneb034652009-09-07 00:49:58 -0700861 } catch (IOException e) {
Dianne Hackborn80b902f2011-09-15 15:15:27 -0700862 if (fromUser) {
863 throw new IllegalArgumentException(e);
864 }
865 Slog.w(TAG, e);
866 return false;
Dianne Hackborneb034652009-09-07 00:49:58 -0700867 }
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700868 break;
869 }
870 }
Dianne Hackborneb034652009-09-07 00:49:58 -0700871 if (wi == null) {
Dianne Hackborn80b902f2011-09-15 15:15:27 -0700872 String msg = "Selected service is not a wallpaper: "
873 + componentName;
874 if (fromUser) {
875 throw new SecurityException(msg);
876 }
877 Slog.w(TAG, msg);
878 return false;
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700879 }
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700880 }
Dianne Hackbornf21adf62009-08-13 10:20:21 -0700881
882 // Bind the service!
Joe Onorato8a9b2202010-02-26 18:56:32 -0800883 if (DEBUG) Slog.v(TAG, "Binding to:" + componentName);
Amith Yamasani37ce3a82012-02-06 12:04:42 -0800884 WallpaperConnection newConn = new WallpaperConnection(wi, wallpaper);
Mike Clerona428b2c2009-11-15 22:53:08 -0800885 intent.setComponent(componentName);
Dianne Hackborndd9b82c2009-09-03 00:18:47 -0700886 intent.putExtra(Intent.EXTRA_CLIENT_LABEL,
887 com.android.internal.R.string.wallpaper_binding_label);
Dianne Hackborn41203752012-08-31 14:05:51 -0700888 intent.putExtra(Intent.EXTRA_CLIENT_INTENT, PendingIntent.getActivityAsUser(
Dianne Hackborneb034652009-09-07 00:49:58 -0700889 mContext, 0,
890 Intent.createChooser(new Intent(Intent.ACTION_SET_WALLPAPER),
891 mContext.getText(com.android.internal.R.string.chooser_wallpaper)),
Dianne Hackborn41203752012-08-31 14:05:51 -0700892 0, null, new UserHandle(serviceUserId)));
Dianne Hackbornc8230512013-07-13 21:32:12 -0700893 if (!mContext.bindServiceAsUser(intent, newConn,
894 Context.BIND_AUTO_CREATE | Context.BIND_SHOWING_UI,
Amith Yamasani27b89e62013-01-16 12:30:11 -0800895 new UserHandle(serviceUserId))) {
Dianne Hackborn80b902f2011-09-15 15:15:27 -0700896 String msg = "Unable to bind service: "
897 + componentName;
898 if (fromUser) {
899 throw new IllegalArgumentException(msg);
900 }
901 Slog.w(TAG, msg);
902 return false;
Dianne Hackbornf21adf62009-08-13 10:20:21 -0700903 }
Amith Yamasani37ce3a82012-02-06 12:04:42 -0800904 if (wallpaper.userId == mCurrentUserId && mLastWallpaper != null) {
905 detachWallpaperLocked(mLastWallpaper);
906 }
907 wallpaper.wallpaperComponent = componentName;
908 wallpaper.connection = newConn;
909 wallpaper.lastDiedTime = SystemClock.uptimeMillis();
Dianne Hackborn5dc5a002012-09-15 19:33:48 -0700910 newConn.mReply = reply;
Dianne Hackbornf21adf62009-08-13 10:20:21 -0700911 try {
Amith Yamasani37ce3a82012-02-06 12:04:42 -0800912 if (wallpaper.userId == mCurrentUserId) {
913 if (DEBUG)
914 Slog.v(TAG, "Adding window token: " + newConn.mToken);
915 mIWindowManager.addWindowToken(newConn.mToken,
916 WindowManager.LayoutParams.TYPE_WALLPAPER);
917 mLastWallpaper = wallpaper;
918 }
Dianne Hackbornf21adf62009-08-13 10:20:21 -0700919 } catch (RemoteException e) {
920 }
Amith Yamasani4e2820c2012-08-28 22:17:23 -0700921 } catch (RemoteException e) {
922 String msg = "Remote exception for " + componentName + "\n" + e;
Dianne Hackborn80b902f2011-09-15 15:15:27 -0700923 if (fromUser) {
924 throw new IllegalArgumentException(msg);
925 }
926 Slog.w(TAG, msg);
927 return false;
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700928 }
Dianne Hackborn80b902f2011-09-15 15:15:27 -0700929 return true;
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700930 }
Amith Yamasani37ce3a82012-02-06 12:04:42 -0800931
932 void detachWallpaperLocked(WallpaperData wallpaper) {
933 if (wallpaper.connection != null) {
Dianne Hackborn5dc5a002012-09-15 19:33:48 -0700934 if (wallpaper.connection.mReply != null) {
935 try {
936 wallpaper.connection.mReply.sendResult(null);
937 } catch (RemoteException e) {
938 }
939 wallpaper.connection.mReply = null;
940 }
Amith Yamasani37ce3a82012-02-06 12:04:42 -0800941 if (wallpaper.connection.mEngine != null) {
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700942 try {
Amith Yamasani37ce3a82012-02-06 12:04:42 -0800943 wallpaper.connection.mEngine.destroy();
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700944 } catch (RemoteException e) {
945 }
946 }
Amith Yamasani37ce3a82012-02-06 12:04:42 -0800947 mContext.unbindService(wallpaper.connection);
Dianne Hackborne9e9bca2009-08-18 15:08:22 -0700948 try {
Amith Yamasani37ce3a82012-02-06 12:04:42 -0800949 if (DEBUG)
950 Slog.v(TAG, "Removing window token: " + wallpaper.connection.mToken);
951 mIWindowManager.removeWindowToken(wallpaper.connection.mToken);
Dianne Hackborne9e9bca2009-08-18 15:08:22 -0700952 } catch (RemoteException e) {
953 }
Amith Yamasani37ce3a82012-02-06 12:04:42 -0800954 wallpaper.connection.mService = null;
955 wallpaper.connection.mEngine = null;
956 wallpaper.connection = null;
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700957 }
958 }
Amith Yamasani37ce3a82012-02-06 12:04:42 -0800959
960 void clearWallpaperComponentLocked(WallpaperData wallpaper) {
961 wallpaper.wallpaperComponent = null;
962 detachWallpaperLocked(wallpaper);
963 }
964
965 void attachServiceLocked(WallpaperConnection conn, WallpaperData wallpaper) {
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700966 try {
Dianne Hackborn3be63c02009-08-20 19:31:38 -0700967 conn.mService.attach(conn, conn.mToken,
968 WindowManager.LayoutParams.TYPE_WALLPAPER, false,
Amith Yamasani37ce3a82012-02-06 12:04:42 -0800969 wallpaper.width, wallpaper.height);
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700970 } catch (RemoteException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800971 Slog.w(TAG, "Failed attaching wallpaper; clearing", e);
Amith Yamasani37ce3a82012-02-06 12:04:42 -0800972 if (!wallpaper.wallpaperUpdating) {
Dianne Hackborn5dc5a002012-09-15 19:33:48 -0700973 bindWallpaperComponentLocked(null, false, false, wallpaper, null);
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800974 }
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700975 }
976 }
Amith Yamasani37ce3a82012-02-06 12:04:42 -0800977
978 private void notifyCallbacksLocked(WallpaperData wallpaper) {
979 final int n = wallpaper.callbacks.beginBroadcast();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800980 for (int i = 0; i < n; i++) {
981 try {
Amith Yamasani37ce3a82012-02-06 12:04:42 -0800982 wallpaper.callbacks.getBroadcastItem(i).onWallpaperChanged();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800983 } catch (RemoteException e) {
984
985 // The RemoteCallbackList will take care of removing
986 // the dead object for us.
987 }
988 }
Amith Yamasani37ce3a82012-02-06 12:04:42 -0800989 wallpaper.callbacks.finishBroadcast();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800990 final Intent intent = new Intent(Intent.ACTION_WALLPAPER_CHANGED);
Dianne Hackborn5ac72a22012-08-29 18:32:08 -0700991 mContext.sendBroadcastAsUser(intent, new UserHandle(mCurrentUserId));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800992 }
993
994 private void checkPermission(String permission) {
Joe Onorato9bb8fd72009-07-28 18:24:51 -0700995 if (PackageManager.PERMISSION_GRANTED!= mContext.checkCallingOrSelfPermission(permission)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800996 throw new SecurityException("Access denied to process: " + Binder.getCallingPid()
997 + ", must have permission " + permission);
998 }
999 }
Joe Onorato9bb8fd72009-07-28 18:24:51 -07001000
Amith Yamasani37ce3a82012-02-06 12:04:42 -08001001 private static JournaledFile makeJournaledFile(int userId) {
Amith Yamasani61f57372012-08-31 12:12:28 -07001002 final String base = new File(getWallpaperDir(userId), WALLPAPER_INFO).getAbsolutePath();
Joe Onorato9bb8fd72009-07-28 18:24:51 -07001003 return new JournaledFile(new File(base), new File(base + ".tmp"));
1004 }
1005
Amith Yamasani37ce3a82012-02-06 12:04:42 -08001006 private void saveSettingsLocked(WallpaperData wallpaper) {
1007 JournaledFile journal = makeJournaledFile(wallpaper.userId);
Joe Onorato9bb8fd72009-07-28 18:24:51 -07001008 FileOutputStream stream = null;
1009 try {
1010 stream = new FileOutputStream(journal.chooseForWrite(), false);
1011 XmlSerializer out = new FastXmlSerializer();
1012 out.setOutput(stream, "utf-8");
1013 out.startDocument(null, true);
1014
1015 out.startTag(null, "wp");
Amith Yamasani37ce3a82012-02-06 12:04:42 -08001016 out.attribute(null, "width", Integer.toString(wallpaper.width));
1017 out.attribute(null, "height", Integer.toString(wallpaper.height));
1018 out.attribute(null, "name", wallpaper.name);
1019 if (wallpaper.wallpaperComponent != null
Dianne Hackbornbce0cbb2012-10-05 11:06:53 -07001020 && !wallpaper.wallpaperComponent.equals(IMAGE_WALLPAPER)) {
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001021 out.attribute(null, "component",
Amith Yamasani37ce3a82012-02-06 12:04:42 -08001022 wallpaper.wallpaperComponent.flattenToShortString());
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001023 }
Joe Onorato9bb8fd72009-07-28 18:24:51 -07001024 out.endTag(null, "wp");
1025
1026 out.endDocument();
1027 stream.close();
1028 journal.commit();
1029 } catch (IOException e) {
1030 try {
1031 if (stream != null) {
1032 stream.close();
1033 }
1034 } catch (IOException ex) {
1035 // Ignore
1036 }
1037 journal.rollback();
1038 }
1039 }
1040
Amith Yamasani37ce3a82012-02-06 12:04:42 -08001041 private void migrateFromOld() {
1042 File oldWallpaper = new File(WallpaperBackupHelper.WALLPAPER_IMAGE_KEY);
1043 File oldInfo = new File(WallpaperBackupHelper.WALLPAPER_INFO_KEY);
1044 if (oldWallpaper.exists()) {
1045 File newWallpaper = new File(getWallpaperDir(0), WALLPAPER);
1046 oldWallpaper.renameTo(newWallpaper);
1047 }
1048 if (oldInfo.exists()) {
1049 File newInfo = new File(getWallpaperDir(0), WALLPAPER_INFO);
1050 oldInfo.renameTo(newInfo);
1051 }
1052 }
1053
1054 private void loadSettingsLocked(int userId) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001055 if (DEBUG) Slog.v(TAG, "loadSettingsLocked");
Mike Clerona428b2c2009-11-15 22:53:08 -08001056
Amith Yamasani37ce3a82012-02-06 12:04:42 -08001057 JournaledFile journal = makeJournaledFile(userId);
Joe Onorato9bb8fd72009-07-28 18:24:51 -07001058 FileInputStream stream = null;
1059 File file = journal.chooseForRead();
Amith Yamasani37ce3a82012-02-06 12:04:42 -08001060 if (!file.exists()) {
1061 // This should only happen one time, when upgrading from a legacy system
1062 migrateFromOld();
1063 }
1064 WallpaperData wallpaper = mWallpaperMap.get(userId);
1065 if (wallpaper == null) {
1066 wallpaper = new WallpaperData(userId);
1067 mWallpaperMap.put(userId, wallpaper);
1068 }
Joe Onorato9bb8fd72009-07-28 18:24:51 -07001069 boolean success = false;
1070 try {
1071 stream = new FileInputStream(file);
1072 XmlPullParser parser = Xml.newPullParser();
1073 parser.setInput(stream, null);
1074
1075 int type;
Joe Onorato9bb8fd72009-07-28 18:24:51 -07001076 do {
1077 type = parser.next();
1078 if (type == XmlPullParser.START_TAG) {
1079 String tag = parser.getName();
1080 if ("wp".equals(tag)) {
Amith Yamasani37ce3a82012-02-06 12:04:42 -08001081 wallpaper.width = Integer.parseInt(parser.getAttributeValue(null, "width"));
1082 wallpaper.height = Integer.parseInt(parser
1083 .getAttributeValue(null, "height"));
1084 wallpaper.name = parser.getAttributeValue(null, "name");
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001085 String comp = parser.getAttributeValue(null, "component");
Amith Yamasani37ce3a82012-02-06 12:04:42 -08001086 wallpaper.nextWallpaperComponent = comp != null
Dianne Hackborn4c62fc02009-08-08 20:40:27 -07001087 ? ComponentName.unflattenFromString(comp)
1088 : null;
Amith Yamasani37ce3a82012-02-06 12:04:42 -08001089 if (wallpaper.nextWallpaperComponent == null
1090 || "android".equals(wallpaper.nextWallpaperComponent
1091 .getPackageName())) {
Dianne Hackbornbce0cbb2012-10-05 11:06:53 -07001092 wallpaper.nextWallpaperComponent = IMAGE_WALLPAPER;
Dianne Hackborn9ea31632011-08-05 14:43:50 -07001093 }
Mike Clerona428b2c2009-11-15 22:53:08 -08001094
1095 if (DEBUG) {
Amith Yamasani37ce3a82012-02-06 12:04:42 -08001096 Slog.v(TAG, "mWidth:" + wallpaper.width);
1097 Slog.v(TAG, "mHeight:" + wallpaper.height);
1098 Slog.v(TAG, "mName:" + wallpaper.name);
1099 Slog.v(TAG, "mNextWallpaperComponent:"
1100 + wallpaper.nextWallpaperComponent);
Mike Clerona428b2c2009-11-15 22:53:08 -08001101 }
Joe Onorato9bb8fd72009-07-28 18:24:51 -07001102 }
1103 }
1104 } while (type != XmlPullParser.END_DOCUMENT);
1105 success = true;
Dianne Hackborn13579ed2012-11-28 18:05:36 -08001106 } catch (FileNotFoundException e) {
1107 Slog.w(TAG, "no current wallpaper -- first boot?");
Joe Onorato9bb8fd72009-07-28 18:24:51 -07001108 } catch (NullPointerException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001109 Slog.w(TAG, "failed parsing " + file + " " + e);
Joe Onorato9bb8fd72009-07-28 18:24:51 -07001110 } catch (NumberFormatException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001111 Slog.w(TAG, "failed parsing " + file + " " + e);
Joe Onorato9bb8fd72009-07-28 18:24:51 -07001112 } catch (XmlPullParserException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001113 Slog.w(TAG, "failed parsing " + file + " " + e);
Joe Onorato9bb8fd72009-07-28 18:24:51 -07001114 } catch (IOException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001115 Slog.w(TAG, "failed parsing " + file + " " + e);
Joe Onorato9bb8fd72009-07-28 18:24:51 -07001116 } catch (IndexOutOfBoundsException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001117 Slog.w(TAG, "failed parsing " + file + " " + e);
Joe Onorato9bb8fd72009-07-28 18:24:51 -07001118 }
1119 try {
1120 if (stream != null) {
1121 stream.close();
1122 }
1123 } catch (IOException e) {
1124 // Ignore
1125 }
1126
1127 if (!success) {
Amith Yamasani37ce3a82012-02-06 12:04:42 -08001128 wallpaper.width = -1;
1129 wallpaper.height = -1;
1130 wallpaper.name = "";
Joe Onorato9bb8fd72009-07-28 18:24:51 -07001131 }
Dianne Hackborn44bc17c2011-04-20 18:18:51 -07001132
1133 // We always want to have some reasonable width hint.
1134 WindowManager wm = (WindowManager)mContext.getSystemService(Context.WINDOW_SERVICE);
1135 Display d = wm.getDefaultDisplay();
1136 int baseSize = d.getMaximumSizeDimension();
Amith Yamasani37ce3a82012-02-06 12:04:42 -08001137 if (wallpaper.width < baseSize) {
1138 wallpaper.width = baseSize;
Dianne Hackborn44bc17c2011-04-20 18:18:51 -07001139 }
Amith Yamasani37ce3a82012-02-06 12:04:42 -08001140 if (wallpaper.height < baseSize) {
1141 wallpaper.height = baseSize;
Dianne Hackborn44bc17c2011-04-20 18:18:51 -07001142 }
Joe Onorato9bb8fd72009-07-28 18:24:51 -07001143 }
1144
Brad Fitzpatrick194b19a2010-09-14 11:30:29 -07001145 // Called by SystemBackupAgent after files are restored to disk.
Joe Onorato9bb8fd72009-07-28 18:24:51 -07001146 void settingsRestored() {
Amith Yamasani37ce3a82012-02-06 12:04:42 -08001147 // TODO: If necessary, make it work for secondary users as well. This currently assumes
1148 // restores only to the primary user
Joe Onorato8a9b2202010-02-26 18:56:32 -08001149 if (DEBUG) Slog.v(TAG, "settingsRestored");
Amith Yamasani37ce3a82012-02-06 12:04:42 -08001150 WallpaperData wallpaper = null;
Joe Onorato9bb8fd72009-07-28 18:24:51 -07001151 boolean success = false;
1152 synchronized (mLock) {
Amith Yamasani37ce3a82012-02-06 12:04:42 -08001153 loadSettingsLocked(0);
1154 wallpaper = mWallpaperMap.get(0);
1155 if (wallpaper.nextWallpaperComponent != null
Dianne Hackbornbce0cbb2012-10-05 11:06:53 -07001156 && !wallpaper.nextWallpaperComponent.equals(IMAGE_WALLPAPER)) {
Amith Yamasani37ce3a82012-02-06 12:04:42 -08001157 if (!bindWallpaperComponentLocked(wallpaper.nextWallpaperComponent, false, false,
Dianne Hackborn5dc5a002012-09-15 19:33:48 -07001158 wallpaper, null)) {
Christopher Tatee3ab4d02009-12-16 14:03:31 -08001159 // No such live wallpaper or other failure; fall back to the default
1160 // live wallpaper (since the profile being restored indicated that the
1161 // user had selected a live rather than static one).
Dianne Hackborn5dc5a002012-09-15 19:33:48 -07001162 bindWallpaperComponentLocked(null, false, false, wallpaper, null);
Christopher Tatee3ab4d02009-12-16 14:03:31 -08001163 }
Joe Onorato9bb8fd72009-07-28 18:24:51 -07001164 success = true;
1165 } else {
Mike Clerona428b2c2009-11-15 22:53:08 -08001166 // If there's a wallpaper name, we use that. If that can't be loaded, then we
1167 // use the default.
Amith Yamasani37ce3a82012-02-06 12:04:42 -08001168 if ("".equals(wallpaper.name)) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001169 if (DEBUG) Slog.v(TAG, "settingsRestored: name is empty");
Mike Clerona428b2c2009-11-15 22:53:08 -08001170 success = true;
1171 } else {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001172 if (DEBUG) Slog.v(TAG, "settingsRestored: attempting to restore named resource");
Amith Yamasani37ce3a82012-02-06 12:04:42 -08001173 success = restoreNamedResourceLocked(wallpaper);
Mike Clerona428b2c2009-11-15 22:53:08 -08001174 }
Joe Onorato8a9b2202010-02-26 18:56:32 -08001175 if (DEBUG) Slog.v(TAG, "settingsRestored: success=" + success);
Mike Clerona428b2c2009-11-15 22:53:08 -08001176 if (success) {
Amith Yamasani37ce3a82012-02-06 12:04:42 -08001177 bindWallpaperComponentLocked(wallpaper.nextWallpaperComponent, false, false,
Dianne Hackborn5dc5a002012-09-15 19:33:48 -07001178 wallpaper, null);
Mike Clerona428b2c2009-11-15 22:53:08 -08001179 }
Joe Onorato9bb8fd72009-07-28 18:24:51 -07001180 }
1181 }
1182
1183 if (!success) {
Amith Yamasani37ce3a82012-02-06 12:04:42 -08001184 Slog.e(TAG, "Failed to restore wallpaper: '" + wallpaper.name + "'");
1185 wallpaper.name = "";
1186 getWallpaperDir(0).delete();
Joe Onorato9bb8fd72009-07-28 18:24:51 -07001187 }
Brad Fitzpatrick194b19a2010-09-14 11:30:29 -07001188
1189 synchronized (mLock) {
Amith Yamasani37ce3a82012-02-06 12:04:42 -08001190 saveSettingsLocked(wallpaper);
Brad Fitzpatrick194b19a2010-09-14 11:30:29 -07001191 }
Joe Onorato9bb8fd72009-07-28 18:24:51 -07001192 }
1193
Amith Yamasani37ce3a82012-02-06 12:04:42 -08001194 boolean restoreNamedResourceLocked(WallpaperData wallpaper) {
1195 if (wallpaper.name.length() > 4 && "res:".equals(wallpaper.name.substring(0, 4))) {
1196 String resName = wallpaper.name.substring(4);
Joe Onorato9bb8fd72009-07-28 18:24:51 -07001197
1198 String pkg = null;
1199 int colon = resName.indexOf(':');
1200 if (colon > 0) {
1201 pkg = resName.substring(0, colon);
1202 }
1203
1204 String ident = null;
1205 int slash = resName.lastIndexOf('/');
1206 if (slash > 0) {
1207 ident = resName.substring(slash+1);
1208 }
1209
1210 String type = null;
1211 if (colon > 0 && slash > 0 && (slash-colon) > 1) {
1212 type = resName.substring(colon+1, slash);
1213 }
1214
1215 if (pkg != null && ident != null && type != null) {
1216 int resId = -1;
1217 InputStream res = null;
1218 FileOutputStream fos = null;
1219 try {
1220 Context c = mContext.createPackageContext(pkg, Context.CONTEXT_RESTRICTED);
1221 Resources r = c.getResources();
1222 resId = r.getIdentifier(resName, null, null);
1223 if (resId == 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001224 Slog.e(TAG, "couldn't resolve identifier pkg=" + pkg + " type=" + type
Joe Onorato9bb8fd72009-07-28 18:24:51 -07001225 + " ident=" + ident);
1226 return false;
1227 }
1228
1229 res = r.openRawResource(resId);
Amith Yamasani37ce3a82012-02-06 12:04:42 -08001230 if (wallpaper.wallpaperFile.exists()) {
1231 wallpaper.wallpaperFile.delete();
Dianne Hackborn1afd1c92010-03-18 22:47:17 -07001232 }
Amith Yamasani37ce3a82012-02-06 12:04:42 -08001233 fos = new FileOutputStream(wallpaper.wallpaperFile);
Joe Onorato9bb8fd72009-07-28 18:24:51 -07001234
1235 byte[] buffer = new byte[32768];
1236 int amt;
1237 while ((amt=res.read(buffer)) > 0) {
1238 fos.write(buffer, 0, amt);
1239 }
1240 // mWallpaperObserver will notice the close and send the change broadcast
1241
Joe Onorato8a9b2202010-02-26 18:56:32 -08001242 Slog.v(TAG, "Restored wallpaper: " + resName);
Joe Onorato9bb8fd72009-07-28 18:24:51 -07001243 return true;
1244 } catch (NameNotFoundException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001245 Slog.e(TAG, "Package name " + pkg + " not found");
Joe Onorato9bb8fd72009-07-28 18:24:51 -07001246 } catch (Resources.NotFoundException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001247 Slog.e(TAG, "Resource not found: " + resId);
Joe Onorato9bb8fd72009-07-28 18:24:51 -07001248 } catch (IOException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001249 Slog.e(TAG, "IOException while restoring wallpaper ", e);
Joe Onorato9bb8fd72009-07-28 18:24:51 -07001250 } finally {
1251 if (res != null) {
1252 try {
1253 res.close();
1254 } catch (IOException ex) {}
1255 }
1256 if (fos != null) {
Dianne Hackborn8bdf5932010-10-15 12:54:40 -07001257 FileUtils.sync(fos);
Joe Onorato9bb8fd72009-07-28 18:24:51 -07001258 try {
1259 fos.close();
1260 } catch (IOException ex) {}
1261 }
1262 }
1263 }
1264 }
1265 return false;
1266 }
Amith Yamasani37ce3a82012-02-06 12:04:42 -08001267
Dianne Hackborneb034652009-09-07 00:49:58 -07001268 @Override
1269 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
1270 if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DUMP)
1271 != PackageManager.PERMISSION_GRANTED) {
1272
1273 pw.println("Permission Denial: can't dump wallpaper service from from pid="
1274 + Binder.getCallingPid()
1275 + ", uid=" + Binder.getCallingUid());
1276 return;
1277 }
1278
1279 synchronized (mLock) {
1280 pw.println("Current Wallpaper Service state:");
Amith Yamasani37ce3a82012-02-06 12:04:42 -08001281 for (int i = 0; i < mWallpaperMap.size(); i++) {
1282 WallpaperData wallpaper = mWallpaperMap.valueAt(i);
1283 pw.println(" User " + wallpaper.userId + ":");
1284 pw.print(" mWidth=");
1285 pw.print(wallpaper.width);
1286 pw.print(" mHeight=");
1287 pw.println(wallpaper.height);
1288 pw.print(" mName=");
1289 pw.println(wallpaper.name);
1290 pw.print(" mWallpaperComponent=");
1291 pw.println(wallpaper.wallpaperComponent);
1292 if (wallpaper.connection != null) {
1293 WallpaperConnection conn = wallpaper.connection;
1294 pw.print(" Wallpaper connection ");
1295 pw.print(conn);
1296 pw.println(":");
1297 if (conn.mInfo != null) {
1298 pw.print(" mInfo.component=");
1299 pw.println(conn.mInfo.getComponent());
1300 }
1301 pw.print(" mToken=");
1302 pw.println(conn.mToken);
1303 pw.print(" mService=");
1304 pw.println(conn.mService);
1305 pw.print(" mEngine=");
1306 pw.println(conn.mEngine);
1307 pw.print(" mLastDiedTime=");
1308 pw.println(wallpaper.lastDiedTime - SystemClock.uptimeMillis());
1309 }
Dianne Hackborneb034652009-09-07 00:49:58 -07001310 }
1311 }
1312 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001313}