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 17 package android.backup; 18 19 import android.content.Context; 20 import android.os.RemoteException; 21 import android.os.ServiceManager; 22 import android.util.Log; 23 24 /** 25 * BackupManager is the interface to the system's backup service. 26 * Applications simply instantiate one, and then use that instance 27 * to communicate with the backup infrastructure. 28 * 29 * <p>When your application has made changes to data it wishes to have 30 * backed up, call {@link #dataChanged()} to notify the backup service. 31 * The system will then schedule a backup operation to occur in the near 32 * future. Repeated calls to {@link #dataChanged()} have no further effect 33 * until the backup operation actually occurs. 34 * 35 * <p>The backup operation itself begins with the system launching the 36 * {@link android.app.BackupAgent} subclass declared in your manifest. See the 37 * documentation for {@link android.app.BackupAgent} for a detailed description 38 * of how the backup then proceeds. 39 * 40 * @hide pending API solidification 41 */ 42 public class BackupManager { 43 private static final String TAG = "BackupManager"; 44 45 /** @hide TODO: REMOVE THIS */ 46 public static final boolean EVEN_THINK_ABOUT_DOING_RESTORE = true; 47 48 private Context mContext; 49 private static IBackupManager sService; 50 checkServiceBinder()51 private static void checkServiceBinder() { 52 if (sService == null) { 53 sService = IBackupManager.Stub.asInterface( 54 ServiceManager.getService(Context.BACKUP_SERVICE)); 55 } 56 } 57 58 /** 59 * Constructs a BackupManager object through which the application can 60 * communicate with the Android backup system. 61 * 62 * @param context The {@link android.content.Context} that was provided when 63 * one of your application's {@link android.app.Activity Activities} 64 * was created. 65 */ BackupManager(Context context)66 public BackupManager(Context context) { 67 mContext = context; 68 } 69 70 /** 71 * Notifies the Android backup system that your application wishes to back up 72 * new changes to its data. A backup operation using your application's 73 * {@link android.app.BackupAgent} subclass will be scheduled when you call this method. 74 */ dataChanged()75 public void dataChanged() { 76 if (!EVEN_THINK_ABOUT_DOING_RESTORE) { 77 return; 78 } 79 checkServiceBinder(); 80 if (sService != null) { 81 try { 82 sService.dataChanged(mContext.getPackageName()); 83 } catch (RemoteException e) { 84 Log.d(TAG, "dataChanged() couldn't connect"); 85 } 86 } 87 } 88 89 /** 90 * Convenience method for callers who need to indicate that some other package 91 * needs a backup pass. This can be relevant in the case of groups of packages 92 * that share a uid, for example. 93 * 94 * This method requires that the application hold the "android.permission.BACKUP" 95 * permission if the package named in the argument is not the caller's own. 96 */ dataChanged(String packageName)97 public static void dataChanged(String packageName) { 98 if (!EVEN_THINK_ABOUT_DOING_RESTORE) { 99 return; 100 } 101 checkServiceBinder(); 102 if (sService != null) { 103 try { 104 sService.dataChanged(packageName); 105 } catch (RemoteException e) { 106 Log.d(TAG, "dataChanged(pkg) couldn't connect"); 107 } 108 } 109 } 110 111 /** 112 * Begin the process of restoring system data from backup. This method requires 113 * that the application hold the "android.permission.BACKUP" permission, and is 114 * not public. 115 * 116 * {@hide} 117 */ beginRestoreSession(String transport)118 public IRestoreSession beginRestoreSession(String transport) { 119 if (!EVEN_THINK_ABOUT_DOING_RESTORE) { 120 return null; 121 } 122 IRestoreSession binder = null; 123 checkServiceBinder(); 124 if (sService != null) { 125 try { 126 binder = sService.beginRestoreSession(transport); 127 } catch (RemoteException e) { 128 Log.d(TAG, "beginRestoreSession() couldn't connect"); 129 } 130 } 131 return binder; 132 } 133 } 134