Bmgr.java revision 6ef58a1509b9d3348a33ca5686917796c2759aa5
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
656ef58a1509b9d3348a33ca5686917796c2759aa5Christopher Tate        if ("enabled".equals(op)) {
666ef58a1509b9d3348a33ca5686917796c2759aa5Christopher Tate            doEnabled();
676ef58a1509b9d3348a33ca5686917796c2759aa5Christopher Tate            return;
686ef58a1509b9d3348a33ca5686917796c2759aa5Christopher Tate        }
696ef58a1509b9d3348a33ca5686917796c2759aa5Christopher Tate
706ef58a1509b9d3348a33ca5686917796c2759aa5Christopher Tate        if ("enable".equals(op)) {
716ef58a1509b9d3348a33ca5686917796c2759aa5Christopher Tate            doEnable();
726ef58a1509b9d3348a33ca5686917796c2759aa5Christopher Tate            return;
736ef58a1509b9d3348a33ca5686917796c2759aa5Christopher Tate        }
746ef58a1509b9d3348a33ca5686917796c2759aa5Christopher Tate
75ace7f094bf07bbd90cb998b9462e4f2d101a498cChristopher Tate        if ("run".equals(op)) {
76ace7f094bf07bbd90cb998b9462e4f2d101a498cChristopher Tate            doRun();
77ace7f094bf07bbd90cb998b9462e4f2d101a498cChristopher Tate            return;
78ace7f094bf07bbd90cb998b9462e4f2d101a498cChristopher Tate        }
79ace7f094bf07bbd90cb998b9462e4f2d101a498cChristopher Tate
80ace7f094bf07bbd90cb998b9462e4f2d101a498cChristopher Tate        if ("backup".equals(op)) {
81ace7f094bf07bbd90cb998b9462e4f2d101a498cChristopher Tate            doBackup();
82ace7f094bf07bbd90cb998b9462e4f2d101a498cChristopher Tate            return;
83ace7f094bf07bbd90cb998b9462e4f2d101a498cChristopher Tate        }
84ace7f094bf07bbd90cb998b9462e4f2d101a498cChristopher Tate
85ace7f094bf07bbd90cb998b9462e4f2d101a498cChristopher Tate        if ("list".equals(op)) {
86ace7f094bf07bbd90cb998b9462e4f2d101a498cChristopher Tate            doList();
87ace7f094bf07bbd90cb998b9462e4f2d101a498cChristopher Tate            return;
88ace7f094bf07bbd90cb998b9462e4f2d101a498cChristopher Tate        }
89f68eb500f99361541049e09eb7f9ddd6f4ef4efaChristopher Tate
90f68eb500f99361541049e09eb7f9ddd6f4ef4efaChristopher Tate        if ("restore".equals(op)) {
91f68eb500f99361541049e09eb7f9ddd6f4ef4efaChristopher Tate            doRestore();
92f68eb500f99361541049e09eb7f9ddd6f4ef4efaChristopher Tate            return;
93f68eb500f99361541049e09eb7f9ddd6f4ef4efaChristopher Tate        }
94abce4e8714bed26a2b37b20ad3f02cf619d71c9aChristopher Tate
95abce4e8714bed26a2b37b20ad3f02cf619d71c9aChristopher Tate        if ("transport".equals(op)) {
96abce4e8714bed26a2b37b20ad3f02cf619d71c9aChristopher Tate            doTransport();
97abce4e8714bed26a2b37b20ad3f02cf619d71c9aChristopher Tate            return;
98abce4e8714bed26a2b37b20ad3f02cf619d71c9aChristopher Tate        }
99abce4e8714bed26a2b37b20ad3f02cf619d71c9aChristopher Tate
100abce4e8714bed26a2b37b20ad3f02cf619d71c9aChristopher Tate        System.err.println("Unknown command");
101abce4e8714bed26a2b37b20ad3f02cf619d71c9aChristopher Tate        showUsage();
102ace7f094bf07bbd90cb998b9462e4f2d101a498cChristopher Tate    }
103ace7f094bf07bbd90cb998b9462e4f2d101a498cChristopher Tate
1046ef58a1509b9d3348a33ca5686917796c2759aa5Christopher Tate    private String enableToString(boolean enabled) {
1056ef58a1509b9d3348a33ca5686917796c2759aa5Christopher Tate        return enabled ? "enabled" : "disabled";
1066ef58a1509b9d3348a33ca5686917796c2759aa5Christopher Tate    }
1076ef58a1509b9d3348a33ca5686917796c2759aa5Christopher Tate
1086ef58a1509b9d3348a33ca5686917796c2759aa5Christopher Tate    private void doEnabled() {
1096ef58a1509b9d3348a33ca5686917796c2759aa5Christopher Tate        try {
1106ef58a1509b9d3348a33ca5686917796c2759aa5Christopher Tate            boolean isEnabled = mBmgr.isBackupEnabled();
1116ef58a1509b9d3348a33ca5686917796c2759aa5Christopher Tate            System.out.println("Backup Manager currently "
1126ef58a1509b9d3348a33ca5686917796c2759aa5Christopher Tate                    + enableToString(isEnabled));
1136ef58a1509b9d3348a33ca5686917796c2759aa5Christopher Tate        } catch (RemoteException e) {
1146ef58a1509b9d3348a33ca5686917796c2759aa5Christopher Tate            System.err.println(e.toString());
1156ef58a1509b9d3348a33ca5686917796c2759aa5Christopher Tate            System.err.println(BMGR_NOT_RUNNING_ERR);
1166ef58a1509b9d3348a33ca5686917796c2759aa5Christopher Tate        }
1176ef58a1509b9d3348a33ca5686917796c2759aa5Christopher Tate    }
1186ef58a1509b9d3348a33ca5686917796c2759aa5Christopher Tate
1196ef58a1509b9d3348a33ca5686917796c2759aa5Christopher Tate    private void doEnable() {
1206ef58a1509b9d3348a33ca5686917796c2759aa5Christopher Tate        String arg = nextArg();
1216ef58a1509b9d3348a33ca5686917796c2759aa5Christopher Tate        if (arg == null) {
1226ef58a1509b9d3348a33ca5686917796c2759aa5Christopher Tate            showUsage();
1236ef58a1509b9d3348a33ca5686917796c2759aa5Christopher Tate            return;
1246ef58a1509b9d3348a33ca5686917796c2759aa5Christopher Tate        }
1256ef58a1509b9d3348a33ca5686917796c2759aa5Christopher Tate
1266ef58a1509b9d3348a33ca5686917796c2759aa5Christopher Tate        try {
1276ef58a1509b9d3348a33ca5686917796c2759aa5Christopher Tate            boolean enable = Boolean.parseBoolean(arg);
1286ef58a1509b9d3348a33ca5686917796c2759aa5Christopher Tate            mBmgr.setBackupEnabled(enable);
1296ef58a1509b9d3348a33ca5686917796c2759aa5Christopher Tate            System.out.println("Backup Manager now " + enableToString(enable));
1306ef58a1509b9d3348a33ca5686917796c2759aa5Christopher Tate        } catch (NumberFormatException e) {
1316ef58a1509b9d3348a33ca5686917796c2759aa5Christopher Tate            showUsage();
1326ef58a1509b9d3348a33ca5686917796c2759aa5Christopher Tate            return;
1336ef58a1509b9d3348a33ca5686917796c2759aa5Christopher Tate        } catch (RemoteException e) {
1346ef58a1509b9d3348a33ca5686917796c2759aa5Christopher Tate            System.err.println(e.toString());
1356ef58a1509b9d3348a33ca5686917796c2759aa5Christopher Tate            System.err.println(BMGR_NOT_RUNNING_ERR);
1366ef58a1509b9d3348a33ca5686917796c2759aa5Christopher Tate        }
1376ef58a1509b9d3348a33ca5686917796c2759aa5Christopher Tate    }
1386ef58a1509b9d3348a33ca5686917796c2759aa5Christopher Tate
139ace7f094bf07bbd90cb998b9462e4f2d101a498cChristopher Tate    private void doRun() {
140ace7f094bf07bbd90cb998b9462e4f2d101a498cChristopher Tate        try {
141ace7f094bf07bbd90cb998b9462e4f2d101a498cChristopher Tate            mBmgr.backupNow();
142ace7f094bf07bbd90cb998b9462e4f2d101a498cChristopher Tate        } catch (RemoteException e) {
143ace7f094bf07bbd90cb998b9462e4f2d101a498cChristopher Tate            System.err.println(e.toString());
144ace7f094bf07bbd90cb998b9462e4f2d101a498cChristopher Tate            System.err.println(BMGR_NOT_RUNNING_ERR);
145ace7f094bf07bbd90cb998b9462e4f2d101a498cChristopher Tate        }
146ace7f094bf07bbd90cb998b9462e4f2d101a498cChristopher Tate    }
147ace7f094bf07bbd90cb998b9462e4f2d101a498cChristopher Tate
148ace7f094bf07bbd90cb998b9462e4f2d101a498cChristopher Tate    private void doBackup() {
149ace7f094bf07bbd90cb998b9462e4f2d101a498cChristopher Tate        boolean isFull = false;
150ace7f094bf07bbd90cb998b9462e4f2d101a498cChristopher Tate        String pkg = nextArg();
151ace7f094bf07bbd90cb998b9462e4f2d101a498cChristopher Tate        if ("-f".equals(pkg)) {
152ace7f094bf07bbd90cb998b9462e4f2d101a498cChristopher Tate            isFull = true;
153ace7f094bf07bbd90cb998b9462e4f2d101a498cChristopher Tate            pkg = nextArg();
154ace7f094bf07bbd90cb998b9462e4f2d101a498cChristopher Tate        }
155ace7f094bf07bbd90cb998b9462e4f2d101a498cChristopher Tate
156ace7f094bf07bbd90cb998b9462e4f2d101a498cChristopher Tate        if (pkg == null || pkg.startsWith("-")) {
157ace7f094bf07bbd90cb998b9462e4f2d101a498cChristopher Tate            showUsage();
158ace7f094bf07bbd90cb998b9462e4f2d101a498cChristopher Tate            return;
159ace7f094bf07bbd90cb998b9462e4f2d101a498cChristopher Tate        }
160ace7f094bf07bbd90cb998b9462e4f2d101a498cChristopher Tate
161ace7f094bf07bbd90cb998b9462e4f2d101a498cChristopher Tate        try {
162ace7f094bf07bbd90cb998b9462e4f2d101a498cChristopher Tate            // !!! TODO: handle full backup
163ace7f094bf07bbd90cb998b9462e4f2d101a498cChristopher Tate            mBmgr.dataChanged(pkg);
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
170abce4e8714bed26a2b37b20ad3f02cf619d71c9aChristopher Tate    private void doTransport() {
171abce4e8714bed26a2b37b20ad3f02cf619d71c9aChristopher Tate        try {
1729171749700853305f3e6abbcdbd9e02f3a71d459Christopher Tate            String which = nextArg();
1739171749700853305f3e6abbcdbd9e02f3a71d459Christopher Tate            String old = mBmgr.selectBackupTransport(which);
1749171749700853305f3e6abbcdbd9e02f3a71d459Christopher Tate            if (old == null) {
1759171749700853305f3e6abbcdbd9e02f3a71d459Christopher Tate                System.out.println("Unknown transport '" + which
1769171749700853305f3e6abbcdbd9e02f3a71d459Christopher Tate                        + "' specified; no changes made.");
1779171749700853305f3e6abbcdbd9e02f3a71d459Christopher Tate            } else {
1789171749700853305f3e6abbcdbd9e02f3a71d459Christopher Tate                System.out.println("Selected transport " + which + " (formerly " + old + ")");
1799171749700853305f3e6abbcdbd9e02f3a71d459Christopher Tate            }
180abce4e8714bed26a2b37b20ad3f02cf619d71c9aChristopher Tate        } catch (RemoteException e) {
181abce4e8714bed26a2b37b20ad3f02cf619d71c9aChristopher Tate            System.err.println(e.toString());
182abce4e8714bed26a2b37b20ad3f02cf619d71c9aChristopher Tate            System.err.println(BMGR_NOT_RUNNING_ERR);
183abce4e8714bed26a2b37b20ad3f02cf619d71c9aChristopher Tate        }
184abce4e8714bed26a2b37b20ad3f02cf619d71c9aChristopher Tate    }
185abce4e8714bed26a2b37b20ad3f02cf619d71c9aChristopher Tate
186ace7f094bf07bbd90cb998b9462e4f2d101a498cChristopher Tate    private void doList() {
187ace7f094bf07bbd90cb998b9462e4f2d101a498cChristopher Tate        String arg = nextArg();     // sets, transports, packages set#
188ace7f094bf07bbd90cb998b9462e4f2d101a498cChristopher Tate        if ("transports".equals(arg)) {
189ace7f094bf07bbd90cb998b9462e4f2d101a498cChristopher Tate            doListTransports();
190ace7f094bf07bbd90cb998b9462e4f2d101a498cChristopher Tate            return;
191ace7f094bf07bbd90cb998b9462e4f2d101a498cChristopher Tate        }
192ace7f094bf07bbd90cb998b9462e4f2d101a498cChristopher Tate
193ace7f094bf07bbd90cb998b9462e4f2d101a498cChristopher Tate        // The rest of the 'list' options work with a restore session on the current transport
194ace7f094bf07bbd90cb998b9462e4f2d101a498cChristopher Tate        try {
1959171749700853305f3e6abbcdbd9e02f3a71d459Christopher Tate            String curTransport = mBmgr.getCurrentTransport();
196ace7f094bf07bbd90cb998b9462e4f2d101a498cChristopher Tate            mRestore = mBmgr.beginRestoreSession(curTransport);
197f68eb500f99361541049e09eb7f9ddd6f4ef4efaChristopher Tate            if (mRestore == null) {
198f68eb500f99361541049e09eb7f9ddd6f4ef4efaChristopher Tate                System.err.println(BMGR_NOT_RUNNING_ERR);
199f68eb500f99361541049e09eb7f9ddd6f4ef4efaChristopher Tate                return;
200f68eb500f99361541049e09eb7f9ddd6f4ef4efaChristopher Tate            }
201ace7f094bf07bbd90cb998b9462e4f2d101a498cChristopher Tate
202ace7f094bf07bbd90cb998b9462e4f2d101a498cChristopher Tate            if ("sets".equals(arg)) {
203ace7f094bf07bbd90cb998b9462e4f2d101a498cChristopher Tate                doListRestoreSets();
2049171749700853305f3e6abbcdbd9e02f3a71d459Christopher Tate            } else if ("transports".equals(arg)) {
2059171749700853305f3e6abbcdbd9e02f3a71d459Christopher Tate                doListTransports();
206ace7f094bf07bbd90cb998b9462e4f2d101a498cChristopher Tate            }
207ace7f094bf07bbd90cb998b9462e4f2d101a498cChristopher Tate
208ace7f094bf07bbd90cb998b9462e4f2d101a498cChristopher Tate            mRestore.endRestoreSession();
209ace7f094bf07bbd90cb998b9462e4f2d101a498cChristopher Tate        } catch (RemoteException e) {
210ace7f094bf07bbd90cb998b9462e4f2d101a498cChristopher Tate            System.err.println(e.toString());
211ace7f094bf07bbd90cb998b9462e4f2d101a498cChristopher Tate            System.err.println(BMGR_NOT_RUNNING_ERR);
212ace7f094bf07bbd90cb998b9462e4f2d101a498cChristopher Tate        }
213ace7f094bf07bbd90cb998b9462e4f2d101a498cChristopher Tate    }
214ace7f094bf07bbd90cb998b9462e4f2d101a498cChristopher Tate
215ace7f094bf07bbd90cb998b9462e4f2d101a498cChristopher Tate    private void doListTransports() {
2169171749700853305f3e6abbcdbd9e02f3a71d459Christopher Tate        try {
2179171749700853305f3e6abbcdbd9e02f3a71d459Christopher Tate            String current = mBmgr.getCurrentTransport();
2189171749700853305f3e6abbcdbd9e02f3a71d459Christopher Tate            String[] transports = mBmgr.listAllTransports();
2199171749700853305f3e6abbcdbd9e02f3a71d459Christopher Tate            if (transports == null || transports.length == 0) {
2209171749700853305f3e6abbcdbd9e02f3a71d459Christopher Tate                System.out.println("No transports available.");
2219171749700853305f3e6abbcdbd9e02f3a71d459Christopher Tate                return;
2229171749700853305f3e6abbcdbd9e02f3a71d459Christopher Tate            }
2239171749700853305f3e6abbcdbd9e02f3a71d459Christopher Tate
2249171749700853305f3e6abbcdbd9e02f3a71d459Christopher Tate            for (String t : transports) {
2259171749700853305f3e6abbcdbd9e02f3a71d459Christopher Tate                String pad = (t.equals(current)) ? "  * " : "    ";
2269171749700853305f3e6abbcdbd9e02f3a71d459Christopher Tate                System.out.println(pad + t);
2279171749700853305f3e6abbcdbd9e02f3a71d459Christopher Tate            }
2289171749700853305f3e6abbcdbd9e02f3a71d459Christopher Tate        } catch (RemoteException e) {
2299171749700853305f3e6abbcdbd9e02f3a71d459Christopher Tate            System.err.println(e.toString());
2309171749700853305f3e6abbcdbd9e02f3a71d459Christopher Tate            System.err.println(BMGR_NOT_RUNNING_ERR);
2319171749700853305f3e6abbcdbd9e02f3a71d459Christopher Tate        }
232ace7f094bf07bbd90cb998b9462e4f2d101a498cChristopher Tate    }
233ace7f094bf07bbd90cb998b9462e4f2d101a498cChristopher Tate
234ace7f094bf07bbd90cb998b9462e4f2d101a498cChristopher Tate    private void doListRestoreSets() {
235ace7f094bf07bbd90cb998b9462e4f2d101a498cChristopher Tate        try {
236ace7f094bf07bbd90cb998b9462e4f2d101a498cChristopher Tate            RestoreSet[] sets = mRestore.getAvailableRestoreSets();
237f68eb500f99361541049e09eb7f9ddd6f4ef4efaChristopher Tate            if (sets == null || sets.length == 0) {
238ace7f094bf07bbd90cb998b9462e4f2d101a498cChristopher Tate                System.out.println("No restore sets available");
239ace7f094bf07bbd90cb998b9462e4f2d101a498cChristopher Tate            } else {
240c73a218c2663e6ae3ec8a9ab8b9524f95702ade9Christopher Tate                printRestoreSets(sets);
241ace7f094bf07bbd90cb998b9462e4f2d101a498cChristopher Tate            }
242ace7f094bf07bbd90cb998b9462e4f2d101a498cChristopher Tate        } catch (RemoteException e) {
243ace7f094bf07bbd90cb998b9462e4f2d101a498cChristopher Tate            System.err.println(e.toString());
244ace7f094bf07bbd90cb998b9462e4f2d101a498cChristopher Tate            System.err.println(TRANSPORT_NOT_RUNNING_ERR);
245ace7f094bf07bbd90cb998b9462e4f2d101a498cChristopher Tate        }
246ace7f094bf07bbd90cb998b9462e4f2d101a498cChristopher Tate    }
247ace7f094bf07bbd90cb998b9462e4f2d101a498cChristopher Tate
248c73a218c2663e6ae3ec8a9ab8b9524f95702ade9Christopher Tate    private void printRestoreSets(RestoreSet[] sets) {
249c73a218c2663e6ae3ec8a9ab8b9524f95702ade9Christopher Tate        for (RestoreSet s : sets) {
250c73a218c2663e6ae3ec8a9ab8b9524f95702ade9Christopher Tate            System.out.println("  " + s.token + " : " + s.name);
251c73a218c2663e6ae3ec8a9ab8b9524f95702ade9Christopher Tate        }
252c73a218c2663e6ae3ec8a9ab8b9524f95702ade9Christopher Tate    }
253c73a218c2663e6ae3ec8a9ab8b9524f95702ade9Christopher Tate
2544a64bded06a0299785c295a975e2818308eb53e2Joe Onorato    class RestoreObserver extends IRestoreObserver.Stub {
2554a64bded06a0299785c295a975e2818308eb53e2Joe Onorato        boolean done;
2564a64bded06a0299785c295a975e2818308eb53e2Joe Onorato        public void restoreStarting(int numPackages) {
2574a64bded06a0299785c295a975e2818308eb53e2Joe Onorato            System.out.println("restoreStarting: " + numPackages + " packages");
2584a64bded06a0299785c295a975e2818308eb53e2Joe Onorato        }
2594a64bded06a0299785c295a975e2818308eb53e2Joe Onorato
2604a64bded06a0299785c295a975e2818308eb53e2Joe Onorato        public void onUpdate(int nowBeingRestored) {
2614a64bded06a0299785c295a975e2818308eb53e2Joe Onorato            System.out.println("onUpdate: " + nowBeingRestored);
2624a64bded06a0299785c295a975e2818308eb53e2Joe Onorato        }
2634a64bded06a0299785c295a975e2818308eb53e2Joe Onorato
2644a64bded06a0299785c295a975e2818308eb53e2Joe Onorato        public void restoreFinished(int error) {
2654a64bded06a0299785c295a975e2818308eb53e2Joe Onorato            System.out.println("restoreFinished: " + error);
2664a64bded06a0299785c295a975e2818308eb53e2Joe Onorato            synchronized (this) {
2674a64bded06a0299785c295a975e2818308eb53e2Joe Onorato                done = true;
2684a64bded06a0299785c295a975e2818308eb53e2Joe Onorato                this.notify();
2694a64bded06a0299785c295a975e2818308eb53e2Joe Onorato            }
2704a64bded06a0299785c295a975e2818308eb53e2Joe Onorato        }
2714a64bded06a0299785c295a975e2818308eb53e2Joe Onorato    }
2724a64bded06a0299785c295a975e2818308eb53e2Joe Onorato
273f68eb500f99361541049e09eb7f9ddd6f4ef4efaChristopher Tate    private void doRestore() {
274156411df4627336b246db78cddca8248ed615b67Dan Egnor        long token;
275f68eb500f99361541049e09eb7f9ddd6f4ef4efaChristopher Tate        try {
276156411df4627336b246db78cddca8248ed615b67Dan Egnor            token = Long.parseLong(nextArg());
277f68eb500f99361541049e09eb7f9ddd6f4ef4efaChristopher Tate        } catch (NumberFormatException e) {
278f68eb500f99361541049e09eb7f9ddd6f4ef4efaChristopher Tate            showUsage();
279f68eb500f99361541049e09eb7f9ddd6f4ef4efaChristopher Tate            return;
280f68eb500f99361541049e09eb7f9ddd6f4ef4efaChristopher Tate        }
281f68eb500f99361541049e09eb7f9ddd6f4ef4efaChristopher Tate
2824a64bded06a0299785c295a975e2818308eb53e2Joe Onorato        RestoreObserver observer = new RestoreObserver();
2834a64bded06a0299785c295a975e2818308eb53e2Joe Onorato
284f68eb500f99361541049e09eb7f9ddd6f4ef4efaChristopher Tate        try {
285c73a218c2663e6ae3ec8a9ab8b9524f95702ade9Christopher Tate            boolean didRestore = false;
2869171749700853305f3e6abbcdbd9e02f3a71d459Christopher Tate            String curTransport = mBmgr.getCurrentTransport();
287f68eb500f99361541049e09eb7f9ddd6f4ef4efaChristopher Tate            mRestore = mBmgr.beginRestoreSession(curTransport);
288f68eb500f99361541049e09eb7f9ddd6f4ef4efaChristopher Tate            if (mRestore == null) {
289f68eb500f99361541049e09eb7f9ddd6f4ef4efaChristopher Tate                System.err.println(BMGR_NOT_RUNNING_ERR);
290f68eb500f99361541049e09eb7f9ddd6f4ef4efaChristopher Tate                return;
291f68eb500f99361541049e09eb7f9ddd6f4ef4efaChristopher Tate            }
292f68eb500f99361541049e09eb7f9ddd6f4ef4efaChristopher Tate            RestoreSet[] sets = mRestore.getAvailableRestoreSets();
293f68eb500f99361541049e09eb7f9ddd6f4ef4efaChristopher Tate            for (RestoreSet s : sets) {
294f68eb500f99361541049e09eb7f9ddd6f4ef4efaChristopher Tate                if (s.token == token) {
295f68eb500f99361541049e09eb7f9ddd6f4ef4efaChristopher Tate                    System.out.println("Scheduling restore: " + s.name);
2964a64bded06a0299785c295a975e2818308eb53e2Joe Onorato                    mRestore.performRestore(token, observer);
297c73a218c2663e6ae3ec8a9ab8b9524f95702ade9Christopher Tate                    didRestore = true;
298f68eb500f99361541049e09eb7f9ddd6f4ef4efaChristopher Tate                    break;
299f68eb500f99361541049e09eb7f9ddd6f4ef4efaChristopher Tate                }
300f68eb500f99361541049e09eb7f9ddd6f4ef4efaChristopher Tate            }
301c73a218c2663e6ae3ec8a9ab8b9524f95702ade9Christopher Tate            if (!didRestore) {
302c73a218c2663e6ae3ec8a9ab8b9524f95702ade9Christopher Tate                if (sets == null || sets.length == 0) {
303c73a218c2663e6ae3ec8a9ab8b9524f95702ade9Christopher Tate                    System.out.println("No available restore sets; no restore performed");
304c73a218c2663e6ae3ec8a9ab8b9524f95702ade9Christopher Tate                } else {
305c73a218c2663e6ae3ec8a9ab8b9524f95702ade9Christopher Tate                    System.out.println("No matching restore set token.  Available sets:");
306c73a218c2663e6ae3ec8a9ab8b9524f95702ade9Christopher Tate                    printRestoreSets(sets);
307c73a218c2663e6ae3ec8a9ab8b9524f95702ade9Christopher Tate                }
308c73a218c2663e6ae3ec8a9ab8b9524f95702ade9Christopher Tate            }
309f68eb500f99361541049e09eb7f9ddd6f4ef4efaChristopher Tate            mRestore.endRestoreSession();
310f68eb500f99361541049e09eb7f9ddd6f4ef4efaChristopher Tate        } catch (RemoteException e) {
311f68eb500f99361541049e09eb7f9ddd6f4ef4efaChristopher Tate            System.err.println(e.toString());
312f68eb500f99361541049e09eb7f9ddd6f4ef4efaChristopher Tate            System.err.println(BMGR_NOT_RUNNING_ERR);
313f68eb500f99361541049e09eb7f9ddd6f4ef4efaChristopher Tate        }
3144a64bded06a0299785c295a975e2818308eb53e2Joe Onorato
3154a64bded06a0299785c295a975e2818308eb53e2Joe Onorato        // now wait for it to be done
3164a64bded06a0299785c295a975e2818308eb53e2Joe Onorato        synchronized (observer) {
3174a64bded06a0299785c295a975e2818308eb53e2Joe Onorato            while (!observer.done) {
3184a64bded06a0299785c295a975e2818308eb53e2Joe Onorato                try {
3194a64bded06a0299785c295a975e2818308eb53e2Joe Onorato                    observer.wait();
3204a64bded06a0299785c295a975e2818308eb53e2Joe Onorato                } catch (InterruptedException ex) {
3214a64bded06a0299785c295a975e2818308eb53e2Joe Onorato                }
3224a64bded06a0299785c295a975e2818308eb53e2Joe Onorato            }
3234a64bded06a0299785c295a975e2818308eb53e2Joe Onorato        }
3244a64bded06a0299785c295a975e2818308eb53e2Joe Onorato        System.out.println("done");
325f68eb500f99361541049e09eb7f9ddd6f4ef4efaChristopher Tate    }
326f68eb500f99361541049e09eb7f9ddd6f4ef4efaChristopher Tate
327ace7f094bf07bbd90cb998b9462e4f2d101a498cChristopher Tate    private String nextArg() {
328ace7f094bf07bbd90cb998b9462e4f2d101a498cChristopher Tate        if (mNextArg >= mArgs.length) {
329ace7f094bf07bbd90cb998b9462e4f2d101a498cChristopher Tate            return null;
330ace7f094bf07bbd90cb998b9462e4f2d101a498cChristopher Tate        }
331ace7f094bf07bbd90cb998b9462e4f2d101a498cChristopher Tate        String arg = mArgs[mNextArg];
332ace7f094bf07bbd90cb998b9462e4f2d101a498cChristopher Tate        mNextArg++;
333ace7f094bf07bbd90cb998b9462e4f2d101a498cChristopher Tate        return arg;
334ace7f094bf07bbd90cb998b9462e4f2d101a498cChristopher Tate    }
335ace7f094bf07bbd90cb998b9462e4f2d101a498cChristopher Tate
336ace7f094bf07bbd90cb998b9462e4f2d101a498cChristopher Tate    private static void showUsage() {
337ace7f094bf07bbd90cb998b9462e4f2d101a498cChristopher Tate        System.err.println("usage: bmgr [backup|restore|list|transport|run]");
3389171749700853305f3e6abbcdbd9e02f3a71d459Christopher Tate        System.err.println("       bmgr backup PACKAGE");
3396ef58a1509b9d3348a33ca5686917796c2759aa5Christopher Tate        System.err.println("       bmgr enable BOOL");
3406ef58a1509b9d3348a33ca5686917796c2759aa5Christopher Tate        System.err.println("       bmgr enabled");
3419171749700853305f3e6abbcdbd9e02f3a71d459Christopher Tate        System.err.println("       bmgr list transports");
342ace7f094bf07bbd90cb998b9462e4f2d101a498cChristopher Tate        System.err.println("       bmgr list sets");
3439171749700853305f3e6abbcdbd9e02f3a71d459Christopher Tate        System.err.println("       bmgr transport WHICH");
3449171749700853305f3e6abbcdbd9e02f3a71d459Christopher Tate        System.err.println("       bmgr restore TOKEN");
345ace7f094bf07bbd90cb998b9462e4f2d101a498cChristopher Tate        System.err.println("       bmgr run");
3469171749700853305f3e6abbcdbd9e02f3a71d459Christopher Tate        System.err.println("");
3479171749700853305f3e6abbcdbd9e02f3a71d459Christopher Tate        System.err.println("The 'backup' command schedules a backup pass for the named package.");
3489171749700853305f3e6abbcdbd9e02f3a71d459Christopher Tate        System.err.println("Note that the backup pass will effectively be a no-op if the package");
3499171749700853305f3e6abbcdbd9e02f3a71d459Christopher Tate        System.err.println("does not actually have changed data to store.");
3509171749700853305f3e6abbcdbd9e02f3a71d459Christopher Tate        System.err.println("");
3516ef58a1509b9d3348a33ca5686917796c2759aa5Christopher Tate        System.err.println("The 'enable' command enables or disables the entire backup mechanism.");
3526ef58a1509b9d3348a33ca5686917796c2759aa5Christopher Tate        System.err.println("If the argument is 'true' it will be enabled, otherwise it will be");
3536ef58a1509b9d3348a33ca5686917796c2759aa5Christopher Tate        System.err.println("disabled.  When disabled, neither backup or restore operations will");
3546ef58a1509b9d3348a33ca5686917796c2759aa5Christopher Tate        System.err.println("be performed.");
3556ef58a1509b9d3348a33ca5686917796c2759aa5Christopher Tate        System.err.println("");
3566ef58a1509b9d3348a33ca5686917796c2759aa5Christopher Tate        System.err.println("The 'enabled' command reports the current enabled/disabled state of");
3576ef58a1509b9d3348a33ca5686917796c2759aa5Christopher Tate        System.err.println("the backup mechanism.");
3586ef58a1509b9d3348a33ca5686917796c2759aa5Christopher Tate        System.err.println("");
3599171749700853305f3e6abbcdbd9e02f3a71d459Christopher Tate        System.err.println("The 'list transports' command reports the names of the backup transports");
3609171749700853305f3e6abbcdbd9e02f3a71d459Christopher Tate        System.err.println("currently available on the device.  These names can be passed as arguments");
3619171749700853305f3e6abbcdbd9e02f3a71d459Christopher Tate        System.err.println("to the 'transport' command.  The currently selected transport is indicated");
3629171749700853305f3e6abbcdbd9e02f3a71d459Christopher Tate        System.err.println("with a '*' character.");
3639171749700853305f3e6abbcdbd9e02f3a71d459Christopher Tate        System.err.println("");
3649171749700853305f3e6abbcdbd9e02f3a71d459Christopher Tate        System.err.println("The 'list sets' command reports the token and name of each restore set");
3659171749700853305f3e6abbcdbd9e02f3a71d459Christopher Tate        System.err.println("available to the device via the current transport.");
3669171749700853305f3e6abbcdbd9e02f3a71d459Christopher Tate        System.err.println("");
3679171749700853305f3e6abbcdbd9e02f3a71d459Christopher Tate        System.err.println("The 'transport' command designates the named transport as the currently");
3689171749700853305f3e6abbcdbd9e02f3a71d459Christopher Tate        System.err.println("active one.  This setting is persistent across reboots.");
3699171749700853305f3e6abbcdbd9e02f3a71d459Christopher Tate        System.err.println("");
3709171749700853305f3e6abbcdbd9e02f3a71d459Christopher Tate        System.err.println("The 'restore' command initiates a restore operation, using the restore set");
3719171749700853305f3e6abbcdbd9e02f3a71d459Christopher Tate        System.err.println("from the current transport whose token matches the argument.");
3729171749700853305f3e6abbcdbd9e02f3a71d459Christopher Tate        System.err.println("");
3739171749700853305f3e6abbcdbd9e02f3a71d459Christopher Tate        System.err.println("The 'run' command causes any scheduled backup operation to be initiated");
3749171749700853305f3e6abbcdbd9e02f3a71d459Christopher Tate        System.err.println("immediately, without the usual waiting period for batching together");
3759171749700853305f3e6abbcdbd9e02f3a71d459Christopher Tate        System.err.println("data changes.");
376ace7f094bf07bbd90cb998b9462e4f2d101a498cChristopher Tate    }
3775e8a4b842c20dd47b82e9915f1bd730ee1b0d46dJoe Onorato}
378