restore all tabs on demand

	Bug: 3214151
	introduced new flag to determine if all tabs should be restored
   	controlled by the Ui implementation

Change-Id: I3e296f87a93fae54693bca186bb06ecd6db11d02
diff --git a/src/com/android/browser/BaseUi.java b/src/com/android/browser/BaseUi.java
index 052c9c5..832b0b3 100644
--- a/src/com/android/browser/BaseUi.java
+++ b/src/com/android/browser/BaseUi.java
@@ -43,6 +43,8 @@
 import android.widget.LinearLayout;
 import android.widget.Toast;
 
+import java.util.List;
+
 /**
  * UI interface definitions
  */
@@ -280,6 +282,11 @@
     }
 
     @Override
+    public boolean needsRestoreAllTabs() {
+        return mXLargeScreenSize;
+    }
+
+    @Override
     public void addTab(Tab tab) {
         if (mXLargeScreenSize) {
             mTabBar.onNewTab(tab);
@@ -313,6 +320,13 @@
     }
 
     @Override
+    public void updateTabs(List<Tab> tabs) {
+        if (mXLargeScreenSize) {
+            mTabBar.updateTabs(tabs);
+        }
+    }
+
+    @Override
     public void removeTab(Tab tab) {
         if (mTabControl.getCurrentTab() == tab) {
             removeTabFromContentView(tab);
diff --git a/src/com/android/browser/Controller.java b/src/com/android/browser/Controller.java
index 63f104a..79fff3d 100644
--- a/src/com/android/browser/Controller.java
+++ b/src/com/android/browser/Controller.java
@@ -83,6 +83,7 @@
 import java.net.URLEncoder;
 import java.util.Calendar;
 import java.util.HashMap;
+import java.util.List;
 
 /**
  * Controller for browser
@@ -239,11 +240,12 @@
         Calendar yesterday = Calendar.getInstance();
         yesterday.add(Calendar.DATE, -1);
 
-        boolean dontRestoreIncognitoTabs = lastActiveDate == null
+        boolean restoreIncognitoTabs = !(lastActiveDate == null
             || lastActiveDate.before(yesterday)
-            || lastActiveDate.after(today);
+            || lastActiveDate.after(today));
 
-        if (!mTabControl.restoreState(icicle, dontRestoreIncognitoTabs)) {
+        if (!mTabControl.restoreState(icicle, restoreIncognitoTabs,
+                mUi.needsRestoreAllTabs())) {
             // there is no quit on Android. But if we can't restore the state,
             // we can treat it as a new Browser, remove the old session cookies.
             CookieManager.getInstance().removeSessionCookie();
@@ -280,7 +282,8 @@
                 loadUrlDataIn(t, urlData);
             }
         } else {
-            if (dontRestoreIncognitoTabs) {
+            mUi.updateTabs(mTabControl.getTabs());
+            if (!restoreIncognitoTabs) {
                 WebView.cleanupPrivateBrowsingFiles(mActivity);
             }
             // TabControl.restoreState() will create a new tab even if
@@ -338,6 +341,11 @@
         return mTabControl;
     }
 
+    @Override
+    public List<Tab> getTabs() {
+        return mTabControl.getTabs();
+    }
+
     // Open the icon database and retain all the icons for visited sites.
     private void retainIconsOnStartup() {
         final WebIconDatabase db = WebIconDatabase.getInstance();
diff --git a/src/com/android/browser/TabBar.java b/src/com/android/browser/TabBar.java
index 169f934..69e0bd2 100644
--- a/src/com/android/browser/TabBar.java
+++ b/src/com/android/browser/TabBar.java
@@ -40,6 +40,7 @@
 import android.widget.TextView;
 
 import java.util.HashMap;
+import java.util.List;
 import java.util.Map;
 
 /**
@@ -96,20 +97,23 @@
         // TODO: Change enabled states based on whether you can go
         // back/forward.  Probably should be done inside onPageStarted.
 
-        // build tabs
-        int tabcount = mTabControl.getTabCount();
-        for (int i = 0; i < tabcount; i++) {
-            Tab tab = mTabControl.getTab(i);
-            TabViewData data = buildTab(tab);
-            TabView tv = buildView(data);
-        }
-        mTabs.setSelectedTab(mTabControl.getCurrentIndex());
+        updateTabs(mUiController.getTabs());
 
         mUserRequestedUrlbar = false;
         mTitleVisible = true;
         mButtonWidth = -1;
     }
 
+    void updateTabs(List<Tab> tabs) {
+        mTabs.clearTabs();
+        mTabMap.clear();
+        for (Tab tab : tabs) {
+            TabViewData data = buildTab(tab);
+            TabView tv = buildView(data);
+        }
+        mTabs.setSelectedTab(mTabControl.getCurrentIndex());
+    }
+
     @Override
     protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
         if (mButtonWidth == -1) {
@@ -270,7 +274,7 @@
             }
             if (mTabData.mTab != null) {
                 mIncognito.setVisibility(
-                        mTabData.mTab.getWebView().isPrivateBrowsingEnabled() ?
+                        mTabData.mTab.isPrivateBrowsingEnabled() ?
                         View.VISIBLE : View.GONE);
             }
         }
diff --git a/src/com/android/browser/TabControl.java b/src/com/android/browser/TabControl.java
index aeffbc0..2d90d23 100644
--- a/src/com/android/browser/TabControl.java
+++ b/src/com/android/browser/TabControl.java
@@ -26,6 +26,7 @@
 import java.io.File;
 import java.util.ArrayList;
 import java.util.HashMap;
+import java.util.List;
 import java.util.Vector;
 
 class TabControl {
@@ -99,6 +100,13 @@
     }
 
     /**
+     * return the list of tabs
+     */
+    List<Tab> getTabs() {
+        return mTabs;
+    }
+
+    /**
      * Return the tab at the specified index.
      * @return The Tab for the specified index or null if the tab does not
      *         exist.
@@ -281,10 +289,14 @@
     /**
      * Restore the state of all the tabs.
      * @param inState The saved state of all the tabs.
+     * @param restoreIncognitoTabs Restoring private browsing tabs
+     * @param restoreAll All webviews get restored, not just the current tab
+     *        (this does not override handling of incognito tabs)
      * @return True if there were previous tabs that were restored. False if
      *         there was no saved state or restoring the state failed.
      */
-    boolean restoreState(Bundle inState, boolean dontRestoreIncognitoTabs) {
+    boolean restoreState(Bundle inState, boolean restoreIncognitoTabs,
+            boolean restoreAll) {
         final int numTabs = (inState == null)
                 ? -1 : inState.getInt(Tab.NUMTABS, -1);
         if (numTabs == -1) {
@@ -295,7 +307,7 @@
             // Determine whether the saved current tab can be restored, and
             // if not, which tab will take its place.
             int currentTab = -1;
-            if (!dontRestoreIncognitoTabs
+            if (restoreIncognitoTabs
                     || !inState.getBundle(Tab.WEBVIEW + oldCurrentTab).getBoolean(Tab.INCOGNITO)) {
                 currentTab = oldCurrentTab;
             } else {
@@ -317,13 +329,15 @@
             for (int i = 0; i < numTabs; i++) {
                 Bundle state = inState.getBundle(Tab.WEBVIEW + i);
 
-                if (dontRestoreIncognitoTabs && state != null && state.getBoolean(Tab.INCOGNITO)) {
+                if (!restoreIncognitoTabs && state != null && state.getBoolean(Tab.INCOGNITO)) {
                     originalTabIndices.put(i, -1);
-                } else if (i == currentTab) {
+                } else if (i == currentTab || restoreAll) {
                     Tab t = createNewTab();
                     // Me must set the current tab before restoring the state
                     // so that all the client classes are set.
-                    setCurrentTab(t);
+                    if (i == currentTab) {
+                        setCurrentTab(t);
+                    }
                     if (!t.restoreState(state)) {
                         Log.w(LOGTAG, "Fail in restoreState, load home page.");
                         t.getWebView().loadUrl(BrowserSettings.getInstance()
diff --git a/src/com/android/browser/UI.java b/src/com/android/browser/UI.java
index b290891..08cf33a 100644
--- a/src/com/android/browser/UI.java
+++ b/src/com/android/browser/UI.java
@@ -26,6 +26,8 @@
 import android.view.View;
 import android.webkit.WebChromeClient.CustomViewCallback;
 
+import java.util.List;
+
 /**
  * UI interface definitions
  */
@@ -41,12 +43,16 @@
 
     public boolean onBackKey();
 
+    public boolean needsRestoreAllTabs();
+
     public void addTab(Tab tab);
 
     public void removeTab(Tab tab);
 
     public void setActiveTab(Tab tab);
 
+    public void updateTabs(List<Tab> tabs);
+
     public void detachTab(Tab tab);
 
     public void attachTab(Tab tab);
diff --git a/src/com/android/browser/UiController.java b/src/com/android/browser/UiController.java
index 1426132..dffebba 100644
--- a/src/com/android/browser/UiController.java
+++ b/src/com/android/browser/UiController.java
@@ -19,6 +19,8 @@
 import android.content.Intent;
 import android.webkit.WebView;
 
+import java.util.List;
+
 
 /**
  * UI aspect of the controller
@@ -33,6 +35,8 @@
 
     TabControl getTabControl();
 
+    List<Tab> getTabs();
+
     Tab openTabToHomePage();
 
     Tab openIncognitoTab();