2020-10-09 12:29:30 +00:00
|
|
|
package eu.faircode.email;
|
|
|
|
|
|
|
|
/*
|
|
|
|
This file is part of FairEmail.
|
|
|
|
|
|
|
|
FairEmail is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
FairEmail is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with FairEmail. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
2022-01-01 08:46:36 +00:00
|
|
|
Copyright 2018-2022 by Marcel Bokhorst (M66B)
|
2020-10-09 12:29:30 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
import android.app.Notification;
|
|
|
|
import android.app.NotificationChannel;
|
2020-10-09 13:22:38 +00:00
|
|
|
import android.app.NotificationChannelGroup;
|
|
|
|
import android.app.NotificationManager;
|
2020-10-09 12:29:30 +00:00
|
|
|
import android.content.Context;
|
2020-10-09 13:22:38 +00:00
|
|
|
import android.graphics.Color;
|
2020-10-09 12:29:30 +00:00
|
|
|
import android.media.Ringtone;
|
|
|
|
import android.media.RingtoneManager;
|
|
|
|
import android.net.Uri;
|
|
|
|
import android.os.Build;
|
|
|
|
import android.text.TextUtils;
|
|
|
|
|
|
|
|
import androidx.annotation.RequiresApi;
|
|
|
|
|
|
|
|
import org.json.JSONException;
|
|
|
|
import org.json.JSONObject;
|
|
|
|
|
2021-10-09 16:52:53 +00:00
|
|
|
import java.util.Arrays;
|
|
|
|
import java.util.Collections;
|
|
|
|
import java.util.List;
|
|
|
|
|
2020-10-09 12:29:30 +00:00
|
|
|
class NotificationHelper {
|
2021-07-15 16:36:39 +00:00
|
|
|
static final int NOTIFICATION_SYNCHRONIZE = 100;
|
|
|
|
static final int NOTIFICATION_SEND = 200;
|
|
|
|
static final int NOTIFICATION_EXTERNAL = 300;
|
|
|
|
static final int NOTIFICATION_UPDATE = 400;
|
|
|
|
static final int NOTIFICATION_TAGGED = 500;
|
|
|
|
|
2021-10-09 16:52:53 +00:00
|
|
|
private static final List<String> PERSISTENT_IDS = Collections.unmodifiableList(Arrays.asList(
|
|
|
|
"service",
|
|
|
|
"send",
|
|
|
|
"notification",
|
|
|
|
"progress",
|
|
|
|
"update",
|
|
|
|
"warning",
|
|
|
|
"error",
|
|
|
|
"alerts"
|
|
|
|
));
|
|
|
|
|
2020-10-09 13:22:38 +00:00
|
|
|
@RequiresApi(api = Build.VERSION_CODES.O)
|
|
|
|
static void createNotificationChannels(Context context) {
|
|
|
|
// https://issuetracker.google.com/issues/65108694
|
2022-04-13 20:27:33 +00:00
|
|
|
NotificationManager nm = Helper.getSystemService(context, NotificationManager.class);
|
2020-10-09 13:22:38 +00:00
|
|
|
|
|
|
|
// Sync
|
|
|
|
NotificationChannel service = new NotificationChannel(
|
|
|
|
"service", context.getString(R.string.channel_service),
|
|
|
|
NotificationManager.IMPORTANCE_MIN);
|
|
|
|
service.setDescription(context.getString(R.string.channel_service_description));
|
|
|
|
service.setSound(null, null);
|
|
|
|
service.enableVibration(false);
|
|
|
|
service.enableLights(false);
|
|
|
|
service.setShowBadge(false);
|
|
|
|
service.setLockscreenVisibility(Notification.VISIBILITY_SECRET);
|
|
|
|
nm.createNotificationChannel(service);
|
|
|
|
|
|
|
|
// Send
|
|
|
|
NotificationChannel send = new NotificationChannel(
|
|
|
|
"send", context.getString(R.string.channel_send),
|
|
|
|
NotificationManager.IMPORTANCE_DEFAULT);
|
|
|
|
send.setDescription(context.getString(R.string.channel_send_description));
|
|
|
|
send.setSound(null, null);
|
|
|
|
send.enableVibration(false);
|
|
|
|
send.enableLights(false);
|
|
|
|
send.setShowBadge(false);
|
|
|
|
send.setLockscreenVisibility(Notification.VISIBILITY_PRIVATE);
|
|
|
|
nm.createNotificationChannel(send);
|
|
|
|
|
|
|
|
// Notify
|
|
|
|
NotificationChannel notification = new NotificationChannel(
|
|
|
|
"notification", context.getString(R.string.channel_notification),
|
|
|
|
NotificationManager.IMPORTANCE_HIGH);
|
|
|
|
notification.setDescription(context.getString(R.string.channel_notification_description));
|
|
|
|
notification.enableLights(true);
|
|
|
|
notification.setLightColor(Color.YELLOW);
|
2021-01-16 11:59:47 +00:00
|
|
|
notification.setLockscreenVisibility(Notification.VISIBILITY_PRIVATE);
|
2021-07-15 09:38:20 +00:00
|
|
|
notification.setBypassDnd(true);
|
2020-10-09 13:22:38 +00:00
|
|
|
nm.createNotificationChannel(notification);
|
|
|
|
|
2021-04-18 05:41:25 +00:00
|
|
|
NotificationChannel progress = new NotificationChannel(
|
|
|
|
"progress", context.getString(R.string.channel_progress),
|
|
|
|
NotificationManager.IMPORTANCE_DEFAULT);
|
|
|
|
notification.setDescription(context.getString(R.string.channel_progress_description));
|
|
|
|
progress.setSound(null, Notification.AUDIO_ATTRIBUTES_DEFAULT);
|
|
|
|
progress.setLockscreenVisibility(Notification.VISIBILITY_PRIVATE);
|
|
|
|
nm.createNotificationChannel(progress);
|
|
|
|
|
2020-10-09 13:22:38 +00:00
|
|
|
// Update
|
|
|
|
if (!Helper.isPlayStoreInstall()) {
|
|
|
|
NotificationChannel update = new NotificationChannel(
|
|
|
|
"update", context.getString(R.string.channel_update),
|
|
|
|
NotificationManager.IMPORTANCE_HIGH);
|
|
|
|
update.setSound(null, Notification.AUDIO_ATTRIBUTES_DEFAULT);
|
|
|
|
update.setLockscreenVisibility(Notification.VISIBILITY_PRIVATE);
|
|
|
|
nm.createNotificationChannel(update);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Warnings
|
|
|
|
NotificationChannel warning = new NotificationChannel(
|
|
|
|
"warning", context.getString(R.string.channel_warning),
|
|
|
|
NotificationManager.IMPORTANCE_HIGH);
|
|
|
|
warning.setLockscreenVisibility(Notification.VISIBILITY_PRIVATE);
|
2021-07-15 09:38:20 +00:00
|
|
|
warning.setBypassDnd(true);
|
2020-10-09 13:22:38 +00:00
|
|
|
nm.createNotificationChannel(warning);
|
|
|
|
|
|
|
|
// Errors
|
|
|
|
NotificationChannel error = new NotificationChannel(
|
|
|
|
"error",
|
|
|
|
context.getString(R.string.channel_error),
|
|
|
|
NotificationManager.IMPORTANCE_HIGH);
|
|
|
|
error.setLockscreenVisibility(Notification.VISIBILITY_PRIVATE);
|
2021-07-15 09:38:20 +00:00
|
|
|
error.setBypassDnd(true);
|
2020-10-09 13:22:38 +00:00
|
|
|
nm.createNotificationChannel(error);
|
|
|
|
|
|
|
|
// Server alerts
|
|
|
|
NotificationChannel alerts = new NotificationChannel(
|
|
|
|
"alerts",
|
|
|
|
context.getString(R.string.channel_alert),
|
|
|
|
NotificationManager.IMPORTANCE_HIGH);
|
|
|
|
alerts.setLockscreenVisibility(Notification.VISIBILITY_PRIVATE);
|
2021-07-15 09:38:20 +00:00
|
|
|
alerts.setBypassDnd(true);
|
2020-10-09 13:22:38 +00:00
|
|
|
nm.createNotificationChannel(alerts);
|
|
|
|
|
|
|
|
// Contacts grouping
|
|
|
|
NotificationChannelGroup group = new NotificationChannelGroup(
|
|
|
|
"contacts",
|
|
|
|
context.getString(R.string.channel_group_contacts));
|
|
|
|
nm.createNotificationChannelGroup(group);
|
|
|
|
}
|
|
|
|
|
2022-06-09 10:38:17 +00:00
|
|
|
static boolean areNotificationsEnabled(NotificationManager nm) {
|
|
|
|
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.TIRAMISU)
|
|
|
|
return true;
|
|
|
|
else
|
|
|
|
return nm.areNotificationsEnabled();
|
|
|
|
}
|
|
|
|
|
2021-10-09 16:52:53 +00:00
|
|
|
@RequiresApi(api = Build.VERSION_CODES.O)
|
|
|
|
static void clear(Context context) {
|
2022-04-13 20:27:33 +00:00
|
|
|
NotificationManager nm = Helper.getSystemService(context, NotificationManager.class);
|
2021-10-09 16:52:53 +00:00
|
|
|
for (NotificationChannel channel : nm.getNotificationChannels()) {
|
|
|
|
String id = channel.getId();
|
|
|
|
if (!PERSISTENT_IDS.contains(id)) {
|
|
|
|
EntityLog.log(context, "Deleting channel=" + id);
|
|
|
|
nm.deleteNotificationChannel(id);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-09 12:29:30 +00:00
|
|
|
@RequiresApi(api = Build.VERSION_CODES.O)
|
|
|
|
static JSONObject channelToJSON(NotificationChannel channel) throws JSONException {
|
|
|
|
JSONObject jchannel = new JSONObject();
|
|
|
|
|
|
|
|
jchannel.put("id", channel.getId());
|
|
|
|
jchannel.put("group", channel.getGroup());
|
|
|
|
jchannel.put("name", channel.getName());
|
|
|
|
jchannel.put("description", channel.getDescription());
|
|
|
|
|
|
|
|
jchannel.put("importance", channel.getImportance());
|
|
|
|
jchannel.put("dnd", channel.canBypassDnd());
|
|
|
|
jchannel.put("visibility", channel.getLockscreenVisibility());
|
|
|
|
jchannel.put("badge", channel.canShowBadge());
|
|
|
|
|
|
|
|
Uri sound = channel.getSound();
|
|
|
|
if (sound != null)
|
|
|
|
jchannel.put("sound", sound.toString());
|
|
|
|
// audio attributes
|
|
|
|
|
|
|
|
jchannel.put("light", channel.shouldShowLights());
|
|
|
|
// color
|
|
|
|
|
|
|
|
jchannel.put("vibrate", channel.shouldVibrate());
|
|
|
|
// pattern
|
|
|
|
|
|
|
|
return jchannel;
|
|
|
|
}
|
|
|
|
|
|
|
|
@RequiresApi(api = Build.VERSION_CODES.O)
|
|
|
|
static NotificationChannel channelFromJSON(Context context, JSONObject jchannel) throws JSONException {
|
|
|
|
NotificationChannel channel = new NotificationChannel(
|
|
|
|
jchannel.getString("id"),
|
|
|
|
jchannel.getString("name"),
|
|
|
|
jchannel.getInt("importance"));
|
|
|
|
|
|
|
|
String group = jchannel.optString("group");
|
|
|
|
if (!TextUtils.isEmpty(group))
|
|
|
|
channel.setGroup(group);
|
|
|
|
|
|
|
|
if (jchannel.has("description") && !jchannel.isNull("description"))
|
|
|
|
channel.setDescription(jchannel.getString("description"));
|
|
|
|
|
|
|
|
channel.setBypassDnd(jchannel.getBoolean("dnd"));
|
2021-09-27 15:07:03 +00:00
|
|
|
|
|
|
|
int visibility = jchannel.getInt("visibility");
|
|
|
|
if (visibility == Notification.VISIBILITY_PRIVATE ||
|
|
|
|
visibility == Notification.VISIBILITY_PUBLIC ||
|
|
|
|
visibility == Notification.VISIBILITY_SECRET)
|
|
|
|
channel.setLockscreenVisibility(visibility);
|
|
|
|
|
2020-10-09 12:29:30 +00:00
|
|
|
channel.setShowBadge(jchannel.getBoolean("badge"));
|
|
|
|
|
|
|
|
if (jchannel.has("sound") && !jchannel.isNull("sound")) {
|
|
|
|
Uri uri = Uri.parse(jchannel.getString("sound"));
|
|
|
|
Ringtone ringtone = RingtoneManager.getRingtone(context, uri);
|
|
|
|
if (ringtone != null)
|
|
|
|
channel.setSound(uri, Notification.AUDIO_ATTRIBUTES_DEFAULT);
|
|
|
|
}
|
|
|
|
|
|
|
|
channel.enableLights(jchannel.getBoolean("light"));
|
|
|
|
channel.enableVibration(jchannel.getBoolean("vibrate"));
|
|
|
|
|
|
|
|
return channel;
|
|
|
|
}
|
|
|
|
}
|