/*  Copyright (C) 2015-2017 0nse, Andreas Shimokawa, Carsten Pfeiffer,
    Normano64

    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
    along with this program.  If not, see <http://www.gnu.org/licenses/>. */
package nodomain.freeyourgadget.gadgetbridge.externalevents;

import android.app.NotificationManager;
import android.app.NotificationManager.Policy;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.PowerManager;
import android.telephony.SmsMessage;

import nodomain.freeyourgadget.gadgetbridge.GBApplication;
import nodomain.freeyourgadget.gadgetbridge.model.NotificationSpec;
import nodomain.freeyourgadget.gadgetbridge.model.NotificationType;
import nodomain.freeyourgadget.gadgetbridge.util.Prefs;

public class SMSReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        Prefs prefs = GBApplication.getPrefs();
        if ("never".equals(prefs.getString("notification_mode_sms", "when_screen_off"))) {
            return;
        }
        if ("when_screen_off".equals(prefs.getString("notification_mode_sms", "when_screen_off"))) {
            PowerManager powermanager = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
            if (powermanager.isScreenOn()) {
                return;
            }
        }

        NotificationSpec notificationSpec = new NotificationSpec();
        notificationSpec.id = -1;
        notificationSpec.type = NotificationType.GENERIC_SMS;

        Bundle bundle = intent.getExtras();
        if (bundle != null) {
            Object[] pdus = (Object[]) bundle.get("pdus");
            if (pdus != null) {
                for (Object pdu1 : pdus) {
                    byte[] pdu = (byte[]) pdu1;
                    SmsMessage message = SmsMessage.createFromPdu(pdu);
                    notificationSpec.body = message.getDisplayMessageBody();
                    notificationSpec.phoneNumber = message.getOriginatingAddress();
                    if (notificationSpec.phoneNumber != null) {
                        switch (GBApplication.getGrantedInterruptionFilter()) {
                            case NotificationManager.INTERRUPTION_FILTER_ALL:
                                break;
                            case NotificationManager.INTERRUPTION_FILTER_ALARMS:
                            case NotificationManager.INTERRUPTION_FILTER_NONE:
                                return;
                            case NotificationManager.INTERRUPTION_FILTER_PRIORITY:
                                if (GBApplication.isPriorityNumber(Policy.PRIORITY_CATEGORY_MESSAGES, notificationSpec.phoneNumber)) {
                                    break;
                                }
                                return;
                        }
                        GBApplication.deviceService().onNotification(notificationSpec);
                    }
                }
            }
        }
    }
}

@@ -1145,16 +1145,71 @@
 ssage;%0A%0A
+import java.util.LinkedHashMap;%0Aimport java.util.Map;%0A%0A
 import n
@@ -2398,121 +2398,258 @@
     
-for (Object pdu1 : pdus) %7B%0A                    byte%5B%5D pdu = (byte%5B%5D) pdu1;%0A                    SmsMessage
+int pduSize = pdus.length;%0A                Map%3CString, StringBuilder%3E messageMap = new LinkedHashMap%3C%3E();%0A                SmsMessage%5B%5D messages = new SmsMessage%5BpduSize%5D;%0A                for (int i = 0; i %3C pduSize; i++) %7B%0A                   
  message
  = S
@@ -2644,16 +2644,20 @@
  message
+s%5Bi%5D
  = SmsMe
@@ -2676,19 +2676,32 @@
 FromPdu(
-pdu
+(byte%5B%5D) pdus%5Bi%5D
 );%0A     
@@ -2719,39 +2719,274 @@
     
-notificationSpec.body =
+String originatingAddress = messages%5Bi%5D.getOriginatingAddress();%0A                    if (!messageMap.containsKey(originatingAddress)) %7B%0A                        messageMap.put(originatingAddress, new StringBuilder());%0A                    %7D%0A                   
  message
 .get
@@ -2985,19 +2985,58 @@
 sage
+Map
 .get
-Display
+(originatingAddress).append(messages%5Bi%5D.get
 Mess
@@ -3044,16 +3044,17 @@
 geBody()
+)
 ;%0A      
@@ -3067,55 +3067,125 @@
     
+%7D%0A
     
-notificationSpec.phoneNumber = message.getO
+            for (Map.Entry%3CString, StringBuilder%3E entry : messageMap.entrySet()) %7B%0A                    String o
 rigi
@@ -3189,32 +3189,47 @@
 iginatingAddress
+ = entry.getKey
 ();%0A            
@@ -3240,16 +3240,147 @@
     if (
+originatingAddress != null) %7B%0A                        notificationSpec.body = entry.getValue().toString();%0A                        
 notifica
@@ -3400,26 +3400,37 @@
 eNumber 
-!
 = 
-null) %7B
+originatingAddress;
 %0A       

Combine multipart SMS message into messages by sender