fix media router button still showing when disabled
This commit is contained in:
parent
c915366c7b
commit
e70f4d5389
|
@ -4,11 +4,12 @@ import android.content.SharedPreferences;
|
|||
import android.os.Bundle;
|
||||
import android.preference.PreferenceManager;
|
||||
import android.support.v7.app.AppCompatActivity;
|
||||
import android.util.Log;
|
||||
import android.view.Menu;
|
||||
import android.view.MenuItem;
|
||||
|
||||
import de.danoeh.antennapod.R;
|
||||
import de.danoeh.antennapod.core.cast.CastManager;
|
||||
import de.danoeh.antennapod.core.cast.SwitchableMediaRouteActionProvider;
|
||||
import de.danoeh.antennapod.core.preferences.UserPreferences;
|
||||
|
||||
/**
|
||||
|
@ -17,17 +18,18 @@ import de.danoeh.antennapod.core.preferences.UserPreferences;
|
|||
*/
|
||||
public abstract class CastEnabledActivity extends AppCompatActivity
|
||||
implements SharedPreferences.OnSharedPreferenceChangeListener {
|
||||
public static final String TAG = "CastEnabledActivity";
|
||||
|
||||
protected CastManager mCastManager;
|
||||
private int castUICounter;
|
||||
protected MenuItem mMediaRouteMenuItem;
|
||||
protected boolean isCastEnabled;
|
||||
private volatile int castUICounter;
|
||||
protected SwitchableMediaRouteActionProvider mMediaRouteActionProvider;
|
||||
protected volatile boolean isCastEnabled;
|
||||
|
||||
@Override
|
||||
protected void onCreate(Bundle savedInstanceState) {
|
||||
super.onCreate(savedInstanceState);
|
||||
|
||||
PreferenceManager.getDefaultSharedPreferences(this).
|
||||
PreferenceManager.getDefaultSharedPreferences(getApplicationContext()).
|
||||
registerOnSharedPreferenceChangeListener(this);
|
||||
|
||||
castUICounter = 0;
|
||||
|
@ -35,6 +37,13 @@ public abstract class CastEnabledActivity extends AppCompatActivity
|
|||
isCastEnabled = UserPreferences.isCastEnabled();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onDestroy() {
|
||||
PreferenceManager.getDefaultSharedPreferences(getApplicationContext())
|
||||
.unregisterOnSharedPreferenceChangeListener(this);
|
||||
super.onDestroy();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean onCreateOptionsMenu(Menu menu) {
|
||||
super.onCreateOptionsMenu(menu);
|
||||
|
@ -45,9 +54,9 @@ public abstract class CastEnabledActivity extends AppCompatActivity
|
|||
@Override
|
||||
public boolean onPrepareOptionsMenu(Menu menu) {
|
||||
super.onPrepareOptionsMenu(menu);
|
||||
mMediaRouteMenuItem = menu.findItem(R.id.media_route_menu_item);
|
||||
mMediaRouteMenuItem.setEnabled(isCastEnabled);
|
||||
mMediaRouteMenuItem = mCastManager.addMediaRouterButton(menu, R.id.media_route_menu_item);
|
||||
mMediaRouteActionProvider = mCastManager
|
||||
.addMediaRouterButton(menu.findItem(R.id.media_route_menu_item));
|
||||
mMediaRouteActionProvider.setEnabled(isCastEnabled);
|
||||
return true;
|
||||
}
|
||||
|
||||
|
@ -71,13 +80,12 @@ public abstract class CastEnabledActivity extends AppCompatActivity
|
|||
}
|
||||
}
|
||||
|
||||
//This whole method might just be useless because it's assumed that the cast button
|
||||
//won't show where the user actually has the power to change the preference.
|
||||
@Override
|
||||
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
|
||||
if (key.equals(UserPreferences.PREF_CAST_ENABLED)) {
|
||||
isCastEnabled = UserPreferences.isCastEnabled();
|
||||
mMediaRouteMenuItem.setEnabled(isCastEnabled);
|
||||
Log.d(TAG, "onSharedPreferenceChanged(), isCastEnabled set to " + isCastEnabled);
|
||||
mMediaRouteActionProvider.setEnabled(isCastEnabled);
|
||||
if (isCastEnabled) {
|
||||
//Test if activity is resumed but without UI counter incremented
|
||||
if (castUICounter==1) {
|
||||
|
|
|
@ -6,6 +6,6 @@
|
|||
android:id="@+id/media_route_menu_item"
|
||||
android:title="@string/cast_media_route_menu_title"
|
||||
android:enabled="false"
|
||||
custom:actionProviderClass="android.support.v7.app.MediaRouteActionProvider"
|
||||
custom:actionProviderClass="de.danoeh.antennapod.core.cast.SwitchableMediaRouteActionProvider"
|
||||
custom:showAsAction="always"/>
|
||||
</menu>
|
|
@ -21,9 +21,11 @@ package de.danoeh.antennapod.core.cast;
|
|||
|
||||
import android.content.Context;
|
||||
import android.os.Build;
|
||||
import android.support.v4.view.MenuItemCompat;
|
||||
import android.support.v7.media.MediaRouter;
|
||||
import android.util.Log;
|
||||
import android.view.KeyEvent;
|
||||
import android.view.MenuItem;
|
||||
|
||||
import com.google.android.gms.cast.ApplicationMetadata;
|
||||
import com.google.android.gms.cast.Cast;
|
||||
|
@ -1713,4 +1715,23 @@ public class CastManager extends BaseCastManager implements OnFailedListener {
|
|||
return defaultVal;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds and wires up the Switchable Media Router cast button. It returns a reference to the
|
||||
* {@link SwitchableMediaRouteActionProvider} associated with the button if the caller needs
|
||||
* such reference. It is assumed that the enclosing
|
||||
* {@link android.app.Activity} inherits (directly or indirectly) from
|
||||
* {@link android.support.v7.app.AppCompatActivity}.
|
||||
*
|
||||
* @param menuItem MenuItem of the Media Router cast button.
|
||||
*/
|
||||
public final SwitchableMediaRouteActionProvider addMediaRouterButton(MenuItem menuItem) {
|
||||
SwitchableMediaRouteActionProvider mediaRouteActionProvider = (SwitchableMediaRouteActionProvider)
|
||||
MenuItemCompat.getActionProvider(menuItem);
|
||||
mediaRouteActionProvider.setRouteSelector(mMediaRouteSelector);
|
||||
if (mCastConfiguration.getMediaRouteDialogFactory() != null) {
|
||||
mediaRouteActionProvider.setDialogFactory(mCastConfiguration.getMediaRouteDialogFactory());
|
||||
}
|
||||
return mediaRouteActionProvider;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -0,0 +1,36 @@
|
|||
package de.danoeh.antennapod.core.cast;
|
||||
|
||||
import android.content.Context;
|
||||
import android.support.v7.app.MediaRouteActionProvider;
|
||||
|
||||
/**
|
||||
* <p>Action Provider that extends {@link MediaRouteActionProvider} and allows the client to
|
||||
* disable completely the button by calling {@link #setEnabled(boolean)}.</p>
|
||||
*
|
||||
* <p>It is disabled by default, so if a client wants to initially have it enabled it must call
|
||||
* <code>setEnabled(true)</code>.</p>
|
||||
*/
|
||||
public class SwitchableMediaRouteActionProvider extends MediaRouteActionProvider {
|
||||
|
||||
private boolean enabled;
|
||||
|
||||
public SwitchableMediaRouteActionProvider(Context context) {
|
||||
super(context);
|
||||
enabled = false;
|
||||
}
|
||||
|
||||
/**
|
||||
* <p>Sets whether the Media Router button should be allowed to become visible or not.</p>
|
||||
*
|
||||
* <p>It's invisible by default.</p>
|
||||
*/
|
||||
public void setEnabled(boolean newVal) {
|
||||
enabled = newVal;
|
||||
refreshVisibility();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isVisible() {
|
||||
return enabled && super.isVisible();
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue