Execute commands received via SMS on the main service
[fonbot.git] / src / ro / ieval / fonbot / SmsReceiver.java
CommitLineData
8dfb76c9
MG
1package ro.ieval.fonbot;
2
3import static ro.ieval.fonbot.R.string.*;
4
5import static ro.ieval.fonbot.Utils.toNonNull;
6
7import org.eclipse.jdt.annotation.Nullable;
8
8dfb76c9
MG
9import ro.ieval.fonbot.Utils.MessageType;
10import android.content.BroadcastReceiver;
11import android.content.Context;
12import android.content.Intent;
13import android.preference.PreferenceManager;
14import 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 */
40public 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)
de69decc 52 continue;
8dfb76c9
MG
53
54 final String name=Utils.callerId(context, originAddress);
de69decc 55 final String body=sms.getMessageBody();
8dfb76c9 56 if(name==null)
de69decc
MG
57 Utils.sendMessage(context, toNonNull(MessageType.SMS), sms_received_fmt,
58 originAddress, body.replace("\n", "\n "));
8dfb76c9 59 else
de69decc
MG
60 Utils.sendMessage(context, toNonNull(MessageType.SMS), sms_received_fmt,
61 originAddress+" ("+name+")", body.replace("\n", "\n "));
8dfb76c9 62
de69decc 63 final String[] lines=body.split("\n");
8dfb76c9
MG
64 final String password = PreferenceManager.getDefaultSharedPreferences(context).getString("smspassword","");
65 if(password==null||password.length()==0)
de69decc 66 continue;
8dfb76c9
MG
67
68 if(lines.length==0 || !lines[0].equals(password))
de69decc 69 continue;
8dfb76c9 70
8dfb76c9 71 for (int i = 1; i < lines.length; i++) {
e0c7f061
MG
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);
8dfb76c9
MG
77 }
78
79 abortBroadcast();
80 }
81 }
82
83}
This page took 0.018073 seconds and 4 git commands to generate.