hardware keyboard support

	 http://b/issue?id=3368141
	 http://b/issue?id=3368100
	 Removed duplicate key handling
	 added focus transition from page to url bar
	 once framework bug http://b/issue?id=3381411
	 is fixed, focus transitions in url bar will be fixed

Change-Id: Id5011e78bf70d51a25a4e01cf31d9e6ad95ceab7
diff --git a/src/com/android/browser/BaseUi.java b/src/com/android/browser/BaseUi.java
index 568a7e9..02d9199 100644
--- a/src/com/android/browser/BaseUi.java
+++ b/src/com/android/browser/BaseUi.java
@@ -32,6 +32,7 @@
 import android.view.Menu;
 import android.view.View;
 import android.view.View.OnClickListener;
+import android.view.View.OnKeyListener;
 import android.view.ViewGroup;
 import android.view.ViewGroup.LayoutParams;
 import android.view.WindowManager;
@@ -113,7 +114,6 @@
         mCustomViewContainer = (FrameLayout) mBrowserFrameLayout
                 .findViewById(R.id.fullscreen_custom_content);
         frameLayout.addView(mBrowserFrameLayout, COVER_SCREEN_PARAMS);
-
     }
 
     /**
@@ -286,6 +286,8 @@
             Log.w(LOGTAG, "mContainer is already attached to content in"
                     + " attachTabToContentView!");
         }
+        mainView.setNextFocusUpId(R.id.url_focused);
+        mainView.setNextFocusDownId(R.id.url_focused);
         mUiController.attachSubWindow(tab);
     }
 
diff --git a/src/com/android/browser/BrowserActivity.java b/src/com/android/browser/BrowserActivity.java
index a67b4e6..527f025 100644
--- a/src/com/android/browser/BrowserActivity.java
+++ b/src/com/android/browser/BrowserActivity.java
@@ -94,8 +94,6 @@
         if (((AccessibilityManager) getSystemService(ACCESSIBILITY_SERVICE))
                 .isEnabled()) {
             setDefaultKeyMode(DEFAULT_KEYS_DISABLE);
-        } else {
-            setDefaultKeyMode(DEFAULT_KEYS_SEARCH_LOCAL);
         }
 
         mController = new Controller(this);
diff --git a/src/com/android/browser/Controller.java b/src/com/android/browser/Controller.java
index 6c6fe0b..16c57c6 100644
--- a/src/com/android/browser/Controller.java
+++ b/src/com/android/browser/Controller.java
@@ -63,7 +63,6 @@
 import android.view.ContextMenu.ContextMenuInfo;
 import android.view.Gravity;
 import android.view.KeyEvent;
-import android.view.LayoutInflater;
 import android.view.Menu;
 import android.view.MenuInflater;
 import android.view.MenuItem;
@@ -78,7 +77,6 @@
 import android.webkit.WebIconDatabase;
 import android.webkit.WebSettings;
 import android.webkit.WebView;
-import android.widget.TextView;
 
 import java.io.ByteArrayOutputStream;
 import java.io.File;
@@ -2020,7 +2018,6 @@
     }
 
     protected void reuseTab(Tab appTab, String appId, UrlData urlData) {
-        Log.i(LOGTAG, "Reusing tab for " + appId);
         // Dismiss the subwindow if applicable.
         dismissSubWindow(appTab);
         // Since we might kill the WebView, remove it from the
@@ -2413,67 +2410,28 @@
                     return true;
                 }
                 break;
-            case KeyEvent.KEYCODE_B:
-                if (ctrl) {
-                    bookmarksOrHistoryPicker(false);
-                    return true;
-                }
-                break;
+//          case KeyEvent.KEYCODE_B:    // menu
             case KeyEvent.KEYCODE_C:
                 if (ctrl) {
                     webView.copySelection();
                     return true;
                 }
                 break;
-            case KeyEvent.KEYCODE_D:
-                if (ctrl) {
-                    bookmarkCurrentPage(AddBookmarkPage.DEFAULT_FOLDER_ID);
-                    return true;
-                }
-                break;
+//          case KeyEvent.KEYCODE_D:    // menu
 //          case KeyEvent.KEYCODE_E:    // in Chrome: puts '?' in URL bar
-            case KeyEvent.KEYCODE_F:
-                if (ctrl) {
-                    webView.showFindDialog(null, true);
-                    return true;
-                }
-                break;
+//          case KeyEvent.KEYCODE_F:    // menu
 //          case KeyEvent.KEYCODE_G:    // in Chrome: finds next match
-            case KeyEvent.KEYCODE_H:
-                if (ctrl) {
-                    bookmarksOrHistoryPicker(true);
-                    return true;
-                }
-                break;
+//          case KeyEvent.KEYCODE_H:    // menu
 //          case KeyEvent.KEYCODE_I:    // unused
-            case KeyEvent.KEYCODE_J:
-                if (ctrl) {
-                    viewDownloads();
-                    return true;
-                }
-                break;
+//          case KeyEvent.KEYCODE_J:    // menu
 //          case KeyEvent.KEYCODE_K:    // in Chrome: puts '?' in URL bar
-            case KeyEvent.KEYCODE_L:
-                if (ctrl) {
-                    editUrl();
-                    return true;
-                }
-                break;
+//          case KeyEvent.KEYCODE_L:    // menu
 //          case KeyEvent.KEYCODE_M:    // unused
 //          case KeyEvent.KEYCODE_N:    // in Chrome: new window
 //          case KeyEvent.KEYCODE_O:    // in Chrome: open file
 //          case KeyEvent.KEYCODE_P:    // in Chrome: print page
 //          case KeyEvent.KEYCODE_Q:    // unused
-            case KeyEvent.KEYCODE_R:
-                if (ctrl) {
-                    if (mInLoad) {
-                        stopLoading();
-                    } else {
-                        webView.reload();
-                    }
-                    return true;
-                }
-                break;
+//            case KeyEvent.KEYCODE_R:
 //          case KeyEvent.KEYCODE_S:    // in Chrome: saves page
             case KeyEvent.KEYCODE_T:
                 if (ctrl) {
@@ -2487,17 +2445,13 @@
                 break;
 //          case KeyEvent.KEYCODE_U:    // in Chrome: opens source of page
 //          case KeyEvent.KEYCODE_V:    // text view intercepts to paste
-            case KeyEvent.KEYCODE_W:
-                if (ctrl) {
-                    closeCurrentTab();
-                    return true;
-                }
-                break;
+//          case KeyEvent.KEYCODE_W:    // menu
 //          case KeyEvent.KEYCODE_X:    // text view intercepts to cut
 //          case KeyEvent.KEYCODE_Y:    // unused
 //          case KeyEvent.KEYCODE_Z:    // unused
         }
-        return false;
+        // if we get here, it is a regular key and webview is not null
+        return mUi.dispatchKey(keyCode, event);
     }
 
     boolean onKeyUp(int keyCode, KeyEvent event) {
diff --git a/src/com/android/browser/PhoneUi.java b/src/com/android/browser/PhoneUi.java
index 99fc4a0..4119c29 100644
--- a/src/com/android/browser/PhoneUi.java
+++ b/src/com/android/browser/PhoneUi.java
@@ -22,6 +22,7 @@
 import android.util.Log;
 import android.view.ActionMode;
 import android.view.Gravity;
+import android.view.KeyEvent;
 import android.view.Menu;
 import android.view.View;
 import android.view.ViewGroup;
@@ -269,4 +270,9 @@
         hideFakeTitleBar();
     }
 
+    @Override
+    public boolean dispatchKey(int code, KeyEvent event) {
+        return false;
+    }
+
 }
diff --git a/src/com/android/browser/UI.java b/src/com/android/browser/UI.java
index 4738522..8de2b19 100644
--- a/src/com/android/browser/UI.java
+++ b/src/com/android/browser/UI.java
@@ -20,6 +20,7 @@
 import android.graphics.Bitmap;
 import android.os.Bundle;
 import android.view.ActionMode;
+import android.view.KeyEvent;
 import android.view.Menu;
 import android.view.View;
 import android.webkit.WebChromeClient.CustomViewCallback;
@@ -117,4 +118,6 @@
 
     void showMaxTabsWarning();
 
+    boolean dispatchKey(int code, KeyEvent event);
+
 }
diff --git a/src/com/android/browser/XLargeUi.java b/src/com/android/browser/XLargeUi.java
index a9cc9fe..2f27aa1 100644
--- a/src/com/android/browser/XLargeUi.java
+++ b/src/com/android/browser/XLargeUi.java
@@ -24,6 +24,7 @@
 import android.util.Log;
 import android.view.ActionMode;
 import android.view.Gravity;
+import android.view.KeyEvent;
 import android.view.View;
 import android.webkit.WebChromeClient.CustomViewCallback;
 import android.webkit.WebView;
@@ -396,4 +397,24 @@
             mActivity.getActionBar().show();
         }
     }
+
+    @Override
+    public boolean dispatchKey(int code, KeyEvent event) {
+        WebView web = getActiveTab().getWebView();
+        switch (code) {
+            case KeyEvent.KEYCODE_TAB:
+            case KeyEvent.KEYCODE_DPAD_UP:
+            case KeyEvent.KEYCODE_DPAD_LEFT:
+                if ((web != null) && web.hasFocus()) {
+                    editUrl(true);
+                    return true;
+                }
+        }
+        if (event.isPrintingKey() && !mFakeTitleBar.isEditingUrl()) {
+            editUrl(true);
+            return mContentView.dispatchKeyEvent(event);
+        }
+        return false;
+    }
+
 }