Merge branch 'develop' of https://github.com/AntennaPod/AntennaPod into issue/940-user-preferences

This commit is contained in:
Martin Fietz 2015-06-23 14:40:37 +02:00
commit f123c61f21
7 changed files with 86 additions and 148 deletions

View File

@ -231,7 +231,7 @@ public class PreferencesTest extends ActivityInstrumentationTestCase2<Preference
return pauseForFocusLoss != UserPreferences.shouldPauseForFocusLoss();
}
}, Timeout.getLargeTimeout());
solo.clickOnText(solo.getString(R.string.pref_auto_delete_title));
solo.clickOnText(solo.getString(R.string.pref_pausePlaybackForFocusLoss_title));
solo.waitForCondition(new Condition() {
@Override
public boolean isSatisfied() {

View File

@ -39,7 +39,7 @@ public class PodcastApp extends Application {
PicassoProvider.setupPicassoInstance(this);
UserPreferences.init(this);
PlaybackPreferences.createInstance(this);
PlaybackPreferences.init(this);
EventDistributor.getInstance();
SPAUtil.sendSPAppsQueryFeedsIntent(this);

View File

@ -8,7 +8,6 @@ import android.content.Intent;
import de.danoeh.antennapod.PodcastApp;
import de.danoeh.antennapod.activity.StorageErrorActivity;
import de.danoeh.antennapod.core.ApplicationCallbacks;
import de.danoeh.antennapod.core.preferences.UserPreferences;
public class ApplicationCallbacksImpl implements ApplicationCallbacks {
@ -22,8 +21,4 @@ public class ApplicationCallbacksImpl implements ApplicationCallbacks {
return new Intent(context, StorageErrorActivity.class);
}
@Override
public void setUpdateInterval(long updateInterval) {
UserPreferences.restartUpdateAlarm(updateInterval, updateInterval);
}
}

View File

@ -20,5 +20,4 @@ public interface ApplicationCallbacks {
*/
public Intent getStorageErrorActivity(Context context);
public void setUpdateInterval(long updateInterval);
}

View File

@ -3,11 +3,7 @@ package de.danoeh.antennapod.core.preferences;
import android.content.Context;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;
import android.util.Log;
import org.apache.commons.lang3.Validate;
import de.danoeh.antennapod.core.BuildConfig;
import de.danoeh.antennapod.core.feed.EventDistributor;
/**
@ -15,159 +11,104 @@ import de.danoeh.antennapod.core.feed.EventDistributor;
* instance of this class must first be instantiated via createInstance() or
* otherwise every public method will throw an Exception when called.
*/
public class PlaybackPreferences implements
SharedPreferences.OnSharedPreferenceChangeListener {
private static final String TAG = "PlaybackPreferences";
public class PlaybackPreferences implements SharedPreferences.OnSharedPreferenceChangeListener {
/**
* Contains the feed id of the currently playing item if it is a FeedMedia
* object.
*/
public static final String PREF_CURRENTLY_PLAYING_FEED_ID = "de.danoeh.antennapod.preferences.lastPlayedFeedId";
private static final String TAG = "PlaybackPreferences";
/**
* Contains the id of the currently playing FeedMedia object or
* NO_MEDIA_PLAYING if the currently playing media is no FeedMedia object.
*/
public static final String PREF_CURRENTLY_PLAYING_FEEDMEDIA_ID = "de.danoeh.antennapod.preferences.lastPlayedFeedMediaId";
/**
* Contains the feed id of the currently playing item if it is a FeedMedia
* object.
*/
public static final String PREF_CURRENTLY_PLAYING_FEED_ID = "de.danoeh.antennapod.preferences.lastPlayedFeedId";
/**
* Type of the media object that is currently being played. This preference
* is set to NO_MEDIA_PLAYING after playback has been completed and is set
* as soon as the 'play' button is pressed.
*/
public static final String PREF_CURRENTLY_PLAYING_MEDIA = "de.danoeh.antennapod.preferences.currentlyPlayingMedia";
/**
* Contains the id of the currently playing FeedMedia object or
* NO_MEDIA_PLAYING if the currently playing media is no FeedMedia object.
*/
public static final String PREF_CURRENTLY_PLAYING_FEEDMEDIA_ID = "de.danoeh.antennapod.preferences.lastPlayedFeedMediaId";
/** True if last played media was streamed. */
public static final String PREF_CURRENT_EPISODE_IS_STREAM = "de.danoeh.antennapod.preferences.lastIsStream";
/**
* Type of the media object that is currently being played. This preference
* is set to NO_MEDIA_PLAYING after playback has been completed and is set
* as soon as the 'play' button is pressed.
*/
public static final String PREF_CURRENTLY_PLAYING_MEDIA = "de.danoeh.antennapod.preferences.currentlyPlayingMedia";
/** True if last played media was a video. */
public static final String PREF_CURRENT_EPISODE_IS_VIDEO = "de.danoeh.antennapod.preferences.lastIsVideo";
/**
* True if last played media was streamed.
*/
public static final String PREF_CURRENT_EPISODE_IS_STREAM = "de.danoeh.antennapod.preferences.lastIsStream";
/** The current player status as int. */
/**
* True if last played media was a video.
*/
public static final String PREF_CURRENT_EPISODE_IS_VIDEO = "de.danoeh.antennapod.preferences.lastIsVideo";
/**
* The current player status as int.
*/
public static final String PREF_CURRENT_PLAYER_STATUS = "de.danoeh.antennapod.preferences.currentPlayerStatus";
/** Value of PREF_CURRENTLY_PLAYING_MEDIA if no media is playing. */
public static final long NO_MEDIA_PLAYING = -1;
/**
* Value of PREF_CURRENTLY_PLAYING_MEDIA if no media is playing.
*/
public static final long NO_MEDIA_PLAYING = -1;
/** Value of PREF_CURRENT_PLAYER_STATUS if media player status is playing. */
/**
* Value of PREF_CURRENT_PLAYER_STATUS if media player status is playing.
*/
public static final int PLAYER_STATUS_PLAYING = 1;
/** Value of PREF_CURRENT_PLAYER_STATUS if media player status is paused. */
/**
* Value of PREF_CURRENT_PLAYER_STATUS if media player status is paused.
*/
public static final int PLAYER_STATUS_PAUSED = 2;
/** Value of PREF_CURRENT_PLAYER_STATUS if media player status is neither playing nor paused. */
/**
* Value of PREF_CURRENT_PLAYER_STATUS if media player status is neither playing nor paused.
*/
public static final int PLAYER_STATUS_OTHER = 3;
private long currentlyPlayingFeedId;
private long currentlyPlayingFeedMediaId;
private long currentlyPlayingMedia;
private boolean currentEpisodeIsStream;
private boolean currentEpisodeIsVideo;
private int currentPlayerStatus;
private static PlaybackPreferences instance;
private static SharedPreferences prefs;
private static PlaybackPreferences instance;
private Context context;
private PlaybackPreferences() {
}
private PlaybackPreferences(Context context) {
this.context = context;
loadPreferences();
}
public static void init(Context context) {
instance = new PlaybackPreferences();
prefs = PreferenceManager.getDefaultSharedPreferences(context);
prefs.registerOnSharedPreferenceChangeListener(instance);
}
/**
* Sets up the UserPreferences class.
*
* @throws IllegalArgumentException
* if context is null
* */
public static void createInstance(Context context) {
if (BuildConfig.DEBUG)
Log.d(TAG, "Creating new instance of UserPreferences");
Validate.notNull(context);
instance = new PlaybackPreferences(context);
PreferenceManager.getDefaultSharedPreferences(context)
.registerOnSharedPreferenceChangeListener(instance);
}
private void loadPreferences() {
SharedPreferences sp = PreferenceManager
.getDefaultSharedPreferences(context);
currentlyPlayingFeedId = sp.getLong(PREF_CURRENTLY_PLAYING_FEED_ID, -1);
currentlyPlayingFeedMediaId = sp.getLong(
PREF_CURRENTLY_PLAYING_FEEDMEDIA_ID, NO_MEDIA_PLAYING);
currentlyPlayingMedia = sp.getLong(PREF_CURRENTLY_PLAYING_MEDIA,
NO_MEDIA_PLAYING);
currentEpisodeIsStream = sp.getBoolean(PREF_CURRENT_EPISODE_IS_STREAM, true);
currentEpisodeIsVideo = sp.getBoolean(PREF_CURRENT_EPISODE_IS_VIDEO, false);
currentPlayerStatus = sp.getInt(PREF_CURRENT_PLAYER_STATUS,
PLAYER_STATUS_OTHER);
}
@Override
public void onSharedPreferenceChanged(SharedPreferences sp, String key) {
if (key.equals(PREF_CURRENTLY_PLAYING_FEED_ID)) {
currentlyPlayingFeedId = sp.getLong(PREF_CURRENTLY_PLAYING_FEED_ID,
-1);
} else if (key.equals(PREF_CURRENTLY_PLAYING_MEDIA)) {
currentlyPlayingMedia = sp
.getLong(PREF_CURRENTLY_PLAYING_MEDIA, -1);
} else if (key.equals(PREF_CURRENT_EPISODE_IS_STREAM)) {
currentEpisodeIsStream = sp.getBoolean(PREF_CURRENT_EPISODE_IS_STREAM, true);
} else if (key.equals(PREF_CURRENT_EPISODE_IS_VIDEO)) {
currentEpisodeIsVideo = sp.getBoolean(PREF_CURRENT_EPISODE_IS_VIDEO, false);
} else if (key.equals(PREF_CURRENTLY_PLAYING_FEEDMEDIA_ID)) {
currentlyPlayingFeedMediaId = sp.getLong(
PREF_CURRENTLY_PLAYING_FEEDMEDIA_ID, NO_MEDIA_PLAYING);
}
else if (key.equals(PREF_CURRENT_PLAYER_STATUS)) {
currentPlayerStatus = sp.getInt(PREF_CURRENT_PLAYER_STATUS,
PLAYER_STATUS_OTHER);
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
if (key.equals(PREF_CURRENT_PLAYER_STATUS)) {
EventDistributor.getInstance().sendPlayerStatusUpdateBroadcast();
}
}
private static void instanceAvailable() {
if (instance == null) {
throw new IllegalStateException(
"UserPreferences was used before being set up");
}
}
}
public static long getLastPlayedFeedId() {
instanceAvailable();
return instance.currentlyPlayingFeedId;
return prefs.getLong(PREF_CURRENTLY_PLAYING_FEED_ID, -1);
}
public static long getCurrentlyPlayingMedia() {
instanceAvailable();
return instance.currentlyPlayingMedia;
return prefs.getLong(PREF_CURRENTLY_PLAYING_MEDIA, NO_MEDIA_PLAYING);
}
public static long getCurrentlyPlayingFeedMediaId() {
return instance.currentlyPlayingFeedMediaId;
return prefs.getLong(PREF_CURRENTLY_PLAYING_FEEDMEDIA_ID, NO_MEDIA_PLAYING);
}
public static boolean getCurrentEpisodeIsStream() {
instanceAvailable();
return instance.currentEpisodeIsStream;
return prefs.getBoolean(PREF_CURRENT_EPISODE_IS_STREAM, true);
}
public static boolean getCurrentEpisodeIsVideo() {
instanceAvailable();
return instance.currentEpisodeIsVideo;
return prefs.getBoolean(PREF_CURRENT_EPISODE_IS_VIDEO, false);
}
public static int getCurrentPlayerStatus() {
instanceAvailable();
return instance.currentPlayerStatus;
return prefs.getInt(PREF_CURRENT_PLAYER_STATUS, PLAYER_STATUS_OTHER);
}
}

View File

@ -5,6 +5,7 @@ import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.SystemClock;
import android.preference.PreferenceManager;
import android.support.v4.app.NotificationCompat;
import android.util.Log;
@ -272,14 +273,12 @@ public class UserPreferences {
}
public static void setPrefFastForwardSecs(int secs) {
Log.d(TAG, "setPrefFastForwardSecs(" + secs +")");
SharedPreferences.Editor editor = prefs.edit();
editor.putInt(PREF_FAST_FORWARD_SECS, secs);
editor.commit();
prefs.edit()
.putInt(PREF_FAST_FORWARD_SECS, secs)
.apply();
}
public static void setPrefRewindSecs(int secs) {
Log.d(TAG, "setPrefRewindSecs(" + secs +")");
prefs.edit()
.putInt(PREF_REWIND_SECS, secs)
.apply();
@ -314,7 +313,7 @@ public class UserPreferences {
prefs.edit()
.putString(PREF_UPDATE_INTERVAL, String.valueOf(hours))
.apply();
restartUpdateAlarm(TimeUnit.HOURS.toMillis(hours), TimeUnit.HOURS.toMillis(hours));
restartUpdateAlarm();
}
/**
@ -493,6 +492,11 @@ public class UserPreferences {
}
}
public static void restartUpdateAlarm() {
long hours = getUpdateInterval();
restartUpdateAlarm(TimeUnit.SECONDS.toMillis(10), hours);
}
/**
* Updates alarm registered with the AlarmManager service or deactivates it.
*/
@ -502,10 +506,12 @@ public class UserPreferences {
PendingIntent updateIntent = PendingIntent.getBroadcast(context, 0,
new Intent(ClientConfig.applicationCallbacks.getApplicationInstance(), FeedUpdateReceiver.class), 0);
alarmManager.cancel(updateIntent);
if (intervalMillis != 0) {
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, triggerAtMillis, intervalMillis,
if (intervalMillis > 0) {
alarmManager.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP,
SystemClock.elapsedRealtime() + triggerAtMillis,
intervalMillis,
updateIntent);
Log.d(TAG, "Changed alarm to new interval");
Log.d(TAG, "Changed alarm to new interval " + TimeUnit.MILLISECONDS.toHours(intervalMillis) + " h");
} else {
Log.d(TAG, "Automatic update was deactivated");
}

View File

@ -7,28 +7,25 @@ import android.util.Log;
import org.apache.commons.lang3.StringUtils;
import de.danoeh.antennapod.core.BuildConfig;
import de.danoeh.antennapod.core.ClientConfig;
import de.danoeh.antennapod.core.preferences.PlaybackPreferences;
import de.danoeh.antennapod.core.preferences.UserPreferences;
/** Listens for events that make it necessary to reset the update alarm. */
public class AlarmUpdateReceiver extends BroadcastReceiver {
private static final String TAG = "AlarmUpdateReceiver";
@Override
public void onReceive(Context context, Intent intent) {
if (BuildConfig.DEBUG)
Log.d(TAG, "Received intent");
Log.d(TAG, "Received intent");
if (StringUtils.equals(intent.getAction(), Intent.ACTION_BOOT_COMPLETED)) {
if (BuildConfig.DEBUG)
Log.d(TAG, "Resetting update alarm after reboot");
Log.d(TAG, "Resetting update alarm after reboot");
} else if (StringUtils.equals(intent.getAction(), Intent.ACTION_PACKAGE_REPLACED)) {
if (BuildConfig.DEBUG)
Log.d(TAG, "Resetting update alarm after app upgrade");
Log.d(TAG, "Resetting update alarm after app upgrade");
}
ClientConfig.applicationCallbacks.setUpdateInterval(UserPreferences.getUpdateInterval());
PlaybackPreferences.init(context);
UserPreferences.init(context);
UserPreferences.restartUpdateAlarm();
}
}