Do not enable sleep mode in Android Auto (#7053)

When playback is started while an Android Auto projection is active, we want to
prevent automatic sleep timer from starting.

Note: the androidx.car.app library has not seen a full release since 1.2.0. We opted to use a release candidate here, which has a downgraded minSdk requirement, compatible with the current minSdk of AntennaPod at the time this dependency is introduced.
This commit is contained in:
hades 2024-04-17 00:01:34 +02:00 committed by GitHub
parent 2f58b4b360
commit 841bda020f
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 17 additions and 0 deletions

View File

@ -26,6 +26,7 @@ dependencies {
implementation project(':ui:chapters') implementation project(':ui:chapters')
annotationProcessor "androidx.annotation:annotation:$annotationVersion" annotationProcessor "androidx.annotation:annotation:$annotationVersion"
implementation "androidx.car.app:app:1.4.0-rc02"
implementation "androidx.core:core:$coreVersion" implementation "androidx.core:core:$coreVersion"
implementation "androidx.appcompat:appcompat:$appcompatVersion" implementation "androidx.appcompat:appcompat:$appcompatVersion"
implementation "androidx.media:media:$mediaVersion" implementation "androidx.media:media:$mediaVersion"

View File

@ -45,9 +45,12 @@ import androidx.annotation.DrawableRes;
import androidx.annotation.NonNull; import androidx.annotation.NonNull;
import androidx.annotation.Nullable; import androidx.annotation.Nullable;
import androidx.annotation.StringRes; import androidx.annotation.StringRes;
import androidx.car.app.connection.CarConnection;
import androidx.core.app.NotificationCompat; import androidx.core.app.NotificationCompat;
import androidx.core.app.NotificationManagerCompat; import androidx.core.app.NotificationManagerCompat;
import androidx.core.content.ContextCompat; import androidx.core.content.ContextCompat;
import androidx.lifecycle.LiveData;
import androidx.lifecycle.Observer;
import androidx.media.MediaBrowserServiceCompat; import androidx.media.MediaBrowserServiceCompat;
import de.danoeh.antennapod.event.PlayerStatusEvent; import de.danoeh.antennapod.event.PlayerStatusEvent;
@ -170,6 +173,9 @@ public class PlaybackService extends MediaBrowserServiceCompat {
private MediaSessionCompat mediaSession; private MediaSessionCompat mediaSession;
private static volatile MediaType currentMediaType = MediaType.UNKNOWN; private static volatile MediaType currentMediaType = MediaType.UNKNOWN;
private LiveData<Integer> androidAutoConnectionState;
private boolean androidAutoConnected = false;
private Observer<Integer> androidAutoConnectionObserver;
private final IBinder mBinder = new LocalBinder(); private final IBinder mBinder = new LocalBinder();
@ -226,6 +232,11 @@ public class PlaybackService extends MediaBrowserServiceCompat {
stateManager = new PlaybackServiceStateManager(this); stateManager = new PlaybackServiceStateManager(this);
notificationBuilder = new PlaybackServiceNotificationBuilder(this); notificationBuilder = new PlaybackServiceNotificationBuilder(this);
androidAutoConnectionState = new CarConnection(this).getType();
androidAutoConnectionObserver = connectionState -> {
androidAutoConnected = connectionState == CarConnection.CONNECTION_TYPE_PROJECTION;
};
androidAutoConnectionState.observeForever(androidAutoConnectionObserver);
ContextCompat.registerReceiver(this, autoStateUpdated, ContextCompat.registerReceiver(this, autoStateUpdated,
new IntentFilter("com.google.android.gms.car.media.STATUS"), ContextCompat.RECEIVER_EXPORTED); new IntentFilter("com.google.android.gms.car.media.STATUS"), ContextCompat.RECEIVER_EXPORTED);
@ -318,6 +329,7 @@ public class PlaybackService extends MediaBrowserServiceCompat {
currentMediaType = MediaType.UNKNOWN; currentMediaType = MediaType.UNKNOWN;
castStateListener.destroy(); castStateListener.destroy();
androidAutoConnectionState.removeObserver(androidAutoConnectionObserver);
cancelPositionObserver(); cancelPositionObserver();
if (mediaSession != null) { if (mediaSession != null) {
mediaSession.release(); mediaSession.release();
@ -860,6 +872,10 @@ public class PlaybackService extends MediaBrowserServiceCompat {
int currentHour = now.get(Calendar.HOUR_OF_DAY); int currentHour = now.get(Calendar.HOUR_OF_DAY);
autoEnableByTime = SleepTimerPreferences.isInTimeRange(fromSetting, toSetting, currentHour); autoEnableByTime = SleepTimerPreferences.isInTimeRange(fromSetting, toSetting, currentHour);
} }
if (androidAutoConnected) {
Log.i(TAG, "Android Auto is connected, sleep timer will not be auto-enabled");
autoEnableByTime = false;
}
if (newInfo.getOldPlayerStatus() != null && newInfo.getOldPlayerStatus() != PlayerStatus.SEEKING if (newInfo.getOldPlayerStatus() != null && newInfo.getOldPlayerStatus() != PlayerStatus.SEEKING
&& SleepTimerPreferences.autoEnable() && autoEnableByTime && !sleepTimerActive()) { && SleepTimerPreferences.autoEnable() && autoEnableByTime && !sleepTimerActive()) {