Add policy transparency to "Remove work profile"

Bug: 32301173

Test: Manual test

Change-Id: Idcb75d2d40b8e7b59467e488d3e9837afe7c424a
This commit is contained in:
Tony Mak
2016-11-07 18:21:46 +00:00
parent 279d9e3b9c
commit c8041d8228

View File

@@ -19,6 +19,7 @@ package com.android.settings.accounts;
import android.accounts.Account; import android.accounts.Account;
import android.accounts.AccountManager; import android.accounts.AccountManager;
import android.annotation.UserIdInt;
import android.app.ActivityManager; import android.app.ActivityManager;
import android.app.Dialog; import android.app.Dialog;
import android.content.BroadcastReceiver; import android.content.BroadcastReceiver;
@@ -61,6 +62,7 @@ import com.android.settings.search.Indexable;
import com.android.settings.search.SearchIndexableRaw; import com.android.settings.search.SearchIndexableRaw;
import com.android.settings.users.UserDialogs; import com.android.settings.users.UserDialogs;
import com.android.settingslib.RestrictedLockUtils; import com.android.settingslib.RestrictedLockUtils;
import com.android.settingslib.RestrictedPreference;
import com.android.settingslib.accounts.AuthenticatorHelper; import com.android.settingslib.accounts.AuthenticatorHelper;
import java.util.ArrayList; import java.util.ArrayList;
@@ -71,6 +73,7 @@ import java.util.List;
import static android.content.Intent.EXTRA_USER; import static android.content.Intent.EXTRA_USER;
import static android.os.UserManager.DISALLOW_MODIFY_ACCOUNTS; import static android.os.UserManager.DISALLOW_MODIFY_ACCOUNTS;
import static android.os.UserManager.DISALLOW_REMOVE_USER;
import static android.provider.Settings.EXTRA_AUTHORITIES; import static android.provider.Settings.EXTRA_AUTHORITIES;
/** /**
@@ -117,7 +120,7 @@ public class AccountSettings extends SettingsPreferenceFragment
/** /**
* The preference that displays the button to remove the managed profile * The preference that displays the button to remove the managed profile
*/ */
public Preference removeWorkProfilePreference; public RestrictedPreference removeWorkProfilePreference;
/** /**
* The preference that displays managed profile settings. * The preference that displays managed profile settings.
*/ */
@@ -296,6 +299,8 @@ public class AccountSettings extends SettingsPreferenceFragment
((AccessiblePreferenceCategory) profileData.preferenceGroup).setContentDescription( ((AccessiblePreferenceCategory) profileData.preferenceGroup).setContentDescription(
getString(R.string.accessibility_category_work, workGroupSummary)); getString(R.string.accessibility_category_work, workGroupSummary));
profileData.removeWorkProfilePreference = newRemoveWorkProfilePreference(context); profileData.removeWorkProfilePreference = newRemoveWorkProfilePreference(context);
enforceRestrictionOnPreference(profileData.removeWorkProfilePreference,
DISALLOW_REMOVE_USER, UserHandle.myUserId());
profileData.managedProfilePreference = newManagedProfileSettings(); profileData.managedProfilePreference = newManagedProfileSettings();
} else { } else {
profileData.preferenceGroup.setTitle(R.string.category_personal); profileData.preferenceGroup.setTitle(R.string.category_personal);
@@ -310,19 +315,29 @@ public class AccountSettings extends SettingsPreferenceFragment
profileData.authenticatorHelper = new AuthenticatorHelper(context, profileData.authenticatorHelper = new AuthenticatorHelper(context,
userInfo.getUserHandle(), this); userInfo.getUserHandle(), this);
profileData.addAccountPreference = newAddAccountPreference(context); profileData.addAccountPreference = newAddAccountPreference(context);
if (RestrictedLockUtils.hasBaseUserRestriction(context, enforceRestrictionOnPreference(profileData.addAccountPreference,
UserManager.DISALLOW_MODIFY_ACCOUNTS, userInfo.id)) { DISALLOW_MODIFY_ACCOUNTS, userInfo.id);
profileData.addAccountPreference.setEnabled(false);
} else {
profileData.addAccountPreference.checkRestrictionAndSetDisabled(
DISALLOW_MODIFY_ACCOUNTS, userInfo.id);
}
} }
mProfiles.put(userInfo.id, profileData); mProfiles.put(userInfo.id, profileData);
Index.getInstance(getActivity()).updateFromClassNameResource( Index.getInstance(getActivity()).updateFromClassNameResource(
AccountSettings.class.getName(), true, true); AccountSettings.class.getName(), true, true);
} }
/**
* Configure the UI of the preference by checking user restriction.
* @param preference The preference we are configuring.
* @param userRestriction The user restriction related to the preference.
* @param userId The user that we retrieve user restriction of.
*/
private void enforceRestrictionOnPreference(RestrictedPreference preference,
String userRestriction, @UserIdInt int userId) {
if (RestrictedLockUtils.hasBaseUserRestriction(getActivity(), userRestriction, userId)) {
preference.setEnabled(false);
} else {
preference.checkRestrictionAndSetDisabled(userRestriction, userId);
}
}
private DimmableIconPreference newAddAccountPreference(Context context) { private DimmableIconPreference newAddAccountPreference(Context context) {
DimmableIconPreference preference = new DimmableIconPreference(getPrefContext()); DimmableIconPreference preference = new DimmableIconPreference(getPrefContext());
preference.setTitle(R.string.add_account_label); preference.setTitle(R.string.add_account_label);
@@ -332,8 +347,8 @@ public class AccountSettings extends SettingsPreferenceFragment
return preference; return preference;
} }
private Preference newRemoveWorkProfilePreference(Context context) { private RestrictedPreference newRemoveWorkProfilePreference(Context context) {
Preference preference = new Preference(getPrefContext()); RestrictedPreference preference = new RestrictedPreference(getPrefContext());
preference.setTitle(R.string.remove_managed_profile_label); preference.setTitle(R.string.remove_managed_profile_label);
preference.setIcon(R.drawable.ic_menu_delete); preference.setIcon(R.drawable.ic_menu_delete);
preference.setOnPreferenceClickListener(this); preference.setOnPreferenceClickListener(this);
@@ -341,7 +356,6 @@ public class AccountSettings extends SettingsPreferenceFragment
return preference; return preference;
} }
private Preference newManagedProfileSettings() { private Preference newManagedProfileSettings() {
Preference preference = new Preference(getPrefContext()); Preference preference = new Preference(getPrefContext());
preference.setTitle(R.string.managed_profile_settings_title); preference.setTitle(R.string.managed_profile_settings_title);
@@ -685,7 +699,8 @@ public class AccountSettings extends SettingsPreferenceFragment
result.add(data); result.add(data);
} }
if (userInfo.isManagedProfile()) { if (userInfo.isManagedProfile()) {
{ if (!RestrictedLockUtils.hasBaseUserRestriction(context,
DISALLOW_REMOVE_USER, UserHandle.myUserId())) {
SearchIndexableRaw data = new SearchIndexableRaw(context); SearchIndexableRaw data = new SearchIndexableRaw(context);
data.title = res.getString(R.string.remove_managed_profile_label); data.title = res.getString(R.string.remove_managed_profile_label);
data.screenTitle = screenTitle; data.screenTitle = screenTitle;