WallpaperManager.java revision a457211af55b2ad5bc58f4228ceb147ac22c92b0
1/*
2 * Copyright (C) 2009 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 android.app;
18
19import android.content.ContentResolver;
20import android.content.Context;
21import android.content.Intent;
22import android.content.pm.PackageManager;
23import android.content.pm.ResolveInfo;
24import android.content.res.Resources;
25import android.graphics.Bitmap;
26import android.graphics.BitmapFactory;
27import android.graphics.BitmapRegionDecoder;
28import android.graphics.Canvas;
29import android.graphics.ColorFilter;
30import android.graphics.Matrix;
31import android.graphics.Paint;
32import android.graphics.PixelFormat;
33import android.graphics.PorterDuff;
34import android.graphics.PorterDuffXfermode;
35import android.graphics.Rect;
36import android.graphics.RectF;
37import android.graphics.drawable.BitmapDrawable;
38import android.graphics.drawable.Drawable;
39import android.net.Uri;
40import android.os.Bundle;
41import android.os.Handler;
42import android.os.IBinder;
43import android.os.Looper;
44import android.os.Message;
45import android.os.ParcelFileDescriptor;
46import android.os.RemoteException;
47import android.os.ServiceManager;
48import android.util.DisplayMetrics;
49import android.util.Log;
50import android.view.WindowManager;
51import android.view.WindowManagerGlobal;
52
53import java.io.BufferedInputStream;
54import java.io.FileOutputStream;
55import java.io.IOException;
56import java.io.InputStream;
57import java.util.List;
58
59/**
60 * Provides access to the system wallpaper. With WallpaperManager, you can
61 * get the current wallpaper, get the desired dimensions for the wallpaper, set
62 * the wallpaper, and more. Get an instance of WallpaperManager with
63 * {@link #getInstance(android.content.Context) getInstance()}.
64 */
65public class WallpaperManager {
66    private static String TAG = "WallpaperManager";
67    private static boolean DEBUG = false;
68    private float mWallpaperXStep = -1;
69    private float mWallpaperYStep = -1;
70
71    /**
72     * Activity Action: Show settings for choosing wallpaper. Do not use directly to construct
73     * an intent; instead, use {@link #getCropAndSetWallpaperIntent}.
74     * <p>Input:  {@link Intent#getData} is the URI of the image to crop and set as wallpaper.
75     * <p>Output: RESULT_OK if user decided to crop/set the wallpaper, RESULT_CANCEL otherwise
76     * Activities that support this intent should specify a MIME filter of "image/*"
77     */
78    public static final String ACTION_CROP_AND_SET_WALLPAPER =
79            "android.service.wallpaper.CROP_AND_SET_WALLPAPER";
80
81    /**
82     * Launch an activity for the user to pick the current global live
83     * wallpaper.
84     */
85    public static final String ACTION_LIVE_WALLPAPER_CHOOSER
86            = "android.service.wallpaper.LIVE_WALLPAPER_CHOOSER";
87
88    /**
89     * Directly launch live wallpaper preview, allowing the user to immediately
90     * confirm to switch to a specific live wallpaper.  You must specify
91     * {@link #EXTRA_LIVE_WALLPAPER_COMPONENT} with the ComponentName of
92     * a live wallpaper component that is to be shown.
93     */
94    public static final String ACTION_CHANGE_LIVE_WALLPAPER
95            = "android.service.wallpaper.CHANGE_LIVE_WALLPAPER";
96
97    /**
98     * Extra in {@link #ACTION_CHANGE_LIVE_WALLPAPER} that specifies the
99     * ComponentName of a live wallpaper that should be shown as a preview,
100     * for the user to confirm.
101     */
102    public static final String EXTRA_LIVE_WALLPAPER_COMPONENT
103            = "android.service.wallpaper.extra.LIVE_WALLPAPER_COMPONENT";
104
105    /**
106     * Manifest entry for activities that respond to {@link Intent#ACTION_SET_WALLPAPER}
107     * which allows them to provide a custom large icon associated with this action.
108     */
109    public static final String WALLPAPER_PREVIEW_META_DATA = "android.wallpaper.preview";
110
111    /**
112     * Command for {@link #sendWallpaperCommand}: reported by the wallpaper
113     * host when the user taps on an empty area (not performing an action
114     * in the host).  The x and y arguments are the location of the tap in
115     * screen coordinates.
116     */
117    public static final String COMMAND_TAP = "android.wallpaper.tap";
118
119    /**
120     * Command for {@link #sendWallpaperCommand}: reported by the wallpaper
121     * host when the user releases a secondary pointer on an empty area
122     * (not performing an action in the host).  The x and y arguments are
123     * the location of the secondary tap in screen coordinates.
124     */
125    public static final String COMMAND_SECONDARY_TAP = "android.wallpaper.secondaryTap";
126
127    /**
128     * Command for {@link #sendWallpaperCommand}: reported by the wallpaper
129     * host when the user drops an object into an area of the host.  The x
130     * and y arguments are the location of the drop.
131     */
132    public static final String COMMAND_DROP = "android.home.drop";
133
134    private final Context mContext;
135
136    /**
137     * Special drawable that draws a wallpaper as fast as possible.  Assumes
138     * no scaling or placement off (0,0) of the wallpaper (this should be done
139     * at the time the bitmap is loaded).
140     */
141    static class FastBitmapDrawable extends Drawable {
142        private final Bitmap mBitmap;
143        private final int mWidth;
144        private final int mHeight;
145        private int mDrawLeft;
146        private int mDrawTop;
147        private final Paint mPaint;
148
149        private FastBitmapDrawable(Bitmap bitmap) {
150            mBitmap = bitmap;
151            mWidth = bitmap.getWidth();
152            mHeight = bitmap.getHeight();
153
154            setBounds(0, 0, mWidth, mHeight);
155
156            mPaint = new Paint();
157            mPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC));
158        }
159
160        @Override
161        public void draw(Canvas canvas) {
162            canvas.drawBitmap(mBitmap, mDrawLeft, mDrawTop, mPaint);
163        }
164
165        @Override
166        public int getOpacity() {
167            return PixelFormat.OPAQUE;
168        }
169
170        @Override
171        public void setBounds(int left, int top, int right, int bottom) {
172            mDrawLeft = left + (right-left - mWidth) / 2;
173            mDrawTop = top + (bottom-top - mHeight) / 2;
174        }
175
176        @Override
177        public void setAlpha(int alpha) {
178            throw new UnsupportedOperationException("Not supported with this drawable");
179        }
180
181        @Override
182        public void setColorFilter(ColorFilter cf) {
183            throw new UnsupportedOperationException("Not supported with this drawable");
184        }
185
186        @Override
187        public void setDither(boolean dither) {
188            throw new UnsupportedOperationException("Not supported with this drawable");
189        }
190
191        @Override
192        public void setFilterBitmap(boolean filter) {
193            throw new UnsupportedOperationException("Not supported with this drawable");
194        }
195
196        @Override
197        public int getIntrinsicWidth() {
198            return mWidth;
199        }
200
201        @Override
202        public int getIntrinsicHeight() {
203            return mHeight;
204        }
205
206        @Override
207        public int getMinimumWidth() {
208            return mWidth;
209        }
210
211        @Override
212        public int getMinimumHeight() {
213            return mHeight;
214        }
215    }
216
217    static class Globals extends IWallpaperManagerCallback.Stub {
218        private IWallpaperManager mService;
219        private Bitmap mWallpaper;
220        private Bitmap mDefaultWallpaper;
221
222        private static final int MSG_CLEAR_WALLPAPER = 1;
223
224        private final Handler mHandler;
225
226        Globals(Looper looper) {
227            IBinder b = ServiceManager.getService(Context.WALLPAPER_SERVICE);
228            mService = IWallpaperManager.Stub.asInterface(b);
229            mHandler = new Handler(looper) {
230                @Override
231                public void handleMessage(Message msg) {
232                    switch (msg.what) {
233                        case MSG_CLEAR_WALLPAPER:
234                            synchronized (this) {
235                                mWallpaper = null;
236                                mDefaultWallpaper = null;
237                            }
238                            break;
239                    }
240                }
241            };
242        }
243
244        public void onWallpaperChanged() {
245            /* The wallpaper has changed but we shouldn't eagerly load the
246             * wallpaper as that would be inefficient. Reset the cached wallpaper
247             * to null so if the user requests the wallpaper again then we'll
248             * fetch it.
249             */
250            mHandler.sendEmptyMessage(MSG_CLEAR_WALLPAPER);
251        }
252
253        public Bitmap peekWallpaperBitmap(Context context, boolean returnDefault) {
254            synchronized (this) {
255                if (mWallpaper != null) {
256                    return mWallpaper;
257                }
258                if (mDefaultWallpaper != null) {
259                    return mDefaultWallpaper;
260                }
261                mWallpaper = null;
262                try {
263                    mWallpaper = getCurrentWallpaperLocked(context);
264                } catch (OutOfMemoryError e) {
265                    Log.w(TAG, "No memory load current wallpaper", e);
266                }
267                if (returnDefault) {
268                    if (mWallpaper == null) {
269                        mDefaultWallpaper = getDefaultWallpaperLocked(context);
270                        return mDefaultWallpaper;
271                    } else {
272                        mDefaultWallpaper = null;
273                    }
274                }
275                return mWallpaper;
276            }
277        }
278
279        public void forgetLoadedWallpaper() {
280            synchronized (this) {
281                mWallpaper = null;
282                mDefaultWallpaper = null;
283            }
284        }
285
286        private Bitmap getCurrentWallpaperLocked(Context context) {
287            try {
288                Bundle params = new Bundle();
289                ParcelFileDescriptor fd = mService.getWallpaper(this, params);
290                if (fd != null) {
291                    int width = params.getInt("width", 0);
292                    int height = params.getInt("height", 0);
293
294                    try {
295                        BitmapFactory.Options options = new BitmapFactory.Options();
296                        Bitmap bm = BitmapFactory.decodeFileDescriptor(
297                                fd.getFileDescriptor(), null, options);
298                        return generateBitmap(context, bm, width, height);
299                    } catch (OutOfMemoryError e) {
300                        Log.w(TAG, "Can't decode file", e);
301                    } finally {
302                        try {
303                            fd.close();
304                        } catch (IOException e) {
305                            // Ignore
306                        }
307                    }
308                }
309            } catch (RemoteException e) {
310                // Ignore
311            }
312            return null;
313        }
314
315        private Bitmap getDefaultWallpaperLocked(Context context) {
316            try {
317                InputStream is = context.getResources().openRawResource(
318                        com.android.internal.R.drawable.default_wallpaper);
319                if (is != null) {
320                    int width = mService.getWidthHint();
321                    int height = mService.getHeightHint();
322
323                    try {
324                        BitmapFactory.Options options = new BitmapFactory.Options();
325                        Bitmap bm = BitmapFactory.decodeStream(is, null, options);
326                        return generateBitmap(context, bm, width, height);
327                    } catch (OutOfMemoryError e) {
328                        Log.w(TAG, "Can't decode stream", e);
329                    } finally {
330                        try {
331                            is.close();
332                        } catch (IOException e) {
333                            // Ignore
334                        }
335                    }
336                }
337            } catch (RemoteException e) {
338                // Ignore
339            }
340            return null;
341        }
342    }
343
344    private static final Object sSync = new Object[0];
345    private static Globals sGlobals;
346
347    static void initGlobals(Looper looper) {
348        synchronized (sSync) {
349            if (sGlobals == null) {
350                sGlobals = new Globals(looper);
351            }
352        }
353    }
354
355    /*package*/ WallpaperManager(Context context, Handler handler) {
356        mContext = context;
357        initGlobals(context.getMainLooper());
358    }
359
360    /**
361     * Retrieve a WallpaperManager associated with the given Context.
362     */
363    public static WallpaperManager getInstance(Context context) {
364        return (WallpaperManager)context.getSystemService(
365                Context.WALLPAPER_SERVICE);
366    }
367
368    /** @hide */
369    public IWallpaperManager getIWallpaperManager() {
370        return sGlobals.mService;
371    }
372
373    /**
374     * Retrieve the current system wallpaper; if
375     * no wallpaper is set, the system built-in static wallpaper is returned.
376     * This is returned as an
377     * abstract Drawable that you can install in a View to display whatever
378     * wallpaper the user has currently set.
379     *
380     * @return Returns a Drawable object that will draw the wallpaper.
381     */
382    public Drawable getDrawable() {
383        Bitmap bm = sGlobals.peekWallpaperBitmap(mContext, true);
384        if (bm != null) {
385            Drawable dr = new BitmapDrawable(mContext.getResources(), bm);
386            dr.setDither(false);
387            return dr;
388        }
389        return null;
390    }
391
392    /**
393     * Returns a drawable for the system built-in static wallpaper .
394     *
395     */
396    public Drawable getBuiltInDrawable() {
397        return getBuiltInDrawable(0, 0, false, 0, 0);
398    }
399
400    /**
401     * Returns a drawable for the system built-in static wallpaper. Based on the parameters, the
402     * drawable can be cropped and scaled
403     *
404     * @param outWidth The width of the returned drawable
405     * @param outWidth The height of the returned drawable
406     * @param scaleToFit If true, scale the wallpaper down rather than just cropping it
407     * @param horizontalAlignment A float value between 0 and 1 specifying where to crop the image;
408     *        0 for left-aligned, 0.5 for horizontal center-aligned, and 1 for right-aligned
409     * @param verticalAlignment A float value between 0 and 1 specifying where to crop the image;
410     *        0 for top-aligned, 0.5 for vertical center-aligned, and 1 for bottom-aligned
411     *
412     */
413    public Drawable getBuiltInDrawable(int outWidth, int outHeight,
414            boolean scaleToFit, float horizontalAlignment, float verticalAlignment) {
415        if (sGlobals.mService == null) {
416            Log.w(TAG, "WallpaperService not running");
417            return null;
418        }
419        Resources resources = mContext.getResources();
420        horizontalAlignment = Math.max(0, Math.min(1, horizontalAlignment));
421        verticalAlignment = Math.max(0, Math.min(1, verticalAlignment));
422
423        InputStream is = new BufferedInputStream(
424                resources.openRawResource(com.android.internal.R.drawable.default_wallpaper));
425
426        if (is == null) {
427            Log.e(TAG, "default wallpaper input stream is null");
428            return null;
429        } else {
430            if (outWidth <= 0 || outHeight <= 0) {
431                Bitmap fullSize = BitmapFactory.decodeStream(is, null, null);
432                return new BitmapDrawable(resources, fullSize);
433            } else {
434                int inWidth;
435                int inHeight;
436                {
437                    BitmapFactory.Options options = new BitmapFactory.Options();
438                    options.inJustDecodeBounds = true;
439                    BitmapFactory.decodeStream(is, null, options);
440                    if (options.outWidth != 0 && options.outHeight != 0) {
441                        inWidth = options.outWidth;
442                        inHeight = options.outHeight;
443                    } else {
444                        Log.e(TAG, "default wallpaper dimensions are 0");
445                        return null;
446                    }
447                }
448
449                is = new BufferedInputStream(resources.openRawResource(
450                        com.android.internal.R.drawable.default_wallpaper));
451
452                RectF cropRectF;
453
454                outWidth = Math.min(inWidth, outWidth);
455                outHeight = Math.min(inHeight, outHeight);
456                if (scaleToFit) {
457                    cropRectF = getMaxCropRect(inWidth, inHeight, outWidth, outHeight,
458                        horizontalAlignment, verticalAlignment);
459                } else {
460                    float left = (inWidth - outWidth) * horizontalAlignment;
461                    float right = left + outWidth;
462                    float top = (inHeight - outHeight) * verticalAlignment;
463                    float bottom = top + outHeight;
464                    cropRectF = new RectF(left, top, right, bottom);
465                }
466                Rect roundedTrueCrop = new Rect();
467                cropRectF.roundOut(roundedTrueCrop);
468
469                if (roundedTrueCrop.width() <= 0 || roundedTrueCrop.height() <= 0) {
470                    Log.w(TAG, "crop has bad values for full size image");
471                    return null;
472                }
473
474                // See how much we're reducing the size of the image
475                int scaleDownSampleSize = Math.min(roundedTrueCrop.width() / outWidth,
476                        roundedTrueCrop.height() / outHeight);
477
478                // Attempt to open a region decoder
479                BitmapRegionDecoder decoder = null;
480                try {
481                    decoder = BitmapRegionDecoder.newInstance(is, true);
482                } catch (IOException e) {
483                    Log.w(TAG, "cannot open region decoder for default wallpaper");
484                }
485
486                Bitmap crop = null;
487                if (decoder != null) {
488                    // Do region decoding to get crop bitmap
489                    BitmapFactory.Options options = new BitmapFactory.Options();
490                    if (scaleDownSampleSize > 1) {
491                        options.inSampleSize = scaleDownSampleSize;
492                    }
493                    crop = decoder.decodeRegion(roundedTrueCrop, options);
494                    decoder.recycle();
495                }
496
497                if (crop == null) {
498                    // BitmapRegionDecoder has failed, try to crop in-memory
499                    is = new BufferedInputStream(resources.openRawResource(
500                            com.android.internal.R.drawable.default_wallpaper));
501                    Bitmap fullSize = null;
502                    if (is != null) {
503                        BitmapFactory.Options options = new BitmapFactory.Options();
504                        if (scaleDownSampleSize > 1) {
505                            options.inSampleSize = scaleDownSampleSize;
506                        }
507                        fullSize = BitmapFactory.decodeStream(is, null, options);
508                    }
509                    if (fullSize != null) {
510                        crop = Bitmap.createBitmap(fullSize, roundedTrueCrop.left,
511                                roundedTrueCrop.top, roundedTrueCrop.width(),
512                                roundedTrueCrop.height());
513                    }
514                }
515
516                if (crop == null) {
517                    Log.w(TAG, "cannot decode default wallpaper");
518                    return null;
519                }
520
521                // Scale down if necessary
522                if (outWidth > 0 && outHeight > 0 &&
523                        (crop.getWidth() != outWidth || crop.getHeight() != outHeight)) {
524                    Matrix m = new Matrix();
525                    RectF cropRect = new RectF(0, 0, crop.getWidth(), crop.getHeight());
526                    RectF returnRect = new RectF(0, 0, outWidth, outHeight);
527                    m.setRectToRect(cropRect, returnRect, Matrix.ScaleToFit.FILL);
528                    Bitmap tmp = Bitmap.createBitmap((int) returnRect.width(),
529                            (int) returnRect.height(), Bitmap.Config.ARGB_8888);
530                    if (tmp != null) {
531                        Canvas c = new Canvas(tmp);
532                        Paint p = new Paint();
533                        p.setFilterBitmap(true);
534                        c.drawBitmap(crop, m, p);
535                        crop = tmp;
536                    }
537                }
538
539                return new BitmapDrawable(resources, crop);
540            }
541        }
542    }
543
544    private static RectF getMaxCropRect(int inWidth, int inHeight, int outWidth, int outHeight,
545                float horizontalAlignment, float verticalAlignment) {
546        RectF cropRect = new RectF();
547        // Get a crop rect that will fit this
548        if (inWidth / (float) inHeight > outWidth / (float) outHeight) {
549             cropRect.top = 0;
550             cropRect.bottom = inHeight;
551             float cropWidth = outWidth * (inHeight / (float) outHeight);
552             cropRect.left = (inWidth - cropWidth) * horizontalAlignment;
553             cropRect.right = cropRect.left + cropWidth;
554        } else {
555            cropRect.left = 0;
556            cropRect.right = inWidth;
557            float cropHeight = outHeight * (inWidth / (float) outWidth);
558            cropRect.top = (inHeight - cropHeight) * verticalAlignment;
559            cropRect.bottom = cropRect.top + cropHeight;
560        }
561        return cropRect;
562    }
563
564    /**
565     * Retrieve the current system wallpaper; if there is no wallpaper set,
566     * a null pointer is returned. This is returned as an
567     * abstract Drawable that you can install in a View to display whatever
568     * wallpaper the user has currently set.
569     *
570     * @return Returns a Drawable object that will draw the wallpaper or a
571     * null pointer if these is none.
572     */
573    public Drawable peekDrawable() {
574        Bitmap bm = sGlobals.peekWallpaperBitmap(mContext, false);
575        if (bm != null) {
576            Drawable dr = new BitmapDrawable(mContext.getResources(), bm);
577            dr.setDither(false);
578            return dr;
579        }
580        return null;
581    }
582
583    /**
584     * Like {@link #getDrawable()}, but the returned Drawable has a number
585     * of limitations to reduce its overhead as much as possible. It will
586     * never scale the wallpaper (only centering it if the requested bounds
587     * do match the bitmap bounds, which should not be typical), doesn't
588     * allow setting an alpha, color filter, or other attributes, etc.  The
589     * bounds of the returned drawable will be initialized to the same bounds
590     * as the wallpaper, so normally you will not need to touch it.  The
591     * drawable also assumes that it will be used in a context running in
592     * the same density as the screen (not in density compatibility mode).
593     *
594     * @return Returns a Drawable object that will draw the wallpaper.
595     */
596    public Drawable getFastDrawable() {
597        Bitmap bm = sGlobals.peekWallpaperBitmap(mContext, true);
598        if (bm != null) {
599            return new FastBitmapDrawable(bm);
600        }
601        return null;
602    }
603
604    /**
605     * Like {@link #getFastDrawable()}, but if there is no wallpaper set,
606     * a null pointer is returned.
607     *
608     * @return Returns an optimized Drawable object that will draw the
609     * wallpaper or a null pointer if these is none.
610     */
611    public Drawable peekFastDrawable() {
612        Bitmap bm = sGlobals.peekWallpaperBitmap(mContext, false);
613        if (bm != null) {
614            return new FastBitmapDrawable(bm);
615        }
616        return null;
617    }
618
619    /**
620     * Like {@link #getDrawable()} but returns a Bitmap.
621     *
622     * @hide
623     */
624    public Bitmap getBitmap() {
625        return sGlobals.peekWallpaperBitmap(mContext, true);
626    }
627
628    /**
629     * Remove all internal references to the last loaded wallpaper.  Useful
630     * for apps that want to reduce memory usage when they only temporarily
631     * need to have the wallpaper.  After calling, the next request for the
632     * wallpaper will require reloading it again from disk.
633     */
634    public void forgetLoadedWallpaper() {
635        sGlobals.forgetLoadedWallpaper();
636    }
637
638    /**
639     * If the current wallpaper is a live wallpaper component, return the
640     * information about that wallpaper.  Otherwise, if it is a static image,
641     * simply return null.
642     */
643    public WallpaperInfo getWallpaperInfo() {
644        try {
645            if (sGlobals.mService == null) {
646                Log.w(TAG, "WallpaperService not running");
647                return null;
648            } else {
649                return sGlobals.mService.getWallpaperInfo();
650            }
651        } catch (RemoteException e) {
652            return null;
653        }
654    }
655
656    /**
657     * Gets an Intent that will launch an activity that crops the given
658     * image and sets the device's wallpaper. If there is a default HOME activity
659     * that supports cropping wallpapers, it will be preferred as the default.
660     * Use this method instead of directly creating a {@link #ACTION_CROP_AND_SET_WALLPAPER}
661     * intent.
662     *
663     * @param imageUri The image URI that will be set in the intent. The must be a content
664     *                 URI and its provider must resolve its type to "image/*"
665     *
666     * @throws IllegalArgumentException if the URI is not a content URI or its MIME type is
667     *         not "image/*"
668     */
669    public Intent getCropAndSetWallpaperIntent(Uri imageUri) {
670        if (!ContentResolver.SCHEME_CONTENT.equals(imageUri.getScheme())) {
671            throw new IllegalArgumentException("Image URI must be of the "
672                    + ContentResolver.SCHEME_CONTENT + " scheme type");
673        }
674
675        final PackageManager packageManager = mContext.getPackageManager();
676        Intent cropAndSetWallpaperIntent =
677                new Intent(ACTION_CROP_AND_SET_WALLPAPER, imageUri);
678        cropAndSetWallpaperIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
679
680        // Find out if the default HOME activity supports CROP_AND_SET_WALLPAPER
681        Intent homeIntent = new Intent(Intent.ACTION_MAIN).addCategory(Intent.CATEGORY_HOME);
682        ResolveInfo resolvedHome = packageManager.resolveActivity(homeIntent,
683                PackageManager.MATCH_DEFAULT_ONLY);
684        if (resolvedHome != null) {
685            cropAndSetWallpaperIntent.setPackage(resolvedHome.activityInfo.packageName);
686
687            List<ResolveInfo> cropAppList = packageManager.queryIntentActivities(
688                    cropAndSetWallpaperIntent, 0);
689            if (cropAppList.size() > 0) {
690                return cropAndSetWallpaperIntent;
691            }
692        }
693
694        // fallback crop activity
695        cropAndSetWallpaperIntent.setPackage("com.android.wallpapercropper");
696        List<ResolveInfo> cropAppList = packageManager.queryIntentActivities(
697                cropAndSetWallpaperIntent, 0);
698        if (cropAppList.size() > 0) {
699            return cropAndSetWallpaperIntent;
700        }
701        // If the URI is not of the right type, or for some reason the system wallpaper
702        // cropper doesn't exist, return null
703        throw new IllegalArgumentException("Cannot use passed URI to set wallpaper; " +
704            "check that the type returned by ContentProvider matches image/*");
705    }
706
707    /**
708     * Change the current system wallpaper to the bitmap in the given resource.
709     * The resource is opened as a raw data stream and copied into the
710     * wallpaper; it must be a valid PNG or JPEG image.  On success, the intent
711     * {@link Intent#ACTION_WALLPAPER_CHANGED} is broadcast.
712     *
713     * <p>This method requires the caller to hold the permission
714     * {@link android.Manifest.permission#SET_WALLPAPER}.
715     *
716     * @param resid The bitmap to save.
717     *
718     * @throws IOException If an error occurs reverting to the built-in
719     * wallpaper.
720     */
721    public void setResource(int resid) throws IOException {
722        if (sGlobals.mService == null) {
723            Log.w(TAG, "WallpaperService not running");
724            return;
725        }
726        try {
727            Resources resources = mContext.getResources();
728            /* Set the wallpaper to the default values */
729            ParcelFileDescriptor fd = sGlobals.mService.setWallpaper(
730                    "res:" + resources.getResourceName(resid));
731            if (fd != null) {
732                FileOutputStream fos = null;
733                try {
734                    fos = new ParcelFileDescriptor.AutoCloseOutputStream(fd);
735                    setWallpaper(resources.openRawResource(resid), fos);
736                } finally {
737                    if (fos != null) {
738                        fos.close();
739                    }
740                }
741            }
742        } catch (RemoteException e) {
743            // Ignore
744        }
745    }
746
747    /**
748     * Change the current system wallpaper to a bitmap.  The given bitmap is
749     * converted to a PNG and stored as the wallpaper.  On success, the intent
750     * {@link Intent#ACTION_WALLPAPER_CHANGED} is broadcast.
751     *
752     * <p>This method requires the caller to hold the permission
753     * {@link android.Manifest.permission#SET_WALLPAPER}.
754     *
755     * @param bitmap The bitmap to save.
756     *
757     * @throws IOException If an error occurs reverting to the built-in
758     * wallpaper.
759     */
760    public void setBitmap(Bitmap bitmap) throws IOException {
761        if (sGlobals.mService == null) {
762            Log.w(TAG, "WallpaperService not running");
763            return;
764        }
765        try {
766            ParcelFileDescriptor fd = sGlobals.mService.setWallpaper(null);
767            if (fd == null) {
768                return;
769            }
770            FileOutputStream fos = null;
771            try {
772                fos = new ParcelFileDescriptor.AutoCloseOutputStream(fd);
773                bitmap.compress(Bitmap.CompressFormat.PNG, 90, fos);
774            } finally {
775                if (fos != null) {
776                    fos.close();
777                }
778            }
779        } catch (RemoteException e) {
780            // Ignore
781        }
782    }
783
784    /**
785     * Change the current system wallpaper to a specific byte stream.  The
786     * give InputStream is copied into persistent storage and will now be
787     * used as the wallpaper.  Currently it must be either a JPEG or PNG
788     * image.  On success, the intent {@link Intent#ACTION_WALLPAPER_CHANGED}
789     * is broadcast.
790     *
791     * <p>This method requires the caller to hold the permission
792     * {@link android.Manifest.permission#SET_WALLPAPER}.
793     *
794     * @param data A stream containing the raw data to install as a wallpaper.
795     *
796     * @throws IOException If an error occurs reverting to the built-in
797     * wallpaper.
798     */
799    public void setStream(InputStream data) throws IOException {
800        if (sGlobals.mService == null) {
801            Log.w(TAG, "WallpaperService not running");
802            return;
803        }
804        try {
805            ParcelFileDescriptor fd = sGlobals.mService.setWallpaper(null);
806            if (fd == null) {
807                return;
808            }
809            FileOutputStream fos = null;
810            try {
811                fos = new ParcelFileDescriptor.AutoCloseOutputStream(fd);
812                setWallpaper(data, fos);
813            } finally {
814                if (fos != null) {
815                    fos.close();
816                }
817            }
818        } catch (RemoteException e) {
819            // Ignore
820        }
821    }
822
823    private void setWallpaper(InputStream data, FileOutputStream fos)
824            throws IOException {
825        byte[] buffer = new byte[32768];
826        int amt;
827        while ((amt=data.read(buffer)) > 0) {
828            fos.write(buffer, 0, amt);
829        }
830    }
831
832    /**
833     * Return whether any users are currently set to use the wallpaper
834     * with the given resource ID.  That is, their wallpaper has been
835     * set through {@link #setResource(int)} with the same resource id.
836     */
837    public boolean hasResourceWallpaper(int resid) {
838        if (sGlobals.mService == null) {
839            Log.w(TAG, "WallpaperService not running");
840            return false;
841        }
842        try {
843            Resources resources = mContext.getResources();
844            String name = "res:" + resources.getResourceName(resid);
845            return sGlobals.mService.hasNamedWallpaper(name);
846        } catch (RemoteException e) {
847            return false;
848        }
849    }
850
851    /**
852     * Returns the desired minimum width for the wallpaper. Callers of
853     * {@link #setBitmap(android.graphics.Bitmap)} or
854     * {@link #setStream(java.io.InputStream)} should check this value
855     * beforehand to make sure the supplied wallpaper respects the desired
856     * minimum width.
857     *
858     * If the returned value is <= 0, the caller should use the width of
859     * the default display instead.
860     *
861     * @return The desired minimum width for the wallpaper. This value should
862     * be honored by applications that set the wallpaper but it is not
863     * mandatory.
864     */
865    public int getDesiredMinimumWidth() {
866        if (sGlobals.mService == null) {
867            Log.w(TAG, "WallpaperService not running");
868            return 0;
869        }
870        try {
871            return sGlobals.mService.getWidthHint();
872        } catch (RemoteException e) {
873            // Shouldn't happen!
874            return 0;
875        }
876    }
877
878    /**
879     * Returns the desired minimum height for the wallpaper. Callers of
880     * {@link #setBitmap(android.graphics.Bitmap)} or
881     * {@link #setStream(java.io.InputStream)} should check this value
882     * beforehand to make sure the supplied wallpaper respects the desired
883     * minimum height.
884     *
885     * If the returned value is <= 0, the caller should use the height of
886     * the default display instead.
887     *
888     * @return The desired minimum height for the wallpaper. This value should
889     * be honored by applications that set the wallpaper but it is not
890     * mandatory.
891     */
892    public int getDesiredMinimumHeight() {
893        if (sGlobals.mService == null) {
894            Log.w(TAG, "WallpaperService not running");
895            return 0;
896        }
897        try {
898            return sGlobals.mService.getHeightHint();
899        } catch (RemoteException e) {
900            // Shouldn't happen!
901            return 0;
902        }
903    }
904
905    /**
906     * For use only by the current home application, to specify the size of
907     * wallpaper it would like to use.  This allows such applications to have
908     * a virtual wallpaper that is larger than the physical screen, matching
909     * the size of their workspace.
910     *
911     * <p>Note developers, who don't seem to be reading this.  This is
912     * for <em>home screens</em> to tell what size wallpaper they would like.
913     * Nobody else should be calling this!  Certainly not other non-home-screen
914     * apps that change the wallpaper.  Those apps are supposed to
915     * <b>retrieve</b> the suggested size so they can construct a wallpaper
916     * that matches it.
917     *
918     * <p>This method requires the caller to hold the permission
919     * {@link android.Manifest.permission#SET_WALLPAPER_HINTS}.
920     *
921     * @param minimumWidth Desired minimum width
922     * @param minimumHeight Desired minimum height
923     */
924    public void suggestDesiredDimensions(int minimumWidth, int minimumHeight) {
925        try {
926            if (sGlobals.mService == null) {
927                Log.w(TAG, "WallpaperService not running");
928            } else {
929                sGlobals.mService.setDimensionHints(minimumWidth, minimumHeight);
930            }
931        } catch (RemoteException e) {
932            // Ignore
933        }
934    }
935
936    /**
937     * Set the position of the current wallpaper within any larger space, when
938     * that wallpaper is visible behind the given window.  The X and Y offsets
939     * are floating point numbers ranging from 0 to 1, representing where the
940     * wallpaper should be positioned within the screen space.  These only
941     * make sense when the wallpaper is larger than the screen.
942     *
943     * @param windowToken The window who these offsets should be associated
944     * with, as returned by {@link android.view.View#getWindowToken()
945     * View.getWindowToken()}.
946     * @param xOffset The offset along the X dimension, from 0 to 1.
947     * @param yOffset The offset along the Y dimension, from 0 to 1.
948     */
949    public void setWallpaperOffsets(IBinder windowToken, float xOffset, float yOffset) {
950        try {
951            //Log.v(TAG, "Sending new wallpaper offsets from app...");
952            WindowManagerGlobal.getWindowSession().setWallpaperPosition(
953                    windowToken, xOffset, yOffset, mWallpaperXStep, mWallpaperYStep);
954            //Log.v(TAG, "...app returning after sending offsets!");
955        } catch (RemoteException e) {
956            // Ignore.
957        }
958    }
959
960    /**
961     * For applications that use multiple virtual screens showing a wallpaper,
962     * specify the step size between virtual screens. For example, if the
963     * launcher has 3 virtual screens, it would specify an xStep of 0.5,
964     * since the X offset for those screens are 0.0, 0.5 and 1.0
965     * @param xStep The X offset delta from one screen to the next one
966     * @param yStep The Y offset delta from one screen to the next one
967     */
968    public void setWallpaperOffsetSteps(float xStep, float yStep) {
969        mWallpaperXStep = xStep;
970        mWallpaperYStep = yStep;
971    }
972
973    /**
974     * Send an arbitrary command to the current active wallpaper.
975     *
976     * @param windowToken The window who these offsets should be associated
977     * with, as returned by {@link android.view.View#getWindowToken()
978     * View.getWindowToken()}.
979     * @param action Name of the command to perform.  This must be a scoped
980     * name to avoid collisions, such as "com.mycompany.wallpaper.DOIT".
981     * @param x Arbitrary integer argument based on command.
982     * @param y Arbitrary integer argument based on command.
983     * @param z Arbitrary integer argument based on command.
984     * @param extras Optional additional information for the command, or null.
985     */
986    public void sendWallpaperCommand(IBinder windowToken, String action,
987            int x, int y, int z, Bundle extras) {
988        try {
989            //Log.v(TAG, "Sending new wallpaper offsets from app...");
990            WindowManagerGlobal.getWindowSession().sendWallpaperCommand(
991                    windowToken, action, x, y, z, extras, false);
992            //Log.v(TAG, "...app returning after sending offsets!");
993        } catch (RemoteException e) {
994            // Ignore.
995        }
996    }
997
998    /**
999     * Clear the offsets previously associated with this window through
1000     * {@link #setWallpaperOffsets(IBinder, float, float)}.  This reverts
1001     * the window to its default state, where it does not cause the wallpaper
1002     * to scroll from whatever its last offsets were.
1003     *
1004     * @param windowToken The window who these offsets should be associated
1005     * with, as returned by {@link android.view.View#getWindowToken()
1006     * View.getWindowToken()}.
1007     */
1008    public void clearWallpaperOffsets(IBinder windowToken) {
1009        try {
1010            WindowManagerGlobal.getWindowSession().setWallpaperPosition(
1011                    windowToken, -1, -1, -1, -1);
1012        } catch (RemoteException e) {
1013            // Ignore.
1014        }
1015    }
1016
1017    /**
1018     * Remove any currently set wallpaper, reverting to the system's built-in
1019     * wallpaper. On success, the intent {@link Intent#ACTION_WALLPAPER_CHANGED}
1020     * is broadcast.
1021     *
1022     * <p>This method requires the caller to hold the permission
1023     * {@link android.Manifest.permission#SET_WALLPAPER}.
1024     *
1025     * @throws IOException If an error occurs reverting to the built-in
1026     * wallpaper.
1027     */
1028    public void clear() throws IOException {
1029        setResource(com.android.internal.R.drawable.default_wallpaper);
1030    }
1031
1032    static Bitmap generateBitmap(Context context, Bitmap bm, int width, int height) {
1033        if (bm == null) {
1034            return null;
1035        }
1036
1037        WindowManager wm = (WindowManager)context.getSystemService(Context.WINDOW_SERVICE);
1038        DisplayMetrics metrics = new DisplayMetrics();
1039        wm.getDefaultDisplay().getMetrics(metrics);
1040        bm.setDensity(metrics.noncompatDensityDpi);
1041
1042        if (width <= 0 || height <= 0
1043                || (bm.getWidth() == width && bm.getHeight() == height)) {
1044            return bm;
1045        }
1046
1047        // This is the final bitmap we want to return.
1048        try {
1049            Bitmap newbm = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
1050            newbm.setDensity(metrics.noncompatDensityDpi);
1051
1052            Canvas c = new Canvas(newbm);
1053            Rect targetRect = new Rect();
1054            targetRect.right = bm.getWidth();
1055            targetRect.bottom = bm.getHeight();
1056
1057            int deltaw = width - targetRect.right;
1058            int deltah = height - targetRect.bottom;
1059
1060            if (deltaw > 0 || deltah > 0) {
1061                // We need to scale up so it covers the entire area.
1062                float scale;
1063                if (deltaw > deltah) {
1064                    scale = width / (float)targetRect.right;
1065                } else {
1066                    scale = height / (float)targetRect.bottom;
1067                }
1068                targetRect.right = (int)(targetRect.right*scale);
1069                targetRect.bottom = (int)(targetRect.bottom*scale);
1070                deltaw = width - targetRect.right;
1071                deltah = height - targetRect.bottom;
1072            }
1073
1074            targetRect.offset(deltaw/2, deltah/2);
1075
1076            Paint paint = new Paint();
1077            paint.setFilterBitmap(true);
1078            paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC));
1079            c.drawBitmap(bm, null, targetRect, paint);
1080
1081            bm.recycle();
1082            c.setBitmap(null);
1083            return newbm;
1084        } catch (OutOfMemoryError e) {
1085            Log.w(TAG, "Can't generate default bitmap", e);
1086            return bm;
1087        }
1088    }
1089}
1090