Merge pull request #143 from arkon/feat/last_added_interval

Add last added playlist interval preference
This commit is contained in:
Lincoln 2017-06-05 22:27:59 -04:00 committed by GitHub
commit 3df7951a26
9 changed files with 192 additions and 18 deletions

View file

@ -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<PlaylistAdapter.ViewH
menu.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
PopupMenu popupMenu = new PopupMenu(activity, view);
final Playlist playlist = dataSet.get(getAdapterPosition());
final PopupMenu popupMenu = new PopupMenu(activity, view);
popupMenu.inflate(getItemViewType() == SMART_PLAYLIST ? R.menu.menu_item_smart_playlist : R.menu.menu_item_playlist);
if (playlist instanceof LastAddedPlaylist) {
popupMenu.getMenu().findItem(R.id.action_clear_playlist).setVisible(false);
}
popupMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(@NonNull MenuItem item) {
if (item.getItemId() == R.id.action_clear_playlist) {
Playlist playlist = dataSet.get(getAdapterPosition());
if (playlist instanceof AbsSmartPlaylist) {
ClearSmartPlaylistDialog.create((AbsSmartPlaylist) playlist).show(activity.getSupportFragmentManager(), "CLEAR_SMART_PLAYLIST_" + playlist.name);
return true;

View file

@ -18,12 +18,7 @@ public class LastAddedLoader {
}
public static Cursor makeLastAddedCursor(@NonNull final Context context) {
long fourWeeksAgo = (System.currentTimeMillis() / 1000) - (4 * 3600 * 24 * 7);
// possible saved timestamp caused by user "clearing" the last added playlist
long cutoff = PreferenceUtil.getInstance(context).getLastAddedCutOffTimestamp() / 1000;
if (cutoff < fourWeeksAgo) {
cutoff = fourWeeksAgo;
}
long cutoff = PreferenceUtil.getInstance(context).getLastAddedCutoff();
return SongLoader.makeSongCursor(
context,

View file

@ -28,7 +28,6 @@ public class LastAddedPlaylist extends AbsSmartPlaylist {
@Override
public void clear(@NonNull Context context) {
PreferenceUtil.getInstance(context).setLastAddedCutoffTimestamp(System.currentTimeMillis());
}

View file

@ -134,6 +134,7 @@ public class SettingsActivity extends AbsBaseActivity implements ColorChooserDia
addPreferencesFromResource(R.xml.pref_images);
addPreferencesFromResource(R.xml.pref_lockscreen);
addPreferencesFromResource(R.xml.pref_audio);
addPreferencesFromResource(R.xml.pref_playlists);
}
@Nullable

View file

@ -0,0 +1,117 @@
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 milliseconds.
*
* @return Time elapsed today in milliseconds.
*/
public long getElapsedToday() {
// Time elapsed so far today
return (calendar.get(Calendar.HOUR_OF_DAY) * 60 + calendar.get(Calendar.MINUTE)) * MS_PER_MINUTE
+ calendar.get(Calendar.SECOND) * 1000
+ calendar.get(Calendar.MILLISECOND);
}
/**
* Returns the time elapsed so far this week in milliseconds.
*
* @return Time elapsed this week in milliseconds.
*/
public long getElapsedWeek() {
// Today + days passed this week
long elapsed = getElapsedToday();
final int passedWeekdays = calendar.get(Calendar.DAY_OF_WEEK) - 1 - calendar.getFirstDayOfWeek();
if (passedWeekdays > 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);
}
}

View file

@ -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() {

View file

@ -110,6 +110,11 @@
<string name="always">Always</string>
<string name="only_on_wifi">Only on Wi-Fi</string>
<string name="never">Never</string>
<string name="today">Today</string>
<string name="this_week">This week</string>
<string name="this_month">This month</string>
<string name="past_three_months">Past 3 months</string>
<string name="this_year">This year</string>
<string name="equalizer">Equalizer</string>
<string name="pref_header_colors">Colors</string>
<string name="pref_header_now_playing_screen">Now playing</string>
@ -118,6 +123,7 @@
<string name="pref_header_general">General</string>
<string name="pref_header_images">Images</string>
<string name="pref_header_lockscreen">Lockscreen</string>
<string name="pref_header_playlists">Playlists</string>
<string name="pref_header_notification">Notification</string>
<string name="pref_title_navigation_bar">Colored navigation bar</string>
<string name="pref_title_app_shortcuts">Colored app shortcuts</string>
@ -137,6 +143,7 @@
<string name="pref_title_playback_controller_card_now_playing">Show card below playback controls</string>
<string name="pref_title_colored_playback_controls_now_playing">Colored playback controls</string>
<string name="pref_title_audio_ducking">Reduce volume on focus loss</string>
<string name="pref_title_last_added_interval">Last added playlist interval</string>
<string name="no_equalizer">No equalizer found.</string>
<string name="no_audio_ID">"Play a song first, then try again."</string>
<string name="delete_action">Delete</string>

View file

@ -39,4 +39,20 @@
<item>never</item>
</string-array>
<string-array name="pref_playlists_last_added_interval_titles">
<item>@string/today</item>
<item>@string/this_week</item>
<item>@string/this_month</item>
<item>@string/past_three_months</item>
<item>@string/this_year</item>
</string-array>
<string-array name="pref_playlists_last_added_interval_values">
<item>today</item>
<item>this_week</item>
<item>this_month</item>
<item>past_three_months</item>
<item>this_year</item>
</string-array>
</resources>

View file

@ -0,0 +1,16 @@
<android.support.v7.preference.PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
<com.kabouzeid.appthemehelper.common.prefs.supportv7.ATEPreferenceCategory android:title="@string/pref_header_playlists">
<com.kabouzeid.appthemehelper.common.prefs.supportv7.ATEListPreference
android:defaultValue="this_month"
android:entries="@array/pref_playlists_last_added_interval_titles"
android:entryValues="@array/pref_playlists_last_added_interval_values"
android:key="last_added_interval"
android:negativeButtonText="@null"
android:positiveButtonText="@null"
android:title="@string/pref_title_last_added_interval" />
</com.kabouzeid.appthemehelper.common.prefs.supportv7.ATEPreferenceCategory>
</android.support.v7.preference.PreferenceScreen>