Skip to content

Commit

Permalink
VEX-6324: Hardening Google Cast session handling (#10)
Browse files Browse the repository at this point in the history
Wraps all RN methods in try/catch and propagates the errors back to JS for handling them there instead.

Jira: VEX-6324
https://jira.tenkasu.net/browse/VEX-6324

There are instances where Google Cast can have no active session but we request some information, this caused a crash. To prevent this from crashing outright all native methods that need session were wrapped in try catch and now reject the promise which moves the error handling to JS for a better user experience and potential recovery.
  • Loading branch information
armands-malejevs authored Mar 1, 2022
1 parent 2a31248 commit 255bc77
Show file tree
Hide file tree
Showing 3 changed files with 343 additions and 200 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@
import com.reactnative.googlecast.types.RNGCMediaTrackType;
import com.reactnative.googlecast.types.RNGCStandbyState;

import android.util.Log;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
Expand Down Expand Up @@ -81,106 +83,151 @@ private void sendEvent(@NonNull String eventName, @Nullable Object params) {

@ReactMethod
public void getActiveInputState(final Promise promise) {
with.withX(new With.WithX<CastSession>() {
@Override
public void execute(CastSession castSession) {
promise.resolve(
RNGCActiveInputState.toJson(castSession.getActiveInputState()));
}
}, promise);
try {
with.withX(new With.WithX<CastSession>() {
@Override
public void execute(CastSession castSession) {
promise.resolve(
RNGCActiveInputState.toJson(castSession.getActiveInputState()));
}
}, promise);
} catch (Exception e) {
Log.e("RNGCCastSession", e.toString());
promise.reject("RNGCCastSession", e.toString());
}
}

@ReactMethod
public void getApplicationMetadata(final Promise promise) {
with.withX(new With.WithX<CastSession>() {
@Override
public void execute(CastSession castSession) {
promise.resolve(RNGCApplicationMetadata.toJson(
castSession.getApplicationMetadata()));
}
}, promise);
try {
with.withX(new With.WithX<CastSession>() {
@Override
public void execute(CastSession castSession) {
promise.resolve(RNGCApplicationMetadata.toJson(
castSession.getApplicationMetadata()));
}
}, promise);
} catch (Exception e) {
Log.e("RNGCCastSession", e.toString());
promise.reject("RNGCCastSession", e.toString());
}
}

@ReactMethod
public void getApplicationStatus(final Promise promise) {
with.withX(new With.WithX<CastSession>() {
@Override
public void execute(CastSession castSession) {
promise.resolve(castSession.getApplicationStatus());
}
}, promise);
try {
with.withX(new With.WithX<CastSession>() {
@Override
public void execute(CastSession castSession) {
promise.resolve(castSession.getApplicationStatus());
}
}, promise);
} catch (Exception e) {
Log.e("RNGCCastSession", e.toString());
promise.reject("RNGCCastSession", e.toString());
}
}

@ReactMethod
public void getCastDevice(final Promise promise) {
with.withX(new With.WithX<CastSession>() {
@Override
public void execute(CastSession castSession) {
promise.resolve(RNGCDevice.toJson(castSession.getCastDevice()));
}
}, promise);
try {
with.withX(new With.WithX<CastSession>() {
@Override
public void execute(CastSession castSession) {
promise.resolve(RNGCDevice.toJson(castSession.getCastDevice()));
}
}, promise);
} catch (Exception e) {
Log.e("RNGCCastSession", e.toString());
promise.reject("RNGCCastSession", e.toString());
}
}

@ReactMethod
public void getStandbyState(final Promise promise) {
with.withX(new With.WithX<CastSession>() {
@Override
public void execute(CastSession castSession) {
promise.resolve(RNGCStandbyState.toJson(castSession.getStandbyState()));
}
}, promise);
try {
with.withX(new With.WithX<CastSession>() {
@Override
public void execute(CastSession castSession) {
promise.resolve(RNGCStandbyState.toJson(castSession.getStandbyState()));
}
}, promise);
} catch (Exception e) {
Log.e("RNGCCastSession", e.toString());
promise.reject("RNGCCastSession", e.toString());
}
}

@ReactMethod
public void getVolume(final Promise promise) {
with.withX(new With.WithX<CastSession>() {
@Override
public void execute(CastSession castSession) {
promise.resolve(castSession.getVolume());
}
}, promise);
try {
with.withX(new With.WithX<CastSession>() {
@Override
public void execute(CastSession castSession) {
promise.resolve(castSession.getVolume());
}
}, promise);
} catch (Exception e) {
Log.e("RNGCCastSession", e.toString());
promise.reject("RNGCCastSession", e.toString());
}
}

@ReactMethod
public void isMute(final Promise promise) {
with.withX(new With.WithX<CastSession>() {
@Override
public void execute(CastSession castSession) {
promise.resolve(castSession.isMute());
}
}, promise);
try {
with.withX(new With.WithX<CastSession>() {
@Override
public void execute(CastSession castSession) {
promise.resolve(castSession.isMute());
}
}, promise);
} catch (Exception e) {
Log.e("RNGCCastSession", e.toString());
promise.reject("RNGCCastSession", e.toString());
}
}

// CHANNELS

@ReactMethod
public void addChannel(final String namespace, final Promise promise) {
with.withX(new With.WithX<CastSession>() {
@Override
public void execute(CastSession castSession) {
try {
castSession.setMessageReceivedCallbacks(namespace, messageCallback);
promise.resolve(null);
} catch (IOException e) {
promise.reject(e);
try {
with.withX(new With.WithX<CastSession>() {
@Override
public void execute(CastSession castSession) {
try {
castSession.setMessageReceivedCallbacks(namespace, messageCallback);
promise.resolve(null);
} catch (IOException e) {
promise.reject(e);
}
}
}
});
});
} catch (Exception e) {
Log.e("RNGCCastSession", e.toString());
promise.reject("RNGCCastSession", e.toString());
}
}

@ReactMethod
public void removeChannel(final String namespace, final Promise promise) {
with.withX(new With.WithX<CastSession>() {
@Override
public void execute(CastSession castSession) {
try {
castSession.removeMessageReceivedCallbacks(namespace);
promise.resolve(null);
} catch (IOException e) {
promise.reject(e);
try {
with.withX(new With.WithX<CastSession>() {
@Override
public void execute(CastSession castSession) {
try {
castSession.removeMessageReceivedCallbacks(namespace);
promise.resolve(null);
} catch (IOException e) {
promise.reject(e);
}
}
}
});
});
} catch (Exception e) {
Log.e("RNGCCastSession", e.toString());
promise.reject("RNGCCastSession", e.toString());
}
}

@ReactMethod
Expand Down
Loading

0 comments on commit 255bc77

Please sign in to comment.