Example usage for android.telephony TelephonyManager EXTRA_STATE_OFFHOOK

List of usage examples for android.telephony TelephonyManager EXTRA_STATE_OFFHOOK

Introduction

In this page you can find the example usage for android.telephony TelephonyManager EXTRA_STATE_OFFHOOK.

Prototype

String EXTRA_STATE_OFFHOOK

To view the source code for android.telephony TelephonyManager EXTRA_STATE_OFFHOOK.

Click Source Link

Document

Value used with #EXTRA_STATE corresponding to #CALL_STATE_OFFHOOK .

Usage

From source file:com.example.plugin.PhoneListener.java

public void initialize(CordovaInterface cordova, CordovaWebView webView) {
    super.initialize(cordova, webView);

    this.phoneListenerCallbackId = null;
    IntentFilter intentFilter = new IntentFilter();
    intentFilter.addAction(TelephonyManager.ACTION_PHONE_STATE_CHANGED);
    if (this.receiver == null) {
        this.receiver = new BroadcastReceiver() {
            @Override// w ww .  j ava  2s  .c o m
            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
        cordova.getActivity().registerReceiver(this.receiver, intentFilter);
        this.registered = true;
    }

}

From source file:org.devgeeks.PhoneListener.java

/**
 * Sets the context of the Command. This can then be used to do things like
 * get file paths associated with the Activity.
 * /*from  ww w .jav a  2  s  .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
        cordova.getActivity().registerReceiver(this.receiver, intentFilter);
    }
}

From source file:com.ripperdesignandmultimedia.phoneblockplugin.PhoneBlockerPlugin.java

/**
 * Sets the context of the Command. This can then be used to do things like
 * get file paths associated with the Activity.
 * //from   w w w  .  j av  a  2  s  .  c  o  m
 * @param ctx The context of the main Activity.
 */
public void setContext(CordovaInterface ctx) {
    super.setContext(ctx);
    this.phoneBlockerCallbackId = 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
        cordova.getActivity().registerReceiver(this.receiver, intentFilter);
    }
}

From source file:edu.stanford.mobisocial.dungbeetle.feed.objects.PhoneStateObj.java

private String asText(JSONObject obj) {
    StringBuilder status = new StringBuilder();
    String a = obj.optString(ACTION);
    String b = obj.optString(NUMBER);
    if (TelephonyManager.EXTRA_STATE_OFFHOOK.equals(a)) {
        status.append("Calling ");
    } else if (TelephonyManager.EXTRA_STATE_IDLE.equals(a)) {
        status.append("Ending phone call with ");
    } else if (TelephonyManager.EXTRA_STATE_RINGING.equals(a)) {
        status.append("Inbound call from ");
    }//from w w  w.  j a v  a2s . c om
    status.append(b).append(".");
    return status.toString();
}

From source file:edu.cmu.plugins.PhoneListener.java

/**
 * 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  .  ja va2  s  . c om*/
 * @param ctx The context of the main Activity.
 */
public void setContext(PhonegapActivity 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);
    intentFilter.addAction("android.provider.Telephony.SMS_RECEIVED");

    if (this.receiver == null) {
        this.receiver = new BroadcastReceiver() {

            @Override
            public void onReceive(Context context, Intent intent) {
                if (intent == null) {
                    return;
                }
                String state = "";
                if (intent.getAction().equals("android.provider.Telephony.SMS_RECEIVED")) {
                    state = "SMS_RECEIVED";
                    Log.i(LOG_TAG, state);
                }
                if (intent.getAction().equals(TelephonyManager.ACTION_PHONE_STATE_CHANGED)) {
                    // State has changed
                    String phoneState = intent.hasExtra(TelephonyManager.EXTRA_STATE)
                            ? intent.getStringExtra(TelephonyManager.EXTRA_STATE)
                            : null;

                    // 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.registerReceiver(this.receiver, intentFilter);
    }
}

From source file:com.yangtsaosoftware.pebblemessenger.receivers.CallStateHandler.java

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

    String state = intent.getStringExtra(TelephonyManager.EXTRA_STATE);
    if (TelephonyManager.EXTRA_STATE_RINGING.equals(state)) {
        String incomingNumber = intent.getStringExtra(TelephonyManager.EXTRA_INCOMING_NUMBER);
        Constants.log(LOG_TAG, "A new call is coming:" + incomingNumber);
        Intent inner_intent = new Intent(MessageProcessingService.class.getName());
        inner_intent.putExtra(Constants.BROADCAST_COMMAND, Constants.BROADCAST_CALL_INCOMING);
        if (incomingNumber != null && !incomingNumber.isEmpty()) {
            inner_intent.putExtra(Constants.BROADCAST_PHONE_NUM, incomingNumber);
            inner_intent.putExtra(Constants.BROADCAST_NAME, Constants.queryNameByNum(context, incomingNumber));
        } else {/*ww  w .  ja v  a  2  s  . c  o m*/
            inner_intent.putExtra(Constants.BROADCAST_PHONE_NUM, "0");
            inner_intent.putExtra(Constants.BROADCAST_NAME,
                    context.getString(R.string.notificationservice_privateNumber));
        }
        LocalBroadcastManager.getInstance(context).sendBroadcast(inner_intent);
    } else if (TelephonyManager.EXTRA_STATE_IDLE.equals(state)) {
        Constants.log(LOG_TAG, "Call is idle");
        Intent inner_intent = new Intent(MessageProcessingService.class.getName());
        inner_intent.putExtra(Constants.BROADCAST_COMMAND, Constants.BROADCAST_CALL_IDLE);
        LocalBroadcastManager.getInstance(context).sendBroadcast(inner_intent);
        inner_intent = new Intent(PebbleCenter.class.getName());
        inner_intent.putExtra(Constants.BROADCAST_COMMAND, Constants.BROADCAST_CALL_IDLE);
        LocalBroadcastManager.getInstance(context).sendBroadcast(inner_intent);
    } else if (TelephonyManager.EXTRA_STATE_OFFHOOK.equals(state)) {
        Constants.log(LOG_TAG, "Call is off hook");
        Intent inner_intent = new Intent(PebbleCenter.class.getName());
        inner_intent.putExtra(Constants.BROADCAST_COMMAND, Constants.BROADCAST_CALL_HOOK);
        LocalBroadcastManager.getInstance(context).sendBroadcast(inner_intent);
        inner_intent = new Intent(MessageProcessingService.class.getName());
        inner_intent.putExtra(Constants.BROADCAST_COMMAND, Constants.BROADCAST_CALL_HOOK);
        LocalBroadcastManager.getInstance(context).sendBroadcast(inner_intent);
    }
}

From source file:de.qspool.clementineremote.backend.receivers.ClementinePhoneStateCheck.java

@Override
public void onReceive(Context context, Intent intent) {
    if (App.getApp() == null || App.ClementineConnection == null || App.Clementine == null
            || !App.ClementineConnection.isConnected()) {
        return;/*from w w  w.j  a  v a2s .  c  o m*/
    }

    if (!intent.getAction().equals("android.intent.action.PHONE_STATE")) {
        return;
    }

    if (ContextCompat.checkSelfPermission(context,
            Manifest.permission.READ_PHONE_STATE) != PackageManager.PERMISSION_GRANTED) {
        return;
    }

    // Check if we need to change the volume
    SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(App.getApp());
    String volumeString = prefs.getString(SharedPreferencesKeys.SP_CALL_VOLUME, Clementine.DefaultCallVolume);
    int volume = Integer.parseInt(volumeString);

    // Get the pebble settings
    if (prefs.getBoolean(SharedPreferencesKeys.SP_LOWER_VOLUME, true)) {
        // Get the current state of the telephone
        String state = intent.getStringExtra(TelephonyManager.EXTRA_STATE);

        // On Lollipop, the state is broadcasted twice. Only process new states.
        if (lastPhoneState.equals(state))
            return;

        Message msg = Message.obtain();

        LastClementineState lastClementineState = new LastClementineState();
        lastClementineState.load();

        if (state.equals(TelephonyManager.EXTRA_STATE_RINGING)
                || state.equals(TelephonyManager.EXTRA_STATE_OFFHOOK)) {

            // Only lower the volume once. When receiving a call, the state is RINGING. On pickup
            // OFFHOOK is broadcasted. So we only need to take action when we previously had the
            // IDLE state.
            if (lastPhoneState.equals(TelephonyManager.EXTRA_STATE_IDLE)) {
                lastClementineState.volume = App.Clementine.getVolume();
                lastClementineState.state = App.Clementine.getState();
                lastClementineState.save();

                if (volume >= 0) {
                    msg.obj = ClementineMessageFactory.buildVolumeMessage(Integer.parseInt(volumeString));
                } else {
                    msg.obj = ClementineMessage.getMessage(ClementineRemoteProtocolBuffer.MsgType.PAUSE);
                }
            }
        } else if (state.equals(TelephonyManager.EXTRA_STATE_IDLE)) {
            if (volume >= 0) {
                msg.obj = ClementineMessageFactory.buildVolumeMessage(lastClementineState.volume);
            } else {
                if (lastClementineState.state.equals(Clementine.State.PLAY)) {
                    msg.obj = ClementineMessage.getMessage(ClementineRemoteProtocolBuffer.MsgType.PLAY);
                }
            }
        }

        // Now send the message
        if (msg != null && msg.obj != null && App.ClementineConnection != null) {
            App.ClementineConnection.mHandler.sendMessage(msg);
        }

        lastPhoneState = state;
    }
}

From source file:org.skt.runtime.original.Device.java

/**
 * Listen for telephony events: RINGING, OFFHOOK and IDLE
 * Send these events to all plugins using
 *      DroidGap.onMessage("telephone", "ringing" | "offhook" | "idle")
 *//*from   www. jav a 2  s .c o  m*/
private void initTelephonyReceiver() {
    IntentFilter intentFilter = new IntentFilter();
    intentFilter.addAction(TelephonyManager.ACTION_PHONE_STATE_CHANGED);
    final RuntimeInterface myctx = this.ctx;
    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");
                        myctx.postMessage("telephone", "ringing");
                    } else if (extraData.equals(TelephonyManager.EXTRA_STATE_OFFHOOK)) {
                        LOG.i(TAG, "Telephone OFFHOOK");
                        myctx.postMessage("telephone", "offhook");
                    } else if (extraData.equals(TelephonyManager.EXTRA_STATE_IDLE)) {
                        LOG.i(TAG, "Telephone IDLE");
                        myctx.postMessage("telephone", "idle");
                    }
                }
            }
        }
    };

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

From source file:org.durka.hallmonitor.CoreReceiver.java

@Override
public void onReceive(Context context, Intent intent) {
    if (intent.getAction().equals(Intent.ACTION_BOOT_COMPLETED) || intent.getAction().equals(QUICKBOOT_POWERON)
            || intent.getAction().equals(HTC_QUICKBOOT_POWERON)) {
        Log.d(LOG_TAG + ".boot", "Boot called.");
        SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
        if (prefs.getBoolean("pref_enabled", false)) {
            Intent mIntent = new Intent(context, CoreService.class);
            context.startService(mIntent);
        }/*w w  w  .j  ava  2s  . co m*/
    }

    if (CoreStateManager.getInitialized()) {
        localContext = CoreStateManager.getContext();
        mStateManager = ((CoreApp) localContext).getStateManager();
    } else {
        return;
    }

    if (!mStateManager.getPreference().getBoolean("pref_enabled", false)) {
        return;
    }

    mStateManager.acquireCPUGlobal();

    if (intent.getAction().equals(Intent.ACTION_SCREEN_ON)) {

        Log.d(LOG_TAG + ".screen", "Screen on event received.");

        if (mStateManager.getCoverClosed()) {
            Log.d(LOG_TAG + ".screen", "Cover is closed, display Default Activity.");
            mStateManager.setBlackScreenTime(0);
            Intent mIntent = new Intent(localContext, CoreService.class);
            mIntent.putExtra(CoreApp.CS_EXTRA_TASK, CoreApp.CS_TASK_LAUNCH_ACTIVITY);
            mStateManager.sendToCoreService(mIntent);
        } else {
            // Log.d(LOG_TAG + ".screen",
            // "Cover is open, free everything.");
            // mStateManager.freeDevice();
            Log.d(LOG_TAG + ".screen", "Cover is open, send to background.");
            Intent stbDAIntent = new Intent(CoreApp.DA_ACTION_SEND_TO_BACKGROUND);
            LocalBroadcastManager.getInstance(localContext).sendBroadcastSync(stbDAIntent);
        }

    } else if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF)) {
        Log.d(LOG_TAG + ".screen", "Screen off event received.");
        // mStateManager.freeDevice();

    } else if (intent.getAction().equals(Intent.ACTION_POWER_CONNECTED)) {
        Intent batteryDAIntent = new Intent(CoreApp.DA_ACTION_BATTERY_REFRESH);
        LocalBroadcastManager.getInstance(localContext).sendBroadcastSync(batteryDAIntent);

        Intent mIntent = new Intent(localContext, CoreService.class);
        mIntent.putExtra(CoreApp.CS_EXTRA_TASK, CoreApp.CS_TASK_WAKEUP_DEVICE);
        mStateManager.sendToCoreService(mIntent);

    } else if (intent.getAction().equals(Intent.ACTION_POWER_DISCONNECTED)) {
        Intent batteryDAIntent = new Intent(CoreApp.DA_ACTION_BATTERY_REFRESH);
        LocalBroadcastManager.getInstance(localContext).sendBroadcastSync(batteryDAIntent);

        Intent mIntent = new Intent(localContext, CoreService.class);
        mIntent.putExtra(CoreApp.CS_EXTRA_TASK, CoreApp.CS_TASK_WAKEUP_DEVICE);
        mStateManager.sendToCoreService(mIntent);

    } else if (intent.getAction().equals(ALARM_ALERT_ACTION)) {

        Log.d(LOG_TAG + ".alarm", "Alarm on event received.");

        // only take action if alarm controls are enabled
        if (mStateManager.getPreference().getBoolean("pref_alarm_controls", false)) {

            Log.d(LOG_TAG + ".alarm", "Alarm controls are enabled, taking action.");
            mStateManager.setAlarmFiring(true);

            Intent mIntent = new Intent(localContext, CoreService.class);
            mIntent.putExtra(CoreApp.CS_EXTRA_TASK, CoreApp.CS_TASK_INCOMMING_ALARM);
            mStateManager.sendToCoreService(mIntent);
        } else {
            Log.d(LOG_TAG + ".alarm", "Alarm controls are not enabled.");
        }

    } else if (intent.getAction().equals(ALARM_DONE_ACTION)) {

        Log.d(LOG_TAG + ".alarm", "Alarm done event received.");

        // only take action if alarm controls are enabled
        if (mStateManager.getPreference().getBoolean("pref_alarm_controls", false)) {
            Log.d(mStateManager.getPreference() + ".alarm", "alarm is over, cleaning up");
            mStateManager.setAlarmFiring(false);

            if (mStateManager.getCoverClosed()) {
                Intent mIntent = new Intent(localContext, CoreService.class);
                mIntent.putExtra(CoreApp.CS_EXTRA_TASK, CoreApp.CS_TASK_LAUNCH_ACTIVITY);
                mStateManager.sendToCoreService(mIntent);
            }
        }
    } else if (intent.getAction().equals(TelephonyManager.ACTION_PHONE_STATE_CHANGED)) {

        if (mStateManager.getPreference().getBoolean("pref_phone_controls", false)) {
            String state = intent.getStringExtra(TelephonyManager.EXTRA_STATE);
            Log.d(LOG_TAG + ".phone", "phone state changed to " + state);
            if (state.equals(TelephonyManager.EXTRA_STATE_RINGING)) {
                Intent mIntent;
                mStateManager.setPhoneRinging(true);
                mStateManager.setCallFrom(intent.getStringExtra(TelephonyManager.EXTRA_INCOMING_NUMBER));
                Log.d(LOG_TAG, "call from " + mStateManager.getCallFrom());
                mIntent = new Intent(localContext, CoreService.class);
                mIntent.putExtra(CoreApp.CS_EXTRA_TASK, CoreApp.CS_TASK_INCOMMING_CALL);
                mStateManager.sendToCoreService(mIntent);
            } else if (state.equals(TelephonyManager.EXTRA_STATE_IDLE)) {
                Intent mIntent;
                mStateManager.setPhoneRinging(false);
                Log.d(LOG_TAG, "call is over, cleaning up");
                if (mStateManager.getCoverClosed()) {
                    mIntent = new Intent(localContext, CoreService.class);
                    mIntent.putExtra(CoreApp.CS_EXTRA_TASK, CoreApp.CS_TASK_LAUNCH_ACTIVITY);
                    mStateManager.sendToCoreService(mIntent);
                }
            } else if (state.equals(TelephonyManager.EXTRA_STATE_OFFHOOK)) {
            }
        } else {
            Log.d(LOG_TAG + ".phone", "phone controls are not enabled");
        }
    } else if (intent.getAction().equals(mStateManager.getActionCover())) {
        int state = intent.getIntExtra(EXTRA_LID_STATE, LID_ABSENT);
        Log.d(LOG_TAG + ".cover", "cover state changed to " + state);
        if (state == LID_CLOSED) {
            Log.d(LOG_TAG + ".cover", "Cover is close, enable Default Activity.");
            mStateManager.setCoverClosed(true);
            Intent mIntent = new Intent(localContext, CoreService.class);
            mIntent.putExtra(CoreApp.CS_EXTRA_TASK, CoreApp.CS_TASK_LAUNCH_ACTIVITY);
            mStateManager.sendToCoreService(mIntent);
        } else if (state == LID_OPEN) {
            // Log.d(LOG_TAG + ".cover",
            // "Cover is open, stopping Default Activity.");
            mStateManager.setCoverClosed(false);
            // mStateManager.freeDevice();
            Log.d(LOG_TAG + ".screen", "Cover is open, send to background.");
            Intent stbDAIntent = new Intent(CoreApp.DA_ACTION_SEND_TO_BACKGROUND);
            LocalBroadcastManager.getInstance(localContext).sendBroadcastSync(stbDAIntent);
            Intent mIntent = new Intent(localContext, CoreService.class);
            mIntent.putExtra(CoreApp.CS_EXTRA_TASK, CoreApp.CS_TASK_WAKEUP_DEVICE);
            mStateManager.sendToCoreService(mIntent);
        }

    } else if (intent.getAction().equals(TORCH_STATE_CHANGED)) {
        if (mStateManager.getPreference().getBoolean("pref_flash_controls", false)) {
            Log.d(LOG_TAG + ".torch", "torch state changed");
            Intent mIntent = new Intent(localContext, CoreService.class);
            mIntent.putExtra(CoreApp.CS_EXTRA_TASK, CoreApp.CS_TASK_TORCH_STATE);
            if (intent.getIntExtra("state", 0) != 0) {
                mIntent.putExtra(CoreApp.CS_EXTRA_STATE, true);
            } else {
                mIntent.putExtra(CoreApp.CS_EXTRA_STATE, false);
            }
            mStateManager.sendToCoreService(mIntent);
        } else {
            Log.d(LOG_TAG + ".torch", "torch controls are not enabled.");
        }

    } else if (intent.getAction().equals(Intent.ACTION_HEADSET_PLUG)) {
        int state = intent.getExtras().getInt("state");
        Log.d(LOG_TAG + ".headset", "headset is " + (state == 0 ? "gone" : "here") + "!");
        Intent mIntent = new Intent(localContext, CoreService.class);
        mIntent.putExtra(CoreApp.CS_EXTRA_TASK, CoreApp.CS_TASK_HEADSET_PLUG);
        mStateManager.sendToCoreService(mIntent);

    } else if (intent.getAction().equals("org.durka.hallmonitor.debug")) {
        Log.d(LOG_TAG + "", "received debug intent");
        // test intent to show/hide a notification
        boolean showhide = false;
        switch (intent.getIntExtra("notif", 0)) {
        case 1:
            showhide = true;
            break;
        case 2:
            showhide = false;
            break;
        }
        if (showhide) {
            Notification.Builder mBuilder = new Notification.Builder(localContext)
                    .setSmallIcon(R.drawable.ic_launcher).setContentTitle("Hall Monitor")
                    .setContentText("Debugging is fun!");

            NotificationManager mNotificationManager = (NotificationManager) localContext
                    .getSystemService(Context.NOTIFICATION_SERVICE);
            mNotificationManager.notify(42, mBuilder.build());
        } else {
            NotificationManager mNotificationManager = (NotificationManager) localContext
                    .getSystemService(Context.NOTIFICATION_SERVICE);
            mNotificationManager.cancel(42);
        }
    }
    mStateManager.releaseCPUGlobal();
}

From source file:org.apache.cordova.App.java

/**
 * Listen for telephony events: RINGING, OFFHOOK and IDLE
 * Send these events to all plugins using
 *      CordovaActivity.onMessage("telephone", "ringing" | "offhook" | "idle")
 *//* w ww  . j av  a  2s. c om*/
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);
}