Merge pull request #3347 from ByteHamster/remove-notification

Playback service notification improvements
This commit is contained in:
H. Lehmann 2019-08-30 13:09:51 +02:00 committed by GitHub
commit f263ca8cd7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 55 additions and 11 deletions

View File

@ -98,6 +98,7 @@ public class PlaybackService extends MediaBrowserServiceCompat {
*/
public static final String EXTRA_SHOULD_STREAM = "extra.de.danoeh.antennapod.core.service.shouldStream";
public static final String EXTRA_ALLOW_STREAM_THIS_TIME = "extra.de.danoeh.antennapod.core.service.allowStream";
public static final String EXTRA_ALLOW_STREAM_ALWAYS = "extra.de.danoeh.antennapod.core.service.allowStreamAlways";
/**
* True if playback should be started immediately after media has been
* prepared.
@ -326,7 +327,7 @@ public class PlaybackService extends MediaBrowserServiceCompat {
public void onDestroy() {
super.onDestroy();
Log.d(TAG, "Service is about to be destroyed");
stateManager.stopForeground(true);
stateManager.stopForeground(!UserPreferences.isPersistNotify());
isRunning = false;
currentMediaType = MediaType.UNKNOWN;
@ -444,9 +445,18 @@ public class PlaybackService extends MediaBrowserServiceCompat {
if (!stateManager.isInForeground()) {
PlaybackServiceNotificationBuilder notificationBuilder = new PlaybackServiceNotificationBuilder(this);
if (mediaPlayer != null && getPlayable() != null) {
notificationBuilder.addMetadata(getPlayable(), mediaSession.getSessionToken(), getStatus(), isCasting);
if (notificationBuilder.isIconCached(getPlayable())) {
notificationBuilder.loadIcon(getPlayable());
}
}
startForeground(NOTIFICATION_ID, notificationBuilder.build());
}
NotificationManagerCompat notificationManager = NotificationManagerCompat.from(this);
notificationManager.cancel(NOTIFICATION_ID_STREAMING);
final int keycode = intent.getIntExtra(MediaButtonReceiver.EXTRA_KEYCODE, -1);
final boolean castDisconnect = intent.getBooleanExtra(EXTRA_CAST_DISCONNECT, false);
Playable playable = intent.getParcelableExtra(EXTRA_PLAYABLE);
@ -471,6 +481,7 @@ public class PlaybackService extends MediaBrowserServiceCompat {
stateManager.validStartCommandWasReceived();
boolean stream = intent.getBooleanExtra(EXTRA_SHOULD_STREAM, true);
boolean allowStreamThisTime = intent.getBooleanExtra(EXTRA_ALLOW_STREAM_THIS_TIME, false);
boolean allowStreamAlways = intent.getBooleanExtra(EXTRA_ALLOW_STREAM_ALWAYS, false);
boolean startWhenPrepared = intent.getBooleanExtra(EXTRA_START_WHEN_PREPARED, false);
boolean prepareImmediately = intent.getBooleanExtra(EXTRA_PREPARE_IMMEDIATELY, false);
sendNotificationBroadcast(NOTIFICATION_TYPE_RELOAD, 0);
@ -479,6 +490,9 @@ public class PlaybackService extends MediaBrowserServiceCompat {
if (playable instanceof FeedMedia) {
playable = DBReader.getFeedMedia(((FeedMedia) playable).getId());
}
if (allowStreamAlways) {
UserPreferences.setAllowMobileStreaming(true);
}
if (stream && !NetworkUtils.isStreamingAllowed() && !allowStreamThisTime) {
displayStreamingNotAllowedNotification(intent);
writePlaybackPreferencesNoMediaPlaying();
@ -496,23 +510,38 @@ public class PlaybackService extends MediaBrowserServiceCompat {
}
private void displayStreamingNotAllowedNotification(Intent originalIntent) {
Intent intent = new Intent(originalIntent);
intent.putExtra(EXTRA_ALLOW_STREAM_THIS_TIME, true);
PendingIntent pendingIntent;
Intent intentAllowThisTime = new Intent(originalIntent);
intentAllowThisTime.putExtra(EXTRA_ALLOW_STREAM_THIS_TIME, true);
PendingIntent pendingIntentAllowThisTime;
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
pendingIntent = PendingIntent.getForegroundService(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
pendingIntentAllowThisTime = PendingIntent.getForegroundService(this, 0, intentAllowThisTime, PendingIntent.FLAG_UPDATE_CURRENT);
} else {
pendingIntent = PendingIntent.getService(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
pendingIntentAllowThisTime = PendingIntent.getService(this, 0, intentAllowThisTime, PendingIntent.FLAG_UPDATE_CURRENT);
}
Intent intentAlwaysAllow = new Intent(intentAllowThisTime);
intentAlwaysAllow.putExtra(EXTRA_ALLOW_STREAM_ALWAYS, true);
PendingIntent pendingIntentAlwaysAllow;
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
pendingIntentAlwaysAllow = PendingIntent.getForegroundService(this, 0, intentAlwaysAllow, PendingIntent.FLAG_UPDATE_CURRENT);
} else {
pendingIntentAlwaysAllow = PendingIntent.getService(this, 0, intentAlwaysAllow, PendingIntent.FLAG_UPDATE_CURRENT);
}
NotificationCompat.Builder builder = new NotificationCompat.Builder(this, NotificationUtils.CHANNEL_ID_USER_ACTION)
.setSmallIcon(R.drawable.stat_notify_sync_error)
.setSmallIcon(R.drawable.ic_stream_white)
.setContentTitle(getString(R.string.confirm_mobile_streaming_notification_title))
.setContentText(getString(R.string.confirm_mobile_streaming_notification_message))
.setStyle(new NotificationCompat.BigTextStyle()
.bigText(getString(R.string.confirm_mobile_streaming_notification_message)))
.setPriority(NotificationCompat.PRIORITY_DEFAULT)
.setContentIntent(pendingIntent)
.setContentIntent(pendingIntentAllowThisTime)
.addAction(R.drawable.ic_stream_white,
getString(R.string.stream_label),
pendingIntentAllowThisTime)
.addAction(R.drawable.ic_stream_white,
getString(R.string.confirm_mobile_streaming_button_always),
pendingIntentAlwaysAllow)
.setAutoCancel(true);
NotificationManagerCompat notificationManager = NotificationManagerCompat.from(this);
notificationManager.notify(NOTIFICATION_ID_STREAMING, builder.build());
@ -733,7 +762,7 @@ public class PlaybackService extends MediaBrowserServiceCompat {
@Override
public void shouldStop() {
stateManager.stopService();
setupNotification(getPlayable()); // Stops foreground if not playing
}
@Override

View File

@ -1,6 +1,8 @@
package de.danoeh.antennapod.core.service.playback;
import android.app.Notification;
import android.app.Service;
import android.os.Build;
class PlaybackServiceStateManager {
private final PlaybackService playbackService;
@ -19,12 +21,19 @@ class PlaybackServiceStateManager {
void stopService() {
stopForeground(true);
isInForeground = false;
playbackService.stopSelf();
}
void stopForeground(boolean removeNotification) {
playbackService.stopForeground(removeNotification);
if (isInForeground) {
if (Build.VERSION.SDK_INT < 24) {
playbackService.stopForeground(removeNotification);
} else if (removeNotification) {
playbackService.stopForeground(Service.STOP_FOREGROUND_REMOVE);
} else {
playbackService.stopForeground(Service.STOP_FOREGROUND_DETACH);
}
}
isInForeground = false;
hasReceivedValidStartCommand = false;
}

View File

@ -0,0 +1,5 @@
<vector android:height="24dp"
android:viewportHeight="24.0" android:viewportWidth="24.0"
android:width="24dp" xmlns:android="http://schemas.android.com/apk/res/android">
<path android:fillColor="#FFFFFFFF" android:pathData="M12,5c-3.87,0 -7,3.13 -7,7h2c0,-2.76 2.24,-5 5,-5s5,2.24 5,5h2c0,-3.87 -3.13,-7 -7,-7zM13,14.29c0.88,-0.39 1.5,-1.26 1.5,-2.29 0,-1.38 -1.12,-2.5 -2.5,-2.5S9.5,10.62 9.5,12c0,1.02 0.62,1.9 1.5,2.29v3.3L7.59,21 9,22.41l3,-3 3,3L16.41,21 13,17.59v-3.3zM12,1C5.93,1 1,5.93 1,12h2c0,-4.97 4.03,-9 9,-9s9,4.03 9,9h2c0,-6.07 -4.93,-11 -11,-11z"/>
</vector>

View File

@ -267,6 +267,7 @@
<string name="confirm_mobile_download_dialog_message">Downloading over mobile data connection is disabled in the settings.\n\nDo you want to allow downloading temporarily?\n\n<small>Your choice will be remembered for 10 minutes.</small></string>
<string name="confirm_mobile_streaming_notification_title">Confirm Mobile streaming</string>
<string name="confirm_mobile_streaming_notification_message">Streaming over mobile data connection is disabled in the settings. Tap to stream anyway.</string>
<string name="confirm_mobile_streaming_button_always">Always allow</string>
<string name="confirm_mobile_download_dialog_only_add_to_queue">Enqueue</string>
<string name="confirm_mobile_download_dialog_enable_temporarily">Allow temporarily</string>