Update list on ACTION_DEVICE_POLICY_MANAGER_STATE_CHANGED

Added BroadcastReceiver for ACTION_DEVICE_POLICY_MANAGER_STATE_CHANGED to
refresh the list, if event has been received. It could be that checkboxes
need to be updated. Also disable the checkbox while admin is being removed.

Bug: 17609838
Change-Id: Id1f72c27111c280a77a03ba6ba26bcdbbb10bb58
diff --git a/src/com/android/settings/DeviceAdminSettings.java b/src/com/android/settings/DeviceAdminSettings.java
index bc22637..334d18f 100644
--- a/src/com/android/settings/DeviceAdminSettings.java
+++ b/src/com/android/settings/DeviceAdminSettings.java
@@ -16,7 +16,6 @@
 
 package com.android.settings;
 
-import org.xmlpull.v1.XmlPullParser;
 import org.xmlpull.v1.XmlPullParserException;
 
 import android.app.Activity;
@@ -25,9 +24,11 @@
 import android.app.admin.DeviceAdminInfo;
 import android.app.admin.DeviceAdminReceiver;
 import android.app.admin.DevicePolicyManager;
+import android.content.BroadcastReceiver;
 import android.content.ComponentName;
 import android.content.Context;
 import android.content.Intent;
+import android.content.IntentFilter;
 import android.content.pm.PackageManager;
 import android.content.pm.ResolveInfo;
 import android.content.res.Resources;
@@ -49,14 +50,13 @@
 
 import java.io.IOException;
 import java.util.ArrayList;
+import java.util.Collection;
 import java.util.Collections;
 import java.util.List;
-import java.util.Collection;
 
 public class DeviceAdminSettings extends ListFragment {
     static final String TAG = "DeviceAdminSettings";
 
-    static final int DIALOG_WARNING = 1;
     private DevicePolicyManager mDPM;
     private UserManager mUm;
 
@@ -70,6 +70,18 @@
     private String mDeviceOwnerPkg;
     private SparseArray<ComponentName> mProfileOwnerComponents = new SparseArray<ComponentName>();
 
+    private final BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() {
+        @Override
+        public void onReceive(Context context, Intent intent) {
+            // Refresh the list, if state change has been received. It could be that checkboxes
+            // need to be updated
+            if (DevicePolicyManager.ACTION_DEVICE_POLICY_MANAGER_STATE_CHANGED.equals(
+                    intent.getAction())) {
+                updateList();
+            }
+        }
+    };
+
     @Override
     public void onCreate(Bundle icicle) {
         super.onCreate(icicle);
@@ -93,6 +105,10 @@
     @Override
     public void onResume() {
         super.onResume();
+        IntentFilter filter = new IntentFilter();
+        filter.addAction(DevicePolicyManager.ACTION_DEVICE_POLICY_MANAGER_STATE_CHANGED);
+        getActivity().registerReceiverAsUser(
+                mBroadcastReceiver, UserHandle.ALL, filter, null, null);
         mDeviceOwnerPkg = mDPM.getDeviceOwner();
         if (mDeviceOwnerPkg != null && !mDPM.isDeviceOwner(mDeviceOwnerPkg)) {
             mDeviceOwnerPkg = null;
@@ -107,6 +123,12 @@
         updateList();
     }
 
+    @Override
+    public void onPause() {
+        getActivity().unregisterReceiver(mBroadcastReceiver);
+        super.onPause();
+    }
+
     /**
      * Update the internal collection of available admins for all profiles associated with the
      * current user.
@@ -264,6 +286,10 @@
                     && (isDeviceOwner(info) || isProfileOwner(info))) {
                 return false;
             }
+            // Disable item if admin is being removed
+            if (isRemovingAdmin(info)) {
+                return false;
+            }
             return true;
         }
 
@@ -340,6 +366,10 @@
         return mDPM.isAdminActiveAsUser(item.getComponent(), getUserId(item));
     }
 
+    private boolean isRemovingAdmin(DeviceAdminInfo item) {
+        return mDPM.isRemovingAdmin(item.getComponent(), getUserId(item));
+    }
+
     /**
      * Add device admins to the internal collection that belong to a profile.
      *