Android port: handle incoming calls.
[maemo-rb.git] / android / src / org / rockbox / RockboxTimer.java
blob68a0e866fbb80afe6fc155649f53f5486390a846
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
10 * Copyright (C) 2010 Thomas Martitz
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.
17 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
18 * KIND, either express or implied.
20 ****************************************************************************/
22 package org.rockbox;
24 import java.util.Timer;
25 import java.util.TimerTask;
27 import android.content.Context;
28 import android.telephony.TelephonyManager;
29 import android.util.Log;
31 public class RockboxTimer extends Timer
33 RockboxTimerTask task;
34 long interval;
36 private class RockboxTimerTask extends TimerTask {
37 private RockboxTimer t;
38 private TelephonyManager tm;
39 private int last_state;
40 public RockboxTimerTask(RockboxService s, RockboxTimer parent) {
41 super();
42 t = parent;
43 tm = (TelephonyManager)s.getSystemService(Context.TELEPHONY_SERVICE);
44 last_state = tm.getCallState();
47 @Override
48 public void run() {
49 timerTask();
50 int state = tm.getCallState();
51 if (state != last_state)
53 switch (state) {
54 case TelephonyManager.CALL_STATE_IDLE:
55 postCallHungUp();
56 break;
57 case TelephonyManager.CALL_STATE_RINGING:
58 postCallIncoming();
59 default:
60 break;
62 last_state = state;
64 synchronized(t) {
65 t.notify();
70 public RockboxTimer(RockboxService instance, long period_inverval_in_ms)
72 super("tick timer", false);
73 task = new RockboxTimerTask(instance, this);
74 schedule(task, 0, period_inverval_in_ms);
75 interval = period_inverval_in_ms;
78 private void LOG(CharSequence text)
80 Log.d("Rockbox", (String) text);
84 /* methods called from native, keep them simple */
85 public void java_wait_for_interrupt()
87 synchronized(this) {
88 try {
89 this.wait();
90 } catch (InterruptedException e) {
91 /* wakeup and return */
92 } catch (Exception e) {
93 LOG(e.toString());
97 public native void timerTask();
98 private native void postCallIncoming();
99 private native void postCallHungUp();