fix restore bug

    Bug: 5023284
    only save tab id's if the tab state can be saved
    this prevents trying to restore tabs that didn't
    get saved

Change-Id: I9ca8a3f71c4cb9029718492fbd5447e782524869
diff --git a/src/com/android/browser/TabControl.java b/src/com/android/browser/TabControl.java
index 5f3995f..1be2016 100644
--- a/src/com/android/browser/TabControl.java
+++ b/src/com/android/browser/TabControl.java
@@ -297,10 +297,12 @@
         long[] ids = new long[numTabs];
         int i = 0;
         for (Tab tab : mTabs) {
-            ids[i++] = tab.getId();
             if (tab.saveState()) {
+                ids[i++] = tab.getId();
                 outState.putBundle(Long.toString(tab.getId()),
                         tab.getSavedState(saveImages));
+            } else {
+                ids[i++] = -1;
             }
         }
         if (!outState.isEmpty()) {
@@ -327,13 +329,12 @@
         }
         final long oldcurrent = inState.getLong(CURRENT);
         long current = -1;
-        if (restoreIncognitoTabs ||
-                !inState.getBundle(Long.toString(oldcurrent)).getBoolean(Tab.INCOGNITO)) {
+        if (restoreIncognitoTabs || (hasState(oldcurrent, inState) && !isIncognito(oldcurrent, inState))) {
                 current = oldcurrent;
         } else {
             // pick first non incognito tab
             for (long id : ids) {
-                if (!inState.getBundle(Long.toString(id)).getBoolean(Tab.INCOGNITO)) {
+                if (hasState(id, inState) && !isIncognito(id, inState)) {
                     current = id;
                     break;
                 }
@@ -342,6 +343,20 @@
         return current;
     }
 
+    private boolean hasState(long id, Bundle state) {
+        if (id == -1) return false;
+        Bundle tab = state.getBundle(Long.toString(id));
+        return ((tab != null) && !tab.isEmpty());
+    }
+
+    private boolean isIncognito(long id, Bundle state) {
+        Bundle tabstate = state.getBundle(Long.toString(id));
+        if ((tabstate != null) && !tabstate.isEmpty()) {
+            return tabstate.getBoolean(Tab.INCOGNITO);
+        }
+        return false;
+    }
+
     /**
      * Restore the state of all the tabs.
      * @param currentId The tab id to restore.