Fix for BlackScreen Bug

Cancel request to go to the NavScreen
and discard callback if tab state changes.

Change-Id: I3b29023c10c2a3821f36a43e465df27d663c0825
diff --git a/src/com/android/browser/BaseUi.java b/src/com/android/browser/BaseUi.java
index 4d8c5a7..b0f9129 100644
--- a/src/com/android/browser/BaseUi.java
+++ b/src/com/android/browser/BaseUi.java
@@ -281,7 +281,9 @@
     public void addTab(Tab tab) {
     }
 
-    @Override
+    public void cancelNavScreenRequest(){
+    }
+
     public void setActiveTab(final Tab tab) {
         if (tab == null) return;
         Tab tabToRemove = null;
@@ -298,6 +300,7 @@
             }
         }
         mActiveTab = tab;
+
         BrowserWebView web = (BrowserWebView) mActiveTab.getWebView();
         updateUrlBarAutoShowManagerTarget();
         attachTabToContentView(tab);
@@ -764,6 +767,7 @@
     }
 
     protected WebView getWebView() {
+
         if (mActiveTab != null) {
             return mActiveTab.getWebView();
         } else {
diff --git a/src/com/android/browser/Controller.java b/src/com/android/browser/Controller.java
index 6b5e424..dd3d76c 100644
--- a/src/com/android/browser/Controller.java
+++ b/src/com/android/browser/Controller.java
@@ -2874,9 +2874,14 @@
     public void setActiveTab(Tab tab) {
         // monkey protection against delayed start
         if (tab != null) {
+
+            //Not going to the Nav Screen AnyMore. Unless NavScreen is already showing.
+            mUi.cancelNavScreenRequest();
             mTabControl.setCurrentTab(tab);
             // the tab is guaranteed to have a webview after setCurrentTab
             mUi.setActiveTab(tab);
+
+
             tab.setTimeStamp();
             //Purge active tabs
             MemoryMonitor.purgeActiveTabs(mActivity.getApplicationContext(), this, mSettings);
@@ -2892,6 +2897,8 @@
     }
 
     protected void reuseTab(Tab appTab, UrlData urlData) {
+        //Cancel navscreen request
+        mUi.cancelNavScreenRequest();
         // Dismiss the subwindow if applicable.
         dismissSubWindow(appTab);
         // Since we might kill the WebView, remove it from the
diff --git a/src/com/android/browser/PhoneUi.java b/src/com/android/browser/PhoneUi.java
index 5a656a2..7edaa7f 100644
--- a/src/com/android/browser/PhoneUi.java
+++ b/src/com/android/browser/PhoneUi.java
@@ -53,6 +53,7 @@
     private AnimScreen mAnimScreen;
     private NavigationBarPhone mNavigationBar;
     private Activity mBrowser;
+    private boolean mNavScreenRequested = false;
 
     boolean mAnimating;
     boolean mShowNav = false;
@@ -232,26 +233,50 @@
         hideNavScreen(mUiController.getTabControl().getCurrentPosition(), animate);
     }
 
+    //Unblock touch events
+    private void unblockEvents() {
+        mUiController.setBlockEvents(false);
+    }
+    //Block touch events
+    private void blockEvents() {
+        mUiController.setBlockEvents(true);
+    }
+
+    @Override
+    public void cancelNavScreenRequest() {
+        mNavScreenRequested = false;
+    }
+
     void showNavScreen() {
         WebView webView = getWebView();
         if (webView != null) {
-            mShowNav = true;
-            dismissIME();
-            mUiController.setBlockEvents(true);
-
+            blockEvents();
+            mNavScreenRequested = true;
             webView.getContentBitmapAsync(1.0f,
                             new Rect(),
                             new ValueCallback<Bitmap>() {
                                 @Override
                                 public void onReceiveValue(Bitmap bitmap) {
-                                    onShowNavScreenContinue(bitmap);
+
+                                    // If something interrupted the NavScreen request, discard
+                                    // the callback
+                                    if (mNavScreenRequested) {
+                                        onShowNavScreenContinue(bitmap);
+                                    } else {
+                                        unblockEvents();
+                                    }
+
                                 }
                             });
         }
     }
 
-    void onShowNavScreenContinue(Bitmap viewportBitmap) {
 
+
+    void onShowNavScreenContinue(Bitmap viewportBitmap) {
+        dismissIME();
+        mShowNav = true;
+        mNavScreenRequested = false;
         if (mNavScreen == null) {
             mNavScreen = new NavScreen(mActivity, mUiController, this);
             mCustomViewContainer.addView(mNavScreen, COVER_SCREEN_PARAMS);
@@ -315,12 +340,12 @@
             public void onAnimationEnd(Animator anim) {
                 mCustomViewContainer.removeView(mAnimScreen.mMain);
                 finishAnimationIn();
-                mUiController.setBlockEvents(false);
+                unblockEvents();
             }
         });
         set1.playSequentially(inanim, disappear);
         set1.start();
-        mUiController.setBlockEvents(false);
+        unblockEvents();
     }
 
     private void finishAnimationIn() {
@@ -332,6 +357,7 @@
     }
 
     void hideNavScreen(int position, boolean animate) {
+
         mShowNav = false;
         if (!showingNavScreen()) return;
         final Tab tab = mUiController.getTabControl().getTab(position);
@@ -356,7 +382,7 @@
             finishAnimateOut();
             return;
         }
-        mUiController.setBlockEvents(true);
+        blockEvents();
         mUiController.setActiveTab(tab);
         mContentView.setVisibility(View.VISIBLE);
         if (mAnimScreen == null) {
@@ -466,7 +492,7 @@
             public void onAnimationEnd(Animator anim) {
                 mCustomViewContainer.removeView(mAnimScreen.mMain);
                 finishAnimateOut();
-                mUiController.setBlockEvents(false);
+                unblockEvents();
             }
         });
         otheralpha.setInterpolator(new DecelerateInterpolator());
diff --git a/src/com/android/browser/UI.java b/src/com/android/browser/UI.java
index e916a35..172509b 100644
--- a/src/com/android/browser/UI.java
+++ b/src/com/android/browser/UI.java
@@ -60,6 +60,8 @@
 
     public void setActiveTab(Tab tab);
 
+    public void cancelNavScreenRequest();
+
     public void updateTabs(List<Tab> tabs);
 
     public void detachTab(Tab tab);