]>
iEval git - fonbot.git/blob - SmsReceiver.java
b6fce8e6276cd503e97d27993f921c0ad3b27f85
1 package ro
.ieval
.fonbot
;
3 import static ro
.ieval
.fonbot
.R
.string
.*;
5 import static ro
.ieval
.fonbot
.Utils
.toNonNull
;
7 import org
.eclipse
.jdt
.annotation
.Nullable
;
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
;
18 * Copyright © 2013 Marius Gavrilescu
20 * This file is part of FonBot.
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.
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.
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/>.
37 * BroadcastReceiver that receives SMSes. It processes command smses and sends {@link MessageType#SMS} notifications.
39 * @author Marius Gavrilescu <marius@ieval.ro>
41 public final class SmsReceiver
extends BroadcastReceiver
{
44 public void onReceive(@Nullable final Context context
, @Nullable final Intent intent
) {
45 if(intent
==null||context
==null)
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)
55 final String name
=Utils
.callerId(context
, originAddress
);
56 final String body
=sms
.getMessageBody();
58 Utils
.sendMessage(context
, toNonNull(MessageType
.SMS
), sms_received_fmt
,
59 originAddress
, body
.replace("\n", "\n "));
61 Utils
.sendMessage(context
, toNonNull(MessageType
.SMS
), sms_received_fmt
,
62 originAddress
+" ("+name
+")", body
.replace("\n", "\n "));
64 final String
[] lines
=body
.split("\n");
65 final String password
= PreferenceManager
.getDefaultSharedPreferences(context
).getString("smspassword","");
66 if(password
==null||password
.length()==0)
69 if(lines
.length
==0 || !lines
[0].equals(password
))
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
);
78 Utils
.processCommand(context
, toNonNull(words
[0]), args
, toNonNull(address
));
This page took 0.043388 seconds and 3 git commands to generate.