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