b6fce8e6276cd503e97d27993f921c0ad3b27f85
[fonbot.git] / src / ro / ieval / fonbot / SmsReceiver.java
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 continue;
54
55 final String name=Utils.callerId(context, originAddress);
56 final String body=sms.getMessageBody();
57 if(name==null)
58 Utils.sendMessage(context, toNonNull(MessageType.SMS), sms_received_fmt,
59 originAddress, body.replace("\n", "\n "));
60 else
61 Utils.sendMessage(context, toNonNull(MessageType.SMS), sms_received_fmt,
62 originAddress+" ("+name+")", body.replace("\n", "\n "));
63
64 final String[] lines=body.split("\n");
65 final String password = PreferenceManager.getDefaultSharedPreferences(context).getString("smspassword","");
66 if(password==null||password.length()==0)
67 continue;
68
69 if(lines.length==0 || !lines[0].equals(password))
70 continue;
71
72 final Address address=new Address(toNonNull(Protocol.SMS), originAddress);//NOPMD variable depends on originAddress
73 for (int i = 1; i < lines.length; i++) {
74 final String[] words=Utils.shellwords(toNonNull(lines[i]));
75 final String[] args=new String[words.length-1];//NOPMD variable size depends on words.length
76 System.arraycopy(words, 1, args, 0, args.length);
77
78 Utils.processCommand(context, toNonNull(words[0]), args, toNonNull(address));
79 }
80
81 abortBroadcast();
82 }
83 }
84
85 }
This page took 0.022989 seconds and 3 git commands to generate.