CB-12835: add a Context getter in CordovaInterface

A custom engine may live outside of the Activity's lifecycle and the
Activity instance may not always be available. This getter allows
Context accesses in all cases.
This commit is contained in:
Xiaolei Yu 2017-05-20 06:54:23 +08:00
parent 3a6e898b12
commit 17906735df
2 changed files with 16 additions and 1 deletions

View File

@ -19,6 +19,7 @@
package org.apache.cordova;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import org.apache.cordova.CordovaPlugin;
@ -51,10 +52,18 @@ public interface CordovaInterface {
/**
* Get the Android activity.
*
* If a custom engine lives outside of the Activity's lifecycle the return value may be null.
*
* @return the Activity
*/
public abstract Activity getActivity();
/**
* Get the Android context.
*
* @return the Context
*/
public Context getContext();
/**
* Called when a message is sent to plugin.

View File

@ -20,6 +20,7 @@
package org.apache.cordova;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Build;
@ -84,6 +85,11 @@ public class CordovaInterfaceImpl implements CordovaInterface {
return activity;
}
@Override
public Context getContext() {
return activity;
}
@Override
public Object onMessage(String id, Object data) {
if ("exit".equals(id)) {