make the UI counter, cast preference state, resumed state synchronized
This commit is contained in:
parent
78b2ceab57
commit
3a5b43a67d
|
@ -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,43 +72,44 @@ public abstract class CastEnabledActivity extends AppCompatActivity
|
||||||
@Override
|
@Override
|
||||||
protected void onResume() {
|
protected void onResume() {
|
||||||
super.onResume();
|
super.onResume();
|
||||||
castUICounter++;
|
synchronized (UI_COUNTER_LOCK) {
|
||||||
if (isCastEnabled) {
|
isResumed = true;
|
||||||
mCastManager.incrementUiCounter();
|
if (isCastEnabled) {
|
||||||
castUICounter++;
|
mCastManager.incrementUiCounter();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void onPause() {
|
protected void onPause() {
|
||||||
super.onPause();
|
super.onPause();
|
||||||
castUICounter--;
|
synchronized (UI_COUNTER_LOCK) {
|
||||||
if (isCastEnabled) {
|
isResumed = false;
|
||||||
mCastManager.decrementUiCounter();
|
if (isCastEnabled) {
|
||||||
castUICounter--;
|
mCastManager.decrementUiCounter();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@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);
|
||||||
|
synchronized (UI_COUNTER_LOCK) {
|
||||||
|
if (isCastEnabled != newValue && isResumed) {
|
||||||
|
if (newValue) {
|
||||||
|
mCastManager.incrementUiCounter();
|
||||||
|
} else {
|
||||||
|
mCastManager.decrementUiCounter();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
isCastEnabled = newValue;
|
||||||
|
}
|
||||||
mMediaRouteActionProvider.setEnabled(isCastEnabled);
|
mMediaRouteActionProvider.setEnabled(isCastEnabled);
|
||||||
if (isCastEnabled) {
|
// PlaybackService has its own listener, so if it's active we don't have to take action here.
|
||||||
//Test if activity is resumed but without UI counter incremented
|
if (!isCastEnabled && !PlaybackService.isRunning) {
|
||||||
if (castUICounter==1) {
|
CastManager.getInstance().disconnect();
|
||||||
mCastManager.incrementUiCounter();
|
|
||||||
castUICounter++;
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
if (castUICounter > 1) {
|
|
||||||
mCastManager.decrementUiCounter();
|
|
||||||
castUICounter--;
|
|
||||||
}
|
|
||||||
if (!PlaybackService.isRunning) {
|
|
||||||
CastManager.getInstance().disconnect();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue