Changed from db backed item speed to preferences backed global value

This commit is contained in:
Jonas Kalderstam 2019-10-05 02:15:26 +02:00
parent 97aa360611
commit 6a37d33b97
13 changed files with 95 additions and 100 deletions

View File

@ -11,8 +11,9 @@ import java.text.DecimalFormatSymbols;
import java.util.Locale;
import java.util.concurrent.atomic.AtomicBoolean;
import de.danoeh.antennapod.core.feed.FeedMedia;
import de.danoeh.antennapod.core.feed.MediaType;
import de.danoeh.antennapod.core.preferences.PlaybackPreferences;
import de.danoeh.antennapod.core.preferences.PlaybackSpeedHelper;
import de.danoeh.antennapod.core.preferences.UserPreferences;
import de.danoeh.antennapod.core.service.playback.PlaybackService;
import de.danoeh.antennapod.core.util.playback.Playable;
@ -83,7 +84,7 @@ public class AudioplayerActivity extends MediaplayerInfoActivity {
}
float speed = 1.0f;
if(controller.canSetPlaybackSpeed()) {
speed = UserPreferences.getPlaybackSpeed(controller.getMedia());
speed = PlaybackSpeedHelper.getCurrentPlaybackSpeed(controller.getMedia());
}
String speedStr = new DecimalFormat("0.00").format(speed);
txtvPlaybackSpeed.setText(speedStr);
@ -132,7 +133,11 @@ public class AudioplayerActivity extends MediaplayerInfoActivity {
}
}
storeNewMediaPlaybackSpeed(newSpeed);
try {
PlaybackPreferences.setCurrentlyPlayingTemporaryPlaybackSpeed(Float.parseFloat(newSpeed));
} catch (NumberFormatException e) {
// Well this was awkward...
}
UserPreferences.setPlaybackSpeed(newSpeed);
controller.setPlaybackSpeed(Float.parseFloat(newSpeed));
onPositionObserverUpdate();
@ -148,15 +153,4 @@ public class AudioplayerActivity extends MediaplayerInfoActivity {
txtvPlaybackSpeed.setVisibility(View.VISIBLE);
}
}
private void storeNewMediaPlaybackSpeed(String speed) {
Playable media = controller.getMedia();
if (media instanceof FeedMedia) {
try {
((FeedMedia) media).updateLastPlaybackSpeed(Float.parseFloat(speed));
} catch (NumberFormatException e) {
// Well this was awkward...
}
}
}
}

View File

@ -14,14 +14,12 @@ import com.afollestad.materialdialogs.MaterialDialog;
import java.util.Locale;
import de.danoeh.antennapod.R;
import de.danoeh.antennapod.core.feed.FeedMedia;
import de.danoeh.antennapod.core.preferences.PlaybackSpeedHelper;
import de.danoeh.antennapod.core.preferences.UserPreferences;
import de.danoeh.antennapod.core.util.Converter;
import de.danoeh.antennapod.core.util.playback.Playable;
import de.danoeh.antennapod.core.util.playback.PlaybackController;
import static de.danoeh.antennapod.core.feed.FeedPreferences.SPEED_USE_GLOBAL;
public class PlaybackControlsDialog extends DialogFragment {
private static final float PLAYBACK_SPEED_STEP = 0.05f;
private static final float DEFAULT_MIN_PLAYBACK_SPEED = 0.5f;
@ -217,6 +215,6 @@ public class PlaybackControlsDialog extends DialogFragment {
media = controller.getMedia();
}
return UserPreferences.getPlaybackSpeed(media);
return PlaybackSpeedHelper.getCurrentPlaybackSpeed(media);
}
}

View File

@ -42,9 +42,9 @@ import de.danoeh.antennapod.core.event.DownloaderUpdate;
import de.danoeh.antennapod.core.event.FeedItemEvent;
import de.danoeh.antennapod.core.event.QueueEvent;
import de.danoeh.antennapod.core.feed.EventDistributor;
import de.danoeh.antennapod.core.feed.Feed;
import de.danoeh.antennapod.core.feed.FeedItem;
import de.danoeh.antennapod.core.feed.FeedMedia;
import de.danoeh.antennapod.core.preferences.PlaybackSpeedHelper;
import de.danoeh.antennapod.core.preferences.UserPreferences;
import de.danoeh.antennapod.core.service.download.DownloadService;
import de.danoeh.antennapod.core.service.download.Downloader;
@ -66,7 +66,6 @@ import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;
import static de.danoeh.antennapod.core.feed.FeedPreferences.SPEED_USE_GLOBAL;
import static de.danoeh.antennapod.dialog.EpisodesApplyActionFragment.ACTION_DELETE;
import static de.danoeh.antennapod.dialog.EpisodesApplyActionFragment.ACTION_REMOVE_FROM_QUEUE;
@ -638,14 +637,7 @@ public class QueueFragment extends Fragment {
if(queue.size() > 0) {
long timeLeft = 0;
for(FeedItem item : queue) {
float playbackSpeed = SPEED_USE_GLOBAL;
Feed feed = item.getFeed();
if (feed != null) {
playbackSpeed = feed.getPreferences().getFeedPlaybackSpeed();
}
if (playbackSpeed == SPEED_USE_GLOBAL) {
playbackSpeed = UserPreferences.getPlaybackSpeed(item.getMedia());
}
float playbackSpeed = PlaybackSpeedHelper.getCurrentPlaybackSpeed(item.getMedia());
if(item.getMedia() != null) {
timeLeft +=
(long) ((item.getMedia().getDuration() - item.getMedia().getPosition())

View File

@ -58,7 +58,6 @@ public class FeedMedia extends FeedFile implements Playable {
private Date playbackCompletionDate;
private int startPosition = -1;
private int playedDurationWhenStarted;
private float lastPlaybackSpeed = LAST_PLAYBACK_SPEED_UNSET;
// if null: unknown, will be checked
private Boolean hasEmbeddedPicture;
@ -95,11 +94,10 @@ public class FeedMedia extends FeedFile implements Playable {
private FeedMedia(long id, FeedItem item, int duration, int position,
long size, String mime_type, String file_url, String download_url,
boolean downloaded, Date playbackCompletionDate, int played_duration,
Boolean hasEmbeddedPicture, long lastPlayedTime, float lastPlaybackSpeed) {
Boolean hasEmbeddedPicture, long lastPlayedTime) {
this(id, item, duration, position, size, mime_type, file_url, download_url, downloaded,
playbackCompletionDate, played_duration, lastPlayedTime);
this.hasEmbeddedPicture = hasEmbeddedPicture;
this.lastPlaybackSpeed = lastPlaybackSpeed;
}
public static FeedMedia fromCursor(Cursor cursor) {
@ -114,7 +112,6 @@ public class FeedMedia extends FeedFile implements Playable {
int indexDownloaded = cursor.getColumnIndex(PodDBAdapter.KEY_DOWNLOADED);
int indexPlayedDuration = cursor.getColumnIndex(PodDBAdapter.KEY_PLAYED_DURATION);
int indexLastPlayedTime = cursor.getColumnIndex(PodDBAdapter.KEY_LAST_PLAYED_TIME);
int indexLastPlaybackSpeed = cursor.getColumnIndex(PodDBAdapter.KEY_MEDIA_LAST_PLAYBACK_SPEED);
long mediaId = cursor.getLong(indexId);
Date playbackCompletionDate = null;
@ -149,8 +146,7 @@ public class FeedMedia extends FeedFile implements Playable {
playbackCompletionDate,
cursor.getInt(indexPlayedDuration),
hasEmbeddedPicture,
cursor.getLong(indexLastPlayedTime),
cursor.getFloat(indexLastPlaybackSpeed)
cursor.getLong(indexLastPlayedTime)
);
}
@ -630,33 +626,4 @@ public class FeedMedia extends FeedFile implements Playable {
}
return super.equals(o);
}
public float getLastPlaybackSpeed() {
return lastPlaybackSpeed;
}
public void updateLastPlaybackSpeed(float newSpeed) {
lastPlaybackSpeed = newSpeed;
DBWriter.setFeedMediaPlaybackInformation(this);
}
/**
*
* @return the current playback speed for the media, or the feed's configured speed
*/
public float getMediaPlaybackSpeed() {
float playbackSpeed = lastPlaybackSpeed;
if (playbackSpeed == LAST_PLAYBACK_SPEED_UNSET) {
FeedItem item = getItem();
if (item != null) {
Feed feed = item.getFeed();
if (feed != null) {
playbackSpeed = feed.getPreferences().getFeedPlaybackSpeed();
}
}
}
return playbackSpeed;
}
}

View File

@ -11,6 +11,8 @@ import de.danoeh.antennapod.core.feed.MediaType;
import de.danoeh.antennapod.core.service.playback.PlayerStatus;
import de.danoeh.antennapod.core.util.playback.Playable;
import static de.danoeh.antennapod.core.feed.FeedPreferences.SPEED_USE_GLOBAL;
/**
* Provides access to preferences set by the playback service. A private
* instance of this class must first be instantiated via createInstance() or
@ -54,6 +56,13 @@ public class PlaybackPreferences implements SharedPreferences.OnSharedPreference
*/
private static final String PREF_CURRENT_PLAYER_STATUS = "de.danoeh.antennapod.preferences.currentPlayerStatus";
/**
* A temporary playback speed which overrides the per-feed playback speed for the currently playing
* media. Considered unset if set to SPEED_USE_GLOBAL;
*/
private static final String PREF_CURRENTLY_PLAYING_TEMPORARY_PLAYBACK_SPEED = "de.danoeh.antennapod.preferences.temporaryPlaybackSpeed";
/**
* Value of PREF_CURRENTLY_PLAYING_MEDIA if no media is playing.
*/
@ -112,6 +121,10 @@ public class PlaybackPreferences implements SharedPreferences.OnSharedPreference
return prefs.getInt(PREF_CURRENT_PLAYER_STATUS, PLAYER_STATUS_OTHER);
}
public static float getCurrentlyPlayingTemporaryPlaybackSpeed() {
return prefs.getFloat(PREF_CURRENTLY_PLAYING_TEMPORARY_PLAYBACK_SPEED, SPEED_USE_GLOBAL);
}
public static void writeNoMediaPlaying() {
SharedPreferences.Editor editor = prefs.edit();
editor.putLong(PREF_CURRENTLY_PLAYING_MEDIA, NO_MEDIA_PLAYING);
@ -154,6 +167,18 @@ public class PlaybackPreferences implements SharedPreferences.OnSharedPreference
editor.apply();
}
public static void setCurrentlyPlayingTemporaryPlaybackSpeed(float speed) {
SharedPreferences.Editor editor = prefs.edit();
editor.putFloat(PREF_CURRENTLY_PLAYING_TEMPORARY_PLAYBACK_SPEED, speed);
editor.apply();
}
public static void clearCurrentlyPlayingTemporaryPlaybackSpeed() {
SharedPreferences.Editor editor = prefs.edit();
editor.remove(PREF_CURRENTLY_PLAYING_TEMPORARY_PLAYBACK_SPEED);
editor.apply();
}
private static int getCurrentPlayerStatusAsInt(PlayerStatus playerStatus) {
int playerStatusAsInt;
switch (playerStatus) {

View File

@ -0,0 +1,41 @@
package de.danoeh.antennapod.core.preferences;
import de.danoeh.antennapod.core.feed.Feed;
import de.danoeh.antennapod.core.feed.FeedItem;
import de.danoeh.antennapod.core.feed.FeedMedia;
import de.danoeh.antennapod.core.feed.MediaType;
import de.danoeh.antennapod.core.util.playback.Playable;
import static de.danoeh.antennapod.core.feed.FeedPreferences.SPEED_USE_GLOBAL;
public class PlaybackSpeedHelper {
/**
* Returns the currently configured playback speed for the specified media.
*/
public static float getCurrentPlaybackSpeed(Playable media) {
float playbackSpeed = SPEED_USE_GLOBAL;
MediaType mediaType = null;
if (media != null) {
mediaType = media.getMediaType();
playbackSpeed = PlaybackPreferences.getCurrentlyPlayingTemporaryPlaybackSpeed();
if (playbackSpeed == SPEED_USE_GLOBAL && media instanceof FeedMedia) {
FeedItem item = ((FeedMedia) media).getItem();
if (item != null) {
Feed feed = item.getFeed();
if (feed != null) {
playbackSpeed = feed.getPreferences().getFeedPlaybackSpeed();
}
}
}
}
if (playbackSpeed == SPEED_USE_GLOBAL) {
playbackSpeed = UserPreferences.getPlaybackSpeed(mediaType);
}
return playbackSpeed;
}
}

View File

@ -23,7 +23,6 @@ import java.util.List;
import java.util.Set;
import java.util.concurrent.TimeUnit;
import de.danoeh.antennapod.core.feed.FeedMedia;
import de.danoeh.antennapod.core.feed.MediaType;
import de.danoeh.antennapod.core.R;
import de.danoeh.antennapod.core.service.download.ProxyConfig;
@ -32,12 +31,9 @@ import de.danoeh.antennapod.core.storage.APNullCleanupAlgorithm;
import de.danoeh.antennapod.core.storage.APQueueCleanupAlgorithm;
import de.danoeh.antennapod.core.storage.EpisodeCleanupAlgorithm;
import de.danoeh.antennapod.core.util.Converter;
import de.danoeh.antennapod.core.util.playback.Playable;
import de.danoeh.antennapod.core.util.SortOrder;
import de.danoeh.antennapod.core.util.download.AutoUpdateManager;
import static de.danoeh.antennapod.core.feed.FeedPreferences.SPEED_USE_GLOBAL;
/**
* Provides access to preferences set by the user in the settings screen. A
* private instance of this class must first be instantiated via
@ -326,23 +322,12 @@ public class UserPreferences {
return prefs.getBoolean(PREF_DELETE_REMOVES_FROM_QUEUE, false);
}
public static float getPlaybackSpeed(Playable media) {
float playbackSpeed = SPEED_USE_GLOBAL;
if (media != null) {
if (media instanceof FeedMedia) {
playbackSpeed = ((FeedMedia) media).getMediaPlaybackSpeed();
}
if (playbackSpeed == SPEED_USE_GLOBAL && media.getMediaType() == MediaType.VIDEO) {
playbackSpeed = getVideoPlaybackSpeed();
}
public static float getPlaybackSpeed(MediaType mediaType) {
if (mediaType == MediaType.VIDEO) {
return getVideoPlaybackSpeed();
} else {
return getAudioPlaybackSpeed();
}
if (playbackSpeed == SPEED_USE_GLOBAL) {
playbackSpeed = getAudioPlaybackSpeed();
}
return playbackSpeed;
}
private static float getAudioPlaybackSpeed() {

View File

@ -24,7 +24,7 @@ import java.util.concurrent.TimeUnit;
import de.danoeh.antennapod.core.R;
import de.danoeh.antennapod.core.glide.ApGlideSettings;
import de.danoeh.antennapod.core.preferences.UserPreferences;
import de.danoeh.antennapod.core.preferences.PlaybackSpeedHelper;
import de.danoeh.antennapod.core.receiver.MediaButtonReceiver;
import de.danoeh.antennapod.core.receiver.PlayerWidget;
import de.danoeh.antennapod.core.service.playback.PlaybackService;
@ -147,7 +147,7 @@ public class PlayerWidgetJobService extends SafeJobIntentService {
progressString = getProgressString(playbackService.getCurrentPosition(),
playbackService.getDuration(), playbackService.getCurrentPlaybackSpeed());
} else {
progressString = getProgressString(media.getPosition(), media.getDuration(), UserPreferences.getPlaybackSpeed(media));
progressString = getProgressString(media.getPosition(), media.getDuration(), PlaybackSpeedHelper.getCurrentPlaybackSpeed(media));
}
if (progressString != null) {

View File

@ -26,8 +26,8 @@ import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.locks.ReentrantLock;
import de.danoeh.antennapod.core.feed.FeedMedia;
import de.danoeh.antennapod.core.feed.MediaType;
import de.danoeh.antennapod.core.preferences.PlaybackSpeedHelper;
import de.danoeh.antennapod.core.preferences.UserPreferences;
import de.danoeh.antennapod.core.util.RewindAfterPauseUtils;
import de.danoeh.antennapod.core.util.playback.AudioPlayer;
@ -35,8 +35,6 @@ import de.danoeh.antennapod.core.util.playback.IPlayer;
import de.danoeh.antennapod.core.util.playback.Playable;
import de.danoeh.antennapod.core.util.playback.VideoPlayer;
import static de.danoeh.antennapod.core.feed.FeedPreferences.SPEED_USE_GLOBAL;
/**
* Manages the MediaPlayer object of the PlaybackService.
*/
@ -245,7 +243,7 @@ public class LocalPSMP extends PlaybackServiceMediaPlayer {
try {
media.loadMetadata();
callback.onMediaChanged(false);
setPlaybackParams(UserPreferences.getPlaybackSpeed(media), UserPreferences.isSkipSilence());
setPlaybackParams(PlaybackSpeedHelper.getCurrentPlaybackSpeed(media), UserPreferences.isSkipSilence());
if (stream) {
mediaPlayer.setDataSource(media.getStreamUrl());
} else if (media.getLocalMediaUrl() != null && new File(media.getLocalMediaUrl()).canRead()) {
@ -309,7 +307,7 @@ public class LocalPSMP extends PlaybackServiceMediaPlayer {
Log.d(TAG, "Resuming/Starting playback");
acquireWifiLockIfNecessary();
setPlaybackParams(UserPreferences.getPlaybackSpeed(media), UserPreferences.isSkipSilence());
setPlaybackParams(PlaybackSpeedHelper.getCurrentPlaybackSpeed(media), UserPreferences.isSkipSilence());
setVolume(UserPreferences.getLeftVolume(), UserPreferences.getRightVolume());
if (playerStatus == PlayerStatus.PREPARED && media.getPosition() > 0) {

View File

@ -959,6 +959,8 @@ public class PlaybackService extends MediaBrowserServiceCompat {
*/
private void onPostPlayback(final Playable playable, boolean ended, boolean skipped,
boolean playingNext) {
// Reset the temporary playback speed because it only referred to the last playable
PlaybackPreferences.clearCurrentlyPlayingTemporaryPlaybackSpeed();
if (playable == null) {
Log.e(TAG, "Cannot do post-playback processing: media was null");
return;

View File

@ -294,8 +294,6 @@ class DBUpgrader {
if (oldVersion < 1070400) {
db.execSQL("ALTER TABLE " + PodDBAdapter.TABLE_NAME_FEEDS
+ " ADD COLUMN " + PodDBAdapter.KEY_FEED_PLAYBACK_SPEED + " REAL DEFAULT " + SPEED_USE_GLOBAL);
db.execSQL("ALTER TABLE " + PodDBAdapter.TABLE_NAME_FEED_MEDIA
+ " ADD COLUMN " + PodDBAdapter.KEY_MEDIA_LAST_PLAYBACK_SPEED + " REAL DEFAULT " + LAST_PLAYBACK_SPEED_UNSET);
}
}

View File

@ -36,7 +36,6 @@ import de.danoeh.antennapod.core.preferences.UserPreferences;
import de.danoeh.antennapod.core.service.download.DownloadStatus;
import de.danoeh.antennapod.core.util.LongIntMap;
import static de.danoeh.antennapod.core.feed.FeedMedia.LAST_PLAYBACK_SPEED_UNSET;
import static de.danoeh.antennapod.core.feed.FeedPreferences.SPEED_USE_GLOBAL;
// TODO Remove media column from feeditem table
@ -108,7 +107,6 @@ public class PodDBAdapter {
public static final String KEY_INCLUDE_FILTER = "include_filter";
public static final String KEY_EXCLUDE_FILTER = "exclude_filter";
public static final String KEY_FEED_PLAYBACK_SPEED = "feed_playback_speed";
public static final String KEY_MEDIA_LAST_PLAYBACK_SPEED = "last_playback_speed";
// Table names
static final String TABLE_NAME_FEEDS = "Feeds";
@ -163,8 +161,7 @@ public class PodDBAdapter {
+ KEY_FEEDITEM + " INTEGER,"
+ KEY_PLAYED_DURATION + " INTEGER,"
+ KEY_HAS_EMBEDDED_PICTURE + " INTEGER,"
+ KEY_LAST_PLAYED_TIME + " INTEGER,"
+ KEY_MEDIA_LAST_PLAYBACK_SPEED + " REAL DEFAULT " + LAST_PLAYBACK_SPEED_UNSET + ")";
+ KEY_LAST_PLAYED_TIME + " INTEGER" + ")";
private static final String CREATE_TABLE_DOWNLOAD_LOG = "CREATE TABLE "
+ TABLE_NAME_DOWNLOAD_LOG + " (" + TABLE_PRIMARY_KEY + KEY_FEEDFILE
@ -435,7 +432,6 @@ public class PodDBAdapter {
values.put(KEY_FILE_URL, media.getFile_url());
values.put(KEY_HAS_EMBEDDED_PICTURE, media.hasEmbeddedPicture());
values.put(KEY_LAST_PLAYED_TIME, media.getLastPlayedTime());
values.put(KEY_MEDIA_LAST_PLAYBACK_SPEED, media.getLastPlaybackSpeed());
if (media.getPlaybackCompletionDate() != null) {
values.put(KEY_PLAYBACK_COMPLETION_DATE, media.getPlaybackCompletionDate().getTime());
@ -461,7 +457,6 @@ public class PodDBAdapter {
values.put(KEY_DURATION, media.getDuration());
values.put(KEY_PLAYED_DURATION, media.getPlayedDuration());
values.put(KEY_LAST_PLAYED_TIME, media.getLastPlayedTime());
values.put(KEY_MEDIA_LAST_PLAYBACK_SPEED, media.getLastPlaybackSpeed());
db.update(TABLE_NAME_FEED_MEDIA, values, KEY_ID + "=?",
new String[]{String.valueOf(media.getId())});
} else {
@ -475,7 +470,6 @@ public class PodDBAdapter {
values.put(KEY_PLAYBACK_COMPLETION_DATE, media.getPlaybackCompletionDate().getTime());
values.put(KEY_PLAYED_DURATION, media.getPlayedDuration());
// Also reset stored playback speed for media
values.put(KEY_MEDIA_LAST_PLAYBACK_SPEED, LAST_PLAYBACK_SPEED_UNSET);
db.update(TABLE_NAME_FEED_MEDIA, values, KEY_ID + "=?",
new String[]{String.valueOf(media.getId())});
} else {

View File

@ -29,6 +29,7 @@ import de.danoeh.antennapod.core.feed.Chapter;
import de.danoeh.antennapod.core.feed.FeedMedia;
import de.danoeh.antennapod.core.feed.MediaType;
import de.danoeh.antennapod.core.preferences.PlaybackPreferences;
import de.danoeh.antennapod.core.preferences.PlaybackSpeedHelper;
import de.danoeh.antennapod.core.preferences.UserPreferences;
import de.danoeh.antennapod.core.service.playback.PlaybackService;
import de.danoeh.antennapod.core.service.playback.PlaybackServiceMediaPlayer;
@ -714,7 +715,7 @@ public class PlaybackController {
if (playbackService != null && canSetPlaybackSpeed()) {
return playbackService.getCurrentPlaybackSpeed();
} else {
return UserPreferences.getPlaybackSpeed(getMedia());
return PlaybackSpeedHelper.getCurrentPlaybackSpeed(getMedia());
}
}