diff --git a/app/src/main/java/com/kabouzeid/gramophone/adapter/PlaylistAdapter.java b/app/src/main/java/com/kabouzeid/gramophone/adapter/PlaylistAdapter.java index 868f4448..9a9b73e1 100644 --- a/app/src/main/java/com/kabouzeid/gramophone/adapter/PlaylistAdapter.java +++ b/app/src/main/java/com/kabouzeid/gramophone/adapter/PlaylistAdapter.java @@ -26,6 +26,7 @@ import com.kabouzeid.gramophone.model.AbsCustomPlaylist; import com.kabouzeid.gramophone.model.Playlist; import com.kabouzeid.gramophone.model.Song; import com.kabouzeid.gramophone.model.smartplaylist.AbsSmartPlaylist; +import com.kabouzeid.gramophone.model.smartplaylist.LastAddedPlaylist; import com.kabouzeid.gramophone.util.MusicUtil; import com.kabouzeid.gramophone.util.NavigationUtil; @@ -192,13 +193,16 @@ public class PlaylistAdapter extends AbsMultiSelectAdapter 0) { + elapsed += passedWeekdays * MS_PER_DAY; + } + + return elapsed; + } + + /** + * Returns the time elapsed so far this month in milliseconds. + * + * @return Time elapsed this month in milliseconds. + */ + public long getElapsedMonth() { + // Today + rest of this month + return getElapsedToday() + + ((calendar.get(Calendar.DAY_OF_MONTH) - 1) * MS_PER_DAY); + } + + /** + * Returns the time elapsed so far this month and the last numMonths months in milliseconds. + * + * @param numMonths Additional number of months prior to the current month to calculate. + * @return Time elapsed this month and the last numMonths months in milliseconds. + */ + public long getElapsedMonths(int numMonths) { + // Today + rest of this month + long elapsed = getElapsedMonth(); + + // Previous numMonths months + int month = calendar.get(Calendar.MONTH); + int year = calendar.get(Calendar.YEAR); + for (int i = 0; i < numMonths; i++) { + month--; + + if (month < Calendar.JANUARY) { + month = Calendar.DECEMBER; + year--; + } + + elapsed += getDaysInMonth(year, month) * MS_PER_DAY; + } + + return elapsed; + } + + /** + * Returns the time elapsed so far this year in milliseconds. + * + * @return Time elapsed this year in milliseconds. + */ + public long getElapsedYear() { + // Today + rest of this month + previous months until January + long elapsed = getElapsedMonth(); + + int month = calendar.get(Calendar.MONTH) - 1; + int year = calendar.get(Calendar.YEAR); + while (month > Calendar.JANUARY) { + elapsed += getDaysInMonth(year, month) * MS_PER_DAY; + + month--; + } + + return elapsed; + } + + /** + * Gets the number of days for the given month in the given year. + * + * @param year The year. + * @param month The month (1 - 12). + * @return The days in that month/year. + */ + private int getDaysInMonth(int year, int month) { + final Calendar monthCal = new GregorianCalendar(calendar.get(Calendar.YEAR), month, 1); + return monthCal.getActualMaximum(Calendar.DAY_OF_MONTH); + } +} diff --git a/app/src/main/java/com/kabouzeid/gramophone/util/PreferenceUtil.java b/app/src/main/java/com/kabouzeid/gramophone/util/PreferenceUtil.java index 69800a43..61e71660 100644 --- a/app/src/main/java/com/kabouzeid/gramophone/util/PreferenceUtil.java +++ b/app/src/main/java/com/kabouzeid/gramophone/util/PreferenceUtil.java @@ -51,7 +51,7 @@ public final class PreferenceUtil { public static final String AUDIO_DUCKING = "audio_ducking"; public static final String GAPLESS_PLAYBACK = "gapless_playback"; - public static final String LAST_ADDED_CUTOFF_TIMESTAMP = "last_added_cutoff_timestamp"; + public static final String LAST_ADDED_CUTOFF = "last_added_interval"; public static final String ALBUM_ART_ON_LOCKSCREEN = "album_art_on_lockscreen"; public static final String BLURRED_ALBUM_ART = "blurred_album_art"; @@ -218,15 +218,34 @@ public final class PreferenceUtil { return mPreferences.getString(SONG_SORT_ORDER, SortOrder.SongSortOrder.SONG_A_Z); } - public long getLastAddedCutOffTimestamp() { - return mPreferences.getLong(LAST_ADDED_CUTOFF_TIMESTAMP, 0L); - } + public long getLastAddedCutoff() { + final CalendarUtil calendarUtil = new CalendarUtil(); + long interval; - @SuppressLint("CommitPrefEdits") - public void setLastAddedCutoffTimestamp(final long timestamp) { - final SharedPreferences.Editor editor = mPreferences.edit(); - editor.putLong(LAST_ADDED_CUTOFF_TIMESTAMP, timestamp); - editor.commit(); + switch (mPreferences.getString(LAST_ADDED_CUTOFF, "")) { + case "today": + interval = calendarUtil.getElapsedToday(); + break; + + case "this_week": + interval = calendarUtil.getElapsedWeek(); + break; + + case "past_three_months": + interval = calendarUtil.getElapsedMonths(3); + break; + + case "this_year": + interval = calendarUtil.getElapsedYear(); + break; + + case "this_month": + default: + interval = calendarUtil.getElapsedMonth(); + break; + } + + return (System.currentTimeMillis() - interval) / 1000; } public int getLastSleepTimerValue() { diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index b151bfc7..50f7c06e 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -110,6 +110,11 @@ Always Only on Wi-Fi Never + Today + This week + This month + Past 3 months + This year Equalizer Colors Now playing @@ -118,6 +123,7 @@ General Images Lockscreen + Playlists Notification Colored navigation bar Colored app shortcuts @@ -137,6 +143,7 @@ Show card below playback controls Colored playback controls Reduce volume on focus loss + Last added playlist interval No equalizer found. "Play a song first, then try again." Delete diff --git a/app/src/main/res/values/strings_activity_settings.xml b/app/src/main/res/values/strings_activity_settings.xml index 33eba14a..2f35d635 100644 --- a/app/src/main/res/values/strings_activity_settings.xml +++ b/app/src/main/res/values/strings_activity_settings.xml @@ -39,4 +39,20 @@ never + + @string/today + @string/this_week + @string/this_month + @string/past_three_months + @string/this_year + + + + today + this_week + this_month + past_three_months + this_year + + diff --git a/app/src/main/res/xml/pref_playlists.xml b/app/src/main/res/xml/pref_playlists.xml new file mode 100644 index 00000000..8ad96828 --- /dev/null +++ b/app/src/main/res/xml/pref_playlists.xml @@ -0,0 +1,16 @@ + + + + + + + + +