WallpaperManagerService.java revision ebebadb56d6b3eab6e11dae9d48d639f0af4946d
1/*
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.wallpaper;
18
19import static android.os.ParcelFileDescriptor.*;
20
21import android.app.ActivityManagerNative;
22import android.app.AppGlobals;
23import android.app.IUserSwitchObserver;
24import android.app.IWallpaperManager;
25import android.app.IWallpaperManagerCallback;
26import android.app.PendingIntent;
27import android.app.WallpaperInfo;
28import android.app.backup.BackupManager;
29import android.app.backup.WallpaperBackupHelper;
30import android.content.BroadcastReceiver;
31import android.content.ComponentName;
32import android.content.Context;
33import android.content.Intent;
34import android.content.IntentFilter;
35import android.content.ServiceConnection;
36import android.content.pm.IPackageManager;
37import android.content.pm.PackageManager;
38import android.content.pm.ResolveInfo;
39import android.content.pm.ServiceInfo;
40import android.content.pm.PackageManager.NameNotFoundException;
41import android.content.pm.UserInfo;
42import android.content.res.Resources;
43import android.graphics.Point;
44import android.os.Binder;
45import android.os.Bundle;
46import android.os.Environment;
47import android.os.FileUtils;
48import android.os.IBinder;
49import android.os.IRemoteCallback;
50import android.os.RemoteException;
51import android.os.FileObserver;
52import android.os.ParcelFileDescriptor;
53import android.os.RemoteCallbackList;
54import android.os.SELinux;
55import android.os.ServiceManager;
56import android.os.SystemClock;
57import android.os.UserHandle;
58import android.os.UserManager;
59import android.service.wallpaper.IWallpaperConnection;
60import android.service.wallpaper.IWallpaperEngine;
61import android.service.wallpaper.IWallpaperService;
62import android.service.wallpaper.WallpaperService;
63import android.util.Slog;
64import android.util.SparseArray;
65import android.util.Xml;
66import android.view.Display;
67import android.view.IWindowManager;
68import android.view.WindowManager;
69
70import java.io.FileDescriptor;
71import java.io.IOException;
72import java.io.InputStream;
73import java.io.File;
74import java.io.FileNotFoundException;
75import java.io.FileInputStream;
76import java.io.FileOutputStream;
77import java.io.PrintWriter;
78import java.util.List;
79
80import org.xmlpull.v1.XmlPullParser;
81import org.xmlpull.v1.XmlPullParserException;
82import org.xmlpull.v1.XmlSerializer;
83
84import com.android.internal.content.PackageMonitor;
85import com.android.internal.util.FastXmlSerializer;
86import com.android.internal.util.JournaledFile;
87
88public class WallpaperManagerService extends IWallpaperManager.Stub {
89    static final String TAG = "WallpaperManagerService";
90    static final boolean DEBUG = false;
91
92    final Object mLock = new Object[0];
93
94    /**
95     * Minimum time between crashes of a wallpaper service for us to consider
96     * restarting it vs. just reverting to the static wallpaper.
97     */
98    static final long MIN_WALLPAPER_CRASH_TIME = 10000;
99    static final String WALLPAPER = "wallpaper";
100    static final String WALLPAPER_INFO = "wallpaper_info.xml";
101    /**
102     * 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    /**
109     * 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     */
114    private class WallpaperObserver extends FileObserver {
115
116        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;
147                        }
148                        bindWallpaperComponentLocked(IMAGE_WALLPAPER, true,
149                                false, mWallpaper, null);
150                        saveSettingsLocked(mWallpaper);
151                    }
152                }
153            }
154        }
155    }
156
157    final Context mContext;
158    final IWindowManager mIWindowManager;
159    final IPackageManager mIPackageManager;
160    final MyPackageMonitor mMonitor;
161    WallpaperData mLastWallpaper;
162
163    SparseArray<WallpaperData> mWallpaperMap = new SparseArray<WallpaperData>();
164
165    int mCurrentUserId;
166
167    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
193        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
213    class WallpaperConnection extends IWallpaperConnection.Stub
214            implements ServiceConnection {
215        final WallpaperInfo mInfo;
216        final Binder mToken = new Binder();
217        IWallpaperService mService;
218        IWallpaperEngine mEngine;
219        WallpaperData mWallpaper;
220        IRemoteCallback mReply;
221
222        boolean mDimensionsChanged = false;
223
224        public WallpaperConnection(WallpaperInfo info, WallpaperData wallpaper) {
225            mInfo = info;
226            mWallpaper = wallpaper;
227        }
228
229        @Override
230        public void onServiceConnected(ComponentName name, IBinder service) {
231            synchronized (mLock) {
232                if (mWallpaper.connection == this) {
233                    mService = IWallpaperService.Stub.asInterface(service);
234                    attachServiceLocked(this, mWallpaper);
235                    // XXX should probably do saveSettingsLocked() later
236                    // when we have an engine, but I'm not sure about
237                    // locking there and anyway we always need to be able to
238                    // recover if there is something wrong.
239                    saveSettingsLocked(mWallpaper);
240                }
241            }
242        }
243
244        @Override
245        public void onServiceDisconnected(ComponentName name) {
246            synchronized (mLock) {
247                mService = null;
248                mEngine = null;
249                if (mWallpaper.connection == this) {
250                    Slog.w(TAG, "Wallpaper service gone: " + mWallpaper.wallpaperComponent);
251                    if (!mWallpaper.wallpaperUpdating
252                            && mWallpaper.userId == mCurrentUserId) {
253                        // There is a race condition which causes
254                        // {@link #mWallpaper.wallpaperUpdating} to be false even if it is
255                        // currently updating since the broadcast notifying us is async.
256                        // This race is overcome by the general rule that we only reset the
257                        // wallpaper if its service was shut down twice
258                        // during {@link #MIN_WALLPAPER_CRASH_TIME} millis.
259                        if (mWallpaper.lastDiedTime != 0
260                                && mWallpaper.lastDiedTime + MIN_WALLPAPER_CRASH_TIME
261                                    > SystemClock.uptimeMillis()) {
262                            Slog.w(TAG, "Reverting to built-in wallpaper!");
263                            clearWallpaperLocked(true, mWallpaper.userId, null);
264                        } else {
265                            mWallpaper.lastDiedTime = SystemClock.uptimeMillis();
266                        }
267                    }
268                }
269            }
270        }
271
272        @Override
273        public void attachEngine(IWallpaperEngine engine) {
274            synchronized (mLock) {
275                mEngine = engine;
276                if (mDimensionsChanged) {
277                    try {
278                        mEngine.setDesiredSize(mWallpaper.width, mWallpaper.height);
279                    } catch (RemoteException e) {
280                        Slog.w(TAG, "Failed to set wallpaper dimensions", e);
281                    }
282                    mDimensionsChanged = false;
283                }
284            }
285        }
286
287        @Override
288        public void engineShown(IWallpaperEngine engine) {
289            synchronized (mLock) {
290                if (mReply != null) {
291                    long ident = Binder.clearCallingIdentity();
292                    try {
293                        mReply.sendResult(null);
294                    } catch (RemoteException e) {
295                        Binder.restoreCallingIdentity(ident);
296                    }
297                    mReply = null;
298                }
299            }
300        }
301
302        @Override
303        public ParcelFileDescriptor setWallpaper(String name) {
304            synchronized (mLock) {
305                if (mWallpaper.connection == this) {
306                    return updateWallpaperBitmapLocked(name, mWallpaper);
307                }
308                return null;
309            }
310        }
311    }
312
313    class MyPackageMonitor extends PackageMonitor {
314        @Override
315        public void onPackageUpdateFinished(String packageName, int uid) {
316            synchronized (mLock) {
317                if (mCurrentUserId != getChangingUserId()) {
318                    return;
319                }
320                WallpaperData wallpaper = mWallpaperMap.get(mCurrentUserId);
321                if (wallpaper != null) {
322                    if (wallpaper.wallpaperComponent != null
323                            && wallpaper.wallpaperComponent.getPackageName().equals(packageName)) {
324                        wallpaper.wallpaperUpdating = false;
325                        ComponentName comp = wallpaper.wallpaperComponent;
326                        clearWallpaperComponentLocked(wallpaper);
327                        if (!bindWallpaperComponentLocked(comp, false, false,
328                                wallpaper, null)) {
329                            Slog.w(TAG, "Wallpaper no longer available; reverting to default");
330                            clearWallpaperLocked(false, wallpaper.userId, null);
331                        }
332                    }
333                }
334            }
335        }
336
337        @Override
338        public void onPackageModified(String packageName) {
339            synchronized (mLock) {
340                if (mCurrentUserId != getChangingUserId()) {
341                    return;
342                }
343                WallpaperData wallpaper = mWallpaperMap.get(mCurrentUserId);
344                if (wallpaper != null) {
345                    if (wallpaper.wallpaperComponent == null
346                            || !wallpaper.wallpaperComponent.getPackageName().equals(packageName)) {
347                        return;
348                    }
349                    doPackagesChangedLocked(true, wallpaper);
350                }
351            }
352        }
353
354        @Override
355        public void onPackageUpdateStarted(String packageName, int uid) {
356            synchronized (mLock) {
357                if (mCurrentUserId != getChangingUserId()) {
358                    return;
359                }
360                WallpaperData wallpaper = mWallpaperMap.get(mCurrentUserId);
361                if (wallpaper != null) {
362                    if (wallpaper.wallpaperComponent != null
363                            && wallpaper.wallpaperComponent.getPackageName().equals(packageName)) {
364                        wallpaper.wallpaperUpdating = true;
365                    }
366                }
367            }
368        }
369
370        @Override
371        public boolean onHandleForceStop(Intent intent, String[] packages, int uid, boolean doit) {
372            synchronized (mLock) {
373                boolean changed = false;
374                if (mCurrentUserId != getChangingUserId()) {
375                    return false;
376                }
377                WallpaperData wallpaper = mWallpaperMap.get(mCurrentUserId);
378                if (wallpaper != null) {
379                    boolean res = doPackagesChangedLocked(doit, wallpaper);
380                    changed |= res;
381                }
382                return changed;
383            }
384        }
385
386        @Override
387        public void onSomePackagesChanged() {
388            synchronized (mLock) {
389                if (mCurrentUserId != getChangingUserId()) {
390                    return;
391                }
392                WallpaperData wallpaper = mWallpaperMap.get(mCurrentUserId);
393                if (wallpaper != null) {
394                    doPackagesChangedLocked(true, wallpaper);
395                }
396            }
397        }
398
399        boolean doPackagesChangedLocked(boolean doit, WallpaperData wallpaper) {
400            boolean changed = false;
401            if (wallpaper.wallpaperComponent != null) {
402                int change = isPackageDisappearing(wallpaper.wallpaperComponent
403                        .getPackageName());
404                if (change == PACKAGE_PERMANENT_CHANGE
405                        || change == PACKAGE_TEMPORARY_CHANGE) {
406                    changed = true;
407                    if (doit) {
408                        Slog.w(TAG, "Wallpaper uninstalled, removing: "
409                                + wallpaper.wallpaperComponent);
410                        clearWallpaperLocked(false, wallpaper.userId, null);
411                    }
412                }
413            }
414            if (wallpaper.nextWallpaperComponent != null) {
415                int change = isPackageDisappearing(wallpaper.nextWallpaperComponent
416                        .getPackageName());
417                if (change == PACKAGE_PERMANENT_CHANGE
418                        || change == PACKAGE_TEMPORARY_CHANGE) {
419                    wallpaper.nextWallpaperComponent = null;
420                }
421            }
422            if (wallpaper.wallpaperComponent != null
423                    && isPackageModified(wallpaper.wallpaperComponent.getPackageName())) {
424                try {
425                    mContext.getPackageManager().getServiceInfo(
426                            wallpaper.wallpaperComponent, 0);
427                } catch (NameNotFoundException e) {
428                    Slog.w(TAG, "Wallpaper component gone, removing: "
429                            + wallpaper.wallpaperComponent);
430                    clearWallpaperLocked(false, wallpaper.userId, null);
431                }
432            }
433            if (wallpaper.nextWallpaperComponent != null
434                    && isPackageModified(wallpaper.nextWallpaperComponent.getPackageName())) {
435                try {
436                    mContext.getPackageManager().getServiceInfo(
437                            wallpaper.nextWallpaperComponent, 0);
438                } catch (NameNotFoundException e) {
439                    wallpaper.nextWallpaperComponent = null;
440                }
441            }
442            return changed;
443        }
444    }
445
446    public WallpaperManagerService(Context context) {
447        if (DEBUG) Slog.v(TAG, "WallpaperService startup");
448        mContext = context;
449        mIWindowManager = IWindowManager.Stub.asInterface(
450                ServiceManager.getService(Context.WINDOW_SERVICE));
451        mIPackageManager = AppGlobals.getPackageManager();
452        mMonitor = new MyPackageMonitor();
453        mMonitor.register(context, null, UserHandle.ALL, true);
454        getWallpaperDir(UserHandle.USER_OWNER).mkdirs();
455        loadSettingsLocked(UserHandle.USER_OWNER);
456    }
457
458    private static File getWallpaperDir(int userId) {
459        return Environment.getUserSystemDirectory(userId);
460    }
461
462    @Override
463    protected void finalize() throws Throwable {
464        super.finalize();
465        for (int i = 0; i < mWallpaperMap.size(); i++) {
466            WallpaperData wallpaper = mWallpaperMap.valueAt(i);
467            wallpaper.wallpaperObserver.stopWatching();
468        }
469    }
470
471    public void systemRunning() {
472        if (DEBUG) Slog.v(TAG, "systemReady");
473        WallpaperData wallpaper = mWallpaperMap.get(UserHandle.USER_OWNER);
474        switchWallpaper(wallpaper, null);
475        wallpaper.wallpaperObserver = new WallpaperObserver(wallpaper);
476        wallpaper.wallpaperObserver.startWatching();
477
478        IntentFilter userFilter = new IntentFilter();
479        userFilter.addAction(Intent.ACTION_USER_REMOVED);
480        userFilter.addAction(Intent.ACTION_USER_STOPPING);
481        mContext.registerReceiver(new BroadcastReceiver() {
482            @Override
483            public void onReceive(Context context, Intent intent) {
484                String action = intent.getAction();
485                if (Intent.ACTION_USER_REMOVED.equals(action)) {
486                    onRemoveUser(intent.getIntExtra(Intent.EXTRA_USER_HANDLE,
487                            UserHandle.USER_NULL));
488                }
489                // TODO: Race condition causing problems when cleaning up on stopping a user.
490                // Comment this out for now.
491                // else if (Intent.ACTION_USER_STOPPING.equals(action)) {
492                //     onStoppingUser(intent.getIntExtra(Intent.EXTRA_USER_HANDLE,
493                //             UserHandle.USER_NULL));
494                // }
495            }
496        }, userFilter);
497
498        try {
499            ActivityManagerNative.getDefault().registerUserSwitchObserver(
500                    new IUserSwitchObserver.Stub() {
501                        @Override
502                        public void onUserSwitching(int newUserId, IRemoteCallback reply) {
503                            switchUser(newUserId, reply);
504                        }
505
506                        @Override
507                        public void onUserSwitchComplete(int newUserId) throws RemoteException {
508                        }
509                    });
510        } catch (RemoteException e) {
511            // TODO Auto-generated catch block
512            e.printStackTrace();
513        }
514    }
515
516    /** Called by SystemBackupAgent */
517    public String getName() {
518        // Verify caller is the system
519        if (Binder.getCallingUid() != android.os.Process.SYSTEM_UID) {
520            throw new RuntimeException("getName() can only be called from the system process");
521        }
522        synchronized (mLock) {
523            return mWallpaperMap.get(0).name;
524        }
525    }
526
527    void onStoppingUser(int userId) {
528        if (userId < 1) return;
529        synchronized (mLock) {
530            WallpaperData wallpaper = mWallpaperMap.get(userId);
531            if (wallpaper != null) {
532                if (wallpaper.wallpaperObserver != null) {
533                    wallpaper.wallpaperObserver.stopWatching();
534                    wallpaper.wallpaperObserver = null;
535                }
536                mWallpaperMap.remove(userId);
537            }
538        }
539    }
540
541    void onRemoveUser(int userId) {
542        if (userId < 1) return;
543        synchronized (mLock) {
544            onStoppingUser(userId);
545            File wallpaperFile = new File(getWallpaperDir(userId), WALLPAPER);
546            wallpaperFile.delete();
547            File wallpaperInfoFile = new File(getWallpaperDir(userId), WALLPAPER_INFO);
548            wallpaperInfoFile.delete();
549        }
550    }
551
552    void switchUser(int userId, IRemoteCallback reply) {
553        synchronized (mLock) {
554            mCurrentUserId = userId;
555            WallpaperData wallpaper = mWallpaperMap.get(userId);
556            if (wallpaper == null) {
557                wallpaper = new WallpaperData(userId);
558                mWallpaperMap.put(userId, wallpaper);
559                loadSettingsLocked(userId);
560            }
561            // Not started watching yet, in case wallpaper data was loaded for other reasons.
562            if (wallpaper.wallpaperObserver == null) {
563                wallpaper.wallpaperObserver = new WallpaperObserver(wallpaper);
564                wallpaper.wallpaperObserver.startWatching();
565            }
566            switchWallpaper(wallpaper, reply);
567        }
568    }
569
570    void switchWallpaper(WallpaperData wallpaper, IRemoteCallback reply) {
571        synchronized (mLock) {
572            RuntimeException e = null;
573            try {
574                ComponentName cname = wallpaper.wallpaperComponent != null ?
575                        wallpaper.wallpaperComponent : wallpaper.nextWallpaperComponent;
576                if (bindWallpaperComponentLocked(cname, true, false, wallpaper, reply)) {
577                    return;
578                }
579            } catch (RuntimeException e1) {
580                e = e1;
581            }
582            Slog.w(TAG, "Failure starting previous wallpaper", e);
583            clearWallpaperLocked(false, wallpaper.userId, reply);
584        }
585    }
586
587    public void clearWallpaper() {
588        if (DEBUG) Slog.v(TAG, "clearWallpaper");
589        synchronized (mLock) {
590            clearWallpaperLocked(false, UserHandle.getCallingUserId(), null);
591        }
592    }
593
594    void clearWallpaperLocked(boolean defaultFailed, int userId, IRemoteCallback reply) {
595        WallpaperData wallpaper = mWallpaperMap.get(userId);
596        File f = new File(getWallpaperDir(userId), WALLPAPER);
597        if (f.exists()) {
598            f.delete();
599        }
600        final long ident = Binder.clearCallingIdentity();
601        RuntimeException e = null;
602        try {
603            wallpaper.imageWallpaperPending = false;
604            if (userId != mCurrentUserId) return;
605            if (bindWallpaperComponentLocked(defaultFailed
606                    ? IMAGE_WALLPAPER
607                    : null, true, false, wallpaper, reply)) {
608                return;
609            }
610        } catch (IllegalArgumentException e1) {
611            e = e1;
612        } finally {
613            Binder.restoreCallingIdentity(ident);
614        }
615
616        // This can happen if the default wallpaper component doesn't
617        // exist.  This should be a system configuration problem, but
618        // let's not let it crash the system and just live with no
619        // wallpaper.
620        Slog.e(TAG, "Default wallpaper component not found!", e);
621        clearWallpaperComponentLocked(wallpaper);
622        if (reply != null) {
623            try {
624                reply.sendResult(null);
625            } catch (RemoteException e1) {
626            }
627        }
628    }
629
630    public boolean hasNamedWallpaper(String name) {
631        synchronized (mLock) {
632            List<UserInfo> users;
633            long ident = Binder.clearCallingIdentity();
634            try {
635                users = ((UserManager) mContext.getSystemService(Context.USER_SERVICE)).getUsers();
636            } finally {
637                Binder.restoreCallingIdentity(ident);
638            }
639            for (UserInfo user: users) {
640                WallpaperData wd = mWallpaperMap.get(user.id);
641                if (wd == null) {
642                    // User hasn't started yet, so load her settings to peek at the wallpaper
643                    loadSettingsLocked(user.id);
644                    wd = mWallpaperMap.get(user.id);
645                }
646                if (wd != null && name.equals(wd.name)) {
647                    return true;
648                }
649            }
650        }
651        return false;
652    }
653
654    private Point getDefaultDisplaySize() {
655        Point p = new Point();
656        WindowManager wm = (WindowManager)mContext.getSystemService(Context.WINDOW_SERVICE);
657        Display d = wm.getDefaultDisplay();
658        d.getRealSize(p);
659        return p;
660    }
661
662    public void setDimensionHints(int width, int height) throws RemoteException {
663        checkPermission(android.Manifest.permission.SET_WALLPAPER_HINTS);
664        synchronized (mLock) {
665            int userId = UserHandle.getCallingUserId();
666            WallpaperData wallpaper = mWallpaperMap.get(userId);
667            if (wallpaper == null) {
668                throw new IllegalStateException("Wallpaper not yet initialized for user " + userId);
669            }
670            if (width <= 0 || height <= 0) {
671                throw new IllegalArgumentException("width and height must be > 0");
672            }
673            // Make sure it is at least as large as the display.
674            Point displaySize = getDefaultDisplaySize();
675            width = Math.max(width, displaySize.x);
676            height = Math.max(height, displaySize.y);
677
678            if (width != wallpaper.width || height != wallpaper.height) {
679                wallpaper.width = width;
680                wallpaper.height = height;
681                saveSettingsLocked(wallpaper);
682                if (mCurrentUserId != userId) return; // Don't change the properties now
683                if (wallpaper.connection != null) {
684                    if (wallpaper.connection.mEngine != null) {
685                        try {
686                            wallpaper.connection.mEngine.setDesiredSize(
687                                    width, height);
688                        } catch (RemoteException e) {
689                        }
690                        notifyCallbacksLocked(wallpaper);
691                    } else if (wallpaper.connection.mService != null) {
692                        // We've attached to the service but the engine hasn't attached back to us
693                        // yet. This means it will be created with the previous dimensions, so we
694                        // need to update it to the new dimensions once it attaches.
695                        wallpaper.connection.mDimensionsChanged = true;
696                    }
697                }
698            }
699        }
700    }
701
702    public int getWidthHint() throws RemoteException {
703        synchronized (mLock) {
704            WallpaperData wallpaper = mWallpaperMap.get(UserHandle.getCallingUserId());
705            return wallpaper.width;
706        }
707    }
708
709    public int getHeightHint() throws RemoteException {
710        synchronized (mLock) {
711            WallpaperData wallpaper = mWallpaperMap.get(UserHandle.getCallingUserId());
712            return wallpaper.height;
713        }
714    }
715
716    public ParcelFileDescriptor getWallpaper(IWallpaperManagerCallback cb,
717            Bundle outParams) {
718        synchronized (mLock) {
719            // This returns the current user's wallpaper, if called by a system service. Else it
720            // returns the wallpaper for the calling user.
721            int callingUid = Binder.getCallingUid();
722            int wallpaperUserId = 0;
723            if (callingUid == android.os.Process.SYSTEM_UID) {
724                wallpaperUserId = mCurrentUserId;
725            } else {
726                wallpaperUserId = UserHandle.getUserId(callingUid);
727            }
728            WallpaperData wallpaper = mWallpaperMap.get(wallpaperUserId);
729            try {
730                if (outParams != null) {
731                    outParams.putInt("width", wallpaper.width);
732                    outParams.putInt("height", wallpaper.height);
733                }
734                wallpaper.callbacks.register(cb);
735                File f = new File(getWallpaperDir(wallpaperUserId), WALLPAPER);
736                if (!f.exists()) {
737                    return null;
738                }
739                return ParcelFileDescriptor.open(f, MODE_READ_ONLY);
740            } catch (FileNotFoundException e) {
741                /* Shouldn't happen as we check to see if the file exists */
742                Slog.w(TAG, "Error getting wallpaper", e);
743            }
744            return null;
745        }
746    }
747
748    public WallpaperInfo getWallpaperInfo() {
749        int userId = UserHandle.getCallingUserId();
750        synchronized (mLock) {
751            WallpaperData wallpaper = mWallpaperMap.get(userId);
752            if (wallpaper.connection != null) {
753                return wallpaper.connection.mInfo;
754            }
755            return null;
756        }
757    }
758
759    public ParcelFileDescriptor setWallpaper(String name) {
760        checkPermission(android.Manifest.permission.SET_WALLPAPER);
761        synchronized (mLock) {
762            if (DEBUG) Slog.v(TAG, "setWallpaper");
763            int userId = UserHandle.getCallingUserId();
764            WallpaperData wallpaper = mWallpaperMap.get(userId);
765            if (wallpaper == null) {
766                throw new IllegalStateException("Wallpaper not yet initialized for user " + userId);
767            }
768            final long ident = Binder.clearCallingIdentity();
769            try {
770                ParcelFileDescriptor pfd = updateWallpaperBitmapLocked(name, wallpaper);
771                if (pfd != null) {
772                    wallpaper.imageWallpaperPending = true;
773                }
774                return pfd;
775            } finally {
776                Binder.restoreCallingIdentity(ident);
777            }
778        }
779    }
780
781    ParcelFileDescriptor updateWallpaperBitmapLocked(String name, WallpaperData wallpaper) {
782        if (name == null) name = "";
783        try {
784            File dir = getWallpaperDir(wallpaper.userId);
785            if (!dir.exists()) {
786                dir.mkdir();
787                FileUtils.setPermissions(
788                        dir.getPath(),
789                        FileUtils.S_IRWXU|FileUtils.S_IRWXG|FileUtils.S_IXOTH,
790                        -1, -1);
791            }
792            File file = new File(dir, WALLPAPER);
793            ParcelFileDescriptor fd = ParcelFileDescriptor.open(file,
794                    MODE_CREATE|MODE_READ_WRITE);
795            if (!SELinux.restorecon(file)) {
796                return null;
797            }
798            wallpaper.name = name;
799            return fd;
800        } catch (FileNotFoundException e) {
801            Slog.w(TAG, "Error setting wallpaper", e);
802        }
803        return null;
804    }
805
806    public void setWallpaperComponent(ComponentName name) {
807        checkPermission(android.Manifest.permission.SET_WALLPAPER_COMPONENT);
808        synchronized (mLock) {
809            if (DEBUG) Slog.v(TAG, "setWallpaperComponent name=" + name);
810            int userId = UserHandle.getCallingUserId();
811            WallpaperData wallpaper = mWallpaperMap.get(userId);
812            if (wallpaper == null) {
813                throw new IllegalStateException("Wallpaper not yet initialized for user " + userId);
814            }
815            final long ident = Binder.clearCallingIdentity();
816            try {
817                wallpaper.imageWallpaperPending = false;
818                bindWallpaperComponentLocked(name, false, true, wallpaper, null);
819            } finally {
820                Binder.restoreCallingIdentity(ident);
821            }
822        }
823    }
824
825    boolean bindWallpaperComponentLocked(ComponentName componentName, boolean force,
826            boolean fromUser, WallpaperData wallpaper, IRemoteCallback reply) {
827        if (DEBUG) Slog.v(TAG, "bindWallpaperComponentLocked: componentName=" + componentName);
828        // Has the component changed?
829        if (!force) {
830            if (wallpaper.connection != null) {
831                if (wallpaper.wallpaperComponent == null) {
832                    if (componentName == null) {
833                        if (DEBUG) Slog.v(TAG, "bindWallpaperComponentLocked: still using default");
834                        // Still using default wallpaper.
835                        return true;
836                    }
837                } else if (wallpaper.wallpaperComponent.equals(componentName)) {
838                    // Changing to same wallpaper.
839                    if (DEBUG) Slog.v(TAG, "same wallpaper");
840                    return true;
841                }
842            }
843        }
844
845        try {
846            if (componentName == null) {
847                String defaultComponent =
848                    mContext.getString(com.android.internal.R.string.default_wallpaper_component);
849                if (defaultComponent != null) {
850                    // See if there is a default wallpaper component specified
851                    componentName = ComponentName.unflattenFromString(defaultComponent);
852                    if (DEBUG) Slog.v(TAG, "Use default component wallpaper:" + componentName);
853                }
854                if (componentName == null) {
855                    // Fall back to static image wallpaper
856                    componentName = IMAGE_WALLPAPER;
857                    //clearWallpaperComponentLocked();
858                    //return;
859                    if (DEBUG) Slog.v(TAG, "Using image wallpaper");
860                }
861            }
862            int serviceUserId = wallpaper.userId;
863            ServiceInfo si = mIPackageManager.getServiceInfo(componentName,
864                    PackageManager.GET_META_DATA | PackageManager.GET_PERMISSIONS, serviceUserId);
865            if (si == null) {
866                // The wallpaper component we're trying to use doesn't exist
867                Slog.w(TAG, "Attempted wallpaper " + componentName + " is unavailable");
868                return false;
869            }
870            if (!android.Manifest.permission.BIND_WALLPAPER.equals(si.permission)) {
871                String msg = "Selected service does not require "
872                        + android.Manifest.permission.BIND_WALLPAPER
873                        + ": " + componentName;
874                if (fromUser) {
875                    throw new SecurityException(msg);
876                }
877                Slog.w(TAG, msg);
878                return false;
879            }
880
881            WallpaperInfo wi = null;
882
883            Intent intent = new Intent(WallpaperService.SERVICE_INTERFACE);
884            if (componentName != null && !componentName.equals(IMAGE_WALLPAPER)) {
885                // Make sure the selected service is actually a wallpaper service.
886                List<ResolveInfo> ris =
887                        mIPackageManager.queryIntentServices(intent,
888                                intent.resolveTypeIfNeeded(mContext.getContentResolver()),
889                                PackageManager.GET_META_DATA, serviceUserId);
890                for (int i=0; i<ris.size(); i++) {
891                    ServiceInfo rsi = ris.get(i).serviceInfo;
892                    if (rsi.name.equals(si.name) &&
893                            rsi.packageName.equals(si.packageName)) {
894                        try {
895                            wi = new WallpaperInfo(mContext, ris.get(i));
896                        } catch (XmlPullParserException e) {
897                            if (fromUser) {
898                                throw new IllegalArgumentException(e);
899                            }
900                            Slog.w(TAG, e);
901                            return false;
902                        } catch (IOException e) {
903                            if (fromUser) {
904                                throw new IllegalArgumentException(e);
905                            }
906                            Slog.w(TAG, e);
907                            return false;
908                        }
909                        break;
910                    }
911                }
912                if (wi == null) {
913                    String msg = "Selected service is not a wallpaper: "
914                            + componentName;
915                    if (fromUser) {
916                        throw new SecurityException(msg);
917                    }
918                    Slog.w(TAG, msg);
919                    return false;
920                }
921            }
922
923            // Bind the service!
924            if (DEBUG) Slog.v(TAG, "Binding to:" + componentName);
925            WallpaperConnection newConn = new WallpaperConnection(wi, wallpaper);
926            intent.setComponent(componentName);
927            intent.putExtra(Intent.EXTRA_CLIENT_LABEL,
928                    com.android.internal.R.string.wallpaper_binding_label);
929            intent.putExtra(Intent.EXTRA_CLIENT_INTENT, PendingIntent.getActivityAsUser(
930                    mContext, 0,
931                    Intent.createChooser(new Intent(Intent.ACTION_SET_WALLPAPER),
932                            mContext.getText(com.android.internal.R.string.chooser_wallpaper)),
933                    0, null, new UserHandle(serviceUserId)));
934            if (!mContext.bindServiceAsUser(intent, newConn,
935                    Context.BIND_AUTO_CREATE | Context.BIND_SHOWING_UI,
936                    new UserHandle(serviceUserId))) {
937                String msg = "Unable to bind service: "
938                        + componentName;
939                if (fromUser) {
940                    throw new IllegalArgumentException(msg);
941                }
942                Slog.w(TAG, msg);
943                return false;
944            }
945            if (wallpaper.userId == mCurrentUserId && mLastWallpaper != null) {
946                detachWallpaperLocked(mLastWallpaper);
947            }
948            wallpaper.wallpaperComponent = componentName;
949            wallpaper.connection = newConn;
950            newConn.mReply = reply;
951            try {
952                if (wallpaper.userId == mCurrentUserId) {
953                    if (DEBUG)
954                        Slog.v(TAG, "Adding window token: " + newConn.mToken);
955                    mIWindowManager.addWindowToken(newConn.mToken,
956                            WindowManager.LayoutParams.TYPE_WALLPAPER);
957                    mLastWallpaper = wallpaper;
958                }
959            } catch (RemoteException e) {
960            }
961        } catch (RemoteException e) {
962            String msg = "Remote exception for " + componentName + "\n" + e;
963            if (fromUser) {
964                throw new IllegalArgumentException(msg);
965            }
966            Slog.w(TAG, msg);
967            return false;
968        }
969        return true;
970    }
971
972    void detachWallpaperLocked(WallpaperData wallpaper) {
973        if (wallpaper.connection != null) {
974            if (wallpaper.connection.mReply != null) {
975                try {
976                    wallpaper.connection.mReply.sendResult(null);
977                } catch (RemoteException e) {
978                }
979                wallpaper.connection.mReply = null;
980            }
981            if (wallpaper.connection.mEngine != null) {
982                try {
983                    wallpaper.connection.mEngine.destroy();
984                } catch (RemoteException e) {
985                }
986            }
987            mContext.unbindService(wallpaper.connection);
988            try {
989                if (DEBUG)
990                    Slog.v(TAG, "Removing window token: " + wallpaper.connection.mToken);
991                mIWindowManager.removeWindowToken(wallpaper.connection.mToken);
992            } catch (RemoteException e) {
993            }
994            wallpaper.connection.mService = null;
995            wallpaper.connection.mEngine = null;
996            wallpaper.connection = null;
997        }
998    }
999
1000    void clearWallpaperComponentLocked(WallpaperData wallpaper) {
1001        wallpaper.wallpaperComponent = null;
1002        detachWallpaperLocked(wallpaper);
1003    }
1004
1005    void attachServiceLocked(WallpaperConnection conn, WallpaperData wallpaper) {
1006        try {
1007            conn.mService.attach(conn, conn.mToken,
1008                    WindowManager.LayoutParams.TYPE_WALLPAPER, false,
1009                    wallpaper.width, wallpaper.height);
1010        } catch (RemoteException e) {
1011            Slog.w(TAG, "Failed attaching wallpaper; clearing", e);
1012            if (!wallpaper.wallpaperUpdating) {
1013                bindWallpaperComponentLocked(null, false, false, wallpaper, null);
1014            }
1015        }
1016    }
1017
1018    private void notifyCallbacksLocked(WallpaperData wallpaper) {
1019        final int n = wallpaper.callbacks.beginBroadcast();
1020        for (int i = 0; i < n; i++) {
1021            try {
1022                wallpaper.callbacks.getBroadcastItem(i).onWallpaperChanged();
1023            } catch (RemoteException e) {
1024
1025                // The RemoteCallbackList will take care of removing
1026                // the dead object for us.
1027            }
1028        }
1029        wallpaper.callbacks.finishBroadcast();
1030        final Intent intent = new Intent(Intent.ACTION_WALLPAPER_CHANGED);
1031        mContext.sendBroadcastAsUser(intent, new UserHandle(mCurrentUserId));
1032    }
1033
1034    private void checkPermission(String permission) {
1035        if (PackageManager.PERMISSION_GRANTED!= mContext.checkCallingOrSelfPermission(permission)) {
1036            throw new SecurityException("Access denied to process: " + Binder.getCallingPid()
1037                    + ", must have permission " + permission);
1038        }
1039    }
1040
1041    private static JournaledFile makeJournaledFile(int userId) {
1042        final String base = new File(getWallpaperDir(userId), WALLPAPER_INFO).getAbsolutePath();
1043        return new JournaledFile(new File(base), new File(base + ".tmp"));
1044    }
1045
1046    private void saveSettingsLocked(WallpaperData wallpaper) {
1047        JournaledFile journal = makeJournaledFile(wallpaper.userId);
1048        FileOutputStream stream = null;
1049        try {
1050            stream = new FileOutputStream(journal.chooseForWrite(), false);
1051            XmlSerializer out = new FastXmlSerializer();
1052            out.setOutput(stream, "utf-8");
1053            out.startDocument(null, true);
1054
1055            out.startTag(null, "wp");
1056            out.attribute(null, "width", Integer.toString(wallpaper.width));
1057            out.attribute(null, "height", Integer.toString(wallpaper.height));
1058            out.attribute(null, "name", wallpaper.name);
1059            if (wallpaper.wallpaperComponent != null
1060                    && !wallpaper.wallpaperComponent.equals(IMAGE_WALLPAPER)) {
1061                out.attribute(null, "component",
1062                        wallpaper.wallpaperComponent.flattenToShortString());
1063            }
1064            out.endTag(null, "wp");
1065
1066            out.endDocument();
1067            stream.close();
1068            journal.commit();
1069        } catch (IOException e) {
1070            try {
1071                if (stream != null) {
1072                    stream.close();
1073                }
1074            } catch (IOException ex) {
1075                // Ignore
1076            }
1077            journal.rollback();
1078        }
1079    }
1080
1081    private void migrateFromOld() {
1082        File oldWallpaper = new File(WallpaperBackupHelper.WALLPAPER_IMAGE_KEY);
1083        File oldInfo = new File(WallpaperBackupHelper.WALLPAPER_INFO_KEY);
1084        if (oldWallpaper.exists()) {
1085            File newWallpaper = new File(getWallpaperDir(0), WALLPAPER);
1086            oldWallpaper.renameTo(newWallpaper);
1087        }
1088        if (oldInfo.exists()) {
1089            File newInfo = new File(getWallpaperDir(0), WALLPAPER_INFO);
1090            oldInfo.renameTo(newInfo);
1091        }
1092    }
1093
1094    private void loadSettingsLocked(int userId) {
1095        if (DEBUG) Slog.v(TAG, "loadSettingsLocked");
1096
1097        JournaledFile journal = makeJournaledFile(userId);
1098        FileInputStream stream = null;
1099        File file = journal.chooseForRead();
1100        if (!file.exists()) {
1101            // This should only happen one time, when upgrading from a legacy system
1102            migrateFromOld();
1103        }
1104        WallpaperData wallpaper = mWallpaperMap.get(userId);
1105        if (wallpaper == null) {
1106            wallpaper = new WallpaperData(userId);
1107            mWallpaperMap.put(userId, wallpaper);
1108        }
1109        boolean success = false;
1110        try {
1111            stream = new FileInputStream(file);
1112            XmlPullParser parser = Xml.newPullParser();
1113            parser.setInput(stream, null);
1114
1115            int type;
1116            do {
1117                type = parser.next();
1118                if (type == XmlPullParser.START_TAG) {
1119                    String tag = parser.getName();
1120                    if ("wp".equals(tag)) {
1121                        wallpaper.width = Integer.parseInt(parser.getAttributeValue(null, "width"));
1122                        wallpaper.height = Integer.parseInt(parser
1123                                .getAttributeValue(null, "height"));
1124                        wallpaper.name = parser.getAttributeValue(null, "name");
1125                        String comp = parser.getAttributeValue(null, "component");
1126                        wallpaper.nextWallpaperComponent = comp != null
1127                                ? ComponentName.unflattenFromString(comp)
1128                                : null;
1129                        if (wallpaper.nextWallpaperComponent == null
1130                                || "android".equals(wallpaper.nextWallpaperComponent
1131                                        .getPackageName())) {
1132                            wallpaper.nextWallpaperComponent = IMAGE_WALLPAPER;
1133                        }
1134
1135                        if (DEBUG) {
1136                            Slog.v(TAG, "mWidth:" + wallpaper.width);
1137                            Slog.v(TAG, "mHeight:" + wallpaper.height);
1138                            Slog.v(TAG, "mName:" + wallpaper.name);
1139                            Slog.v(TAG, "mNextWallpaperComponent:"
1140                                    + wallpaper.nextWallpaperComponent);
1141                        }
1142                    }
1143                }
1144            } while (type != XmlPullParser.END_DOCUMENT);
1145            success = true;
1146        } catch (FileNotFoundException e) {
1147            Slog.w(TAG, "no current wallpaper -- first boot?");
1148        } catch (NullPointerException e) {
1149            Slog.w(TAG, "failed parsing " + file + " " + e);
1150        } catch (NumberFormatException e) {
1151            Slog.w(TAG, "failed parsing " + file + " " + e);
1152        } catch (XmlPullParserException e) {
1153            Slog.w(TAG, "failed parsing " + file + " " + e);
1154        } catch (IOException e) {
1155            Slog.w(TAG, "failed parsing " + file + " " + e);
1156        } catch (IndexOutOfBoundsException e) {
1157            Slog.w(TAG, "failed parsing " + file + " " + e);
1158        }
1159        try {
1160            if (stream != null) {
1161                stream.close();
1162            }
1163        } catch (IOException e) {
1164            // Ignore
1165        }
1166
1167        if (!success) {
1168            wallpaper.width = -1;
1169            wallpaper.height = -1;
1170            wallpaper.name = "";
1171        }
1172
1173        // We always want to have some reasonable width hint.
1174        int baseSize = getMaximumSizeDimension();
1175        if (wallpaper.width < baseSize) {
1176            wallpaper.width = baseSize;
1177        }
1178        if (wallpaper.height < baseSize) {
1179            wallpaper.height = baseSize;
1180        }
1181    }
1182
1183    private int getMaximumSizeDimension() {
1184        WindowManager wm = (WindowManager)mContext.getSystemService(Context.WINDOW_SERVICE);
1185        Display d = wm.getDefaultDisplay();
1186        return d.getMaximumSizeDimension();
1187    }
1188
1189    // Called by SystemBackupAgent after files are restored to disk.
1190    public void settingsRestored() {
1191        // Verify caller is the system
1192        if (Binder.getCallingUid() != android.os.Process.SYSTEM_UID) {
1193            throw new RuntimeException("settingsRestored() can only be called from the system process");
1194        }
1195        // TODO: If necessary, make it work for secondary users as well. This currently assumes
1196        // restores only to the primary user
1197        if (DEBUG) Slog.v(TAG, "settingsRestored");
1198        WallpaperData wallpaper = null;
1199        boolean success = false;
1200        synchronized (mLock) {
1201            loadSettingsLocked(0);
1202            wallpaper = mWallpaperMap.get(0);
1203            if (wallpaper.nextWallpaperComponent != null
1204                    && !wallpaper.nextWallpaperComponent.equals(IMAGE_WALLPAPER)) {
1205                if (!bindWallpaperComponentLocked(wallpaper.nextWallpaperComponent, false, false,
1206                        wallpaper, null)) {
1207                    // No such live wallpaper or other failure; fall back to the default
1208                    // live wallpaper (since the profile being restored indicated that the
1209                    // user had selected a live rather than static one).
1210                    bindWallpaperComponentLocked(null, false, false, wallpaper, null);
1211                }
1212                success = true;
1213            } else {
1214                // If there's a wallpaper name, we use that.  If that can't be loaded, then we
1215                // use the default.
1216                if ("".equals(wallpaper.name)) {
1217                    if (DEBUG) Slog.v(TAG, "settingsRestored: name is empty");
1218                    success = true;
1219                } else {
1220                    if (DEBUG) Slog.v(TAG, "settingsRestored: attempting to restore named resource");
1221                    success = restoreNamedResourceLocked(wallpaper);
1222                }
1223                if (DEBUG) Slog.v(TAG, "settingsRestored: success=" + success);
1224                if (success) {
1225                    bindWallpaperComponentLocked(wallpaper.nextWallpaperComponent, false, false,
1226                            wallpaper, null);
1227                }
1228            }
1229        }
1230
1231        if (!success) {
1232            Slog.e(TAG, "Failed to restore wallpaper: '" + wallpaper.name + "'");
1233            wallpaper.name = "";
1234            getWallpaperDir(0).delete();
1235        }
1236
1237        synchronized (mLock) {
1238            saveSettingsLocked(wallpaper);
1239        }
1240    }
1241
1242    boolean restoreNamedResourceLocked(WallpaperData wallpaper) {
1243        if (wallpaper.name.length() > 4 && "res:".equals(wallpaper.name.substring(0, 4))) {
1244            String resName = wallpaper.name.substring(4);
1245
1246            String pkg = null;
1247            int colon = resName.indexOf(':');
1248            if (colon > 0) {
1249                pkg = resName.substring(0, colon);
1250            }
1251
1252            String ident = null;
1253            int slash = resName.lastIndexOf('/');
1254            if (slash > 0) {
1255                ident = resName.substring(slash+1);
1256            }
1257
1258            String type = null;
1259            if (colon > 0 && slash > 0 && (slash-colon) > 1) {
1260                type = resName.substring(colon+1, slash);
1261            }
1262
1263            if (pkg != null && ident != null && type != null) {
1264                int resId = -1;
1265                InputStream res = null;
1266                FileOutputStream fos = null;
1267                try {
1268                    Context c = mContext.createPackageContext(pkg, Context.CONTEXT_RESTRICTED);
1269                    Resources r = c.getResources();
1270                    resId = r.getIdentifier(resName, null, null);
1271                    if (resId == 0) {
1272                        Slog.e(TAG, "couldn't resolve identifier pkg=" + pkg + " type=" + type
1273                                + " ident=" + ident);
1274                        return false;
1275                    }
1276
1277                    res = r.openRawResource(resId);
1278                    if (wallpaper.wallpaperFile.exists()) {
1279                        wallpaper.wallpaperFile.delete();
1280                    }
1281                    fos = new FileOutputStream(wallpaper.wallpaperFile);
1282
1283                    byte[] buffer = new byte[32768];
1284                    int amt;
1285                    while ((amt=res.read(buffer)) > 0) {
1286                        fos.write(buffer, 0, amt);
1287                    }
1288                    // mWallpaperObserver will notice the close and send the change broadcast
1289
1290                    Slog.v(TAG, "Restored wallpaper: " + resName);
1291                    return true;
1292                } catch (NameNotFoundException e) {
1293                    Slog.e(TAG, "Package name " + pkg + " not found");
1294                } catch (Resources.NotFoundException e) {
1295                    Slog.e(TAG, "Resource not found: " + resId);
1296                } catch (IOException e) {
1297                    Slog.e(TAG, "IOException while restoring wallpaper ", e);
1298                } finally {
1299                    if (res != null) {
1300                        try {
1301                            res.close();
1302                        } catch (IOException ex) {}
1303                    }
1304                    if (fos != null) {
1305                        FileUtils.sync(fos);
1306                        try {
1307                            fos.close();
1308                        } catch (IOException ex) {}
1309                    }
1310                }
1311            }
1312        }
1313        return false;
1314    }
1315
1316    @Override
1317    protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
1318        if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DUMP)
1319                != PackageManager.PERMISSION_GRANTED) {
1320
1321            pw.println("Permission Denial: can't dump wallpaper service from from pid="
1322                    + Binder.getCallingPid()
1323                    + ", uid=" + Binder.getCallingUid());
1324            return;
1325        }
1326
1327        synchronized (mLock) {
1328            pw.println("Current Wallpaper Service state:");
1329            for (int i = 0; i < mWallpaperMap.size(); i++) {
1330                WallpaperData wallpaper = mWallpaperMap.valueAt(i);
1331                pw.println(" User " + wallpaper.userId + ":");
1332                pw.print("  mWidth=");
1333                pw.print(wallpaper.width);
1334                pw.print(" mHeight=");
1335                pw.println(wallpaper.height);
1336                pw.print("  mName=");
1337                pw.println(wallpaper.name);
1338                pw.print("  mWallpaperComponent=");
1339                pw.println(wallpaper.wallpaperComponent);
1340                if (wallpaper.connection != null) {
1341                    WallpaperConnection conn = wallpaper.connection;
1342                    pw.print("  Wallpaper connection ");
1343                    pw.print(conn);
1344                    pw.println(":");
1345                    if (conn.mInfo != null) {
1346                        pw.print("    mInfo.component=");
1347                        pw.println(conn.mInfo.getComponent());
1348                    }
1349                    pw.print("    mToken=");
1350                    pw.println(conn.mToken);
1351                    pw.print("    mService=");
1352                    pw.println(conn.mService);
1353                    pw.print("    mEngine=");
1354                    pw.println(conn.mEngine);
1355                    pw.print("    mLastDiedTime=");
1356                    pw.println(wallpaper.lastDiedTime - SystemClock.uptimeMillis());
1357                }
1358            }
1359        }
1360    }
1361}
1362