BackupManager.java revision 2e5979236ccc06beec8b8f7f631f31bdedc79614
1a8bf815c6153290b173f34b071dddb0a0034a115Christopher Tate/*
2a8bf815c6153290b173f34b071dddb0a0034a115Christopher Tate * Copyright (C) 2009 The Android Open Source Project
3a8bf815c6153290b173f34b071dddb0a0034a115Christopher Tate *
4a8bf815c6153290b173f34b071dddb0a0034a115Christopher Tate * Licensed under the Apache License, Version 2.0 (the "License");
5a8bf815c6153290b173f34b071dddb0a0034a115Christopher Tate * you may not use this file except in compliance with the License.
6a8bf815c6153290b173f34b071dddb0a0034a115Christopher Tate * You may obtain a copy of the License at
7a8bf815c6153290b173f34b071dddb0a0034a115Christopher Tate *
8a8bf815c6153290b173f34b071dddb0a0034a115Christopher Tate *      http://www.apache.org/licenses/LICENSE-2.0
9a8bf815c6153290b173f34b071dddb0a0034a115Christopher Tate *
10a8bf815c6153290b173f34b071dddb0a0034a115Christopher Tate * Unless required by applicable law or agreed to in writing, software
11a8bf815c6153290b173f34b071dddb0a0034a115Christopher Tate * distributed under the License is distributed on an "AS IS" BASIS,
12a8bf815c6153290b173f34b071dddb0a0034a115Christopher Tate * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13a8bf815c6153290b173f34b071dddb0a0034a115Christopher Tate * See the License for the specific language governing permissions and
14a8bf815c6153290b173f34b071dddb0a0034a115Christopher Tate * limitations under the License.
15a8bf815c6153290b173f34b071dddb0a0034a115Christopher Tate */
16a8bf815c6153290b173f34b071dddb0a0034a115Christopher Tate
174528186e0d65fc68ef0dd1941aa2ac8aefcd55a3Christopher Tatepackage android.app.backup;
18a8bf815c6153290b173f34b071dddb0a0034a115Christopher Tate
19d5cf722ae62d06d1fb6a9505c6f4c403a5d14a37Christopher Tateimport android.annotation.SystemApi;
204528186e0d65fc68ef0dd1941aa2ac8aefcd55a3Christopher Tateimport android.app.backup.RestoreSession;
214528186e0d65fc68ef0dd1941aa2ac8aefcd55a3Christopher Tateimport android.app.backup.IBackupManager;
224528186e0d65fc68ef0dd1941aa2ac8aefcd55a3Christopher Tateimport android.app.backup.IRestoreSession;
23a8bf815c6153290b173f34b071dddb0a0034a115Christopher Tateimport android.content.Context;
24a8bf815c6153290b173f34b071dddb0a0034a115Christopher Tateimport android.os.RemoteException;
25a8bf815c6153290b173f34b071dddb0a0034a115Christopher Tateimport android.os.ServiceManager;
26c8daa769256b039b6bc4c5acbe6b558cd776c00aChristopher Tateimport android.util.Log;
27a8bf815c6153290b173f34b071dddb0a0034a115Christopher Tate
28a8bf815c6153290b173f34b071dddb0a0034a115Christopher Tate/**
29d17da43c82c4edb97514d6138bc208eeba321636Scott Main * The interface through which an application interacts with the Android backup service to
30d17da43c82c4edb97514d6138bc208eeba321636Scott Main * request backup and restore operations.
31d17da43c82c4edb97514d6138bc208eeba321636Scott Main * Applications instantiate it using the constructor and issue calls through that instance.
325a20ea16d7e5b70dc7bad8700f54170e4f220d12Kenny Root * <p>
335a20ea16d7e5b70dc7bad8700f54170e4f220d12Kenny Root * When an application has made changes to data which should be backed up, a
345a20ea16d7e5b70dc7bad8700f54170e4f220d12Kenny Root * call to {@link #dataChanged()} will notify the backup service. The system
355a20ea16d7e5b70dc7bad8700f54170e4f220d12Kenny Root * will then schedule a backup operation to occur in the near future. Repeated
365a20ea16d7e5b70dc7bad8700f54170e4f220d12Kenny Root * calls to {@link #dataChanged()} have no further effect until the backup
375a20ea16d7e5b70dc7bad8700f54170e4f220d12Kenny Root * operation actually occurs.
385a20ea16d7e5b70dc7bad8700f54170e4f220d12Kenny Root * <p>
39d17da43c82c4edb97514d6138bc208eeba321636Scott Main * A backup or restore operation for your application begins when the system launches the
40d17da43c82c4edb97514d6138bc208eeba321636Scott Main * {@link android.app.backup.BackupAgent} subclass you've declared in your manifest. See the
414528186e0d65fc68ef0dd1941aa2ac8aefcd55a3Christopher Tate * documentation for {@link android.app.backup.BackupAgent} for a detailed description
424e14a829129feee14ebe453f61a124784c870610Christopher Tate * of how the operation then proceeds.
435a20ea16d7e5b70dc7bad8700f54170e4f220d12Kenny Root * <p>
444e14a829129feee14ebe453f61a124784c870610Christopher Tate * Several attributes affecting the operation of the backup and restore mechanism
4561fd1e8d8c3ccf2d6b7d4af1c19e8f0988d5a1ecJoe Fernandez * can be set on the <code>
4661fd1e8d8c3ccf2d6b7d4af1c19e8f0988d5a1ecJoe Fernandez * <a href="{@docRoot}guide/topics/manifest/application-element.html">&lt;application&gt;</a></code>
47d17da43c82c4edb97514d6138bc208eeba321636Scott Main * tag in your application's AndroidManifest.xml file.
48d17da43c82c4edb97514d6138bc208eeba321636Scott Main *
4961fd1e8d8c3ccf2d6b7d4af1c19e8f0988d5a1ecJoe Fernandez * <div class="special reference">
5061fd1e8d8c3ccf2d6b7d4af1c19e8f0988d5a1ecJoe Fernandez * <h3>Developer Guides</h3>
5161fd1e8d8c3ccf2d6b7d4af1c19e8f0988d5a1ecJoe Fernandez * <p>For more information about using BackupManager, read the
5261fd1e8d8c3ccf2d6b7d4af1c19e8f0988d5a1ecJoe Fernandez * <a href="{@docRoot}guide/topics/data/backup.html">Data Backup</a> developer guide.</p></div>
5361fd1e8d8c3ccf2d6b7d4af1c19e8f0988d5a1ecJoe Fernandez *
545a20ea16d7e5b70dc7bad8700f54170e4f220d12Kenny Root * @attr ref android.R.styleable#AndroidManifestApplication_allowBackup
555a20ea16d7e5b70dc7bad8700f54170e4f220d12Kenny Root * @attr ref android.R.styleable#AndroidManifestApplication_backupAgent
565a20ea16d7e5b70dc7bad8700f54170e4f220d12Kenny Root * @attr ref android.R.styleable#AndroidManifestApplication_killAfterRestore
574e14a829129feee14ebe453f61a124784c870610Christopher Tate * @attr ref android.R.styleable#AndroidManifestApplication_restoreAnyVersion
58a8bf815c6153290b173f34b071dddb0a0034a115Christopher Tate */
59a8bf815c6153290b173f34b071dddb0a0034a115Christopher Tatepublic class BackupManager {
60c8daa769256b039b6bc4c5acbe6b558cd776c00aChristopher Tate    private static final String TAG = "BackupManager";
61c8daa769256b039b6bc4c5acbe6b558cd776c00aChristopher Tate
62a8bf815c6153290b173f34b071dddb0a0034a115Christopher Tate    private Context mContext;
63c8daa769256b039b6bc4c5acbe6b558cd776c00aChristopher Tate    private static IBackupManager sService;
64a8bf815c6153290b173f34b071dddb0a0034a115Christopher Tate
65c8daa769256b039b6bc4c5acbe6b558cd776c00aChristopher Tate    private static void checkServiceBinder() {
66c8daa769256b039b6bc4c5acbe6b558cd776c00aChristopher Tate        if (sService == null) {
67c8daa769256b039b6bc4c5acbe6b558cd776c00aChristopher Tate            sService = IBackupManager.Stub.asInterface(
68c8daa769256b039b6bc4c5acbe6b558cd776c00aChristopher Tate                    ServiceManager.getService(Context.BACKUP_SERVICE));
69c8daa769256b039b6bc4c5acbe6b558cd776c00aChristopher Tate        }
70c8daa769256b039b6bc4c5acbe6b558cd776c00aChristopher Tate    }
71043dadc7516d20c3b3ccbcb20c53aaeef076a237Christopher Tate
72043dadc7516d20c3b3ccbcb20c53aaeef076a237Christopher Tate    /**
73a8bf815c6153290b173f34b071dddb0a0034a115Christopher Tate     * Constructs a BackupManager object through which the application can
74a8bf815c6153290b173f34b071dddb0a0034a115Christopher Tate     * communicate with the Android backup system.
75c114eb55b442981e2ea0a8989aa6ed458fc418e4Christopher Tate     *
76a8bf815c6153290b173f34b071dddb0a0034a115Christopher Tate     * @param context The {@link android.content.Context} that was provided when
77a8bf815c6153290b173f34b071dddb0a0034a115Christopher Tate     *                one of your application's {@link android.app.Activity Activities}
78a8bf815c6153290b173f34b071dddb0a0034a115Christopher Tate     *                was created.
79a8bf815c6153290b173f34b071dddb0a0034a115Christopher Tate     */
80c114eb55b442981e2ea0a8989aa6ed458fc418e4Christopher Tate    public BackupManager(Context context) {
81a8bf815c6153290b173f34b071dddb0a0034a115Christopher Tate        mContext = context;
82a8bf815c6153290b173f34b071dddb0a0034a115Christopher Tate    }
83a8bf815c6153290b173f34b071dddb0a0034a115Christopher Tate
84a8bf815c6153290b173f34b071dddb0a0034a115Christopher Tate    /**
85a8bf815c6153290b173f34b071dddb0a0034a115Christopher Tate     * Notifies the Android backup system that your application wishes to back up
86a8bf815c6153290b173f34b071dddb0a0034a115Christopher Tate     * new changes to its data.  A backup operation using your application's
874e14a829129feee14ebe453f61a124784c870610Christopher Tate     * {@link android.app.backup.BackupAgent} subclass will be scheduled when you
884e14a829129feee14ebe453f61a124784c870610Christopher Tate     * call this method.
89a8bf815c6153290b173f34b071dddb0a0034a115Christopher Tate     */
90a8bf815c6153290b173f34b071dddb0a0034a115Christopher Tate    public void dataChanged() {
91c8daa769256b039b6bc4c5acbe6b558cd776c00aChristopher Tate        checkServiceBinder();
92c8daa769256b039b6bc4c5acbe6b558cd776c00aChristopher Tate        if (sService != null) {
93c8daa769256b039b6bc4c5acbe6b558cd776c00aChristopher Tate            try {
94c8daa769256b039b6bc4c5acbe6b558cd776c00aChristopher Tate                sService.dataChanged(mContext.getPackageName());
95c8daa769256b039b6bc4c5acbe6b558cd776c00aChristopher Tate            } catch (RemoteException e) {
96c8daa769256b039b6bc4c5acbe6b558cd776c00aChristopher Tate                Log.d(TAG, "dataChanged() couldn't connect");
97c8daa769256b039b6bc4c5acbe6b558cd776c00aChristopher Tate            }
98c8daa769256b039b6bc4c5acbe6b558cd776c00aChristopher Tate        }
99c8daa769256b039b6bc4c5acbe6b558cd776c00aChristopher Tate    }
100c8daa769256b039b6bc4c5acbe6b558cd776c00aChristopher Tate
101c8daa769256b039b6bc4c5acbe6b558cd776c00aChristopher Tate    /**
102c8daa769256b039b6bc4c5acbe6b558cd776c00aChristopher Tate     * Convenience method for callers who need to indicate that some other package
1034e14a829129feee14ebe453f61a124784c870610Christopher Tate     * needs a backup pass.  This can be useful in the case of groups of packages
1044e14a829129feee14ebe453f61a124784c870610Christopher Tate     * that share a uid.
1054e14a829129feee14ebe453f61a124784c870610Christopher Tate     * <p>
106c8daa769256b039b6bc4c5acbe6b558cd776c00aChristopher Tate     * This method requires that the application hold the "android.permission.BACKUP"
1074e14a829129feee14ebe453f61a124784c870610Christopher Tate     * permission if the package named in the argument does not run under the same uid
1084e14a829129feee14ebe453f61a124784c870610Christopher Tate     * as the caller.
109d17da43c82c4edb97514d6138bc208eeba321636Scott Main     *
110d17da43c82c4edb97514d6138bc208eeba321636Scott Main     * @param packageName The package name identifying the application to back up.
111c8daa769256b039b6bc4c5acbe6b558cd776c00aChristopher Tate     */
112c8daa769256b039b6bc4c5acbe6b558cd776c00aChristopher Tate    public static void dataChanged(String packageName) {
113c8daa769256b039b6bc4c5acbe6b558cd776c00aChristopher Tate        checkServiceBinder();
114c8daa769256b039b6bc4c5acbe6b558cd776c00aChristopher Tate        if (sService != null) {
1158a27f923eb9dbbe3c2d0184e82d9f1a98f1e4cdcChristopher Tate            try {
116c8daa769256b039b6bc4c5acbe6b558cd776c00aChristopher Tate                sService.dataChanged(packageName);
1178a27f923eb9dbbe3c2d0184e82d9f1a98f1e4cdcChristopher Tate            } catch (RemoteException e) {
118d5cf722ae62d06d1fb6a9505c6f4c403a5d14a37Christopher Tate                Log.e(TAG, "dataChanged(pkg) couldn't connect");
1198a27f923eb9dbbe3c2d0184e82d9f1a98f1e4cdcChristopher Tate            }
120a8bf815c6153290b173f34b071dddb0a0034a115Christopher Tate        }
121a8bf815c6153290b173f34b071dddb0a0034a115Christopher Tate    }
1229b3905c4a25f2d785ce7535d1f2e1540b46bb561Christopher Tate
1239b3905c4a25f2d785ce7535d1f2e1540b46bb561Christopher Tate    /**
1249c3cee9824026764275e4d84ba9b5d9fdc5da690Christopher Tate     * Restore the calling application from backup.  The data will be restored from the
1259c3cee9824026764275e4d84ba9b5d9fdc5da690Christopher Tate     * current backup dataset if the application has stored data there, or from
1269c3cee9824026764275e4d84ba9b5d9fdc5da690Christopher Tate     * the dataset used during the last full device setup operation if the current
1279c3cee9824026764275e4d84ba9b5d9fdc5da690Christopher Tate     * backup dataset has no matching data.  If no backup data exists for this application
1289c3cee9824026764275e4d84ba9b5d9fdc5da690Christopher Tate     * in either source, a nonzero value will be returned.
1299c3cee9824026764275e4d84ba9b5d9fdc5da690Christopher Tate     *
1309c3cee9824026764275e4d84ba9b5d9fdc5da690Christopher Tate     * <p>If this method returns zero (meaning success), the OS will attempt to retrieve
1319c3cee9824026764275e4d84ba9b5d9fdc5da690Christopher Tate     * a backed-up dataset from the remote transport, instantiate the application's
1329c3cee9824026764275e4d84ba9b5d9fdc5da690Christopher Tate     * backup agent, and pass the dataset to the agent's
1339c3cee9824026764275e4d84ba9b5d9fdc5da690Christopher Tate     * {@link android.app.backup.BackupAgent#onRestore(BackupDataInput, int, android.os.ParcelFileDescriptor) onRestore()}
1349c3cee9824026764275e4d84ba9b5d9fdc5da690Christopher Tate     * method.
1359c3cee9824026764275e4d84ba9b5d9fdc5da690Christopher Tate     *
136d17da43c82c4edb97514d6138bc208eeba321636Scott Main     * @param observer The {@link RestoreObserver} to receive callbacks during the restore
137d17da43c82c4edb97514d6138bc208eeba321636Scott Main     * operation. This must not be null.
138d17da43c82c4edb97514d6138bc208eeba321636Scott Main     *
1399c3cee9824026764275e4d84ba9b5d9fdc5da690Christopher Tate     * @return Zero on success; nonzero on error.
1409c3cee9824026764275e4d84ba9b5d9fdc5da690Christopher Tate     */
1419c3cee9824026764275e4d84ba9b5d9fdc5da690Christopher Tate    public int requestRestore(RestoreObserver observer) {
1429c3cee9824026764275e4d84ba9b5d9fdc5da690Christopher Tate        int result = -1;
1439c3cee9824026764275e4d84ba9b5d9fdc5da690Christopher Tate        checkServiceBinder();
1449c3cee9824026764275e4d84ba9b5d9fdc5da690Christopher Tate        if (sService != null) {
1459c3cee9824026764275e4d84ba9b5d9fdc5da690Christopher Tate            RestoreSession session = null;
1469c3cee9824026764275e4d84ba9b5d9fdc5da690Christopher Tate            try {
14744ab8453e1c4c46790f792a46d026fa1017d8cfeChris Tate                IRestoreSession binder = sService.beginRestoreSession(mContext.getPackageName(),
14844ab8453e1c4c46790f792a46d026fa1017d8cfeChris Tate                        null);
149f5491fc1b61088843f280a6b55c1a995e2e6f939Christopher Tate                if (binder != null) {
150f5491fc1b61088843f280a6b55c1a995e2e6f939Christopher Tate                    session = new RestoreSession(mContext, binder);
151f5491fc1b61088843f280a6b55c1a995e2e6f939Christopher Tate                    result = session.restorePackage(mContext.getPackageName(), observer);
152f5491fc1b61088843f280a6b55c1a995e2e6f939Christopher Tate                }
1539c3cee9824026764275e4d84ba9b5d9fdc5da690Christopher Tate            } catch (RemoteException e) {
154d5cf722ae62d06d1fb6a9505c6f4c403a5d14a37Christopher Tate                Log.e(TAG, "restoreSelf() unable to contact service");
1559c3cee9824026764275e4d84ba9b5d9fdc5da690Christopher Tate            } finally {
1569c3cee9824026764275e4d84ba9b5d9fdc5da690Christopher Tate                if (session != null) {
1579c3cee9824026764275e4d84ba9b5d9fdc5da690Christopher Tate                    session.endRestoreSession();
1589c3cee9824026764275e4d84ba9b5d9fdc5da690Christopher Tate                }
1599c3cee9824026764275e4d84ba9b5d9fdc5da690Christopher Tate            }
1609c3cee9824026764275e4d84ba9b5d9fdc5da690Christopher Tate        }
1619c3cee9824026764275e4d84ba9b5d9fdc5da690Christopher Tate        return result;
1629c3cee9824026764275e4d84ba9b5d9fdc5da690Christopher Tate    }
1639c3cee9824026764275e4d84ba9b5d9fdc5da690Christopher Tate
1642e5979236ccc06beec8b8f7f631f31bdedc79614Christopher Tate    // system APIs start here
1652e5979236ccc06beec8b8f7f631f31bdedc79614Christopher Tate
1669c3cee9824026764275e4d84ba9b5d9fdc5da690Christopher Tate    /**
167e28290e21f908b4e917099ff2aa41e3aab9310c2Christopher Tate     * Begin the process of restoring data from backup.  See the
1684528186e0d65fc68ef0dd1941aa2ac8aefcd55a3Christopher Tate     * {@link android.app.backup.RestoreSession} class for documentation on that process.
1699c3cee9824026764275e4d84ba9b5d9fdc5da690Christopher Tate     * @hide
1709b3905c4a25f2d785ce7535d1f2e1540b46bb561Christopher Tate     */
1712e5979236ccc06beec8b8f7f631f31bdedc79614Christopher Tate    @SystemApi
17280202c8cb8c8e4ab507079e79b864c61a8eeeee9Christopher Tate    public RestoreSession beginRestoreSession() {
17380202c8cb8c8e4ab507079e79b864c61a8eeeee9Christopher Tate        RestoreSession session = null;
174c8daa769256b039b6bc4c5acbe6b558cd776c00aChristopher Tate        checkServiceBinder();
175c8daa769256b039b6bc4c5acbe6b558cd776c00aChristopher Tate        if (sService != null) {
1768a27f923eb9dbbe3c2d0184e82d9f1a98f1e4cdcChristopher Tate            try {
17744ab8453e1c4c46790f792a46d026fa1017d8cfeChris Tate                // All packages, current transport
17844ab8453e1c4c46790f792a46d026fa1017d8cfeChris Tate                IRestoreSession binder = sService.beginRestoreSession(null, null);
179f5491fc1b61088843f280a6b55c1a995e2e6f939Christopher Tate                if (binder != null) {
180f5491fc1b61088843f280a6b55c1a995e2e6f939Christopher Tate                    session = new RestoreSession(mContext, binder);
181f5491fc1b61088843f280a6b55c1a995e2e6f939Christopher Tate                }
1828a27f923eb9dbbe3c2d0184e82d9f1a98f1e4cdcChristopher Tate            } catch (RemoteException e) {
183d5cf722ae62d06d1fb6a9505c6f4c403a5d14a37Christopher Tate                Log.e(TAG, "beginRestoreSession() couldn't connect");
1848a27f923eb9dbbe3c2d0184e82d9f1a98f1e4cdcChristopher Tate            }
1859b3905c4a25f2d785ce7535d1f2e1540b46bb561Christopher Tate        }
18680202c8cb8c8e4ab507079e79b864c61a8eeeee9Christopher Tate        return session;
1879b3905c4a25f2d785ce7535d1f2e1540b46bb561Christopher Tate    }
188d5cf722ae62d06d1fb6a9505c6f4c403a5d14a37Christopher Tate
189d5cf722ae62d06d1fb6a9505c6f4c403a5d14a37Christopher Tate    /**
190d5cf722ae62d06d1fb6a9505c6f4c403a5d14a37Christopher Tate     * Enable/disable the backup service entirely.  When disabled, no backup
191d5cf722ae62d06d1fb6a9505c6f4c403a5d14a37Christopher Tate     * or restore operations will take place.  Data-changed notifications will
192d5cf722ae62d06d1fb6a9505c6f4c403a5d14a37Christopher Tate     * still be observed and collected, however, so that changes made while the
193d5cf722ae62d06d1fb6a9505c6f4c403a5d14a37Christopher Tate     * mechanism was disabled will still be backed up properly if it is enabled
194d5cf722ae62d06d1fb6a9505c6f4c403a5d14a37Christopher Tate     * at some point in the future.
195d5cf722ae62d06d1fb6a9505c6f4c403a5d14a37Christopher Tate     *
196d5cf722ae62d06d1fb6a9505c6f4c403a5d14a37Christopher Tate     * <p>Callers must hold the android.permission.BACKUP permission to use this method.
197d5cf722ae62d06d1fb6a9505c6f4c403a5d14a37Christopher Tate     *
198d5cf722ae62d06d1fb6a9505c6f4c403a5d14a37Christopher Tate     * @hide
199d5cf722ae62d06d1fb6a9505c6f4c403a5d14a37Christopher Tate     */
200d5cf722ae62d06d1fb6a9505c6f4c403a5d14a37Christopher Tate    @SystemApi
201d5cf722ae62d06d1fb6a9505c6f4c403a5d14a37Christopher Tate    public void setBackupEnabled(boolean isEnabled) {
202d5cf722ae62d06d1fb6a9505c6f4c403a5d14a37Christopher Tate        checkServiceBinder();
203d5cf722ae62d06d1fb6a9505c6f4c403a5d14a37Christopher Tate        if (sService != null) {
204d5cf722ae62d06d1fb6a9505c6f4c403a5d14a37Christopher Tate            try {
205d5cf722ae62d06d1fb6a9505c6f4c403a5d14a37Christopher Tate                sService.setBackupEnabled(isEnabled);
206d5cf722ae62d06d1fb6a9505c6f4c403a5d14a37Christopher Tate            } catch (RemoteException e) {
207d5cf722ae62d06d1fb6a9505c6f4c403a5d14a37Christopher Tate                Log.e(TAG, "setBackupEnabled() couldn't connect");
208d5cf722ae62d06d1fb6a9505c6f4c403a5d14a37Christopher Tate            }
209d5cf722ae62d06d1fb6a9505c6f4c403a5d14a37Christopher Tate        }
210d5cf722ae62d06d1fb6a9505c6f4c403a5d14a37Christopher Tate    }
211d5cf722ae62d06d1fb6a9505c6f4c403a5d14a37Christopher Tate
212d5cf722ae62d06d1fb6a9505c6f4c403a5d14a37Christopher Tate    /**
213d5cf722ae62d06d1fb6a9505c6f4c403a5d14a37Christopher Tate     * Report whether the backup mechanism is currently enabled.
214d5cf722ae62d06d1fb6a9505c6f4c403a5d14a37Christopher Tate     *
215d5cf722ae62d06d1fb6a9505c6f4c403a5d14a37Christopher Tate     * <p>Callers must hold the android.permission.BACKUP permission to use this method.
216d5cf722ae62d06d1fb6a9505c6f4c403a5d14a37Christopher Tate     *
217d5cf722ae62d06d1fb6a9505c6f4c403a5d14a37Christopher Tate     * @hide
218d5cf722ae62d06d1fb6a9505c6f4c403a5d14a37Christopher Tate     */
219d5cf722ae62d06d1fb6a9505c6f4c403a5d14a37Christopher Tate    @SystemApi
220d5cf722ae62d06d1fb6a9505c6f4c403a5d14a37Christopher Tate    public boolean isBackupEnabled() {
221d5cf722ae62d06d1fb6a9505c6f4c403a5d14a37Christopher Tate        if (sService != null) {
222d5cf722ae62d06d1fb6a9505c6f4c403a5d14a37Christopher Tate            try {
223d5cf722ae62d06d1fb6a9505c6f4c403a5d14a37Christopher Tate                return sService.isBackupEnabled();
224d5cf722ae62d06d1fb6a9505c6f4c403a5d14a37Christopher Tate            } catch (RemoteException e) {
225d5cf722ae62d06d1fb6a9505c6f4c403a5d14a37Christopher Tate                Log.e(TAG, "isBackupEnabled() couldn't connect");
226d5cf722ae62d06d1fb6a9505c6f4c403a5d14a37Christopher Tate            }
227d5cf722ae62d06d1fb6a9505c6f4c403a5d14a37Christopher Tate        }
228d5cf722ae62d06d1fb6a9505c6f4c403a5d14a37Christopher Tate        return false;
229d5cf722ae62d06d1fb6a9505c6f4c403a5d14a37Christopher Tate    }
230d5cf722ae62d06d1fb6a9505c6f4c403a5d14a37Christopher Tate
231d5cf722ae62d06d1fb6a9505c6f4c403a5d14a37Christopher Tate    /**
232d5cf722ae62d06d1fb6a9505c6f4c403a5d14a37Christopher Tate     * Identify the currently selected transport.  Callers must hold the
233d5cf722ae62d06d1fb6a9505c6f4c403a5d14a37Christopher Tate     * android.permission.BACKUP permission to use this method.
234d5cf722ae62d06d1fb6a9505c6f4c403a5d14a37Christopher Tate     * @return The name of the currently active backup transport.  In case of
235d5cf722ae62d06d1fb6a9505c6f4c403a5d14a37Christopher Tate     *   failure or if no transport is currently active, this method returns {@code null}.
236d5cf722ae62d06d1fb6a9505c6f4c403a5d14a37Christopher Tate     *
237d5cf722ae62d06d1fb6a9505c6f4c403a5d14a37Christopher Tate     * @hide
238d5cf722ae62d06d1fb6a9505c6f4c403a5d14a37Christopher Tate     */
239d5cf722ae62d06d1fb6a9505c6f4c403a5d14a37Christopher Tate    @SystemApi
240d5cf722ae62d06d1fb6a9505c6f4c403a5d14a37Christopher Tate    public String getCurrentTransport() {
241d5cf722ae62d06d1fb6a9505c6f4c403a5d14a37Christopher Tate        checkServiceBinder();
242d5cf722ae62d06d1fb6a9505c6f4c403a5d14a37Christopher Tate        if (sService != null) {
243d5cf722ae62d06d1fb6a9505c6f4c403a5d14a37Christopher Tate            try {
244d5cf722ae62d06d1fb6a9505c6f4c403a5d14a37Christopher Tate                return sService.getCurrentTransport();
245d5cf722ae62d06d1fb6a9505c6f4c403a5d14a37Christopher Tate            } catch (RemoteException e) {
246d5cf722ae62d06d1fb6a9505c6f4c403a5d14a37Christopher Tate                Log.e(TAG, "getCurrentTransport() couldn't connect");
247d5cf722ae62d06d1fb6a9505c6f4c403a5d14a37Christopher Tate            }
248d5cf722ae62d06d1fb6a9505c6f4c403a5d14a37Christopher Tate        }
249d5cf722ae62d06d1fb6a9505c6f4c403a5d14a37Christopher Tate        return null;
250d5cf722ae62d06d1fb6a9505c6f4c403a5d14a37Christopher Tate    }
251d5cf722ae62d06d1fb6a9505c6f4c403a5d14a37Christopher Tate
252d5cf722ae62d06d1fb6a9505c6f4c403a5d14a37Christopher Tate    /**
253d5cf722ae62d06d1fb6a9505c6f4c403a5d14a37Christopher Tate     * Request a list of all available backup transports' names.  Callers must
254d5cf722ae62d06d1fb6a9505c6f4c403a5d14a37Christopher Tate     * hold the android.permission.BACKUP permission to use this method.
255d5cf722ae62d06d1fb6a9505c6f4c403a5d14a37Christopher Tate     *
256d5cf722ae62d06d1fb6a9505c6f4c403a5d14a37Christopher Tate     * @hide
257d5cf722ae62d06d1fb6a9505c6f4c403a5d14a37Christopher Tate     */
258d5cf722ae62d06d1fb6a9505c6f4c403a5d14a37Christopher Tate    @SystemApi
259d5cf722ae62d06d1fb6a9505c6f4c403a5d14a37Christopher Tate    public String[] listAllTransports() {
260d5cf722ae62d06d1fb6a9505c6f4c403a5d14a37Christopher Tate        checkServiceBinder();
261d5cf722ae62d06d1fb6a9505c6f4c403a5d14a37Christopher Tate        if (sService != null) {
262d5cf722ae62d06d1fb6a9505c6f4c403a5d14a37Christopher Tate            try {
263d5cf722ae62d06d1fb6a9505c6f4c403a5d14a37Christopher Tate                return sService.listAllTransports();
264d5cf722ae62d06d1fb6a9505c6f4c403a5d14a37Christopher Tate            } catch (RemoteException e) {
265d5cf722ae62d06d1fb6a9505c6f4c403a5d14a37Christopher Tate                Log.e(TAG, "listAllTransports() couldn't connect");
266d5cf722ae62d06d1fb6a9505c6f4c403a5d14a37Christopher Tate            }
267d5cf722ae62d06d1fb6a9505c6f4c403a5d14a37Christopher Tate        }
268d5cf722ae62d06d1fb6a9505c6f4c403a5d14a37Christopher Tate        return null;
269d5cf722ae62d06d1fb6a9505c6f4c403a5d14a37Christopher Tate    }
270d5cf722ae62d06d1fb6a9505c6f4c403a5d14a37Christopher Tate
271d5cf722ae62d06d1fb6a9505c6f4c403a5d14a37Christopher Tate    /**
272d5cf722ae62d06d1fb6a9505c6f4c403a5d14a37Christopher Tate     * Specify the current backup transport.  Callers must hold the
273d5cf722ae62d06d1fb6a9505c6f4c403a5d14a37Christopher Tate     * android.permission.BACKUP permission to use this method.
274d5cf722ae62d06d1fb6a9505c6f4c403a5d14a37Christopher Tate     *
275d5cf722ae62d06d1fb6a9505c6f4c403a5d14a37Christopher Tate     * @param transport The name of the transport to select.  This should be one
276d5cf722ae62d06d1fb6a9505c6f4c403a5d14a37Christopher Tate     *   of the names returned by {@link #listAllTransports()}.
277d5cf722ae62d06d1fb6a9505c6f4c403a5d14a37Christopher Tate     * @return The name of the previously selected transport.  If the given transport
278d5cf722ae62d06d1fb6a9505c6f4c403a5d14a37Christopher Tate     *   name is not one of the currently available transports, no change is made to
279d5cf722ae62d06d1fb6a9505c6f4c403a5d14a37Christopher Tate     *   the current transport setting and the method returns null.
280d5cf722ae62d06d1fb6a9505c6f4c403a5d14a37Christopher Tate     *
281d5cf722ae62d06d1fb6a9505c6f4c403a5d14a37Christopher Tate     * @hide
282d5cf722ae62d06d1fb6a9505c6f4c403a5d14a37Christopher Tate     */
283d5cf722ae62d06d1fb6a9505c6f4c403a5d14a37Christopher Tate    @SystemApi
284d5cf722ae62d06d1fb6a9505c6f4c403a5d14a37Christopher Tate    public String selectBackupTransport(String transport) {
285d5cf722ae62d06d1fb6a9505c6f4c403a5d14a37Christopher Tate        checkServiceBinder();
286d5cf722ae62d06d1fb6a9505c6f4c403a5d14a37Christopher Tate        if (sService != null) {
287d5cf722ae62d06d1fb6a9505c6f4c403a5d14a37Christopher Tate            try {
288d5cf722ae62d06d1fb6a9505c6f4c403a5d14a37Christopher Tate                return sService.selectBackupTransport(transport);
289d5cf722ae62d06d1fb6a9505c6f4c403a5d14a37Christopher Tate            } catch (RemoteException e) {
290d5cf722ae62d06d1fb6a9505c6f4c403a5d14a37Christopher Tate                Log.e(TAG, "selectBackupTransport() couldn't connect");
291d5cf722ae62d06d1fb6a9505c6f4c403a5d14a37Christopher Tate            }
292d5cf722ae62d06d1fb6a9505c6f4c403a5d14a37Christopher Tate        }
293d5cf722ae62d06d1fb6a9505c6f4c403a5d14a37Christopher Tate        return null;
294d5cf722ae62d06d1fb6a9505c6f4c403a5d14a37Christopher Tate    }
295d5cf722ae62d06d1fb6a9505c6f4c403a5d14a37Christopher Tate
296d5cf722ae62d06d1fb6a9505c6f4c403a5d14a37Christopher Tate    /**
297d5cf722ae62d06d1fb6a9505c6f4c403a5d14a37Christopher Tate     * Schedule an immediate backup attempt for all pending key/value updates.  This
298d5cf722ae62d06d1fb6a9505c6f4c403a5d14a37Christopher Tate     * is primarily intended for transports to use when they detect a suitable
299d5cf722ae62d06d1fb6a9505c6f4c403a5d14a37Christopher Tate     * opportunity for doing a backup pass.  If there are no pending updates to
300d5cf722ae62d06d1fb6a9505c6f4c403a5d14a37Christopher Tate     * be sent, no action will be taken.  Even if some updates are pending, the
301d5cf722ae62d06d1fb6a9505c6f4c403a5d14a37Christopher Tate     * transport will still be asked to confirm via the usual requestBackupTime()
302d5cf722ae62d06d1fb6a9505c6f4c403a5d14a37Christopher Tate     * method.
303d5cf722ae62d06d1fb6a9505c6f4c403a5d14a37Christopher Tate     *
304d5cf722ae62d06d1fb6a9505c6f4c403a5d14a37Christopher Tate     * <p>Callers must hold the android.permission.BACKUP permission to use this method.
305d5cf722ae62d06d1fb6a9505c6f4c403a5d14a37Christopher Tate     *
306d5cf722ae62d06d1fb6a9505c6f4c403a5d14a37Christopher Tate     * @hide
307d5cf722ae62d06d1fb6a9505c6f4c403a5d14a37Christopher Tate     */
308d5cf722ae62d06d1fb6a9505c6f4c403a5d14a37Christopher Tate    @SystemApi
309d5cf722ae62d06d1fb6a9505c6f4c403a5d14a37Christopher Tate    public void backupNow() {
310d5cf722ae62d06d1fb6a9505c6f4c403a5d14a37Christopher Tate        checkServiceBinder();
311d5cf722ae62d06d1fb6a9505c6f4c403a5d14a37Christopher Tate        if (sService != null) {
312d5cf722ae62d06d1fb6a9505c6f4c403a5d14a37Christopher Tate            try {
313d5cf722ae62d06d1fb6a9505c6f4c403a5d14a37Christopher Tate                sService.backupNow();
314d5cf722ae62d06d1fb6a9505c6f4c403a5d14a37Christopher Tate            } catch (RemoteException e) {
315d5cf722ae62d06d1fb6a9505c6f4c403a5d14a37Christopher Tate                Log.e(TAG, "backupNow() couldn't connect");
316d5cf722ae62d06d1fb6a9505c6f4c403a5d14a37Christopher Tate            }
317d5cf722ae62d06d1fb6a9505c6f4c403a5d14a37Christopher Tate        }
318d5cf722ae62d06d1fb6a9505c6f4c403a5d14a37Christopher Tate    }
319a8bf815c6153290b173f34b071dddb0a0034a115Christopher Tate}
320