package ru.shutoff.caralarm;

import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.PendingIntent.CanceledException;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.media.Ringtone;
import android.media.RingtoneManager;
import android.net.Uri;
import android.os.Vibrator;
import android.preference.PreferenceManager;
import android.support.v4.app.NotificationCompat;
import android.telephony.SmsMessage;

public class SmsMonitor extends BroadcastReceiver {

    private static final String ACTION = "android.provider.Telephony.SMS_RECEIVED"; //$NON-NLS-1$

    @Override
    public void onReceive(Context context, Intent intent) {
        if (intent != null && intent.getAction() != null &&
                ACTION.compareToIgnoreCase(intent.getAction()) == 0) {
            Object[] pduArray = (Object[]) intent.getExtras().get("pdus"); //$NON-NLS-1$
            SmsMessage[] messages = new SmsMessage[pduArray.length];
            for (int i = 0; i < pduArray.length; i++) {
                messages[i] = SmsMessage.createFromPdu((byte[]) pduArray[i]);
            }
            String sms_from = messages[0].getOriginatingAddress();
            StringBuilder bodyText = new StringBuilder();
            for (SmsMessage m : messages) {
                bodyText.append(m.getMessageBody());
            }
            String body = bodyText.toString();
            SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
            String phone_config = digitsOnly(preferences.getString(Names.PHONE, ""));
            if ((phone_config.length() > 0) && phone_config.equals(digitsOnly(sms_from))) {
                if (processCarMessage(context, body))
                    abortBroadcast();
            }

            if (body.matches("[0-9A-Fa-f]{30}")) {
                Intent keyIntent = new Intent(context, ApiKeyDialog.class);
                keyIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                keyIntent.putExtra(Names.KEY, body); //$NON-NLS-1$
                context.startActivity(keyIntent);
                abortBroadcast();
            }
        }

    }

    String digitsOnly(String phone) {
        return phone.replaceAll("[^0-9]", ""); //$NON-NLS-1$ //$NON-NLS-2$
    }

    static String[] notifications = {
            "Light shock",
            "Low Card Battery",
            "Supply reserve",
            "Supply regular",
            "ERROR LAN-devices",
            "Low reserve voltage",
            "Roaming. Internet OFF"
    };

    static String[] alarms = {
            "Heavy shock",
            "Trunk",
            "Hood",
            "Doors",
            "Lock",
            "MovTilt sensor",
            "Rogue"
    };

    boolean processCarMessage(Context context, String body) {
        if ((State.waitAnswer != null) && (body.substring(0, State.waitAnswer.length()).equalsIgnoreCase(State.waitAnswer))) {
            try {
                State.waitAnswerPI.send(Names.ANSWER_OK);
            } catch (CanceledException e) {
                // ignore
            }
            return true;
        }
        for (int i = 0; i < notifications.length; i++) {
            String n = notifications[i];
            if (body.matches(".*" + n + ".*")) {
                String[] msg = context.getString(R.string.notification).split("\\|");
                showNotification(context, msg[i]);
                return true;
            }
        }
        for (int i = 0; i < alarms.length; i++) {
            String n = alarms[i];
            if (body.matches("ALARM " + n + ".*")) {
                String[] msg = context.getString(R.string.alarm).split("\\|");
                showAlarm(context, msg[i]);
                return true;
            }
        }
        return false;
    }

    private void showNotification(Context context, String text) {
        NotificationCompat.Builder builder =
                new NotificationCompat.Builder(context)
                        .setSmallIcon(R.drawable.ic_launcher)
                        .setContentTitle("Car Alarm")
                        .setContentText(text);

        Intent notificationIntent = new Intent(context, MainActivity.class);
        notificationIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        notificationIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        PendingIntent contentIntent = PendingIntent.getActivity(context, 0, notificationIntent,
                PendingIntent.FLAG_UPDATE_CURRENT);
        builder.setContentIntent(contentIntent);

        SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
        int id = preferences.getInt(Names.IDS, 0);
        id++;
        SharedPreferences.Editor ed = preferences.edit();
        ed.putInt(Names.IDS, id);
        ed.commit();

        // Add as notification
        NotificationManager manager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
        manager.notify(id, builder.build());

        String sound = preferences.getString(Names.NOTIFY, "");
        Uri uri = Uri.parse(sound);
        Ringtone ringtone = RingtoneManager.getRingtone(context, uri);
        if (ringtone == null)
            uri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
        try {
            AudioManager audioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
            MediaPlayer player = new MediaPlayer();
            player.setDataSource(context, uri);
            if (audioManager.getStreamVolume(AudioManager.STREAM_NOTIFICATION) != 0) {
                player.setAudioStreamType(AudioManager.STREAM_NOTIFICATION);
                player.setLooping(false);
                player.prepare();
                player.start();
            }
            Vibrator vibrator = (Vibrator) context.getSystemService(Context.VIBRATOR_SERVICE);
            if (vibrator != null)
                vibrator.vibrate(500);
        } catch (Exception err) {
            // ignore
        }
    }

    private void showAlarm(Context context, String text) {
        Intent alarmIntent = new Intent(context, Alarm.class);
        alarmIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        alarmIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        alarmIntent.putExtra(Names.ALARM, text);
        context.startActivity(alarmIntent);
    }

}

@@ -2559,16 +2559,22 @@
        %22
+ALARM 
 Light sh
@@ -2572,32 +2572,32 @@
  Light shock%22,%0D%0A
-
             %22Low
@@ -2837,16 +2837,22 @@
        %22
+ALARM 
 Heavy sh
@@ -2871,16 +2871,22 @@
        %22
+ALARM 
 Trunk%22,%0D
@@ -2899,16 +2899,22 @@
        %22
+ALARM 
 Hood%22,%0D%0A
@@ -2926,16 +2926,22 @@
        %22
+ALARM 
 Doors%22,%0D
@@ -2954,16 +2954,22 @@
        %22
+ALARM 
 Lock%22,%0D%0A
@@ -2981,16 +2981,22 @@
        %22
+ALARM 
 MovTilt 
@@ -3018,16 +3018,22 @@
        %22
+ALARM 
 Rogue%22%0D%0A
@@ -3509,85 +3509,52 @@
     
-String n = notifications%5Bi%5D;%0D%0A            if (body.matches(%22.*%22 + n + %22.*%22
+if (body.equalsIgnoreCase(notifications%5Bi%5D
 ))
- 
 %7B%0D%0A 
@@ -3814,79 +3814,43 @@
     
-String n = alarms%5Bi%5D;%0D%0A            if (body.matches(%22ALARM %22 + n + %22.*%22
+if (body.equalsIgnoreCase(alarms%5Bi%5D
 )) %7B

Fix sms processing