From 89ed582051658d6b5967fa378ba764f151e5da62 Mon Sep 17 00:00:00 2001 From: Jim Chen Date: Thu, 15 Dec 2016 18:23:54 -0500 Subject: [PATCH] Bug 1322714 - Convert RecentTabsAdapter and GeckoPreferences events; r=sebastian Convert "ClosedTabs:Data" and "Sanitize:Finished" events used in RecentTabsAdapter and GeckoPreferences from NativeJSObject events to GeckoBundle events. --- .../mozilla/gecko/home/RecentTabsAdapter.java | 97 +++++++++---------- .../gecko/preferences/GeckoPreferences.java | 32 ++---- mobile/android/components/SessionStore.js | 2 +- 3 files changed, 54 insertions(+), 77 deletions(-) diff --git a/mobile/android/base/java/org/mozilla/gecko/home/RecentTabsAdapter.java b/mobile/android/base/java/org/mozilla/gecko/home/RecentTabsAdapter.java index 3091f77da95f..d0b6fb7d4289 100755 --- a/mobile/android/base/java/org/mozilla/gecko/home/RecentTabsAdapter.java +++ b/mobile/android/base/java/org/mozilla/gecko/home/RecentTabsAdapter.java @@ -25,9 +25,9 @@ import org.mozilla.gecko.R; import org.mozilla.gecko.SessionParser; import org.mozilla.gecko.home.CombinedHistoryAdapter.RecentTabsUpdateHandler; import org.mozilla.gecko.home.CombinedHistoryPanel.PanelStateUpdateHandler; +import org.mozilla.gecko.util.BundleEventListener; import org.mozilla.gecko.util.EventCallback; -import org.mozilla.gecko.util.NativeEventListener; -import org.mozilla.gecko.util.NativeJSObject; +import org.mozilla.gecko.util.GeckoBundle; import org.mozilla.gecko.util.ThreadUtils; import java.util.ArrayList; @@ -37,7 +37,8 @@ import static org.mozilla.gecko.home.CombinedHistoryItem.ItemType; import static org.mozilla.gecko.home.CombinedHistoryPanel.OnPanelLevelChangeListener.PanelLevel.CHILD_RECENT_TABS; public class RecentTabsAdapter extends RecyclerView.Adapter - implements CombinedHistoryRecyclerView.AdapterContextMenuBuilder, NativeEventListener { + implements CombinedHistoryRecyclerView.AdapterContextMenuBuilder, + BundleEventListener { private static final String LOGTAG = "GeckoRecentTabsAdapter"; private static final int NAVIGATION_BACK_BUTTON_INDEX = 0; @@ -82,26 +83,26 @@ public class RecentTabsAdapter extends RecyclerView.Adapter } public void startListeningForClosedTabs() { - EventDispatcher.getInstance().registerGeckoThreadListener(this, "ClosedTabs:Data"); + EventDispatcher.getInstance().registerUiThreadListener(this, "ClosedTabs:Data"); GeckoAppShell.notifyObservers("ClosedTabs:StartNotifications", null); } public void stopListeningForClosedTabs() { GeckoAppShell.notifyObservers("ClosedTabs:StopNotifications", null); - EventDispatcher.getInstance().unregisterGeckoThreadListener(this, "ClosedTabs:Data"); + EventDispatcher.getInstance().unregisterUiThreadListener(this, "ClosedTabs:Data"); recentlyClosedTabsReceived = false; } public void startListeningForHistorySanitize() { - EventDispatcher.getInstance().registerGeckoThreadListener(this, "Sanitize:Finished"); + EventDispatcher.getInstance().registerUiThreadListener(this, "Sanitize:Finished"); } public void stopListeningForHistorySanitize() { - EventDispatcher.getInstance().unregisterGeckoThreadListener(this, "Sanitize:Finished"); + EventDispatcher.getInstance().unregisterUiThreadListener(this, "Sanitize:Finished"); } @Override - public void handleMessage(String event, NativeJSObject message, EventCallback callback) { + public void handleMessage(String event, GeckoBundle message, EventCallback callback) { switch (event) { case "ClosedTabs:Data": updateRecentlyClosedTabs(message); @@ -112,39 +113,35 @@ public class RecentTabsAdapter extends RecyclerView.Adapter } } - private void updateRecentlyClosedTabs(NativeJSObject message) { - final NativeJSObject[] tabs = message.getObjectArray("tabs"); + private void updateRecentlyClosedTabs(final GeckoBundle message) { + final GeckoBundle[] tabs = message.getBundleArray("tabs"); final int length = tabs.length; final ClosedTab[] closedTabs = new ClosedTab[length]; for (int i = 0; i < length; i++) { - final NativeJSObject tab = tabs[i]; - closedTabs[i] = new ClosedTab(tab.getString("url"), tab.getString("title"), tab.getObject("data").toString()); + final GeckoBundle tab = tabs[i]; + closedTabs[i] = new ClosedTab(tab.getString("url"), tab.getString("title"), + tab.getString("data")); } - // Only modify recentlyClosedTabs on the UI thread. - ThreadUtils.postToUiThread(new Runnable() { - @Override - public void run() { - // Save some data about the old panel state, so we can be - // smarter about notifying the recycler view which bits changed. - int prevClosedTabsCount = recentlyClosedTabs.length; - boolean prevSectionHeaderVisibility = isSectionHeaderVisible(); - int prevSectionHeaderIndex = getSectionHeaderIndex(); + // Save some data about the old panel state, so we can be + // smarter about notifying the recycler view which bits changed. + int prevClosedTabsCount = recentlyClosedTabs.length; + boolean prevSectionHeaderVisibility = isSectionHeaderVisible(); + int prevSectionHeaderIndex = getSectionHeaderIndex(); - recentlyClosedTabs = closedTabs; - recentlyClosedTabsReceived = true; - recentTabsUpdateHandler.onRecentTabsCountUpdated( - getClosedTabsCount(), recentlyClosedTabsReceived); - panelStateUpdateHandler.onPanelStateUpdated(CHILD_RECENT_TABS); + recentlyClosedTabs = closedTabs; + recentlyClosedTabsReceived = true; + recentTabsUpdateHandler.onRecentTabsCountUpdated( + getClosedTabsCount(), recentlyClosedTabsReceived); + panelStateUpdateHandler.onPanelStateUpdated(CHILD_RECENT_TABS); - // Handle the section header hiding/unhiding. - updateHeaderVisibility(prevSectionHeaderVisibility, prevSectionHeaderIndex); + // Handle the section header hiding/unhiding. + updateHeaderVisibility(prevSectionHeaderVisibility, prevSectionHeaderIndex); - // Update the "Recently closed" part of the tab list. - updateTabsList(prevClosedTabsCount, recentlyClosedTabs.length, getFirstRecentTabIndex(), getLastRecentTabIndex()); - } - }); + // Update the "Recently closed" part of the tab list. + updateTabsList(prevClosedTabsCount, recentlyClosedTabs.length, + getFirstRecentTabIndex(), getLastRecentTabIndex()); } private void readPreviousSessionData() { @@ -211,30 +208,24 @@ public class RecentTabsAdapter extends RecyclerView.Adapter private void clearLastSessionData() { final ClosedTab[] emptyLastSessionTabs = new ClosedTab[0]; - // Only modify mLastSessionTabs on the UI thread. - ThreadUtils.postToUiThread(new Runnable() { - @Override - public void run() { - // Save some data about the old panel state, so we can be - // smarter about notifying the recycler view which bits changed. - int prevClosedTabsCount = lastSessionTabs.length; - boolean prevSectionHeaderVisibility = isSectionHeaderVisible(); - int prevSectionHeaderIndex = getSectionHeaderIndex(); + // Save some data about the old panel state, so we can be + // smarter about notifying the recycler view which bits changed. + int prevClosedTabsCount = lastSessionTabs.length; + boolean prevSectionHeaderVisibility = isSectionHeaderVisible(); + int prevSectionHeaderIndex = getSectionHeaderIndex(); - lastSessionTabs = emptyLastSessionTabs; - recentTabsUpdateHandler.onRecentTabsCountUpdated( - getClosedTabsCount(), recentlyClosedTabsReceived); - panelStateUpdateHandler.onPanelStateUpdated(CHILD_RECENT_TABS); + lastSessionTabs = emptyLastSessionTabs; + recentTabsUpdateHandler.onRecentTabsCountUpdated( + getClosedTabsCount(), recentlyClosedTabsReceived); + panelStateUpdateHandler.onPanelStateUpdated(CHILD_RECENT_TABS); - // Handle the section header hiding. - updateHeaderVisibility(prevSectionHeaderVisibility, prevSectionHeaderIndex); + // Handle the section header hiding. + updateHeaderVisibility(prevSectionHeaderVisibility, prevSectionHeaderIndex); - // Handle the "tabs from last time" being cleared. - if (prevClosedTabsCount > 0) { - notifyItemRangeRemoved(getFirstLastSessionTabIndex(), prevClosedTabsCount); - } - } - }); + // Handle the "tabs from last time" being cleared. + if (prevClosedTabsCount > 0) { + notifyItemRangeRemoved(getFirstLastSessionTabIndex(), prevClosedTabsCount); + } } private void updateHeaderVisibility(boolean prevSectionHeaderVisibility, int prevSectionHeaderIndex) { diff --git a/mobile/android/base/java/org/mozilla/gecko/preferences/GeckoPreferences.java b/mobile/android/base/java/org/mozilla/gecko/preferences/GeckoPreferences.java index 2b015bb2de5a..cfe03b2dcb62 100644 --- a/mobile/android/base/java/org/mozilla/gecko/preferences/GeckoPreferences.java +++ b/mobile/android/base/java/org/mozilla/gecko/preferences/GeckoPreferences.java @@ -47,7 +47,6 @@ import org.mozilla.gecko.util.EventCallback; import org.mozilla.gecko.util.GeckoBundle; import org.mozilla.gecko.util.HardwareUtils; import org.mozilla.gecko.util.InputOptionsUtils; -import org.mozilla.gecko.util.NativeEventListener; import org.mozilla.gecko.util.NativeJSObject; import org.mozilla.gecko.util.ThreadUtils; @@ -106,7 +105,6 @@ public class GeckoPreferences extends AppCompatPreferenceActivity implements BundleEventListener, GeckoActivityStatus, - NativeEventListener, OnPreferenceChangeListener, OnSharedPreferenceChangeListener { @@ -371,8 +369,7 @@ public class GeckoPreferences // Use setResourceToOpen to specify these extras. Bundle intentExtras = getIntent().getExtras(); - EventDispatcher.getInstance().registerGeckoThreadListener((NativeEventListener) this, - "Sanitize:Finished"); + EventDispatcher.getInstance().registerUiThreadListener(this, "Sanitize:Finished"); // Add handling for long-press click. // This is only for Android 3.0 and below (which use the long-press-context-menu paradigm). @@ -507,8 +504,7 @@ public class GeckoPreferences protected void onDestroy() { super.onDestroy(); - EventDispatcher.getInstance().unregisterGeckoThreadListener((NativeEventListener) this, - "Sanitize:Finished"); + EventDispatcher.getInstance().unregisterUiThreadListener(this, "Sanitize:Finished"); if (mPrefsRequest != null) { PrefsHelper.removeObserver(mPrefsRequest); @@ -619,25 +615,15 @@ public class GeckoPreferences .fromEvent(message) .callback(callback) .buildAndShow(); - } - } - @Override - public void handleMessage(final String event, final NativeJSObject message, final EventCallback callback) { - try { - switch (event) { - case "Sanitize:Finished": - boolean success = message.getBoolean("success"); - final int stringRes = success ? R.string.private_data_success : R.string.private_data_fail; + } else if ("Sanitize:Finished".equals(event)) { + final boolean success = message.getBoolean("success"); + final int stringRes = success ? R.string.private_data_success : R.string.private_data_fail; - SnackbarBuilder.builder(GeckoPreferences.this) - .message(stringRes) - .duration(Snackbar.LENGTH_LONG) - .buildAndShow(); - break; - } - } catch (Exception e) { - Log.e(LOGTAG, "Exception handling message \"" + event + "\":", e); + SnackbarBuilder.builder(this) + .message(stringRes) + .duration(Snackbar.LENGTH_LONG) + .buildAndShow(); } } diff --git a/mobile/android/components/SessionStore.js b/mobile/android/components/SessionStore.js index 8d5bc1fdf01c..9a8bb7de66f3 100644 --- a/mobile/android/components/SessionStore.js +++ b/mobile/android/components/SessionStore.js @@ -1766,7 +1766,7 @@ SessionStore.prototype = { return { url: lastEntry.url, title: lastEntry.title || "", - data: tab + data: JSON.stringify(tab), }; });