2
0
mirror of https://github.com/KDE/kdeconnect-android synced 2025-08-30 13:47:41 +00:00

Replace Handlers by lightweight callbacks

This commit is contained in:
Albert Vaca Cintora
2023-03-22 20:52:39 +01:00
committed by Albert Vaca Cintora
parent b8cbd2b382
commit 118e045990
3 changed files with 69 additions and 86 deletions

View File

@@ -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 -> {

View File

@@ -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<String> playerList = mpris.getPlayerList();
final ArrayAdapter<String> 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<String> playerList = mpris.getPlayerList();
final ArrayAdapter<String> 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);
});
});
});
}

View File

@@ -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<String, MprisPlayer> players = new ConcurrentHashMap<>();
private boolean supportAlbumArtPayload = false;
private final ConcurrentHashMap<String, Handler> playerStatusUpdated = new ConcurrentHashMap<>();
private final ConcurrentHashMap<String, Handler> playerListUpdated = new ConcurrentHashMap<>();
private final ConcurrentHashMap<String, Callback> playerStatusUpdated = new ConcurrentHashMap<>();
private final ConcurrentHashMap<String, Callback> 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);