2024-01-10 18:54:00 +01:00
|
|
|
/* Copyright (C) 2015-2024 Andreas Shimokawa, Carsten Pfeiffer, Daniele
|
|
|
|
Gobbetti, mvn23, Normano64, Zhong Jianxin
|
2017-03-10 14:53:19 +01:00
|
|
|
|
|
|
|
This file is part of Gadgetbridge.
|
|
|
|
|
|
|
|
Gadgetbridge is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU Affero General Public License as published
|
|
|
|
by the Free Software Foundation, either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
Gadgetbridge 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 Affero General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU Affero General Public License
|
2024-01-10 18:54:00 +01:00
|
|
|
along with this program. If not, see <https://www.gnu.org/licenses/>. */
|
2015-04-20 22:39:35 +02:00
|
|
|
package nodomain.freeyourgadget.gadgetbridge.externalevents;
|
2015-01-24 12:21:15 +01:00
|
|
|
|
2016-05-19 16:34:59 +02:00
|
|
|
import android.app.NotificationManager;
|
2016-05-19 23:58:13 +02:00
|
|
|
import android.app.NotificationManager.Policy;
|
2015-01-24 12:21:15 +01:00
|
|
|
import android.content.BroadcastReceiver;
|
|
|
|
import android.content.Context;
|
|
|
|
import android.content.Intent;
|
|
|
|
import android.os.Bundle;
|
2015-03-04 23:47:47 +01:00
|
|
|
import android.os.PowerManager;
|
2015-01-24 12:21:15 +01:00
|
|
|
import android.telephony.SmsMessage;
|
|
|
|
|
2018-11-01 16:58:20 +01:00
|
|
|
import java.util.ArrayList;
|
2017-11-14 10:31:07 +01:00
|
|
|
import java.util.LinkedHashMap;
|
|
|
|
import java.util.Map;
|
|
|
|
|
2015-08-21 00:58:18 +02:00
|
|
|
import nodomain.freeyourgadget.gadgetbridge.GBApplication;
|
2015-09-24 14:45:21 +02:00
|
|
|
import nodomain.freeyourgadget.gadgetbridge.model.NotificationSpec;
|
|
|
|
import nodomain.freeyourgadget.gadgetbridge.model.NotificationType;
|
2016-04-25 23:18:55 +02:00
|
|
|
import nodomain.freeyourgadget.gadgetbridge.util.Prefs;
|
2015-04-20 22:39:35 +02:00
|
|
|
|
2015-01-24 12:21:15 +01:00
|
|
|
public class SMSReceiver extends BroadcastReceiver {
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onReceive(Context context, Intent intent) {
|
2016-04-25 23:18:55 +02:00
|
|
|
Prefs prefs = GBApplication.getPrefs();
|
|
|
|
if ("never".equals(prefs.getString("notification_mode_sms", "when_screen_off"))) {
|
2015-03-04 23:47:47 +01:00
|
|
|
return;
|
|
|
|
}
|
2016-04-25 23:18:55 +02:00
|
|
|
if ("when_screen_off".equals(prefs.getString("notification_mode_sms", "when_screen_off"))) {
|
2015-05-04 01:03:56 +02:00
|
|
|
PowerManager powermanager = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
|
2018-11-15 15:53:04 +01:00
|
|
|
if (powermanager != null && powermanager.isScreenOn()) {
|
2015-03-06 14:00:56 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2015-03-04 23:47:47 +01:00
|
|
|
|
2015-09-24 14:45:21 +02:00
|
|
|
NotificationSpec notificationSpec = new NotificationSpec();
|
2016-10-11 11:54:52 +02:00
|
|
|
notificationSpec.type = NotificationType.GENERIC_SMS;
|
2015-09-24 14:45:21 +02:00
|
|
|
|
2015-01-24 12:21:15 +01:00
|
|
|
Bundle bundle = intent.getExtras();
|
|
|
|
if (bundle != null) {
|
|
|
|
Object[] pdus = (Object[]) bundle.get("pdus");
|
2015-11-23 21:59:13 +01:00
|
|
|
if (pdus != null) {
|
2017-11-14 10:31:07 +01:00
|
|
|
int pduSize = pdus.length;
|
|
|
|
Map<String, StringBuilder> messageMap = new LinkedHashMap<>();
|
|
|
|
SmsMessage[] messages = new SmsMessage[pduSize];
|
|
|
|
for (int i = 0; i < pduSize; i++) {
|
|
|
|
messages[i] = SmsMessage.createFromPdu((byte[]) pdus[i]);
|
|
|
|
String originatingAddress = messages[i].getOriginatingAddress();
|
|
|
|
if (!messageMap.containsKey(originatingAddress)) {
|
|
|
|
messageMap.put(originatingAddress, new StringBuilder());
|
|
|
|
}
|
|
|
|
messageMap.get(originatingAddress).append(messages[i].getMessageBody());
|
|
|
|
}
|
|
|
|
for (Map.Entry<String, StringBuilder> entry : messageMap.entrySet()) {
|
|
|
|
String originatingAddress = entry.getKey();
|
|
|
|
if (originatingAddress != null) {
|
|
|
|
notificationSpec.body = entry.getValue().toString();
|
|
|
|
notificationSpec.phoneNumber = originatingAddress;
|
2018-11-01 16:58:20 +01:00
|
|
|
notificationSpec.attachedActions = new ArrayList<>();
|
2018-11-15 15:53:04 +01:00
|
|
|
|
|
|
|
// REPLY action
|
2018-11-01 16:58:20 +01:00
|
|
|
NotificationSpec.Action replyAction = new NotificationSpec.Action();
|
|
|
|
replyAction.title = "Reply";
|
2018-11-15 15:53:04 +01:00
|
|
|
replyAction.type = NotificationSpec.Action.TYPE_SYNTECTIC_REPLY_PHONENR;
|
2018-11-01 16:58:20 +01:00
|
|
|
notificationSpec.attachedActions.add(replyAction);
|
|
|
|
|
2018-11-15 15:53:04 +01:00
|
|
|
// DISMISS ALL action
|
|
|
|
NotificationSpec.Action dismissAllAction = new NotificationSpec.Action();
|
|
|
|
dismissAllAction.title = "Dismiss All";
|
|
|
|
dismissAllAction.type = NotificationSpec.Action.TYPE_SYNTECTIC_DISMISS_ALL;
|
|
|
|
notificationSpec.attachedActions.add(dismissAllAction);
|
|
|
|
|
2022-05-15 16:51:41 +02:00
|
|
|
int dndSuppressed = 0;
|
2016-05-19 23:58:13 +02:00
|
|
|
switch (GBApplication.getGrantedInterruptionFilter()) {
|
|
|
|
case NotificationManager.INTERRUPTION_FILTER_ALL:
|
|
|
|
break;
|
|
|
|
case NotificationManager.INTERRUPTION_FILTER_ALARMS:
|
|
|
|
case NotificationManager.INTERRUPTION_FILTER_NONE:
|
2022-05-15 16:51:41 +02:00
|
|
|
dndSuppressed = 1;
|
|
|
|
break;
|
2016-05-19 23:58:13 +02:00
|
|
|
case NotificationManager.INTERRUPTION_FILTER_PRIORITY:
|
|
|
|
if (GBApplication.isPriorityNumber(Policy.PRIORITY_CATEGORY_MESSAGES, notificationSpec.phoneNumber)) {
|
|
|
|
break;
|
2016-05-19 16:34:59 +02:00
|
|
|
}
|
2022-05-15 16:51:41 +02:00
|
|
|
dndSuppressed = 1;
|
|
|
|
}
|
|
|
|
if (prefs.getBoolean("notification_filter", false) && dndSuppressed == 1) {
|
|
|
|
return;
|
2016-05-19 16:34:59 +02:00
|
|
|
}
|
2022-05-15 16:51:41 +02:00
|
|
|
notificationSpec.dndSuppressed = dndSuppressed;
|
2015-11-23 21:59:13 +01:00
|
|
|
GBApplication.deviceService().onNotification(notificationSpec);
|
|
|
|
}
|
2015-01-24 12:21:15 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|