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