From d00ce6ef63c649493306d9a8587dcbaae9fdfc07 Mon Sep 17 00:00:00 2001 From: Kamil Mrowiec Date: Mon, 24 Jan 2022 19:12:20 +0100 Subject: [PATCH 1/2] Wrapped unregisterReceiver with try catch --- .../pharsh/sms_user_consent/SmsUserConsentPlugin.kt | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/android/src/main/kotlin/dev/pharsh/sms_user_consent/SmsUserConsentPlugin.kt b/android/src/main/kotlin/dev/pharsh/sms_user_consent/SmsUserConsentPlugin.kt index b4de8fb..16fc0ac 100644 --- a/android/src/main/kotlin/dev/pharsh/sms_user_consent/SmsUserConsentPlugin.kt +++ b/android/src/main/kotlin/dev/pharsh/sms_user_consent/SmsUserConsentPlugin.kt @@ -69,11 +69,19 @@ class SmsUserConsentPlugin : FlutterPlugin, MethodCallHandler, ActivityAware { if (resultCode == Activity.RESULT_OK && data != null) { channel .invokeMethod("receivedSms", data.getStringExtra(SmsRetriever.EXTRA_SMS_MESSAGE)) - mActivity.unregisterReceiver(smsVerificationReceiver) + try { + mActivity.unregisterReceiver(smsVerificationReceiver) + } catch (ex: Exception) { + // silent catch to avoid crash if receiver is not registered + } } else { // Consent denied. User can type OTC manually. channel.invokeMethod("receivedSms", null) - mActivity.unregisterReceiver(smsVerificationReceiver) + try { + mActivity.unregisterReceiver(smsVerificationReceiver) + } catch (ex: Exception) { + // silent catch to avoid crash if receiver is not registered + } } true } From 61bba8b85396b82c55d95b1e4211982b9d9f7a36 Mon Sep 17 00:00:00 2001 From: Kamil Mrowiec Date: Mon, 24 Jan 2022 19:35:44 +0100 Subject: [PATCH 2/2] Wrapped more code with try catch --- .../pharsh/sms_user_consent/SmsUserConsentPlugin.kt | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/android/src/main/kotlin/dev/pharsh/sms_user_consent/SmsUserConsentPlugin.kt b/android/src/main/kotlin/dev/pharsh/sms_user_consent/SmsUserConsentPlugin.kt index 16fc0ac..74a7fed 100644 --- a/android/src/main/kotlin/dev/pharsh/sms_user_consent/SmsUserConsentPlugin.kt +++ b/android/src/main/kotlin/dev/pharsh/sms_user_consent/SmsUserConsentPlugin.kt @@ -67,19 +67,18 @@ class SmsUserConsentPlugin : FlutterPlugin, MethodCallHandler, ActivityAware { } SMS_CONSENT_REQUEST -> {// Obtain the phone number from the result if (resultCode == Activity.RESULT_OK && data != null) { - channel - .invokeMethod("receivedSms", data.getStringExtra(SmsRetriever.EXTRA_SMS_MESSAGE)) try { + channel.invokeMethod("receivedSms", data.getStringExtra(SmsRetriever.EXTRA_SMS_MESSAGE)) mActivity.unregisterReceiver(smsVerificationReceiver) - } catch (ex: Exception) { + } catch (e: Exception) { // silent catch to avoid crash if receiver is not registered } } else { - // Consent denied. User can type OTC manually. - channel.invokeMethod("receivedSms", null) try { + // Consent denied. User can type OTC manually. + channel.invokeMethod("receivedSms", null) mActivity.unregisterReceiver(smsVerificationReceiver) - } catch (ex: Exception) { + } catch (e: Exception) { // silent catch to avoid crash if receiver is not registered } }