Initial commit
[fonbot.git] / src / ro / ieval / fonbot / SmsStatusReceiver.java
1 package ro.ieval.fonbot;
2
3 import org.eclipse.jdt.annotation.Nullable;
4
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;
10
11 /*
12 * Copyright © 2013 Marius Gavrilescu
13 *
14 * This file is part of FonBot.
15 *
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.
20 *
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.
25 *
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/>.
28 */
29
30 /**
31 * BroadcastReceiver that receives SMS status broadcasts, notifying the user.
32 *
33 * @author Marius Gavrilescu <marius@ieval.ro>
34 */
35 public final class SmsStatusReceiver extends BroadcastReceiver {
36 /**
37 * Formats a SMS message intent into a human-readable string
38 *
39 * @param intent the SMS message
40 * @return the string representation of the SMS
41 */
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)+")";
45 }
46
47 /**
48 * Broadcast action: SMS sent
49 */
50 public static final String SENT_ACTION="ro.ieval.fonbot.SmsStatusReceiver.SENT";
51 /**
52 * Broadcast action: SMS delivered
53 */
54 public static final String DELIVERED_ACTION="ro.ieval.fonbot.SmsStatusReceiver.DELIVERED";
55 /**
56 * Extra: SMS destination (phone number)
57 */
58 public static final String EXTRA_DESTINATION="destination";
59 /**
60 * Extra: SMS part number
61 */
62 public static final String EXTRA_PART="part";
63 /**
64 * Extra: SMS part count
65 */
66 public static final String EXTRA_TOTAL="total";
67 /**
68 * Extra: Notification address
69 */
70 public static final String EXTRA_REPLY_TO="reply_to";
71
72 @Override
73 public void onReceive(@Nullable final Context context, @Nullable final Intent intent) {
74 if(intent==null||context==null)
75 return;
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));
81 break;
82 case SmsManager.RESULT_ERROR_GENERIC_FAILURE:
83 Utils.sendMessage(context, replyTo, "SMS not sent: Generic failure"+describeSMS(intent));
84 break;
85 case SmsManager.RESULT_ERROR_NO_SERVICE:
86 Utils.sendMessage(context, replyTo, "SMS not sent: No service"+describeSMS(intent));
87 break;
88 case SmsManager.RESULT_ERROR_NULL_PDU:
89 Utils.sendMessage(context, replyTo, "SMS not sent: Null PDU"+describeSMS(intent));
90 break;
91 case SmsManager.RESULT_ERROR_RADIO_OFF:
92 Utils.sendMessage(context, replyTo, "SMS not sent: Radio off"+describeSMS(intent));
93 break;
94 }
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));
99 break;
100 case Activity.RESULT_CANCELED:
101 Utils.sendMessage(context, replyTo, "SMS not delivered"+describeSMS(intent));
102 break;
103 }
104 }
105 }
This page took 0.024569 seconds and 4 git commands to generate.