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
This commit is contained in:
Fyodor Kupolov
2014-11-25 17:34:22 -08:00
parent 647f9752ce
commit cfae855d67

View File

@@ -16,7 +16,6 @@
package com.android.settings; package com.android.settings;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException; import org.xmlpull.v1.XmlPullParserException;
import android.app.Activity; import android.app.Activity;
@@ -25,9 +24,11 @@ import android.app.ListFragment;
import android.app.admin.DeviceAdminInfo; import android.app.admin.DeviceAdminInfo;
import android.app.admin.DeviceAdminReceiver; import android.app.admin.DeviceAdminReceiver;
import android.app.admin.DevicePolicyManager; import android.app.admin.DevicePolicyManager;
import android.content.BroadcastReceiver;
import android.content.ComponentName; import android.content.ComponentName;
import android.content.Context; import android.content.Context;
import android.content.Intent; import android.content.Intent;
import android.content.IntentFilter;
import android.content.pm.PackageManager; import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo; import android.content.pm.ResolveInfo;
import android.content.res.Resources; import android.content.res.Resources;
@@ -49,14 +50,13 @@ import android.widget.TextView;
import java.io.IOException; import java.io.IOException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections; import java.util.Collections;
import java.util.List; import java.util.List;
import java.util.Collection;
public class DeviceAdminSettings extends ListFragment { public class DeviceAdminSettings extends ListFragment {
static final String TAG = "DeviceAdminSettings"; static final String TAG = "DeviceAdminSettings";
static final int DIALOG_WARNING = 1;
private DevicePolicyManager mDPM; private DevicePolicyManager mDPM;
private UserManager mUm; private UserManager mUm;
@@ -70,6 +70,18 @@ public class DeviceAdminSettings extends ListFragment {
private String mDeviceOwnerPkg; private String mDeviceOwnerPkg;
private SparseArray<ComponentName> mProfileOwnerComponents = new SparseArray<ComponentName>(); 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 @Override
public void onCreate(Bundle icicle) { public void onCreate(Bundle icicle) {
super.onCreate(icicle); super.onCreate(icicle);
@@ -93,6 +105,10 @@ public class DeviceAdminSettings extends ListFragment {
@Override @Override
public void onResume() { public void onResume() {
super.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(); mDeviceOwnerPkg = mDPM.getDeviceOwner();
if (mDeviceOwnerPkg != null && !mDPM.isDeviceOwner(mDeviceOwnerPkg)) { if (mDeviceOwnerPkg != null && !mDPM.isDeviceOwner(mDeviceOwnerPkg)) {
mDeviceOwnerPkg = null; mDeviceOwnerPkg = null;
@@ -107,6 +123,12 @@ public class DeviceAdminSettings extends ListFragment {
updateList(); updateList();
} }
@Override
public void onPause() {
getActivity().unregisterReceiver(mBroadcastReceiver);
super.onPause();
}
/** /**
* Update the internal collection of available admins for all profiles associated with the * Update the internal collection of available admins for all profiles associated with the
* current user. * current user.
@@ -264,6 +286,10 @@ public class DeviceAdminSettings extends ListFragment {
&& (isDeviceOwner(info) || isProfileOwner(info))) { && (isDeviceOwner(info) || isProfileOwner(info))) {
return false; return false;
} }
// Disable item if admin is being removed
if (isRemovingAdmin(info)) {
return false;
}
return true; return true;
} }
@@ -340,6 +366,10 @@ public class DeviceAdminSettings extends ListFragment {
return mDPM.isAdminActiveAsUser(item.getComponent(), getUserId(item)); 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. * Add device admins to the internal collection that belong to a profile.
* *