use of android.support.v7.media.MediaRouter.RouteInfo in project Shuttle by timusus.
the class BaseCastManager method reconnectSessionIfPossible.
/**
* This method tries to automatically re-establish connection to a session if
* <ul>
* <li>User had not done a manual disconnect in the last session
* <li>The Cast Device that user had connected to previously is still running the same session
* </ul>
* Under these conditions, a best-effort attempt will be made to continue with the same
* session.
* This attempt will go on for <code>timeoutInSeconds</code> seconds.
*
* @param timeoutInSeconds the length of time, in seconds, to attempt reconnection before giving
* up
* @param ssidName The name of Wifi SSID
*/
@TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH)
public void reconnectSessionIfPossible(final int timeoutInSeconds, String ssidName) {
LOGD(TAG, String.format("reconnectSessionIfPossible(%d, %s)", timeoutInSeconds, ssidName));
if (isConnected()) {
return;
}
String routeId = mPreferenceAccessor.getStringFromPreference(PREFS_KEY_ROUTE_ID);
if (canConsiderSessionRecovery(ssidName)) {
List<RouteInfo> routes = mMediaRouter.getRoutes();
RouteInfo theRoute = null;
if (routes != null) {
for (RouteInfo route : routes) {
if (route.getId().equals(routeId)) {
theRoute = route;
break;
}
}
}
if (theRoute != null) {
// route has already been discovered, so lets just get the device
reconnectSessionIfPossibleInternal(theRoute);
} else {
// we set a flag so if the route is discovered within a short period, we let
// onRouteAdded callback of CastMediaRouterCallback take care of that
setReconnectionStatus(RECONNECTION_STATUS_STARTED);
}
// cancel any prior reconnection task
if (mReconnectionTask != null && !mReconnectionTask.isCancelled()) {
mReconnectionTask.cancel(true);
}
// we may need to reconnect to an existing session
mReconnectionTask = new AsyncTask<Void, Integer, Boolean>() {
@Override
protected Boolean doInBackground(Void... params) {
for (int i = 0; i < timeoutInSeconds; i++) {
LOGD(TAG, "Reconnection: Attempt " + (i + 1));
if (isCancelled()) {
return true;
}
try {
if (isConnected()) {
cancel(true);
}
Thread.sleep(1000);
} catch (InterruptedException e) {
// ignore
}
}
return false;
}
@Override
protected void onPostExecute(Boolean result) {
if (result == null || !result) {
LOGD(TAG, "Couldn't reconnect, dropping connection");
setReconnectionStatus(RECONNECTION_STATUS_INACTIVE);
onDeviceSelected(null, /* CastDevice */
null);
}
}
};
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
mReconnectionTask.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
} else {
mReconnectionTask.execute();
}
}
}
use of android.support.v7.media.MediaRouter.RouteInfo in project Shuttle by timusus.
the class DataCastManager method onApplicationConnected.
@Override
public void onApplicationConnected(ApplicationMetadata appMetadata, String applicationStatus, String sessionId, boolean wasLaunched) {
LOGD(TAG, "onApplicationConnected() reached with sessionId: " + sessionId);
// saving session for future retrieval; we only save the last session info
mPreferenceAccessor.saveStringToPreference(PREFS_KEY_SESSION_ID, sessionId);
if (mReconnectionStatus == RECONNECTION_STATUS_IN_PROGRESS) {
// we have tried to reconnect and successfully launched the app, so
// it is time to select the route and make the cast icon happy :-)
List<RouteInfo> routes = mMediaRouter.getRoutes();
if (routes != null) {
String routeId = mPreferenceAccessor.getStringFromPreference(PREFS_KEY_ROUTE_ID);
boolean found = false;
for (RouteInfo routeInfo : routes) {
if (routeId.equals(routeInfo.getId())) {
// found the right route
LOGD(TAG, "Found the correct route during reconnection attempt");
found = true;
mReconnectionStatus = RECONNECTION_STATUS_FINALIZED;
mMediaRouter.selectRoute(routeInfo);
break;
}
}
if (!found) {
// we were hoping to have the route that we wanted, but we
// didn't so we deselect the device
onDeviceSelected(null, /* CastDevice */
null);
mReconnectionStatus = RECONNECTION_STATUS_INACTIVE;
return;
}
}
}
// registering namespaces, if any
try {
attachDataChannels();
mSessionId = sessionId;
for (DataCastConsumer consumer : mDataConsumers) {
consumer.onApplicationConnected(appMetadata, applicationStatus, sessionId, wasLaunched);
}
} catch (IllegalStateException | IOException e) {
LOGE(TAG, "Failed to attach namespaces", e);
}
}
use of android.support.v7.media.MediaRouter.RouteInfo in project Shuttle by timusus.
the class VideoCastManager method onApplicationConnected.
@Override
protected void onApplicationConnected(ApplicationMetadata appMetadata, String applicationStatus, String sessionId, boolean wasLaunched) {
LOGD(TAG, "onApplicationConnected() reached with sessionId: " + sessionId + ", and mReconnectionStatus=" + mReconnectionStatus);
mApplicationErrorCode = NO_APPLICATION_ERROR;
if (mReconnectionStatus == RECONNECTION_STATUS_IN_PROGRESS) {
// we have tried to reconnect and successfully launched the app, so
// it is time to select the route and make the cast icon happy :-)
List<RouteInfo> routes = mMediaRouter.getRoutes();
if (routes != null) {
String routeId = mPreferenceAccessor.getStringFromPreference(PREFS_KEY_ROUTE_ID);
for (RouteInfo routeInfo : routes) {
if (routeId.equals(routeInfo.getId())) {
// found the right route
LOGD(TAG, "Found the correct route during reconnection attempt");
mReconnectionStatus = RECONNECTION_STATUS_FINALIZED;
mMediaRouter.selectRoute(routeInfo);
break;
}
}
}
}
startNotificationService();
try {
attachDataChannel();
attachMediaChannel();
mSessionId = sessionId;
// saving device for future retrieval; we only save the last session info
mPreferenceAccessor.saveStringToPreference(PREFS_KEY_SESSION_ID, mSessionId);
mRemoteMediaPlayer.requestStatus(mApiClient).setResultCallback(new ResultCallback<RemoteMediaPlayer.MediaChannelResult>() {
@Override
public void onResult(MediaChannelResult result) {
if (!result.getStatus().isSuccess()) {
onFailed(R.string.ccl_failed_status_request, result.getStatus().getStatusCode());
}
}
});
for (VideoCastConsumer consumer : mVideoConsumers) {
consumer.onApplicationConnected(appMetadata, mSessionId, wasLaunched);
}
} catch (TransientNetworkDisconnectionException e) {
LOGE(TAG, "Failed to attach media/data channel due to network issues", e);
onFailed(R.string.ccl_failed_no_connection_trans, NO_STATUS_CODE);
} catch (NoConnectionException e) {
LOGE(TAG, "Failed to attach media/data channel due to network issues", e);
onFailed(R.string.ccl_failed_no_connection, NO_STATUS_CODE);
}
}
use of android.support.v7.media.MediaRouter.RouteInfo in project AndroidChromium by JackyAndroid.
the class CastMediaRouteProvider method startObservingMediaSinks.
@Override
public void startObservingMediaSinks(String sourceId) {
if (mAndroidMediaRouter == null)
return;
MediaSource source = MediaSource.from(sourceId);
if (source == null)
return;
MediaRouteSelector routeSelector = source.buildRouteSelector();
if (routeSelector == null) {
// If the application invalid, report no devices available.
onSinksReceived(sourceId, new ArrayList<MediaSink>());
return;
}
String applicationId = source.getApplicationId();
DiscoveryCallback callback = mDiscoveryCallbacks.get(applicationId);
if (callback != null) {
callback.addSourceUrn(sourceId);
return;
}
List<MediaSink> knownSinks = new ArrayList<MediaSink>();
for (RouteInfo route : mAndroidMediaRouter.getRoutes()) {
if (route.matchesSelector(routeSelector)) {
knownSinks.add(MediaSink.fromRoute(route));
}
}
callback = new DiscoveryCallback(sourceId, knownSinks, this, routeSelector);
mAndroidMediaRouter.addCallback(routeSelector, callback, MediaRouter.CALLBACK_FLAG_REQUEST_DISCOVERY);
mDiscoveryCallbacks.put(applicationId, callback);
}
use of android.support.v7.media.MediaRouter.RouteInfo in project Shuttle by timusus.
the class VideoCastManager method updateVolume.
/**
* Increments or decrements volume by <code>delta</code> if {@code delta < 0} or
* {@code delta > 0}, respectively. Note that the volume range is between 0 and {@code
* RouteInfo.getVolumeMax()}.
*/
public void updateVolume(int delta) {
RouteInfo info = mMediaRouter.getSelectedRoute();
info.requestUpdateVolume(delta);
}
Aggregations