Merge "Migrate ManagedProfileSettings to DashboardFragment"
This commit is contained in:
committed by
Android (Google) Code Review
commit
cbcbb74a5d
@@ -0,0 +1,88 @@
|
||||
/*
|
||||
* Copyright (C) 2018 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 static android.provider.Settings.Secure.MANAGED_PROFILE_CONTACT_REMOTE_SEARCH;
|
||||
|
||||
import android.content.Context;
|
||||
import android.os.UserHandle;
|
||||
import android.provider.Settings;
|
||||
import android.support.v7.preference.Preference;
|
||||
|
||||
import com.android.settings.core.BasePreferenceController;
|
||||
import com.android.settings.slices.SliceData;
|
||||
import com.android.settingslib.RestrictedLockUtils;
|
||||
import com.android.settingslib.RestrictedSwitchPreference;
|
||||
|
||||
public class ContactSearchPreferenceController extends BasePreferenceController implements
|
||||
Preference.OnPreferenceChangeListener {
|
||||
|
||||
private UserHandle mManagedUser;
|
||||
|
||||
public ContactSearchPreferenceController(Context context, String key) {
|
||||
super(context, key);
|
||||
}
|
||||
|
||||
public void setManagedUser(UserHandle managedUser) {
|
||||
mManagedUser = managedUser;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getAvailabilityStatus() {
|
||||
return (mManagedUser != null) ? AVAILABLE : DISABLED_FOR_USER;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void updateState(Preference preference) {
|
||||
super.updateState(preference);
|
||||
if (preference instanceof RestrictedSwitchPreference) {
|
||||
final RestrictedSwitchPreference pref = (RestrictedSwitchPreference) preference;
|
||||
pref.setChecked(isChecked());
|
||||
if (mManagedUser != null) {
|
||||
final RestrictedLockUtils.EnforcedAdmin enforcedAdmin =
|
||||
RestrictedLockUtils.checkIfRemoteContactSearchDisallowed(
|
||||
mContext, mManagedUser.getIdentifier());
|
||||
pref.setDisabledByAdmin(enforcedAdmin);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private boolean isChecked() {
|
||||
if (mManagedUser == null) {
|
||||
return false;
|
||||
}
|
||||
return 0 != Settings.Secure.getIntForUser(mContext.getContentResolver(),
|
||||
MANAGED_PROFILE_CONTACT_REMOTE_SEARCH, 0, mManagedUser.getIdentifier());
|
||||
}
|
||||
|
||||
private boolean setChecked(boolean isChecked) {
|
||||
if (mManagedUser != null) {
|
||||
final int value = isChecked ? 1 : 0;
|
||||
Settings.Secure.putIntForUser(mContext.getContentResolver(),
|
||||
MANAGED_PROFILE_CONTACT_REMOTE_SEARCH, value, mManagedUser.getIdentifier());
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public final boolean onPreferenceChange(Preference preference, Object newValue) {
|
||||
return setChecked((boolean) newValue);
|
||||
}
|
||||
|
||||
@Override
|
||||
@SliceData.SliceType
|
||||
public int getSliceType() {
|
||||
return SliceData.SliceType.SWITCH;
|
||||
}
|
||||
}
|
@@ -16,8 +16,6 @@
|
||||
|
||||
package com.android.settings.accounts;
|
||||
|
||||
import static android.provider.Settings.Secure.MANAGED_PROFILE_CONTACT_REMOTE_SEARCH;
|
||||
|
||||
import android.content.BroadcastReceiver;
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
@@ -25,67 +23,61 @@ 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.nano.MetricsProto;
|
||||
import com.android.settings.R;
|
||||
import com.android.settings.SettingsPreferenceFragment;
|
||||
import com.android.settings.Utils;
|
||||
import com.android.settingslib.RestrictedLockUtils;
|
||||
import com.android.settingslib.RestrictedSwitchPreference;
|
||||
import com.android.settings.dashboard.DashboardFragment;
|
||||
|
||||
/**
|
||||
* 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;
|
||||
public class ManagedProfileSettings extends DashboardFragment {
|
||||
|
||||
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();
|
||||
protected String getLogTag() {
|
||||
return TAG;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected int getPreferenceScreenResId() {
|
||||
return R.xml.managed_profile_settings;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onAttach(Context context) {
|
||||
super.onAttach(context);
|
||||
mUserManager = (UserManager) getSystemService(Context.USER_SERVICE);
|
||||
mManagedUser = getManagedUserFromArgument();
|
||||
if (mManagedUser == null) {
|
||||
getActivity().finish();
|
||||
}
|
||||
use(WorkModePreferenceController.class).setManagedUser(mManagedUser);
|
||||
use(ContactSearchPreferenceController.class).setManagedUser(mManagedUser);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onCreate(Bundle icicle) {
|
||||
super.onCreate(icicle);
|
||||
mManagedProfileBroadcastReceiver = new ManagedProfileBroadcastReceiver();
|
||||
mManagedProfileBroadcastReceiver.register(getActivity());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onResume() {
|
||||
super.onResume();
|
||||
loadDataAndPopulateUi();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onDestroy() {
|
||||
super.onDestroy();
|
||||
mManagedProfileBroadcastReceiver.unregister(getActivity());
|
||||
if (mManagedProfileBroadcastReceiver != null) {
|
||||
mManagedProfileBroadcastReceiver.unregister(getActivity());
|
||||
}
|
||||
}
|
||||
|
||||
private UserHandle getManagedUserFromArgument() {
|
||||
@@ -102,59 +94,21 @@ public class ManagedProfileSettings extends SettingsPreferenceFragment
|
||||
return Utils.getManagedProfile(mUserManager);
|
||||
}
|
||||
|
||||
private void loadDataAndPopulateUi() {
|
||||
if (mWorkModePreference != null) {
|
||||
updateWorkModePreference();
|
||||
}
|
||||
|
||||
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
|
||||
public int getMetricsCategory() {
|
||||
return MetricsProto.MetricsEvent.ACCOUNTS_WORK_PROFILE_SETTINGS;
|
||||
}
|
||||
|
||||
private void updateWorkModePreference() {
|
||||
boolean isWorkModeOn = !mUserManager.isQuietModeEnabled(mManagedUser);
|
||||
mWorkModePreference.setChecked(isWorkModeOn);
|
||||
mWorkModePreference.setSummary(isWorkModeOn
|
||||
? R.string.work_mode_on_summary
|
||||
: R.string.work_mode_off_summary);
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public boolean onPreferenceChange(Preference preference, Object newValue) {
|
||||
if (preference == mWorkModePreference) {
|
||||
boolean quietModeEnabled = !(boolean) newValue;
|
||||
mUserManager.requestQuietModeEnabled(quietModeEnabled, mManagedUser);
|
||||
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) {
|
||||
if (intent == null) {
|
||||
return;
|
||||
}
|
||||
final String action = intent.getAction();
|
||||
Log.v(TAG, "Received broadcast: " + action);
|
||||
if (action.equals(Intent.ACTION_MANAGED_PROFILE_REMOVED)) {
|
||||
if (Intent.ACTION_MANAGED_PROFILE_REMOVED.equals(action)) {
|
||||
if (intent.getIntExtra(Intent.EXTRA_USER_HANDLE,
|
||||
UserHandle.USER_NULL) == mManagedUser.getIdentifier()) {
|
||||
getActivity().finish();
|
||||
@@ -162,23 +116,12 @@ public class ManagedProfileSettings extends SettingsPreferenceFragment
|
||||
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()) {
|
||||
updateWorkModePreference();
|
||||
}
|
||||
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);
|
||||
}
|
||||
|
||||
@@ -186,5 +129,4 @@ public class ManagedProfileSettings extends SettingsPreferenceFragment
|
||||
context.unregisterReceiver(this);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
@@ -0,0 +1,146 @@
|
||||
/*
|
||||
* Copyright (C) 2018 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.UserHandle;
|
||||
import android.os.UserManager;
|
||||
import android.support.annotation.VisibleForTesting;
|
||||
import android.support.v7.preference.Preference;
|
||||
import android.support.v7.preference.PreferenceScreen;
|
||||
import android.support.v7.preference.TwoStatePreference;
|
||||
import android.util.Log;
|
||||
|
||||
import com.android.settings.R;
|
||||
import com.android.settings.core.BasePreferenceController;
|
||||
import com.android.settings.slices.SliceData;
|
||||
import com.android.settingslib.core.lifecycle.LifecycleObserver;
|
||||
import com.android.settingslib.core.lifecycle.events.OnStart;
|
||||
import com.android.settingslib.core.lifecycle.events.OnStop;
|
||||
|
||||
public class WorkModePreferenceController extends BasePreferenceController implements
|
||||
Preference.OnPreferenceChangeListener, LifecycleObserver, OnStart, OnStop {
|
||||
|
||||
private static final String TAG = "WorkModeController";
|
||||
|
||||
private UserManager mUserManager;
|
||||
private UserHandle mManagedUser;
|
||||
|
||||
private Preference mPreference;
|
||||
private IntentFilter mIntentFilter;
|
||||
|
||||
public WorkModePreferenceController(Context context, String key) {
|
||||
super(context, key);
|
||||
mUserManager = (UserManager) context.getSystemService(Context.USER_SERVICE);
|
||||
mIntentFilter = new IntentFilter();
|
||||
mIntentFilter.addAction(Intent.ACTION_MANAGED_PROFILE_AVAILABLE);
|
||||
mIntentFilter.addAction(Intent.ACTION_MANAGED_PROFILE_UNAVAILABLE);
|
||||
}
|
||||
|
||||
public void setManagedUser(UserHandle managedUser) {
|
||||
mManagedUser = managedUser;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onStart() {
|
||||
mContext.registerReceiver(mReceiver, mIntentFilter);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onStop() {
|
||||
mContext.unregisterReceiver(mReceiver);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getAvailabilityStatus() {
|
||||
return (mManagedUser != null) ? AVAILABLE : DISABLED_FOR_USER;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void displayPreference(PreferenceScreen screen) {
|
||||
super.displayPreference(screen);
|
||||
mPreference = screen.findPreference(getPreferenceKey());
|
||||
}
|
||||
|
||||
@Override
|
||||
public CharSequence getSummary() {
|
||||
return mContext.getText(isChecked()
|
||||
? R.string.work_mode_on_summary
|
||||
: R.string.work_mode_off_summary);
|
||||
}
|
||||
|
||||
private boolean isChecked() {
|
||||
boolean isWorkModeOn = false;
|
||||
if (mUserManager != null && mManagedUser != null) {
|
||||
isWorkModeOn = !mUserManager.isQuietModeEnabled(mManagedUser);
|
||||
}
|
||||
return isWorkModeOn;
|
||||
}
|
||||
|
||||
private boolean setChecked(boolean isChecked) {
|
||||
if (mUserManager != null && mManagedUser != null) {
|
||||
final boolean quietModeEnabled = !isChecked;
|
||||
mUserManager.requestQuietModeEnabled(quietModeEnabled, mManagedUser);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void updateState(Preference preference) {
|
||||
super.updateState(preference);
|
||||
if (preference instanceof TwoStatePreference) {
|
||||
((TwoStatePreference) preference).setChecked(isChecked());
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public final boolean onPreferenceChange(Preference preference, Object newValue) {
|
||||
return setChecked((boolean) newValue);
|
||||
}
|
||||
|
||||
/**
|
||||
* Receiver that listens to {@link Intent#ACTION_MANAGED_PROFILE_AVAILABLE} and
|
||||
* {@link Intent#ACTION_MANAGED_PROFILE_UNAVAILABLE}, and updates the work mode
|
||||
*/
|
||||
@VisibleForTesting
|
||||
final BroadcastReceiver mReceiver = new BroadcastReceiver() {
|
||||
@Override
|
||||
public void onReceive(Context context, Intent intent) {
|
||||
if (intent == null) {
|
||||
return;
|
||||
}
|
||||
final String action = intent.getAction();
|
||||
Log.v(TAG, "Received broadcast: " + action);
|
||||
|
||||
if (Intent.ACTION_MANAGED_PROFILE_AVAILABLE.equals(action)
|
||||
|| Intent.ACTION_MANAGED_PROFILE_UNAVAILABLE.equals(action)) {
|
||||
if (intent.getIntExtra(Intent.EXTRA_USER_HANDLE,
|
||||
UserHandle.USER_NULL) == mManagedUser.getIdentifier()) {
|
||||
updateState(mPreference);
|
||||
}
|
||||
return;
|
||||
}
|
||||
Log.w(TAG, "Cannot handle received broadcast: " + intent.getAction());
|
||||
}
|
||||
};
|
||||
|
||||
@Override
|
||||
@SliceData.SliceType
|
||||
public int getSliceType() {
|
||||
return SliceData.SliceType.SWITCH;
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user