Skip to content

Commit

Permalink
Add protection for not update-to-date or missing google play services
Browse files Browse the repository at this point in the history
  • Loading branch information
Josh Hargreaves authored and petrbela committed Oct 26, 2019
1 parent 0aed931 commit ec143cd
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 17 deletions.
10 changes: 8 additions & 2 deletions CastButton.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

import React from 'react'
import PropTypes from 'prop-types'
import { requireNativeComponent } from 'react-native'
import { requireNativeComponent, NativeModules } from 'react-native';

const { RNGoogleCast: GoogleCast } = NativeModules;

/**
* Button that presents the Cast icon.
Expand All @@ -14,7 +16,11 @@ import { requireNativeComponent } from 'react-native'
*/
class CastButton extends React.Component {
render() {
return <GoogleCastButton {...this.props} />
if (GoogleCast.CAST_AVAILABLE) {
return <GoogleCastButton {...this.props} />;
} else {
return null;
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
import com.facebook.react.modules.core.DefaultHardwareBackBtnHandler;
import com.facebook.react.modules.core.PermissionAwareActivity;
import com.facebook.react.modules.core.PermissionListener;
import com.google.android.gms.cast.framework.CastContext;
import com.reactnative.googlecast.GoogleCastModule;

import javax.annotation.Nullable;

Expand Down Expand Up @@ -51,7 +51,7 @@ protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mDelegate.onCreate(savedInstanceState);
// lazy load Google Cast context
CastContext.getSharedInstance(this);
GoogleCastModule.initializeCast(this);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.reactnative.googlecast;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.support.annotation.NonNull;
Expand Down Expand Up @@ -62,13 +63,24 @@ public class GoogleCastModule

protected static final String CHANNEL_MESSAGE_RECEIVED = "GoogleCast:ChannelMessageReceived";

protected static final String E_CAST_NOT_AVAILABLE = "E_CAST_NOT_AVAILABLE";
protected static final String GOOGLE_CAST_NOT_AVAILABLE_MESSAGE = "Google Cast not available";

private CastSession mCastSession;
private SessionManagerListener<CastSession> mSessionManagerListener;

/*
'CAST_AVAILABLE' is volatile because 'initializeCast' is called on the main thread, but
react-native modules may be initialized on any thread.
*/
private static volatile boolean CAST_AVAILABLE = true;

public GoogleCastModule(ReactApplicationContext reactContext) {
super(reactContext);
reactContext.addLifecycleEventListener(this);
setupCastListener();
if (CAST_AVAILABLE) {
reactContext.addLifecycleEventListener(this);
setupCastListener();
}
}

@Override
Expand All @@ -94,6 +106,8 @@ public Map<String, Object> getConstants() {
constants.put("MEDIA_PLAYBACK_ENDED", MEDIA_PLAYBACK_ENDED);
constants.put("MEDIA_PROGRESS_UPDATED", MEDIA_PROGRESS_UPDATED);

constants.put("CAST_AVAILABLE", CAST_AVAILABLE);

constants.put("CHANNEL_MESSAGE_RECEIVED", CHANNEL_MESSAGE_RECEIVED);
return constants;
}
Expand Down Expand Up @@ -135,6 +149,14 @@ public void run() {
});
}

public static void initializeCast(Context context){
try {
CastContext.getSharedInstance(context);
} catch(RuntimeException e) {
CAST_AVAILABLE = false;
}
}

private MediaInfo buildMediaInfo(ReadableMap params) {
MediaMetadata movieMetadata =
new MediaMetadata(MediaMetadata.MEDIA_TYPE_MOVIE);
Expand Down Expand Up @@ -212,9 +234,13 @@ public void getCastState(final Promise promise) {
getReactApplicationContext().runOnUiQueueThread(new Runnable() {
@Override
public void run() {
CastContext castContext =
if (CAST_AVAILABLE) {
CastContext castContext =
CastContext.getSharedInstance(getReactApplicationContext());
promise.resolve(castContext.getCastState() - 1);
promise.resolve(castContext.getCastState() - 1);
} else {
promise.reject(E_CAST_NOT_AVAILABLE, GOOGLE_CAST_NOT_AVAILABLE_MESSAGE);
}
}
});
}
Expand Down Expand Up @@ -327,22 +353,30 @@ public void endSession(final boolean stopCasting, final Promise promise) {
getReactApplicationContext().runOnUiQueueThread(new Runnable() {
@Override
public void run() {
SessionManager sessionManager =
if (CAST_AVAILABLE) {
SessionManager sessionManager =
CastContext.getSharedInstance(getReactApplicationContext())
.getSessionManager();
sessionManager.endCurrentSession(stopCasting);
promise.resolve(true);
sessionManager.endCurrentSession(stopCasting);
promise.resolve(true);
} else {
promise.reject(E_CAST_NOT_AVAILABLE, GOOGLE_CAST_NOT_AVAILABLE_MESSAGE);
}
}
});
}

@ReactMethod
public void launchExpandedControls() {
ReactApplicationContext context = getReactApplicationContext();
Intent intent =
new Intent(context, GoogleCastExpandedControlsActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intent);
if (CAST_AVAILABLE) {
ReactApplicationContext context = getReactApplicationContext();
Intent intent = new Intent(context, GoogleCastExpandedControlsActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intent);
} else {
Log.i(REACT_CLASS, "Error :> " + GOOGLE_CAST_NOT_AVAILABLE_MESSAGE);
}

}

private void setupCastListener() {
Expand Down
4 changes: 3 additions & 1 deletion ios/RNGoogleCast/RNGoogleCast.m
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,9 @@ - (NSDictionary *)constantsToExport {

@"CHANNEL_CONNECTED" : CHANNEL_CONNECTED,
@"CHANNEL_MESSAGE_RECEIVED" : CHANNEL_MESSAGE_RECEIVED,
@"CHANNEL_DISCONNECTED" : CHANNEL_DISCONNECTED
@"CHANNEL_DISCONNECTED" : CHANNEL_DISCONNECTED,

@"CAST_AVAILABLE" : @YES
};
}

Expand Down

0 comments on commit ec143cd

Please sign in to comment.