package com.android.browser;
-import com.google.android.providers.GoogleSettings.Partner;
+import com.android.browser.search.SearchEngine;
+import com.android.browser.search.SearchEngines;
+import android.app.ActivityManager;
+import android.content.ComponentName;
import android.content.ContentResolver;
import android.content.Context;
import android.content.pm.ActivityInfo;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
+import android.database.ContentObserver;
+import android.os.Handler;
import android.preference.PreferenceActivity;
import android.preference.PreferenceScreen;
+import android.provider.Settings;
+import android.util.Log;
import android.webkit.CookieManager;
import android.webkit.GeolocationPermissions;
import android.webkit.ValueCallback;
// NOTE: these defaults need to be kept in sync with the XML
// until the performance of PreferenceManager.setDefaultValues()
// is improved.
- private boolean loadsImagesAutomatically = true;
- private boolean javaScriptEnabled = true;
- private boolean pluginsEnabled = true;
- private boolean javaScriptCanOpenWindowsAutomatically = false;
- private boolean showSecurityWarnings = true;
- private boolean rememberPasswords = true;
- private boolean saveFormData = true;
- private boolean openInBackground = false;
+ // Note: boolean variables are set inside reset function.
+ private boolean loadsImagesAutomatically;
+ private boolean javaScriptEnabled;
+ private WebSettings.PluginState pluginState;
+ private boolean javaScriptCanOpenWindowsAutomatically;
+ private boolean showSecurityWarnings;
+ private boolean rememberPasswords;
+ private boolean saveFormData;
+ private boolean openInBackground;
private String defaultTextEncodingName;
private String homeUrl = "";
- private boolean loginInitialized = false;
- private boolean autoFitPage = true;
- private boolean landscapeOnly = false;
- private boolean loadsPageInOverviewMode = true;
- private boolean showDebugSettings = false;
+ private SearchEngine searchEngine;
+ private boolean showSearchSuggestions;
+ private boolean autoFitPage;
+ private boolean landscapeOnly;
+ private boolean loadsPageInOverviewMode;
+ private boolean showDebugSettings;
// HTML5 API flags
- private boolean appCacheEnabled = true;
- private boolean databaseEnabled = true;
- private boolean domStorageEnabled = true;
- private boolean geolocationEnabled = true;
- private boolean workersEnabled = true; // only affects V8. JSC does not have a similar setting
+ private boolean appCacheEnabled;
+ private boolean databaseEnabled;
+ private boolean domStorageEnabled;
+ private boolean geolocationEnabled;
+ private boolean workersEnabled; // only affects V8. JSC does not have a similar setting
// HTML5 API configuration params
private long appCacheMaxSize = Long.MAX_VALUE;
private String appCachePath; // default value set in loadFromDb().
WebSettings.TextSize.NORMAL;
private static WebSettings.ZoomDensity zoomDensity =
WebSettings.ZoomDensity.MEDIUM;
+ private static int pageCacheCapacity;
// Preference keys that are used outside this class
public final static String PREF_CLEAR_CACHE = "privacy_clear_cache";
public final static String PREF_CLEAR_COOKIES = "privacy_clear_cookies";
public final static String PREF_CLEAR_HISTORY = "privacy_clear_history";
public final static String PREF_HOMEPAGE = "homepage";
+ public final static String PREF_SEARCH_ENGINE = "search_engine";
+ public final static String PREF_SHOW_SEARCH_SUGGESTIONS = "show_search_suggestions";
public final static String PREF_CLEAR_FORM_DATA =
"privacy_clear_form_data";
public final static String PREF_CLEAR_PASSWORDS =
"privacy_clear_geolocation_access";
private static final String DESKTOP_USERAGENT = "Mozilla/5.0 (Macintosh; " +
- "U; Intel Mac OS X 10_5_7; en-us) AppleWebKit/530.17 (KHTML, " +
- "like Gecko) Version/4.0 Safari/530.17";
+ "U; Intel Mac OS X 10_6_3; en-us) AppleWebKit/533.16 (KHTML, " +
+ "like Gecko) Version/5.0 Safari/533.16";
private static final String IPHONE_USERAGENT = "Mozilla/5.0 (iPhone; U; " +
- "CPU iPhone OS 3_0 like Mac OS X; en-us) AppleWebKit/528.18 " +
- "(KHTML, like Gecko) Version/4.0 Mobile/7A341 Safari/528.16";
+ "CPU iPhone OS 4_0 like Mac OS X; en-us) AppleWebKit/532.9 " +
+ "(KHTML, like Gecko) Version/4.0.5 Mobile/8A293 Safari/6531.22.7";
+
+ private static final String IPAD_USERAGENT = "Mozilla/5.0 (iPad; U; " +
+ "CPU OS 3_2 like Mac OS X; en-us) AppleWebKit/531.21.10 " +
+ "(KHTML, like Gecko) Version/4.0.4 Mobile/7B367 Safari/531.21.10";
+
+ private static final String FROYO_USERAGENT = "Mozilla/5.0 (Linux; U; " +
+ "Android 2.2; en-us; Nexus One Build/FRF91) AppleWebKit/533.1 " +
+ "(KHTML, like Gecko) Version/4.0 Mobile Safari/533.1";
// Value to truncate strings when adding them to a TextView within
// a ListView
s.setUserAgentString(DESKTOP_USERAGENT);
} else if (b.userAgent == 2) {
s.setUserAgentString(IPHONE_USERAGENT);
+ } else if (b.userAgent == 3) {
+ s.setUserAgentString(IPAD_USERAGENT);
+ } else if (b.userAgent == 4) {
+ s.setUserAgentString(FROYO_USERAGENT);
}
s.setUseWideViewPort(b.useWideViewPort);
s.setLoadsImagesAutomatically(b.loadsImagesAutomatically);
s.setJavaScriptEnabled(b.javaScriptEnabled);
- s.setPluginsEnabled(b.pluginsEnabled);
+ s.setPluginState(b.pluginState);
s.setJavaScriptCanOpenWindowsAutomatically(
b.javaScriptCanOpenWindowsAutomatically);
s.setDefaultTextEncodingName(b.defaultTextEncodingName);
s.setSaveFormData(b.saveFormData);
s.setSavePassword(b.rememberPasswords);
s.setLoadWithOverviewMode(b.loadsPageInOverviewMode);
+ s.setPageCacheCapacity(pageCacheCapacity);
// WebView inside Browser doesn't want initial focus to be set.
s.setNeedInitialFocus(false);
s.setDatabasePath(b.databasePath);
s.setGeolocationDatabasePath(b.geolocationDatabasePath);
- // Enable/Disable the error console.
- b.mTabControl.getBrowserActivity().setShouldShowErrorConsole(
- b.showDebugSettings && b.showConsole);
+ b.updateTabControlSettings();
}
}
* stored in this BrowserSettings object. This will update all
* observers of this object.
*/
- public void loadFromDb(Context ctx) {
+ public void loadFromDb(final Context ctx) {
SharedPreferences p =
PreferenceManager.getDefaultSharedPreferences(ctx);
// Set the default value for the Application Caches path.
// Set the default value for the Geolocation database path.
geolocationDatabasePath = ctx.getDir("geolocation", 0).getPath();
- homeUrl = getFactoryResetHomeUrl(ctx);
+ if (p.getString(PREF_HOMEPAGE, "") == "") {
+ // No home page preferences is set, set it to default.
+ setHomePage(ctx, getFactoryResetHomeUrl(ctx));
+ }
+
+ // the cost of one cached page is ~3M (measured using nytimes.com). For
+ // low end devices, we only cache one page. For high end devices, we try
+ // to cache more pages, currently choose 5.
+ ActivityManager am = (ActivityManager) ctx
+ .getSystemService(Context.ACTIVITY_SERVICE);
+ if (am.getMemoryClass() > 16) {
+ pageCacheCapacity = 5;
+ } else {
+ pageCacheCapacity = 1;
+ }
- // Load the defaults from the xml
+ final ContentResolver cr = ctx.getContentResolver();
+ cr.registerContentObserver(
+ Settings.System.getUriFor(Settings.System.SHOW_WEB_SUGGESTIONS), false,
+ new ContentObserver(new Handler()) {
+ @Override
+ public void onChange(boolean selfChange) {
+ SharedPreferences p =
+ PreferenceManager.getDefaultSharedPreferences(ctx);
+ updateShowWebSuggestions(cr, p);
+ }
+ });
+ updateShowWebSuggestions(cr, p);
+
+ // Load the defaults from the xml
// This call is TOO SLOW, need to manually keep the defaults
// in sync
//PreferenceManager.setDefaultValues(ctx, R.xml.browser_preferences);
- syncSharedPreferences(p);
+ syncSharedPreferences(ctx, p);
}
- /* package */ void syncSharedPreferences(SharedPreferences p) {
+ /* package */ void syncSharedPreferences(Context ctx, SharedPreferences p) {
homeUrl =
p.getString(PREF_HOMEPAGE, homeUrl);
+ String searchEngineName = p.getString(PREF_SEARCH_ENGINE, null);
+ if (searchEngine == null || !searchEngine.getName().equals(searchEngineName)) {
+ if (searchEngine != null) {
+ if (searchEngine.supportsVoiceSearch()) {
+ // One or more tabs could have been in voice search mode.
+ // Clear it, since the new SearchEngine may not support
+ // it, or may handle it differently.
+ for (int i = 0; i < mTabControl.getTabCount(); i++) {
+ mTabControl.getTab(i).revertVoiceSearchMode();
+ }
+ }
+ searchEngine.close();
+ }
+ searchEngine = SearchEngines.get(ctx, searchEngineName);
+ }
+ Log.i(TAG, "Selected search engine: " + searchEngine);
+ showSearchSuggestions = p.getBoolean(PREF_SHOW_SEARCH_SUGGESTIONS, true);
+ // Persist to system settings
+ saveShowWebSuggestions(ctx.getContentResolver());
loadsImagesAutomatically = p.getBoolean("load_images",
loadsImagesAutomatically);
javaScriptEnabled = p.getBoolean("enable_javascript",
javaScriptEnabled);
- pluginsEnabled = p.getBoolean("enable_plugins",
- pluginsEnabled);
+ pluginState = WebSettings.PluginState.valueOf(
+ p.getString("plugin_state", pluginState.name()));
javaScriptCanOpenWindowsAutomatically = !p.getBoolean(
"block_popup_windows",
!javaScriptCanOpenWindowsAutomatically);
CookieManager.getInstance().acceptCookie());
CookieManager.getInstance().setAcceptCookie(accept_cookies);
openInBackground = p.getBoolean("open_in_background", openInBackground);
- loginInitialized = p.getBoolean("login_initialized", loginInitialized);
textSize = WebSettings.TextSize.valueOf(
p.getString(PREF_TEXT_SIZE, textSize.name()));
zoomDensity = WebSettings.ZoomDensity.valueOf(
p.getBoolean("landscape_only", landscapeOnly);
if (landscapeOnlyTemp != landscapeOnly) {
landscapeOnly = landscapeOnlyTemp;
- mTabControl.getBrowserActivity().setRequestedOrientation(
- landscapeOnly ? ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE
- : ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
}
useWideViewPort = true; // use wide view port for either setting
if (autoFitPage) {
// regardless of the setting we read here. This setting is only used after debug
// is enabled.
showConsole = p.getBoolean("javascript_console", showConsole);
- mTabControl.getBrowserActivity().setShouldShowErrorConsole(
- showDebugSettings && showConsole);
// HTML5 API flags
appCacheEnabled = p.getBoolean("enable_appcache", appCacheEnabled);
update();
}
+ private void saveShowWebSuggestions(ContentResolver cr) {
+ int value = showSearchSuggestions ? 1 : 0;
+ Settings.System.putInt(cr, Settings.System.SHOW_WEB_SUGGESTIONS, value);
+ }
+
+ private void updateShowWebSuggestions(ContentResolver cr, SharedPreferences p) {
+ showSearchSuggestions =
+ Settings.System.getInt(cr,
+ Settings.System.SHOW_WEB_SUGGESTIONS, 1) == 1;
+ p.edit().putBoolean(PREF_SHOW_SEARCH_SUGGESTIONS, showSearchSuggestions).commit();
+ }
+
public String getHomePage() {
return homeUrl;
}
+ public SearchEngine getSearchEngine() {
+ return searchEngine;
+ }
+
+ public boolean getShowSearchSuggestions() {
+ return showSearchSuggestions;
+ }
+
public String getJsFlags() {
return jsFlags;
}
Editor ed = PreferenceManager.
getDefaultSharedPreferences(context).edit();
ed.putString(PREF_HOMEPAGE, url);
- ed.commit();
+ ed.apply();
homeUrl = url;
}
- public boolean isLoginInitialized() {
- return loginInitialized;
- }
-
- public void setLoginInitialized(Context context) {
- loginInitialized = true;
- Editor ed = PreferenceManager.
- getDefaultSharedPreferences(context).edit();
- ed.putBoolean("login_initialized", loginInitialized);
- ed.commit();
- }
-
public WebSettings.TextSize getTextSize() {
return textSize;
}
*/
/* package */void setTabControl(TabControl tabControl) {
mTabControl = tabControl;
+ updateTabControlSettings();
}
/*
/* package */ void clearFormData(Context context) {
WebViewDatabase.getInstance(context).clearFormData();
if (mTabControl != null) {
- mTabControl.getCurrentTopWebView().clearFormData();
+ WebView currentTopView = mTabControl.getCurrentTopWebView();
+ if (currentTopView != null) {
+ currentTopView.clearFormData();
+ }
}
}
db.clearHttpAuthUsernamePassword();
}
+ private void updateTabControlSettings() {
+ // Enable/disable the error console.
+ mTabControl.getBrowserActivity().setShouldShowErrorConsole(
+ showDebugSettings && showConsole);
+ mTabControl.getBrowserActivity().setRequestedOrientation(
+ landscapeOnly ? ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE
+ : ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
+ }
+
private void maybeDisableWebsiteSettings(Context context) {
PreferenceActivity activity = (PreferenceActivity) context;
final PreferenceScreen screen = (PreferenceScreen)
}
});
- GeolocationPermissions.getInstance().getOrigins(new ValueCallback<Set>() {
- public void onReceiveValue(Set geolocationOrigins) {
+ GeolocationPermissions.getInstance().getOrigins(new ValueCallback<Set<String> >() {
+ public void onReceiveValue(Set<String> geolocationOrigins) {
if ((geolocationOrigins != null) && !geolocationOrigins.isEmpty()) {
screen.setEnabled(true);
}
}
/*package*/ void resetDefaultPreferences(Context ctx) {
+ reset();
SharedPreferences p =
PreferenceManager.getDefaultSharedPreferences(ctx);
- p.edit().clear().commit();
+ p.edit().clear().apply();
PreferenceManager.setDefaultValues(ctx, R.xml.browser_preferences,
true);
// reset homeUrl
private String getFactoryResetHomeUrl(Context context) {
String url = context.getResources().getString(R.string.homepage_base);
if (url.indexOf("{CID}") != -1) {
- url = url.replace("{CID}", Partner.getString(context
- .getContentResolver(), Partner.CLIENT_ID, "android-google"));
+ url = url.replace("{CID}",
+ BrowserProvider.getClientId(context.getContentResolver()));
}
return url;
}
// Private constructor that does nothing.
private BrowserSettings() {
+ reset();
+ }
+
+ private void reset() {
+ // Private variables for settings
+ // NOTE: these defaults need to be kept in sync with the XML
+ // until the performance of PreferenceManager.setDefaultValues()
+ // is improved.
+ loadsImagesAutomatically = true;
+ javaScriptEnabled = true;
+ pluginState = WebSettings.PluginState.ON;
+ javaScriptCanOpenWindowsAutomatically = false;
+ showSecurityWarnings = true;
+ rememberPasswords = true;
+ saveFormData = true;
+ openInBackground = false;
+ autoFitPage = true;
+ landscapeOnly = false;
+ loadsPageInOverviewMode = true;
+ showDebugSettings = false;
+ // HTML5 API flags
+ appCacheEnabled = true;
+ databaseEnabled = true;
+ domStorageEnabled = true;
+ geolocationEnabled = true;
+ workersEnabled = true; // only affects V8. JSC does not have a similar setting
}
}