Merge pull request #3292 from ByteHamster/mobile-updates-setting

Mobile updates setting
This commit is contained in:
H. Lehmann 2019-08-10 18:37:56 +02:00 committed by GitHub
commit 867a2812e8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
15 changed files with 173 additions and 44 deletions

View File

@ -49,7 +49,7 @@ class AdapterUtils {
} else if (!media.isDownloaded()) {
if (media.getSize() > 0) {
txtvPos.setText(Converter.byteToString(media.getSize()));
} else if(NetworkUtils.isDownloadAllowed() && !media.checkedOnSizeButUnknown()) {
} else if(NetworkUtils.isEpisodeHeadDownloadAllowed() && !media.checkedOnSizeButUnknown()) {
txtvPos.setText("{fa-spinner}");
Iconify.addIcons(txtvPos);
NetworkUtils.getFeedMediaSizeObservable(media)

View File

@ -130,7 +130,7 @@ public class AllEpisodesRecycleAdapter extends RecyclerView.Adapter<AllEpisodesR
holder.txtvDuration.setText(Converter.getDurationStringLong(media.getDuration()));
} else if (media.getSize() > 0) {
holder.txtvDuration.setText(Converter.byteToString(media.getSize()));
} else if(NetworkUtils.isDownloadAllowed() && !media.checkedOnSizeButUnknown()) {
} else if(NetworkUtils.isEpisodeHeadDownloadAllowed() && !media.checkedOnSizeButUnknown()) {
holder.txtvDuration.setText("{fa-spinner}");
Iconify.addIcons(holder.txtvDuration);
NetworkUtils.getFeedMediaSizeObservable(media)

View File

@ -252,7 +252,7 @@ public class QueueRecyclerAdapter extends RecyclerView.Adapter<QueueRecyclerAdap
} else {
if(media.getSize() > 0) {
progressLeft.setText(Converter.byteToString(media.getSize()));
} else if(NetworkUtils.isDownloadAllowed() && !media.checkedOnSizeButUnknown()) {
} else if(NetworkUtils.isEpisodeHeadDownloadAllowed() && !media.checkedOnSizeButUnknown()) {
progressLeft.setText("{fa-spinner}");
Iconify.addIcons(progressLeft);
NetworkUtils.getFeedMediaSizeObservable(media)

View File

@ -43,7 +43,7 @@ class DownloadActionButton extends ItemActionButton {
return;
}
if (NetworkUtils.isDownloadAllowed() || MobileDownloadHelper.userAllowedMobileDownloads()) {
if (NetworkUtils.isEpisodeDownloadAllowed() || MobileDownloadHelper.userAllowedMobileDownloads()) {
downloadEpisode(context);
} else if (MobileDownloadHelper.userChoseAddToQueue() && !isInQueue) {
addEpisodeToQueue(context);

View File

@ -66,7 +66,6 @@ public class AutoDownloadPreferencesFragment extends PreferenceFragmentCompat {
findPreference(UserPreferences.PREF_ENABLE_AUTODL_ON_BATTERY).setEnabled(autoDownload);
findPreference(UserPreferences.PREF_ENABLE_AUTODL_WIFI_FILTER).setEnabled(autoDownload);
findPreference(UserPreferences.PREF_EPISODE_CLEANUP).setEnabled(autoDownload);
findPreference(UserPreferences.PREF_ENABLE_AUTODL_ON_MOBILE).setEnabled(autoDownload);
setSelectedNetworksEnabled(autoDownload && UserPreferences.isEnableAutodownloadWifiFilter());
}

View File

@ -37,7 +37,7 @@ public class PreferenceUpgrader {
}
if (oldVersion < 1070197) {
if (prefs.getBoolean("prefMobileUpdate", false)) {
prefs.edit().putString(UserPreferences.PREF_MOBILE_UPDATE, "everything").apply();
prefs.edit().putString("prefMobileUpdateAllowed", "everything").apply();
}
}
if (oldVersion < 1070300) {
@ -47,6 +47,23 @@ public class PreferenceUpgrader {
prefs.edit().putString(UserPreferences.PREF_MEDIA_PLAYER,
UserPreferences.PREF_MEDIA_PLAYER_EXOPLAYER).apply();
}
if (prefs.getBoolean("prefEnableAutoDownloadOnMobile", false)) {
UserPreferences.setAllowMobileAutoDownload(true);
}
switch (prefs.getString("prefMobileUpdateAllowed", "images")) {
case "everything":
UserPreferences.setAllowMobileFeedRefresh(true);
UserPreferences.setAllowMobileEpisodeDownload(true);
UserPreferences.setAllowMobileImages(true);
break;
case "images":
UserPreferences.setAllowMobileImages(true);
break;
case "nothing":
UserPreferences.setAllowMobileImages(false);
break;
}
}
}
}

View File

@ -30,11 +30,6 @@
android:title="@string/pref_automatic_download_on_battery_title"
android:summary="@string/pref_automatic_download_on_battery_sum"
android:defaultValue="true"/>
<SwitchPreference
android:key="prefEnableAutoDownloadOnMobile"
android:title="@string/pref_autodl_allow_on_mobile_title"
android:summary="@string/pref_autodl_allow_on_mobile_sum"
android:defaultValue="false"/>
<SwitchPreference
android:key="prefEnableAutoDownloadWifiFilter"
android:title="@string/pref_autodl_wifi_filter_title"

View File

@ -16,11 +16,11 @@
</PreferenceCategory>
<PreferenceCategory android:title="@string/download_pref_details">
<ListPreference
android:defaultValue="images"
<MultiSelectListPreference
android:defaultValue="@array/mobile_update_default_value"
android:entries="@array/mobile_update_entries"
android:entryValues="@array/mobile_update_values"
android:key="prefMobileUpdateAllowed"
android:key="prefMobileUpdateTypes"
android:summary="@string/pref_mobileUpdate_sum"
android:title="@string/pref_mobileUpdate_title"/>
<de.danoeh.antennapod.preferences.NumberPickerPreference

View File

@ -17,7 +17,9 @@ import java.io.IOException;
import java.net.Proxy;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.concurrent.TimeUnit;
import de.danoeh.antennapod.core.R;
@ -78,14 +80,13 @@ public class UserPreferences {
// Network
private static final String PREF_ENQUEUE_DOWNLOADED = "prefEnqueueDownloaded";
public static final String PREF_UPDATE_INTERVAL = "prefAutoUpdateIntervall";
public static final String PREF_MOBILE_UPDATE = "prefMobileUpdateAllowed";
private static final String PREF_MOBILE_UPDATE = "prefMobileUpdateTypes";
public static final String PREF_EPISODE_CLEANUP = "prefEpisodeCleanup";
public static final String PREF_PARALLEL_DOWNLOADS = "prefParallelDownloads";
public static final String PREF_EPISODE_CACHE_SIZE = "prefEpisodeCacheSize";
public static final String PREF_ENABLE_AUTODL = "prefEnableAutoDl";
public static final String PREF_ENABLE_AUTODL_ON_BATTERY = "prefEnableAutoDownloadOnBattery";
public static final String PREF_ENABLE_AUTODL_WIFI_FILTER = "prefEnableAutoDownloadWifiFilter";
public static final String PREF_ENABLE_AUTODL_ON_MOBILE = "prefEnableAutoDownloadOnMobile";
private static final String PREF_AUTODL_SELECTED_NETWORKS = "prefAutodownloadSelectedNetworks";
private static final String PREF_PROXY_TYPE = "prefProxyType";
private static final String PREF_PROXY_HOST = "prefProxyHost";
@ -379,16 +380,63 @@ public class UserPreferences {
return prefs.getString(PREF_UPDATE_INTERVAL, "").equals("0");
}
public static String getMobileUpdatesEnabled() {
return prefs.getString(PREF_MOBILE_UPDATE, "images");
private static boolean isAllowMobileFor(String type) {
HashSet<String> defaultValue = new HashSet<>();
defaultValue.add("images");
Set<String> allowed = prefs.getStringSet(PREF_MOBILE_UPDATE, defaultValue);
return allowed.contains(type);
}
public static boolean isAllowMobileUpdate() {
return getMobileUpdatesEnabled().equals("everything");
public static boolean isAllowMobileFeedRefresh() {
return isAllowMobileFor("feed_refresh");
}
public static boolean isAllowMobileEpisodeDownload() {
return isAllowMobileFor("episode_download");
}
public static boolean isAllowMobileAutoDownload() {
return isAllowMobileFor("auto_download");
}
public static boolean isAllowMobileStreaming() {
return isAllowMobileFor("streaming");
}
public static boolean isAllowMobileImages() {
return isAllowMobileUpdate() || getMobileUpdatesEnabled().equals("images");
return isAllowMobileFor("images");
}
private static void setAllowMobileFor(String type, boolean allow) {
HashSet<String> defaultValue = new HashSet<>();
defaultValue.add("images");
Set<String> allowed = prefs.getStringSet(PREF_MOBILE_UPDATE, defaultValue);
if (allow) {
allowed.remove(type);
} else {
allowed.add(type);
}
prefs.edit().putStringSet(PREF_MOBILE_UPDATE, allowed).apply();
}
public static void setAllowMobileFeedRefresh(boolean allow) {
setAllowMobileFor("feed_refresh", allow);
}
public static void setAllowMobileEpisodeDownload(boolean allow) {
setAllowMobileFor("episode_download", allow);
}
public static void setAllowMobileAutoDownload(boolean allow) {
setAllowMobileFor("auto_download", allow);
}
public static void setAllowMobileStreaming(boolean allow) {
setAllowMobileFor("streaming", allow);
}
public static void setAllowMobileImages(boolean allow) {
setAllowMobileFor("images", allow);
}
public static int getParallelDownloads() {
@ -420,11 +468,6 @@ public class UserPreferences {
return prefs.getBoolean(PREF_ENABLE_AUTODL_WIFI_FILTER, false);
}
public static boolean isEnableAutodownloadOnMobile() {
return prefs.getBoolean(PREF_ENABLE_AUTODL_ON_MOBILE, false);
}
public static int getImageCacheSize() {
String cacheSizeString = prefs.getString(PREF_IMAGE_CACHE_SIZE, IMAGE_CACHE_DEFAULT_VALUE);
int cacheSizeInt = Integer.parseInt(cacheSizeString);

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,10 +68,12 @@ 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;
import de.danoeh.antennapod.core.util.playback.Playable;
import de.danoeh.antennapod.core.util.playback.PlaybackServiceStarter;
import org.greenrobot.eventbus.EventBus;
/**
@ -94,6 +97,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 +107,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 +209,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 +471,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 +481,12 @@ public class PlaybackService extends MediaBrowserServiceCompat {
if (playable instanceof FeedMedia) {
playable = DBReader.getFeedMedia(((FeedMedia) playable).getId());
}
if (stream && !NetworkUtils.isStreamingAllowed() && !allowStreamThisTime) {
displayStreamingNotAllowedNotification(intent);
writePlaybackPreferencesNoMediaPlaying();
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 +497,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, PendingIntent.FLAG_UPDATE_CURRENT);
} else {
pendingIntent = PendingIntent.getService(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
}
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
@ -824,7 +857,23 @@ public class PlaybackService extends MediaBrowserServiceCompat {
Log.e(TAG, "Error handling the queue in order to retrieve the next item", e);
return null;
}
return (nextItem != null) ? nextItem.getMedia() : null;
if (nextItem == null || nextItem.getMedia() == null) {
return null;
}
if (!nextItem.getMedia().localFileAvailable() && !NetworkUtils.isStreamingAllowed()) {
displayStreamingNotAllowedNotification(
new PlaybackServiceStarter(this, nextItem.getMedia())
.prepareImmediately(true)
.startWhenPrepared(true)
.shouldStream(true)
.getIntent());
writePlaybackPreferencesNoMediaPlaying();
stopService();
return null;
}
return nextItem.getMedia();
}

View File

@ -24,7 +24,7 @@ public class FeedUpdateUtils {
with().pollInterval(1, TimeUnit.SECONDS)
.await()
.atMost(10, TimeUnit.SECONDS)
.until(() -> NetworkUtils.networkAvailable() && NetworkUtils.isDownloadAllowed());
.until(() -> NetworkUtils.networkAvailable() && NetworkUtils.isFeedRefreshAllowed());
DBTasks.refreshAllFeeds(context, null, callback);
} catch (ConditionTimeoutException ignore) {
Log.d(TAG, "Blocking automatic update: no wifi available / no mobile updates allowed");

View File

@ -74,7 +74,7 @@ public class NetworkUtils {
return true;
}
} else {
if (!UserPreferences.isEnableAutodownloadOnMobile()) {
if (!UserPreferences.isAllowMobileAutoDownload()) {
Log.d(TAG, "Auto Download not enabled on Mobile");
return false;
}
@ -95,14 +95,28 @@ public class NetworkUtils {
return info != null && info.isConnected();
}
public static boolean isDownloadAllowed() {
return UserPreferences.isAllowMobileUpdate() || !NetworkUtils.isNetworkMetered();
public static boolean isEpisodeDownloadAllowed() {
return UserPreferences.isAllowMobileEpisodeDownload() || !NetworkUtils.isNetworkMetered();
}
public static boolean isEpisodeHeadDownloadAllowed() {
// It is not an image but it is a similarly tiny request
// that is probably not even considered a download by most users
return isImageAllowed();
}
public static boolean isImageAllowed() {
return UserPreferences.isAllowMobileImages() || !NetworkUtils.isNetworkMetered();
}
public static boolean isStreamingAllowed() {
return UserPreferences.isAllowMobileStreaming() || !NetworkUtils.isNetworkMetered();
}
public static boolean isFeedRefreshAllowed() {
return UserPreferences.isAllowMobileFeedRefresh() || !NetworkUtils.isNetworkMetered();
}
private static boolean isNetworkMetered() {
ConnectivityManager connManager = (ConnectivityManager) context
.getSystemService(Context.CONNECTIVITY_SERVICE);
@ -123,7 +137,7 @@ public class NetworkUtils {
public static Single<Long> getFeedMediaSizeObservable(FeedMedia media) {
return Single.create((SingleOnSubscribe<Long>) emitter -> {
if (!NetworkUtils.isDownloadAllowed()) {
if (!NetworkUtils.isEpisodeHeadDownloadAllowed()) {
emitter.onSuccess(0L);
return;
}

View File

@ -67,7 +67,7 @@ public class AutoUpdateManager {
private static Constraints getConstraints() {
Constraints.Builder constraints = new Constraints.Builder();
if (UserPreferences.isAllowMobileUpdate()) {
if (UserPreferences.isAllowMobileFeedRefresh()) {
constraints.setRequiredNetworkType(NetworkType.CONNECTED);
} else {
constraints.setRequiredNetworkType(NetworkType.UNMETERED);

View File

@ -64,15 +64,23 @@
</string-array>
<string-array name="mobile_update_entries">
<item>@string/pref_mobileUpdate_nothing</item>
<item>@string/pref_mobileUpdate_refresh</item>
<item>@string/pref_mobileUpdate_episode_download</item>
<item>@string/pref_mobileUpdate_auto_download</item>
<item>@string/pref_mobileUpdate_streaming</item>
<item>@string/pref_mobileUpdate_images</item>
<item>@string/pref_mobileUpdate_everything</item>
</string-array>
<string-array name="mobile_update_values">
<item>nothing</item>
<item>feed_refresh</item>
<item>episode_download</item>
<item>auto_download</item>
<item>streaming</item>
<item>images</item>
</string-array>
<string-array name="mobile_update_default_value">
<item>images</item>
<item>everything</item>
</string-array>
<string-array name="episode_cleanup_entries">

View File

@ -265,6 +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_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>
@ -386,10 +388,12 @@
<string name="pref_unpauseOnHeadsetReconnect_title">Headphones Reconnect</string>
<string name="pref_unpauseOnBluetoothReconnect_title">Bluetooth Reconnect</string>
<string name="pref_mobileUpdate_title">Mobile Updates</string>
<string name="pref_mobileUpdate_sum">Allow updates and manual episode downloads over the mobile data connection</string>
<string name="pref_mobileUpdate_nothing">Nothing</string>
<string name="pref_mobileUpdate_images">Images only</string>
<string name="pref_mobileUpdate_everything">Everything</string>
<string name="pref_mobileUpdate_sum">Select what should be allowed over the mobile data connection</string>
<string name="pref_mobileUpdate_refresh">Feed refresh</string>
<string name="pref_mobileUpdate_images">Cover images</string>
<string name="pref_mobileUpdate_auto_download">Auto download</string>
<string name="pref_mobileUpdate_episode_download">Episode download</string>
<string name="pref_mobileUpdate_streaming">Streaming</string>
<string name="refreshing_label">Refreshing</string>
<string name="user_interface_label">User Interface</string>
<string name="pref_set_theme_title">Select Theme</string>