Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 0d937f7a authored by Danesh M's avatar Danesh M Committed by Zhao Wei Liew
Browse files

AudioService: Launch default music player on headset connect

This squashes the following commits from cm-13.0:
ffcfe4c5 AudioService: launch default music player on headset connect [2/3]
291b3914 Do not start music app when headset is unplugged
f87c5508 AudioService: don't launch music player while in call
95a503c4 AudioService: Prevent crash when there is no music app

Change-Id: Ibbb0607a56ab4d9246c14632eb4199558866854a
parent 3fccac57
Loading
Loading
Loading
Loading
+30 −0
Original line number Diff line number Diff line
@@ -39,6 +39,7 @@ import android.bluetooth.BluetoothClass;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothHeadset;
import android.bluetooth.BluetoothProfile;
import android.content.ActivityNotFoundException;
import android.content.BroadcastReceiver;
import android.content.ComponentName;
import android.content.ContentResolver;
@@ -5377,12 +5378,18 @@ public class AudioService extends IAudioService.Stub {
            connType = AudioRoutesInfo.MAIN_HEADSET;
            intent.setAction(Intent.ACTION_HEADSET_PLUG);
            intent.putExtra("microphone", 1);
            if (state == 1) {
                launchMusicPlayer();
            }
        } else if (device == AudioSystem.DEVICE_OUT_WIRED_HEADPHONE ||
                   device == AudioSystem.DEVICE_OUT_LINE) {
            /*do apps care about line-out vs headphones?*/
            connType = AudioRoutesInfo.MAIN_HEADPHONES;
            intent.setAction(Intent.ACTION_HEADSET_PLUG);
            intent.putExtra("microphone", 0);
            if (state == 1) {
                launchMusicPlayer();
            }
        } else if (device == AudioSystem.DEVICE_OUT_HDMI ||
                device == AudioSystem.DEVICE_OUT_HDMI_ARC) {
            connType = AudioRoutesInfo.MAIN_HDMI;
@@ -5415,6 +5422,29 @@ public class AudioService extends IAudioService.Stub {
        }
    }

    private void launchMusicPlayer() {
        boolean shouldLaunch = CMSettings.System.getIntForUser(mContext.getContentResolver(),
                CMSettings.System.HEADSET_CONNECT_PLAYER, 0, UserHandle.USER_CURRENT) == 1;
        if (!shouldLaunch) {
            return;
        }

        TelecomManager tm = (TelecomManager) mContext.getSystemService(Context.TELECOM_SERVICE);
        if (tm.isInCall()) {
            return;
        }

        Intent intent = new Intent(Intent.ACTION_MAIN);
        intent.addCategory(Intent.CATEGORY_APP_MUSIC);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

        try {
            mContext.startActivity(intent);
        } catch (ActivityNotFoundException e) {
            Log.w(TAG, "No music player Activity was found");
        }
    }

    private void onSetWiredDeviceConnectionState(int device, int state, String address,
            String deviceName, String caller) {
        if (DEBUG_DEVICES) {