Bug 1315359 - Refactor GeckoApp.onBackPressed to allow it to be used by both GeckoApp and CustomTabsActivity. r=snorp
authorDylan Roeh <droeh@mozilla.com>
Fri, 04 Nov 2016 15:06:09 -0500
changeset 351478 81f2a2b396f806b0b581fcaa83e54b2c41645fab
parent 351477 bb5895ace33fc2bbd37a99b7e20f091e3fee18d4
child 351479 7e61b840c16203a16144765b789f8777bf067369
push id6795
push userjlund@mozilla.com
push dateMon, 23 Jan 2017 14:19:46 +0000
treeherdermozilla-esr52@76101b503191 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewerssnorp
bugs1315359
milestone52.0a1
Bug 1315359 - Refactor GeckoApp.onBackPressed to allow it to be used by both GeckoApp and CustomTabsActivity. r=snorp
mobile/android/base/java/org/mozilla/gecko/GeckoApp.java
mobile/android/base/java/org/mozilla/gecko/customtabs/CustomTabsActivity.java
--- a/mobile/android/base/java/org/mozilla/gecko/GeckoApp.java
+++ b/mobile/android/base/java/org/mozilla/gecko/GeckoApp.java
@@ -2404,16 +2404,20 @@ public abstract class GeckoApp
             if (cleanupVersion != CURRENT_CLEANUP_VERSION) {
                 SharedPreferences.Editor editor = GeckoSharedPrefs.forApp(context).edit();
                 editor.putInt(CLEANUP_VERSION, CURRENT_CLEANUP_VERSION);
                 editor.apply();
             }
         }
     }
 
+    protected void onDone() {
+        moveTaskToBack(true);
+    }
+
     @Override
     public void onBackPressed() {
         if (getSupportFragmentManager().getBackStackEntryCount() > 0) {
             super.onBackPressed();
             return;
         }
 
         if (autoHideTabs()) {
@@ -2434,17 +2438,17 @@ public abstract class GeckoApp
         if (mLayerView != null && mLayerView.isFullScreen()) {
             GeckoAppShell.notifyObservers("FullScreen:Exit", null);
             return;
         }
 
         final Tabs tabs = Tabs.getInstance();
         final Tab tab = tabs.getSelectedTab();
         if (tab == null) {
-            moveTaskToBack(true);
+            onDone();
             return;
         }
 
         // Give Gecko a chance to handle the back press first, then fallback to the Java UI.
         GeckoAppShell.sendRequestToGecko(new GeckoRequest("Browser:OnBackPressed", null) {
             @Override
             public void onResponse(NativeJSObject nativeJSObject) {
                 if (!nativeJSObject.getBoolean("handled")) {
@@ -2464,17 +2468,17 @@ public abstract class GeckoApp
                 ThreadUtils.postToUiThread(new Runnable() {
                     @Override
                     public void run() {
                         if (tab.doBack()) {
                             return;
                         }
 
                         if (tab.isExternal()) {
-                            moveTaskToBack(true);
+                            onDone();
                             Tab nextSelectedTab = Tabs.getInstance().getNextTab(tab);
                             if (nextSelectedTab != null) {
                                 int nextSelectedTabId = nextSelectedTab.getId();
                                 GeckoAppShell.notifyObservers("Tab:KeepZombified", Integer.toString(nextSelectedTabId));
                             }
                             tabs.closeTab(tab);
                             return;
                         }
@@ -2482,17 +2486,17 @@ public abstract class GeckoApp
                         final int parentId = tab.getParentId();
                         final Tab parent = tabs.getTab(parentId);
                         if (parent != null) {
                             // The back button should always return to the parent (not a sibling).
                             tabs.closeTab(tab, parent);
                             return;
                         }
 
-                        moveTaskToBack(true);
+                        onDone();
                     }
                 });
             }
         });
     }
 
     @Override
     protected void onActivityResult(int requestCode, int resultCode, Intent data) {
--- a/mobile/android/base/java/org/mozilla/gecko/customtabs/CustomTabsActivity.java
+++ b/mobile/android/base/java/org/mozilla/gecko/customtabs/CustomTabsActivity.java
@@ -23,45 +23,12 @@ public class CustomTabsActivity extends 
     }
 
     @Override
     public int getLayout() {
         return R.layout.customtabs_activity;
     }
 
     @Override
-    public void onBackPressed() {
-        final Tabs tabs = Tabs.getInstance();
-        final Tab tab = tabs.getSelectedTab();
-
-        // Give Gecko a chance to handle the back press first, then fallback to the Java UI.
-        GeckoAppShell.sendRequestToGecko(new GeckoRequest("Browser:OnBackPressed", null) {
-            @Override
-            public void onResponse(NativeJSObject nativeJSObject) {
-                if (!nativeJSObject.getBoolean("handled")) {
-                    // Default behavior is Gecko didn't prevent.
-                    onDefault();
-                }
-            }
-
-            @Override
-            public void onError(NativeJSObject error) {
-                // Default behavior is Gecko didn't prevent, via failure.
-                onDefault();
-            }
-
-            // Return from Gecko thread, then back-press through the Java UI.
-            private void onDefault() {
-                ThreadUtils.postToUiThread(new Runnable() {
-                    @Override
-                    public void run() {
-                        if (tab.doBack()) {
-                            return;
-                        }
-
-                        tabs.closeTab(tab);
-                        finish();
-                    }
-                });
-            }
-        });
+    protected void onDone() {
+        finish();
     }
 }