Refactor AdbPreferenceController
- Refactor AdbPreferenceController to use AbstractEnableAdbPreferenceController Bug: 34203528 Test: make RunSettingsRoboTests -j40 Change-Id: If34e2968ff8900276023e12f89d9cc86468adf7a
This commit is contained in:
@@ -18,70 +18,44 @@ 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.annotation.Nullable;
|
||||
import android.support.v7.preference.Preference;
|
||||
import android.support.v7.preference.PreferenceScreen;
|
||||
|
||||
import com.android.settings.core.PreferenceControllerMixin;
|
||||
import com.android.settingslib.development.DeveloperOptionsPreferenceController;
|
||||
import com.android.settingslib.development.AbstractEnableAdbPreferenceController;
|
||||
|
||||
public class AdbPreferenceController extends DeveloperOptionsPreferenceController implements
|
||||
Preference.OnPreferenceChangeListener, PreferenceControllerMixin {
|
||||
|
||||
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";
|
||||
public class AdbPreferenceController extends AbstractEnableAdbPreferenceController implements
|
||||
PreferenceControllerMixin {
|
||||
|
||||
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();
|
||||
public void onAdbDialogConfirmed() {
|
||||
writeAdbSetting(true);
|
||||
}
|
||||
|
||||
public void onAdbDialogDismissed() {
|
||||
updateState(mPreference);
|
||||
}
|
||||
|
||||
@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) {
|
||||
public void showConfirmationDialog(@Nullable Preference preference) {
|
||||
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);
|
||||
public void dismissConfirmationDialog() {
|
||||
// intentional no-op
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isConfirmationDialogShowing() {
|
||||
// intentional no-op
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -92,28 +66,7 @@ public class AdbPreferenceController extends DeveloperOptionsPreferenceControlle
|
||||
@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));
|
||||
}
|
||||
}
|
||||
|
@@ -222,7 +222,7 @@ public class DevelopmentSettingsDashboardFragment extends RestrictedDashboardFra
|
||||
private void registerReceivers() {
|
||||
LocalBroadcastManager.getInstance(getContext())
|
||||
.registerReceiver(mEnableAdbReceiver, new IntentFilter(
|
||||
AdbPreferenceController.ADB_STATE_CHANGED));
|
||||
AdbPreferenceController.ACTION_ENABLE_ADB_STATE_CHANGED));
|
||||
}
|
||||
|
||||
private void unregisterReceivers() {
|
||||
|
@@ -18,12 +18,11 @@ package com.android.settings.development;
|
||||
|
||||
import static com.google.common.truth.Truth.assertThat;
|
||||
|
||||
import static org.mockito.Mockito.doNothing;
|
||||
import static org.mockito.Mockito.doReturn;
|
||||
import static org.mockito.Mockito.spy;
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
import android.content.ContentResolver;
|
||||
import android.content.Context;
|
||||
import android.os.UserManager;
|
||||
import android.provider.Settings;
|
||||
@@ -44,8 +43,7 @@ import org.robolectric.annotation.Config;
|
||||
@RunWith(SettingsRobolectricTestRunner.class)
|
||||
@Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION)
|
||||
public class AdbPreferenceControllerTest {
|
||||
@Mock
|
||||
private Context mContext;
|
||||
|
||||
@Mock
|
||||
private SwitchPreference mPreference;
|
||||
@Mock
|
||||
@@ -55,71 +53,24 @@ public class AdbPreferenceControllerTest {
|
||||
@Mock
|
||||
private DevelopmentSettingsDashboardFragment mFragment;
|
||||
|
||||
private ContentResolver mContentResolver;
|
||||
private Context mContext;
|
||||
private AdbPreferenceController mController;
|
||||
|
||||
@Before
|
||||
public void setup() {
|
||||
MockitoAnnotations.initMocks(this);
|
||||
mContentResolver = RuntimeEnvironment.application.getContentResolver();
|
||||
mContext = RuntimeEnvironment.application;
|
||||
mController = spy(new AdbPreferenceController(mContext, mFragment));
|
||||
doNothing().when(mController).notifyStateChanged();
|
||||
when(mContext.getSystemService(UserManager.class)).thenReturn(mUserManager);
|
||||
when(mContext.getContentResolver()).thenReturn(mContentResolver);
|
||||
doReturn(true).when(mController).isAvailable();
|
||||
when(mPreferenceScreen.findPreference(mController.getPreferenceKey())).thenReturn(
|
||||
mPreference);
|
||||
mController.displayPreference(mPreferenceScreen);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isAvailable_notAdmin_shouldBeFalse() {
|
||||
when(mUserManager.isAdminUser()).thenReturn(false);
|
||||
|
||||
assertThat(mController.isAvailable()).isFalse();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isAvailable_isAdmin_shouldBeTrue() {
|
||||
when(mUserManager.isAdminUser()).thenReturn(true);
|
||||
|
||||
assertThat(mController.isAvailable()).isTrue();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void onPreferenceChanged_settingDisabled_shouldTurnOffAdb() {
|
||||
when(mContext.getApplicationContext()).thenReturn(RuntimeEnvironment.application);
|
||||
mController.onPreferenceChange(null, false);
|
||||
|
||||
final int mode = Settings.System.getInt(mContentResolver,
|
||||
Settings.Global.ADB_ENABLED, -1);
|
||||
|
||||
assertThat(mode).isEqualTo(AdbPreferenceController.ADB_SETTING_OFF);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void updateState_settingEnabled_preferenceShouldBeChecked() {
|
||||
Settings.System.putInt(mContentResolver, Settings.Global.ADB_ENABLED,
|
||||
AdbPreferenceController.ADB_SETTING_ON);
|
||||
mController.updateState(mPreference);
|
||||
|
||||
verify(mPreference).setChecked(true);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void updateState_settingDisabled_preferenceShouldNotBeChecked() {
|
||||
Settings.System.putInt(mContentResolver, Settings.Global.ADB_ENABLED,
|
||||
AdbPreferenceController.ADB_SETTING_OFF);
|
||||
mController.updateState(mPreference);
|
||||
|
||||
verify(mPreference).setChecked(false);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void onDeveloperOptionsDisabled_shouldDisablePreference() {
|
||||
when(mContext.getApplicationContext()).thenReturn(RuntimeEnvironment.application);
|
||||
when(mUserManager.isAdminUser()).thenReturn(true);
|
||||
mController.onDeveloperOptionsDisabled();
|
||||
final int mode = Settings.System.getInt(mContentResolver,
|
||||
final int mode = Settings.System.getInt(mContext.getContentResolver(),
|
||||
Settings.Global.ADB_ENABLED, -1);
|
||||
|
||||
assertThat(mode).isEqualTo(AdbPreferenceController.ADB_SETTING_OFF);
|
||||
@@ -138,7 +89,7 @@ public class AdbPreferenceControllerTest {
|
||||
@Test
|
||||
public void onAdbDialogConfirmed_shouldEnableAdbSetting() {
|
||||
mController.onAdbDialogConfirmed();
|
||||
final int mode = Settings.System.getInt(mContentResolver,
|
||||
final int mode = Settings.System.getInt(mContext.getContentResolver(),
|
||||
Settings.Global.ADB_ENABLED, -1);
|
||||
|
||||
assertThat(mode).isEqualTo(AdbPreferenceController.ADB_SETTING_ON);
|
||||
@@ -146,7 +97,7 @@ public class AdbPreferenceControllerTest {
|
||||
|
||||
@Test
|
||||
public void onAdbDialogDismissed_preferenceShouldNotBeChecked() {
|
||||
Settings.System.putInt(mContentResolver, Settings.Global.ADB_ENABLED,
|
||||
Settings.System.putInt(mContext.getContentResolver(), Settings.Global.ADB_ENABLED,
|
||||
AdbPreferenceController.ADB_SETTING_OFF);
|
||||
mController.onAdbDialogDismissed();
|
||||
|
||||
|
@@ -34,6 +34,7 @@ import com.android.settings.testutils.SettingsRobolectricTestRunner;
|
||||
import com.android.settings.testutils.shadow.SettingsShadowResources;
|
||||
import com.android.settings.widget.SwitchBar;
|
||||
import com.android.settings.widget.ToggleSwitch;
|
||||
import com.android.settingslib.development.AbstractEnableAdbPreferenceController;
|
||||
import com.android.settingslib.development.DevelopmentSettingsEnabler;
|
||||
|
||||
import org.junit.After;
|
||||
@@ -243,7 +244,7 @@ public class DevelopmentSettingsDashboardFragmentTest {
|
||||
}
|
||||
}
|
||||
|
||||
@Implements(AdbPreferenceController.class)
|
||||
@Implements(AbstractEnableAdbPreferenceController.class)
|
||||
public static class ShadowAdbPreferenceController {
|
||||
@Implementation
|
||||
public boolean isAvailable() {
|
||||
|
Reference in New Issue
Block a user