close external tabs on back

      Bug: 4466495
      close externally created tabs
      when the back button is used and no further
      back history is available

Change-Id: If605474d929fb759226a1e1f4125bfaab43e6653
diff --git a/src/com/android/browser/Controller.java b/src/com/android/browser/Controller.java
index 142375d..89b700b 100644
--- a/src/com/android/browser/Controller.java
+++ b/src/com/android/browser/Controller.java
@@ -2415,6 +2415,9 @@
                 // Now we close the other tab
                 closeTab(current);
             } else {
+                if ((current.getAppId() != null) || current.closeOnBack()) {
+                    closeCurrentTab();
+                }
                 /*
                  * Instead of finishing the activity, simply push this to the back
                  * of the stack and let ActivityManager to choose the foreground
diff --git a/src/com/android/browser/IntentHandler.java b/src/com/android/browser/IntentHandler.java
index 6ed5b72..c76197d 100644
--- a/src/com/android/browser/IntentHandler.java
+++ b/src/com/android/browser/IntentHandler.java
@@ -17,10 +17,6 @@
 
 package com.android.browser;
 
-import com.android.browser.search.SearchEngine;
-import com.android.common.Search;
-import com.android.common.speech.LoggingEvents;
-
 import android.app.Activity;
 import android.app.SearchManager;
 import android.content.ContentResolver;
@@ -36,6 +32,10 @@
 import android.text.TextUtils;
 import android.util.Patterns;
 
+import com.android.browser.search.SearchEngine;
+import com.android.common.Search;
+import com.android.common.speech.LoggingEvents;
+
 import java.util.HashMap;
 import java.util.Iterator;
 import java.util.Map;
@@ -187,6 +187,9 @@
                     Tab tab = mController.openTab(urlData);
                     if (tab != null) {
                         tab.setAppId(appId);
+                        if ((intent.getFlags() & Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT) != 0) {
+                            tab.setCloseOnBack(true);
+                        }
                     }
                 }
             } else {
diff --git a/src/com/android/browser/Tab.java b/src/com/android/browser/Tab.java
index a251212..39bf49c 100644
--- a/src/com/android/browser/Tab.java
+++ b/src/com/android/browser/Tab.java
@@ -145,6 +145,8 @@
     // Application identifier used to find tabs that another application wants
     // to reuse.
     private String mAppId;
+    // flag to indicate if tab should be closed on back
+    private boolean mCloseOnBack;
     // Keep the original url around to avoid killing the old WebView if the url
     // has not changed.
     // Error console for the tab
@@ -225,6 +227,7 @@
     static final String APPID = "appid";
     static final String INCOGNITO = "privateBrowsingEnabled";
     static final String USERAGENT = "useragent";
+    static final String CLOSEFLAG = "closeOnBack";
 
     // -------------------------------------------------------------------------
 
@@ -1785,6 +1788,14 @@
         mAppId = id;
     }
 
+    boolean closeOnBack() {
+        return mCloseOnBack;
+    }
+
+    void setCloseOnBack(boolean close) {
+        mCloseOnBack = close;
+    }
+
     String getUrl() {
         return UrlUtils.filteredUrl(mCurrentState.mUrl);
     }
@@ -1895,6 +1906,7 @@
         if (mAppId != null) {
             mSavedState.putString(APPID, mAppId);
         }
+        mSavedState.putBoolean(CLOSEFLAG, mCloseOnBack);
         // Remember the parent tab so the relationship can be restored.
         if (mParent != null) {
             mSavedState.putLong(PARENTTAB, mParent.mId);
@@ -1916,6 +1928,7 @@
         // This will maintain the app id, original url and close-on-exit values.
         mId = b.getLong(ID);
         mAppId = b.getString(APPID);
+        mCloseOnBack = b.getBoolean(CLOSEFLAG);
         if (b.getBoolean(USERAGENT)
                 != mSettings.hasDesktopUseragent(getWebView())) {
             mSettings.toggleDesktopUseragent(getWebView());