Merge pull request #4615 from ByteHamster/notification-channels-consistency

Use system notification settings on supported devices instead of our own
This commit is contained in:
ByteHamster 2020-11-06 14:37:43 +01:00 committed by GitHub
commit d6c00574b4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 137 additions and 52 deletions

View File

@ -1,7 +1,9 @@
package de.danoeh.antennapod.fragment.preferences; package de.danoeh.antennapod.fragment.preferences;
import android.content.Intent; import android.content.Intent;
import android.os.Build;
import android.os.Bundle; import android.os.Bundle;
import android.provider.Settings;
import androidx.appcompat.app.AppCompatActivity; import androidx.appcompat.app.AppCompatActivity;
import androidx.preference.PreferenceFragmentCompat; import androidx.preference.PreferenceFragmentCompat;
import com.bytehamster.lib.preferencesearch.SearchConfiguration; import com.bytehamster.lib.preferencesearch.SearchConfiguration;
@ -10,6 +12,7 @@ import de.danoeh.antennapod.R;
import de.danoeh.antennapod.activity.BugReportActivity; import de.danoeh.antennapod.activity.BugReportActivity;
import de.danoeh.antennapod.activity.PreferenceActivity; import de.danoeh.antennapod.activity.PreferenceActivity;
import de.danoeh.antennapod.core.util.IntentUtils; import de.danoeh.antennapod.core.util.IntentUtils;
import de.danoeh.antennapod.core.util.gui.NotificationUtils;
import de.danoeh.antennapod.fragment.preferences.about.AboutFragment; import de.danoeh.antennapod.fragment.preferences.about.AboutFragment;
public class MainPreferencesFragment extends PreferenceFragmentCompat { public class MainPreferencesFragment extends PreferenceFragmentCompat {
@ -70,10 +73,16 @@ public class MainPreferencesFragment extends PreferenceFragmentCompat {
return true; return true;
}); });
findPreference(PREF_NOTIFICATION).setOnPreferenceClickListener(preference -> { findPreference(PREF_NOTIFICATION).setOnPreferenceClickListener(preference -> {
((PreferenceActivity) getActivity()).openScreen(R.xml.preferences_notifications); if (Build.VERSION.SDK_INT >= 26) {
Intent intent = new Intent();
intent.setAction(Settings.ACTION_APP_NOTIFICATION_SETTINGS);
intent.putExtra(Settings.EXTRA_APP_PACKAGE, getActivity().getPackageName());
startActivity(intent);
} else {
((PreferenceActivity) getActivity()).openScreen(R.xml.preferences_notifications);
}
return true; return true;
}); });
findPreference(PREF_ABOUT).setOnPreferenceClickListener( findPreference(PREF_ABOUT).setOnPreferenceClickListener(
preference -> { preference -> {
getParentFragmentManager().beginTransaction().replace(R.id.content, new AboutFragment()) getParentFragmentManager().beginTransaction().replace(R.id.content, new AboutFragment())

View File

@ -2,21 +2,28 @@
<PreferenceScreen <PreferenceScreen
xmlns:android="http://schemas.android.com/apk/res/android"> xmlns:android="http://schemas.android.com/apk/res/android">
<SwitchPreferenceCompat <PreferenceCategory
android:defaultValue="true" android:title="@string/notification_group_news">
android:enabled="true" <SwitchPreferenceCompat
android:key="prefShowDownloadReport" android:defaultValue="false"
android:summary="@string/pref_showDownloadReport_sum" android:enabled="true"
android:title="@string/pref_showDownloadReport_title" /> android:key="prefShowAutoDownloadReport"
<SwitchPreferenceCompat android:summary="@string/notification_channel_episode_auto_download"
android:defaultValue="false" android:title="@string/notification_channel_auto_download" />
android:enabled="true" </PreferenceCategory>
android:key="prefShowAutoDownloadReport"
android:summary="@string/pref_showAutoDownloadReport_sum" <PreferenceCategory
android:title="@string/pref_showAutoDownloadReport_title" /> android:title="@string/notification_group_errors">
<SwitchPreferenceCompat <SwitchPreferenceCompat
android:defaultValue="true" android:defaultValue="true"
android:key="pref_gpodnet_notifications" android:enabled="true"
android:summary="@string/pref_gpodnet_notifications_sum" android:key="prefShowDownloadReport"
android:title="@string/pref_gpodnet_notifications_title" /> android:summary="@string/notification_channel_download_error_description"
android:title="@string/notification_channel_download_error" />
<SwitchPreferenceCompat
android:defaultValue="true"
android:key="pref_gpodnet_notifications"
android:summary="@string/notification_channel_sync_error_description"
android:title="@string/notification_channel_sync_error" />
</PreferenceCategory>
</PreferenceScreen> </PreferenceScreen>

View File

@ -301,10 +301,30 @@ public class UserPreferences {
* @return {@code true} if download reports are shown, {@code false} otherwise * @return {@code true} if download reports are shown, {@code false} otherwise
*/ */
public static boolean showDownloadReport() { public static boolean showDownloadReport() {
if (Build.VERSION.SDK_INT >= 26) {
return true; // System handles notification preferences
}
return prefs.getBoolean(PREF_SHOW_DOWNLOAD_REPORT, true);
}
/**
* Used for migration of the preference to system notification channels.
*/
public static boolean getShowDownloadReportRaw() {
return prefs.getBoolean(PREF_SHOW_DOWNLOAD_REPORT, true); return prefs.getBoolean(PREF_SHOW_DOWNLOAD_REPORT, true);
} }
public static boolean showAutoDownloadReport() { public static boolean showAutoDownloadReport() {
if (Build.VERSION.SDK_INT >= 26) {
return true; // System handles notification preferences
}
return prefs.getBoolean(PREF_SHOW_AUTO_DOWNLOAD_REPORT, false);
}
/**
* Used for migration of the preference to system notification channels.
*/
public static boolean getShowAutoDownloadReportRaw() {
return prefs.getBoolean(PREF_SHOW_AUTO_DOWNLOAD_REPORT, false); return prefs.getBoolean(PREF_SHOW_AUTO_DOWNLOAD_REPORT, false);
} }
@ -728,6 +748,16 @@ public class UserPreferences {
} }
public static boolean gpodnetNotificationsEnabled() { public static boolean gpodnetNotificationsEnabled() {
if (Build.VERSION.SDK_INT >= 26) {
return true; // System handles notification preferences
}
return prefs.getBoolean(PREF_GPODNET_NOTIFICATIONS, true);
}
/**
* Used for migration of the preference to system notification channels.
*/
public static boolean getGpodnetNotificationsEnabledRaw() {
return prefs.getBoolean(PREF_GPODNET_NOTIFICATIONS, true); return prefs.getBoolean(PREF_GPODNET_NOTIFICATIONS, true);
} }

View File

@ -148,7 +148,7 @@ public class DownloadServiceNotification {
id = R.id.notification_auto_download_report; id = R.id.notification_auto_download_report;
content = createAutoDownloadNotificationContent(reportQueue); content = createAutoDownloadNotificationContent(reportQueue);
} else { } else {
channelId = NotificationUtils.CHANNEL_ID_ERROR; channelId = NotificationUtils.CHANNEL_ID_DOWNLOAD_ERROR;
titleId = R.string.download_report_title; titleId = R.string.download_report_title;
iconId = R.drawable.ic_notification_sync_error; iconId = R.drawable.ic_notification_sync_error;
intent = ClientConfig.downloadServiceCallbacks.getReportNotificationContentIntent(context); intent = ClientConfig.downloadServiceCallbacks.getReportNotificationContentIntent(context);

View File

@ -485,7 +485,11 @@ public class SyncService extends Worker {
} }
private void updateErrorNotification(SyncServiceException exception) { private void updateErrorNotification(SyncServiceException exception) {
Log.d(TAG, "Posting error notification"); if (!UserPreferences.gpodnetNotificationsEnabled()) {
Log.d(TAG, "Skipping sync error notification because of user setting");
return;
}
Log.d(TAG, "Posting sync error notification");
final String description = getApplicationContext().getString(R.string.gpodnetsync_error_descr) final String description = getApplicationContext().getString(R.string.gpodnetsync_error_descr)
+ exception.getMessage(); + exception.getMessage();

View File

@ -2,28 +2,36 @@ package de.danoeh.antennapod.core.util.gui;
import android.app.NotificationChannel; import android.app.NotificationChannel;
import android.app.NotificationChannelGroup;
import android.app.NotificationManager; import android.app.NotificationManager;
import android.content.Context; import android.content.Context;
import android.os.Build; import android.os.Build;
import androidx.annotation.RequiresApi; import androidx.annotation.RequiresApi;
import de.danoeh.antennapod.core.R; import de.danoeh.antennapod.core.R;
import de.danoeh.antennapod.core.preferences.UserPreferences;
public class NotificationUtils { public class NotificationUtils {
public static final String CHANNEL_ID_USER_ACTION = "user_action"; public static final String CHANNEL_ID_USER_ACTION = "user_action";
public static final String CHANNEL_ID_DOWNLOADING = "downloading"; public static final String CHANNEL_ID_DOWNLOADING = "downloading";
public static final String CHANNEL_ID_PLAYING = "playing"; public static final String CHANNEL_ID_PLAYING = "playing";
public static final String CHANNEL_ID_ERROR = "error"; public static final String CHANNEL_ID_DOWNLOAD_ERROR = "error";
public static final String CHANNEL_ID_SYNC_ERROR = "sync_error"; public static final String CHANNEL_ID_SYNC_ERROR = "sync_error";
public static final String CHANNEL_ID_AUTO_DOWNLOAD = "auto_download"; public static final String CHANNEL_ID_AUTO_DOWNLOAD = "auto_download";
public static final String GROUP_ID_ERRORS = "group_errors";
public static final String GROUP_ID_NEWS = "group_news";
public static void createChannels(Context context) { public static void createChannels(Context context) {
if (android.os.Build.VERSION.SDK_INT < 26) { if (Build.VERSION.SDK_INT < 26) {
return; return;
} }
NotificationManager mNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE); NotificationManager mNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
if (mNotificationManager != null) { if (mNotificationManager != null) {
mNotificationManager.createNotificationChannelGroup(createGroupErrors(context));
mNotificationManager.createNotificationChannelGroup(createGroupNews(context));
mNotificationManager.createNotificationChannel(createChannelUserAction(context)); mNotificationManager.createNotificationChannel(createChannelUserAction(context));
mNotificationManager.createNotificationChannel(createChannelDownloading(context)); mNotificationManager.createNotificationChannel(createChannelDownloading(context));
mNotificationManager.createNotificationChannel(createChannelPlaying(context)); mNotificationManager.createNotificationChannel(createChannelPlaying(context));
@ -35,36 +43,43 @@ public class NotificationUtils {
@RequiresApi(api = Build.VERSION_CODES.O) @RequiresApi(api = Build.VERSION_CODES.O)
private static NotificationChannel createChannelUserAction(Context c) { private static NotificationChannel createChannelUserAction(Context c) {
NotificationChannel mChannel = new NotificationChannel(CHANNEL_ID_USER_ACTION, NotificationChannel notificationChannel = new NotificationChannel(CHANNEL_ID_USER_ACTION,
c.getString(R.string.notification_channel_user_action), NotificationManager.IMPORTANCE_HIGH); c.getString(R.string.notification_channel_user_action), NotificationManager.IMPORTANCE_HIGH);
mChannel.setDescription(c.getString(R.string.notification_channel_user_action_description)); notificationChannel.setDescription(c.getString(R.string.notification_channel_user_action_description));
return mChannel; notificationChannel.setGroup(GROUP_ID_ERRORS);
return notificationChannel;
} }
@RequiresApi(api = Build.VERSION_CODES.O) @RequiresApi(api = Build.VERSION_CODES.O)
private static NotificationChannel createChannelDownloading(Context c) { private static NotificationChannel createChannelDownloading(Context c) {
NotificationChannel mChannel = new NotificationChannel(CHANNEL_ID_DOWNLOADING, NotificationChannel notificationChannel = new NotificationChannel(CHANNEL_ID_DOWNLOADING,
c.getString(R.string.notification_channel_downloading), NotificationManager.IMPORTANCE_LOW); c.getString(R.string.notification_channel_downloading), NotificationManager.IMPORTANCE_LOW);
mChannel.setDescription(c.getString(R.string.notification_channel_downloading_description)); notificationChannel.setDescription(c.getString(R.string.notification_channel_downloading_description));
mChannel.setShowBadge(false); notificationChannel.setShowBadge(false);
return mChannel; return notificationChannel;
} }
@RequiresApi(api = Build.VERSION_CODES.O) @RequiresApi(api = Build.VERSION_CODES.O)
private static NotificationChannel createChannelPlaying(Context c) { private static NotificationChannel createChannelPlaying(Context c) {
NotificationChannel mChannel = new NotificationChannel(CHANNEL_ID_PLAYING, NotificationChannel notificationChannel = new NotificationChannel(CHANNEL_ID_PLAYING,
c.getString(R.string.notification_channel_playing), NotificationManager.IMPORTANCE_LOW); c.getString(R.string.notification_channel_playing), NotificationManager.IMPORTANCE_LOW);
mChannel.setDescription(c.getString(R.string.notification_channel_playing_description)); notificationChannel.setDescription(c.getString(R.string.notification_channel_playing_description));
mChannel.setShowBadge(false); notificationChannel.setShowBadge(false);
return mChannel; return notificationChannel;
} }
@RequiresApi(api = Build.VERSION_CODES.O) @RequiresApi(api = Build.VERSION_CODES.O)
private static NotificationChannel createChannelError(Context c) { private static NotificationChannel createChannelError(Context c) {
NotificationChannel mChannel = new NotificationChannel(CHANNEL_ID_ERROR, NotificationChannel notificationChannel = new NotificationChannel(CHANNEL_ID_DOWNLOAD_ERROR,
c.getString(R.string.notification_channel_error), NotificationManager.IMPORTANCE_HIGH); c.getString(R.string.notification_channel_download_error), NotificationManager.IMPORTANCE_HIGH);
mChannel.setDescription(c.getString(R.string.notification_channel_error_description)); notificationChannel.setDescription(c.getString(R.string.notification_channel_download_error_description));
return mChannel; notificationChannel.setGroup(GROUP_ID_ERRORS);
if (!UserPreferences.getShowDownloadReportRaw()) {
// Migration from app managed setting: disable notification
notificationChannel.setImportance(NotificationManager.IMPORTANCE_NONE);
}
return notificationChannel;
} }
@RequiresApi(api = Build.VERSION_CODES.O) @RequiresApi(api = Build.VERSION_CODES.O)
@ -72,14 +87,38 @@ public class NotificationUtils {
NotificationChannel notificationChannel = new NotificationChannel(CHANNEL_ID_SYNC_ERROR, NotificationChannel notificationChannel = new NotificationChannel(CHANNEL_ID_SYNC_ERROR,
c.getString(R.string.notification_channel_sync_error), NotificationManager.IMPORTANCE_HIGH); c.getString(R.string.notification_channel_sync_error), NotificationManager.IMPORTANCE_HIGH);
notificationChannel.setDescription(c.getString(R.string.notification_channel_sync_error_description)); notificationChannel.setDescription(c.getString(R.string.notification_channel_sync_error_description));
notificationChannel.setGroup(GROUP_ID_ERRORS);
if (!UserPreferences.getGpodnetNotificationsEnabledRaw()) {
// Migration from app managed setting: disable notification
notificationChannel.setImportance(NotificationManager.IMPORTANCE_NONE);
}
return notificationChannel; return notificationChannel;
} }
@RequiresApi(api = Build.VERSION_CODES.O) @RequiresApi(api = Build.VERSION_CODES.O)
private static NotificationChannel createChannelAutoDownload(Context c) { private static NotificationChannel createChannelAutoDownload(Context c) {
NotificationChannel mChannel = new NotificationChannel(CHANNEL_ID_AUTO_DOWNLOAD, NotificationChannel notificationChannel = new NotificationChannel(CHANNEL_ID_AUTO_DOWNLOAD,
c.getString(R.string.notification_channel_auto_download), NotificationManager.IMPORTANCE_DEFAULT); c.getString(R.string.notification_channel_auto_download), NotificationManager.IMPORTANCE_NONE);
mChannel.setDescription(c.getString(R.string.notification_channel_episode_auto_download)); notificationChannel.setDescription(c.getString(R.string.notification_channel_episode_auto_download));
return mChannel; notificationChannel.setGroup(GROUP_ID_NEWS);
if (UserPreferences.getShowAutoDownloadReportRaw()) {
// Migration from app managed setting: enable notification
notificationChannel.setImportance(NotificationManager.IMPORTANCE_DEFAULT);
}
return notificationChannel;
}
@RequiresApi(api = Build.VERSION_CODES.O)
private static NotificationChannelGroup createGroupErrors(Context c) {
return new NotificationChannelGroup(GROUP_ID_ERRORS,
c.getString(R.string.notification_group_errors));
}
@RequiresApi(api = Build.VERSION_CODES.O)
private static NotificationChannelGroup createGroupNews(Context c) {
return new NotificationChannelGroup(GROUP_ID_NEWS,
c.getString(R.string.notification_group_news));
} }
} }

View File

@ -446,8 +446,6 @@
<string name="pref_gpodnet_full_sync_title">Force full synchronization</string> <string name="pref_gpodnet_full_sync_title">Force full synchronization</string>
<string name="pref_gpodnet_full_sync_sum">Sync all subscriptions and episode states with gpodder.net.</string> <string name="pref_gpodnet_full_sync_sum">Sync all subscriptions and episode states with gpodder.net.</string>
<string name="pref_gpodnet_login_status"><![CDATA[Logged in as <i>%1$s</i> with device <i>%2$s</i>]]></string> <string name="pref_gpodnet_login_status"><![CDATA[Logged in as <i>%1$s</i> with device <i>%2$s</i>]]></string>
<string name="pref_gpodnet_notifications_title">Synchronization failed</string>
<string name="pref_gpodnet_notifications_sum">This setting does not apply to authentication errors.</string>
<string name="pref_playback_speed_sum">Customize the speeds available for variable speed playback</string> <string name="pref_playback_speed_sum">Customize the speeds available for variable speed playback</string>
<string name="pref_feed_playback_speed_sum">The speed to use when starting audio playback for episodes in this podcast</string> <string name="pref_feed_playback_speed_sum">The speed to use when starting audio playback for episodes in this podcast</string>
<string name="pref_feed_skip">Auto Skip</string> <string name="pref_feed_skip">Auto Skip</string>
@ -474,10 +472,6 @@
<string name="pref_compact_notification_buttons_dialog_error">You can only select a maximum of %1$d items.</string> <string name="pref_compact_notification_buttons_dialog_error">You can only select a maximum of %1$d items.</string>
<string name="pref_lockscreen_background_title">Set Lockscreen Background</string> <string name="pref_lockscreen_background_title">Set Lockscreen Background</string>
<string name="pref_lockscreen_background_sum">Set the lockscreen background to the current episode\'s image. As a side effect, this will also show the image in third party apps.</string> <string name="pref_lockscreen_background_sum">Set the lockscreen background to the current episode\'s image. As a side effect, this will also show the image in third party apps.</string>
<string name="pref_showDownloadReport_title">Download failed</string>
<string name="pref_showDownloadReport_sum">If downloads fail, generate a report that shows the details of the failure.</string>
<string name="pref_showAutoDownloadReport_title">Automatic download completed</string>
<string name="pref_showAutoDownloadReport_sum">Show a notification for automatically downloaded episodes.</string>
<string name="pref_expand_notify_unsupport_toast">Android versions before 4.1 do not support expanded notifications.</string> <string name="pref_expand_notify_unsupport_toast">Android versions before 4.1 do not support expanded notifications.</string>
<string name="pref_enqueue_location_title">Enqueue Location</string> <string name="pref_enqueue_location_title">Enqueue Location</string>
<string name="pref_enqueue_location_sum">Add episodes to: %1$s</string> <string name="pref_enqueue_location_sum">Add episodes to: %1$s</string>
@ -850,17 +844,19 @@
<string name="cast_failed_media_error_skipping">Error playing media. Skipping&#8230;</string> <string name="cast_failed_media_error_skipping">Error playing media. Skipping&#8230;</string>
<!-- Notification channels --> <!-- Notification channels -->
<string name="notification_group_errors">Errors</string>
<string name="notification_group_news">News</string>
<string name="notification_channel_user_action">Action required</string> <string name="notification_channel_user_action">Action required</string>
<string name="notification_channel_user_action_description">Shown if your action is required, for example if you need to enter a password.</string> <string name="notification_channel_user_action_description">Shown if your action is required, for example if you need to enter a password.</string>
<string name="notification_channel_downloading">Downloading</string> <string name="notification_channel_downloading">Downloading</string>
<string name="notification_channel_downloading_description">Shown while currently downloading.</string> <string name="notification_channel_downloading_description">Shown while currently downloading.</string>
<string name="notification_channel_playing">Currently playing</string> <string name="notification_channel_playing">Currently playing</string>
<string name="notification_channel_playing_description">Allows to control playback. This is the main notification you see while playing a podcast.</string> <string name="notification_channel_playing_description">Allows to control playback. This is the main notification you see while playing a podcast.</string>
<string name="notification_channel_error">Errors</string> <string name="notification_channel_download_error">Download failed</string>
<string name="notification_channel_error_description">Shown if something went wrong, for example if download or feed update fails.</string> <string name="notification_channel_download_error_description">Shown when download or feed update fails.</string>
<string name="notification_channel_sync_error">Synchronization Errors</string> <string name="notification_channel_sync_error">Synchronization failed</string>
<string name="notification_channel_sync_error_description">Shown when gpodder synchronization fails.</string> <string name="notification_channel_sync_error_description">Shown when gpodder synchronization fails.</string>
<string name="notification_channel_auto_download">Auto Downloads</string> <string name="notification_channel_auto_download">Automatic download completed</string>
<string name="notification_channel_episode_auto_download">Shown when episodes have been automatically downloaded.</string> <string name="notification_channel_episode_auto_download">Shown when episodes have been automatically downloaded.</string>
<!-- Widget settings --> <!-- Widget settings -->