diff options
author | Thomas Martitz <kugel@rockbox.org> | 2011-07-18 21:02:47 +0000 |
---|---|---|
committer | Thomas Martitz <kugel@rockbox.org> | 2011-07-18 21:02:47 +0000 |
commit | 33de9cdaefd8684e3411486612e68c0c2bd6be3d (patch) | |
tree | f1219711bfc0c1333ad8ec611a45f7ab0c416216 /android/src/org/rockbox/monitors | |
parent | 788e246c996c9ac6e8efae87d13b14af001fd354 (diff) | |
download | rockbox-33de9cdaefd8684e3411486612e68c0c2bd6be3d.tar.gz rockbox-33de9cdaefd8684e3411486612e68c0c2bd6be3d.zip |
Android: Refactor some of the glue code.
* Cleanup RockboxService.java by moving the battery and
headphone monitors to separate classes and detaching their instances
* Move those monitors and RockboxTelephony.java into a new
monitors subdirectory
* Call those monitors all the same from native code by creating
the objects there
git-svn-id: svn://svn.rockbox.org/rockbox/trunk@30160 a1c6a512-1295-4272-9138-f99709370657
Diffstat (limited to 'android/src/org/rockbox/monitors')
-rw-r--r-- | android/src/org/rockbox/monitors/BatteryMonitor.java | 74 | ||||
-rw-r--r-- | android/src/org/rockbox/monitors/HeadphoneMonitor.java | 60 | ||||
-rw-r--r-- | android/src/org/rockbox/monitors/TelephonyMonitor.java | 97 |
3 files changed, 231 insertions, 0 deletions
diff --git a/android/src/org/rockbox/monitors/BatteryMonitor.java b/android/src/org/rockbox/monitors/BatteryMonitor.java new file mode 100644 index 0000000000..0896a58242 --- /dev/null +++ b/android/src/org/rockbox/monitors/BatteryMonitor.java @@ -0,0 +1,74 @@ +/*************************************************************************** + * __________ __ ___. + * Open \______ \ ____ ____ | | _\_ |__ _______ ___ + * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ / + * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < < + * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \ + * \/ \/ \/ \/ \/ + * $Id$ + * + * Copyright (C) 2011 Thomas Martitz + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY + * KIND, either express or implied. + * + ****************************************************************************/ + +package org.rockbox.monitors; + +import java.util.Timer; +import java.util.TimerTask; +import android.content.BroadcastReceiver; +import android.content.Context; +import android.content.Intent; +import android.content.IntentFilter; + +public class BatteryMonitor extends BroadcastReceiver +{ + private final IntentFilter mBattFilter; + private final Context mContext; + @SuppressWarnings("unused") + private int mBattLevel; /* read by native code */ + + /* + * We get literally spammed with battery status updates + * Therefore we actually unregister after each onReceive() and + * setup a timer to re-register in 30s */ + public BatteryMonitor(Context c) + { + mBattFilter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED); + mContext = c; + Timer t = new Timer(); + TimerTask task = new TimerTask() + { + public void run() + { + attach(); + } + }; + t.schedule(task, 5000, 30000); + attach(); + } + + @Override + public void onReceive(Context arg0, Intent intent) + { + int rawlevel = intent.getIntExtra("level", -1); + int scale = intent.getIntExtra("scale", -1); + if (rawlevel >= 0 && scale > 0) + mBattLevel = (rawlevel * 100) / scale; + else + mBattLevel = -1; + mContext.unregisterReceiver(this); + } + + void attach() + { + mContext.registerReceiver(this, mBattFilter); + } +} diff --git a/android/src/org/rockbox/monitors/HeadphoneMonitor.java b/android/src/org/rockbox/monitors/HeadphoneMonitor.java new file mode 100644 index 0000000000..99d2f7ab8a --- /dev/null +++ b/android/src/org/rockbox/monitors/HeadphoneMonitor.java @@ -0,0 +1,60 @@ +/*************************************************************************** + * __________ __ ___. + * Open \______ \ ____ ____ | | _\_ |__ _______ ___ + * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ / + * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < < + * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \ + * \/ \/ \/ \/ \/ + * $Id$ + * + * Copyright (C) 2011 Thomas Martitz + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY + * KIND, either express or implied. + * + ****************************************************************************/ + +package org.rockbox.monitors; + +import android.content.BroadcastReceiver; +import android.content.Context; +import android.content.Intent; +import android.content.IntentFilter; + +public class HeadphoneMonitor extends BroadcastReceiver +{ + @SuppressWarnings("unused") + private int mHpState; /* read by native code */ + + public HeadphoneMonitor(Context c) + { + IntentFilter hpFilter = new IntentFilter(Intent.ACTION_HEADSET_PLUG); + /* caution: hidden API; might break */ + IntentFilter noisyFilter = new IntentFilter("android.media.AUDIO_BECOMING_NOISY"); + + c.registerReceiver(this, hpFilter); + c.registerReceiver(new NoisyMonitor(), noisyFilter); + } + + @Override + public void onReceive(Context arg0, Intent intent) + { + int state = intent.getIntExtra("state", -1); + mHpState = state; + } + + /* audio becoming noise acts as headphones extracted */ + private class NoisyMonitor extends BroadcastReceiver + { + @Override + public void onReceive(Context arg0, Intent arg1) + { + mHpState = 0; + } + } +} diff --git a/android/src/org/rockbox/monitors/TelephonyMonitor.java b/android/src/org/rockbox/monitors/TelephonyMonitor.java new file mode 100644 index 0000000000..6881f243af --- /dev/null +++ b/android/src/org/rockbox/monitors/TelephonyMonitor.java @@ -0,0 +1,97 @@ +/*************************************************************************** + * __________ __ ___. + * Open \______ \ ____ ____ | | _\_ |__ _______ ___ + * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ / + * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < < + * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \ + * \/ \/ \/ \/ \/ + * $Id$ + * + * Copyright (C) 2010 Thomas Martitz + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY + * KIND, either express or implied. + * + ****************************************************************************/ + +package org.rockbox.monitors; + +import android.content.Context; +import android.os.Handler; +import android.telephony.PhoneStateListener; +import android.telephony.TelephonyManager; + +public class TelephonyMonitor +{ + public TelephonyMonitor(Context c) + { + final Handler handler = new Handler(c.getMainLooper()); + final TelephonyManager tm = (TelephonyManager) + c.getSystemService(Context.TELEPHONY_SERVICE); + handler.post(new Runnable() + { + @Override + public void run() + { /* need to instantiate from a thread that has a Looper */ + tm.listen(new RockboxCallStateListener(), PhoneStateListener.LISTEN_CALL_STATE); + } + }); + } + + private class RockboxCallStateListener extends PhoneStateListener + { + private int last_state; + + public RockboxCallStateListener() + { + super(); + /* set artificial initial state, + * we will get an initial event shortly after this, + * so to handle it correctly we need an invalid state set */ + last_state = TelephonyManager.CALL_STATE_IDLE - 10; + } + + private void handleState(int state) + { + if (state == last_state) + return; + switch (state) + { + case TelephonyManager.CALL_STATE_IDLE: + postCallHungUp(); + break; + case TelephonyManager.CALL_STATE_RINGING: + postCallIncoming(); + break; + case TelephonyManager.CALL_STATE_OFFHOOK: + /* for incoming calls we handled at RINGING already, + * if the previous state was IDLE then + * this is an outgoing call + */ + if (last_state == TelephonyManager.CALL_STATE_IDLE) + { /* currently handled the same as incoming calls */ + postCallIncoming(); + } + break; + default: + return; + } + last_state = state; + + } + + @Override + public void onCallStateChanged(int state, String number) + { + super.onCallStateChanged(state, number); + handleState(state); + } + } + private native void postCallIncoming(); + private native void postCallHungUp(); +} |