Merge "Remove location_modes_previous references"

This commit is contained in:
Maggie Wang
2018-01-24 01:47:54 +00:00
committed by Android (Google) Code Review
6 changed files with 73 additions and 40 deletions

View File

@@ -34,8 +34,10 @@ import com.android.settingslib.core.lifecycle.events.OnPause;
import com.android.settingslib.core.lifecycle.events.OnResume; import com.android.settingslib.core.lifecycle.events.OnResume;
import static com.android.settingslib.Utils.updateLocationMode; import static com.android.settingslib.Utils.updateLocationMode;
import static com.android.settingslib.Utils.updateLocationEnabled;
import static com.android.settingslib.RestrictedLockUtils.checkIfRestrictionEnforced; import static com.android.settingslib.RestrictedLockUtils.checkIfRestrictionEnforced;
/** /**
* A class that listens to location settings change and modifies location settings * A class that listens to location settings change and modifies location settings
* settings. * settings.
@@ -106,6 +108,25 @@ public class LocationEnabler implements LifecycleObserver, OnResume, OnPause {
} }
} }
void setLocationEnabled(boolean enabled) {
final int currentMode = Settings.Secure.getInt(mContext.getContentResolver(),
Settings.Secure.LOCATION_MODE, Settings.Secure.LOCATION_MODE_OFF);
if (isRestricted()) {
// Location toggling disabled by user restriction. Read the current location mode to
// update the location master switch.
if (Log.isLoggable(TAG, Log.INFO)) {
Log.i(TAG, "Restricted user, not setting location mode");
}
if (mListener != null) {
mListener.onLocationModeChanged(currentMode, true);
}
return;
}
updateLocationEnabled(mContext, enabled, UserHandle.myUserId());
refreshLocationMode();
}
void setLocationMode(int mode) { void setLocationMode(int mode) {
final int currentMode = Settings.Secure.getInt(mContext.getContentResolver(), final int currentMode = Settings.Secure.getInt(mContext.getContentResolver(),
Settings.Secure.LOCATION_MODE, Settings.Secure.LOCATION_MODE_OFF); Settings.Secure.LOCATION_MODE, Settings.Secure.LOCATION_MODE_OFF);

View File

@@ -47,15 +47,8 @@ import java.util.List;
* <ul> * <ul>
* <li>Platform location controls</li> * <li>Platform location controls</li>
* <ul> * <ul>
* <li>In switch bar: location master switch. Used to toggle * <li>In switch bar: location master switch. Used to toggle location on and off.
* {@link android.provider.Settings.Secure#LOCATION_MODE} between
* {@link android.provider.Settings.Secure#LOCATION_MODE_OFF} and another location mode.
* </li> * </li>
* <li>Mode preference: only available if the master switch is on, selects between
* {@link android.provider.Settings.Secure#LOCATION_MODE} of
* {@link android.provider.Settings.Secure#LOCATION_MODE_HIGH_ACCURACY},
* {@link android.provider.Settings.Secure#LOCATION_MODE_BATTERY_SAVING}, or
* {@link android.provider.Settings.Secure#LOCATION_MODE_SENSORS_ONLY}.</li>
* </ul> * </ul>
* <li>Recent location requests: automatically populated by {@link RecentLocationApps}</li> * <li>Recent location requests: automatically populated by {@link RecentLocationApps}</li>
* <li>Location services: multi-app settings provided from outside the Android framework. Each * <li>Location services: multi-app settings provided from outside the Android framework. Each

View File

@@ -96,9 +96,6 @@ public class LocationSwitchBarController implements SwitchBar.OnSwitchChangeList
*/ */
@Override @Override
public void onSwitchChanged(Switch switchView, boolean isChecked) { public void onSwitchChanged(Switch switchView, boolean isChecked) {
mLocationEnabler.setLocationMode(isChecked mLocationEnabler.setLocationEnabled(isChecked);
? android.provider.Settings.Secure.LOCATION_MODE_PREVIOUS
: android.provider.Settings.Secure.LOCATION_MODE_OFF);
} }
} }

View File

@@ -16,6 +16,7 @@
package com.android.settings.widget; package com.android.settings.widget;
import android.app.ActivityManager;
import android.app.PendingIntent; import android.app.PendingIntent;
import android.appwidget.AppWidgetManager; import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider; import android.appwidget.AppWidgetProvider;
@@ -33,10 +34,12 @@ import android.os.AsyncTask;
import android.os.Handler; import android.os.Handler;
import android.os.IPowerManager; import android.os.IPowerManager;
import android.os.PowerManager; import android.os.PowerManager;
import android.os.Process;
import android.os.RemoteException; import android.os.RemoteException;
import android.os.ServiceManager; import android.os.ServiceManager;
import android.os.UserManager; import android.os.UserManager;
import android.provider.Settings; import android.provider.Settings;
import android.provider.Settings.Secure;
import android.util.Log; import android.util.Log;
import android.widget.RemoteViews; import android.widget.RemoteViews;
@@ -561,27 +564,14 @@ public class SettingsAppWidgetProvider extends AppWidgetProvider {
final UserManager um = final UserManager um =
(UserManager) context.getSystemService(Context.USER_SERVICE); (UserManager) context.getSystemService(Context.USER_SERVICE);
if (!um.hasUserRestriction(UserManager.DISALLOW_SHARE_LOCATION)) { if (!um.hasUserRestriction(UserManager.DISALLOW_SHARE_LOCATION)) {
int currentMode = Settings.Secure.getInt(resolver, LocationManager lm =
Settings.Secure.LOCATION_MODE, Settings.Secure.LOCATION_MODE_OFF); (LocationManager) context.getSystemService(
int mode = Settings.Secure.LOCATION_MODE_HIGH_ACCURACY; Context.LOCATION_SERVICE);
switch (currentMode) { boolean currentLocationEnabled = lm.isLocationEnabled();
case Settings.Secure.LOCATION_MODE_HIGH_ACCURACY: lm.setLocationEnabledForUser(
mode = Settings.Secure.LOCATION_MODE_BATTERY_SAVING; !currentLocationEnabled, Process.myUserHandle());
break; return lm.isLocationEnabled();
case Settings.Secure.LOCATION_MODE_BATTERY_SAVING:
mode = Settings.Secure.LOCATION_MODE_HIGH_ACCURACY;
break;
case Settings.Secure.LOCATION_MODE_SENSORS_ONLY:
mode = Settings.Secure.LOCATION_MODE_OFF;
break;
case Settings.Secure.LOCATION_MODE_OFF:
mode = Settings.Secure.LOCATION_MODE_PREVIOUS;
break;
}
Settings.Secure.putInt(resolver, Settings.Secure.LOCATION_MODE, mode);
return mode != Settings.Secure.LOCATION_MODE_OFF;
} }
return getActualState(context) == STATE_ENABLED; return getActualState(context) == STATE_ENABLED;
} }

View File

@@ -43,6 +43,7 @@ import com.android.settings.TestConfig;
import com.android.settings.testutils.SettingsRobolectricTestRunner; import com.android.settings.testutils.SettingsRobolectricTestRunner;
import com.android.settings.testutils.shadow.ShadowSecureSettings; import com.android.settings.testutils.shadow.ShadowSecureSettings;
import com.android.settingslib.core.lifecycle.Lifecycle; import com.android.settingslib.core.lifecycle.Lifecycle;
import com.android.settingslib.wrapper.LocationManagerWrapper;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import org.junit.Before; import org.junit.Before;
@@ -53,11 +54,15 @@ import org.mockito.Mock;
import org.mockito.MockitoAnnotations; import org.mockito.MockitoAnnotations;
import org.robolectric.RuntimeEnvironment; import org.robolectric.RuntimeEnvironment;
import org.robolectric.annotation.Config; import org.robolectric.annotation.Config;
import org.robolectric.annotation.Implementation;
import org.robolectric.annotation.Implements;
@RunWith(SettingsRobolectricTestRunner.class) @RunWith(SettingsRobolectricTestRunner.class)
@Config(manifest = TestConfig.MANIFEST_PATH, @Config(manifest = TestConfig.MANIFEST_PATH,
sdk = TestConfig.SDK_VERSION, sdk = TestConfig.SDK_VERSION,
shadows = {ShadowSecureSettings.class}) shadows = {
ShadowSecureSettings.class,
LocationEnablerTest.ShadowLocationManagerWrapper.class})
public class LocationEnablerTest { public class LocationEnablerTest {
@Mock @Mock
@@ -124,7 +129,7 @@ public class LocationEnablerTest {
} }
@Test @Test
public void isEnabled_locationONotRestricted_shouldReturnTrue() { public void isEnabled_locationNotRestricted_shouldReturnTrue() {
when(mUserManager.hasUserRestriction(anyString())).thenReturn(false); when(mUserManager.hasUserRestriction(anyString())).thenReturn(false);
assertThat(mEnabler.isEnabled(Settings.Secure.LOCATION_MODE_BATTERY_SAVING)).isTrue(); assertThat(mEnabler.isEnabled(Settings.Secure.LOCATION_MODE_BATTERY_SAVING)).isTrue();
@@ -178,14 +183,35 @@ public class LocationEnablerTest {
when(mUserManager.hasUserRestriction(anyString())).thenReturn(false); when(mUserManager.hasUserRestriction(anyString())).thenReturn(false);
Settings.Secure.putInt(mContext.getContentResolver(), Settings.Secure.putInt(mContext.getContentResolver(),
Settings.Secure.LOCATION_MODE, Settings.Secure.LOCATION_MODE_BATTERY_SAVING); Settings.Secure.LOCATION_MODE, Settings.Secure.LOCATION_MODE_BATTERY_SAVING);
mEnabler.setLocationMode(Settings.Secure.LOCATION_MODE_HIGH_ACCURACY); mEnabler.setLocationMode(Settings.Secure.LOCATION_MODE_HIGH_ACCURACY);
verify(mContext).sendBroadcastAsUser( verify(mContext).sendBroadcastAsUser(
argThat(actionMatches(LocationManager.MODE_CHANGING_ACTION)), argThat(actionMatches(LocationManager.MODE_CHANGING_ACTION)),
eq(UserHandle.of(ActivityManager.getCurrentUser())), eq(UserHandle.of(ActivityManager.getCurrentUser())),
eq(WRITE_SECURE_SETTINGS)); eq(WRITE_SECURE_SETTINGS));
}
@Test
public void setLocationEnabled_notRestricted_shouldRefreshLocation() {
when(mUserManager.hasUserRestriction(anyString())).thenReturn(false);
Settings.Secure.putInt(mContext.getContentResolver(),
Settings.Secure.LOCATION_MODE, Settings.Secure.LOCATION_MODE_OFF);
mEnabler.setLocationEnabled(true);
verify(mEnabler).refreshLocationMode();
}
@Test
public void setLocationEnabled_notRestricted_shouldBroadcastUpdate() {
when(mUserManager.hasUserRestriction(anyString())).thenReturn(false);
Settings.Secure.putInt(mContext.getContentResolver(),
Settings.Secure.LOCATION_MODE, Settings.Secure.LOCATION_MODE_OFF);
mEnabler.setLocationEnabled(true);
verify(mContext).sendBroadcastAsUser(
argThat(actionMatches(LocationManager.MODE_CHANGING_ACTION)),
eq(UserHandle.of(ActivityManager.getCurrentUser())),
eq(WRITE_SECURE_SETTINGS));
} }
@Test @Test
@@ -241,5 +267,12 @@ public class LocationEnablerTest {
return intent -> TextUtils.equals(expected, intent.getAction()); return intent -> TextUtils.equals(expected, intent.getAction());
} }
@Implements(value = LocationManagerWrapper.class)
public static class ShadowLocationManagerWrapper {
@Implementation
public void setLocationEnabledForUser(boolean enabled, UserHandle userHandle) {
// Do nothing
}
}
} }

View File

@@ -88,18 +88,17 @@ public class LocationSwitchBarControllerTest {
} }
@Test @Test
public void onSwitchChanged_switchChecked_shouldSetPreviousLocationMode() { public void onSwitchChanged_switchChecked_shouldSetLocationEnabled() {
mController.onSwitchChanged(mSwitch, true); mController.onSwitchChanged(mSwitch, true);
verify(mEnabler).setLocationMode( verify(mEnabler).setLocationEnabled(true);
android.provider.Settings.Secure.LOCATION_MODE_PREVIOUS);
} }
@Test @Test
public void onSwitchChanged_switchUnchecked_shouldSetLocationModeOff() { public void onSwitchChanged_switchUnchecked_shouldSetLocationDisabled() {
mController.onSwitchChanged(mSwitch, false); mController.onSwitchChanged(mSwitch, false);
verify(mEnabler).setLocationMode(android.provider.Settings.Secure.LOCATION_MODE_OFF); verify(mEnabler).setLocationEnabled(false);
} }
@Test @Test