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