WallpaperManagerService.java revision 6172266154e9071abba2c0aab9ffb31e0ec8c239
19066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project/*
29066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * Copyright (C) 2008 The Android Open Source Project
39066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project *
49066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * Licensed under the Apache License, Version 2.0 (the "License");
59066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * you may not use this file except in compliance with the License.
69066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * You may obtain a copy of the License at
79066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project *
89066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project *      http://www.apache.org/licenses/LICENSE-2.0
99066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project *
109066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * Unless required by applicable law or agreed to in writing, software
119066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * distributed under the License is distributed on an "AS IS" BASIS,
129066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
139066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * See the License for the specific language governing permissions and
149066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * limitations under the License.
159066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project */
169066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
179066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectpackage com.android.server.wallpaper;
189066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
199066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectimport static android.app.WallpaperManager.FLAG_LOCK;
209066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectimport static android.app.WallpaperManager.FLAG_SYSTEM;
219066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectimport static android.os.ParcelFileDescriptor.MODE_CREATE;
229066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectimport static android.os.ParcelFileDescriptor.MODE_READ_ONLY;
238f028a94fc533e75077485a7d11a04e4de820335Makoto Onukiimport static android.os.ParcelFileDescriptor.MODE_READ_WRITE;
248f028a94fc533e75077485a7d11a04e4de820335Makoto Onukiimport static android.os.ParcelFileDescriptor.MODE_TRUNCATE;
259066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
269066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectimport android.app.ActivityManager;
279066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectimport android.app.ActivityManagerNative;
289066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectimport android.app.AppGlobals;
299066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectimport android.app.AppOpsManager;
309066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectimport android.app.IUserSwitchObserver;
319066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectimport android.app.IWallpaperManager;
329066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectimport android.app.IWallpaperManagerCallback;
339066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectimport android.app.PendingIntent;
349066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectimport android.app.WallpaperInfo;
359066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectimport android.app.WallpaperManager;
369066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectimport android.app.admin.DevicePolicyManager;
379066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectimport android.app.backup.WallpaperBackupHelper;
389066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectimport android.content.BroadcastReceiver;
399066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectimport android.content.ComponentName;
409066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectimport android.content.Context;
419066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectimport android.content.Intent;
429066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectimport android.content.IntentFilter;
4360586f2ec65d16d185767fce4311d3ed0e9112acDan Egnorimport android.content.ServiceConnection;
449066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectimport android.content.pm.IPackageManager;
459066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectimport android.content.pm.PackageManager;
46e97c2006bf7c391c933307e520a392e532aa5d6aBob Leeimport android.content.pm.PackageManager.NameNotFoundException;
479066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectimport android.content.pm.ResolveInfo;
489066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectimport android.content.pm.ServiceInfo;
4960586f2ec65d16d185767fce4311d3ed0e9112acDan Egnorimport android.content.pm.UserInfo;
5060586f2ec65d16d185767fce4311d3ed0e9112acDan Egnorimport android.content.res.Resources;
5160586f2ec65d16d185767fce4311d3ed0e9112acDan Egnorimport android.graphics.Bitmap;
5260586f2ec65d16d185767fce4311d3ed0e9112acDan Egnorimport android.graphics.BitmapFactory;
539d4b57545300c6de1722094404ae09bf0f6be937Dan Egnorimport android.graphics.BitmapRegionDecoder;
5460586f2ec65d16d185767fce4311d3ed0e9112acDan Egnorimport android.graphics.Point;
5560586f2ec65d16d185767fce4311d3ed0e9112acDan Egnorimport android.graphics.Rect;
5660586f2ec65d16d185767fce4311d3ed0e9112acDan Egnorimport android.os.Binder;
5760586f2ec65d16d185767fce4311d3ed0e9112acDan Egnorimport android.os.Bundle;
589d4b57545300c6de1722094404ae09bf0f6be937Dan Egnorimport android.os.Environment;
599d4b57545300c6de1722094404ae09bf0f6be937Dan Egnorimport android.os.FileObserver;
609d4b57545300c6de1722094404ae09bf0f6be937Dan Egnorimport android.os.FileUtils;
619066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectimport android.os.IBinder;
629066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectimport android.os.IRemoteCallback;
6360586f2ec65d16d185767fce4311d3ed0e9112acDan Egnorimport android.os.Process;
649066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectimport android.os.ParcelFileDescriptor;
6560586f2ec65d16d185767fce4311d3ed0e9112acDan Egnorimport android.os.RemoteCallbackList;
669066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectimport android.os.RemoteException;
6760586f2ec65d16d185767fce4311d3ed0e9112acDan Egnorimport android.os.SELinux;
6860586f2ec65d16d185767fce4311d3ed0e9112acDan Egnorimport android.os.ServiceManager;
6960586f2ec65d16d185767fce4311d3ed0e9112acDan Egnorimport android.os.SystemClock;
709066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectimport android.os.UserHandle;
719066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectimport android.os.UserManager;
729066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectimport android.service.wallpaper.IWallpaperConnection;
7360586f2ec65d16d185767fce4311d3ed0e9112acDan Egnorimport android.service.wallpaper.IWallpaperEngine;
7460586f2ec65d16d185767fce4311d3ed0e9112acDan Egnorimport android.service.wallpaper.IWallpaperService;
759066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectimport android.service.wallpaper.WallpaperService;
7660586f2ec65d16d185767fce4311d3ed0e9112acDan Egnorimport android.system.ErrnoException;
7760586f2ec65d16d185767fce4311d3ed0e9112acDan Egnorimport android.system.Os;
789d4b57545300c6de1722094404ae09bf0f6be937Dan Egnorimport android.util.EventLog;
799066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectimport android.util.Slog;
8060586f2ec65d16d185767fce4311d3ed0e9112acDan Egnorimport android.util.SparseArray;
8160586f2ec65d16d185767fce4311d3ed0e9112acDan Egnorimport android.util.Xml;
829d4b57545300c6de1722094404ae09bf0f6be937Dan Egnorimport android.view.Display;
839066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectimport android.view.IWindowManager;
849066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectimport android.view.WindowManager;
859d4b57545300c6de1722094404ae09bf0f6be937Dan Egnor
869d4b57545300c6de1722094404ae09bf0f6be937Dan Egnorimport com.android.internal.R;
8760586f2ec65d16d185767fce4311d3ed0e9112acDan Egnorimport com.android.internal.content.PackageMonitor;
8860586f2ec65d16d185767fce4311d3ed0e9112acDan Egnorimport com.android.internal.os.BackgroundThread;
899d4b57545300c6de1722094404ae09bf0f6be937Dan Egnorimport com.android.internal.util.FastXmlSerializer;
909066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectimport com.android.internal.util.JournaledFile;
919066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectimport com.android.server.EventLogTags;
929066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectimport com.android.server.SystemService;
939d4b57545300c6de1722094404ae09bf0f6be937Dan Egnor
949066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectimport libcore.io.IoUtils;
9560586f2ec65d16d185767fce4311d3ed0e9112acDan Egnor
9660586f2ec65d16d185767fce4311d3ed0e9112acDan Egnorimport org.xmlpull.v1.XmlPullParser;
9760586f2ec65d16d185767fce4311d3ed0e9112acDan Egnorimport org.xmlpull.v1.XmlPullParserException;
989066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectimport org.xmlpull.v1.XmlSerializer;
9960586f2ec65d16d185767fce4311d3ed0e9112acDan Egnor
1009d4b57545300c6de1722094404ae09bf0f6be937Dan Egnorimport java.io.BufferedOutputStream;
1019066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectimport java.io.File;
1029066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectimport java.io.FileDescriptor;
1039066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectimport java.io.FileInputStream;
1049d4b57545300c6de1722094404ae09bf0f6be937Dan Egnorimport java.io.FileNotFoundException;
1059d4b57545300c6de1722094404ae09bf0f6be937Dan Egnorimport java.io.FileOutputStream;
1069066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectimport java.io.IOException;
10760586f2ec65d16d185767fce4311d3ed0e9112acDan Egnorimport java.io.InputStream;
10860586f2ec65d16d185767fce4311d3ed0e9112acDan Egnorimport java.io.PrintWriter;
10960586f2ec65d16d185767fce4311d3ed0e9112acDan Egnorimport java.nio.charset.StandardCharsets;
11060586f2ec65d16d185767fce4311d3ed0e9112acDan Egnorimport java.util.Arrays;
11160586f2ec65d16d185767fce4311d3ed0e9112acDan Egnorimport java.util.List;
1129d4b57545300c6de1722094404ae09bf0f6be937Dan Egnor
1139d4b57545300c6de1722094404ae09bf0f6be937Dan Egnorpublic class WallpaperManagerService extends IWallpaperManager.Stub {
1149d4b57545300c6de1722094404ae09bf0f6be937Dan Egnor    static final String TAG = "WallpaperManagerService";
1159d4b57545300c6de1722094404ae09bf0f6be937Dan Egnor    static final boolean DEBUG = false;
1169d4b57545300c6de1722094404ae09bf0f6be937Dan Egnor
1179d4b57545300c6de1722094404ae09bf0f6be937Dan Egnor    public static class Lifecycle extends SystemService {
1189d4b57545300c6de1722094404ae09bf0f6be937Dan Egnor        private WallpaperManagerService mService;
1199d4b57545300c6de1722094404ae09bf0f6be937Dan Egnor
1209d4b57545300c6de1722094404ae09bf0f6be937Dan Egnor        public Lifecycle(Context context) {
1219d4b57545300c6de1722094404ae09bf0f6be937Dan Egnor            super(context);
1229d4b57545300c6de1722094404ae09bf0f6be937Dan Egnor        }
1239d4b57545300c6de1722094404ae09bf0f6be937Dan Egnor
1249d4b57545300c6de1722094404ae09bf0f6be937Dan Egnor        @Override
1259d4b57545300c6de1722094404ae09bf0f6be937Dan Egnor        public void onStart() {
1269d4b57545300c6de1722094404ae09bf0f6be937Dan Egnor            mService = new WallpaperManagerService(getContext());
1279d4b57545300c6de1722094404ae09bf0f6be937Dan Egnor            publishBinderService(Context.WALLPAPER_SERVICE, mService);
1289d4b57545300c6de1722094404ae09bf0f6be937Dan Egnor        }
1299066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
1309066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        @Override
13160586f2ec65d16d185767fce4311d3ed0e9112acDan Egnor        public void onBootPhase(int phase) {
13260586f2ec65d16d185767fce4311d3ed0e9112acDan Egnor            if (phase == SystemService.PHASE_ACTIVITY_MANAGER_READY) {
13360586f2ec65d16d185767fce4311d3ed0e9112acDan Egnor                mService.systemReady();
13460586f2ec65d16d185767fce4311d3ed0e9112acDan Egnor            } else if (phase == SystemService.PHASE_THIRD_PARTY_APPS_CAN_START) {
13560586f2ec65d16d185767fce4311d3ed0e9112acDan Egnor                mService.switchUser(UserHandle.USER_SYSTEM, null);
13660586f2ec65d16d185767fce4311d3ed0e9112acDan Egnor            }
13760586f2ec65d16d185767fce4311d3ed0e9112acDan Egnor        }
13860586f2ec65d16d185767fce4311d3ed0e9112acDan Egnor
13960586f2ec65d16d185767fce4311d3ed0e9112acDan Egnor        @Override
14060586f2ec65d16d185767fce4311d3ed0e9112acDan Egnor        public void onUnlockUser(int userHandle) {
14160586f2ec65d16d185767fce4311d3ed0e9112acDan Egnor            mService.onUnlockUser(userHandle);
14260586f2ec65d16d185767fce4311d3ed0e9112acDan Egnor        }
14360586f2ec65d16d185767fce4311d3ed0e9112acDan Egnor    }
1449d4b57545300c6de1722094404ae09bf0f6be937Dan Egnor
1459066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    final Object mLock = new Object();
1469066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
14760586f2ec65d16d185767fce4311d3ed0e9112acDan Egnor    /**
14860586f2ec65d16d185767fce4311d3ed0e9112acDan Egnor     * Minimum time between crashes of a wallpaper service for us to consider
14960586f2ec65d16d185767fce4311d3ed0e9112acDan Egnor     * restarting it vs. just reverting to the static wallpaper.
15060586f2ec65d16d185767fce4311d3ed0e9112acDan Egnor     */
15160586f2ec65d16d185767fce4311d3ed0e9112acDan Egnor    static final long MIN_WALLPAPER_CRASH_TIME = 10000;
15260586f2ec65d16d185767fce4311d3ed0e9112acDan Egnor    static final int MAX_WALLPAPER_COMPONENT_LOG_LENGTH = 128;
15360586f2ec65d16d185767fce4311d3ed0e9112acDan Egnor    static final String WALLPAPER = "wallpaper_orig";
15460586f2ec65d16d185767fce4311d3ed0e9112acDan Egnor    static final String WALLPAPER_CROP = "wallpaper";
1559066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    static final String WALLPAPER_LOCK_ORIG = "wallpaper_lock_orig";
15660586f2ec65d16d185767fce4311d3ed0e9112acDan Egnor    static final String WALLPAPER_LOCK_CROP = "wallpaper_lock";
1579066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    static final String WALLPAPER_INFO = "wallpaper_info.xml";
15860586f2ec65d16d185767fce4311d3ed0e9112acDan Egnor
1599d4b57545300c6de1722094404ae09bf0f6be937Dan Egnor    // All the various per-user state files we need to be aware of
1609d4b57545300c6de1722094404ae09bf0f6be937Dan Egnor    static final String[] sPerUserFiles = new String[] {
1619d4b57545300c6de1722094404ae09bf0f6be937Dan Egnor        WALLPAPER, WALLPAPER_CROP,
16260586f2ec65d16d185767fce4311d3ed0e9112acDan Egnor        WALLPAPER_LOCK_ORIG, WALLPAPER_LOCK_CROP,
1639d4b57545300c6de1722094404ae09bf0f6be937Dan Egnor        WALLPAPER_INFO
1649d4b57545300c6de1722094404ae09bf0f6be937Dan Egnor    };
1659d4b57545300c6de1722094404ae09bf0f6be937Dan Egnor
1669d4b57545300c6de1722094404ae09bf0f6be937Dan Egnor    /**
1679d4b57545300c6de1722094404ae09bf0f6be937Dan Egnor     * Observes the wallpaper for changes and notifies all IWallpaperServiceCallbacks
16860586f2ec65d16d185767fce4311d3ed0e9112acDan Egnor     * that the wallpaper has changed. The CREATE is triggered when there is no
1699066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * wallpaper set and is created for the first time. The CLOSE_WRITE is triggered
17060586f2ec65d16d185767fce4311d3ed0e9112acDan Egnor     * every time the wallpaper is changed.
17160586f2ec65d16d185767fce4311d3ed0e9112acDan Egnor     */
17260586f2ec65d16d185767fce4311d3ed0e9112acDan Egnor    private class WallpaperObserver extends FileObserver {
17360586f2ec65d16d185767fce4311d3ed0e9112acDan Egnor
1749066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        final int mUserId;
17560586f2ec65d16d185767fce4311d3ed0e9112acDan Egnor        final WallpaperData mWallpaper;
1769066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        final File mWallpaperDir;
1779066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        final File mWallpaperFile;
1789066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        final File mWallpaperLockFile;
17960586f2ec65d16d185767fce4311d3ed0e9112acDan Egnor
18060586f2ec65d16d185767fce4311d3ed0e9112acDan Egnor        public WallpaperObserver(WallpaperData wallpaper) {
18160586f2ec65d16d185767fce4311d3ed0e9112acDan Egnor            super(getWallpaperDir(wallpaper.userId).getAbsolutePath(),
18260586f2ec65d16d185767fce4311d3ed0e9112acDan Egnor                    CLOSE_WRITE | MOVED_TO | DELETE | DELETE_SELF);
18360586f2ec65d16d185767fce4311d3ed0e9112acDan Egnor            mUserId = wallpaper.userId;
1849066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            mWallpaperDir = getWallpaperDir(wallpaper.userId);
1859066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            mWallpaper = wallpaper;
18660586f2ec65d16d185767fce4311d3ed0e9112acDan Egnor            mWallpaperFile = new File(mWallpaperDir, WALLPAPER);
18760586f2ec65d16d185767fce4311d3ed0e9112acDan Egnor            mWallpaperLockFile = new File(mWallpaperDir, WALLPAPER_LOCK_ORIG);
18860586f2ec65d16d185767fce4311d3ed0e9112acDan Egnor        }
18960586f2ec65d16d185767fce4311d3ed0e9112acDan Egnor
19060586f2ec65d16d185767fce4311d3ed0e9112acDan Egnor        private WallpaperData dataForEvent(boolean sysChanged, boolean lockChanged) {
1919066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            WallpaperData wallpaper = null;
1929066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            synchronized (mLock) {
19360586f2ec65d16d185767fce4311d3ed0e9112acDan Egnor                if (lockChanged) {
19460586f2ec65d16d185767fce4311d3ed0e9112acDan Egnor                    wallpaper = mLockWallpaperMap.get(mUserId);
19560586f2ec65d16d185767fce4311d3ed0e9112acDan Egnor                }
19660586f2ec65d16d185767fce4311d3ed0e9112acDan Egnor                if (wallpaper == null) {
19760586f2ec65d16d185767fce4311d3ed0e9112acDan Egnor                    // no lock-specific wallpaper exists, or sys case, handled together
19860586f2ec65d16d185767fce4311d3ed0e9112acDan Egnor                    wallpaper = mWallpaperMap.get(mUserId);
19960586f2ec65d16d185767fce4311d3ed0e9112acDan Egnor                }
2009066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            }
2019066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            return (wallpaper != null) ? wallpaper : mWallpaper;
20260586f2ec65d16d185767fce4311d3ed0e9112acDan Egnor        }
20360586f2ec65d16d185767fce4311d3ed0e9112acDan Egnor
20460586f2ec65d16d185767fce4311d3ed0e9112acDan Egnor        @Override
20560586f2ec65d16d185767fce4311d3ed0e9112acDan Egnor        public void onEvent(int event, String path) {
20660586f2ec65d16d185767fce4311d3ed0e9112acDan Egnor            if (path == null) {
2079066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                return;
2089066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            }
20960586f2ec65d16d185767fce4311d3ed0e9112acDan Egnor            final boolean moved = (event == MOVED_TO);
21060586f2ec65d16d185767fce4311d3ed0e9112acDan Egnor            final boolean written = (event == CLOSE_WRITE || moved);
21160586f2ec65d16d185767fce4311d3ed0e9112acDan Egnor            final File changedFile = new File(mWallpaperDir, path);
21260586f2ec65d16d185767fce4311d3ed0e9112acDan Egnor
21360586f2ec65d16d185767fce4311d3ed0e9112acDan Egnor            // System and system+lock changes happen on the system wallpaper input file;
21460586f2ec65d16d185767fce4311d3ed0e9112acDan Egnor            // lock-only changes happen on the dedicated lock wallpaper input file
21560586f2ec65d16d185767fce4311d3ed0e9112acDan Egnor            final boolean sysWallpaperChanged = (mWallpaperFile.equals(changedFile));
21660586f2ec65d16d185767fce4311d3ed0e9112acDan Egnor            final boolean lockWallpaperChanged = (mWallpaperLockFile.equals(changedFile));
2179066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            WallpaperData wallpaper = dataForEvent(sysWallpaperChanged, lockWallpaperChanged);
21860586f2ec65d16d185767fce4311d3ed0e9112acDan Egnor
21960586f2ec65d16d185767fce4311d3ed0e9112acDan Egnor            if (DEBUG) {
22060586f2ec65d16d185767fce4311d3ed0e9112acDan Egnor                Slog.v(TAG, "Wallpaper file change: evt=" + event
22160586f2ec65d16d185767fce4311d3ed0e9112acDan Egnor                        + " path=" + path
22260586f2ec65d16d185767fce4311d3ed0e9112acDan Egnor                        + " sys=" + sysWallpaperChanged
2239066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                        + " lock=" + lockWallpaperChanged
2249066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                        + " imagePending=" + wallpaper.imageWallpaperPending
22560586f2ec65d16d185767fce4311d3ed0e9112acDan Egnor                        + " whichPending=0x" + Integer.toHexString(wallpaper.whichPending)
2269066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                        + " written=" + written);
22760586f2ec65d16d185767fce4311d3ed0e9112acDan Egnor            }
2289066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
2299066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            if (moved && lockWallpaperChanged) {
23060586f2ec65d16d185767fce4311d3ed0e9112acDan Egnor                // We just migrated sys -> lock to preserve imagery for an impending
2319066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                // new system-only wallpaper.  Tell keyguard about it and make sure it
23260586f2ec65d16d185767fce4311d3ed0e9112acDan Egnor                // has the right SELinux label.
2339066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                if (DEBUG) {
2349066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                    Slog.i(TAG, "Sys -> lock MOVED_TO");
235                }
236                SELinux.restorecon(changedFile);
237                notifyLockWallpaperChanged();
238                return;
239            }
240
241            synchronized (mLock) {
242                if (sysWallpaperChanged || lockWallpaperChanged) {
243                    notifyCallbacksLocked(wallpaper);
244                    if (wallpaper.wallpaperComponent == null
245                            || event != CLOSE_WRITE // includes the MOVED_TO case
246                            || wallpaper.imageWallpaperPending) {
247                        if (written) {
248                            // The image source has finished writing the source image,
249                            // so we now produce the crop rect (in the background), and
250                            // only publish the new displayable (sub)image as a result
251                            // of that work.
252                            if (DEBUG) {
253                                Slog.v(TAG, "Wallpaper written; generating crop");
254                            }
255                            SELinux.restorecon(changedFile);
256                            if (moved) {
257                                // This is a restore, so generate the crop using any just-restored new
258                                // crop guidelines, making sure to preserve our local dimension hints.
259                                // We also make sure to reapply the correct SELinux label.
260                                if (DEBUG) {
261                                    Slog.v(TAG, "moved-to, therefore restore; reloading metadata");
262                                }
263                                loadSettingsLocked(wallpaper.userId, true);
264                            }
265                            generateCrop(wallpaper);
266                            if (DEBUG) {
267                                Slog.v(TAG, "Crop done; invoking completion callback");
268                            }
269                            wallpaper.imageWallpaperPending = false;
270                            if (wallpaper.setComplete != null) {
271                                try {
272                                    wallpaper.setComplete.onWallpaperChanged();
273                                } catch (RemoteException e) {
274                                    // if this fails we don't really care; the setting app may just
275                                    // have crashed and that sort of thing is a fact of life.
276                                }
277                            }
278                            if (sysWallpaperChanged) {
279                                // If this was the system wallpaper, rebind...
280                                bindWallpaperComponentLocked(mImageWallpaper, true,
281                                        false, wallpaper, null);
282                            }
283                            if (lockWallpaperChanged
284                                    || (wallpaper.whichPending & FLAG_LOCK) != 0) {
285                                if (DEBUG) {
286                                    Slog.i(TAG, "Lock-relevant wallpaper changed");
287                                }
288                                // either a lock-only wallpaper commit or a system+lock event.
289                                // if it's system-plus-lock we need to wipe the lock bookkeeping;
290                                // we're falling back to displaying the system wallpaper there.
291                                if (!lockWallpaperChanged) {
292                                    mLockWallpaperMap.remove(wallpaper.userId);
293                                }
294                                // and in any case, tell keyguard about it
295                                notifyLockWallpaperChanged();
296                            }
297                            saveSettingsLocked(wallpaper.userId);
298                        }
299                    }
300                }
301            }
302        }
303    }
304
305    void notifyLockWallpaperChanged() {
306        final IWallpaperManagerCallback cb = mKeyguardListener;
307        if (cb != null) {
308            try {
309                cb.onWallpaperChanged();
310            } catch (RemoteException e) {
311                // Oh well it went away; no big deal
312            }
313        }
314    }
315
316    /**
317     * Once a new wallpaper has been written via setWallpaper(...), it needs to be cropped
318     * for display.
319     */
320    private void generateCrop(WallpaperData wallpaper) {
321        boolean success = false;
322
323        Rect cropHint = new Rect(wallpaper.cropHint);
324
325        if (DEBUG) {
326            Slog.v(TAG, "Generating crop for new wallpaper(s): 0x"
327                    + Integer.toHexString(wallpaper.whichPending)
328                    + " to " + wallpaper.cropFile.getName()
329                    + " crop=(" + cropHint.width() + 'x' + cropHint.height()
330                    + ") dim=(" + wallpaper.width + 'x' + wallpaper.height + ')');
331        }
332
333        // Analyse the source; needed in multiple cases
334        BitmapFactory.Options options = new BitmapFactory.Options();
335        options.inJustDecodeBounds = true;
336        BitmapFactory.decodeFile(wallpaper.wallpaperFile.getAbsolutePath(), options);
337        if (options.outWidth <= 0 || options.outHeight <= 0) {
338            Slog.e(TAG, "Invalid wallpaper data");
339            success = false;
340        } else {
341            boolean needCrop = false;
342            boolean needScale = false;
343
344            // Empty crop means use the full image
345            if (cropHint.isEmpty()) {
346                cropHint.left = cropHint.top = 0;
347                cropHint.right = options.outWidth;
348                cropHint.bottom = options.outHeight;
349            } else {
350                // force the crop rect to lie within the measured bounds
351                cropHint.offset(
352                        (cropHint.right > options.outWidth ? options.outWidth - cropHint.right : 0),
353                        (cropHint.bottom > options.outHeight ? options.outHeight - cropHint.bottom : 0));
354
355                // Don't bother cropping if what we're left with is identity
356                needCrop = (options.outHeight > cropHint.height()
357                        && options.outWidth > cropHint.width());
358            }
359
360            // scale if the crop height winds up not matching the recommended metrics
361            needScale = (wallpaper.height != cropHint.height());
362
363            if (DEBUG) {
364                Slog.v(TAG, "crop: w=" + cropHint.width() + " h=" + cropHint.height());
365                Slog.v(TAG, "dims: w=" + wallpaper.width + " h=" + wallpaper.height);
366                Slog.v(TAG, "meas: w=" + options.outWidth + " h=" + options.outHeight);
367                Slog.v(TAG, "crop?=" + needCrop + " scale?=" + needScale);
368            }
369
370            if (!needCrop && !needScale) {
371                // Simple case:  the nominal crop fits what we want, so we take
372                // the whole thing and just copy the image file directly.
373                if (DEBUG) {
374                    Slog.v(TAG, "Null crop of new wallpaper; copying");
375                }
376                success = FileUtils.copyFile(wallpaper.wallpaperFile, wallpaper.cropFile);
377                if (!success) {
378                    wallpaper.cropFile.delete();
379                    // TODO: fall back to default wallpaper in this case
380                }
381            } else {
382                // Fancy case: crop and scale.  First, we decode and scale down if appropriate.
383                FileOutputStream f = null;
384                BufferedOutputStream bos = null;
385                try {
386                    BitmapRegionDecoder decoder = BitmapRegionDecoder.newInstance(
387                            wallpaper.wallpaperFile.getAbsolutePath(), false);
388
389                    // This actually downsamples only by powers of two, but that's okay; we do
390                    // a proper scaling blit later.  This is to minimize transient RAM use.
391                    // We calculate the largest power-of-two under the actual ratio rather than
392                    // just let the decode take care of it because we also want to remap where the
393                    // cropHint rectangle lies in the decoded [super]rect.
394                    final BitmapFactory.Options scaler;
395                    final int actualScale = cropHint.height() / wallpaper.height;
396                    int scale = 1;
397                    while (2*scale < actualScale) {
398                        scale *= 2;
399                    }
400                    if (scale > 1) {
401                        scaler = new BitmapFactory.Options();
402                        scaler.inSampleSize = scale;
403                        if (DEBUG) {
404                            Slog.v(TAG, "Downsampling cropped rect with scale " + scale);
405                        }
406                    } else {
407                        scaler = null;
408                    }
409                    Bitmap cropped = decoder.decodeRegion(cropHint, scaler);
410                    decoder.recycle();
411
412                    if (cropped == null) {
413                        Slog.e(TAG, "Could not decode new wallpaper");
414                    } else {
415                        // We've got the extracted crop; now we want to scale it properly to
416                        // the desired rectangle.  That's a height-biased operation: make it
417                        // fit the hinted height, and accept whatever width we end up with.
418                        cropHint.offsetTo(0, 0);
419                        cropHint.right /= scale;    // adjust by downsampling factor
420                        cropHint.bottom /= scale;
421                        final float heightR = ((float)wallpaper.height) / ((float)cropHint.height());
422                        if (DEBUG) {
423                            Slog.v(TAG, "scale " + heightR + ", extracting " + cropHint);
424                        }
425                        final int destWidth = (int)(cropHint.width() * heightR);
426                        final Bitmap finalCrop = Bitmap.createScaledBitmap(cropped,
427                                destWidth, wallpaper.height, true);
428                        if (DEBUG) {
429                            Slog.v(TAG, "Final extract:");
430                            Slog.v(TAG, "  dims: w=" + wallpaper.width
431                                    + " h=" + wallpaper.height);
432                            Slog.v(TAG, "   out: w=" + finalCrop.getWidth()
433                                    + " h=" + finalCrop.getHeight());
434                        }
435
436                        f = new FileOutputStream(wallpaper.cropFile);
437                        bos = new BufferedOutputStream(f, 32*1024);
438                        finalCrop.compress(Bitmap.CompressFormat.JPEG, 100, bos);
439                        bos.flush();  // don't rely on the implicit flush-at-close when noting success
440                        success = true;
441                    }
442                } catch (Exception e) {
443                    if (DEBUG) {
444                        Slog.e(TAG, "Error decoding crop", e);
445                    }
446                } finally {
447                    IoUtils.closeQuietly(bos);
448                    IoUtils.closeQuietly(f);
449                }
450            }
451        }
452
453        if (!success) {
454            Slog.e(TAG, "Unable to apply new wallpaper");
455            wallpaper.cropFile.delete();
456        }
457
458        if (wallpaper.cropFile.exists()) {
459            boolean didRestorecon = SELinux.restorecon(wallpaper.cropFile.getAbsoluteFile());
460            if (DEBUG) {
461                Slog.v(TAG, "restorecon() of crop file returned " + didRestorecon);
462            }
463        }
464    }
465
466    final Context mContext;
467    final IWindowManager mIWindowManager;
468    final IPackageManager mIPackageManager;
469    final MyPackageMonitor mMonitor;
470    final AppOpsManager mAppOpsManager;
471    WallpaperData mLastWallpaper;
472    IWallpaperManagerCallback mKeyguardListener;
473    boolean mWaitingForUnlock;
474
475    /**
476     * ID of the current wallpaper, changed every time anything sets a wallpaper.
477     * This is used for external detection of wallpaper update activity.
478     */
479    int mWallpaperId;
480
481    /**
482     * Name of the component used to display bitmap wallpapers from either the gallery or
483     * built-in wallpapers.
484     */
485    final ComponentName mImageWallpaper;
486
487    final SparseArray<WallpaperData> mWallpaperMap = new SparseArray<WallpaperData>();
488    final SparseArray<WallpaperData> mLockWallpaperMap = new SparseArray<WallpaperData>();
489
490    final SparseArray<Boolean> mUserRestorecon = new SparseArray<Boolean>();
491    int mCurrentUserId;
492
493    static class WallpaperData {
494
495        int userId;
496
497        final File wallpaperFile;   // source image
498        final File cropFile;        // eventual destination
499
500        /**
501         * True while the client is writing a new wallpaper
502         */
503        boolean imageWallpaperPending;
504
505        /**
506         * Which new wallpapers are being written; mirrors the 'which'
507         * selector bit field to setWallpaper().
508         */
509        int whichPending;
510
511        /**
512         * Callback once the set + crop is finished
513         */
514        IWallpaperManagerCallback setComplete;
515
516        /**
517         * Is the OS allowed to back up this wallpaper imagery?
518         */
519        boolean allowBackup;
520
521        /**
522         * Resource name if using a picture from the wallpaper gallery
523         */
524        String name = "";
525
526        /**
527         * The component name of the currently set live wallpaper.
528         */
529        ComponentName wallpaperComponent;
530
531        /**
532         * The component name of the wallpaper that should be set next.
533         */
534        ComponentName nextWallpaperComponent;
535
536        /**
537         * The ID of this wallpaper
538         */
539        int wallpaperId;
540
541        WallpaperConnection connection;
542        long lastDiedTime;
543        boolean wallpaperUpdating;
544        WallpaperObserver wallpaperObserver;
545
546        /**
547         * List of callbacks registered they should each be notified when the wallpaper is changed.
548         */
549        private RemoteCallbackList<IWallpaperManagerCallback> callbacks
550                = new RemoteCallbackList<IWallpaperManagerCallback>();
551
552        int width = -1;
553        int height = -1;
554
555        /**
556         * The crop hint supplied for displaying a subset of the source image
557         */
558        final Rect cropHint = new Rect(0, 0, 0, 0);
559
560        final Rect padding = new Rect(0, 0, 0, 0);
561
562        WallpaperData(int userId, String inputFileName, String cropFileName) {
563            this.userId = userId;
564            final File wallpaperDir = getWallpaperDir(userId);
565            wallpaperFile = new File(wallpaperDir, inputFileName);
566            cropFile = new File(wallpaperDir, cropFileName);
567        }
568
569        // Called during initialization of a given user's wallpaper bookkeeping
570        boolean cropExists() {
571            return cropFile.exists();
572        }
573    }
574
575    int makeWallpaperIdLocked() {
576        do {
577            ++mWallpaperId;
578        } while (mWallpaperId == 0);
579        return mWallpaperId;
580    }
581
582    class WallpaperConnection extends IWallpaperConnection.Stub
583            implements ServiceConnection {
584        final WallpaperInfo mInfo;
585        final Binder mToken = new Binder();
586        IWallpaperService mService;
587        IWallpaperEngine mEngine;
588        WallpaperData mWallpaper;
589        IRemoteCallback mReply;
590
591        boolean mDimensionsChanged = false;
592        boolean mPaddingChanged = false;
593
594        public WallpaperConnection(WallpaperInfo info, WallpaperData wallpaper) {
595            mInfo = info;
596            mWallpaper = wallpaper;
597        }
598
599        @Override
600        public void onServiceConnected(ComponentName name, IBinder service) {
601            synchronized (mLock) {
602                if (mWallpaper.connection == this) {
603                    mService = IWallpaperService.Stub.asInterface(service);
604                    attachServiceLocked(this, mWallpaper);
605                    // XXX should probably do saveSettingsLocked() later
606                    // when we have an engine, but I'm not sure about
607                    // locking there and anyway we always need to be able to
608                    // recover if there is something wrong.
609                    saveSettingsLocked(mWallpaper.userId);
610                }
611            }
612        }
613
614        @Override
615        public void onServiceDisconnected(ComponentName name) {
616            synchronized (mLock) {
617                mService = null;
618                mEngine = null;
619                if (mWallpaper.connection == this) {
620                    Slog.w(TAG, "Wallpaper service gone: " + mWallpaper.wallpaperComponent);
621                    if (!mWallpaper.wallpaperUpdating
622                            && mWallpaper.userId == mCurrentUserId) {
623                        // There is a race condition which causes
624                        // {@link #mWallpaper.wallpaperUpdating} to be false even if it is
625                        // currently updating since the broadcast notifying us is async.
626                        // This race is overcome by the general rule that we only reset the
627                        // wallpaper if its service was shut down twice
628                        // during {@link #MIN_WALLPAPER_CRASH_TIME} millis.
629                        if (mWallpaper.lastDiedTime != 0
630                                && mWallpaper.lastDiedTime + MIN_WALLPAPER_CRASH_TIME
631                                    > SystemClock.uptimeMillis()) {
632                            Slog.w(TAG, "Reverting to built-in wallpaper!");
633                            clearWallpaperLocked(true, FLAG_SYSTEM, mWallpaper.userId, null);
634                        } else {
635                            mWallpaper.lastDiedTime = SystemClock.uptimeMillis();
636                        }
637                        final String flattened = name.flattenToString();
638                        EventLog.writeEvent(EventLogTags.WP_WALLPAPER_CRASHED,
639                                flattened.substring(0, Math.min(flattened.length(),
640                                        MAX_WALLPAPER_COMPONENT_LOG_LENGTH)));
641                    }
642                }
643            }
644        }
645
646        @Override
647        public void attachEngine(IWallpaperEngine engine) {
648            synchronized (mLock) {
649                mEngine = engine;
650                if (mDimensionsChanged) {
651                    try {
652                        mEngine.setDesiredSize(mWallpaper.width, mWallpaper.height);
653                    } catch (RemoteException e) {
654                        Slog.w(TAG, "Failed to set wallpaper dimensions", e);
655                    }
656                    mDimensionsChanged = false;
657                }
658                if (mPaddingChanged) {
659                    try {
660                        mEngine.setDisplayPadding(mWallpaper.padding);
661                    } catch (RemoteException e) {
662                        Slog.w(TAG, "Failed to set wallpaper padding", e);
663                    }
664                    mPaddingChanged = false;
665                }
666            }
667        }
668
669        @Override
670        public void engineShown(IWallpaperEngine engine) {
671            synchronized (mLock) {
672                if (mReply != null) {
673                    long ident = Binder.clearCallingIdentity();
674                    try {
675                        mReply.sendResult(null);
676                    } catch (RemoteException e) {
677                        Binder.restoreCallingIdentity(ident);
678                    }
679                    mReply = null;
680                }
681            }
682        }
683
684        @Override
685        public ParcelFileDescriptor setWallpaper(String name) {
686            synchronized (mLock) {
687                if (mWallpaper.connection == this) {
688                    return updateWallpaperBitmapLocked(name, mWallpaper, null);
689                }
690                return null;
691            }
692        }
693    }
694
695    class MyPackageMonitor extends PackageMonitor {
696        @Override
697        public void onPackageUpdateFinished(String packageName, int uid) {
698            synchronized (mLock) {
699                if (mCurrentUserId != getChangingUserId()) {
700                    return;
701                }
702                WallpaperData wallpaper = mWallpaperMap.get(mCurrentUserId);
703                if (wallpaper != null) {
704                    if (wallpaper.wallpaperComponent != null
705                            && wallpaper.wallpaperComponent.getPackageName().equals(packageName)) {
706                        wallpaper.wallpaperUpdating = false;
707                        ComponentName comp = wallpaper.wallpaperComponent;
708                        clearWallpaperComponentLocked(wallpaper);
709                        if (!bindWallpaperComponentLocked(comp, false, false,
710                                wallpaper, null)) {
711                            Slog.w(TAG, "Wallpaper no longer available; reverting to default");
712                            clearWallpaperLocked(false, FLAG_SYSTEM, wallpaper.userId, null);
713                        }
714                    }
715                }
716            }
717        }
718
719        @Override
720        public void onPackageModified(String packageName) {
721            synchronized (mLock) {
722                if (mCurrentUserId != getChangingUserId()) {
723                    return;
724                }
725                WallpaperData wallpaper = mWallpaperMap.get(mCurrentUserId);
726                if (wallpaper != null) {
727                    if (wallpaper.wallpaperComponent == null
728                            || !wallpaper.wallpaperComponent.getPackageName().equals(packageName)) {
729                        return;
730                    }
731                    doPackagesChangedLocked(true, wallpaper);
732                }
733            }
734        }
735
736        @Override
737        public void onPackageUpdateStarted(String packageName, int uid) {
738            synchronized (mLock) {
739                if (mCurrentUserId != getChangingUserId()) {
740                    return;
741                }
742                WallpaperData wallpaper = mWallpaperMap.get(mCurrentUserId);
743                if (wallpaper != null) {
744                    if (wallpaper.wallpaperComponent != null
745                            && wallpaper.wallpaperComponent.getPackageName().equals(packageName)) {
746                        wallpaper.wallpaperUpdating = true;
747                    }
748                }
749            }
750        }
751
752        @Override
753        public boolean onHandleForceStop(Intent intent, String[] packages, int uid, boolean doit) {
754            synchronized (mLock) {
755                boolean changed = false;
756                if (mCurrentUserId != getChangingUserId()) {
757                    return false;
758                }
759                WallpaperData wallpaper = mWallpaperMap.get(mCurrentUserId);
760                if (wallpaper != null) {
761                    boolean res = doPackagesChangedLocked(doit, wallpaper);
762                    changed |= res;
763                }
764                return changed;
765            }
766        }
767
768        @Override
769        public void onSomePackagesChanged() {
770            synchronized (mLock) {
771                if (mCurrentUserId != getChangingUserId()) {
772                    return;
773                }
774                WallpaperData wallpaper = mWallpaperMap.get(mCurrentUserId);
775                if (wallpaper != null) {
776                    doPackagesChangedLocked(true, wallpaper);
777                }
778            }
779        }
780
781        boolean doPackagesChangedLocked(boolean doit, WallpaperData wallpaper) {
782            boolean changed = false;
783            if (wallpaper.wallpaperComponent != null) {
784                int change = isPackageDisappearing(wallpaper.wallpaperComponent
785                        .getPackageName());
786                if (change == PACKAGE_PERMANENT_CHANGE
787                        || change == PACKAGE_TEMPORARY_CHANGE) {
788                    changed = true;
789                    if (doit) {
790                        Slog.w(TAG, "Wallpaper uninstalled, removing: "
791                                + wallpaper.wallpaperComponent);
792                        clearWallpaperLocked(false, FLAG_SYSTEM, wallpaper.userId, null);
793                    }
794                }
795            }
796            if (wallpaper.nextWallpaperComponent != null) {
797                int change = isPackageDisappearing(wallpaper.nextWallpaperComponent
798                        .getPackageName());
799                if (change == PACKAGE_PERMANENT_CHANGE
800                        || change == PACKAGE_TEMPORARY_CHANGE) {
801                    wallpaper.nextWallpaperComponent = null;
802                }
803            }
804            if (wallpaper.wallpaperComponent != null
805                    && isPackageModified(wallpaper.wallpaperComponent.getPackageName())) {
806                try {
807                    mContext.getPackageManager().getServiceInfo(wallpaper.wallpaperComponent,
808                            PackageManager.MATCH_DIRECT_BOOT_AWARE
809                                    | PackageManager.MATCH_DIRECT_BOOT_UNAWARE);
810                } catch (NameNotFoundException e) {
811                    Slog.w(TAG, "Wallpaper component gone, removing: "
812                            + wallpaper.wallpaperComponent);
813                    clearWallpaperLocked(false, FLAG_SYSTEM, wallpaper.userId, null);
814                }
815            }
816            if (wallpaper.nextWallpaperComponent != null
817                    && isPackageModified(wallpaper.nextWallpaperComponent.getPackageName())) {
818                try {
819                    mContext.getPackageManager().getServiceInfo(wallpaper.nextWallpaperComponent,
820                            PackageManager.MATCH_DIRECT_BOOT_AWARE
821                                    | PackageManager.MATCH_DIRECT_BOOT_UNAWARE);
822                } catch (NameNotFoundException e) {
823                    wallpaper.nextWallpaperComponent = null;
824                }
825            }
826            return changed;
827        }
828    }
829
830    public WallpaperManagerService(Context context) {
831        if (DEBUG) Slog.v(TAG, "WallpaperService startup");
832        mContext = context;
833        mImageWallpaper = ComponentName.unflattenFromString(
834                context.getResources().getString(R.string.image_wallpaper_component));
835        mIWindowManager = IWindowManager.Stub.asInterface(
836                ServiceManager.getService(Context.WINDOW_SERVICE));
837        mIPackageManager = AppGlobals.getPackageManager();
838        mAppOpsManager = (AppOpsManager) mContext.getSystemService(Context.APP_OPS_SERVICE);
839        mMonitor = new MyPackageMonitor();
840        mMonitor.register(context, null, UserHandle.ALL, true);
841        getWallpaperDir(UserHandle.USER_SYSTEM).mkdirs();
842        loadSettingsLocked(UserHandle.USER_SYSTEM, false);
843    }
844
845    private static File getWallpaperDir(int userId) {
846        return Environment.getUserSystemDirectory(userId);
847    }
848
849    @Override
850    protected void finalize() throws Throwable {
851        super.finalize();
852        for (int i = 0; i < mWallpaperMap.size(); i++) {
853            WallpaperData wallpaper = mWallpaperMap.valueAt(i);
854            wallpaper.wallpaperObserver.stopWatching();
855        }
856    }
857
858    void systemReady() {
859        if (DEBUG) Slog.v(TAG, "systemReady");
860        WallpaperData wallpaper = mWallpaperMap.get(UserHandle.USER_SYSTEM);
861        // If we think we're going to be using the system image wallpaper imagery, make
862        // sure we have something to render
863        if (mImageWallpaper.equals(wallpaper.nextWallpaperComponent)) {
864            // No crop file? Make sure we've finished the processing sequence if necessary
865            if (!wallpaper.cropExists()) {
866                if (DEBUG) {
867                    Slog.i(TAG, "No crop; regenerating from source");
868                }
869                generateCrop(wallpaper);
870            }
871            // Still nothing?  Fall back to default.
872            if (!wallpaper.cropExists()) {
873                if (DEBUG) {
874                    Slog.i(TAG, "Unable to regenerate crop; resetting");
875                }
876                clearWallpaperLocked(false, FLAG_SYSTEM, UserHandle.USER_SYSTEM, null);
877            }
878        } else {
879            if (DEBUG) {
880                Slog.i(TAG, "Nondefault wallpaper component; gracefully ignoring");
881            }
882        }
883
884        IntentFilter userFilter = new IntentFilter();
885        userFilter.addAction(Intent.ACTION_USER_REMOVED);
886        mContext.registerReceiver(new BroadcastReceiver() {
887            @Override
888            public void onReceive(Context context, Intent intent) {
889                final String action = intent.getAction();
890                if (Intent.ACTION_USER_REMOVED.equals(action)) {
891                    onRemoveUser(intent.getIntExtra(Intent.EXTRA_USER_HANDLE,
892                            UserHandle.USER_NULL));
893                }
894            }
895        }, userFilter);
896
897        try {
898            ActivityManagerNative.getDefault().registerUserSwitchObserver(
899                    new IUserSwitchObserver.Stub() {
900                        @Override
901                        public void onUserSwitching(int newUserId, IRemoteCallback reply) {
902                            switchUser(newUserId, reply);
903                        }
904
905                        @Override
906                        public void onUserSwitchComplete(int newUserId) throws RemoteException {
907                        }
908
909                        @Override
910                        public void onForegroundProfileSwitch(int newProfileId) {
911                            // Ignore.
912                        }
913                    }, TAG);
914        } catch (RemoteException e) {
915            e.rethrowAsRuntimeException();
916        }
917    }
918
919    /** Called by SystemBackupAgent */
920    public String getName() {
921        // Verify caller is the system
922        if (Binder.getCallingUid() != android.os.Process.SYSTEM_UID) {
923            throw new RuntimeException("getName() can only be called from the system process");
924        }
925        synchronized (mLock) {
926            return mWallpaperMap.get(0).name;
927        }
928    }
929
930    void stopObserver(WallpaperData wallpaper) {
931        if (wallpaper != null) {
932            if (wallpaper.wallpaperObserver != null) {
933                wallpaper.wallpaperObserver.stopWatching();
934                wallpaper.wallpaperObserver = null;
935            }
936        }
937    }
938
939    void stopObserversLocked(int userId) {
940        stopObserver(mWallpaperMap.get(userId));
941        stopObserver(mLockWallpaperMap.get(userId));
942        mWallpaperMap.remove(userId);
943        mLockWallpaperMap.remove(userId);
944    }
945
946    void onUnlockUser(final int userId) {
947        synchronized (mLock) {
948            if (mCurrentUserId == userId) {
949                if (mWaitingForUnlock) {
950                    // If we're switching users, now is when we transition the wallpaper
951                    switchUser(userId, null);
952                }
953
954                // Make sure that the SELinux labeling of all the relevant files is correct.
955                // This corrects for mislabeling bugs that might have arisen from move-to
956                // operations involving the wallpaper files.  This isn't timing-critical,
957                // so we do it in the background to avoid holding up the user unlock operation.
958                if (mUserRestorecon.get(userId) != Boolean.TRUE) {
959                    mUserRestorecon.put(userId, Boolean.TRUE);
960                    Runnable relabeler = new Runnable() {
961                        @Override
962                        public void run() {
963                            final File wallpaperDir = getWallpaperDir(userId);
964                            for (String filename : sPerUserFiles) {
965                                File f = new File(wallpaperDir, filename);
966                                if (f.exists()) {
967                                    SELinux.restorecon(f);
968                                }
969                            }
970                        }
971                    };
972                    BackgroundThread.getHandler().post(relabeler);
973                }
974            }
975        }
976    }
977
978    void onRemoveUser(int userId) {
979        if (userId < 1) return;
980
981        final File wallpaperDir = getWallpaperDir(userId);
982        synchronized (mLock) {
983            stopObserversLocked(userId);
984            for (String filename : sPerUserFiles) {
985                new File(wallpaperDir, filename).delete();
986            }
987        }
988    }
989
990    void switchUser(int userId, IRemoteCallback reply) {
991        synchronized (mLock) {
992            mCurrentUserId = userId;
993            WallpaperData wallpaper = getWallpaperSafeLocked(userId, FLAG_SYSTEM);
994            // Not started watching yet, in case wallpaper data was loaded for other reasons.
995            if (wallpaper.wallpaperObserver == null) {
996                wallpaper.wallpaperObserver = new WallpaperObserver(wallpaper);
997                wallpaper.wallpaperObserver.startWatching();
998            }
999            switchWallpaper(wallpaper, reply);
1000        }
1001    }
1002
1003    void switchWallpaper(WallpaperData wallpaper, IRemoteCallback reply) {
1004        synchronized (mLock) {
1005            mWaitingForUnlock = false;
1006            final ComponentName cname = wallpaper.wallpaperComponent != null ?
1007                    wallpaper.wallpaperComponent : wallpaper.nextWallpaperComponent;
1008            if (!bindWallpaperComponentLocked(cname, true, false, wallpaper, reply)) {
1009                // We failed to bind the desired wallpaper, but that might
1010                // happen if the wallpaper isn't direct-boot aware
1011                ServiceInfo si = null;
1012                try {
1013                    si = mIPackageManager.getServiceInfo(cname,
1014                            PackageManager.MATCH_DIRECT_BOOT_UNAWARE, wallpaper.userId);
1015                } catch (RemoteException ignored) {
1016                }
1017
1018                if (si == null) {
1019                    Slog.w(TAG, "Failure starting previous wallpaper; clearing");
1020                    clearWallpaperLocked(false, FLAG_SYSTEM, wallpaper.userId, reply);
1021                } else {
1022                    Slog.w(TAG, "Wallpaper isn't direct boot aware; using fallback until unlocked");
1023                    // We might end up persisting the current wallpaper data
1024                    // while locked, so pretend like the component was actually
1025                    // bound into place
1026                    wallpaper.wallpaperComponent = wallpaper.nextWallpaperComponent;
1027                    final WallpaperData fallback = new WallpaperData(wallpaper.userId,
1028                            WALLPAPER_LOCK_ORIG, WALLPAPER_LOCK_CROP);
1029                    ensureSaneWallpaperData(fallback);
1030                    bindWallpaperComponentLocked(mImageWallpaper, true, false, fallback, reply);
1031                    mWaitingForUnlock = true;
1032                }
1033            }
1034        }
1035    }
1036
1037    @Override
1038    public void clearWallpaper(String callingPackage, int which, int userId) {
1039        if (DEBUG) Slog.v(TAG, "clearWallpaper");
1040        checkPermission(android.Manifest.permission.SET_WALLPAPER);
1041        if (!isWallpaperSupported(callingPackage) || !isSetWallpaperAllowed(callingPackage)) {
1042            return;
1043        }
1044        userId = ActivityManager.handleIncomingUser(Binder.getCallingPid(),
1045                Binder.getCallingUid(), userId, false, true, "clearWallpaper", null);
1046
1047        synchronized (mLock) {
1048            clearWallpaperLocked(false, which, userId, null);
1049        }
1050    }
1051
1052    void clearWallpaperLocked(boolean defaultFailed, int which, int userId, IRemoteCallback reply) {
1053        if (which != FLAG_SYSTEM && which != FLAG_LOCK) {
1054            throw new IllegalArgumentException("Must specify exactly one kind of wallpaper to read");
1055        }
1056
1057        WallpaperData wallpaper = null;
1058        if (which == FLAG_LOCK) {
1059            wallpaper = mLockWallpaperMap.get(userId);
1060            if (wallpaper == null) {
1061                // It's already gone; we're done.
1062                if (DEBUG) {
1063                    Slog.i(TAG, "Lock wallpaper already cleared");
1064                }
1065                return;
1066            }
1067        } else {
1068            wallpaper = mWallpaperMap.get(userId);
1069            if (wallpaper == null) {
1070                // Might need to bring it in the first time to establish our rewrite
1071                loadSettingsLocked(userId, false);
1072                wallpaper = mWallpaperMap.get(userId);
1073            }
1074        }
1075        if (wallpaper == null) {
1076            return;
1077        }
1078
1079        final long ident = Binder.clearCallingIdentity();
1080        try {
1081            if (wallpaper.wallpaperFile.exists()) {
1082                wallpaper.wallpaperFile.delete();
1083                wallpaper.cropFile.delete();
1084                if (which == FLAG_LOCK) {
1085                    mLockWallpaperMap.remove(userId);
1086                    final IWallpaperManagerCallback cb = mKeyguardListener;
1087                    if (cb != null) {
1088                        if (DEBUG) {
1089                            Slog.i(TAG, "Notifying keyguard of lock wallpaper clear");
1090                        }
1091                        try {
1092                            cb.onWallpaperChanged();
1093                        } catch (RemoteException e) {
1094                            // Oh well it went away; no big deal
1095                        }
1096                    }
1097                    saveSettingsLocked(userId);
1098                    return;
1099                }
1100            }
1101
1102            RuntimeException e = null;
1103            try {
1104                wallpaper.imageWallpaperPending = false;
1105                if (userId != mCurrentUserId) return;
1106                if (bindWallpaperComponentLocked(defaultFailed
1107                        ? mImageWallpaper
1108                                : null, true, false, wallpaper, reply)) {
1109                    return;
1110                }
1111            } catch (IllegalArgumentException e1) {
1112                e = e1;
1113            }
1114
1115            // This can happen if the default wallpaper component doesn't
1116            // exist.  This should be a system configuration problem, but
1117            // let's not let it crash the system and just live with no
1118            // wallpaper.
1119            Slog.e(TAG, "Default wallpaper component not found!", e);
1120            clearWallpaperComponentLocked(wallpaper);
1121            if (reply != null) {
1122                try {
1123                    reply.sendResult(null);
1124                } catch (RemoteException e1) {
1125                }
1126            }
1127        } finally {
1128            Binder.restoreCallingIdentity(ident);
1129        }
1130    }
1131
1132    public boolean hasNamedWallpaper(String name) {
1133        synchronized (mLock) {
1134            List<UserInfo> users;
1135            long ident = Binder.clearCallingIdentity();
1136            try {
1137                users = ((UserManager) mContext.getSystemService(Context.USER_SERVICE)).getUsers();
1138            } finally {
1139                Binder.restoreCallingIdentity(ident);
1140            }
1141            for (UserInfo user: users) {
1142                // ignore managed profiles
1143                if (user.isManagedProfile()) {
1144                    continue;
1145                }
1146                WallpaperData wd = mWallpaperMap.get(user.id);
1147                if (wd == null) {
1148                    // User hasn't started yet, so load her settings to peek at the wallpaper
1149                    loadSettingsLocked(user.id, false);
1150                    wd = mWallpaperMap.get(user.id);
1151                }
1152                if (wd != null && name.equals(wd.name)) {
1153                    return true;
1154                }
1155            }
1156        }
1157        return false;
1158    }
1159
1160    private Point getDefaultDisplaySize() {
1161        Point p = new Point();
1162        WindowManager wm = (WindowManager)mContext.getSystemService(Context.WINDOW_SERVICE);
1163        Display d = wm.getDefaultDisplay();
1164        d.getRealSize(p);
1165        return p;
1166    }
1167
1168    public void setDimensionHints(int width, int height, String callingPackage)
1169            throws RemoteException {
1170        checkPermission(android.Manifest.permission.SET_WALLPAPER_HINTS);
1171        if (!isWallpaperSupported(callingPackage)) {
1172            return;
1173        }
1174        synchronized (mLock) {
1175            int userId = UserHandle.getCallingUserId();
1176            WallpaperData wallpaper = getWallpaperSafeLocked(userId, FLAG_SYSTEM);
1177            if (width <= 0 || height <= 0) {
1178                throw new IllegalArgumentException("width and height must be > 0");
1179            }
1180            // Make sure it is at least as large as the display.
1181            Point displaySize = getDefaultDisplaySize();
1182            width = Math.max(width, displaySize.x);
1183            height = Math.max(height, displaySize.y);
1184
1185            if (width != wallpaper.width || height != wallpaper.height) {
1186                wallpaper.width = width;
1187                wallpaper.height = height;
1188                saveSettingsLocked(userId);
1189                if (mCurrentUserId != userId) return; // Don't change the properties now
1190                if (wallpaper.connection != null) {
1191                    if (wallpaper.connection.mEngine != null) {
1192                        try {
1193                            wallpaper.connection.mEngine.setDesiredSize(
1194                                    width, height);
1195                        } catch (RemoteException e) {
1196                        }
1197                        notifyCallbacksLocked(wallpaper);
1198                    } else if (wallpaper.connection.mService != null) {
1199                        // We've attached to the service but the engine hasn't attached back to us
1200                        // yet. This means it will be created with the previous dimensions, so we
1201                        // need to update it to the new dimensions once it attaches.
1202                        wallpaper.connection.mDimensionsChanged = true;
1203                    }
1204                }
1205            }
1206        }
1207    }
1208
1209    public int getWidthHint() throws RemoteException {
1210        synchronized (mLock) {
1211            WallpaperData wallpaper = mWallpaperMap.get(UserHandle.getCallingUserId());
1212            if (wallpaper != null) {
1213                return wallpaper.width;
1214            } else {
1215                return 0;
1216            }
1217        }
1218    }
1219
1220    public int getHeightHint() throws RemoteException {
1221        synchronized (mLock) {
1222            WallpaperData wallpaper = mWallpaperMap.get(UserHandle.getCallingUserId());
1223            if (wallpaper != null) {
1224                return wallpaper.height;
1225            } else {
1226                return 0;
1227            }
1228        }
1229    }
1230
1231    public void setDisplayPadding(Rect padding, String callingPackage) {
1232        checkPermission(android.Manifest.permission.SET_WALLPAPER_HINTS);
1233        if (!isWallpaperSupported(callingPackage)) {
1234            return;
1235        }
1236        synchronized (mLock) {
1237            int userId = UserHandle.getCallingUserId();
1238            WallpaperData wallpaper = getWallpaperSafeLocked(userId, FLAG_SYSTEM);
1239            if (padding.left < 0 || padding.top < 0 || padding.right < 0 || padding.bottom < 0) {
1240                throw new IllegalArgumentException("padding must be positive: " + padding);
1241            }
1242
1243            if (!padding.equals(wallpaper.padding)) {
1244                wallpaper.padding.set(padding);
1245                saveSettingsLocked(userId);
1246                if (mCurrentUserId != userId) return; // Don't change the properties now
1247                if (wallpaper.connection != null) {
1248                    if (wallpaper.connection.mEngine != null) {
1249                        try {
1250                            wallpaper.connection.mEngine.setDisplayPadding(padding);
1251                        } catch (RemoteException e) {
1252                        }
1253                        notifyCallbacksLocked(wallpaper);
1254                    } else if (wallpaper.connection.mService != null) {
1255                        // We've attached to the service but the engine hasn't attached back to us
1256                        // yet. This means it will be created with the previous dimensions, so we
1257                        // need to update it to the new dimensions once it attaches.
1258                        wallpaper.connection.mPaddingChanged = true;
1259                    }
1260                }
1261            }
1262        }
1263    }
1264
1265    @Override
1266    public ParcelFileDescriptor getWallpaper(IWallpaperManagerCallback cb, final int which,
1267            Bundle outParams, int wallpaperUserId) {
1268        wallpaperUserId = ActivityManager.handleIncomingUser(Binder.getCallingPid(),
1269                Binder.getCallingUid(), wallpaperUserId, false, true, "getWallpaper", null);
1270
1271        if (which != FLAG_SYSTEM && which != FLAG_LOCK) {
1272            throw new IllegalArgumentException("Must specify exactly one kind of wallpaper to read");
1273        }
1274
1275        synchronized (mLock) {
1276            final SparseArray<WallpaperData> whichSet =
1277                    (which == FLAG_LOCK) ? mLockWallpaperMap : mWallpaperMap;
1278            WallpaperData wallpaper = whichSet.get(wallpaperUserId);
1279            if (wallpaper == null) {
1280                // common case, this is the first lookup post-boot of the system or
1281                // unified lock, so we bring up the saved state lazily now and recheck.
1282                loadSettingsLocked(wallpaperUserId, false);
1283                wallpaper = whichSet.get(wallpaperUserId);
1284                if (wallpaper == null) {
1285                    return null;
1286                }
1287            }
1288            try {
1289                if (outParams != null) {
1290                    outParams.putInt("width", wallpaper.width);
1291                    outParams.putInt("height", wallpaper.height);
1292                }
1293                if (cb != null) {
1294                    wallpaper.callbacks.register(cb);
1295                }
1296                if (!wallpaper.cropFile.exists()) {
1297                    return null;
1298                }
1299                return ParcelFileDescriptor.open(wallpaper.cropFile, MODE_READ_ONLY);
1300            } catch (FileNotFoundException e) {
1301                /* Shouldn't happen as we check to see if the file exists */
1302                Slog.w(TAG, "Error getting wallpaper", e);
1303            }
1304            return null;
1305        }
1306    }
1307
1308    @Override
1309    public WallpaperInfo getWallpaperInfo(int userId) {
1310        userId = ActivityManager.handleIncomingUser(Binder.getCallingPid(),
1311                Binder.getCallingUid(), userId, false, true, "getWallpaperIdForUser", null);
1312        synchronized (mLock) {
1313            WallpaperData wallpaper = mWallpaperMap.get(userId);
1314            if (wallpaper != null && wallpaper.connection != null) {
1315                return wallpaper.connection.mInfo;
1316            }
1317            return null;
1318        }
1319    }
1320
1321    @Override
1322    public int getWallpaperIdForUser(int which, int userId) {
1323        userId = ActivityManager.handleIncomingUser(Binder.getCallingPid(),
1324                Binder.getCallingUid(), userId, false, true, "getWallpaperIdForUser", null);
1325
1326        if (which != FLAG_SYSTEM && which != FLAG_LOCK) {
1327            throw new IllegalArgumentException("Must specify exactly one kind of wallpaper");
1328        }
1329
1330        final SparseArray<WallpaperData> map =
1331                (which == FLAG_LOCK) ? mLockWallpaperMap : mWallpaperMap;
1332        synchronized (mLock) {
1333            WallpaperData wallpaper = map.get(userId);
1334            if (wallpaper != null) {
1335                return wallpaper.wallpaperId;
1336            }
1337        }
1338        return -1;
1339    }
1340
1341    @Override
1342    public boolean setLockWallpaperCallback(IWallpaperManagerCallback cb) {
1343        checkPermission(android.Manifest.permission.INTERNAL_SYSTEM_WINDOW);
1344        synchronized (mLock) {
1345            mKeyguardListener = cb;
1346        }
1347        return true;
1348    }
1349
1350    @Override
1351    public ParcelFileDescriptor setWallpaper(String name, String callingPackage,
1352            Rect cropHint, boolean allowBackup, Bundle extras, int which,
1353            IWallpaperManagerCallback completion) {
1354        checkPermission(android.Manifest.permission.SET_WALLPAPER);
1355
1356        if ((which & (FLAG_LOCK|FLAG_SYSTEM)) == 0) {
1357            final String msg = "Must specify a valid wallpaper category to set";
1358            Slog.e(TAG, msg);
1359            throw new IllegalArgumentException(msg);
1360        }
1361
1362        if (!isWallpaperSupported(callingPackage) || !isSetWallpaperAllowed(callingPackage)) {
1363            return null;
1364        }
1365
1366        // "null" means the no-op crop, preserving the full input image
1367        if (cropHint == null) {
1368            cropHint = new Rect(0, 0, 0, 0);
1369        } else {
1370            if (cropHint.isEmpty()
1371                    || cropHint.left < 0
1372                    || cropHint.top < 0) {
1373                throw new IllegalArgumentException("Invalid crop rect supplied: " + cropHint);
1374            }
1375        }
1376
1377        final int userId = UserHandle.getCallingUserId();
1378
1379        synchronized (mLock) {
1380            if (DEBUG) Slog.v(TAG, "setWallpaper which=0x" + Integer.toHexString(which));
1381            WallpaperData wallpaper;
1382
1383            /* If we're setting system but not lock, and lock is currently sharing the system
1384             * wallpaper, we need to migrate that image over to being lock-only before
1385             * the caller here writes new bitmap data.
1386             */
1387            if (which == FLAG_SYSTEM && mLockWallpaperMap.get(userId) == null) {
1388                if (DEBUG) {
1389                    Slog.i(TAG, "Migrating system->lock to preserve");
1390                }
1391                migrateSystemToLockWallpaperLocked(userId);
1392            }
1393
1394            wallpaper = getWallpaperSafeLocked(userId, which);
1395            final long ident = Binder.clearCallingIdentity();
1396            try {
1397                ParcelFileDescriptor pfd = updateWallpaperBitmapLocked(name, wallpaper, extras);
1398                if (pfd != null) {
1399                    wallpaper.imageWallpaperPending = true;
1400                    wallpaper.whichPending = which;
1401                    wallpaper.setComplete = completion;
1402                    wallpaper.cropHint.set(cropHint);
1403                    if ((which & FLAG_SYSTEM) != 0) {
1404                        wallpaper.allowBackup = allowBackup;
1405                    }
1406                }
1407                return pfd;
1408            } finally {
1409                Binder.restoreCallingIdentity(ident);
1410            }
1411        }
1412    }
1413
1414    private void migrateSystemToLockWallpaperLocked(int userId) {
1415        WallpaperData sysWP = mWallpaperMap.get(userId);
1416        if (sysWP == null) {
1417            if (DEBUG) {
1418                Slog.i(TAG, "No system wallpaper?  Not tracking for lock-only");
1419            }
1420            return;
1421        }
1422
1423        // We know a-priori that there is no lock-only wallpaper currently
1424        WallpaperData lockWP = new WallpaperData(userId,
1425                WALLPAPER_LOCK_ORIG, WALLPAPER_LOCK_CROP);
1426        lockWP.wallpaperId = sysWP.wallpaperId;
1427        lockWP.cropHint.set(sysWP.cropHint);
1428        lockWP.width = sysWP.width;
1429        lockWP.height = sysWP.height;
1430        lockWP.allowBackup = false;
1431
1432        // Migrate the bitmap files outright; no need to copy
1433        try {
1434            Os.rename(sysWP.wallpaperFile.getAbsolutePath(), lockWP.wallpaperFile.getAbsolutePath());
1435            Os.rename(sysWP.cropFile.getAbsolutePath(), lockWP.cropFile.getAbsolutePath());
1436        } catch (ErrnoException e) {
1437            Slog.e(TAG, "Can't migrate system wallpaper: " + e.getMessage());
1438            lockWP.wallpaperFile.delete();
1439            lockWP.cropFile.delete();
1440            return;
1441        }
1442
1443        mLockWallpaperMap.put(userId, lockWP);
1444    }
1445
1446    ParcelFileDescriptor updateWallpaperBitmapLocked(String name, WallpaperData wallpaper,
1447            Bundle extras) {
1448        if (name == null) name = "";
1449        try {
1450            File dir = getWallpaperDir(wallpaper.userId);
1451            if (!dir.exists()) {
1452                dir.mkdir();
1453                FileUtils.setPermissions(
1454                        dir.getPath(),
1455                        FileUtils.S_IRWXU|FileUtils.S_IRWXG|FileUtils.S_IXOTH,
1456                        -1, -1);
1457            }
1458            ParcelFileDescriptor fd = ParcelFileDescriptor.open(wallpaper.wallpaperFile,
1459                    MODE_CREATE|MODE_READ_WRITE|MODE_TRUNCATE);
1460            if (!SELinux.restorecon(wallpaper.wallpaperFile)) {
1461                return null;
1462            }
1463            wallpaper.name = name;
1464            wallpaper.wallpaperId = makeWallpaperIdLocked();
1465            if (extras != null) {
1466                extras.putInt(WallpaperManager.EXTRA_NEW_WALLPAPER_ID, wallpaper.wallpaperId);
1467            }
1468            if (DEBUG) {
1469                Slog.v(TAG, "updateWallpaperBitmapLocked() : id=" + wallpaper.wallpaperId
1470                        + " name=" + name + " file=" + wallpaper.wallpaperFile.getName());
1471            }
1472            return fd;
1473        } catch (FileNotFoundException e) {
1474            Slog.w(TAG, "Error setting wallpaper", e);
1475        }
1476        return null;
1477    }
1478
1479    @Override
1480    public void setWallpaperComponentChecked(ComponentName name, String callingPackage,
1481            int userId) {
1482
1483        if (isWallpaperSupported(callingPackage) && isSetWallpaperAllowed(callingPackage)) {
1484            setWallpaperComponent(name, userId);
1485        }
1486    }
1487
1488    // ToDo: Remove this version of the function
1489    @Override
1490    public void setWallpaperComponent(ComponentName name) {
1491        setWallpaperComponent(name, UserHandle.getCallingUserId());
1492    }
1493
1494    private void setWallpaperComponent(ComponentName name, int userId) {
1495        userId = ActivityManager.handleIncomingUser(getCallingPid(), getCallingUid(), userId,
1496                false /* all */, true /* full */, "changing live wallpaper", null /* pkg */);
1497        checkPermission(android.Manifest.permission.SET_WALLPAPER_COMPONENT);
1498
1499        synchronized (mLock) {
1500            if (DEBUG) Slog.v(TAG, "setWallpaperComponent name=" + name);
1501            WallpaperData wallpaper = mWallpaperMap.get(userId);
1502            if (wallpaper == null) {
1503                throw new IllegalStateException("Wallpaper not yet initialized for user " + userId);
1504            }
1505            final long ident = Binder.clearCallingIdentity();
1506            try {
1507                wallpaper.imageWallpaperPending = false;
1508                if (bindWallpaperComponentLocked(name, false, true, wallpaper, null)) {
1509                    wallpaper.wallpaperId = makeWallpaperIdLocked();
1510                    notifyCallbacksLocked(wallpaper);
1511                }
1512            } finally {
1513                Binder.restoreCallingIdentity(ident);
1514            }
1515        }
1516    }
1517
1518    boolean bindWallpaperComponentLocked(ComponentName componentName, boolean force,
1519            boolean fromUser, WallpaperData wallpaper, IRemoteCallback reply) {
1520        if (DEBUG) Slog.v(TAG, "bindWallpaperComponentLocked: componentName=" + componentName);
1521        // Has the component changed?
1522        if (!force) {
1523            if (wallpaper.connection != null) {
1524                if (wallpaper.wallpaperComponent == null) {
1525                    if (componentName == null) {
1526                        if (DEBUG) Slog.v(TAG, "bindWallpaperComponentLocked: still using default");
1527                        // Still using default wallpaper.
1528                        return true;
1529                    }
1530                } else if (wallpaper.wallpaperComponent.equals(componentName)) {
1531                    // Changing to same wallpaper.
1532                    if (DEBUG) Slog.v(TAG, "same wallpaper");
1533                    return true;
1534                }
1535            }
1536        }
1537
1538        try {
1539            if (componentName == null) {
1540                componentName = WallpaperManager.getDefaultWallpaperComponent(mContext);
1541                if (componentName == null) {
1542                    // Fall back to static image wallpaper
1543                    componentName = mImageWallpaper;
1544                    //clearWallpaperComponentLocked();
1545                    //return;
1546                    if (DEBUG) Slog.v(TAG, "Using image wallpaper");
1547                }
1548            }
1549            int serviceUserId = wallpaper.userId;
1550            ServiceInfo si = mIPackageManager.getServiceInfo(componentName,
1551                    PackageManager.GET_META_DATA | PackageManager.GET_PERMISSIONS, serviceUserId);
1552            if (si == null) {
1553                // The wallpaper component we're trying to use doesn't exist
1554                Slog.w(TAG, "Attempted wallpaper " + componentName + " is unavailable");
1555                return false;
1556            }
1557            if (!android.Manifest.permission.BIND_WALLPAPER.equals(si.permission)) {
1558                String msg = "Selected service does not require "
1559                        + android.Manifest.permission.BIND_WALLPAPER
1560                        + ": " + componentName;
1561                if (fromUser) {
1562                    throw new SecurityException(msg);
1563                }
1564                Slog.w(TAG, msg);
1565                return false;
1566            }
1567
1568            WallpaperInfo wi = null;
1569
1570            Intent intent = new Intent(WallpaperService.SERVICE_INTERFACE);
1571            if (componentName != null && !componentName.equals(mImageWallpaper)) {
1572                // Make sure the selected service is actually a wallpaper service.
1573                List<ResolveInfo> ris =
1574                        mIPackageManager.queryIntentServices(intent,
1575                                intent.resolveTypeIfNeeded(mContext.getContentResolver()),
1576                                PackageManager.GET_META_DATA, serviceUserId).getList();
1577                for (int i=0; i<ris.size(); i++) {
1578                    ServiceInfo rsi = ris.get(i).serviceInfo;
1579                    if (rsi.name.equals(si.name) &&
1580                            rsi.packageName.equals(si.packageName)) {
1581                        try {
1582                            wi = new WallpaperInfo(mContext, ris.get(i));
1583                        } catch (XmlPullParserException e) {
1584                            if (fromUser) {
1585                                throw new IllegalArgumentException(e);
1586                            }
1587                            Slog.w(TAG, e);
1588                            return false;
1589                        } catch (IOException e) {
1590                            if (fromUser) {
1591                                throw new IllegalArgumentException(e);
1592                            }
1593                            Slog.w(TAG, e);
1594                            return false;
1595                        }
1596                        break;
1597                    }
1598                }
1599                if (wi == null) {
1600                    String msg = "Selected service is not a wallpaper: "
1601                            + componentName;
1602                    if (fromUser) {
1603                        throw new SecurityException(msg);
1604                    }
1605                    Slog.w(TAG, msg);
1606                    return false;
1607                }
1608            }
1609
1610            // Bind the service!
1611            if (DEBUG) Slog.v(TAG, "Binding to:" + componentName);
1612            WallpaperConnection newConn = new WallpaperConnection(wi, wallpaper);
1613            intent.setComponent(componentName);
1614            intent.putExtra(Intent.EXTRA_CLIENT_LABEL,
1615                    com.android.internal.R.string.wallpaper_binding_label);
1616            intent.putExtra(Intent.EXTRA_CLIENT_INTENT, PendingIntent.getActivityAsUser(
1617                    mContext, 0,
1618                    Intent.createChooser(new Intent(Intent.ACTION_SET_WALLPAPER),
1619                            mContext.getText(com.android.internal.R.string.chooser_wallpaper)),
1620                    0, null, new UserHandle(serviceUserId)));
1621            if (!mContext.bindServiceAsUser(intent, newConn,
1622                    Context.BIND_AUTO_CREATE | Context.BIND_SHOWING_UI
1623                            | Context.BIND_FOREGROUND_SERVICE_WHILE_AWAKE,
1624                    new UserHandle(serviceUserId))) {
1625                String msg = "Unable to bind service: "
1626                        + componentName;
1627                if (fromUser) {
1628                    throw new IllegalArgumentException(msg);
1629                }
1630                Slog.w(TAG, msg);
1631                return false;
1632            }
1633            if (wallpaper.userId == mCurrentUserId && mLastWallpaper != null) {
1634                detachWallpaperLocked(mLastWallpaper);
1635            }
1636            wallpaper.wallpaperComponent = componentName;
1637            wallpaper.connection = newConn;
1638            newConn.mReply = reply;
1639            try {
1640                if (wallpaper.userId == mCurrentUserId) {
1641                    if (DEBUG)
1642                        Slog.v(TAG, "Adding window token: " + newConn.mToken);
1643                    mIWindowManager.addWindowToken(newConn.mToken,
1644                            WindowManager.LayoutParams.TYPE_WALLPAPER);
1645                    mLastWallpaper = wallpaper;
1646                }
1647            } catch (RemoteException e) {
1648            }
1649        } catch (RemoteException e) {
1650            String msg = "Remote exception for " + componentName + "\n" + e;
1651            if (fromUser) {
1652                throw new IllegalArgumentException(msg);
1653            }
1654            Slog.w(TAG, msg);
1655            return false;
1656        }
1657        return true;
1658    }
1659
1660    void detachWallpaperLocked(WallpaperData wallpaper) {
1661        if (wallpaper.connection != null) {
1662            if (wallpaper.connection.mReply != null) {
1663                try {
1664                    wallpaper.connection.mReply.sendResult(null);
1665                } catch (RemoteException e) {
1666                }
1667                wallpaper.connection.mReply = null;
1668            }
1669            if (wallpaper.connection.mEngine != null) {
1670                try {
1671                    wallpaper.connection.mEngine.destroy();
1672                } catch (RemoteException e) {
1673                }
1674            }
1675            mContext.unbindService(wallpaper.connection);
1676            try {
1677                if (DEBUG)
1678                    Slog.v(TAG, "Removing window token: " + wallpaper.connection.mToken);
1679                mIWindowManager.removeWindowToken(wallpaper.connection.mToken);
1680            } catch (RemoteException e) {
1681            }
1682            wallpaper.connection.mService = null;
1683            wallpaper.connection.mEngine = null;
1684            wallpaper.connection = null;
1685        }
1686    }
1687
1688    void clearWallpaperComponentLocked(WallpaperData wallpaper) {
1689        wallpaper.wallpaperComponent = null;
1690        detachWallpaperLocked(wallpaper);
1691    }
1692
1693    void attachServiceLocked(WallpaperConnection conn, WallpaperData wallpaper) {
1694        try {
1695            conn.mService.attach(conn, conn.mToken,
1696                    WindowManager.LayoutParams.TYPE_WALLPAPER, false,
1697                    wallpaper.width, wallpaper.height, wallpaper.padding);
1698        } catch (RemoteException e) {
1699            Slog.w(TAG, "Failed attaching wallpaper; clearing", e);
1700            if (!wallpaper.wallpaperUpdating) {
1701                bindWallpaperComponentLocked(null, false, false, wallpaper, null);
1702            }
1703        }
1704    }
1705
1706    private void notifyCallbacksLocked(WallpaperData wallpaper) {
1707        final int n = wallpaper.callbacks.beginBroadcast();
1708        for (int i = 0; i < n; i++) {
1709            try {
1710                wallpaper.callbacks.getBroadcastItem(i).onWallpaperChanged();
1711            } catch (RemoteException e) {
1712
1713                // The RemoteCallbackList will take care of removing
1714                // the dead object for us.
1715            }
1716        }
1717        wallpaper.callbacks.finishBroadcast();
1718        final Intent intent = new Intent(Intent.ACTION_WALLPAPER_CHANGED);
1719        mContext.sendBroadcastAsUser(intent, new UserHandle(mCurrentUserId));
1720    }
1721
1722    private void checkPermission(String permission) {
1723        if (PackageManager.PERMISSION_GRANTED!= mContext.checkCallingOrSelfPermission(permission)) {
1724            throw new SecurityException("Access denied to process: " + Binder.getCallingPid()
1725                    + ", must have permission " + permission);
1726        }
1727    }
1728
1729    /**
1730     * Certain user types do not support wallpapers (e.g. managed profiles). The check is
1731     * implemented through through the OP_WRITE_WALLPAPER AppOp.
1732     */
1733    public boolean isWallpaperSupported(String callingPackage) {
1734        return mAppOpsManager.checkOpNoThrow(AppOpsManager.OP_WRITE_WALLPAPER, Binder.getCallingUid(),
1735                callingPackage) == AppOpsManager.MODE_ALLOWED;
1736    }
1737
1738    @Override
1739    public boolean isSetWallpaperAllowed(String callingPackage) {
1740        final PackageManager pm = mContext.getPackageManager();
1741        String[] uidPackages = pm.getPackagesForUid(Binder.getCallingUid());
1742        boolean uidMatchPackage = Arrays.asList(uidPackages).contains(callingPackage);
1743        if (!uidMatchPackage) {
1744            return false;   // callingPackage was faked.
1745        }
1746
1747        final DevicePolicyManager dpm = mContext.getSystemService(DevicePolicyManager.class);
1748        if (dpm.isDeviceOwnerApp(callingPackage) || dpm.isProfileOwnerApp(callingPackage)) {
1749            return true;
1750        }
1751        final UserManager um = (UserManager) mContext.getSystemService(Context.USER_SERVICE);
1752        return !um.hasUserRestriction(UserManager.DISALLOW_SET_WALLPAPER);
1753    }
1754
1755    @Override
1756    public boolean isWallpaperBackupEligible(int which, int userId) {
1757        if (Binder.getCallingUid() != Process.SYSTEM_UID) {
1758            throw new SecurityException("Only the system may call isWallpaperBackupEligible");
1759        }
1760
1761        WallpaperData wallpaper = (which == FLAG_LOCK)
1762                ? mWallpaperMap.get(userId)
1763                : mLockWallpaperMap.get(userId);
1764        return (wallpaper != null) ? wallpaper.allowBackup : false;
1765    }
1766
1767    private static JournaledFile makeJournaledFile(int userId) {
1768        final String base = new File(getWallpaperDir(userId), WALLPAPER_INFO).getAbsolutePath();
1769        return new JournaledFile(new File(base), new File(base + ".tmp"));
1770    }
1771
1772    private void saveSettingsLocked(int userId) {
1773        JournaledFile journal = makeJournaledFile(userId);
1774        FileOutputStream fstream = null;
1775        BufferedOutputStream stream = null;
1776        try {
1777            XmlSerializer out = new FastXmlSerializer();
1778            fstream = new FileOutputStream(journal.chooseForWrite(), false);
1779            stream = new BufferedOutputStream(fstream);
1780            out.setOutput(stream, StandardCharsets.UTF_8.name());
1781            out.startDocument(null, true);
1782
1783            WallpaperData wallpaper;
1784
1785            wallpaper = mWallpaperMap.get(userId);
1786            if (wallpaper != null) {
1787                writeWallpaperAttributes(out, "wp", wallpaper);
1788            }
1789            wallpaper = mLockWallpaperMap.get(userId);
1790            if (wallpaper != null) {
1791                writeWallpaperAttributes(out, "kwp", wallpaper);
1792            }
1793
1794            out.endDocument();
1795
1796            stream.flush(); // also flushes fstream
1797            FileUtils.sync(fstream);
1798            stream.close(); // also closes fstream
1799            journal.commit();
1800        } catch (IOException e) {
1801            IoUtils.closeQuietly(stream);
1802            journal.rollback();
1803        }
1804    }
1805
1806    private void writeWallpaperAttributes(XmlSerializer out, String tag, WallpaperData wallpaper)
1807            throws IllegalArgumentException, IllegalStateException, IOException {
1808        out.startTag(null, tag);
1809        out.attribute(null, "id", Integer.toString(wallpaper.wallpaperId));
1810        out.attribute(null, "width", Integer.toString(wallpaper.width));
1811        out.attribute(null, "height", Integer.toString(wallpaper.height));
1812
1813        out.attribute(null, "cropLeft", Integer.toString(wallpaper.cropHint.left));
1814        out.attribute(null, "cropTop", Integer.toString(wallpaper.cropHint.top));
1815        out.attribute(null, "cropRight", Integer.toString(wallpaper.cropHint.right));
1816        out.attribute(null, "cropBottom", Integer.toString(wallpaper.cropHint.bottom));
1817
1818        if (wallpaper.padding.left != 0) {
1819            out.attribute(null, "paddingLeft", Integer.toString(wallpaper.padding.left));
1820        }
1821        if (wallpaper.padding.top != 0) {
1822            out.attribute(null, "paddingTop", Integer.toString(wallpaper.padding.top));
1823        }
1824        if (wallpaper.padding.right != 0) {
1825            out.attribute(null, "paddingRight", Integer.toString(wallpaper.padding.right));
1826        }
1827        if (wallpaper.padding.bottom != 0) {
1828            out.attribute(null, "paddingBottom", Integer.toString(wallpaper.padding.bottom));
1829        }
1830
1831        out.attribute(null, "name", wallpaper.name);
1832        if (wallpaper.wallpaperComponent != null
1833                && !wallpaper.wallpaperComponent.equals(mImageWallpaper)) {
1834            out.attribute(null, "component",
1835                    wallpaper.wallpaperComponent.flattenToShortString());
1836        }
1837
1838        if (wallpaper.allowBackup) {
1839            out.attribute(null, "backup", "true");
1840        }
1841
1842        out.endTag(null, tag);
1843    }
1844
1845    private void migrateFromOld() {
1846        File oldWallpaper = new File(WallpaperBackupHelper.WALLPAPER_IMAGE_KEY);
1847        File oldInfo = new File(WallpaperBackupHelper.WALLPAPER_INFO_KEY);
1848        if (oldWallpaper.exists()) {
1849            File newWallpaper = new File(getWallpaperDir(0), WALLPAPER);
1850            oldWallpaper.renameTo(newWallpaper);
1851        }
1852        if (oldInfo.exists()) {
1853            File newInfo = new File(getWallpaperDir(0), WALLPAPER_INFO);
1854            oldInfo.renameTo(newInfo);
1855        }
1856    }
1857
1858    private int getAttributeInt(XmlPullParser parser, String name, int defValue) {
1859        String value = parser.getAttributeValue(null, name);
1860        if (value == null) {
1861            return defValue;
1862        }
1863        return Integer.parseInt(value);
1864    }
1865
1866    /**
1867     * Sometimes it is expected the wallpaper map may not have a user's data.  E.g. This could
1868     * happen during user switch.  The async user switch observer may not have received
1869     * the event yet.  We use this safe method when we don't care about this ordering and just
1870     * want to update the data.  The data is going to be applied when the user switch observer
1871     * is eventually executed.
1872     */
1873    private WallpaperData getWallpaperSafeLocked(int userId, int which) {
1874        // We're setting either just system (work with the system wallpaper),
1875        // both (also work with the system wallpaper), or just the lock
1876        // wallpaper (update against the existing lock wallpaper if any).
1877        // Combined or just-system operations use the 'system' WallpaperData
1878        // for this use; lock-only operations use the dedicated one.
1879        final SparseArray<WallpaperData> whichSet =
1880                (which == FLAG_LOCK) ? mLockWallpaperMap : mWallpaperMap;
1881        WallpaperData wallpaper = whichSet.get(userId);
1882        if (wallpaper == null) {
1883            // common case, this is the first lookup post-boot of the system or
1884            // unified lock, so we bring up the saved state lazily now and recheck.
1885            loadSettingsLocked(userId, false);
1886            wallpaper = whichSet.get(userId);
1887            // if it's still null here, this is a lock-only operation and there is not
1888            // yet a lock-only wallpaper set for this user, so we need to establish
1889            // it now.
1890            if (wallpaper == null) {
1891                if (which == FLAG_LOCK) {
1892                    wallpaper = new WallpaperData(userId,
1893                            WALLPAPER_LOCK_ORIG, WALLPAPER_LOCK_CROP);
1894                    mLockWallpaperMap.put(userId, wallpaper);
1895                    ensureSaneWallpaperData(wallpaper);
1896                } else {
1897                    // sanity fallback: we're in bad shape, but establishing a known
1898                    // valid system+lock WallpaperData will keep us from dying.
1899                    Slog.wtf(TAG, "Didn't find wallpaper in non-lock case!");
1900                    wallpaper = new WallpaperData(userId, WALLPAPER, WALLPAPER_CROP);
1901                    mWallpaperMap.put(userId, wallpaper);
1902                    ensureSaneWallpaperData(wallpaper);
1903                }
1904            }
1905        }
1906        return wallpaper;
1907    }
1908
1909    private void loadSettingsLocked(int userId, boolean keepDimensionHints) {
1910        if (DEBUG) Slog.v(TAG, "loadSettingsLocked");
1911
1912        JournaledFile journal = makeJournaledFile(userId);
1913        FileInputStream stream = null;
1914        File file = journal.chooseForRead();
1915        if (!file.exists()) {
1916            // This should only happen one time, when upgrading from a legacy system
1917            migrateFromOld();
1918        }
1919        WallpaperData wallpaper = mWallpaperMap.get(userId);
1920        if (wallpaper == null) {
1921            wallpaper = new WallpaperData(userId, WALLPAPER, WALLPAPER_CROP);
1922            wallpaper.allowBackup = true;
1923            mWallpaperMap.put(userId, wallpaper);
1924            if (!wallpaper.cropExists()) {
1925                generateCrop(wallpaper);
1926            }
1927        }
1928        boolean success = false;
1929        try {
1930            stream = new FileInputStream(file);
1931            XmlPullParser parser = Xml.newPullParser();
1932            parser.setInput(stream, StandardCharsets.UTF_8.name());
1933
1934            int type;
1935            do {
1936                type = parser.next();
1937                if (type == XmlPullParser.START_TAG) {
1938                    String tag = parser.getName();
1939                    if ("wp".equals(tag)) {
1940                        // Common to system + lock wallpapers
1941                        parseWallpaperAttributes(parser, wallpaper, keepDimensionHints);
1942
1943                        // A system wallpaper might also be a live wallpaper
1944                        String comp = parser.getAttributeValue(null, "component");
1945                        wallpaper.nextWallpaperComponent = comp != null
1946                                ? ComponentName.unflattenFromString(comp)
1947                                : null;
1948                        if (wallpaper.nextWallpaperComponent == null
1949                                || "android".equals(wallpaper.nextWallpaperComponent
1950                                        .getPackageName())) {
1951                            wallpaper.nextWallpaperComponent = mImageWallpaper;
1952                        }
1953
1954                        if (DEBUG) {
1955                            Slog.v(TAG, "mWidth:" + wallpaper.width);
1956                            Slog.v(TAG, "mHeight:" + wallpaper.height);
1957                            Slog.v(TAG, "cropRect:" + wallpaper.cropHint);
1958                            Slog.v(TAG, "mName:" + wallpaper.name);
1959                            Slog.v(TAG, "mNextWallpaperComponent:"
1960                                    + wallpaper.nextWallpaperComponent);
1961                        }
1962                    } else if ("kwp".equals(tag)) {
1963                        // keyguard-specific wallpaper for this user
1964                        WallpaperData lockWallpaper = mLockWallpaperMap.get(userId);
1965                        if (lockWallpaper == null) {
1966                            lockWallpaper = new WallpaperData(userId,
1967                                    WALLPAPER_LOCK_ORIG, WALLPAPER_LOCK_CROP);
1968                            mLockWallpaperMap.put(userId, lockWallpaper);
1969                        }
1970                        parseWallpaperAttributes(parser, lockWallpaper, false);
1971                    }
1972                }
1973            } while (type != XmlPullParser.END_DOCUMENT);
1974            success = true;
1975        } catch (FileNotFoundException e) {
1976            Slog.w(TAG, "no current wallpaper -- first boot?");
1977        } catch (NullPointerException e) {
1978            Slog.w(TAG, "failed parsing " + file + " " + e);
1979        } catch (NumberFormatException e) {
1980            Slog.w(TAG, "failed parsing " + file + " " + e);
1981        } catch (XmlPullParserException e) {
1982            Slog.w(TAG, "failed parsing " + file + " " + e);
1983        } catch (IOException e) {
1984            Slog.w(TAG, "failed parsing " + file + " " + e);
1985        } catch (IndexOutOfBoundsException e) {
1986            Slog.w(TAG, "failed parsing " + file + " " + e);
1987        }
1988        IoUtils.closeQuietly(stream);
1989
1990        if (!success) {
1991            wallpaper.width = -1;
1992            wallpaper.height = -1;
1993            wallpaper.cropHint.set(0, 0, 0, 0);
1994            wallpaper.padding.set(0, 0, 0, 0);
1995            wallpaper.name = "";
1996
1997            mLockWallpaperMap.remove(userId);
1998        } else {
1999            if (wallpaper.wallpaperId <= 0) {
2000                wallpaper.wallpaperId = makeWallpaperIdLocked();
2001                if (DEBUG) {
2002                    Slog.w(TAG, "Didn't set wallpaper id in loadSettingsLocked(" + userId
2003                            + "); now " + wallpaper.wallpaperId);
2004                }
2005            }
2006        }
2007
2008        ensureSaneWallpaperData(wallpaper);
2009        WallpaperData lockWallpaper = mLockWallpaperMap.get(userId);
2010        if (lockWallpaper != null) {
2011            ensureSaneWallpaperData(lockWallpaper);
2012        }
2013    }
2014
2015    private void ensureSaneWallpaperData(WallpaperData wallpaper) {
2016        // We always want to have some reasonable width hint.
2017        int baseSize = getMaximumSizeDimension();
2018        if (wallpaper.width < baseSize) {
2019            wallpaper.width = baseSize;
2020        }
2021        if (wallpaper.height < baseSize) {
2022            wallpaper.height = baseSize;
2023        }
2024        // and crop, if not previously specified
2025        if (wallpaper.cropHint.width() <= 0
2026                || wallpaper.cropHint.height() <= 0) {
2027            wallpaper.cropHint.set(0, 0, wallpaper.width, wallpaper.height);
2028        }
2029    }
2030
2031    private void parseWallpaperAttributes(XmlPullParser parser, WallpaperData wallpaper,
2032            boolean keepDimensionHints) {
2033        final String idString = parser.getAttributeValue(null, "id");
2034        if (idString != null) {
2035            final int id = wallpaper.wallpaperId = Integer.parseInt(idString);
2036            if (id > mWallpaperId) {
2037                mWallpaperId = id;
2038            }
2039        } else {
2040            wallpaper.wallpaperId = makeWallpaperIdLocked();
2041        }
2042
2043        if (!keepDimensionHints) {
2044            wallpaper.width = Integer.parseInt(parser.getAttributeValue(null, "width"));
2045            wallpaper.height = Integer.parseInt(parser
2046                    .getAttributeValue(null, "height"));
2047        }
2048        wallpaper.cropHint.left = getAttributeInt(parser, "cropLeft", 0);
2049        wallpaper.cropHint.top = getAttributeInt(parser, "cropTop", 0);
2050        wallpaper.cropHint.right = getAttributeInt(parser, "cropRight", 0);
2051        wallpaper.cropHint.bottom = getAttributeInt(parser, "cropBottom", 0);
2052        wallpaper.padding.left = getAttributeInt(parser, "paddingLeft", 0);
2053        wallpaper.padding.top = getAttributeInt(parser, "paddingTop", 0);
2054        wallpaper.padding.right = getAttributeInt(parser, "paddingRight", 0);
2055        wallpaper.padding.bottom = getAttributeInt(parser, "paddingBottom", 0);
2056        wallpaper.name = parser.getAttributeValue(null, "name");
2057        wallpaper.allowBackup = "true".equals(parser.getAttributeValue(null, "backup"));
2058    }
2059
2060    private int getMaximumSizeDimension() {
2061        WindowManager wm = (WindowManager)mContext.getSystemService(Context.WINDOW_SERVICE);
2062        Display d = wm.getDefaultDisplay();
2063        return d.getMaximumSizeDimension();
2064    }
2065
2066    // Called by SystemBackupAgent after files are restored to disk.
2067    public void settingsRestored() {
2068        // Verify caller is the system
2069        if (Binder.getCallingUid() != android.os.Process.SYSTEM_UID) {
2070            throw new RuntimeException("settingsRestored() can only be called from the system process");
2071        }
2072        // TODO: If necessary, make it work for secondary users as well. This currently assumes
2073        // restores only to the primary user
2074        if (DEBUG) Slog.v(TAG, "settingsRestored");
2075        WallpaperData wallpaper = null;
2076        boolean success = false;
2077        synchronized (mLock) {
2078            loadSettingsLocked(UserHandle.USER_SYSTEM, false);
2079            wallpaper = mWallpaperMap.get(UserHandle.USER_SYSTEM);
2080            wallpaper.wallpaperId = makeWallpaperIdLocked();    // always bump id at restore
2081            wallpaper.allowBackup = true;   // by definition if it was restored
2082            if (wallpaper.nextWallpaperComponent != null
2083                    && !wallpaper.nextWallpaperComponent.equals(mImageWallpaper)) {
2084                if (!bindWallpaperComponentLocked(wallpaper.nextWallpaperComponent, false, false,
2085                        wallpaper, null)) {
2086                    // No such live wallpaper or other failure; fall back to the default
2087                    // live wallpaper (since the profile being restored indicated that the
2088                    // user had selected a live rather than static one).
2089                    bindWallpaperComponentLocked(null, false, false, wallpaper, null);
2090                }
2091                success = true;
2092            } else {
2093                // If there's a wallpaper name, we use that.  If that can't be loaded, then we
2094                // use the default.
2095                if ("".equals(wallpaper.name)) {
2096                    if (DEBUG) Slog.v(TAG, "settingsRestored: name is empty");
2097                    success = true;
2098                } else {
2099                    if (DEBUG) Slog.v(TAG, "settingsRestored: attempting to restore named resource");
2100                    success = restoreNamedResourceLocked(wallpaper);
2101                }
2102                if (DEBUG) Slog.v(TAG, "settingsRestored: success=" + success
2103                        + " id=" + wallpaper.wallpaperId);
2104                if (success) {
2105                    generateCrop(wallpaper);    // based on the new image + metadata
2106                    bindWallpaperComponentLocked(wallpaper.nextWallpaperComponent, true, false,
2107                            wallpaper, null);
2108                }
2109            }
2110        }
2111
2112        if (!success) {
2113            Slog.e(TAG, "Failed to restore wallpaper: '" + wallpaper.name + "'");
2114            wallpaper.name = "";
2115            getWallpaperDir(UserHandle.USER_SYSTEM).delete();
2116        }
2117
2118        synchronized (mLock) {
2119            saveSettingsLocked(UserHandle.USER_SYSTEM);
2120        }
2121    }
2122
2123    // Restore the named resource bitmap to both source + crop files
2124    boolean restoreNamedResourceLocked(WallpaperData wallpaper) {
2125        if (wallpaper.name.length() > 4 && "res:".equals(wallpaper.name.substring(0, 4))) {
2126            String resName = wallpaper.name.substring(4);
2127
2128            String pkg = null;
2129            int colon = resName.indexOf(':');
2130            if (colon > 0) {
2131                pkg = resName.substring(0, colon);
2132            }
2133
2134            String ident = null;
2135            int slash = resName.lastIndexOf('/');
2136            if (slash > 0) {
2137                ident = resName.substring(slash+1);
2138            }
2139
2140            String type = null;
2141            if (colon > 0 && slash > 0 && (slash-colon) > 1) {
2142                type = resName.substring(colon+1, slash);
2143            }
2144
2145            if (pkg != null && ident != null && type != null) {
2146                int resId = -1;
2147                InputStream res = null;
2148                FileOutputStream fos = null;
2149                FileOutputStream cos = null;
2150                try {
2151                    Context c = mContext.createPackageContext(pkg, Context.CONTEXT_RESTRICTED);
2152                    Resources r = c.getResources();
2153                    resId = r.getIdentifier(resName, null, null);
2154                    if (resId == 0) {
2155                        Slog.e(TAG, "couldn't resolve identifier pkg=" + pkg + " type=" + type
2156                                + " ident=" + ident);
2157                        return false;
2158                    }
2159
2160                    res = r.openRawResource(resId);
2161                    if (wallpaper.wallpaperFile.exists()) {
2162                        wallpaper.wallpaperFile.delete();
2163                        wallpaper.cropFile.delete();
2164                    }
2165                    fos = new FileOutputStream(wallpaper.wallpaperFile);
2166                    cos = new FileOutputStream(wallpaper.cropFile);
2167
2168                    byte[] buffer = new byte[32768];
2169                    int amt;
2170                    while ((amt=res.read(buffer)) > 0) {
2171                        fos.write(buffer, 0, amt);
2172                        cos.write(buffer, 0, amt);
2173                    }
2174                    // mWallpaperObserver will notice the close and send the change broadcast
2175
2176                    Slog.v(TAG, "Restored wallpaper: " + resName);
2177                    return true;
2178                } catch (NameNotFoundException e) {
2179                    Slog.e(TAG, "Package name " + pkg + " not found");
2180                } catch (Resources.NotFoundException e) {
2181                    Slog.e(TAG, "Resource not found: " + resId);
2182                } catch (IOException e) {
2183                    Slog.e(TAG, "IOException while restoring wallpaper ", e);
2184                } finally {
2185                    IoUtils.closeQuietly(res);
2186                    if (fos != null) {
2187                        FileUtils.sync(fos);
2188                    }
2189                    if (cos != null) {
2190                        FileUtils.sync(cos);
2191                    }
2192                    IoUtils.closeQuietly(fos);
2193                    IoUtils.closeQuietly(cos);
2194                }
2195            }
2196        }
2197        return false;
2198    }
2199
2200    @Override
2201    protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
2202        if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DUMP)
2203                != PackageManager.PERMISSION_GRANTED) {
2204
2205            pw.println("Permission Denial: can't dump wallpaper service from from pid="
2206                    + Binder.getCallingPid()
2207                    + ", uid=" + Binder.getCallingUid());
2208            return;
2209        }
2210
2211        synchronized (mLock) {
2212            pw.println("System wallpaper state:");
2213            for (int i = 0; i < mWallpaperMap.size(); i++) {
2214                WallpaperData wallpaper = mWallpaperMap.valueAt(i);
2215                pw.print(" User "); pw.print(wallpaper.userId);
2216                    pw.print(": id="); pw.println(wallpaper.wallpaperId);
2217                pw.print("  mWidth=");
2218                    pw.print(wallpaper.width);
2219                    pw.print(" mHeight=");
2220                    pw.println(wallpaper.height);
2221                pw.print("  mCropHint="); pw.println(wallpaper.cropHint);
2222                pw.print("  mPadding="); pw.println(wallpaper.padding);
2223                pw.print("  mName=");  pw.println(wallpaper.name);
2224                pw.print("  mWallpaperComponent="); pw.println(wallpaper.wallpaperComponent);
2225                if (wallpaper.connection != null) {
2226                    WallpaperConnection conn = wallpaper.connection;
2227                    pw.print("  Wallpaper connection ");
2228                    pw.print(conn);
2229                    pw.println(":");
2230                    if (conn.mInfo != null) {
2231                        pw.print("    mInfo.component=");
2232                        pw.println(conn.mInfo.getComponent());
2233                    }
2234                    pw.print("    mToken=");
2235                    pw.println(conn.mToken);
2236                    pw.print("    mService=");
2237                    pw.println(conn.mService);
2238                    pw.print("    mEngine=");
2239                    pw.println(conn.mEngine);
2240                    pw.print("    mLastDiedTime=");
2241                    pw.println(wallpaper.lastDiedTime - SystemClock.uptimeMillis());
2242                }
2243            }
2244            pw.println("Lock wallpaper state:");
2245            for (int i = 0; i < mLockWallpaperMap.size(); i++) {
2246                WallpaperData wallpaper = mLockWallpaperMap.valueAt(i);
2247                pw.print(" User "); pw.print(wallpaper.userId);
2248                    pw.print(": id="); pw.println(wallpaper.wallpaperId);
2249                pw.print("  mWidth="); pw.print(wallpaper.width);
2250                    pw.print(" mHeight="); pw.println(wallpaper.height);
2251                pw.print("  mCropHint="); pw.println(wallpaper.cropHint);
2252                pw.print("  mPadding="); pw.println(wallpaper.padding);
2253                pw.print("  mName=");  pw.println(wallpaper.name);
2254            }
2255
2256        }
2257    }
2258}
2259