Add switch bar to enable/disable dev settings in new page.

Bug: 65522852
Test: make RunSettingsRoboTests -j40 ROBOTEST_FILTER=Development
Change-Id: I0958950dc6aaee24d8d5e0be58d7564d108bc72e
This commit is contained in:
Fan Zhang
2017-09-08 15:29:54 -07:00
parent c939be6551
commit d77881f88d
5 changed files with 283 additions and 4 deletions

View File

@@ -17,12 +17,16 @@
package com.android.settings.development;
import android.content.Context;
import android.os.Bundle;
import android.os.UserManager;
import android.provider.SearchIndexableResource;
import android.util.Log;
import android.widget.Switch;
import com.android.internal.logging.nano.MetricsProto;
import com.android.settings.R;
import com.android.settings.SettingsActivity;
import com.android.settings.Utils;
import com.android.settings.dashboard.RestrictedDashboardFragment;
import com.android.settings.search.BaseSearchIndexProvider;
import com.android.settings.search.Indexable;
@@ -33,21 +37,67 @@ import com.android.settingslib.development.DevelopmentSettingsEnabler;
import java.util.Arrays;
import java.util.List;
public class DevelopmentSettingsDashboardFragment extends RestrictedDashboardFragment {
public class DevelopmentSettingsDashboardFragment extends RestrictedDashboardFragment
implements SwitchBar.OnSwitchChangeListener {
private static final String TAG = "DevSettingsDashboard";
private boolean mIsAvailable = true;
private SwitchBar mSwitchBar;
private DevelopmentSwitchBarController mSwitchBarController;
public DevelopmentSettingsDashboardFragment() {
super(UserManager.DISALLOW_DEBUGGING_FEATURES);
}
@Override
public void onActivityCreated(Bundle icicle) {
super.onActivityCreated(icicle);
// Apply page-level restrictions
setIfOnlyAvailableForAdmins(true);
if (isUiRestricted() || !Utils.isDeviceProvisioned(getActivity())) {
// Block access to developer options if the user is not the owner, if user policy
// restricts it, or if the device has not been provisioned
mIsAvailable = false;
// Show error message
if (!isUiRestrictedByOnlyAdmin()) {
getEmptyTextView().setText(R.string.development_settings_not_available);
}
getPreferenceScreen().removeAll();
return;
}
// Set up master switch
mSwitchBar = ((SettingsActivity) getActivity()).getSwitchBar();
mSwitchBarController = new DevelopmentSwitchBarController(
this /* DevelopmentSettings */, mSwitchBar, mIsAvailable, getLifecycle());
mSwitchBar.show();
}
@Override
public int getMetricsCategory() {
return MetricsProto.MetricsEvent.DEVELOPMENT;
}
@Override
public void onSwitchChanged(Switch switchView, boolean isChecked) {
if (switchView != mSwitchBar.getSwitch()) {
return;
}
final boolean developmentEnabledState =
DevelopmentSettingsEnabler.isDevelopmentSettingsEnabled(getContext());
if (isChecked != developmentEnabledState) {
if (isChecked) {
EnableDevelopmentSettingWarningDialog.show(this /* host */);
} else {
// TODO: Reset dangerous options (move logic from DevelopmentSettings).
// resetDangerousOptions();
DevelopmentSettingsEnabler.setDevelopmentSettingsEnabled(getContext(), false);
// TODO: Refresh all prefs' enabled state (move logic from DevelopmentSettings).
// setPrefsEnabledState(false);
}
}
}
@Override
protected String getLogTag() {
return TAG;
@@ -69,6 +119,16 @@ public class DevelopmentSettingsDashboardFragment extends RestrictedDashboardFra
return buildPreferenceControllers(context);
}
void onEnableDevelopmentOptionsConfirmed() {
DevelopmentSettingsEnabler.setDevelopmentSettingsEnabled(getContext(), true);
// TODO: Refresh all prefs' enabled state (move logic from DevelopmentSettings).
}
void onEnableDevelopmentOptionsRejected() {
// Reset the toggle
mSwitchBar.setChecked(false);
}
private static List<AbstractPreferenceController> buildPreferenceControllers(Context context) {
return null;
}

View File

@@ -22,18 +22,39 @@ import com.android.settingslib.core.lifecycle.Lifecycle;
import com.android.settingslib.core.lifecycle.LifecycleObserver;
import com.android.settingslib.core.lifecycle.events.OnStart;
import com.android.settingslib.core.lifecycle.events.OnStop;
import com.android.settingslib.development.DevelopmentSettingsEnabler;
public class DevelopmentSwitchBarController implements LifecycleObserver, OnStart, OnStop {
private final SwitchBar mSwitchBar;
private final boolean mIsAvailable;
private final DevelopmentSettings mSettings;
private final DevelopmentSettingsDashboardFragment mNewSettings;
/**
* @deprecated in favor of the other constructor.
*/
@Deprecated
public DevelopmentSwitchBarController(DevelopmentSettings settings, SwitchBar switchBar,
boolean isAvailable, Lifecycle lifecycle) {
mSwitchBar = switchBar;
mIsAvailable = isAvailable && !Utils.isMonkeyRunning();
mSettings = settings;
mNewSettings = null;
if (mIsAvailable) {
lifecycle.addObserver(this);
} else {
mSwitchBar.setEnabled(false);
}
}
public DevelopmentSwitchBarController(DevelopmentSettingsDashboardFragment settings,
SwitchBar switchBar, boolean isAvailable, Lifecycle lifecycle) {
mSwitchBar = switchBar;
mIsAvailable = isAvailable && !Utils.isMonkeyRunning();
mSettings = null;
mNewSettings = settings;
if (mIsAvailable) {
lifecycle.addObserver(this);
@@ -44,11 +65,24 @@ public class DevelopmentSwitchBarController implements LifecycleObserver, OnStar
@Override
public void onStart() {
mSwitchBar.addOnSwitchChangeListener(mSettings);
if (mSettings != null) {
mSwitchBar.addOnSwitchChangeListener(mSettings);
}
if (mNewSettings != null) {
final boolean developmentEnabledState = DevelopmentSettingsEnabler
.isDevelopmentSettingsEnabled(mNewSettings.getContext());
mSwitchBar.setChecked(developmentEnabledState);
mSwitchBar.addOnSwitchChangeListener(mNewSettings);
}
}
@Override
public void onStop() {
mSwitchBar.removeOnSwitchChangeListener(mSettings);
if (mSettings != null) {
mSwitchBar.removeOnSwitchChangeListener(mSettings);
}
if (mNewSettings != null) {
mSwitchBar.removeOnSwitchChangeListener(mNewSettings);
}
}
}

View File

@@ -0,0 +1,70 @@
/*
* Copyright (C) 2017 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.development;
import android.app.AlertDialog;
import android.app.Dialog;
import android.app.FragmentManager;
import android.content.DialogInterface;
import android.os.Bundle;
import com.android.internal.logging.nano.MetricsProto;
import com.android.settings.R;
import com.android.settings.core.instrumentation.InstrumentedDialogFragment;
public class EnableDevelopmentSettingWarningDialog extends InstrumentedDialogFragment
implements DialogInterface.OnClickListener {
public static final String TAG = "EnableDevSettingDlg";
public static void show(
DevelopmentSettingsDashboardFragment host) {
final EnableDevelopmentSettingWarningDialog dialog =
new EnableDevelopmentSettingWarningDialog();
dialog.setTargetFragment(host, 0 /* requestCode */);
final FragmentManager manager = host.getActivity().getFragmentManager();
if (manager.findFragmentByTag(TAG) == null) {
dialog.show(manager, TAG);
}
}
@Override
public int getMetricsCategory() {
return MetricsProto.MetricsEvent.DIALOG_ENABLE_DEVELOPMENT_OPTIONS;
}
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
return new AlertDialog.Builder(getActivity())
.setMessage(R.string.dev_settings_warning_message)
.setTitle(R.string.dev_settings_warning_title)
.setPositiveButton(android.R.string.yes, this)
.setNegativeButton(android.R.string.no, this)
.create();
}
@Override
public void onClick(DialogInterface dialog, int which) {
final DevelopmentSettingsDashboardFragment host =
(DevelopmentSettingsDashboardFragment) getTargetFragment();
if (which == DialogInterface.BUTTON_POSITIVE) {
host.onEnableDevelopmentOptionsConfirmed();
} else {
host.onEnableDevelopmentOptionsRejected();
}
}
}