New useragent switcher design

 Uses a popup menu off of the globe on tablets or favicon on phones

Change-Id: Ia3634b5a218456127abb8b266a6cfc664836b037
diff --git a/res/drawable-mdpi/ic_ua_android.png b/res/drawable-mdpi/ic_ua_android.png
deleted file mode 100644
index 4f7aa45..0000000
--- a/res/drawable-mdpi/ic_ua_android.png
+++ /dev/null
Binary files differ
diff --git a/res/drawable-mdpi/ic_ua_desktop.png b/res/drawable-mdpi/ic_ua_desktop.png
deleted file mode 100644
index 4e2f5c2..0000000
--- a/res/drawable-mdpi/ic_ua_desktop.png
+++ /dev/null
Binary files differ
diff --git a/res/layout/help_dialog_useragent_switcher.xml b/res/layout/help_dialog_useragent_switcher.xml
deleted file mode 100644
index cc29a2d..0000000
--- a/res/layout/help_dialog_useragent_switcher.xml
+++ /dev/null
@@ -1,37 +0,0 @@
-<?xml version="1.0" encoding="utf-8"?>
-<!-- Copyright (C) 2011 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.
--->
-
-<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
-    android:layout_width="wrap_content"
-    android:layout_height="wrap_content"
-    android:orientation="vertical">
-
-    <TextView
-        android:layout_width="wrap_content"
-        android:layout_height="wrap_content"
-        android:text="@string/help_useragent_switcher" />
-
-    <ImageView
-        android:layout_width="wrap_content"
-        android:layout_height="wrap_content"
-        android:src="@drawable/help_useragent_tablet" />
-
-    <ImageView
-        android:layout_width="wrap_content"
-        android:layout_height="wrap_content"
-        android:src="@drawable/help_useragent_laptop" />
-
-</LinearLayout>
diff --git a/res/menu/ua_switcher.xml b/res/menu/ua_switcher.xml
new file mode 100644
index 0000000..5076fb2
--- /dev/null
+++ b/res/menu/ua_switcher.xml
@@ -0,0 +1,27 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!-- Copyright (C) 2011 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.
+-->
+
+<menu xmlns:android="http://schemas.android.com/apk/res/android">
+    <item
+        android:id="@+id/ua_mobile_menu_id"
+        android:checkable="true"
+        android:title="@string/ua_switcher_mobile" />
+    <item
+        android:id="@+id/ua_desktop_menu_id"
+        android:checkable="true"
+        android:title="@string/ua_switcher_desktop" />
+</menu>
+
diff --git a/res/values/strings.xml b/res/values/strings.xml
index e51538c..b86edb5 100644
--- a/res/values/strings.xml
+++ b/res/values/strings.xml
@@ -665,13 +665,6 @@
     <!-- Summary for the fullscreen lab feature [CHAR LIMIT=120] -->
     <string name="pref_lab_fullscreen_summary">
       Use fullscreen mode to hide the status bar.</string>
-    <!-- Title for the useragent switcher lab feature [CHAR LIMIT=40] -->
-    <string name="pref_enable_useragent_switcher">Useragent Switcher</string>
-    <!-- Summary for the useragent switcher lab feature [CHAR LIMIT=120] -->
-    <string name="pref_enable_useragent_switcher_summary">Toggle between the tablet and desktop versions of a site</string>
-    <!-- Text explaining how to use the useragent switcher lab feature [CHAR LIMIT=none] -->
-    <string name="help_useragent_switcher">Tap on the Android or laptop icon to toggle between the tablet and desktop versions of a site.</string>
-
     <!-- Title for a dialog displayed when the browser has a data connectivity
             problem -->
     <string name="browserFrameNetworkErrorLabel">Data connectivity problem</string>
@@ -1016,4 +1009,8 @@
     <string name="preview">Preview:</string>
     <!-- Name for the user's unsynced, local bookmarks [CHAR LIMIT=50] -->
     <string name="local_bookmarks">Local</string>
+    <!-- Popup menu option that allows the user to select the mobile version of a webpage [CHAR LIMIT=50] -->
+    <string name="ua_switcher_mobile">Mobile</string>
+    <!-- Popup menu option that allows the user to select the desktop version of a webpage [CHAR LIMIT=50] -->
+    <string name="ua_switcher_desktop">Desktop</string>
 </resources>
diff --git a/res/xml-sw600dp/lab_preferences.xml b/res/xml-sw600dp/lab_preferences.xml
index c858c38..705f740 100644
--- a/res/xml-sw600dp/lab_preferences.xml
+++ b/res/xml-sw600dp/lab_preferences.xml
@@ -35,9 +35,4 @@
         android:title="@string/pref_use_instant_search"
         android:summary="@string/pref_use_instant_search_summary" />
 
-    <CheckBoxPreference
-        android:key="enable_useragent_switcher"
-        android:defaultValue="false"
-        android:title="@string/pref_enable_useragent_switcher"
-        android:summary="@string/pref_enable_useragent_switcher_summary" />
 </PreferenceScreen>
diff --git a/src/com/android/browser/BrowserSettings.java b/src/com/android/browser/BrowserSettings.java
index 46d47b0..101effa 100644
--- a/src/com/android/browser/BrowserSettings.java
+++ b/src/com/android/browser/BrowserSettings.java
@@ -16,11 +16,6 @@
 
 package com.android.browser;
 
-import com.android.browser.homepages.HomeProvider;
-import com.android.browser.provider.BrowserProvider;
-import com.android.browser.search.SearchEngine;
-import com.android.browser.search.SearchEngines;
-
 import android.app.ActivityManager;
 import android.content.ContentResolver;
 import android.content.Context;
@@ -43,6 +38,11 @@
 import android.webkit.WebView;
 import android.webkit.WebViewDatabase;
 
+import com.android.browser.homepages.HomeProvider;
+import com.android.browser.provider.BrowserProvider;
+import com.android.browser.search.SearchEngine;
+import com.android.browser.search.SearchEngines;
+
 import java.lang.ref.WeakReference;
 import java.util.Iterator;
 import java.util.LinkedList;
@@ -165,7 +165,7 @@
         settings.setAutoFillProfile(getAutoFillProfile());
 
         String ua = mCustomUserAgents.get(settings);
-        if (enableUseragentSwitcher() && ua != null) {
+        if (ua != null) {
             settings.setUserAgentString(ua);
         } else {
             settings.setUserAgentString(USER_AGENTS[getUserAgent()]);
@@ -606,10 +606,6 @@
         return mPrefs.getBoolean(PREF_FULLSCREEN, false);
     }
 
-    public boolean enableUseragentSwitcher() {
-        return mPrefs.getBoolean(PREF_ENABLE_USERAGENT_SWITCHER, false);
-    }
-
     // -----------------------------
     // getter/setters for privacy_security_preferences.xml
     // -----------------------------
diff --git a/src/com/android/browser/PreferenceKeys.java b/src/com/android/browser/PreferenceKeys.java
index cff9f70..f7230df 100644
--- a/src/com/android/browser/PreferenceKeys.java
+++ b/src/com/android/browser/PreferenceKeys.java
@@ -78,7 +78,6 @@
     static final String PREF_USE_MOST_VISITED_HOMEPAGE = "use_most_visited_homepage";
     static final String PREF_USE_INSTANT_SEARCH = "use_instant_search";
     static final String PREF_FULLSCREEN = "fullscreen";
-    static final String PREF_ENABLE_USERAGENT_SWITCHER = "enable_useragent_switcher";
 
     // ----------------------
     // Keys for privacy_security_preferences.xml
diff --git a/src/com/android/browser/TitleBarBase.java b/src/com/android/browser/TitleBarBase.java
index 4dc960c..b95711e 100644
--- a/src/com/android/browser/TitleBarBase.java
+++ b/src/com/android/browser/TitleBarBase.java
@@ -35,6 +35,8 @@
 import android.view.Gravity;
 import android.view.KeyEvent;
 import android.view.LayoutInflater;
+import android.view.Menu;
+import android.view.MenuItem;
 import android.view.View;
 import android.view.View.OnClickListener;
 import android.view.View.OnFocusChangeListener;
@@ -48,6 +50,8 @@
 import android.widget.Button;
 import android.widget.FrameLayout;
 import android.widget.ImageView;
+import android.widget.PopupMenu;
+import android.widget.PopupMenu.OnMenuItemClickListener;
 import android.widget.ProgressBar;
 import android.widget.RelativeLayout;
 import android.widget.Spinner;
@@ -60,7 +64,8 @@
  */
 public class TitleBarBase extends RelativeLayout
         implements OnClickListener, OnFocusChangeListener, UrlInputListener,
-        TextChangeWatcher, DeviceAccountLogin.AutoLoginCallback {
+        TextChangeWatcher, DeviceAccountLogin.AutoLoginCallback,
+        OnMenuItemClickListener {
 
     protected static final int PROGRESS_MAX = 100;
 
@@ -75,7 +80,7 @@
     protected UrlInputView mUrlInput;
     protected boolean mInVoiceMode;
     protected View mContainer;
-
+    private View mUaSwitcher;
 
     // Auto-login UI
     protected View mAutoLogin;
@@ -385,6 +390,14 @@
         return mAutoLoginHandler != null;
     }
 
+    public void setUaSwitcher(View v) {
+        if (mUaSwitcher != null) {
+            mUaSwitcher.setOnClickListener(null);
+        }
+        mUaSwitcher = v;
+        mUaSwitcher.setOnClickListener(this);
+    }
+
     @Override
     public void onClick(View v) {
         if (mAutoLoginCancel == v) {
@@ -402,10 +415,45 @@
                 mAutoLoginHandler.login(
                         mAutoLoginAccount.getSelectedItemPosition(), this);
             }
+        } else if (mUaSwitcher == v) {
+            BrowserSettings settings = BrowserSettings.getInstance();
+            WebView web = getCurrentWebView();
+            if (web == null) return;
+            boolean desktop = settings.hasDesktopUseragent(web);
+            PopupMenu popup = new PopupMenu(mContext, mUaSwitcher);
+            Menu menu = popup.getMenu();
+            popup.getMenuInflater().inflate(R.menu.ua_switcher, menu);
+            menu.findItem(R.id.ua_mobile_menu_id).setChecked(!desktop);
+            menu.findItem(R.id.ua_desktop_menu_id).setChecked(desktop);
+            popup.setOnMenuItemClickListener(this);
+            popup.show();
         }
     }
 
     @Override
+    public boolean onMenuItemClick(MenuItem item) {
+        BrowserSettings settings = BrowserSettings.getInstance();
+        WebView web = getCurrentWebView();
+        if (web == null) return false;
+        boolean desktop = settings.hasDesktopUseragent(web);
+        switch (item.getItemId()) {
+        case R.id.ua_mobile_menu_id:
+            if (desktop) {
+                settings.toggleDesktopUseragent(web);
+                web.loadUrl(web.getOriginalUrl());
+            }
+            return true;
+        case R.id.ua_desktop_menu_id:
+            if (!desktop) {
+                settings.toggleDesktopUseragent(web);
+                web.loadUrl(web.getOriginalUrl());
+            }
+            return true;
+        }
+        return false;
+    }
+
+    @Override
     public void onFocusChange(View view, boolean hasFocus) {
         // if losing focus and not in touch mode, leave as is
         if (hasFocus || view.isInTouchMode() || mUrlInput.needsUpdate()) {
diff --git a/src/com/android/browser/TitleBarPhone.java b/src/com/android/browser/TitleBarPhone.java
index 97fe747..75b9fb7 100644
--- a/src/com/android/browser/TitleBarPhone.java
+++ b/src/com/android/browser/TitleBarPhone.java
@@ -71,6 +71,7 @@
         Resources res = context.getResources();
         mStopDrawable = res.getDrawable(R.drawable.ic_stop_holo_dark);
         mRefreshDrawable = res.getDrawable(R.drawable.ic_refresh_holo_dark);
+        setUaSwitcher(mFavicon);
     }
 
     @Override
diff --git a/src/com/android/browser/TitleBarXLarge.java b/src/com/android/browser/TitleBarXLarge.java
index 8c03e4c..91709cc 100644
--- a/src/com/android/browser/TitleBarXLarge.java
+++ b/src/com/android/browser/TitleBarXLarge.java
@@ -62,8 +62,6 @@
     private ImageView mVoiceSearch;
     private Drawable mFocusDrawable;
     private Drawable mUnfocusDrawable;
-    private boolean mHasFocus = false;
-    private BrowserSettings mSettings;
 
     public TitleBarXLarge(Activity activity, UiController controller,
             XLargeUi ui, FrameLayout parent) {
@@ -77,10 +75,7 @@
         mUnfocusDrawable = resources.getDrawable(
                 R.drawable.textfield_default_holo_dark);
         mInVoiceMode = false;
-        mSettings = BrowserSettings.getInstance();
         initLayout(activity, R.layout.url_bar);
-        PreferenceManager.getDefaultSharedPreferences(activity)
-                .registerOnSharedPreferenceChangeListener(mSharedPrefsListener);
     }
 
     @Override
@@ -109,7 +104,7 @@
         mGoButton.setOnClickListener(this);
         mClearButton.setOnClickListener(this);
         mVoiceSearch.setOnClickListener(this);
-        mUrlIcon.setOnClickListener(this);
+        setUaSwitcher(mUrlIcon);
         mUrlInput.setContainer(mUrlContainer);
         setFocusState(false);
     }
@@ -187,13 +182,6 @@
             clearOrClose();
         } else if (mVoiceSearch == v) {
             mUiController.startVoiceSearch();
-        } else if (mUrlIcon == v) {
-            WebView web = mUiController.getCurrentWebView();
-            if (mSettings.enableUseragentSwitcher() && web != null) {
-                mSettings.toggleDesktopUseragent(web);
-                web.loadUrl(web.getOriginalUrl());
-                updateUrlIcon();
-            }
         } else {
             super.onClick(v);
         }
@@ -213,38 +201,14 @@
     }
 
     void updateUrlIcon() {
-        if (mHasFocus) {
-            return;
-        }
-        if (!mInVoiceMode && mSettings.enableUseragentSwitcher()) {
-            WebView web = mUiController.getCurrentWebView();
-            if (mSettings.hasDesktopUseragent(web)) {
-                mUrlIcon.setImageResource(R.drawable.ic_ua_desktop);
-            } else {
-                mUrlIcon.setImageResource(R.drawable.ic_ua_android);
-            }
-        } else {
-            mUrlIcon.setImageResource(mInVoiceMode ?
-                    R.drawable.ic_search_holo_dark
-                    : R.drawable.ic_web_holo_dark);
-        }
+        mUrlIcon.setImageResource(mInVoiceMode ?
+                R.drawable.ic_search_holo_dark
+                : R.drawable.ic_web_holo_dark);
     }
 
-    private OnSharedPreferenceChangeListener mSharedPrefsListener =
-            new OnSharedPreferenceChangeListener() {
-
-        @Override
-        public void onSharedPreferenceChanged(
-                SharedPreferences sharedPreferences, String key) {
-            updateUrlIcon();
-        }
-
-    };
-
     @Override
     protected void setFocusState(boolean focus) {
         super.setFocusState(focus);
-        mHasFocus = focus;
         if (focus) {
             mSearchButton.setVisibility(View.GONE);
             mStar.setVisibility(View.GONE);
diff --git a/src/com/android/browser/preferences/LabPreferencesFragment.java b/src/com/android/browser/preferences/LabPreferencesFragment.java
index d9ef3df..0dd8805 100644
--- a/src/com/android/browser/preferences/LabPreferencesFragment.java
+++ b/src/com/android/browser/preferences/LabPreferencesFragment.java
@@ -45,7 +45,6 @@
         // Load the XML preferences file
         addPreferencesFromResource(R.xml.lab_preferences);
         registerChangeListener(PreferenceKeys.PREF_ENABLE_QUICK_CONTROLS);
-        registerChangeListener(PreferenceKeys.PREF_ENABLE_USERAGENT_SWITCHER);
         useInstantPref = findPreference(PreferenceKeys.PREF_USE_INSTANT_SEARCH);
     }
 
@@ -81,17 +80,6 @@
             startActivity(new Intent(BrowserActivity.ACTION_RESTART, null,
                     getActivity(), BrowserActivity.class));
         }
-        if (PreferenceKeys.PREF_ENABLE_USERAGENT_SWITCHER.equals(key)) {
-            if ((Boolean)newValue) {
-                // Show the help
-                LayoutInflater inflater = LayoutInflater.from(getActivity());
-                View content = inflater.inflate(R.layout.help_dialog_useragent_switcher, null);
-                new AlertDialog.Builder(getActivity())
-                        .setView(content)
-                        .setNeutralButton(android.R.string.ok, null)
-                        .show();
-            }
-        }
         return true;
     }
 }