Option to disable some notifications on pre-O devices

Thanks to an anonymous contributor
This commit is contained in:
xynngh 2020-05-16 20:49:50 +04:00
parent 1d80f22bc9
commit ad70e3e871
6 changed files with 77 additions and 5 deletions

View File

@ -41,7 +41,7 @@ public class NotificationHelper {
private static final String CHANNEL_ID_UNKNOWN = "unknown_calls";
private static final String CHANNEL_ID_NEGATIVE = "negative_calls";
private static final String CHANNEL_ID_BLOCKED_INFO = "blocked_info";
public static final String CHANNEL_ID_TASKS = "tasks";
private static final String CHANNEL_ID_TASKS = "tasks";
public static void notify(Context context, int id, Notification notification) {
NotificationManagerCompat.from(context).notify(id, notification);
@ -52,9 +52,22 @@ public class NotificationHelper {
}
public static void showIncomingCallNotification(Context context, NumberInfo numberInfo) {
Notification notification = createIncomingCallNotification(context, numberInfo);
NotificationWithInfo notificationWithInfo = createIncomingCallNotification(context, numberInfo);
notify(context, NOTIFICATION_TAG_INCOMING_CALL, NOTIFICATION_ID_INCOMING_CALL, notification);
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O) {
if (CHANNEL_ID_KNOWN.equals(notificationWithInfo.channelId)) {
if (!App.getSettings().getNotificationsForKnownCallers()) {
return;
}
} else if (CHANNEL_ID_UNKNOWN.equals(notificationWithInfo.channelId)) {
if (!App.getSettings().getNotificationsForUnknownCallers()) {
return;
}
}
}
notify(context, NOTIFICATION_TAG_INCOMING_CALL, NOTIFICATION_ID_INCOMING_CALL,
notificationWithInfo.notification);
}
public static void hideIncomingCallNotification(Context context) {
@ -82,7 +95,8 @@ public class NotificationHelper {
.setContentTitle(title).build();
}
private static Notification createIncomingCallNotification(Context context, NumberInfo numberInfo) {
private static NotificationWithInfo createIncomingCallNotification(
Context context, NumberInfo numberInfo) {
boolean unknown = false;
String channelId;
String title;
@ -142,7 +156,7 @@ public class NotificationHelper {
addCallNotificationIntents(context, builder, numberInfo);
return builder.build();
return new NotificationWithInfo(builder.build(), channelId);
}
private static Notification createBlockedCallNotification(Context context, NumberInfo numberInfo) {
@ -276,4 +290,14 @@ public class NotificationHelper {
}
}
private static class NotificationWithInfo {
private Notification notification;
private String channelId;
NotificationWithInfo(Notification notification, String channelId) {
this.notification = notification;
this.channelId = channelId;
}
}
}

View File

@ -9,6 +9,8 @@ public class Settings extends GenericSettings {
public static final String PREF_INCOMING_CALL_NOTIFICATIONS = "incomingCallNotifications";
public static final String PREF_BLOCK_CALLS = "blockCalls";
public static final String PREF_USE_CONTACTS = "useContacts";
public static final String PREF_NOTIFICATIONS_KNOWN = "showNotificationsForKnownCallers";
public static final String PREF_NOTIFICATIONS_UNKNOWN = "showNotificationsForUnknownCallers";
public static final String PREF_LAST_UPDATE_TIME = "lastUpdateTime";
public static final String PREF_LAST_UPDATE_CHECK_TIME = "lastUpdateCheckTime";
@ -74,6 +76,22 @@ public class Settings extends GenericSettings {
setBoolean(PREF_USE_CONTACTS, use);
}
public boolean getNotificationsForKnownCallers() {
return getBoolean(PREF_NOTIFICATIONS_KNOWN);
}
public void setNotificationsForKnownCallers(boolean show) {
setBoolean(PREF_NOTIFICATIONS_KNOWN, show);
}
public boolean getNotificationsForUnknownCallers() {
return getBoolean(PREF_NOTIFICATIONS_UNKNOWN);
}
public void setNotificationsForUnknownCallers(boolean show) {
setBoolean(PREF_NOTIFICATIONS_UNKNOWN, show);
}
public long getLastUpdateTime() {
return getLong(PREF_LAST_UPDATE_TIME, 0);
}

View File

@ -1,5 +1,6 @@
package dummydomain.yetanothercallblocker;
import android.os.Build;
import android.os.Bundle;
import androidx.appcompat.app.ActionBar;
@ -35,6 +36,7 @@ public class SettingsActivity extends AppCompatActivity {
public static class SettingsFragment extends PreferenceFragmentCompat {
private static final String PREF_AUTO_UPDATE_ENABLED = "autoUpdateEnabled";
private static final String PREF_CATEGORY_NOTIFICATIONS = "categoryNotifications";
private final UpdateScheduler updateScheduler = UpdateScheduler.get(App.getInstance());
@ -81,6 +83,11 @@ public class SettingsActivity extends AppCompatActivity {
return true;
});
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
Preference category = requireNonNull(findPreference(PREF_CATEGORY_NOTIFICATIONS));
category.setVisible(false);
}
PreferenceScreen preferenceScreen = getPreferenceScreen();
int count = preferenceScreen.getPreferenceCount();
for (int i = 0; i < count; i++) {

View File

@ -72,4 +72,9 @@
<string name="open_settings_activity">Настройки</string>
<string name="title_settings_activity">Настройки</string>
<string name="settings_category_main">Основные</string>
<string name="settings_category_notifications_incoming_calls">Уведомления на входящие</string>
<string name="show_notifications_for_known_callers">Уведомления для известных звонящих</string>
<string name="show_notifications_for_known_callers_summary">Показывать уведомления для известных звонящих (номеров из телефонной книги)</string>
<string name="show_notifications_for_unknown_callers">Уведомления для неизвестных звонящих</string>
<string name="show_notifications_for_unknown_callers_summary">Показывать уведомления для неизвестных звонящих (отсутствующих в телефонной книге и в базе номеров)</string>
</resources>

View File

@ -76,6 +76,11 @@
<string name="open_settings_activity">Settings</string>
<string name="title_settings_activity">Settings</string>
<string name="settings_category_main">Main</string>
<string name="settings_category_notifications_incoming_calls">Incoming calls notifications</string>
<string name="show_notifications_for_known_callers">Notifications for known callers</string>
<string name="show_notifications_for_known_callers_summary">Display notifications for known callers (numbers in Contacts)</string>
<string name="show_notifications_for_unknown_callers">Notifications for unknown callers</string>
<string name="show_notifications_for_unknown_callers_summary">Display notifications for unknown callers (neither in Contacts nor in number database)</string>
<string name="incoming_call_notifications">Incoming call notifications</string>
<string name="incoming_call_notifications_summary">Displays a notification with phone number summary (rating, reviews count, category) during incoming calls</string>

View File

@ -22,4 +22,17 @@
app:title="@string/use_contacts" />
</PreferenceCategory>
<PreferenceCategory
app:key="categoryNotifications"
app:title="@string/settings_category_notifications_incoming_calls">
<SwitchPreferenceCompat
app:key="showNotificationsForKnownCallers"
app:summary="@string/show_notifications_for_known_callers_summary"
app:title="@string/show_notifications_for_known_callers" />
<SwitchPreferenceCompat
app:key="showNotificationsForUnknownCallers"
app:summary="@string/show_notifications_for_unknown_callers_summary"
app:title="@string/show_notifications_for_unknown_callers" />
</PreferenceCategory>
</PreferenceScreen>