Bmgr.java revision 9171749700853305f3e6abbcdbd9e02f3a71d459
1ace7f094bf07bbd90cb998b9462e4f2d101a498cChristopher Tate/*
2ace7f094bf07bbd90cb998b9462e4f2d101a498cChristopher Tate * Copyright (C) 2009 The Android Open Source Project
3ace7f094bf07bbd90cb998b9462e4f2d101a498cChristopher Tate *
4ace7f094bf07bbd90cb998b9462e4f2d101a498cChristopher Tate * Licensed under the Apache License, Version 2.0 (the "License");
5ace7f094bf07bbd90cb998b9462e4f2d101a498cChristopher Tate * you may not use this file except in compliance with the License.
6ace7f094bf07bbd90cb998b9462e4f2d101a498cChristopher Tate * You may obtain a copy of the License at
7ace7f094bf07bbd90cb998b9462e4f2d101a498cChristopher Tate *
8ace7f094bf07bbd90cb998b9462e4f2d101a498cChristopher Tate *      http://www.apache.org/licenses/LICENSE-2.0
9ace7f094bf07bbd90cb998b9462e4f2d101a498cChristopher Tate *
10ace7f094bf07bbd90cb998b9462e4f2d101a498cChristopher Tate * Unless required by applicable law or agreed to in writing, software
11ace7f094bf07bbd90cb998b9462e4f2d101a498cChristopher Tate * distributed under the License is distributed on an "AS IS" BASIS,
12ace7f094bf07bbd90cb998b9462e4f2d101a498cChristopher Tate * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13ace7f094bf07bbd90cb998b9462e4f2d101a498cChristopher Tate * See the License for the specific language governing permissions and
14ace7f094bf07bbd90cb998b9462e4f2d101a498cChristopher Tate * limitations under the License.
15ace7f094bf07bbd90cb998b9462e4f2d101a498cChristopher Tate */
16ace7f094bf07bbd90cb998b9462e4f2d101a498cChristopher Tate
17ace7f094bf07bbd90cb998b9462e4f2d101a498cChristopher Tatepackage com.android.commands.bmgr;
18ace7f094bf07bbd90cb998b9462e4f2d101a498cChristopher Tate
19ace7f094bf07bbd90cb998b9462e4f2d101a498cChristopher Tateimport android.backup.IBackupManager;
205e8a4b842c20dd47b82e9915f1bd730ee1b0d46dJoe Onoratoimport android.backup.IRestoreObserver;
21ace7f094bf07bbd90cb998b9462e4f2d101a498cChristopher Tateimport android.backup.IRestoreSession;
22ace7f094bf07bbd90cb998b9462e4f2d101a498cChristopher Tateimport android.backup.RestoreSet;
23ace7f094bf07bbd90cb998b9462e4f2d101a498cChristopher Tateimport android.os.RemoteException;
24ace7f094bf07bbd90cb998b9462e4f2d101a498cChristopher Tateimport android.os.ServiceManager;
25ace7f094bf07bbd90cb998b9462e4f2d101a498cChristopher Tate
26ace7f094bf07bbd90cb998b9462e4f2d101a498cChristopher Tatepublic final class Bmgr {
27ace7f094bf07bbd90cb998b9462e4f2d101a498cChristopher Tate    IBackupManager mBmgr;
28ace7f094bf07bbd90cb998b9462e4f2d101a498cChristopher Tate    IRestoreSession mRestore;
29ace7f094bf07bbd90cb998b9462e4f2d101a498cChristopher Tate
30ace7f094bf07bbd90cb998b9462e4f2d101a498cChristopher Tate    static final String BMGR_NOT_RUNNING_ERR =
31ace7f094bf07bbd90cb998b9462e4f2d101a498cChristopher Tate            "Error: Could not access the Backup Manager.  Is the system running?";
32ace7f094bf07bbd90cb998b9462e4f2d101a498cChristopher Tate    static final String TRANSPORT_NOT_RUNNING_ERR =
33ace7f094bf07bbd90cb998b9462e4f2d101a498cChristopher Tate        "Error: Could not access the backup transport.  Is the system running?";
34ace7f094bf07bbd90cb998b9462e4f2d101a498cChristopher Tate
35ace7f094bf07bbd90cb998b9462e4f2d101a498cChristopher Tate    private String[] mArgs;
36ace7f094bf07bbd90cb998b9462e4f2d101a498cChristopher Tate    private int mNextArg;
37ace7f094bf07bbd90cb998b9462e4f2d101a498cChristopher Tate    private String mCurArgData;
38ace7f094bf07bbd90cb998b9462e4f2d101a498cChristopher Tate
39ace7f094bf07bbd90cb998b9462e4f2d101a498cChristopher Tate    public static void main(String[] args) {
40f68eb500f99361541049e09eb7f9ddd6f4ef4efaChristopher Tate        try {
41f68eb500f99361541049e09eb7f9ddd6f4ef4efaChristopher Tate            new Bmgr().run(args);
42f68eb500f99361541049e09eb7f9ddd6f4ef4efaChristopher Tate        } catch (Exception e) {
43f68eb500f99361541049e09eb7f9ddd6f4ef4efaChristopher Tate            System.err.println("Exception caught:");
44f68eb500f99361541049e09eb7f9ddd6f4ef4efaChristopher Tate            e.printStackTrace();
45f68eb500f99361541049e09eb7f9ddd6f4ef4efaChristopher Tate        }
46ace7f094bf07bbd90cb998b9462e4f2d101a498cChristopher Tate    }
47f68eb500f99361541049e09eb7f9ddd6f4ef4efaChristopher Tate
48ace7f094bf07bbd90cb998b9462e4f2d101a498cChristopher Tate    public void run(String[] args) {
49ace7f094bf07bbd90cb998b9462e4f2d101a498cChristopher Tate        boolean validCommand = false;
50ace7f094bf07bbd90cb998b9462e4f2d101a498cChristopher Tate        if (args.length < 1) {
51ace7f094bf07bbd90cb998b9462e4f2d101a498cChristopher Tate            showUsage();
52ace7f094bf07bbd90cb998b9462e4f2d101a498cChristopher Tate            return;
53ace7f094bf07bbd90cb998b9462e4f2d101a498cChristopher Tate        }
54ace7f094bf07bbd90cb998b9462e4f2d101a498cChristopher Tate
55ace7f094bf07bbd90cb998b9462e4f2d101a498cChristopher Tate        mBmgr = IBackupManager.Stub.asInterface(ServiceManager.getService("backup"));
56ace7f094bf07bbd90cb998b9462e4f2d101a498cChristopher Tate        if (mBmgr == null) {
57ace7f094bf07bbd90cb998b9462e4f2d101a498cChristopher Tate            System.err.println(BMGR_NOT_RUNNING_ERR);
58ace7f094bf07bbd90cb998b9462e4f2d101a498cChristopher Tate            return;
59ace7f094bf07bbd90cb998b9462e4f2d101a498cChristopher Tate        }
60ace7f094bf07bbd90cb998b9462e4f2d101a498cChristopher Tate
61ace7f094bf07bbd90cb998b9462e4f2d101a498cChristopher Tate        mArgs = args;
62ace7f094bf07bbd90cb998b9462e4f2d101a498cChristopher Tate        String op = args[0];
63ace7f094bf07bbd90cb998b9462e4f2d101a498cChristopher Tate        mNextArg = 1;
64ace7f094bf07bbd90cb998b9462e4f2d101a498cChristopher Tate
65ace7f094bf07bbd90cb998b9462e4f2d101a498cChristopher Tate        if ("run".equals(op)) {
66ace7f094bf07bbd90cb998b9462e4f2d101a498cChristopher Tate            doRun();
67ace7f094bf07bbd90cb998b9462e4f2d101a498cChristopher Tate            return;
68ace7f094bf07bbd90cb998b9462e4f2d101a498cChristopher Tate        }
69ace7f094bf07bbd90cb998b9462e4f2d101a498cChristopher Tate
70ace7f094bf07bbd90cb998b9462e4f2d101a498cChristopher Tate        if ("backup".equals(op)) {
71ace7f094bf07bbd90cb998b9462e4f2d101a498cChristopher Tate            doBackup();
72ace7f094bf07bbd90cb998b9462e4f2d101a498cChristopher Tate            return;
73ace7f094bf07bbd90cb998b9462e4f2d101a498cChristopher Tate        }
74ace7f094bf07bbd90cb998b9462e4f2d101a498cChristopher Tate
75ace7f094bf07bbd90cb998b9462e4f2d101a498cChristopher Tate        if ("list".equals(op)) {
76ace7f094bf07bbd90cb998b9462e4f2d101a498cChristopher Tate            doList();
77ace7f094bf07bbd90cb998b9462e4f2d101a498cChristopher Tate            return;
78ace7f094bf07bbd90cb998b9462e4f2d101a498cChristopher Tate        }
79f68eb500f99361541049e09eb7f9ddd6f4ef4efaChristopher Tate
80f68eb500f99361541049e09eb7f9ddd6f4ef4efaChristopher Tate        if ("restore".equals(op)) {
81f68eb500f99361541049e09eb7f9ddd6f4ef4efaChristopher Tate            doRestore();
82f68eb500f99361541049e09eb7f9ddd6f4ef4efaChristopher Tate            return;
83f68eb500f99361541049e09eb7f9ddd6f4ef4efaChristopher Tate        }
84abce4e8714bed26a2b37b20ad3f02cf619d71c9aChristopher Tate
85abce4e8714bed26a2b37b20ad3f02cf619d71c9aChristopher Tate        if ("transport".equals(op)) {
86abce4e8714bed26a2b37b20ad3f02cf619d71c9aChristopher Tate            doTransport();
87abce4e8714bed26a2b37b20ad3f02cf619d71c9aChristopher Tate            return;
88abce4e8714bed26a2b37b20ad3f02cf619d71c9aChristopher Tate        }
89abce4e8714bed26a2b37b20ad3f02cf619d71c9aChristopher Tate
90abce4e8714bed26a2b37b20ad3f02cf619d71c9aChristopher Tate        System.err.println("Unknown command");
91abce4e8714bed26a2b37b20ad3f02cf619d71c9aChristopher Tate        showUsage();
92ace7f094bf07bbd90cb998b9462e4f2d101a498cChristopher Tate    }
93ace7f094bf07bbd90cb998b9462e4f2d101a498cChristopher Tate
94ace7f094bf07bbd90cb998b9462e4f2d101a498cChristopher Tate    private void doRun() {
95ace7f094bf07bbd90cb998b9462e4f2d101a498cChristopher Tate        try {
96ace7f094bf07bbd90cb998b9462e4f2d101a498cChristopher Tate            mBmgr.backupNow();
97ace7f094bf07bbd90cb998b9462e4f2d101a498cChristopher Tate        } catch (RemoteException e) {
98ace7f094bf07bbd90cb998b9462e4f2d101a498cChristopher Tate            System.err.println(e.toString());
99ace7f094bf07bbd90cb998b9462e4f2d101a498cChristopher Tate            System.err.println(BMGR_NOT_RUNNING_ERR);
100ace7f094bf07bbd90cb998b9462e4f2d101a498cChristopher Tate        }
101ace7f094bf07bbd90cb998b9462e4f2d101a498cChristopher Tate    }
102ace7f094bf07bbd90cb998b9462e4f2d101a498cChristopher Tate
103ace7f094bf07bbd90cb998b9462e4f2d101a498cChristopher Tate    private void doBackup() {
104ace7f094bf07bbd90cb998b9462e4f2d101a498cChristopher Tate        boolean isFull = false;
105ace7f094bf07bbd90cb998b9462e4f2d101a498cChristopher Tate        String pkg = nextArg();
106ace7f094bf07bbd90cb998b9462e4f2d101a498cChristopher Tate        if ("-f".equals(pkg)) {
107ace7f094bf07bbd90cb998b9462e4f2d101a498cChristopher Tate            isFull = true;
108ace7f094bf07bbd90cb998b9462e4f2d101a498cChristopher Tate            pkg = nextArg();
109ace7f094bf07bbd90cb998b9462e4f2d101a498cChristopher Tate        }
110ace7f094bf07bbd90cb998b9462e4f2d101a498cChristopher Tate
111ace7f094bf07bbd90cb998b9462e4f2d101a498cChristopher Tate        if (pkg == null || pkg.startsWith("-")) {
112ace7f094bf07bbd90cb998b9462e4f2d101a498cChristopher Tate            showUsage();
113ace7f094bf07bbd90cb998b9462e4f2d101a498cChristopher Tate            return;
114ace7f094bf07bbd90cb998b9462e4f2d101a498cChristopher Tate        }
115ace7f094bf07bbd90cb998b9462e4f2d101a498cChristopher Tate
116ace7f094bf07bbd90cb998b9462e4f2d101a498cChristopher Tate        try {
117ace7f094bf07bbd90cb998b9462e4f2d101a498cChristopher Tate            // !!! TODO: handle full backup
118ace7f094bf07bbd90cb998b9462e4f2d101a498cChristopher Tate            mBmgr.dataChanged(pkg);
119ace7f094bf07bbd90cb998b9462e4f2d101a498cChristopher Tate        } catch (RemoteException e) {
120ace7f094bf07bbd90cb998b9462e4f2d101a498cChristopher Tate            System.err.println(e.toString());
121ace7f094bf07bbd90cb998b9462e4f2d101a498cChristopher Tate            System.err.println(BMGR_NOT_RUNNING_ERR);
122ace7f094bf07bbd90cb998b9462e4f2d101a498cChristopher Tate        }
123ace7f094bf07bbd90cb998b9462e4f2d101a498cChristopher Tate    }
124ace7f094bf07bbd90cb998b9462e4f2d101a498cChristopher Tate
125abce4e8714bed26a2b37b20ad3f02cf619d71c9aChristopher Tate    private void doTransport() {
126abce4e8714bed26a2b37b20ad3f02cf619d71c9aChristopher Tate        try {
1279171749700853305f3e6abbcdbd9e02f3a71d459Christopher Tate            String which = nextArg();
1289171749700853305f3e6abbcdbd9e02f3a71d459Christopher Tate            String old = mBmgr.selectBackupTransport(which);
1299171749700853305f3e6abbcdbd9e02f3a71d459Christopher Tate            if (old == null) {
1309171749700853305f3e6abbcdbd9e02f3a71d459Christopher Tate                System.out.println("Unknown transport '" + which
1319171749700853305f3e6abbcdbd9e02f3a71d459Christopher Tate                        + "' specified; no changes made.");
1329171749700853305f3e6abbcdbd9e02f3a71d459Christopher Tate            } else {
1339171749700853305f3e6abbcdbd9e02f3a71d459Christopher Tate                System.out.println("Selected transport " + which + " (formerly " + old + ")");
1349171749700853305f3e6abbcdbd9e02f3a71d459Christopher Tate            }
135abce4e8714bed26a2b37b20ad3f02cf619d71c9aChristopher Tate        } catch (RemoteException e) {
136abce4e8714bed26a2b37b20ad3f02cf619d71c9aChristopher Tate            System.err.println(e.toString());
137abce4e8714bed26a2b37b20ad3f02cf619d71c9aChristopher Tate            System.err.println(BMGR_NOT_RUNNING_ERR);
138abce4e8714bed26a2b37b20ad3f02cf619d71c9aChristopher Tate        }
139abce4e8714bed26a2b37b20ad3f02cf619d71c9aChristopher Tate    }
140abce4e8714bed26a2b37b20ad3f02cf619d71c9aChristopher Tate
141ace7f094bf07bbd90cb998b9462e4f2d101a498cChristopher Tate    private void doList() {
142ace7f094bf07bbd90cb998b9462e4f2d101a498cChristopher Tate        String arg = nextArg();     // sets, transports, packages set#
143ace7f094bf07bbd90cb998b9462e4f2d101a498cChristopher Tate        if ("transports".equals(arg)) {
144ace7f094bf07bbd90cb998b9462e4f2d101a498cChristopher Tate            doListTransports();
145ace7f094bf07bbd90cb998b9462e4f2d101a498cChristopher Tate            return;
146ace7f094bf07bbd90cb998b9462e4f2d101a498cChristopher Tate        }
147ace7f094bf07bbd90cb998b9462e4f2d101a498cChristopher Tate
148ace7f094bf07bbd90cb998b9462e4f2d101a498cChristopher Tate        // The rest of the 'list' options work with a restore session on the current transport
149ace7f094bf07bbd90cb998b9462e4f2d101a498cChristopher Tate        try {
1509171749700853305f3e6abbcdbd9e02f3a71d459Christopher Tate            String curTransport = mBmgr.getCurrentTransport();
151ace7f094bf07bbd90cb998b9462e4f2d101a498cChristopher Tate            mRestore = mBmgr.beginRestoreSession(curTransport);
152f68eb500f99361541049e09eb7f9ddd6f4ef4efaChristopher Tate            if (mRestore == null) {
153f68eb500f99361541049e09eb7f9ddd6f4ef4efaChristopher Tate                System.err.println(BMGR_NOT_RUNNING_ERR);
154f68eb500f99361541049e09eb7f9ddd6f4ef4efaChristopher Tate                return;
155f68eb500f99361541049e09eb7f9ddd6f4ef4efaChristopher Tate            }
156ace7f094bf07bbd90cb998b9462e4f2d101a498cChristopher Tate
157ace7f094bf07bbd90cb998b9462e4f2d101a498cChristopher Tate            if ("sets".equals(arg)) {
158ace7f094bf07bbd90cb998b9462e4f2d101a498cChristopher Tate                doListRestoreSets();
1599171749700853305f3e6abbcdbd9e02f3a71d459Christopher Tate            } else if ("transports".equals(arg)) {
1609171749700853305f3e6abbcdbd9e02f3a71d459Christopher Tate                doListTransports();
161ace7f094bf07bbd90cb998b9462e4f2d101a498cChristopher Tate            }
162ace7f094bf07bbd90cb998b9462e4f2d101a498cChristopher Tate
163ace7f094bf07bbd90cb998b9462e4f2d101a498cChristopher Tate            mRestore.endRestoreSession();
164ace7f094bf07bbd90cb998b9462e4f2d101a498cChristopher Tate        } catch (RemoteException e) {
165ace7f094bf07bbd90cb998b9462e4f2d101a498cChristopher Tate            System.err.println(e.toString());
166ace7f094bf07bbd90cb998b9462e4f2d101a498cChristopher Tate            System.err.println(BMGR_NOT_RUNNING_ERR);
167ace7f094bf07bbd90cb998b9462e4f2d101a498cChristopher Tate        }
168ace7f094bf07bbd90cb998b9462e4f2d101a498cChristopher Tate    }
169ace7f094bf07bbd90cb998b9462e4f2d101a498cChristopher Tate
170ace7f094bf07bbd90cb998b9462e4f2d101a498cChristopher Tate    private void doListTransports() {
1719171749700853305f3e6abbcdbd9e02f3a71d459Christopher Tate        try {
1729171749700853305f3e6abbcdbd9e02f3a71d459Christopher Tate            String current = mBmgr.getCurrentTransport();
1739171749700853305f3e6abbcdbd9e02f3a71d459Christopher Tate            String[] transports = mBmgr.listAllTransports();
1749171749700853305f3e6abbcdbd9e02f3a71d459Christopher Tate            if (transports == null || transports.length == 0) {
1759171749700853305f3e6abbcdbd9e02f3a71d459Christopher Tate                System.out.println("No transports available.");
1769171749700853305f3e6abbcdbd9e02f3a71d459Christopher Tate                return;
1779171749700853305f3e6abbcdbd9e02f3a71d459Christopher Tate            }
1789171749700853305f3e6abbcdbd9e02f3a71d459Christopher Tate
1799171749700853305f3e6abbcdbd9e02f3a71d459Christopher Tate            for (String t : transports) {
1809171749700853305f3e6abbcdbd9e02f3a71d459Christopher Tate                String pad = (t.equals(current)) ? "  * " : "    ";
1819171749700853305f3e6abbcdbd9e02f3a71d459Christopher Tate                System.out.println(pad + t);
1829171749700853305f3e6abbcdbd9e02f3a71d459Christopher Tate            }
1839171749700853305f3e6abbcdbd9e02f3a71d459Christopher Tate        } catch (RemoteException e) {
1849171749700853305f3e6abbcdbd9e02f3a71d459Christopher Tate            System.err.println(e.toString());
1859171749700853305f3e6abbcdbd9e02f3a71d459Christopher Tate            System.err.println(BMGR_NOT_RUNNING_ERR);
1869171749700853305f3e6abbcdbd9e02f3a71d459Christopher Tate        }
187ace7f094bf07bbd90cb998b9462e4f2d101a498cChristopher Tate    }
188ace7f094bf07bbd90cb998b9462e4f2d101a498cChristopher Tate
189ace7f094bf07bbd90cb998b9462e4f2d101a498cChristopher Tate    private void doListRestoreSets() {
190ace7f094bf07bbd90cb998b9462e4f2d101a498cChristopher Tate        try {
191ace7f094bf07bbd90cb998b9462e4f2d101a498cChristopher Tate            RestoreSet[] sets = mRestore.getAvailableRestoreSets();
192f68eb500f99361541049e09eb7f9ddd6f4ef4efaChristopher Tate            if (sets == null || sets.length == 0) {
193ace7f094bf07bbd90cb998b9462e4f2d101a498cChristopher Tate                System.out.println("No restore sets available");
194ace7f094bf07bbd90cb998b9462e4f2d101a498cChristopher Tate            } else {
195c73a218c2663e6ae3ec8a9ab8b9524f95702ade9Christopher Tate                printRestoreSets(sets);
196ace7f094bf07bbd90cb998b9462e4f2d101a498cChristopher Tate            }
197ace7f094bf07bbd90cb998b9462e4f2d101a498cChristopher Tate        } catch (RemoteException e) {
198ace7f094bf07bbd90cb998b9462e4f2d101a498cChristopher Tate            System.err.println(e.toString());
199ace7f094bf07bbd90cb998b9462e4f2d101a498cChristopher Tate            System.err.println(TRANSPORT_NOT_RUNNING_ERR);
200ace7f094bf07bbd90cb998b9462e4f2d101a498cChristopher Tate        }
201ace7f094bf07bbd90cb998b9462e4f2d101a498cChristopher Tate    }
202ace7f094bf07bbd90cb998b9462e4f2d101a498cChristopher Tate
203c73a218c2663e6ae3ec8a9ab8b9524f95702ade9Christopher Tate    private void printRestoreSets(RestoreSet[] sets) {
204c73a218c2663e6ae3ec8a9ab8b9524f95702ade9Christopher Tate        for (RestoreSet s : sets) {
205c73a218c2663e6ae3ec8a9ab8b9524f95702ade9Christopher Tate            System.out.println("  " + s.token + " : " + s.name);
206c73a218c2663e6ae3ec8a9ab8b9524f95702ade9Christopher Tate        }
207c73a218c2663e6ae3ec8a9ab8b9524f95702ade9Christopher Tate    }
208c73a218c2663e6ae3ec8a9ab8b9524f95702ade9Christopher Tate
2094a64bded06a0299785c295a975e2818308eb53e2Joe Onorato    class RestoreObserver extends IRestoreObserver.Stub {
2104a64bded06a0299785c295a975e2818308eb53e2Joe Onorato        boolean done;
2114a64bded06a0299785c295a975e2818308eb53e2Joe Onorato        public void restoreStarting(int numPackages) {
2124a64bded06a0299785c295a975e2818308eb53e2Joe Onorato            System.out.println("restoreStarting: " + numPackages + " packages");
2134a64bded06a0299785c295a975e2818308eb53e2Joe Onorato        }
2144a64bded06a0299785c295a975e2818308eb53e2Joe Onorato
2154a64bded06a0299785c295a975e2818308eb53e2Joe Onorato        public void onUpdate(int nowBeingRestored) {
2164a64bded06a0299785c295a975e2818308eb53e2Joe Onorato            System.out.println("onUpdate: " + nowBeingRestored);
2174a64bded06a0299785c295a975e2818308eb53e2Joe Onorato        }
2184a64bded06a0299785c295a975e2818308eb53e2Joe Onorato
2194a64bded06a0299785c295a975e2818308eb53e2Joe Onorato        public void restoreFinished(int error) {
2204a64bded06a0299785c295a975e2818308eb53e2Joe Onorato            System.out.println("restoreFinished: " + error);
2214a64bded06a0299785c295a975e2818308eb53e2Joe Onorato            synchronized (this) {
2224a64bded06a0299785c295a975e2818308eb53e2Joe Onorato                done = true;
2234a64bded06a0299785c295a975e2818308eb53e2Joe Onorato                this.notify();
2244a64bded06a0299785c295a975e2818308eb53e2Joe Onorato            }
2254a64bded06a0299785c295a975e2818308eb53e2Joe Onorato        }
2264a64bded06a0299785c295a975e2818308eb53e2Joe Onorato    }
2274a64bded06a0299785c295a975e2818308eb53e2Joe Onorato
228f68eb500f99361541049e09eb7f9ddd6f4ef4efaChristopher Tate    private void doRestore() {
229156411df4627336b246db78cddca8248ed615b67Dan Egnor        long token;
230f68eb500f99361541049e09eb7f9ddd6f4ef4efaChristopher Tate        try {
231156411df4627336b246db78cddca8248ed615b67Dan Egnor            token = Long.parseLong(nextArg());
232f68eb500f99361541049e09eb7f9ddd6f4ef4efaChristopher Tate        } catch (NumberFormatException e) {
233f68eb500f99361541049e09eb7f9ddd6f4ef4efaChristopher Tate            showUsage();
234f68eb500f99361541049e09eb7f9ddd6f4ef4efaChristopher Tate            return;
235f68eb500f99361541049e09eb7f9ddd6f4ef4efaChristopher Tate        }
236f68eb500f99361541049e09eb7f9ddd6f4ef4efaChristopher Tate
2374a64bded06a0299785c295a975e2818308eb53e2Joe Onorato        RestoreObserver observer = new RestoreObserver();
2384a64bded06a0299785c295a975e2818308eb53e2Joe Onorato
239f68eb500f99361541049e09eb7f9ddd6f4ef4efaChristopher Tate        try {
240c73a218c2663e6ae3ec8a9ab8b9524f95702ade9Christopher Tate            boolean didRestore = false;
2419171749700853305f3e6abbcdbd9e02f3a71d459Christopher Tate            String curTransport = mBmgr.getCurrentTransport();
242f68eb500f99361541049e09eb7f9ddd6f4ef4efaChristopher Tate            mRestore = mBmgr.beginRestoreSession(curTransport);
243f68eb500f99361541049e09eb7f9ddd6f4ef4efaChristopher Tate            if (mRestore == null) {
244f68eb500f99361541049e09eb7f9ddd6f4ef4efaChristopher Tate                System.err.println(BMGR_NOT_RUNNING_ERR);
245f68eb500f99361541049e09eb7f9ddd6f4ef4efaChristopher Tate                return;
246f68eb500f99361541049e09eb7f9ddd6f4ef4efaChristopher Tate            }
247f68eb500f99361541049e09eb7f9ddd6f4ef4efaChristopher Tate            RestoreSet[] sets = mRestore.getAvailableRestoreSets();
248f68eb500f99361541049e09eb7f9ddd6f4ef4efaChristopher Tate            for (RestoreSet s : sets) {
249f68eb500f99361541049e09eb7f9ddd6f4ef4efaChristopher Tate                if (s.token == token) {
250f68eb500f99361541049e09eb7f9ddd6f4ef4efaChristopher Tate                    System.out.println("Scheduling restore: " + s.name);
2514a64bded06a0299785c295a975e2818308eb53e2Joe Onorato                    mRestore.performRestore(token, observer);
252c73a218c2663e6ae3ec8a9ab8b9524f95702ade9Christopher Tate                    didRestore = true;
253f68eb500f99361541049e09eb7f9ddd6f4ef4efaChristopher Tate                    break;
254f68eb500f99361541049e09eb7f9ddd6f4ef4efaChristopher Tate                }
255f68eb500f99361541049e09eb7f9ddd6f4ef4efaChristopher Tate            }
256c73a218c2663e6ae3ec8a9ab8b9524f95702ade9Christopher Tate            if (!didRestore) {
257c73a218c2663e6ae3ec8a9ab8b9524f95702ade9Christopher Tate                if (sets == null || sets.length == 0) {
258c73a218c2663e6ae3ec8a9ab8b9524f95702ade9Christopher Tate                    System.out.println("No available restore sets; no restore performed");
259c73a218c2663e6ae3ec8a9ab8b9524f95702ade9Christopher Tate                } else {
260c73a218c2663e6ae3ec8a9ab8b9524f95702ade9Christopher Tate                    System.out.println("No matching restore set token.  Available sets:");
261c73a218c2663e6ae3ec8a9ab8b9524f95702ade9Christopher Tate                    printRestoreSets(sets);
262c73a218c2663e6ae3ec8a9ab8b9524f95702ade9Christopher Tate                }
263c73a218c2663e6ae3ec8a9ab8b9524f95702ade9Christopher Tate            }
264f68eb500f99361541049e09eb7f9ddd6f4ef4efaChristopher Tate            mRestore.endRestoreSession();
265f68eb500f99361541049e09eb7f9ddd6f4ef4efaChristopher Tate        } catch (RemoteException e) {
266f68eb500f99361541049e09eb7f9ddd6f4ef4efaChristopher Tate            System.err.println(e.toString());
267f68eb500f99361541049e09eb7f9ddd6f4ef4efaChristopher Tate            System.err.println(BMGR_NOT_RUNNING_ERR);
268f68eb500f99361541049e09eb7f9ddd6f4ef4efaChristopher Tate        }
2694a64bded06a0299785c295a975e2818308eb53e2Joe Onorato
2704a64bded06a0299785c295a975e2818308eb53e2Joe Onorato        // now wait for it to be done
2714a64bded06a0299785c295a975e2818308eb53e2Joe Onorato        synchronized (observer) {
2724a64bded06a0299785c295a975e2818308eb53e2Joe Onorato            while (!observer.done) {
2734a64bded06a0299785c295a975e2818308eb53e2Joe Onorato                try {
2744a64bded06a0299785c295a975e2818308eb53e2Joe Onorato                    observer.wait();
2754a64bded06a0299785c295a975e2818308eb53e2Joe Onorato                } catch (InterruptedException ex) {
2764a64bded06a0299785c295a975e2818308eb53e2Joe Onorato                }
2774a64bded06a0299785c295a975e2818308eb53e2Joe Onorato            }
2784a64bded06a0299785c295a975e2818308eb53e2Joe Onorato        }
2794a64bded06a0299785c295a975e2818308eb53e2Joe Onorato        System.out.println("done");
280f68eb500f99361541049e09eb7f9ddd6f4ef4efaChristopher Tate    }
281f68eb500f99361541049e09eb7f9ddd6f4ef4efaChristopher Tate
282ace7f094bf07bbd90cb998b9462e4f2d101a498cChristopher Tate    private String nextArg() {
283ace7f094bf07bbd90cb998b9462e4f2d101a498cChristopher Tate        if (mNextArg >= mArgs.length) {
284ace7f094bf07bbd90cb998b9462e4f2d101a498cChristopher Tate            return null;
285ace7f094bf07bbd90cb998b9462e4f2d101a498cChristopher Tate        }
286ace7f094bf07bbd90cb998b9462e4f2d101a498cChristopher Tate        String arg = mArgs[mNextArg];
287ace7f094bf07bbd90cb998b9462e4f2d101a498cChristopher Tate        mNextArg++;
288ace7f094bf07bbd90cb998b9462e4f2d101a498cChristopher Tate        return arg;
289ace7f094bf07bbd90cb998b9462e4f2d101a498cChristopher Tate    }
290ace7f094bf07bbd90cb998b9462e4f2d101a498cChristopher Tate
291ace7f094bf07bbd90cb998b9462e4f2d101a498cChristopher Tate    private static void showUsage() {
292ace7f094bf07bbd90cb998b9462e4f2d101a498cChristopher Tate        System.err.println("usage: bmgr [backup|restore|list|transport|run]");
2939171749700853305f3e6abbcdbd9e02f3a71d459Christopher Tate        System.err.println("       bmgr backup PACKAGE");
2949171749700853305f3e6abbcdbd9e02f3a71d459Christopher Tate        System.err.println("       bmgr list transports");
295ace7f094bf07bbd90cb998b9462e4f2d101a498cChristopher Tate        System.err.println("       bmgr list sets");
2969171749700853305f3e6abbcdbd9e02f3a71d459Christopher Tate        System.err.println("       bmgr transport WHICH");
2979171749700853305f3e6abbcdbd9e02f3a71d459Christopher Tate        System.err.println("       bmgr restore TOKEN");
298ace7f094bf07bbd90cb998b9462e4f2d101a498cChristopher Tate        System.err.println("       bmgr run");
2999171749700853305f3e6abbcdbd9e02f3a71d459Christopher Tate        System.err.println("");
3009171749700853305f3e6abbcdbd9e02f3a71d459Christopher Tate        System.err.println("The 'backup' command schedules a backup pass for the named package.");
3019171749700853305f3e6abbcdbd9e02f3a71d459Christopher Tate        System.err.println("Note that the backup pass will effectively be a no-op if the package");
3029171749700853305f3e6abbcdbd9e02f3a71d459Christopher Tate        System.err.println("does not actually have changed data to store.");
3039171749700853305f3e6abbcdbd9e02f3a71d459Christopher Tate        System.err.println("");
3049171749700853305f3e6abbcdbd9e02f3a71d459Christopher Tate        System.err.println("The 'list transports' command reports the names of the backup transports");
3059171749700853305f3e6abbcdbd9e02f3a71d459Christopher Tate        System.err.println("currently available on the device.  These names can be passed as arguments");
3069171749700853305f3e6abbcdbd9e02f3a71d459Christopher Tate        System.err.println("to the 'transport' command.  The currently selected transport is indicated");
3079171749700853305f3e6abbcdbd9e02f3a71d459Christopher Tate        System.err.println("with a '*' character.");
3089171749700853305f3e6abbcdbd9e02f3a71d459Christopher Tate        System.err.println("");
3099171749700853305f3e6abbcdbd9e02f3a71d459Christopher Tate        System.err.println("The 'list sets' command reports the token and name of each restore set");
3109171749700853305f3e6abbcdbd9e02f3a71d459Christopher Tate        System.err.println("available to the device via the current transport.");
3119171749700853305f3e6abbcdbd9e02f3a71d459Christopher Tate        System.err.println("");
3129171749700853305f3e6abbcdbd9e02f3a71d459Christopher Tate        System.err.println("The 'transport' command designates the named transport as the currently");
3139171749700853305f3e6abbcdbd9e02f3a71d459Christopher Tate        System.err.println("active one.  This setting is persistent across reboots.");
3149171749700853305f3e6abbcdbd9e02f3a71d459Christopher Tate        System.err.println("");
3159171749700853305f3e6abbcdbd9e02f3a71d459Christopher Tate        System.err.println("The 'restore' command initiates a restore operation, using the restore set");
3169171749700853305f3e6abbcdbd9e02f3a71d459Christopher Tate        System.err.println("from the current transport whose token matches the argument.");
3179171749700853305f3e6abbcdbd9e02f3a71d459Christopher Tate        System.err.println("");
3189171749700853305f3e6abbcdbd9e02f3a71d459Christopher Tate        System.err.println("The 'run' command causes any scheduled backup operation to be initiated");
3199171749700853305f3e6abbcdbd9e02f3a71d459Christopher Tate        System.err.println("immediately, without the usual waiting period for batching together");
3209171749700853305f3e6abbcdbd9e02f3a71d459Christopher Tate        System.err.println("data changes.");
321ace7f094bf07bbd90cb998b9462e4f2d101a498cChristopher Tate    }
3225e8a4b842c20dd47b82e9915f1bd730ee1b0d46dJoe Onorato}
323