mirror of
https://github.com/KDE/kdeconnect-android
synced 2025-09-01 14:45:08 +00:00
Replace Handlers by lightweight callbacks
This commit is contained in:
committed by
Albert Vaca Cintora
parent
b8cbd2b382
commit
118e045990
@@ -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 -> {
|
||||
|
||||
|
@@ -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,16 +212,8 @@ 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.setPlayerListUpdatedHandler("activity", new Handler() {
|
||||
@Override
|
||||
public void handleMessage(Message msg) {
|
||||
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,
|
||||
@@ -286,7 +277,6 @@ public class MprisNowPlayingFragment extends Fragment implements VolumeKeyListen
|
||||
}
|
||||
updatePlayerStatus(mpris);
|
||||
});
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
|
@@ -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);
|
||||
|
Reference in New Issue
Block a user