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