From 4d47ba640550714968fbb4e5c07ab913474db6c0 Mon Sep 17 00:00:00 2001 From: saqura Date: Sun, 3 Apr 2016 00:32:55 +0200 Subject: [PATCH] Add dialog to choose lock screen playback buttons This adds a dialog to choose the playback buttons on the lock screen notification. It only allows selecting a maximum of two values, because the lock screen notification can only display up to 3 buttons and the play/pause button is always included. It defaults to additionally show the skip button. The minimum sdk has been changed back to 10. --- .../preferences/PreferenceController.java | 60 +++++++++++++++++++ app/src/main/res/xml/preferences.xml | 12 ++-- build.gradle | 2 +- .../core/preferences/UserPreferences.java | 19 ++++-- .../service/playback/PlaybackService.java | 9 ++- core/src/main/res/values/arrays.xml | 14 ++--- core/src/main/res/values/strings.xml | 6 +- 7 files changed, 93 insertions(+), 29 deletions(-) diff --git a/app/src/main/java/de/danoeh/antennapod/preferences/PreferenceController.java b/app/src/main/java/de/danoeh/antennapod/preferences/PreferenceController.java index 116272578..0e79b46ea 100644 --- a/app/src/main/java/de/danoeh/antennapod/preferences/PreferenceController.java +++ b/app/src/main/java/de/danoeh/antennapod/preferences/PreferenceController.java @@ -6,6 +6,7 @@ import android.app.Activity; import android.app.TimePickerDialog; import android.content.ActivityNotFoundException; import android.content.Context; +import android.content.DialogInterface; import android.content.Intent; import android.content.SharedPreferences; import android.content.pm.PackageManager; @@ -221,6 +222,12 @@ public class PreferenceController implements SharedPreferences.OnSharedPreferenc return true; }); + ui.findPreference(UserPreferences.PREF_NOTIFICATION_BUTTONS) + .setOnPreferenceClickListener(preference -> { + showNotificationButtonsDialog(); + return true; + }); + ui.findPreference(UserPreferences.PREF_UPDATE_INTERVAL) .setOnPreferenceClickListener(preference -> { showUpdateIntervalTimePreferencesDialog(); @@ -735,6 +742,59 @@ public class PreferenceController implements SharedPreferences.OnSharedPreferenc builder.create().show(); } + private void showNotificationButtonsDialog() { + final Context context = ui.getActivity(); + final List preferredButtons = UserPreferences.getNotificationButtons(); + final String[] allButtonNames = context.getResources().getStringArray( + R.array.notification_buttons_options); + final String[] allButtonIDs = context.getResources().getStringArray( + R.array.notification_buttons_values); + boolean[] checked = new boolean[allButtonIDs.length]; // booleans default to false in java + + for(int i=0; i < allButtonIDs.length; i++) { + String id = allButtonIDs[i]; + if(preferredButtons.contains(id)) { + checked[i] = true; + } + } + + AlertDialog.Builder builder = new AlertDialog.Builder(context); + builder.setTitle(String.format(context.getResources().getString( + R.string.pref_notification_buttons_dialog_title), 2)); + builder.setMultiChoiceItems(allButtonNames, checked,new DialogInterface.OnMultiChoiceClickListener() { + int count = preferredButtons.size(); + + public void onClick(DialogInterface dialog, int which, boolean isChecked) { + checked[which] = isChecked; + if (isChecked) { + if (count < 2) { + preferredButtons.add(allButtonIDs[which]); + count++; + } else { + // Only allow a maximum of two selections. This is because the notification + // on the lock screen can only display 3 buttons, and the play/pause button + // is always included. + checked[which] = false; + ((AlertDialog) dialog).getListView().setItemChecked(which, false); + Toast.makeText( + context, + String.format(context.getResources().getString( + R.string.pref_notification_buttons_dialog_error), 2), + Toast.LENGTH_SHORT).show(); + } + } else { + preferredButtons.remove(allButtonIDs[which]); + count--; + } + } + }); + builder.setPositiveButton(R.string.confirm_label, (dialog, which) -> { + UserPreferences.setNotificationButtons(preferredButtons); + }); + builder.setNegativeButton(R.string.cancel_label, null); + builder.create().show(); + } + // CHOOSE DATA FOLDER private void requestPermission() { diff --git a/app/src/main/res/xml/preferences.xml b/app/src/main/res/xml/preferences.xml index e40de7c46..b3707f023 100644 --- a/app/src/main/res/xml/preferences.xml +++ b/app/src/main/res/xml/preferences.xml @@ -55,14 +55,10 @@ android:key="prefPersistNotify" android:summary="@string/pref_persistNotify_sum" android:title="@string/pref_persistNotify_title"/> - + (Arrays.asList(TextUtils.split(hiddenItems, ","))); } + public static List getNotificationButtons() { + String hiddenItems = prefs.getString(PREF_NOTIFICATION_BUTTONS, "skip"); + return new ArrayList<>(Arrays.asList(TextUtils.split(hiddenItems, ","))); + } + public static int getFeedOrder() { String value = prefs.getString(PREF_DRAWER_FEED_ORDER, "0"); return Integer.parseInt(value); @@ -182,10 +186,6 @@ public class UserPreferences { return prefs.getBoolean(PREF_SHOW_SUBSCRIPTIONS_IN_DRAWER, true); } - public static Set getPrioritisedNotificationButtons() { - return prefs.getStringSet(PREF_PRIORITISED_NOTIFICATION_BUTTONS, null); - } - /** * Returns notification priority. * @@ -533,6 +533,13 @@ public class UserPreferences { .apply(); } + public static void setNotificationButtons(List items) { + String str = TextUtils.join(",", items); + prefs.edit() + .putString(PREF_NOTIFICATION_BUTTONS, str) + .apply(); + } + public static void setQueueLocked(boolean locked) { prefs.edit() .putBoolean(PREF_QUEUE_LOCKED, locked) diff --git a/core/src/main/java/de/danoeh/antennapod/core/service/playback/PlaybackService.java b/core/src/main/java/de/danoeh/antennapod/core/service/playback/PlaybackService.java index 7a7c2d635..39dc35535 100644 --- a/core/src/main/java/de/danoeh/antennapod/core/service/playback/PlaybackService.java +++ b/core/src/main/java/de/danoeh/antennapod/core/service/playback/PlaybackService.java @@ -30,7 +30,6 @@ import android.widget.Toast; import com.bumptech.glide.Glide; import java.util.List; -import java.util.Set; import de.danoeh.antennapod.core.ClientConfig; import de.danoeh.antennapod.core.R; @@ -859,7 +858,7 @@ public class PlaybackService extends Service { .setPriority(UserPreferences.getNotifyPriority()); // set notification priority IntList compactActionList = new IntList(); - Set prioritisedButtons = UserPreferences.getPrioritisedNotificationButtons(); + final List notificationButtons = UserPreferences.getNotificationButtons(); int numActions = 0; // we start and 0 and then increment by 1 for each call to addAction // always let them rewind @@ -868,7 +867,7 @@ public class PlaybackService extends Service { notificationBuilder.addAction(android.R.drawable.ic_media_rew, getString(R.string.rewind_label), rewindButtonPendingIntent); - if(prioritisedButtons.contains("0")) { + if(notificationButtons.contains("rewind")) { // show the rewind button even on the lock screen compactActionList.add(numActions++); } else { @@ -898,7 +897,7 @@ public class PlaybackService extends Service { notificationBuilder.addAction(android.R.drawable.ic_media_ff, getString(R.string.fast_forward_label), ffButtonPendingIntent); - if(prioritisedButtons.contains("1")) { + if(notificationButtons.contains("fastforward")) { // show the fast forward button even on the lock screen compactActionList.add(numActions++); } else { @@ -911,7 +910,7 @@ public class PlaybackService extends Service { notificationBuilder.addAction(android.R.drawable.ic_media_next, getString(R.string.skip_episode_label), skipButtonPendingIntent); - if(prioritisedButtons.contains("2")) { + if(notificationButtons.contains("skip")) { // show the skip button even on the lock screen compactActionList.add(numActions++); } else { diff --git a/core/src/main/res/values/arrays.xml b/core/src/main/res/values/arrays.xml index ea2bb7a52..04e0d97cd 100644 --- a/core/src/main/res/values/arrays.xml +++ b/core/src/main/res/values/arrays.xml @@ -212,18 +212,18 @@ 500 - + @string/rewind_label @string/fast_forward_label @string/skip_episode_label - - 0 - 1 - 2 + + rewind + fastforward + skip - - 2 + + skip diff --git a/core/src/main/res/values/strings.xml b/core/src/main/res/values/strings.xml index b2f4e11f9..25da5ec1d 100644 --- a/core/src/main/res/values/strings.xml +++ b/core/src/main/res/values/strings.xml @@ -384,8 +384,10 @@ Always expand the notification to show playback buttons. Persistent Playback Controls Keep notification and lockscreen controls when playback is paused. - Prioritise Notification Buttons - Change the playback buttons on the lock screen notification. + Select Notification Buttons + Change the playback buttons on the lock screen notification. + Select a maximum of %1$d items + You can only select a maximum of %1$d items. Show Subscriptions Show subscription list directly in navigation drawer Set Lockscreen Background