]>
iEval git - fonbot.git/blob - ieval/fonbot/SmsReceiver.java
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
.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
;
17 * Copyright © 2013 Marius Gavrilescu
19 * This file is part of FonBot.
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.
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.
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/>.
36 * BroadcastReceiver that receives SMSes. It processes command smses and sends {@link MessageType#SMS} notifications.
38 * @author Marius Gavrilescu <marius@ieval.ro>
40 public final class SmsReceiver
extends BroadcastReceiver
{
43 public void onReceive(@Nullable final Context context
, @Nullable final Intent intent
) {
44 if(intent
==null||context
==null)
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)
54 final String name
=Utils
.callerId(context
, originAddress
);
55 final String body
=sms
.getMessageBody();
57 Utils
.sendMessage(context
, toNonNull(MessageType
.SMS
), sms_received_fmt
,
58 originAddress
, body
.replace("\n", "\n "));
60 Utils
.sendMessage(context
, toNonNull(MessageType
.SMS
), sms_received_fmt
,
61 originAddress
+" ("+name
+")", body
.replace("\n", "\n "));
63 final String
[] lines
=body
.split("\n");
64 final String password
= PreferenceManager
.getDefaultSharedPreferences(context
).getString("smspassword","");
65 if(password
==null||password
.length()==0)
68 if(lines
.length
==0 || !lines
[0].equals(password
))
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
);
This page took 0.048824 seconds and 4 git commands to generate.