New way to detect car mode to fix skip to next episode (#7442)

This commit is contained in:
Tony Tam 2024-11-01 22:58:55 -10:00 committed by GitHub
parent a0d9f2aa7e
commit 51f92e94eb
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 15 additions and 4 deletions

View File

@ -1,8 +1,6 @@
package de.danoeh.antennapod.playback.service.internal; package de.danoeh.antennapod.playback.service.internal;
import android.app.UiModeManager;
import android.content.Context; import android.content.Context;
import android.content.res.Configuration;
import android.media.AudioManager; import android.media.AudioManager;
import android.os.Handler; import android.os.Handler;
import android.os.Looper; import android.os.Looper;
@ -10,6 +8,9 @@ import android.util.Log;
import android.util.Pair; import android.util.Pair;
import android.view.SurfaceHolder; import android.view.SurfaceHolder;
import androidx.annotation.NonNull; 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.AudioAttributesCompat;
import androidx.media.AudioFocusRequestCompat; import androidx.media.AudioFocusRequestCompat;
import androidx.media.AudioManagerCompat; import androidx.media.AudioManagerCompat;
@ -58,6 +59,9 @@ public class LocalPSMP extends PlaybackServiceMediaPlayer {
private final Handler audioFocusCanceller; private final Handler audioFocusCanceller;
private boolean isShutDown = false; private boolean isShutDown = false;
private CountDownLatch seekLatch; private CountDownLatch seekLatch;
private LiveData<Integer> androidAutoConnectionState;
private boolean androidAutoConnected;
private Observer<Integer> androidAutoConnectionObserver;
public LocalPSMP(@NonNull Context context, public LocalPSMP(@NonNull Context context,
@NonNull PlaybackServiceMediaPlayer.PSMPCallback callback) { @NonNull PlaybackServiceMediaPlayer.PSMPCallback callback) {
@ -71,6 +75,12 @@ public class LocalPSMP extends PlaybackServiceMediaPlayer {
mediaType = MediaType.UNKNOWN; mediaType = MediaType.UNKNOWN;
videoSize = null; videoSize = null;
androidAutoConnectionState = new CarConnection(context).getType();
androidAutoConnectionObserver = connectionState -> {
androidAutoConnected = connectionState == CarConnection.CONNECTION_TYPE_PROJECTION;
};
androidAutoConnectionState.observeForever(androidAutoConnectionObserver);
AudioAttributesCompat audioAttributes = new AudioAttributesCompat.Builder() AudioAttributesCompat audioAttributes = new AudioAttributesCompat.Builder()
.setUsage(AudioAttributesCompat.USAGE_MEDIA) .setUsage(AudioAttributesCompat.USAGE_MEDIA)
.setContentType(AudioAttributesCompat.CONTENT_TYPE_SPEECH) .setContentType(AudioAttributesCompat.CONTENT_TYPE_SPEECH)
@ -182,8 +192,8 @@ public class LocalPSMP extends PlaybackServiceMediaPlayer {
} else { } else {
throw new IOException("Unable to read local file " + media.getLocalFileUrl()); 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); setPlayerStatus(PlayerStatus.INITIALIZED, media);
} }
@ -525,6 +535,7 @@ public class LocalPSMP extends PlaybackServiceMediaPlayer {
mediaPlayer = null; mediaPlayer = null;
playerStatus = PlayerStatus.STOPPED; playerStatus = PlayerStatus.STOPPED;
} }
androidAutoConnectionState.removeObserver(androidAutoConnectionObserver);
isShutDown = true; isShutDown = true;
abandonAudioFocus(); abandonAudioFocus();
releaseWifiLockIfNecessary(); releaseWifiLockIfNecessary();