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