Add last added playlist interval preference (#107)
This commit is contained in:
parent
abb2c21add
commit
c20262eb03
9 changed files with 175 additions and 18 deletions
|
|
@ -0,0 +1,100 @@
|
|||
package com.kabouzeid.gramophone.util;
|
||||
|
||||
import java.util.Calendar;
|
||||
import java.util.GregorianCalendar;
|
||||
|
||||
/**
|
||||
* @author Karim Abou Zeid (kabouzeid)
|
||||
*/
|
||||
public class CalendarUtil {
|
||||
private static final long MS_PER_MINUTE = 60 * 1000;
|
||||
private static final long MS_PER_DAY = 24 * 60 * MS_PER_MINUTE;
|
||||
|
||||
private Calendar calendar;
|
||||
|
||||
public CalendarUtil() {
|
||||
this.calendar = Calendar.getInstance();
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the time elapsed so far today in seconds.
|
||||
*
|
||||
* @return Time elapsed today in seconds.
|
||||
*/
|
||||
public long getElapsedToday() {
|
||||
// Time elapsed so far today
|
||||
return (calendar.get(Calendar.HOUR_OF_DAY) * 60 + calendar.get(Calendar.MINUTE)) * MS_PER_MINUTE;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the time elapsed so far this week in milliseconds.
|
||||
*
|
||||
* @return Time elapsed this week in milliseconds.
|
||||
*/
|
||||
public long getElapsedWeek() {
|
||||
// Today + days passed
|
||||
return getElapsedToday() + ((calendar.get(Calendar.DAY_OF_WEEK) - 1 - calendar.getFirstDayOfWeek()) * MS_PER_DAY);
|
||||
}
|
||||
|
||||
/**
|
||||
* 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--;
|
||||
}
|
||||
|
||||
final Calendar monthCal = new GregorianCalendar(year, month, 1);
|
||||
final int daysInMonth = monthCal.getActualMaximum(Calendar.DAY_OF_MONTH);
|
||||
|
||||
elapsed += daysInMonth * 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;
|
||||
while (month > Calendar.JANUARY) {
|
||||
final Calendar monthCal = new GregorianCalendar(calendar.get(Calendar.YEAR), month, 1);
|
||||
final int daysInMonth = monthCal.getActualMaximum(Calendar.DAY_OF_MONTH);
|
||||
|
||||
elapsed += daysInMonth * MS_PER_DAY;
|
||||
|
||||
month--;
|
||||
}
|
||||
|
||||
return elapsed;
|
||||
}
|
||||
}
|
||||
|
|
@ -49,7 +49,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";
|
||||
|
|
@ -206,15 +206,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() {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue