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