WallpaperManagerService.java revision 9a7c3bd577ba1d3ea43082814529ea3d6379d024
1/*
2 * Copyright (C) 2008 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.server.wallpaper;
18
19import static android.app.WallpaperManager.FLAG_LOCK;
20import static android.app.WallpaperManager.FLAG_SYSTEM;
21import static android.os.ParcelFileDescriptor.MODE_CREATE;
22import static android.os.ParcelFileDescriptor.MODE_READ_ONLY;
23import static android.os.ParcelFileDescriptor.MODE_READ_WRITE;
24import static android.os.ParcelFileDescriptor.MODE_TRUNCATE;
25
26import android.app.ActivityManager;
27import android.app.ActivityManagerNative;
28import android.app.AppGlobals;
29import android.app.AppOpsManager;
30import android.app.IUserSwitchObserver;
31import android.app.IWallpaperManager;
32import android.app.IWallpaperManagerCallback;
33import android.app.PendingIntent;
34import android.app.WallpaperInfo;
35import android.app.WallpaperManager;
36import android.app.admin.DevicePolicyManager;
37import android.app.backup.WallpaperBackupHelper;
38import android.content.BroadcastReceiver;
39import android.content.ComponentName;
40import android.content.Context;
41import android.content.Intent;
42import android.content.IntentFilter;
43import android.content.ServiceConnection;
44import android.content.pm.IPackageManager;
45import android.content.pm.PackageManager;
46import android.content.pm.PackageManager.NameNotFoundException;
47import android.content.pm.ResolveInfo;
48import android.content.pm.ServiceInfo;
49import android.content.pm.UserInfo;
50import android.content.res.Resources;
51import android.graphics.Bitmap;
52import android.graphics.BitmapFactory;
53import android.graphics.BitmapRegionDecoder;
54import android.graphics.Point;
55import android.graphics.Rect;
56import android.os.Binder;
57import android.os.Bundle;
58import android.os.Environment;
59import android.os.FileObserver;
60import android.os.FileUtils;
61import android.os.IBinder;
62import android.os.IRemoteCallback;
63import android.os.Process;
64import android.os.ParcelFileDescriptor;
65import android.os.RemoteCallbackList;
66import android.os.RemoteException;
67import android.os.SELinux;
68import android.os.ServiceManager;
69import android.os.SystemClock;
70import android.os.UserHandle;
71import android.os.UserManager;
72import android.service.wallpaper.IWallpaperConnection;
73import android.service.wallpaper.IWallpaperEngine;
74import android.service.wallpaper.IWallpaperService;
75import android.service.wallpaper.WallpaperService;
76import android.system.ErrnoException;
77import android.system.Os;
78import android.util.EventLog;
79import android.util.Slog;
80import android.util.SparseArray;
81import android.util.Xml;
82import android.view.Display;
83import android.view.IWindowManager;
84import android.view.WindowManager;
85
86import com.android.internal.R;
87import com.android.internal.content.PackageMonitor;
88import com.android.internal.os.BackgroundThread;
89import com.android.internal.util.FastXmlSerializer;
90import com.android.internal.util.JournaledFile;
91import com.android.server.EventLogTags;
92import com.android.server.SystemService;
93
94import libcore.io.IoUtils;
95
96import org.xmlpull.v1.XmlPullParser;
97import org.xmlpull.v1.XmlPullParserException;
98import org.xmlpull.v1.XmlSerializer;
99
100import java.io.BufferedOutputStream;
101import java.io.File;
102import java.io.FileDescriptor;
103import java.io.FileInputStream;
104import java.io.FileNotFoundException;
105import java.io.FileOutputStream;
106import java.io.IOException;
107import java.io.InputStream;
108import java.io.PrintWriter;
109import java.nio.charset.StandardCharsets;
110import java.util.Arrays;
111import java.util.List;
112
113public class WallpaperManagerService extends IWallpaperManager.Stub {
114    static final String TAG = "WallpaperManagerService";
115    static final boolean DEBUG = false;
116
117    public static class Lifecycle extends SystemService {
118        private WallpaperManagerService mService;
119
120        public Lifecycle(Context context) {
121            super(context);
122        }
123
124        @Override
125        public void onStart() {
126            mService = new WallpaperManagerService(getContext());
127            publishBinderService(Context.WALLPAPER_SERVICE, mService);
128        }
129
130        @Override
131        public void onBootPhase(int phase) {
132            if (phase == SystemService.PHASE_ACTIVITY_MANAGER_READY) {
133                mService.systemReady();
134            } else if (phase == SystemService.PHASE_THIRD_PARTY_APPS_CAN_START) {
135                mService.switchUser(UserHandle.USER_SYSTEM, null);
136            }
137        }
138
139        @Override
140        public void onUnlockUser(int userHandle) {
141            mService.onUnlockUser(userHandle);
142        }
143    }
144
145    final Object mLock = new Object();
146
147    /**
148     * Minimum time between crashes of a wallpaper service for us to consider
149     * restarting it vs. just reverting to the static wallpaper.
150     */
151    static final long MIN_WALLPAPER_CRASH_TIME = 10000;
152    static final int MAX_WALLPAPER_COMPONENT_LOG_LENGTH = 128;
153    static final String WALLPAPER = "wallpaper_orig";
154    static final String WALLPAPER_CROP = "wallpaper";
155    static final String WALLPAPER_LOCK_ORIG = "wallpaper_lock_orig";
156    static final String WALLPAPER_LOCK_CROP = "wallpaper_lock";
157    static final String WALLPAPER_INFO = "wallpaper_info.xml";
158
159    // All the various per-user state files we need to be aware of
160    static final String[] sPerUserFiles = new String[] {
161        WALLPAPER, WALLPAPER_CROP,
162        WALLPAPER_LOCK_ORIG, WALLPAPER_LOCK_CROP,
163        WALLPAPER_INFO
164    };
165
166    /**
167     * Observes the wallpaper for changes and notifies all IWallpaperServiceCallbacks
168     * that the wallpaper has changed. The CREATE is triggered when there is no
169     * wallpaper set and is created for the first time. The CLOSE_WRITE is triggered
170     * every time the wallpaper is changed.
171     */
172    private class WallpaperObserver extends FileObserver {
173
174        final int mUserId;
175        final WallpaperData mWallpaper;
176        final File mWallpaperDir;
177        final File mWallpaperFile;
178        final File mWallpaperLockFile;
179
180        public WallpaperObserver(WallpaperData wallpaper) {
181            super(getWallpaperDir(wallpaper.userId).getAbsolutePath(),
182                    CLOSE_WRITE | MOVED_TO | DELETE | DELETE_SELF);
183            mUserId = wallpaper.userId;
184            mWallpaperDir = getWallpaperDir(wallpaper.userId);
185            mWallpaper = wallpaper;
186            mWallpaperFile = new File(mWallpaperDir, WALLPAPER);
187            mWallpaperLockFile = new File(mWallpaperDir, WALLPAPER_LOCK_ORIG);
188        }
189
190        private WallpaperData dataForEvent(boolean sysChanged, boolean lockChanged) {
191            WallpaperData wallpaper = null;
192            synchronized (mLock) {
193                if (lockChanged) {
194                    wallpaper = mLockWallpaperMap.get(mUserId);
195                }
196                if (wallpaper == null) {
197                    // no lock-specific wallpaper exists, or sys case, handled together
198                    wallpaper = mWallpaperMap.get(mUserId);
199                }
200            }
201            return (wallpaper != null) ? wallpaper : mWallpaper;
202        }
203
204        @Override
205        public void onEvent(int event, String path) {
206            if (path == null) {
207                return;
208            }
209            final boolean moved = (event == MOVED_TO);
210            final boolean written = (event == CLOSE_WRITE || moved);
211            final File changedFile = new File(mWallpaperDir, path);
212
213            // System and system+lock changes happen on the system wallpaper input file;
214            // lock-only changes happen on the dedicated lock wallpaper input file
215            final boolean sysWallpaperChanged = (mWallpaperFile.equals(changedFile));
216            final boolean lockWallpaperChanged = (mWallpaperLockFile.equals(changedFile));
217            WallpaperData wallpaper = dataForEvent(sysWallpaperChanged, lockWallpaperChanged);
218
219            if (DEBUG) {
220                Slog.v(TAG, "Wallpaper file change: evt=" + event
221                        + " path=" + path
222                        + " sys=" + sysWallpaperChanged
223                        + " lock=" + lockWallpaperChanged
224                        + " imagePending=" + wallpaper.imageWallpaperPending
225                        + " whichPending=0x" + Integer.toHexString(wallpaper.whichPending)
226                        + " written=" + written);
227            }
228
229            if (moved && lockWallpaperChanged) {
230                // We just migrated sys -> lock to preserve imagery for an impending
231                // new system-only wallpaper.  Tell keyguard about it and make sure it
232                // has the right SELinux label.
233                if (DEBUG) {
234                    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                            if (moved) {
256                                // This is a restore, so generate the crop using any just-restored new
257                                // crop guidelines, making sure to preserve our local dimension hints.
258                                // We also make sure to reapply the correct SELinux label.
259                                if (DEBUG) {
260                                    Slog.v(TAG, "moved-to, therefore restore; reloading metadata");
261                                }
262                                SELinux.restorecon(changedFile);
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    int mCurrentUserId;
491
492    static class WallpaperData {
493
494        int userId;
495
496        final File wallpaperFile;   // source image
497        final File cropFile;        // eventual destination
498
499        /**
500         * True while the client is writing a new wallpaper
501         */
502        boolean imageWallpaperPending;
503
504        /**
505         * Which new wallpapers are being written; mirrors the 'which'
506         * selector bit field to setWallpaper().
507         */
508        int whichPending;
509
510        /**
511         * Callback once the set + crop is finished
512         */
513        IWallpaperManagerCallback setComplete;
514
515        /**
516         * Is the OS allowed to back up this wallpaper imagery?
517         */
518        boolean allowBackup;
519
520        /**
521         * Resource name if using a picture from the wallpaper gallery
522         */
523        String name = "";
524
525        /**
526         * The component name of the currently set live wallpaper.
527         */
528        ComponentName wallpaperComponent;
529
530        /**
531         * The component name of the wallpaper that should be set next.
532         */
533        ComponentName nextWallpaperComponent;
534
535        /**
536         * The ID of this wallpaper
537         */
538        int wallpaperId;
539
540        WallpaperConnection connection;
541        long lastDiedTime;
542        boolean wallpaperUpdating;
543        WallpaperObserver wallpaperObserver;
544
545        /**
546         * List of callbacks registered they should each be notified when the wallpaper is changed.
547         */
548        private RemoteCallbackList<IWallpaperManagerCallback> callbacks
549                = new RemoteCallbackList<IWallpaperManagerCallback>();
550
551        int width = -1;
552        int height = -1;
553
554        /**
555         * The crop hint supplied for displaying a subset of the source image
556         */
557        final Rect cropHint = new Rect(0, 0, 0, 0);
558
559        final Rect padding = new Rect(0, 0, 0, 0);
560
561        WallpaperData(int userId, String inputFileName, String cropFileName) {
562            this.userId = userId;
563            final File wallpaperDir = getWallpaperDir(userId);
564            wallpaperFile = new File(wallpaperDir, inputFileName);
565            cropFile = new File(wallpaperDir, cropFileName);
566        }
567
568        // Called during initialization of a given user's wallpaper bookkeeping
569        boolean cropExists() {
570            return cropFile.exists();
571        }
572    }
573
574    int makeWallpaperIdLocked() {
575        do {
576            ++mWallpaperId;
577        } while (mWallpaperId == 0);
578        return mWallpaperId;
579    }
580
581    class WallpaperConnection extends IWallpaperConnection.Stub
582            implements ServiceConnection {
583        final WallpaperInfo mInfo;
584        final Binder mToken = new Binder();
585        IWallpaperService mService;
586        IWallpaperEngine mEngine;
587        WallpaperData mWallpaper;
588        IRemoteCallback mReply;
589
590        boolean mDimensionsChanged = false;
591        boolean mPaddingChanged = false;
592
593        public WallpaperConnection(WallpaperInfo info, WallpaperData wallpaper) {
594            mInfo = info;
595            mWallpaper = wallpaper;
596        }
597
598        @Override
599        public void onServiceConnected(ComponentName name, IBinder service) {
600            synchronized (mLock) {
601                if (mWallpaper.connection == this) {
602                    mService = IWallpaperService.Stub.asInterface(service);
603                    attachServiceLocked(this, mWallpaper);
604                    // XXX should probably do saveSettingsLocked() later
605                    // when we have an engine, but I'm not sure about
606                    // locking there and anyway we always need to be able to
607                    // recover if there is something wrong.
608                    saveSettingsLocked(mWallpaper.userId);
609                }
610            }
611        }
612
613        @Override
614        public void onServiceDisconnected(ComponentName name) {
615            synchronized (mLock) {
616                mService = null;
617                mEngine = null;
618                if (mWallpaper.connection == this) {
619                    Slog.w(TAG, "Wallpaper service gone: " + mWallpaper.wallpaperComponent);
620                    if (!mWallpaper.wallpaperUpdating
621                            && mWallpaper.userId == mCurrentUserId) {
622                        // There is a race condition which causes
623                        // {@link #mWallpaper.wallpaperUpdating} to be false even if it is
624                        // currently updating since the broadcast notifying us is async.
625                        // This race is overcome by the general rule that we only reset the
626                        // wallpaper if its service was shut down twice
627                        // during {@link #MIN_WALLPAPER_CRASH_TIME} millis.
628                        if (mWallpaper.lastDiedTime != 0
629                                && mWallpaper.lastDiedTime + MIN_WALLPAPER_CRASH_TIME
630                                    > SystemClock.uptimeMillis()) {
631                            Slog.w(TAG, "Reverting to built-in wallpaper!");
632                            clearWallpaperLocked(true, FLAG_SYSTEM, mWallpaper.userId, null);
633                        } else {
634                            mWallpaper.lastDiedTime = SystemClock.uptimeMillis();
635                        }
636                        final String flattened = name.flattenToString();
637                        EventLog.writeEvent(EventLogTags.WP_WALLPAPER_CRASHED,
638                                flattened.substring(0, Math.min(flattened.length(),
639                                        MAX_WALLPAPER_COMPONENT_LOG_LENGTH)));
640                    }
641                }
642            }
643        }
644
645        @Override
646        public void attachEngine(IWallpaperEngine engine) {
647            synchronized (mLock) {
648                mEngine = engine;
649                if (mDimensionsChanged) {
650                    try {
651                        mEngine.setDesiredSize(mWallpaper.width, mWallpaper.height);
652                    } catch (RemoteException e) {
653                        Slog.w(TAG, "Failed to set wallpaper dimensions", e);
654                    }
655                    mDimensionsChanged = false;
656                }
657                if (mPaddingChanged) {
658                    try {
659                        mEngine.setDisplayPadding(mWallpaper.padding);
660                    } catch (RemoteException e) {
661                        Slog.w(TAG, "Failed to set wallpaper padding", e);
662                    }
663                    mPaddingChanged = false;
664                }
665            }
666        }
667
668        @Override
669        public void engineShown(IWallpaperEngine engine) {
670            synchronized (mLock) {
671                if (mReply != null) {
672                    long ident = Binder.clearCallingIdentity();
673                    try {
674                        mReply.sendResult(null);
675                    } catch (RemoteException e) {
676                        Binder.restoreCallingIdentity(ident);
677                    }
678                    mReply = null;
679                }
680            }
681        }
682
683        @Override
684        public ParcelFileDescriptor setWallpaper(String name) {
685            synchronized (mLock) {
686                if (mWallpaper.connection == this) {
687                    return updateWallpaperBitmapLocked(name, mWallpaper, null);
688                }
689                return null;
690            }
691        }
692    }
693
694    class MyPackageMonitor extends PackageMonitor {
695        @Override
696        public void onPackageUpdateFinished(String packageName, int uid) {
697            synchronized (mLock) {
698                if (mCurrentUserId != getChangingUserId()) {
699                    return;
700                }
701                WallpaperData wallpaper = mWallpaperMap.get(mCurrentUserId);
702                if (wallpaper != null) {
703                    if (wallpaper.wallpaperComponent != null
704                            && wallpaper.wallpaperComponent.getPackageName().equals(packageName)) {
705                        wallpaper.wallpaperUpdating = false;
706                        ComponentName comp = wallpaper.wallpaperComponent;
707                        clearWallpaperComponentLocked(wallpaper);
708                        if (!bindWallpaperComponentLocked(comp, false, false,
709                                wallpaper, null)) {
710                            Slog.w(TAG, "Wallpaper no longer available; reverting to default");
711                            clearWallpaperLocked(false, FLAG_SYSTEM, wallpaper.userId, null);
712                        }
713                    }
714                }
715            }
716        }
717
718        @Override
719        public void onPackageModified(String packageName) {
720            synchronized (mLock) {
721                if (mCurrentUserId != getChangingUserId()) {
722                    return;
723                }
724                WallpaperData wallpaper = mWallpaperMap.get(mCurrentUserId);
725                if (wallpaper != null) {
726                    if (wallpaper.wallpaperComponent == null
727                            || !wallpaper.wallpaperComponent.getPackageName().equals(packageName)) {
728                        return;
729                    }
730                    doPackagesChangedLocked(true, wallpaper);
731                }
732            }
733        }
734
735        @Override
736        public void onPackageUpdateStarted(String packageName, int uid) {
737            synchronized (mLock) {
738                if (mCurrentUserId != getChangingUserId()) {
739                    return;
740                }
741                WallpaperData wallpaper = mWallpaperMap.get(mCurrentUserId);
742                if (wallpaper != null) {
743                    if (wallpaper.wallpaperComponent != null
744                            && wallpaper.wallpaperComponent.getPackageName().equals(packageName)) {
745                        wallpaper.wallpaperUpdating = true;
746                    }
747                }
748            }
749        }
750
751        @Override
752        public boolean onHandleForceStop(Intent intent, String[] packages, int uid, boolean doit) {
753            synchronized (mLock) {
754                boolean changed = false;
755                if (mCurrentUserId != getChangingUserId()) {
756                    return false;
757                }
758                WallpaperData wallpaper = mWallpaperMap.get(mCurrentUserId);
759                if (wallpaper != null) {
760                    boolean res = doPackagesChangedLocked(doit, wallpaper);
761                    changed |= res;
762                }
763                return changed;
764            }
765        }
766
767        @Override
768        public void onSomePackagesChanged() {
769            synchronized (mLock) {
770                if (mCurrentUserId != getChangingUserId()) {
771                    return;
772                }
773                WallpaperData wallpaper = mWallpaperMap.get(mCurrentUserId);
774                if (wallpaper != null) {
775                    doPackagesChangedLocked(true, wallpaper);
776                }
777            }
778        }
779
780        boolean doPackagesChangedLocked(boolean doit, WallpaperData wallpaper) {
781            boolean changed = false;
782            if (wallpaper.wallpaperComponent != null) {
783                int change = isPackageDisappearing(wallpaper.wallpaperComponent
784                        .getPackageName());
785                if (change == PACKAGE_PERMANENT_CHANGE
786                        || change == PACKAGE_TEMPORARY_CHANGE) {
787                    changed = true;
788                    if (doit) {
789                        Slog.w(TAG, "Wallpaper uninstalled, removing: "
790                                + wallpaper.wallpaperComponent);
791                        clearWallpaperLocked(false, FLAG_SYSTEM, wallpaper.userId, null);
792                    }
793                }
794            }
795            if (wallpaper.nextWallpaperComponent != null) {
796                int change = isPackageDisappearing(wallpaper.nextWallpaperComponent
797                        .getPackageName());
798                if (change == PACKAGE_PERMANENT_CHANGE
799                        || change == PACKAGE_TEMPORARY_CHANGE) {
800                    wallpaper.nextWallpaperComponent = null;
801                }
802            }
803            if (wallpaper.wallpaperComponent != null
804                    && isPackageModified(wallpaper.wallpaperComponent.getPackageName())) {
805                try {
806                    mContext.getPackageManager().getServiceInfo(wallpaper.wallpaperComponent,
807                            PackageManager.MATCH_DIRECT_BOOT_AWARE
808                                    | PackageManager.MATCH_DIRECT_BOOT_UNAWARE);
809                } catch (NameNotFoundException e) {
810                    Slog.w(TAG, "Wallpaper component gone, removing: "
811                            + wallpaper.wallpaperComponent);
812                    clearWallpaperLocked(false, FLAG_SYSTEM, wallpaper.userId, null);
813                }
814            }
815            if (wallpaper.nextWallpaperComponent != null
816                    && isPackageModified(wallpaper.nextWallpaperComponent.getPackageName())) {
817                try {
818                    mContext.getPackageManager().getServiceInfo(wallpaper.nextWallpaperComponent,
819                            PackageManager.MATCH_DIRECT_BOOT_AWARE
820                                    | PackageManager.MATCH_DIRECT_BOOT_UNAWARE);
821                } catch (NameNotFoundException e) {
822                    wallpaper.nextWallpaperComponent = null;
823                }
824            }
825            return changed;
826        }
827    }
828
829    public WallpaperManagerService(Context context) {
830        if (DEBUG) Slog.v(TAG, "WallpaperService startup");
831        mContext = context;
832        mImageWallpaper = ComponentName.unflattenFromString(
833                context.getResources().getString(R.string.image_wallpaper_component));
834        mIWindowManager = IWindowManager.Stub.asInterface(
835                ServiceManager.getService(Context.WINDOW_SERVICE));
836        mIPackageManager = AppGlobals.getPackageManager();
837        mAppOpsManager = (AppOpsManager) mContext.getSystemService(Context.APP_OPS_SERVICE);
838        mMonitor = new MyPackageMonitor();
839        mMonitor.register(context, null, UserHandle.ALL, true);
840        getWallpaperDir(UserHandle.USER_SYSTEM).mkdirs();
841        loadSettingsLocked(UserHandle.USER_SYSTEM, false);
842    }
843
844    private static File getWallpaperDir(int userId) {
845        return Environment.getUserSystemDirectory(userId);
846    }
847
848    @Override
849    protected void finalize() throws Throwable {
850        super.finalize();
851        for (int i = 0; i < mWallpaperMap.size(); i++) {
852            WallpaperData wallpaper = mWallpaperMap.valueAt(i);
853            wallpaper.wallpaperObserver.stopWatching();
854        }
855    }
856
857    void systemReady() {
858        if (DEBUG) Slog.v(TAG, "systemReady");
859        WallpaperData wallpaper = mWallpaperMap.get(UserHandle.USER_SYSTEM);
860        // If we think we're going to be using the system image wallpaper imagery, make
861        // sure we have something to render
862        if (mImageWallpaper.equals(wallpaper.nextWallpaperComponent)) {
863            // No crop file? Make sure we've finished the processing sequence if necessary
864            if (!wallpaper.cropExists()) {
865                if (DEBUG) {
866                    Slog.i(TAG, "No crop; regenerating from source");
867                }
868                generateCrop(wallpaper);
869            }
870            // Still nothing?  Fall back to default.
871            if (!wallpaper.cropExists()) {
872                if (DEBUG) {
873                    Slog.i(TAG, "Unable to regenerate crop; resetting");
874                }
875                clearWallpaperLocked(false, FLAG_SYSTEM, UserHandle.USER_SYSTEM, null);
876            }
877        } else {
878            if (DEBUG) {
879                Slog.i(TAG, "Nondefault wallpaper component; gracefully ignoring");
880            }
881        }
882
883        IntentFilter userFilter = new IntentFilter();
884        userFilter.addAction(Intent.ACTION_USER_REMOVED);
885        mContext.registerReceiver(new BroadcastReceiver() {
886            @Override
887            public void onReceive(Context context, Intent intent) {
888                final String action = intent.getAction();
889                if (Intent.ACTION_USER_REMOVED.equals(action)) {
890                    onRemoveUser(intent.getIntExtra(Intent.EXTRA_USER_HANDLE,
891                            UserHandle.USER_NULL));
892                }
893            }
894        }, userFilter);
895
896        try {
897            ActivityManagerNative.getDefault().registerUserSwitchObserver(
898                    new IUserSwitchObserver.Stub() {
899                        @Override
900                        public void onUserSwitching(int newUserId, IRemoteCallback reply) {
901                            switchUser(newUserId, reply);
902                        }
903
904                        @Override
905                        public void onUserSwitchComplete(int newUserId) throws RemoteException {
906                        }
907
908                        @Override
909                        public void onForegroundProfileSwitch(int newProfileId) {
910                            // Ignore.
911                        }
912                    }, TAG);
913        } catch (RemoteException e) {
914            e.rethrowAsRuntimeException();
915        }
916    }
917
918    /** Called by SystemBackupAgent */
919    public String getName() {
920        // Verify caller is the system
921        if (Binder.getCallingUid() != android.os.Process.SYSTEM_UID) {
922            throw new RuntimeException("getName() can only be called from the system process");
923        }
924        synchronized (mLock) {
925            return mWallpaperMap.get(0).name;
926        }
927    }
928
929    void stopObserver(WallpaperData wallpaper) {
930        if (wallpaper != null) {
931            if (wallpaper.wallpaperObserver != null) {
932                wallpaper.wallpaperObserver.stopWatching();
933                wallpaper.wallpaperObserver = null;
934            }
935        }
936    }
937
938    void stopObserversLocked(int userId) {
939        stopObserver(mWallpaperMap.get(userId));
940        stopObserver(mLockWallpaperMap.get(userId));
941        mWallpaperMap.remove(userId);
942        mLockWallpaperMap.remove(userId);
943    }
944
945    void onUnlockUser(final int userId) {
946        synchronized (mLock) {
947            if (mCurrentUserId == userId && mWaitingForUnlock) {
948                switchUser(userId, null);
949
950                // Make sure that the SELinux labeling of all the relevant files is correct.
951                // This corrects for mislabeling bugs that might have arisen from move-to
952                // operations involving the wallpaper files.  This isn't timing-critical,
953                // so we do it in the background to avoid holding up the user unlock operation.
954                Runnable relabeler = new Runnable() {
955                    @Override
956                    public void run() {
957                        final File wallpaperDir = getWallpaperDir(userId);
958                        for (String filename : sPerUserFiles) {
959                            File f = new File(wallpaperDir, filename);
960                            if (f.exists()) {
961                                SELinux.restorecon(f);
962                            }
963                        }
964                    }
965                };
966                BackgroundThread.getHandler().post(relabeler);
967            }
968        }
969    }
970
971    void onRemoveUser(int userId) {
972        if (userId < 1) return;
973
974        final File wallpaperDir = getWallpaperDir(userId);
975        synchronized (mLock) {
976            stopObserversLocked(userId);
977            for (String filename : sPerUserFiles) {
978                new File(wallpaperDir, filename).delete();
979            }
980        }
981    }
982
983    void switchUser(int userId, IRemoteCallback reply) {
984        synchronized (mLock) {
985            mCurrentUserId = userId;
986            WallpaperData wallpaper = getWallpaperSafeLocked(userId, FLAG_SYSTEM);
987            // Not started watching yet, in case wallpaper data was loaded for other reasons.
988            if (wallpaper.wallpaperObserver == null) {
989                wallpaper.wallpaperObserver = new WallpaperObserver(wallpaper);
990                wallpaper.wallpaperObserver.startWatching();
991            }
992            switchWallpaper(wallpaper, reply);
993        }
994    }
995
996    void switchWallpaper(WallpaperData wallpaper, IRemoteCallback reply) {
997        synchronized (mLock) {
998            mWaitingForUnlock = false;
999            final ComponentName cname = wallpaper.wallpaperComponent != null ?
1000                    wallpaper.wallpaperComponent : wallpaper.nextWallpaperComponent;
1001            if (!bindWallpaperComponentLocked(cname, true, false, wallpaper, reply)) {
1002                // We failed to bind the desired wallpaper, but that might
1003                // happen if the wallpaper isn't direct-boot aware
1004                ServiceInfo si = null;
1005                try {
1006                    si = mIPackageManager.getServiceInfo(cname,
1007                            PackageManager.MATCH_DIRECT_BOOT_UNAWARE, wallpaper.userId);
1008                } catch (RemoteException ignored) {
1009                }
1010
1011                if (si == null) {
1012                    Slog.w(TAG, "Failure starting previous wallpaper; clearing");
1013                    clearWallpaperLocked(false, FLAG_SYSTEM, wallpaper.userId, reply);
1014                } else {
1015                    Slog.w(TAG, "Wallpaper isn't direct boot aware; using fallback until unlocked");
1016                    // We might end up persisting the current wallpaper data
1017                    // while locked, so pretend like the component was actually
1018                    // bound into place
1019                    wallpaper.wallpaperComponent = wallpaper.nextWallpaperComponent;
1020                    final WallpaperData fallback = new WallpaperData(wallpaper.userId,
1021                            WALLPAPER_LOCK_ORIG, WALLPAPER_LOCK_CROP);
1022                    ensureSaneWallpaperData(fallback);
1023                    bindWallpaperComponentLocked(mImageWallpaper, true, false, fallback, reply);
1024                    mWaitingForUnlock = true;
1025                }
1026            }
1027        }
1028    }
1029
1030    @Override
1031    public void clearWallpaper(String callingPackage, int which, int userId) {
1032        if (DEBUG) Slog.v(TAG, "clearWallpaper");
1033        checkPermission(android.Manifest.permission.SET_WALLPAPER);
1034        if (!isWallpaperSupported(callingPackage) || !isSetWallpaperAllowed(callingPackage)) {
1035            return;
1036        }
1037        userId = ActivityManager.handleIncomingUser(Binder.getCallingPid(),
1038                Binder.getCallingUid(), userId, false, true, "clearWallpaper", null);
1039
1040        synchronized (mLock) {
1041            clearWallpaperLocked(false, which, userId, null);
1042        }
1043    }
1044
1045    void clearWallpaperLocked(boolean defaultFailed, int which, int userId, IRemoteCallback reply) {
1046        if (which != FLAG_SYSTEM && which != FLAG_LOCK) {
1047            throw new IllegalArgumentException("Must specify exactly one kind of wallpaper to read");
1048        }
1049
1050        WallpaperData wallpaper = null;
1051        if (which == FLAG_LOCK) {
1052            wallpaper = mLockWallpaperMap.get(userId);
1053            if (wallpaper == null) {
1054                // It's already gone; we're done.
1055                if (DEBUG) {
1056                    Slog.i(TAG, "Lock wallpaper already cleared");
1057                }
1058                return;
1059            }
1060        } else {
1061            wallpaper = mWallpaperMap.get(userId);
1062            if (wallpaper == null) {
1063                // Might need to bring it in the first time to establish our rewrite
1064                loadSettingsLocked(userId, false);
1065                wallpaper = mWallpaperMap.get(userId);
1066            }
1067        }
1068        if (wallpaper == null) {
1069            return;
1070        }
1071
1072        final long ident = Binder.clearCallingIdentity();
1073        try {
1074            if (wallpaper.wallpaperFile.exists()) {
1075                wallpaper.wallpaperFile.delete();
1076                wallpaper.cropFile.delete();
1077                if (which == FLAG_LOCK) {
1078                    mLockWallpaperMap.remove(userId);
1079                    final IWallpaperManagerCallback cb = mKeyguardListener;
1080                    if (cb != null) {
1081                        if (DEBUG) {
1082                            Slog.i(TAG, "Notifying keyguard of lock wallpaper clear");
1083                        }
1084                        try {
1085                            cb.onWallpaperChanged();
1086                        } catch (RemoteException e) {
1087                            // Oh well it went away; no big deal
1088                        }
1089                    }
1090                    saveSettingsLocked(userId);
1091                    return;
1092                }
1093            }
1094
1095            RuntimeException e = null;
1096            try {
1097                wallpaper.imageWallpaperPending = false;
1098                if (userId != mCurrentUserId) return;
1099                if (bindWallpaperComponentLocked(defaultFailed
1100                        ? mImageWallpaper
1101                                : null, true, false, wallpaper, reply)) {
1102                    return;
1103                }
1104            } catch (IllegalArgumentException e1) {
1105                e = e1;
1106            }
1107
1108            // This can happen if the default wallpaper component doesn't
1109            // exist.  This should be a system configuration problem, but
1110            // let's not let it crash the system and just live with no
1111            // wallpaper.
1112            Slog.e(TAG, "Default wallpaper component not found!", e);
1113            clearWallpaperComponentLocked(wallpaper);
1114            if (reply != null) {
1115                try {
1116                    reply.sendResult(null);
1117                } catch (RemoteException e1) {
1118                }
1119            }
1120        } finally {
1121            Binder.restoreCallingIdentity(ident);
1122        }
1123    }
1124
1125    public boolean hasNamedWallpaper(String name) {
1126        synchronized (mLock) {
1127            List<UserInfo> users;
1128            long ident = Binder.clearCallingIdentity();
1129            try {
1130                users = ((UserManager) mContext.getSystemService(Context.USER_SERVICE)).getUsers();
1131            } finally {
1132                Binder.restoreCallingIdentity(ident);
1133            }
1134            for (UserInfo user: users) {
1135                // ignore managed profiles
1136                if (user.isManagedProfile()) {
1137                    continue;
1138                }
1139                WallpaperData wd = mWallpaperMap.get(user.id);
1140                if (wd == null) {
1141                    // User hasn't started yet, so load her settings to peek at the wallpaper
1142                    loadSettingsLocked(user.id, false);
1143                    wd = mWallpaperMap.get(user.id);
1144                }
1145                if (wd != null && name.equals(wd.name)) {
1146                    return true;
1147                }
1148            }
1149        }
1150        return false;
1151    }
1152
1153    private Point getDefaultDisplaySize() {
1154        Point p = new Point();
1155        WindowManager wm = (WindowManager)mContext.getSystemService(Context.WINDOW_SERVICE);
1156        Display d = wm.getDefaultDisplay();
1157        d.getRealSize(p);
1158        return p;
1159    }
1160
1161    public void setDimensionHints(int width, int height, String callingPackage)
1162            throws RemoteException {
1163        checkPermission(android.Manifest.permission.SET_WALLPAPER_HINTS);
1164        if (!isWallpaperSupported(callingPackage)) {
1165            return;
1166        }
1167        synchronized (mLock) {
1168            int userId = UserHandle.getCallingUserId();
1169            WallpaperData wallpaper = getWallpaperSafeLocked(userId, FLAG_SYSTEM);
1170            if (width <= 0 || height <= 0) {
1171                throw new IllegalArgumentException("width and height must be > 0");
1172            }
1173            // Make sure it is at least as large as the display.
1174            Point displaySize = getDefaultDisplaySize();
1175            width = Math.max(width, displaySize.x);
1176            height = Math.max(height, displaySize.y);
1177
1178            if (width != wallpaper.width || height != wallpaper.height) {
1179                wallpaper.width = width;
1180                wallpaper.height = height;
1181                saveSettingsLocked(userId);
1182                if (mCurrentUserId != userId) return; // Don't change the properties now
1183                if (wallpaper.connection != null) {
1184                    if (wallpaper.connection.mEngine != null) {
1185                        try {
1186                            wallpaper.connection.mEngine.setDesiredSize(
1187                                    width, height);
1188                        } catch (RemoteException e) {
1189                        }
1190                        notifyCallbacksLocked(wallpaper);
1191                    } else if (wallpaper.connection.mService != null) {
1192                        // We've attached to the service but the engine hasn't attached back to us
1193                        // yet. This means it will be created with the previous dimensions, so we
1194                        // need to update it to the new dimensions once it attaches.
1195                        wallpaper.connection.mDimensionsChanged = true;
1196                    }
1197                }
1198            }
1199        }
1200    }
1201
1202    public int getWidthHint() throws RemoteException {
1203        synchronized (mLock) {
1204            WallpaperData wallpaper = mWallpaperMap.get(UserHandle.getCallingUserId());
1205            if (wallpaper != null) {
1206                return wallpaper.width;
1207            } else {
1208                return 0;
1209            }
1210        }
1211    }
1212
1213    public int getHeightHint() throws RemoteException {
1214        synchronized (mLock) {
1215            WallpaperData wallpaper = mWallpaperMap.get(UserHandle.getCallingUserId());
1216            if (wallpaper != null) {
1217                return wallpaper.height;
1218            } else {
1219                return 0;
1220            }
1221        }
1222    }
1223
1224    public void setDisplayPadding(Rect padding, String callingPackage) {
1225        checkPermission(android.Manifest.permission.SET_WALLPAPER_HINTS);
1226        if (!isWallpaperSupported(callingPackage)) {
1227            return;
1228        }
1229        synchronized (mLock) {
1230            int userId = UserHandle.getCallingUserId();
1231            WallpaperData wallpaper = getWallpaperSafeLocked(userId, FLAG_SYSTEM);
1232            if (padding.left < 0 || padding.top < 0 || padding.right < 0 || padding.bottom < 0) {
1233                throw new IllegalArgumentException("padding must be positive: " + padding);
1234            }
1235
1236            if (!padding.equals(wallpaper.padding)) {
1237                wallpaper.padding.set(padding);
1238                saveSettingsLocked(userId);
1239                if (mCurrentUserId != userId) return; // Don't change the properties now
1240                if (wallpaper.connection != null) {
1241                    if (wallpaper.connection.mEngine != null) {
1242                        try {
1243                            wallpaper.connection.mEngine.setDisplayPadding(padding);
1244                        } catch (RemoteException e) {
1245                        }
1246                        notifyCallbacksLocked(wallpaper);
1247                    } else if (wallpaper.connection.mService != null) {
1248                        // We've attached to the service but the engine hasn't attached back to us
1249                        // yet. This means it will be created with the previous dimensions, so we
1250                        // need to update it to the new dimensions once it attaches.
1251                        wallpaper.connection.mPaddingChanged = true;
1252                    }
1253                }
1254            }
1255        }
1256    }
1257
1258    @Override
1259    public ParcelFileDescriptor getWallpaper(IWallpaperManagerCallback cb, final int which,
1260            Bundle outParams, int wallpaperUserId) {
1261        wallpaperUserId = ActivityManager.handleIncomingUser(Binder.getCallingPid(),
1262                Binder.getCallingUid(), wallpaperUserId, false, true, "getWallpaper", null);
1263
1264        if (which != FLAG_SYSTEM && which != FLAG_LOCK) {
1265            throw new IllegalArgumentException("Must specify exactly one kind of wallpaper to read");
1266        }
1267
1268        synchronized (mLock) {
1269            final SparseArray<WallpaperData> whichSet =
1270                    (which == FLAG_LOCK) ? mLockWallpaperMap : mWallpaperMap;
1271            WallpaperData wallpaper = whichSet.get(wallpaperUserId);
1272            if (wallpaper == null) {
1273                // common case, this is the first lookup post-boot of the system or
1274                // unified lock, so we bring up the saved state lazily now and recheck.
1275                loadSettingsLocked(wallpaperUserId, false);
1276                wallpaper = whichSet.get(wallpaperUserId);
1277                if (wallpaper == null) {
1278                    return null;
1279                }
1280            }
1281            try {
1282                if (outParams != null) {
1283                    outParams.putInt("width", wallpaper.width);
1284                    outParams.putInt("height", wallpaper.height);
1285                }
1286                if (cb != null) {
1287                    wallpaper.callbacks.register(cb);
1288                }
1289                if (!wallpaper.cropFile.exists()) {
1290                    return null;
1291                }
1292                return ParcelFileDescriptor.open(wallpaper.cropFile, MODE_READ_ONLY);
1293            } catch (FileNotFoundException e) {
1294                /* Shouldn't happen as we check to see if the file exists */
1295                Slog.w(TAG, "Error getting wallpaper", e);
1296            }
1297            return null;
1298        }
1299    }
1300
1301    @Override
1302    public WallpaperInfo getWallpaperInfo(int userId) {
1303        userId = ActivityManager.handleIncomingUser(Binder.getCallingPid(),
1304                Binder.getCallingUid(), userId, false, true, "getWallpaperIdForUser", null);
1305        synchronized (mLock) {
1306            WallpaperData wallpaper = mWallpaperMap.get(userId);
1307            if (wallpaper != null && wallpaper.connection != null) {
1308                return wallpaper.connection.mInfo;
1309            }
1310            return null;
1311        }
1312    }
1313
1314    @Override
1315    public int getWallpaperIdForUser(int which, int userId) {
1316        userId = ActivityManager.handleIncomingUser(Binder.getCallingPid(),
1317                Binder.getCallingUid(), userId, false, true, "getWallpaperIdForUser", null);
1318
1319        if (which != FLAG_SYSTEM && which != FLAG_LOCK) {
1320            throw new IllegalArgumentException("Must specify exactly one kind of wallpaper");
1321        }
1322
1323        final SparseArray<WallpaperData> map =
1324                (which == FLAG_LOCK) ? mLockWallpaperMap : mWallpaperMap;
1325        synchronized (mLock) {
1326            WallpaperData wallpaper = map.get(userId);
1327            if (wallpaper != null) {
1328                return wallpaper.wallpaperId;
1329            }
1330        }
1331        return -1;
1332    }
1333
1334    @Override
1335    public boolean setLockWallpaperCallback(IWallpaperManagerCallback cb) {
1336        checkPermission(android.Manifest.permission.INTERNAL_SYSTEM_WINDOW);
1337        synchronized (mLock) {
1338            mKeyguardListener = cb;
1339        }
1340        return true;
1341    }
1342
1343    @Override
1344    public ParcelFileDescriptor setWallpaper(String name, String callingPackage,
1345            Rect cropHint, boolean allowBackup, Bundle extras, int which,
1346            IWallpaperManagerCallback completion) {
1347        checkPermission(android.Manifest.permission.SET_WALLPAPER);
1348
1349        if ((which & (FLAG_LOCK|FLAG_SYSTEM)) == 0) {
1350            final String msg = "Must specify a valid wallpaper category to set";
1351            Slog.e(TAG, msg);
1352            throw new IllegalArgumentException(msg);
1353        }
1354
1355        if (!isWallpaperSupported(callingPackage) || !isSetWallpaperAllowed(callingPackage)) {
1356            return null;
1357        }
1358
1359        // "null" means the no-op crop, preserving the full input image
1360        if (cropHint == null) {
1361            cropHint = new Rect(0, 0, 0, 0);
1362        } else {
1363            if (cropHint.isEmpty()
1364                    || cropHint.left < 0
1365                    || cropHint.top < 0) {
1366                throw new IllegalArgumentException("Invalid crop rect supplied: " + cropHint);
1367            }
1368        }
1369
1370        final int userId = UserHandle.getCallingUserId();
1371
1372        synchronized (mLock) {
1373            if (DEBUG) Slog.v(TAG, "setWallpaper which=0x" + Integer.toHexString(which));
1374            WallpaperData wallpaper;
1375
1376            /* If we're setting system but not lock, and lock is currently sharing the system
1377             * wallpaper, we need to migrate that image over to being lock-only before
1378             * the caller here writes new bitmap data.
1379             */
1380            if (which == FLAG_SYSTEM && mLockWallpaperMap.get(userId) == null) {
1381                if (DEBUG) {
1382                    Slog.i(TAG, "Migrating system->lock to preserve");
1383                }
1384                migrateSystemToLockWallpaperLocked(userId);
1385            }
1386
1387            wallpaper = getWallpaperSafeLocked(userId, which);
1388            final long ident = Binder.clearCallingIdentity();
1389            try {
1390                ParcelFileDescriptor pfd = updateWallpaperBitmapLocked(name, wallpaper, extras);
1391                if (pfd != null) {
1392                    wallpaper.imageWallpaperPending = true;
1393                    wallpaper.whichPending = which;
1394                    wallpaper.setComplete = completion;
1395                    wallpaper.cropHint.set(cropHint);
1396                    if ((which & FLAG_SYSTEM) != 0) {
1397                        wallpaper.allowBackup = allowBackup;
1398                    }
1399                }
1400                return pfd;
1401            } finally {
1402                Binder.restoreCallingIdentity(ident);
1403            }
1404        }
1405    }
1406
1407    private void migrateSystemToLockWallpaperLocked(int userId) {
1408        WallpaperData sysWP = mWallpaperMap.get(userId);
1409        if (sysWP == null) {
1410            if (DEBUG) {
1411                Slog.i(TAG, "No system wallpaper?  Not tracking for lock-only");
1412            }
1413            return;
1414        }
1415
1416        // We know a-priori that there is no lock-only wallpaper currently
1417        WallpaperData lockWP = new WallpaperData(userId,
1418                WALLPAPER_LOCK_ORIG, WALLPAPER_LOCK_CROP);
1419        lockWP.wallpaperId = sysWP.wallpaperId;
1420        lockWP.cropHint.set(sysWP.cropHint);
1421        lockWP.width = sysWP.width;
1422        lockWP.height = sysWP.height;
1423        lockWP.allowBackup = false;
1424
1425        // Migrate the bitmap files outright; no need to copy
1426        try {
1427            Os.rename(sysWP.wallpaperFile.getAbsolutePath(), lockWP.wallpaperFile.getAbsolutePath());
1428            Os.rename(sysWP.cropFile.getAbsolutePath(), lockWP.cropFile.getAbsolutePath());
1429        } catch (ErrnoException e) {
1430            Slog.e(TAG, "Can't migrate system wallpaper: " + e.getMessage());
1431            lockWP.wallpaperFile.delete();
1432            lockWP.cropFile.delete();
1433            return;
1434        }
1435
1436        mLockWallpaperMap.put(userId, lockWP);
1437    }
1438
1439    ParcelFileDescriptor updateWallpaperBitmapLocked(String name, WallpaperData wallpaper,
1440            Bundle extras) {
1441        if (name == null) name = "";
1442        try {
1443            File dir = getWallpaperDir(wallpaper.userId);
1444            if (!dir.exists()) {
1445                dir.mkdir();
1446                FileUtils.setPermissions(
1447                        dir.getPath(),
1448                        FileUtils.S_IRWXU|FileUtils.S_IRWXG|FileUtils.S_IXOTH,
1449                        -1, -1);
1450            }
1451            ParcelFileDescriptor fd = ParcelFileDescriptor.open(wallpaper.wallpaperFile,
1452                    MODE_CREATE|MODE_READ_WRITE|MODE_TRUNCATE);
1453            if (!SELinux.restorecon(wallpaper.wallpaperFile)) {
1454                return null;
1455            }
1456            wallpaper.name = name;
1457            wallpaper.wallpaperId = makeWallpaperIdLocked();
1458            if (extras != null) {
1459                extras.putInt(WallpaperManager.EXTRA_NEW_WALLPAPER_ID, wallpaper.wallpaperId);
1460            }
1461            if (DEBUG) {
1462                Slog.v(TAG, "updateWallpaperBitmapLocked() : id=" + wallpaper.wallpaperId
1463                        + " name=" + name + " file=" + wallpaper.wallpaperFile.getName());
1464            }
1465            return fd;
1466        } catch (FileNotFoundException e) {
1467            Slog.w(TAG, "Error setting wallpaper", e);
1468        }
1469        return null;
1470    }
1471
1472    @Override
1473    public void setWallpaperComponentChecked(ComponentName name, String callingPackage,
1474            int userId) {
1475
1476        if (isWallpaperSupported(callingPackage) && isSetWallpaperAllowed(callingPackage)) {
1477            setWallpaperComponent(name, userId);
1478        }
1479    }
1480
1481    // ToDo: Remove this version of the function
1482    @Override
1483    public void setWallpaperComponent(ComponentName name) {
1484        setWallpaperComponent(name, UserHandle.getCallingUserId());
1485    }
1486
1487    private void setWallpaperComponent(ComponentName name, int userId) {
1488        userId = ActivityManager.handleIncomingUser(getCallingPid(), getCallingUid(), userId,
1489                false /* all */, true /* full */, "changing live wallpaper", null /* pkg */);
1490        checkPermission(android.Manifest.permission.SET_WALLPAPER_COMPONENT);
1491
1492        synchronized (mLock) {
1493            if (DEBUG) Slog.v(TAG, "setWallpaperComponent name=" + name);
1494            WallpaperData wallpaper = mWallpaperMap.get(userId);
1495            if (wallpaper == null) {
1496                throw new IllegalStateException("Wallpaper not yet initialized for user " + userId);
1497            }
1498            final long ident = Binder.clearCallingIdentity();
1499            try {
1500                wallpaper.imageWallpaperPending = false;
1501                if (bindWallpaperComponentLocked(name, false, true, wallpaper, null)) {
1502                    wallpaper.wallpaperId = makeWallpaperIdLocked();
1503                    notifyCallbacksLocked(wallpaper);
1504                }
1505            } finally {
1506                Binder.restoreCallingIdentity(ident);
1507            }
1508        }
1509    }
1510
1511    boolean bindWallpaperComponentLocked(ComponentName componentName, boolean force,
1512            boolean fromUser, WallpaperData wallpaper, IRemoteCallback reply) {
1513        if (DEBUG) Slog.v(TAG, "bindWallpaperComponentLocked: componentName=" + componentName);
1514        // Has the component changed?
1515        if (!force) {
1516            if (wallpaper.connection != null) {
1517                if (wallpaper.wallpaperComponent == null) {
1518                    if (componentName == null) {
1519                        if (DEBUG) Slog.v(TAG, "bindWallpaperComponentLocked: still using default");
1520                        // Still using default wallpaper.
1521                        return true;
1522                    }
1523                } else if (wallpaper.wallpaperComponent.equals(componentName)) {
1524                    // Changing to same wallpaper.
1525                    if (DEBUG) Slog.v(TAG, "same wallpaper");
1526                    return true;
1527                }
1528            }
1529        }
1530
1531        try {
1532            if (componentName == null) {
1533                componentName = WallpaperManager.getDefaultWallpaperComponent(mContext);
1534                if (componentName == null) {
1535                    // Fall back to static image wallpaper
1536                    componentName = mImageWallpaper;
1537                    //clearWallpaperComponentLocked();
1538                    //return;
1539                    if (DEBUG) Slog.v(TAG, "Using image wallpaper");
1540                }
1541            }
1542            int serviceUserId = wallpaper.userId;
1543            ServiceInfo si = mIPackageManager.getServiceInfo(componentName,
1544                    PackageManager.GET_META_DATA | PackageManager.GET_PERMISSIONS, serviceUserId);
1545            if (si == null) {
1546                // The wallpaper component we're trying to use doesn't exist
1547                Slog.w(TAG, "Attempted wallpaper " + componentName + " is unavailable");
1548                return false;
1549            }
1550            if (!android.Manifest.permission.BIND_WALLPAPER.equals(si.permission)) {
1551                String msg = "Selected service does not require "
1552                        + android.Manifest.permission.BIND_WALLPAPER
1553                        + ": " + componentName;
1554                if (fromUser) {
1555                    throw new SecurityException(msg);
1556                }
1557                Slog.w(TAG, msg);
1558                return false;
1559            }
1560
1561            WallpaperInfo wi = null;
1562
1563            Intent intent = new Intent(WallpaperService.SERVICE_INTERFACE);
1564            if (componentName != null && !componentName.equals(mImageWallpaper)) {
1565                // Make sure the selected service is actually a wallpaper service.
1566                List<ResolveInfo> ris =
1567                        mIPackageManager.queryIntentServices(intent,
1568                                intent.resolveTypeIfNeeded(mContext.getContentResolver()),
1569                                PackageManager.GET_META_DATA, serviceUserId).getList();
1570                for (int i=0; i<ris.size(); i++) {
1571                    ServiceInfo rsi = ris.get(i).serviceInfo;
1572                    if (rsi.name.equals(si.name) &&
1573                            rsi.packageName.equals(si.packageName)) {
1574                        try {
1575                            wi = new WallpaperInfo(mContext, ris.get(i));
1576                        } catch (XmlPullParserException e) {
1577                            if (fromUser) {
1578                                throw new IllegalArgumentException(e);
1579                            }
1580                            Slog.w(TAG, e);
1581                            return false;
1582                        } catch (IOException e) {
1583                            if (fromUser) {
1584                                throw new IllegalArgumentException(e);
1585                            }
1586                            Slog.w(TAG, e);
1587                            return false;
1588                        }
1589                        break;
1590                    }
1591                }
1592                if (wi == null) {
1593                    String msg = "Selected service is not a wallpaper: "
1594                            + componentName;
1595                    if (fromUser) {
1596                        throw new SecurityException(msg);
1597                    }
1598                    Slog.w(TAG, msg);
1599                    return false;
1600                }
1601            }
1602
1603            // Bind the service!
1604            if (DEBUG) Slog.v(TAG, "Binding to:" + componentName);
1605            WallpaperConnection newConn = new WallpaperConnection(wi, wallpaper);
1606            intent.setComponent(componentName);
1607            intent.putExtra(Intent.EXTRA_CLIENT_LABEL,
1608                    com.android.internal.R.string.wallpaper_binding_label);
1609            intent.putExtra(Intent.EXTRA_CLIENT_INTENT, PendingIntent.getActivityAsUser(
1610                    mContext, 0,
1611                    Intent.createChooser(new Intent(Intent.ACTION_SET_WALLPAPER),
1612                            mContext.getText(com.android.internal.R.string.chooser_wallpaper)),
1613                    0, null, new UserHandle(serviceUserId)));
1614            if (!mContext.bindServiceAsUser(intent, newConn,
1615                    Context.BIND_AUTO_CREATE | Context.BIND_SHOWING_UI
1616                            | Context.BIND_FOREGROUND_SERVICE_WHILE_AWAKE,
1617                    new UserHandle(serviceUserId))) {
1618                String msg = "Unable to bind service: "
1619                        + componentName;
1620                if (fromUser) {
1621                    throw new IllegalArgumentException(msg);
1622                }
1623                Slog.w(TAG, msg);
1624                return false;
1625            }
1626            if (wallpaper.userId == mCurrentUserId && mLastWallpaper != null) {
1627                detachWallpaperLocked(mLastWallpaper);
1628            }
1629            wallpaper.wallpaperComponent = componentName;
1630            wallpaper.connection = newConn;
1631            newConn.mReply = reply;
1632            try {
1633                if (wallpaper.userId == mCurrentUserId) {
1634                    if (DEBUG)
1635                        Slog.v(TAG, "Adding window token: " + newConn.mToken);
1636                    mIWindowManager.addWindowToken(newConn.mToken,
1637                            WindowManager.LayoutParams.TYPE_WALLPAPER);
1638                    mLastWallpaper = wallpaper;
1639                }
1640            } catch (RemoteException e) {
1641            }
1642        } catch (RemoteException e) {
1643            String msg = "Remote exception for " + componentName + "\n" + e;
1644            if (fromUser) {
1645                throw new IllegalArgumentException(msg);
1646            }
1647            Slog.w(TAG, msg);
1648            return false;
1649        }
1650        return true;
1651    }
1652
1653    void detachWallpaperLocked(WallpaperData wallpaper) {
1654        if (wallpaper.connection != null) {
1655            if (wallpaper.connection.mReply != null) {
1656                try {
1657                    wallpaper.connection.mReply.sendResult(null);
1658                } catch (RemoteException e) {
1659                }
1660                wallpaper.connection.mReply = null;
1661            }
1662            if (wallpaper.connection.mEngine != null) {
1663                try {
1664                    wallpaper.connection.mEngine.destroy();
1665                } catch (RemoteException e) {
1666                }
1667            }
1668            mContext.unbindService(wallpaper.connection);
1669            try {
1670                if (DEBUG)
1671                    Slog.v(TAG, "Removing window token: " + wallpaper.connection.mToken);
1672                mIWindowManager.removeWindowToken(wallpaper.connection.mToken);
1673            } catch (RemoteException e) {
1674            }
1675            wallpaper.connection.mService = null;
1676            wallpaper.connection.mEngine = null;
1677            wallpaper.connection = null;
1678        }
1679    }
1680
1681    void clearWallpaperComponentLocked(WallpaperData wallpaper) {
1682        wallpaper.wallpaperComponent = null;
1683        detachWallpaperLocked(wallpaper);
1684    }
1685
1686    void attachServiceLocked(WallpaperConnection conn, WallpaperData wallpaper) {
1687        try {
1688            conn.mService.attach(conn, conn.mToken,
1689                    WindowManager.LayoutParams.TYPE_WALLPAPER, false,
1690                    wallpaper.width, wallpaper.height, wallpaper.padding);
1691        } catch (RemoteException e) {
1692            Slog.w(TAG, "Failed attaching wallpaper; clearing", e);
1693            if (!wallpaper.wallpaperUpdating) {
1694                bindWallpaperComponentLocked(null, false, false, wallpaper, null);
1695            }
1696        }
1697    }
1698
1699    private void notifyCallbacksLocked(WallpaperData wallpaper) {
1700        final int n = wallpaper.callbacks.beginBroadcast();
1701        for (int i = 0; i < n; i++) {
1702            try {
1703                wallpaper.callbacks.getBroadcastItem(i).onWallpaperChanged();
1704            } catch (RemoteException e) {
1705
1706                // The RemoteCallbackList will take care of removing
1707                // the dead object for us.
1708            }
1709        }
1710        wallpaper.callbacks.finishBroadcast();
1711        final Intent intent = new Intent(Intent.ACTION_WALLPAPER_CHANGED);
1712        mContext.sendBroadcastAsUser(intent, new UserHandle(mCurrentUserId));
1713    }
1714
1715    private void checkPermission(String permission) {
1716        if (PackageManager.PERMISSION_GRANTED!= mContext.checkCallingOrSelfPermission(permission)) {
1717            throw new SecurityException("Access denied to process: " + Binder.getCallingPid()
1718                    + ", must have permission " + permission);
1719        }
1720    }
1721
1722    /**
1723     * Certain user types do not support wallpapers (e.g. managed profiles). The check is
1724     * implemented through through the OP_WRITE_WALLPAPER AppOp.
1725     */
1726    public boolean isWallpaperSupported(String callingPackage) {
1727        return mAppOpsManager.checkOpNoThrow(AppOpsManager.OP_WRITE_WALLPAPER, Binder.getCallingUid(),
1728                callingPackage) == AppOpsManager.MODE_ALLOWED;
1729    }
1730
1731    @Override
1732    public boolean isSetWallpaperAllowed(String callingPackage) {
1733        final PackageManager pm = mContext.getPackageManager();
1734        String[] uidPackages = pm.getPackagesForUid(Binder.getCallingUid());
1735        boolean uidMatchPackage = Arrays.asList(uidPackages).contains(callingPackage);
1736        if (!uidMatchPackage) {
1737            return false;   // callingPackage was faked.
1738        }
1739
1740        final DevicePolicyManager dpm = mContext.getSystemService(DevicePolicyManager.class);
1741        if (dpm.isDeviceOwnerApp(callingPackage) || dpm.isProfileOwnerApp(callingPackage)) {
1742            return true;
1743        }
1744        final UserManager um = (UserManager) mContext.getSystemService(Context.USER_SERVICE);
1745        return !um.hasUserRestriction(UserManager.DISALLOW_SET_WALLPAPER);
1746    }
1747
1748    @Override
1749    public boolean isWallpaperBackupEligible(int userId) {
1750        if (Binder.getCallingUid() != Process.SYSTEM_UID) {
1751            throw new SecurityException("Only the system may call isWallpaperBackupEligible");
1752        }
1753
1754        WallpaperData wallpaper = getWallpaperSafeLocked(userId, FLAG_SYSTEM);
1755        return (wallpaper != null) ? wallpaper.allowBackup : false;
1756    }
1757
1758    private static JournaledFile makeJournaledFile(int userId) {
1759        final String base = new File(getWallpaperDir(userId), WALLPAPER_INFO).getAbsolutePath();
1760        return new JournaledFile(new File(base), new File(base + ".tmp"));
1761    }
1762
1763    private void saveSettingsLocked(int userId) {
1764        JournaledFile journal = makeJournaledFile(userId);
1765        FileOutputStream fstream = null;
1766        BufferedOutputStream stream = null;
1767        try {
1768            XmlSerializer out = new FastXmlSerializer();
1769            fstream = new FileOutputStream(journal.chooseForWrite(), false);
1770            stream = new BufferedOutputStream(fstream);
1771            out.setOutput(stream, StandardCharsets.UTF_8.name());
1772            out.startDocument(null, true);
1773
1774            WallpaperData wallpaper;
1775
1776            wallpaper = mWallpaperMap.get(userId);
1777            if (wallpaper != null) {
1778                writeWallpaperAttributes(out, "wp", wallpaper);
1779            }
1780            wallpaper = mLockWallpaperMap.get(userId);
1781            if (wallpaper != null) {
1782                writeWallpaperAttributes(out, "kwp", wallpaper);
1783            }
1784
1785            out.endDocument();
1786
1787            stream.flush(); // also flushes fstream
1788            FileUtils.sync(fstream);
1789            stream.close(); // also closes fstream
1790            journal.commit();
1791        } catch (IOException e) {
1792            IoUtils.closeQuietly(stream);
1793            journal.rollback();
1794        }
1795    }
1796
1797    private void writeWallpaperAttributes(XmlSerializer out, String tag, WallpaperData wallpaper)
1798            throws IllegalArgumentException, IllegalStateException, IOException {
1799        out.startTag(null, tag);
1800        out.attribute(null, "id", Integer.toString(wallpaper.wallpaperId));
1801        out.attribute(null, "width", Integer.toString(wallpaper.width));
1802        out.attribute(null, "height", Integer.toString(wallpaper.height));
1803
1804        out.attribute(null, "cropLeft", Integer.toString(wallpaper.cropHint.left));
1805        out.attribute(null, "cropTop", Integer.toString(wallpaper.cropHint.top));
1806        out.attribute(null, "cropRight", Integer.toString(wallpaper.cropHint.right));
1807        out.attribute(null, "cropBottom", Integer.toString(wallpaper.cropHint.bottom));
1808
1809        if (wallpaper.padding.left != 0) {
1810            out.attribute(null, "paddingLeft", Integer.toString(wallpaper.padding.left));
1811        }
1812        if (wallpaper.padding.top != 0) {
1813            out.attribute(null, "paddingTop", Integer.toString(wallpaper.padding.top));
1814        }
1815        if (wallpaper.padding.right != 0) {
1816            out.attribute(null, "paddingRight", Integer.toString(wallpaper.padding.right));
1817        }
1818        if (wallpaper.padding.bottom != 0) {
1819            out.attribute(null, "paddingBottom", Integer.toString(wallpaper.padding.bottom));
1820        }
1821
1822        out.attribute(null, "name", wallpaper.name);
1823        if (wallpaper.wallpaperComponent != null
1824                && !wallpaper.wallpaperComponent.equals(mImageWallpaper)) {
1825            out.attribute(null, "component",
1826                    wallpaper.wallpaperComponent.flattenToShortString());
1827        }
1828
1829        if (wallpaper.allowBackup) {
1830            out.attribute(null, "backup", "true");
1831        }
1832
1833        out.endTag(null, tag);
1834    }
1835
1836    private void migrateFromOld() {
1837        File oldWallpaper = new File(WallpaperBackupHelper.WALLPAPER_IMAGE_KEY);
1838        File oldInfo = new File(WallpaperBackupHelper.WALLPAPER_INFO_KEY);
1839        if (oldWallpaper.exists()) {
1840            File newWallpaper = new File(getWallpaperDir(0), WALLPAPER);
1841            oldWallpaper.renameTo(newWallpaper);
1842        }
1843        if (oldInfo.exists()) {
1844            File newInfo = new File(getWallpaperDir(0), WALLPAPER_INFO);
1845            oldInfo.renameTo(newInfo);
1846        }
1847    }
1848
1849    private int getAttributeInt(XmlPullParser parser, String name, int defValue) {
1850        String value = parser.getAttributeValue(null, name);
1851        if (value == null) {
1852            return defValue;
1853        }
1854        return Integer.parseInt(value);
1855    }
1856
1857    /**
1858     * Sometimes it is expected the wallpaper map may not have a user's data.  E.g. This could
1859     * happen during user switch.  The async user switch observer may not have received
1860     * the event yet.  We use this safe method when we don't care about this ordering and just
1861     * want to update the data.  The data is going to be applied when the user switch observer
1862     * is eventually executed.
1863     */
1864    private WallpaperData getWallpaperSafeLocked(int userId, int which) {
1865        // We're setting either just system (work with the system wallpaper),
1866        // both (also work with the system wallpaper), or just the lock
1867        // wallpaper (update against the existing lock wallpaper if any).
1868        // Combined or just-system operations use the 'system' WallpaperData
1869        // for this use; lock-only operations use the dedicated one.
1870        final SparseArray<WallpaperData> whichSet =
1871                (which == FLAG_LOCK) ? mLockWallpaperMap : mWallpaperMap;
1872        WallpaperData wallpaper = whichSet.get(userId);
1873        if (wallpaper == null) {
1874            // common case, this is the first lookup post-boot of the system or
1875            // unified lock, so we bring up the saved state lazily now and recheck.
1876            loadSettingsLocked(userId, false);
1877            wallpaper = whichSet.get(userId);
1878            // if it's still null here, this is a lock-only operation and there is not
1879            // yet a lock-only wallpaper set for this user, so we need to establish
1880            // it now.
1881            if (wallpaper == null) {
1882                if (which == FLAG_LOCK) {
1883                    wallpaper = new WallpaperData(userId,
1884                            WALLPAPER_LOCK_ORIG, WALLPAPER_LOCK_CROP);
1885                    mLockWallpaperMap.put(userId, wallpaper);
1886                    ensureSaneWallpaperData(wallpaper);
1887                } else {
1888                    // sanity fallback: we're in bad shape, but establishing a known
1889                    // valid system+lock WallpaperData will keep us from dying.
1890                    Slog.wtf(TAG, "Didn't find wallpaper in non-lock case!");
1891                    wallpaper = new WallpaperData(userId, WALLPAPER, WALLPAPER_CROP);
1892                    mWallpaperMap.put(userId, wallpaper);
1893                    ensureSaneWallpaperData(wallpaper);
1894                }
1895            }
1896        }
1897        return wallpaper;
1898    }
1899
1900    private void loadSettingsLocked(int userId, boolean keepDimensionHints) {
1901        if (DEBUG) Slog.v(TAG, "loadSettingsLocked");
1902
1903        JournaledFile journal = makeJournaledFile(userId);
1904        FileInputStream stream = null;
1905        File file = journal.chooseForRead();
1906        if (!file.exists()) {
1907            // This should only happen one time, when upgrading from a legacy system
1908            migrateFromOld();
1909        }
1910        WallpaperData wallpaper = mWallpaperMap.get(userId);
1911        if (wallpaper == null) {
1912            wallpaper = new WallpaperData(userId, WALLPAPER, WALLPAPER_CROP);
1913            wallpaper.allowBackup = true;
1914            mWallpaperMap.put(userId, wallpaper);
1915            if (!wallpaper.cropExists()) {
1916                generateCrop(wallpaper);
1917            }
1918        }
1919        boolean success = false;
1920        try {
1921            stream = new FileInputStream(file);
1922            XmlPullParser parser = Xml.newPullParser();
1923            parser.setInput(stream, StandardCharsets.UTF_8.name());
1924
1925            int type;
1926            do {
1927                type = parser.next();
1928                if (type == XmlPullParser.START_TAG) {
1929                    String tag = parser.getName();
1930                    if ("wp".equals(tag)) {
1931                        // Common to system + lock wallpapers
1932                        parseWallpaperAttributes(parser, wallpaper, keepDimensionHints);
1933
1934                        // A system wallpaper might also be a live wallpaper
1935                        String comp = parser.getAttributeValue(null, "component");
1936                        wallpaper.nextWallpaperComponent = comp != null
1937                                ? ComponentName.unflattenFromString(comp)
1938                                : null;
1939                        if (wallpaper.nextWallpaperComponent == null
1940                                || "android".equals(wallpaper.nextWallpaperComponent
1941                                        .getPackageName())) {
1942                            wallpaper.nextWallpaperComponent = mImageWallpaper;
1943                        }
1944
1945                        if (DEBUG) {
1946                            Slog.v(TAG, "mWidth:" + wallpaper.width);
1947                            Slog.v(TAG, "mHeight:" + wallpaper.height);
1948                            Slog.v(TAG, "cropRect:" + wallpaper.cropHint);
1949                            Slog.v(TAG, "mName:" + wallpaper.name);
1950                            Slog.v(TAG, "mNextWallpaperComponent:"
1951                                    + wallpaper.nextWallpaperComponent);
1952                        }
1953                    } else if ("kwp".equals(tag)) {
1954                        // keyguard-specific wallpaper for this user
1955                        WallpaperData lockWallpaper = mLockWallpaperMap.get(userId);
1956                        if (lockWallpaper == null) {
1957                            lockWallpaper = new WallpaperData(userId,
1958                                    WALLPAPER_LOCK_ORIG, WALLPAPER_LOCK_CROP);
1959                            mLockWallpaperMap.put(userId, lockWallpaper);
1960                        }
1961                        parseWallpaperAttributes(parser, lockWallpaper, false);
1962                    }
1963                }
1964            } while (type != XmlPullParser.END_DOCUMENT);
1965            success = true;
1966        } catch (FileNotFoundException e) {
1967            Slog.w(TAG, "no current wallpaper -- first boot?");
1968        } catch (NullPointerException e) {
1969            Slog.w(TAG, "failed parsing " + file + " " + e);
1970        } catch (NumberFormatException e) {
1971            Slog.w(TAG, "failed parsing " + file + " " + e);
1972        } catch (XmlPullParserException e) {
1973            Slog.w(TAG, "failed parsing " + file + " " + e);
1974        } catch (IOException e) {
1975            Slog.w(TAG, "failed parsing " + file + " " + e);
1976        } catch (IndexOutOfBoundsException e) {
1977            Slog.w(TAG, "failed parsing " + file + " " + e);
1978        }
1979        IoUtils.closeQuietly(stream);
1980
1981        if (!success) {
1982            wallpaper.width = -1;
1983            wallpaper.height = -1;
1984            wallpaper.cropHint.set(0, 0, 0, 0);
1985            wallpaper.padding.set(0, 0, 0, 0);
1986            wallpaper.name = "";
1987
1988            mLockWallpaperMap.remove(userId);
1989        } else {
1990            if (wallpaper.wallpaperId <= 0) {
1991                wallpaper.wallpaperId = makeWallpaperIdLocked();
1992                if (DEBUG) {
1993                    Slog.w(TAG, "Didn't set wallpaper id in loadSettingsLocked(" + userId
1994                            + "); now " + wallpaper.wallpaperId);
1995                }
1996            }
1997        }
1998
1999        ensureSaneWallpaperData(wallpaper);
2000        WallpaperData lockWallpaper = mLockWallpaperMap.get(userId);
2001        if (lockWallpaper != null) {
2002            ensureSaneWallpaperData(lockWallpaper);
2003        }
2004    }
2005
2006    private void ensureSaneWallpaperData(WallpaperData wallpaper) {
2007        // We always want to have some reasonable width hint.
2008        int baseSize = getMaximumSizeDimension();
2009        if (wallpaper.width < baseSize) {
2010            wallpaper.width = baseSize;
2011        }
2012        if (wallpaper.height < baseSize) {
2013            wallpaper.height = baseSize;
2014        }
2015        // and crop, if not previously specified
2016        if (wallpaper.cropHint.width() <= 0
2017                || wallpaper.cropHint.height() <= 0) {
2018            wallpaper.cropHint.set(0, 0, wallpaper.width, wallpaper.height);
2019        }
2020    }
2021
2022    private void parseWallpaperAttributes(XmlPullParser parser, WallpaperData wallpaper,
2023            boolean keepDimensionHints) {
2024        final String idString = parser.getAttributeValue(null, "id");
2025        if (idString != null) {
2026            final int id = wallpaper.wallpaperId = Integer.parseInt(idString);
2027            if (id > mWallpaperId) {
2028                mWallpaperId = id;
2029            }
2030        } else {
2031            wallpaper.wallpaperId = makeWallpaperIdLocked();
2032        }
2033
2034        if (!keepDimensionHints) {
2035            wallpaper.width = Integer.parseInt(parser.getAttributeValue(null, "width"));
2036            wallpaper.height = Integer.parseInt(parser
2037                    .getAttributeValue(null, "height"));
2038        }
2039        wallpaper.cropHint.left = getAttributeInt(parser, "cropLeft", 0);
2040        wallpaper.cropHint.top = getAttributeInt(parser, "cropTop", 0);
2041        wallpaper.cropHint.right = getAttributeInt(parser, "cropRight", 0);
2042        wallpaper.cropHint.bottom = getAttributeInt(parser, "cropBottom", 0);
2043        wallpaper.padding.left = getAttributeInt(parser, "paddingLeft", 0);
2044        wallpaper.padding.top = getAttributeInt(parser, "paddingTop", 0);
2045        wallpaper.padding.right = getAttributeInt(parser, "paddingRight", 0);
2046        wallpaper.padding.bottom = getAttributeInt(parser, "paddingBottom", 0);
2047        wallpaper.name = parser.getAttributeValue(null, "name");
2048        wallpaper.allowBackup = "true".equals(parser.getAttributeValue(null, "backup"));
2049    }
2050
2051    private int getMaximumSizeDimension() {
2052        WindowManager wm = (WindowManager)mContext.getSystemService(Context.WINDOW_SERVICE);
2053        Display d = wm.getDefaultDisplay();
2054        return d.getMaximumSizeDimension();
2055    }
2056
2057    // Called by SystemBackupAgent after files are restored to disk.
2058    public void settingsRestored() {
2059        // Verify caller is the system
2060        if (Binder.getCallingUid() != android.os.Process.SYSTEM_UID) {
2061            throw new RuntimeException("settingsRestored() can only be called from the system process");
2062        }
2063        // TODO: If necessary, make it work for secondary users as well. This currently assumes
2064        // restores only to the primary user
2065        if (DEBUG) Slog.v(TAG, "settingsRestored");
2066        WallpaperData wallpaper = null;
2067        boolean success = false;
2068        synchronized (mLock) {
2069            loadSettingsLocked(UserHandle.USER_SYSTEM, false);
2070            wallpaper = mWallpaperMap.get(UserHandle.USER_SYSTEM);
2071            wallpaper.wallpaperId = makeWallpaperIdLocked();    // always bump id at restore
2072            wallpaper.allowBackup = true;   // by definition if it was restored
2073            if (wallpaper.nextWallpaperComponent != null
2074                    && !wallpaper.nextWallpaperComponent.equals(mImageWallpaper)) {
2075                if (!bindWallpaperComponentLocked(wallpaper.nextWallpaperComponent, false, false,
2076                        wallpaper, null)) {
2077                    // No such live wallpaper or other failure; fall back to the default
2078                    // live wallpaper (since the profile being restored indicated that the
2079                    // user had selected a live rather than static one).
2080                    bindWallpaperComponentLocked(null, false, false, wallpaper, null);
2081                }
2082                success = true;
2083            } else {
2084                // If there's a wallpaper name, we use that.  If that can't be loaded, then we
2085                // use the default.
2086                if ("".equals(wallpaper.name)) {
2087                    if (DEBUG) Slog.v(TAG, "settingsRestored: name is empty");
2088                    success = true;
2089                } else {
2090                    if (DEBUG) Slog.v(TAG, "settingsRestored: attempting to restore named resource");
2091                    success = restoreNamedResourceLocked(wallpaper);
2092                }
2093                if (DEBUG) Slog.v(TAG, "settingsRestored: success=" + success
2094                        + " id=" + wallpaper.wallpaperId);
2095                if (success) {
2096                    generateCrop(wallpaper);    // based on the new image + metadata
2097                    bindWallpaperComponentLocked(wallpaper.nextWallpaperComponent, true, false,
2098                            wallpaper, null);
2099                }
2100            }
2101        }
2102
2103        if (!success) {
2104            Slog.e(TAG, "Failed to restore wallpaper: '" + wallpaper.name + "'");
2105            wallpaper.name = "";
2106            getWallpaperDir(UserHandle.USER_SYSTEM).delete();
2107        }
2108
2109        synchronized (mLock) {
2110            saveSettingsLocked(UserHandle.USER_SYSTEM);
2111        }
2112    }
2113
2114    // Restore the named resource bitmap to both source + crop files
2115    boolean restoreNamedResourceLocked(WallpaperData wallpaper) {
2116        if (wallpaper.name.length() > 4 && "res:".equals(wallpaper.name.substring(0, 4))) {
2117            String resName = wallpaper.name.substring(4);
2118
2119            String pkg = null;
2120            int colon = resName.indexOf(':');
2121            if (colon > 0) {
2122                pkg = resName.substring(0, colon);
2123            }
2124
2125            String ident = null;
2126            int slash = resName.lastIndexOf('/');
2127            if (slash > 0) {
2128                ident = resName.substring(slash+1);
2129            }
2130
2131            String type = null;
2132            if (colon > 0 && slash > 0 && (slash-colon) > 1) {
2133                type = resName.substring(colon+1, slash);
2134            }
2135
2136            if (pkg != null && ident != null && type != null) {
2137                int resId = -1;
2138                InputStream res = null;
2139                FileOutputStream fos = null;
2140                FileOutputStream cos = null;
2141                try {
2142                    Context c = mContext.createPackageContext(pkg, Context.CONTEXT_RESTRICTED);
2143                    Resources r = c.getResources();
2144                    resId = r.getIdentifier(resName, null, null);
2145                    if (resId == 0) {
2146                        Slog.e(TAG, "couldn't resolve identifier pkg=" + pkg + " type=" + type
2147                                + " ident=" + ident);
2148                        return false;
2149                    }
2150
2151                    res = r.openRawResource(resId);
2152                    if (wallpaper.wallpaperFile.exists()) {
2153                        wallpaper.wallpaperFile.delete();
2154                        wallpaper.cropFile.delete();
2155                    }
2156                    fos = new FileOutputStream(wallpaper.wallpaperFile);
2157                    cos = new FileOutputStream(wallpaper.cropFile);
2158
2159                    byte[] buffer = new byte[32768];
2160                    int amt;
2161                    while ((amt=res.read(buffer)) > 0) {
2162                        fos.write(buffer, 0, amt);
2163                        cos.write(buffer, 0, amt);
2164                    }
2165                    // mWallpaperObserver will notice the close and send the change broadcast
2166
2167                    Slog.v(TAG, "Restored wallpaper: " + resName);
2168                    return true;
2169                } catch (NameNotFoundException e) {
2170                    Slog.e(TAG, "Package name " + pkg + " not found");
2171                } catch (Resources.NotFoundException e) {
2172                    Slog.e(TAG, "Resource not found: " + resId);
2173                } catch (IOException e) {
2174                    Slog.e(TAG, "IOException while restoring wallpaper ", e);
2175                } finally {
2176                    IoUtils.closeQuietly(res);
2177                    if (fos != null) {
2178                        FileUtils.sync(fos);
2179                    }
2180                    if (cos != null) {
2181                        FileUtils.sync(cos);
2182                    }
2183                    IoUtils.closeQuietly(fos);
2184                    IoUtils.closeQuietly(cos);
2185                }
2186            }
2187        }
2188        return false;
2189    }
2190
2191    @Override
2192    protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
2193        if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DUMP)
2194                != PackageManager.PERMISSION_GRANTED) {
2195
2196            pw.println("Permission Denial: can't dump wallpaper service from from pid="
2197                    + Binder.getCallingPid()
2198                    + ", uid=" + Binder.getCallingUid());
2199            return;
2200        }
2201
2202        synchronized (mLock) {
2203            pw.println("System wallpaper state:");
2204            for (int i = 0; i < mWallpaperMap.size(); i++) {
2205                WallpaperData wallpaper = mWallpaperMap.valueAt(i);
2206                pw.print(" User "); pw.print(wallpaper.userId);
2207                    pw.print(": id="); pw.println(wallpaper.wallpaperId);
2208                pw.print("  mWidth=");
2209                    pw.print(wallpaper.width);
2210                    pw.print(" mHeight=");
2211                    pw.println(wallpaper.height);
2212                pw.print("  mCropHint="); pw.println(wallpaper.cropHint);
2213                pw.print("  mPadding="); pw.println(wallpaper.padding);
2214                pw.print("  mName=");  pw.println(wallpaper.name);
2215                pw.print("  mWallpaperComponent="); pw.println(wallpaper.wallpaperComponent);
2216                if (wallpaper.connection != null) {
2217                    WallpaperConnection conn = wallpaper.connection;
2218                    pw.print("  Wallpaper connection ");
2219                    pw.print(conn);
2220                    pw.println(":");
2221                    if (conn.mInfo != null) {
2222                        pw.print("    mInfo.component=");
2223                        pw.println(conn.mInfo.getComponent());
2224                    }
2225                    pw.print("    mToken=");
2226                    pw.println(conn.mToken);
2227                    pw.print("    mService=");
2228                    pw.println(conn.mService);
2229                    pw.print("    mEngine=");
2230                    pw.println(conn.mEngine);
2231                    pw.print("    mLastDiedTime=");
2232                    pw.println(wallpaper.lastDiedTime - SystemClock.uptimeMillis());
2233                }
2234            }
2235            pw.println("Lock wallpaper state:");
2236            for (int i = 0; i < mLockWallpaperMap.size(); i++) {
2237                WallpaperData wallpaper = mLockWallpaperMap.valueAt(i);
2238                pw.print(" User "); pw.print(wallpaper.userId);
2239                    pw.print(": id="); pw.println(wallpaper.wallpaperId);
2240                pw.print("  mWidth="); pw.print(wallpaper.width);
2241                    pw.print(" mHeight="); pw.println(wallpaper.height);
2242                pw.print("  mCropHint="); pw.println(wallpaper.cropHint);
2243                pw.print("  mPadding="); pw.println(wallpaper.padding);
2244                pw.print("  mName=");  pw.println(wallpaper.name);
2245            }
2246
2247        }
2248    }
2249}
2250