Bmgr.java revision ace7f094bf07bbd90cb998b9462e4f2d101a498c
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.IRestoreSession; 21import android.backup.RestoreSet; 22import android.os.RemoteException; 23import android.os.ServiceManager; 24 25public final class Bmgr { 26 IBackupManager mBmgr; 27 IRestoreSession mRestore; 28 29 static final String BMGR_NOT_RUNNING_ERR = 30 "Error: Could not access the Backup Manager. Is the system running?"; 31 static final String TRANSPORT_NOT_RUNNING_ERR = 32 "Error: Could not access the backup transport. Is the system running?"; 33 34 private String[] mArgs; 35 private int mNextArg; 36 private String mCurArgData; 37 38 public static void main(String[] args) { 39 new Bmgr().run(args); 40 } 41 42 public void run(String[] args) { 43 boolean validCommand = false; 44 if (args.length < 1) { 45 showUsage(); 46 return; 47 } 48 49 mBmgr = IBackupManager.Stub.asInterface(ServiceManager.getService("backup")); 50 if (mBmgr == null) { 51 System.err.println(BMGR_NOT_RUNNING_ERR); 52 return; 53 } 54 55 mArgs = args; 56 String op = args[0]; 57 mNextArg = 1; 58 59 if ("run".equals(op)) { 60 doRun(); 61 return; 62 } 63 64 if ("backup".equals(op)) { 65 doBackup(); 66 return; 67 } 68 69 if ("list".equals(op)) { 70 doList(); 71 return; 72 } 73 } 74 75 private void doRun() { 76 try { 77 mBmgr.backupNow(); 78 } catch (RemoteException e) { 79 System.err.println(e.toString()); 80 System.err.println(BMGR_NOT_RUNNING_ERR); 81 } 82 } 83 84 private void doBackup() { 85 boolean isFull = false; 86 String pkg = nextArg(); 87 if ("-f".equals(pkg)) { 88 isFull = true; 89 pkg = nextArg(); 90 } 91 92 if (pkg == null || pkg.startsWith("-")) { 93 showUsage(); 94 return; 95 } 96 97 try { 98 // !!! TODO: handle full backup 99 mBmgr.dataChanged(pkg); 100 } catch (RemoteException e) { 101 System.err.println(e.toString()); 102 System.err.println(BMGR_NOT_RUNNING_ERR); 103 } 104 } 105 106 private void doList() { 107 String arg = nextArg(); // sets, transports, packages set# 108 if ("transports".equals(arg)) { 109 doListTransports(); 110 return; 111 } 112 113 // The rest of the 'list' options work with a restore session on the current transport 114 try { 115 int curTransport = mBmgr.getCurrentTransport(); 116 mRestore = mBmgr.beginRestoreSession(curTransport); 117 118 if ("sets".equals(arg)) { 119 doListRestoreSets(); 120 } 121 122 mRestore.endRestoreSession(); 123 } catch (RemoteException e) { 124 System.err.println(e.toString()); 125 System.err.println(BMGR_NOT_RUNNING_ERR); 126 } 127 } 128 129 private void doListTransports() { 130 131 } 132 133 private void doListRestoreSets() { 134 try { 135 RestoreSet[] sets = mRestore.getAvailableRestoreSets(); 136 if (sets.length == 0) { 137 System.out.println("No restore sets available"); 138 } else { 139 for (RestoreSet s : sets) { 140 System.out.println(" " + s.token + " : " + s.name); 141 } 142 } 143 } catch (RemoteException e) { 144 System.err.println(e.toString()); 145 System.err.println(TRANSPORT_NOT_RUNNING_ERR); 146 } 147 } 148 149 private String nextArg() { 150 if (mNextArg >= mArgs.length) { 151 return null; 152 } 153 String arg = mArgs[mNextArg]; 154 mNextArg++; 155 return arg; 156 } 157 158 private static void showUsage() { 159 System.err.println("usage: bmgr [backup|restore|list|transport|run]"); 160 System.err.println(" bmgr backup [-f] package"); 161 System.err.println(" bmgr list sets"); 162 System.err.println(" #bmgr list transports"); 163 System.err.println(" #bmgr transport which#"); 164 System.err.println(" #bmgr restore set#"); 165 System.err.println(" bmgr run"); 166 } 167}