From dc8471c2f8f1eff88160f224ba82d3db634606c6 Mon Sep 17 00:00:00 2001 From: Tom Klein Date: Sat, 12 Nov 2016 09:09:17 -0600 Subject: [PATCH] Bug 1302936 - Ignore tab clicks when the tab has already been removed. r=sebastian MozReview-Commit-ID: K0eUnMHI9j1 --HG-- extra : rebase_source : 69ffde7ceb4cddc11116ae63a126c8e023e01195 --- .../base/java/org/mozilla/gecko/Tabs.java | 2 +- .../mozilla/gecko/tabs/TabsGridLayout.java | 23 ++++++++++--------- .../org/mozilla/gecko/tabs/TabsLayout.java | 18 +++++++++++---- 3 files changed, 27 insertions(+), 16 deletions(-) diff --git a/mobile/android/base/java/org/mozilla/gecko/Tabs.java b/mobile/android/base/java/org/mozilla/gecko/Tabs.java index 279e5df2ef8b..c7e024fe0346 100644 --- a/mobile/android/base/java/org/mozilla/gecko/Tabs.java +++ b/mobile/android/base/java/org/mozilla/gecko/Tabs.java @@ -265,7 +265,7 @@ public class Tabs implements GeckoEventListener { // This avoids a NPE below, but callers need to be careful to // handle this case. if (tab == null || oldTab == tab) { - return null; + return tab; } mSelectedTab = tab; diff --git a/mobile/android/base/java/org/mozilla/gecko/tabs/TabsGridLayout.java b/mobile/android/base/java/org/mozilla/gecko/tabs/TabsGridLayout.java index af380c610b98..ead7db9fe63f 100644 --- a/mobile/android/base/java/org/mozilla/gecko/tabs/TabsGridLayout.java +++ b/mobile/android/base/java/org/mozilla/gecko/tabs/TabsGridLayout.java @@ -102,13 +102,14 @@ class TabsGridLayout extends GridView setOnItemClickListener(new OnItemClickListener() { @Override public void onItemClick(AdapterView parent, View view, int position, long id) { - final TabsLayoutItemView tab = (TabsLayoutItemView) view; - final int tabId = tab.getTabId(); - Tabs.getInstance().selectTab(tabId); + final TabsLayoutItemView tabView = (TabsLayoutItemView) view; + final int tabId = tabView.getTabId(); + final Tab tab = Tabs.getInstance().selectTab(tabId); + if (tab == null) { + return; + } autoHidePanel(); - Tabs.getInstance().notifyListeners( - Tabs.getInstance().getTab(tabId), Tabs.TabEvents.OPENED_FROM_TABS_TRAY - ); + Tabs.getInstance().notifyListeners(tab, Tabs.TabEvents.OPENED_FROM_TABS_TRAY); } }); @@ -577,11 +578,11 @@ class TabsGridLayout extends GridView if (!mSwiping) { final TabsLayoutItemView item = (TabsLayoutItemView) mSwipeView; final int tabId = item.getTabId(); - Tabs.getInstance().selectTab(tabId); - autoHidePanel(); - Tabs.getInstance().notifyListeners( - Tabs.getInstance().getTab(tabId), Tabs.TabEvents.OPENED_FROM_TABS_TRAY - ); + final Tab tab = Tabs.getInstance().selectTab(tabId); + if (tab != null) { + autoHidePanel(); + Tabs.getInstance().notifyListeners(tab, Tabs.TabEvents.OPENED_FROM_TABS_TRAY); + } mVelocityTracker.recycle(); mVelocityTracker = null; diff --git a/mobile/android/base/java/org/mozilla/gecko/tabs/TabsLayout.java b/mobile/android/base/java/org/mozilla/gecko/tabs/TabsLayout.java index e6f0501df718..d5362f1f10c6 100644 --- a/mobile/android/base/java/org/mozilla/gecko/tabs/TabsLayout.java +++ b/mobile/android/base/java/org/mozilla/gecko/tabs/TabsLayout.java @@ -132,10 +132,15 @@ public abstract class TabsLayout extends RecyclerView public void onItemClicked(RecyclerView recyclerView, int position, View v) { final TabsLayoutItemView item = (TabsLayoutItemView) v; final int tabId = item.getTabId(); - final Tabs tabs = Tabs.getInstance(); - tabs.selectTab(tabId); + final Tab tab = Tabs.getInstance().selectTab(tabId); + if (tab == null) { + // The tab that was clicked no longer exists in the tabs list (which can happen if you + // tap on a tab while its remove animation is running), so ignore the click. + return; + } + autoHidePanel(); - tabs.notifyListeners(tabs.getTab(tabId), Tabs.TabEvents.OPENED_FROM_TABS_TRAY); + Tabs.getInstance().notifyListeners(tab, Tabs.TabEvents.OPENED_FROM_TABS_TRAY); } // Updates the selected position in the list so that it will be scrolled to the right place. @@ -165,9 +170,14 @@ public abstract class TabsLayout extends RecyclerView private void closeTab(View view) { final TabsLayoutItemView itemView = (TabsLayoutItemView) view; final Tab tab = getTabForView(itemView); + if (tab == null) { + // We can be null here if this is the second closeTab call resulting from a sufficiently + // fast double tap on the close tab button. + return; + } + final boolean closingLastTab = tabsAdapter.getItemCount() == 1; Tabs.getInstance().closeTab(tab, true); - if (closingLastTab) { autoHidePanel(); }