FairEmail/app/src/main/java/eu/faircode/email/NotificationHelper.java

96 lines
3.3 KiB
Java
Raw Normal View History

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/>.
Copyright 2018-2020 by Marcel Bokhorst (M66B)
*/
import android.app.Notification;
import android.app.NotificationChannel;
import android.content.Context;
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;
class NotificationHelper {
@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"));
channel.setLockscreenVisibility(jchannel.getInt("visibility"));
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;
}
}