Merge pull request #1981 from domingos86/null-action-provider-workaround

fixes CastManager.addMediaRouterButton NPE
This commit is contained in:
Martin Fietz 2016-06-10 19:34:43 +02:00 committed by GitHub
commit a7a2043682
2 changed files with 23 additions and 6 deletions

View File

@ -67,9 +67,16 @@ public abstract class CastEnabledActivity extends AppCompatActivity
@CallSuper @CallSuper
public boolean onPrepareOptionsMenu(Menu menu) { public boolean onPrepareOptionsMenu(Menu menu) {
super.onPrepareOptionsMenu(menu); super.onPrepareOptionsMenu(menu);
mediaRouteActionProvider = castManager MenuItem mediaRouteButton = menu.findItem(R.id.media_route_menu_item);
.addMediaRouterButton(menu.findItem(R.id.media_route_menu_item)); if (mediaRouteButton == null) {
Log.wtf(TAG, "MediaRoute item could not be found on the menu!", new Exception());
mediaRouteActionProvider = null;
return true;
}
mediaRouteActionProvider = castManager.addMediaRouterButton(mediaRouteButton);
if (mediaRouteActionProvider != null) {
mediaRouteActionProvider.setEnabled(castButtonVisibilityManager.shouldEnable()); mediaRouteActionProvider.setEnabled(castButtonVisibilityManager.shouldEnable());
}
return true; return true;
} }

View File

@ -24,6 +24,8 @@ package de.danoeh.antennapod.core.cast;
import android.content.Context; import android.content.Context;
import android.os.Build; import android.os.Build;
import android.support.annotation.NonNull;
import android.support.v4.view.ActionProvider;
import android.support.v4.view.MenuItemCompat; import android.support.v4.view.MenuItemCompat;
import android.support.v7.media.MediaRouter; import android.support.v7.media.MediaRouter;
import android.util.Log; import android.util.Log;
@ -1739,9 +1741,17 @@ public class CastManager extends BaseCastManager implements OnFailedListener {
* *
* @param menuItem MenuItem of the Media Router cast button. * @param menuItem MenuItem of the Media Router cast button.
*/ */
public final SwitchableMediaRouteActionProvider addMediaRouterButton(MenuItem menuItem) { public final SwitchableMediaRouteActionProvider addMediaRouterButton(@NonNull MenuItem menuItem) {
SwitchableMediaRouteActionProvider mediaRouteActionProvider = (SwitchableMediaRouteActionProvider) ActionProvider actionProvider = MenuItemCompat.getActionProvider(menuItem);
MenuItemCompat.getActionProvider(menuItem); if (!(actionProvider instanceof SwitchableMediaRouteActionProvider)) {
Log.wtf(TAG, "MenuItem provided to addMediaRouterButton() is not compatible with " +
"SwitchableMediaRouteActionProvider." +
((actionProvider == null) ? " Its action provider is null!" : ""),
new ClassCastException());
return null;
}
SwitchableMediaRouteActionProvider mediaRouteActionProvider =
(SwitchableMediaRouteActionProvider) actionProvider;
mediaRouteActionProvider.setRouteSelector(mMediaRouteSelector); mediaRouteActionProvider.setRouteSelector(mMediaRouteSelector);
if (mCastConfiguration.getMediaRouteDialogFactory() != null) { if (mCastConfiguration.getMediaRouteDialogFactory() != null) {
mediaRouteActionProvider.setDialogFactory(mCastConfiguration.getMediaRouteDialogFactory()); mediaRouteActionProvider.setDialogFactory(mCastConfiguration.getMediaRouteDialogFactory());