Bug 701826 - Preferences gets unchecked temporarily when going to the preferences page [r=mfinkle]

This commit is contained in:
Brian Nicholson 2011-11-17 11:22:09 -08:00
parent 924bbcb930
commit 602c998a17
2 changed files with 65 additions and 44 deletions

View File

@ -58,6 +58,7 @@ import java.util.concurrent.*;
import java.lang.reflect.*;
import org.json.*;
import org.xmlpull.v1.*;
import android.os.*;
import android.app.*;
@ -73,6 +74,7 @@ import android.graphics.drawable.BitmapDrawable;
import android.widget.*;
import android.hardware.*;
import android.location.*;
import android.preference.*;
import android.util.*;
import android.net.*;
@ -108,7 +110,6 @@ abstract public class GeckoApp
public static BrowserToolbar mBrowserToolbar;
public static DoorHangerPopup mDoorHangerPopup;
public Favicons mFavicons;
private static boolean sIsGeckoReady = false;
private IntentFilter mBatteryFilter;
private BroadcastReceiver mBatteryReceiver;
private Geocoder mGeocoder;
@ -398,7 +399,7 @@ abstract public class GeckoApp
}
}
if (!sIsGeckoReady)
if (!GeckoPreferences.isLoaded())
aMenu.findItem(R.id.preferences).setEnabled(false);
Tab tab = Tabs.getInstance().getSelectedTab();
@ -749,15 +750,34 @@ abstract public class GeckoApp
handleDoorHangerRemove(message);
} else if (event.equals("Preferences:Data")) {
JSONArray jsonPrefs = message.getJSONArray("preferences");
GeckoPreferences.refresh(jsonPrefs);
} else if (event.equals("Gecko:Ready")) {
sIsGeckoReady = true;
GeckoPreferences.setData(jsonPrefs);
mMainHandler.post(new Runnable() {
public void run() {
if (sMenu != null)
sMenu.findItem(R.id.preferences).setEnabled(true);
}
});
} else if (event.equals("Gecko:Ready")) {
// retrieve the list of preferences from our preferences.xml file
XmlResourceParser parser = getResources().getXml(R.xml.preferences);
ArrayList<String> prefs = new ArrayList<String>();
while (parser.getEventType() != XmlPullParser.END_DOCUMENT) {
if (parser.getEventType() == XmlPullParser.START_TAG) {
String attr = parser.getAttributeValue("http://schemas.android.com/apk/res/android", "key");
if (attr != null) {
prefs.add(attr);
}
}
parser.next();
}
parser.close();
// request the preferences. doing it here means we don't need
// to wait when we open the GeckoPreferences activity.
JSONArray jsonPrefs = new JSONArray(prefs);
GeckoEvent getPrefsEvent = new GeckoEvent("Preferences:Get", jsonPrefs.toString());
GeckoAppShell.sendEventToGecko(getPrefsEvent);
connectGeckoLayerClient();
} else if (event.equals("PanZoom:Ack")) {
Rect rect = RectUtils.create(message.getJSONObject("rect"));

View File

@ -54,9 +54,10 @@ public class GeckoPreferences
implements OnPreferenceChangeListener
{
private static final String LOG_FILE_NAME = "GeckoPreferences";
private ArrayList<String> mPreferencesList = new ArrayList<String>();
private static PreferenceScreen mPreferenceScreen;
private static Context sContext;
private static JSONArray sJSONPrefs = null;
private ArrayList<String> mPreferencesList = new ArrayList<String>();
private PreferenceScreen mPreferenceScreen;
@Override
protected void onCreate(Bundle savedInstanceState) {
@ -65,7 +66,10 @@ public class GeckoPreferences
addPreferencesFromResource(R.xml.preferences);
mPreferenceScreen = getPreferenceScreen();
initGroups(mPreferenceScreen);
initValues();
// setData() must have been called already
if (sJSONPrefs != null)
refresh();
}
private void initGroups(PreferenceGroup preferences) {
@ -81,12 +85,6 @@ public class GeckoPreferences
}
}
@Override
protected void onDestroy() {
super.onDestroy();
mPreferenceScreen = null;
}
@Override
public boolean onPreferenceChange(Preference preference, Object newValue) {
String prefName = preference.getKey();
@ -96,20 +94,17 @@ public class GeckoPreferences
return true;
}
// Initialize preferences by sending the "Preferences:Get" command to Gecko
private void initValues() {
JSONArray jsonPrefs = new JSONArray(mPreferencesList);
GeckoEvent event = new GeckoEvent("Preferences:Get", jsonPrefs.toString());
GeckoAppShell.sendEventToGecko(event);
public static void setData(JSONArray jsonPrefs) {
sJSONPrefs = jsonPrefs;
}
// Use values received from Gecko to update preferences UI
public static void refresh(JSONArray jsonPrefs) {
try {
if (mPreferenceScreen == null)
return;
public static boolean isLoaded() {
return sJSONPrefs != null;
}
// Update the preferences UI with our in-memory JSON preferences object
private void refresh() {
try {
// set the current page URL for the "Home page" preference
final String[] homepageValues = sContext.getResources().getStringArray(R.array.pref_homepage_values);
final Preference homepagePref = mPreferenceScreen.findPreference("browser.startup.homepage");
@ -121,9 +116,9 @@ public class GeckoPreferences
}
});
final int length = jsonPrefs.length();
final int length = sJSONPrefs.length();
for (int i = 0; i < length; i++) {
JSONObject jPref = jsonPrefs.getJSONObject(i);
JSONObject jPref = sJSONPrefs.getJSONObject(i);
final String prefName = jPref.getString("name");
final String prefType = jPref.getString("type");
final Preference pref = mPreferenceScreen.findPreference(prefName);
@ -166,28 +161,34 @@ public class GeckoPreferences
}
}
// Send the "Preferences:Set" command with a preference value to Gecko
public static void setPreference(String pref, Object value) {
// update the in-memory preferences cache
JSONObject jsonPref = null;
try {
JSONObject jsonPref = new JSONObject();
jsonPref.put("name", pref);
if (value instanceof Boolean) {
jsonPref.put("type", "bool");
jsonPref.put("value", ((Boolean)value).booleanValue());
for (int i = 0; i < sJSONPrefs.length(); i++) {
if (sJSONPrefs.getJSONObject(i).getString("name").equals(pref)) {
jsonPref = sJSONPrefs.getJSONObject(i);
if (value instanceof Boolean)
jsonPref.put("value", ((Boolean)value).booleanValue());
else if (value instanceof Integer)
jsonPref.put("value", ((Integer)value).intValue());
else
jsonPref.put("value", String.valueOf(value));
break;
}
}
else if (value instanceof Integer) {
jsonPref.put("type", "int");
jsonPref.put("value", ((Integer)value).intValue());
}
else {
jsonPref.put("type", "string");
jsonPref.put("value", String.valueOf(value));
}
GeckoEvent event = new GeckoEvent("Preferences:Set", jsonPref.toString());
GeckoAppShell.sendEventToGecko(event);
} catch (JSONException e) {
Log.e(LOG_FILE_NAME, "JSON exception: ", e);
return;
}
if (jsonPref == null) {
Log.e(LOG_FILE_NAME, "invalid preference given to setPreference()");
return;
}
// send the Preferences:Set message to Gecko
GeckoEvent event = new GeckoEvent("Preferences:Set", jsonPref.toString());
GeckoAppShell.sendEventToGecko(event);
}
}