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