From 118e0459901c33b42e1bfb8e5acf95bcd41d3e0f Mon Sep 17 00:00:00 2001 From: Albert Vaca Cintora Date: Wed, 22 Mar 2023 20:52:39 +0100 Subject: [PATCH] Replace Handlers by lightweight callbacks --- .../MprisPlugin/MprisMediaSession.java | 14 +-- .../MprisPlugin/MprisNowPlayingFragment.java | 118 ++++++++---------- .../Plugins/MprisPlugin/MprisPlugin.java | 23 ++-- 3 files changed, 69 insertions(+), 86 deletions(-) diff --git a/src/org/kde/kdeconnect/Plugins/MprisPlugin/MprisMediaSession.java b/src/org/kde/kdeconnect/Plugins/MprisPlugin/MprisMediaSession.java index f6d489eb..7580d297 100644 --- a/src/org/kde/kdeconnect/Plugins/MprisPlugin/MprisMediaSession.java +++ b/src/org/kde/kdeconnect/Plugins/MprisPlugin/MprisMediaSession.java @@ -14,9 +14,6 @@ import android.content.SharedPreferences; import android.graphics.Bitmap; import android.media.AudioManager; import android.os.Build; -import android.os.Handler; -import android.os.Looper; -import android.os.Message; import android.preference.PreferenceManager; import android.service.notification.StatusBarNotification; import android.support.v4.media.MediaMetadataCompat; @@ -77,13 +74,6 @@ public class MprisMediaSession implements private Context context; private MediaSessionCompat mediaSession; - //Callback for mpris plugin updates - private final Handler mediaNotificationHandler = new Handler(Looper.getMainLooper()) { - @Override - public void handleMessage(Message msg) { - updateMediaNotification(); - } - }; //Callback for control via the media session API private final MediaSessionCompat.Callback mediaSessionCallback = new MediaSessionCompat.Callback() { @Override @@ -134,8 +124,8 @@ public class MprisMediaSession implements context = _context; mprisDevices.add(device); - mpris.setPlayerListUpdatedHandler("media_notification", mediaNotificationHandler); - mpris.setPlayerStatusUpdatedHandler("media_notification", mediaNotificationHandler); + mpris.setPlayerListUpdatedHandler("media_notification", this::updateMediaNotification); + mpris.setPlayerStatusUpdatedHandler("media_notification", this::updateMediaNotification); NotificationReceiver.RunCommand(context, service -> { diff --git a/src/org/kde/kdeconnect/Plugins/MprisPlugin/MprisNowPlayingFragment.java b/src/org/kde/kdeconnect/Plugins/MprisPlugin/MprisNowPlayingFragment.java index 01c1d7c7..07f523bb 100644 --- a/src/org/kde/kdeconnect/Plugins/MprisPlugin/MprisNowPlayingFragment.java +++ b/src/org/kde/kdeconnect/Plugins/MprisPlugin/MprisNowPlayingFragment.java @@ -9,7 +9,6 @@ import android.graphics.drawable.Drawable; import android.net.Uri; import android.os.Bundle; import android.os.Handler; -import android.os.Message; import android.preference.PreferenceManager; import android.text.TextUtils; import android.view.LayoutInflater; @@ -213,80 +212,71 @@ public class MprisNowPlayingFragment extends Fragment implements VolumeKeyListen BackgroundService.RunWithPlugin(requireContext(), deviceId, MprisPlugin.class, mpris -> { targetPlayer = mpris.getPlayerStatus(targetPlayerName); - mpris.setPlayerStatusUpdatedHandler("activity", new Handler() { - @Override - public void handleMessage(Message msg) { - requireActivity().runOnUiThread(() -> updatePlayerStatus(mpris)); - } - }); + mpris.setPlayerStatusUpdatedHandler("activity", () -> requireActivity().runOnUiThread(() -> updatePlayerStatus(mpris))); + mpris.setPlayerListUpdatedHandler("activity", () -> { + final List playerList = mpris.getPlayerList(); + final ArrayAdapter adapter = new ArrayAdapter<>(requireContext(), + android.R.layout.simple_spinner_item, + playerList.toArray(ArrayUtils.EMPTY_STRING_ARRAY) + ); - mpris.setPlayerListUpdatedHandler("activity", new Handler() { - @Override - public void handleMessage(Message msg) { - final List playerList = mpris.getPlayerList(); - final ArrayAdapter adapter = new ArrayAdapter<>(requireContext(), - android.R.layout.simple_spinner_item, - playerList.toArray(ArrayUtils.EMPTY_STRING_ARRAY) - ); + adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); + requireActivity().runOnUiThread(() -> { + mprisControlBinding.playerSpinner.setAdapter(adapter); - adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); - requireActivity().runOnUiThread(() -> { - mprisControlBinding.playerSpinner.setAdapter(adapter); + if (playerList.isEmpty()) { + mprisControlBinding.noPlayers.setVisibility(View.VISIBLE); + mprisControlBinding.playerSpinner.setVisibility(View.GONE); + mprisControlBinding.nowPlayingTextview.setText(""); + } else { + mprisControlBinding.noPlayers.setVisibility(View.GONE); + mprisControlBinding.playerSpinner.setVisibility(View.VISIBLE); + } - if (playerList.isEmpty()) { - mprisControlBinding.noPlayers.setVisibility(View.VISIBLE); - mprisControlBinding.playerSpinner.setVisibility(View.GONE); - mprisControlBinding.nowPlayingTextview.setText(""); - } else { - mprisControlBinding.noPlayers.setVisibility(View.GONE); - mprisControlBinding.playerSpinner.setVisibility(View.VISIBLE); - } + mprisControlBinding.playerSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() { + @Override + public void onItemSelected(AdapterView arg0, View arg1, int pos, long id) { - mprisControlBinding.playerSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() { - @Override - public void onItemSelected(AdapterView arg0, View arg1, int pos, long id) { + if (pos >= playerList.size()) return; - if (pos >= playerList.size()) return; - - String player = playerList.get(pos); - if (targetPlayer != null && player.equals(targetPlayer.getPlayer())) { - return; //Player hasn't actually changed - } - targetPlayer = mpris.getPlayerStatus(player); - updatePlayerStatus(mpris); - - if (targetPlayer != null && targetPlayer.isPlaying()) { - MprisMediaSession.getInstance().playerSelected(targetPlayer); - } + String player = playerList.get(pos); + if (targetPlayer != null && player.equals(targetPlayer.getPlayer())) { + return; //Player hasn't actually changed } + targetPlayer = mpris.getPlayerStatus(player); + updatePlayerStatus(mpris); - @Override - public void onNothingSelected(AdapterView arg0) { - targetPlayer = null; - } - }); - - if (targetPlayer == null) { - //If no player is selected, try to select a playing player - targetPlayer = mpris.getPlayingPlayer(); - } - //Try to select the specified player - if (targetPlayer != null) { - int targetIndex = adapter.getPosition(targetPlayer.getPlayer()); - if (targetIndex >= 0) { - mprisControlBinding.playerSpinner.setSelection(targetIndex); - } else { - targetPlayer = null; + if (targetPlayer != null && targetPlayer.isPlaying()) { + MprisMediaSession.getInstance().playerSelected(targetPlayer); } } - //If no player selected, select the first one (if any) - if (targetPlayer == null && !playerList.isEmpty()) { - targetPlayer = mpris.getPlayerStatus(playerList.get(0)); - mprisControlBinding.playerSpinner.setSelection(0); + + @Override + public void onNothingSelected(AdapterView arg0) { + targetPlayer = null; } - updatePlayerStatus(mpris); }); - } + + if (targetPlayer == null) { + //If no player is selected, try to select a playing player + targetPlayer = mpris.getPlayingPlayer(); + } + //Try to select the specified player + if (targetPlayer != null) { + int targetIndex = adapter.getPosition(targetPlayer.getPlayer()); + if (targetIndex >= 0) { + mprisControlBinding.playerSpinner.setSelection(targetIndex); + } else { + targetPlayer = null; + } + } + //If no player selected, select the first one (if any) + if (targetPlayer == null && !playerList.isEmpty()) { + targetPlayer = mpris.getPlayerStatus(playerList.get(0)); + mprisControlBinding.playerSpinner.setSelection(0); + } + updatePlayerStatus(mpris); + }); }); }); } diff --git a/src/org/kde/kdeconnect/Plugins/MprisPlugin/MprisPlugin.java b/src/org/kde/kdeconnect/Plugins/MprisPlugin/MprisPlugin.java index 89ef02c3..2f2e6773 100644 --- a/src/org/kde/kdeconnect/Plugins/MprisPlugin/MprisPlugin.java +++ b/src/org/kde/kdeconnect/Plugins/MprisPlugin/MprisPlugin.java @@ -227,14 +227,18 @@ public class MprisPlugin extends Plugin { } } + public interface Callback { + void callback(); + } + public final static String DEVICE_ID_KEY = "deviceId"; private final static String PACKET_TYPE_MPRIS = "kdeconnect.mpris"; private final static String PACKET_TYPE_MPRIS_REQUEST = "kdeconnect.mpris.request"; private final ConcurrentHashMap players = new ConcurrentHashMap<>(); private boolean supportAlbumArtPayload = false; - private final ConcurrentHashMap playerStatusUpdated = new ConcurrentHashMap<>(); - private final ConcurrentHashMap playerListUpdated = new ConcurrentHashMap<>(); + private final ConcurrentHashMap playerStatusUpdated = new ConcurrentHashMap<>(); + private final ConcurrentHashMap playerListUpdated = new ConcurrentHashMap<>(); @Override public String getDisplayName() { @@ -349,7 +353,7 @@ public class MprisPlugin extends Plugin { for (String key : playerStatusUpdated.keySet()) { try { - playerStatusUpdated.get(key).dispatchMessage(new Message()); + playerStatusUpdated.get(key).callback(); } catch (Exception e) { Log.e("MprisControl", "Exception", e); playerStatusUpdated.remove(key); @@ -390,7 +394,7 @@ public class MprisPlugin extends Plugin { if (!equals) { for (String key : playerListUpdated.keySet()) { try { - playerListUpdated.get(key).dispatchMessage(new Message()); + playerListUpdated.get(key).callback(); } catch (Exception e) { Log.e("MprisControl", "Exception", e); playerListUpdated.remove(key); @@ -412,20 +416,19 @@ public class MprisPlugin extends Plugin { return new String[]{PACKET_TYPE_MPRIS_REQUEST}; } - public void setPlayerStatusUpdatedHandler(String id, Handler h) { + public void setPlayerStatusUpdatedHandler(String id, Callback h) { playerStatusUpdated.put(id, h); - - h.dispatchMessage(new Message()); + h.callback(); } public void removePlayerStatusUpdatedHandler(String id) { playerStatusUpdated.remove(id); } - public void setPlayerListUpdatedHandler(String id, Handler h) { + public void setPlayerListUpdatedHandler(String id, Callback h) { playerListUpdated.put(id, h); - h.dispatchMessage(new Message()); + h.callback(); } public void removePlayerListUpdatedHandler(String id) { @@ -500,7 +503,7 @@ public class MprisPlugin extends Plugin { if (players.values().stream().anyMatch(player -> url.equals(player.albumArtUrl))) { for (String key : playerStatusUpdated.keySet()) { try { - playerStatusUpdated.get(key).dispatchMessage(new Message()); + playerStatusUpdated.get(key).callback(); } catch (Exception e) { Log.e("MprisControl", "Exception", e); playerStatusUpdated.remove(key);