am 32e016b5: Reloads status messages for injected items

* commit '32e016b5fae1f4caf673ff1f062869dce0e94757':
  Reloads status messages for injected items
This commit is contained in:
Lifu Tang
2013-08-22 18:26:49 -07:00
committed by Android Git Automerger
2 changed files with 73 additions and 22 deletions

View File

@@ -18,7 +18,11 @@ package com.android.settings.location;
import android.app.ActionBar; import android.app.ActionBar;
import android.app.Activity; import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent; import android.content.Intent;
import android.content.IntentFilter;
import android.location.SettingInjectorService;
import android.os.Bundle; import android.os.Bundle;
import android.preference.Preference; import android.preference.Preference;
import android.preference.PreferenceActivity; import android.preference.PreferenceActivity;
@@ -53,8 +57,9 @@ public class LocationSettings extends LocationSettingsBase
private Switch mSwitch; private Switch mSwitch;
private boolean mValidListener; private boolean mValidListener;
private Preference mLocationMode; private Preference mLocationMode;
private BatteryStatsHelper mStatsHelper; private BatteryStatsHelper mStatsHelper;
/** Receives UPDATE_INTENT */
private BroadcastReceiver mReceiver;
public LocationSettings() { public LocationSettings() {
mValidListener = false; mValidListener = false;
@@ -89,6 +94,11 @@ public class LocationSettings extends LocationSettingsBase
@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();
mValidListener = false; mValidListener = false;
mSwitch.setOnCheckedChangeListener(null); mSwitch.setOnCheckedChangeListener(null);
@@ -156,7 +166,18 @@ public class LocationSettings extends LocationSettingsBase
PreferenceCategory categoryLocationServices = PreferenceCategory categoryLocationServices =
(PreferenceCategory) root.findPreference(KEY_LOCATION_SERVICES); (PreferenceCategory) root.findPreference(KEY_LOCATION_SERVICES);
List<Preference> locationServices = SettingsInjector.getInjectedSettings(activity); final SettingsInjector injector = new SettingsInjector(activity);
List<Preference> locationServices = injector.getInjectedSettings();
mReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
injector.reloadStatusMessages();
}
};
activity.registerReceiver(
mReceiver, new IntentFilter(SettingInjectorService.UPDATE_INTENT));
if (locationServices.size() > 0) { if (locationServices.size() > 0) {
addPreferencesSorted(locationServices, categoryLocationServices); addPreferencesSorted(locationServices, categoryLocationServices);
} else { } else {

View File

@@ -79,6 +79,13 @@ class SettingsInjector {
*/ */
public static final String ATTRIBUTES_NAME = "injected-location-setting"; public static final String ATTRIBUTES_NAME = "injected-location-setting";
private Context mContext;
private StatusLoader mLoader = null;
public SettingsInjector(Context context) {
mContext = context;
}
/** /**
* Returns a list with one {@link InjectedSetting} object for each {@link android.app.Service} * Returns a list with one {@link InjectedSetting} object for each {@link android.app.Service}
* that responds to {@link #RECEIVER_INTENT} and provides the expected setting metadata. * that responds to {@link #RECEIVER_INTENT} and provides the expected setting metadata.
@@ -87,8 +94,8 @@ class SettingsInjector {
* *
* TODO: unit test * TODO: unit test
*/ */
public static List<InjectedSetting> getSettings(Context context) { private List<InjectedSetting> getSettings() {
PackageManager pm = context.getPackageManager(); PackageManager pm = mContext.getPackageManager();
Intent receiverIntent = new Intent(RECEIVER_INTENT); Intent receiverIntent = new Intent(RECEIVER_INTENT);
List<ResolveInfo> resolveInfos = List<ResolveInfo> resolveInfos =
@@ -189,8 +196,7 @@ class SettingsInjector {
} }
} }
private static final class StatusLoader { private final class StatusLoader {
private final Context mContext;
private final Intent mIntent; private final Intent mIntent;
private final StatusLoader mPrev; private final StatusLoader mPrev;
@@ -199,12 +205,29 @@ class SettingsInjector {
/** /**
* Creates a loader and chains with the previous loader. * Creates a loader and chains with the previous loader.
*/ */
public StatusLoader(Context context, Intent intent, StatusLoader prev) { public StatusLoader(Intent intent, StatusLoader prev) {
mContext = context;
mIntent = intent; mIntent = intent;
mPrev = prev; mPrev = prev;
} }
/**
* Clears the loaded flag for the whole chain.
*/
private void setNotLoaded() {
mLoaded = false;
if (mPrev != null) {
mPrev.setNotLoaded();
}
}
/**
* Reloads the whole chain.
*/
public void reload() {
setNotLoaded();
loadIfNotLoaded();
}
/** /**
* If the current message hasn't been loaded, loads the status messages * If the current message hasn't been loaded, loads the status messages
* and set time out for the next message. * and set time out for the next message.
@@ -238,34 +261,41 @@ class SettingsInjector {
* TODO: extract InjectedLocationSettingGetter that returns an iterable over * TODO: extract InjectedLocationSettingGetter that returns an iterable over
* InjectedSetting objects, so that this class can focus on UI * InjectedSetting objects, so that this class can focus on UI
*/ */
public static List<Preference> getInjectedSettings(Context context) { public List<Preference> getInjectedSettings() {
Iterable<InjectedSetting> settings = getSettings();
Iterable<InjectedSetting> settings = getSettings(context);
ArrayList<Preference> prefs = new ArrayList<Preference>(); ArrayList<Preference> prefs = new ArrayList<Preference>();
StatusLoader loader = null; mLoader = null;
for (InjectedSetting setting : settings) { for (InjectedSetting setting : settings) {
Preference pref = addServiceSetting(context, prefs, setting); Preference pref = addServiceSetting(prefs, setting);
Intent intent = createUpdatingIntent(context, pref, setting, loader); Intent intent = createUpdatingIntent(pref, setting, mLoader);
loader = new StatusLoader(context, intent, loader); mLoader = new StatusLoader(intent, mLoader);
} }
// Start a thread to load each list item status. // Start a thread to load each list item status.
if (loader != null) { if (mLoader != null) {
loader.loadIfNotLoaded(); mLoader.loadIfNotLoaded();
} }
return prefs; return prefs;
} }
/**
* Reloads the status messages for all the preference items.
*/
public void reloadStatusMessages() {
if (mLoader != null) {
mLoader.reload();
}
}
/** /**
* Adds an injected setting to the root with status "Loading...". * Adds an injected setting to the root with status "Loading...".
*/ */
private static Preference addServiceSetting( private Preference addServiceSetting(List<Preference> prefs, InjectedSetting info) {
Context context, List<Preference> prefs, InjectedSetting info) { Preference pref = new Preference(mContext);
Preference pref = new Preference(context);
pref.setTitle(info.title); pref.setTitle(info.title);
pref.setSummary(R.string.location_loading_injected_setting); pref.setSummary(R.string.location_loading_injected_setting);
PackageManager pm = context.getPackageManager(); PackageManager pm = mContext.getPackageManager();
Drawable icon = pm.getDrawable(info.packageName, info.iconId, null); Drawable icon = pm.getDrawable(info.packageName, info.iconId, null);
pref.setIcon(icon); pref.setIcon(icon);
@@ -281,7 +311,7 @@ class SettingsInjector {
* Creates an Intent to ask the receiver for the current status for the setting, and display it * Creates an Intent to ask the receiver for the current status for the setting, and display it
* when it replies. * when it replies.
*/ */
private static Intent createUpdatingIntent(Context context, private static Intent createUpdatingIntent(
final Preference pref, final InjectedSetting info, final StatusLoader prev) { final Preference pref, final InjectedSetting info, final StatusLoader prev) {
final Intent receiverIntent = info.getServiceIntent(); final Intent receiverIntent = info.getServiceIntent();
Handler handler = new Handler() { Handler handler = new Handler() {