Draft volume reduction settings per feed

This commit is contained in:
Max Bechtold 2019-06-29 11:55:59 +02:00
parent 0bd3b72652
commit c628fdfcfd
12 changed files with 125 additions and 21 deletions

View File

@ -129,7 +129,7 @@ public class PlaybackServiceMediaPlayerTest {
private Playable writeTestPlayable(String downloadUrl, String fileUrl) { private Playable writeTestPlayable(String downloadUrl, String fileUrl) {
final Context c = getInstrumentation().getTargetContext(); final Context c = getInstrumentation().getTargetContext();
Feed f = new Feed(0, null, "f", "l", "d", null, null, null, null, "i", null, null, "l", false); Feed f = new Feed(0, null, "f", "l", "d", null, null, null, null, "i", null, null, "l", false);
FeedPreferences prefs = new FeedPreferences(f.getId(), false, FeedPreferences.AutoDeleteAction.NO, null, null); FeedPreferences prefs = new FeedPreferences(f.getId(), false, FeedPreferences.AutoDeleteAction.NO, FeedPreferences.VolumeReductionSetting.OFF, null, null);
f.setPreferences(prefs); f.setPreferences(prefs);
f.setItems(new ArrayList<>()); f.setItems(new ArrayList<>());
FeedItem i = new FeedItem(0, "t", "i", "l", new Date(), FeedItem.UNPLAYED, f); FeedItem i = new FeedItem(0, "t", "i", "l", new Date(), FeedItem.UNPLAYED, f);

View File

@ -270,7 +270,7 @@ public class OnlineFeedViewActivity extends AppCompatActivity {
url = URLChecker.prepareURL(url); url = URLChecker.prepareURL(url);
feed = new Feed(url, null); feed = new Feed(url, null);
if (username != null && password != null) { if (username != null && password != null) {
feed.setPreferences(new FeedPreferences(0, false, FeedPreferences.AutoDeleteAction.GLOBAL, username, password)); feed.setPreferences(new FeedPreferences(0, false, FeedPreferences.AutoDeleteAction.GLOBAL, FeedPreferences.VolumeReductionSetting.OFF, username, password));
} }
String fileUrl = new File(getExternalCacheDir(), String fileUrl = new File(getExternalCacheDir(),
FileNameGenerator.generateFileName(feed.getDownload_url())).toString(); FileNameGenerator.generateFileName(feed.getDownload_url())).toString();

View File

@ -38,10 +38,12 @@ public class FeedSettingsFragment extends PreferenceFragmentCompat {
setupAutoDownloadPreference(); setupAutoDownloadPreference();
setupKeepUpdatedPreference(); setupKeepUpdatedPreference();
setupAutoDeletePreference(); setupAutoDeletePreference();
setupVolumeReductionPreferences();
setupAuthentificationPreference(); setupAuthentificationPreference();
setupEpisodeFilterPreference(); setupEpisodeFilterPreference();
updateAutoDeleteSummary(); updateAutoDeleteSummary();
updateVolumeReductionSummary();
updateAutoDownloadEnabled(); updateAutoDownloadEnabled();
}).dispose(); }).dispose();
} }
@ -114,6 +116,46 @@ public class FeedSettingsFragment extends PreferenceFragmentCompat {
} }
} }
private void setupVolumeReductionPreferences() {
ListPreference volumeReductionPreference = (ListPreference) findPreference("volumeReduction");
volumeReductionPreference.setOnPreferenceChangeListener((preference, newValue) -> {
switch ((String) newValue) {
case "off":
feedPreferences.setVolumeReductionSetting(FeedPreferences.VolumeReductionSetting.OFF);
break;
case "light":
feedPreferences.setVolumeReductionSetting(FeedPreferences.VolumeReductionSetting.LIGHT);
break;
case "heavy":
feedPreferences.setVolumeReductionSetting(FeedPreferences.VolumeReductionSetting.HEAVY);
break;
}
feed.savePreferences();
updateVolumeReductionSummary();
// TODO maxbechtold Check if we can call setVolume for the PlaybackService, if running
return false;
});
}
private void updateVolumeReductionSummary() {
ListPreference volumeReductionPreference = (ListPreference) findPreference("volumeReduction");
switch (feedPreferences.getVolumeReductionSetting()) {
case OFF:
volumeReductionPreference.setSummary(R.string.feed_volume_reduction_off);
volumeReductionPreference.setValue("off");
break;
case LIGHT:
volumeReductionPreference.setSummary(R.string.feed_volume_reduction_light);
volumeReductionPreference.setValue("light");
break;
case HEAVY:
volumeReductionPreference.setSummary(R.string.feed_volume_reduction_heavy);
volumeReductionPreference.setValue("heavy");
break;
}
}
private void setupKeepUpdatedPreference() { private void setupKeepUpdatedPreference() {
SwitchPreference pref = (SwitchPreference) findPreference("keepUpdated"); SwitchPreference pref = (SwitchPreference) findPreference("keepUpdated");

View File

@ -18,6 +18,14 @@
android:title="@string/auto_delete_label" android:title="@string/auto_delete_label"
android:key="autoDelete"/> android:key="autoDelete"/>
<ListPreference
android:entries="@array/spnVolumeReductionItems"
android:entryValues="@array/spnVolumeReductionValues"
android:summary="Reduce volume when playing episodes of this feed"
android:title="@string/feed_volume_reduction"
android:key="volumeReduction"/>
<PreferenceCategory android:title="@string/auto_download_settings_label"> <PreferenceCategory android:title="@string/auto_download_settings_label">
<SwitchPreference <SwitchPreference
android:key="autoDownload" android:key="autoDownload"

View File

@ -160,7 +160,7 @@ public class Feed extends FeedFile implements ImageResource {
*/ */
public Feed(String url, String lastUpdate, String title, String username, String password) { public Feed(String url, String lastUpdate, String title, String username, String password) {
this(url, lastUpdate, title); this(url, lastUpdate, title);
preferences = new FeedPreferences(0, true, FeedPreferences.AutoDeleteAction.GLOBAL, username, password); preferences = new FeedPreferences(0, true, FeedPreferences.AutoDeleteAction.GLOBAL, FeedPreferences.VolumeReductionSetting.OFF, username, password);
} }
public static Feed fromCursor(Cursor cursor) { public static Feed fromCursor(Cursor cursor) {

View File

@ -26,18 +26,27 @@ public class FeedPreferences {
NO NO
} }
private AutoDeleteAction auto_delete_action; private AutoDeleteAction auto_delete_action;
public enum VolumeReductionSetting {
OFF,
LIGHT,
HEAVY
}
private VolumeReductionSetting volumeReductionSetting;
private String username; private String username;
private String password; private String password;
public FeedPreferences(long feedID, boolean autoDownload, AutoDeleteAction auto_delete_action, String username, String password) { public FeedPreferences(long feedID, boolean autoDownload, AutoDeleteAction auto_delete_action, VolumeReductionSetting volumeReductionSetting, String username, String password) {
this(feedID, autoDownload, true, auto_delete_action, username, password, new FeedFilter()); this(feedID, autoDownload, true, auto_delete_action, volumeReductionSetting, username, password, new FeedFilter());
} }
private FeedPreferences(long feedID, boolean autoDownload, boolean keepUpdated, AutoDeleteAction auto_delete_action, String username, String password, @NonNull FeedFilter filter) { private FeedPreferences(long feedID, boolean autoDownload, boolean keepUpdated, AutoDeleteAction auto_delete_action, VolumeReductionSetting volumeReductionSetting, String username, String password, @NonNull FeedFilter filter) {
this.feedID = feedID; this.feedID = feedID;
this.autoDownload = autoDownload; this.autoDownload = autoDownload;
this.keepUpdated = keepUpdated; this.keepUpdated = keepUpdated;
this.auto_delete_action = auto_delete_action; this.auto_delete_action = auto_delete_action;
this.volumeReductionSetting = volumeReductionSetting;
this.username = username; this.username = username;
this.password = password; this.password = password;
this.filter = filter; this.filter = filter;
@ -48,6 +57,7 @@ public class FeedPreferences {
int indexAutoDownload = cursor.getColumnIndex(PodDBAdapter.KEY_AUTO_DOWNLOAD); int indexAutoDownload = cursor.getColumnIndex(PodDBAdapter.KEY_AUTO_DOWNLOAD);
int indexAutoRefresh = cursor.getColumnIndex(PodDBAdapter.KEY_KEEP_UPDATED); int indexAutoRefresh = cursor.getColumnIndex(PodDBAdapter.KEY_KEEP_UPDATED);
int indexAutoDeleteAction = cursor.getColumnIndex(PodDBAdapter.KEY_AUTO_DELETE_ACTION); int indexAutoDeleteAction = cursor.getColumnIndex(PodDBAdapter.KEY_AUTO_DELETE_ACTION);
int indexVolumeReduction = cursor.getColumnIndex(PodDBAdapter.KEY_FEED_VOLUME_REDUCTION);
int indexUsername = cursor.getColumnIndex(PodDBAdapter.KEY_USERNAME); int indexUsername = cursor.getColumnIndex(PodDBAdapter.KEY_USERNAME);
int indexPassword = cursor.getColumnIndex(PodDBAdapter.KEY_PASSWORD); int indexPassword = cursor.getColumnIndex(PodDBAdapter.KEY_PASSWORD);
int indexIncludeFilter = cursor.getColumnIndex(PodDBAdapter.KEY_INCLUDE_FILTER); int indexIncludeFilter = cursor.getColumnIndex(PodDBAdapter.KEY_INCLUDE_FILTER);
@ -58,11 +68,13 @@ public class FeedPreferences {
boolean autoRefresh = cursor.getInt(indexAutoRefresh) > 0; boolean autoRefresh = cursor.getInt(indexAutoRefresh) > 0;
int autoDeleteActionIndex = cursor.getInt(indexAutoDeleteAction); int autoDeleteActionIndex = cursor.getInt(indexAutoDeleteAction);
AutoDeleteAction autoDeleteAction = AutoDeleteAction.values()[autoDeleteActionIndex]; AutoDeleteAction autoDeleteAction = AutoDeleteAction.values()[autoDeleteActionIndex];
int volumeReductionIndex = cursor.getInt(indexVolumeReduction);
VolumeReductionSetting volumeReductionSetting = VolumeReductionSetting.values()[volumeReductionIndex];
String username = cursor.getString(indexUsername); String username = cursor.getString(indexUsername);
String password = cursor.getString(indexPassword); String password = cursor.getString(indexPassword);
String includeFilter = cursor.getString(indexIncludeFilter); String includeFilter = cursor.getString(indexIncludeFilter);
String excludeFilter = cursor.getString(indexExcludeFilter); String excludeFilter = cursor.getString(indexExcludeFilter);
return new FeedPreferences(feedId, autoDownload, autoRefresh, autoDeleteAction, username, password, new FeedFilter(includeFilter, excludeFilter)); return new FeedPreferences(feedId, autoDownload, autoRefresh, autoDeleteAction, volumeReductionSetting, username, password, new FeedFilter(includeFilter, excludeFilter));
} }
/** /**
@ -138,10 +150,18 @@ public class FeedPreferences {
return auto_delete_action; return auto_delete_action;
} }
public VolumeReductionSetting getVolumeReductionSetting() {
return volumeReductionSetting;
}
public void setAutoDeleteAction(AutoDeleteAction auto_delete_action) { public void setAutoDeleteAction(AutoDeleteAction auto_delete_action) {
this.auto_delete_action = auto_delete_action; this.auto_delete_action = auto_delete_action;
} }
public void setVolumeReductionSetting(VolumeReductionSetting volumeReductionSetting) {
this.volumeReductionSetting = volumeReductionSetting;
}
public boolean getCurrentAutoDelete() { public boolean getCurrentAutoDelete() {
switch (auto_delete_action) { switch (auto_delete_action) {
case GLOBAL: case GLOBAL:

View File

@ -764,7 +764,7 @@ public class DownloadService extends Service {
feed.setId(request.getFeedfileId()); feed.setId(request.getFeedfileId());
feed.setDownloaded(true); feed.setDownloaded(true);
feed.setPreferences(new FeedPreferences(0, true, FeedPreferences.AutoDeleteAction.GLOBAL, feed.setPreferences(new FeedPreferences(0, true, FeedPreferences.AutoDeleteAction.GLOBAL,
request.getUsername(), request.getPassword())); FeedPreferences.VolumeReductionSetting.OFF, request.getUsername(), request.getPassword()));
feed.setPageNr(request.getArguments().getInt(DownloadRequester.REQUEST_ARG_PAGE_NR, 0)); feed.setPageNr(request.getArguments().getInt(DownloadRequester.REQUEST_ARG_PAGE_NR, 0));
DownloadError reason = null; DownloadError reason = null;

View File

@ -313,18 +313,10 @@ public class LocalPSMP extends PlaybackServiceMediaPlayer {
UserPreferences.setPlaybackSpeed(String.valueOf(speed)); UserPreferences.setPlaybackSpeed(String.valueOf(speed));
} }
setPlaybackParams(speed, UserPreferences.isSkipSilence()); setPlaybackParams(speed, UserPreferences.isSkipSilence());
// TODO MAX Here and everywhere else volume is adapted
Playable playable = getPlayable();
if (playable instanceof FeedMedia) {
FeedMedia feedMedia = (FeedMedia) playable;
FeedPreferences preferences = feedMedia.getItem().getFeed().getPreferences();
// TODO MAX Check if this feed should have volume adjusted (e.g. louder than others) float leftVolume = UserPreferences.getLeftVolume();
float rightVolume = UserPreferences.getRightVolume();
setVolume(leftVolume, rightVolume);
}
setVolume(UserPreferences.getLeftVolume(), UserPreferences.getRightVolume());
if (playerStatus == PlayerStatus.PREPARED && media.getPosition() > 0) { if (playerStatus == PlayerStatus.PREPARED && media.getPosition() > 0) {
int newPosition = RewindAfterPauseUtils.calculatePositionWithRewind( int newPosition = RewindAfterPauseUtils.calculatePositionWithRewind(
@ -344,8 +336,25 @@ public class LocalPSMP extends PlaybackServiceMediaPlayer {
} }
} }
private float getVolumeReductionFactor() {
Playable playable = getPlayable();
if (playable instanceof FeedMedia) {
FeedMedia feedMedia = (FeedMedia) playable;
FeedPreferences preferences = feedMedia.getItem().getFeed().getPreferences();
FeedPreferences.VolumeReductionSetting volumeReductionSetting = preferences.getVolumeReductionSetting();
/** // TODO maxbechtold These numbers should be tested
if (volumeReductionSetting == FeedPreferences.VolumeReductionSetting.LIGHT) {
return 0.4f;
} else if (volumeReductionSetting == FeedPreferences.VolumeReductionSetting.HEAVY) {
return 0.2f;
}
}
return 1.0f;
}
/**
* Saves the current position and pauses playback. Note that, if audiofocus * Saves the current position and pauses playback. Note that, if audiofocus
* is abandoned, the lockscreen controls will also disapear. * is abandoned, the lockscreen controls will also disapear.
* <p/> * <p/>
@ -681,6 +690,10 @@ public class LocalPSMP extends PlaybackServiceMediaPlayer {
private void setVolumeSync(float volumeLeft, float volumeRight) { private void setVolumeSync(float volumeLeft, float volumeRight) {
playerLock.lock(); playerLock.lock();
if (media != null && media.getMediaType() == MediaType.AUDIO) { if (media != null && media.getMediaType() == MediaType.AUDIO) {
// TODO maxbechtold does not apply to currently playing episode
float reductionFactor = getVolumeReductionFactor();
volumeLeft *= reductionFactor;
volumeRight *= reductionFactor;
mediaPlayer.setVolume(volumeLeft, volumeRight); mediaPlayer.setVolume(volumeLeft, volumeRight);
Log.d(TAG, "Media player volume was set to " + volumeLeft + " " + volumeRight); Log.d(TAG, "Media player volume was set to " + volumeLeft + " " + volumeRight);
} }

View File

@ -193,6 +193,7 @@ class DBUpgrader {
db.execSQL(sql); db.execSQL(sql);
} }
if (oldVersion <= 17) { if (oldVersion <= 17) {
// TODO maxbechtold Something like this for volume reduction
db.execSQL("ALTER TABLE " + PodDBAdapter.TABLE_NAME_FEEDS db.execSQL("ALTER TABLE " + PodDBAdapter.TABLE_NAME_FEEDS
+ " ADD COLUMN " + PodDBAdapter.KEY_AUTO_DELETE_ACTION + " INTEGER DEFAULT 0"); + " ADD COLUMN " + PodDBAdapter.KEY_AUTO_DELETE_ACTION + " INTEGER DEFAULT 0");
} }

View File

@ -103,6 +103,7 @@ public class PodDBAdapter {
public static final String KEY_AUTO_DOWNLOAD = "auto_download"; public static final String KEY_AUTO_DOWNLOAD = "auto_download";
public static final String KEY_KEEP_UPDATED = "keep_updated"; public static final String KEY_KEEP_UPDATED = "keep_updated";
public static final String KEY_AUTO_DELETE_ACTION = "auto_delete_action"; public static final String KEY_AUTO_DELETE_ACTION = "auto_delete_action";
public static final String KEY_FEED_VOLUME_REDUCTION = "feed_volume_reduction";
public static final String KEY_PLAYED_DURATION = "played_duration"; public static final String KEY_PLAYED_DURATION = "played_duration";
public static final String KEY_USERNAME = "username"; public static final String KEY_USERNAME = "username";
public static final String KEY_PASSWORD = "password"; public static final String KEY_PASSWORD = "password";
@ -146,7 +147,8 @@ public class PodDBAdapter {
+ KEY_NEXT_PAGE_LINK + " TEXT," + KEY_NEXT_PAGE_LINK + " TEXT,"
+ KEY_HIDE + " TEXT," + KEY_HIDE + " TEXT,"
+ KEY_LAST_UPDATE_FAILED + " INTEGER DEFAULT 0," + KEY_LAST_UPDATE_FAILED + " INTEGER DEFAULT 0,"
+ KEY_AUTO_DELETE_ACTION + " INTEGER DEFAULT 0)"; + KEY_AUTO_DELETE_ACTION + " INTEGER DEFAULT 0,"
+ KEY_FEED_VOLUME_REDUCTION + " INTEGER DEFAULT 0)";
private static final String CREATE_TABLE_FEED_ITEMS = "CREATE TABLE " private static final String CREATE_TABLE_FEED_ITEMS = "CREATE TABLE "
+ TABLE_NAME_FEED_ITEMS + " (" + TABLE_PRIMARY_KEY + KEY_TITLE + TABLE_NAME_FEED_ITEMS + " (" + TABLE_PRIMARY_KEY + KEY_TITLE
@ -242,6 +244,7 @@ public class PodDBAdapter {
TABLE_NAME_FEEDS + "." + KEY_HIDE, TABLE_NAME_FEEDS + "." + KEY_HIDE,
TABLE_NAME_FEEDS + "." + KEY_LAST_UPDATE_FAILED, TABLE_NAME_FEEDS + "." + KEY_LAST_UPDATE_FAILED,
TABLE_NAME_FEEDS + "." + KEY_AUTO_DELETE_ACTION, TABLE_NAME_FEEDS + "." + KEY_AUTO_DELETE_ACTION,
TABLE_NAME_FEEDS + "." + KEY_FEED_VOLUME_REDUCTION,
TABLE_NAME_FEEDS + "." + KEY_INCLUDE_FILTER, TABLE_NAME_FEEDS + "." + KEY_INCLUDE_FILTER,
TABLE_NAME_FEEDS + "." + KEY_EXCLUDE_FILTER TABLE_NAME_FEEDS + "." + KEY_EXCLUDE_FILTER
}; };
@ -404,6 +407,7 @@ public class PodDBAdapter {
values.put(KEY_AUTO_DOWNLOAD, prefs.getAutoDownload()); values.put(KEY_AUTO_DOWNLOAD, prefs.getAutoDownload());
values.put(KEY_KEEP_UPDATED, prefs.getKeepUpdated()); values.put(KEY_KEEP_UPDATED, prefs.getKeepUpdated());
values.put(KEY_AUTO_DELETE_ACTION, prefs.getAutoDeleteAction().ordinal()); values.put(KEY_AUTO_DELETE_ACTION, prefs.getAutoDeleteAction().ordinal());
values.put(KEY_FEED_VOLUME_REDUCTION, prefs.getVolumeReductionSetting().ordinal());
values.put(KEY_USERNAME, prefs.getUsername()); values.put(KEY_USERNAME, prefs.getUsername());
values.put(KEY_PASSWORD, prefs.getPassword()); values.put(KEY_PASSWORD, prefs.getPassword());
values.put(KEY_INCLUDE_FILTER, prefs.getFilter().getIncludeFilter()); values.put(KEY_INCLUDE_FILTER, prefs.getFilter().getIncludeFilter());

View File

@ -13,6 +13,18 @@
<item>never</item> <item>never</item>
</string-array> </string-array>
<string-array name="spnVolumeReductionItems">
<item>@string/feed_volume_reduction_off</item>
<item>@string/feed_volume_reduction_light</item>
<item>@string/feed_volume_reduction_heavy</item>
</string-array>
<string-array name="spnVolumeReductionValues">
<item>off</item>
<item>light</item>
<item>heavy</item>
</string-array>
<string-array name="smart_mark_as_played_values"> <string-array name="smart_mark_as_played_values">
<item>0</item> <item>0</item>
<item>15</item> <item>15</item>

View File

@ -97,6 +97,10 @@
<string name="auto_download_apply_to_items_title">Apply to Previous Episodes</string> <string name="auto_download_apply_to_items_title">Apply to Previous Episodes</string>
<string name="auto_download_apply_to_items_message">The new <i>Auto Download</i> setting will automatically be applied to new episodes.\nDo you also want to apply it to previously published episodes?</string> <string name="auto_download_apply_to_items_message">The new <i>Auto Download</i> setting will automatically be applied to new episodes.\nDo you also want to apply it to previously published episodes?</string>
<string name="auto_delete_label">Auto Delete Episode</string> <string name="auto_delete_label">Auto Delete Episode</string>
<string name="feed_volume_reduction">Volume Reduction</string>
<string name="feed_volume_reduction_off">Off</string>
<string name="feed_volume_reduction_light">Light</string>
<string name="feed_volume_reduction_heavy">Heavy</string>
<string name="parallel_downloads_suffix">\u0020parallel downloads</string> <string name="parallel_downloads_suffix">\u0020parallel downloads</string>
<string name="feed_auto_download_global">Global default</string> <string name="feed_auto_download_global">Global default</string>
<string name="feed_auto_download_always">Always</string> <string name="feed_auto_download_always">Always</string>