Bug 835022 - Switch to correct tab when a tab is closed. r=mfinkle
authorBrian Nicholson <bnicholson@mozilla.com>
Fri, 15 Feb 2013 22:44:14 -0800
changeset 131988 c3e41bbb40a58d0e77d27c49a4baba8edf6e5588
parent 131987 1d136a0c8e3f80b060f4d4ce3dc3191ba9dd8297
child 131989 d72db91639711e5cfbd4a3b949e5c1029223933b
child 131994 00c09b326fee9d461fe6409b6df90ce521557725
push id2323
push userbbajaj@mozilla.com
push dateMon, 01 Apr 2013 19:47:02 +0000
treeherdermozilla-beta@7712be144d91 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersmfinkle
bugs835022
milestone21.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 835022 - Switch to correct tab when a tab is closed. r=mfinkle
mobile/android/base/Tabs.java
--- a/mobile/android/base/Tabs.java
+++ b/mobile/android/base/Tabs.java
@@ -170,25 +170,42 @@ public class Tabs implements GeckoEventL
             }
         });
 
         // Pass a message to Gecko to update tab state in BrowserApp
         GeckoAppShell.sendEventToGecko(GeckoEvent.createBroadcastEvent("Tab:Selected", String.valueOf(tab.getId())));
         return tab;
     }
 
-    public int getIndexOf(Tab tab) {
+    private int getIndexOf(Tab tab) {
         return mOrder.lastIndexOf(tab);
     }
 
-    public Tab getTabAt(int index) {
-        if (index >= 0 && index < mOrder.size())
-            return mOrder.get(index);
-        else
-            return null;
+    private Tab getNextTabFrom(Tab tab, boolean getPrivate) {
+        int numTabs = mOrder.size();
+        int index = getIndexOf(tab);
+        for (int i = index + 1; i < numTabs; i++) {
+            Tab next = mOrder.get(i);
+            if (next.isPrivate() == getPrivate) {
+                return next;
+            }
+        }
+        return null;
+    }
+
+    private Tab getPreviousTabFrom(Tab tab, boolean getPrivate) {
+        int numTabs = mOrder.size();
+        int index = getIndexOf(tab);
+        for (int i = index - 1; i >= 0; i--) {
+            Tab prev = mOrder.get(i);
+            if (prev.isPrivate() == getPrivate) {
+                return prev;
+            }
+        }
+        return null;
     }
 
     /**
      * Gets the selected tab.
      *
      * The selected tab can be null if we're doing a session restore after a
      * crash and Gecko isn't ready yet.
      *
@@ -240,20 +257,25 @@ public class Tabs implements GeckoEventL
     }
 
     /** Return the tab that will be selected by default after this one is closed */
     public Tab getNextTab(Tab tab) {
         Tab selectedTab = getSelectedTab();
         if (selectedTab != tab)
             return selectedTab;
 
-        int index = getIndexOf(tab);
-        Tab nextTab = getTabAt(index + 1);
+        boolean getPrivate = tab.isPrivate();
+        Tab nextTab = getNextTabFrom(tab, getPrivate);
         if (nextTab == null)
-            nextTab = getTabAt(index - 1);
+            nextTab = getPreviousTabFrom(tab, getPrivate);
+        if (nextTab == null && getPrivate) {
+            // If there are no private tabs remaining, get the last normal tab
+            Tab lastTab = mOrder.get(mOrder.size() - 1);
+            nextTab = getPreviousTabFrom(lastTab, false);
+        }
 
         Tab parent = getTab(tab.getParentId());
         if (parent != null) {
             // If the next tab is a sibling, switch to it. Otherwise go back to the parent.
             if (nextTab != null && nextTab.getParentId() == tab.getParentId())
                 return nextTab;
             else
                 return parent;