Add managed profile setting page

Bug: 28281560
Change-Id: I1187f834dd9d002ec898a7d343198025963d1c26
This commit is contained in:
Tony Mak
2016-05-03 18:55:41 +01:00
parent 1ec941db0c
commit 64f6d9896f
6 changed files with 266 additions and 59 deletions

View File

@@ -138,6 +138,7 @@ public class Settings extends SettingsActivity {
}
public static class ScreenLockSuggestionActivity extends ChooseLockGeneric { /* empty */ }
public static class WallpaperSettingsActivity extends SettingsActivity { /* empty */ }
public static class ManagedProfileSettingsActivity extends SettingsActivity { /* empty */ }
// Categories.
public static class WirelessSettings extends SettingsActivity { /* empty */ }

View File

@@ -56,6 +56,7 @@ import com.android.settings.accessibility.AccessibilitySettingsForSetupWizard;
import com.android.settings.accessibility.CaptionPropertiesFragment;
import com.android.settings.accounts.AccountSettings;
import com.android.settings.accounts.AccountSyncSettings;
import com.android.settings.accounts.ManagedProfileSettings;
import com.android.settings.applications.AdvancedAppSettings;
import com.android.settings.applications.DrawOverlayDetails;
import com.android.settings.applications.InstalledAppDetails;
@@ -328,6 +329,7 @@ public class SettingsActivity extends SettingsDrawerActivity
AdvancedAppSettings.class.getName(),
WallpaperTypeSettings.class.getName(),
VrListenerSettings.class.getName(),
ManagedProfileSettings.class.getName(),
};

View File

@@ -39,9 +39,7 @@ import android.os.Process;
import android.os.UserHandle;
import android.os.UserManager;
import android.provider.SearchIndexableResource;
import android.support.v14.preference.SwitchPreference;
import android.support.v7.preference.Preference;
import android.support.v7.preference.Preference.OnPreferenceChangeListener;
import android.support.v7.preference.Preference.OnPreferenceClickListener;
import android.support.v7.preference.PreferenceGroup;
import android.support.v7.preference.PreferenceScreen;
@@ -55,6 +53,7 @@ import com.android.internal.logging.MetricsProto.MetricsEvent;
import com.android.settings.AccessiblePreferenceCategory;
import com.android.settings.DimmableIconPreference;
import com.android.settings.R;
import com.android.settings.SettingsActivity;
import com.android.settings.SettingsPreferenceFragment;
import com.android.settings.Utils;
import com.android.settings.search.BaseSearchIndexProvider;
@@ -84,7 +83,7 @@ import static android.provider.Settings.EXTRA_AUTHORITIES;
*/
public class AccountSettings extends SettingsPreferenceFragment
implements AuthenticatorHelper.OnAccountsUpdateListener,
OnPreferenceClickListener, OnPreferenceChangeListener, Indexable {
OnPreferenceClickListener, Indexable {
public static final String TAG = "AccountSettings";
private static final String KEY_ACCOUNT = "account";
@@ -116,14 +115,14 @@ public class AccountSettings extends SettingsPreferenceFragment
* The preference that displays the add account button.
*/
public DimmableIconPreference addAccountPreference;
/**
* The preference that displays the button to toggle work profile.
*/
public SwitchPreference workModeSwitch;
/**
* The preference that displays the button to remove the managed profile
*/
public Preference removeWorkProfilePreference;
/**
* The preference that displays managed profile settings.
*/
public Preference managedProfilePreference;
/**
* The {@link AuthenticatorHelper} that holds accounts data for this profile.
*/
@@ -239,23 +238,12 @@ public class AccountSettings extends SettingsPreferenceFragment
).show();
return true;
}
}
return false;
}
@Override
public boolean onPreferenceChange(Preference preference, Object newValue) {
// Check the preference
final int count = mProfiles.size();
for (int i = 0; i < count; i++) {
ProfileData profileData = mProfiles.valueAt(i);
if (preference == profileData.workModeSwitch) {
final int userId = profileData.userInfo.id;
if ((boolean) newValue) {
mUm.trySetQuietModeDisabled(userId, null);
} else {
mUm.setQuietModeEnabled(userId, true);
}
if (preference == profileData.managedProfilePreference) {
Bundle arguments = new Bundle();
arguments.putParcelable(Intent.EXTRA_USER, profileData.userInfo.getUserHandle());
((SettingsActivity) getActivity()).startPreferencePanel(
ManagedProfileSettings.class.getName(), arguments,
R.string.managed_profile_settings_title, null, null, 0);
return true;
}
}
@@ -314,10 +302,8 @@ public class AccountSettings extends SettingsPreferenceFragment
profileData.preferenceGroup.setSummary(workGroupSummary);
((AccessiblePreferenceCategory) profileData.preferenceGroup).setContentDescription(
getString(R.string.accessibility_category_work, workGroupSummary));
profileData.workModeSwitch = newWorkModeSwitchPreference(context);
final UserHandle userHandle = profileData.userInfo.getUserHandle();
profileData.workModeSwitch.setChecked(!mUm.isQuietModeEnabled(userHandle));
profileData.removeWorkProfilePreference = newRemoveWorkProfilePreference(context);
profileData.managedProfilePreference = newManagedProfileSettings();
} else {
profileData.preferenceGroup.setTitle(R.string.category_personal);
((AccessiblePreferenceCategory) profileData.preferenceGroup).setContentDescription(
@@ -351,15 +337,6 @@ public class AccountSettings extends SettingsPreferenceFragment
return preference;
}
private SwitchPreference newWorkModeSwitchPreference(Context context) {
SwitchPreference preference = new SwitchPreference(getPrefContext());
preference.setTitle(R.string.work_mode_label);
preference.setSummary(R.string.work_mode_summary);
preference.setOnPreferenceChangeListener(this);
preference.setOrder(ORDER_NEXT_TO_LAST);
return preference;
}
private Preference newRemoveWorkProfilePreference(Context context) {
Preference preference = new Preference(getPrefContext());
preference.setTitle(R.string.remove_managed_profile_label);
@@ -369,6 +346,16 @@ public class AccountSettings extends SettingsPreferenceFragment
return preference;
}
private Preference newManagedProfileSettings() {
Preference preference = new Preference(getPrefContext());
preference.setTitle(R.string.managed_profile_settings_title);
preference.setIcon(R.drawable.ic_sysbar_quicksettings);
preference.setOnPreferenceClickListener(this);
preference.setOrder(ORDER_NEXT_TO_LAST);
return preference;
}
private String getWorkGroupSummary(Context context, UserInfo userInfo) {
PackageManager packageManager = context.getPackageManager();
ApplicationInfo adminApplicationInfo = Utils.getAdminApplicationInfo(context, userInfo.id);
@@ -428,12 +415,12 @@ public class AccountSettings extends SettingsPreferenceFragment
R.string.managed_profile_not_available_label);
profileData.preferenceGroup.addPreference(mProfileNotAvailablePreference);
}
if (profileData.workModeSwitch != null) {
profileData.preferenceGroup.addPreference(profileData.workModeSwitch);
}
if (profileData.removeWorkProfilePreference != null) {
profileData.preferenceGroup.addPreference(profileData.removeWorkProfilePreference);
}
if (profileData.managedProfilePreference != null) {
profileData.preferenceGroup.addPreference(profileData.managedProfilePreference);
}
}
private ArrayList<AccountPreference> getAccountTypePreferences(AuthenticatorHelper helper,
@@ -594,18 +581,6 @@ public class AccountSettings extends SettingsPreferenceFragment
getActivity().invalidateOptionsMenu();
return;
}
if (action.equals(Intent.ACTION_MANAGED_PROFILE_AVAILABLE)
|| action.equals(Intent.ACTION_MANAGED_PROFILE_UNAVAILABLE)) {
// We assume there's only one managed profile, otherwise this needs to change.
ProfileData profileData = mProfiles.valueAt(1);
if (intent.getIntExtra(Intent.EXTRA_USER_HANDLE,
UserHandle.USER_NULL) == profileData.userInfo.id) {
profileData.workModeSwitch.setChecked(
!mUm.isQuietModeEnabled(profileData.userInfo.getUserHandle()));
}
return;
}
Log.w(TAG, "Cannot handle received broadcast: " + intent.getAction());
}
@@ -614,8 +589,6 @@ public class AccountSettings extends SettingsPreferenceFragment
IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction(Intent.ACTION_MANAGED_PROFILE_REMOVED);
intentFilter.addAction(Intent.ACTION_MANAGED_PROFILE_ADDED);
intentFilter.addAction(Intent.ACTION_MANAGED_PROFILE_AVAILABLE);
intentFilter.addAction(Intent.ACTION_MANAGED_PROFILE_UNAVAILABLE);
context.registerReceiver(this, intentFilter);
listeningToManagedProfileEvents = true;
}
@@ -729,17 +702,23 @@ public class AccountSettings extends SettingsPreferenceFragment
if (!RestrictedLockUtils.hasBaseUserRestriction(context,
DISALLOW_MODIFY_ACCOUNTS, userInfo.id)) {
SearchIndexableRaw data = new SearchIndexableRaw(context);
data = new SearchIndexableRaw(context);
data.title = res.getString(R.string.add_account_label);
data.screenTitle = screenTitle;
result.add(data);
}
if (userInfo.isManagedProfile()) {
SearchIndexableRaw data = new SearchIndexableRaw(context);
data = new SearchIndexableRaw(context);
data.title = res.getString(R.string.remove_managed_profile_label);
data.screenTitle = screenTitle;
result.add(data);
{
SearchIndexableRaw data = new SearchIndexableRaw(context);
data.title = res.getString(R.string.remove_managed_profile_label);
data.screenTitle = screenTitle;
result.add(data);
}
{
SearchIndexableRaw data = new SearchIndexableRaw(context);
data.title = res.getString(R.string.managed_profile_settings_title);
data.screenTitle = screenTitle;
result.add(data);
}
}
}
}

View File

@@ -0,0 +1,185 @@
/*
* Copyright (C) 2016 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.settings.accounts;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.os.UserHandle;
import android.os.UserManager;
import android.provider.Settings;
import android.support.v14.preference.SwitchPreference;
import android.support.v7.preference.Preference;
import android.util.Log;
import com.android.internal.logging.MetricsProto;
import com.android.settings.R;
import com.android.settings.SettingsPreferenceFragment;
import com.android.settingslib.RestrictedLockUtils;
import com.android.settingslib.RestrictedSwitchPreference;
import static android.provider.Settings.Secure.MANAGED_PROFILE_CONTACT_REMOTE_SEARCH;
/**
* Setting page for managed profile.
* FIXME: It currently assumes there is only one managed profile.
*/
public class ManagedProfileSettings extends SettingsPreferenceFragment
implements Preference.OnPreferenceChangeListener {
private SwitchPreference mWorkModePreference;
private RestrictedSwitchPreference mContactPrefrence;
private UserManager mUserManager;
private UserHandle mManagedUser;
private Context mContext;
private ManagedProfileBroadcastReceiver mManagedProfileBroadcastReceiver;
private static final String KEY_WORK_MODE = "work_mode";
private static final String KEY_CONTACT = "contacts_search";
private static final String TAG = "ManagedProfileSettings";
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
addPreferencesFromResource(R.xml.managed_profile_settings);
mWorkModePreference = (SwitchPreference) findPreference(KEY_WORK_MODE);
mWorkModePreference.setOnPreferenceChangeListener(this);
mContactPrefrence = (RestrictedSwitchPreference) findPreference(KEY_CONTACT);
mContactPrefrence.setOnPreferenceChangeListener(this);
mContext = getActivity().getApplicationContext();
mUserManager = (UserManager) getSystemService(Context.USER_SERVICE);
mManagedUser = getManagedUserFromArgument();
if (mManagedUser == null) {
getActivity().finish();
}
mManagedProfileBroadcastReceiver = new ManagedProfileBroadcastReceiver();
mManagedProfileBroadcastReceiver.register(getActivity());
}
@Override
public void onResume() {
super.onResume();
loadDataAndPopulateUi();
}
@Override
public void onDestroy() {
super.onDestroy();
mManagedProfileBroadcastReceiver.unregister(getActivity());
}
private UserHandle getManagedUserFromArgument() {
Bundle arguments = getArguments();
if (arguments != null) {
UserHandle userHandle = arguments.getParcelable(Intent.EXTRA_USER);
if (userHandle != null) {
if (mUserManager.isManagedProfile(userHandle.getIdentifier())) {
return userHandle;
}
}
}
return null;
}
private void loadDataAndPopulateUi() {
if (mWorkModePreference != null) {
mWorkModePreference.setChecked(
!mUserManager.isQuietModeEnabled(mManagedUser));
}
if (mContactPrefrence != null) {
int value = Settings.Secure.getIntForUser(getContentResolver(),
MANAGED_PROFILE_CONTACT_REMOTE_SEARCH, 0, mManagedUser.getIdentifier());
mContactPrefrence.setChecked(value != 0);
RestrictedLockUtils.EnforcedAdmin enforcedAdmin =
RestrictedLockUtils.checkIfRemoteContactSearchDisallowed(
mContext, mManagedUser.getIdentifier());
mContactPrefrence.setDisabledByAdmin(enforcedAdmin);
}
}
@Override
protected int getMetricsCategory() {
return MetricsProto.MetricsEvent.ACCOUNTS_WORK_PROFILE_SETTINGS;
}
@Override
public boolean onPreferenceChange(Preference preference, Object newValue) {
if (preference == mWorkModePreference) {
if ((boolean) newValue) {
mUserManager.trySetQuietModeDisabled(mManagedUser.getIdentifier(), null);
} else {
mUserManager.setQuietModeEnabled(mManagedUser.getIdentifier(), true);
}
return true;
}
if (preference == mContactPrefrence) {
int value = ((boolean) newValue == true) ? 1 : 0;
Settings.Secure.putIntForUser(getContentResolver(),
MANAGED_PROFILE_CONTACT_REMOTE_SEARCH, value, mManagedUser.getIdentifier());
return true;
}
return false;
}
private class ManagedProfileBroadcastReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
final String action = intent.getAction();
Log.v(TAG, "Received broadcast: " + action);
if (action.equals(Intent.ACTION_MANAGED_PROFILE_REMOVED)) {
if (intent.getIntExtra(Intent.EXTRA_USER_HANDLE,
UserHandle.USER_NULL) == mManagedUser.getIdentifier()) {
getActivity().finish();
}
return;
}
if (action.equals(Intent.ACTION_MANAGED_PROFILE_AVAILABLE)
|| action.equals(Intent.ACTION_MANAGED_PROFILE_UNAVAILABLE)) {
if (intent.getIntExtra(Intent.EXTRA_USER_HANDLE,
UserHandle.USER_NULL) == mManagedUser.getIdentifier()) {
mWorkModePreference.setChecked(
!mUserManager.isQuietModeEnabled(mManagedUser));
}
return;
}
Log.w(TAG, "Cannot handle received broadcast: " + intent.getAction());
}
public void register(Context context) {
IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction(Intent.ACTION_MANAGED_PROFILE_REMOVED);
intentFilter.addAction(Intent.ACTION_MANAGED_PROFILE_AVAILABLE);
intentFilter.addAction(Intent.ACTION_MANAGED_PROFILE_UNAVAILABLE);
context.registerReceiver(this, intentFilter);
}
public void unregister(Context context) {
context.unregisterReceiver(this);
}
}
}