Skip to content

Commit

Permalink
CB-5504: Moving code to the App plugin inside Cordova, the place wher…
Browse files Browse the repository at this point in the history
…e the grey area beween plugin and platform exists
  • Loading branch information
infil00p committed Dec 9, 2013
1 parent 146e296 commit adba84a
Showing 1 changed file with 62 additions and 0 deletions.
62 changes: 62 additions & 0 deletions framework/src/org/apache/cordova/App.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,35 @@ Licensed to the Apache Software Foundation (ASF) under one
import org.json.JSONException;
import org.json.JSONObject;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.telephony.TelephonyManager;

import java.util.HashMap;

/**
* This class exposes methods in Cordova that can be called from JavaScript.
*/
public class App extends CordovaPlugin {

protected static final String TAG = "CordovaApp";
private BroadcastReceiver telephonyReceiver;

/**
* Sets the context of the Command. This can then be used to do things like
* get file paths associated with the Activity.
*
* @param cordova The context of the main Activity.
* @param webView The CordovaWebView Cordova is running in.
*/
public void initialize(CordovaInterface cordova, CordovaWebView webView) {
super.initialize(cordova, webView);
this.initTelephonyReceiver();
}


/**
* Executes the request and returns PluginResult.
*
Expand Down Expand Up @@ -221,5 +243,45 @@ public boolean isBackbuttonOverridden() {
public void exitApp() {
this.webView.postMessage("exit", null);
}


/**
* Listen for telephony events: RINGING, OFFHOOK and IDLE
* Send these events to all plugins using
* CordovaActivity.onMessage("telephone", "ringing" | "offhook" | "idle")
*/
private void initTelephonyReceiver() {
IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction(TelephonyManager.ACTION_PHONE_STATE_CHANGED);
//final CordovaInterface mycordova = this.cordova;
this.telephonyReceiver = new BroadcastReceiver() {

@Override
public void onReceive(Context context, Intent intent) {

// If state has changed
if ((intent != null) && intent.getAction().equals(TelephonyManager.ACTION_PHONE_STATE_CHANGED)) {
if (intent.hasExtra(TelephonyManager.EXTRA_STATE)) {
String extraData = intent.getStringExtra(TelephonyManager.EXTRA_STATE);
if (extraData.equals(TelephonyManager.EXTRA_STATE_RINGING)) {
LOG.i(TAG, "Telephone RINGING");
webView.postMessage("telephone", "ringing");
}
else if (extraData.equals(TelephonyManager.EXTRA_STATE_OFFHOOK)) {
LOG.i(TAG, "Telephone OFFHOOK");
webView.postMessage("telephone", "offhook");
}
else if (extraData.equals(TelephonyManager.EXTRA_STATE_IDLE)) {
LOG.i(TAG, "Telephone IDLE");
webView.postMessage("telephone", "idle");
}
}
}
}
};

// Register the receiver
this.cordova.getActivity().registerReceiver(this.telephonyReceiver, intentFilter);
}

}

0 comments on commit adba84a

Please sign in to comment.