mirror of
https://github.com/apache/cordova-android.git
synced 2025-02-20 23:56:20 +08:00
CB-8684 Add onStart/onStop hooks for plugins (close #173)
This commit is contained in:
parent
581252febc
commit
a652d892ca
@ -268,6 +268,34 @@ public class CordovaActivity extends Activity {
|
||||
this.appView.handleResume(this.keepRunning);
|
||||
}
|
||||
|
||||
/**
|
||||
* 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.
|
||||
*/
|
||||
|
@ -148,6 +148,18 @@ public class CordovaPlugin {
|
||||
public void onResume(boolean multitasking) {
|
||||
}
|
||||
|
||||
/**
|
||||
* 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.
|
||||
*/
|
||||
|
@ -61,6 +61,10 @@ public interface CordovaWebView {
|
||||
|
||||
void handleResume(boolean keepRunning);
|
||||
|
||||
void handleStart();
|
||||
|
||||
void handleStop();
|
||||
|
||||
void handleDestroy();
|
||||
|
||||
/**
|
||||
|
@ -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() {
|
||||
|
@ -264,6 +264,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.
|
||||
*/
|
||||
|
@ -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);
|
||||
}
|
||||
}
|
@ -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>
|
||||
|
49
test/src/org/apache/cordova/test/LifeCyclePlugin.java
Normal file
49
test/src/org/apache/cordova/test/LifeCyclePlugin.java
Normal file
@ -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");
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user