| 1 | package ro.ieval.fonbot; |
| 2 | |
| 3 | import static ro.ieval.fonbot.R.string.phone_status_idle; |
| 4 | import static ro.ieval.fonbot.R.string.phone_status_offhook; |
| 5 | import static ro.ieval.fonbot.R.string.phone_status_ringing; |
| 6 | import static ro.ieval.fonbot.Utils.toNonNull; |
| 7 | |
| 8 | import org.eclipse.jdt.annotation.Nullable; |
| 9 | |
| 10 | import ro.ieval.fonbot.Utils.MessageType; |
| 11 | import android.content.Context; |
| 12 | import android.telephony.PhoneStateListener; |
| 13 | import android.telephony.TelephonyManager; |
| 14 | |
| 15 | /* |
| 16 | * Copyright © 2013 Marius Gavrilescu |
| 17 | * |
| 18 | * This file is part of FonBot. |
| 19 | * |
| 20 | * FonBot is free software: you can redistribute it and/or modify |
| 21 | * it under the terms of the GNU General Public License as published by |
| 22 | * the Free Software Foundation, either version 3 of the License, or |
| 23 | * (at your option) any later version. |
| 24 | * |
| 25 | * FonBot is distributed in the hope that it will be useful, |
| 26 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 27 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 28 | * GNU General Public License for more details. |
| 29 | * |
| 30 | * You should have received a copy of the GNU General Public License |
| 31 | * along with FonBot. If not, see <http://www.gnu.org/licenses/>. |
| 32 | */ |
| 33 | |
| 34 | /** |
| 35 | * PhoneStateListener that sends {@link MessageType#PHONE_STATE} notifications. |
| 36 | * |
| 37 | * @author Marius Gavrilescu <marius@ieval.ro> |
| 38 | */ |
| 39 | final class FonBotPhoneStateListener extends PhoneStateListener { |
| 40 | /** |
| 41 | * Context instance used in this class. |
| 42 | */ |
| 43 | private final Context context; |
| 44 | |
| 45 | /** |
| 46 | * Construct a FonBotPhoneStateListener with a given context. |
| 47 | * |
| 48 | * @param context Context instance |
| 49 | */ |
| 50 | public FonBotPhoneStateListener(final Context context) { |
| 51 | super(); |
| 52 | this.context=context; |
| 53 | } |
| 54 | |
| 55 | @Override |
| 56 | public void onCallStateChanged(final int state, @Nullable final String incomingNumber) { |
| 57 | @SuppressWarnings("hiding") |
| 58 | final Context context=this.context; |
| 59 | if(context==null) |
| 60 | return; |
| 61 | |
| 62 | switch(state){ |
| 63 | case TelephonyManager.CALL_STATE_IDLE: |
| 64 | Utils.sendMessage(context, toNonNull(MessageType.PHONE_STATE), phone_status_idle); |
| 65 | break; |
| 66 | case TelephonyManager.CALL_STATE_RINGING: |
| 67 | if(incomingNumber==null) |
| 68 | return; |
| 69 | final String name=Utils.callerId(context, incomingNumber); |
| 70 | if(name==null) |
| 71 | Utils.sendMessage(context, toNonNull(MessageType.PHONE_STATE), phone_status_ringing, incomingNumber); |
| 72 | else |
| 73 | Utils.sendMessage(context, toNonNull(MessageType.PHONE_STATE), |
| 74 | phone_status_ringing, incomingNumber + " ("+name+")"); |
| 75 | break; |
| 76 | case TelephonyManager.CALL_STATE_OFFHOOK: |
| 77 | Utils.sendMessage(context, toNonNull(MessageType.PHONE_STATE), phone_status_offhook); |
| 78 | break; |
| 79 | } |
| 80 | } |
| 81 | } |