If a tab was opened solely to download a file, close the tab.

Fix for http://b/issue?id=2217707
diff --git a/src/com/android/browser/BrowserActivity.java b/src/com/android/browser/BrowserActivity.java
index d4a016d..b550714 100644
--- a/src/com/android/browser/BrowserActivity.java
+++ b/src/com/android/browser/BrowserActivity.java
@@ -2149,7 +2149,7 @@
         resetTitleIconAndProgress();
     }
 
-    private void goBackOnePageOrQuit() {
+    /* package */ void goBackOnePageOrQuit() {
         Tab current = mTabControl.getCurrentTab();
         if (current == null) {
             /*
diff --git a/src/com/android/browser/Tab.java b/src/com/android/browser/Tab.java
index afd9b09..512f2b7 100644
--- a/src/com/android/browser/Tab.java
+++ b/src/com/android/browser/Tab.java
@@ -48,6 +48,7 @@
 import android.view.View.OnClickListener;
 import android.webkit.ConsoleMessage;
 import android.webkit.CookieSyncManager;
+import android.webkit.DownloadListener;
 import android.webkit.GeolocationPermissions;
 import android.webkit.HttpAuthHandler;
 import android.webkit.SslErrorHandler;
@@ -123,6 +124,9 @@
     private final LayoutInflater mInflateService;
     // The BrowserActivity which owners the Tab
     private final BrowserActivity mActivity;
+    // The listener that gets invoked when a download is started from the
+    // mMainView
+    private final DownloadListener mDownloadListener;
 
     // AsyncTask for downloading touch icons
     DownloadTouchIcon mTouchIconLoader;
@@ -1224,6 +1228,27 @@
             (GeolocationPermissionsPrompt) mContainer.findViewById(
                 R.id.geolocation_permissions_prompt);
 
+        mDownloadListener = new DownloadListener() {
+            public void onDownloadStart(String url, String userAgent,
+                    String contentDisposition, String mimetype,
+                    long contentLength) {
+                mActivity.onDownloadStart(url, userAgent, contentDisposition,
+                        mimetype, contentLength);
+                if (mMainView.copyBackForwardList().getSize() == 0) {
+                    // This Tab was opened for the sole purpose of downloading a
+                    // file. Remove it.
+                    if (mActivity.getTabControl().getCurrentWebView()
+                            == mMainView) {
+                        // In this case, the Tab is still on top.
+                        mActivity.goBackOnePageOrQuit();
+                    } else {
+                        // In this case, it is not.
+                        mActivity.closeTab(Tab.this);
+                    }
+                }
+            }
+        };
+
         setWebView(w);
     }
 
@@ -1246,10 +1271,15 @@
 
         // set the new one
         mMainView = w;
-        // attached the WebViewClient and WebChromeClient
+        // attach the WebViewClient, WebChromeClient and DownloadListener
         if (mMainView != null) {
             mMainView.setWebViewClient(mWebViewClient);
             mMainView.setWebChromeClient(mWebChromeClient);
+            // Attach DownloadManager so that downloads can start in an active
+            // or a non-active window. This can happen when going to a site that
+            // does a redirect after a period of time. The user could have
+            // switched to another tab while waiting for the download to start.
+            mMainView.setDownloadListener(mDownloadListener);
         }
     }
 
@@ -1297,7 +1327,21 @@
             mSubView.setWebViewClient(new SubWindowClient(mWebViewClient));
             mSubView.setWebChromeClient(new SubWindowChromeClient(
                     mWebChromeClient));
-            mSubView.setDownloadListener(mActivity);
+            // Set a different DownloadListener for the mSubView, since it will
+            // just need to dismiss the mSubView, rather than close the Tab
+            mSubView.setDownloadListener(new DownloadListener() {
+                public void onDownloadStart(String url, String userAgent,
+                        String contentDisposition, String mimetype,
+                        long contentLength) {
+                    mActivity.onDownloadStart(url, userAgent,
+                            contentDisposition, mimetype, contentLength);
+                    if (mSubView.copyBackForwardList().getSize() == 0) {
+                        // This subwindow was opened for the sole purpose of
+                        // downloading a file. Remove it.
+                        dismissSubWindow();
+                    }
+                }
+            });
             mSubView.setOnCreateContextMenuListener(mActivity);
             final BrowserSettings s = BrowserSettings.getInstance();
             s.addObserver(mSubView.getSettings()).update(s, null);
diff --git a/src/com/android/browser/TabControl.java b/src/com/android/browser/TabControl.java
index 33b73e8..e64f3fb 100644
--- a/src/com/android/browser/TabControl.java
+++ b/src/com/android/browser/TabControl.java
@@ -533,11 +533,6 @@
         w.setMapTrackballToArrowKeys(false); // use trackball directly
         // Enable the built-in zoom
         w.getSettings().setBuiltInZoomControls(true);
-        // Attach DownloadManager so that downloads can start in an active or
-        // a non-active window. This can happen when going to a site that does
-        // a redirect after a period of time. The user could have switched to
-        // another tab while waiting for the download to start.
-        w.setDownloadListener(mActivity);
         // Add this WebView to the settings observer list and update the
         // settings
         final BrowserSettings s = BrowserSettings.getInstance();