Code example for Context

Methods: checkCallingOrSelfPermissiongetSharedPreferences

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 int getWidthHint() throws RemoteException { 
        return mWidth;
    } 
 
    public int getHeightHint() throws RemoteException { 
        return mHeight;
    } 
 
    public ParcelFileDescriptor getWallpaper(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 ParcelFileDescriptor setWallpaper() {
        checkPermission(android.Manifest.permission.SET_WALLPAPER);
        try { 
            ParcelFileDescriptor fd = ParcelFileDescriptor.open(WALLPAPER_FILE,
                    MODE_CREATE|MODE_READ_WRITE);
 
            // changing the wallpaper means we'll need to back up the new one 
            long origId = Binder.clearCallingIdentity();
            BackupManager bm = new BackupManager(mContext);
            bm.dataChanged();
            Binder.restoreCallingIdentity(origId);
 
            return fd;
        } catch (FileNotFoundException e) {
            if (Config.LOGD) Log.d(TAG, "Error setting wallpaper", e);
        } 
        return null; 
    } 
 
    private void createFilesDir() { 
        if (!WALLPAPER_DIR.exists()) {
            WALLPAPER_DIR.mkdirs();
        } 
    } 
 
    private void notifyCallbacks() { 
        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);
    } 
 
    private void checkPermission(String permission) {
        if (PackageManager.PERMISSION_GRANTED != mContext.checkCallingOrSelfPermission(permission)) {
            throw new SecurityException("Access denied to process: " + Binder.getCallingPid()
                    + ", must have permission " + permission);
        } 
    } 
}