Override hiding the cast button whenever it's connected

This commit is contained in:
Domingos Lopes 2016-04-22 23:58:18 -04:00
parent e79a077313
commit 9b7edd5bfa
1 changed files with 43 additions and 8 deletions

View File

@ -112,8 +112,10 @@ public abstract class CastEnabledActivity extends AppCompatActivity
private void onCastConnectionChanged(boolean connected) {
if (connected) {
castButtonVisibilityManager.onConnected();
setVolumeControlStream(AudioManager.USE_DEFAULT_STREAM_TYPE);
} else {
castButtonVisibilityManager.onDisconnected();
setVolumeControlStream(AudioManager.STREAM_MUSIC);
}
}
@ -131,10 +133,12 @@ public abstract class CastEnabledActivity extends AppCompatActivity
private volatile boolean prefEnabled = false;
private volatile boolean viewRequested = false;
private volatile boolean resumed = false;
private volatile boolean connected = false;
private volatile int showAsAction = MenuItem.SHOW_AS_ACTION_IF_ROOM;
private Menu menu;
public synchronized void setPrefEnabled(boolean newValue) {
if (prefEnabled != newValue && resumed && viewRequested) {
if (prefEnabled != newValue && resumed && (viewRequested || connected)) {
if (newValue) {
castManager.incrementUiCounter();
} else {
@ -143,7 +147,7 @@ public abstract class CastEnabledActivity extends AppCompatActivity
}
prefEnabled = newValue;
if (mediaRouteActionProvider != null) {
mediaRouteActionProvider.setEnabled(prefEnabled && viewRequested);
mediaRouteActionProvider.setEnabled(prefEnabled && (viewRequested || connected));
}
}
@ -153,7 +157,7 @@ public abstract class CastEnabledActivity extends AppCompatActivity
return;
}
resumed = newValue;
if (prefEnabled && viewRequested) {
if (prefEnabled && (viewRequested || connected)) {
if (resumed) {
castManager.incrementUiCounter();
} else {
@ -163,7 +167,7 @@ public abstract class CastEnabledActivity extends AppCompatActivity
}
public synchronized void setViewRequested(boolean newValue) {
if (viewRequested != newValue && resumed && prefEnabled) {
if (viewRequested != newValue && resumed && prefEnabled && !connected) {
if (newValue) {
castManager.incrementUiCounter();
} else {
@ -172,7 +176,21 @@ public abstract class CastEnabledActivity extends AppCompatActivity
}
viewRequested = newValue;
if (mediaRouteActionProvider != null) {
mediaRouteActionProvider.setEnabled(prefEnabled && viewRequested);
mediaRouteActionProvider.setEnabled(prefEnabled && (viewRequested || connected));
}
}
public synchronized void setConnected(boolean newValue) {
if (connected != newValue && resumed && prefEnabled && !prefEnabled) {
if (newValue) {
castManager.incrementUiCounter();
} else {
castManager.decrementUiCounter();
}
}
connected = newValue;
if (mediaRouteActionProvider != null) {
mediaRouteActionProvider.setEnabled(prefEnabled && (viewRequested || connected));
}
}
@ -182,21 +200,38 @@ public abstract class CastEnabledActivity extends AppCompatActivity
public void setMenu(Menu menu) {
setViewRequested(false);
showAsAction = MenuItem.SHOW_AS_ACTION_IF_ROOM;
this.menu = menu;
setShowAsAction();
}
public void requestCastButton(int showAsAction) {
setViewRequested(true);
this.showAsAction = showAsAction;
setShowAsAction();
}
public void onConnected() {
setConnected(true);
setShowAsAction();
}
public void onDisconnected() {
setConnected(false);
setShowAsAction();
}
private void setShowAsAction() {
if (menu == null) {
Log.e(TAG, "Cast button requested without a menu");
Log.d(TAG, "setShowAsAction() without a menu");
return;
}
MenuItem item = menu.findItem(R.id.media_route_menu_item);
if (item == null) {
Log.e(TAG, "Cast button requested, but not inflated");
Log.e(TAG, "setShowAsAction(), but cast button not inflated");
return;
}
MenuItemCompat.setShowAsAction(menu.findItem(R.id.media_route_menu_item), showAsAction);
MenuItemCompat.setShowAsAction(item, connected? MenuItem.SHOW_AS_ACTION_ALWAYS : showAsAction);
}
}
}