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