2011-10-14 12:48:02 -07:00
|
|
|
/* -*- Mode: Java; c-basic-offset: 4; tab-width: 4; indent-tabs-mode: nil; -*-
|
|
|
|
* ***** BEGIN LICENSE BLOCK *****
|
|
|
|
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
|
|
|
|
*
|
|
|
|
* The contents of this file are subject to the Mozilla Public License Version
|
|
|
|
* 1.1 (the "License"); you may not use this file except in compliance with
|
|
|
|
* the License. You may obtain a copy of the License at
|
|
|
|
* http://www.mozilla.org/MPL/
|
|
|
|
*
|
|
|
|
* Software distributed under the License is distributed on an "AS IS" basis,
|
|
|
|
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
|
|
|
|
* for the specific language governing rights and limitations under the
|
|
|
|
* License.
|
|
|
|
*
|
|
|
|
* The Original Code is Mozilla Android code.
|
|
|
|
*
|
|
|
|
* The Initial Developer of the Original Code is Mozilla Corporation.
|
|
|
|
* Portions created by the Initial Developer are Copyright (C) 2011
|
|
|
|
* the Initial Developer. All Rights Reserved.
|
|
|
|
*
|
|
|
|
* Contributor(s):
|
|
|
|
* Alex Pakhotin <alexp@mozilla.com>
|
|
|
|
*
|
|
|
|
* Alternatively, the contents of this file may be used under the terms of
|
|
|
|
* either the GNU General Public License Version 2 or later (the "GPL"), or
|
|
|
|
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
|
|
|
|
* in which case the provisions of the GPL or the LGPL are applicable instead
|
|
|
|
* of those above. If you wish to allow use of your version of this file only
|
|
|
|
* under the terms of either the GPL or the LGPL, and not to allow others to
|
|
|
|
* use your version of this file under the terms of the MPL, indicate your
|
|
|
|
* decision by deleting the provisions above and replace them with the notice
|
|
|
|
* and other provisions required by the GPL or the LGPL. If you do not delete
|
|
|
|
* the provisions above, a recipient may use your version of this file under
|
|
|
|
* the terms of any one of the MPL, the GPL or the LGPL.
|
|
|
|
*
|
|
|
|
* ***** END LICENSE BLOCK ***** */
|
|
|
|
|
|
|
|
package org.mozilla.gecko;
|
|
|
|
|
2011-10-25 14:31:55 -07:00
|
|
|
import java.util.ArrayList;
|
|
|
|
|
2011-10-14 12:48:02 -07:00
|
|
|
import android.os.Bundle;
|
|
|
|
import android.content.res.Resources;
|
2011-10-25 14:31:55 -07:00
|
|
|
import android.content.Context;
|
2011-10-14 12:48:02 -07:00
|
|
|
import android.preference.*;
|
|
|
|
import android.preference.Preference.*;
|
|
|
|
import android.util.Log;
|
2011-10-25 14:31:55 -07:00
|
|
|
import android.view.View;
|
|
|
|
import android.view.ViewGroup;
|
2011-10-14 12:48:02 -07:00
|
|
|
import org.json.*;
|
|
|
|
|
|
|
|
public class GeckoPreferences
|
|
|
|
extends PreferenceActivity
|
|
|
|
implements OnPreferenceChangeListener
|
|
|
|
{
|
|
|
|
private static final String LOG_FILE_NAME = "GeckoPreferences";
|
|
|
|
private ArrayList<String> mPreferencesList = new ArrayList<String>();
|
|
|
|
private static PreferenceScreen mPreferenceScreen;
|
2011-10-25 14:31:55 -07:00
|
|
|
private static Context sContext;
|
2011-10-14 12:48:02 -07:00
|
|
|
|
|
|
|
@Override
|
|
|
|
protected void onCreate(Bundle savedInstanceState) {
|
|
|
|
super.onCreate(savedInstanceState);
|
2011-10-25 14:31:55 -07:00
|
|
|
sContext = this;
|
2011-10-14 12:48:02 -07:00
|
|
|
addPreferencesFromResource(R.xml.preferences);
|
|
|
|
mPreferenceScreen = getPreferenceScreen();
|
|
|
|
initGroups(mPreferenceScreen);
|
|
|
|
initValues();
|
|
|
|
}
|
|
|
|
|
|
|
|
private void initGroups(PreferenceGroup preferences) {
|
|
|
|
final int count = preferences.getPreferenceCount();
|
|
|
|
for (int i = 0; i < count; i++) {
|
|
|
|
Preference pref = preferences.getPreference(i);
|
|
|
|
if (pref instanceof PreferenceGroup)
|
|
|
|
initGroups((PreferenceGroup)pref);
|
|
|
|
else {
|
|
|
|
pref.setOnPreferenceChangeListener(this);
|
|
|
|
mPreferencesList.add(pref.getKey());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected void onDestroy() {
|
|
|
|
super.onDestroy();
|
|
|
|
mPreferenceScreen = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean onPreferenceChange(Preference preference, Object newValue) {
|
|
|
|
String prefName = preference.getKey();
|
|
|
|
setPreference(prefName, newValue);
|
2011-10-25 16:27:54 -07:00
|
|
|
if (preference instanceof ListPreference)
|
|
|
|
((ListPreference)preference).setSummary((String)newValue);
|
2011-10-14 12:48:02 -07:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Use values received from Gecko to update preferences UI
|
|
|
|
public static void refresh(JSONArray jsonPrefs) {
|
|
|
|
try {
|
|
|
|
if (mPreferenceScreen == null)
|
|
|
|
return;
|
|
|
|
|
2011-10-25 14:31:55 -07:00
|
|
|
// set the current page URL for the "Home page" preference
|
2011-11-01 10:14:37 -07:00
|
|
|
final String[] homepageValues = sContext.getResources().getStringArray(R.array.pref_homepage_values);
|
2011-10-25 14:31:55 -07:00
|
|
|
final Preference homepagePref = mPreferenceScreen.findPreference("browser.startup.homepage");
|
|
|
|
GeckoAppShell.getMainHandler().post(new Runnable() {
|
|
|
|
public void run() {
|
2011-11-01 10:14:37 -07:00
|
|
|
Tab tab = Tabs.getInstance().getSelectedTab();
|
|
|
|
homepageValues[2] = tab.getURL();
|
2011-10-25 14:31:55 -07:00
|
|
|
((ListPreference)homepagePref).setEntryValues(homepageValues);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2011-10-14 12:48:02 -07:00
|
|
|
final int length = jsonPrefs.length();
|
|
|
|
for (int i = 0; i < length; i++) {
|
|
|
|
JSONObject jPref = jsonPrefs.getJSONObject(i);
|
|
|
|
final String prefName = jPref.getString("name");
|
|
|
|
final String prefType = jPref.getString("type");
|
|
|
|
final Preference pref = mPreferenceScreen.findPreference(prefName);
|
2011-10-25 14:31:55 -07:00
|
|
|
|
|
|
|
if (prefName.equals("browser.startup.homepage")) {
|
|
|
|
final String value = jPref.getString("value");
|
|
|
|
GeckoAppShell.getMainHandler().post(new Runnable() {
|
|
|
|
public void run() {
|
|
|
|
pref.setSummary(value);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2011-10-14 12:48:02 -07:00
|
|
|
if (pref instanceof CheckBoxPreference && "bool".equals(prefType)) {
|
|
|
|
final boolean value = jPref.getBoolean("value");
|
|
|
|
GeckoAppShell.getMainHandler().post(new Runnable() {
|
|
|
|
public void run() {
|
|
|
|
if (((CheckBoxPreference)pref).isChecked() != value)
|
|
|
|
((CheckBoxPreference)pref).setChecked(value);
|
|
|
|
}
|
|
|
|
});
|
2011-10-25 14:31:55 -07:00
|
|
|
} else if (pref instanceof EditTextPreference && "string".equals(prefType)) {
|
|
|
|
final String value = jPref.getString("value");
|
|
|
|
GeckoAppShell.getMainHandler().post(new Runnable() {
|
|
|
|
public void run() {
|
|
|
|
((EditTextPreference)pref).setText(value);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
} else if (pref instanceof ListPreference && "string".equals(prefType)) {
|
|
|
|
final String value = jPref.getString("value");
|
|
|
|
GeckoAppShell.getMainHandler().post(new Runnable() {
|
|
|
|
public void run() {
|
|
|
|
((ListPreference)pref).setValue(value);
|
|
|
|
}
|
|
|
|
});
|
2011-10-14 12:48:02 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} catch (JSONException e) {
|
|
|
|
Log.e(LOG_FILE_NAME, "Problem parsing preferences response: ", e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Send the "Preferences:Set" command with a preference value to Gecko
|
|
|
|
public static void setPreference(String pref, Object value) {
|
|
|
|
try {
|
|
|
|
JSONObject jsonPref = new JSONObject();
|
|
|
|
jsonPref.put("name", pref);
|
|
|
|
if (value instanceof Boolean) {
|
|
|
|
jsonPref.put("type", "bool");
|
|
|
|
jsonPref.put("value", ((Boolean)value).booleanValue());
|
|
|
|
}
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|