1 package ro
.ieval
.fonbot
;
3 import static ro
.ieval
.fonbot
.R
.string
.*;
4 import org
.eclipse
.jdt
.annotation
.Nullable
;
6 import android
.app
.Activity
;
7 import android
.content
.BroadcastReceiver
;
8 import android
.content
.Context
;
9 import android
.content
.Intent
;
10 import android
.telephony
.SmsManager
;
13 * Copyright © 2013 Marius Gavrilescu
15 * This file is part of FonBot.
17 * FonBot is free software: you can redistribute it and/or modify
18 * it under the terms of the GNU General Public License as published by
19 * the Free Software Foundation, either version 3 of the License, or
20 * (at your option) any later version.
22 * FonBot is distributed in the hope that it will be useful,
23 * but WITHOUT ANY WARRANTY; without even the implied warranty of
24 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25 * GNU General Public License for more details.
27 * You should have received a copy of the GNU General Public License
28 * along with FonBot. If not, see <http://www.gnu.org/licenses/>.
32 * BroadcastReceiver that receives SMS status broadcasts, notifying the user.
34 * @author Marius Gavrilescu <marius@ieval.ro>
36 public final class SmsStatusReceiver
extends BroadcastReceiver
{
38 * Formats a SMS message intent into a human-readable string
40 * @param intent the SMS message
41 * @return the string representation of the SMS
43 private static String
describeSMS(final Intent intent
){
44 return "To: "+intent
.getStringExtra(EXTRA_DESTINATION
)+
45 ", Part: "+intent
.getIntExtra(EXTRA_PART
, 1)+" of "+intent
.getIntExtra(EXTRA_TOTAL
, 1);
49 * Broadcast action: SMS sent
51 public static final String SENT_ACTION
="ro.ieval.fonbot.SmsStatusReceiver.SENT";
53 * Broadcast action: SMS delivered
55 public static final String DELIVERED_ACTION
="ro.ieval.fonbot.SmsStatusReceiver.DELIVERED";
57 * Extra: SMS destination (phone number)
59 public static final String EXTRA_DESTINATION
="destination";
61 * Extra: SMS part number
63 public static final String EXTRA_PART
="part";
65 * Extra: SMS part count
67 public static final String EXTRA_TOTAL
="total";
69 * Extra: Notification address
71 public static final String EXTRA_REPLY_TO
="reply_to";
74 public void onReceive(@Nullable final Context context
, @Nullable final Intent intent
) {
75 if(intent
==null||context
==null)
77 final Address replyTo
=new Address(Utils
.toNonNull(intent
.getStringExtra(EXTRA_REPLY_TO
)));
78 if(intent
.getAction().startsWith(SENT_ACTION
))
79 switch(getResultCode()){
80 case Activity
.RESULT_OK
:
81 Utils
.sendConfirmMessage(context
, replyTo
, sms_sent
, describeSMS(intent
));
83 case SmsManager
.RESULT_ERROR_GENERIC_FAILURE
:
84 Utils
.sendMessage(context
, replyTo
, sms_not_sent_generic_failure
, describeSMS(intent
));
86 case SmsManager
.RESULT_ERROR_NO_SERVICE
:
87 Utils
.sendMessage(context
, replyTo
, sms_not_sent_no_service
, describeSMS(intent
));
89 case SmsManager
.RESULT_ERROR_NULL_PDU
:
90 Utils
.sendMessage(context
, replyTo
, sms_not_sent_null_pdu
, describeSMS(intent
));
92 case SmsManager
.RESULT_ERROR_RADIO_OFF
:
93 Utils
.sendMessage(context
, replyTo
, sms_not_sent_radio_off
, describeSMS(intent
));
96 else if(intent
.getAction().startsWith(DELIVERED_ACTION
))
97 switch(getResultCode()){
98 case Activity
.RESULT_OK
:
99 Utils
.sendMessage(context
, replyTo
, sms_delivered
, describeSMS(intent
));
101 case Activity
.RESULT_CANCELED
:
102 Utils
.sendMessage(context
, replyTo
, sms_not_delivered
, describeSMS(intent
));