Use EmergencyNumberUtils to get/set emergency settings.
EmergencyNumberUtils now contains centralized logic for interacting with emergency gesture settings. Settings UI does not need to pay attention to underlying data access, permission controler, etc etc. Bug: 180236600 Test: robotests Change-Id: If641ee36f237d153f1d790251af408969379a57a
This commit is contained in:
@@ -17,7 +17,6 @@
|
||||
package com.android.settings.emergency;
|
||||
|
||||
import android.content.Context;
|
||||
import android.provider.Settings;
|
||||
import android.widget.Switch;
|
||||
|
||||
import androidx.annotation.VisibleForTesting;
|
||||
@@ -26,6 +25,7 @@ import androidx.preference.PreferenceScreen;
|
||||
|
||||
import com.android.settings.R;
|
||||
import com.android.settings.core.BasePreferenceController;
|
||||
import com.android.settingslib.emergencynumber.EmergencyNumberUtils;
|
||||
import com.android.settingslib.widget.MainSwitchPreference;
|
||||
import com.android.settingslib.widget.OnMainSwitchChangeListener;
|
||||
|
||||
@@ -36,16 +36,13 @@ public class EmergencyGesturePreferenceController extends BasePreferenceControll
|
||||
OnMainSwitchChangeListener {
|
||||
|
||||
@VisibleForTesting
|
||||
static final int ON = 1;
|
||||
@VisibleForTesting
|
||||
static final int OFF = 0;
|
||||
|
||||
private static final String SECURE_KEY = Settings.Secure.EMERGENCY_GESTURE_ENABLED;
|
||||
EmergencyNumberUtils mEmergencyNumberUtils;
|
||||
|
||||
private MainSwitchPreference mSwitchBar;
|
||||
|
||||
public EmergencyGesturePreferenceController(Context context, String key) {
|
||||
super(context, key);
|
||||
mEmergencyNumberUtils = new EmergencyNumberUtils(context);
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -71,11 +68,11 @@ public class EmergencyGesturePreferenceController extends BasePreferenceControll
|
||||
|
||||
@VisibleForTesting
|
||||
public boolean isChecked() {
|
||||
return Settings.Secure.getInt(mContext.getContentResolver(), SECURE_KEY, ON) == ON;
|
||||
return mEmergencyNumberUtils.getEmergencyGestureEnabled();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onSwitchChanged(Switch switchView, boolean isChecked) {
|
||||
Settings.Secure.putInt(mContext.getContentResolver(), SECURE_KEY, isChecked ? ON : OFF);
|
||||
mEmergencyNumberUtils.setEmergencyGestureEnabled(isChecked);
|
||||
}
|
||||
}
|
||||
|
@@ -17,12 +17,12 @@
|
||||
package com.android.settings.emergency;
|
||||
|
||||
import android.content.Context;
|
||||
import android.provider.Settings;
|
||||
|
||||
import androidx.annotation.VisibleForTesting;
|
||||
|
||||
import com.android.settings.R;
|
||||
import com.android.settings.core.TogglePreferenceController;
|
||||
import com.android.settingslib.emergencynumber.EmergencyNumberUtils;
|
||||
|
||||
/**
|
||||
* Preference controller for emergency sos gesture setting
|
||||
@@ -30,14 +30,11 @@ import com.android.settings.core.TogglePreferenceController;
|
||||
public class EmergencyGestureSoundPreferenceController extends TogglePreferenceController {
|
||||
|
||||
@VisibleForTesting
|
||||
static final int ON = 1;
|
||||
@VisibleForTesting
|
||||
static final int OFF = 0;
|
||||
|
||||
private static final String SECURE_KEY = Settings.Secure.EMERGENCY_GESTURE_SOUND_ENABLED;
|
||||
EmergencyNumberUtils mEmergencyNumberUtils;
|
||||
|
||||
public EmergencyGestureSoundPreferenceController(Context context, String key) {
|
||||
super(context, key);
|
||||
mEmergencyNumberUtils = new EmergencyNumberUtils(context);
|
||||
}
|
||||
|
||||
private static boolean isGestureAvailable(Context context) {
|
||||
@@ -57,12 +54,12 @@ public class EmergencyGestureSoundPreferenceController extends TogglePreferenceC
|
||||
|
||||
@Override
|
||||
public boolean isChecked() {
|
||||
return Settings.Secure.getInt(mContext.getContentResolver(), SECURE_KEY, ON) == ON;
|
||||
return mEmergencyNumberUtils.getEmergencyGestureSoundEnabled();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean setChecked(boolean isChecked) {
|
||||
return Settings.Secure.putInt(mContext.getContentResolver(), SECURE_KEY,
|
||||
isChecked ? ON : OFF);
|
||||
mEmergencyNumberUtils.setEmergencySoundEnabled(isChecked);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
@@ -18,24 +18,25 @@ package com.android.settings.emergency;
|
||||
|
||||
import static com.android.settings.core.BasePreferenceController.AVAILABLE;
|
||||
import static com.android.settings.core.BasePreferenceController.UNSUPPORTED_ON_DEVICE;
|
||||
import static com.android.settings.emergency.EmergencyGesturePreferenceController.OFF;
|
||||
import static com.android.settings.emergency.EmergencyGesturePreferenceController.ON;
|
||||
|
||||
import static com.google.common.truth.Truth.assertThat;
|
||||
|
||||
import android.content.ContentResolver;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
import android.content.Context;
|
||||
import android.provider.Settings;
|
||||
|
||||
import androidx.test.core.app.ApplicationProvider;
|
||||
|
||||
import com.android.settings.R;
|
||||
import com.android.settings.testutils.shadow.SettingsShadowResources;
|
||||
import com.android.settingslib.emergencynumber.EmergencyNumberUtils;
|
||||
|
||||
import org.junit.After;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.MockitoAnnotations;
|
||||
import org.robolectric.RobolectricTestRunner;
|
||||
import org.robolectric.annotation.Config;
|
||||
|
||||
@@ -43,16 +44,18 @@ import org.robolectric.annotation.Config;
|
||||
@Config(shadows = SettingsShadowResources.class)
|
||||
public class EmergencyGesturePreferenceControllerTest {
|
||||
|
||||
@Mock
|
||||
private EmergencyNumberUtils mEmergencyNumberUtils;
|
||||
private Context mContext;
|
||||
private ContentResolver mContentResolver;
|
||||
private EmergencyGesturePreferenceController mController;
|
||||
private static final String PREF_KEY = "gesture_emergency_button";
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
MockitoAnnotations.initMocks(this);
|
||||
mContext = ApplicationProvider.getApplicationContext();
|
||||
mContentResolver = mContext.getContentResolver();
|
||||
mController = new EmergencyGesturePreferenceController(mContext, PREF_KEY);
|
||||
mController.mEmergencyNumberUtils = mEmergencyNumberUtils;
|
||||
}
|
||||
|
||||
@After
|
||||
@@ -81,8 +84,7 @@ public class EmergencyGesturePreferenceControllerTest {
|
||||
@Test
|
||||
public void isChecked_configIsNotSet_shouldReturnTrue() {
|
||||
// Set the setting to be enabled.
|
||||
Settings.Secure.putInt(mContentResolver, Settings.Secure.EMERGENCY_GESTURE_ENABLED, ON);
|
||||
mController = new EmergencyGesturePreferenceController(mContext, PREF_KEY);
|
||||
when(mEmergencyNumberUtils.getEmergencyGestureEnabled()).thenReturn(true);
|
||||
|
||||
assertThat(mController.isChecked()).isTrue();
|
||||
}
|
||||
@@ -90,8 +92,7 @@ public class EmergencyGesturePreferenceControllerTest {
|
||||
@Test
|
||||
public void isChecked_configIsSet_shouldReturnFalse() {
|
||||
// Set the setting to be disabled.
|
||||
Settings.Secure.putInt(mContentResolver, Settings.Secure.EMERGENCY_GESTURE_ENABLED, OFF);
|
||||
mController = new EmergencyGesturePreferenceController(mContext, PREF_KEY);
|
||||
when(mEmergencyNumberUtils.getEmergencyGestureEnabled()).thenReturn(false);
|
||||
|
||||
assertThat(mController.isChecked()).isFalse();
|
||||
}
|
||||
|
@@ -16,40 +16,42 @@
|
||||
|
||||
package com.android.settings.emergency;
|
||||
|
||||
import static com.android.settings.emergency.EmergencyGestureSoundPreferenceController.OFF;
|
||||
import static com.android.settings.emergency.EmergencyGestureSoundPreferenceController.ON;
|
||||
|
||||
import static com.google.common.truth.Truth.assertThat;
|
||||
|
||||
import android.content.ContentResolver;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
import android.content.Context;
|
||||
import android.provider.Settings;
|
||||
|
||||
import androidx.test.core.app.ApplicationProvider;
|
||||
|
||||
import com.android.settings.R;
|
||||
import com.android.settings.testutils.shadow.SettingsShadowResources;
|
||||
import com.android.settingslib.emergencynumber.EmergencyNumberUtils;
|
||||
|
||||
import org.junit.After;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.MockitoAnnotations;
|
||||
import org.robolectric.RobolectricTestRunner;
|
||||
import org.robolectric.annotation.Config;
|
||||
|
||||
@RunWith(RobolectricTestRunner.class)
|
||||
@Config(shadows = SettingsShadowResources.class)
|
||||
public class EmergencyGestureSoundPreferenceControllerTest {
|
||||
|
||||
@Mock
|
||||
private EmergencyNumberUtils mEmergencyNumberUtils;
|
||||
private Context mContext;
|
||||
private ContentResolver mContentResolver;
|
||||
private EmergencyGestureSoundPreferenceController mController;
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
MockitoAnnotations.initMocks(this);
|
||||
mContext = ApplicationProvider.getApplicationContext();
|
||||
mContentResolver = mContext.getContentResolver();
|
||||
mController = new EmergencyGestureSoundPreferenceController(mContext, "test_key");
|
||||
mController.mEmergencyNumberUtils = mEmergencyNumberUtils;
|
||||
}
|
||||
|
||||
@After
|
||||
@@ -78,8 +80,7 @@ public class EmergencyGestureSoundPreferenceControllerTest {
|
||||
@Test
|
||||
public void isChecked_configIsSet_shouldReturnTrue() {
|
||||
// Set the setting to be enabled.
|
||||
Settings.Secure.putInt(mContentResolver, Settings.Secure.EMERGENCY_GESTURE_SOUND_ENABLED,
|
||||
ON);
|
||||
when(mEmergencyNumberUtils.getEmergencyGestureSoundEnabled()).thenReturn(true);
|
||||
|
||||
assertThat(mController.isChecked()).isTrue();
|
||||
}
|
||||
@@ -87,8 +88,7 @@ public class EmergencyGestureSoundPreferenceControllerTest {
|
||||
@Test
|
||||
public void isChecked_configIsSetToFalse_shouldReturnFalse() {
|
||||
// Set the setting to be disabled.
|
||||
Settings.Secure.putInt(mContentResolver, Settings.Secure.EMERGENCY_GESTURE_SOUND_ENABLED,
|
||||
OFF);
|
||||
when(mEmergencyNumberUtils.getEmergencyGestureSoundEnabled()).thenReturn(false);
|
||||
|
||||
assertThat(mController.isChecked()).isFalse();
|
||||
}
|
||||
|
Reference in New Issue
Block a user