diff options
author | Domingos Lopes <domingos86lopes+github@gmail.com> | 2016-04-21 03:11:13 -0400 |
---|---|---|
committer | Domingos Lopes <domingos86lopes+github@gmail.com> | 2016-04-24 22:06:27 -0400 |
commit | 1a33830991b26fb306ef231e692eebdbd53e6683 (patch) | |
tree | ee12d6f6e73b81c88bee65cc7444a6775e89ea7e /core | |
parent | a7848f0576d63bf0524d033b2e6597fcbf8fd728 (diff) | |
download | AntennaPod-1a33830991b26fb306ef231e692eebdbd53e6683.zip |
allow fine control on where to show the cast button
Diffstat (limited to 'core')
-rw-r--r-- | core/src/main/java/de/danoeh/antennapod/core/cast/SwitchableMediaRouteActionProvider.java | 70 |
1 files changed, 70 insertions, 0 deletions
diff --git a/core/src/main/java/de/danoeh/antennapod/core/cast/SwitchableMediaRouteActionProvider.java b/core/src/main/java/de/danoeh/antennapod/core/cast/SwitchableMediaRouteActionProvider.java index 3d40c90fd..f063cf5e3 100644 --- a/core/src/main/java/de/danoeh/antennapod/core/cast/SwitchableMediaRouteActionProvider.java +++ b/core/src/main/java/de/danoeh/antennapod/core/cast/SwitchableMediaRouteActionProvider.java @@ -1,7 +1,15 @@ package de.danoeh.antennapod.core.cast; +import android.app.Activity; import android.content.Context; +import android.content.ContextWrapper; +import android.support.v4.app.FragmentActivity; +import android.support.v4.app.FragmentManager; import android.support.v7.app.MediaRouteActionProvider; +import android.support.v7.app.MediaRouteChooserDialogFragment; +import android.support.v7.app.MediaRouteControllerDialogFragment; +import android.support.v7.media.MediaRouter; +import android.util.Log; /** * <p>Action Provider that extends {@link MediaRouteActionProvider} and allows the client to @@ -11,7 +19,12 @@ import android.support.v7.app.MediaRouteActionProvider; * <code>setEnabled(true)</code>.</p> */ public class SwitchableMediaRouteActionProvider extends MediaRouteActionProvider { + public static final String TAG = "SwitchblMediaRtActProv"; + private static final String CHOOSER_FRAGMENT_TAG = + "android.support.v7.mediarouter:MediaRouteChooserDialogFragment"; + private static final String CONTROLLER_FRAGMENT_TAG = + "android.support.v7.mediarouter:MediaRouteControllerDialogFragment"; private boolean enabled; public SwitchableMediaRouteActionProvider(Context context) { @@ -33,4 +46,61 @@ public class SwitchableMediaRouteActionProvider extends MediaRouteActionProvider public boolean isVisible() { return enabled && super.isVisible(); } + + @Override + public boolean onPerformDefaultAction() { + if (!super.onPerformDefaultAction()) { + // there is no button, but we should still show the dialog if it's the case. + if (!isVisible()) { + return false; + } + FragmentManager fm = getFragmentManager(); + if (fm == null) { + return false; + } + MediaRouter.RouteInfo route = MediaRouter.getInstance(getContext()).getSelectedRoute(); + if (route.isDefault() || !route.matchesSelector(getRouteSelector())) { + if (fm.findFragmentByTag(CHOOSER_FRAGMENT_TAG) != null) { + Log.w(TAG, "showDialog(): Route chooser dialog already showing!"); + return false; + } + MediaRouteChooserDialogFragment f = + getDialogFactory().onCreateChooserDialogFragment(); + f.setRouteSelector(getRouteSelector()); + f.show(fm, CHOOSER_FRAGMENT_TAG); + } else { + if (fm.findFragmentByTag(CONTROLLER_FRAGMENT_TAG) != null) { + Log.w(TAG, "showDialog(): Route controller dialog already showing!"); + return false; + } + MediaRouteControllerDialogFragment f = + getDialogFactory().onCreateControllerDialogFragment(); + f.show(fm, CONTROLLER_FRAGMENT_TAG); + } + return true; + + } else { + return true; + } + } + + private FragmentManager getFragmentManager() { + Activity activity = getActivity(); + if (activity instanceof FragmentActivity) { + return ((FragmentActivity)activity).getSupportFragmentManager(); + } + return null; + } + + private Activity getActivity() { + // Gross way of unwrapping the Activity so we can get the FragmentManager + Context context = getContext(); + while (context instanceof ContextWrapper) { + if (context instanceof Activity) { + return (Activity)context; + } + context = ((ContextWrapper)context).getBaseContext(); + } + return null; + } } |