| 1 | package ro.ieval.fonbot; |
| 2 | |
| 3 | import static ro.ieval.fonbot.R.string.*; |
| 4 | |
| 5 | import static ro.ieval.fonbot.Utils.toNonNull; |
| 6 | |
| 7 | import org.eclipse.jdt.annotation.Nullable; |
| 8 | |
| 9 | import ro.ieval.fonbot.Utils.MessageType; |
| 10 | import android.content.BroadcastReceiver; |
| 11 | import android.content.Context; |
| 12 | import android.content.Intent; |
| 13 | import android.preference.PreferenceManager; |
| 14 | import android.telephony.SmsMessage; |
| 15 | |
| 16 | /* |
| 17 | * Copyright © 2013 Marius Gavrilescu |
| 18 | * |
| 19 | * This file is part of FonBot. |
| 20 | * |
| 21 | * FonBot is free software: you can redistribute it and/or modify |
| 22 | * it under the terms of the GNU General Public License as published by |
| 23 | * the Free Software Foundation, either version 3 of the License, or |
| 24 | * (at your option) any later version. |
| 25 | * |
| 26 | * FonBot is distributed in the hope that it will be useful, |
| 27 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 28 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 29 | * GNU General Public License for more details. |
| 30 | * |
| 31 | * You should have received a copy of the GNU General Public License |
| 32 | * along with FonBot. If not, see <http://www.gnu.org/licenses/>. |
| 33 | */ |
| 34 | |
| 35 | /** |
| 36 | * BroadcastReceiver that receives SMSes. It processes command smses and sends {@link MessageType#SMS} notifications. |
| 37 | * |
| 38 | * @author Marius Gavrilescu <marius@ieval.ro> |
| 39 | */ |
| 40 | public final class SmsReceiver extends BroadcastReceiver { |
| 41 | |
| 42 | @Override |
| 43 | public void onReceive(@Nullable final Context context, @Nullable final Intent intent) { |
| 44 | if(intent==null||context==null) |
| 45 | return; |
| 46 | |
| 47 | final Object[] pdus=(Object[]) intent.getExtras().get("pdus"); |
| 48 | for (Object pdu : pdus) { |
| 49 | final SmsMessage sms=SmsMessage.createFromPdu((byte[]) pdu); |
| 50 | final String originAddress=sms.getOriginatingAddress(); |
| 51 | if(sms.getMessageBody() == null || originAddress == null) |
| 52 | continue; |
| 53 | |
| 54 | final String name=Utils.callerId(context, originAddress); |
| 55 | final String body=sms.getMessageBody(); |
| 56 | if(name==null) |
| 57 | Utils.sendMessage(context, toNonNull(MessageType.SMS), sms_received_fmt, |
| 58 | originAddress, body.replace("\n", "\n ")); |
| 59 | else |
| 60 | Utils.sendMessage(context, toNonNull(MessageType.SMS), sms_received_fmt, |
| 61 | originAddress+" ("+name+")", body.replace("\n", "\n ")); |
| 62 | |
| 63 | final String[] lines=body.split("\n"); |
| 64 | final String password = PreferenceManager.getDefaultSharedPreferences(context).getString("smspassword",""); |
| 65 | if(password==null||password.length()==0) |
| 66 | continue; |
| 67 | |
| 68 | if(lines.length==0 || !lines[0].equals(password)) |
| 69 | continue; |
| 70 | |
| 71 | for (int i = 1; i < lines.length; i++) { |
| 72 | final Intent process_intent = new Intent(context, FonBotMainService.class); |
| 73 | process_intent.setAction(FonBotMainService.ACTION_PROCESS_COMMAND); |
| 74 | process_intent.putExtra(FonBotMainService.EXTRA_COMMAND_LINE, lines[i]); |
| 75 | process_intent.putExtra(FonBotMainService.EXTRA_SMS_ORIGIN_ADDRESS, originAddress); |
| 76 | context.startService(process_intent); |
| 77 | } |
| 78 | |
| 79 | abortBroadcast(); |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | } |