cordova-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From agri...@apache.org
Subject android commit: CB-8684 Add onStart/onStop hooks for plugins (close #173)
Date Thu, 09 Apr 2015 00:34:07 GMT
Repository: cordova-android
Updated Branches:
  refs/heads/master 581252feb -> a652d892c


CB-8684 Add onStart/onStop hooks for plugins (close #173)


Project: http://git-wip-us.apache.org/repos/asf/cordova-android/repo
Commit: http://git-wip-us.apache.org/repos/asf/cordova-android/commit/a652d892
Tree: http://git-wip-us.apache.org/repos/asf/cordova-android/tree/a652d892
Diff: http://git-wip-us.apache.org/repos/asf/cordova-android/diff/a652d892

Branch: refs/heads/master
Commit: a652d892ca93d077038310af50d2a40ab5fabfd6
Parents: 581252f
Author: Tony Homer <tony.homer@intel.com>
Authored: Wed Apr 8 15:00:38 2015 -0400
Committer: Andrew Grieve <agrieve@chromium.org>
Committed: Wed Apr 8 20:33:31 2015 -0400

----------------------------------------------------------------------
 .../src/org/apache/cordova/CordovaActivity.java | 28 ++++++++
 .../src/org/apache/cordova/CordovaPlugin.java   | 12 ++++
 .../src/org/apache/cordova/CordovaWebView.java  |  4 ++
 .../org/apache/cordova/CordovaWebViewImpl.java  | 15 +++++
 .../src/org/apache/cordova/PluginManager.java   | 22 ++++++
 .../apache/cordova/test/CordovaPluginTest.java  | 70 ++++++++++++++++++++
 test/res/xml/config.xml                         |  4 ++
 .../apache/cordova/test/LifeCyclePlugin.java    | 49 ++++++++++++++
 8 files changed, 204 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/cordova-android/blob/a652d892/framework/src/org/apache/cordova/CordovaActivity.java
----------------------------------------------------------------------
diff --git a/framework/src/org/apache/cordova/CordovaActivity.java b/framework/src/org/apache/cordova/CordovaActivity.java
index 8e185dd..e1f917c 100755
--- a/framework/src/org/apache/cordova/CordovaActivity.java
+++ b/framework/src/org/apache/cordova/CordovaActivity.java
@@ -269,6 +269,34 @@ public class CordovaActivity extends Activity {
     }
 
     /**
+     * Called when the activity is no longer visible to the user.
+     */
+    @Override
+    protected void onStop() {
+        super.onStop();
+        LOG.d(TAG, "Stopped the activity.");
+
+        if (this.appView == null) {
+            return;
+        }
+        this.appView.handleStop();
+    }
+
+    /**
+     * Called when the activity is becoming visible to the user.
+     */
+    @Override
+    protected void onStart() {
+        super.onStart();
+        LOG.d(TAG, "Started the activity.");
+
+        if (this.appView == null) {
+            return;
+        }
+        this.appView.handleStart();
+    }
+
+    /**
      * The final call you receive before your activity is destroyed.
      */
     @Override

http://git-wip-us.apache.org/repos/asf/cordova-android/blob/a652d892/framework/src/org/apache/cordova/CordovaPlugin.java
----------------------------------------------------------------------
diff --git a/framework/src/org/apache/cordova/CordovaPlugin.java b/framework/src/org/apache/cordova/CordovaPlugin.java
index 0d08117..7cf8528 100644
--- a/framework/src/org/apache/cordova/CordovaPlugin.java
+++ b/framework/src/org/apache/cordova/CordovaPlugin.java
@@ -149,6 +149,18 @@ public class CordovaPlugin {
     }
 
     /**
+     * Called when the activity is becoming visible to the user.
+     */
+    public void onStart() {
+    }
+
+    /**
+     * Called when the activity is no longer visible to the user.
+     */
+    public void onStop() {
+    }
+
+    /**
      * Called when the activity receives a new intent.
      */
     public void onNewIntent(Intent intent) {

http://git-wip-us.apache.org/repos/asf/cordova-android/blob/a652d892/framework/src/org/apache/cordova/CordovaWebView.java
----------------------------------------------------------------------
diff --git a/framework/src/org/apache/cordova/CordovaWebView.java b/framework/src/org/apache/cordova/CordovaWebView.java
index d3071f4..52dfb92 100644
--- a/framework/src/org/apache/cordova/CordovaWebView.java
+++ b/framework/src/org/apache/cordova/CordovaWebView.java
@@ -61,6 +61,10 @@ public interface CordovaWebView {
 
     void handleResume(boolean keepRunning);
 
+    void handleStart();
+
+    void handleStop();
+
     void handleDestroy();
 
     /**

http://git-wip-us.apache.org/repos/asf/cordova-android/blob/a652d892/framework/src/org/apache/cordova/CordovaWebViewImpl.java
----------------------------------------------------------------------
diff --git a/framework/src/org/apache/cordova/CordovaWebViewImpl.java b/framework/src/org/apache/cordova/CordovaWebViewImpl.java
index 7115db4..868ad4f 100644
--- a/framework/src/org/apache/cordova/CordovaWebViewImpl.java
+++ b/framework/src/org/apache/cordova/CordovaWebViewImpl.java
@@ -115,6 +115,7 @@ public class CordovaWebViewImpl implements CordovaWebView {
 
         pluginManager.addService(CoreAndroid.PLUGIN_NAME, "org.apache.cordova.CoreAndroid");
         pluginManager.init();
+
     }
 
     @Override
@@ -446,6 +447,20 @@ public class CordovaWebViewImpl implements CordovaWebView {
         this.pluginManager.onResume(keepRunning);
         sendJavascriptEvent("resume");
     }
+    @Override
+    public void handleStart() {
+        if (!isInitialized()) {
+            return;
+        }
+        pluginManager.onStart();
+    }
+    @Override
+    public void handleStop() {
+        if (!isInitialized()) {
+            return;
+        }
+        pluginManager.onStop();
+    }
 
     @Override
     public void handleDestroy() {

http://git-wip-us.apache.org/repos/asf/cordova-android/blob/a652d892/framework/src/org/apache/cordova/PluginManager.java
----------------------------------------------------------------------
diff --git a/framework/src/org/apache/cordova/PluginManager.java b/framework/src/org/apache/cordova/PluginManager.java
index 87663be..a541e77 100755
--- a/framework/src/org/apache/cordova/PluginManager.java
+++ b/framework/src/org/apache/cordova/PluginManager.java
@@ -265,6 +265,28 @@ public class PluginManager {
     }
 
     /**
+     * Called when the activity is becoming visible to the user.
+     */
+    public void onStart() {
+        for (CordovaPlugin plugin : this.pluginMap.values()) {
+            if (plugin != null) {
+                plugin.onStart();
+            }
+        }
+    }
+
+    /**
+     * Called when the activity is no longer visible to the user.
+     */
+    public void onStop() {
+        for (CordovaPlugin plugin : this.pluginMap.values()) {
+            if (plugin != null) {
+                plugin.onStop();
+            }
+        }
+    }
+
+    /**
      * The final call you receive before your activity is destroyed.
      */
     public void onDestroy() {

http://git-wip-us.apache.org/repos/asf/cordova-android/blob/a652d892/test/androidTest/src/org/apache/cordova/test/CordovaPluginTest.java
----------------------------------------------------------------------
diff --git a/test/androidTest/src/org/apache/cordova/test/CordovaPluginTest.java b/test/androidTest/src/org/apache/cordova/test/CordovaPluginTest.java
new file mode 100644
index 0000000..f9f254a
--- /dev/null
+++ b/test/androidTest/src/org/apache/cordova/test/CordovaPluginTest.java
@@ -0,0 +1,70 @@
+/*
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ *
+ */
+
+package org.apache.cordova.test;
+
+
+import android.app.Activity;
+import android.test.ActivityInstrumentationTestCase2;
+
+import org.apache.cordova.CordovaWebView;
+
+import java.io.IOException;
+import java.lang.reflect.Method;
+
+public class CordovaPluginTest extends BaseCordovaIntegrationTest {
+
+    protected void setUp() throws Exception {
+        super.setUp();
+        setUpWithStartUrl(null);
+    }
+
+    private void invokeBlockingCallToLifeCycleEvent(final String lifeCycleEventName) {
+        testActivity.runOnUiThread( new Runnable() {
+            public void run() {
+                try {
+                    Method method = getInstrumentation().getClass().getMethod(lifeCycleEventName,
Activity.class);
+                    method.invoke(getInstrumentation(), testActivity);
+                } catch (Exception e) {
+                    fail("An Exception occurred in invokeBlockingCallToLifeCycleEvent while
invoking " + lifeCycleEventName);
+                }
+            }
+        });
+        getInstrumentation().waitForIdleSync();
+    }
+
+    public void testPluginLifeCycle() throws IOException {
+        //TODO: add coverage for both cases where handleOnStart is called in CordovaActivity
(onStart and init)
+        //currently only one of the cases is covered
+        //TODO: add coverage for both cases where onStart is called in CordovaWebViewImpl
(handleOnStart and init)
+        //currently only one of the cases is covered
+        LifeCyclePlugin testPlugin = (LifeCyclePlugin)cordovaWebView.getPluginManager().getPlugin("LifeCycle");
+
+        assertEquals("start,pause,stop,", testPlugin.calls);
+        testPlugin.calls = "";
+        // testOnStart
+        invokeBlockingCallToLifeCycleEvent("callActivityOnStart");
+        invokeBlockingCallToLifeCycleEvent("callActivityOnResume");
+        invokeBlockingCallToLifeCycleEvent("callActivityOnPause");
+        invokeBlockingCallToLifeCycleEvent("callActivityOnStop");
+        assertEquals("start,resume,pause,stop,", testPlugin.calls);
+    }
+}

http://git-wip-us.apache.org/repos/asf/cordova-android/blob/a652d892/test/res/xml/config.xml
----------------------------------------------------------------------
diff --git a/test/res/xml/config.xml b/test/res/xml/config.xml
index 894c336..7da4638 100644
--- a/test/res/xml/config.xml
+++ b/test/res/xml/config.xml
@@ -38,4 +38,8 @@
     <feature name="Activity">
         <param name="android-package" value="org.apache.cordova.test.ActivityPlugin" />
     </feature>
+    <feature name="LifeCycle">
+        <param name="android-package" value="org.apache.cordova.test.LifeCyclePlugin"
/>
+        <param name="onload" value="true" />
+    </feature>
 </widget>

http://git-wip-us.apache.org/repos/asf/cordova-android/blob/a652d892/test/src/org/apache/cordova/test/LifeCyclePlugin.java
----------------------------------------------------------------------
diff --git a/test/src/org/apache/cordova/test/LifeCyclePlugin.java b/test/src/org/apache/cordova/test/LifeCyclePlugin.java
new file mode 100644
index 0000000..62ab7fa
--- /dev/null
+++ b/test/src/org/apache/cordova/test/LifeCyclePlugin.java
@@ -0,0 +1,49 @@
+/*
+       Licensed to the Apache Software Foundation (ASF) under one
+       or more contributor license agreements.  See the NOTICE file
+       distributed with this work for additional information
+       regarding copyright ownership.  The ASF licenses this file
+       to you under the Apache License, Version 2.0 (the
+       "License"); you may not use this file except in compliance
+       with the License.  You may obtain a copy of the License at
+
+         http://www.apache.org/licenses/LICENSE-2.0
+
+       Unless required by applicable law or agreed to in writing,
+       software distributed under the License is distributed on an
+       "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+       KIND, either express or implied.  See the License for the
+       specific language governing permissions and limitations
+       under the License.
+*/
+package org.apache.cordova.test;
+
+import org.apache.cordova.CordovaPlugin;
+import org.apache.cordova.LOG;
+
+public class LifeCyclePlugin extends CordovaPlugin {
+
+    static String TAG = "LifeCyclePlugin";
+    String calls = "";
+
+    @Override
+    public void onStart() {
+        calls += "start,";
+        LOG.d(TAG, "onStart");
+    }
+    @Override
+    public void onPause(boolean multitasking) {
+        calls += "pause,";
+        LOG.d(TAG, "onPause");
+    }
+    @Override
+    public void onResume(boolean multitasking) {
+        calls += "resume,";
+        LOG.d(TAG, "onResume");
+    }
+    @Override
+    public void onStop() {
+        calls += "stop,";
+        LOG.d(TAG, "onStop");
+    }
+}


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@cordova.apache.org
For additional commands, e-mail: commits-help@cordova.apache.org


Mime
View raw message