make the UI counter, cast preference state, resumed state synchronized

This commit is contained in:
Domingos Lopes 2016-04-19 00:05:55 -04:00
parent 78b2ceab57
commit 3a5b43a67d
1 changed files with 28 additions and 27 deletions

View File

@ -27,9 +27,10 @@ public abstract class CastEnabledActivity extends AppCompatActivity
public static final String TAG = "CastEnabledActivity"; public static final String TAG = "CastEnabledActivity";
protected CastManager mCastManager; protected CastManager mCastManager;
private volatile int castUICounter; private final Object UI_COUNTER_LOCK = new Object();
private volatile boolean isResumed = false;
protected SwitchableMediaRouteActionProvider mMediaRouteActionProvider; protected SwitchableMediaRouteActionProvider mMediaRouteActionProvider;
protected volatile boolean isCastEnabled; protected volatile boolean isCastEnabled = false;
@Override @Override
protected void onCreate(Bundle savedInstanceState) { protected void onCreate(Bundle savedInstanceState) {
@ -38,7 +39,6 @@ public abstract class CastEnabledActivity extends AppCompatActivity
PreferenceManager.getDefaultSharedPreferences(getApplicationContext()). PreferenceManager.getDefaultSharedPreferences(getApplicationContext()).
registerOnSharedPreferenceChangeListener(this); registerOnSharedPreferenceChangeListener(this);
castUICounter = 0;
mCastManager = CastManager.getInstance(); mCastManager = CastManager.getInstance();
mCastManager.addCastConsumer(castConsumer); mCastManager.addCastConsumer(castConsumer);
isCastEnabled = UserPreferences.isCastEnabled(); isCastEnabled = UserPreferences.isCastEnabled();
@ -72,46 +72,47 @@ public abstract class CastEnabledActivity extends AppCompatActivity
@Override @Override
protected void onResume() { protected void onResume() {
super.onResume(); super.onResume();
castUICounter++; synchronized (UI_COUNTER_LOCK) {
isResumed = true;
if (isCastEnabled) { if (isCastEnabled) {
mCastManager.incrementUiCounter(); mCastManager.incrementUiCounter();
castUICounter++; }
} }
} }
@Override @Override
protected void onPause() { protected void onPause() {
super.onPause(); super.onPause();
castUICounter--; synchronized (UI_COUNTER_LOCK) {
isResumed = false;
if (isCastEnabled) { if (isCastEnabled) {
mCastManager.decrementUiCounter(); mCastManager.decrementUiCounter();
castUICounter--; }
} }
} }
@Override @Override
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) { public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
if (UserPreferences.PREF_CAST_ENABLED.equals(key)) { if (UserPreferences.PREF_CAST_ENABLED.equals(key)) {
isCastEnabled = UserPreferences.isCastEnabled(); boolean newValue = UserPreferences.isCastEnabled();
Log.d(TAG, "onSharedPreferenceChanged(), isCastEnabled set to " + isCastEnabled); Log.d(TAG, "onSharedPreferenceChanged(), isCastEnabled set to " + newValue);
mMediaRouteActionProvider.setEnabled(isCastEnabled); synchronized (UI_COUNTER_LOCK) {
if (isCastEnabled) { if (isCastEnabled != newValue && isResumed) {
//Test if activity is resumed but without UI counter incremented if (newValue) {
if (castUICounter==1) {
mCastManager.incrementUiCounter(); mCastManager.incrementUiCounter();
castUICounter++;
}
} else { } else {
if (castUICounter > 1) {
mCastManager.decrementUiCounter(); mCastManager.decrementUiCounter();
castUICounter--;
} }
if (!PlaybackService.isRunning) { }
isCastEnabled = newValue;
}
mMediaRouteActionProvider.setEnabled(isCastEnabled);
// PlaybackService has its own listener, so if it's active we don't have to take action here.
if (!isCastEnabled && !PlaybackService.isRunning) {
CastManager.getInstance().disconnect(); CastManager.getInstance().disconnect();
} }
} }
} }
}
CastConsumer castConsumer = new CastConsumerImpl() { CastConsumer castConsumer = new CastConsumerImpl() {
@Override @Override