Bmgr.java revision c73a218c2663e6ae3ec8a9ab8b9524f95702ade9
1/* 2 * Copyright (C) 2009 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17package com.android.commands.bmgr; 18 19import android.backup.IBackupManager; 20import android.backup.IRestoreObserver; 21import android.backup.IRestoreSession; 22import android.backup.RestoreSet; 23import android.os.RemoteException; 24import android.os.ServiceManager; 25 26public final class Bmgr { 27 IBackupManager mBmgr; 28 IRestoreSession mRestore; 29 30 static final String BMGR_NOT_RUNNING_ERR = 31 "Error: Could not access the Backup Manager. Is the system running?"; 32 static final String TRANSPORT_NOT_RUNNING_ERR = 33 "Error: Could not access the backup transport. Is the system running?"; 34 35 private String[] mArgs; 36 private int mNextArg; 37 private String mCurArgData; 38 39 public static void main(String[] args) { 40 try { 41 new Bmgr().run(args); 42 } catch (Exception e) { 43 System.err.println("Exception caught:"); 44 e.printStackTrace(); 45 } 46 } 47 48 public void run(String[] args) { 49 boolean validCommand = false; 50 if (args.length < 1) { 51 showUsage(); 52 return; 53 } 54 55 mBmgr = IBackupManager.Stub.asInterface(ServiceManager.getService("backup")); 56 if (mBmgr == null) { 57 System.err.println(BMGR_NOT_RUNNING_ERR); 58 return; 59 } 60 61 mArgs = args; 62 String op = args[0]; 63 mNextArg = 1; 64 65 if ("run".equals(op)) { 66 doRun(); 67 return; 68 } 69 70 if ("backup".equals(op)) { 71 doBackup(); 72 return; 73 } 74 75 if ("list".equals(op)) { 76 doList(); 77 return; 78 } 79 80 if ("restore".equals(op)) { 81 doRestore(); 82 return; 83 } 84 85 if ("transport".equals(op)) { 86 doTransport(); 87 return; 88 } 89 90 System.err.println("Unknown command"); 91 showUsage(); 92 } 93 94 private void doRun() { 95 try { 96 mBmgr.backupNow(); 97 } catch (RemoteException e) { 98 System.err.println(e.toString()); 99 System.err.println(BMGR_NOT_RUNNING_ERR); 100 } 101 } 102 103 private void doBackup() { 104 boolean isFull = false; 105 String pkg = nextArg(); 106 if ("-f".equals(pkg)) { 107 isFull = true; 108 pkg = nextArg(); 109 } 110 111 if (pkg == null || pkg.startsWith("-")) { 112 showUsage(); 113 return; 114 } 115 116 try { 117 // !!! TODO: handle full backup 118 mBmgr.dataChanged(pkg); 119 } catch (RemoteException e) { 120 System.err.println(e.toString()); 121 System.err.println(BMGR_NOT_RUNNING_ERR); 122 } 123 } 124 125 private void doTransport() { 126 try { 127 int which = Integer.parseInt(nextArg()); 128 int old = mBmgr.selectBackupTransport(which); 129 System.out.println("Selected transport " + which + " (formerly " + old + ")"); 130 } catch (NumberFormatException e) { 131 showUsage(); 132 } catch (RemoteException e) { 133 System.err.println(e.toString()); 134 System.err.println(BMGR_NOT_RUNNING_ERR); 135 } 136 } 137 138 private void doList() { 139 String arg = nextArg(); // sets, transports, packages set# 140 if ("transports".equals(arg)) { 141 doListTransports(); 142 return; 143 } 144 145 // The rest of the 'list' options work with a restore session on the current transport 146 try { 147 int curTransport = mBmgr.getCurrentTransport(); 148 mRestore = mBmgr.beginRestoreSession(curTransport); 149 if (mRestore == null) { 150 System.err.println(BMGR_NOT_RUNNING_ERR); 151 return; 152 } 153 154 if ("sets".equals(arg)) { 155 doListRestoreSets(); 156 } 157 158 mRestore.endRestoreSession(); 159 } catch (RemoteException e) { 160 System.err.println(e.toString()); 161 System.err.println(BMGR_NOT_RUNNING_ERR); 162 } 163 } 164 165 private void doListTransports() { 166 } 167 168 private void doListRestoreSets() { 169 try { 170 RestoreSet[] sets = mRestore.getAvailableRestoreSets(); 171 if (sets == null || sets.length == 0) { 172 System.out.println("No restore sets available"); 173 } else { 174 printRestoreSets(sets); 175 } 176 } catch (RemoteException e) { 177 System.err.println(e.toString()); 178 System.err.println(TRANSPORT_NOT_RUNNING_ERR); 179 } 180 } 181 182 private void printRestoreSets(RestoreSet[] sets) { 183 for (RestoreSet s : sets) { 184 System.out.println(" " + s.token + " : " + s.name); 185 } 186 } 187 188 class RestoreObserver extends IRestoreObserver.Stub { 189 boolean done; 190 public void restoreStarting(int numPackages) { 191 System.out.println("restoreStarting: " + numPackages + " packages"); 192 } 193 194 public void onUpdate(int nowBeingRestored) { 195 System.out.println("onUpdate: " + nowBeingRestored); 196 } 197 198 public void restoreFinished(int error) { 199 System.out.println("restoreFinished: " + error); 200 synchronized (this) { 201 done = true; 202 this.notify(); 203 } 204 } 205 } 206 207 private void doRestore() { 208 int token; 209 try { 210 token = Integer.parseInt(nextArg()); 211 } catch (NumberFormatException e) { 212 showUsage(); 213 return; 214 } 215 216 RestoreObserver observer = new RestoreObserver(); 217 218 try { 219 boolean didRestore = false; 220 int curTransport = mBmgr.getCurrentTransport(); 221 mRestore = mBmgr.beginRestoreSession(curTransport); 222 if (mRestore == null) { 223 System.err.println(BMGR_NOT_RUNNING_ERR); 224 return; 225 } 226 RestoreSet[] sets = mRestore.getAvailableRestoreSets(); 227 for (RestoreSet s : sets) { 228 if (s.token == token) { 229 System.out.println("Scheduling restore: " + s.name); 230 mRestore.performRestore(token, observer); 231 didRestore = true; 232 break; 233 } 234 } 235 if (!didRestore) { 236 if (sets == null || sets.length == 0) { 237 System.out.println("No available restore sets; no restore performed"); 238 } else { 239 System.out.println("No matching restore set token. Available sets:"); 240 printRestoreSets(sets); 241 } 242 } 243 mRestore.endRestoreSession(); 244 } catch (RemoteException e) { 245 System.err.println(e.toString()); 246 System.err.println(BMGR_NOT_RUNNING_ERR); 247 } 248 249 // now wait for it to be done 250 synchronized (observer) { 251 while (!observer.done) { 252 try { 253 observer.wait(); 254 } catch (InterruptedException ex) { 255 } 256 } 257 } 258 System.out.println("done"); 259 } 260 261 private String nextArg() { 262 if (mNextArg >= mArgs.length) { 263 return null; 264 } 265 String arg = mArgs[mNextArg]; 266 mNextArg++; 267 return arg; 268 } 269 270 private static void showUsage() { 271 System.err.println("usage: bmgr [backup|restore|list|transport|run]"); 272 System.err.println(" bmgr backup [-f] package"); 273 System.err.println(" bmgr list sets"); 274 System.err.println(" #bmgr list transports"); 275 System.err.println(" #bmgr transport which#"); 276 System.err.println(" bmgr restore token#"); 277 System.err.println(" bmgr run"); 278 } 279} 280