Example usage for android.media AudioManager isWiredHeadsetOn

List of usage examples for android.media AudioManager isWiredHeadsetOn

Introduction

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

Prototype

public boolean isWiredHeadsetOn() 

Source Link

Document

Checks whether a wired headset is connected or not.

Usage

From source file:de.tubs.ibr.dtn.dtalkie.TalkieActivity.java

@SuppressWarnings("deprecation")
private void setAudioOutput() {
    AudioManager am = (AudioManager) getSystemService(Context.AUDIO_SERVICE);

    if (am.isBluetoothA2dpOn()) {
        // play without speaker
        am.setSpeakerphoneOn(false);//from w  w w  .  ja v  a2 s.com
    } else if (am.isWiredHeadsetOn()) {
        // play without speaker
        am.setSpeakerphoneOn(false);
    } else {
        // without headset, enable speaker
        am.setSpeakerphoneOn(true);
    }
}

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  www .  j ava 2s .  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.mitre.svmp.events.WebrtcHandler.java

public WebrtcHandler(BaseServer baseServer, VideoStreamInfo vidInfo, Context c) {
    base = baseServer;/*  www  . j a v a 2s  .  co m*/
    context = c;
    // Pass in context to allow access to Android managed Audio driver.
    PeerConnectionFactory.initializeAndroidGlobals(context);
    //        "Failed to initializeAndroidGlobals");

    AudioManager audioManager = ((AudioManager) context.getSystemService(Context.AUDIO_SERVICE));
    boolean isWiredHeadsetOn = audioManager.isWiredHeadsetOn();
    audioManager.setMode(isWiredHeadsetOn ? AudioManager.MODE_IN_CALL : AudioManager.MODE_IN_COMMUNICATION);
    audioManager.setSpeakerphoneOn(!isWiredHeadsetOn);

    sdpMediaConstraints = new MediaConstraints();
    sdpMediaConstraints.mandatory.add(new MediaConstraints.KeyValuePair("OfferToReceiveAudio", "true"));
    sdpMediaConstraints.mandatory.add(new MediaConstraints.KeyValuePair("OfferToReceiveVideo", "true"));

    pcConstraints = constraintsFromJSON(vidInfo.getPcConstraints());
    Log.d(TAG, "pcConstraints: " + pcConstraints);

    videoConstraints = constraintsFromJSON(vidInfo.getVideoConstraints());
    Log.d(TAG, "videoConstraints: " + videoConstraints);
    //        
    //        videoConstraints = new MediaConstraints();
    //        videoConstraints.mandatory.add(new MediaConstraints.KeyValuePair("minWidth","720"));
    //        videoConstraints.mandatory.add(new MediaConstraints.KeyValuePair("minHeight","1280"));
    //        videoConstraints.mandatory.add(new MediaConstraints.KeyValuePair("maxWidth","720"));
    //        videoConstraints.mandatory.add(new MediaConstraints.KeyValuePair("maxHeight","1280"));
    //        videoConstraints.mandatory.add(new MediaConstraints.KeyValuePair("minFrameRate","24"));

    audioConstraints = new MediaConstraints(); //null;
    audioConstraints.mandatory.add(new MediaConstraints.KeyValuePair("audio", "true"));

    iceServers = iceServersFromPCConfigJSON(vidInfo.getIceServers());
    onIceServers(iceServers);
}

From source file:de.tubs.ibr.dtn.chat.service.ChatService.java

@SuppressWarnings("deprecation")
private void showNotification(Intent intent) {
    int defaults = 0;

    SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
    if (prefs.getBoolean("vibrateOnMessage", true)) {
        defaults |= Notification.DEFAULT_VIBRATE;
    }//from www .j  a  v a  2  s .  c  o m

    Long buddyId = intent.getLongExtra(EXTRA_BUDDY_ID, -1L);
    String displayName = intent.getStringExtra(EXTRA_DISPLAY_NAME);
    String textBody = intent.getStringExtra(EXTRA_TEXT_BODY);

    CharSequence tickerText = getString(R.string.new_message_from) + " " + displayName;
    CharSequence contentTitle = getString(R.string.new_message);
    CharSequence contentText = displayName + ":\n" + textBody;

    TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);

    // forward intent to the activity
    intent.setClass(this, MainActivity.class);

    // Adds the intent to the main view
    stackBuilder.addNextIntent(intent);
    // Gets a PendingIntent containing the entire back stack
    PendingIntent contentIntent = stackBuilder.getPendingIntent(buddyId.intValue(),
            PendingIntent.FLAG_UPDATE_CURRENT);

    NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
    builder.setContentTitle(contentTitle);
    builder.setContentText(contentText);
    builder.setSmallIcon(R.drawable.ic_message);
    builder.setTicker(tickerText);
    builder.setDefaults(defaults);
    builder.setWhen(System.currentTimeMillis());
    builder.setContentIntent(contentIntent);
    builder.setLights(0xffff0000, 300, 1000);
    builder.setSound(
            Uri.parse(prefs.getString("ringtoneOnMessage", "content://settings/system/notification_sound")));
    builder.setAutoCancel(true);

    Notification notification = builder.getNotification();

    NotificationManager mNotificationManager = (NotificationManager) getSystemService(
            Context.NOTIFICATION_SERVICE);
    mNotificationManager.notify(buddyId.toString(), MESSAGE_NOTIFICATION, notification);

    if (prefs.getBoolean("ttsWhenOnHeadset", false)) {
        AudioManager am = (AudioManager) getSystemService(Context.AUDIO_SERVICE);

        if (am.isBluetoothA2dpOn() || am.isWiredHeadsetOn()) {
            // speak the notification
            Intent tts_intent = new Intent(this, TTSService.class);
            tts_intent.setAction(TTSService.INTENT_SPEAK);
            tts_intent.putExtra("speechText", tickerText + ": " + textBody);
            startService(tts_intent);
        }
    }
}

From source file:com.socialdisasters.other.service.ChatService.java

@SuppressWarnings("deprecation")
private void showNotification(Intent intent) {
    int defaults = 0;

    SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
    if (prefs.getBoolean("vibrateOnMessage", true)) {
        defaults |= Notification.DEFAULT_VIBRATE;
    }/*from www. ja  v a2  s.  c o m*/

    Long buddyId = intent.getLongExtra(EXTRA_BUDDY_ID, -1L);
    String displayName = intent.getStringExtra(EXTRA_DISPLAY_NAME);
    String textBody = intent.getStringExtra(EXTRA_TEXT_BODY);

    CharSequence tickerText = getString(R.string.new_message_from) + " " + displayName;
    CharSequence contentTitle = getString(R.string.new_message);
    CharSequence contentText = displayName + ":\n" + textBody;

    TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);

    // forward intent to the activity
    intent.setClass(this, MainActivity.class);

    // Adds the intent to the main view
    stackBuilder.addNextIntent(intent);

    // Gets a PendingIntent containing the entire back stack
    PendingIntent contentIntent = stackBuilder.getPendingIntent(buddyId.intValue(),
            PendingIntent.FLAG_UPDATE_CURRENT);

    // create a reply intent
    String replyLabel = getResources().getString(R.string.reply_label);
    RemoteInput remoteInput = new RemoteInput.Builder(EXTRA_VOICE_REPLY).setLabel(replyLabel).build();

    Intent replyIntent = new Intent(this, ReplyActivity.class);
    replyIntent.putExtra(EXTRA_BUDDY_ID, buddyId);
    PendingIntent replyPendingIntent = PendingIntent.getActivity(this, buddyId.intValue(), replyIntent,
            PendingIntent.FLAG_UPDATE_CURRENT);
    NotificationCompat.Action action = new NotificationCompat.Action.Builder(R.drawable.ic_action_reply,
            getString(R.string.reply_label), replyPendingIntent).addRemoteInput(remoteInput).build();

    NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
    builder.setContentTitle(contentTitle);
    builder.setContentText(contentText);
    builder.setSmallIcon(R.drawable.ic_stat_message);
    builder.setTicker(tickerText);
    builder.setDefaults(defaults);
    builder.setWhen(System.currentTimeMillis());
    builder.setContentIntent(contentIntent);
    builder.setLights(0xffff0000, 300, 1000);
    builder.setSound(
            Uri.parse(prefs.getString("ringtoneOnMessage", "content://settings/system/notification_sound")));
    builder.extend(new WearableExtender().addAction(action));
    builder.setAutoCancel(false);

    Notification notification = builder.getNotification();

    NotificationManager mNotificationManager = (NotificationManager) getSystemService(
            Context.NOTIFICATION_SERVICE);
    mNotificationManager.notify(buddyId.toString(), MESSAGE_NOTIFICATION, notification);

    if (prefs.getBoolean("ttsWhenOnHeadset", false)) {
        AudioManager am = (AudioManager) getSystemService(Context.AUDIO_SERVICE);

        if (am.isBluetoothA2dpOn() || am.isWiredHeadsetOn()) {
            // speak the notification
            Intent tts_intent = new Intent(this, TTSService.class);
            tts_intent.setAction(TTSService.INTENT_SPEAK);
            tts_intent.putExtra("speechText", tickerText + ": " + textBody);
            startService(tts_intent);
        }
    }
}

From source file:com.yohpapa.research.simplemusicplayer.PlaybackService.java

private int getCurrentAudioPathOtherThanA2dp() {
    AudioManager manager = (AudioManager) getSystemService(AUDIO_SERVICE);
    if (manager == null)
        return AUDIO_PATH_UNKNOWN;

    // This method was deprecated in API level 14.
    // Use only to check is a headset is connected or not.
    boolean result = manager.isWiredHeadsetOn();
    if (result) {
        return AUDIO_PATH_WIRED;
    }/*from ww  w .j a  va  2 s  .c  om*/

    // setSpeakerphoneOn does not work well...
    return AUDIO_PATH_SPEAKER;
}

From source file:com.yohpapa.research.simplemusicplayer.PlaybackService.java

private int getCurrentAudioPath() {
    AudioManager manager = (AudioManager) getSystemService(AUDIO_SERVICE);
    if (manager == null)
        return AUDIO_PATH_UNKNOWN;

    boolean result = manager.isBluetoothA2dpOn();
    if (result) {
        return AUDIO_PATH_A2DP;
    }//from w ww . j av  a 2s . c  om

    // This method was deprecated in API level 14.
    // Use only to check is a headset is connected or not.
    result = manager.isWiredHeadsetOn();
    if (result) {
        return AUDIO_PATH_WIRED;
    }

    // setSpeakerphoneOn does not work well...
    return AUDIO_PATH_SPEAKER;
}

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);
    // ??/*w w  w . j  av a2 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.digium.respokesdk.RespokeCall.java

private void addLocalStreams(Context context) {
    AudioManager audioManager = ((AudioManager) context.getSystemService(Context.AUDIO_SERVICE));
    // TODO(fischman): figure out how to do this Right(tm) and remove the suppression.
    @SuppressWarnings("deprecation")
    boolean isWiredHeadsetOn = audioManager.isWiredHeadsetOn();
    audioManager.setMode(isWiredHeadsetOn ? AudioManager.MODE_IN_CALL : AudioManager.MODE_IN_COMMUNICATION);
    audioManager.setSpeakerphoneOn(!isWiredHeadsetOn);

    localStream = peerConnectionFactory.createLocalMediaStream("ARDAMS");

    if (!audioOnly) {
        VideoCapturer capturer = getVideoCapturer();
        MediaConstraints videoConstraints = new MediaConstraints();
        videoSource = peerConnectionFactory.createVideoSource(capturer, videoConstraints);
        VideoTrack videoTrack = peerConnectionFactory.createVideoTrack("ARDAMSv0", videoSource);
        videoTrack.addRenderer(new VideoRenderer(localRender));
        localStream.addTrack(videoTrack);
    }/*from ww  w  .  ja  v a 2  s.  c o  m*/

    localStream.addTrack(peerConnectionFactory.createAudioTrack("ARDAMSa0",
            peerConnectionFactory.createAudioSource(new MediaConstraints())));

    peerConnection.addStream(localStream);
}

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));
    // ??/*  w  w w  .  ja v  a  2s.co  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())));

        }
    }
}