You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@cordova.apache.org by ag...@apache.org on 2014/06/24 20:35:37 UTC

[2/2] git commit: First attempt at hooking back button up to exit app.

First attempt at hooking back button up to exit app.

Requires changes to cordova-android and x-walk plugin (so use master).
Currently has a bug where hitting back navigates to about:blank before
minimizing.


Project: http://git-wip-us.apache.org/repos/asf/cordova-app-harness/repo
Commit: http://git-wip-us.apache.org/repos/asf/cordova-app-harness/commit/3f7bd69c
Tree: http://git-wip-us.apache.org/repos/asf/cordova-app-harness/tree/3f7bd69c
Diff: http://git-wip-us.apache.org/repos/asf/cordova-app-harness/diff/3f7bd69c

Branch: refs/heads/master
Commit: 3f7bd69cc1f9005a1d20e2c00a6587e25867e8d1
Parents: a7d4a44
Author: Andrew Grieve <ag...@chromium.org>
Authored: Tue Jun 24 14:16:28 2014 -0400
Committer: Andrew Grieve <ag...@chromium.org>
Committed: Tue Jun 24 14:35:16 2014 -0400

----------------------------------------------------------------------
 AppHarnessUI/AppHarnessUI.java   | 24 +++++++++++++++++++++++-
 createproject.sh                 | 11 +++++++++++
 template-overrides/Activity.java | 29 +++++++++++++++++++++++++++++
 www/cdvah/js/InAppMenuCtrl.js    |  2 ++
 4 files changed, 65 insertions(+), 1 deletion(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/cordova-app-harness/blob/3f7bd69c/AppHarnessUI/AppHarnessUI.java
----------------------------------------------------------------------
diff --git a/AppHarnessUI/AppHarnessUI.java b/AppHarnessUI/AppHarnessUI.java
index 834603b..fd64295 100644
--- a/AppHarnessUI/AppHarnessUI.java
+++ b/AppHarnessUI/AppHarnessUI.java
@@ -58,6 +58,14 @@ public class AppHarnessUI extends CordovaPlugin {
     boolean slaveVisible;
     CallbackContext eventsCallback;
 
+    public boolean isSlaveVisible() {
+        return slaveVisible;
+    }
+
+    public boolean isSlaveCreated() {
+        return slaveWebView != null && slaveWebView.getParent() != null;
+    }
+
     @Override
     public boolean execute(String action, CordovaArgs args, final CallbackContext callbackContext) throws JSONException {
         if ("create".equals(action)) {
@@ -100,7 +108,7 @@ public class AppHarnessUI extends CordovaPlugin {
         return true;
     }
 
-    private void sendEvent(String eventName) {
+    public void sendEvent(String eventName) {
         if (eventsCallback != null) {
             PluginResult pluginResult = new PluginResult(PluginResult.Status.OK, eventName);
             pluginResult.setKeepCallback(true);
@@ -281,5 +289,19 @@ public class AppHarnessUI extends CordovaPlugin {
             // Needed for the view to stay in the bottom when rotating.
             setPivotY(h);
         }
+
+        @Override
+        public boolean backHistory() {
+            if (getView().getNavigationHistory().canGoBack()) {
+                return super.backHistory();
+            }
+            if (slaveVisible) {
+                sendEvent("showMenu");
+                return true;
+            }
+            // Should never get here since the webview does not have focus.
+            Log.w(LOG_TAG, "Somehow back button was pressed when app not visible");
+            return false;
+        }
     }
 }

http://git-wip-us.apache.org/repos/asf/cordova-app-harness/blob/3f7bd69c/createproject.sh
----------------------------------------------------------------------
diff --git a/createproject.sh b/createproject.sh
index 9c79286..e1094c9 100755
--- a/createproject.sh
+++ b/createproject.sh
@@ -92,6 +92,17 @@ set -x
 $CORDOVA platform add $PLATFORM_ARGS || exit 1
 set +x
 
+if [[ "$PLATFORMS" = *android* ]]; then
+    echo 'var fs = require("fs");
+          var fname = "platforms/android/src/org/chromium/appdevtool/ChromeAppDeveloperTool.java";
+          var tname = "'$AH_PATH'/template-overrides/Activity.java";
+          var orig = fs.readFileSync(fname, "utf8");
+          var templ = fs.readFileSync(tname, "utf8");
+          var newData = orig.replace(/}\s*$/, templ + "\n}\n").replace(/import.*?$/m, "import org.apache.appharness.AppHarnessUI;\n$&");
+          fs.writeFileSync(fname, newData);
+          ' | node || exit $?
+fi
+
 mkdir -p hooks/after_prepare
 cp "$AH_PATH"/template-overrides/after-hook.js hooks/after_prepare
 

http://git-wip-us.apache.org/repos/asf/cordova-app-harness/blob/3f7bd69c/template-overrides/Activity.java
----------------------------------------------------------------------
diff --git a/template-overrides/Activity.java b/template-overrides/Activity.java
new file mode 100644
index 0000000..1a6de33
--- /dev/null
+++ b/template-overrides/Activity.java
@@ -0,0 +1,29 @@
+
+    @Override
+    public void onBackPressed() {
+        // If app is running, quit it.
+        AppHarnessUI ahui = (AppHarnessUI)appView.getPlugin("AppHarnessUI");
+        if (ahui != null) {
+            if (ahui.isSlaveCreated()) {
+                ahui.sendEvent("quitApp");
+                return;
+            }
+        }
+        // Otherwise, hide instead of calling .finish().
+        moveTaskToBack(true);
+    }
+
+    @Override
+    public Object onMessage(String id, Object data) {
+        // Capture the app calling navigator.app.exitApp().
+        if ("exit".equals(id)) {
+            AppHarnessUI ahui = (AppHarnessUI)appView.getPlugin("AppHarnessUI");
+            if (ahui != null) {
+                if (ahui.isSlaveCreated()) {
+                    ahui.sendEvent("quitApp");
+                    return new Object();
+                }
+            }
+        }
+        return super.onMessage(id, data);
+    }

http://git-wip-us.apache.org/repos/asf/cordova-app-harness/blob/3f7bd69c/www/cdvah/js/InAppMenuCtrl.js
----------------------------------------------------------------------
diff --git a/www/cdvah/js/InAppMenuCtrl.js b/www/cdvah/js/InAppMenuCtrl.js
index ee21620..9ec9ed4 100644
--- a/www/cdvah/js/InAppMenuCtrl.js
+++ b/www/cdvah/js/InAppMenuCtrl.js
@@ -53,6 +53,8 @@
                 AppHarnessUI.setVisible(false);
             } else if (eventName == 'hideMenu') {
                 AppHarnessUI.setVisible(true);
+            } else if (eventName == 'quitApp') {
+                return AppsService.quitApp();
             } else if (eventName == 'destroyed') {
                 $window.history.back();
             } else {