Updated SWE Browser "About" menu item

- Changed "About" menu to preference screen layout,
  instead of popup
- Added Version, Build date, Build Hash, User Agent, Help
  and Feedback

Change-Id: I2c5b8f74f9ffc945adb305042c1ebcd61283e047
diff --git a/src/com/android/browser/BrowserPreferencesPage.java b/src/com/android/browser/BrowserPreferencesPage.java
index 8f31ef9..a40c7c7 100644
--- a/src/com/android/browser/BrowserPreferencesPage.java
+++ b/src/com/android/browser/BrowserPreferencesPage.java
@@ -17,12 +17,17 @@
 package com.android.browser;
 
 import android.app.Activity;
+import android.app.Fragment;
 import android.content.Intent;
 import android.os.Bundle;
 import android.preference.PreferenceActivity;
 
+import com.android.browser.preferences.AboutPreferencesFragment;
 import com.android.browser.preferences.GeneralPreferencesFragment;
 
+import java.lang.reflect.Constructor;
+import java.lang.reflect.InvocationTargetException;
+
 public class BrowserPreferencesPage extends Activity {
 
     public static void startPreferencesForResult(Activity callerActivity, String url, int requestCode) {
@@ -37,6 +42,16 @@
         callerActivity.startActivityForResult(intent, requestCode);
     }
 
+    public static void startPreferenceFragmentExtraForResult(Activity callerActivity,
+                                                             String fragmentName,
+                                                             Bundle bundle,
+                                                             int requestCode) {
+        final Intent intent = new Intent(callerActivity, BrowserPreferencesPage.class);
+        intent.putExtra(PreferenceActivity.EXTRA_SHOW_FRAGMENT, fragmentName);
+        intent.putExtra(PreferenceActivity.EXTRA_SHOW_FRAGMENT_ARGUMENTS, bundle);
+        callerActivity.startActivityForResult(intent, requestCode);
+    }
+
 
     @Override
     public void onCreate(Bundle icicle) {
@@ -49,6 +64,35 @@
             if ("android.intent.action.MANAGE_NETWORK_USAGE".equals(action)) {
                 // TODO: switch to the Network fragment here?
             }
+
+            Bundle extras = intent.getExtras();
+            String fragment = (String) extras.getCharSequence(PreferenceActivity.EXTRA_SHOW_FRAGMENT);
+            if (fragment != null) {
+                try {
+                    Class<?> cls = Class.forName(fragment);
+                    Constructor<?> ctor = cls.getConstructor();
+                    Object obj = ctor.newInstance();
+
+                    if (obj instanceof Fragment) {
+                        Fragment frag = (Fragment) obj;
+
+                        Bundle bundle = extras.getBundle(PreferenceActivity.EXTRA_SHOW_FRAGMENT_ARGUMENTS);
+                        if (bundle != null) {
+                            frag.setArguments(bundle);
+                        }
+
+                        getFragmentManager().beginTransaction().replace(
+                                android.R.id.content,
+                                (Fragment) obj).commit();
+                    }
+                } catch (ClassNotFoundException e) {
+                } catch (NoSuchMethodException e) {
+                } catch (InvocationTargetException e) {
+                } catch (InstantiationException e) {
+                } catch (IllegalAccessException e) {
+                }
+                return;
+            }
         }
 
         getFragmentManager().beginTransaction().replace(android.R.id.content,
diff --git a/src/com/android/browser/Controller.java b/src/com/android/browser/Controller.java
index 181b148..733e122 100644
--- a/src/com/android/browser/Controller.java
+++ b/src/com/android/browser/Controller.java
@@ -40,8 +40,6 @@
 import android.database.sqlite.SQLiteException;
 import android.graphics.Bitmap;
 import android.graphics.BitmapFactory;
-import android.graphics.Canvas;
-import android.graphics.Paint;
 import android.graphics.Rect;
 import android.net.ConnectivityManager;
 import android.net.NetworkInfo;
@@ -56,7 +54,6 @@
 import android.os.Message;
 import android.os.PowerManager;
 import android.os.PowerManager.WakeLock;
-import android.preference.PreferenceActivity;
 import android.provider.ContactsContract;
 import android.provider.ContactsContract.Intents.Insert;
 import android.provider.Settings;
@@ -79,12 +76,12 @@
 import android.webkit.MimeTypeMap;
 import android.webkit.ValueCallback;
 import android.webkit.WebChromeClient.CustomViewCallback;
-import android.widget.Button;
 import android.widget.EditText;
 import android.widget.Toast;
 
 import org.codeaurora.swe.CookieManager;
 import org.codeaurora.swe.CookieSyncManager;
+import org.codeaurora.swe.Engine;
 import org.codeaurora.swe.HttpAuthHandler;
 import org.codeaurora.swe.SslErrorHandler;
 import org.codeaurora.swe.WebSettings;
@@ -102,6 +99,7 @@
 import com.android.browser.platformsupport.BrowserContract;
 import com.android.browser.platformsupport.WebAddress;
 import com.android.browser.platformsupport.BrowserContract.Images;
+import com.android.browser.preferences.AboutPreferencesFragment;
 import com.android.browser.provider.BrowserProvider2.Thumbnails;
 import com.android.browser.provider.SnapshotProvider.Snapshots;
 import com.android.browser.reflect.ReflectHelper;
@@ -116,7 +114,6 @@
 import java.text.DateFormat;
 import java.text.SimpleDateFormat;
 import java.util.ArrayList;
-import java.util.Calendar;
 import java.util.Date;
 import java.util.HashMap;
 import java.util.List;
@@ -2110,20 +2107,12 @@
                 break;
 
             case R.id.about_menu_id:
-                final AlertDialog.Builder builder = new AlertDialog.Builder(getContext());
-                builder.setTitle(R.string.about);
-                builder.setCancelable(true);
-                String ua = "";
-                final WebView currentWebView = getCurrentWebView();
-                if (currentWebView != null) {
-                    final WebSettings s = currentWebView.getSettings();
-                    if (s != null) {
-                        ua = s.getUserAgentString();
-                    }
-                }
-                builder.setMessage("Agent:" + ua);
-                builder.setPositiveButton(android.R.string.ok, null);
-                builder.create().show();
+                Bundle bundle = new Bundle();
+                bundle.putCharSequence("UA", Engine.getDefaultUserAgent());
+                bundle.putCharSequence("TabTitle", mTabControl.getCurrentTab().getTitle());
+                bundle.putCharSequence("TabURL", mTabControl.getCurrentTab().getUrl());
+                BrowserPreferencesPage.startPreferenceFragmentExtraForResult(mActivity,
+                        AboutPreferencesFragment.class.getName(), bundle, 0);
                 break;
 
             case R.id.add_to_homescreen:
diff --git a/src/com/android/browser/PreferenceKeys.java b/src/com/android/browser/PreferenceKeys.java
index f3e752f..ea6b1d6 100644
--- a/src/com/android/browser/PreferenceKeys.java
+++ b/src/com/android/browser/PreferenceKeys.java
@@ -121,4 +121,15 @@
      * Key for whether or not the last run was paused.
      */
     static final String KEY_LAST_RUN_PAUSED = "last_paused";
+
+    // ----------------------
+    // Keys for about_preferences.xml
+    // ----------------------
+    static final String PREF_ABOUT = "about";
+    static final String PREF_VERSION = "version";
+    static final String PREF_BUILD_DATE = "built";
+    static final String PREF_BUILD_HASH = "hash";
+    static final String PREF_USER_AGENT = "user_agent";
+    static final String PREF_HELP = "help_about";
+    static final String PREF_FEEDBACK = "feedback";
 }
diff --git a/src/com/android/browser/preferences/AboutPreferencesFragment.java b/src/com/android/browser/preferences/AboutPreferencesFragment.java
index d979333..e3758b0 100644
--- a/src/com/android/browser/preferences/AboutPreferencesFragment.java
+++ b/src/com/android/browser/preferences/AboutPreferencesFragment.java
@@ -30,31 +30,169 @@
 
 package com.android.browser.preferences;
 
+import android.app.ActionBar;
+import android.content.Intent;
+import android.net.Uri;
 import android.os.Bundle;
 import android.preference.Preference;
 import android.preference.Preference.OnPreferenceClickListener;
 import android.preference.PreferenceFragment;
+import android.preference.PreferenceScreen;
 
+import com.android.browser.BrowserActivity;
+import com.android.browser.PreferenceKeys;
 import com.android.browser.R;
 
+import org.codeaurora.swe.BrowserCommandLine;
+
 public class AboutPreferencesFragment extends PreferenceFragment
                             implements OnPreferenceClickListener {
 
-    static final String PREF_ABOUT = "about_preference";
+    final String CMD_LINE_SWITCH_FEEDBACK = "mail-feedback-to";
+    final String CMD_LINE_SWITCH_HELPURL  = "help-url";
+
+    final String ABOUT_TEXT_VERSION_KEY = "Version:";
+    final String ABOUT_TEXT_BUILT_KEY = "Built:";
+    final String ABOUT_TEXT_HASH_KEY = "Hash:";
+
+    String mFeedbackRecipient = "";
+    String mHelpURL = "";
+    String mVersion = "";
+    String mBuilt = "";
+    String mHash = "";
+    String mTabTitle = "";
+    String mTabURL = "";
+
+    String mAboutText = "";
+    PreferenceScreen mHeadPref = null;
+
+    private String findValueFromAboutText(String aboutKey) {
+        int start = mAboutText.indexOf(aboutKey);
+        int end = mAboutText.indexOf("\n", start);
+        String value = "";
+
+        if (start != -1 && end != -1) {
+            start += aboutKey.length();
+            value = mAboutText.substring(start, end);
+        }
+        return value;
+    }
+
+    private void setPreference(String prefKey, String value) {
+        Preference pref = findPreference(prefKey);
+        if (pref == null) {
+            return;
+        }
+
+        if (value.isEmpty()) {
+            if (mHeadPref != null)
+                mHeadPref.removePreference(pref);
+        } else {
+            pref.setSummary(value);
+        }
+    }
+
+    private void setOnClickListener(String prefKey, boolean set) {
+        Preference pref = findPreference(prefKey);
+        if (pref == null) {
+            return;
+        }
+
+        if (set) {
+            pref.setOnPreferenceClickListener(this);
+        } else {
+            if (mHeadPref != null)
+                mHeadPref.removePreference(pref);
+        }
+    }
 
     @Override
     public void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
 
+        ActionBar bar = getActivity().getActionBar();
+        if (bar != null) {
+            bar.setTitle(R.string.about);
+        }
+
+        mAboutText = getString(R.string.about_text);
+
         addPreferencesFromResource(R.xml.about_preferences);
-        Preference aboutPreference = (Preference) findPreference(PREF_ABOUT);
-        String about_text = getString(R.string.about_text);
-        about_text = about_text.substring(about_text.indexOf("Hash"), about_text.length());
-        aboutPreference.setSummary(about_text);
+        mHeadPref = (PreferenceScreen) findPreference(PreferenceKeys.PREF_ABOUT);
+
+        mVersion = findValueFromAboutText(ABOUT_TEXT_VERSION_KEY);
+        setPreference(PreferenceKeys.PREF_VERSION, mVersion);
+
+        mBuilt = findValueFromAboutText(ABOUT_TEXT_BUILT_KEY);
+        setPreference(PreferenceKeys.PREF_BUILD_DATE, mBuilt);
+
+        mHash = findValueFromAboutText(ABOUT_TEXT_HASH_KEY);
+        setPreference(PreferenceKeys.PREF_BUILD_HASH, mHash);
+
+        final Bundle arguments = getArguments();
+        String user_agent = "";
+        if (arguments != null) {
+            user_agent = (String) arguments.getCharSequence("UA");
+            mTabTitle = (String) arguments.getCharSequence("TabTitle");
+            mTabURL = (String) arguments.getCharSequence("TabURL");
+        }
+
+        setPreference(PreferenceKeys.PREF_USER_AGENT, user_agent);
+
+        if (BrowserCommandLine.hasSwitch(CMD_LINE_SWITCH_HELPURL)) {
+            mHelpURL = BrowserCommandLine.getSwitchValue(CMD_LINE_SWITCH_HELPURL);
+        }
+
+        setOnClickListener(PreferenceKeys.PREF_HELP, !mHelpURL.isEmpty());
+
+        if (BrowserCommandLine.hasSwitch(CMD_LINE_SWITCH_FEEDBACK)) {
+            mFeedbackRecipient = BrowserCommandLine.getSwitchValue(CMD_LINE_SWITCH_FEEDBACK);
+        }
+
+        setOnClickListener(PreferenceKeys.PREF_FEEDBACK, !mFeedbackRecipient.isEmpty());
     }
 
     @Override
     public boolean onPreferenceClick(Preference preference) {
+        if (preference.getKey().equals(PreferenceKeys.PREF_HELP)) {
+            Intent intent = new Intent(getActivity(), BrowserActivity.class);
+            intent.setAction(Intent.ACTION_VIEW);
+            intent.setData(Uri.parse(mHelpURL));
+            getActivity().startActivity(intent);
+            return true;
+        } else if (preference.getKey().equals(PreferenceKeys.PREF_FEEDBACK)) {
+            Intent intent = new Intent(Intent.ACTION_SEND);
+            intent.setType("message/rfc822");
+            intent.putExtra(Intent.EXTRA_EMAIL, new String[]{mFeedbackRecipient});
+            intent.putExtra(Intent.EXTRA_SUBJECT,"Browser Feedback");
+
+            String message = "";
+            if (!mVersion.isEmpty()) {
+                message += "Version: " + mVersion + "\n";
+            }
+
+            if (!mBuilt.isEmpty()) {
+                message += "Build Date: " + mBuilt + "\n";
+            }
+
+            if (!mHash.isEmpty()) {
+                message += "Build Hash: " + mHash + "\n";
+            }
+
+            if (!mTabTitle.isEmpty()) {
+                message += "Tab Title: " + mTabTitle + "\n";
+            }
+
+            if (!mTabURL.isEmpty()) {
+                message += "Tab URL: " + mTabURL + "\n";
+            }
+
+            message += "\nEnter your feedback here...";
+
+            intent.putExtra(Intent.EXTRA_TEXT, message);
+            startActivity(Intent.createChooser(intent, "Select email application"));
+            return true;
+        }
         return false;
     }
 }