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

Commit 2b2278c4 authored by Ajay Panicker's avatar Ajay Panicker
Browse files

Send correct key state and ignore keys from inactive device (2/2)

Also make it clearer whether the key state is pushed or released.

Bug: 79178216
Test: Run host native test net_test_avrcp
Change-Id: I92a2006412fad2fec8f0a5d57e71c63d344f87ae
(cherry picked from commit e68b1b5fcf7aaf3b656e36d13e9f1f082f4a22e1)
parent ba0051a5
Loading
Loading
Loading
Loading
+8 −7
Original line number Diff line number Diff line
@@ -39,7 +39,7 @@ static std::shared_timed_mutex interface_mutex;
static std::shared_timed_mutex callbacks_mutex;

// Forward Declarations
static void sendMediaKeyEvent(int, int);
static void sendMediaKeyEvent(int, KeyState);
static std::string getCurrentMediaId();
static SongInfo getSongInfo();
static PlayStatus getCurrentPlayStatus();
@@ -67,8 +67,8 @@ static void setVolume(int8_t volume);
// as it is hard to get a handle on the JNI thread from here.
class AvrcpMediaInterfaceImpl : public MediaInterface {
 public:
  void SendKeyEvent(uint8_t key, uint8_t status) {
    sendMediaKeyEvent(key, status);
  void SendKeyEvent(uint8_t key, KeyState state) {
    sendMediaKeyEvent(key, state);
  }

  void GetSongInfo(SongInfoCallback cb) override {
@@ -169,7 +169,7 @@ static void classInitNative(JNIEnv* env, jclass clazz) {
      clazz, "getPlayStatus", "()Lcom/android/bluetooth/avrcp/PlayStatus;");

  method_sendMediaKeyEvent =
      env->GetMethodID(clazz, "sendMediaKeyEvent", "(II)V");
      env->GetMethodID(clazz, "sendMediaKeyEvent", "(IZ)V");

  method_getCurrentMediaId =
      env->GetMethodID(clazz, "getCurrentMediaId", "()Ljava/lang/String;");
@@ -294,13 +294,14 @@ jboolean disconnectDeviceNative(JNIEnv* env, jobject object, jstring address) {
                                                             : JNI_FALSE;
}

static void sendMediaKeyEvent(int key, int state) {
static void sendMediaKeyEvent(int key, KeyState state) {
  ALOGD("%s", __func__);
  std::shared_lock<std::shared_timed_mutex> lock(callbacks_mutex);
  CallbackEnv sCallbackEnv(__func__);
  if (!sCallbackEnv.valid() || !mJavaInterface) return;
  sCallbackEnv->CallVoidMethod(mJavaInterface, method_sendMediaKeyEvent, key,
                               state);
  sCallbackEnv->CallVoidMethod(
      mJavaInterface, method_sendMediaKeyEvent, key,
      state == KeyState::PUSHED ? JNI_TRUE : JNI_FALSE);
}

static SongInfo getSongInfoFromJavaObj(JNIEnv* env, jobject metadata) {
+3 −3
Original line number Diff line number Diff line
@@ -75,14 +75,14 @@ public class AvrcpNativeInterface {
        return mAvrcpService.getPlayState();
    }

    void sendMediaKeyEvent(int keyEvent, int state) {
        d("sendMediaKeyEvent: keyEvent=" + keyEvent + " state=" + state);
    void sendMediaKeyEvent(int keyEvent, boolean pushed) {
        d("sendMediaKeyEvent: keyEvent=" + keyEvent + " pushed=" + pushed);
        if (mAvrcpService == null) {
            Log.w(TAG, "sendMediaKeyEvent(): AvrcpTargetService is null");
            return;
        }

        mAvrcpService.sendMediaKeyEvent(keyEvent, state);
        mAvrcpService.sendMediaKeyEvent(keyEvent, pushed);
    }

    String getCurrentMediaId() {
+3 −3
Original line number Diff line number Diff line
@@ -288,9 +288,9 @@ public class AvrcpTargetService extends ProfileService {

    // TODO (apanicke): Handle key events here in the service. Currently it was more convenient to
    // handle them there but logically they make more sense handled here.
    void sendMediaKeyEvent(int event, int state) {
        if (DEBUG) Log.d(TAG, "getMediaKeyEvent: event=" + event + " state=" + state);
        mMediaPlayerList.sendMediaKeyEvent(event, state);
    void sendMediaKeyEvent(int event, boolean pushed) {
        if (DEBUG) Log.d(TAG, "getMediaKeyEvent: event=" + event + " pushed=" + pushed);
        mMediaPlayerList.sendMediaKeyEvent(event, pushed);
    }

    void setActiveDevice(String address) {
+4 −4
Original line number Diff line number Diff line
@@ -480,10 +480,10 @@ public class MediaPlayerList {
        sendMediaUpdate(getActivePlayer().getCurrentMediaData());
    }

    // TODO (apanicke): Add logging for media key events
    void sendMediaKeyEvent(int key, int state) {
        d("sendMediaKeyEvent: key=" + key + " state=" + state);
        int action = state == 0 ? KeyEvent.ACTION_DOWN : KeyEvent.ACTION_UP;
    // TODO (apanicke): Add logging for media key events in dumpsys
    void sendMediaKeyEvent(int key, boolean pushed) {
        d("sendMediaKeyEvent: key=" + key + " pushed=" + pushed);
        int action = pushed ? KeyEvent.ACTION_DOWN : KeyEvent.ACTION_UP;
        KeyEvent event = new KeyEvent(action, AvrcpPassthrough.toKeyCode(key));
        mMediaSessionManager.dispatchMediaKeyEvent(event);
    }