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 19d86b8fea43ebb6e5c31691b44d8ceb0d8d3c9072Jeff Sharkeyimport android.annotation.RequiresPermission; 20d5cf722ae62d06d1fb6a9505c6f4c403a5d14a37Christopher Tateimport android.annotation.SystemApi; 21865303fce57b968f5bd7efb4dd23ccb1a3747b93Shreyas Basargeimport android.content.ComponentName; 22a8bf815c6153290b173f34b071dddb0a0034a115Christopher Tateimport android.content.Context; 23b1f573dca3ccdd113ca513f32c9964211ca71c78Stefanotimport android.os.Bundle; 24fe06bf64d204c459699b0bf6465f9fb69208345eSergey Poromovimport android.os.Handler; 25fe06bf64d204c459699b0bf6465f9fb69208345eSergey Poromovimport android.os.Message; 26a8bf815c6153290b173f34b071dddb0a0034a115Christopher Tateimport android.os.RemoteException; 27a8bf815c6153290b173f34b071dddb0a0034a115Christopher Tateimport android.os.ServiceManager; 28c8daa769256b039b6bc4c5acbe6b558cd776c00aChristopher Tateimport android.util.Log; 29fe06bf64d204c459699b0bf6465f9fb69208345eSergey Poromovimport android.util.Pair; 30a8bf815c6153290b173f34b071dddb0a0034a115Christopher Tate 31a8bf815c6153290b173f34b071dddb0a0034a115Christopher Tate/** 32d17da43c82c4edb97514d6138bc208eeba321636Scott Main * The interface through which an application interacts with the Android backup service to 33d17da43c82c4edb97514d6138bc208eeba321636Scott Main * request backup and restore operations. 34d17da43c82c4edb97514d6138bc208eeba321636Scott Main * Applications instantiate it using the constructor and issue calls through that instance. 355a20ea16d7e5b70dc7bad8700f54170e4f220d12Kenny Root * <p> 365a20ea16d7e5b70dc7bad8700f54170e4f220d12Kenny Root * When an application has made changes to data which should be backed up, a 375a20ea16d7e5b70dc7bad8700f54170e4f220d12Kenny Root * call to {@link #dataChanged()} will notify the backup service. The system 385a20ea16d7e5b70dc7bad8700f54170e4f220d12Kenny Root * will then schedule a backup operation to occur in the near future. Repeated 395a20ea16d7e5b70dc7bad8700f54170e4f220d12Kenny Root * calls to {@link #dataChanged()} have no further effect until the backup 405a20ea16d7e5b70dc7bad8700f54170e4f220d12Kenny Root * operation actually occurs. 415a20ea16d7e5b70dc7bad8700f54170e4f220d12Kenny Root * <p> 42d17da43c82c4edb97514d6138bc208eeba321636Scott Main * A backup or restore operation for your application begins when the system launches the 43d17da43c82c4edb97514d6138bc208eeba321636Scott Main * {@link android.app.backup.BackupAgent} subclass you've declared in your manifest. See the 444528186e0d65fc68ef0dd1941aa2ac8aefcd55a3Christopher Tate * documentation for {@link android.app.backup.BackupAgent} for a detailed description 454e14a829129feee14ebe453f61a124784c870610Christopher Tate * of how the operation then proceeds. 465a20ea16d7e5b70dc7bad8700f54170e4f220d12Kenny Root * <p> 474e14a829129feee14ebe453f61a124784c870610Christopher Tate * Several attributes affecting the operation of the backup and restore mechanism 4861fd1e8d8c3ccf2d6b7d4af1c19e8f0988d5a1ecJoe Fernandez * can be set on the <code> 4961fd1e8d8c3ccf2d6b7d4af1c19e8f0988d5a1ecJoe Fernandez * <a href="{@docRoot}guide/topics/manifest/application-element.html"><application></a></code> 50d17da43c82c4edb97514d6138bc208eeba321636Scott Main * tag in your application's AndroidManifest.xml file. 51d17da43c82c4edb97514d6138bc208eeba321636Scott Main * 5261fd1e8d8c3ccf2d6b7d4af1c19e8f0988d5a1ecJoe Fernandez * <div class="special reference"> 5361fd1e8d8c3ccf2d6b7d4af1c19e8f0988d5a1ecJoe Fernandez * <h3>Developer Guides</h3> 5461fd1e8d8c3ccf2d6b7d4af1c19e8f0988d5a1ecJoe Fernandez * <p>For more information about using BackupManager, read the 5561fd1e8d8c3ccf2d6b7d4af1c19e8f0988d5a1ecJoe Fernandez * <a href="{@docRoot}guide/topics/data/backup.html">Data Backup</a> developer guide.</p></div> 5661fd1e8d8c3ccf2d6b7d4af1c19e8f0988d5a1ecJoe Fernandez * 575a20ea16d7e5b70dc7bad8700f54170e4f220d12Kenny Root * @attr ref android.R.styleable#AndroidManifestApplication_allowBackup 585a20ea16d7e5b70dc7bad8700f54170e4f220d12Kenny Root * @attr ref android.R.styleable#AndroidManifestApplication_backupAgent 595a20ea16d7e5b70dc7bad8700f54170e4f220d12Kenny Root * @attr ref android.R.styleable#AndroidManifestApplication_killAfterRestore 604e14a829129feee14ebe453f61a124784c870610Christopher Tate * @attr ref android.R.styleable#AndroidManifestApplication_restoreAnyVersion 61a8bf815c6153290b173f34b071dddb0a0034a115Christopher Tate */ 62a8bf815c6153290b173f34b071dddb0a0034a115Christopher Tatepublic class BackupManager { 63c8daa769256b039b6bc4c5acbe6b558cd776c00aChristopher Tate private static final String TAG = "BackupManager"; 64c8daa769256b039b6bc4c5acbe6b558cd776c00aChristopher Tate 65fe06bf64d204c459699b0bf6465f9fb69208345eSergey Poromov // BackupObserver status codes 66fe06bf64d204c459699b0bf6465f9fb69208345eSergey Poromov /** 67fe06bf64d204c459699b0bf6465f9fb69208345eSergey Poromov * Indicates that backup succeeded. 68fe06bf64d204c459699b0bf6465f9fb69208345eSergey Poromov * 69fe06bf64d204c459699b0bf6465f9fb69208345eSergey Poromov * @hide 70fe06bf64d204c459699b0bf6465f9fb69208345eSergey Poromov */ 71fe06bf64d204c459699b0bf6465f9fb69208345eSergey Poromov @SystemApi 72fe06bf64d204c459699b0bf6465f9fb69208345eSergey Poromov public static final int SUCCESS = 0; 73fe06bf64d204c459699b0bf6465f9fb69208345eSergey Poromov 74fe06bf64d204c459699b0bf6465f9fb69208345eSergey Poromov /** 75fe06bf64d204c459699b0bf6465f9fb69208345eSergey Poromov * Indicates that backup is either not enabled at all or 76fe06bf64d204c459699b0bf6465f9fb69208345eSergey Poromov * backup for the package was rejected by backup service 77fe06bf64d204c459699b0bf6465f9fb69208345eSergey Poromov * or backup transport, 78fe06bf64d204c459699b0bf6465f9fb69208345eSergey Poromov * 79fe06bf64d204c459699b0bf6465f9fb69208345eSergey Poromov * @hide 80fe06bf64d204c459699b0bf6465f9fb69208345eSergey Poromov */ 81fe06bf64d204c459699b0bf6465f9fb69208345eSergey Poromov @SystemApi 82fe06bf64d204c459699b0bf6465f9fb69208345eSergey Poromov public static final int ERROR_BACKUP_NOT_ALLOWED = -2001; 83fe06bf64d204c459699b0bf6465f9fb69208345eSergey Poromov 84fe06bf64d204c459699b0bf6465f9fb69208345eSergey Poromov /** 85fe06bf64d204c459699b0bf6465f9fb69208345eSergey Poromov * The requested app is not installed on the device. 86fe06bf64d204c459699b0bf6465f9fb69208345eSergey Poromov * 87fe06bf64d204c459699b0bf6465f9fb69208345eSergey Poromov * @hide 88fe06bf64d204c459699b0bf6465f9fb69208345eSergey Poromov */ 89fe06bf64d204c459699b0bf6465f9fb69208345eSergey Poromov @SystemApi 90fe06bf64d204c459699b0bf6465f9fb69208345eSergey Poromov public static final int ERROR_PACKAGE_NOT_FOUND = -2002; 91fe06bf64d204c459699b0bf6465f9fb69208345eSergey Poromov 92fe06bf64d204c459699b0bf6465f9fb69208345eSergey Poromov /** 93c3704427c9a34397fd90bf5438adae2eebcc97f6Shreyas Basarge * The backup operation was cancelled. 94c3704427c9a34397fd90bf5438adae2eebcc97f6Shreyas Basarge * 95c3704427c9a34397fd90bf5438adae2eebcc97f6Shreyas Basarge * @hide 96c3704427c9a34397fd90bf5438adae2eebcc97f6Shreyas Basarge */ 97c3704427c9a34397fd90bf5438adae2eebcc97f6Shreyas Basarge @SystemApi 98c3704427c9a34397fd90bf5438adae2eebcc97f6Shreyas Basarge public static final int ERROR_BACKUP_CANCELLED = -2003; 99c3704427c9a34397fd90bf5438adae2eebcc97f6Shreyas Basarge 100c3704427c9a34397fd90bf5438adae2eebcc97f6Shreyas Basarge /** 101fe06bf64d204c459699b0bf6465f9fb69208345eSergey Poromov * The transport for some reason was not in a good state and 102fe06bf64d204c459699b0bf6465f9fb69208345eSergey Poromov * aborted the entire backup request. This is a transient 103fe06bf64d204c459699b0bf6465f9fb69208345eSergey Poromov * failure and should not be retried immediately. 104fe06bf64d204c459699b0bf6465f9fb69208345eSergey Poromov * 105fe06bf64d204c459699b0bf6465f9fb69208345eSergey Poromov * @hide 106fe06bf64d204c459699b0bf6465f9fb69208345eSergey Poromov */ 107fe06bf64d204c459699b0bf6465f9fb69208345eSergey Poromov @SystemApi 108fe06bf64d204c459699b0bf6465f9fb69208345eSergey Poromov public static final int ERROR_TRANSPORT_ABORTED = BackupTransport.TRANSPORT_ERROR; 109fe06bf64d204c459699b0bf6465f9fb69208345eSergey Poromov 110fe06bf64d204c459699b0bf6465f9fb69208345eSergey Poromov /** 111fe06bf64d204c459699b0bf6465f9fb69208345eSergey Poromov * Returned when the transport was unable to process the 112fe06bf64d204c459699b0bf6465f9fb69208345eSergey Poromov * backup request for a given package, for example if the 113fe06bf64d204c459699b0bf6465f9fb69208345eSergey Poromov * transport hit a transient network failure. The remaining 114fe06bf64d204c459699b0bf6465f9fb69208345eSergey Poromov * packages provided to {@link #requestBackup(String[], BackupObserver)} 115fe06bf64d204c459699b0bf6465f9fb69208345eSergey Poromov * will still be attempted. 116fe06bf64d204c459699b0bf6465f9fb69208345eSergey Poromov * 117fe06bf64d204c459699b0bf6465f9fb69208345eSergey Poromov * @hide 118fe06bf64d204c459699b0bf6465f9fb69208345eSergey Poromov */ 119fe06bf64d204c459699b0bf6465f9fb69208345eSergey Poromov @SystemApi 120fe06bf64d204c459699b0bf6465f9fb69208345eSergey Poromov public static final int ERROR_TRANSPORT_PACKAGE_REJECTED = 121fe06bf64d204c459699b0bf6465f9fb69208345eSergey Poromov BackupTransport.TRANSPORT_PACKAGE_REJECTED; 122fe06bf64d204c459699b0bf6465f9fb69208345eSergey Poromov 123fe06bf64d204c459699b0bf6465f9fb69208345eSergey Poromov /** 124872d3b6e19933af6fa9ae65214b9f6df04fc3222Sergey Poromov * Returned when the transport reject the attempt to backup because 125872d3b6e19933af6fa9ae65214b9f6df04fc3222Sergey Poromov * backup data size exceeded current quota limit for this package. 126872d3b6e19933af6fa9ae65214b9f6df04fc3222Sergey Poromov * 127872d3b6e19933af6fa9ae65214b9f6df04fc3222Sergey Poromov * @hide 128872d3b6e19933af6fa9ae65214b9f6df04fc3222Sergey Poromov */ 129872d3b6e19933af6fa9ae65214b9f6df04fc3222Sergey Poromov @SystemApi 130872d3b6e19933af6fa9ae65214b9f6df04fc3222Sergey Poromov public static final int ERROR_TRANSPORT_QUOTA_EXCEEDED = 131872d3b6e19933af6fa9ae65214b9f6df04fc3222Sergey Poromov BackupTransport.TRANSPORT_QUOTA_EXCEEDED; 132872d3b6e19933af6fa9ae65214b9f6df04fc3222Sergey Poromov 133872d3b6e19933af6fa9ae65214b9f6df04fc3222Sergey Poromov /** 134fe06bf64d204c459699b0bf6465f9fb69208345eSergey Poromov * The {@link BackupAgent} for the requested package failed for some reason 135fe06bf64d204c459699b0bf6465f9fb69208345eSergey Poromov * and didn't provide appropriate backup data. 136fe06bf64d204c459699b0bf6465f9fb69208345eSergey Poromov * 137fe06bf64d204c459699b0bf6465f9fb69208345eSergey Poromov * @hide 138fe06bf64d204c459699b0bf6465f9fb69208345eSergey Poromov */ 139fe06bf64d204c459699b0bf6465f9fb69208345eSergey Poromov @SystemApi 140fe06bf64d204c459699b0bf6465f9fb69208345eSergey Poromov public static final int ERROR_AGENT_FAILURE = BackupTransport.AGENT_ERROR; 141fe06bf64d204c459699b0bf6465f9fb69208345eSergey Poromov 14238c9c306405a4c706cb405ccbea098c5b661b1c0Christopher Tate /** 14338c9c306405a4c706cb405ccbea098c5b661b1c0Christopher Tate * Intent extra when any subsidiary backup-related UI is launched from Settings: does 14438c9c306405a4c706cb405ccbea098c5b661b1c0Christopher Tate * device policy or configuration permit backup operations to run at all? 14538c9c306405a4c706cb405ccbea098c5b661b1c0Christopher Tate * 14638c9c306405a4c706cb405ccbea098c5b661b1c0Christopher Tate * @hide 14738c9c306405a4c706cb405ccbea098c5b661b1c0Christopher Tate */ 14838c9c306405a4c706cb405ccbea098c5b661b1c0Christopher Tate public static final String EXTRA_BACKUP_SERVICES_AVAILABLE = "backup_services_available"; 14938c9c306405a4c706cb405ccbea098c5b661b1c0Christopher Tate 15038e7486d3870797f7cc6e2cd2f5b59462a66b1a6Shreyas Basarge /** 15138e7486d3870797f7cc6e2cd2f5b59462a66b1a6Shreyas Basarge * If this flag is passed to {@link #requestBackup(String[], BackupObserver, int)}, 15238e7486d3870797f7cc6e2cd2f5b59462a66b1a6Shreyas Basarge * BackupManager will pass a blank old state to BackupAgents of requested packages. 15338e7486d3870797f7cc6e2cd2f5b59462a66b1a6Shreyas Basarge * 15438e7486d3870797f7cc6e2cd2f5b59462a66b1a6Shreyas Basarge * @hide 15538e7486d3870797f7cc6e2cd2f5b59462a66b1a6Shreyas Basarge */ 15638e7486d3870797f7cc6e2cd2f5b59462a66b1a6Shreyas Basarge @SystemApi 15738e7486d3870797f7cc6e2cd2f5b59462a66b1a6Shreyas Basarge public static final int FLAG_NON_INCREMENTAL_BACKUP = 1; 15838e7486d3870797f7cc6e2cd2f5b59462a66b1a6Shreyas Basarge 15938e7486d3870797f7cc6e2cd2f5b59462a66b1a6Shreyas Basarge /** 16038e7486d3870797f7cc6e2cd2f5b59462a66b1a6Shreyas Basarge * Use with {@link #requestBackup} to force backup of 16138e7486d3870797f7cc6e2cd2f5b59462a66b1a6Shreyas Basarge * package meta data. Typically you do not need to explicitly request this be backed up as it is 16238e7486d3870797f7cc6e2cd2f5b59462a66b1a6Shreyas Basarge * handled internally by the BackupManager. If you are requesting backups with 16338e7486d3870797f7cc6e2cd2f5b59462a66b1a6Shreyas Basarge * FLAG_NON_INCREMENTAL, this package won't automatically be backed up and you have to 16438e7486d3870797f7cc6e2cd2f5b59462a66b1a6Shreyas Basarge * explicitly request for its backup. 16538e7486d3870797f7cc6e2cd2f5b59462a66b1a6Shreyas Basarge * 16638e7486d3870797f7cc6e2cd2f5b59462a66b1a6Shreyas Basarge * @hide 16738e7486d3870797f7cc6e2cd2f5b59462a66b1a6Shreyas Basarge */ 16838e7486d3870797f7cc6e2cd2f5b59462a66b1a6Shreyas Basarge @SystemApi 16938e7486d3870797f7cc6e2cd2f5b59462a66b1a6Shreyas Basarge public static final String PACKAGE_MANAGER_SENTINEL = "@pm@"; 17038e7486d3870797f7cc6e2cd2f5b59462a66b1a6Shreyas Basarge 171865303fce57b968f5bd7efb4dd23ccb1a3747b93Shreyas Basarge 172865303fce57b968f5bd7efb4dd23ccb1a3747b93Shreyas Basarge /** 173865303fce57b968f5bd7efb4dd23ccb1a3747b93Shreyas Basarge * This error code is passed to {@link SelectBackupTransportCallback#onFailure(int)} 174865303fce57b968f5bd7efb4dd23ccb1a3747b93Shreyas Basarge * if the requested transport is unavailable. 175865303fce57b968f5bd7efb4dd23ccb1a3747b93Shreyas Basarge * 176865303fce57b968f5bd7efb4dd23ccb1a3747b93Shreyas Basarge * @hide 177865303fce57b968f5bd7efb4dd23ccb1a3747b93Shreyas Basarge */ 178865303fce57b968f5bd7efb4dd23ccb1a3747b93Shreyas Basarge @SystemApi 179865303fce57b968f5bd7efb4dd23ccb1a3747b93Shreyas Basarge public static final int ERROR_TRANSPORT_UNAVAILABLE = -1; 180865303fce57b968f5bd7efb4dd23ccb1a3747b93Shreyas Basarge 181865303fce57b968f5bd7efb4dd23ccb1a3747b93Shreyas Basarge /** 182865303fce57b968f5bd7efb4dd23ccb1a3747b93Shreyas Basarge * This error code is passed to {@link SelectBackupTransportCallback#onFailure(int)} if the 183865303fce57b968f5bd7efb4dd23ccb1a3747b93Shreyas Basarge * requested transport is not a valid BackupTransport. 184865303fce57b968f5bd7efb4dd23ccb1a3747b93Shreyas Basarge * 185865303fce57b968f5bd7efb4dd23ccb1a3747b93Shreyas Basarge * @hide 186865303fce57b968f5bd7efb4dd23ccb1a3747b93Shreyas Basarge */ 187865303fce57b968f5bd7efb4dd23ccb1a3747b93Shreyas Basarge @SystemApi 188865303fce57b968f5bd7efb4dd23ccb1a3747b93Shreyas Basarge public static final int ERROR_TRANSPORT_INVALID = -2; 189865303fce57b968f5bd7efb4dd23ccb1a3747b93Shreyas Basarge 190a8bf815c6153290b173f34b071dddb0a0034a115Christopher Tate private Context mContext; 191c8daa769256b039b6bc4c5acbe6b558cd776c00aChristopher Tate private static IBackupManager sService; 192a8bf815c6153290b173f34b071dddb0a0034a115Christopher Tate 193c8daa769256b039b6bc4c5acbe6b558cd776c00aChristopher Tate private static void checkServiceBinder() { 194c8daa769256b039b6bc4c5acbe6b558cd776c00aChristopher Tate if (sService == null) { 195c8daa769256b039b6bc4c5acbe6b558cd776c00aChristopher Tate sService = IBackupManager.Stub.asInterface( 196c8daa769256b039b6bc4c5acbe6b558cd776c00aChristopher Tate ServiceManager.getService(Context.BACKUP_SERVICE)); 197c8daa769256b039b6bc4c5acbe6b558cd776c00aChristopher Tate } 198c8daa769256b039b6bc4c5acbe6b558cd776c00aChristopher Tate } 199043dadc7516d20c3b3ccbcb20c53aaeef076a237Christopher Tate 200043dadc7516d20c3b3ccbcb20c53aaeef076a237Christopher Tate /** 201a8bf815c6153290b173f34b071dddb0a0034a115Christopher Tate * Constructs a BackupManager object through which the application can 202a8bf815c6153290b173f34b071dddb0a0034a115Christopher Tate * communicate with the Android backup system. 203c114eb55b442981e2ea0a8989aa6ed458fc418e4Christopher Tate * 204a8bf815c6153290b173f34b071dddb0a0034a115Christopher Tate * @param context The {@link android.content.Context} that was provided when 205a8bf815c6153290b173f34b071dddb0a0034a115Christopher Tate * one of your application's {@link android.app.Activity Activities} 206a8bf815c6153290b173f34b071dddb0a0034a115Christopher Tate * was created. 207a8bf815c6153290b173f34b071dddb0a0034a115Christopher Tate */ 208c114eb55b442981e2ea0a8989aa6ed458fc418e4Christopher Tate public BackupManager(Context context) { 209a8bf815c6153290b173f34b071dddb0a0034a115Christopher Tate mContext = context; 210a8bf815c6153290b173f34b071dddb0a0034a115Christopher Tate } 211a8bf815c6153290b173f34b071dddb0a0034a115Christopher Tate 212a8bf815c6153290b173f34b071dddb0a0034a115Christopher Tate /** 213a8bf815c6153290b173f34b071dddb0a0034a115Christopher Tate * Notifies the Android backup system that your application wishes to back up 214a8bf815c6153290b173f34b071dddb0a0034a115Christopher Tate * new changes to its data. A backup operation using your application's 2154e14a829129feee14ebe453f61a124784c870610Christopher Tate * {@link android.app.backup.BackupAgent} subclass will be scheduled when you 2164e14a829129feee14ebe453f61a124784c870610Christopher Tate * call this method. 217a8bf815c6153290b173f34b071dddb0a0034a115Christopher Tate */ 218a8bf815c6153290b173f34b071dddb0a0034a115Christopher Tate public void dataChanged() { 219c8daa769256b039b6bc4c5acbe6b558cd776c00aChristopher Tate checkServiceBinder(); 220c8daa769256b039b6bc4c5acbe6b558cd776c00aChristopher Tate if (sService != null) { 221c8daa769256b039b6bc4c5acbe6b558cd776c00aChristopher Tate try { 222c8daa769256b039b6bc4c5acbe6b558cd776c00aChristopher Tate sService.dataChanged(mContext.getPackageName()); 223c8daa769256b039b6bc4c5acbe6b558cd776c00aChristopher Tate } catch (RemoteException e) { 224c8daa769256b039b6bc4c5acbe6b558cd776c00aChristopher Tate Log.d(TAG, "dataChanged() couldn't connect"); 225c8daa769256b039b6bc4c5acbe6b558cd776c00aChristopher Tate } 226c8daa769256b039b6bc4c5acbe6b558cd776c00aChristopher Tate } 227c8daa769256b039b6bc4c5acbe6b558cd776c00aChristopher Tate } 228c8daa769256b039b6bc4c5acbe6b558cd776c00aChristopher Tate 229c8daa769256b039b6bc4c5acbe6b558cd776c00aChristopher Tate /** 230c8daa769256b039b6bc4c5acbe6b558cd776c00aChristopher Tate * Convenience method for callers who need to indicate that some other package 2314e14a829129feee14ebe453f61a124784c870610Christopher Tate * needs a backup pass. This can be useful in the case of groups of packages 2324e14a829129feee14ebe453f61a124784c870610Christopher Tate * that share a uid. 2334e14a829129feee14ebe453f61a124784c870610Christopher Tate * <p> 234c8daa769256b039b6bc4c5acbe6b558cd776c00aChristopher Tate * This method requires that the application hold the "android.permission.BACKUP" 2354e14a829129feee14ebe453f61a124784c870610Christopher Tate * permission if the package named in the argument does not run under the same uid 2364e14a829129feee14ebe453f61a124784c870610Christopher Tate * as the caller. 237d17da43c82c4edb97514d6138bc208eeba321636Scott Main * 238d17da43c82c4edb97514d6138bc208eeba321636Scott Main * @param packageName The package name identifying the application to back up. 239c8daa769256b039b6bc4c5acbe6b558cd776c00aChristopher Tate */ 240c8daa769256b039b6bc4c5acbe6b558cd776c00aChristopher Tate public static void dataChanged(String packageName) { 241c8daa769256b039b6bc4c5acbe6b558cd776c00aChristopher Tate checkServiceBinder(); 242c8daa769256b039b6bc4c5acbe6b558cd776c00aChristopher Tate if (sService != null) { 2438a27f923eb9dbbe3c2d0184e82d9f1a98f1e4cdcChristopher Tate try { 244c8daa769256b039b6bc4c5acbe6b558cd776c00aChristopher Tate sService.dataChanged(packageName); 2458a27f923eb9dbbe3c2d0184e82d9f1a98f1e4cdcChristopher Tate } catch (RemoteException e) { 246d5cf722ae62d06d1fb6a9505c6f4c403a5d14a37Christopher Tate Log.e(TAG, "dataChanged(pkg) couldn't connect"); 2478a27f923eb9dbbe3c2d0184e82d9f1a98f1e4cdcChristopher Tate } 248a8bf815c6153290b173f34b071dddb0a0034a115Christopher Tate } 249a8bf815c6153290b173f34b071dddb0a0034a115Christopher Tate } 2509b3905c4a25f2d785ce7535d1f2e1540b46bb561Christopher Tate 2519b3905c4a25f2d785ce7535d1f2e1540b46bb561Christopher Tate /** 2529c3cee9824026764275e4d84ba9b5d9fdc5da690Christopher Tate * Restore the calling application from backup. The data will be restored from the 2539c3cee9824026764275e4d84ba9b5d9fdc5da690Christopher Tate * current backup dataset if the application has stored data there, or from 2549c3cee9824026764275e4d84ba9b5d9fdc5da690Christopher Tate * the dataset used during the last full device setup operation if the current 2559c3cee9824026764275e4d84ba9b5d9fdc5da690Christopher Tate * backup dataset has no matching data. If no backup data exists for this application 2569c3cee9824026764275e4d84ba9b5d9fdc5da690Christopher Tate * in either source, a nonzero value will be returned. 2579c3cee9824026764275e4d84ba9b5d9fdc5da690Christopher Tate * 2589c3cee9824026764275e4d84ba9b5d9fdc5da690Christopher Tate * <p>If this method returns zero (meaning success), the OS will attempt to retrieve 2599c3cee9824026764275e4d84ba9b5d9fdc5da690Christopher Tate * a backed-up dataset from the remote transport, instantiate the application's 2609c3cee9824026764275e4d84ba9b5d9fdc5da690Christopher Tate * backup agent, and pass the dataset to the agent's 2619c3cee9824026764275e4d84ba9b5d9fdc5da690Christopher Tate * {@link android.app.backup.BackupAgent#onRestore(BackupDataInput, int, android.os.ParcelFileDescriptor) onRestore()} 2629c3cee9824026764275e4d84ba9b5d9fdc5da690Christopher Tate * method. 2639c3cee9824026764275e4d84ba9b5d9fdc5da690Christopher Tate * 264d17da43c82c4edb97514d6138bc208eeba321636Scott Main * @param observer The {@link RestoreObserver} to receive callbacks during the restore 265d17da43c82c4edb97514d6138bc208eeba321636Scott Main * operation. This must not be null. 266d17da43c82c4edb97514d6138bc208eeba321636Scott Main * 2679c3cee9824026764275e4d84ba9b5d9fdc5da690Christopher Tate * @return Zero on success; nonzero on error. 2689c3cee9824026764275e4d84ba9b5d9fdc5da690Christopher Tate */ 2699c3cee9824026764275e4d84ba9b5d9fdc5da690Christopher Tate public int requestRestore(RestoreObserver observer) { 270f4e237c6855fcbb8767695f072174ace1cf801bcStefanot return requestRestore(observer, null); 271f4e237c6855fcbb8767695f072174ace1cf801bcStefanot } 272f4e237c6855fcbb8767695f072174ace1cf801bcStefanot 273f4e237c6855fcbb8767695f072174ace1cf801bcStefanot // system APIs start here 274f4e237c6855fcbb8767695f072174ace1cf801bcStefanot 275f4e237c6855fcbb8767695f072174ace1cf801bcStefanot /** 276f4e237c6855fcbb8767695f072174ace1cf801bcStefanot * Restore the calling application from backup. The data will be restored from the 277f4e237c6855fcbb8767695f072174ace1cf801bcStefanot * current backup dataset if the application has stored data there, or from 278f4e237c6855fcbb8767695f072174ace1cf801bcStefanot * the dataset used during the last full device setup operation if the current 279f4e237c6855fcbb8767695f072174ace1cf801bcStefanot * backup dataset has no matching data. If no backup data exists for this application 280f4e237c6855fcbb8767695f072174ace1cf801bcStefanot * in either source, a nonzero value will be returned. 281f4e237c6855fcbb8767695f072174ace1cf801bcStefanot * 282f4e237c6855fcbb8767695f072174ace1cf801bcStefanot * <p>If this method returns zero (meaning success), the OS will attempt to retrieve 283f4e237c6855fcbb8767695f072174ace1cf801bcStefanot * a backed-up dataset from the remote transport, instantiate the application's 284f4e237c6855fcbb8767695f072174ace1cf801bcStefanot * backup agent, and pass the dataset to the agent's 285f4e237c6855fcbb8767695f072174ace1cf801bcStefanot * {@link android.app.backup.BackupAgent#onRestore(BackupDataInput, int, android.os.ParcelFileDescriptor) onRestore()} 286f4e237c6855fcbb8767695f072174ace1cf801bcStefanot * method. 287f4e237c6855fcbb8767695f072174ace1cf801bcStefanot * 288f4e237c6855fcbb8767695f072174ace1cf801bcStefanot * @param observer The {@link RestoreObserver} to receive callbacks during the restore 289f4e237c6855fcbb8767695f072174ace1cf801bcStefanot * operation. This must not be null. 290f4e237c6855fcbb8767695f072174ace1cf801bcStefanot * 291f4e237c6855fcbb8767695f072174ace1cf801bcStefanot * @param monitor the {@link BackupManagerMonitor} to receive callbacks during the restore 292f4e237c6855fcbb8767695f072174ace1cf801bcStefanot * operation. 293f4e237c6855fcbb8767695f072174ace1cf801bcStefanot * 294f4e237c6855fcbb8767695f072174ace1cf801bcStefanot * @return Zero on success; nonzero on error. 295f4e237c6855fcbb8767695f072174ace1cf801bcStefanot * 296f4e237c6855fcbb8767695f072174ace1cf801bcStefanot * @hide 297f4e237c6855fcbb8767695f072174ace1cf801bcStefanot */ 298f4e237c6855fcbb8767695f072174ace1cf801bcStefanot @SystemApi 299f4e237c6855fcbb8767695f072174ace1cf801bcStefanot public int requestRestore(RestoreObserver observer, BackupManagerMonitor monitor) { 3009c3cee9824026764275e4d84ba9b5d9fdc5da690Christopher Tate int result = -1; 3019c3cee9824026764275e4d84ba9b5d9fdc5da690Christopher Tate checkServiceBinder(); 3029c3cee9824026764275e4d84ba9b5d9fdc5da690Christopher Tate if (sService != null) { 3039c3cee9824026764275e4d84ba9b5d9fdc5da690Christopher Tate RestoreSession session = null; 3049c3cee9824026764275e4d84ba9b5d9fdc5da690Christopher Tate try { 30544ab8453e1c4c46790f792a46d026fa1017d8cfeChris Tate IRestoreSession binder = sService.beginRestoreSession(mContext.getPackageName(), 306f4e237c6855fcbb8767695f072174ace1cf801bcStefanot null); 307f5491fc1b61088843f280a6b55c1a995e2e6f939Christopher Tate if (binder != null) { 308f5491fc1b61088843f280a6b55c1a995e2e6f939Christopher Tate session = new RestoreSession(mContext, binder); 309f4e237c6855fcbb8767695f072174ace1cf801bcStefanot result = session.restorePackage(mContext.getPackageName(), observer, monitor); 310f5491fc1b61088843f280a6b55c1a995e2e6f939Christopher Tate } 3119c3cee9824026764275e4d84ba9b5d9fdc5da690Christopher Tate } catch (RemoteException e) { 312d5cf722ae62d06d1fb6a9505c6f4c403a5d14a37Christopher Tate Log.e(TAG, "restoreSelf() unable to contact service"); 3139c3cee9824026764275e4d84ba9b5d9fdc5da690Christopher Tate } finally { 3149c3cee9824026764275e4d84ba9b5d9fdc5da690Christopher Tate if (session != null) { 3159c3cee9824026764275e4d84ba9b5d9fdc5da690Christopher Tate session.endRestoreSession(); 3169c3cee9824026764275e4d84ba9b5d9fdc5da690Christopher Tate } 3179c3cee9824026764275e4d84ba9b5d9fdc5da690Christopher Tate } 3189c3cee9824026764275e4d84ba9b5d9fdc5da690Christopher Tate } 3199c3cee9824026764275e4d84ba9b5d9fdc5da690Christopher Tate return result; 3209c3cee9824026764275e4d84ba9b5d9fdc5da690Christopher Tate } 3219c3cee9824026764275e4d84ba9b5d9fdc5da690Christopher Tate 3229c3cee9824026764275e4d84ba9b5d9fdc5da690Christopher Tate /** 323e28290e21f908b4e917099ff2aa41e3aab9310c2Christopher Tate * Begin the process of restoring data from backup. See the 3244528186e0d65fc68ef0dd1941aa2ac8aefcd55a3Christopher Tate * {@link android.app.backup.RestoreSession} class for documentation on that process. 3259c3cee9824026764275e4d84ba9b5d9fdc5da690Christopher Tate * @hide 3269b3905c4a25f2d785ce7535d1f2e1540b46bb561Christopher Tate */ 3272e5979236ccc06beec8b8f7f631f31bdedc79614Christopher Tate @SystemApi 328d86b8fea43ebb6e5c31691b44d8ceb0d8d3c9072Jeff Sharkey @RequiresPermission(android.Manifest.permission.BACKUP) 32980202c8cb8c8e4ab507079e79b864c61a8eeeee9Christopher Tate public RestoreSession beginRestoreSession() { 33080202c8cb8c8e4ab507079e79b864c61a8eeeee9Christopher Tate RestoreSession session = null; 331c8daa769256b039b6bc4c5acbe6b558cd776c00aChristopher Tate checkServiceBinder(); 332c8daa769256b039b6bc4c5acbe6b558cd776c00aChristopher Tate if (sService != null) { 3338a27f923eb9dbbe3c2d0184e82d9f1a98f1e4cdcChristopher Tate try { 33444ab8453e1c4c46790f792a46d026fa1017d8cfeChris Tate // All packages, current transport 33544ab8453e1c4c46790f792a46d026fa1017d8cfeChris Tate IRestoreSession binder = sService.beginRestoreSession(null, null); 336f5491fc1b61088843f280a6b55c1a995e2e6f939Christopher Tate if (binder != null) { 337f5491fc1b61088843f280a6b55c1a995e2e6f939Christopher Tate session = new RestoreSession(mContext, binder); 338f5491fc1b61088843f280a6b55c1a995e2e6f939Christopher Tate } 3398a27f923eb9dbbe3c2d0184e82d9f1a98f1e4cdcChristopher Tate } catch (RemoteException e) { 340d5cf722ae62d06d1fb6a9505c6f4c403a5d14a37Christopher Tate Log.e(TAG, "beginRestoreSession() couldn't connect"); 3418a27f923eb9dbbe3c2d0184e82d9f1a98f1e4cdcChristopher Tate } 3429b3905c4a25f2d785ce7535d1f2e1540b46bb561Christopher Tate } 34380202c8cb8c8e4ab507079e79b864c61a8eeeee9Christopher Tate return session; 3449b3905c4a25f2d785ce7535d1f2e1540b46bb561Christopher Tate } 345d5cf722ae62d06d1fb6a9505c6f4c403a5d14a37Christopher Tate 346d5cf722ae62d06d1fb6a9505c6f4c403a5d14a37Christopher Tate /** 347d5cf722ae62d06d1fb6a9505c6f4c403a5d14a37Christopher Tate * Enable/disable the backup service entirely. When disabled, no backup 348d5cf722ae62d06d1fb6a9505c6f4c403a5d14a37Christopher Tate * or restore operations will take place. Data-changed notifications will 349d5cf722ae62d06d1fb6a9505c6f4c403a5d14a37Christopher Tate * still be observed and collected, however, so that changes made while the 350d5cf722ae62d06d1fb6a9505c6f4c403a5d14a37Christopher Tate * mechanism was disabled will still be backed up properly if it is enabled 351d5cf722ae62d06d1fb6a9505c6f4c403a5d14a37Christopher Tate * at some point in the future. 352d5cf722ae62d06d1fb6a9505c6f4c403a5d14a37Christopher Tate * 353d5cf722ae62d06d1fb6a9505c6f4c403a5d14a37Christopher Tate * @hide 354d5cf722ae62d06d1fb6a9505c6f4c403a5d14a37Christopher Tate */ 355d5cf722ae62d06d1fb6a9505c6f4c403a5d14a37Christopher Tate @SystemApi 356d86b8fea43ebb6e5c31691b44d8ceb0d8d3c9072Jeff Sharkey @RequiresPermission(android.Manifest.permission.BACKUP) 357d5cf722ae62d06d1fb6a9505c6f4c403a5d14a37Christopher Tate public void setBackupEnabled(boolean isEnabled) { 358d5cf722ae62d06d1fb6a9505c6f4c403a5d14a37Christopher Tate checkServiceBinder(); 359d5cf722ae62d06d1fb6a9505c6f4c403a5d14a37Christopher Tate if (sService != null) { 360d5cf722ae62d06d1fb6a9505c6f4c403a5d14a37Christopher Tate try { 361d5cf722ae62d06d1fb6a9505c6f4c403a5d14a37Christopher Tate sService.setBackupEnabled(isEnabled); 362d5cf722ae62d06d1fb6a9505c6f4c403a5d14a37Christopher Tate } catch (RemoteException e) { 363d5cf722ae62d06d1fb6a9505c6f4c403a5d14a37Christopher Tate Log.e(TAG, "setBackupEnabled() couldn't connect"); 364d5cf722ae62d06d1fb6a9505c6f4c403a5d14a37Christopher Tate } 365d5cf722ae62d06d1fb6a9505c6f4c403a5d14a37Christopher Tate } 366d5cf722ae62d06d1fb6a9505c6f4c403a5d14a37Christopher Tate } 367d5cf722ae62d06d1fb6a9505c6f4c403a5d14a37Christopher Tate 368d5cf722ae62d06d1fb6a9505c6f4c403a5d14a37Christopher Tate /** 369d5cf722ae62d06d1fb6a9505c6f4c403a5d14a37Christopher Tate * Report whether the backup mechanism is currently enabled. 370d5cf722ae62d06d1fb6a9505c6f4c403a5d14a37Christopher Tate * 371d5cf722ae62d06d1fb6a9505c6f4c403a5d14a37Christopher Tate * @hide 372d5cf722ae62d06d1fb6a9505c6f4c403a5d14a37Christopher Tate */ 373d5cf722ae62d06d1fb6a9505c6f4c403a5d14a37Christopher Tate @SystemApi 374d86b8fea43ebb6e5c31691b44d8ceb0d8d3c9072Jeff Sharkey @RequiresPermission(android.Manifest.permission.BACKUP) 375d5cf722ae62d06d1fb6a9505c6f4c403a5d14a37Christopher Tate public boolean isBackupEnabled() { 3769e079298edd022c43a960729442a53557fd16e45Christopher Tate checkServiceBinder(); 377d5cf722ae62d06d1fb6a9505c6f4c403a5d14a37Christopher Tate if (sService != null) { 378d5cf722ae62d06d1fb6a9505c6f4c403a5d14a37Christopher Tate try { 379d5cf722ae62d06d1fb6a9505c6f4c403a5d14a37Christopher Tate return sService.isBackupEnabled(); 380d5cf722ae62d06d1fb6a9505c6f4c403a5d14a37Christopher Tate } catch (RemoteException e) { 381d5cf722ae62d06d1fb6a9505c6f4c403a5d14a37Christopher Tate Log.e(TAG, "isBackupEnabled() couldn't connect"); 382d5cf722ae62d06d1fb6a9505c6f4c403a5d14a37Christopher Tate } 383d5cf722ae62d06d1fb6a9505c6f4c403a5d14a37Christopher Tate } 384d5cf722ae62d06d1fb6a9505c6f4c403a5d14a37Christopher Tate return false; 385d5cf722ae62d06d1fb6a9505c6f4c403a5d14a37Christopher Tate } 386d5cf722ae62d06d1fb6a9505c6f4c403a5d14a37Christopher Tate 387d5cf722ae62d06d1fb6a9505c6f4c403a5d14a37Christopher Tate /** 388a63246d6daa02c6f3e4e78d0072d991387e14c87Christopher Tate * Enable/disable data restore at application install time. When enabled, app 389a63246d6daa02c6f3e4e78d0072d991387e14c87Christopher Tate * installation will include an attempt to fetch the app's historical data from 390a63246d6daa02c6f3e4e78d0072d991387e14c87Christopher Tate * the archival restore dataset (if any). When disabled, no such attempt will 391a63246d6daa02c6f3e4e78d0072d991387e14c87Christopher Tate * be made. 392a63246d6daa02c6f3e4e78d0072d991387e14c87Christopher Tate * 393a63246d6daa02c6f3e4e78d0072d991387e14c87Christopher Tate * @hide 394a63246d6daa02c6f3e4e78d0072d991387e14c87Christopher Tate */ 395a63246d6daa02c6f3e4e78d0072d991387e14c87Christopher Tate @SystemApi 396d86b8fea43ebb6e5c31691b44d8ceb0d8d3c9072Jeff Sharkey @RequiresPermission(android.Manifest.permission.BACKUP) 397a63246d6daa02c6f3e4e78d0072d991387e14c87Christopher Tate public void setAutoRestore(boolean isEnabled) { 398a63246d6daa02c6f3e4e78d0072d991387e14c87Christopher Tate checkServiceBinder(); 399a63246d6daa02c6f3e4e78d0072d991387e14c87Christopher Tate if (sService != null) { 400a63246d6daa02c6f3e4e78d0072d991387e14c87Christopher Tate try { 401a63246d6daa02c6f3e4e78d0072d991387e14c87Christopher Tate sService.setAutoRestore(isEnabled); 402a63246d6daa02c6f3e4e78d0072d991387e14c87Christopher Tate } catch (RemoteException e) { 403a63246d6daa02c6f3e4e78d0072d991387e14c87Christopher Tate Log.e(TAG, "setAutoRestore() couldn't connect"); 404a63246d6daa02c6f3e4e78d0072d991387e14c87Christopher Tate } 405a63246d6daa02c6f3e4e78d0072d991387e14c87Christopher Tate } 406a63246d6daa02c6f3e4e78d0072d991387e14c87Christopher Tate } 407a63246d6daa02c6f3e4e78d0072d991387e14c87Christopher Tate 408a63246d6daa02c6f3e4e78d0072d991387e14c87Christopher Tate /** 409d86b8fea43ebb6e5c31691b44d8ceb0d8d3c9072Jeff Sharkey * Identify the currently selected transport. 410d5cf722ae62d06d1fb6a9505c6f4c403a5d14a37Christopher Tate * @return The name of the currently active backup transport. In case of 411d5cf722ae62d06d1fb6a9505c6f4c403a5d14a37Christopher Tate * failure or if no transport is currently active, this method returns {@code null}. 412d5cf722ae62d06d1fb6a9505c6f4c403a5d14a37Christopher Tate * 413d5cf722ae62d06d1fb6a9505c6f4c403a5d14a37Christopher Tate * @hide 414d5cf722ae62d06d1fb6a9505c6f4c403a5d14a37Christopher Tate */ 415d5cf722ae62d06d1fb6a9505c6f4c403a5d14a37Christopher Tate @SystemApi 416d86b8fea43ebb6e5c31691b44d8ceb0d8d3c9072Jeff Sharkey @RequiresPermission(android.Manifest.permission.BACKUP) 417d5cf722ae62d06d1fb6a9505c6f4c403a5d14a37Christopher Tate public String getCurrentTransport() { 418d5cf722ae62d06d1fb6a9505c6f4c403a5d14a37Christopher Tate checkServiceBinder(); 419d5cf722ae62d06d1fb6a9505c6f4c403a5d14a37Christopher Tate if (sService != null) { 420d5cf722ae62d06d1fb6a9505c6f4c403a5d14a37Christopher Tate try { 421d5cf722ae62d06d1fb6a9505c6f4c403a5d14a37Christopher Tate return sService.getCurrentTransport(); 422d5cf722ae62d06d1fb6a9505c6f4c403a5d14a37Christopher Tate } catch (RemoteException e) { 423d5cf722ae62d06d1fb6a9505c6f4c403a5d14a37Christopher Tate Log.e(TAG, "getCurrentTransport() couldn't connect"); 424d5cf722ae62d06d1fb6a9505c6f4c403a5d14a37Christopher Tate } 425d5cf722ae62d06d1fb6a9505c6f4c403a5d14a37Christopher Tate } 426d5cf722ae62d06d1fb6a9505c6f4c403a5d14a37Christopher Tate return null; 427d5cf722ae62d06d1fb6a9505c6f4c403a5d14a37Christopher Tate } 428d5cf722ae62d06d1fb6a9505c6f4c403a5d14a37Christopher Tate 429d5cf722ae62d06d1fb6a9505c6f4c403a5d14a37Christopher Tate /** 430d86b8fea43ebb6e5c31691b44d8ceb0d8d3c9072Jeff Sharkey * Request a list of all available backup transports' names. 431d5cf722ae62d06d1fb6a9505c6f4c403a5d14a37Christopher Tate * 432d5cf722ae62d06d1fb6a9505c6f4c403a5d14a37Christopher Tate * @hide 433d5cf722ae62d06d1fb6a9505c6f4c403a5d14a37Christopher Tate */ 434d5cf722ae62d06d1fb6a9505c6f4c403a5d14a37Christopher Tate @SystemApi 435d86b8fea43ebb6e5c31691b44d8ceb0d8d3c9072Jeff Sharkey @RequiresPermission(android.Manifest.permission.BACKUP) 436d5cf722ae62d06d1fb6a9505c6f4c403a5d14a37Christopher Tate public String[] listAllTransports() { 437d5cf722ae62d06d1fb6a9505c6f4c403a5d14a37Christopher Tate checkServiceBinder(); 438d5cf722ae62d06d1fb6a9505c6f4c403a5d14a37Christopher Tate if (sService != null) { 439d5cf722ae62d06d1fb6a9505c6f4c403a5d14a37Christopher Tate try { 440d5cf722ae62d06d1fb6a9505c6f4c403a5d14a37Christopher Tate return sService.listAllTransports(); 441d5cf722ae62d06d1fb6a9505c6f4c403a5d14a37Christopher Tate } catch (RemoteException e) { 442d5cf722ae62d06d1fb6a9505c6f4c403a5d14a37Christopher Tate Log.e(TAG, "listAllTransports() couldn't connect"); 443d5cf722ae62d06d1fb6a9505c6f4c403a5d14a37Christopher Tate } 444d5cf722ae62d06d1fb6a9505c6f4c403a5d14a37Christopher Tate } 445d5cf722ae62d06d1fb6a9505c6f4c403a5d14a37Christopher Tate return null; 446d5cf722ae62d06d1fb6a9505c6f4c403a5d14a37Christopher Tate } 447d5cf722ae62d06d1fb6a9505c6f4c403a5d14a37Christopher Tate 448d5cf722ae62d06d1fb6a9505c6f4c403a5d14a37Christopher Tate /** 449865303fce57b968f5bd7efb4dd23ccb1a3747b93Shreyas Basarge * Specify the current backup transport. 450865303fce57b968f5bd7efb4dd23ccb1a3747b93Shreyas Basarge * 451d5cf722ae62d06d1fb6a9505c6f4c403a5d14a37Christopher Tate * @param transport The name of the transport to select. This should be one 452865303fce57b968f5bd7efb4dd23ccb1a3747b93Shreyas Basarge * of the names returned by {@link #listAllTransports()}. This is the String returned by 453865303fce57b968f5bd7efb4dd23ccb1a3747b93Shreyas Basarge * {@link BackupTransport#name()} for the particular transport. 454d5cf722ae62d06d1fb6a9505c6f4c403a5d14a37Christopher Tate * @return The name of the previously selected transport. If the given transport 455d5cf722ae62d06d1fb6a9505c6f4c403a5d14a37Christopher Tate * name is not one of the currently available transports, no change is made to 456d5cf722ae62d06d1fb6a9505c6f4c403a5d14a37Christopher Tate * the current transport setting and the method returns null. 457d5cf722ae62d06d1fb6a9505c6f4c403a5d14a37Christopher Tate * 458d5cf722ae62d06d1fb6a9505c6f4c403a5d14a37Christopher Tate * @hide 459d5cf722ae62d06d1fb6a9505c6f4c403a5d14a37Christopher Tate */ 460865303fce57b968f5bd7efb4dd23ccb1a3747b93Shreyas Basarge @Deprecated 461d5cf722ae62d06d1fb6a9505c6f4c403a5d14a37Christopher Tate @SystemApi 462d86b8fea43ebb6e5c31691b44d8ceb0d8d3c9072Jeff Sharkey @RequiresPermission(android.Manifest.permission.BACKUP) 463d5cf722ae62d06d1fb6a9505c6f4c403a5d14a37Christopher Tate public String selectBackupTransport(String transport) { 464d5cf722ae62d06d1fb6a9505c6f4c403a5d14a37Christopher Tate checkServiceBinder(); 465d5cf722ae62d06d1fb6a9505c6f4c403a5d14a37Christopher Tate if (sService != null) { 466d5cf722ae62d06d1fb6a9505c6f4c403a5d14a37Christopher Tate try { 467d5cf722ae62d06d1fb6a9505c6f4c403a5d14a37Christopher Tate return sService.selectBackupTransport(transport); 468d5cf722ae62d06d1fb6a9505c6f4c403a5d14a37Christopher Tate } catch (RemoteException e) { 469d5cf722ae62d06d1fb6a9505c6f4c403a5d14a37Christopher Tate Log.e(TAG, "selectBackupTransport() couldn't connect"); 470d5cf722ae62d06d1fb6a9505c6f4c403a5d14a37Christopher Tate } 471d5cf722ae62d06d1fb6a9505c6f4c403a5d14a37Christopher Tate } 472d5cf722ae62d06d1fb6a9505c6f4c403a5d14a37Christopher Tate return null; 473d5cf722ae62d06d1fb6a9505c6f4c403a5d14a37Christopher Tate } 474d5cf722ae62d06d1fb6a9505c6f4c403a5d14a37Christopher Tate 475d5cf722ae62d06d1fb6a9505c6f4c403a5d14a37Christopher Tate /** 476865303fce57b968f5bd7efb4dd23ccb1a3747b93Shreyas Basarge * Specify the current backup transport and get notified when the transport is ready to be used. 477865303fce57b968f5bd7efb4dd23ccb1a3747b93Shreyas Basarge * This method is async because BackupManager might need to bind to the specified transport 478865303fce57b968f5bd7efb4dd23ccb1a3747b93Shreyas Basarge * which is in a separate process. 479865303fce57b968f5bd7efb4dd23ccb1a3747b93Shreyas Basarge * 480865303fce57b968f5bd7efb4dd23ccb1a3747b93Shreyas Basarge * @param transport ComponentName of the service hosting the transport. This is different from 481865303fce57b968f5bd7efb4dd23ccb1a3747b93Shreyas Basarge * the transport's name that is returned by {@link BackupTransport#name()}. 482865303fce57b968f5bd7efb4dd23ccb1a3747b93Shreyas Basarge * @param listener A listener object to get a callback on the transport being selected. 483865303fce57b968f5bd7efb4dd23ccb1a3747b93Shreyas Basarge * 484865303fce57b968f5bd7efb4dd23ccb1a3747b93Shreyas Basarge * @hide 485865303fce57b968f5bd7efb4dd23ccb1a3747b93Shreyas Basarge */ 486865303fce57b968f5bd7efb4dd23ccb1a3747b93Shreyas Basarge @SystemApi 487d86b8fea43ebb6e5c31691b44d8ceb0d8d3c9072Jeff Sharkey @RequiresPermission(android.Manifest.permission.BACKUP) 488865303fce57b968f5bd7efb4dd23ccb1a3747b93Shreyas Basarge public void selectBackupTransport(ComponentName transport, 489865303fce57b968f5bd7efb4dd23ccb1a3747b93Shreyas Basarge SelectBackupTransportCallback listener) { 490865303fce57b968f5bd7efb4dd23ccb1a3747b93Shreyas Basarge checkServiceBinder(); 491865303fce57b968f5bd7efb4dd23ccb1a3747b93Shreyas Basarge if (sService != null) { 492865303fce57b968f5bd7efb4dd23ccb1a3747b93Shreyas Basarge try { 493865303fce57b968f5bd7efb4dd23ccb1a3747b93Shreyas Basarge SelectTransportListenerWrapper wrapper = listener == null ? 494865303fce57b968f5bd7efb4dd23ccb1a3747b93Shreyas Basarge null : new SelectTransportListenerWrapper(mContext, listener); 495865303fce57b968f5bd7efb4dd23ccb1a3747b93Shreyas Basarge sService.selectBackupTransportAsync(transport, wrapper); 496865303fce57b968f5bd7efb4dd23ccb1a3747b93Shreyas Basarge } catch (RemoteException e) { 497865303fce57b968f5bd7efb4dd23ccb1a3747b93Shreyas Basarge Log.e(TAG, "selectBackupTransportAsync() couldn't connect"); 498865303fce57b968f5bd7efb4dd23ccb1a3747b93Shreyas Basarge } 499865303fce57b968f5bd7efb4dd23ccb1a3747b93Shreyas Basarge } 500865303fce57b968f5bd7efb4dd23ccb1a3747b93Shreyas Basarge } 501865303fce57b968f5bd7efb4dd23ccb1a3747b93Shreyas Basarge 502865303fce57b968f5bd7efb4dd23ccb1a3747b93Shreyas Basarge /** 503d5cf722ae62d06d1fb6a9505c6f4c403a5d14a37Christopher Tate * Schedule an immediate backup attempt for all pending key/value updates. This 504d5cf722ae62d06d1fb6a9505c6f4c403a5d14a37Christopher Tate * is primarily intended for transports to use when they detect a suitable 505d5cf722ae62d06d1fb6a9505c6f4c403a5d14a37Christopher Tate * opportunity for doing a backup pass. If there are no pending updates to 506d5cf722ae62d06d1fb6a9505c6f4c403a5d14a37Christopher Tate * be sent, no action will be taken. Even if some updates are pending, the 507d5cf722ae62d06d1fb6a9505c6f4c403a5d14a37Christopher Tate * transport will still be asked to confirm via the usual requestBackupTime() 508d5cf722ae62d06d1fb6a9505c6f4c403a5d14a37Christopher Tate * method. 509d5cf722ae62d06d1fb6a9505c6f4c403a5d14a37Christopher Tate * 510d5cf722ae62d06d1fb6a9505c6f4c403a5d14a37Christopher Tate * @hide 511d5cf722ae62d06d1fb6a9505c6f4c403a5d14a37Christopher Tate */ 512d5cf722ae62d06d1fb6a9505c6f4c403a5d14a37Christopher Tate @SystemApi 513d86b8fea43ebb6e5c31691b44d8ceb0d8d3c9072Jeff Sharkey @RequiresPermission(android.Manifest.permission.BACKUP) 514d5cf722ae62d06d1fb6a9505c6f4c403a5d14a37Christopher Tate public void backupNow() { 515d5cf722ae62d06d1fb6a9505c6f4c403a5d14a37Christopher Tate checkServiceBinder(); 516d5cf722ae62d06d1fb6a9505c6f4c403a5d14a37Christopher Tate if (sService != null) { 517d5cf722ae62d06d1fb6a9505c6f4c403a5d14a37Christopher Tate try { 518d5cf722ae62d06d1fb6a9505c6f4c403a5d14a37Christopher Tate sService.backupNow(); 519d5cf722ae62d06d1fb6a9505c6f4c403a5d14a37Christopher Tate } catch (RemoteException e) { 520d5cf722ae62d06d1fb6a9505c6f4c403a5d14a37Christopher Tate Log.e(TAG, "backupNow() couldn't connect"); 521d5cf722ae62d06d1fb6a9505c6f4c403a5d14a37Christopher Tate } 522d5cf722ae62d06d1fb6a9505c6f4c403a5d14a37Christopher Tate } 523d5cf722ae62d06d1fb6a9505c6f4c403a5d14a37Christopher Tate } 524511d02fcc37dce092e17354d53023db44817ebe6Christopher Tate 525511d02fcc37dce092e17354d53023db44817ebe6Christopher Tate /** 526511d02fcc37dce092e17354d53023db44817ebe6Christopher Tate * Ask the framework which dataset, if any, the given package's data would be 527511d02fcc37dce092e17354d53023db44817ebe6Christopher Tate * restored from if we were to install it right now. 528511d02fcc37dce092e17354d53023db44817ebe6Christopher Tate * 529511d02fcc37dce092e17354d53023db44817ebe6Christopher Tate * @param packageName The name of the package whose most-suitable dataset we 530511d02fcc37dce092e17354d53023db44817ebe6Christopher Tate * wish to look up 531511d02fcc37dce092e17354d53023db44817ebe6Christopher Tate * @return The dataset token from which a restore should be attempted, or zero if 532511d02fcc37dce092e17354d53023db44817ebe6Christopher Tate * no suitable data is available. 533511d02fcc37dce092e17354d53023db44817ebe6Christopher Tate * 534511d02fcc37dce092e17354d53023db44817ebe6Christopher Tate * @hide 535511d02fcc37dce092e17354d53023db44817ebe6Christopher Tate */ 536511d02fcc37dce092e17354d53023db44817ebe6Christopher Tate @SystemApi 537d86b8fea43ebb6e5c31691b44d8ceb0d8d3c9072Jeff Sharkey @RequiresPermission(android.Manifest.permission.BACKUP) 538511d02fcc37dce092e17354d53023db44817ebe6Christopher Tate public long getAvailableRestoreToken(String packageName) { 539511d02fcc37dce092e17354d53023db44817ebe6Christopher Tate checkServiceBinder(); 540511d02fcc37dce092e17354d53023db44817ebe6Christopher Tate if (sService != null) { 541511d02fcc37dce092e17354d53023db44817ebe6Christopher Tate try { 542511d02fcc37dce092e17354d53023db44817ebe6Christopher Tate return sService.getAvailableRestoreToken(packageName); 543511d02fcc37dce092e17354d53023db44817ebe6Christopher Tate } catch (RemoteException e) { 544511d02fcc37dce092e17354d53023db44817ebe6Christopher Tate Log.e(TAG, "getAvailableRestoreToken() couldn't connect"); 545511d02fcc37dce092e17354d53023db44817ebe6Christopher Tate } 546511d02fcc37dce092e17354d53023db44817ebe6Christopher Tate } 547511d02fcc37dce092e17354d53023db44817ebe6Christopher Tate return 0; 548511d02fcc37dce092e17354d53023db44817ebe6Christopher Tate } 549fe06bf64d204c459699b0bf6465f9fb69208345eSergey Poromov 550fe06bf64d204c459699b0bf6465f9fb69208345eSergey Poromov /** 5519448196076d5a5266b3ae7e4945216b30ee88aa7Sergey Poromov * Ask the framework whether this app is eligible for backup. 5529448196076d5a5266b3ae7e4945216b30ee88aa7Sergey Poromov * 5539448196076d5a5266b3ae7e4945216b30ee88aa7Sergey Poromov * @param packageName The name of the package. 5549448196076d5a5266b3ae7e4945216b30ee88aa7Sergey Poromov * @return Whether this app is eligible for backup. 5559448196076d5a5266b3ae7e4945216b30ee88aa7Sergey Poromov * 5569448196076d5a5266b3ae7e4945216b30ee88aa7Sergey Poromov * @hide 5579448196076d5a5266b3ae7e4945216b30ee88aa7Sergey Poromov */ 5589448196076d5a5266b3ae7e4945216b30ee88aa7Sergey Poromov @SystemApi 559d86b8fea43ebb6e5c31691b44d8ceb0d8d3c9072Jeff Sharkey @RequiresPermission(android.Manifest.permission.BACKUP) 5609448196076d5a5266b3ae7e4945216b30ee88aa7Sergey Poromov public boolean isAppEligibleForBackup(String packageName) { 5619448196076d5a5266b3ae7e4945216b30ee88aa7Sergey Poromov checkServiceBinder(); 5629448196076d5a5266b3ae7e4945216b30ee88aa7Sergey Poromov if (sService != null) { 5639448196076d5a5266b3ae7e4945216b30ee88aa7Sergey Poromov try { 5649448196076d5a5266b3ae7e4945216b30ee88aa7Sergey Poromov return sService.isAppEligibleForBackup(packageName); 5659448196076d5a5266b3ae7e4945216b30ee88aa7Sergey Poromov } catch (RemoteException e) { 5669448196076d5a5266b3ae7e4945216b30ee88aa7Sergey Poromov Log.e(TAG, "isAppEligibleForBackup(pkg) couldn't connect"); 5679448196076d5a5266b3ae7e4945216b30ee88aa7Sergey Poromov } 5689448196076d5a5266b3ae7e4945216b30ee88aa7Sergey Poromov } 5699448196076d5a5266b3ae7e4945216b30ee88aa7Sergey Poromov return false; 5709448196076d5a5266b3ae7e4945216b30ee88aa7Sergey Poromov } 5719448196076d5a5266b3ae7e4945216b30ee88aa7Sergey Poromov 5729448196076d5a5266b3ae7e4945216b30ee88aa7Sergey Poromov /** 573fe06bf64d204c459699b0bf6465f9fb69208345eSergey Poromov * Request an immediate backup, providing an observer to which results of the backup operation 574fe06bf64d204c459699b0bf6465f9fb69208345eSergey Poromov * will be published. The Android backup system will decide for each package whether it will 575fe06bf64d204c459699b0bf6465f9fb69208345eSergey Poromov * be full app data backup or key/value-pair-based backup. 576fe06bf64d204c459699b0bf6465f9fb69208345eSergey Poromov * 577fe06bf64d204c459699b0bf6465f9fb69208345eSergey Poromov * <p>If this method returns {@link BackupManager#SUCCESS}, the OS will attempt to backup all 578fe06bf64d204c459699b0bf6465f9fb69208345eSergey Poromov * provided packages using the remote transport. 579fe06bf64d204c459699b0bf6465f9fb69208345eSergey Poromov * 580fe06bf64d204c459699b0bf6465f9fb69208345eSergey Poromov * @param packages List of package names to backup. 581fe06bf64d204c459699b0bf6465f9fb69208345eSergey Poromov * @param observer The {@link BackupObserver} to receive callbacks during the backup 582899edf6f4fef302ff219ee346b47afc5c8b640aaSergey Poromov * operation. Could be {@code null}. 583fe06bf64d204c459699b0bf6465f9fb69208345eSergey Poromov * @return {@link BackupManager#SUCCESS} on success; nonzero on error. 584fe06bf64d204c459699b0bf6465f9fb69208345eSergey Poromov * @exception IllegalArgumentException on null or empty {@code packages} param. 585fe06bf64d204c459699b0bf6465f9fb69208345eSergey Poromov * 586fe06bf64d204c459699b0bf6465f9fb69208345eSergey Poromov * @hide 587fe06bf64d204c459699b0bf6465f9fb69208345eSergey Poromov */ 588fe06bf64d204c459699b0bf6465f9fb69208345eSergey Poromov @SystemApi 589d86b8fea43ebb6e5c31691b44d8ceb0d8d3c9072Jeff Sharkey @RequiresPermission(android.Manifest.permission.BACKUP) 590fe06bf64d204c459699b0bf6465f9fb69208345eSergey Poromov public int requestBackup(String[] packages, BackupObserver observer) { 591b1f573dca3ccdd113ca513f32c9964211ca71c78Stefanot return requestBackup(packages, observer, null, 0); 59238e7486d3870797f7cc6e2cd2f5b59462a66b1a6Shreyas Basarge } 59338e7486d3870797f7cc6e2cd2f5b59462a66b1a6Shreyas Basarge 59438e7486d3870797f7cc6e2cd2f5b59462a66b1a6Shreyas Basarge /** 59538e7486d3870797f7cc6e2cd2f5b59462a66b1a6Shreyas Basarge * Request an immediate backup, providing an observer to which results of the backup operation 59638e7486d3870797f7cc6e2cd2f5b59462a66b1a6Shreyas Basarge * will be published. The Android backup system will decide for each package whether it will 59738e7486d3870797f7cc6e2cd2f5b59462a66b1a6Shreyas Basarge * be full app data backup or key/value-pair-based backup. 59838e7486d3870797f7cc6e2cd2f5b59462a66b1a6Shreyas Basarge * 59938e7486d3870797f7cc6e2cd2f5b59462a66b1a6Shreyas Basarge * <p>If this method returns {@link BackupManager#SUCCESS}, the OS will attempt to backup all 60038e7486d3870797f7cc6e2cd2f5b59462a66b1a6Shreyas Basarge * provided packages using the remote transport. 60138e7486d3870797f7cc6e2cd2f5b59462a66b1a6Shreyas Basarge * 60238e7486d3870797f7cc6e2cd2f5b59462a66b1a6Shreyas Basarge * @param packages List of package names to backup. 60338e7486d3870797f7cc6e2cd2f5b59462a66b1a6Shreyas Basarge * @param observer The {@link BackupObserver} to receive callbacks during the backup 60438e7486d3870797f7cc6e2cd2f5b59462a66b1a6Shreyas Basarge * operation. Could be {@code null}. 605b1f573dca3ccdd113ca513f32c9964211ca71c78Stefanot * @param monitor The {@link BackupManagerMonitorWrapper} to receive callbacks of important 606b1f573dca3ccdd113ca513f32c9964211ca71c78Stefanot * events during the backup operation. Could be {@code null}. 60738e7486d3870797f7cc6e2cd2f5b59462a66b1a6Shreyas Basarge * @param flags {@link #FLAG_NON_INCREMENTAL_BACKUP}. 60838e7486d3870797f7cc6e2cd2f5b59462a66b1a6Shreyas Basarge * @return {@link BackupManager#SUCCESS} on success; nonzero on error. 60938e7486d3870797f7cc6e2cd2f5b59462a66b1a6Shreyas Basarge * @throws IllegalArgumentException on null or empty {@code packages} param. 61038e7486d3870797f7cc6e2cd2f5b59462a66b1a6Shreyas Basarge * @hide 61138e7486d3870797f7cc6e2cd2f5b59462a66b1a6Shreyas Basarge */ 61238e7486d3870797f7cc6e2cd2f5b59462a66b1a6Shreyas Basarge @SystemApi 613d86b8fea43ebb6e5c31691b44d8ceb0d8d3c9072Jeff Sharkey @RequiresPermission(android.Manifest.permission.BACKUP) 614b1f573dca3ccdd113ca513f32c9964211ca71c78Stefanot public int requestBackup(String[] packages, BackupObserver observer, 615b1f573dca3ccdd113ca513f32c9964211ca71c78Stefanot BackupManagerMonitor monitor, int flags) { 616fe06bf64d204c459699b0bf6465f9fb69208345eSergey Poromov checkServiceBinder(); 617fe06bf64d204c459699b0bf6465f9fb69208345eSergey Poromov if (sService != null) { 618fe06bf64d204c459699b0bf6465f9fb69208345eSergey Poromov try { 619899edf6f4fef302ff219ee346b47afc5c8b640aaSergey Poromov BackupObserverWrapper observerWrapper = observer == null 620899edf6f4fef302ff219ee346b47afc5c8b640aaSergey Poromov ? null 621899edf6f4fef302ff219ee346b47afc5c8b640aaSergey Poromov : new BackupObserverWrapper(mContext, observer); 622b1f573dca3ccdd113ca513f32c9964211ca71c78Stefanot BackupManagerMonitorWrapper monitorWrapper = monitor == null 623b1f573dca3ccdd113ca513f32c9964211ca71c78Stefanot ? null 624b1f573dca3ccdd113ca513f32c9964211ca71c78Stefanot : new BackupManagerMonitorWrapper(monitor); 625b1f573dca3ccdd113ca513f32c9964211ca71c78Stefanot return sService.requestBackup(packages, observerWrapper, monitorWrapper, flags); 626fe06bf64d204c459699b0bf6465f9fb69208345eSergey Poromov } catch (RemoteException e) { 627fe06bf64d204c459699b0bf6465f9fb69208345eSergey Poromov Log.e(TAG, "requestBackup() couldn't connect"); 628fe06bf64d204c459699b0bf6465f9fb69208345eSergey Poromov } 629fe06bf64d204c459699b0bf6465f9fb69208345eSergey Poromov } 630fe06bf64d204c459699b0bf6465f9fb69208345eSergey Poromov return -1; 631fe06bf64d204c459699b0bf6465f9fb69208345eSergey Poromov } 632fe06bf64d204c459699b0bf6465f9fb69208345eSergey Poromov 633c3704427c9a34397fd90bf5438adae2eebcc97f6Shreyas Basarge /** 634c3704427c9a34397fd90bf5438adae2eebcc97f6Shreyas Basarge * Cancel all running backups. After this call returns, no currently running backups will 635c3704427c9a34397fd90bf5438adae2eebcc97f6Shreyas Basarge * interact with the selected transport. 636c3704427c9a34397fd90bf5438adae2eebcc97f6Shreyas Basarge * 637c3704427c9a34397fd90bf5438adae2eebcc97f6Shreyas Basarge * @hide 638c3704427c9a34397fd90bf5438adae2eebcc97f6Shreyas Basarge */ 639c3704427c9a34397fd90bf5438adae2eebcc97f6Shreyas Basarge @SystemApi 640d86b8fea43ebb6e5c31691b44d8ceb0d8d3c9072Jeff Sharkey @RequiresPermission(android.Manifest.permission.BACKUP) 641c3704427c9a34397fd90bf5438adae2eebcc97f6Shreyas Basarge public void cancelBackups() { 642c3704427c9a34397fd90bf5438adae2eebcc97f6Shreyas Basarge checkServiceBinder(); 643c3704427c9a34397fd90bf5438adae2eebcc97f6Shreyas Basarge if (sService != null) { 644c3704427c9a34397fd90bf5438adae2eebcc97f6Shreyas Basarge try { 645c3704427c9a34397fd90bf5438adae2eebcc97f6Shreyas Basarge sService.cancelBackups(); 646c3704427c9a34397fd90bf5438adae2eebcc97f6Shreyas Basarge } catch (RemoteException e) { 647c3704427c9a34397fd90bf5438adae2eebcc97f6Shreyas Basarge Log.e(TAG, "cancelBackups() couldn't connect."); 648c3704427c9a34397fd90bf5438adae2eebcc97f6Shreyas Basarge } 649c3704427c9a34397fd90bf5438adae2eebcc97f6Shreyas Basarge } 650c3704427c9a34397fd90bf5438adae2eebcc97f6Shreyas Basarge } 651c3704427c9a34397fd90bf5438adae2eebcc97f6Shreyas Basarge 652fe06bf64d204c459699b0bf6465f9fb69208345eSergey Poromov /* 653fe06bf64d204c459699b0bf6465f9fb69208345eSergey Poromov * We wrap incoming binder calls with a private class implementation that 654fe06bf64d204c459699b0bf6465f9fb69208345eSergey Poromov * redirects them into main-thread actions. This serializes the backup 655fe06bf64d204c459699b0bf6465f9fb69208345eSergey Poromov * progress callbacks nicely within the usual main-thread lifecycle pattern. 656fe06bf64d204c459699b0bf6465f9fb69208345eSergey Poromov */ 657fe06bf64d204c459699b0bf6465f9fb69208345eSergey Poromov @SystemApi 658fe06bf64d204c459699b0bf6465f9fb69208345eSergey Poromov private class BackupObserverWrapper extends IBackupObserver.Stub { 659fe06bf64d204c459699b0bf6465f9fb69208345eSergey Poromov final Handler mHandler; 660fe06bf64d204c459699b0bf6465f9fb69208345eSergey Poromov final BackupObserver mObserver; 661fe06bf64d204c459699b0bf6465f9fb69208345eSergey Poromov 662fe06bf64d204c459699b0bf6465f9fb69208345eSergey Poromov static final int MSG_UPDATE = 1; 663fe06bf64d204c459699b0bf6465f9fb69208345eSergey Poromov static final int MSG_RESULT = 2; 664fe06bf64d204c459699b0bf6465f9fb69208345eSergey Poromov static final int MSG_FINISHED = 3; 665fe06bf64d204c459699b0bf6465f9fb69208345eSergey Poromov 666fe06bf64d204c459699b0bf6465f9fb69208345eSergey Poromov BackupObserverWrapper(Context context, BackupObserver observer) { 667fe06bf64d204c459699b0bf6465f9fb69208345eSergey Poromov mHandler = new Handler(context.getMainLooper()) { 668fe06bf64d204c459699b0bf6465f9fb69208345eSergey Poromov @Override 669fe06bf64d204c459699b0bf6465f9fb69208345eSergey Poromov public void handleMessage(Message msg) { 670fe06bf64d204c459699b0bf6465f9fb69208345eSergey Poromov switch (msg.what) { 671fe06bf64d204c459699b0bf6465f9fb69208345eSergey Poromov case MSG_UPDATE: 672fe06bf64d204c459699b0bf6465f9fb69208345eSergey Poromov Pair<String, BackupProgress> obj = 673fe06bf64d204c459699b0bf6465f9fb69208345eSergey Poromov (Pair<String, BackupProgress>) msg.obj; 674fe06bf64d204c459699b0bf6465f9fb69208345eSergey Poromov mObserver.onUpdate(obj.first, obj.second); 675fe06bf64d204c459699b0bf6465f9fb69208345eSergey Poromov break; 676fe06bf64d204c459699b0bf6465f9fb69208345eSergey Poromov case MSG_RESULT: 677fe06bf64d204c459699b0bf6465f9fb69208345eSergey Poromov mObserver.onResult((String)msg.obj, msg.arg1); 678fe06bf64d204c459699b0bf6465f9fb69208345eSergey Poromov break; 679fe06bf64d204c459699b0bf6465f9fb69208345eSergey Poromov case MSG_FINISHED: 680fe06bf64d204c459699b0bf6465f9fb69208345eSergey Poromov mObserver.backupFinished(msg.arg1); 681fe06bf64d204c459699b0bf6465f9fb69208345eSergey Poromov break; 682fe06bf64d204c459699b0bf6465f9fb69208345eSergey Poromov default: 683fe06bf64d204c459699b0bf6465f9fb69208345eSergey Poromov Log.w(TAG, "Unknown message: " + msg); 684fe06bf64d204c459699b0bf6465f9fb69208345eSergey Poromov break; 685fe06bf64d204c459699b0bf6465f9fb69208345eSergey Poromov } 686fe06bf64d204c459699b0bf6465f9fb69208345eSergey Poromov } 687fe06bf64d204c459699b0bf6465f9fb69208345eSergey Poromov }; 688fe06bf64d204c459699b0bf6465f9fb69208345eSergey Poromov mObserver = observer; 689fe06bf64d204c459699b0bf6465f9fb69208345eSergey Poromov } 690fe06bf64d204c459699b0bf6465f9fb69208345eSergey Poromov 691fe06bf64d204c459699b0bf6465f9fb69208345eSergey Poromov // Binder calls into this object just enqueue on the main-thread handler 692fe06bf64d204c459699b0bf6465f9fb69208345eSergey Poromov @Override 693fe06bf64d204c459699b0bf6465f9fb69208345eSergey Poromov public void onUpdate(String currentPackage, BackupProgress backupProgress) { 694fe06bf64d204c459699b0bf6465f9fb69208345eSergey Poromov mHandler.sendMessage( 695fe06bf64d204c459699b0bf6465f9fb69208345eSergey Poromov mHandler.obtainMessage(MSG_UPDATE, Pair.create(currentPackage, backupProgress))); 696fe06bf64d204c459699b0bf6465f9fb69208345eSergey Poromov } 697fe06bf64d204c459699b0bf6465f9fb69208345eSergey Poromov 698fe06bf64d204c459699b0bf6465f9fb69208345eSergey Poromov @Override 699fe06bf64d204c459699b0bf6465f9fb69208345eSergey Poromov public void onResult(String currentPackage, int status) { 700fe06bf64d204c459699b0bf6465f9fb69208345eSergey Poromov mHandler.sendMessage( 7014b51d99a019f9b14e5698ba16e9540c179bdcd05Sergey Poromov mHandler.obtainMessage(MSG_RESULT, status, 0, currentPackage)); 702fe06bf64d204c459699b0bf6465f9fb69208345eSergey Poromov } 703fe06bf64d204c459699b0bf6465f9fb69208345eSergey Poromov 704fe06bf64d204c459699b0bf6465f9fb69208345eSergey Poromov @Override 705fe06bf64d204c459699b0bf6465f9fb69208345eSergey Poromov public void backupFinished(int status) { 706fe06bf64d204c459699b0bf6465f9fb69208345eSergey Poromov mHandler.sendMessage( 707fe06bf64d204c459699b0bf6465f9fb69208345eSergey Poromov mHandler.obtainMessage(MSG_FINISHED, status, 0)); 708fe06bf64d204c459699b0bf6465f9fb69208345eSergey Poromov } 709fe06bf64d204c459699b0bf6465f9fb69208345eSergey Poromov } 710865303fce57b968f5bd7efb4dd23ccb1a3747b93Shreyas Basarge 711865303fce57b968f5bd7efb4dd23ccb1a3747b93Shreyas Basarge private class SelectTransportListenerWrapper extends ISelectBackupTransportCallback.Stub { 712865303fce57b968f5bd7efb4dd23ccb1a3747b93Shreyas Basarge 713865303fce57b968f5bd7efb4dd23ccb1a3747b93Shreyas Basarge private final Handler mHandler; 714865303fce57b968f5bd7efb4dd23ccb1a3747b93Shreyas Basarge private final SelectBackupTransportCallback mListener; 715865303fce57b968f5bd7efb4dd23ccb1a3747b93Shreyas Basarge 716865303fce57b968f5bd7efb4dd23ccb1a3747b93Shreyas Basarge SelectTransportListenerWrapper(Context context, SelectBackupTransportCallback listener) { 717865303fce57b968f5bd7efb4dd23ccb1a3747b93Shreyas Basarge mHandler = new Handler(context.getMainLooper()); 718865303fce57b968f5bd7efb4dd23ccb1a3747b93Shreyas Basarge mListener = listener; 719865303fce57b968f5bd7efb4dd23ccb1a3747b93Shreyas Basarge } 720865303fce57b968f5bd7efb4dd23ccb1a3747b93Shreyas Basarge 721865303fce57b968f5bd7efb4dd23ccb1a3747b93Shreyas Basarge @Override 722865303fce57b968f5bd7efb4dd23ccb1a3747b93Shreyas Basarge public void onSuccess(final String transportName) { 723865303fce57b968f5bd7efb4dd23ccb1a3747b93Shreyas Basarge mHandler.post(new Runnable() { 724865303fce57b968f5bd7efb4dd23ccb1a3747b93Shreyas Basarge @Override 725865303fce57b968f5bd7efb4dd23ccb1a3747b93Shreyas Basarge public void run() { 726865303fce57b968f5bd7efb4dd23ccb1a3747b93Shreyas Basarge mListener.onSuccess(transportName); 727865303fce57b968f5bd7efb4dd23ccb1a3747b93Shreyas Basarge } 728865303fce57b968f5bd7efb4dd23ccb1a3747b93Shreyas Basarge }); 729865303fce57b968f5bd7efb4dd23ccb1a3747b93Shreyas Basarge } 730865303fce57b968f5bd7efb4dd23ccb1a3747b93Shreyas Basarge 731865303fce57b968f5bd7efb4dd23ccb1a3747b93Shreyas Basarge @Override 732865303fce57b968f5bd7efb4dd23ccb1a3747b93Shreyas Basarge public void onFailure(final int reason) { 733865303fce57b968f5bd7efb4dd23ccb1a3747b93Shreyas Basarge mHandler.post(new Runnable() { 734865303fce57b968f5bd7efb4dd23ccb1a3747b93Shreyas Basarge @Override 735865303fce57b968f5bd7efb4dd23ccb1a3747b93Shreyas Basarge public void run() { 736865303fce57b968f5bd7efb4dd23ccb1a3747b93Shreyas Basarge mListener.onFailure(reason); 737865303fce57b968f5bd7efb4dd23ccb1a3747b93Shreyas Basarge } 738865303fce57b968f5bd7efb4dd23ccb1a3747b93Shreyas Basarge }); 739865303fce57b968f5bd7efb4dd23ccb1a3747b93Shreyas Basarge } 740865303fce57b968f5bd7efb4dd23ccb1a3747b93Shreyas Basarge } 741b1f573dca3ccdd113ca513f32c9964211ca71c78Stefanot 742b1f573dca3ccdd113ca513f32c9964211ca71c78Stefanot private class BackupManagerMonitorWrapper extends IBackupManagerMonitor.Stub { 743b1f573dca3ccdd113ca513f32c9964211ca71c78Stefanot final BackupManagerMonitor mMonitor; 744b1f573dca3ccdd113ca513f32c9964211ca71c78Stefanot 745b1f573dca3ccdd113ca513f32c9964211ca71c78Stefanot BackupManagerMonitorWrapper(BackupManagerMonitor monitor) { 746b1f573dca3ccdd113ca513f32c9964211ca71c78Stefanot mMonitor = monitor; 747b1f573dca3ccdd113ca513f32c9964211ca71c78Stefanot } 748b1f573dca3ccdd113ca513f32c9964211ca71c78Stefanot 749b1f573dca3ccdd113ca513f32c9964211ca71c78Stefanot @Override 750b1f573dca3ccdd113ca513f32c9964211ca71c78Stefanot public void onEvent(final Bundle event) throws RemoteException { 751b1f573dca3ccdd113ca513f32c9964211ca71c78Stefanot mMonitor.onEvent(event); 752b1f573dca3ccdd113ca513f32c9964211ca71c78Stefanot } 753b1f573dca3ccdd113ca513f32c9964211ca71c78Stefanot } 754b1f573dca3ccdd113ca513f32c9964211ca71c78Stefanot 755a8bf815c6153290b173f34b071dddb0a0034a115Christopher Tate} 756