am 9c43016b: Merge "Bug 5045498 Keep track of RemoteControlClient play state change time" into ics-factoryrom

* commit '9c43016b52afa855156aac2dc81a2bbe1e4ab436':
  Bug 5045498 Keep track of RemoteControlClient play state change time
This commit is contained in:
Jean-Michel Trivi
2011-09-21 09:36:56 -07:00
committed by Android Git Automerger
3 changed files with 49 additions and 7 deletions

View File

@@ -35,6 +35,7 @@ import android.os.Bundle;
import android.os.Handler; import android.os.Handler;
import android.os.Message; import android.os.Message;
import android.os.RemoteException; import android.os.RemoteException;
import android.os.SystemClock;
import android.text.Spannable; import android.text.Spannable;
import android.text.TextUtils; import android.text.TextUtils;
import android.text.style.ForegroundColorSpan; import android.text.style.ForegroundColorSpan;
@@ -59,6 +60,7 @@ public class TransportControlView extends FrameLayout implements OnClickListener
private static final int MSG_SET_ARTWORK = 103; private static final int MSG_SET_ARTWORK = 103;
private static final int MSG_SET_GENERATION_ID = 104; private static final int MSG_SET_GENERATION_ID = 104;
private static final int MAXDIM = 512; private static final int MAXDIM = 512;
private static final int DISPLAY_TIMEOUT_MS = 5000; // 5s
protected static final boolean DEBUG = true; protected static final boolean DEBUG = true;
protected static final String TAG = "TransportControlView"; protected static final String TAG = "TransportControlView";
@@ -142,7 +144,7 @@ public class TransportControlView extends FrameLayout implements OnClickListener
mLocalHandler = new WeakReference<Handler>(handler); mLocalHandler = new WeakReference<Handler>(handler);
} }
public void setPlaybackState(int generationId, int state) { public void setPlaybackState(int generationId, int state, long stateChangeTimeMs) {
Handler handler = mLocalHandler.get(); Handler handler = mLocalHandler.get();
if (handler != null) { if (handler != null) {
handler.obtainMessage(MSG_UPDATE_STATE, generationId, state).sendToTarget(); handler.obtainMessage(MSG_UPDATE_STATE, generationId, state).sendToTarget();
@@ -401,4 +403,33 @@ public class TransportControlView extends FrameLayout implements OnClickListener
return false; return false;
} }
private boolean wasPlayingRecently(int state, long stateChangeTimeMs) {
switch (state) {
case RemoteControlClient.PLAYSTATE_PLAYING:
case RemoteControlClient.PLAYSTATE_FAST_FORWARDING:
case RemoteControlClient.PLAYSTATE_REWINDING:
case RemoteControlClient.PLAYSTATE_SKIPPING_FORWARDS:
case RemoteControlClient.PLAYSTATE_SKIPPING_BACKWARDS:
case RemoteControlClient.PLAYSTATE_BUFFERING:
// actively playing or about to play
return true;
case RemoteControlClient.PLAYSTATE_NONE:
return false;
case RemoteControlClient.PLAYSTATE_STOPPED:
case RemoteControlClient.PLAYSTATE_PAUSED:
case RemoteControlClient.PLAYSTATE_ERROR:
// we have stopped playing, check how long ago
if (DEBUG) {
if ((SystemClock.elapsedRealtime() - stateChangeTimeMs) < DISPLAY_TIMEOUT_MS) {
Log.v(TAG, "wasPlayingRecently: time < TIMEOUT was playing recently");
} else {
Log.v(TAG, "wasPlayingRecently: time > TIMEOUT");
}
}
return ((SystemClock.elapsedRealtime() - stateChangeTimeMs) < DISPLAY_TIMEOUT_MS);
default:
Log.e(TAG, "Unknown playback state " + state + " in wasPlayingRecently()");
return false;
}
}
} }

View File

@@ -40,7 +40,7 @@ oneway interface IRemoteControlDisplay
void setCurrentClientId(int clientGeneration, in PendingIntent clientMediaIntent, void setCurrentClientId(int clientGeneration, in PendingIntent clientMediaIntent,
boolean clearing); boolean clearing);
void setPlaybackState(int generationId, int state); void setPlaybackState(int generationId, int state, long stateChangeTimeMs);
void setTransportControlFlags(int generationId, int transportControlFlags); void setTransportControlFlags(int generationId, int transportControlFlags);

View File

@@ -29,6 +29,7 @@ import android.os.Handler;
import android.os.Looper; import android.os.Looper;
import android.os.Message; import android.os.Message;
import android.os.RemoteException; import android.os.RemoteException;
import android.os.SystemClock;
import android.util.Log; import android.util.Log;
import java.lang.IllegalArgumentException; import java.lang.IllegalArgumentException;
@@ -494,11 +495,15 @@ public class RemoteControlClient
*/ */
public void setPlaybackState(int state) { public void setPlaybackState(int state) {
synchronized(mCacheLock) { synchronized(mCacheLock) {
// store locally if (mPlaybackState != state) {
mPlaybackState = state; // store locally
mPlaybackState = state;
// keep track of when the state change occurred
mPlaybackStateChangeTimeMs = SystemClock.elapsedRealtime();
// send to remote control display if conditions are met // send to remote control display if conditions are met
sendPlaybackState_syncCacheLock(); sendPlaybackState_syncCacheLock();
}
} }
} }
@@ -533,6 +538,11 @@ public class RemoteControlClient
* Access synchronized on mCacheLock * Access synchronized on mCacheLock
*/ */
private int mPlaybackState = PLAYSTATE_NONE; private int mPlaybackState = PLAYSTATE_NONE;
/**
* Time of last play state change
* Access synchronized on mCacheLock
*/
private long mPlaybackStateChangeTimeMs = 0;
/** /**
* Cache for the artwork bitmap. * Cache for the artwork bitmap.
* Access synchronized on mCacheLock * Access synchronized on mCacheLock
@@ -716,7 +726,8 @@ public class RemoteControlClient
private void sendPlaybackState_syncCacheLock() { private void sendPlaybackState_syncCacheLock() {
if ((mCurrentClientGenId == mInternalClientGenId) && (mRcDisplay != null)) { if ((mCurrentClientGenId == mInternalClientGenId) && (mRcDisplay != null)) {
try { try {
mRcDisplay.setPlaybackState(mInternalClientGenId, mPlaybackState); mRcDisplay.setPlaybackState(mInternalClientGenId, mPlaybackState,
mPlaybackStateChangeTimeMs);
} catch (RemoteException e) { } catch (RemoteException e) {
Log.e(TAG, "Error in setPlaybackState(), dead display "+e); Log.e(TAG, "Error in setPlaybackState(), dead display "+e);
detachFromDisplay_syncCacheLock(); detachFromDisplay_syncCacheLock();