OSDN Git Service

Remove the WebView from its container to change focus.
authorPatrick Scott <phanna@android.com>
Thu, 17 Sep 2009 12:00:31 +0000 (08:00 -0400)
committerPatrick Scott <phanna@android.com>
Thu, 17 Sep 2009 13:56:23 +0000 (09:56 -0400)
When the container is removed from the content view, its child views do not lose
focus. Removing the WebView from the container as well will trigger a focus
change from the WebView. Move the attach/remove logic to TabControl since it
knows all about the containers.

src/com/android/browser/BrowserActivity.java
src/com/android/browser/TabControl.java

index c8a717b..42bc1ab 100644 (file)
@@ -1761,7 +1761,7 @@ public class BrowserActivity extends Activity
     private void attachTabToContentView(TabControl.Tab t) {
         // Attach the container that contains the main WebView and any other UI
         // associated with the tab.
-        mContentView.addView(t.getContainer(), COVER_SCREEN_PARAMS);
+        t.attachTabToContentView(mContentView);
 
         if (mShouldShowErrorConsole) {
             ErrorConsoleView errorConsole = mTabControl.getCurrentErrorConsole(true);
@@ -1783,27 +1783,20 @@ public class BrowserActivity extends Activity
 
         WebView view = t.getWebView();
         view.setEmbeddedTitleBar(mTitleBar);
-        // Attach the sub window if necessary
-        attachSubWindow(t);
         // Request focus on the top window.
         t.getTopWindow().requestFocus();
     }
 
     // Attach a sub window to the main WebView of the given tab.
     private void attachSubWindow(TabControl.Tab t) {
-        // If a sub window exists, attach it to the content view.
-        final WebView subView = t.getSubWebView();
-        if (subView != null) {
-            final View container = t.getSubWebViewContainer();
-            mContentView.addView(container, COVER_SCREEN_PARAMS);
-            subView.requestFocus();
-        }
+        t.attachSubWindow(mContentView);
+        getTopWindow().requestFocus();
     }
 
     // Remove the given tab from the content view.
     private void removeTabFromContentView(TabControl.Tab t) {
         // Remove the container that contains the main WebView.
-        mContentView.removeView(t.getContainer());
+        t.removeTabFromContentView(mContentView);
 
         if (mTabControl.getCurrentErrorConsole(false) != null) {
             mErrorConsoleContainer.removeView(mTabControl.getCurrentErrorConsole(false));
@@ -1814,11 +1807,6 @@ public class BrowserActivity extends Activity
             view.setEmbeddedTitleBar(null);
         }
 
-        // Remove the sub window if it exists.
-        if (t.getSubWebView() != null) {
-            mContentView.removeView(t.getSubWebViewContainer());
-        }
-
         if (t == mTabControl.getCurrentTab()) {
             t.setLockIconType(getLockIconType());
             t.setPrevLockIconType(getPrevLockType());
@@ -1828,15 +1816,11 @@ public class BrowserActivity extends Activity
     // Remove the sub window if it exists. Also called by TabControl when the
     // user clicks the 'X' to dismiss a sub window.
     /* package */ void dismissSubWindow(TabControl.Tab t) {
-        final WebView mainView = t.getWebView();
-        if (t.getSubWebView() != null) {
-            // Remove the container view and request focus on the main WebView.
-            mContentView.removeView(t.getSubWebViewContainer());
-            mainView.requestFocus();
-            // Tell the TabControl to dismiss the subwindow. This will destroy
-            // the WebView.
-            mTabControl.dismissSubWindow(t);
-        }
+        t.removeSubWindow(mContentView);
+        // Tell the TabControl to dismiss the subwindow. This will destroy
+        // the WebView.
+        mTabControl.dismissSubWindow(t);
+        getTopWindow().requestFocus();
     }
 
     // A wrapper function of {@link #openTabAndShow(UrlData, boolean, String)}
index 4089cac..6e4bae2 100644 (file)
@@ -224,10 +224,6 @@ class TabControl {
 
             // The tab consists of a container view, which contains the main
             // WebView, as well as any other UI elements associated with the tab.
-            //
-            // FIXME: Fix the interaction between this layout and the animation
-            // used when switching to and from the tab picker. This may not be
-            // required if the tab selection UI is redesigned.
             LayoutInflater factory = LayoutInflater.from(context);
             mContainer = factory.inflate(R.layout.tab, null);
 
@@ -240,7 +236,7 @@ class TabControl {
 
         /**
          * Sets the WebView for this tab, correctly removing the old WebView
-         * from, and inserting the new WebView into, the container view.
+         * from the container view.
          */
         public void setWebView(WebView w) {
             if (mMainView == w) {
@@ -250,13 +246,65 @@ class TabControl {
             // permission requests are void.
             mGeolocationPermissionsPrompt.hide();
 
-            FrameLayout wrapper = (FrameLayout) mContainer.findViewById(R.id.webview_wrapper);
-            if (mMainView != null) {
-                wrapper.removeView(mMainView);
-            }
+            // Just remove the old one.
+            FrameLayout wrapper =
+                    (FrameLayout) mContainer.findViewById(R.id.webview_wrapper);
+            wrapper.removeView(mMainView);
             mMainView = w;
-            if (mMainView != null) {
-                wrapper.addView(mMainView);
+        }
+
+        /**
+         * This method attaches both the WebView and any sub window to the
+         * given content view.
+         */
+        public void attachTabToContentView(ViewGroup content) {
+            if (mMainView == null) {
+                return;
+            }
+
+            // Attach the WebView to the container and then attach the
+            // container to the content view.
+            FrameLayout wrapper =
+                    (FrameLayout) mContainer.findViewById(R.id.webview_wrapper);
+            wrapper.addView(mMainView);
+            content.addView(mContainer, BrowserActivity.COVER_SCREEN_PARAMS);
+            attachSubWindow(content);
+        }
+
+        /**
+         * Remove the WebView and any sub window from the given content view.
+         */
+        public void removeTabFromContentView(ViewGroup content) {
+            if (mMainView == null) {
+                return;
+            }
+
+            // Remove the container from the content and then remove the
+            // WebView from the container. This will trigger a focus change
+            // needed by WebView.
+            FrameLayout wrapper =
+                    (FrameLayout) mContainer.findViewById(R.id.webview_wrapper);
+            wrapper.removeView(mMainView);
+            content.removeView(mContainer);
+            removeSubWindow(content);
+        }
+
+        /**
+         * Attach the sub window to the content view.
+         */
+        public void attachSubWindow(ViewGroup content) {
+            if (mSubView != null) {
+                content.addView(mSubViewContainer,
+                        BrowserActivity.COVER_SCREEN_PARAMS);
+            }
+        }
+
+        /**
+         * Remove the sub window from the content view.
+         */
+        public void removeSubWindow(ViewGroup content) {
+            if (mSubView != null) {
+                content.removeView(mSubViewContainer);
             }
         }
 
@@ -283,13 +331,6 @@ class TabControl {
         }
 
         /**
-         * @return The container for this tab.
-         */
-        public View getContainer() {
-            return mContainer;
-        }
-
-        /**
          * @return The geolocation permissions prompt for this tab.
          */
         public GeolocationPermissionsPrompt getGeolocationPermissionsPrompt() {
@@ -305,15 +346,6 @@ class TabControl {
         }
 
         /**
-         * Return the subwindow container of this tab or null if there is no
-         * subwindow.
-         * @return The subwindow's container View.
-         */
-        public View getSubWebViewContainer() {
-            return mSubViewContainer;
-        }
-
-        /**
          * Get the url of this tab.  Valid after calling populatePickerData, but
          * before calling wipePickerData, or if the webview has been destroyed.
          * 
@@ -612,9 +644,10 @@ class TabControl {
             // observers.
             BrowserSettings.getInstance().deleteObserver(
                     t.mMainView.getSettings());
-            // Destroy the main view and subview
-            t.mMainView.destroy();
+            WebView w = t.mMainView;
             t.setWebView(null);
+            // Destroy the main view
+            w.destroy();
         }
         // clear it's references to parent and children
         t.removeFromTree();
@@ -674,8 +707,9 @@ class TabControl {
             if (t.mMainView != null) {
                 dismissSubWindow(t);
                 s.deleteObserver(t.mMainView.getSettings());
-                t.mMainView.destroy();
+                WebView w = t.mMainView;
                 t.setWebView(null);
+                w.destroy();
             }
         }
         mTabs.clear();
@@ -845,8 +879,9 @@ class TabControl {
         // Remove the WebView's settings from the BrowserSettings list of
         // observers.
         BrowserSettings.getInstance().deleteObserver(t.mMainView.getSettings());
-        t.mMainView.destroy();
+        WebView w = t.mMainView;
         t.setWebView(null);
+        w.destroy();
     }
 
     /**