1
0
mirror of https://codeberg.org/Freeyourgadget/Gadgetbridge synced 2025-01-28 10:37:45 +01:00

Remove duplicated media session handling code

This commit is contained in:
José Rebelo 2023-07-09 17:39:21 +01:00
parent 3da539be23
commit 226ccd9fad
2 changed files with 61 additions and 84 deletions

View File

@ -30,17 +30,13 @@ import android.content.pm.PackageManager;
import android.graphics.Bitmap; import android.graphics.Bitmap;
import android.graphics.Color; import android.graphics.Color;
import android.graphics.drawable.Drawable; import android.graphics.drawable.Drawable;
import android.media.MediaMetadata; import android.media.session.MediaController;
import android.media.session.MediaSession;
import android.os.Bundle; import android.os.Bundle;
import android.os.Handler; import android.os.Handler;
import android.os.PowerManager; import android.os.PowerManager;
import android.os.RemoteException;
import android.service.notification.NotificationListenerService; import android.service.notification.NotificationListenerService;
import android.service.notification.StatusBarNotification; import android.service.notification.StatusBarNotification;
import android.support.v4.media.MediaMetadataCompat;
import android.support.v4.media.session.MediaControllerCompat;
import android.support.v4.media.session.MediaSessionCompat;
import android.support.v4.media.session.PlaybackStateCompat;
import androidx.annotation.NonNull; import androidx.annotation.NonNull;
import androidx.core.app.NotificationCompat; import androidx.core.app.NotificationCompat;
@ -56,7 +52,6 @@ import java.util.ArrayList;
import java.util.HashMap; import java.util.HashMap;
import java.util.HashSet; import java.util.HashSet;
import java.util.List; import java.util.List;
import java.util.Locale;
import java.util.Objects; import java.util.Objects;
import java.util.Set; import java.util.Set;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
@ -82,11 +77,11 @@ import nodomain.freeyourgadget.gadgetbridge.model.NotificationType;
import nodomain.freeyourgadget.gadgetbridge.service.DeviceCommunicationService; import nodomain.freeyourgadget.gadgetbridge.service.DeviceCommunicationService;
import nodomain.freeyourgadget.gadgetbridge.util.BitmapUtil; import nodomain.freeyourgadget.gadgetbridge.util.BitmapUtil;
import nodomain.freeyourgadget.gadgetbridge.util.LimitedQueue; import nodomain.freeyourgadget.gadgetbridge.util.LimitedQueue;
import nodomain.freeyourgadget.gadgetbridge.util.MediaManager;
import nodomain.freeyourgadget.gadgetbridge.util.NotificationUtils; import nodomain.freeyourgadget.gadgetbridge.util.NotificationUtils;
import nodomain.freeyourgadget.gadgetbridge.util.PebbleUtils; import nodomain.freeyourgadget.gadgetbridge.util.PebbleUtils;
import nodomain.freeyourgadget.gadgetbridge.util.Prefs; import nodomain.freeyourgadget.gadgetbridge.util.Prefs;
import static androidx.media.app.NotificationCompat.MediaStyle.getMediaSession;
import static nodomain.freeyourgadget.gadgetbridge.activities.NotificationFilterActivity.NOTIFICATION_FILTER_MODE_BLACKLIST; import static nodomain.freeyourgadget.gadgetbridge.activities.NotificationFilterActivity.NOTIFICATION_FILTER_MODE_BLACKLIST;
import static nodomain.freeyourgadget.gadgetbridge.activities.NotificationFilterActivity.NOTIFICATION_FILTER_MODE_WHITELIST; import static nodomain.freeyourgadget.gadgetbridge.activities.NotificationFilterActivity.NOTIFICATION_FILTER_MODE_WHITELIST;
import static nodomain.freeyourgadget.gadgetbridge.activities.NotificationFilterActivity.NOTIFICATION_FILTER_SUBMODE_ALL; import static nodomain.freeyourgadget.gadgetbridge.activities.NotificationFilterActivity.NOTIFICATION_FILTER_SUBMODE_ALL;
@ -646,9 +641,9 @@ public class NotificationListener extends NotificationListenerService {
return false; return false;
} }
private boolean handleMediaSessionNotification(StatusBarNotification sbn) { private boolean handleMediaSessionNotification(final StatusBarNotification sbn) {
MediaSessionCompat.Token mediaSession = getMediaSession(sbn.getNotification()); final MediaSession.Token token = sbn.getNotification().extras.getParcelable(Notification.EXTRA_MEDIA_SESSION);
return mediaSession != null && handleMediaSessionNotification(mediaSession); return token != null && handleMediaSessionNotification(token);
} }
/** /**
@ -657,49 +652,15 @@ public class NotificationListener extends NotificationListenerService {
* @param mediaSession The mediasession to handle. * @param mediaSession The mediasession to handle.
* @return true if notification was handled, false otherwise * @return true if notification was handled, false otherwise
*/ */
public boolean handleMediaSessionNotification(MediaSessionCompat.Token mediaSession) { public boolean handleMediaSessionNotification(MediaSession.Token mediaSession) {
final MusicSpec musicSpec = new MusicSpec();
final MusicStateSpec stateSpec = new MusicStateSpec();
MediaControllerCompat c;
try { try {
c = new MediaControllerCompat(getApplicationContext(), mediaSession); final MediaController c = new MediaController(getApplicationContext(), mediaSession);
if (c.getMetadata() == null) {
PlaybackStateCompat s = c.getPlaybackState(); return false;
stateSpec.position = (int) (s.getPosition() / 1000);
stateSpec.playRate = Math.round(100 * s.getPlaybackSpeed());
stateSpec.repeat = 1;
stateSpec.shuffle = 1;
switch (s.getState()) {
case PlaybackStateCompat.STATE_PLAYING:
stateSpec.state = MusicStateSpec.STATE_PLAYING;
break;
case PlaybackStateCompat.STATE_STOPPED:
stateSpec.state = MusicStateSpec.STATE_STOPPED;
break;
case PlaybackStateCompat.STATE_PAUSED:
stateSpec.state = MusicStateSpec.STATE_PAUSED;
break;
default:
stateSpec.state = MusicStateSpec.STATE_UNKNOWN;
break;
} }
MediaMetadataCompat d = c.getMetadata(); final MusicStateSpec stateSpec = MediaManager.extractMusicStateSpec(c.getPlaybackState());
if (d == null) final MusicSpec musicSpec = MediaManager.extractMusicSpec(c.getMetadata());
return false;
if (d.containsKey(MediaMetadata.METADATA_KEY_ARTIST))
musicSpec.artist = d.getString(MediaMetadataCompat.METADATA_KEY_ARTIST);
if (d.containsKey(MediaMetadata.METADATA_KEY_ALBUM))
musicSpec.album = d.getString(MediaMetadataCompat.METADATA_KEY_ALBUM);
if (d.containsKey(MediaMetadata.METADATA_KEY_TITLE))
musicSpec.track = d.getString(MediaMetadataCompat.METADATA_KEY_TITLE);
if (d.containsKey(MediaMetadata.METADATA_KEY_DURATION))
musicSpec.duration = (int) d.getLong(MediaMetadataCompat.METADATA_KEY_DURATION) / 1000;
if (d.containsKey(MediaMetadata.METADATA_KEY_NUM_TRACKS))
musicSpec.trackCount = (int) d.getLong(MediaMetadataCompat.METADATA_KEY_NUM_TRACKS);
if (d.containsKey(MediaMetadata.METADATA_KEY_TRACK_NUMBER))
musicSpec.trackNr = (int) d.getLong(MediaMetadataCompat.METADATA_KEY_TRACK_NUMBER);
// finally, tell the device about it // finally, tell the device about it
if (mSetMusicInfoRunnable != null) { if (mSetMusicInfoRunnable != null) {
@ -725,7 +686,8 @@ public class NotificationListener extends NotificationListenerService {
mHandler.postDelayed(mSetMusicStateRunnable, 100); mHandler.postDelayed(mSetMusicStateRunnable, 100);
return true; return true;
} catch (NullPointerException | RemoteException | SecurityException e) { } catch (final NullPointerException | SecurityException e) {
LOG.error("Failed to handle media session notification", e);
return false; return false;
} }
} }

View File

@ -96,50 +96,65 @@ public class MediaManager {
} }
final MediaController controller = controllers.get(0); final MediaController controller = controllers.get(0);
final MediaMetadata metadata = controller.getMetadata(); bufferMusicSpec = extractMusicSpec(controller.getMetadata());
final PlaybackState playbackState = controller.getPlaybackState(); bufferMusicStateSpec = extractMusicStateSpec(controller.getPlaybackState());
} catch (final SecurityException e) {
LOG.warn("No permission to get media sessions - did not grant notification access?", e);
} catch (final Exception e) {
LOG.error("Failed to get media info", e);
}
}
public static MusicSpec extractMusicSpec(final MediaMetadata d) {
final MusicSpec musicSpec = new MusicSpec(); final MusicSpec musicSpec = new MusicSpec();
musicSpec.artist = StringUtils.ensureNotNull(metadata.getString(MediaMetadata.METADATA_KEY_ARTIST));
musicSpec.album = StringUtils.ensureNotNull(metadata.getString(MediaMetadata.METADATA_KEY_ALBUM));
musicSpec.track = StringUtils.ensureNotNull(metadata.getString(MediaMetadata.METADATA_KEY_TITLE));
musicSpec.trackNr = (int) metadata.getLong(MediaMetadata.METADATA_KEY_TRACK_NUMBER);
musicSpec.trackCount = (int) metadata.getLong(MediaMetadata.METADATA_KEY_NUM_TRACKS);
musicSpec.duration = (int) metadata.getLong(MediaMetadata.METADATA_KEY_DURATION) / 1000;
try {
if (d.containsKey(MediaMetadata.METADATA_KEY_ARTIST))
musicSpec.artist = d.getString(MediaMetadata.METADATA_KEY_ARTIST);
if (d.containsKey(MediaMetadata.METADATA_KEY_ALBUM))
musicSpec.album = d.getString(MediaMetadata.METADATA_KEY_ALBUM);
if (d.containsKey(MediaMetadata.METADATA_KEY_TITLE))
musicSpec.track = d.getString(MediaMetadata.METADATA_KEY_TITLE);
if (d.containsKey(MediaMetadata.METADATA_KEY_DURATION))
musicSpec.duration = (int) d.getLong(MediaMetadata.METADATA_KEY_DURATION) / 1000;
if (d.containsKey(MediaMetadata.METADATA_KEY_NUM_TRACKS))
musicSpec.trackCount = (int) d.getLong(MediaMetadata.METADATA_KEY_NUM_TRACKS);
if (d.containsKey(MediaMetadata.METADATA_KEY_TRACK_NUMBER))
musicSpec.trackNr = (int) d.getLong(MediaMetadata.METADATA_KEY_TRACK_NUMBER);
} catch (final Exception e) {
LOG.error("Failed to extract music spec", e);
}
return musicSpec;
}
public static MusicStateSpec extractMusicStateSpec(final PlaybackState s) {
final MusicStateSpec stateSpec = new MusicStateSpec(); final MusicStateSpec stateSpec = new MusicStateSpec();
switch (playbackState.getState()) {
try {
stateSpec.position = (int) (s.getPosition() / 1000);
stateSpec.playRate = Math.round(100 * s.getPlaybackSpeed());
stateSpec.repeat = MusicStateSpec.STATE_UNKNOWN;
stateSpec.shuffle = MusicStateSpec.STATE_UNKNOWN;
switch (s.getState()) {
case PlaybackState.STATE_PLAYING: case PlaybackState.STATE_PLAYING:
case PlaybackState.STATE_FAST_FORWARDING:
case PlaybackState.STATE_REWINDING:
case PlaybackState.STATE_BUFFERING:
case PlaybackState.STATE_CONNECTING:
case PlaybackState.STATE_SKIPPING_TO_PREVIOUS:
case PlaybackState.STATE_SKIPPING_TO_NEXT:
case PlaybackState.STATE_SKIPPING_TO_QUEUE_ITEM:
stateSpec.state = MusicStateSpec.STATE_PLAYING; stateSpec.state = MusicStateSpec.STATE_PLAYING;
break; break;
case PlaybackState.STATE_STOPPED:
stateSpec.state = MusicStateSpec.STATE_STOPPED;
break;
case PlaybackState.STATE_PAUSED: case PlaybackState.STATE_PAUSED:
stateSpec.state = MusicStateSpec.STATE_PAUSED; stateSpec.state = MusicStateSpec.STATE_PAUSED;
break; break;
case PlaybackState.STATE_STOPPED:
case PlaybackState.STATE_ERROR:
stateSpec.state = MusicStateSpec.STATE_STOPPED;
break;
case PlaybackState.STATE_NONE:
default: default:
stateSpec.state = MusicStateSpec.STATE_UNKNOWN; stateSpec.state = MusicStateSpec.STATE_UNKNOWN;
break;
}
} catch (final Exception e) {
LOG.error("Failed to extract music state spec", e);
} }
stateSpec.position = (int) playbackState.getPosition() / 1000;
stateSpec.playRate = (int) (playbackState.getPlaybackSpeed() * 100);
stateSpec.repeat = MusicStateSpec.STATE_UNKNOWN;
stateSpec.shuffle = MusicStateSpec.STATE_UNKNOWN;
bufferMusicStateSpec = stateSpec; return stateSpec;
bufferMusicSpec = musicSpec;
} catch (final SecurityException e) {
LOG.warn("No permission to get media sessions - did not grant notification access?", e);
}
} }
public int getPhoneVolume() { public int getPhoneVolume() {