Display notification when trying to stream

This commit is contained in:
ByteHamster 2019-07-25 01:08:16 +02:00
parent be42cc0c71
commit be8e7c32f1
7 changed files with 44 additions and 30 deletions

View File

@ -13,7 +13,7 @@ import de.danoeh.antennapod.core.storage.DBTasks;
import de.danoeh.antennapod.core.storage.DBWriter;
import de.danoeh.antennapod.core.storage.DownloadRequestException;
public class MobileDownloadHelper {
class MobileDownloadHelper {
private static long addToQueueTimestamp;
private static long allowMobileDownloadTimestamp;
private static final int TEN_MINUTES_IN_MILLIS = 10 * 60 * 1000;
@ -41,16 +41,6 @@ public class MobileDownloadHelper {
builder.show();
}
public static void confirmMobileStreaming(final Context context, MaterialDialog.SingleButtonCallback onAllowed) {
MaterialDialog.Builder builder = new MaterialDialog.Builder(context)
.title(R.string.confirm_mobile_streaming_dialog_title)
.content(R.string.confirm_mobile_streaming_dialog_message)
.positiveText(R.string.yes)
.onPositive(onAllowed)
.negativeText(R.string.no);
builder.show();
}
private static void addToQueue(Context context, FeedItem item) {
addToQueueTimestamp = System.currentTimeMillis();
DBWriter.addQueueItem(context, item);

View File

@ -35,7 +35,6 @@ import com.bumptech.glide.request.RequestOptions;
import com.joanzapata.iconify.Iconify;
import com.joanzapata.iconify.widget.IconButton;
import de.danoeh.antennapod.adapter.actionbutton.MobileDownloadHelper;
import org.apache.commons.lang3.ArrayUtils;
import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
@ -250,15 +249,8 @@ public class ItemFragment extends Fragment implements OnSwipeGesture {
if (item.hasMedia()) {
FeedMedia media = item.getMedia();
if (!media.isDownloaded()) { // Stream
if (NetworkUtils.isStreamingAllowed()) {
DBTasks.playMedia(getActivity(), media, true, true, true);
((MainActivity) getActivity()).dismissChildFragment();
} else {
MobileDownloadHelper.confirmMobileStreaming(getContext(), (dialog, which) -> {
DBTasks.playMedia(getActivity(), media, true, true, true);
((MainActivity) getActivity()).dismissChildFragment();
});
}
DBTasks.playMedia(getActivity(), media, true, true, true);
((MainActivity) getActivity()).dismissChildFragment();
} else {
DBWriter.deleteFeedMediaOfItem(getActivity(), media.getId());
}

View File

@ -2,14 +2,11 @@ package de.danoeh.antennapod.preferences;
import android.content.Context;
import android.content.SharedPreferences;
import android.preference.MultiSelectListPreference;
import android.preference.PreferenceManager;
import de.danoeh.antennapod.BuildConfig;
import de.danoeh.antennapod.core.preferences.UserPreferences;
import de.danoeh.antennapod.core.util.gui.NotificationUtils;
import java.util.HashSet;
public class PreferenceUpgrader {
private static final String PREF_CONFIGURED_VERSION = "configuredVersion";
private static final String PREF_NAME = "PreferenceUpgrader";

View File

@ -17,7 +17,7 @@
<PreferenceCategory android:title="@string/download_pref_details">
<MultiSelectListPreference
android:defaultValue="images"
android:defaultValue="@array/mobile_update_default_value"
android:entries="@array/mobile_update_entries"
android:entryValues="@array/mobile_update_values"
android:key="prefMobileUpdateTypes"

View File

@ -25,6 +25,7 @@ import android.preference.PreferenceManager;
import android.support.annotation.NonNull;
import android.support.annotation.StringRes;
import android.support.v4.app.NotificationCompat;
import android.support.v4.app.NotificationManagerCompat;
import android.support.v4.media.MediaBrowserCompat;
import android.support.v4.media.MediaBrowserServiceCompat;
import android.support.v4.media.MediaDescriptionCompat;
@ -67,6 +68,7 @@ import de.danoeh.antennapod.core.storage.DBWriter;
import de.danoeh.antennapod.core.storage.FeedSearcher;
import de.danoeh.antennapod.core.util.IntList;
import de.danoeh.antennapod.core.util.IntentUtils;
import de.danoeh.antennapod.core.util.NetworkUtils;
import de.danoeh.antennapod.core.util.QueueAccess;
import de.danoeh.antennapod.core.util.gui.NotificationUtils;
import de.danoeh.antennapod.core.util.playback.ExternalMedia;
@ -94,6 +96,7 @@ public class PlaybackService extends MediaBrowserServiceCompat {
* True if media should be streamed.
*/
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";
/**
* True if playback should be started immediately after media has been
* prepared.
@ -103,7 +106,6 @@ public class PlaybackService extends MediaBrowserServiceCompat {
public static final String EXTRA_PREPARE_IMMEDIATELY = "extra.de.danoeh.antennapod.core.service.prepareImmediately";
public static final String ACTION_PLAYER_STATUS_CHANGED = "action.de.danoeh.antennapod.core.service.playerStatusChanged";
public static final String EXTRA_NEW_PLAYER_STATUS = "extra.de.danoeh.antennapod.service.playerStatusChanged.newStatus";
private static final String AVRCP_ACTION_PLAYER_STATUS_CHANGED = "com.android.music.playstatechanged";
private static final String AVRCP_ACTION_META_CHANGED = "com.android.music.metachanged";
@ -206,6 +208,7 @@ public class PlaybackService extends MediaBrowserServiceCompat {
private static volatile boolean isCasting = false;
private static final int NOTIFICATION_ID = 1;
private static final int NOTIFICATION_ID_STREAMING = 2;
private PlaybackServiceMediaPlayer mediaPlayer;
private PlaybackServiceTaskManager taskManager;
@ -467,8 +470,8 @@ public class PlaybackService extends MediaBrowserServiceCompat {
}
} else if (!flavorHelper.castDisconnect(castDisconnect) && playable != null) {
started = true;
boolean stream = intent.getBooleanExtra(EXTRA_SHOULD_STREAM,
true);
boolean stream = intent.getBooleanExtra(EXTRA_SHOULD_STREAM, true);
boolean allowStreamThisTime = intent.getBooleanExtra(EXTRA_ALLOW_STREAM_THIS_TIME, false);
boolean startWhenPrepared = intent.getBooleanExtra(EXTRA_START_WHEN_PREPARED, false);
boolean prepareImmediately = intent.getBooleanExtra(EXTRA_PREPARE_IMMEDIATELY, false);
sendNotificationBroadcast(NOTIFICATION_TYPE_RELOAD, 0);
@ -477,6 +480,11 @@ public class PlaybackService extends MediaBrowserServiceCompat {
if (playable instanceof FeedMedia) {
playable = DBReader.getFeedMedia(((FeedMedia) playable).getId());
}
if (stream && !NetworkUtils.isStreamingAllowed() && !allowStreamThisTime) {
displayStreamingNotAllowedNotification(intent);
stopService();
return Service.START_NOT_STICKY;
}
mediaPlayer.playMediaObject(playable, stream, startWhenPrepared, prepareImmediately);
} else {
Log.d(TAG, "Did not handle intent to PlaybackService: " + intent);
@ -487,6 +495,29 @@ public class PlaybackService extends MediaBrowserServiceCompat {
return Service.START_NOT_STICKY;
}
private void displayStreamingNotAllowedNotification(Intent originalIntent) {
Intent intent = new Intent(originalIntent);
intent.putExtra(EXTRA_ALLOW_STREAM_THIS_TIME, true);
PendingIntent pendingIntent;
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
pendingIntent = PendingIntent.getForegroundService(this, 0, intent, 0);
} else {
pendingIntent = PendingIntent.getService(this, 0, intent, 0);
}
NotificationCompat.Builder builder = new NotificationCompat.Builder(this, NotificationUtils.CHANNEL_ID_USER_ACTION)
.setSmallIcon(R.drawable.stat_notify_sync_error)
.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)
.setAutoCancel(true);
NotificationManagerCompat notificationManager = NotificationManagerCompat.from(this);
notificationManager.notify(NOTIFICATION_ID_STREAMING, builder.build());
}
/**
* Handles media button events
* return: keycode was handled

View File

@ -77,6 +77,10 @@
<item>images</item>
</string-array>
<string-array name="mobile_update_default_value">
<item>images</item>
</string-array>
<string-array name="episode_cleanup_entries">
<item>@string/episode_cleanup_queue_removal</item>
<item>0</item>

View File

@ -265,8 +265,8 @@
<string name="confirm_mobile_download_dialog_title">Confirm Mobile Download</string>
<string name="confirm_mobile_download_dialog_message_not_in_queue">Downloading over mobile data connection is disabled in the settings.\n\nYou can choose to either only add the episode to the queue or you can allow downloading temporarily.\n\n<small>Your choice will be remembered for 10 minutes.</small></string>
<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_dialog_title">Confirm Mobile streaming</string>
<string name="confirm_mobile_streaming_dialog_message">Streaming over mobile data connection is disabled in the settings. Do you want to stream anyway?</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_download_dialog_only_add_to_queue">Enqueue</string>
<string name="confirm_mobile_download_dialog_enable_temporarily">Allow temporarily</string>