| 1 | package ro.ieval.fonbot; |
| 2 | |
| 3 | import static ro.ieval.fonbot.R.string.*; |
| 4 | import org.eclipse.jdt.annotation.Nullable; |
| 5 | |
| 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; |
| 11 | |
| 12 | /* |
| 13 | * Copyright © 2013 Marius Gavrilescu |
| 14 | * |
| 15 | * This file is part of FonBot. |
| 16 | * |
| 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. |
| 21 | * |
| 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. |
| 26 | * |
| 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/>. |
| 29 | */ |
| 30 | |
| 31 | /** |
| 32 | * BroadcastReceiver that receives SMS status broadcasts, notifying the user. |
| 33 | * |
| 34 | * @author Marius Gavrilescu <marius@ieval.ro> |
| 35 | */ |
| 36 | public final class SmsStatusReceiver extends BroadcastReceiver { |
| 37 | /** |
| 38 | * Formats a SMS message intent into a human-readable string |
| 39 | * |
| 40 | * @param intent the SMS message |
| 41 | * @return the string representation of the SMS |
| 42 | */ |
| 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); |
| 46 | } |
| 47 | |
| 48 | /** |
| 49 | * Broadcast action: SMS sent |
| 50 | */ |
| 51 | public static final String SENT_ACTION="ro.ieval.fonbot.SmsStatusReceiver.SENT"; |
| 52 | /** |
| 53 | * Broadcast action: SMS delivered |
| 54 | */ |
| 55 | public static final String DELIVERED_ACTION="ro.ieval.fonbot.SmsStatusReceiver.DELIVERED"; |
| 56 | /** |
| 57 | * Extra: SMS destination (phone number) |
| 58 | */ |
| 59 | public static final String EXTRA_DESTINATION="destination"; |
| 60 | /** |
| 61 | * Extra: SMS part number |
| 62 | */ |
| 63 | public static final String EXTRA_PART="part"; |
| 64 | /** |
| 65 | * Extra: SMS part count |
| 66 | */ |
| 67 | public static final String EXTRA_TOTAL="total"; |
| 68 | /** |
| 69 | * Extra: Notification address |
| 70 | */ |
| 71 | public static final String EXTRA_REPLY_TO="reply_to"; |
| 72 | |
| 73 | @Override |
| 74 | public void onReceive(@Nullable final Context context, @Nullable final Intent intent) { |
| 75 | if(intent==null||context==null) |
| 76 | return; |
| 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.sendMessage(context, replyTo, sms_sent, describeSMS(intent)); |
| 82 | break; |
| 83 | case SmsManager.RESULT_ERROR_GENERIC_FAILURE: |
| 84 | Utils.sendMessage(context, replyTo, sms_not_sent_generic_failure, describeSMS(intent)); |
| 85 | break; |
| 86 | case SmsManager.RESULT_ERROR_NO_SERVICE: |
| 87 | Utils.sendMessage(context, replyTo, sms_not_sent_no_service, describeSMS(intent)); |
| 88 | break; |
| 89 | case SmsManager.RESULT_ERROR_NULL_PDU: |
| 90 | Utils.sendMessage(context, replyTo, sms_not_sent_null_pdu, describeSMS(intent)); |
| 91 | break; |
| 92 | case SmsManager.RESULT_ERROR_RADIO_OFF: |
| 93 | Utils.sendMessage(context, replyTo, sms_not_sent_radio_off, describeSMS(intent)); |
| 94 | break; |
| 95 | } |
| 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)); |
| 100 | break; |
| 101 | case Activity.RESULT_CANCELED: |
| 102 | Utils.sendMessage(context, replyTo, sms_not_delivered, describeSMS(intent)); |
| 103 | break; |
| 104 | } |
| 105 | } |
| 106 | } |