blob: be8108c6d559ebe2faa31e0cbfb98694049dd5da [file] [log] [blame]
Christopher Tatea8bf8152009-04-30 11:36:21 -07001/*
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
Christopher Tate45281862010-03-05 15:46:30 -080017package android.app.backup;
Christopher Tatea8bf8152009-04-30 11:36:21 -070018
Christopher Tate45281862010-03-05 15:46:30 -080019import android.app.backup.RestoreSession;
20import android.app.backup.IBackupManager;
21import android.app.backup.IRestoreSession;
Christopher Tatea8bf8152009-04-30 11:36:21 -070022import android.content.Context;
23import android.os.RemoteException;
24import android.os.ServiceManager;
Christopher Tatec8daa762009-07-06 19:04:57 -070025import android.util.Log;
Christopher Tatea8bf8152009-04-30 11:36:21 -070026
27/**
Scott Maind17da432010-04-29 21:42:58 -070028 * The interface through which an application interacts with the Android backup service to
29 * request backup and restore operations.
30 * Applications instantiate it using the constructor and issue calls through that instance.
Kenny Root5a20ea12010-02-23 18:49:11 -080031 * <p>
32 * When an application has made changes to data which should be backed up, a
33 * call to {@link #dataChanged()} will notify the backup service. The system
34 * will then schedule a backup operation to occur in the near future. Repeated
35 * calls to {@link #dataChanged()} have no further effect until the backup
36 * operation actually occurs.
37 * <p>
Scott Maind17da432010-04-29 21:42:58 -070038 * A backup or restore operation for your application begins when the system launches the
39 * {@link android.app.backup.BackupAgent} subclass you've declared in your manifest. See the
Christopher Tate45281862010-03-05 15:46:30 -080040 * documentation for {@link android.app.backup.BackupAgent} for a detailed description
Christopher Tate4e14a822010-04-08 12:54:23 -070041 * of how the operation then proceeds.
Kenny Root5a20ea12010-02-23 18:49:11 -080042 * <p>
Christopher Tate4e14a822010-04-08 12:54:23 -070043 * Several attributes affecting the operation of the backup and restore mechanism
Joe Fernandez61fd1e82011-10-26 13:39:11 -070044 * can be set on the <code>
45 * <a href="{@docRoot}guide/topics/manifest/application-element.html">&lt;application&gt;</a></code>
Scott Maind17da432010-04-29 21:42:58 -070046 * tag in your application's AndroidManifest.xml file.
47 *
Joe Fernandez61fd1e82011-10-26 13:39:11 -070048 * <div class="special reference">
49 * <h3>Developer Guides</h3>
50 * <p>For more information about using BackupManager, read the
51 * <a href="{@docRoot}guide/topics/data/backup.html">Data Backup</a> developer guide.</p></div>
52 *
Kenny Root5a20ea12010-02-23 18:49:11 -080053 * @attr ref android.R.styleable#AndroidManifestApplication_allowBackup
54 * @attr ref android.R.styleable#AndroidManifestApplication_backupAgent
Kenny Root5a20ea12010-02-23 18:49:11 -080055 * @attr ref android.R.styleable#AndroidManifestApplication_killAfterRestore
Christopher Tate4e14a822010-04-08 12:54:23 -070056 * @attr ref android.R.styleable#AndroidManifestApplication_restoreAnyVersion
Christopher Tatea8bf8152009-04-30 11:36:21 -070057 */
58public class BackupManager {
Christopher Tatec8daa762009-07-06 19:04:57 -070059 private static final String TAG = "BackupManager";
Christopher Tatea8bf8152009-04-30 11:36:21 -070060
Christopher Tatec8daa762009-07-06 19:04:57 -070061 private Context mContext;
62 private static IBackupManager sService;
63
64 private static void checkServiceBinder() {
65 if (sService == null) {
66 sService = IBackupManager.Stub.asInterface(
67 ServiceManager.getService(Context.BACKUP_SERVICE));
68 }
69 }
Christopher Tate043dadc2009-06-02 16:11:00 -070070
71 /**
Christopher Tatea8bf8152009-04-30 11:36:21 -070072 * Constructs a BackupManager object through which the application can
73 * communicate with the Android backup system.
Christopher Tatec114eb52009-04-30 12:40:19 -070074 *
Christopher Tatea8bf8152009-04-30 11:36:21 -070075 * @param context The {@link android.content.Context} that was provided when
76 * one of your application's {@link android.app.Activity Activities}
77 * was created.
78 */
Christopher Tatec114eb52009-04-30 12:40:19 -070079 public BackupManager(Context context) {
Christopher Tatea8bf8152009-04-30 11:36:21 -070080 mContext = context;
Christopher Tatea8bf8152009-04-30 11:36:21 -070081 }
82
83 /**
84 * Notifies the Android backup system that your application wishes to back up
85 * new changes to its data. A backup operation using your application's
Christopher Tate4e14a822010-04-08 12:54:23 -070086 * {@link android.app.backup.BackupAgent} subclass will be scheduled when you
87 * call this method.
Christopher Tatea8bf8152009-04-30 11:36:21 -070088 */
89 public void dataChanged() {
Christopher Tatec8daa762009-07-06 19:04:57 -070090 checkServiceBinder();
91 if (sService != null) {
Christopher Tate8a27f922009-06-26 11:49:18 -070092 try {
Christopher Tatec8daa762009-07-06 19:04:57 -070093 sService.dataChanged(mContext.getPackageName());
Christopher Tate8a27f922009-06-26 11:49:18 -070094 } catch (RemoteException e) {
Christopher Tatec8daa762009-07-06 19:04:57 -070095 Log.d(TAG, "dataChanged() couldn't connect");
96 }
97 }
98 }
99
100 /**
101 * Convenience method for callers who need to indicate that some other package
Christopher Tate4e14a822010-04-08 12:54:23 -0700102 * needs a backup pass. This can be useful in the case of groups of packages
103 * that share a uid.
104 * <p>
Christopher Tatec8daa762009-07-06 19:04:57 -0700105 * This method requires that the application hold the "android.permission.BACKUP"
Christopher Tate4e14a822010-04-08 12:54:23 -0700106 * permission if the package named in the argument does not run under the same uid
107 * as the caller.
Scott Maind17da432010-04-29 21:42:58 -0700108 *
109 * @param packageName The package name identifying the application to back up.
Christopher Tatec8daa762009-07-06 19:04:57 -0700110 */
111 public static void dataChanged(String packageName) {
112 checkServiceBinder();
113 if (sService != null) {
114 try {
115 sService.dataChanged(packageName);
116 } catch (RemoteException e) {
117 Log.d(TAG, "dataChanged(pkg) couldn't connect");
Christopher Tate8a27f922009-06-26 11:49:18 -0700118 }
Christopher Tatea8bf8152009-04-30 11:36:21 -0700119 }
120 }
Christopher Tate9b3905c2009-06-08 15:24:01 -0700121
122 /**
Christopher Tate9c3cee92010-03-25 16:06:43 -0700123 * Restore the calling application from backup. The data will be restored from the
124 * current backup dataset if the application has stored data there, or from
125 * the dataset used during the last full device setup operation if the current
126 * backup dataset has no matching data. If no backup data exists for this application
127 * in either source, a nonzero value will be returned.
128 *
129 * <p>If this method returns zero (meaning success), the OS will attempt to retrieve
130 * a backed-up dataset from the remote transport, instantiate the application's
131 * backup agent, and pass the dataset to the agent's
132 * {@link android.app.backup.BackupAgent#onRestore(BackupDataInput, int, android.os.ParcelFileDescriptor) onRestore()}
133 * method.
134 *
Scott Maind17da432010-04-29 21:42:58 -0700135 * @param observer The {@link RestoreObserver} to receive callbacks during the restore
136 * operation. This must not be null.
137 *
Christopher Tate9c3cee92010-03-25 16:06:43 -0700138 * @return Zero on success; nonzero on error.
139 */
140 public int requestRestore(RestoreObserver observer) {
141 int result = -1;
142 checkServiceBinder();
143 if (sService != null) {
144 RestoreSession session = null;
145 try {
Chris Tate44ab8452010-11-16 15:10:49 -0800146 IRestoreSession binder = sService.beginRestoreSession(mContext.getPackageName(),
147 null);
Christopher Tatef5491fc2012-05-25 14:14:49 -0700148 if (binder != null) {
149 session = new RestoreSession(mContext, binder);
150 result = session.restorePackage(mContext.getPackageName(), observer);
151 }
Christopher Tate9c3cee92010-03-25 16:06:43 -0700152 } catch (RemoteException e) {
153 Log.w(TAG, "restoreSelf() unable to contact service");
154 } finally {
155 if (session != null) {
156 session.endRestoreSession();
157 }
158 }
159 }
160 return result;
161 }
162
163 /**
Christopher Tatee28290e2010-02-16 15:22:26 -0800164 * Begin the process of restoring data from backup. See the
Christopher Tate45281862010-03-05 15:46:30 -0800165 * {@link android.app.backup.RestoreSession} class for documentation on that process.
Christopher Tate9c3cee92010-03-25 16:06:43 -0700166 * @hide
Christopher Tate9b3905c2009-06-08 15:24:01 -0700167 */
Christopher Tate80202c82010-01-25 19:37:47 -0800168 public RestoreSession beginRestoreSession() {
Christopher Tate80202c82010-01-25 19:37:47 -0800169 RestoreSession session = null;
Christopher Tatec8daa762009-07-06 19:04:57 -0700170 checkServiceBinder();
171 if (sService != null) {
Christopher Tate8a27f922009-06-26 11:49:18 -0700172 try {
Chris Tate44ab8452010-11-16 15:10:49 -0800173 // All packages, current transport
174 IRestoreSession binder = sService.beginRestoreSession(null, null);
Christopher Tatef5491fc2012-05-25 14:14:49 -0700175 if (binder != null) {
176 session = new RestoreSession(mContext, binder);
177 }
Christopher Tate8a27f922009-06-26 11:49:18 -0700178 } catch (RemoteException e) {
Christopher Tatee28290e2010-02-16 15:22:26 -0800179 Log.w(TAG, "beginRestoreSession() couldn't connect");
Christopher Tate8a27f922009-06-26 11:49:18 -0700180 }
Christopher Tate9b3905c2009-06-08 15:24:01 -0700181 }
Christopher Tate80202c82010-01-25 19:37:47 -0800182 return session;
Christopher Tate9b3905c2009-06-08 15:24:01 -0700183 }
Christopher Tatea8bf8152009-04-30 11:36:21 -0700184}