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

Commit f90b8133 authored by ezio84's avatar ezio84 Committed by Łukasz Patron
Browse files

LineageButtons: Add support for skipping tracks with remote control

For Spotify and other players that allow music controls for
remote devices (PC, PS4) through the media notification

Change-Id: I38887f8b1cff1a0c1e3adadbfe37d5af59b5cdcc
parent d8742f07
Loading
Loading
Loading
Loading
+37 −8
Original line number Original line Diff line number Diff line
/**
/**
 * Copyright (C) 2017 The LineageOS Project
 * Copyright (C) 2018,2020 The LineageOS Project
 *
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * you may not use this file except in compliance with the License.
@@ -21,7 +21,10 @@ import android.content.Context;
import android.content.res.Resources;
import android.content.res.Resources;
import android.database.ContentObserver;
import android.database.ContentObserver;
import android.media.AudioManager;
import android.media.AudioManager;
import android.media.session.MediaController;
import android.media.session.MediaSessionLegacyHelper;
import android.media.session.MediaSessionLegacyHelper;
import android.media.session.MediaSessionManager;
import android.media.session.PlaybackState;
import android.os.Handler;
import android.os.Handler;
import android.os.Message;
import android.os.Message;
import android.os.UserHandle;
import android.os.UserHandle;
@@ -31,6 +34,8 @@ import android.view.ViewConfiguration;


import lineageos.providers.LineageSettings;
import lineageos.providers.LineageSettings;


import java.util.List;

public final class LineageButtons {
public final class LineageButtons {
    private final String TAG = "LineageButtons";
    private final String TAG = "LineageButtons";
    private final boolean DEBUG = false;
    private final boolean DEBUG = false;
@@ -38,7 +43,8 @@ public final class LineageButtons {
    private static final int MSG_DISPATCH_VOLKEY_WITH_WAKELOCK = 1;
    private static final int MSG_DISPATCH_VOLKEY_WITH_WAKELOCK = 1;


    private final Context mContext;
    private final Context mContext;
    private ButtonHandler mHandler;
    private final ButtonHandler mHandler;
    private final MediaSessionManager mMediaSessionManager;


    private boolean mIsLongPress = false;
    private boolean mIsLongPress = false;


@@ -54,8 +60,7 @@ public final class LineageButtons {
                    if (DEBUG) {
                    if (DEBUG) {
                        Slog.d(TAG, "Dispatching key to audio service");
                        Slog.d(TAG, "Dispatching key to audio service");
                    }
                    }
                    dispatchMediaKeyToAudioService(ev);
                    onSkipTrackEvent(ev);
                    dispatchMediaKeyToAudioService(KeyEvent.changeAction(ev, KeyEvent.ACTION_UP));
                    break;
                    break;
            }
            }
        }
        }
@@ -64,6 +69,7 @@ public final class LineageButtons {
    public LineageButtons(Context context) {
    public LineageButtons(Context context) {
        mContext = context;
        mContext = context;
        mHandler = new ButtonHandler();
        mHandler = new ButtonHandler();
        mMediaSessionManager = mContext.getSystemService(MediaSessionManager.class);


        SettingsObserver observer = new SettingsObserver(new Handler());
        SettingsObserver observer = new SettingsObserver(new Handler());
        observer.observe();
        observer.observe();
@@ -129,11 +135,34 @@ public final class LineageButtons {
        return true;
        return true;
    }
    }


    void dispatchMediaKeyToAudioService(KeyEvent ev) {
    private void triggerKeyEvents(KeyEvent evDown, MediaController controller) {
        if (DEBUG) {
        final KeyEvent evUp = KeyEvent.changeAction(evDown, KeyEvent.ACTION_UP);
            Slog.d(TAG, "Dispatching KeyEvent " + ev + " to audio service");
        mHandler.post(() -> controller.dispatchMediaButtonEvent(evDown));
        mHandler.postDelayed(() -> controller.dispatchMediaButtonEvent(evUp), 20);
    }

    public void onSkipTrackEvent(KeyEvent ev) {
        if (mMediaSessionManager != null) {
            final List<MediaController> sessions = mMediaSessionManager.getActiveSessionsForUser(
                    null, UserHandle.USER_ALL);
            for (MediaController mediaController : sessions) {
                if (PlaybackState.STATE_PLAYING ==
                        getMediaControllerPlaybackState(mediaController)) {
                    triggerKeyEvents(ev, mediaController);
                    break;
                }
            }
        }
    }

    private int getMediaControllerPlaybackState(MediaController controller) {
        if (controller != null) {
            final PlaybackState playbackState = controller.getPlaybackState();
            if (playbackState != null) {
                return playbackState.getState();
            }
        }
        }
        MediaSessionLegacyHelper.getHelper(mContext).sendMediaButtonEvent(ev, true);
        return PlaybackState.STATE_NONE;
    }
    }


    class SettingsObserver extends ContentObserver {
    class SettingsObserver extends ContentObserver {