Index: firmware/target/hosted/android/kernel-android.c =================================================================== --- firmware/target/hosted/android/kernel-android.c (revision 30701) +++ firmware/target/hosted/android/kernel-android.c (working copy) @@ -19,7 +19,7 @@ * ****************************************************************************/ - +#include #include #include #include @@ -67,21 +67,25 @@ * * Can be possibly be attached if it really needs to be. but let's * keep this leightweight */ +static timer_t timerid = 0; +static int nsec; + void tick_start(unsigned int interval_in_ms) { int ret = 0; - timer_t timerid; struct itimerspec ts; sigevent_t sigev; /* initializing in the declaration causes some weird warnings */ memset(&sigev, 0, sizeof(sigevent_t)); - sigev.sigev_notify = SIGEV_THREAD, - sigev.sigev_notify_function = timer_signal, + sigev.sigev_notify = SIGEV_THREAD; + sigev.sigev_notify_function = timer_signal; + nsec = interval_in_ms * 1000000; ts.it_value.tv_sec = ts.it_interval.tv_sec = 0; - ts.it_value.tv_nsec = ts.it_interval.tv_nsec = interval_in_ms*1000*1000; + ts.it_value.tv_nsec = ts.it_interval.tv_nsec = nsec; + /* add the timer */ ret |= timer_create(CLOCK_REALTIME, &sigev, &timerid); ret |= timer_settime(timerid, 0, &ts, NULL); @@ -95,7 +99,27 @@ panicf("%s(): %s\n", __func__, strerror(errno)); } +JNIEXPORT void JNICALL +Java_org_rockbox_RockboxService_tickpause(JNIEnv*env, jobject this) +{ + (void)env; + (void)this; + const struct itimerspec newtimer = { {0, 0}, {0, 0} }; + if (timerid) + timer_settime(timerid, 0, &newtimer, NULL); +} +JNIEXPORT void JNICALL +Java_org_rockbox_RockboxService_tickresume(JNIEnv*env, jobject this) +{ + (void)env; + (void)this; + const struct itimerspec newtimer = { {0, nsec}, {0, 0} }; + + if (timerid) + timer_settime(timerid, 0, &newtimer, NULL); +} + bool timer_register(int reg_prio, void (*unregister_callback)(void), long cycles, void (*timer_callback)(void)) { Index: android/src/org/rockbox/RockboxService.java =================================================================== --- android/src/org/rockbox/RockboxService.java (revision 30701) +++ android/src/org/rockbox/RockboxService.java (working copy) @@ -60,6 +60,10 @@ private MediaButtonReceiver mMediaButtonReceiver; private ResultReceiver resultReceiver; + private boolean music_playing = false; + private boolean app_open = false; + private boolean paused = false; + public static final int RESULT_INVOKING_MAIN = 0; public static final int RESULT_LIB_LOAD_PROGRESS = 1; public static final int RESULT_SERVICE_RUNNING = 3; @@ -90,9 +94,37 @@ { return current_activity; } + + private native void tickpause(); + private native void tickresume(); + + private void check_sleep() + { + if (paused) + { + if (music_playing || app_open) + { + LOG("Tick resume"); + tickresume(); + paused = false; + } + } + else + { + if (!music_playing && !app_open) + { + LOG("Tick pause"); + tickpause(); + paused = true; + } + } + } + public void set_activity(Activity a) { + app_open = (a != null); current_activity = a; + check_sleep(); } private void do_start(Intent intent) @@ -138,20 +170,22 @@ private void LOG(CharSequence text) { - Log.d("Rockbox", (String) text); + Log.d("RockboxService", (String) text); } private void LOG(CharSequence text, Throwable tr) { - Log.d("Rockbox", (String) text, tr); + Log.d("RockboxService", (String) text, tr); } public void onStart(Intent intent, int startId) { + LOG("onStart"); do_start(intent); } public int onStartCommand(Intent intent, int flags, int startId) { + LOG("onStartCommand"); /* if null, then the service was most likely restarted by android * after getting killed for memory pressure earlier */ if (intent == null) @@ -324,12 +358,18 @@ void startForeground() { + LOG("startForeground"); + music_playing = true; + check_sleep(); fg_runner.startForeground(); } void stopForeground() { + LOG("stopForeground"); + music_playing = false; fg_runner.stopForeground(); + check_sleep(); } @Override