Merge "Used BroadcastReceiver to monitor mode changes"

This commit is contained in:
Lifu Tang
2014-01-31 20:46:49 +00:00
committed by Android (Google) Code Review
2 changed files with 31 additions and 39 deletions

View File

@@ -17,14 +17,11 @@
package com.android.settings.location; package com.android.settings.location;
import android.app.ActionBar; import android.app.ActionBar;
import android.app.Activity;
import android.content.BroadcastReceiver; import android.content.BroadcastReceiver;
import android.content.Context; import android.content.Context;
import android.content.Intent; import android.content.Intent;
import android.content.IntentFilter; import android.content.IntentFilter;
import android.location.LocationManager;
import android.location.SettingInjectorService; import android.location.SettingInjectorService;
import android.os.Bundle;
import android.preference.Preference; import android.preference.Preference;
import android.preference.PreferenceActivity; import android.preference.PreferenceActivity;
import android.preference.PreferenceCategory; import android.preference.PreferenceCategory;
@@ -63,6 +60,7 @@ public class LocationSettings extends LocationSettingsBase
private PreferenceCategory mCategoryRecentLocationRequests; private PreferenceCategory mCategoryRecentLocationRequests;
/** Receives UPDATE_INTENT */ /** Receives UPDATE_INTENT */
private BroadcastReceiver mReceiver; private BroadcastReceiver mReceiver;
private SettingsInjector injector;
public LocationSettings() { public LocationSettings() {
mValidListener = false; mValidListener = false;
@@ -166,15 +164,12 @@ public class LocationSettings extends LocationSettingsBase
* category if there are no injected settings. * category if there are no injected settings.
* *
* Reloads the settings whenever receives * Reloads the settings whenever receives
* {@link SettingInjectorService#ACTION_INJECTED_SETTING_CHANGED}. As a safety measure, * {@link SettingInjectorService#ACTION_INJECTED_SETTING_CHANGED}.
* also reloads on {@link LocationManager#MODE_CHANGED_ACTION} to ensure the settings are
* up-to-date after mode changes even if an affected app doesn't send the setting changed
* broadcast.
*/ */
private void addLocationServices(Context context, PreferenceScreen root) { private void addLocationServices(Context context, PreferenceScreen root) {
PreferenceCategory categoryLocationServices = PreferenceCategory categoryLocationServices =
(PreferenceCategory) root.findPreference(KEY_LOCATION_SERVICES); (PreferenceCategory) root.findPreference(KEY_LOCATION_SERVICES);
final SettingsInjector injector = new SettingsInjector(context); injector = new SettingsInjector(context);
List<Preference> locationServices = injector.getInjectedSettings(); List<Preference> locationServices = injector.getInjectedSettings();
mReceiver = new BroadcastReceiver() { mReceiver = new BroadcastReceiver() {
@@ -189,7 +184,6 @@ public class LocationSettings extends LocationSettingsBase
IntentFilter filter = new IntentFilter(); IntentFilter filter = new IntentFilter();
filter.addAction(SettingInjectorService.ACTION_INJECTED_SETTING_CHANGED); filter.addAction(SettingInjectorService.ACTION_INJECTED_SETTING_CHANGED);
filter.addAction(LocationManager.MODE_CHANGED_ACTION);
context.registerReceiver(mReceiver, filter); context.registerReceiver(mReceiver, filter);
if (locationServices.size() > 0) { if (locationServices.size() > 0) {
@@ -242,6 +236,9 @@ public class LocationSettings extends LocationSettingsBase
mSwitch.setOnCheckedChangeListener(this); mSwitch.setOnCheckedChangeListener(this);
} }
} }
// As a safety measure, also reloads on location mode change to ensure the settings are
// up-to-date even if an affected app doesn't send the setting changed broadcast.
injector.reloadStatusMessages();
} }
/** /**

View File

@@ -16,12 +16,11 @@
package com.android.settings.location; package com.android.settings.location;
import android.app.LoaderManager.LoaderCallbacks; import android.content.BroadcastReceiver;
import android.content.Context; import android.content.Context;
import android.content.CursorLoader;
import android.content.Intent; import android.content.Intent;
import android.content.Loader; import android.content.IntentFilter;
import android.database.Cursor; import android.location.LocationManager;
import android.os.Bundle; import android.os.Bundle;
import android.os.UserManager; import android.os.UserManager;
import android.provider.Settings; import android.provider.Settings;
@@ -33,8 +32,7 @@ import com.android.settings.SettingsPreferenceFragment;
* A base class that listens to location settings change and modifies location * A base class that listens to location settings change and modifies location
* settings. * settings.
*/ */
public abstract class LocationSettingsBase extends SettingsPreferenceFragment public abstract class LocationSettingsBase extends SettingsPreferenceFragment {
implements LoaderCallbacks<Cursor> {
private static final String TAG = "LocationSettingsBase"; private static final String TAG = "LocationSettingsBase";
/** Broadcast intent action when the location mode is about to change. */ /** Broadcast intent action when the location mode is about to change. */
private static final String MODE_CHANGING_ACTION = private static final String MODE_CHANGING_ACTION =
@@ -42,8 +40,8 @@ public abstract class LocationSettingsBase extends SettingsPreferenceFragment
private static final String CURRENT_MODE_KEY = "CURRENT_MODE"; private static final String CURRENT_MODE_KEY = "CURRENT_MODE";
private static final String NEW_MODE_KEY = "NEW_MODE"; private static final String NEW_MODE_KEY = "NEW_MODE";
private static final int LOADER_ID_LOCATION_MODE = 1;
private int mCurrentMode; private int mCurrentMode;
private BroadcastReceiver mReceiver;
/** /**
* Whether the fragment is actively running. * Whether the fragment is actively running.
@@ -53,17 +51,33 @@ public abstract class LocationSettingsBase extends SettingsPreferenceFragment
@Override @Override
public void onCreate(Bundle icicle) { public void onCreate(Bundle icicle) {
super.onCreate(icicle); super.onCreate(icicle);
getLoaderManager().initLoader(LOADER_ID_LOCATION_MODE, null, this); mReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
if (Log.isLoggable(TAG, Log.DEBUG)) {
Log.d(TAG, "Received location mode change intent: " + intent);
}
refreshLocationMode();
}
};
} }
@Override @Override
public void onResume() { public void onResume() {
super.onResume(); super.onResume();
mActive = true; mActive = true;
IntentFilter filter = new IntentFilter();
filter.addAction(LocationManager.MODE_CHANGED_ACTION);
getActivity().registerReceiver(mReceiver, filter);
} }
@Override @Override
public void onPause() { public void onPause() {
try {
getActivity().unregisterReceiver(mReceiver);
} catch (RuntimeException e) {
// Ignore exceptions caused by race condition
}
super.onPause(); super.onPause();
mActive = false; mActive = false;
} }
@@ -103,29 +117,10 @@ public abstract class LocationSettingsBase extends SettingsPreferenceFragment
int mode = Settings.Secure.getInt(getContentResolver(), Settings.Secure.LOCATION_MODE, int mode = Settings.Secure.getInt(getContentResolver(), Settings.Secure.LOCATION_MODE,
Settings.Secure.LOCATION_MODE_OFF); Settings.Secure.LOCATION_MODE_OFF);
mCurrentMode = mode; mCurrentMode = mode;
if (Log.isLoggable(TAG, Log.INFO)) {
Log.i(TAG, "Location mode has been changed");
}
onModeChanged(mode, isRestricted()); onModeChanged(mode, isRestricted());
} }
} }
@Override
public Loader<Cursor> onCreateLoader(int id, Bundle args) {
switch (id) {
case LOADER_ID_LOCATION_MODE:
return new CursorLoader(getActivity(), Settings.Secure.CONTENT_URI, null,
"(" + Settings.System.NAME + "=?)",
new String[] { Settings.Secure.LOCATION_MODE }, null);
default:
return null;
}
}
@Override
public void onLoadFinished(Loader<Cursor> loader, Cursor data) {
refreshLocationMode();
}
@Override
public void onLoaderReset(Loader<Cursor> loader) {
// Nothing to do here.
}
} }