OSDN Git Service

Disable bmgr if BMS is not running
authorAnnie Meng <anniemeng@google.com>
Mon, 4 Jun 2018 18:12:16 +0000 (19:12 +0100)
committerAnnie Meng <anniemeng@google.com>
Tue, 5 Jun 2018 18:25:37 +0000 (19:25 +0100)
If BMS is not running, we should not run any bmgr commands and print an
error. This can occur after a device with a lockscreen reboots and has
not unlocked yet, as the backup service is not active before unlocking.

Bug: 80691476
Test: 1) Device with lock + adb reboot -> run any 'adb shell bmgr'
command -> prints error
2) Device with no lock + adb reboot -> run any 'adb shell bmgr' command
-> success

Change-Id: I101b61d18a637cdb945ffc4a5e989a5dd270ee32

cmds/bmgr/src/com/android/commands/bmgr/Bmgr.java

index 84a04e5..5e61633 100644 (file)
@@ -71,9 +71,7 @@ public final class Bmgr {
             return;
         }
 
-        mBmgr = IBackupManager.Stub.asInterface(ServiceManager.getService("backup"));
-        if (mBmgr == null) {
-            System.err.println(BMGR_NOT_RUNNING_ERR);
+        if (!isBmgrActive()) {
             return;
         }
 
@@ -150,6 +148,27 @@ public final class Bmgr {
         showUsage();
     }
 
+    private boolean isBmgrActive() {
+        mBmgr = IBackupManager.Stub.asInterface(ServiceManager.getService("backup"));
+        if (mBmgr == null) {
+            System.err.println(BMGR_NOT_RUNNING_ERR);
+            return false;
+        }
+
+        try {
+            if (!mBmgr.isBackupServiceActive(UserHandle.USER_SYSTEM)) {
+                System.err.println(BMGR_NOT_RUNNING_ERR);
+                return false;
+            }
+        } catch (RemoteException e) {
+            System.err.println(e.toString());
+            System.err.println(BMGR_NOT_RUNNING_ERR);
+            return false;
+        }
+
+        return true;
+    }
+
     private String enableToString(boolean enabled) {
         return enabled ? "enabled" : "disabled";
     }