From c8daa769256b039b6bc4c5acbe6b558cd776c00a Mon Sep 17 00:00:00 2001 From: Christopher Tate Date: Mon, 6 Jul 2009 19:04:57 -0700 Subject: BackupManager wrapper class improvements + Now rechecks the cached IBinder each time the wrapper is used, and if it's still null (i.e. the BackupManager was constructed before the system service came up) it's refetched. This lets even system code cache a single BackupManager instance and just keep making calls through it without worrying about interactions with the life cycle of the backup service. + Added a static dataChanged(packageName) method as a convenience for code that needs to indicate that some other package needs a backup pass. This is useful even for third party code in the case of multiple packages in a shared-uid situation. --- core/java/android/backup/BackupManager.java | 49 ++++++++++++++++------ .../com/android/backuptest/BackupTestActivity.java | 14 +++---- 2 files changed, 43 insertions(+), 20 deletions(-) diff --git a/core/java/android/backup/BackupManager.java b/core/java/android/backup/BackupManager.java index 5b4ac0da305f..34a1a0c8f2f4 100644 --- a/core/java/android/backup/BackupManager.java +++ b/core/java/android/backup/BackupManager.java @@ -19,6 +19,7 @@ package android.backup; import android.content.Context; import android.os.RemoteException; import android.os.ServiceManager; +import android.util.Log; /** * BackupManager is the interface to the system's backup service. @@ -39,14 +40,17 @@ import android.os.ServiceManager; * @hide pending API solidification */ public class BackupManager { + private static final String TAG = "BackupManager"; + private Context mContext; - private IBackupManager mService; + private static IBackupManager sService; - /** - * Defined backup transports understood by {@link IBackupManager.selectBackupTransport}. - */ - public static final int TRANSPORT_LOCAL = 1; - public static final int TRANSPORT_GOOGLE = 2; + private static void checkServiceBinder() { + if (sService == null) { + sService = IBackupManager.Stub.asInterface( + ServiceManager.getService(Context.BACKUP_SERVICE)); + } + } /** * Constructs a BackupManager object through which the application can @@ -58,8 +62,6 @@ public class BackupManager { */ public BackupManager(Context context) { mContext = context; - mService = IBackupManager.Stub.asInterface( - ServiceManager.getService(Context.BACKUP_SERVICE)); } /** @@ -68,10 +70,31 @@ public class BackupManager { * {@link android.app.BackupAgent} subclass will be scheduled when you call this method. */ public void dataChanged() { - if (mService != null) { + checkServiceBinder(); + if (sService != null) { + try { + sService.dataChanged(mContext.getPackageName()); + } catch (RemoteException e) { + Log.d(TAG, "dataChanged() couldn't connect"); + } + } + } + + /** + * Convenience method for callers who need to indicate that some other package + * needs a backup pass. This can be relevant in the case of groups of packages + * that share a uid, for example. + * + * This method requires that the application hold the "android.permission.BACKUP" + * permission if the package named in the argument is not the caller's own. + */ + public static void dataChanged(String packageName) { + checkServiceBinder(); + if (sService != null) { try { - mService.dataChanged(mContext.getPackageName()); + sService.dataChanged(packageName); } catch (RemoteException e) { + Log.d(TAG, "dataChanged(pkg) couldn't connect"); } } } @@ -85,10 +108,12 @@ public class BackupManager { */ public IRestoreSession beginRestoreSession(String transport) { IRestoreSession binder = null; - if (mService != null) { + checkServiceBinder(); + if (sService != null) { try { - binder = mService.beginRestoreSession(transport); + binder = sService.beginRestoreSession(transport); } catch (RemoteException e) { + Log.d(TAG, "beginRestoreSession() couldn't connect"); } } return binder; diff --git a/tests/backup/src/com/android/backuptest/BackupTestActivity.java b/tests/backup/src/com/android/backuptest/BackupTestActivity.java index 7f30c916cec2..afbc70304ea4 100644 --- a/tests/backup/src/com/android/backuptest/BackupTestActivity.java +++ b/tests/backup/src/com/android/backuptest/BackupTestActivity.java @@ -52,6 +52,8 @@ public class BackupTestActivity extends ListActivity static final String PREF_KEY = "pref"; static final String FILE_NAME = "file.txt"; + BackupManager sBm = new BackupManager(this); + Test[] mTests = new Test[] { new Test("Show File") { void run() { @@ -85,8 +87,7 @@ public class BackupTestActivity extends ListActivity output.close(); } } - BackupManager bm = new BackupManager(BackupTestActivity.this); - bm.dataChanged(); + sBm.dataChanged(); } }, new Test("Clear File") { @@ -100,14 +101,12 @@ public class BackupTestActivity extends ListActivity output.close(); } } - BackupManager bm = new BackupManager(BackupTestActivity.this); - bm.dataChanged(); + sBm.dataChanged(); } }, new Test("Poke") { void run() { - BackupManager bm = new BackupManager(BackupTestActivity.this); - bm.dataChanged(); + sBm.dataChanged(); } }, new Test("Show Shared Pref") { @@ -126,8 +125,7 @@ public class BackupTestActivity extends ListActivity SharedPreferences.Editor editor = prefs.edit(); editor.putInt(PREF_KEY, val+1); editor.commit(); - BackupManager bm = new BackupManager(BackupTestActivity.this); - bm.dataChanged(); + sBm.dataChanged(); } }, new Test("Backup Helpers") { -- cgit v1.2.3-59-g8ed1b