FileDocCategorySizeDatePackage
WallpaperService.javaAPI DocAndroid 1.5 API6640Wed May 06 22:42:00 BST 2009com.android.server

WallpaperService

public class WallpaperService extends IWallpaperService.Stub

Fields Summary
private static final String
TAG
private static final File
WALLPAPER_DIR
private static final String
WALLPAPER
private static final File
WALLPAPER_FILE
private static final String
PREFERENCES
private static final String
HINT_WIDTH
private static final String
HINT_HEIGHT
private final android.os.RemoteCallbackList
mCallbacks
List of callbacks registered they should each be notified when the wallpaper is changed.
private final android.os.FileObserver
mWallpaperObserver
Observes the wallpaper for changes and notifies all IWallpaperServiceCallbacks that the wallpaper has changed. The CREATE is triggered when there is no wallpaper set and is created for the first time. The CLOSE_WRITE is triggered everytime the wallpaper is changed.
private final android.content.Context
mContext
private int
mWidth
private int
mHeight
Constructors Summary
public WallpaperService(android.content.Context context)


       
        if (Config.LOGD) Log.d(TAG, "WallpaperService startup");
        mContext = context;
        createFilesDir();
        mWallpaperObserver.startWatching();

        SharedPreferences preferences = mContext.getSharedPreferences(PREFERENCES,
                    Context.MODE_PRIVATE);
        mWidth = preferences.getInt(HINT_WIDTH, -1);
        mHeight = preferences.getInt(HINT_HEIGHT, -1);
    
Methods Summary
private voidcheckPermission(java.lang.String permission)

        if (PackageManager.PERMISSION_GRANTED != mContext.checkCallingOrSelfPermission(permission)) {
            throw new SecurityException("Access denied to process: " + Binder.getCallingPid()
                    + ", must have permission " + permission);
        }
    
public voidclearWallpaper()

        File f = WALLPAPER_FILE;
        if (f.exists()) {
            f.delete();
        }
    
private voidcreateFilesDir()

        if (!WALLPAPER_DIR.exists()) {
            WALLPAPER_DIR.mkdirs();
        }
    
protected voidfinalize()

        super.finalize();
        mWallpaperObserver.stopWatching();
    
public intgetHeightHint()

        return mHeight;
    
public android.os.ParcelFileDescriptorgetWallpaper(android.app.IWallpaperServiceCallback cb)

        try {
            mCallbacks.register(cb);
            File f = WALLPAPER_FILE;
            if (!f.exists()) {
                return null;
            }
            return ParcelFileDescriptor.open(f, MODE_READ_ONLY);
        } catch (FileNotFoundException e) {
            
            /* Shouldn't happen as we check to see if the file exists */
            if (Config.LOGD) Log.d(TAG, "Error getting wallpaper", e);
        }
        return null;
    
public intgetWidthHint()

        return mWidth;
    
private voidnotifyCallbacks()

        final int n = mCallbacks.beginBroadcast();
        for (int i = 0; i < n; i++) {
            try {
                mCallbacks.getBroadcastItem(i).onWallpaperChanged();
            } catch (RemoteException e) {

                // The RemoteCallbackList will take care of removing
                // the dead object for us.
            }
        }
        mCallbacks.finishBroadcast();
        final Intent intent = new Intent(Intent.ACTION_WALLPAPER_CHANGED);
        mContext.sendBroadcast(intent);
    
public voidsetDimensionHints(int width, int height)

        checkPermission(android.Manifest.permission.SET_WALLPAPER_HINTS);

        if (width <= 0 || height <= 0) {
            throw new IllegalArgumentException("width and height must be > 0");
        }

        if (width != mWidth || height != mHeight) {
            mWidth = width;
            mHeight = height;

            SharedPreferences preferences = mContext.getSharedPreferences(PREFERENCES,
                    Context.MODE_PRIVATE);

            final SharedPreferences.Editor editor = preferences.edit();
            editor.putInt(HINT_WIDTH, width);
            editor.putInt(HINT_HEIGHT, height);
            editor.commit();
        }
    
public android.os.ParcelFileDescriptorsetWallpaper()

        checkPermission(android.Manifest.permission.SET_WALLPAPER);
        try {
            return ParcelFileDescriptor.open(WALLPAPER_FILE, MODE_CREATE|MODE_READ_WRITE);
        } catch (FileNotFoundException e) {
            if (Config.LOGD) Log.d(TAG, "Error setting wallpaper", e);
        }
        return null;