diff options
Diffstat (limited to 'android')
-rw-r--r-- | android/src/org/rockbox/RockboxService.java | 45 |
1 files changed, 45 insertions, 0 deletions
diff --git a/android/src/org/rockbox/RockboxService.java b/android/src/org/rockbox/RockboxService.java index 8ef6b5a302..6dc69b3404 100644 --- a/android/src/org/rockbox/RockboxService.java +++ b/android/src/org/rockbox/RockboxService.java | |||
@@ -9,6 +9,8 @@ import java.io.IOException; | |||
9 | import java.lang.reflect.InvocationTargetException; | 9 | import java.lang.reflect.InvocationTargetException; |
10 | import java.lang.reflect.Method; | 10 | import java.lang.reflect.Method; |
11 | import java.util.Enumeration; | 11 | import java.util.Enumeration; |
12 | import java.util.Timer; | ||
13 | import java.util.TimerTask; | ||
12 | import java.util.zip.ZipEntry; | 14 | import java.util.zip.ZipEntry; |
13 | import java.util.zip.ZipFile; | 15 | import java.util.zip.ZipFile; |
14 | 16 | ||
@@ -16,7 +18,10 @@ import android.app.Notification; | |||
16 | import android.app.NotificationManager; | 18 | import android.app.NotificationManager; |
17 | import android.app.PendingIntent; | 19 | import android.app.PendingIntent; |
18 | import android.app.Service; | 20 | import android.app.Service; |
21 | import android.content.BroadcastReceiver; | ||
22 | import android.content.Context; | ||
19 | import android.content.Intent; | 23 | import android.content.Intent; |
24 | import android.content.IntentFilter; | ||
20 | import android.os.IBinder; | 25 | import android.os.IBinder; |
21 | import android.util.Log; | 26 | import android.util.Log; |
22 | 27 | ||
@@ -36,6 +41,9 @@ public class RockboxService extends Service | |||
36 | private Method mStopForeground; | 41 | private Method mStopForeground; |
37 | private Object[] mStartForegroundArgs = new Object[2]; | 42 | private Object[] mStartForegroundArgs = new Object[2]; |
38 | private Object[] mStopForegroundArgs = new Object[1]; | 43 | private Object[] mStopForegroundArgs = new Object[1]; |
44 | private IntentFilter itf; | ||
45 | private BroadcastReceiver batt_monitor; | ||
46 | private int battery_level; | ||
39 | @Override | 47 | @Override |
40 | public void onCreate() | 48 | public void onCreate() |
41 | { | 49 | { |
@@ -161,6 +169,43 @@ public class RockboxService extends Service | |||
161 | return null; | 169 | return null; |
162 | } | 170 | } |
163 | 171 | ||
172 | |||
173 | @SuppressWarnings("unused") | ||
174 | /* | ||
175 | * Sets up the battery monitor which receives the battery level | ||
176 | * about each 30 seconds | ||
177 | */ | ||
178 | private void initBatteryMonitor() | ||
179 | { | ||
180 | itf = new IntentFilter(Intent.ACTION_BATTERY_CHANGED); | ||
181 | batt_monitor = new BroadcastReceiver() | ||
182 | { | ||
183 | @Override | ||
184 | public void onReceive(Context context, Intent intent) | ||
185 | { | ||
186 | /* we get literally spammed with battery statuses | ||
187 | * if we don't delay the re-attaching | ||
188 | */ | ||
189 | TimerTask tk = new TimerTask() { | ||
190 | public void run() { | ||
191 | registerReceiver(batt_monitor, itf); | ||
192 | } | ||
193 | }; | ||
194 | Timer t = new Timer(); | ||
195 | context.unregisterReceiver(this); | ||
196 | int rawlevel = intent.getIntExtra("level", -1); | ||
197 | int scale = intent.getIntExtra("scale", -1); | ||
198 | if (rawlevel >= 0 && scale > 0) | ||
199 | battery_level = (rawlevel * 100) / scale; | ||
200 | else | ||
201 | battery_level = -1; | ||
202 | /* query every 30s should be sufficient */ | ||
203 | t.schedule(tk, 30000); | ||
204 | } | ||
205 | }; | ||
206 | registerReceiver(batt_monitor, itf); | ||
207 | } | ||
208 | |||
164 | /* all below is heavily based on the examples found on | 209 | /* all below is heavily based on the examples found on |
165 | * http://developer.android.com/reference/android/app/Service.html | 210 | * http://developer.android.com/reference/android/app/Service.html |
166 | */ | 211 | */ |