Clean up sendMessage calls
[fonbot.git] / src / ro / ieval / fonbot / FonBotPhoneStateListener.java
CommitLineData
8dfb76c9
MG
1package ro.ieval.fonbot;
2
3import static ro.ieval.fonbot.R.string.phone_status_idle;
4import static ro.ieval.fonbot.R.string.phone_status_offhook;
5import static ro.ieval.fonbot.R.string.phone_status_ringing;
6import static ro.ieval.fonbot.Utils.toNonNull;
7
8import org.eclipse.jdt.annotation.Nullable;
9
10import ro.ieval.fonbot.Utils.MessageType;
11import android.content.Context;
12import android.telephony.PhoneStateListener;
13import 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 */
39final 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:
e4e4926f 64 Utils.sendMessage(context, toNonNull(MessageType.PHONE_STATE), phone_status_idle);
8dfb76c9
MG
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)
e4e4926f 71 Utils.sendMessage(context, toNonNull(MessageType.PHONE_STATE), phone_status_ringing, incomingNumber);
8dfb76c9
MG
72 else
73 Utils.sendMessage(context, toNonNull(MessageType.PHONE_STATE),
e4e4926f 74 phone_status_ringing, incomingNumber + " ("+name+")");
8dfb76c9
MG
75 break;
76 case TelephonyManager.CALL_STATE_OFFHOOK:
e4e4926f 77 Utils.sendMessage(context, toNonNull(MessageType.PHONE_STATE), phone_status_offhook);
8dfb76c9
MG
78 break;
79 }
80 }
81}
This page took 0.015157 seconds and 4 git commands to generate.