Bug 701824 - "Show character encoding" (Text encoding) preference should be hooked up. r=mfinkle

This commit is contained in:
Brian Nicholson 2012-01-23 13:47:48 -08:00
parent 46c2d6d19c
commit a716263099
9 changed files with 190 additions and 14 deletions

View File

@ -440,6 +440,7 @@ abstract public class GeckoApp
MenuItem share = aMenu.findItem(R.id.share); MenuItem share = aMenu.findItem(R.id.share);
MenuItem saveAsPDF = aMenu.findItem(R.id.save_as_pdf); MenuItem saveAsPDF = aMenu.findItem(R.id.save_as_pdf);
MenuItem downloads = aMenu.findItem(R.id.downloads); MenuItem downloads = aMenu.findItem(R.id.downloads);
MenuItem charEncoding = aMenu.findItem(R.id.char_encoding);
if (tab == null) { if (tab == null) {
bookmark.setEnabled(false); bookmark.setEnabled(false);
@ -476,6 +477,8 @@ abstract public class GeckoApp
if (Build.VERSION.SDK_INT < 12) if (Build.VERSION.SDK_INT < 12)
downloads.setVisible(false); downloads.setVisible(false);
charEncoding.setVisible(GeckoPreferences.getCharEncodingState());
return true; return true;
} }
@ -538,6 +541,9 @@ abstract public class GeckoApp
intent = new Intent(DownloadManager.ACTION_VIEW_DOWNLOADS); intent = new Intent(DownloadManager.ACTION_VIEW_DOWNLOADS);
startActivity(intent); startActivity(intent);
return true; return true;
case R.id.char_encoding:
GeckoAppShell.sendEventToGecko(new GeckoEvent("CharEncoding:Get", null));
return true;
default: default:
return super.onOptionsItemSelected(item); return super.onOptionsItemSelected(item);
} }
@ -985,6 +991,51 @@ abstract public class GeckoApp
int size = message.getInt("size"); int size = message.getInt("size");
handleDownloadDone(displayName, path, mimeType, size); handleDownloadDone(displayName, path, mimeType, size);
} else if (event.equals("CharEncoding:Data")) {
final JSONArray charsets = message.getJSONArray("charsets");
int selected = message.getInt("selected");
final int len = charsets.length();
final String[] titleArray = new String[len];
for (int i = 0; i < len; i++) {
JSONObject charset = charsets.getJSONObject(i);
titleArray[i] = charset.getString("title");
}
final AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(this);
dialogBuilder.setSingleChoiceItems(titleArray, selected, new AlertDialog.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
try {
JSONObject charset = charsets.getJSONObject(which);
GeckoAppShell.sendEventToGecko(new GeckoEvent("CharEncoding:Set", charset.getString("code")));
dialog.dismiss();
} catch (JSONException e) {
Log.e(LOGTAG, "error parsing json", e);
}
}
});
dialogBuilder.setNegativeButton(R.string.button_cancel, new AlertDialog.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
mMainHandler.post(new Runnable() {
public void run() {
dialogBuilder.show();
}
});
} else if (event.equals("CharEncoding:State")) {
final boolean visible = message.getString("visible").equals("true");
GeckoPreferences.setCharEncodingState(visible);
if (sMenu != null) {
mMainHandler.post(new Runnable() {
public void run() {
sMenu.findItem(R.id.char_encoding).setVisible(visible);
}
});
}
} }
} catch (Exception e) { } catch (Exception e) {
Log.e(LOGTAG, "Exception handling message \"" + event + "\":", e); Log.e(LOGTAG, "Exception handling message \"" + event + "\":", e);
@ -1598,6 +1649,8 @@ abstract public class GeckoApp
GeckoAppShell.registerGeckoEventListener("FormAssist:AutoComplete", GeckoApp.mAppContext); GeckoAppShell.registerGeckoEventListener("FormAssist:AutoComplete", GeckoApp.mAppContext);
GeckoAppShell.registerGeckoEventListener("Permissions:Data", GeckoApp.mAppContext); GeckoAppShell.registerGeckoEventListener("Permissions:Data", GeckoApp.mAppContext);
GeckoAppShell.registerGeckoEventListener("Downloads:Done", GeckoApp.mAppContext); GeckoAppShell.registerGeckoEventListener("Downloads:Done", GeckoApp.mAppContext);
GeckoAppShell.registerGeckoEventListener("CharEncoding:Data", GeckoApp.mAppContext);
GeckoAppShell.registerGeckoEventListener("CharEncoding:State", GeckoApp.mAppContext);
mConnectivityFilter = new IntentFilter(); mConnectivityFilter = new IntentFilter();
mConnectivityFilter.addAction(ConnectivityManager.CONNECTIVITY_ACTION); mConnectivityFilter.addAction(ConnectivityManager.CONNECTIVITY_ACTION);
@ -1926,6 +1979,8 @@ abstract public class GeckoApp
GeckoAppShell.unregisterGeckoEventListener("FormAssist:AutoComplete", GeckoApp.mAppContext); GeckoAppShell.unregisterGeckoEventListener("FormAssist:AutoComplete", GeckoApp.mAppContext);
GeckoAppShell.unregisterGeckoEventListener("Permissions:Data", GeckoApp.mAppContext); GeckoAppShell.unregisterGeckoEventListener("Permissions:Data", GeckoApp.mAppContext);
GeckoAppShell.unregisterGeckoEventListener("Downloads:Done", GeckoApp.mAppContext); GeckoAppShell.unregisterGeckoEventListener("Downloads:Done", GeckoApp.mAppContext);
GeckoAppShell.unregisterGeckoEventListener("CharEncoding:Data", GeckoApp.mAppContext);
GeckoAppShell.unregisterGeckoEventListener("CharEncoding:State", GeckoApp.mAppContext);
mFavicons.close(); mFavicons.close();

View File

@ -73,6 +73,7 @@ public class GeckoPreferences
private ArrayList<String> mPreferencesList = new ArrayList<String>(); private ArrayList<String> mPreferencesList = new ArrayList<String>();
private PreferenceScreen mPreferenceScreen; private PreferenceScreen mPreferenceScreen;
private static boolean sIsCharEncodingEnabled = false;
@Override @Override
protected void onCreate(Bundle savedInstanceState) { protected void onCreate(Bundle savedInstanceState) {
@ -140,12 +141,22 @@ public class GeckoPreferences
final private int DIALOG_CREATE_MASTER_PASSWORD = 0; final private int DIALOG_CREATE_MASTER_PASSWORD = 0;
final private int DIALOG_REMOVE_MASTER_PASSWORD = 1; final private int DIALOG_REMOVE_MASTER_PASSWORD = 1;
public static void setCharEncodingState(boolean enabled) {
sIsCharEncodingEnabled = enabled;
}
public static boolean getCharEncodingState() {
return sIsCharEncodingEnabled;
}
@Override @Override
public boolean onPreferenceChange(Preference preference, Object newValue) { public boolean onPreferenceChange(Preference preference, Object newValue) {
String prefName = preference.getKey(); String prefName = preference.getKey();
if (prefName != null && prefName.equals("privacy.masterpassword.enabled")) { if (prefName != null && prefName.equals("privacy.masterpassword.enabled")) {
showDialog((Boolean)newValue ? DIALOG_CREATE_MASTER_PASSWORD : DIALOG_REMOVE_MASTER_PASSWORD); showDialog((Boolean)newValue ? DIALOG_CREATE_MASTER_PASSWORD : DIALOG_REMOVE_MASTER_PASSWORD);
return false; return false;
} else if (prefName != null && prefName.equals("browser.menu.showCharacterEncoding")) {
setCharEncodingState(((String) newValue).equals("true"));
} }
setPreference(prefName, newValue); setPreference(prefName, newValue);

View File

@ -51,7 +51,9 @@
<!ENTITY pref_telemetry "Send performance data"> <!ENTITY pref_telemetry "Send performance data">
<!ENTITY pref_remember_signons "Remember passwords"> <!ENTITY pref_remember_signons "Remember passwords">
<!ENTITY pref_cookies "Enable cookies"> <!ENTITY pref_cookies "Enable cookies">
<!ENTITY pref_char_encoding "Show character encoding"> <!ENTITY pref_char_encoding "Character encoding">
<!ENTITY pref_char_encoding_on "Show menu">
<!ENTITY pref_char_encoding_off "Don\'t show menu">
<!ENTITY pref_clear_history "Clear history"> <!ENTITY pref_clear_history "Clear history">
<!ENTITY pref_clear_history_confirm "Browsing history will be deleted"> <!ENTITY pref_clear_history_confirm "Browsing history will be deleted">
<!ENTITY pref_clear_private_data "Clear private data"> <!ENTITY pref_clear_private_data "Clear private data">
@ -72,6 +74,7 @@
<!ENTITY addons "Add-ons"> <!ENTITY addons "Add-ons">
<!ENTITY downloads "Downloads"> <!ENTITY downloads "Downloads">
<!ENTITY char_encoding "Character Encoding">
<!ENTITY share "Share"> <!ENTITY share "Share">
<!ENTITY save_as_pdf "Save as PDF"> <!ENTITY save_as_pdf "Save as PDF">

View File

@ -30,6 +30,10 @@
<item android:id="@+id/downloads" <item android:id="@+id/downloads"
android:title="@string/downloads"/> android:title="@string/downloads"/>
<item android:id="@+id/char_encoding"
android:visible="false"
android:title="@string/char_encoding"/>
<item android:id="@+id/settings" <item android:id="@+id/settings"
android:title="@string/settings" /> android:title="@string/settings" />

View File

@ -25,4 +25,12 @@
<item>160</item> <item>160</item>
<item>240</item> <item>240</item>
</string-array> </string-array>
<string-array name="pref_char_encoding_entries">
<item>@string/pref_char_encoding_on</item>
<item>@string/pref_char_encoding_off</item>
</string-array>
<string-array name="pref_char_encoding_values">
<item>true</item>
<item>false</item>
</string-array>
</resources> </resources>

View File

@ -15,8 +15,10 @@
<PreferenceCategory android:title="@string/pref_category_content"> <PreferenceCategory android:title="@string/pref_category_content">
<CheckBoxPreference android:key="browser.menu.showCharacterEncoding" <ListPreference android:key="browser.menu.showCharacterEncoding"
android:title="@string/pref_char_encoding" android:title="@string/pref_char_encoding"
android:entries="@array/pref_char_encoding_entries"
android:entryValues="@array/pref_char_encoding_values"
android:persistent="false" /> android:persistent="false" />
<ListPreference android:key="plugin.enable" <ListPreference android:key="plugin.enable"

View File

@ -58,6 +58,8 @@
<string name="pref_remember_signons">&pref_remember_signons;</string> <string name="pref_remember_signons">&pref_remember_signons;</string>
<string name="pref_cookies">&pref_cookies;</string> <string name="pref_cookies">&pref_cookies;</string>
<string name="pref_char_encoding">&pref_char_encoding;</string> <string name="pref_char_encoding">&pref_char_encoding;</string>
<string name="pref_char_encoding_on">&pref_char_encoding_on;</string>
<string name="pref_char_encoding_off">&pref_char_encoding_off;</string>
<string name="pref_clear_history">&pref_clear_history;</string> <string name="pref_clear_history">&pref_clear_history;</string>
<string name="pref_clear_history_confirm">&pref_clear_history_confirm;</string> <string name="pref_clear_history_confirm">&pref_clear_history_confirm;</string>
<string name="pref_clear_private_data">&pref_clear_private_data;</string> <string name="pref_clear_private_data">&pref_clear_private_data;</string>
@ -78,6 +80,7 @@
<string name="new_tab">&new_tab;</string> <string name="new_tab">&new_tab;</string>
<string name="addons">&addons;</string> <string name="addons">&addons;</string>
<string name="downloads">&downloads;</string> <string name="downloads">&downloads;</string>
<string name="char_encoding">&char_encoding;</string>
<string name="site_settings_title">&site_settings_title;</string> <string name="site_settings_title">&site_settings_title;</string>
<string name="site_settings_cancel">&site_settings_cancel;</string> <string name="site_settings_cancel">&site_settings_cancel;</string>

View File

@ -146,7 +146,8 @@ function resolveGeckoURI(aURI) {
var Strings = {}; var Strings = {};
[ [
["brand", "chrome://branding/locale/brand.properties"], ["brand", "chrome://branding/locale/brand.properties"],
["browser", "chrome://browser/locale/browser.properties"] ["browser", "chrome://browser/locale/browser.properties"],
["charset", "chrome://global/locale/charsetTitles.properties"]
].forEach(function (aStringBundle) { ].forEach(function (aStringBundle) {
let [name, bundle] = aStringBundle; let [name, bundle] = aStringBundle;
XPCOMUtils.defineLazyGetter(Strings, name, function() { XPCOMUtils.defineLazyGetter(Strings, name, function() {
@ -252,6 +253,7 @@ var BrowserApp = {
ConsoleAPI.init(); ConsoleAPI.init();
ClipboardHelper.init(); ClipboardHelper.init();
PermissionsHelper.init(); PermissionsHelper.init();
CharacterEncoding.init();
// Init LoginManager // Init LoginManager
Cc["@mozilla.org/login-manager;1"].getService(Ci.nsILoginManager); Cc["@mozilla.org/login-manager;1"].getService(Ci.nsILoginManager);
@ -357,6 +359,7 @@ var BrowserApp = {
ViewportHandler.uninit(); ViewportHandler.uninit();
XPInstallObserver.uninit(); XPInstallObserver.uninit();
ConsoleAPI.uninit(); ConsoleAPI.uninit();
CharacterEncoding.uninit();
}, },
get tabs() { get tabs() {
@ -638,10 +641,6 @@ var BrowserApp = {
pref.type = "bool"; pref.type = "bool";
pref.value = pref.value == 0; pref.value = pref.value == 0;
break; break;
case "browser.menu.showCharacterEncoding":
pref.type = "bool";
pref.value = pref.value == "true";
break;
case "font.size.inflation.minTwips": case "font.size.inflation.minTwips":
pref.type = "string"; pref.type = "string";
pref.value = pref.value.toString(); pref.value = pref.value.toString();
@ -683,10 +682,6 @@ var BrowserApp = {
json.type = "int"; json.type = "int";
json.value = (json.value ? 0 : 2); json.value = (json.value ? 0 : 2);
break; break;
case "browser.menu.showCharacterEncoding":
json.type = "string";
json.value = (json.value ? "true" : "false");
break;
case "font.size.inflation.minTwips": case "font.size.inflation.minTwips":
json.type = "int"; json.type = "int";
json.value = parseInt(json.value); json.value = parseInt(json.value);
@ -3819,7 +3814,7 @@ var PermissionsHelper = {
Services.contentPrefs.removePref(aURI, aType + ".request.remember"); Services.contentPrefs.removePref(aURI, aType + ".request.remember");
} }
} }
} };
var MasterPassword = { var MasterPassword = {
pref: "privacy.masterpassword.enabled", pref: "privacy.masterpassword.enabled",
@ -3899,4 +3894,99 @@ var MasterPassword = {
} }
}); });
} }
} };
var CharacterEncoding = {
_charsets: [],
init: function init() {
Services.obs.addObserver(this, "CharEncoding:Get", false);
Services.obs.addObserver(this, "CharEncoding:Set", false);
this.sendState();
},
uninit: function uninit() {
Services.obs.removeObserver(this, "CharEncoding:Get", false);
Services.obs.removeObserver(this, "CharEncoding:Set", false);
},
observe: function observe(aSubject, aTopic, aData) {
switch (aTopic) {
case "CharEncoding:Get":
this.getEncoding();
break;
case "CharEncoding:Set":
this.setEncoding(aData);
break;
}
},
sendState: function sendState() {
let showCharEncoding = "false";
try {
showCharEncoding = Services.prefs.getComplexValue("browser.menu.showCharacterEncoding", Ci.nsIPrefLocalizedString).data;
} catch (e) { /* Optional */ }
sendMessageToJava({
gecko: {
type: "CharEncoding:State",
visible: showCharEncoding
}
});
},
getEncoding: function getEncoding() {
function normalizeCharsetCode(charsetCode) {
return charsetCode.trim().toLowerCase();
}
function getTitle(charsetCode) {
let charsetTitle = charsetCode;
try {
charsetTitle = Strings.charset.GetStringFromName(charsetCode + ".title");
} catch (e) {
dump("error: title not found for " + charsetCode);
}
return charsetTitle;
}
if (!this._charsets.length) {
let charsets = Services.prefs.getComplexValue("intl.charsetmenu.browser.static", Ci.nsIPrefLocalizedString).data;
this._charsets = charsets.split(",").map(function (charset) {
return {
code: normalizeCharsetCode(charset),
title: getTitle(charset)
};
});
}
// if document charset is not in charset options, add it
let docCharset = normalizeCharsetCode(BrowserApp.selectedBrowser.contentDocument.characterSet);
let selected = 0;
let charsetCount = this._charsets.length;
for (; selected < charsetCount && this._charsets[selected].code != docCharset; selected++);
if (selected == charsetCount) {
this._charsets.push({
code: docCharset,
title: getTitle(docCharset)
});
}
sendMessageToJava({
gecko: {
type: "CharEncoding:Data",
charsets: this._charsets,
selected: selected
}
});
},
setEncoding: function setEncoding(aEncoding) {
let browser = BrowserApp.selectedBrowser;
let docCharset = browser.docShell.QueryInterface(Ci.nsIDocCharset);
docCharset.charset = aEncoding;
browser.reload(Ci.nsIWebNavigation.LOAD_FLAGS_CHARSET_CHANGE);
},
};

View File

@ -172,7 +172,7 @@ browser.menu.showCharacterEncoding=false
# LOCALIZATION NOTE (intl.charsetmenu.browser.static): Set to a series of comma separated # LOCALIZATION NOTE (intl.charsetmenu.browser.static): Set to a series of comma separated
# values for charsets that the user can select from in the Character Encoding menu. # values for charsets that the user can select from in the Character Encoding menu.
intl.charsetmenu.browser.static=iso-8859-1,utf-8,x-gbk,big5,iso-2022-jp,shift_jis,euc-jp intl.charsetmenu.browser.static=iso-8859-1,utf-8,big5,iso-2022-jp,shift_jis,euc-jp
# Application Menu # Application Menu
appMenu.more=More appMenu.more=More