1 package ro
.ieval
.fonbot
;
3 import org
.eclipse
.jdt
.annotation
.Nullable
;
5 import android
.app
.Activity
;
6 import android
.content
.BroadcastReceiver
;
7 import android
.content
.Context
;
8 import android
.content
.Intent
;
9 import android
.telephony
.SmsManager
;
12 * Copyright © 2013 Marius Gavrilescu
14 * This file is part of FonBot.
16 * FonBot is free software: you can redistribute it and/or modify
17 * it under the terms of the GNU General Public License as published by
18 * the Free Software Foundation, either version 3 of the License, or
19 * (at your option) any later version.
21 * FonBot is distributed in the hope that it will be useful,
22 * but WITHOUT ANY WARRANTY; without even the implied warranty of
23 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 * GNU General Public License for more details.
26 * You should have received a copy of the GNU General Public License
27 * along with FonBot. If not, see <http://www.gnu.org/licenses/>.
31 * BroadcastReceiver that receives SMS status broadcasts, notifying the user.
33 * @author Marius Gavrilescu <marius@ieval.ro>
35 public final class SmsStatusReceiver
extends BroadcastReceiver
{
37 * Formats a SMS message intent into a human-readable string
39 * @param intent the SMS message
40 * @return the string representation of the SMS
42 private static String
describeSMS(final Intent intent
){
43 return " ("+"To: "+intent
.getStringExtra(EXTRA_DESTINATION
)+
44 ", Part: "+intent
.getIntExtra(EXTRA_PART
, 1)+" of "+intent
.getIntExtra(EXTRA_TOTAL
, 1)+")";
48 * Broadcast action: SMS sent
50 public static final String SENT_ACTION
="ro.ieval.fonbot.SmsStatusReceiver.SENT";
52 * Broadcast action: SMS delivered
54 public static final String DELIVERED_ACTION
="ro.ieval.fonbot.SmsStatusReceiver.DELIVERED";
56 * Extra: SMS destination (phone number)
58 public static final String EXTRA_DESTINATION
="destination";
60 * Extra: SMS part number
62 public static final String EXTRA_PART
="part";
64 * Extra: SMS part count
66 public static final String EXTRA_TOTAL
="total";
68 * Extra: Notification address
70 public static final String EXTRA_REPLY_TO
="reply_to";
73 public void onReceive(@Nullable final Context context
, @Nullable final Intent intent
) {
74 if(intent
==null||context
==null)
76 final Address replyTo
=new Address(Utils
.toNonNull(intent
.getStringExtra(EXTRA_REPLY_TO
)));
77 if(intent
.getAction().startsWith(SENT_ACTION
))
78 switch(getResultCode()){
79 case Activity
.RESULT_OK
:
80 Utils
.sendMessage(context
, replyTo
, "SMS sent"+describeSMS(intent
));
82 case SmsManager
.RESULT_ERROR_GENERIC_FAILURE
:
83 Utils
.sendMessage(context
, replyTo
, "SMS not sent: Generic failure"+describeSMS(intent
));
85 case SmsManager
.RESULT_ERROR_NO_SERVICE
:
86 Utils
.sendMessage(context
, replyTo
, "SMS not sent: No service"+describeSMS(intent
));
88 case SmsManager
.RESULT_ERROR_NULL_PDU
:
89 Utils
.sendMessage(context
, replyTo
, "SMS not sent: Null PDU"+describeSMS(intent
));
91 case SmsManager
.RESULT_ERROR_RADIO_OFF
:
92 Utils
.sendMessage(context
, replyTo
, "SMS not sent: Radio off"+describeSMS(intent
));
95 else if(intent
.getAction().startsWith(DELIVERED_ACTION
))
96 switch(getResultCode()){
97 case Activity
.RESULT_OK
:
98 Utils
.sendMessage(context
, replyTo
, "SMS delivered"+describeSMS(intent
));
100 case Activity
.RESULT_CANCELED
:
101 Utils
.sendMessage(context
, replyTo
, "SMS not delivered"+describeSMS(intent
));