BackupManager.java revision d17da43c82c4edb97514d6138bc208eeba321636
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
17package android.app.backup;
18
19import android.app.backup.RestoreSession;
20import android.app.backup.IBackupManager;
21import android.app.backup.IRestoreSession;
22import android.content.Context;
23import android.os.RemoteException;
24import android.os.ServiceManager;
25import android.util.Log;
26
27/**
28 * 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.
31 * <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>
38 * 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
40 * documentation for {@link android.app.backup.BackupAgent} for a detailed description
41 * of how the operation then proceeds.
42 * <p>
43 * Several attributes affecting the operation of the backup and restore mechanism
44 * can be set on the <code><a
45 * href="{@docRoot}guide/topics/manifest/application-element.html">&lt;application&gt;</a></code>
46 * tag in your application's AndroidManifest.xml file.
47 *
48 * @attr ref android.R.styleable#AndroidManifestApplication_allowBackup
49 * @attr ref android.R.styleable#AndroidManifestApplication_backupAgent
50 * @attr ref android.R.styleable#AndroidManifestApplication_killAfterRestore
51 * @attr ref android.R.styleable#AndroidManifestApplication_restoreAnyVersion
52 */
53public class BackupManager {
54    private static final String TAG = "BackupManager";
55
56    private Context mContext;
57    private static IBackupManager sService;
58
59    private static void checkServiceBinder() {
60        if (sService == null) {
61            sService = IBackupManager.Stub.asInterface(
62                    ServiceManager.getService(Context.BACKUP_SERVICE));
63        }
64    }
65
66    /**
67     * Constructs a BackupManager object through which the application can
68     * communicate with the Android backup system.
69     *
70     * @param context The {@link android.content.Context} that was provided when
71     *                one of your application's {@link android.app.Activity Activities}
72     *                was created.
73     */
74    public BackupManager(Context context) {
75        mContext = context;
76    }
77
78    /**
79     * Notifies the Android backup system that your application wishes to back up
80     * new changes to its data.  A backup operation using your application's
81     * {@link android.app.backup.BackupAgent} subclass will be scheduled when you
82     * call this method.
83     */
84    public void dataChanged() {
85        checkServiceBinder();
86        if (sService != null) {
87            try {
88                sService.dataChanged(mContext.getPackageName());
89            } catch (RemoteException e) {
90                Log.d(TAG, "dataChanged() couldn't connect");
91            }
92        }
93    }
94
95    /**
96     * Convenience method for callers who need to indicate that some other package
97     * needs a backup pass.  This can be useful in the case of groups of packages
98     * that share a uid.
99     * <p>
100     * This method requires that the application hold the "android.permission.BACKUP"
101     * permission if the package named in the argument does not run under the same uid
102     * as the caller.
103     *
104     * @param packageName The package name identifying the application to back up.
105     */
106    public static void dataChanged(String packageName) {
107        checkServiceBinder();
108        if (sService != null) {
109            try {
110                sService.dataChanged(packageName);
111            } catch (RemoteException e) {
112                Log.d(TAG, "dataChanged(pkg) couldn't connect");
113            }
114        }
115    }
116
117    /**
118     * Restore the calling application from backup.  The data will be restored from the
119     * current backup dataset if the application has stored data there, or from
120     * the dataset used during the last full device setup operation if the current
121     * backup dataset has no matching data.  If no backup data exists for this application
122     * in either source, a nonzero value will be returned.
123     *
124     * <p>If this method returns zero (meaning success), the OS will attempt to retrieve
125     * a backed-up dataset from the remote transport, instantiate the application's
126     * backup agent, and pass the dataset to the agent's
127     * {@link android.app.backup.BackupAgent#onRestore(BackupDataInput, int, android.os.ParcelFileDescriptor) onRestore()}
128     * method.
129     *
130     * @param observer The {@link RestoreObserver} to receive callbacks during the restore
131     * operation. This must not be null.
132     *
133     * @return Zero on success; nonzero on error.
134     */
135    public int requestRestore(RestoreObserver observer) {
136        int result = -1;
137        checkServiceBinder();
138        if (sService != null) {
139            RestoreSession session = null;
140            try {
141                String transport = sService.getCurrentTransport();
142                IRestoreSession binder = sService.beginRestoreSession(transport);
143                session = new RestoreSession(mContext, binder);
144                result = session.restorePackage(mContext.getPackageName(), observer);
145            } catch (RemoteException e) {
146                Log.w(TAG, "restoreSelf() unable to contact service");
147            } finally {
148                if (session != null) {
149                    session.endRestoreSession();
150                }
151            }
152        }
153        return result;
154    }
155
156    /**
157     * Begin the process of restoring data from backup.  See the
158     * {@link android.app.backup.RestoreSession} class for documentation on that process.
159     * @hide
160     */
161    public RestoreSession beginRestoreSession() {
162        RestoreSession session = null;
163        checkServiceBinder();
164        if (sService != null) {
165            try {
166                String transport = sService.getCurrentTransport();
167                IRestoreSession binder = sService.beginRestoreSession(transport);
168                session = new RestoreSession(mContext, binder);
169            } catch (RemoteException e) {
170                Log.w(TAG, "beginRestoreSession() couldn't connect");
171            }
172        }
173        return session;
174    }
175}
176