com.google.android.gms.cast.CastDevice Java Examples

The following examples show how to use com.google.android.gms.cast.CastDevice. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: CastMediaRouterCallback.java    From android with Apache License 2.0 6 votes vote down vote up
@Override
public void onRouteAdded(MediaRouter router, RouteInfo route) {
    super.onRouteAdded(router, route);
    if (!router.getDefaultRoute().equals(route)) {
        if (++mRouteCount == 1) {
            BaseCastManager.getCastManager().onCastAvailabilityChanged(true);
        }
        selectDeviceInterface.onCastDeviceDetected(route);
    }
    if (BaseCastManager.getCastManager().getReconnectionStatus() == ReconnectionStatus.STARTED) {
        String routeId = Utils.getStringFromPreference(mContext,
                BaseCastManager.PREFS_KEY_ROUTE_ID);
        if (route.getId().equals(routeId)) {
            // we found the route, so lets go with that
            LOGD(TAG, "onRouteAdded: Attempting to recover a session with info=" + route);
            BaseCastManager.getCastManager().setReconnectionStatus(
                    ReconnectionStatus.IN_PROGRESS);

            CastDevice device = CastDevice.getFromBundle(route.getExtras());
            LOGD(TAG, "onRouteAdded: Attempting to recover a session with device: "
                    + device.getFriendlyName());
            selectDeviceInterface.onDeviceSelected(device);
        }
    }
}
 
Example #2
Source File: CastDeviceControllerImpl.java    From android_packages_apps_GmsCore with Apache License 2.0 6 votes vote down vote up
public CastDeviceControllerImpl(Context context, String packageName, Bundle extras) {
    this.context = context;
    this.packageName = packageName;

    extras.setClassLoader(BinderWrapper.class.getClassLoader());
    this.castDevice = CastDevice.getFromBundle(extras);
    this.notificationEnabled = extras.getBoolean("com.google.android.gms.cast.EXTRA_CAST_FRAMEWORK_NOTIFICATION_ENABLED");
    this.castFlags = extras.getLong("com.google.android.gms.cast.EXTRA_CAST_FLAGS");
    BinderWrapper listenerWrapper = (BinderWrapper)extras.get("listener");
    if (listenerWrapper != null) {
        this.listener = ICastDeviceControllerListener.Stub.asInterface(listenerWrapper.binder);
    }

    this.chromecast = new ChromeCast(this.castDevice.getAddress());
    this.chromecast.registerListener(this);
    this.chromecast.registerRawMessageListener(this);
    this.chromecast.registerConnectionListener(this);
}
 
Example #3
Source File: BaseCastManager.java    From android with Apache License 2.0 6 votes vote down vote up
private void reconnectSessionIfPossibleInternal(RouteInfo theRoute) {
    if (isConnected()) {
        return;
    }
    String sessionId = Utils.getStringFromPreference(mContext, PREFS_KEY_SESSION_ID);
    String routeId = Utils.getStringFromPreference(mContext, PREFS_KEY_ROUTE_ID);
    LOGD(TAG, "reconnectSessionIfPossible() Retrieved from preferences: " + "sessionId="
            + sessionId + ", routeId=" + routeId);
    if (null == sessionId || null == routeId) {
        return;
    }
    mReconnectionStatus = ReconnectionStatus.IN_PROGRESS;
    CastDevice device = CastDevice.getFromBundle(theRoute.getExtras());

    if (null != device) {
        LOGD(TAG, "trying to acquire Cast Client for " + device);
        onDeviceSelected(device);
    }
}
 
Example #4
Source File: CastSessionImpl.java    From 365browser with Apache License 2.0 6 votes vote down vote up
/**
 * @param device The {@link CastDevice} queried for it's capabilities.
 * @return The capabilities of the Cast device.
 * TODO(zqzhang): move to a CastUtils class?
 */
protected static List<String> getCapabilities(CastDevice device) {
    List<String> capabilities = new ArrayList<String>();
    if (device.hasCapability(CastDevice.CAPABILITY_AUDIO_IN)) {
        capabilities.add("audio_in");
    }
    if (device.hasCapability(CastDevice.CAPABILITY_AUDIO_OUT)) {
        capabilities.add("audio_out");
    }
    if (device.hasCapability(CastDevice.CAPABILITY_VIDEO_IN)) {
        capabilities.add("video_in");
    }
    if (device.hasCapability(CastDevice.CAPABILITY_VIDEO_OUT)) {
        capabilities.add("video_out");
    }
    return capabilities;
}
 
Example #5
Source File: CastSessionImpl.java    From AndroidChromium with Apache License 2.0 6 votes vote down vote up
/**
 * @param device The {@link CastDevice} queried for it's capabilities.
 * @return The capabilities of the Cast device.
 * TODO(zqzhang): move to a CastUtils class?
 */
protected static List<String> getCapabilities(CastDevice device) {
    List<String> capabilities = new ArrayList<String>();
    if (device.hasCapability(CastDevice.CAPABILITY_AUDIO_IN)) {
        capabilities.add("audio_in");
    }
    if (device.hasCapability(CastDevice.CAPABILITY_AUDIO_OUT)) {
        capabilities.add("audio_out");
    }
    if (device.hasCapability(CastDevice.CAPABILITY_VIDEO_IN)) {
        capabilities.add("video_in");
    }
    if (device.hasCapability(CastDevice.CAPABILITY_VIDEO_OUT)) {
        capabilities.add("video_out");
    }
    return capabilities;
}
 
Example #6
Source File: ChromeCastService.java    From DeviceConnect-Android with MIT License 6 votes vote down vote up
@Override
public void onCastDeviceUpdate(final ArrayList<CastDevice> devices) {
    if (BuildConfig.DEBUG) {
        Log.d("TEST", "onCastDeviceUpdate#");
    }
    if (devices.size() == 0) {
        if (BuildConfig.DEBUG) {
            Log.d("TEST", "size:0");
        }
        ChromeCastApplication app = (ChromeCastApplication) getApplication();

        if (app.getController() != null) {
            app.getController().teardown();
        }
    }
}
 
Example #7
Source File: GoogleCastModule.java    From react-native-google-cast with MIT License 6 votes vote down vote up
@ReactMethod
public void initChannel(final String namespace, final Promise promise) {
    if (mCastSession != null) {
        getReactApplicationContext().runOnUiQueueThread(new Runnable() {
            @Override
            public void run() {
                try {
                    mCastSession.setMessageReceivedCallbacks(namespace, new Cast.MessageReceivedCallback() {
                        @Override
                        public void onMessageReceived(CastDevice castDevice, String channelNameSpace, String message) {
                            WritableMap map = Arguments.createMap();
                            map.putString("channel", channelNameSpace);
                            map.putString("message", message);
                            emitMessageToRN(CHANNEL_MESSAGE_RECEIVED, map);
                        }
                    });
                    promise.resolve(true);
                } catch (IOException e) {
                    e.printStackTrace();
                    promise.reject(e);
                }
            }
        });
    }
}
 
Example #8
Source File: CastSessionImpl.java    From delion with Apache License 2.0 6 votes vote down vote up
/**
 * @param device The {@link CastDevice} queried for it's capabilities.
 * @return The capabilities of the Cast device.
 * TODO(zqzhang): move to a CastUtils class?
 */
protected static List<String> getCapabilities(CastDevice device) {
    List<String> capabilities = new ArrayList<String>();
    if (device.hasCapability(CastDevice.CAPABILITY_AUDIO_IN)) {
        capabilities.add("audio_in");
    }
    if (device.hasCapability(CastDevice.CAPABILITY_AUDIO_OUT)) {
        capabilities.add("audio_out");
    }
    if (device.hasCapability(CastDevice.CAPABILITY_VIDEO_IN)) {
        capabilities.add("video_in");
    }
    if (device.hasCapability(CastDevice.CAPABILITY_VIDEO_OUT)) {
        capabilities.add("video_out");
    }
    return capabilities;
}
 
Example #9
Source File: BaseCastManager.java    From UTubeTV with The Unlicense 6 votes vote down vote up
private void reconnectSessionIfPossibleInternal(RouteInfo theRoute) {
  if (isConnected()) {
    return;
  }
  String sessionId = CastUtils.getStringFromPreference(mContext, PREFS_KEY_SESSION_ID);
  String routeId = CastUtils.getStringFromPreference(mContext, PREFS_KEY_ROUTE_ID);
  CastUtils.LOGD(TAG, "reconnectSessionIfPossible() Retrieved from preferences: " + "sessionId=" + sessionId + ", routeId=" + routeId);
  if (null == sessionId || null == routeId) {
    return;
  }
  mReconnectionStatus = ReconnectionStatus.IN_PROGRESS;
  CastDevice device = CastDevice.getFromBundle(theRoute.getExtras());

  if (null != device) {
    CastUtils.LOGD(TAG, "trying to acquire Cast Client for " + device);
    onDeviceSelected(device);
  }
}
 
Example #10
Source File: CastMediaRouterCallback.java    From UTubeTV with The Unlicense 6 votes vote down vote up
@Override
public void onRouteAdded(MediaRouter router, RouteInfo route) {
  super.onRouteAdded(router, route);
  if (!router.getDefaultRoute().equals(route)) {
    selectDeviceInterface.onCastDeviceDetected(route);
  }
  if (BaseCastManager.getCastManager().getReconnectionStatus() == ReconnectionStatus.STARTED) {
    String routeId = CastUtils.getStringFromPreference(mContext, BaseCastManager.PREFS_KEY_ROUTE_ID);
    if (route.getId().equals(routeId)) {
      // we found the route, so lets go with that
      CastUtils.LOGD(TAG, "onRouteAdded: Attempting to recover a session with info=" + route);
      BaseCastManager.getCastManager().setReconnectionStatus(ReconnectionStatus.IN_PROGRESS);

      CastDevice device = CastDevice.getFromBundle(route.getExtras());
      CastUtils.LOGD(TAG, "onRouteAdded: Attempting to recover a session with device: " + device.getFriendlyName());
      selectDeviceInterface.onDeviceSelected(device);
    }
  }
}
 
Example #11
Source File: DataCastManager.java    From android with Apache License 2.0 5 votes vote down vote up
@Override
protected Builder getCastOptionBuilder(CastDevice device) {

    Builder builder = Cast.CastOptions.builder(
            mSelectedCastDevice, new CastListener());
    if (isFeatureEnabled(FEATURE_DEBUGGING)) {
        builder.setVerboseLoggingEnabled(true);
    }
    return builder;
}
 
Example #12
Source File: DataCastManager.java    From android with Apache License 2.0 5 votes vote down vote up
/*************************************************************************/

    @Override
    public void onMessageReceived(CastDevice castDevice, String namespace, String message) {
        synchronized (mDataConsumers) {
            for (IDataCastConsumer consumer : mDataConsumers) {
                try {
                    consumer.onMessageReceived(castDevice, namespace, message);
                } catch (Exception e) {
                    LOGE(TAG, "onMessageReceived(): Failed to inform " + consumer, e);
                }
            }
        }
    }
 
Example #13
Source File: ChromeCastService.java    From DeviceConnect-Android with MIT License 5 votes vote down vote up
@Override
public void onCastDeviceSelected(final CastDevice selectedDevice) {
    if (BuildConfig.DEBUG) {
        Log.d("TEST", "onCastDeviceSelected#" + selectedDevice.getDeviceId());
    }
    ChromeCastApplication app = (ChromeCastApplication) getApplication();
    if (app == null) {
        return;
    }

    CastDevice currentDevice = app.getController().getSelectedDevice();
    if (currentDevice != null) {
        DConnectService castService = getServiceProvider().getService(currentDevice.getDeviceId());
        if (castService == null) {
            castService = new ChromeCastDeviceService(currentDevice);
            getServiceProvider().addService(castService);
        }
        castService.setOnline(true);
        if (!currentDevice.getDeviceId().equals(selectedDevice.getDeviceId())) {
            app.getController().setSelectedDevice(selectedDevice);
            app.getController().reconnect();
        } else {
            app.getController().connect();
        }
    } else {
        app.getController().setSelectedDevice(selectedDevice);
        app.getController().connect();
    }
}
 
Example #14
Source File: ChromeCastService.java    From DeviceConnect-Android with MIT License 5 votes vote down vote up
@Override
public void onCastDeviceUnselected(final CastDevice unselectedDevice) {
    if (BuildConfig.DEBUG) {
        Log.d("TEST", "onCastDeviceUnselected#start");
    }
    ChromeCastApplication app = (ChromeCastApplication) getApplication();

    if (app == null) {
        return;
    }
    CastDevice currentDevice = unselectedDevice;
    if (currentDevice == null) {
        if (app.getController().getSelectedDevice() == null) {
            return;
        }
        currentDevice = app.getController().getSelectedDevice();
    }
    if (BuildConfig.DEBUG) {
        Log.d("TEST", "onCastDeviceUnselected#start+ " + currentDevice.getDeviceId());
    }

    DConnectService castService = getServiceProvider().getService(currentDevice.getDeviceId());
    if (castService != null) {
        castService.setOnline(false);
    }
    app.getController().teardown();
}
 
Example #15
Source File: VideoCastManager.java    From android with Apache License 2.0 5 votes vote down vote up
@Override
Builder getCastOptionBuilder(CastDevice device) {
    Builder builder = Cast.CastOptions.builder(mSelectedCastDevice, new CastListener());
    if (isFeatureEnabled(FEATURE_DEBUGGING)) {
        builder.setVerboseLoggingEnabled(true);
    }
    return builder;
}
 
Example #16
Source File: ChromeCastService.java    From DeviceConnect-Android with MIT License 5 votes vote down vote up
@Override
public synchronized void onChromeCastConnected() {
    ChromeCastApplication app = (ChromeCastApplication) getApplication();
    if (app != null) {
        CastDevice currentDevice = app.getController().getSelectedDevice();
        DConnectService castService = getServiceProvider().getService(currentDevice.getDeviceId());
        if (castService == null) {
            castService = new ChromeCastDeviceService(currentDevice);
            getServiceProvider().addService(castService);
        }
        castService.setOnline(true);
    }
}
 
Example #17
Source File: MediaSink.java    From 365browser with Apache License 2.0 5 votes vote down vote up
/**
 * @param route The route information provided by Android.
 * @return A new MediaSink instance corresponding to the specified {@link RouteInfo}.
 */
public static MediaSink fromRoute(MediaRouter.RouteInfo route) {
    return new MediaSink(
        route.getId(),
        route.getName(),
        CastDevice.getFromBundle(route.getExtras()));
}
 
Example #18
Source File: ChromeCastDiscovery.java    From DeviceConnect-Android with MIT License 5 votes vote down vote up
@Override
public void onSessionStarting(CastSession castSession) {
    if (BuildConfig.DEBUG) {
        Log.d(TAG, "================================>");
        Log.d(TAG, "SessionManagerListener.onSessionStarting");
        Log.d(TAG, "<================================");
    }
    synchronized (this) {
        CastDevice device = castSession.getCastDevice();
        if (device != null) {
            mRouteNames.add(device);
        }
    }
    mCallbacks.onCastDeviceUpdate(mRouteNames);
}
 
Example #19
Source File: GoogleCompat.java    From Popeens-DSub with GNU General Public License v3.0 5 votes vote down vote up
public static RemoteController getController(DownloadService downloadService, MediaRouter.RouteInfo info) {
    CastDevice device = CastDevice.getFromBundle(info.getExtras());
    if(device != null) {
        return new ChromeCastController(downloadService, device);
    } else {
        return null;
    }
}
 
Example #20
Source File: CastMediaRouteProvider.java    From android_packages_apps_GmsCore with Apache License 2.0 5 votes vote down vote up
private void onChromeCastDiscovered(
        String id, String name, InetAddress host, int port, String
        deviceVersion, String friendlyName, String modelName, String
        iconPath, int status) {
    if (!this.castDevices.containsKey(id)) {
        // TODO: Capabilities
        int capabilities = CastDevice.CAPABILITY_VIDEO_OUT | CastDevice.CAPABILITY_AUDIO_OUT;

        CastDevice castDevice = new CastDevice(id, name, host, port, deviceVersion, friendlyName, modelName, iconPath, status, capabilities);
        this.castDevices.put(id, castDevice);
        this.serviceCastIds.put(name, id);
    }

    publishRoutesInMainThread();
}
 
Example #21
Source File: CastMediaRouteProvider.java    From android_packages_apps_GmsCore with Apache License 2.0 5 votes vote down vote up
@Override
public RouteController onCreateRouteController(String routeId) {
    CastDevice castDevice = this.castDevices.get(routeId);
    if (castDevice == null) {
        return null;
    }
    return new CastMediaRouteController(this, routeId, castDevice.getAddress());
}
 
Example #22
Source File: CastMediaRouteProvider.java    From android_packages_apps_GmsCore with Apache License 2.0 5 votes vote down vote up
private void publishRoutes() {
    MediaRouteProviderDescriptor.Builder builder = new MediaRouteProviderDescriptor.Builder();
    for (CastDevice castDevice : this.castDevices.values()) {
        ArrayList<IntentFilter> controlFilters = new ArrayList<IntentFilter>(BASE_CONTROL_FILTERS);
        // Include any app-specific control filters that have been requested.
        // TODO: Do we need to check with the device?
        for (String category : this.customCategories) {
            IntentFilter filter = new IntentFilter();
            filter.addCategory(category);
            controlFilters.add(filter);
        }

        Bundle extras = new Bundle();
        castDevice.putInBundle(extras);
        MediaRouteDescriptor route = new MediaRouteDescriptor.Builder(
            castDevice.getDeviceId(),
            castDevice.getFriendlyName())
            .setDescription(castDevice.getModelName())
            .addControlFilters(controlFilters)
            .setDeviceType(MediaRouter.RouteInfo.DEVICE_TYPE_TV)
            .setPlaybackType(MediaRouter.RouteInfo.PLAYBACK_TYPE_REMOTE)
            .setVolumeHandling(MediaRouter.RouteInfo.PLAYBACK_VOLUME_FIXED)
            .setVolumeMax(20)
            .setVolume(0)
            .setEnabled(true)
            .setExtras(extras)
            .setConnectionState(MediaRouter.RouteInfo.CONNECTION_STATE_DISCONNECTED)
            .build();
        builder.addRoute(route);
    }
    this.setDescriptor(builder.build());
}
 
Example #23
Source File: SessionImpl.java    From android_packages_apps_GmsCore with Apache License 2.0 5 votes vote down vote up
public void start(CastContextImpl castContext, CastDevice castDevice, String routeId, Bundle routeInfoExtra) throws RemoteException {
    this.castContext = castContext;
    this.castDevice = castDevice;
    this.routeInfoExtra = routeInfoExtra;
    this.routeId = routeId;

    this.mIsConnecting = true;
    this.mIsConnected = false;
    this.castContext.getSessionManagerImpl().onSessionStarting(this);
    this.proxy.start(routeInfoExtra);
}
 
Example #24
Source File: MediaRouterCallbackImpl.java    From android_packages_apps_GmsCore with Apache License 2.0 5 votes vote down vote up
@Override
public void onRouteSelected(String routeId, Bundle extras) throws RemoteException {
    CastDevice castDevice = CastDevice.getFromBundle(extras);

    SessionImpl session = (SessionImpl) ObjectWrapper.unwrap(this.castContext.defaultSessionProvider.getSession(null));
    Bundle routeInfoExtras = this.castContext.getRouter().getRouteInfoExtrasById(routeId);
    if (routeInfoExtras != null) {
        session.start(this.castContext, castDevice, routeId, routeInfoExtras);
    }
}
 
Example #25
Source File: MainActivity.java    From AndroidDemoProjects with Apache License 2.0 5 votes vote down vote up
@Override
public void onRouteSelected(MediaRouter router, RouteInfo info) {
    initCastClientListener();
    initRemoteMediaPlayer();

    mSelectedDevice = CastDevice.getFromBundle( info.getExtras() );

    launchReceiver();
}
 
Example #26
Source File: CastApplication.java    From googleads-ima-android with Apache License 2.0 5 votes vote down vote up
@Override
public void onMessageReceived(CastDevice castDevice, String namespace, String message) {
  Log.d(TAG, "onMessageReceived: " + message);
  String[] splitMessage = message.split(",");
  String event = splitMessage[0];
  switch (event) {
    case "onContentPauseRequested":
      mCastAdPlaying = true;
      mCastContentTime = Double.parseDouble(splitMessage[1]);
      break;
    case "onContentResumeRequested":
      mCastAdPlaying = false;
      break;
  }
}
 
Example #27
Source File: DataCastManager.java    From UTubeTV with The Unlicense 5 votes vote down vote up
@Override
protected Builder getCastOptionBuilder(CastDevice device) {

  Builder builder = Cast.CastOptions.builder(mSelectedCastDevice, new CastListener());
  if (isFeatureEnabled(FEATURE_DEBUGGING)) {
    builder.setVerboseLoggingEnabled(true);
  }
  return builder;
}
 
Example #28
Source File: DataCastManager.java    From UTubeTV with The Unlicense 5 votes vote down vote up
/**
 * *********************************************************************
 */

@Override
public void onMessageReceived(CastDevice castDevice, String namespace, String message) {
  for (IDataCastConsumer consumer : mDataConsumers) {
    try {
      consumer.onMessageReceived(castDevice, namespace, message);
    } catch (Exception e) {
      CastUtils.LOGE(TAG, "onMessageReceived(): Failed to inform " + consumer, e);
    }
  }

}
 
Example #29
Source File: VideoCastManager.java    From UTubeTV with The Unlicense 5 votes vote down vote up
/**
 * ** Implementing abstract methods of BaseCastManager *****
 */

@Override
Builder getCastOptionBuilder(CastDevice device) {
  Builder builder = Cast.CastOptions.builder(mSelectedCastDevice, new CastListener());
  if (isFeatureEnabled(FEATURE_DEBUGGING)) {
    builder.setVerboseLoggingEnabled(true);
  }
  return builder;
}
 
Example #30
Source File: CastMediaRouterCallback.java    From UTubeTV with The Unlicense 5 votes vote down vote up
@Override
public void onRouteSelected(MediaRouter router, RouteInfo info) {
  CastUtils.LOGD(TAG, "onRouteSelected: info=" + info);
  if (BaseCastManager.getCastManager()
      .getReconnectionStatus() == BaseCastManager.ReconnectionStatus.FINALIZE) {
    BaseCastManager.getCastManager().setReconnectionStatus(ReconnectionStatus.INACTIVE);
    BaseCastManager.getCastManager().cancelReconnectionTask();
    return;
  }
  CastUtils.saveStringToPreference(mContext, BaseCastManager.PREFS_KEY_ROUTE_ID, info.getId());
  CastDevice device = CastDevice.getFromBundle(info.getExtras());
  selectDeviceInterface.onDeviceSelected(device);
  CastUtils.LOGD(TAG, "onResult: mSelectedDevice=" + device.getFriendlyName());
}