File indexing completed on 2024-12-22 04:41:39
0001 /* 0002 * SPDX-FileCopyrightText: 2017 Matthijs Tijink <matthijstijink@gmail.com> 0003 * 0004 * SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL 0005 */ 0006 0007 package org.kde.kdeconnect.Plugins.MprisPlugin; 0008 0009 import android.content.BroadcastReceiver; 0010 import android.content.Context; 0011 import android.content.Intent; 0012 import android.support.v4.media.session.MediaSessionCompat; 0013 0014 import org.kde.kdeconnect.KdeConnect; 0015 0016 /** 0017 * Called when the mpris media notification's buttons are pressed 0018 */ 0019 public class MprisMediaNotificationReceiver extends BroadcastReceiver { 0020 public static final String ACTION_PLAY = "ACTION_PLAY"; 0021 public static final String ACTION_PAUSE = "ACTION_PAUSE"; 0022 public static final String ACTION_PREVIOUS = "ACTION_PREVIOUS"; 0023 public static final String ACTION_NEXT = "ACTION_NEXT"; 0024 public static final String ACTION_CLOSE_NOTIFICATION = "ACTION_CLOSE_NOTIFICATION"; 0025 0026 public static final String EXTRA_DEVICE_ID = "deviceId"; 0027 public static final String EXTRA_MPRIS_PLAYER = "player"; 0028 0029 @Override 0030 public void onReceive(Context context, Intent intent) { 0031 //First case: buttons send by other applications via the media session APIs. They don't target a specific device. 0032 if (Intent.ACTION_MEDIA_BUTTON.equals(intent.getAction())) { 0033 //Route these buttons to the media session, which will handle them 0034 MediaSessionCompat mediaSession = MprisMediaSession.getMediaSession(); 0035 if (mediaSession == null) return; 0036 mediaSession.getController().dispatchMediaButtonEvent(intent.getParcelableExtra(Intent.EXTRA_KEY_EVENT)); 0037 } else { 0038 //Second case: buttons on the notification, which we created ourselves 0039 0040 //Get the correct device, the mpris plugin and the mpris player 0041 String deviceId = intent.getStringExtra(EXTRA_DEVICE_ID); 0042 MprisPlugin plugin = KdeConnect.getInstance().getDevicePlugin(deviceId, MprisPlugin.class); 0043 if (plugin == null) return; 0044 MprisPlugin.MprisPlayer player = plugin.getPlayerStatus(intent.getStringExtra(EXTRA_MPRIS_PLAYER)); 0045 if (player == null) return; 0046 0047 //Forward the action to the player 0048 switch (intent.getAction()) { 0049 case ACTION_PLAY: 0050 player.play(); 0051 break; 0052 case ACTION_PAUSE: 0053 player.pause(); 0054 break; 0055 case ACTION_PREVIOUS: 0056 player.previous(); 0057 break; 0058 case ACTION_NEXT: 0059 player.next(); 0060 break; 0061 case ACTION_CLOSE_NOTIFICATION: 0062 //The user dismissed the notification: actually handle its removal correctly 0063 MprisMediaSession.getInstance().closeMediaNotification(); 0064 break; 0065 } 0066 } 0067 } 0068 0069 }