Example usage for org.apache.cordova.api CordovaInterface getContext

List of usage examples for org.apache.cordova.api CordovaInterface getContext

Introduction

In this page you can find the example usage for org.apache.cordova.api CordovaInterface getContext.

Prototype

public abstract Context getContext();

Source Link

Usage

From source file:org.devgeeks.PhoneListener.java

License:Open Source License

/**
 * Sets the context of the Command. This can then be used to do things like
 * get file paths associated with the Activity.
 * /*  w  w w. j  a va 2s.co m*/
 * @param ctx The context of the main Activity.
 */
public void setContext(CordovaInterface ctx) {
    super.setContext(ctx);
    this.phoneListenerCallbackId = null;

    // We need to listen to connectivity events to update navigator.connection
    IntentFilter intentFilter = new IntentFilter();
    intentFilter.addAction(TelephonyManager.ACTION_PHONE_STATE_CHANGED);
    if (this.receiver == null) {
        this.receiver = new BroadcastReceiver() {
            @Override
            public void onReceive(Context context, Intent intent) {
                if (intent != null && intent.getAction().equals(TelephonyManager.ACTION_PHONE_STATE_CHANGED)) {
                    // State has changed
                    String phoneState = intent.hasExtra(TelephonyManager.EXTRA_STATE)
                            ? intent.getStringExtra(TelephonyManager.EXTRA_STATE)
                            : null;
                    String state;
                    // See if the new state is 'ringing', 'off hook' or 'idle'
                    if (phoneState != null && phoneState.equals(TelephonyManager.EXTRA_STATE_RINGING)) {
                        // phone is ringing, awaiting either answering or canceling
                        state = "RINGING";
                        Log.i(LOG_TAG, state);
                    } else if (phoneState != null && phoneState.equals(TelephonyManager.EXTRA_STATE_OFFHOOK)) {
                        // actually talking on the phone... either making a call or having answered one
                        state = "OFFHOOK";
                        Log.i(LOG_TAG, state);
                    } else if (phoneState != null && phoneState.equals(TelephonyManager.EXTRA_STATE_IDLE)) {
                        // idle means back to no calls in or out. default state.
                        state = "IDLE";
                        Log.i(LOG_TAG, state);
                    } else {
                        state = TYPE_NONE;
                        Log.i(LOG_TAG, state);
                    }
                    updatePhoneState(state, true);
                }
            }
        };
        // register the receiver... this is so it doesn't have to be added to AndroidManifest.xml
        ctx.getContext().registerReceiver(this.receiver, intentFilter);
    }
}