Debug settings changes

 Bug: 3250498
 Makes OpenGL rendering and user agent normally visible in settings->debug
 Changing the OpenGL setting now automatically restarts the browser

Change-Id: I04036b580f2463f77376edb4bee2dfefe3d123ed
diff --git a/res/xml/debug_preferences.xml b/res/xml/debug_preferences.xml
index 8f8d296..50d4c14 100644
--- a/res/xml/debug_preferences.xml
+++ b/res/xml/debug_preferences.xml
@@ -17,53 +17,11 @@
 <PreferenceScreen 
         xmlns:android="http://schemas.android.com/apk/res/android" >
 
-    <!-- The javascript console is enabled by default when the user has
-         also enabled debug mode by navigating to about:debug. -->
-    <CheckBoxPreference
-        android:key="javascript_console"
-        android:defaultValue="true"
-        android:title="@string/pref_development_error_console" />
-                    
-    <CheckBoxPreference
-        android:key="small_screen"
-        android:defaultValue="false"
-        android:title="@string/pref_development_single_column_rendering" />   
-
-    <CheckBoxPreference
-        android:key="wide_viewport"
-        android:defaultValue="true"
-        android:title="@string/pref_development_viewport" />
-                
-    <CheckBoxPreference
-        android:key="normal_layout"
-        android:defaultValue="false"
-        android:title="@string/pref_development_normal_rendering" />       
-    
-    <CheckBoxPreference
-        android:key="enable_tracing"
-        android:defaultValue="false"
-        android:title="@string/pref_development_trace" />
-
-    <CheckBoxPreference
-        android:key="enable_light_touch"
-        android:defaultValue="false"
-        android:title="Enable light touch" />
-
-    <CheckBoxPreference
-        android:key="enable_nav_dump"
-        android:defaultValue="false"
-        android:title="@string/pref_development_nav_dump" />
-
     <CheckBoxPreference
         android:key="enable_hardware_accel"
         android:defaultValue="true"
         android:title="@string/pref_development_hardware_accel" />
 
-    <EditTextPreference
-        android:key="js_engine_flags"
-        android:title="@string/js_engine_flags"
-        android:singleLine="true" />
-
     <ListPreference
         android:key="user_agent"
         android:title="@string/pref_development_uastring"
diff --git a/res/xml/hidden_debug_preferences.xml b/res/xml/hidden_debug_preferences.xml
new file mode 100644
index 0000000..6d66eaa
--- /dev/null
+++ b/res/xml/hidden_debug_preferences.xml
@@ -0,0 +1,62 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!-- Copyright (C) 2008 The Android Open Source Project
+
+     Licensed under the Apache License, Version 2.0 (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.apache.org/licenses/LICENSE-2.0
+
+     Unless required by applicable law or agreed to in writing, software
+     distributed under the License is distributed on an "AS IS" BASIS,
+     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+     See the License for the specific language governing permissions and
+     limitations under the License.
+-->
+
+<PreferenceScreen
+        xmlns:android="http://schemas.android.com/apk/res/android" >
+
+    <!-- The javascript console is enabled by default when the user has
+         also enabled debug mode by navigating to about:debug. -->
+    <CheckBoxPreference
+        android:key="javascript_console"
+        android:defaultValue="true"
+        android:title="@string/pref_development_error_console" />
+
+    <CheckBoxPreference
+        android:key="small_screen"
+        android:defaultValue="false"
+        android:title="@string/pref_development_single_column_rendering" />
+
+    <CheckBoxPreference
+        android:key="wide_viewport"
+        android:defaultValue="true"
+        android:title="@string/pref_development_viewport" />
+
+    <CheckBoxPreference
+        android:key="normal_layout"
+        android:defaultValue="false"
+        android:title="@string/pref_development_normal_rendering" />
+
+    <CheckBoxPreference
+        android:key="enable_tracing"
+        android:defaultValue="false"
+        android:title="@string/pref_development_trace" />
+
+    <CheckBoxPreference
+        android:key="enable_light_touch"
+        android:defaultValue="false"
+        android:title="Enable light touch" />
+
+    <CheckBoxPreference
+        android:key="enable_nav_dump"
+        android:defaultValue="false"
+        android:title="@string/pref_development_nav_dump" />
+
+    <EditTextPreference
+        android:key="js_engine_flags"
+        android:title="@string/js_engine_flags"
+        android:singleLine="true" />
+
+</PreferenceScreen>
diff --git a/src/com/android/browser/BrowserActivity.java b/src/com/android/browser/BrowserActivity.java
index a8c393a..1c15153 100644
--- a/src/com/android/browser/BrowserActivity.java
+++ b/src/com/android/browser/BrowserActivity.java
@@ -39,6 +39,9 @@
 
 public class BrowserActivity extends Activity {
 
+    public static final String ACTION_RESTART = "--restart--";
+    private static final String EXTRA_STATE = "state";
+
     private final static String LOGTAG = "browser";
 
     private final static boolean LOGV_ENABLED =
@@ -94,6 +97,10 @@
         mController.setUi(mUi);
         mController.setWebViewFactory((BaseUi) mUi);
 
+        Bundle state = getIntent().getBundleExtra(EXTRA_STATE);
+        if (state != null && icicle == null) {
+            icicle = state;
+        }
         mController.start(icicle, getIntent());
     }
 
@@ -104,6 +111,16 @@
 
     @Override
     protected void onNewIntent(Intent intent) {
+        if (ACTION_RESTART.equals(intent.getAction())) {
+            Bundle outState = new Bundle();
+            mController.onSaveInstanceState(outState);
+            finish();
+            getApplicationContext().startActivity(
+                    new Intent(getApplicationContext(), BrowserActivity.class)
+                    .addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
+                    .putExtra(EXTRA_STATE, outState));
+            return;
+        }
         mController.handleNewIntent(intent);
     }
 
diff --git a/src/com/android/browser/BrowserPreferencesPage.java b/src/com/android/browser/BrowserPreferencesPage.java
index d93e70f..c47675d 100644
--- a/src/com/android/browser/BrowserPreferencesPage.java
+++ b/src/com/android/browser/BrowserPreferencesPage.java
@@ -34,7 +34,7 @@
     public void onBuildHeaders(List<Header> target) {
         loadHeadersFromResource(R.xml.preference_headers, target);
 
-        if (BrowserSettings.getInstance().showDebugSettings()) {
+        if (BrowserSettings.DEV_BUILD || BrowserSettings.getInstance().showDebugSettings()) {
             Header debug = new Header();
             debug.title = getText(R.string.pref_development_title);
             debug.fragment = DebugPreferencesFragment.class.getName();
diff --git a/src/com/android/browser/BrowserSettings.java b/src/com/android/browser/BrowserSettings.java
index ba2f3fe..61ef76b 100644
--- a/src/com/android/browser/BrowserSettings.java
+++ b/src/com/android/browser/BrowserSettings.java
@@ -24,6 +24,7 @@
 import android.content.ComponentName;
 import android.content.ContentResolver;
 import android.content.Context;
+import android.content.SharedPreferences.OnSharedPreferenceChangeListener;
 import android.content.pm.ActivityInfo;
 import android.content.SharedPreferences;
 import android.content.SharedPreferences.Editor;
@@ -68,8 +69,7 @@
  * To remove an observer:
  * s.deleteObserver(webView.getSettings());
  */
-public class BrowserSettings extends Observable {
-
+public class BrowserSettings extends Observable implements OnSharedPreferenceChangeListener {
     // Private variables for settings
     // NOTE: these defaults need to be kept in sync with the XML
     // until the performance of PreferenceManager.setDefaultValues()
@@ -164,6 +164,8 @@
     public final static String PREF_AUTOFILL_ENABLED = "autofill_enabled";
     public final static String PREF_AUTOFILL_PROFILE = "autofill_profile";
     public final static String PREF_AUTOFILL_ACTIVE_PROFILE_ID = "autofill_active_profile_id";
+    public final static String PREF_HARDWARE_ACCEL = "enable_hardware_accel";
+    public final static String PREF_USER_AGENT = "user_agent";
 
     private static final String DESKTOP_USERAGENT = "Mozilla/5.0 (Macintosh; " +
             "U; Intel Mac OS X 10_6_3; en-us) AppleWebKit/533.16 (KHTML, " +
@@ -189,6 +191,9 @@
 
     public static final Uri RLZ_PROVIDER_URI = Uri.parse("content://" + RLZ_PROVIDER + "/");
 
+    // Set to true to enable some of the about:debug options
+    public static final boolean DEV_BUILD = true;
+
     private Controller mController;
 
     // Single instance of the BrowserSettings for use in the Browser app.
@@ -390,6 +395,7 @@
 
             // PreferenceManager.setDefaultValues is TOO SLOW, need to manually keep
             // the defaults in sync
+            p.registerOnSharedPreferenceChangeListener(BrowserSettings.this);
             syncSharedPreferences(mContext, p);
 
             synchronized (sSingleton) {
@@ -482,13 +488,13 @@
             tracing = p.getBoolean("enable_tracing", tracing);
             lightTouch = p.getBoolean("enable_light_touch", lightTouch);
             navDump = p.getBoolean("enable_nav_dump", navDump);
-            userAgent = Integer.parseInt(p.getString("user_agent", "0"));
         }
 
-        // This setting can only be modified when the debug settings have been
-        // enabled but it is read and used by the browser at startup so we must
-        // initialize it regardless of the status of the debug settings.
-        hardwareAccelerated = p.getBoolean("enable_hardware_accel", hardwareAccelerated);
+        // Only set these on startup if it is a dev build
+        if (DEV_BUILD) {
+            userAgent = Integer.parseInt(p.getString(PREF_USER_AGENT, "0"));
+            hardwareAccelerated = p.getBoolean(PREF_HARDWARE_ACCEL, hardwareAccelerated);
+        }
 
         // JS flags is loaded from DB even if showDebugSettings is false,
         // so that it can be set once and be effective all the time.
@@ -642,10 +648,10 @@
     }
 
     /*
-     * Package level method for obtaining a single app instance of the
+     * Application level method for obtaining a single app instance of the
      * BrowserSettings.
      */
-    /*package*/ static BrowserSettings getInstance() {
+    public static BrowserSettings getInstance() {
         if (sSingleton == null ) {
             sSingleton = new BrowserSettings();
         }
@@ -821,4 +827,15 @@
             return null;
         }
     }
+
+    @Override
+    public void onSharedPreferenceChanged(
+            SharedPreferences p, String key) {
+        if (PREF_HARDWARE_ACCEL.equals(key)) {
+            hardwareAccelerated = p.getBoolean(PREF_HARDWARE_ACCEL, hardwareAccelerated);
+        } else if (PREF_USER_AGENT.equals(key)) {
+            userAgent = Integer.parseInt(p.getString(PREF_USER_AGENT, "0"));
+            update();
+        }
+    }
 }
diff --git a/src/com/android/browser/preferences/DebugPreferencesFragment.java b/src/com/android/browser/preferences/DebugPreferencesFragment.java
index d643a97..0a82371 100644
--- a/src/com/android/browser/preferences/DebugPreferencesFragment.java
+++ b/src/com/android/browser/preferences/DebugPreferencesFragment.java
@@ -16,17 +16,46 @@
 
 package com.android.browser.preferences;
 
+import com.android.browser.BrowserActivity;
+import com.android.browser.BrowserSettings;
+import com.android.browser.Controller;
 import com.android.browser.R;
 
+import android.content.Context;
+import android.content.Intent;
+import android.os.AsyncTask;
 import android.os.Bundle;
+import android.preference.Preference;
+import android.preference.Preference.OnPreferenceChangeListener;
+import android.preference.PreferenceActivity.Header;
 import android.preference.PreferenceFragment;
+import android.preference.PreferenceManager.OnActivityResultListener;
 
-public class DebugPreferencesFragment extends PreferenceFragment {
+import java.io.IOException;
+import java.io.Serializable;
+
+public class DebugPreferencesFragment extends PreferenceFragment
+        implements OnPreferenceChangeListener {
     @Override
     public void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
 
         // Load the XML preferences file
         addPreferencesFromResource(R.xml.debug_preferences);
+
+        if (BrowserSettings.getInstance().showDebugSettings()) {
+            addPreferencesFromResource(R.xml.hidden_debug_preferences);
+        }
+
+        Preference e = findPreference(BrowserSettings.PREF_HARDWARE_ACCEL);
+        e.setOnPreferenceChangeListener(this);
+    }
+
+    @Override
+    public boolean onPreferenceChange(Preference preference, Object newValue) {
+        // Attempt to restart
+        startActivity(new Intent(BrowserActivity.ACTION_RESTART, null,
+                getActivity(), BrowserActivity.class));
+        return true;
     }
 }