Upstream version 10.39.225.0
[platform/framework/web/crosswalk.git] / src / chrome / android / shell / java / src / org / chromium / chrome / shell / ChromeShellToolbar.java
index 48b91fd..42f5ed2 100644 (file)
@@ -5,6 +5,7 @@
 package org.chromium.chrome.shell;
 
 import android.content.Context;
+import android.content.res.Configuration;
 import android.graphics.drawable.ClipDrawable;
 import android.util.AttributeSet;
 import android.view.KeyEvent;
@@ -18,11 +19,15 @@ import android.widget.LinearLayout;
 import android.widget.TextView;
 import android.widget.TextView.OnEditorActionListener;
 
+import org.chromium.base.ApiCompatibilityUtils;
+import org.chromium.base.CommandLine;
 import org.chromium.chrome.browser.EmptyTabObserver;
 import org.chromium.chrome.browser.Tab;
 import org.chromium.chrome.browser.TabObserver;
 import org.chromium.chrome.browser.appmenu.AppMenuButtonHelper;
 import org.chromium.chrome.browser.appmenu.AppMenuHandler;
+import org.chromium.chrome.shell.omnibox.SuggestionPopup;
+import org.chromium.content.common.ContentSwitches;
 
 /**
  * A Toolbar {@link View} that shows the URL and navigation buttons.
@@ -37,21 +42,50 @@ public class ChromeShellToolbar extends LinearLayout {
         }
     };
 
+    private final Runnable mUpdateProgressRunnable = new Runnable() {
+        @Override
+        public void run() {
+            mProgressDrawable.setLevel(100 * mProgress);
+            if (mLoading) {
+                mStopReloadButton.setImageResource(R.drawable.btn_stop_normal);
+            } else {
+                mStopReloadButton.setImageResource(R.drawable.btn_reload_normal);
+                ApiCompatibilityUtils.postOnAnimationDelayed(ChromeShellToolbar.this,
+                        mClearProgressRunnable, COMPLETED_PROGRESS_TIMEOUT_MS);
+            }
+        }
+    };
+
     private EditText mUrlTextView;
     private ClipDrawable mProgressDrawable;
 
     private ChromeShellTab mTab;
-    private final TabObserver mTabObserver = new TabObserverImpl();
+    private final TabObserver mTabObserver;
 
     private AppMenuHandler mMenuHandler;
     private AppMenuButtonHelper mAppMenuButtonHelper;
 
+    private SuggestionPopup mSuggestionPopup;
+
+    private ImageButton mStopReloadButton;
+    private int mProgress = 0;
+    private boolean mLoading = true;
+
     /**
      * @param context The Context the view is running in.
      * @param attrs   The attributes of the XML tag that is inflating the view.
      */
     public ChromeShellToolbar(Context context, AttributeSet attrs) {
         super(context, attrs);
+        // When running performance benchmark, we don't want to observe the tab
+        // invalidation which would interfere with browser's processing content
+        // frame. See crbug.com/394976.
+        if (CommandLine.getInstance().hasSwitch(
+                ContentSwitches.RUNNING_PERFORMANCE_BENCHMARK)) {
+            mTabObserver = new EmptyTabObserver();
+        } else {
+            mTabObserver = new TabObserverImpl();
+        }
     }
 
     /**
@@ -62,7 +96,7 @@ public class ChromeShellToolbar extends LinearLayout {
         if (mTab != null) mTab.removeObserver(mTabObserver);
         mTab = tab;
         mTab.addObserver(mTabObserver);
-        mUrlTextView.setText(mTab.getContentView().getUrl());
+        mUrlTextView.setText(mTab.getWebContents().getUrl());
     }
 
     private void onUpdateUrl(String url) {
@@ -71,8 +105,17 @@ public class ChromeShellToolbar extends LinearLayout {
 
     private void onLoadProgressChanged(int progress) {
         removeCallbacks(mClearProgressRunnable);
-        mProgressDrawable.setLevel((int) (100.0 * progress));
-        if (progress == 100) postDelayed(mClearProgressRunnable, COMPLETED_PROGRESS_TIMEOUT_MS);
+        removeCallbacks(mUpdateProgressRunnable);
+        mProgress = progress;
+        mLoading = progress != 100;
+        ApiCompatibilityUtils.postOnAnimation(this, mUpdateProgressRunnable);
+    }
+
+    /**
+     * Closes the suggestion popup.
+     */
+    public void hideSuggestions() {
+        if (mSuggestionPopup != null) mSuggestionPopup.hideSuggestions();
     }
 
     @Override
@@ -82,6 +125,7 @@ public class ChromeShellToolbar extends LinearLayout {
         mProgressDrawable = (ClipDrawable) findViewById(R.id.toolbar).getBackground();
         initializeUrlField();
         initializeMenuButton();
+        initializeStopReloadButton();
     }
 
     void setMenuHandler(AppMenuHandler menuHandler) {
@@ -97,14 +141,14 @@ public class ChromeShellToolbar extends LinearLayout {
             public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                 if ((actionId != EditorInfo.IME_ACTION_GO) && (event == null ||
                         event.getKeyCode() != KeyEvent.KEYCODE_ENTER ||
-                        event.getKeyCode() != KeyEvent.ACTION_DOWN)) {
+                        event.getAction() != KeyEvent.ACTION_DOWN)) {
                     return false;
                 }
 
                 mTab.loadUrlWithSanitization(mUrlTextView.getText().toString());
                 mUrlTextView.clearFocus();
                 setKeyboardVisibilityForUrl(false);
-                mTab.getContentView().requestFocus();
+                mTab.getView().requestFocus();
                 return true;
             }
         });
@@ -113,10 +157,27 @@ public class ChromeShellToolbar extends LinearLayout {
             public void onFocusChange(View v, boolean hasFocus) {
                 setKeyboardVisibilityForUrl(hasFocus);
                 if (!hasFocus) {
-                    mUrlTextView.setText(mTab.getContentView().getUrl());
+                    mUrlTextView.setText(mTab.getWebContents().getUrl());
+                    mSuggestionPopup.dismissPopup();
+                }
+            }
+        });
+        mUrlTextView.setOnKeyListener(new OnKeyListener() {
+            @Override
+            public boolean onKey(View v, int keyCode, KeyEvent event) {
+                if (keyCode == KeyEvent.KEYCODE_BACK) {
+                    mUrlTextView.clearFocus();
+                    if (mTab != null) {
+                        mTab.getView().requestFocus();
+                    }
+                    return true;
                 }
+                return false;
             }
         });
+
+        mSuggestionPopup = new SuggestionPopup(getContext(), mUrlTextView, this);
+        mUrlTextView.addTextChangedListener(mSuggestionPopup);
     }
 
     private void initializeMenuButton() {
@@ -135,7 +196,32 @@ public class ChromeShellToolbar extends LinearLayout {
         });
     }
 
-    private void setKeyboardVisibilityForUrl(boolean visible) {
+    private void initializeStopReloadButton() {
+        mStopReloadButton = (ImageButton) findViewById(R.id.stop_reload_button);
+        mStopReloadButton.setOnClickListener(new OnClickListener() {
+            @Override
+            public void onClick(View v) {
+                if (mLoading) {
+                    mTab.getWebContents().stop();
+                } else {
+                    mTab.getWebContents().getNavigationController().reload(true);
+                }
+            }
+        });
+    }
+
+    /**
+     * @return Current tab that is shown by ChromeShell.
+     */
+    public ChromeShellTab getCurrentTab() {
+        return mTab;
+    }
+
+    /**
+     * Change the visibility of the software keyboard.
+     * @param visible Whether the keyboard should be shown or hidden.
+     */
+    public void setKeyboardVisibilityForUrl(boolean visible) {
         InputMethodManager imm = (InputMethodManager) getContext().getSystemService(
                 Context.INPUT_METHOD_SERVICE);
         if (visible) {
@@ -145,6 +231,12 @@ public class ChromeShellToolbar extends LinearLayout {
         }
     }
 
+    @Override
+    protected void onConfigurationChanged(Configuration newConfig) {
+        super.onConfigurationChanged(newConfig);
+        if (mMenuHandler != null) mMenuHandler.hideAppMenu();
+    }
+
     private class TabObserverImpl extends EmptyTabObserver {
         @Override
         public void onLoadProgressChanged(Tab tab, int progress) {