From 51f92e94ebc150b71dc7f9b404526ebefcee408c Mon Sep 17 00:00:00 2001 From: Tony Tam <149837+tonytamsf@users.noreply.github.com> Date: Fri, 1 Nov 2024 22:58:55 -1000 Subject: [PATCH] New way to detect car mode to fix skip to next episode (#7442) --- .../playback/service/internal/LocalPSMP.java | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) diff --git a/playback/service/src/main/java/de/danoeh/antennapod/playback/service/internal/LocalPSMP.java b/playback/service/src/main/java/de/danoeh/antennapod/playback/service/internal/LocalPSMP.java index c1d55c8e8..37cacd262 100644 --- a/playback/service/src/main/java/de/danoeh/antennapod/playback/service/internal/LocalPSMP.java +++ b/playback/service/src/main/java/de/danoeh/antennapod/playback/service/internal/LocalPSMP.java @@ -1,8 +1,6 @@ package de.danoeh.antennapod.playback.service.internal; -import android.app.UiModeManager; import android.content.Context; -import android.content.res.Configuration; import android.media.AudioManager; import android.os.Handler; import android.os.Looper; @@ -10,6 +8,9 @@ import android.util.Log; import android.util.Pair; import android.view.SurfaceHolder; import androidx.annotation.NonNull; +import androidx.car.app.connection.CarConnection; +import androidx.lifecycle.LiveData; +import androidx.lifecycle.Observer; import androidx.media.AudioAttributesCompat; import androidx.media.AudioFocusRequestCompat; import androidx.media.AudioManagerCompat; @@ -58,6 +59,9 @@ public class LocalPSMP extends PlaybackServiceMediaPlayer { private final Handler audioFocusCanceller; private boolean isShutDown = false; private CountDownLatch seekLatch; + private LiveData androidAutoConnectionState; + private boolean androidAutoConnected; + private Observer androidAutoConnectionObserver; public LocalPSMP(@NonNull Context context, @NonNull PlaybackServiceMediaPlayer.PSMPCallback callback) { @@ -71,6 +75,12 @@ public class LocalPSMP extends PlaybackServiceMediaPlayer { mediaType = MediaType.UNKNOWN; videoSize = null; + androidAutoConnectionState = new CarConnection(context).getType(); + androidAutoConnectionObserver = connectionState -> { + androidAutoConnected = connectionState == CarConnection.CONNECTION_TYPE_PROJECTION; + }; + androidAutoConnectionState.observeForever(androidAutoConnectionObserver); + AudioAttributesCompat audioAttributes = new AudioAttributesCompat.Builder() .setUsage(AudioAttributesCompat.USAGE_MEDIA) .setContentType(AudioAttributesCompat.CONTENT_TYPE_SPEECH) @@ -182,8 +192,8 @@ public class LocalPSMP extends PlaybackServiceMediaPlayer { } else { throw new IOException("Unable to read local file " + media.getLocalFileUrl()); } - UiModeManager uiModeManager = (UiModeManager) context.getSystemService(Context.UI_MODE_SERVICE); - if (uiModeManager.getCurrentModeType() != Configuration.UI_MODE_TYPE_CAR) { + + if (!androidAutoConnected) { setPlayerStatus(PlayerStatus.INITIALIZED, media); } @@ -525,6 +535,7 @@ public class LocalPSMP extends PlaybackServiceMediaPlayer { mediaPlayer = null; playerStatus = PlayerStatus.STOPPED; } + androidAutoConnectionState.removeObserver(androidAutoConnectionObserver); isShutDown = true; abandonAudioFocus(); releaseWifiLockIfNecessary();