Merge "Introduce AdbPreferenceController"
This commit is contained in:
committed by
Android (Google) Code Review
commit
cfc2ede22f
33
src/com/android/settings/development/AdbDialogHost.java
Normal file
33
src/com/android/settings/development/AdbDialogHost.java
Normal file
@@ -0,0 +1,33 @@
|
||||
/*
|
||||
* 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;
|
||||
|
||||
/**
|
||||
* Interface for EnableAdbWarningDialog callbacks.
|
||||
*/
|
||||
public interface AdbDialogHost {
|
||||
/**
|
||||
* Called when the user presses enable on the warning dialog.
|
||||
*/
|
||||
void onEnableAdbDialogConfirmed();
|
||||
|
||||
/**
|
||||
* Called when the user dismisses or cancels the warning dialog.
|
||||
*/
|
||||
void onEnableAdbDialogDismissed();
|
||||
}
|
@@ -0,0 +1,28 @@
|
||||
/*
|
||||
* 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;
|
||||
|
||||
/**
|
||||
* Interface for callbacks in Adb setting changes.
|
||||
*/
|
||||
public interface AdbOnChangeListener {
|
||||
|
||||
/**
|
||||
* Called when the global Adb setting changes
|
||||
*/
|
||||
void onAdbSettingChanged();
|
||||
}
|
@@ -0,0 +1,116 @@
|
||||
/*
|
||||
* 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.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.os.UserManager;
|
||||
import android.provider.Settings;
|
||||
import android.support.annotation.VisibleForTesting;
|
||||
import android.support.v14.preference.SwitchPreference;
|
||||
import android.support.v4.content.LocalBroadcastManager;
|
||||
import android.support.v7.preference.Preference;
|
||||
import android.support.v7.preference.PreferenceScreen;
|
||||
|
||||
public class AdbPreferenceController extends DeveloperOptionsPreferenceController implements
|
||||
Preference.OnPreferenceChangeListener {
|
||||
|
||||
public static final String ADB_STATE_CHANGED =
|
||||
"com.android.settings.development.AdbPreferenceController.ADB_STATE_CHANGED";
|
||||
public static final int ADB_SETTING_ON = 1;
|
||||
public static final int ADB_SETTING_OFF = 0;
|
||||
|
||||
private static final String KEY_ENABLE_ADB = "enable_adb";
|
||||
|
||||
private final DevelopmentSettingsDashboardFragment mFragment;
|
||||
private SwitchPreference mPreference;
|
||||
|
||||
public AdbPreferenceController(Context context, DevelopmentSettingsDashboardFragment fragment) {
|
||||
super(context);
|
||||
mFragment = fragment;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isAvailable() {
|
||||
return mContext.getSystemService(UserManager.class).isAdminUser();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getPreferenceKey() {
|
||||
return KEY_ENABLE_ADB;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void displayPreference(PreferenceScreen screen) {
|
||||
super.displayPreference(screen);
|
||||
|
||||
mPreference = (SwitchPreference) screen.findPreference(getPreferenceKey());
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean onPreferenceChange(Preference preference, Object newValue) {
|
||||
final boolean isAdbEnabled = (Boolean) newValue;
|
||||
if (isAdbEnabled) {
|
||||
EnableAdbWarningDialog.show(mFragment);
|
||||
} else {
|
||||
writeAdbSetting(isAdbEnabled);
|
||||
notifyStateChanged();
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void updateState(Preference preference) {
|
||||
final int adbMode = Settings.Global.getInt(mContext.getContentResolver(),
|
||||
Settings.Global.ADB_ENABLED, 0 /* default */);
|
||||
mPreference.setChecked(adbMode != ADB_SETTING_OFF);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onDeveloperOptionsSwitchEnabled() {
|
||||
mPreference.setEnabled(true);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onDeveloperOptionsSwitchDisabled() {
|
||||
writeAdbSetting(false);
|
||||
notifyStateChanged();
|
||||
mPreference.setEnabled(false);
|
||||
mPreference.setChecked(false);
|
||||
}
|
||||
|
||||
public void onAdbDialogConfirmed() {
|
||||
writeAdbSetting(true);
|
||||
notifyStateChanged();
|
||||
}
|
||||
|
||||
public void onAdbDialogDismissed() {
|
||||
updateState(mPreference);
|
||||
}
|
||||
|
||||
private void writeAdbSetting(boolean enabled) {
|
||||
Settings.Global.putInt(mContext.getContentResolver(),
|
||||
Settings.Global.ADB_ENABLED, enabled ? ADB_SETTING_ON : ADB_SETTING_OFF);
|
||||
}
|
||||
|
||||
@VisibleForTesting
|
||||
void notifyStateChanged() {
|
||||
LocalBroadcastManager.getInstance(mContext)
|
||||
.sendBroadcast(new Intent(ADB_STATE_CHANGED));
|
||||
}
|
||||
}
|
@@ -17,13 +17,19 @@
|
||||
package com.android.settings.development;
|
||||
|
||||
import android.app.Activity;
|
||||
import android.content.BroadcastReceiver;
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.content.IntentFilter;
|
||||
import android.os.Bundle;
|
||||
import android.os.UserManager;
|
||||
import android.provider.SearchIndexableResource;
|
||||
import android.support.annotation.VisibleForTesting;
|
||||
import android.support.v4.content.LocalBroadcastManager;
|
||||
import android.util.Log;
|
||||
import android.view.LayoutInflater;
|
||||
import android.view.View;
|
||||
import android.view.ViewGroup;
|
||||
import android.widget.Switch;
|
||||
|
||||
import com.android.internal.logging.nano.MetricsProto;
|
||||
@@ -43,7 +49,7 @@ import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
public class DevelopmentSettingsDashboardFragment extends RestrictedDashboardFragment
|
||||
implements SwitchBar.OnSwitchChangeListener, OemUnlockDialogHost {
|
||||
implements SwitchBar.OnSwitchChangeListener, OemUnlockDialogHost, AdbDialogHost {
|
||||
|
||||
private static final String TAG = "DevSettingsDashboard";
|
||||
|
||||
@@ -52,6 +58,17 @@ public class DevelopmentSettingsDashboardFragment extends RestrictedDashboardFra
|
||||
private DevelopmentSwitchBarController mSwitchBarController;
|
||||
private List<AbstractPreferenceController> mPreferenceControllers = new ArrayList<>();
|
||||
|
||||
private final BroadcastReceiver mEnableAdbReceiver = new BroadcastReceiver() {
|
||||
@Override
|
||||
public void onReceive(Context context, Intent intent) {
|
||||
for (AbstractPreferenceController controller : mPreferenceControllers) {
|
||||
if (controller instanceof AdbOnChangeListener) {
|
||||
((AdbOnChangeListener) controller).onAdbSettingChanged();
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
public DevelopmentSettingsDashboardFragment() {
|
||||
super(UserManager.DISALLOW_DEBUGGING_FEATURES);
|
||||
}
|
||||
@@ -79,6 +96,19 @@ public class DevelopmentSettingsDashboardFragment extends RestrictedDashboardFra
|
||||
mSwitchBar.show();
|
||||
}
|
||||
|
||||
@Override
|
||||
public View onCreateView(LayoutInflater inflater, ViewGroup container,
|
||||
Bundle savedInstanceState) {
|
||||
registerReceivers();
|
||||
return super.onCreateView(inflater, container, savedInstanceState);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onDestroyView() {
|
||||
super.onDestroyView();
|
||||
unregisterReceivers();
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getMetricsCategory() {
|
||||
return MetricsProto.MetricsEvent.DEVELOPMENT;
|
||||
@@ -120,6 +150,21 @@ public class DevelopmentSettingsDashboardFragment extends RestrictedDashboardFra
|
||||
controller.onOemUnlockDismissed();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onEnableAdbDialogConfirmed() {
|
||||
final AdbPreferenceController controller = getDevelopmentOptionsController(
|
||||
AdbPreferenceController.class);
|
||||
controller.onAdbDialogConfirmed();
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onEnableAdbDialogDismissed() {
|
||||
final AdbPreferenceController controller = getDevelopmentOptionsController(
|
||||
AdbPreferenceController.class);
|
||||
controller.onAdbDialogDismissed();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onActivityResult(int requestCode, int resultCode, Intent data) {
|
||||
boolean handledResult = false;
|
||||
@@ -160,6 +205,16 @@ public class DevelopmentSettingsDashboardFragment extends RestrictedDashboardFra
|
||||
return mPreferenceControllers;
|
||||
}
|
||||
|
||||
private void registerReceivers() {
|
||||
LocalBroadcastManager.getInstance(getContext())
|
||||
.registerReceiver(mEnableAdbReceiver, new IntentFilter(
|
||||
AdbPreferenceController.ADB_STATE_CHANGED));
|
||||
}
|
||||
|
||||
private void unregisterReceivers() {
|
||||
LocalBroadcastManager.getInstance(getContext()).unregisterReceiver(mEnableAdbReceiver);
|
||||
}
|
||||
|
||||
void onEnableDevelopmentOptionsConfirmed() {
|
||||
DevelopmentSettingsEnabler.setDevelopmentSettingsEnabled(getContext(), true);
|
||||
for (AbstractPreferenceController controller : mPreferenceControllers) {
|
||||
@@ -191,7 +246,7 @@ public class DevelopmentSettingsDashboardFragment extends RestrictedDashboardFra
|
||||
controllers.add(new DisableAutomaticUpdatesPreferenceController(context));
|
||||
// system ui demo mode
|
||||
// quick settings developer tiles
|
||||
// usb debugging
|
||||
controllers.add(new AdbPreferenceController(context, fragment));
|
||||
// revoke usb debugging authorizations
|
||||
controllers.add(new LocalTerminalPreferenceController(context));
|
||||
// bug report shortcut
|
||||
|
@@ -27,6 +27,10 @@ import com.android.settings.R;
|
||||
import com.android.settings.core.PreferenceControllerMixin;
|
||||
import com.android.settingslib.development.AbstractEnableAdbPreferenceController;
|
||||
|
||||
/**
|
||||
* @deprecated in favor of {@link AdbPreferenceController}
|
||||
*/
|
||||
@Deprecated
|
||||
public class EnableAdbPreferenceController extends AbstractEnableAdbPreferenceController
|
||||
implements PreferenceControllerMixin {
|
||||
|
||||
|
@@ -0,0 +1,81 @@
|
||||
/*
|
||||
* 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.Fragment;
|
||||
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 EnableAdbWarningDialog extends InstrumentedDialogFragment implements
|
||||
DialogInterface.OnClickListener, DialogInterface.OnDismissListener {
|
||||
|
||||
public static final String TAG = "EnableAdbDialog";
|
||||
|
||||
public static void show(Fragment host) {
|
||||
final FragmentManager manager = host.getActivity().getFragmentManager();
|
||||
if (manager.findFragmentByTag(TAG) == null) {
|
||||
final EnableAdbWarningDialog dialog = new EnableAdbWarningDialog();
|
||||
dialog.setTargetFragment(host, 0 /* requestCode */);
|
||||
dialog.show(manager, TAG);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getMetricsCategory() {
|
||||
return MetricsProto.MetricsEvent.DIALOG_ENABLE_ADB;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Dialog onCreateDialog(Bundle savedInstanceState) {
|
||||
return new AlertDialog.Builder(getActivity())
|
||||
.setTitle(R.string.adb_warning_title)
|
||||
.setMessage(R.string.adb_warning_message)
|
||||
.setPositiveButton(android.R.string.yes, this /* onClickListener */)
|
||||
.setNegativeButton(android.R.string.no, this /* onClickListener */)
|
||||
.create();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onClick(DialogInterface dialog, int which) {
|
||||
final AdbDialogHost host = (AdbDialogHost) getTargetFragment();
|
||||
if (host == null) {
|
||||
return;
|
||||
}
|
||||
if (which == DialogInterface.BUTTON_POSITIVE) {
|
||||
host.onEnableAdbDialogConfirmed();
|
||||
} else {
|
||||
host.onEnableAdbDialogDismissed();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onDismiss(DialogInterface dialog) {
|
||||
super.onDismiss(dialog);
|
||||
final AdbDialogHost host = (AdbDialogHost) getTargetFragment();
|
||||
if (host == null) {
|
||||
return;
|
||||
}
|
||||
host.onEnableAdbDialogDismissed();
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user