Example usage for android.media AudioManager isSpeakerphoneOn

List of usage examples for android.media AudioManager isSpeakerphoneOn

Introduction

In this page you can find the example usage for android.media AudioManager isSpeakerphoneOn.

Prototype

public boolean isSpeakerphoneOn() 

Source Link

Document

Checks whether the speakerphone is on or off.

Usage

From source file:com.luhuiguo.cordova.voice.VoiceHandler.java

/**
 * Get the voice device to be used for playback.
 *
 * @return                  1=earpiece, 2=speaker
 *//*from w  w  w  .ja v  a 2 s. c o m*/
public int getVoiceOutputDevice() {
    AudioManager audiMgr = (AudioManager) this.cordova.getActivity().getSystemService(Context.AUDIO_SERVICE);
    if (audiMgr.isSpeakerphoneOn()) {
        return 2;
    } else {
        return 1;
    }
}

From source file:fr.inria.ucn.collectors.SysStateCollector.java

@SuppressWarnings("deprecation")
private JSONObject getAudioState(Context c) throws JSONException {
    AudioManager am = (AudioManager) c.getSystemService(Context.AUDIO_SERVICE);

    JSONObject data = new JSONObject();

    data.put("is_bluetooth_a2dp_on", am.isBluetoothA2dpOn());
    data.put("is_microphone_mute", am.isMicrophoneMute());
    data.put("is_music_active", am.isMusicActive());
    data.put("is_speaker_phone_on", am.isSpeakerphoneOn());
    data.put("is_wired_headset_on", am.isWiredHeadsetOn());

    switch (am.getMode()) {
    case AudioManager.MODE_IN_CALL:
        data.put("mode", "in_call");
        break;/*from  ww  w  .  jav a2s .c om*/
    case AudioManager.MODE_IN_COMMUNICATION:
        data.put("mode", "in_comm");
        break;
    case AudioManager.MODE_NORMAL:
        data.put("mode", "normal");
        break;
    case AudioManager.MODE_RINGTONE:
        data.put("mode", "ringtone");
        break;
    case AudioManager.MODE_INVALID:
    default:
        data.put("mode", "invalid");
        break;
    }

    switch (am.getRingerMode()) {
    case AudioManager.RINGER_MODE_VIBRATE:
        data.put("ringer_mode", "vibrate");
        break;
    case AudioManager.RINGER_MODE_SILENT:
        data.put("ringer_mode", "silent");
        break;
    case AudioManager.RINGER_MODE_NORMAL:
        data.put("ringer_mode", "normal");
        break;
    default:
        data.put("ringer_mode", "invalid");
        break;
    }
    return data;
}

From source file:org.jitsi.android.gui.call.VideoCallActivity.java

/**
 * Fired when speakerphone button is clicked.
 * @param v the speakerphone button <tt>View</tt>.
 *//*from w  w  w .j a  va 2s  .  co  m*/
public void onSpeakerphoneClicked(View v) {
    AudioManager audioManager = JitsiApplication.getAudioManager();
    audioManager.setSpeakerphoneOn(!audioManager.isSpeakerphoneOn());
    updateSpeakerphoneStatus();
}

From source file:im.vector.activity.CallViewActivity.java

private void refreshSpeakerButton() {
    if ((null != mCall) && mCall.getCallState().equals(IMXCall.CALL_STATE_CONNECTED)) {
        mSpeakerSelectionView.setVisibility(View.VISIBLE);

        AudioManager audioManager = (AudioManager) CallViewActivity.this
                .getSystemService(Context.AUDIO_SERVICE);
        if (audioManager.isSpeakerphoneOn()) {
            mSpeakerSelectionView.setImageResource(R.drawable.ic_material_call);
        } else {/*from   w ww  .  ja va 2s.c o m*/
            mSpeakerSelectionView.setImageResource(R.drawable.ic_material_volume);
        }
        CallViewActivity.this.setVolumeControlStream(audioManager.getMode());

    } else {
        mSpeakerSelectionView.setVisibility(View.GONE);
    }
}

From source file:com.ferdi2005.secondgram.voip.VoIPService.java

@SuppressLint("NewApi")
@Override//  ww  w. ja  v  a2 s  .co  m
public void onSensorChanged(SensorEvent event) {
    if (event.sensor.getType() == Sensor.TYPE_PROXIMITY) {
        AudioManager am = (AudioManager) getSystemService(AUDIO_SERVICE);
        if (isHeadsetPlugged || am.isSpeakerphoneOn()
                || (isBluetoothHeadsetConnected() && am.isBluetoothScoOn())) {
            return;
        }
        boolean newIsNear = event.values[0] < Math.min(event.sensor.getMaximumRange(), 3);
        if (newIsNear != isProximityNear) {
            FileLog.d("proximity " + newIsNear);
            isProximityNear = newIsNear;
            try {
                if (isProximityNear) {
                    proximityWakelock.acquire();
                } else {
                    proximityWakelock.release(1); // this is non-public API before L
                }
            } catch (Exception x) {
                FileLog.e(x);
            }
        }
    }
}

From source file:com.yangtsaosoftware.pebblemessenger.services.PebbleCenter.java

private void answerCall(boolean isSpeakon) {

    Context context = getApplicationContext();
    AudioManager audioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
    // ??//from   ww  w  .j a  va 2 s.  co m
    if (!(audioManager.isWiredHeadsetOn() || audioManager.isBluetoothA2dpOn())) {
        // 4.1??? 4.1??Permission Denial: not allowed to
        // send broadcast android.intent.action.HEADSET_PLUG from pid=1324,
        // uid=10017
        // ??????android.permission.CALL_PRIVLEGED?????4.1??????????NULL??

        Constants.log("speakerset", String.format("AudioManager before mode:%d speaker mod:%s",
                audioManager.getMode(), String.valueOf(audioManager.isSpeakerphoneOn())));
        Intent meidaButtonIntent = new Intent(Intent.ACTION_MEDIA_BUTTON);
        KeyEvent keyEvent = new KeyEvent(KeyEvent.ACTION_UP, KeyEvent.KEYCODE_HEADSETHOOK);
        meidaButtonIntent.putExtra(Intent.EXTRA_KEY_EVENT, keyEvent);
        context.sendOrderedBroadcast(meidaButtonIntent, "android.permission.CALL_PRIVILEGED");

        /*TelephonyManager telMag = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
        Class<TelephonyManager> c = TelephonyManager.class;
        Method mthAnswerCall;
        try {
        mthAnswerCall = c.getDeclaredMethod("getITelephony", (Class[]) null);
        mthAnswerCall.setAccessible(true);
        ITelephony iTel = (ITelephony) mthAnswerCall.invoke(telMag, (Object[]) null);
        iTel.answerRingingCall();
                
        } catch (Exception e) {
        e.printStackTrace();
        }*/
        if (isSpeakon) {
            try {
                Thread.sleep(500);

            } catch (InterruptedException e) {
                Constants.log("speakerset", "Problem while sleeping");
            }
            Constants.log("speakerset", "AudioManager answer mode:" + audioManager.getMode() + " speaker mod:"
                    + String.valueOf(audioManager.isSpeakerphoneOn()));
            while (audioManager.getMode() != AudioManager.MODE_IN_CALL) {
                try {
                    Thread.sleep(300);

                } catch (InterruptedException e) {
                    Constants.log("speakerset", "Problem while sleeping");
                }
            }
            // audioManager.setMicrophoneMute(true);
            audioManager.setSpeakerphoneOn(true);
            // audioManager.setMode(AudioManager.MODE_IN_CALL);
            Constants.log("speakerset", String.format("AudioManager set mode:%d speaker mod:%s",
                    audioManager.getMode(), String.valueOf(audioManager.isSpeakerphoneOn())));

        }
    } else {
        Constants.log("speakerset", String.format("AudioManager before mode:%d speaker mod:%s",
                audioManager.getMode(), String.valueOf(audioManager.isSpeakerphoneOn())));

        Intent meidaButtonIntent = new Intent(Intent.ACTION_MEDIA_BUTTON);
        KeyEvent keyEvent = new KeyEvent(KeyEvent.ACTION_UP, KeyEvent.KEYCODE_HEADSETHOOK);
        meidaButtonIntent.putExtra(Intent.EXTRA_KEY_EVENT, keyEvent);
        context.sendOrderedBroadcast(meidaButtonIntent, "android.permission.CALL_PRIVILEGED");

    }
}

From source file:com.yangtsaosoftware.pebblemessenger.services.PebbleCenter.java

private void dialNumber(String phoneNumber, boolean isSpeakon) {
    Context context = getApplicationContext();

    AudioManager audioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
    Constants.log(TAG_NAME, String.format("Dial phone:%s", phoneNumber));
    // ??// www  .  j a va 2s.c  o  m
    Intent callIntent = new Intent(Intent.ACTION_CALL);
    callIntent.setData(Uri.parse(String.format("tel:%s", phoneNumber)));
    callIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    callIntent.addFlags(Intent.FLAG_FROM_BACKGROUND);
    startActivity(callIntent);

    if (!(audioManager.isWiredHeadsetOn() || audioManager.isBluetoothA2dpOn())) {
        // 4.1??? 4.1??Permission Denial: not allowed to
        // send broadcast android.intent.action.HEADSET_PLUG from pid=1324,
        // uid=10017
        // ??????android.permission.CALL_PRIVLEGED?????4.1??????????NULL??

        Constants.log("speakerset", String.format("AudioManager before mode:%d speaker mod:%s",
                audioManager.getMode(), String.valueOf(audioManager.isSpeakerphoneOn())));

        if (isSpeakon) {
            try {
                Thread.sleep(500);

            } catch (InterruptedException e) {
                Constants.log("speakerset", "Problem while sleeping");
            }
            Constants.log("speakerset", String.format("AudioManager answer mode:%d speaker mod:%s",
                    audioManager.getMode(), String.valueOf(audioManager.isSpeakerphoneOn())));
            while (audioManager.getMode() != AudioManager.MODE_IN_CALL) {
                try {
                    Thread.sleep(300);

                } catch (InterruptedException e) {
                    Constants.log("speakerset", "Problem while sleeping");
                }
            }
            // audioManager.setMicrophoneMute(true);
            audioManager.setSpeakerphoneOn(true);
            // audioManager.setMode(AudioManager.MODE_IN_CALL);
            Constants.log("speakerset", String.format("AudioManager set mode:%d speaker mod:%s",
                    audioManager.getMode(), String.valueOf(audioManager.isSpeakerphoneOn())));

        }
    }
}