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