WallpaperManagerpublic class WallpaperManager extends Object Provides access to the system wallpaper. With WallpaperManager, you can
get the current wallpaper, get the desired dimensions for the wallpaper, set
the wallpaper, and more. Get an instance of WallpaperManager with
{@link #getInstance(android.content.Context) getInstance()}. |
Fields Summary |
---|
private static String | TAG | private static boolean | DEBUG | private float | mWallpaperXStep | private float | mWallpaperYStep | private static final String | PROP_WALLPAPER{@hide} | private static final String | PROP_WALLPAPER_COMPONENT{@hide} | public static final String | ACTION_CROP_AND_SET_WALLPAPERActivity Action: Show settings for choosing wallpaper. Do not use directly to construct
an intent; instead, use {@link #getCropAndSetWallpaperIntent}.
Input: {@link Intent#getData} is the URI of the image to crop and set as wallpaper.
Output: RESULT_OK if user decided to crop/set the wallpaper, RESULT_CANCEL otherwise
Activities that support this intent should specify a MIME filter of "image/*" | public static final String | ACTION_LIVE_WALLPAPER_CHOOSERLaunch an activity for the user to pick the current global live
wallpaper. | public static final String | ACTION_CHANGE_LIVE_WALLPAPERDirectly launch live wallpaper preview, allowing the user to immediately
confirm to switch to a specific live wallpaper. You must specify
{@link #EXTRA_LIVE_WALLPAPER_COMPONENT} with the ComponentName of
a live wallpaper component that is to be shown. | public static final String | EXTRA_LIVE_WALLPAPER_COMPONENTExtra in {@link #ACTION_CHANGE_LIVE_WALLPAPER} that specifies the
ComponentName of a live wallpaper that should be shown as a preview,
for the user to confirm. | public static final String | WALLPAPER_PREVIEW_META_DATAManifest entry for activities that respond to {@link Intent#ACTION_SET_WALLPAPER}
which allows them to provide a custom large icon associated with this action. | public static final String | COMMAND_TAPCommand for {@link #sendWallpaperCommand}: reported by the wallpaper
host when the user taps on an empty area (not performing an action
in the host). The x and y arguments are the location of the tap in
screen coordinates. | public static final String | COMMAND_SECONDARY_TAPCommand for {@link #sendWallpaperCommand}: reported by the wallpaper
host when the user releases a secondary pointer on an empty area
(not performing an action in the host). The x and y arguments are
the location of the secondary tap in screen coordinates. | public static final String | COMMAND_DROPCommand for {@link #sendWallpaperCommand}: reported by the wallpaper
host when the user drops an object into an area of the host. The x
and y arguments are the location of the drop. | private final android.content.Context | mContext | private static final Object | sSync | private static Globals | sGlobals |
Methods Summary |
---|
public void | clear()Remove any currently set wallpaper, reverting to the system's built-in
wallpaper. On success, the intent {@link Intent#ACTION_WALLPAPER_CHANGED}
is broadcast.
This method requires the caller to hold the permission
{@link android.Manifest.permission#SET_WALLPAPER}.
setStream(openDefaultWallpaper(mContext));
| public void | clearWallpaperOffsets(android.os.IBinder windowToken)Clear the offsets previously associated with this window through
{@link #setWallpaperOffsets(IBinder, float, float)}. This reverts
the window to its default state, where it does not cause the wallpaper
to scroll from whatever its last offsets were.
try {
WindowManagerGlobal.getWindowSession().setWallpaperPosition(
windowToken, -1, -1, -1, -1);
} catch (RemoteException e) {
// Ignore.
}
| public void | forgetLoadedWallpaper()Remove all internal references to the last loaded wallpaper. Useful
for apps that want to reduce memory usage when they only temporarily
need to have the wallpaper. After calling, the next request for the
wallpaper will require reloading it again from disk.
sGlobals.forgetLoadedWallpaper();
| public android.graphics.Bitmap | getBitmap()Like {@link #getDrawable()} but returns a Bitmap.
return sGlobals.peekWallpaperBitmap(mContext, true);
| public android.graphics.drawable.Drawable | getBuiltInDrawable()Returns a drawable for the system built-in static wallpaper .
return getBuiltInDrawable(0, 0, false, 0, 0);
| public android.graphics.drawable.Drawable | getBuiltInDrawable(int outWidth, int outHeight, boolean scaleToFit, float horizontalAlignment, float verticalAlignment)Returns a drawable for the system built-in static wallpaper. Based on the parameters, the
drawable can be cropped and scaled
if (sGlobals.mService == null) {
Log.w(TAG, "WallpaperService not running");
return null;
}
Resources resources = mContext.getResources();
horizontalAlignment = Math.max(0, Math.min(1, horizontalAlignment));
verticalAlignment = Math.max(0, Math.min(1, verticalAlignment));
InputStream is = new BufferedInputStream(openDefaultWallpaper(mContext));
if (is == null) {
Log.e(TAG, "default wallpaper input stream is null");
return null;
} else {
if (outWidth <= 0 || outHeight <= 0) {
Bitmap fullSize = BitmapFactory.decodeStream(is, null, null);
return new BitmapDrawable(resources, fullSize);
} else {
int inWidth;
int inHeight;
{
BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeStream(is, null, options);
if (options.outWidth != 0 && options.outHeight != 0) {
inWidth = options.outWidth;
inHeight = options.outHeight;
} else {
Log.e(TAG, "default wallpaper dimensions are 0");
return null;
}
}
is = new BufferedInputStream(openDefaultWallpaper(mContext));
RectF cropRectF;
outWidth = Math.min(inWidth, outWidth);
outHeight = Math.min(inHeight, outHeight);
if (scaleToFit) {
cropRectF = getMaxCropRect(inWidth, inHeight, outWidth, outHeight,
horizontalAlignment, verticalAlignment);
} else {
float left = (inWidth - outWidth) * horizontalAlignment;
float right = left + outWidth;
float top = (inHeight - outHeight) * verticalAlignment;
float bottom = top + outHeight;
cropRectF = new RectF(left, top, right, bottom);
}
Rect roundedTrueCrop = new Rect();
cropRectF.roundOut(roundedTrueCrop);
if (roundedTrueCrop.width() <= 0 || roundedTrueCrop.height() <= 0) {
Log.w(TAG, "crop has bad values for full size image");
return null;
}
// See how much we're reducing the size of the image
int scaleDownSampleSize = Math.min(roundedTrueCrop.width() / outWidth,
roundedTrueCrop.height() / outHeight);
// Attempt to open a region decoder
BitmapRegionDecoder decoder = null;
try {
decoder = BitmapRegionDecoder.newInstance(is, true);
} catch (IOException e) {
Log.w(TAG, "cannot open region decoder for default wallpaper");
}
Bitmap crop = null;
if (decoder != null) {
// Do region decoding to get crop bitmap
BitmapFactory.Options options = new BitmapFactory.Options();
if (scaleDownSampleSize > 1) {
options.inSampleSize = scaleDownSampleSize;
}
crop = decoder.decodeRegion(roundedTrueCrop, options);
decoder.recycle();
}
if (crop == null) {
// BitmapRegionDecoder has failed, try to crop in-memory
is = new BufferedInputStream(openDefaultWallpaper(mContext));
Bitmap fullSize = null;
if (is != null) {
BitmapFactory.Options options = new BitmapFactory.Options();
if (scaleDownSampleSize > 1) {
options.inSampleSize = scaleDownSampleSize;
}
fullSize = BitmapFactory.decodeStream(is, null, options);
}
if (fullSize != null) {
crop = Bitmap.createBitmap(fullSize, roundedTrueCrop.left,
roundedTrueCrop.top, roundedTrueCrop.width(),
roundedTrueCrop.height());
}
}
if (crop == null) {
Log.w(TAG, "cannot decode default wallpaper");
return null;
}
// Scale down if necessary
if (outWidth > 0 && outHeight > 0 &&
(crop.getWidth() != outWidth || crop.getHeight() != outHeight)) {
Matrix m = new Matrix();
RectF cropRect = new RectF(0, 0, crop.getWidth(), crop.getHeight());
RectF returnRect = new RectF(0, 0, outWidth, outHeight);
m.setRectToRect(cropRect, returnRect, Matrix.ScaleToFit.FILL);
Bitmap tmp = Bitmap.createBitmap((int) returnRect.width(),
(int) returnRect.height(), Bitmap.Config.ARGB_8888);
if (tmp != null) {
Canvas c = new Canvas(tmp);
Paint p = new Paint();
p.setFilterBitmap(true);
c.drawBitmap(crop, m, p);
crop = tmp;
}
}
return new BitmapDrawable(resources, crop);
}
}
| public android.content.Intent | getCropAndSetWallpaperIntent(android.net.Uri imageUri)Gets an Intent that will launch an activity that crops the given
image and sets the device's wallpaper. If there is a default HOME activity
that supports cropping wallpapers, it will be preferred as the default.
Use this method instead of directly creating a {@link #ACTION_CROP_AND_SET_WALLPAPER}
intent.
if (imageUri == null) {
throw new IllegalArgumentException("Image URI must not be null");
}
if (!ContentResolver.SCHEME_CONTENT.equals(imageUri.getScheme())) {
throw new IllegalArgumentException("Image URI must be of the "
+ ContentResolver.SCHEME_CONTENT + " scheme type");
}
final PackageManager packageManager = mContext.getPackageManager();
Intent cropAndSetWallpaperIntent =
new Intent(ACTION_CROP_AND_SET_WALLPAPER, imageUri);
cropAndSetWallpaperIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
// Find out if the default HOME activity supports CROP_AND_SET_WALLPAPER
Intent homeIntent = new Intent(Intent.ACTION_MAIN).addCategory(Intent.CATEGORY_HOME);
ResolveInfo resolvedHome = packageManager.resolveActivity(homeIntent,
PackageManager.MATCH_DEFAULT_ONLY);
if (resolvedHome != null) {
cropAndSetWallpaperIntent.setPackage(resolvedHome.activityInfo.packageName);
List<ResolveInfo> cropAppList = packageManager.queryIntentActivities(
cropAndSetWallpaperIntent, 0);
if (cropAppList.size() > 0) {
return cropAndSetWallpaperIntent;
}
}
// fallback crop activity
cropAndSetWallpaperIntent.setPackage("com.android.wallpapercropper");
List<ResolveInfo> cropAppList = packageManager.queryIntentActivities(
cropAndSetWallpaperIntent, 0);
if (cropAppList.size() > 0) {
return cropAndSetWallpaperIntent;
}
// If the URI is not of the right type, or for some reason the system wallpaper
// cropper doesn't exist, return null
throw new IllegalArgumentException("Cannot use passed URI to set wallpaper; " +
"check that the type returned by ContentProvider matches image/*");
| public static android.content.ComponentName | getDefaultWallpaperComponent(android.content.Context context)Return {@link ComponentName} of the default live wallpaper, or
{@code null} if none is defined.
String flat = SystemProperties.get(PROP_WALLPAPER_COMPONENT);
if (!TextUtils.isEmpty(flat)) {
final ComponentName cn = ComponentName.unflattenFromString(flat);
if (cn != null) {
return cn;
}
}
flat = context.getString(com.android.internal.R.string.default_wallpaper_component);
if (!TextUtils.isEmpty(flat)) {
final ComponentName cn = ComponentName.unflattenFromString(flat);
if (cn != null) {
return cn;
}
}
return null;
| public int | getDesiredMinimumHeight()Returns the desired minimum height for the wallpaper. Callers of
{@link #setBitmap(android.graphics.Bitmap)} or
{@link #setStream(java.io.InputStream)} should check this value
beforehand to make sure the supplied wallpaper respects the desired
minimum height.
If the returned value is <= 0, the caller should use the height of
the default display instead.
if (sGlobals.mService == null) {
Log.w(TAG, "WallpaperService not running");
return 0;
}
try {
return sGlobals.mService.getHeightHint();
} catch (RemoteException e) {
// Shouldn't happen!
return 0;
}
| public int | getDesiredMinimumWidth()Returns the desired minimum width for the wallpaper. Callers of
{@link #setBitmap(android.graphics.Bitmap)} or
{@link #setStream(java.io.InputStream)} should check this value
beforehand to make sure the supplied wallpaper respects the desired
minimum width.
If the returned value is <= 0, the caller should use the width of
the default display instead.
if (sGlobals.mService == null) {
Log.w(TAG, "WallpaperService not running");
return 0;
}
try {
return sGlobals.mService.getWidthHint();
} catch (RemoteException e) {
// Shouldn't happen!
return 0;
}
| public android.graphics.drawable.Drawable | getDrawable()Retrieve the current system wallpaper; if
no wallpaper is set, the system built-in static wallpaper is returned.
This is returned as an
abstract Drawable that you can install in a View to display whatever
wallpaper the user has currently set.
Bitmap bm = sGlobals.peekWallpaperBitmap(mContext, true);
if (bm != null) {
Drawable dr = new BitmapDrawable(mContext.getResources(), bm);
dr.setDither(false);
return dr;
}
return null;
| public android.graphics.drawable.Drawable | getFastDrawable()Like {@link #getDrawable()}, but the returned Drawable has a number
of limitations to reduce its overhead as much as possible. It will
never scale the wallpaper (only centering it if the requested bounds
do match the bitmap bounds, which should not be typical), doesn't
allow setting an alpha, color filter, or other attributes, etc. The
bounds of the returned drawable will be initialized to the same bounds
as the wallpaper, so normally you will not need to touch it. The
drawable also assumes that it will be used in a context running in
the same density as the screen (not in density compatibility mode).
Bitmap bm = sGlobals.peekWallpaperBitmap(mContext, true);
if (bm != null) {
return new FastBitmapDrawable(bm);
}
return null;
| public IWallpaperManager | getIWallpaperManager()
return sGlobals.mService;
| public static android.app.WallpaperManager | getInstance(android.content.Context context)Retrieve a WallpaperManager associated with the given Context.
return (WallpaperManager)context.getSystemService(
Context.WALLPAPER_SERVICE);
| private static android.graphics.RectF | getMaxCropRect(int inWidth, int inHeight, int outWidth, int outHeight, float horizontalAlignment, float verticalAlignment)
RectF cropRect = new RectF();
// Get a crop rect that will fit this
if (inWidth / (float) inHeight > outWidth / (float) outHeight) {
cropRect.top = 0;
cropRect.bottom = inHeight;
float cropWidth = outWidth * (inHeight / (float) outHeight);
cropRect.left = (inWidth - cropWidth) * horizontalAlignment;
cropRect.right = cropRect.left + cropWidth;
} else {
cropRect.left = 0;
cropRect.right = inWidth;
float cropHeight = outHeight * (inWidth / (float) outWidth);
cropRect.top = (inHeight - cropHeight) * verticalAlignment;
cropRect.bottom = cropRect.top + cropHeight;
}
return cropRect;
| public WallpaperInfo | getWallpaperInfo()If the current wallpaper is a live wallpaper component, return the
information about that wallpaper. Otherwise, if it is a static image,
simply return null.
try {
if (sGlobals.mService == null) {
Log.w(TAG, "WallpaperService not running");
return null;
} else {
return sGlobals.mService.getWallpaperInfo();
}
} catch (RemoteException e) {
return null;
}
| public boolean | hasResourceWallpaper(int resid)Return whether any users are currently set to use the wallpaper
with the given resource ID. That is, their wallpaper has been
set through {@link #setResource(int)} with the same resource id.
if (sGlobals.mService == null) {
Log.w(TAG, "WallpaperService not running");
return false;
}
try {
Resources resources = mContext.getResources();
String name = "res:" + resources.getResourceName(resid);
return sGlobals.mService.hasNamedWallpaper(name);
} catch (RemoteException e) {
return false;
}
| static void | initGlobals(android.os.Looper looper)
synchronized (sSync) {
if (sGlobals == null) {
sGlobals = new Globals(looper);
}
}
| public static java.io.InputStream | openDefaultWallpaper(android.content.Context context)Open stream representing the default static image wallpaper.
final String path = SystemProperties.get(PROP_WALLPAPER);
if (!TextUtils.isEmpty(path)) {
final File file = new File(path);
if (file.exists()) {
try {
return new FileInputStream(file);
} catch (IOException e) {
// Ignored, fall back to platform default below
}
}
}
return context.getResources().openRawResource(
com.android.internal.R.drawable.default_wallpaper);
| public android.graphics.drawable.Drawable | peekDrawable()Retrieve the current system wallpaper; if there is no wallpaper set,
a null pointer is returned. This is returned as an
abstract Drawable that you can install in a View to display whatever
wallpaper the user has currently set.
Bitmap bm = sGlobals.peekWallpaperBitmap(mContext, false);
if (bm != null) {
Drawable dr = new BitmapDrawable(mContext.getResources(), bm);
dr.setDither(false);
return dr;
}
return null;
| public android.graphics.drawable.Drawable | peekFastDrawable()Like {@link #getFastDrawable()}, but if there is no wallpaper set,
a null pointer is returned.
Bitmap bm = sGlobals.peekWallpaperBitmap(mContext, false);
if (bm != null) {
return new FastBitmapDrawable(bm);
}
return null;
| public void | sendWallpaperCommand(android.os.IBinder windowToken, java.lang.String action, int x, int y, int z, android.os.Bundle extras)Send an arbitrary command to the current active wallpaper.
try {
//Log.v(TAG, "Sending new wallpaper offsets from app...");
WindowManagerGlobal.getWindowSession().sendWallpaperCommand(
windowToken, action, x, y, z, extras, false);
//Log.v(TAG, "...app returning after sending offsets!");
} catch (RemoteException e) {
// Ignore.
}
| public void | setBitmap(android.graphics.Bitmap bitmap)Change the current system wallpaper to a bitmap. The given bitmap is
converted to a PNG and stored as the wallpaper. On success, the intent
{@link Intent#ACTION_WALLPAPER_CHANGED} is broadcast.
This method requires the caller to hold the permission
{@link android.Manifest.permission#SET_WALLPAPER}.
if (sGlobals.mService == null) {
Log.w(TAG, "WallpaperService not running");
return;
}
try {
ParcelFileDescriptor fd = sGlobals.mService.setWallpaper(null);
if (fd == null) {
return;
}
FileOutputStream fos = null;
try {
fos = new ParcelFileDescriptor.AutoCloseOutputStream(fd);
bitmap.compress(Bitmap.CompressFormat.PNG, 90, fos);
} finally {
if (fos != null) {
fos.close();
}
}
} catch (RemoteException e) {
// Ignore
}
| public void | setDisplayOffset(android.os.IBinder windowToken, int x, int y)Apply a raw offset to the wallpaper window. Should only be used in
combination with {@link #setDisplayPadding(android.graphics.Rect)} when you
have ensured that the wallpaper will extend outside of the display area so that
it can be moved without leaving part of the display uncovered.
try {
//Log.v(TAG, "Sending new wallpaper display offsets from app...");
WindowManagerGlobal.getWindowSession().setWallpaperDisplayOffset(
windowToken, x, y);
//Log.v(TAG, "...app returning after sending display offset!");
} catch (RemoteException e) {
// Ignore.
}
| public void | setDisplayPadding(android.graphics.Rect padding)Specify extra padding that the wallpaper should have outside of the display.
That is, the given padding supplies additional pixels the wallpaper should extend
outside of the display itself.
try {
if (sGlobals.mService == null) {
Log.w(TAG, "WallpaperService not running");
} else {
sGlobals.mService.setDisplayPadding(padding);
}
} catch (RemoteException e) {
// Ignore
}
| public void | setResource(int resid)Change the current system wallpaper to the bitmap in the given resource.
The resource is opened as a raw data stream and copied into the
wallpaper; it must be a valid PNG or JPEG image. On success, the intent
{@link Intent#ACTION_WALLPAPER_CHANGED} is broadcast.
This method requires the caller to hold the permission
{@link android.Manifest.permission#SET_WALLPAPER}.
if (sGlobals.mService == null) {
Log.w(TAG, "WallpaperService not running");
return;
}
try {
Resources resources = mContext.getResources();
/* Set the wallpaper to the default values */
ParcelFileDescriptor fd = sGlobals.mService.setWallpaper(
"res:" + resources.getResourceName(resid));
if (fd != null) {
FileOutputStream fos = null;
try {
fos = new ParcelFileDescriptor.AutoCloseOutputStream(fd);
setWallpaper(resources.openRawResource(resid), fos);
} finally {
if (fos != null) {
fos.close();
}
}
}
} catch (RemoteException e) {
// Ignore
}
| public void | setStream(java.io.InputStream data)Change the current system wallpaper to a specific byte stream. The
give InputStream is copied into persistent storage and will now be
used as the wallpaper. Currently it must be either a JPEG or PNG
image. On success, the intent {@link Intent#ACTION_WALLPAPER_CHANGED}
is broadcast.
This method requires the caller to hold the permission
{@link android.Manifest.permission#SET_WALLPAPER}.
if (sGlobals.mService == null) {
Log.w(TAG, "WallpaperService not running");
return;
}
try {
ParcelFileDescriptor fd = sGlobals.mService.setWallpaper(null);
if (fd == null) {
return;
}
FileOutputStream fos = null;
try {
fos = new ParcelFileDescriptor.AutoCloseOutputStream(fd);
setWallpaper(data, fos);
} finally {
if (fos != null) {
fos.close();
}
}
} catch (RemoteException e) {
// Ignore
}
| private void | setWallpaper(java.io.InputStream data, java.io.FileOutputStream fos)
byte[] buffer = new byte[32768];
int amt;
while ((amt=data.read(buffer)) > 0) {
fos.write(buffer, 0, amt);
}
| public void | setWallpaperOffsetSteps(float xStep, float yStep)For applications that use multiple virtual screens showing a wallpaper,
specify the step size between virtual screens. For example, if the
launcher has 3 virtual screens, it would specify an xStep of 0.5,
since the X offset for those screens are 0.0, 0.5 and 1.0
mWallpaperXStep = xStep;
mWallpaperYStep = yStep;
| public void | setWallpaperOffsets(android.os.IBinder windowToken, float xOffset, float yOffset)Set the position of the current wallpaper within any larger space, when
that wallpaper is visible behind the given window. The X and Y offsets
are floating point numbers ranging from 0 to 1, representing where the
wallpaper should be positioned within the screen space. These only
make sense when the wallpaper is larger than the screen.
try {
//Log.v(TAG, "Sending new wallpaper offsets from app...");
WindowManagerGlobal.getWindowSession().setWallpaperPosition(
windowToken, xOffset, yOffset, mWallpaperXStep, mWallpaperYStep);
//Log.v(TAG, "...app returning after sending offsets!");
} catch (RemoteException e) {
// Ignore.
}
| public void | suggestDesiredDimensions(int minimumWidth, int minimumHeight)For use only by the current home application, to specify the size of
wallpaper it would like to use. This allows such applications to have
a virtual wallpaper that is larger than the physical screen, matching
the size of their workspace.
Note developers, who don't seem to be reading this. This is
for home screens to tell what size wallpaper they would like.
Nobody else should be calling this! Certainly not other non-home-screen
apps that change the wallpaper. Those apps are supposed to
retrieve the suggested size so they can construct a wallpaper
that matches it.
This method requires the caller to hold the permission
{@link android.Manifest.permission#SET_WALLPAPER_HINTS}.
try {
/**
* The framework makes no attempt to limit the window size
* to the maximum texture size. Any window larger than this
* cannot be composited.
*
* Read maximum texture size from system property and scale down
* minimumWidth and minimumHeight accordingly.
*/
int maximumTextureSize;
try {
maximumTextureSize = SystemProperties.getInt("sys.max_texture_size", 0);
} catch (Exception e) {
maximumTextureSize = 0;
}
if (maximumTextureSize > 0) {
if ((minimumWidth > maximumTextureSize) ||
(minimumHeight > maximumTextureSize)) {
float aspect = (float)minimumHeight / (float)minimumWidth;
if (minimumWidth > minimumHeight) {
minimumWidth = maximumTextureSize;
minimumHeight = (int)((minimumWidth * aspect) + 0.5);
} else {
minimumHeight = maximumTextureSize;
minimumWidth = (int)((minimumHeight / aspect) + 0.5);
}
}
}
if (sGlobals.mService == null) {
Log.w(TAG, "WallpaperService not running");
} else {
sGlobals.mService.setDimensionHints(minimumWidth, minimumHeight);
}
} catch (RemoteException e) {
// Ignore
}
|
|