]>
Commit | Line | Data |
---|---|---|
8dfb76c9 MG |
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), | |
65 | toNonNull(context.getString(phone_status_idle))); | |
66 | break; | |
67 | case TelephonyManager.CALL_STATE_RINGING: | |
68 | if(incomingNumber==null) | |
69 | return; | |
70 | final String name=Utils.callerId(context, incomingNumber); | |
71 | if(name==null) | |
72 | Utils.sendMessage(context, toNonNull(MessageType.PHONE_STATE), | |
73 | context.getString(phone_status_ringing)+incomingNumber); | |
74 | else | |
75 | Utils.sendMessage(context, toNonNull(MessageType.PHONE_STATE), | |
76 | context.getString(phone_status_ringing)+incomingNumber+" ("+name+")"); | |
77 | break; | |
78 | case TelephonyManager.CALL_STATE_OFFHOOK: | |
79 | Utils.sendMessage(context, toNonNull(MessageType.PHONE_STATE), | |
80 | toNonNull(context.getString(phone_status_offhook))); | |
81 | break; | |
82 | } | |
83 | } | |
84 | } |