Christopher Tate | 80202c8 | 2010-01-25 19:37:47 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2010 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
Christopher Tate | 4528186 | 2010-03-05 15:46:30 -0800 | [diff] [blame] | 17 | package android.app.backup; |
Christopher Tate | 80202c8 | 2010-01-25 19:37:47 -0800 | [diff] [blame] | 18 | |
Christopher Tate | 4528186 | 2010-03-05 15:46:30 -0800 | [diff] [blame] | 19 | import android.app.backup.RestoreObserver; |
| 20 | import android.app.backup.RestoreSet; |
| 21 | import android.app.backup.IRestoreObserver; |
| 22 | import android.app.backup.IRestoreSession; |
Christopher Tate | 80202c8 | 2010-01-25 19:37:47 -0800 | [diff] [blame] | 23 | import android.content.Context; |
| 24 | import android.os.Handler; |
Christopher Tate | d3cd359 | 2010-02-01 15:31:09 -0800 | [diff] [blame] | 25 | import android.os.Message; |
Christopher Tate | 80202c8 | 2010-01-25 19:37:47 -0800 | [diff] [blame] | 26 | import android.os.RemoteException; |
| 27 | import android.util.Log; |
| 28 | |
| 29 | /** |
Christopher Tate | 9c3cee9 | 2010-03-25 16:06:43 -0700 | [diff] [blame] | 30 | * Interface for managing a restore session. |
| 31 | * @hide |
Christopher Tate | 80202c8 | 2010-01-25 19:37:47 -0800 | [diff] [blame] | 32 | */ |
| 33 | public class RestoreSession { |
| 34 | static final String TAG = "RestoreSession"; |
| 35 | |
| 36 | final Context mContext; |
| 37 | IRestoreSession mBinder; |
| 38 | RestoreObserverWrapper mObserver = null; |
| 39 | |
| 40 | /** |
| 41 | * Ask the current transport what the available restore sets are. |
| 42 | * |
Christopher Tate | 2d449afe | 2010-03-29 19:14:24 -0700 | [diff] [blame] | 43 | * @param observer a RestoreObserver object whose restoreSetsAvailable() method will |
| 44 | * be called on the application's main thread in order to supply the results of |
| 45 | * the restore set lookup by the backup transport. This parameter must not be |
| 46 | * null. |
| 47 | * @return Zero on success, nonzero on error. The observer's restoreSetsAvailable() |
| 48 | * method will only be called if this method returned zero. |
Christopher Tate | 80202c8 | 2010-01-25 19:37:47 -0800 | [diff] [blame] | 49 | */ |
Christopher Tate | 2d449afe | 2010-03-29 19:14:24 -0700 | [diff] [blame] | 50 | public int getAvailableRestoreSets(RestoreObserver observer) { |
| 51 | int err = -1; |
| 52 | RestoreObserverWrapper obsWrapper = new RestoreObserverWrapper(mContext, observer); |
Christopher Tate | 80202c8 | 2010-01-25 19:37:47 -0800 | [diff] [blame] | 53 | try { |
Christopher Tate | 2d449afe | 2010-03-29 19:14:24 -0700 | [diff] [blame] | 54 | err = mBinder.getAvailableRestoreSets(obsWrapper); |
Christopher Tate | 80202c8 | 2010-01-25 19:37:47 -0800 | [diff] [blame] | 55 | } catch (RemoteException e) { |
| 56 | Log.d(TAG, "Can't contact server to get available sets"); |
Christopher Tate | 80202c8 | 2010-01-25 19:37:47 -0800 | [diff] [blame] | 57 | } |
Christopher Tate | 2d449afe | 2010-03-29 19:14:24 -0700 | [diff] [blame] | 58 | return err; |
Christopher Tate | 80202c8 | 2010-01-25 19:37:47 -0800 | [diff] [blame] | 59 | } |
| 60 | |
| 61 | /** |
| 62 | * Restore the given set onto the device, replacing the current data of any app |
| 63 | * contained in the restore set with the data previously backed up. |
| 64 | * |
Christopher Tate | 8472581 | 2010-02-04 15:52:40 -0800 | [diff] [blame] | 65 | * <p>Callers must hold the android.permission.BACKUP permission to use this method. |
| 66 | * |
Christopher Tate | 80202c8 | 2010-01-25 19:37:47 -0800 | [diff] [blame] | 67 | * @return Zero on success; nonzero on error. The observer will only receive |
| 68 | * progress callbacks if this method returned zero. |
Kenny Root | 7951eaa | 2010-02-17 13:55:17 -0800 | [diff] [blame] | 69 | * @param token The token from {@link #getAvailableRestoreSets()} corresponding to |
Christopher Tate | 80202c8 | 2010-01-25 19:37:47 -0800 | [diff] [blame] | 70 | * the restore set that should be used. |
Christopher Tate | 8472581 | 2010-02-04 15:52:40 -0800 | [diff] [blame] | 71 | * @param observer If non-null, this binder points to an object that will receive |
| 72 | * progress callbacks during the restore operation. |
Christopher Tate | 80202c8 | 2010-01-25 19:37:47 -0800 | [diff] [blame] | 73 | */ |
Christopher Tate | 8472581 | 2010-02-04 15:52:40 -0800 | [diff] [blame] | 74 | public int restoreAll(long token, RestoreObserver observer) { |
Christopher Tate | 80202c8 | 2010-01-25 19:37:47 -0800 | [diff] [blame] | 75 | int err = -1; |
| 76 | if (mObserver != null) { |
Christopher Tate | 8472581 | 2010-02-04 15:52:40 -0800 | [diff] [blame] | 77 | Log.d(TAG, "restoreAll() called during active restore"); |
Christopher Tate | 80202c8 | 2010-01-25 19:37:47 -0800 | [diff] [blame] | 78 | return -1; |
| 79 | } |
| 80 | mObserver = new RestoreObserverWrapper(mContext, observer); |
| 81 | try { |
Christopher Tate | 8472581 | 2010-02-04 15:52:40 -0800 | [diff] [blame] | 82 | err = mBinder.restoreAll(token, mObserver); |
Christopher Tate | 80202c8 | 2010-01-25 19:37:47 -0800 | [diff] [blame] | 83 | } catch (RemoteException e) { |
Christopher Tate | 8472581 | 2010-02-04 15:52:40 -0800 | [diff] [blame] | 84 | Log.d(TAG, "Can't contact server to restore"); |
| 85 | } |
| 86 | return err; |
| 87 | } |
| 88 | |
| 89 | /** |
Christopher Tate | 284f1bb | 2011-07-07 14:31:18 -0700 | [diff] [blame] | 90 | * Restore select packages from the given set onto the device, replacing the |
| 91 | * current data of any app contained in the set with the data previously |
| 92 | * backed up. |
| 93 | * |
| 94 | * <p>Callers must hold the android.permission.BACKUP permission to use this method. |
| 95 | * |
| 96 | * @return Zero on success, nonzero on error. The observer will only receive |
| 97 | * progress callbacks if this method returned zero. |
| 98 | * @param token The token from {@link getAvailableRestoreSets()} corresponding to |
| 99 | * the restore set that should be used. |
| 100 | * @param observer If non-null, this binder points to an object that will receive |
| 101 | * progress callbacks during the restore operation. |
| 102 | * @param packages The set of packages for which to attempt a restore. Regardless of |
| 103 | * the contents of the actual back-end dataset named by {@code token}, only |
| 104 | * applications mentioned in this list will have their data restored. |
| 105 | * |
| 106 | * @hide |
| 107 | */ |
| 108 | public int restoreSome(long token, RestoreObserver observer, String[] packages) { |
| 109 | int err = -1; |
| 110 | if (mObserver != null) { |
| 111 | Log.d(TAG, "restoreAll() called during active restore"); |
| 112 | return -1; |
| 113 | } |
| 114 | mObserver = new RestoreObserverWrapper(mContext, observer); |
| 115 | try { |
| 116 | err = mBinder.restoreSome(token, mObserver, packages); |
| 117 | } catch (RemoteException e) { |
| 118 | Log.d(TAG, "Can't contact server to restore packages"); |
| 119 | } |
| 120 | return err; |
| 121 | } |
| 122 | |
| 123 | /** |
Christopher Tate | 8472581 | 2010-02-04 15:52:40 -0800 | [diff] [blame] | 124 | * Restore a single application from backup. The data will be restored from the |
| 125 | * current backup dataset if the given package has stored data there, or from |
| 126 | * the dataset used during the last full device setup operation if the current |
| 127 | * backup dataset has no matching data. If no backup data exists for this package |
| 128 | * in either source, a nonzero value will be returned. |
| 129 | * |
| 130 | * @return Zero on success; nonzero on error. The observer will only receive |
| 131 | * progress callbacks if this method returned zero. |
| 132 | * @param packageName The name of the package whose data to restore. If this is |
| 133 | * not the name of the caller's own package, then the android.permission.BACKUP |
| 134 | * permission must be held. |
| 135 | * @param observer If non-null, this binder points to an object that will receive |
| 136 | * progress callbacks during the restore operation. |
| 137 | */ |
| 138 | public int restorePackage(String packageName, RestoreObserver observer) { |
| 139 | int err = -1; |
| 140 | if (mObserver != null) { |
| 141 | Log.d(TAG, "restorePackage() called during active restore"); |
| 142 | return -1; |
| 143 | } |
| 144 | mObserver = new RestoreObserverWrapper(mContext, observer); |
| 145 | try { |
| 146 | err = mBinder.restorePackage(packageName, mObserver); |
| 147 | } catch (RemoteException e) { |
| 148 | Log.d(TAG, "Can't contact server to restore package"); |
Christopher Tate | 80202c8 | 2010-01-25 19:37:47 -0800 | [diff] [blame] | 149 | } |
| 150 | return err; |
| 151 | } |
| 152 | |
| 153 | /** |
| 154 | * End this restore session. After this method is called, the RestoreSession |
| 155 | * object is no longer valid. |
| 156 | * |
| 157 | * <p><b>Note:</b> The caller <i>must</i> invoke this method to end the restore session, |
Kenny Root | 7951eaa | 2010-02-17 13:55:17 -0800 | [diff] [blame] | 158 | * even if {@link #restorePackage(String, RestoreObserver)} failed. |
Christopher Tate | 80202c8 | 2010-01-25 19:37:47 -0800 | [diff] [blame] | 159 | */ |
| 160 | public void endRestoreSession() { |
| 161 | try { |
| 162 | mBinder.endRestoreSession(); |
| 163 | } catch (RemoteException e) { |
| 164 | Log.d(TAG, "Can't contact server to get available sets"); |
| 165 | } finally { |
| 166 | mBinder = null; |
| 167 | } |
| 168 | } |
| 169 | |
| 170 | /* |
| 171 | * Nonpublic implementation here |
| 172 | */ |
| 173 | |
| 174 | RestoreSession(Context context, IRestoreSession binder) { |
| 175 | mContext = context; |
| 176 | mBinder = binder; |
| 177 | } |
| 178 | |
| 179 | /* |
| 180 | * We wrap incoming binder calls with a private class implementation that |
Christopher Tate | 44a2790 | 2010-01-27 17:15:49 -0800 | [diff] [blame] | 181 | * redirects them into main-thread actions. This serializes the restore |
Christopher Tate | 80202c8 | 2010-01-25 19:37:47 -0800 | [diff] [blame] | 182 | * progress callbacks nicely within the usual main-thread lifecycle pattern. |
| 183 | */ |
| 184 | private class RestoreObserverWrapper extends IRestoreObserver.Stub { |
| 185 | final Handler mHandler; |
| 186 | final RestoreObserver mAppObserver; |
| 187 | |
Christopher Tate | d3cd359 | 2010-02-01 15:31:09 -0800 | [diff] [blame] | 188 | static final int MSG_RESTORE_STARTING = 1; |
| 189 | static final int MSG_UPDATE = 2; |
| 190 | static final int MSG_RESTORE_FINISHED = 3; |
Christopher Tate | 2d449afe | 2010-03-29 19:14:24 -0700 | [diff] [blame] | 191 | static final int MSG_RESTORE_SETS_AVAILABLE = 4; |
Christopher Tate | d3cd359 | 2010-02-01 15:31:09 -0800 | [diff] [blame] | 192 | |
Christopher Tate | 80202c8 | 2010-01-25 19:37:47 -0800 | [diff] [blame] | 193 | RestoreObserverWrapper(Context context, RestoreObserver appObserver) { |
Christopher Tate | d3cd359 | 2010-02-01 15:31:09 -0800 | [diff] [blame] | 194 | mHandler = new Handler(context.getMainLooper()) { |
| 195 | @Override |
| 196 | public void handleMessage(Message msg) { |
| 197 | switch (msg.what) { |
| 198 | case MSG_RESTORE_STARTING: |
| 199 | mAppObserver.restoreStarting(msg.arg1); |
| 200 | break; |
| 201 | case MSG_UPDATE: |
Christopher Tate | 9c3cee9 | 2010-03-25 16:06:43 -0700 | [diff] [blame] | 202 | mAppObserver.onUpdate(msg.arg1, (String)msg.obj); |
Christopher Tate | d3cd359 | 2010-02-01 15:31:09 -0800 | [diff] [blame] | 203 | break; |
| 204 | case MSG_RESTORE_FINISHED: |
| 205 | mAppObserver.restoreFinished(msg.arg1); |
| 206 | break; |
Christopher Tate | 2d449afe | 2010-03-29 19:14:24 -0700 | [diff] [blame] | 207 | case MSG_RESTORE_SETS_AVAILABLE: |
| 208 | mAppObserver.restoreSetsAvailable((RestoreSet[])msg.obj); |
| 209 | break; |
Christopher Tate | d3cd359 | 2010-02-01 15:31:09 -0800 | [diff] [blame] | 210 | } |
| 211 | } |
| 212 | }; |
Christopher Tate | 80202c8 | 2010-01-25 19:37:47 -0800 | [diff] [blame] | 213 | mAppObserver = appObserver; |
| 214 | } |
| 215 | |
Christopher Tate | d3cd359 | 2010-02-01 15:31:09 -0800 | [diff] [blame] | 216 | // Binder calls into this object just enqueue on the main-thread handler |
Christopher Tate | 2d449afe | 2010-03-29 19:14:24 -0700 | [diff] [blame] | 217 | public void restoreSetsAvailable(RestoreSet[] result) { |
| 218 | mHandler.sendMessage( |
| 219 | mHandler.obtainMessage(MSG_RESTORE_SETS_AVAILABLE, result)); |
| 220 | } |
| 221 | |
Christopher Tate | 80202c8 | 2010-01-25 19:37:47 -0800 | [diff] [blame] | 222 | public void restoreStarting(int numPackages) { |
Christopher Tate | d3cd359 | 2010-02-01 15:31:09 -0800 | [diff] [blame] | 223 | mHandler.sendMessage( |
| 224 | mHandler.obtainMessage(MSG_RESTORE_STARTING, numPackages, 0)); |
Christopher Tate | 80202c8 | 2010-01-25 19:37:47 -0800 | [diff] [blame] | 225 | } |
| 226 | |
Christopher Tate | 9c3cee9 | 2010-03-25 16:06:43 -0700 | [diff] [blame] | 227 | public void onUpdate(int nowBeingRestored, String currentPackage) { |
Christopher Tate | d3cd359 | 2010-02-01 15:31:09 -0800 | [diff] [blame] | 228 | mHandler.sendMessage( |
Christopher Tate | 9c3cee9 | 2010-03-25 16:06:43 -0700 | [diff] [blame] | 229 | mHandler.obtainMessage(MSG_UPDATE, nowBeingRestored, 0, currentPackage)); |
Christopher Tate | 80202c8 | 2010-01-25 19:37:47 -0800 | [diff] [blame] | 230 | } |
| 231 | |
| 232 | public void restoreFinished(int error) { |
Christopher Tate | d3cd359 | 2010-02-01 15:31:09 -0800 | [diff] [blame] | 233 | mHandler.sendMessage( |
| 234 | mHandler.obtainMessage(MSG_RESTORE_FINISHED, error, 0)); |
Christopher Tate | 80202c8 | 2010-01-25 19:37:47 -0800 | [diff] [blame] | 235 | } |
| 236 | } |
| 237 | } |