diff options
Diffstat (limited to 'android/src/org/rockbox/RockboxTelephony.java')
-rw-r--r-- | android/src/org/rockbox/RockboxTelephony.java | 97 |
1 files changed, 97 insertions, 0 deletions
diff --git a/android/src/org/rockbox/RockboxTelephony.java b/android/src/org/rockbox/RockboxTelephony.java new file mode 100644 index 0000000000..faaf0c36b7 --- /dev/null +++ b/android/src/org/rockbox/RockboxTelephony.java | |||
@@ -0,0 +1,97 @@ | |||
1 | /*************************************************************************** | ||
2 | * __________ __ ___. | ||
3 | * Open \______ \ ____ ____ | | _\_ |__ _______ ___ | ||
4 | * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ / | ||
5 | * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < < | ||
6 | * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \ | ||
7 | * \/ \/ \/ \/ \/ | ||
8 | * $Id$ | ||
9 | * | ||
10 | * Copyright (C) 2010 Thomas Martitz | ||
11 | * | ||
12 | * This program is free software; you can redistribute it and/or | ||
13 | * modify it under the terms of the GNU General Public License | ||
14 | * as published by the Free Software Foundation; either version 2 | ||
15 | * of the License, or (at your option) any later version. | ||
16 | * | ||
17 | * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY | ||
18 | * KIND, either express or implied. | ||
19 | * | ||
20 | ****************************************************************************/ | ||
21 | |||
22 | package org.rockbox; | ||
23 | |||
24 | import android.content.Context; | ||
25 | import android.os.Handler; | ||
26 | import android.telephony.PhoneStateListener; | ||
27 | import android.telephony.TelephonyManager; | ||
28 | |||
29 | public class RockboxTelephony | ||
30 | { | ||
31 | public RockboxTelephony(Context c) | ||
32 | { | ||
33 | final Handler handler = new Handler(c.getMainLooper()); | ||
34 | final TelephonyManager tm = (TelephonyManager) | ||
35 | c.getSystemService(Context.TELEPHONY_SERVICE); | ||
36 | handler.post(new Runnable() | ||
37 | { | ||
38 | @Override | ||
39 | public void run() | ||
40 | { /* need to instantiate from a thread that has a Looper */ | ||
41 | tm.listen(new RockboxCallStateListener(), PhoneStateListener.LISTEN_CALL_STATE); | ||
42 | } | ||
43 | }); | ||
44 | } | ||
45 | |||
46 | private class RockboxCallStateListener extends PhoneStateListener | ||
47 | { | ||
48 | private int last_state; | ||
49 | |||
50 | public RockboxCallStateListener() | ||
51 | { | ||
52 | super(); | ||
53 | /* set artificial initial state, | ||
54 | * we will get an initial event shortly after this, | ||
55 | * so to handle it correctly we need an invalid state set */ | ||
56 | last_state = TelephonyManager.CALL_STATE_IDLE - 10; | ||
57 | } | ||
58 | |||
59 | private void handleState(int state) | ||
60 | { | ||
61 | if (state == last_state) | ||
62 | return; | ||
63 | switch (state) | ||
64 | { | ||
65 | case TelephonyManager.CALL_STATE_IDLE: | ||
66 | postCallHungUp(); | ||
67 | break; | ||
68 | case TelephonyManager.CALL_STATE_RINGING: | ||
69 | postCallIncoming(); | ||
70 | break; | ||
71 | case TelephonyManager.CALL_STATE_OFFHOOK: | ||
72 | /* for incoming calls we handled at RINGING already, | ||
73 | * if the previous state was IDLE then | ||
74 | * this is an outgoing call | ||
75 | */ | ||
76 | if (last_state == TelephonyManager.CALL_STATE_IDLE) | ||
77 | { /* currently handled the same as incoming calls */ | ||
78 | postCallIncoming(); | ||
79 | } | ||
80 | break; | ||
81 | default: | ||
82 | return; | ||
83 | } | ||
84 | last_state = state; | ||
85 | |||
86 | } | ||
87 | |||
88 | @Override | ||
89 | public void onCallStateChanged(int state, String number) | ||
90 | { | ||
91 | super.onCallStateChanged(state, number); | ||
92 | handleState(state); | ||
93 | } | ||
94 | } | ||
95 | private native void postCallIncoming(); | ||
96 | private native void postCallHungUp(); | ||
97 | } | ||