Use nanoseconds to detect delayed callbacks

This commit is contained in:
Adrian Ulrich 2015-11-08 15:55:01 +01:00
parent 3459a421b5
commit 9b39b1e944
3 changed files with 43 additions and 8 deletions

View File

@ -35,7 +35,6 @@ import android.os.HandlerThread;
import android.os.Looper;
import android.os.Message;
import android.os.Process;
import android.os.SystemClock;
import android.os.Environment;
import android.view.ContextMenu;
import android.view.KeyEvent;
@ -237,7 +236,7 @@ public abstract class PlaybackActivity extends Activity
protected void setState(final int state)
{
mLastStateEvent = SystemClock.uptimeMillis();
mLastStateEvent = System.nanoTime();
if (mState != state) {
final int toggled = mState ^ state;
@ -257,7 +256,7 @@ public abstract class PlaybackActivity extends Activity
*/
public void setState(long uptime, int state)
{
if (uptime > mLastStateEvent)
if (uptime >= mLastStateEvent)
setState(state);
}
@ -285,7 +284,7 @@ public abstract class PlaybackActivity extends Activity
protected void setSong(final Song song)
{
mLastSongEvent = SystemClock.uptimeMillis();
mLastSongEvent = System.nanoTime();
runOnUiThread(new Runnable() {
@Override
public void run()
@ -311,7 +310,7 @@ public abstract class PlaybackActivity extends Activity
*/
public void setSong(long uptime, Song song)
{
if (uptime > mLastSongEvent)
if (uptime >= mLastSongEvent)
setSong(song);
}

View File

@ -942,7 +942,7 @@ public final class PlaybackService extends Service
if (state != oldState) {
mHandler.sendMessage(mHandler.obtainMessage(MSG_PROCESS_STATE, oldState, state));
mHandler.sendMessage(mHandler.obtainMessage(MSG_BROADCAST_CHANGE, state, 0));
mHandler.sendMessage(mHandler.obtainMessage(MSG_BROADCAST_CHANGE, state, 0, new TimestampedObject(null)));
}
return state;
@ -1292,7 +1292,7 @@ public final class PlaybackService extends Service
mMediaPlayerInitialized = false;
mHandler.sendMessage(mHandler.obtainMessage(MSG_PROCESS_SONG, song));
mHandler.sendMessage(mHandler.obtainMessage(MSG_BROADCAST_CHANGE, -1, 0, song));
mHandler.sendMessage(mHandler.obtainMessage(MSG_BROADCAST_CHANGE, -1, 0, new TimestampedObject(song)));
return song;
}
@ -1515,7 +1515,8 @@ public final class PlaybackService extends Service
processNewState(message.arg1, message.arg2);
break;
case MSG_BROADCAST_CHANGE:
broadcastChange(message.arg1, (Song)message.obj, message.getWhen());
TimestampedObject tso = (TimestampedObject)message.obj;
broadcastChange(message.arg1, (Song)tso.object, tso.uptime);
break;
case MSG_ENTER_SLEEP_STATE:
enterSleepState();

View File

@ -0,0 +1,35 @@
/*
* Copyright (C) 2015 Adrian Ulrich <adrian@blinkenlights.ch>
*
* 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 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package ch.blinkenlights.android.vanilla;
public class TimestampedObject {
public long uptime;
public Object object;
/**
* Encapsulates given object and marks the creation timestamp
* in nanoseconds
*/
public TimestampedObject(Object object) {
this.object = object;
this.uptime = System.nanoTime();
}
}