2011-11-18 10:28:17 -08:00
|
|
|
/* -*- Mode: Java; c-basic-offset: 4; tab-width: 4; indent-tabs-mode: nil; -*-
|
2012-05-21 04:12:37 -07:00
|
|
|
* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
2011-11-18 10:28:17 -08:00
|
|
|
|
|
|
|
package org.mozilla.gecko;
|
|
|
|
|
2012-11-13 09:47:05 -08:00
|
|
|
import org.mozilla.gecko.background.announcements.AnnouncementsConstants;
|
2012-08-02 17:13:40 -07:00
|
|
|
import org.mozilla.gecko.util.GeckoEventListener;
|
2013-03-15 03:52:53 -07:00
|
|
|
import org.mozilla.gecko.util.ThreadUtils;
|
2012-08-02 17:13:40 -07:00
|
|
|
|
2012-07-27 17:53:54 -07:00
|
|
|
import org.json.JSONArray;
|
|
|
|
import org.json.JSONObject;
|
2011-11-18 10:28:17 -08:00
|
|
|
|
2011-12-21 13:08:01 -08:00
|
|
|
import android.app.AlertDialog;
|
2012-07-27 17:53:54 -07:00
|
|
|
import android.app.Dialog;
|
2011-11-18 10:28:17 -08:00
|
|
|
import android.content.Context;
|
2012-07-27 17:53:54 -07:00
|
|
|
import android.content.DialogInterface;
|
2012-10-11 10:10:49 -07:00
|
|
|
import android.content.Intent;
|
|
|
|
import android.content.SharedPreferences;
|
2012-11-02 12:31:02 -07:00
|
|
|
import android.os.Build;
|
2012-07-27 17:53:54 -07:00
|
|
|
import android.os.Bundle;
|
2012-07-27 17:54:28 -07:00
|
|
|
import android.preference.CheckBoxPreference;
|
|
|
|
import android.preference.EditTextPreference;
|
|
|
|
import android.preference.ListPreference;
|
|
|
|
import android.preference.Preference;
|
|
|
|
import android.preference.Preference.OnPreferenceChangeListener;
|
|
|
|
import android.preference.PreferenceActivity;
|
|
|
|
import android.preference.PreferenceGroup;
|
2012-10-11 10:10:49 -07:00
|
|
|
import android.preference.PreferenceManager;
|
2012-07-27 17:54:28 -07:00
|
|
|
import android.preference.PreferenceScreen;
|
2012-07-27 17:53:54 -07:00
|
|
|
import android.text.Editable;
|
2011-12-21 13:08:01 -08:00
|
|
|
import android.text.InputType;
|
2012-07-27 17:53:54 -07:00
|
|
|
import android.text.TextUtils;
|
|
|
|
import android.text.TextWatcher;
|
2011-11-18 10:28:17 -08:00
|
|
|
import android.util.Log;
|
2011-11-23 11:04:47 -08:00
|
|
|
import android.view.MenuItem;
|
2011-11-18 10:28:17 -08:00
|
|
|
import android.view.View;
|
2011-12-21 13:08:01 -08:00
|
|
|
import android.widget.EditText;
|
|
|
|
import android.widget.LinearLayout;
|
2012-06-26 23:15:24 -07:00
|
|
|
import android.widget.Toast;
|
2011-12-01 17:27:11 -08:00
|
|
|
|
2012-07-27 17:53:54 -07:00
|
|
|
import java.util.ArrayList;
|
2013-04-22 14:39:30 -07:00
|
|
|
import java.util.List;
|
2011-11-18 10:28:17 -08:00
|
|
|
|
2012-07-17 17:54:54 -07:00
|
|
|
public class GeckoPreferences
|
2011-11-18 10:28:17 -08:00
|
|
|
extends PreferenceActivity
|
2013-01-29 13:59:42 -08:00
|
|
|
implements OnPreferenceChangeListener, GeckoEventListener, GeckoActivityStatus
|
2011-11-18 10:28:17 -08:00
|
|
|
{
|
|
|
|
private static final String LOGTAG = "GeckoPreferences";
|
|
|
|
|
2012-02-14 16:20:07 -08:00
|
|
|
private ArrayList<String> mPreferencesList;
|
2011-11-18 10:28:17 -08:00
|
|
|
private PreferenceScreen mPreferenceScreen;
|
2012-01-23 13:47:48 -08:00
|
|
|
private static boolean sIsCharEncodingEnabled = false;
|
2012-06-26 23:15:24 -07:00
|
|
|
private static final String NON_PREF_PREFIX = "android.not_a_preference.";
|
2013-04-22 14:39:30 -07:00
|
|
|
private boolean mInitialized = false;
|
2011-11-18 10:28:17 -08:00
|
|
|
|
2012-10-04 12:05:05 -07:00
|
|
|
// These match keys in resources/xml/preferences.xml.in.
|
2013-04-09 17:10:47 -07:00
|
|
|
private static String PREFS_ANNOUNCEMENTS_ENABLED = NON_PREF_PREFIX + "privacy.announcements.enabled";
|
|
|
|
private static String PREFS_CATEGORY_GENERAL = "category_general";
|
|
|
|
private static String PREFS_CATEGORY_PRIVACY = "category_privacy";
|
|
|
|
private static String PREFS_MENU_CHAR_ENCODING = "browser.menu.showCharacterEncoding";
|
|
|
|
private static String PREFS_MP_ENABLED = "privacy.masterpassword.enabled";
|
|
|
|
private static String PREFS_TELEMETRY_ENABLED = "toolkit.telemetry.enabled";
|
|
|
|
private static String PREFS_TELEMETRY_ENABLED_PRERELEASE = "toolkit.telemetry.enabledPreRelease";
|
|
|
|
private static String PREFS_UPDATER_AUTODOWNLOAD = "app.update.autodownload";
|
2013-04-25 23:43:03 -07:00
|
|
|
private static String PREFS_TITLEBAR_MODE = "android.not_a_preference.privacy.titlebar";
|
2012-10-04 12:05:05 -07:00
|
|
|
|
2011-11-18 10:28:17 -08:00
|
|
|
@Override
|
|
|
|
protected void onCreate(Bundle savedInstanceState) {
|
|
|
|
super.onCreate(savedInstanceState);
|
2013-04-22 14:39:30 -07:00
|
|
|
|
|
|
|
// If this is a smaller screen
|
|
|
|
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.HONEYCOMB || !onIsMultiPane()) {
|
|
|
|
addPreferencesFromResource(R.xml.preferences_general);
|
|
|
|
addPreferencesFromResource(R.xml.preferences_content);
|
|
|
|
addPreferencesFromResource(R.xml.preferences_privacy);
|
|
|
|
}
|
|
|
|
|
2012-08-02 18:38:45 -07:00
|
|
|
registerEventListener("Sanitize:Finished");
|
2012-11-02 12:31:02 -07:00
|
|
|
|
|
|
|
if (Build.VERSION.SDK_INT >= 14)
|
|
|
|
getActionBar().setHomeButtonEnabled(true);
|
2013-04-22 14:39:30 -07:00
|
|
|
}
|
2013-04-09 17:10:47 -07:00
|
|
|
|
2013-04-22 14:39:30 -07:00
|
|
|
@Override
|
|
|
|
public void onBuildHeaders(List<Header> target) {
|
|
|
|
if (onIsMultiPane())
|
|
|
|
loadHeadersFromResource(R.xml.preference_headers, target);
|
2012-09-17 11:22:28 -07:00
|
|
|
}
|
2012-02-14 16:20:07 -08:00
|
|
|
|
2012-09-17 11:22:28 -07:00
|
|
|
@Override
|
|
|
|
public void onWindowFocusChanged(boolean hasFocus) {
|
2013-04-22 14:39:30 -07:00
|
|
|
if (!hasFocus || mInitialized)
|
2012-02-14 16:20:07 -08:00
|
|
|
return;
|
|
|
|
|
2013-04-22 14:39:30 -07:00
|
|
|
mInitialized = true;
|
|
|
|
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.HONEYCOMB || !onIsMultiPane()) {
|
|
|
|
PreferenceScreen screen = getPreferenceScreen();
|
|
|
|
setupPreferences(screen);
|
|
|
|
}
|
2011-12-01 17:27:11 -08:00
|
|
|
}
|
2011-11-18 10:28:17 -08:00
|
|
|
|
2011-12-01 17:27:11 -08:00
|
|
|
@Override
|
|
|
|
protected void onDestroy() {
|
|
|
|
super.onDestroy();
|
2012-08-02 18:38:45 -07:00
|
|
|
unregisterEventListener("Sanitize:Finished");
|
2011-12-01 17:27:11 -08:00
|
|
|
}
|
|
|
|
|
2013-01-29 13:59:42 -08:00
|
|
|
@Override
|
|
|
|
public void onPause() {
|
|
|
|
super.onPause();
|
|
|
|
|
|
|
|
if (getApplication() instanceof GeckoApplication) {
|
|
|
|
((GeckoApplication) getApplication()).onActivityPause(this);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onResume() {
|
|
|
|
super.onResume();
|
|
|
|
|
|
|
|
if (getApplication() instanceof GeckoApplication) {
|
|
|
|
((GeckoApplication) getApplication()).onActivityResume(this);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-02-26 21:48:00 -08:00
|
|
|
@Override
|
2011-12-01 17:27:11 -08:00
|
|
|
public void handleMessage(String event, JSONObject message) {
|
|
|
|
try {
|
2012-09-17 11:22:28 -07:00
|
|
|
if (event.equals("Sanitize:Finished")) {
|
2012-06-26 23:15:24 -07:00
|
|
|
boolean success = message.getBoolean("success");
|
|
|
|
final int stringRes = success ? R.string.private_data_success : R.string.private_data_fail;
|
|
|
|
final Context context = this;
|
2013-03-15 03:52:53 -07:00
|
|
|
ThreadUtils.postToUiThread(new Runnable () {
|
2013-02-26 21:48:00 -08:00
|
|
|
@Override
|
2012-06-26 23:15:24 -07:00
|
|
|
public void run() {
|
|
|
|
Toast.makeText(context, stringRes, Toast.LENGTH_SHORT).show();
|
|
|
|
}
|
|
|
|
});
|
2011-12-01 17:27:11 -08:00
|
|
|
}
|
|
|
|
} catch (Exception e) {
|
|
|
|
Log.e(LOGTAG, "Exception handling message \"" + event + "\":", e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-04-22 14:39:30 -07:00
|
|
|
public void setupPreferences(PreferenceGroup prefs) {
|
|
|
|
ArrayList<String> list = new ArrayList<String>();
|
|
|
|
setupPreferences(prefs, list);
|
|
|
|
getGeckoPreferences(prefs, list);
|
|
|
|
}
|
|
|
|
|
|
|
|
private void setupPreferences(PreferenceGroup preferences, ArrayList<String> prefs) {
|
|
|
|
for (int i = 0; i < preferences.getPreferenceCount(); i++) {
|
2011-11-18 10:28:17 -08:00
|
|
|
Preference pref = preferences.getPreference(i);
|
2013-04-22 14:39:30 -07:00
|
|
|
if (pref instanceof PreferenceGroup) {
|
|
|
|
setupPreferences((PreferenceGroup)pref, prefs);
|
|
|
|
} else {
|
|
|
|
String key = pref.getKey();
|
|
|
|
|
2011-11-18 10:28:17 -08:00
|
|
|
pref.setOnPreferenceChangeListener(this);
|
2013-04-22 14:39:30 -07:00
|
|
|
if (PREFS_UPDATER_AUTODOWNLOAD.equals(key) && !AppConstants.MOZ_UPDATER) {
|
|
|
|
preferences.removePreference(pref);
|
|
|
|
i--;
|
|
|
|
continue;
|
|
|
|
} else if (PREFS_TELEMETRY_ENABLED.equals(key)) {
|
|
|
|
if (AppConstants.MOZ_TELEMETRY_REPORTING) {
|
|
|
|
if (AppConstants.MOZ_TELEMETRY_ON_BY_DEFAULT) {
|
|
|
|
pref.setKey(PREFS_TELEMETRY_ENABLED_PRERELEASE);
|
|
|
|
key = PREFS_TELEMETRY_ENABLED_PRERELEASE;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
preferences.removePreference(pref);
|
|
|
|
i--;
|
|
|
|
continue;
|
|
|
|
}
|
2013-04-25 23:43:03 -07:00
|
|
|
} else if (PREFS_TITLEBAR_MODE.equals(key)) {
|
|
|
|
setupTitlebarPref((ListPreference)pref);
|
2013-04-22 14:39:30 -07:00
|
|
|
}
|
2012-06-26 23:15:24 -07:00
|
|
|
|
|
|
|
// Some Preference UI elements are not actually preferences,
|
|
|
|
// but they require a key to work correctly. For example,
|
|
|
|
// "Clear private data" requires a key for its state to be
|
|
|
|
// saved when the orientation changes. It uses the
|
|
|
|
// "android.not_a_preference.privacy.clear" key - which doesn't
|
|
|
|
// exist in Gecko - to satisfy this requirement.
|
2012-07-19 18:08:40 -07:00
|
|
|
if (key != null && !key.startsWith(NON_PREF_PREFIX)) {
|
2013-04-22 14:39:30 -07:00
|
|
|
prefs.add(key);
|
2012-07-19 18:08:40 -07:00
|
|
|
}
|
2011-11-18 10:28:17 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2011-12-08 09:37:57 -08:00
|
|
|
|
2011-11-23 11:04:47 -08:00
|
|
|
@Override
|
|
|
|
public boolean onOptionsItemSelected(MenuItem item) {
|
|
|
|
switch (item.getItemId()) {
|
|
|
|
case android.R.id.home:
|
|
|
|
finish();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return super.onOptionsItemSelected(item);
|
|
|
|
}
|
2011-11-18 10:28:17 -08:00
|
|
|
|
2011-12-21 13:08:01 -08:00
|
|
|
final private int DIALOG_CREATE_MASTER_PASSWORD = 0;
|
|
|
|
final private int DIALOG_REMOVE_MASTER_PASSWORD = 1;
|
|
|
|
|
2012-01-23 13:47:48 -08:00
|
|
|
public static void setCharEncodingState(boolean enabled) {
|
|
|
|
sIsCharEncodingEnabled = enabled;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static boolean getCharEncodingState() {
|
|
|
|
return sIsCharEncodingEnabled;
|
|
|
|
}
|
|
|
|
|
2012-10-11 10:10:49 -07:00
|
|
|
/**
|
|
|
|
* Broadcast an intent with <code>pref</code>, <code>branch</code>, and
|
|
|
|
* <code>enabled</code> extras. This is intended to represent the
|
|
|
|
* notification of a preference value to observers.
|
|
|
|
*/
|
|
|
|
public static void broadcastPrefAction(final Context context,
|
|
|
|
final String action,
|
|
|
|
final String pref,
|
|
|
|
final boolean value) {
|
|
|
|
final Intent intent = new Intent(action);
|
|
|
|
intent.setAction(action);
|
|
|
|
intent.putExtra("pref", pref);
|
|
|
|
intent.putExtra("branch", GeckoApp.PREFS_NAME);
|
|
|
|
intent.putExtra("enabled", value);
|
|
|
|
Log.d(LOGTAG, "Broadcast: " + action + ", " + pref + ", " + GeckoApp.PREFS_NAME + ", " + value);
|
|
|
|
context.sendBroadcast(intent);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Broadcast the provided value as the value of the
|
|
|
|
* <code>PREFS_ANNOUNCEMENTS_ENABLED</code> pref.
|
|
|
|
*/
|
|
|
|
public static void broadcastAnnouncementsPref(final Context context, final boolean value) {
|
|
|
|
broadcastPrefAction(context,
|
2012-11-13 09:47:05 -08:00
|
|
|
AnnouncementsConstants.ACTION_ANNOUNCEMENTS_PREF,
|
2012-10-11 10:10:49 -07:00
|
|
|
PREFS_ANNOUNCEMENTS_ENABLED,
|
|
|
|
value);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Broadcast the current value of the
|
|
|
|
* <code>PREFS_ANNOUNCEMENTS_ENABLED</code> pref.
|
|
|
|
*/
|
|
|
|
public static void broadcastAnnouncementsPref(final Context context) {
|
2012-11-13 09:47:05 -08:00
|
|
|
final boolean value = getBooleanPref(context, PREFS_ANNOUNCEMENTS_ENABLED, true);
|
|
|
|
broadcastAnnouncementsPref(context, value);
|
2012-10-11 10:10:49 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return the value of the named preference in the default preferences file.
|
|
|
|
*
|
|
|
|
* This corresponds to the storage that backs preferences.xml.
|
|
|
|
* @param context a <code>Context</code>; the
|
|
|
|
* <code>PreferenceActivity</code> will suffice, but this
|
|
|
|
* method is intended to be called from other contexts
|
|
|
|
* within the application, not just this <code>Activity</code>.
|
|
|
|
* @param name the name of the preference to retrieve.
|
|
|
|
* @param def the default value to return if the preference is not present.
|
|
|
|
* @return the value of the preference, or the default.
|
|
|
|
*/
|
|
|
|
public static boolean getBooleanPref(final Context context, final String name, boolean def) {
|
|
|
|
final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
|
|
|
|
return prefs.getBoolean(name, def);
|
|
|
|
}
|
|
|
|
|
2011-11-18 10:28:17 -08:00
|
|
|
@Override
|
|
|
|
public boolean onPreferenceChange(Preference preference, Object newValue) {
|
|
|
|
String prefName = preference.getKey();
|
2013-04-22 14:39:30 -07:00
|
|
|
if (PREFS_MP_ENABLED.equals(prefName)) {
|
2012-10-04 12:05:05 -07:00
|
|
|
showDialog((Boolean) newValue ? DIALOG_CREATE_MASTER_PASSWORD : DIALOG_REMOVE_MASTER_PASSWORD);
|
2011-12-21 13:08:01 -08:00
|
|
|
return false;
|
2013-04-22 14:39:30 -07:00
|
|
|
} else if (PREFS_MENU_CHAR_ENCODING.equals(prefName)) {
|
2012-01-23 13:47:48 -08:00
|
|
|
setCharEncodingState(((String) newValue).equals("true"));
|
2013-04-22 14:39:30 -07:00
|
|
|
} else if (PREFS_ANNOUNCEMENTS_ENABLED.equals(prefName)) {
|
2012-10-11 10:10:49 -07:00
|
|
|
// Send a broadcast intent to the product announcements service, either to start or
|
|
|
|
// to stop the repeated background checks.
|
|
|
|
broadcastAnnouncementsPref(GeckoApp.mAppContext, ((Boolean) newValue).booleanValue());
|
2013-04-22 14:39:30 -07:00
|
|
|
} else if (PREFS_UPDATER_AUTODOWNLOAD.equals(prefName)) {
|
2012-10-15 06:56:42 -07:00
|
|
|
org.mozilla.gecko.updater.UpdateServiceHelper.registerForUpdates(GeckoApp.mAppContext, (String)newValue);
|
2011-12-21 13:08:01 -08:00
|
|
|
}
|
|
|
|
|
2012-09-17 11:22:28 -07:00
|
|
|
if (!TextUtils.isEmpty(prefName)) {
|
|
|
|
PrefsHelper.setPref(prefName, newValue);
|
|
|
|
}
|
2011-12-13 14:28:45 -08:00
|
|
|
if (preference instanceof ListPreference) {
|
|
|
|
// We need to find the entry for the new value
|
|
|
|
int newIndex = ((ListPreference)preference).findIndexOfValue((String) newValue);
|
|
|
|
CharSequence newEntry = ((ListPreference)preference).getEntries()[newIndex];
|
|
|
|
((ListPreference)preference).setSummary(newEntry);
|
2012-07-19 18:08:40 -07:00
|
|
|
} else if (preference instanceof LinkPreference) {
|
2011-12-08 09:37:57 -08:00
|
|
|
finish();
|
2012-07-19 18:08:40 -07:00
|
|
|
} else if (preference instanceof FontSizePreference) {
|
|
|
|
final FontSizePreference fontSizePref = (FontSizePreference) preference;
|
|
|
|
fontSizePref.setSummary(fontSizePref.getSavedFontSizeName());
|
|
|
|
}
|
2011-11-18 10:28:17 -08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2011-12-21 13:08:01 -08:00
|
|
|
private EditText getTextBox(int aHintText) {
|
|
|
|
EditText input = new EditText(GeckoApp.mAppContext);
|
|
|
|
int inputtype = InputType.TYPE_CLASS_TEXT;
|
|
|
|
inputtype |= InputType.TYPE_TEXT_VARIATION_PASSWORD | InputType.TYPE_TEXT_FLAG_NO_SUGGESTIONS;
|
|
|
|
input.setInputType(inputtype);
|
|
|
|
|
|
|
|
String hint = getResources().getString(aHintText);
|
|
|
|
input.setHint(aHintText);
|
|
|
|
return input;
|
|
|
|
}
|
|
|
|
|
2012-07-17 17:54:54 -07:00
|
|
|
private class PasswordTextWatcher implements TextWatcher {
|
2011-12-21 13:08:01 -08:00
|
|
|
EditText input1 = null;
|
|
|
|
EditText input2 = null;
|
2012-01-03 15:53:29 -08:00
|
|
|
AlertDialog dialog = null;
|
2011-12-21 13:08:01 -08:00
|
|
|
|
2012-01-03 15:53:29 -08:00
|
|
|
PasswordTextWatcher(EditText aInput1, EditText aInput2, AlertDialog aDialog) {
|
2011-12-21 13:08:01 -08:00
|
|
|
input1 = aInput1;
|
|
|
|
input2 = aInput2;
|
2012-01-03 15:53:29 -08:00
|
|
|
dialog = aDialog;
|
2011-12-21 13:08:01 -08:00
|
|
|
}
|
|
|
|
|
2013-02-26 21:48:00 -08:00
|
|
|
@Override
|
2011-12-21 13:08:01 -08:00
|
|
|
public void afterTextChanged(Editable s) {
|
2012-01-03 15:53:29 -08:00
|
|
|
if (dialog == null)
|
2011-12-21 13:08:01 -08:00
|
|
|
return;
|
|
|
|
|
|
|
|
String text1 = input1.getText().toString();
|
|
|
|
String text2 = input2.getText().toString();
|
2012-01-18 08:50:11 -08:00
|
|
|
boolean disabled = TextUtils.isEmpty(text1) || TextUtils.isEmpty(text2) || !text1.equals(text2);
|
|
|
|
dialog.getButton(DialogInterface.BUTTON_POSITIVE).setEnabled(!disabled);
|
2011-12-21 13:08:01 -08:00
|
|
|
}
|
|
|
|
|
2013-02-26 21:48:00 -08:00
|
|
|
@Override
|
2011-12-21 13:08:01 -08:00
|
|
|
public void beforeTextChanged(CharSequence s, int start, int count, int after) { }
|
2013-02-26 21:48:00 -08:00
|
|
|
@Override
|
2011-12-21 13:08:01 -08:00
|
|
|
public void onTextChanged(CharSequence s, int start, int before, int count) { }
|
|
|
|
}
|
|
|
|
|
2012-12-03 12:22:05 -08:00
|
|
|
private class EmptyTextWatcher implements TextWatcher {
|
|
|
|
EditText input = null;
|
|
|
|
AlertDialog dialog = null;
|
|
|
|
|
|
|
|
EmptyTextWatcher(EditText aInput, AlertDialog aDialog) {
|
|
|
|
input = aInput;
|
|
|
|
dialog = aDialog;
|
|
|
|
}
|
|
|
|
|
2013-02-26 21:48:00 -08:00
|
|
|
@Override
|
2012-12-03 12:22:05 -08:00
|
|
|
public void afterTextChanged(Editable s) {
|
|
|
|
if (dialog == null)
|
|
|
|
return;
|
|
|
|
|
|
|
|
String text = input.getText().toString();
|
|
|
|
boolean disabled = TextUtils.isEmpty(text);
|
|
|
|
dialog.getButton(DialogInterface.BUTTON_POSITIVE).setEnabled(!disabled);
|
|
|
|
}
|
|
|
|
|
2013-02-26 21:48:00 -08:00
|
|
|
@Override
|
2012-12-03 12:22:05 -08:00
|
|
|
public void beforeTextChanged(CharSequence s, int start, int count, int after) { }
|
2013-02-26 21:48:00 -08:00
|
|
|
@Override
|
2012-12-03 12:22:05 -08:00
|
|
|
public void onTextChanged(CharSequence s, int start, int before, int count) { }
|
|
|
|
}
|
|
|
|
|
2013-02-26 21:48:00 -08:00
|
|
|
@Override
|
2011-12-21 13:08:01 -08:00
|
|
|
protected Dialog onCreateDialog(int id) {
|
|
|
|
AlertDialog.Builder builder = new AlertDialog.Builder(this);
|
|
|
|
LinearLayout linearLayout = new LinearLayout(this);
|
|
|
|
linearLayout.setOrientation(LinearLayout.VERTICAL);
|
2012-01-03 15:53:29 -08:00
|
|
|
AlertDialog dialog = null;
|
2011-12-21 13:08:01 -08:00
|
|
|
switch(id) {
|
|
|
|
case DIALOG_CREATE_MASTER_PASSWORD:
|
|
|
|
final EditText input1 = getTextBox(R.string.masterpassword_password);
|
|
|
|
final EditText input2 = getTextBox(R.string.masterpassword_confirm);
|
|
|
|
linearLayout.addView(input1);
|
|
|
|
linearLayout.addView(input2);
|
|
|
|
|
|
|
|
builder.setTitle(R.string.masterpassword_create_title)
|
|
|
|
.setView((View)linearLayout)
|
|
|
|
.setPositiveButton(R.string.button_ok, new DialogInterface.OnClickListener() {
|
2013-02-26 21:48:00 -08:00
|
|
|
@Override
|
2011-12-21 13:08:01 -08:00
|
|
|
public void onClick(DialogInterface dialog, int which) {
|
|
|
|
JSONObject jsonPref = new JSONObject();
|
|
|
|
try {
|
2012-10-04 12:05:05 -07:00
|
|
|
jsonPref.put("name", PREFS_MP_ENABLED);
|
2011-12-21 13:08:01 -08:00
|
|
|
jsonPref.put("type", "string");
|
|
|
|
jsonPref.put("value", input1.getText().toString());
|
|
|
|
|
2012-02-08 23:18:27 -08:00
|
|
|
GeckoEvent event = GeckoEvent.createBroadcastEvent("Preferences:Set", jsonPref.toString());
|
2011-12-21 13:08:01 -08:00
|
|
|
GeckoAppShell.sendEventToGecko(event);
|
|
|
|
} catch(Exception ex) {
|
2012-10-04 12:05:05 -07:00
|
|
|
Log.e(LOGTAG, "Error setting master password", ex);
|
2011-12-21 13:08:01 -08:00
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.setNegativeButton(R.string.button_cancel, new DialogInterface.OnClickListener() {
|
2013-02-26 21:48:00 -08:00
|
|
|
@Override
|
2011-12-21 13:08:01 -08:00
|
|
|
public void onClick(DialogInterface dialog, int which) {
|
2012-01-03 15:53:29 -08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
dialog = builder.create();
|
2012-01-18 08:50:11 -08:00
|
|
|
dialog.setOnShowListener(new DialogInterface.OnShowListener() {
|
2013-02-26 21:48:00 -08:00
|
|
|
@Override
|
2012-01-18 08:50:11 -08:00
|
|
|
public void onShow(DialogInterface dialog) {
|
2011-12-21 13:08:01 -08:00
|
|
|
input1.setText("");
|
|
|
|
input2.setText("");
|
2012-01-03 15:53:29 -08:00
|
|
|
input1.requestFocus();
|
2011-12-21 13:08:01 -08:00
|
|
|
}
|
|
|
|
});
|
2012-01-03 15:53:29 -08:00
|
|
|
|
|
|
|
PasswordTextWatcher watcher = new PasswordTextWatcher(input1, input2, dialog);
|
|
|
|
input1.addTextChangedListener((TextWatcher)watcher);
|
|
|
|
input2.addTextChangedListener((TextWatcher)watcher);
|
|
|
|
|
2011-12-21 13:08:01 -08:00
|
|
|
break;
|
|
|
|
case DIALOG_REMOVE_MASTER_PASSWORD:
|
|
|
|
final EditText input = getTextBox(R.string.masterpassword_password);
|
|
|
|
linearLayout.addView(input);
|
|
|
|
|
|
|
|
builder.setTitle(R.string.masterpassword_remove_title)
|
|
|
|
.setView((View)linearLayout)
|
|
|
|
.setPositiveButton(R.string.button_ok, new DialogInterface.OnClickListener() {
|
2013-02-26 21:48:00 -08:00
|
|
|
@Override
|
2011-12-21 13:08:01 -08:00
|
|
|
public void onClick(DialogInterface dialog, int which) {
|
2012-10-04 12:05:05 -07:00
|
|
|
PrefsHelper.setPref(PREFS_MP_ENABLED, input.getText().toString());
|
2011-12-21 13:08:01 -08:00
|
|
|
}
|
|
|
|
})
|
2012-01-03 15:53:29 -08:00
|
|
|
.setNegativeButton(R.string.button_cancel, new DialogInterface.OnClickListener() {
|
2013-02-26 21:48:00 -08:00
|
|
|
@Override
|
2011-12-21 13:08:01 -08:00
|
|
|
public void onClick(DialogInterface dialog, int which) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
});
|
2012-01-03 15:53:29 -08:00
|
|
|
dialog = builder.create();
|
|
|
|
dialog.setOnDismissListener(new DialogInterface.OnDismissListener() {
|
2013-02-26 21:48:00 -08:00
|
|
|
@Override
|
2012-01-03 15:53:29 -08:00
|
|
|
public void onDismiss(DialogInterface dialog) {
|
|
|
|
input.setText("");
|
|
|
|
}
|
|
|
|
});
|
2012-12-03 12:22:05 -08:00
|
|
|
dialog.setOnShowListener(new DialogInterface.OnShowListener() {
|
2013-02-26 21:48:00 -08:00
|
|
|
@Override
|
2012-12-03 12:22:05 -08:00
|
|
|
public void onShow(DialogInterface dialog) {
|
|
|
|
input.setText("");
|
|
|
|
}
|
|
|
|
});
|
|
|
|
input.addTextChangedListener(new EmptyTextWatcher(input, dialog));
|
2011-12-21 13:08:01 -08:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return null;
|
|
|
|
}
|
2012-01-03 15:53:29 -08:00
|
|
|
|
|
|
|
return dialog;
|
2011-12-21 13:08:01 -08:00
|
|
|
}
|
|
|
|
|
2012-09-17 11:22:28 -07:00
|
|
|
// Initialize preferences by requesting the preference values from Gecko
|
2013-04-22 14:39:30 -07:00
|
|
|
private void getGeckoPreferences(final PreferenceGroup screen, ArrayList<String> prefs) {
|
|
|
|
JSONArray jsonPrefs = new JSONArray(prefs);
|
|
|
|
|
2012-09-17 11:22:28 -07:00
|
|
|
PrefsHelper.getPrefs(jsonPrefs, new PrefsHelper.PrefHandlerBase() {
|
|
|
|
private Preference getField(String prefName) {
|
2013-04-22 14:39:30 -07:00
|
|
|
return screen.findPreference(prefName);
|
2011-12-01 17:27:11 -08:00
|
|
|
}
|
2011-11-18 10:28:17 -08:00
|
|
|
|
2013-04-22 14:39:30 -07:00
|
|
|
@Override
|
|
|
|
public void prefValue(String prefName, final boolean value) {
|
2012-09-17 11:22:28 -07:00
|
|
|
final Preference pref = getField(prefName);
|
|
|
|
if (pref instanceof CheckBoxPreference) {
|
2013-03-15 03:52:53 -07:00
|
|
|
ThreadUtils.postToUiThread(new Runnable() {
|
2013-02-26 21:48:00 -08:00
|
|
|
@Override
|
2011-11-18 10:28:17 -08:00
|
|
|
public void run() {
|
|
|
|
if (((CheckBoxPreference)pref).isChecked() != value)
|
|
|
|
((CheckBoxPreference)pref).setChecked(value);
|
|
|
|
}
|
|
|
|
});
|
2012-09-17 11:22:28 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-04-22 14:39:30 -07:00
|
|
|
@Override
|
|
|
|
public void prefValue(String prefName, final String value) {
|
2012-09-17 11:22:28 -07:00
|
|
|
final Preference pref = getField(prefName);
|
|
|
|
if (pref instanceof EditTextPreference) {
|
2013-03-15 03:52:53 -07:00
|
|
|
ThreadUtils.postToUiThread(new Runnable() {
|
2013-02-26 21:48:00 -08:00
|
|
|
@Override
|
2011-11-18 10:28:17 -08:00
|
|
|
public void run() {
|
|
|
|
((EditTextPreference)pref).setText(value);
|
|
|
|
}
|
|
|
|
});
|
2012-09-17 11:22:28 -07:00
|
|
|
} else if (pref instanceof ListPreference) {
|
2013-03-15 03:52:53 -07:00
|
|
|
ThreadUtils.postToUiThread(new Runnable() {
|
2013-02-26 21:48:00 -08:00
|
|
|
@Override
|
2011-11-18 10:28:17 -08:00
|
|
|
public void run() {
|
|
|
|
((ListPreference)pref).setValue(value);
|
2011-12-13 14:28:45 -08:00
|
|
|
// Set the summary string to the current entry
|
|
|
|
CharSequence selectedEntry = ((ListPreference)pref).getEntry();
|
|
|
|
((ListPreference)pref).setSummary(selectedEntry);
|
2011-11-18 10:28:17 -08:00
|
|
|
}
|
|
|
|
});
|
2012-07-19 18:08:40 -07:00
|
|
|
} else if (pref instanceof FontSizePreference) {
|
2012-07-19 18:08:40 -07:00
|
|
|
final FontSizePreference fontSizePref = (FontSizePreference) pref;
|
2012-09-17 11:22:28 -07:00
|
|
|
fontSizePref.setSavedFontSize(value);
|
2012-07-19 18:08:40 -07:00
|
|
|
final String fontSizeName = fontSizePref.getSavedFontSizeName();
|
2013-03-15 03:52:53 -07:00
|
|
|
ThreadUtils.postToUiThread(new Runnable() {
|
2013-02-26 21:48:00 -08:00
|
|
|
@Override
|
2012-07-19 18:08:40 -07:00
|
|
|
public void run() {
|
2012-07-19 18:08:40 -07:00
|
|
|
fontSizePref.setSummary(fontSizeName); // Ex: "Small".
|
2012-07-19 18:08:40 -07:00
|
|
|
}
|
|
|
|
});
|
2011-11-18 10:28:17 -08:00
|
|
|
}
|
|
|
|
}
|
2011-12-14 13:36:27 -08:00
|
|
|
|
2013-04-22 14:39:30 -07:00
|
|
|
@Override
|
|
|
|
public void finish() {
|
2012-09-17 11:22:28 -07:00
|
|
|
// enable all preferences once we have them from gecko
|
2013-03-15 03:52:53 -07:00
|
|
|
ThreadUtils.postToUiThread(new Runnable() {
|
2013-02-26 21:48:00 -08:00
|
|
|
@Override
|
2012-09-17 11:22:28 -07:00
|
|
|
public void run() {
|
2013-04-22 14:39:30 -07:00
|
|
|
screen.setEnabled(true);
|
2012-09-17 11:22:28 -07:00
|
|
|
}
|
|
|
|
});
|
2011-12-01 17:27:11 -08:00
|
|
|
}
|
2012-09-17 11:22:28 -07:00
|
|
|
});
|
2011-11-18 10:28:17 -08:00
|
|
|
}
|
2012-08-02 18:38:45 -07:00
|
|
|
|
|
|
|
private void registerEventListener(String event) {
|
|
|
|
GeckoAppShell.getEventDispatcher().registerEventListener(event, this);
|
|
|
|
}
|
|
|
|
|
|
|
|
private void unregisterEventListener(String event) {
|
|
|
|
GeckoAppShell.getEventDispatcher().unregisterEventListener(event, this);
|
|
|
|
}
|
2013-01-29 13:59:42 -08:00
|
|
|
|
2013-02-26 21:48:00 -08:00
|
|
|
@Override
|
2013-01-29 13:59:42 -08:00
|
|
|
public boolean isGeckoActivityOpened() {
|
|
|
|
return false;
|
|
|
|
}
|
2013-04-25 23:43:03 -07:00
|
|
|
|
|
|
|
private void setupTitlebarPref(final ListPreference pref) {
|
|
|
|
final SharedPreferences settings = getSharedPreferences(BrowserToolbar.PREFS_NAME, 0);
|
|
|
|
boolean value = settings.getBoolean(BrowserToolbar.PREFS_SHOW_URL, false);
|
|
|
|
|
|
|
|
final String[] entries = new String[] {
|
|
|
|
getResources().getString(R.string.pref_titlebar_mode_url),
|
|
|
|
getResources().getString(R.string.pref_titlebar_mode_title)
|
|
|
|
};
|
|
|
|
pref.setEntries(entries);
|
|
|
|
pref.setEntryValues(entries);
|
|
|
|
pref.setValueIndex(value ? 0 : 1);
|
|
|
|
pref.setSummary(value ? entries[0] : entries[1]);
|
|
|
|
|
|
|
|
pref.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {
|
|
|
|
@Override
|
|
|
|
public boolean onPreferenceChange(Preference preference, Object newValue) {
|
|
|
|
settings.edit()
|
|
|
|
.putBoolean(BrowserToolbar.PREFS_SHOW_URL, newValue.toString().equals(entries[0]))
|
|
|
|
.commit();
|
|
|
|
pref.setSummary(newValue.toString());
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2011-11-18 10:28:17 -08:00
|
|
|
}
|