Bmgr.java revision 4a64bded06a0299785c295a975e2818308eb53e2
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                for (RestoreSet s : sets) {
175                    System.out.println("  " + s.token + " : " + s.name);
176                }
177            }
178        } catch (RemoteException e) {
179            System.err.println(e.toString());
180            System.err.println(TRANSPORT_NOT_RUNNING_ERR);
181        }
182    }
183
184    class RestoreObserver extends IRestoreObserver.Stub {
185        boolean done;
186        public void restoreStarting(int numPackages) {
187            System.out.println("restoreStarting: " + numPackages + " packages");
188        }
189
190        public void onUpdate(int nowBeingRestored) {
191            System.out.println("onUpdate: " + nowBeingRestored);
192        }
193
194        public void restoreFinished(int error) {
195            System.out.println("restoreFinished: " + error);
196            synchronized (this) {
197                done = true;
198                this.notify();
199            }
200        }
201    }
202
203    private void doRestore() {
204        int token;
205        try {
206            token = Integer.parseInt(nextArg());
207        } catch (NumberFormatException e) {
208            showUsage();
209            return;
210        }
211
212        RestoreObserver observer = new RestoreObserver();
213
214        try {
215            int curTransport = mBmgr.getCurrentTransport();
216            mRestore = mBmgr.beginRestoreSession(curTransport);
217            if (mRestore == null) {
218                System.err.println(BMGR_NOT_RUNNING_ERR);
219                return;
220            }
221            RestoreSet[] sets = mRestore.getAvailableRestoreSets();
222            for (RestoreSet s : sets) {
223                if (s.token == token) {
224                    System.out.println("Scheduling restore: " + s.name);
225                    mRestore.performRestore(token, observer);
226                    break;
227                }
228            }
229            mRestore.endRestoreSession();
230        } catch (RemoteException e) {
231            System.err.println(e.toString());
232            System.err.println(BMGR_NOT_RUNNING_ERR);
233        }
234
235        // now wait for it to be done
236        synchronized (observer) {
237            while (!observer.done) {
238                try {
239                    observer.wait();
240                } catch (InterruptedException ex) {
241                }
242            }
243        }
244        System.out.println("done");
245    }
246
247    private String nextArg() {
248        if (mNextArg >= mArgs.length) {
249            return null;
250        }
251        String arg = mArgs[mNextArg];
252        mNextArg++;
253        return arg;
254    }
255
256    private static void showUsage() {
257        System.err.println("usage: bmgr [backup|restore|list|transport|run]");
258        System.err.println("       bmgr backup [-f] package");
259        System.err.println("       bmgr list sets");
260        System.err.println("       #bmgr list transports");
261        System.err.println("       #bmgr transport which#");
262        System.err.println("       bmgr restore token#");
263        System.err.println("       bmgr run");
264    }
265}
266