Titlebar on phone UI update

	 introduce new states for titlebar:
	 normal, highlighted, editing, loading

Change-Id: I8a6f88afc3457a07063aba40ef4d3e5f6c806b8f
diff --git a/src/com/android/browser/TitleBarBase.java b/src/com/android/browser/TitleBarBase.java
index ae11038..de44306 100644
--- a/src/com/android/browser/TitleBarBase.java
+++ b/src/com/android/browser/TitleBarBase.java
@@ -493,13 +493,6 @@
     }
 
     protected void setFocusState(boolean focus) {
-        if (focus) {
-            updateSearchMode(false);
-        }
-    }
-
-    protected void updateSearchMode(boolean userEdited) {
-        setSearchMode(!userEdited || TextUtils.isEmpty(mUrlInput.getUserText()));
     }
 
     protected void setSearchMode(boolean voiceSearchEnabled) {}
@@ -523,8 +516,6 @@
     @Override
     public void onTextChanged(String newText) {
         if (mUrlInput.hasFocus()) {
-            // check if input field is empty and adjust voice search state
-            updateSearchMode(true);
             // clear voice mode when user types
             setInVoiceMode(false, null);
         }
diff --git a/src/com/android/browser/TitleBarPhone.java b/src/com/android/browser/TitleBarPhone.java
index 528ea43..f41eca7 100644
--- a/src/com/android/browser/TitleBarPhone.java
+++ b/src/com/android/browser/TitleBarPhone.java
@@ -16,8 +16,6 @@
 
 package com.android.browser;
 
-import com.android.browser.autocomplete.SuggestedTextController.TextChangeWatcher;
-
 import android.app.Activity;
 import android.content.Context;
 import android.content.res.Resources;
@@ -29,9 +27,11 @@
 import android.view.View.OnFocusChangeListener;
 import android.webkit.WebView;
 import android.widget.FrameLayout;
-import android.widget.ImageButton;
 import android.widget.ImageView;
 
+import com.android.browser.UrlInputView.StateListener;
+import com.android.browser.autocomplete.SuggestedTextController.TextChangeWatcher;
+
 import java.util.List;
 
 /**
@@ -39,16 +39,17 @@
  * browser.
  */
 public class TitleBarPhone extends TitleBarBase implements OnFocusChangeListener,
-        OnClickListener, TextChangeWatcher {
+        OnClickListener, TextChangeWatcher, StateListener {
 
     private Activity mActivity;
     private ImageView mStopButton;
     private ImageView mVoiceButton;
-    private boolean mHasLockIcon;
-    private ImageButton mForward;
     private Drawable mStopDrawable;
     private Drawable mRefreshDrawable;
     private View mTabSwitcher;
+    private View mComboIcon;
+    private View mTitleContainer;
+    private Drawable mTextfieldBgDrawable;
 
     public TitleBarPhone(Activity activity, UiController controller, PhoneUi ui,
             FrameLayout parent) {
@@ -66,16 +67,18 @@
         mStopButton.setOnClickListener(this);
         mVoiceButton = (ImageView) findViewById(R.id.voice);
         mVoiceButton.setOnClickListener(this);
-        mForward = (ImageButton) findViewById(R.id.forward);
-        mForward.setOnClickListener(this);
         mTabSwitcher = findViewById(R.id.tab_switcher);
         mTabSwitcher.setOnClickListener(this);
+        mComboIcon = findViewById(R.id.iconcombo);
+        mTitleContainer = findViewById(R.id.title_bg);
         setFocusState(false);
         Resources res = context.getResources();
         mStopDrawable = res.getDrawable(R.drawable.ic_stop_holo_dark);
         mRefreshDrawable = res.getDrawable(R.drawable.ic_refresh_holo_dark);
-        setUaSwitcher(mFavicon);
+        mTextfieldBgDrawable = res.getDrawable(R.drawable.textfield_active_holo_dark);
+        setUaSwitcher(mComboIcon);
         mUrlInput.setContainer(this);
+        mUrlInput.setStateListener(this);
     }
 
     @Override
@@ -99,29 +102,23 @@
     }
 
     @Override
-    protected void setFocusState(boolean focus) {
-        super.setFocusState(focus);
-        if (focus) {
-            mHasLockIcon = (mLockIcon.getVisibility() == View.VISIBLE);
-            mLockIcon.setVisibility(View.GONE);
-            mStopButton.setVisibility(View.GONE);
-            mVoiceButton.setVisibility(View.VISIBLE);
-        } else {
-            mLockIcon.setVisibility(mHasLockIcon ? View.VISIBLE : View.GONE);
-            mStopButton.setVisibility(View.VISIBLE);
-            mVoiceButton.setVisibility(View.GONE);
-        }
-    }
-
-    @Override
     void setProgress(int progress) {
         super.setProgress(progress);
         if (progress == 100) {
+            mStopButton.setVisibility(View.GONE);
             mStopButton.setImageDrawable(mRefreshDrawable);
-        } else if (mStopButton.getDrawable() != mStopDrawable) {
-            mStopButton.setImageDrawable(mStopDrawable);
+            if (!isEditingUrl()) {
+                mComboIcon.setVisibility(View.VISIBLE);
+            }
+        } else {
+            if (mStopButton.getDrawable() != mStopDrawable) {
+                mStopButton.setImageDrawable(mStopDrawable);
+                if (mStopButton.getVisibility() != View.VISIBLE) {
+                    mComboIcon.setVisibility(View.GONE);
+                    mStopButton.setVisibility(View.VISIBLE);
+                }
+            }
         }
-        updateNavigationState();
     }
 
     /**
@@ -138,7 +135,6 @@
                 mUrlInput.setText(title);
             }
             mUrlInput.setSelection(0);
-            updateNavigationState();
         }
     }
 
@@ -160,16 +156,12 @@
             } else {
                 WebView web = mBaseUi.getWebView();
                 if (web != null) {
+                    stopEditingUrl();
                     web.reload();
                 }
             }
         } else if (v == mVoiceButton) {
             mUiController.startVoiceSearch();
-        } else if (v == mForward) {
-            WebView web = mBaseUi.getWebView();
-            if (web != null) {
-                web.goForward();
-            }
         } else if (v == mTabSwitcher) {
             mBaseUi.onMenuKey();
         } else {
@@ -177,10 +169,30 @@
         }
     }
 
-    private void updateNavigationState() {
-        WebView web = mBaseUi.getWebView();
-        if (web != null) {
-          mForward.setVisibility(web.canGoForward() ? View.VISIBLE : View.GONE);
+    @Override
+    public void onStateChanged(int state) {
+        switch(state) {
+        case StateListener.STATE_NORMAL:
+            mComboIcon.setVisibility(View.VISIBLE);
+            mStopButton.setVisibility(View.GONE);
+            setSearchMode(false);
+            mTabSwitcher.setVisibility(View.VISIBLE);
+            mTitleContainer.setBackgroundDrawable(null);
+            break;
+        case StateListener.STATE_HIGHLIGHTED:
+            mComboIcon.setVisibility(View.GONE);
+            mStopButton.setVisibility(View.VISIBLE);
+            setSearchMode(true);
+            mTabSwitcher.setVisibility(View.GONE);
+            mTitleContainer.setBackgroundDrawable(mTextfieldBgDrawable);
+            break;
+        case StateListener.STATE_EDITED:
+            mComboIcon.setVisibility(View.GONE);
+            mStopButton.setVisibility(View.GONE);
+            setSearchMode(false);
+            mTabSwitcher.setVisibility(View.GONE);
+            mTitleContainer.setBackgroundDrawable(mTextfieldBgDrawable);
+            break;
         }
     }
 
diff --git a/src/com/android/browser/TitleBarXLarge.java b/src/com/android/browser/TitleBarXLarge.java
index 3d4d2ec..6aed86c 100644
--- a/src/com/android/browser/TitleBarXLarge.java
+++ b/src/com/android/browser/TitleBarXLarge.java
@@ -218,7 +218,6 @@
         mStopButton.setImageDrawable(mReloadDrawable);
     }
 
-    @Override
     protected void updateSearchMode(boolean userEdited) {
         setSearchMode(!userEdited || TextUtils.isEmpty(mUrlInput.getUserText()));
     }
diff --git a/src/com/android/browser/UrlInputView.java b/src/com/android/browser/UrlInputView.java
index 7545e6a..8b5c292 100644
--- a/src/com/android/browser/UrlInputView.java
+++ b/src/com/android/browser/UrlInputView.java
@@ -16,14 +16,6 @@
 
 package com.android.browser;
 
-import com.android.browser.SuggestionsAdapter.CompletionListener;
-import com.android.browser.SuggestionsAdapter.SuggestItem;
-import com.android.browser.UI.DropdownChangeListener;
-import com.android.browser.autocomplete.SuggestiveAutoCompleteTextView;
-import com.android.browser.search.SearchEngine;
-import com.android.browser.search.SearchEngineInfo;
-import com.android.browser.search.SearchEngines;
-
 import android.content.Context;
 import android.content.res.Configuration;
 import android.database.DataSetObserver;
@@ -32,6 +24,7 @@
 import android.util.AttributeSet;
 import android.util.Patterns;
 import android.view.KeyEvent;
+import android.view.MotionEvent;
 import android.view.View;
 import android.view.inputmethod.EditorInfo;
 import android.view.inputmethod.InputMethodManager;
@@ -40,6 +33,15 @@
 import android.widget.TextView;
 import android.widget.TextView.OnEditorActionListener;
 
+import com.android.browser.SuggestionsAdapter.CompletionListener;
+import com.android.browser.SuggestionsAdapter.SuggestItem;
+import com.android.browser.UI.DropdownChangeListener;
+import com.android.browser.autocomplete.SuggestedTextController.TextChangeWatcher;
+import com.android.browser.autocomplete.SuggestiveAutoCompleteTextView;
+import com.android.browser.search.SearchEngine;
+import com.android.browser.search.SearchEngineInfo;
+import com.android.browser.search.SearchEngines;
+
 import java.util.List;
 
 /**
@@ -48,13 +50,20 @@
  */
 public class UrlInputView extends SuggestiveAutoCompleteTextView
         implements OnEditorActionListener,
-        CompletionListener, OnItemClickListener {
-
+        CompletionListener, OnItemClickListener, TextChangeWatcher {
 
     static final String TYPED = "browser-type";
     static final String SUGGESTED = "browser-suggest";
     static final String VOICE = "voice-search";
 
+    static interface StateListener {
+        static final int STATE_NORMAL = 0;
+        static final int STATE_HIGHLIGHTED = 1;
+        static final int STATE_EDITED = 2;
+
+        public void onStateChanged(int state);
+    }
+
     private UrlInputListener   mListener;
     private InputMethodManager mInputManager;
     private SuggestionsAdapter mAdapter;
@@ -64,6 +73,9 @@
     private boolean mNeedsUpdate;
     private DropdownChangeListener mDropdownListener;
 
+    private int mState;
+    private StateListener mStateListener;
+
     public UrlInputView(Context context, AttributeSet attrs, int defStyle) {
         super(context, attrs, defStyle);
         init(context);
@@ -90,6 +102,7 @@
         setOnItemClickListener(this);
         mNeedsUpdate = false;
         mDropdownListener = null;
+        addQueryTextWatcher(this);
 
         mAdapter.registerDataSetObserver(new DataSetObserver() {
             @Override
@@ -105,6 +118,32 @@
                 dispatchChange();
             }
         });
+        mState = StateListener.STATE_NORMAL;
+    }
+
+    protected void onFocusChanged(boolean focused, int direction, Rect prevRect) {
+        super.onFocusChanged(focused, direction, prevRect);
+        if (focused) {
+            if (hasSelection()) {
+                changeState(StateListener.STATE_HIGHLIGHTED);
+            } else {
+                changeState(StateListener.STATE_EDITED);
+            }
+        } else {
+            // reset the selection state
+            changeState(StateListener.STATE_NORMAL);
+        }
+    }
+
+    @Override
+    public boolean onTouchEvent(MotionEvent evt) {
+        boolean hasSelection = hasSelection();
+        boolean res = super.onTouchEvent(evt);
+        if ((MotionEvent.ACTION_DOWN == evt.getActionMasked())
+              && hasSelection) {
+            changeState(StateListener.STATE_EDITED);
+        }
+        return res;
     }
 
     /**
@@ -135,6 +174,19 @@
         mListener = listener;
     }
 
+    public void setStateListener(StateListener listener) {
+        mStateListener = listener;
+        // update listener
+        changeState(mState);
+    }
+
+    private void changeState(int newState) {
+        mState = newState;
+        if (mStateListener != null) {
+            mStateListener.onStateChanged(mState);
+        }
+    }
+
     void setVoiceResults(List<String> voiceResults) {
         mAdapter.setVoiceResults(voiceResults);
     }
@@ -305,4 +357,12 @@
     public boolean requestRectangleOnScreen(Rect rect, boolean immediate) {
         return false;
     }
+
+    @Override
+    public void onTextChanged(String newText) {
+        if (StateListener.STATE_HIGHLIGHTED == mState) {
+            changeState(StateListener.STATE_EDITED);
+        }
+    }
+
 }