Bmgr.java revision f68eb500f99361541049e09eb7f9ddd6f4ef4efa
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        try {
40            new Bmgr().run(args);
41        } catch (Exception e) {
42            System.err.println("Exception caught:");
43            e.printStackTrace();
44        }
45    }
46
47    public void run(String[] args) {
48        boolean validCommand = false;
49        if (args.length < 1) {
50            showUsage();
51            return;
52        }
53
54        mBmgr = IBackupManager.Stub.asInterface(ServiceManager.getService("backup"));
55        if (mBmgr == null) {
56            System.err.println(BMGR_NOT_RUNNING_ERR);
57            return;
58        }
59
60        mArgs = args;
61        String op = args[0];
62        mNextArg = 1;
63
64        if ("run".equals(op)) {
65            doRun();
66            return;
67        }
68
69        if ("backup".equals(op)) {
70            doBackup();
71            return;
72        }
73
74        if ("list".equals(op)) {
75            doList();
76            return;
77        }
78
79        if ("restore".equals(op)) {
80            doRestore();
81            return;
82        }
83    }
84
85    private void doRun() {
86        try {
87            mBmgr.backupNow();
88        } catch (RemoteException e) {
89            System.err.println(e.toString());
90            System.err.println(BMGR_NOT_RUNNING_ERR);
91        }
92    }
93
94    private void doBackup() {
95        boolean isFull = false;
96        String pkg = nextArg();
97        if ("-f".equals(pkg)) {
98            isFull = true;
99            pkg = nextArg();
100        }
101
102        if (pkg == null || pkg.startsWith("-")) {
103            showUsage();
104            return;
105        }
106
107        try {
108            // !!! TODO: handle full backup
109            mBmgr.dataChanged(pkg);
110        } catch (RemoteException e) {
111            System.err.println(e.toString());
112            System.err.println(BMGR_NOT_RUNNING_ERR);
113        }
114    }
115
116    private void doList() {
117        String arg = nextArg();     // sets, transports, packages set#
118        if ("transports".equals(arg)) {
119            doListTransports();
120            return;
121        }
122
123        // The rest of the 'list' options work with a restore session on the current transport
124        try {
125            int curTransport = mBmgr.getCurrentTransport();
126            mRestore = mBmgr.beginRestoreSession(curTransport);
127            if (mRestore == null) {
128                System.err.println(BMGR_NOT_RUNNING_ERR);
129                return;
130            }
131
132            if ("sets".equals(arg)) {
133                doListRestoreSets();
134            }
135
136            mRestore.endRestoreSession();
137        } catch (RemoteException e) {
138            System.err.println(e.toString());
139            System.err.println(BMGR_NOT_RUNNING_ERR);
140        }
141    }
142
143    private void doListTransports() {
144    }
145
146    private void doListRestoreSets() {
147        try {
148            RestoreSet[] sets = mRestore.getAvailableRestoreSets();
149            if (sets == null || sets.length == 0) {
150                System.out.println("No restore sets available");
151            } else {
152                for (RestoreSet s : sets) {
153                    System.out.println("  " + s.token + " : " + s.name);
154                }
155            }
156        } catch (RemoteException e) {
157            System.err.println(e.toString());
158            System.err.println(TRANSPORT_NOT_RUNNING_ERR);
159        }
160    }
161
162    private void doRestore() {
163        int token;
164        try {
165            token = Integer.parseInt(nextArg());
166        } catch (NumberFormatException e) {
167            showUsage();
168            return;
169        }
170
171        try {
172            int curTransport = mBmgr.getCurrentTransport();
173            mRestore = mBmgr.beginRestoreSession(curTransport);
174            if (mRestore == null) {
175                System.err.println(BMGR_NOT_RUNNING_ERR);
176                return;
177            }
178            RestoreSet[] sets = mRestore.getAvailableRestoreSets();
179            for (RestoreSet s : sets) {
180                if (s.token == token) {
181                    System.out.println("Scheduling restore: " + s.name);
182                    mRestore.performRestore(token);
183                    break;
184                }
185            }
186            mRestore.endRestoreSession();
187        } catch (RemoteException e) {
188            System.err.println(e.toString());
189            System.err.println(BMGR_NOT_RUNNING_ERR);
190        }
191    }
192
193    private String nextArg() {
194        if (mNextArg >= mArgs.length) {
195            return null;
196        }
197        String arg = mArgs[mNextArg];
198        mNextArg++;
199        return arg;
200    }
201
202    private static void showUsage() {
203        System.err.println("usage: bmgr [backup|restore|list|transport|run]");
204        System.err.println("       bmgr backup [-f] package");
205        System.err.println("       bmgr list sets");
206        System.err.println("       #bmgr list transports");
207        System.err.println("       #bmgr transport which#");
208        System.err.println("       bmgr restore token#");
209        System.err.println("       bmgr run");
210    }
211}