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