Filtering Or Hiding Available ChromeCast Devices
I'm working on an app using Chromecast and I want to be able to filter the available devices or routes based on device name or description. So when a user clicks the MediaRouteButton only some of the available devices would show. The use case for my app happens in a place where many cast devices are available and I want to make sure the user doesn't accidentally select a device in another room. The user information in the app stores the room information that the user is based in and the chromecasts are being named in an intelligent way so that, ideally, only the chromecast device for a specific user's room would show up as available to them.
I have tried grabbing the MediaRouteDialogFactory
and filtering devices at that level but have had no luck. There doesn't seem to be any mechanism that I can find to hide or remove routes.
To filter Chromecast devices from chooser dialog you can use onFilterRoute:
public boolean onFilterRoute (MediaRouter.RouteInfo route)
Returns true if the route should be included in the list.
The default implementation returns true for enabled non-default routes that match the selector. Subclasses can override this method to filter routes differently.
You need to create a CustomMediaRouteChooserDialog
:
public class CustomMediaRouteChooserDialog extends MediaRouteChooserDialog {
public CustomMediaRouteChooserDialog(Context context) {
super(context);
}
public CustomMediaRouteChooserDialog(Context context, int theme) {
super(context, theme);
}
@Override
public boolean onFilterRoute(MediaRouter.RouteInfo route) {
// Apply your logic here.
// Return false to hide the device, true otherwise
if (TextUtils.equals(route.getName(), "Chromecast-hidden"))
return false;
else
return true;
}
}
Then create a CustomMediaRouteChooserDialogFragment
:
public class CustomMediaRouteChooserDialogFragment extends MediaRouteChooserDialogFragment {
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
CustomMediaRouteChooserDialog dialog = new CustomMediaRouteChooserDialog(getActivity());
dialog.setRouteSelector(getRouteSelector());
return dialog;
}
}
Then create a CustomMediaRouteDialogFactory
:
public class CustomMediaRouteDialogFactory extends MediaRouteDialogFactory {
@Override
public MediaRouteChooserDialogFragment onCreateChooserDialogFragment() {
return new CustomMediaRouteChooserDialogFragment();
}
}
Then after create your MediaRouteActionProvider
call setDialogFactory
:
mediaRouteActionProvider.setDialogFactory(new CustomMediaRouteDialogFactory());
One approach would be the following:
MediaRouteDialogFactory
and override onCreateChooserDialogFragment()
to return your own chooser dialog fragment, say 'MyChooserDialogFragment'; this should extend MediaRouteChooserDialogFactory. MyChooserDialogFragment
, override onCreateChooserDialog()
to return, say, MyChooserDialog
which extends MediaRouteChooserDialog
In MyChooserDialog
, override onFilterRoute(MediaRouter.RouteInfo route))
. In this override, you are passed in a route and you can return true
to accept or false
to reject that route; so a naive implementation would be return route.getName().startsWith('room10')
(in reality, it should be a bit smarter like:
String validPrefix = 'room10'; boolean validRoute = route.getName().startsWith(validPrefix); return !route.isDefault() && route.matchesSelector(getRouteSelector()) && validRoute;
上一篇: 检查设备上执行的设备管理员策略
下一篇: 过滤或隐藏可用的ChromeCast设备