Skip to content

Commit

Permalink
tv-casting-app: Handled uncaught exceptions and null pointer issues
Browse files Browse the repository at this point in the history
  • Loading branch information
sharadb-amazon committed Feb 7, 2023
1 parent 2e0a6f2 commit f02e9cd
Show file tree
Hide file tree
Showing 8 changed files with 150 additions and 66 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,12 @@ public void onClick(View v) {
private void runCertTests(Activity activity) {
CertTestMatterSuccessFailureCallback successFailureCallback =
new CertTestMatterSuccessFailureCallback(activity);
CertTestMatterSuccessFailureCallbackInteger successFailureCallbackInteger =
new CertTestMatterSuccessFailureCallbackInteger(successFailureCallback);
CertTestMatterSuccessCallback successCallback =
new CertTestMatterSuccessCallback(successFailureCallback);
CertTestMatterFailureCallback failureCallback =
new CertTestMatterFailureCallback(successFailureCallback);
CertTestMatterSuccessCallbackInteger successCallbackInteger =
new CertTestMatterSuccessCallbackInteger(successFailureCallback);
CertTestMatterCallbackHandler callback =
new CertTestMatterCallbackHandler(successFailureCallback);

Expand Down Expand Up @@ -269,39 +273,39 @@ private void runCertTests(Activity activity) {
successFailureCallback,
() -> {
tvCastingApp.applicationBasic_readApplicationVersion(
kContentApp, successFailureCallback, successFailureCallback);
kContentApp, successCallback, failureCallback);
});

runAndWait(
"applicationBasic_readVendorName",
successFailureCallback,
() -> {
tvCastingApp.applicationBasic_readVendorName(
kContentApp, successFailureCallback, successFailureCallback);
kContentApp, successCallback, failureCallback);
});

runAndWait(
"applicationBasic_readApplicationName",
successFailureCallback,
() -> {
tvCastingApp.applicationBasic_readApplicationName(
kContentApp, successFailureCallback, successFailureCallback);
kContentApp, successCallback, failureCallback);
});

runAndWait(
"applicationBasic_readVendorID",
successFailureCallback,
() -> {
tvCastingApp.applicationBasic_readVendorID(
kContentApp, successFailureCallbackInteger, successFailureCallbackInteger);
kContentApp, successCallbackInteger, failureCallback);
});

runAndWait(
"applicationBasic_readProductID",
successFailureCallback,
() -> {
tvCastingApp.applicationBasic_readProductID(
kContentApp, successFailureCallbackInteger, successFailureCallbackInteger);
kContentApp, successCallbackInteger, failureCallback);
});

runAndWait(
Expand All @@ -320,7 +324,7 @@ public void handle(MediaPlaybackTypes.PlaybackStateEnum response) {
activity, MatterError.NO_ERROR, "mediaPlayback_subscribeToCurrentState");
}
},
successFailureCallback,
failureCallback,
0,
20,
new SubscriptionEstablishedCallback() {
Expand Down Expand Up @@ -415,8 +419,7 @@ public void handle(MatterError error) {
}
}

class CertTestMatterSuccessFailureCallback extends FailureCallback
implements SuccessCallback<String> {
class CertTestMatterSuccessFailureCallback {
private Activity activity;
private String testMethod;
private CountDownLatch cdl;
Expand All @@ -433,7 +436,6 @@ public void setCountDownLatch(CountDownLatch cdl) {
this.cdl = cdl;
}

@Override
public void handle(MatterError error) {
try {
cdl.countDown();
Expand All @@ -446,7 +448,6 @@ public void handle(MatterError error) {
}
}

@Override
public void handle(String response) {
try {
cdl.countDown();
Expand All @@ -460,23 +461,45 @@ public void handle(String response) {
}
}

class CertTestMatterSuccessFailureCallbackInteger extends FailureCallback
implements SuccessCallback<Integer> {
class CertTestMatterSuccessCallback extends SuccessCallback<String> {
private CertTestMatterSuccessFailureCallback delegate;

private CertTestMatterSuccessFailureCallback delegate;
CertTestMatterSuccessCallback(CertTestMatterSuccessFailureCallback delegate)
{
this.delegate = delegate;
}

CertTestMatterSuccessFailureCallbackInteger(CertTestMatterSuccessFailureCallback delegate) {
this.delegate = delegate;
}
@Override
public void handle(String response) {
delegate.handle(response);
}
}

@Override
public void handle(MatterError error) {
delegate.handle(error);
}
class CertTestMatterFailureCallback extends FailureCallback {
private CertTestMatterSuccessFailureCallback delegate;

@Override
public void handle(Integer response) {
delegate.handle(response.toString());
}
CertTestMatterFailureCallback(CertTestMatterSuccessFailureCallback delegate)
{
this.delegate = delegate;
}

@Override
public void handle(MatterError err) {
delegate.handle(err);
}
}

class CertTestMatterSuccessCallbackInteger extends SuccessCallback<Integer> {

private CertTestMatterSuccessFailureCallback delegate;

CertTestMatterSuccessCallbackInteger(CertTestMatterSuccessFailureCallback delegate) {
this.delegate = delegate;
}

@Override
public void handle(Integer response) {
delegate.handle(response.toString());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
import android.widget.TextView;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentActivity;

import com.chip.casting.ContentApp;
import com.chip.casting.FailureCallback;
import com.chip.casting.MatterError;
Expand Down Expand Up @@ -62,23 +64,27 @@ public void onClick(View v) {
getView().findViewById(R.id.currentStateSubscriptionEstablishedStatus);
TextView currentStateValue = getView().findViewById(R.id.currentStateValue);

SuccessCallback<MediaPlaybackTypes.PlaybackStateEnum> successCallback =
playbackStateEnum -> {
Log.d(
TAG,
"handle() called on SuccessCallback<MediaPlaybackResponseTypes.PlaybackStateEnum> with "
+ playbackStateEnum);
getActivity()
.runOnUiThread(
new Runnable() {
@Override
public void run() {
if (playbackStateEnum != null) {
currentStateValue.setText(playbackStateEnum.toString());
}
}
});
};
SuccessCallback<MediaPlaybackTypes.PlaybackStateEnum> successCallback = new SuccessCallback<MediaPlaybackTypes.PlaybackStateEnum>() {
@Override
public void handle(MediaPlaybackTypes.PlaybackStateEnum playbackStateEnum) {
Log.d(
TAG,
"handle() called on SuccessCallback<MediaPlaybackResponseTypes.PlaybackStateEnum> with "
+ playbackStateEnum);
FragmentActivity fragmentActivity = getActivity();
if (fragmentActivity != null) {
fragmentActivity.runOnUiThread(
new Runnable() {
@Override
public void run() {
if (playbackStateEnum != null) {
currentStateValue.setText(playbackStateEnum.toString());
}
}
});
}
}
};

FailureCallback failureCallback =
new FailureCallback() {
Expand All @@ -96,19 +102,22 @@ public void run() {
}
};

SubscriptionEstablishedCallback subscriptionEstablishedCallback =
(SubscriptionEstablishedCallback)
() -> {
Log.d(TAG, "handle() called on SubscriptionEstablishedCallback");
getActivity()
.runOnUiThread(
new Runnable() {
@Override
public void run() {
subscriptionStatus.setText("Subscription established!");
}
});
};
SubscriptionEstablishedCallback subscriptionEstablishedCallback = new SubscriptionEstablishedCallback() {
@Override
public void handle() {
Log.d(TAG, "handle() called on SubscriptionEstablishedCallback");
FragmentActivity fragmentActivity = getActivity();
if (fragmentActivity != null) {
fragmentActivity.runOnUiThread(
new Runnable() {
@Override
public void run() {
subscriptionStatus.setText("Subscription established!");
}
});
}
}
};

boolean retVal =
tvCastingApp.mediaPlayback_subscribeToCurrentState(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,20 @@
*/
package com.chip.casting;

import android.util.Log;

public abstract class FailureCallback {
private static final String TAG = FailureCallback.class.getSimpleName();

public abstract void handle(MatterError err);

public final void handle(int errorCode, String errorMessage) {
handle(new MatterError(errorCode, errorMessage));
private final void handleInternal(int errorCode, String errorMessage) {
try {
handle(new MatterError(errorCode, errorMessage));
}
catch(Exception e)
{
Log.e(TAG, "FailureCallback::Caught an unhandled exception from the client: " + e);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,20 @@
*/
package com.chip.casting;

import android.util.Log;

public abstract class MatterCallbackHandler {
private static final String TAG = MatterCallbackHandler.class.getSimpleName();

public abstract void handle(MatterError err);

public final void handle(int errorCode, String errorMessage) {
handle(new MatterError(errorCode, errorMessage));
private final void handleInternal(int errorCode, String errorMessage) {
try {
handle(new MatterError(errorCode, errorMessage));
}
catch(Exception e)
{
Log.e(TAG, "MatterCallbackHandler::Caught an unhandled exception from the client: " + e);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,21 @@
*/
package com.chip.casting;

public interface SubscriptionEstablishedCallback {
void handle();
import android.util.Log;

public abstract class SubscriptionEstablishedCallback {
private static final String TAG = SubscriptionEstablishedCallback.class.getSimpleName();

public abstract void handle();

private void handleInternal()
{
try
{
handle();
}
catch(Exception e) {
Log.e(TAG, "SubscriptionEstablishedCallback::Caught an unhandled exception from the client: " + e);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,20 @@
*/
package com.chip.casting;

public interface SuccessCallback<R> {
void handle(R response);
import android.util.Log;
public abstract class SuccessCallback<R> {
private static final String TAG = SuccessCallback.class.getSimpleName();

public abstract void handle(R response);

public void handleInternal(R response)
{
try
{
handle(response);
}
catch(Exception e) {
Log.e(TAG, "SuccessCallback::Caught an unhandled exception from the client: " + e);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ CHIP_ERROR convertJAppParametersToCppAppParams(jobject appParameters, AppParams
CHIP_ERROR convertJContentAppToTargetEndpointInfo(jobject contentApp, TargetEndpointInfo & outTargetEndpointInfo)
{
ChipLogProgress(AppServer, "convertJContentAppToTargetEndpointInfo called");
VerifyOrReturnError(contentApp != nullptr, CHIP_ERROR_INVALID_ARGUMENT);
JNIEnv * env = chip::JniReferences::GetInstance().GetEnvForCurrentThread();

jclass jContentAppClass;
Expand Down Expand Up @@ -124,6 +125,7 @@ CHIP_ERROR convertTargetEndpointInfoToJContentApp(TargetEndpointInfo * targetEnd
CHIP_ERROR convertJVideoPlayerToTargetVideoPlayerInfo(jobject videoPlayer, TargetVideoPlayerInfo & outTargetVideoPlayerInfo)
{
ChipLogProgress(AppServer, "convertJVideoPlayerToTargetVideoPlayerInfo called");
VerifyOrReturnError(videoPlayer != nullptr, CHIP_ERROR_INVALID_ARGUMENT);
JNIEnv * env = chip::JniReferences::GetInstance().GetEnvForCurrentThread();

jclass jVideoPlayerClass;
Expand Down Expand Up @@ -241,6 +243,7 @@ CHIP_ERROR convertJDiscoveredNodeDataToCppDiscoveredNodeData(jobject jDiscovered
chip::Dnssd::DiscoveredNodeData & outCppDiscoveredNodeData)
{
ChipLogProgress(AppServer, "convertJDiscoveredNodeDataToCppDiscoveredNodeData called");
VerifyOrReturnError(jDiscoveredNodeData != nullptr, CHIP_ERROR_INVALID_ARGUMENT);

JNIEnv * env = chip::JniReferences::GetInstance().GetEnvForCurrentThread();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,10 @@ CHIP_ERROR CallbackBaseJNI::SetUp(JNIEnv * env, jobject inHandler)
mClazz = env->GetObjectClass(mObject);
VerifyOrExit(mClazz != nullptr, ChipLogError(AppServer, "Failed to get handler Java class"));

mMethod = env->GetMethodID(mClazz, "handle", mMethodSignature);
mMethod = env->GetMethodID(mClazz, "handleInternal", mMethodSignature);
if (mMethod == nullptr)
{
ChipLogError(AppServer, "Failed to access 'handle' method with signature %s", mMethodSignature);
ChipLogError(AppServer, "Failed to access 'handleInternal' method with signature %s", mMethodSignature);
env->ExceptionClear();
}

Expand Down

0 comments on commit f02e9cd

Please sign in to comment.