Fixed auto-flattr when playback ends

This commit is contained in:
daniel oeh 2014-07-26 13:53:03 +02:00
parent 421786e5c8
commit df288f8f68
2 changed files with 25 additions and 1 deletions

View File

@ -408,8 +408,17 @@ public class UserPreferences implements
instance.updateInterval = newValue;
}
/**
* Change the auto-flattr settings
*
* @param context For accessing the shared preferences
* @param enabled Whether automatic flattring should be enabled at all
* @param autoFlattrThreshold The percentage of playback time after which an episode should be
* flattrd. Must be a value between 0 and 1 (inclusive)
* */
public static void setAutoFlattrSettings(Context context, boolean enabled, float autoFlattrThreshold) {
instanceAvailable();
Validate.inclusiveBetween(0.0, 1.0, autoFlattrThreshold);
PreferenceManager.getDefaultSharedPreferences(context.getApplicationContext())
.edit()
.putBoolean(PREF_AUTO_FLATTR, enabled)

View File

@ -43,6 +43,7 @@ import de.danoeh.antennapod.storage.DBTasks;
import de.danoeh.antennapod.storage.DBWriter;
import de.danoeh.antennapod.util.BitmapDecoder;
import de.danoeh.antennapod.util.QueueAccess;
import de.danoeh.antennapod.util.flattr.FlattrThing;
import de.danoeh.antennapod.util.flattr.FlattrUtils;
import de.danoeh.antennapod.util.playback.Playable;
@ -512,6 +513,11 @@ public class PlaybackService extends Service {
DBWriter.removeQueueItem(PlaybackService.this, item.getId(), true);
}
DBWriter.addItemToPlaybackHistory(PlaybackService.this, (FeedMedia) media);
// auto-flattr if enabled
if (isAutoFlattrable(media) && UserPreferences.getAutoFlattrPlayedDurationThreshold() == 1.0f) {
DBTasks.flattrItemIfLoggedIn(PlaybackService.this, item);
}
}
// Load next episode if previous episode was in the queue and if there
@ -762,7 +768,7 @@ public class PlaybackService extends Service {
FeedItem item = m.getItem();
m.setPlayedDuration(m.getPlayedDuration() + ((int)(deltaPlayedDuration * playbackSpeed)));
// Auto flattr
if (FlattrUtils.hasToken() && UserPreferences.isAutoFlattr() && item.getPaymentLink() != null && item.getFlattrStatus().getUnflattred() &&
if (isAutoFlattrable(m) &&
(m.getPlayedDuration() > UserPreferences.getAutoFlattrPlayedDurationThreshold() * duration)) {
if (BuildConfig.DEBUG)
@ -1051,4 +1057,13 @@ public class PlaybackService extends Service {
return mediaPlayer.getVideoSize();
}
private boolean isAutoFlattrable(Playable p) {
if (p != null && p instanceof FeedMedia) {
FeedMedia media = (FeedMedia) p;
FeedItem item = ((FeedMedia) p).getItem();
return item != null && FlattrUtils.hasToken() && UserPreferences.isAutoFlattr() && item.getPaymentLink() != null && item.getFlattrStatus().getUnflattred();
} else {
return false;
}
}
}