Location setting now updates summary timely.
Subscribed the LocationPreferenceController to listen to the location providers changed action. This allows timely summary update. Previous approach, directly calling the updateSummary method onResume failed in the scenario when user changed the location settings via the QuickSettings. Test: Added robolectric tests, and manually verified the intended behavior on a device. Bug: 37956060 Change-Id: I2f81713d59da3384f3c98b327d377d529d440a88
This commit is contained in:
@@ -15,20 +15,46 @@
|
||||
*/
|
||||
package com.android.settings.location;
|
||||
|
||||
import android.content.BroadcastReceiver;
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.content.IntentFilter;
|
||||
import android.location.LocationManager;
|
||||
import android.provider.Settings.Secure;
|
||||
import android.support.annotation.VisibleForTesting;
|
||||
import android.support.v7.preference.Preference;
|
||||
import android.support.v7.preference.PreferenceScreen;
|
||||
import com.android.settings.R;
|
||||
import com.android.settings.core.PreferenceController;
|
||||
import com.android.settings.core.lifecycle.Lifecycle;
|
||||
import com.android.settings.core.lifecycle.LifecycleObserver;
|
||||
import com.android.settings.core.lifecycle.events.OnPause;
|
||||
import com.android.settings.core.lifecycle.events.OnResume;
|
||||
|
||||
public class LocationPreferenceController extends PreferenceController {
|
||||
public class LocationPreferenceController extends PreferenceController implements
|
||||
LifecycleObserver, OnResume, OnPause {
|
||||
|
||||
private static final String KEY_LOCATION = "location";
|
||||
private Context mContext;
|
||||
private Preference mPreference;
|
||||
|
||||
public LocationPreferenceController(Context context) {
|
||||
@VisibleForTesting
|
||||
BroadcastReceiver mLocationProvidersChangedReceiver;
|
||||
|
||||
public LocationPreferenceController(Context context, Lifecycle lifecycle) {
|
||||
super(context);
|
||||
mContext = context;
|
||||
mLocationProvidersChangedReceiver = new BroadcastReceiver() {
|
||||
@Override
|
||||
public void onReceive(Context context, Intent intent) {
|
||||
if (intent.getAction().equals(LocationManager.PROVIDERS_CHANGED_ACTION)) {
|
||||
updateSummary();
|
||||
}
|
||||
}
|
||||
};
|
||||
if (lifecycle != null) {
|
||||
lifecycle.addObserver(this);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -37,6 +63,21 @@ public class LocationPreferenceController extends PreferenceController {
|
||||
mPreference = screen.findPreference(KEY_LOCATION);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onResume() {
|
||||
if (mLocationProvidersChangedReceiver != null) {
|
||||
mContext.registerReceiver(mLocationProvidersChangedReceiver, new IntentFilter(
|
||||
LocationManager.PROVIDERS_CHANGED_ACTION));
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onPause() {
|
||||
if (mLocationProvidersChangedReceiver != null) {
|
||||
mContext.unregisterReceiver(mLocationProvidersChangedReceiver);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void updateState(Preference preference) {
|
||||
preference.setSummary(getLocationSummary(mContext));
|
||||
@@ -58,10 +99,10 @@ public class LocationPreferenceController extends PreferenceController {
|
||||
|
||||
public static String getLocationSummary(Context context) {
|
||||
int mode = Secure.getInt(context.getContentResolver(),
|
||||
Secure.LOCATION_MODE, Secure.LOCATION_MODE_OFF);
|
||||
Secure.LOCATION_MODE, Secure.LOCATION_MODE_OFF);
|
||||
if (mode != Secure.LOCATION_MODE_OFF) {
|
||||
return context.getString(R.string.location_on_summary,
|
||||
context.getString(getLocationString(mode)));
|
||||
context.getString(getLocationString(mode)));
|
||||
}
|
||||
return context.getString(R.string.location_off_summary);
|
||||
}
|
||||
@@ -79,5 +120,4 @@ public class LocationPreferenceController extends PreferenceController {
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
}
|
||||
|
Reference in New Issue
Block a user