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 | |
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')
-rw-r--r-- | android/src/org/rockbox/RockboxService.java | 81 | ||||
-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 (renamed from android/src/org/rockbox/RockboxTelephony.java) | 6 |
4 files changed, 137 insertions, 84 deletions
diff --git a/android/src/org/rockbox/RockboxService.java b/android/src/org/rockbox/RockboxService.java index 49abb52dd5..20b8d0c806 100644 --- a/android/src/org/rockbox/RockboxService.java +++ b/android/src/org/rockbox/RockboxService.java | |||
@@ -27,20 +27,13 @@ import java.io.File; | |||
27 | import java.io.FileOutputStream; | 27 | import java.io.FileOutputStream; |
28 | import java.io.OutputStreamWriter; | 28 | import java.io.OutputStreamWriter; |
29 | import java.util.Enumeration; | 29 | import java.util.Enumeration; |
30 | import java.util.Timer; | ||
31 | import java.util.TimerTask; | ||
32 | import java.util.zip.ZipEntry; | 30 | import java.util.zip.ZipEntry; |
33 | import java.util.zip.ZipFile; | 31 | import java.util.zip.ZipFile; |
34 | |||
35 | import org.rockbox.Helper.MediaButtonReceiver; | 32 | import org.rockbox.Helper.MediaButtonReceiver; |
36 | import org.rockbox.Helper.RunForegroundManager; | 33 | import org.rockbox.Helper.RunForegroundManager; |
37 | |||
38 | import android.app.Activity; | 34 | import android.app.Activity; |
39 | import android.app.Service; | 35 | import android.app.Service; |
40 | import android.content.BroadcastReceiver; | ||
41 | import android.content.Context; | ||
42 | import android.content.Intent; | 36 | import android.content.Intent; |
43 | import android.content.IntentFilter; | ||
44 | import android.os.Bundle; | 37 | import android.os.Bundle; |
45 | import android.os.Environment; | 38 | import android.os.Environment; |
46 | import android.os.IBinder; | 39 | import android.os.IBinder; |
@@ -63,17 +56,9 @@ public class RockboxService extends Service | |||
63 | /* locals needed for the c code and rockbox state */ | 56 | /* locals needed for the c code and rockbox state */ |
64 | private static volatile boolean rockbox_running; | 57 | private static volatile boolean rockbox_running; |
65 | private Activity current_activity = null; | 58 | private Activity current_activity = null; |
66 | private IntentFilter itf; | ||
67 | private IntentFilter ifh; | ||
68 | private BroadcastReceiver batt_monitor; | ||
69 | private BroadcastReceiver headphone_monitor; | ||
70 | private BroadcastReceiver noisy_monitor; | ||
71 | private RunForegroundManager fg_runner; | 59 | private RunForegroundManager fg_runner; |
72 | private MediaButtonReceiver mMediaButtonReceiver; | 60 | private MediaButtonReceiver mMediaButtonReceiver; |
73 | private int battery_level; | ||
74 | private int headphone_state; | ||
75 | private ResultReceiver resultReceiver; | 61 | private ResultReceiver resultReceiver; |
76 | private RockboxService rbservice; | ||
77 | 62 | ||
78 | public static final int RESULT_INVOKING_MAIN = 0; | 63 | public static final int RESULT_INVOKING_MAIN = 0; |
79 | public static final int RESULT_LIB_LOAD_PROGRESS = 1; | 64 | public static final int RESULT_LIB_LOAD_PROGRESS = 1; |
@@ -314,72 +299,6 @@ public class RockboxService extends Service | |||
314 | return null; | 299 | return null; |
315 | } | 300 | } |
316 | 301 | ||
317 | |||
318 | private void initBatteryMonitor() | ||
319 | { | ||
320 | itf = new IntentFilter(Intent.ACTION_BATTERY_CHANGED); | ||
321 | batt_monitor = new BroadcastReceiver() | ||
322 | { | ||
323 | @Override | ||
324 | public void onReceive(Context context, Intent intent) | ||
325 | { | ||
326 | /* we get literally spammed with battery statuses | ||
327 | * if we don't delay the re-attaching | ||
328 | */ | ||
329 | TimerTask tk = new TimerTask() | ||
330 | { | ||
331 | public void run() | ||
332 | { | ||
333 | registerReceiver(batt_monitor, itf); | ||
334 | } | ||
335 | }; | ||
336 | Timer t = new Timer(); | ||
337 | context.unregisterReceiver(this); | ||
338 | int rawlevel = intent.getIntExtra("level", -1); | ||
339 | int scale = intent.getIntExtra("scale", -1); | ||
340 | if (rawlevel >= 0 && scale > 0) | ||
341 | battery_level = (rawlevel * 100) / scale; | ||
342 | else | ||
343 | battery_level = -1; | ||
344 | /* query every 30s should be sufficient */ | ||
345 | t.schedule(tk, 30000); | ||
346 | } | ||
347 | }; | ||
348 | registerReceiver(batt_monitor, itf); | ||
349 | } | ||
350 | |||
351 | |||
352 | private void initHeadphoneMonitor() | ||
353 | { | ||
354 | ifh = new IntentFilter(Intent.ACTION_HEADSET_PLUG); | ||
355 | headphone_monitor = new BroadcastReceiver() | ||
356 | { | ||
357 | @Override | ||
358 | public void onReceive(Context context, Intent intent) | ||
359 | { | ||
360 | int state = intent.getIntExtra("state", -1); | ||
361 | LOG("headphone state:" + state); | ||
362 | headphone_state = state; | ||
363 | } | ||
364 | }; | ||
365 | registerReceiver(headphone_monitor, ifh); | ||
366 | noisy_monitor = new BroadcastReceiver() | ||
367 | { | ||
368 | @Override | ||
369 | public void onReceive(Context context, Intent intent) | ||
370 | { | ||
371 | LOG("audio becoming noisy"); | ||
372 | headphone_state = 0; | ||
373 | } | ||
374 | }; | ||
375 | rbservice = RockboxService.get_instance(); | ||
376 | /* We're relying on internal API's here, | ||
377 | this can break in the future! */ | ||
378 | rbservice.registerReceiver(noisy_monitor, | ||
379 | new IntentFilter("android.media.AUDIO_BECOMING_NOISY")); | ||
380 | } | ||
381 | |||
382 | |||
383 | void startForeground() | 302 | void startForeground() |
384 | { | 303 | { |
385 | fg_runner.startForeground(); | 304 | fg_runner.startForeground(); |
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 @@ | |||
1 | /*************************************************************************** | ||
2 | * __________ __ ___. | ||
3 | * Open \______ \ ____ ____ | | _\_ |__ _______ ___ | ||
4 | * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ / | ||
5 | * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < < | ||
6 | * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \ | ||
7 | * \/ \/ \/ \/ \/ | ||
8 | * $Id$ | ||
9 | * | ||
10 | * Copyright (C) 2011 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.monitors; | ||
23 | |||
24 | import java.util.Timer; | ||
25 | import java.util.TimerTask; | ||
26 | import android.content.BroadcastReceiver; | ||
27 | import android.content.Context; | ||
28 | import android.content.Intent; | ||
29 | import android.content.IntentFilter; | ||
30 | |||
31 | public class BatteryMonitor extends BroadcastReceiver | ||
32 | { | ||
33 | private final IntentFilter mBattFilter; | ||
34 | private final Context mContext; | ||
35 | @SuppressWarnings("unused") | ||
36 | private int mBattLevel; /* read by native code */ | ||
37 | |||
38 | /* | ||
39 | * We get literally spammed with battery status updates | ||
40 | * Therefore we actually unregister after each onReceive() and | ||
41 | * setup a timer to re-register in 30s */ | ||
42 | public BatteryMonitor(Context c) | ||
43 | { | ||
44 | mBattFilter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED); | ||
45 | mContext = c; | ||
46 | Timer t = new Timer(); | ||
47 | TimerTask task = new TimerTask() | ||
48 | { | ||
49 | public void run() | ||
50 | { | ||
51 | attach(); | ||
52 | } | ||
53 | }; | ||
54 | t.schedule(task, 5000, 30000); | ||
55 | attach(); | ||
56 | } | ||
57 | |||
58 | @Override | ||
59 | public void onReceive(Context arg0, Intent intent) | ||
60 | { | ||
61 | int rawlevel = intent.getIntExtra("level", -1); | ||
62 | int scale = intent.getIntExtra("scale", -1); | ||
63 | if (rawlevel >= 0 && scale > 0) | ||
64 | mBattLevel = (rawlevel * 100) / scale; | ||
65 | else | ||
66 | mBattLevel = -1; | ||
67 | mContext.unregisterReceiver(this); | ||
68 | } | ||
69 | |||
70 | void attach() | ||
71 | { | ||
72 | mContext.registerReceiver(this, mBattFilter); | ||
73 | } | ||
74 | } | ||
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 @@ | |||
1 | /*************************************************************************** | ||
2 | * __________ __ ___. | ||
3 | * Open \______ \ ____ ____ | | _\_ |__ _______ ___ | ||
4 | * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ / | ||
5 | * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < < | ||
6 | * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \ | ||
7 | * \/ \/ \/ \/ \/ | ||
8 | * $Id$ | ||
9 | * | ||
10 | * Copyright (C) 2011 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.monitors; | ||
23 | |||
24 | import android.content.BroadcastReceiver; | ||
25 | import android.content.Context; | ||
26 | import android.content.Intent; | ||
27 | import android.content.IntentFilter; | ||
28 | |||
29 | public class HeadphoneMonitor extends BroadcastReceiver | ||
30 | { | ||
31 | @SuppressWarnings("unused") | ||
32 | private int mHpState; /* read by native code */ | ||
33 | |||
34 | public HeadphoneMonitor(Context c) | ||
35 | { | ||
36 | IntentFilter hpFilter = new IntentFilter(Intent.ACTION_HEADSET_PLUG); | ||
37 | /* caution: hidden API; might break */ | ||
38 | IntentFilter noisyFilter = new IntentFilter("android.media.AUDIO_BECOMING_NOISY"); | ||
39 | |||
40 | c.registerReceiver(this, hpFilter); | ||
41 | c.registerReceiver(new NoisyMonitor(), noisyFilter); | ||
42 | } | ||
43 | |||
44 | @Override | ||
45 | public void onReceive(Context arg0, Intent intent) | ||
46 | { | ||
47 | int state = intent.getIntExtra("state", -1); | ||
48 | mHpState = state; | ||
49 | } | ||
50 | |||
51 | /* audio becoming noise acts as headphones extracted */ | ||
52 | private class NoisyMonitor extends BroadcastReceiver | ||
53 | { | ||
54 | @Override | ||
55 | public void onReceive(Context arg0, Intent arg1) | ||
56 | { | ||
57 | mHpState = 0; | ||
58 | } | ||
59 | } | ||
60 | } | ||
diff --git a/android/src/org/rockbox/RockboxTelephony.java b/android/src/org/rockbox/monitors/TelephonyMonitor.java index faaf0c36b7..6881f243af 100644 --- a/android/src/org/rockbox/RockboxTelephony.java +++ b/android/src/org/rockbox/monitors/TelephonyMonitor.java | |||
@@ -19,16 +19,16 @@ | |||
19 | * | 19 | * |
20 | ****************************************************************************/ | 20 | ****************************************************************************/ |
21 | 21 | ||
22 | package org.rockbox; | 22 | package org.rockbox.monitors; |
23 | 23 | ||
24 | import android.content.Context; | 24 | import android.content.Context; |
25 | import android.os.Handler; | 25 | import android.os.Handler; |
26 | import android.telephony.PhoneStateListener; | 26 | import android.telephony.PhoneStateListener; |
27 | import android.telephony.TelephonyManager; | 27 | import android.telephony.TelephonyManager; |
28 | 28 | ||
29 | public class RockboxTelephony | 29 | public class TelephonyMonitor |
30 | { | 30 | { |
31 | public RockboxTelephony(Context c) | 31 | public TelephonyMonitor(Context c) |
32 | { | 32 | { |
33 | final Handler handler = new Handler(c.getMainLooper()); | 33 | final Handler handler = new Handler(c.getMainLooper()); |
34 | final TelephonyManager tm = (TelephonyManager) | 34 | final TelephonyManager tm = (TelephonyManager) |