From 2b7b8bad13e6847623fbe4ae98217247a65614be Mon Sep 17 00:00:00 2001 From: zafirskthelifehacker Date: Tue, 8 Feb 2022 16:10:26 +0400 Subject: [PATCH 01/22] Added catch statement --- src/ios/SpeechRecognition.m | 28 +++++++++++++++++++++++----- 1 file changed, 23 insertions(+), 5 deletions(-) diff --git a/src/ios/SpeechRecognition.m b/src/ios/SpeechRecognition.m index ef3553b..e5b2146 100644 --- a/src/ios/SpeechRecognition.m +++ b/src/ios/SpeechRecognition.m @@ -81,8 +81,8 @@ - (void)startListening:(CDVInvokedUrlCommand*)command { } AVAudioSession *audioSession = [AVAudioSession sharedInstance]; - [audioSession setCategory:AVAudioSessionCategoryPlayAndRecord withOptions:AVAudioSessionCategoryOptionDefaultToSpeaker error:nil]; - [audioSession setMode:AVAudioSessionModeDefault error:nil]; + [audioSession setCategory:AVAudioSessionCategoryPlayAndRecord error:nil]; + [audioSession setMode:AVAudioSessionModeMeasurement error:nil]; [audioSession setActive:YES withOptions:AVAudioSessionSetActiveOptionNotifyOthersOnDeactivation error:nil]; self.recognitionRequest = [[SFSpeechAudioBufferRecognitionRequest alloc] init]; @@ -142,10 +142,28 @@ - (void)startListening:(CDVInvokedUrlCommand*)command { self.recognitionTask = nil; } }]; + + @try { + [inputNode installTapOnBus:0 bufferSize:1024 format:format block:^(AVAudioPCMBuffer *buffer, AVAudioTime *when) { + [self.recognitionRequest appendAudioPCMBuffer:buffer]; + }]; + } @catch (NSException *exception) { + NSLog(@"startListening() recognitionTask error: %@", exception); - [inputNode installTapOnBus:0 bufferSize:1024 format:format block:^(AVAudioPCMBuffer *buffer, AVAudioTime *when) { - [self.recognitionRequest appendAudioPCMBuffer:buffer]; - }]; + [self.audioEngine stop]; + [self.audioEngine.inputNode removeTapOnBus:0]; + + self.recognitionRequest = nil; + self.recognitionTask = nil; + + NSString *errorMicInUse = @"Microphone in use by another app(iOS)"; + + CDVPluginResult *pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsString:errorMicInUse]; + if (showPartial){ + [pluginResult setKeepCallbackAsBool:YES]; + } + [self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId]; + } [self.audioEngine prepare]; [self.audioEngine startAndReturnError:nil]; From f3d6f0b9f6793eff9ff1e696ab22aab63ea222d2 Mon Sep 17 00:00:00 2001 From: zafirskthelifehacker Date: Wed, 9 Feb 2022 09:52:08 +0400 Subject: [PATCH 02/22] Stop Recognition Request --- src/ios/SpeechRecognition.m | 1 + 1 file changed, 1 insertion(+) diff --git a/src/ios/SpeechRecognition.m b/src/ios/SpeechRecognition.m index e5b2146..65936a6 100644 --- a/src/ios/SpeechRecognition.m +++ b/src/ios/SpeechRecognition.m @@ -152,6 +152,7 @@ - (void)startListening:(CDVInvokedUrlCommand*)command { [self.audioEngine stop]; [self.audioEngine.inputNode removeTapOnBus:0]; + [self.recognitionRequest endAudio]; self.recognitionRequest = nil; self.recognitionTask = nil; From 85bdcf7e4fc95821bd8f85521e8d1452626d433e Mon Sep 17 00:00:00 2001 From: zafirskthelifehacker Date: Wed, 9 Feb 2022 10:18:36 +0400 Subject: [PATCH 03/22] Reset Line 84 --- src/ios/SpeechRecognition.m | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/ios/SpeechRecognition.m b/src/ios/SpeechRecognition.m index 65936a6..e98f7c0 100644 --- a/src/ios/SpeechRecognition.m +++ b/src/ios/SpeechRecognition.m @@ -81,8 +81,8 @@ - (void)startListening:(CDVInvokedUrlCommand*)command { } AVAudioSession *audioSession = [AVAudioSession sharedInstance]; - [audioSession setCategory:AVAudioSessionCategoryPlayAndRecord error:nil]; - [audioSession setMode:AVAudioSessionModeMeasurement error:nil]; + [audioSession setCategory:AVAudioSessionCategoryPlayAndRecord withOptions:AVAudioSessionCategoryOptionDefaultToSpeaker error:nil]; + [audioSession setMode:AVAudioSessionModeDefault error:nil]; [audioSession setActive:YES withOptions:AVAudioSessionSetActiveOptionNotifyOthersOnDeactivation error:nil]; self.recognitionRequest = [[SFSpeechAudioBufferRecognitionRequest alloc] init]; From c7e20c475ae5b3a077a0c67933dc2aa9373fd982 Mon Sep 17 00:00:00 2001 From: zafirskthelifehacker Date: Thu, 3 Mar 2022 11:17:41 +0400 Subject: [PATCH 04/22] Modify Error Message --- src/ios/SpeechRecognition.m | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/src/ios/SpeechRecognition.m b/src/ios/SpeechRecognition.m index e98f7c0..42b63ed 100644 --- a/src/ios/SpeechRecognition.m +++ b/src/ios/SpeechRecognition.m @@ -15,7 +15,7 @@ #define MESSAGE_RESTRICTED @"Speech recognition restricted on this device" #define MESSAGE_NOT_DETERMINED @"Speech recognition not determined on this device" #define MESSAGE_ACCESS_DENIED_MICROPHONE @"User denied access to microphone" -#define MESSAGE_ONGOING @"Ongoing speech recognition" +#define MESSAGE_COULD_NOT_START_SPEECHRECOGNITION @"Could not start Speech Recongition. Microphone maybe in use by another app" @interface SpeechRecognition() @@ -157,9 +157,7 @@ - (void)startListening:(CDVInvokedUrlCommand*)command { self.recognitionRequest = nil; self.recognitionTask = nil; - NSString *errorMicInUse = @"Microphone in use by another app(iOS)"; - - CDVPluginResult *pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsString:errorMicInUse]; + CDVPluginResult *pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsString:MESSAGE_COULD_NOT_START_SPEECHRECOGNITION]; if (showPartial){ [pluginResult setKeepCallbackAsBool:YES]; } From 63a5724099fa53cc32c388da84fc2d6ce3c95e45 Mon Sep 17 00:00:00 2001 From: zafirskthelifehacker Date: Thu, 3 Mar 2022 11:43:10 +0400 Subject: [PATCH 05/22] Reset Ongoing Message --- src/ios/SpeechRecognition.m | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/ios/SpeechRecognition.m b/src/ios/SpeechRecognition.m index 42b63ed..0cee26e 100644 --- a/src/ios/SpeechRecognition.m +++ b/src/ios/SpeechRecognition.m @@ -15,7 +15,8 @@ #define MESSAGE_RESTRICTED @"Speech recognition restricted on this device" #define MESSAGE_NOT_DETERMINED @"Speech recognition not determined on this device" #define MESSAGE_ACCESS_DENIED_MICROPHONE @"User denied access to microphone" -#define MESSAGE_COULD_NOT_START_SPEECHRECOGNITION @"Could not start Speech Recongition. Microphone maybe in use by another app" +#define MESSAGE_ONGOING @"Ongoing speech recognition" +#define MESSAGE_COULD_NOT_START_SPEECHRECOGNITION @"Could not start Speech Recongition. Maybe Microphone is in use by another app" @interface SpeechRecognition() From 0276c68e836b147a231d729764ebbf7798bfdabb Mon Sep 17 00:00:00 2001 From: zafirskthelifehacker Date: Thu, 3 Mar 2022 11:45:46 +0400 Subject: [PATCH 06/22] Reset --- src/ios/SpeechRecognition.m | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/ios/SpeechRecognition.m b/src/ios/SpeechRecognition.m index 0cee26e..1b970c6 100644 --- a/src/ios/SpeechRecognition.m +++ b/src/ios/SpeechRecognition.m @@ -15,7 +15,7 @@ #define MESSAGE_RESTRICTED @"Speech recognition restricted on this device" #define MESSAGE_NOT_DETERMINED @"Speech recognition not determined on this device" #define MESSAGE_ACCESS_DENIED_MICROPHONE @"User denied access to microphone" -#define MESSAGE_ONGOING @"Ongoing speech recognition" +#define MESSAGE_ONGOING @"Ongoing speech recognition" s #define MESSAGE_COULD_NOT_START_SPEECHRECOGNITION @"Could not start Speech Recongition. Maybe Microphone is in use by another app" @interface SpeechRecognition() From 642ae26a97055546ef19aabf712d0ca476815c7d Mon Sep 17 00:00:00 2001 From: zafirskthelifehacker Date: Thu, 3 Mar 2022 11:46:06 +0400 Subject: [PATCH 07/22] Reset --- src/ios/SpeechRecognition.m | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/ios/SpeechRecognition.m b/src/ios/SpeechRecognition.m index 1b970c6..0cee26e 100644 --- a/src/ios/SpeechRecognition.m +++ b/src/ios/SpeechRecognition.m @@ -15,7 +15,7 @@ #define MESSAGE_RESTRICTED @"Speech recognition restricted on this device" #define MESSAGE_NOT_DETERMINED @"Speech recognition not determined on this device" #define MESSAGE_ACCESS_DENIED_MICROPHONE @"User denied access to microphone" -#define MESSAGE_ONGOING @"Ongoing speech recognition" s +#define MESSAGE_ONGOING @"Ongoing speech recognition" #define MESSAGE_COULD_NOT_START_SPEECHRECOGNITION @"Could not start Speech Recongition. Maybe Microphone is in use by another app" @interface SpeechRecognition() From 1a627fc7d6cfcb88cb4b1a089c9b50981ffaa71c Mon Sep 17 00:00:00 2001 From: zafirskthelifehacker Date: Thu, 3 Mar 2022 12:15:05 +0400 Subject: [PATCH 08/22] Change Message --- src/ios/SpeechRecognition.m | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/ios/SpeechRecognition.m b/src/ios/SpeechRecognition.m index 0cee26e..db7cce5 100644 --- a/src/ios/SpeechRecognition.m +++ b/src/ios/SpeechRecognition.m @@ -16,7 +16,7 @@ #define MESSAGE_NOT_DETERMINED @"Speech recognition not determined on this device" #define MESSAGE_ACCESS_DENIED_MICROPHONE @"User denied access to microphone" #define MESSAGE_ONGOING @"Ongoing speech recognition" -#define MESSAGE_COULD_NOT_START_SPEECHRECOGNITION @"Could not start Speech Recongition. Maybe Microphone is in use by another app" +#define MESSAGE_COULD_NOT_START_SPEECHRECOGNITION @"Could not start Speech Recongition. Microphone maybe busy." @interface SpeechRecognition() From dfd3663c167fa9c4fa24ca601029466ce23b5391 Mon Sep 17 00:00:00 2001 From: zafirskthelifehacker Date: Thu, 3 Mar 2022 12:19:18 +0400 Subject: [PATCH 09/22] Fix Message --- src/ios/SpeechRecognition.m | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/ios/SpeechRecognition.m b/src/ios/SpeechRecognition.m index db7cce5..62b1d85 100644 --- a/src/ios/SpeechRecognition.m +++ b/src/ios/SpeechRecognition.m @@ -16,7 +16,7 @@ #define MESSAGE_NOT_DETERMINED @"Speech recognition not determined on this device" #define MESSAGE_ACCESS_DENIED_MICROPHONE @"User denied access to microphone" #define MESSAGE_ONGOING @"Ongoing speech recognition" -#define MESSAGE_COULD_NOT_START_SPEECHRECOGNITION @"Could not start Speech Recongition. Microphone maybe busy." +#define MESSAGE_COULD_NOT_START_SPEECHRECOGNITION @"Could not start Speech Recognition. Microphone maybe busy" @interface SpeechRecognition() From 6ea61c36679c40f6b507939c8a4420835bdc09ca Mon Sep 17 00:00:00 2001 From: zafirskthelifehacker Date: Thu, 3 Mar 2022 12:23:45 +0400 Subject: [PATCH 10/22] Fix Error Message --- src/ios/SpeechRecognition.m | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/ios/SpeechRecognition.m b/src/ios/SpeechRecognition.m index 62b1d85..88037b9 100644 --- a/src/ios/SpeechRecognition.m +++ b/src/ios/SpeechRecognition.m @@ -16,7 +16,7 @@ #define MESSAGE_NOT_DETERMINED @"Speech recognition not determined on this device" #define MESSAGE_ACCESS_DENIED_MICROPHONE @"User denied access to microphone" #define MESSAGE_ONGOING @"Ongoing speech recognition" -#define MESSAGE_COULD_NOT_START_SPEECHRECOGNITION @"Could not start Speech Recognition. Microphone maybe busy" +#define MESSAGE_COULD_NOT_START_SPEECHRECOGNITION @"Could not start Speech Recognition. Microphone maybe busy." @interface SpeechRecognition() From edfdf4bc16f8bdd906a1bfb39077ba34845d429d Mon Sep 17 00:00:00 2001 From: zafirskthelifehacker Date: Fri, 4 Mar 2022 10:32:14 +0400 Subject: [PATCH 11/22] Modify Message --- src/ios/SpeechRecognition.m | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/ios/SpeechRecognition.m b/src/ios/SpeechRecognition.m index 88037b9..5ab9a3f 100644 --- a/src/ios/SpeechRecognition.m +++ b/src/ios/SpeechRecognition.m @@ -16,7 +16,7 @@ #define MESSAGE_NOT_DETERMINED @"Speech recognition not determined on this device" #define MESSAGE_ACCESS_DENIED_MICROPHONE @"User denied access to microphone" #define MESSAGE_ONGOING @"Ongoing speech recognition" -#define MESSAGE_COULD_NOT_START_SPEECHRECOGNITION @"Could not start Speech Recognition. Microphone maybe busy." +#define MESSAGE_COULD_NOT_START_SPEECHRECOGNITION @"Could not start Speech Recognition. Microphone may be busy." @interface SpeechRecognition() From f648071eedde055b9b5da42532e388c93a7da802 Mon Sep 17 00:00:00 2001 From: zafirskthelifehacker Date: Mon, 7 Mar 2022 14:48:32 +0400 Subject: [PATCH 12/22] v1.3.0 & Spoon Consulting Changes --- CHANGELOG.md | 4 ++++ README.md | 4 ++-- package.json | 14 +++++++------- plugin.xml | 8 ++++---- 4 files changed, 17 insertions(+), 13 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 2694848..216e0f1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,9 @@ # Changelog +## v1.3.0 + +- iOS: Catch exception when Microphone in use by another application + ## v1.2.0 - Android: add `stopListening` [by Simone Compagnone] diff --git a/README.md b/README.md index 8d0ccab..ba3c374 100644 --- a/README.md +++ b/README.md @@ -10,7 +10,7 @@ This is a cordova plugin for Speech Recognition. ## Installation ``` -cordova plugin add cordova-plugin-speechrecognition +cordova plugin add @spoonconsulting/cordova-plugin-speechrecognition ``` ## Supported Platforms @@ -220,7 +220,7 @@ The plugin works in [AVAudioSessionCategoryPlayAndRecord](https://developer.appl ### Peter Bakondy - https://github.com/pbakondy - +- https://github.com/spoonconsulting ## LICENSE diff --git a/package.json b/package.json index 96f064b..5f3d8b9 100644 --- a/package.json +++ b/package.json @@ -1,9 +1,9 @@ { - "name": "cordova-plugin-speechrecognition", - "version": "1.2.0", + "name": "@spoonconsulting/cordova-plugin-speechrecognition", + "version": "1.3.0", "description": "Cordova Plugin for Speech Recognition", "cordova": { - "id": "cordova-plugin-speechrecognition", + "id": "@spoonconsulting/cordova-plugin-speechrecognition", "platforms": [ "android", "ios" @@ -11,7 +11,7 @@ }, "repository": { "type": "git", - "url": "https://github.com/pbakondy/cordova-plugin-speechrecognition.git" + "url": "https://github.com/spoonconsulting/cordova-plugin-speechrecognition.git" }, "keywords": [ "cordova", @@ -27,10 +27,10 @@ "version": ">=3.0.0" } ], - "author": "Peter Bakondy", + "author": "Peter Bakondy & Spoon Consulting Ltd", "license": "MIT", "bugs": { - "url": "https://github.com/pbakondy/cordova-plugin-speechrecognition/issues" + "url": "https://github.com/spoonconsulting/cordova-plugin-speechrecognition/issues" }, - "homepage": "https://github.com/pbakondy/cordova-plugin-speechrecognition#readme" + "homepage": "https://github.com/spoonconsulting/cordova-plugin-speechrecognition#readme" } diff --git a/plugin.xml b/plugin.xml index 3c65c86..69e8837 100644 --- a/plugin.xml +++ b/plugin.xml @@ -1,15 +1,15 @@ + id="@spoonconsulting/cordova-plugin-speechrecognition" + version="1.3.0"> Speech Recognition Cordova Plugin for Speech Recognition MIT cordova,speech,recognition - https://github.com/pbakondy/cordova-plugin-speechrecognition - https://github.com/pbakondy/cordova-plugin-speechrecognition/issues + https://github.com/spoonconsulting/cordova-plugin-speechrecognition + https://github.com/spoonconsulting/cordova-plugin-speechrecognition/issues From e94480ed8b7f219475450350d3fb6f36a1bc4485 Mon Sep 17 00:00:00 2001 From: zafirskthelifehacker Date: Tue, 8 Mar 2022 15:09:22 +0400 Subject: [PATCH 13/22] Add Spoon as Author in README --- README.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/README.md b/README.md index ba3c374..493a5ed 100644 --- a/README.md +++ b/README.md @@ -220,6 +220,9 @@ The plugin works in [AVAudioSessionCategoryPlayAndRecord](https://developer.appl ### Peter Bakondy - https://github.com/pbakondy + +### Spoon Consulting Ltd + - https://github.com/spoonconsulting ## LICENSE From f2b14fdcfa7251083e2db4de922814865e6a6c0c Mon Sep 17 00:00:00 2001 From: zafirskthelifehacker Date: Tue, 8 Mar 2022 17:06:09 +0400 Subject: [PATCH 14/22] 1.2.1 --- CHANGELOG.md | 2 +- package.json | 2 +- plugin.xml | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 216e0f1..0438fa7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,6 @@ # Changelog -## v1.3.0 +## v1.2.1 - iOS: Catch exception when Microphone in use by another application diff --git a/package.json b/package.json index 5f3d8b9..1ed7fdf 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@spoonconsulting/cordova-plugin-speechrecognition", - "version": "1.3.0", + "version": "1.2.1", "description": "Cordova Plugin for Speech Recognition", "cordova": { "id": "@spoonconsulting/cordova-plugin-speechrecognition", diff --git a/plugin.xml b/plugin.xml index 69e8837..aeb6348 100644 --- a/plugin.xml +++ b/plugin.xml @@ -2,7 +2,7 @@ + version="1.2.1"> Speech Recognition Cordova Plugin for Speech Recognition From 360c9c6aba2e22e83aafd8b4784c1a09c9f8ec43 Mon Sep 17 00:00:00 2001 From: zafirskthelifehacker Date: Thu, 17 Mar 2022 11:12:45 +0400 Subject: [PATCH 15/22] runBlockInBackgroundWithTryCatch and Check if audioEngine is running --- src/ios/SpeechRecognition.m | 351 +++++++++++++++++++----------------- 1 file changed, 190 insertions(+), 161 deletions(-) diff --git a/src/ios/SpeechRecognition.m b/src/ios/SpeechRecognition.m index 5ab9a3f..ec035e4 100644 --- a/src/ios/SpeechRecognition.m +++ b/src/ios/SpeechRecognition.m @@ -17,6 +17,8 @@ #define MESSAGE_ACCESS_DENIED_MICROPHONE @"User denied access to microphone" #define MESSAGE_ONGOING @"Ongoing speech recognition" #define MESSAGE_COULD_NOT_START_SPEECHRECOGNITION @"Could not start Speech Recognition. Microphone may be busy." +#define MESSAGE_PLUGIN_ERROR @"Plugin error" + @interface SpeechRecognition() @@ -31,229 +33,256 @@ @interface SpeechRecognition() @implementation SpeechRecognition - (void)isRecognitionAvailable:(CDVInvokedUrlCommand*)command { - CDVPluginResult *pluginResult = nil; + [self runBlockInBackgroundWithTryCatch:^{ + CDVPluginResult *pluginResult = nil; - if ([SFSpeechRecognizer class]) { - pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsBool:YES]; - } else { - pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsBool:NO]; - } + if ([SFSpeechRecognizer class]) { + pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsBool:YES]; + } else { + pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsBool:NO]; + } - [self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId]; + [self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId]; + } forCommand:command]; } - (void)startListening:(CDVInvokedUrlCommand*)command { - if ( self.audioEngine.isRunning ) { - CDVPluginResult *pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsString:MESSAGE_ONGOING]; - [self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId]; - return; - } - - NSLog(@"startListening()"); + [self runBlockInBackgroundWithTryCatch:^{ + if ( self.audioEngine.isRunning ) { + CDVPluginResult *pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsString:MESSAGE_ONGOING]; + [self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId]; + return; + } - SFSpeechRecognizerAuthorizationStatus status = [SFSpeechRecognizer authorizationStatus]; - if (status != SFSpeechRecognizerAuthorizationStatusAuthorized) { - NSLog(@"startListening() speech recognition access not authorized"); - CDVPluginResult *pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsString:MESSAGE_MISSING_PERMISSION]; - [self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId]; - return; - } + NSLog(@"startListening()"); - [[AVAudioSession sharedInstance] requestRecordPermission:^(BOOL granted){ - if (!granted) { - NSLog(@"startListening() microphone access not authorized"); - CDVPluginResult *pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsString:MESSAGE_ACCESS_DENIED_MICROPHONE]; + SFSpeechRecognizerAuthorizationStatus status = [SFSpeechRecognizer authorizationStatus]; + if (status != SFSpeechRecognizerAuthorizationStatusAuthorized) { + NSLog(@"startListening() speech recognition access not authorized"); + CDVPluginResult *pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsString:MESSAGE_MISSING_PERMISSION]; [self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId]; return; } - NSString* language = [command argumentAtIndex:0 withDefault:DEFAULT_LANGUAGE]; - int matches = [[command argumentAtIndex:1 withDefault:@(DEFAULT_MATCHES)] intValue]; - BOOL showPartial = [[command argumentAtIndex:3 withDefault:@(NO)] boolValue]; + [[AVAudioSession sharedInstance] requestRecordPermission:^(BOOL granted){ + if (!granted) { + NSLog(@"startListening() microphone access not authorized"); + CDVPluginResult *pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsString:MESSAGE_ACCESS_DENIED_MICROPHONE]; + [self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId]; + return; + } - NSLocale *locale = [[NSLocale alloc] initWithLocaleIdentifier:language]; - self.speechRecognizer = [[SFSpeechRecognizer alloc] initWithLocale:locale]; - self.audioEngine = [[AVAudioEngine alloc] init]; + NSString* language = [command argumentAtIndex:0 withDefault:DEFAULT_LANGUAGE]; + int matches = [[command argumentAtIndex:1 withDefault:@(DEFAULT_MATCHES)] intValue]; + BOOL showPartial = [[command argumentAtIndex:3 withDefault:@(NO)] boolValue]; - // Cancel the previous task if it's running. - if ( self.recognitionTask ) { - [self.recognitionTask cancel]; - self.recognitionTask = nil; - } + NSLocale *locale = [[NSLocale alloc] initWithLocaleIdentifier:language]; + self.speechRecognizer = [[SFSpeechRecognizer alloc] initWithLocale:locale]; + self.audioEngine = [[AVAudioEngine alloc] init]; + + // Cancel the previous task if it's running. + if ( self.recognitionTask ) { + [self.recognitionTask cancel]; + self.recognitionTask = nil; + } - AVAudioSession *audioSession = [AVAudioSession sharedInstance]; - [audioSession setCategory:AVAudioSessionCategoryPlayAndRecord withOptions:AVAudioSessionCategoryOptionDefaultToSpeaker error:nil]; - [audioSession setMode:AVAudioSessionModeDefault error:nil]; - [audioSession setActive:YES withOptions:AVAudioSessionSetActiveOptionNotifyOthersOnDeactivation error:nil]; + AVAudioSession *audioSession = [AVAudioSession sharedInstance]; + [audioSession setCategory:AVAudioSessionCategoryPlayAndRecord withOptions:AVAudioSessionCategoryOptionDefaultToSpeaker error:nil]; + [audioSession setMode:AVAudioSessionModeDefault error:nil]; + [audioSession setActive:YES withOptions:AVAudioSessionSetActiveOptionNotifyOthersOnDeactivation error:nil]; - self.recognitionRequest = [[SFSpeechAudioBufferRecognitionRequest alloc] init]; - self.recognitionRequest.shouldReportPartialResults = showPartial; + self.recognitionRequest = [[SFSpeechAudioBufferRecognitionRequest alloc] init]; + self.recognitionRequest.shouldReportPartialResults = showPartial; - AVAudioInputNode *inputNode = self.audioEngine.inputNode; - AVAudioFormat *format = [inputNode outputFormatForBus:0]; + AVAudioInputNode *inputNode = self.audioEngine.inputNode; + AVAudioFormat *format = [inputNode outputFormatForBus:0]; - self.recognitionTask = [self.speechRecognizer recognitionTaskWithRequest:self.recognitionRequest resultHandler:^(SFSpeechRecognitionResult *result, NSError *error) { + self.recognitionTask = [self.speechRecognizer recognitionTaskWithRequest:self.recognitionRequest resultHandler:^(SFSpeechRecognitionResult *result, NSError *error) { - if ( result ) { + if ( result ) { - NSMutableArray *resultArray = [[NSMutableArray alloc] init]; + NSMutableArray *resultArray = [[NSMutableArray alloc] init]; - int counter = 0; - for ( SFTranscription *transcription in result.transcriptions ) { - if (matches > 0 && counter < matches) { - [resultArray addObject:transcription.formattedString]; + int counter = 0; + for ( SFTranscription *transcription in result.transcriptions ) { + if (matches > 0 && counter < matches) { + [resultArray addObject:transcription.formattedString]; + } + counter++; } - counter++; + + NSArray *transcriptions = [NSArray arrayWithArray:resultArray]; + + NSLog(@"startListening() recognitionTask result array: %@", transcriptions.description); + + CDVPluginResult *pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsArray:transcriptions]; + if (showPartial){ + [pluginResult setKeepCallbackAsBool:YES]; + } + [self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId]; } - NSArray *transcriptions = [NSArray arrayWithArray:resultArray]; + if ( error ) { + NSLog(@"startListening() recognitionTask error: %@", error.description); - NSLog(@"startListening() recognitionTask result array: %@", transcriptions.description); + [self.audioEngine stop]; + [self.audioEngine.inputNode removeTapOnBus:0]; - CDVPluginResult *pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsArray:transcriptions]; - if (showPartial){ - [pluginResult setKeepCallbackAsBool:YES]; + self.recognitionRequest = nil; + self.recognitionTask = nil; + + CDVPluginResult *pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsString:error.description]; + if (showPartial){ + [pluginResult setKeepCallbackAsBool:YES]; + } + [self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId]; } - [self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId]; - } - if ( error ) { - NSLog(@"startListening() recognitionTask error: %@", error.description); + if ( result.isFinal ) { + NSLog(@"startListening() recognitionTask isFinal"); - [self.audioEngine stop]; - [self.audioEngine.inputNode removeTapOnBus:0]; + [self.audioEngine stop]; + [self.audioEngine.inputNode removeTapOnBus:0]; + + self.recognitionRequest = nil; + self.recognitionTask = nil; + } + }]; + + @try { + [inputNode installTapOnBus:0 bufferSize:1024 format:format block:^(AVAudioPCMBuffer *buffer, AVAudioTime *when) { + [self.recognitionRequest appendAudioPCMBuffer:buffer]; + }]; + } @catch (NSException *exception) { + NSLog(@"startListening() recognitionTask error: %@", exception); + + if ( self.audioEngine.isRunning ) { + [self.audioEngine stop]; + [self.audioEngine.inputNode removeTapOnBus:0]; + } + + if ( self.recognitionRequest ) { + [self.recognitionRequest endAudio]; + } self.recognitionRequest = nil; self.recognitionTask = nil; - - CDVPluginResult *pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsString:error.description]; + + CDVPluginResult *pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsString:MESSAGE_COULD_NOT_START_SPEECHRECOGNITION]; if (showPartial){ [pluginResult setKeepCallbackAsBool:YES]; } [self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId]; } - if ( result.isFinal ) { - NSLog(@"startListening() recognitionTask isFinal"); + [self.audioEngine prepare]; + [self.audioEngine startAndReturnError:nil]; - [self.audioEngine stop]; - [self.audioEngine.inputNode removeTapOnBus:0]; - - self.recognitionRequest = nil; - self.recognitionTask = nil; - } }]; - - @try { - [inputNode installTapOnBus:0 bufferSize:1024 format:format block:^(AVAudioPCMBuffer *buffer, AVAudioTime *when) { - [self.recognitionRequest appendAudioPCMBuffer:buffer]; - }]; - } @catch (NSException *exception) { - NSLog(@"startListening() recognitionTask error: %@", exception); - - [self.audioEngine stop]; - [self.audioEngine.inputNode removeTapOnBus:0]; - [self.recognitionRequest endAudio]; - - self.recognitionRequest = nil; - self.recognitionTask = nil; - - CDVPluginResult *pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsString:MESSAGE_COULD_NOT_START_SPEECHRECOGNITION]; - if (showPartial){ - [pluginResult setKeepCallbackAsBool:YES]; - } - [self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId]; - } - - [self.audioEngine prepare]; - [self.audioEngine startAndReturnError:nil]; - - }]; - + } forCommand:command]; } - (void)stopListening:(CDVInvokedUrlCommand*)command { - [self.commandDelegate runInBackground:^{ - NSLog(@"stopListening()"); + [self runBlockInBackgroundWithTryCatch:^{ + [self.commandDelegate runInBackground:^{ + NSLog(@"stopListening()"); - if ( self.audioEngine.isRunning ) { - [self.audioEngine stop]; - [self.recognitionRequest endAudio]; - } + if ( self.audioEngine.isRunning ) { + [self.audioEngine stop]; + [self.recognitionRequest endAudio]; + } - CDVPluginResult *pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK]; - [self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId]; - }]; + CDVPluginResult *pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK]; + [self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId]; + }]; + } forCommand:command]; } - (void)getSupportedLanguages:(CDVInvokedUrlCommand*)command { - NSSet *supportedLocales = [SFSpeechRecognizer supportedLocales]; + [self runBlockInBackgroundWithTryCatch:^{ + NSSet *supportedLocales = [SFSpeechRecognizer supportedLocales]; - NSMutableArray *localesArray = [[NSMutableArray alloc] init]; + NSMutableArray *localesArray = [[NSMutableArray alloc] init]; - for(NSLocale *locale in supportedLocales) { - [localesArray addObject:[locale localeIdentifier]]; - } + for(NSLocale *locale in supportedLocales) { + [localesArray addObject:[locale localeIdentifier]]; + } - CDVPluginResult *pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsArray:localesArray]; - [self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId]; + CDVPluginResult *pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsArray:localesArray]; + [self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId]; + } forCommand:command]; } - (void)hasPermission:(CDVInvokedUrlCommand*)command { - SFSpeechRecognizerAuthorizationStatus status = [SFSpeechRecognizer authorizationStatus]; - BOOL speechAuthGranted = (status == SFSpeechRecognizerAuthorizationStatusAuthorized); + [self runBlockInBackgroundWithTryCatch:^{ + SFSpeechRecognizerAuthorizationStatus status = [SFSpeechRecognizer authorizationStatus]; + BOOL speechAuthGranted = (status == SFSpeechRecognizerAuthorizationStatusAuthorized); - if (!speechAuthGranted) { - CDVPluginResult *pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsBool:NO]; - [self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId]; - return; - } + if (!speechAuthGranted) { + CDVPluginResult *pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsBool:NO]; + [self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId]; + return; + } - [[AVAudioSession sharedInstance] requestRecordPermission:^(BOOL granted){ - CDVPluginResult *pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsBool:granted]; - [self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId]; - }]; + [[AVAudioSession sharedInstance] requestRecordPermission:^(BOOL granted){ + CDVPluginResult *pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsBool:granted]; + [self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId]; + }]; + } forCommand:command]; } - (void)requestPermission:(CDVInvokedUrlCommand*)command { - [SFSpeechRecognizer requestAuthorization:^(SFSpeechRecognizerAuthorizationStatus status){ - dispatch_async(dispatch_get_main_queue(), ^{ - CDVPluginResult *pluginResult = nil; - BOOL speechAuthGranted = NO; - - switch (status) { - case SFSpeechRecognizerAuthorizationStatusAuthorized: - pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK]; - speechAuthGranted = YES; - break; - case SFSpeechRecognizerAuthorizationStatusDenied: - pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsString:MESSAGE_ACCESS_DENIED]; - break; - case SFSpeechRecognizerAuthorizationStatusRestricted: - pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsString:MESSAGE_RESTRICTED]; - break; - case SFSpeechRecognizerAuthorizationStatusNotDetermined: - pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsString:MESSAGE_NOT_DETERMINED]; - break; - } - - if (!speechAuthGranted) { - [self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId]; - return; - } - - [[AVAudioSession sharedInstance] requestRecordPermission:^(BOOL granted){ + [self runBlockInBackgroundWithTryCatch:^{ + [SFSpeechRecognizer requestAuthorization:^(SFSpeechRecognizerAuthorizationStatus status){ + dispatch_async(dispatch_get_main_queue(), ^{ CDVPluginResult *pluginResult = nil; + BOOL speechAuthGranted = NO; + + switch (status) { + case SFSpeechRecognizerAuthorizationStatusAuthorized: + pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK]; + speechAuthGranted = YES; + break; + case SFSpeechRecognizerAuthorizationStatusDenied: + pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsString:MESSAGE_ACCESS_DENIED]; + break; + case SFSpeechRecognizerAuthorizationStatusRestricted: + pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsString:MESSAGE_RESTRICTED]; + break; + case SFSpeechRecognizerAuthorizationStatusNotDetermined: + pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsString:MESSAGE_NOT_DETERMINED]; + break; + } - if (granted) { - pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK]; - } else { - pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsString:MESSAGE_ACCESS_DENIED_MICROPHONE]; + if (!speechAuthGranted) { + [self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId]; + return; } - [self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId]; - }]; - }); + [[AVAudioSession sharedInstance] requestRecordPermission:^(BOOL granted){ + CDVPluginResult *pluginResult = nil; + + if (granted) { + pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK]; + } else { + pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsString:MESSAGE_ACCESS_DENIED_MICROPHONE]; + } + + [self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId]; + }]; + }); + }]; + } forCommand:command]; +} + +-(void)runBlockInBackgroundWithTryCatch:(void (^)(void))block forCommand:(CDVInvokedUrlCommand*)command{ + [self.commandDelegate runInBackground:^{ + @try { + block(); + } @catch (NSException *exception) { + CDVPluginResult *pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsString:MESSAGE_PLUGIN_ERROR]; + [self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId]; + } }]; } From e9d1b183c1551109a9747295e5362d8e6e9d132c Mon Sep 17 00:00:00 2001 From: zafirskthelifehacker Date: Thu, 17 Mar 2022 15:54:36 +0400 Subject: [PATCH 16/22] Remove Spoon Consulting and V1.2.1 --- CHANGELOG.md | 4 ---- README.md | 6 +----- package.json | 12 ++++++------ plugin.xml | 8 ++++---- 4 files changed, 11 insertions(+), 19 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 0438fa7..2694848 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,9 +1,5 @@ # Changelog -## v1.2.1 - -- iOS: Catch exception when Microphone in use by another application - ## v1.2.0 - Android: add `stopListening` [by Simone Compagnone] diff --git a/README.md b/README.md index 493a5ed..9e3760f 100644 --- a/README.md +++ b/README.md @@ -10,7 +10,7 @@ This is a cordova plugin for Speech Recognition. ## Installation ``` -cordova plugin add @spoonconsulting/cordova-plugin-speechrecognition +cordova plugin add cordova-plugin-speechrecognition ``` ## Supported Platforms @@ -221,10 +221,6 @@ The plugin works in [AVAudioSessionCategoryPlayAndRecord](https://developer.appl - https://github.com/pbakondy -### Spoon Consulting Ltd - -- https://github.com/spoonconsulting - ## LICENSE **cordova-plugin-speechrecognition** is licensed under the MIT Open Source license. For more information, see the LICENSE file in this repository. diff --git a/package.json b/package.json index 1ed7fdf..bf49bf3 100644 --- a/package.json +++ b/package.json @@ -1,9 +1,9 @@ { - "name": "@spoonconsulting/cordova-plugin-speechrecognition", - "version": "1.2.1", + "name": "cordova-plugin-speechrecognition", + "version": "1.2.0", "description": "Cordova Plugin for Speech Recognition", "cordova": { - "id": "@spoonconsulting/cordova-plugin-speechrecognition", + "id": "cordova-plugin-speechrecognition", "platforms": [ "android", "ios" @@ -11,7 +11,7 @@ }, "repository": { "type": "git", - "url": "https://github.com/spoonconsulting/cordova-plugin-speechrecognition.git" + "url": "https://github.com/pbakondy/cordova-plugin-speechrecognition.git" }, "keywords": [ "cordova", @@ -30,7 +30,7 @@ "author": "Peter Bakondy & Spoon Consulting Ltd", "license": "MIT", "bugs": { - "url": "https://github.com/spoonconsulting/cordova-plugin-speechrecognition/issues" + "url": "https://github.com/pbakondy/cordova-plugin-speechrecognition/issues" }, - "homepage": "https://github.com/spoonconsulting/cordova-plugin-speechrecognition#readme" + "homepage": "https://github.com/pbakondy/cordova-plugin-speechrecognition#readme" } diff --git a/plugin.xml b/plugin.xml index aeb6348..3c65c86 100644 --- a/plugin.xml +++ b/plugin.xml @@ -1,15 +1,15 @@ + id="cordova-plugin-speechrecognition" + version="1.2.0"> Speech Recognition Cordova Plugin for Speech Recognition MIT cordova,speech,recognition - https://github.com/spoonconsulting/cordova-plugin-speechrecognition - https://github.com/spoonconsulting/cordova-plugin-speechrecognition/issues + https://github.com/pbakondy/cordova-plugin-speechrecognition + https://github.com/pbakondy/cordova-plugin-speechrecognition/issues From bbbcda5c6893d4dddca683e341a7feeceac56955 Mon Sep 17 00:00:00 2001 From: zafirskthelifehacker Date: Thu, 17 Mar 2022 15:56:34 +0400 Subject: [PATCH 17/22] Remove SC --- README.md | 1 + package.json | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 9e3760f..8d0ccab 100644 --- a/README.md +++ b/README.md @@ -221,6 +221,7 @@ The plugin works in [AVAudioSessionCategoryPlayAndRecord](https://developer.appl - https://github.com/pbakondy + ## LICENSE **cordova-plugin-speechrecognition** is licensed under the MIT Open Source license. For more information, see the LICENSE file in this repository. diff --git a/package.json b/package.json index bf49bf3..96f064b 100644 --- a/package.json +++ b/package.json @@ -27,7 +27,7 @@ "version": ">=3.0.0" } ], - "author": "Peter Bakondy & Spoon Consulting Ltd", + "author": "Peter Bakondy", "license": "MIT", "bugs": { "url": "https://github.com/pbakondy/cordova-plugin-speechrecognition/issues" From 4c94e9b97f8721a0463373569e778e5fe98780cc Mon Sep 17 00:00:00 2001 From: Sk Heerah Zafir Date: Mon, 21 Mar 2022 17:33:05 +0400 Subject: [PATCH 18/22] runBlockWithTryCatch() --- .../com/pbakondy/SpeechRecognition.java | 148 ++++++++++-------- src/ios/SpeechRecognition.m | 28 ++-- 2 files changed, 98 insertions(+), 78 deletions(-) diff --git a/src/android/com/pbakondy/SpeechRecognition.java b/src/android/com/pbakondy/SpeechRecognition.java index a2e8071..f8307fc 100644 --- a/src/android/com/pbakondy/SpeechRecognition.java +++ b/src/android/com/pbakondy/SpeechRecognition.java @@ -60,19 +60,21 @@ public class SpeechRecognition extends CordovaPlugin { @Override public void initialize(CordovaInterface cordova, CordovaWebView webView) { - super.initialize(cordova, webView); - - activity = cordova.getActivity(); - context = webView.getContext(); - view = webView.getView(); - - view.post(new Runnable() { - @Override - public void run() { - recognizer = SpeechRecognizer.createSpeechRecognizer(activity); - SpeechRecognitionListener listener = new SpeechRecognitionListener(); - recognizer.setRecognitionListener(listener); - } + runBlockWithTryCatch(() -> { + super.initialize(cordova, webView); + + activity = cordova.getActivity(); + context = webView.getContext(); + view = webView.getView(); + + view.post(new Runnable() { + @Override + public void run() { + recognizer = SpeechRecognizer.createSpeechRecognizer(activity); + SpeechRecognitionListener listener = new SpeechRecognitionListener(); + recognizer.setRecognitionListener(listener); + } + }); }); } @@ -162,57 +164,65 @@ private boolean isRecognitionAvailable() { } private void startListening(String language, int matches, String prompt, final Boolean showPartial, Boolean showPopup) { - Log.d(LOG_TAG, "startListening() language: " + language + ", matches: " + matches + ", prompt: " + prompt + ", showPartial: " + showPartial + ", showPopup: " + showPopup); - - final Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH); - intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, - RecognizerIntent.LANGUAGE_MODEL_FREE_FORM); - intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE, language); - intent.putExtra(RecognizerIntent.EXTRA_MAX_RESULTS, matches); - intent.putExtra(RecognizerIntent.EXTRA_CALLING_PACKAGE, - activity.getPackageName()); - intent.putExtra(RecognizerIntent.EXTRA_PARTIAL_RESULTS, showPartial); - intent.putExtra("android.speech.extra.DICTATION_MODE", showPartial); - - if (prompt != null) { - intent.putExtra(RecognizerIntent.EXTRA_PROMPT, prompt); - } + runBlockWithTryCatch(() -> { + Log.d(LOG_TAG, "startListening() language: " + language + ", matches: " + matches + ", prompt: " + prompt + ", showPartial: " + showPartial + ", showPopup: " + showPopup); + + final Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH); + intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, + RecognizerIntent.LANGUAGE_MODEL_FREE_FORM); + intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE, language); + intent.putExtra(RecognizerIntent.EXTRA_MAX_RESULTS, matches); + intent.putExtra(RecognizerIntent.EXTRA_CALLING_PACKAGE, + activity.getPackageName()); + intent.putExtra(RecognizerIntent.EXTRA_PARTIAL_RESULTS, showPartial); + intent.putExtra("android.speech.extra.DICTATION_MODE", showPartial); + + if (prompt != null) { + intent.putExtra(RecognizerIntent.EXTRA_PROMPT, prompt); + } - if (showPopup) { - cordova.startActivityForResult(this, intent, REQUEST_CODE_SPEECH); - } else { - view.post(new Runnable() { - @Override - public void run() { - recognizer.startListening(intent); - } - }); - } + if (showPopup) { + cordova.startActivityForResult(this, intent, REQUEST_CODE_SPEECH); + } else { + view.post(new Runnable() { + @Override + public void run() { + recognizer.startListening(intent); + } + }); + } + }); } private void getSupportedLanguages() { - if (languageDetailsChecker == null) { - languageDetailsChecker = new LanguageDetailsChecker(callbackContext); - } + runBlockWithTryCatch(() -> { + if (languageDetailsChecker == null) { + languageDetailsChecker = new LanguageDetailsChecker(callbackContext); + } - List supportedLanguages = languageDetailsChecker.getSupportedLanguages(); - if (supportedLanguages != null) { - JSONArray languages = new JSONArray(supportedLanguages); - callbackContext.success(languages); - return; - } + List supportedLanguages = languageDetailsChecker.getSupportedLanguages(); + if (supportedLanguages != null) { + JSONArray languages = new JSONArray(supportedLanguages); + callbackContext.success(languages); + return; + } - Intent detailsIntent = new Intent(RecognizerIntent.ACTION_GET_LANGUAGE_DETAILS); - activity.sendOrderedBroadcast(detailsIntent, null, languageDetailsChecker, null, Activity.RESULT_OK, null, null); + Intent detailsIntent = new Intent(RecognizerIntent.ACTION_GET_LANGUAGE_DETAILS); + activity.sendOrderedBroadcast(detailsIntent, null, languageDetailsChecker, null, Activity.RESULT_OK, null, null); + }); } private void hasAudioPermission() { - PluginResult result = new PluginResult(PluginResult.Status.OK, audioPermissionGranted(RECORD_AUDIO_PERMISSION)); - this.callbackContext.sendPluginResult(result); + runBlockWithTryCatch(() -> { + PluginResult result = new PluginResult(PluginResult.Status.OK, audioPermissionGranted(RECORD_AUDIO_PERMISSION)); + this.callbackContext.sendPluginResult(result); + }); } private void requestAudioPermission() { - requestPermission(RECORD_AUDIO_PERMISSION); + runBlockWithTryCatch(() -> { + requestPermission(RECORD_AUDIO_PERMISSION); + }); } private boolean audioPermissionGranted(String type) { @@ -223,20 +233,24 @@ private boolean audioPermissionGranted(String type) { } private void requestPermission(String type) { - if (!audioPermissionGranted(type)) { - cordova.requestPermission(this, 23456, type); - } else { - this.callbackContext.success(); - } + runBlockWithTryCatch(() -> { + if (!audioPermissionGranted(type)) { + cordova.requestPermission(this, 23456, type); + } else { + this.callbackContext.success(); + } + }); } @Override public void onRequestPermissionResult(int requestCode, String[] permissions, int[] grantResults) throws JSONException { - if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) { - this.callbackContext.success(); - } else { - this.callbackContext.error("Permission denied"); - } + runBlockWithTryCatch(() -> { + if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) { + this.callbackContext.success(); + } else { + this.callbackContext.error("Permission denied"); + } + }); } @Override @@ -296,7 +310,7 @@ public void onPartialResults(Bundle bundle) { try { if (matches != null && matches.size() > 0 - && !mLastPartialResults.equals(matchesJSON)) { + && !mLastPartialResults.equals(matchesJSON)) { mLastPartialResults = matchesJSON; PluginResult pluginResult = new PluginResult(PluginResult.Status.OK, matchesJSON); pluginResult.setKeepCallback(true); @@ -368,4 +382,12 @@ private String getErrorText(int errorCode) { } } + void runBlockWithTryCatch(Runnable runnable) { + try { + runnable.run(); + } catch (Exception e) { + e.printStackTrace(); + callbackContext.error(e.getMessage()); + } + } } diff --git a/src/ios/SpeechRecognition.m b/src/ios/SpeechRecognition.m index ec035e4..6013b35 100644 --- a/src/ios/SpeechRecognition.m +++ b/src/ios/SpeechRecognition.m @@ -33,7 +33,7 @@ @interface SpeechRecognition() @implementation SpeechRecognition - (void)isRecognitionAvailable:(CDVInvokedUrlCommand*)command { - [self runBlockInBackgroundWithTryCatch:^{ + [self runBlockWithTryCatch:^{ CDVPluginResult *pluginResult = nil; if ([SFSpeechRecognizer class]) { @@ -47,7 +47,7 @@ - (void)isRecognitionAvailable:(CDVInvokedUrlCommand*)command { } - (void)startListening:(CDVInvokedUrlCommand*)command { - [self runBlockInBackgroundWithTryCatch:^{ + [self runBlockWithTryCatch:^{ if ( self.audioEngine.isRunning ) { CDVPluginResult *pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsString:MESSAGE_ONGOING]; [self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId]; @@ -183,7 +183,7 @@ - (void)startListening:(CDVInvokedUrlCommand*)command { } - (void)stopListening:(CDVInvokedUrlCommand*)command { - [self runBlockInBackgroundWithTryCatch:^{ + [self runBlockWithTryCatch:^{ [self.commandDelegate runInBackground:^{ NSLog(@"stopListening()"); @@ -199,7 +199,7 @@ - (void)stopListening:(CDVInvokedUrlCommand*)command { } - (void)getSupportedLanguages:(CDVInvokedUrlCommand*)command { - [self runBlockInBackgroundWithTryCatch:^{ + [self runBlockWithTryCatch:^{ NSSet *supportedLocales = [SFSpeechRecognizer supportedLocales]; NSMutableArray *localesArray = [[NSMutableArray alloc] init]; @@ -214,7 +214,7 @@ - (void)getSupportedLanguages:(CDVInvokedUrlCommand*)command { } - (void)hasPermission:(CDVInvokedUrlCommand*)command { - [self runBlockInBackgroundWithTryCatch:^{ + [self runBlockWithTryCatch:^{ SFSpeechRecognizerAuthorizationStatus status = [SFSpeechRecognizer authorizationStatus]; BOOL speechAuthGranted = (status == SFSpeechRecognizerAuthorizationStatusAuthorized); @@ -232,7 +232,7 @@ - (void)hasPermission:(CDVInvokedUrlCommand*)command { } - (void)requestPermission:(CDVInvokedUrlCommand*)command { - [self runBlockInBackgroundWithTryCatch:^{ + [self runBlockWithTryCatch:^{ [SFSpeechRecognizer requestAuthorization:^(SFSpeechRecognizerAuthorizationStatus status){ dispatch_async(dispatch_get_main_queue(), ^{ CDVPluginResult *pluginResult = nil; @@ -275,15 +275,13 @@ - (void)requestPermission:(CDVInvokedUrlCommand*)command { } forCommand:command]; } --(void)runBlockInBackgroundWithTryCatch:(void (^)(void))block forCommand:(CDVInvokedUrlCommand*)command{ - [self.commandDelegate runInBackground:^{ - @try { - block(); - } @catch (NSException *exception) { - CDVPluginResult *pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsString:MESSAGE_PLUGIN_ERROR]; - [self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId]; - } - }]; +-(void)runBlockWithTryCatch:(void (^)(void))block forCommand:(CDVInvokedUrlCommand*)command{ + @try { + block(); + } @catch (NSException *exception) { + CDVPluginResult *pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsString:MESSAGE_PLUGIN_ERROR]; + [self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId]; + } } @end From dd41f76b5f8200f57b346ea60e1faee17275c815 Mon Sep 17 00:00:00 2001 From: Sk Heerah Zafir Date: Mon, 21 Mar 2022 17:35:46 +0400 Subject: [PATCH 19/22] runBlockWithTryCatch() --- .../com/pbakondy/SpeechRecognition.java | 148 ++++++++++-------- src/ios/SpeechRecognition.m | 28 ++-- 2 files changed, 98 insertions(+), 78 deletions(-) diff --git a/src/android/com/pbakondy/SpeechRecognition.java b/src/android/com/pbakondy/SpeechRecognition.java index a2e8071..f8307fc 100644 --- a/src/android/com/pbakondy/SpeechRecognition.java +++ b/src/android/com/pbakondy/SpeechRecognition.java @@ -60,19 +60,21 @@ public class SpeechRecognition extends CordovaPlugin { @Override public void initialize(CordovaInterface cordova, CordovaWebView webView) { - super.initialize(cordova, webView); - - activity = cordova.getActivity(); - context = webView.getContext(); - view = webView.getView(); - - view.post(new Runnable() { - @Override - public void run() { - recognizer = SpeechRecognizer.createSpeechRecognizer(activity); - SpeechRecognitionListener listener = new SpeechRecognitionListener(); - recognizer.setRecognitionListener(listener); - } + runBlockWithTryCatch(() -> { + super.initialize(cordova, webView); + + activity = cordova.getActivity(); + context = webView.getContext(); + view = webView.getView(); + + view.post(new Runnable() { + @Override + public void run() { + recognizer = SpeechRecognizer.createSpeechRecognizer(activity); + SpeechRecognitionListener listener = new SpeechRecognitionListener(); + recognizer.setRecognitionListener(listener); + } + }); }); } @@ -162,57 +164,65 @@ private boolean isRecognitionAvailable() { } private void startListening(String language, int matches, String prompt, final Boolean showPartial, Boolean showPopup) { - Log.d(LOG_TAG, "startListening() language: " + language + ", matches: " + matches + ", prompt: " + prompt + ", showPartial: " + showPartial + ", showPopup: " + showPopup); - - final Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH); - intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, - RecognizerIntent.LANGUAGE_MODEL_FREE_FORM); - intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE, language); - intent.putExtra(RecognizerIntent.EXTRA_MAX_RESULTS, matches); - intent.putExtra(RecognizerIntent.EXTRA_CALLING_PACKAGE, - activity.getPackageName()); - intent.putExtra(RecognizerIntent.EXTRA_PARTIAL_RESULTS, showPartial); - intent.putExtra("android.speech.extra.DICTATION_MODE", showPartial); - - if (prompt != null) { - intent.putExtra(RecognizerIntent.EXTRA_PROMPT, prompt); - } + runBlockWithTryCatch(() -> { + Log.d(LOG_TAG, "startListening() language: " + language + ", matches: " + matches + ", prompt: " + prompt + ", showPartial: " + showPartial + ", showPopup: " + showPopup); + + final Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH); + intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, + RecognizerIntent.LANGUAGE_MODEL_FREE_FORM); + intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE, language); + intent.putExtra(RecognizerIntent.EXTRA_MAX_RESULTS, matches); + intent.putExtra(RecognizerIntent.EXTRA_CALLING_PACKAGE, + activity.getPackageName()); + intent.putExtra(RecognizerIntent.EXTRA_PARTIAL_RESULTS, showPartial); + intent.putExtra("android.speech.extra.DICTATION_MODE", showPartial); + + if (prompt != null) { + intent.putExtra(RecognizerIntent.EXTRA_PROMPT, prompt); + } - if (showPopup) { - cordova.startActivityForResult(this, intent, REQUEST_CODE_SPEECH); - } else { - view.post(new Runnable() { - @Override - public void run() { - recognizer.startListening(intent); - } - }); - } + if (showPopup) { + cordova.startActivityForResult(this, intent, REQUEST_CODE_SPEECH); + } else { + view.post(new Runnable() { + @Override + public void run() { + recognizer.startListening(intent); + } + }); + } + }); } private void getSupportedLanguages() { - if (languageDetailsChecker == null) { - languageDetailsChecker = new LanguageDetailsChecker(callbackContext); - } + runBlockWithTryCatch(() -> { + if (languageDetailsChecker == null) { + languageDetailsChecker = new LanguageDetailsChecker(callbackContext); + } - List supportedLanguages = languageDetailsChecker.getSupportedLanguages(); - if (supportedLanguages != null) { - JSONArray languages = new JSONArray(supportedLanguages); - callbackContext.success(languages); - return; - } + List supportedLanguages = languageDetailsChecker.getSupportedLanguages(); + if (supportedLanguages != null) { + JSONArray languages = new JSONArray(supportedLanguages); + callbackContext.success(languages); + return; + } - Intent detailsIntent = new Intent(RecognizerIntent.ACTION_GET_LANGUAGE_DETAILS); - activity.sendOrderedBroadcast(detailsIntent, null, languageDetailsChecker, null, Activity.RESULT_OK, null, null); + Intent detailsIntent = new Intent(RecognizerIntent.ACTION_GET_LANGUAGE_DETAILS); + activity.sendOrderedBroadcast(detailsIntent, null, languageDetailsChecker, null, Activity.RESULT_OK, null, null); + }); } private void hasAudioPermission() { - PluginResult result = new PluginResult(PluginResult.Status.OK, audioPermissionGranted(RECORD_AUDIO_PERMISSION)); - this.callbackContext.sendPluginResult(result); + runBlockWithTryCatch(() -> { + PluginResult result = new PluginResult(PluginResult.Status.OK, audioPermissionGranted(RECORD_AUDIO_PERMISSION)); + this.callbackContext.sendPluginResult(result); + }); } private void requestAudioPermission() { - requestPermission(RECORD_AUDIO_PERMISSION); + runBlockWithTryCatch(() -> { + requestPermission(RECORD_AUDIO_PERMISSION); + }); } private boolean audioPermissionGranted(String type) { @@ -223,20 +233,24 @@ private boolean audioPermissionGranted(String type) { } private void requestPermission(String type) { - if (!audioPermissionGranted(type)) { - cordova.requestPermission(this, 23456, type); - } else { - this.callbackContext.success(); - } + runBlockWithTryCatch(() -> { + if (!audioPermissionGranted(type)) { + cordova.requestPermission(this, 23456, type); + } else { + this.callbackContext.success(); + } + }); } @Override public void onRequestPermissionResult(int requestCode, String[] permissions, int[] grantResults) throws JSONException { - if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) { - this.callbackContext.success(); - } else { - this.callbackContext.error("Permission denied"); - } + runBlockWithTryCatch(() -> { + if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) { + this.callbackContext.success(); + } else { + this.callbackContext.error("Permission denied"); + } + }); } @Override @@ -296,7 +310,7 @@ public void onPartialResults(Bundle bundle) { try { if (matches != null && matches.size() > 0 - && !mLastPartialResults.equals(matchesJSON)) { + && !mLastPartialResults.equals(matchesJSON)) { mLastPartialResults = matchesJSON; PluginResult pluginResult = new PluginResult(PluginResult.Status.OK, matchesJSON); pluginResult.setKeepCallback(true); @@ -368,4 +382,12 @@ private String getErrorText(int errorCode) { } } + void runBlockWithTryCatch(Runnable runnable) { + try { + runnable.run(); + } catch (Exception e) { + e.printStackTrace(); + callbackContext.error(e.getMessage()); + } + } } diff --git a/src/ios/SpeechRecognition.m b/src/ios/SpeechRecognition.m index ec035e4..6013b35 100644 --- a/src/ios/SpeechRecognition.m +++ b/src/ios/SpeechRecognition.m @@ -33,7 +33,7 @@ @interface SpeechRecognition() @implementation SpeechRecognition - (void)isRecognitionAvailable:(CDVInvokedUrlCommand*)command { - [self runBlockInBackgroundWithTryCatch:^{ + [self runBlockWithTryCatch:^{ CDVPluginResult *pluginResult = nil; if ([SFSpeechRecognizer class]) { @@ -47,7 +47,7 @@ - (void)isRecognitionAvailable:(CDVInvokedUrlCommand*)command { } - (void)startListening:(CDVInvokedUrlCommand*)command { - [self runBlockInBackgroundWithTryCatch:^{ + [self runBlockWithTryCatch:^{ if ( self.audioEngine.isRunning ) { CDVPluginResult *pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsString:MESSAGE_ONGOING]; [self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId]; @@ -183,7 +183,7 @@ - (void)startListening:(CDVInvokedUrlCommand*)command { } - (void)stopListening:(CDVInvokedUrlCommand*)command { - [self runBlockInBackgroundWithTryCatch:^{ + [self runBlockWithTryCatch:^{ [self.commandDelegate runInBackground:^{ NSLog(@"stopListening()"); @@ -199,7 +199,7 @@ - (void)stopListening:(CDVInvokedUrlCommand*)command { } - (void)getSupportedLanguages:(CDVInvokedUrlCommand*)command { - [self runBlockInBackgroundWithTryCatch:^{ + [self runBlockWithTryCatch:^{ NSSet *supportedLocales = [SFSpeechRecognizer supportedLocales]; NSMutableArray *localesArray = [[NSMutableArray alloc] init]; @@ -214,7 +214,7 @@ - (void)getSupportedLanguages:(CDVInvokedUrlCommand*)command { } - (void)hasPermission:(CDVInvokedUrlCommand*)command { - [self runBlockInBackgroundWithTryCatch:^{ + [self runBlockWithTryCatch:^{ SFSpeechRecognizerAuthorizationStatus status = [SFSpeechRecognizer authorizationStatus]; BOOL speechAuthGranted = (status == SFSpeechRecognizerAuthorizationStatusAuthorized); @@ -232,7 +232,7 @@ - (void)hasPermission:(CDVInvokedUrlCommand*)command { } - (void)requestPermission:(CDVInvokedUrlCommand*)command { - [self runBlockInBackgroundWithTryCatch:^{ + [self runBlockWithTryCatch:^{ [SFSpeechRecognizer requestAuthorization:^(SFSpeechRecognizerAuthorizationStatus status){ dispatch_async(dispatch_get_main_queue(), ^{ CDVPluginResult *pluginResult = nil; @@ -275,15 +275,13 @@ - (void)requestPermission:(CDVInvokedUrlCommand*)command { } forCommand:command]; } --(void)runBlockInBackgroundWithTryCatch:(void (^)(void))block forCommand:(CDVInvokedUrlCommand*)command{ - [self.commandDelegate runInBackground:^{ - @try { - block(); - } @catch (NSException *exception) { - CDVPluginResult *pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsString:MESSAGE_PLUGIN_ERROR]; - [self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId]; - } - }]; +-(void)runBlockWithTryCatch:(void (^)(void))block forCommand:(CDVInvokedUrlCommand*)command{ + @try { + block(); + } @catch (NSException *exception) { + CDVPluginResult *pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsString:MESSAGE_PLUGIN_ERROR]; + [self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId]; + } } @end From 8e8c1aa155c20f062dbc3f0389ad7e2ddbd01f3c Mon Sep 17 00:00:00 2001 From: Sk Heerah Zafir Date: Tue, 5 Apr 2022 12:24:26 +0400 Subject: [PATCH 20/22] Add runBlockWithTryCatch() in execute and @override methods --- .../com/pbakondy/SpeechRecognition.java | 170 ++++++++---------- 1 file changed, 79 insertions(+), 91 deletions(-) diff --git a/src/android/com/pbakondy/SpeechRecognition.java b/src/android/com/pbakondy/SpeechRecognition.java index f8307fc..6cad6d9 100644 --- a/src/android/com/pbakondy/SpeechRecognition.java +++ b/src/android/com/pbakondy/SpeechRecognition.java @@ -70,9 +70,11 @@ public void initialize(CordovaInterface cordova, CordovaWebView webView) { view.post(new Runnable() { @Override public void run() { - recognizer = SpeechRecognizer.createSpeechRecognizer(activity); - SpeechRecognitionListener listener = new SpeechRecognitionListener(); - recognizer.setRecognitionListener(listener); + runBlockWithTryCatch(() -> { + recognizer = SpeechRecognizer.createSpeechRecognizer(activity); + SpeechRecognitionListener listener = new SpeechRecognitionListener(); + recognizer.setRecognitionListener(listener); + }); } }); }); @@ -80,26 +82,26 @@ public void run() { @Override public boolean execute(String action, JSONArray args, CallbackContext callbackContext) throws JSONException { - this.callbackContext = callbackContext; + runBlockWithTryCatch(() -> { + this.callbackContext = callbackContext; - Log.d(LOG_TAG, "execute() action " + action); + Log.d(LOG_TAG, "execute() action " + action); - try { if (IS_RECOGNITION_AVAILABLE.equals(action)) { boolean available = isRecognitionAvailable(); PluginResult result = new PluginResult(PluginResult.Status.OK, available); callbackContext.sendPluginResult(result); - return true; + return; } if (START_LISTENING.equals(action)) { if (!isRecognitionAvailable()) { callbackContext.error(NOT_AVAILABLE); - return true; + return; } if (!audioPermissionGranted(RECORD_AUDIO_PERMISSION)) { callbackContext.error(MISSING_PERMISSION); - return true; + return; } String lang = args.optString(0); @@ -117,9 +119,9 @@ public boolean execute(String action, JSONArray args, CallbackContext callbackCo mLastPartialResults = new JSONArray(); Boolean showPartial = args.optBoolean(3, false); Boolean showPopup = args.optBoolean(4, true); - startListening(lang, matches, prompt,showPartial, showPopup); + startListening(lang, matches, prompt, showPartial, showPopup); - return true; + return; } if (STOP_LISTENING.equals(action)) { @@ -127,36 +129,34 @@ public boolean execute(String action, JSONArray args, CallbackContext callbackCo view.post(new Runnable() { @Override public void run() { - if(recognizer != null) { - recognizer.stopListening(); - } - callbackContextStop.success(); + runBlockWithTryCatch(() -> { + if (recognizer != null) { + recognizer.stopListening(); + } + callbackContextStop.success(); + }); } }); - return true; + return; } if (GET_SUPPORTED_LANGUAGES.equals(action)) { getSupportedLanguages(); - return true; + return; } if (HAS_PERMISSION.equals(action)) { hasAudioPermission(); - return true; + return; } if (REQUEST_PERMISSION.equals(action)) { requestAudioPermission(); - return true; + return; } + }); - } catch (Exception e) { - e.printStackTrace(); - callbackContext.error(e.getMessage()); - } - - return false; + return true; } private boolean isRecognitionAvailable() { @@ -164,65 +164,59 @@ private boolean isRecognitionAvailable() { } private void startListening(String language, int matches, String prompt, final Boolean showPartial, Boolean showPopup) { - runBlockWithTryCatch(() -> { - Log.d(LOG_TAG, "startListening() language: " + language + ", matches: " + matches + ", prompt: " + prompt + ", showPartial: " + showPartial + ", showPopup: " + showPopup); - - final Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH); - intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, - RecognizerIntent.LANGUAGE_MODEL_FREE_FORM); - intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE, language); - intent.putExtra(RecognizerIntent.EXTRA_MAX_RESULTS, matches); - intent.putExtra(RecognizerIntent.EXTRA_CALLING_PACKAGE, - activity.getPackageName()); - intent.putExtra(RecognizerIntent.EXTRA_PARTIAL_RESULTS, showPartial); - intent.putExtra("android.speech.extra.DICTATION_MODE", showPartial); - - if (prompt != null) { - intent.putExtra(RecognizerIntent.EXTRA_PROMPT, prompt); - } + Log.d(LOG_TAG, "startListening() language: " + language + ", matches: " + matches + ", prompt: " + prompt + ", showPartial: " + showPartial + ", showPopup: " + showPopup); + + final Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH); + intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, + RecognizerIntent.LANGUAGE_MODEL_FREE_FORM); + intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE, language); + intent.putExtra(RecognizerIntent.EXTRA_MAX_RESULTS, matches); + intent.putExtra(RecognizerIntent.EXTRA_CALLING_PACKAGE, + activity.getPackageName()); + intent.putExtra(RecognizerIntent.EXTRA_PARTIAL_RESULTS, showPartial); + intent.putExtra("android.speech.extra.DICTATION_MODE", showPartial); + + if (prompt != null) { + intent.putExtra(RecognizerIntent.EXTRA_PROMPT, prompt); + } - if (showPopup) { - cordova.startActivityForResult(this, intent, REQUEST_CODE_SPEECH); - } else { - view.post(new Runnable() { - @Override - public void run() { + if (showPopup) { + cordova.startActivityForResult(this, intent, REQUEST_CODE_SPEECH); + } else { + view.post(new Runnable() { + @Override + public void run() { + runBlockWithTryCatch(() -> { recognizer.startListening(intent); - } - }); - } - }); + }); + } + }); + } } private void getSupportedLanguages() { - runBlockWithTryCatch(() -> { - if (languageDetailsChecker == null) { - languageDetailsChecker = new LanguageDetailsChecker(callbackContext); - } + if (languageDetailsChecker == null) { + languageDetailsChecker = new LanguageDetailsChecker(callbackContext); + } - List supportedLanguages = languageDetailsChecker.getSupportedLanguages(); - if (supportedLanguages != null) { - JSONArray languages = new JSONArray(supportedLanguages); - callbackContext.success(languages); - return; - } + List supportedLanguages = languageDetailsChecker.getSupportedLanguages(); + if (supportedLanguages != null) { + JSONArray languages = new JSONArray(supportedLanguages); + callbackContext.success(languages); + return; + } - Intent detailsIntent = new Intent(RecognizerIntent.ACTION_GET_LANGUAGE_DETAILS); - activity.sendOrderedBroadcast(detailsIntent, null, languageDetailsChecker, null, Activity.RESULT_OK, null, null); - }); + Intent detailsIntent = new Intent(RecognizerIntent.ACTION_GET_LANGUAGE_DETAILS); + activity.sendOrderedBroadcast(detailsIntent, null, languageDetailsChecker, null, Activity.RESULT_OK, null, null); } private void hasAudioPermission() { - runBlockWithTryCatch(() -> { - PluginResult result = new PluginResult(PluginResult.Status.OK, audioPermissionGranted(RECORD_AUDIO_PERMISSION)); - this.callbackContext.sendPluginResult(result); - }); + PluginResult result = new PluginResult(PluginResult.Status.OK, audioPermissionGranted(RECORD_AUDIO_PERMISSION)); + this.callbackContext.sendPluginResult(result); } private void requestAudioPermission() { - runBlockWithTryCatch(() -> { - requestPermission(RECORD_AUDIO_PERMISSION); - }); + requestPermission(RECORD_AUDIO_PERMISSION); } private boolean audioPermissionGranted(String type) { @@ -233,13 +227,11 @@ private boolean audioPermissionGranted(String type) { } private void requestPermission(String type) { - runBlockWithTryCatch(() -> { - if (!audioPermissionGranted(type)) { - cordova.requestPermission(this, 23456, type); - } else { - this.callbackContext.success(); - } - }); + if (!audioPermissionGranted(type)) { + cordova.requestPermission(this, 23456, type); + } else { + this.callbackContext.success(); + } } @Override @@ -255,25 +247,21 @@ public void onRequestPermissionResult(int requestCode, String[] permissions, int @Override public void onActivityResult(int requestCode, int resultCode, Intent data) { - Log.d(LOG_TAG, "onActivityResult() requestCode: " + requestCode + ", resultCode: " + resultCode); + runBlockWithTryCatch(() -> { + Log.d(LOG_TAG, "onActivityResult() requestCode: " + requestCode + ", resultCode: " + resultCode); - if (requestCode == REQUEST_CODE_SPEECH) { - if (resultCode == Activity.RESULT_OK) { - try { + if (requestCode == REQUEST_CODE_SPEECH) { + if (resultCode == Activity.RESULT_OK) { ArrayList matches = data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS); JSONArray jsonMatches = new JSONArray(matches); this.callbackContext.success(jsonMatches); - } catch (Exception e) { - e.printStackTrace(); - this.callbackContext.error(e.getMessage()); + } else { + this.callbackContext.error(Integer.toString(resultCode)); } - } else { - this.callbackContext.error(Integer.toString(resultCode)); + return; } - return; - } - - super.onActivityResult(requestCode, resultCode, data); + super.onActivityResult(requestCode, resultCode, data); + }); } From 5e1bcbf56d0077f802ef1e6508cbc5b20834c55d Mon Sep 17 00:00:00 2001 From: Sk Heerah Zafir Date: Mon, 11 Apr 2022 14:13:34 +0400 Subject: [PATCH 21/22] Remove runBlockWithTryCatch() in execute() Method --- .../com/pbakondy/SpeechRecognition.java | 42 ++++++++++--------- 1 file changed, 22 insertions(+), 20 deletions(-) diff --git a/src/android/com/pbakondy/SpeechRecognition.java b/src/android/com/pbakondy/SpeechRecognition.java index 6cad6d9..d1b2097 100644 --- a/src/android/com/pbakondy/SpeechRecognition.java +++ b/src/android/com/pbakondy/SpeechRecognition.java @@ -82,26 +82,26 @@ public void run() { @Override public boolean execute(String action, JSONArray args, CallbackContext callbackContext) throws JSONException { - runBlockWithTryCatch(() -> { - this.callbackContext = callbackContext; + this.callbackContext = callbackContext; - Log.d(LOG_TAG, "execute() action " + action); + Log.d(LOG_TAG, "execute() action " + action); + try { if (IS_RECOGNITION_AVAILABLE.equals(action)) { boolean available = isRecognitionAvailable(); PluginResult result = new PluginResult(PluginResult.Status.OK, available); callbackContext.sendPluginResult(result); - return; + return true; } if (START_LISTENING.equals(action)) { if (!isRecognitionAvailable()) { callbackContext.error(NOT_AVAILABLE); - return; + return true; } if (!audioPermissionGranted(RECORD_AUDIO_PERMISSION)) { callbackContext.error(MISSING_PERMISSION); - return; + return true; } String lang = args.optString(0); @@ -119,9 +119,9 @@ public boolean execute(String action, JSONArray args, CallbackContext callbackCo mLastPartialResults = new JSONArray(); Boolean showPartial = args.optBoolean(3, false); Boolean showPopup = args.optBoolean(4, true); - startListening(lang, matches, prompt, showPartial, showPopup); + startListening(lang, matches, prompt,showPartial, showPopup); - return; + return true; } if (STOP_LISTENING.equals(action)) { @@ -129,34 +129,36 @@ public boolean execute(String action, JSONArray args, CallbackContext callbackCo view.post(new Runnable() { @Override public void run() { - runBlockWithTryCatch(() -> { - if (recognizer != null) { - recognizer.stopListening(); - } - callbackContextStop.success(); - }); + if(recognizer != null) { + recognizer.stopListening(); + } + callbackContextStop.success(); } }); - return; + return true; } if (GET_SUPPORTED_LANGUAGES.equals(action)) { getSupportedLanguages(); - return; + return true; } if (HAS_PERMISSION.equals(action)) { hasAudioPermission(); - return; + return true; } if (REQUEST_PERMISSION.equals(action)) { requestAudioPermission(); - return; + return true; } - }); - return true; + } catch (Exception e) { + e.printStackTrace(); + callbackContext.error(e.getMessage()); + } + + return false; } private boolean isRecognitionAvailable() { From caf1e66be3140b82fd1d784782a93567fad9fdbb Mon Sep 17 00:00:00 2001 From: Sk Heerah Zafir Date: Wed, 13 Apr 2022 16:20:48 +0400 Subject: [PATCH 22/22] Use Explicit Try/Catch --- .../com/pbakondy/SpeechRecognition.java | 178 ++++++++++-------- 1 file changed, 103 insertions(+), 75 deletions(-) diff --git a/src/android/com/pbakondy/SpeechRecognition.java b/src/android/com/pbakondy/SpeechRecognition.java index d1b2097..0252429 100644 --- a/src/android/com/pbakondy/SpeechRecognition.java +++ b/src/android/com/pbakondy/SpeechRecognition.java @@ -60,7 +60,7 @@ public class SpeechRecognition extends CordovaPlugin { @Override public void initialize(CordovaInterface cordova, CordovaWebView webView) { - runBlockWithTryCatch(() -> { + try { super.initialize(cordova, webView); activity = cordova.getActivity(); @@ -70,14 +70,18 @@ public void initialize(CordovaInterface cordova, CordovaWebView webView) { view.post(new Runnable() { @Override public void run() { - runBlockWithTryCatch(() -> { + try { recognizer = SpeechRecognizer.createSpeechRecognizer(activity); SpeechRecognitionListener listener = new SpeechRecognitionListener(); recognizer.setRecognitionListener(listener); - }); + } catch (Exception e) { + exceptionCallback(e); + } } }); - }); + } catch (Exception e) { + exceptionCallback(e); + } } @Override @@ -129,10 +133,14 @@ public boolean execute(String action, JSONArray args, CallbackContext callbackCo view.post(new Runnable() { @Override public void run() { - if(recognizer != null) { - recognizer.stopListening(); + try { + if (recognizer != null) { + recognizer.stopListening(); + } + callbackContextStop.success(); + } catch (Exception e) { + exceptionCallback(e); } - callbackContextStop.success(); } }); return true; @@ -154,8 +162,7 @@ public void run() { } } catch (Exception e) { - e.printStackTrace(); - callbackContext.error(e.getMessage()); + exceptionCallback(e); } return false; @@ -166,59 +173,77 @@ private boolean isRecognitionAvailable() { } private void startListening(String language, int matches, String prompt, final Boolean showPartial, Boolean showPopup) { - Log.d(LOG_TAG, "startListening() language: " + language + ", matches: " + matches + ", prompt: " + prompt + ", showPartial: " + showPartial + ", showPopup: " + showPopup); - - final Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH); - intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, - RecognizerIntent.LANGUAGE_MODEL_FREE_FORM); - intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE, language); - intent.putExtra(RecognizerIntent.EXTRA_MAX_RESULTS, matches); - intent.putExtra(RecognizerIntent.EXTRA_CALLING_PACKAGE, - activity.getPackageName()); - intent.putExtra(RecognizerIntent.EXTRA_PARTIAL_RESULTS, showPartial); - intent.putExtra("android.speech.extra.DICTATION_MODE", showPartial); - - if (prompt != null) { - intent.putExtra(RecognizerIntent.EXTRA_PROMPT, prompt); - } + try { + Log.d(LOG_TAG, "startListening() language: " + language + ", matches: " + matches + ", prompt: " + prompt + ", showPartial: " + showPartial + ", showPopup: " + showPopup); + + final Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH); + intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, + RecognizerIntent.LANGUAGE_MODEL_FREE_FORM); + intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE, language); + intent.putExtra(RecognizerIntent.EXTRA_MAX_RESULTS, matches); + intent.putExtra(RecognizerIntent.EXTRA_CALLING_PACKAGE, + activity.getPackageName()); + intent.putExtra(RecognizerIntent.EXTRA_PARTIAL_RESULTS, showPartial); + intent.putExtra("android.speech.extra.DICTATION_MODE", showPartial); + + if (prompt != null) { + intent.putExtra(RecognizerIntent.EXTRA_PROMPT, prompt); + } - if (showPopup) { - cordova.startActivityForResult(this, intent, REQUEST_CODE_SPEECH); - } else { - view.post(new Runnable() { - @Override - public void run() { - runBlockWithTryCatch(() -> { - recognizer.startListening(intent); - }); - } - }); + if (showPopup) { + cordova.startActivityForResult(this, intent, REQUEST_CODE_SPEECH); + } else { + view.post(new Runnable() { + @Override + public void run() { + try { + recognizer.startListening(intent); + } catch (Exception e) { + exceptionCallback(e); + } + } + }); + } + } catch (Exception e) { + exceptionCallback(e); } } private void getSupportedLanguages() { - if (languageDetailsChecker == null) { - languageDetailsChecker = new LanguageDetailsChecker(callbackContext); - } + try { + if (languageDetailsChecker == null) { + languageDetailsChecker = new LanguageDetailsChecker(callbackContext); + } - List supportedLanguages = languageDetailsChecker.getSupportedLanguages(); - if (supportedLanguages != null) { - JSONArray languages = new JSONArray(supportedLanguages); - callbackContext.success(languages); - return; - } + List supportedLanguages = languageDetailsChecker.getSupportedLanguages(); + if (supportedLanguages != null) { + JSONArray languages = new JSONArray(supportedLanguages); + callbackContext.success(languages); + return; + } - Intent detailsIntent = new Intent(RecognizerIntent.ACTION_GET_LANGUAGE_DETAILS); - activity.sendOrderedBroadcast(detailsIntent, null, languageDetailsChecker, null, Activity.RESULT_OK, null, null); + Intent detailsIntent = new Intent(RecognizerIntent.ACTION_GET_LANGUAGE_DETAILS); + activity.sendOrderedBroadcast(detailsIntent, null, languageDetailsChecker, null, Activity.RESULT_OK, null, null); + } catch (Exception e) { + exceptionCallback(e); + } } private void hasAudioPermission() { - PluginResult result = new PluginResult(PluginResult.Status.OK, audioPermissionGranted(RECORD_AUDIO_PERMISSION)); - this.callbackContext.sendPluginResult(result); + try { + PluginResult result = new PluginResult(PluginResult.Status.OK, audioPermissionGranted(RECORD_AUDIO_PERMISSION)); + this.callbackContext.sendPluginResult(result); + } catch (Exception e) { + exceptionCallback(e); + } } private void requestAudioPermission() { - requestPermission(RECORD_AUDIO_PERMISSION); + try { + requestPermission(RECORD_AUDIO_PERMISSION); + } catch(Exception e) { + exceptionCallback(e); + } } private boolean audioPermissionGranted(String type) { @@ -229,41 +254,50 @@ private boolean audioPermissionGranted(String type) { } private void requestPermission(String type) { - if (!audioPermissionGranted(type)) { - cordova.requestPermission(this, 23456, type); - } else { - this.callbackContext.success(); + try { + if (!audioPermissionGranted(type)) { + cordova.requestPermission(this, 23456, type); + } else { + this.callbackContext.success(); + } + } catch (Exception e) { + exceptionCallback(e); } } @Override public void onRequestPermissionResult(int requestCode, String[] permissions, int[] grantResults) throws JSONException { - runBlockWithTryCatch(() -> { + try { if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) { this.callbackContext.success(); } else { this.callbackContext.error("Permission denied"); } - }); + } catch(Exception e) { + exceptionCallback(e); + } } @Override public void onActivityResult(int requestCode, int resultCode, Intent data) { - runBlockWithTryCatch(() -> { - Log.d(LOG_TAG, "onActivityResult() requestCode: " + requestCode + ", resultCode: " + resultCode); + Log.d(LOG_TAG, "onActivityResult() requestCode: " + requestCode + ", resultCode: " + resultCode); - if (requestCode == REQUEST_CODE_SPEECH) { - if (resultCode == Activity.RESULT_OK) { + if (requestCode == REQUEST_CODE_SPEECH) { + if (resultCode == Activity.RESULT_OK) { + try { ArrayList matches = data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS); JSONArray jsonMatches = new JSONArray(matches); this.callbackContext.success(jsonMatches); - } else { - this.callbackContext.error(Integer.toString(resultCode)); + } catch (Exception e) { + exceptionCallback(e); } - return; + } else { + this.callbackContext.error(Integer.toString(resultCode)); } - super.onActivityResult(requestCode, resultCode, data); - }); + return; + } + + super.onActivityResult(requestCode, resultCode, data); } @@ -307,8 +341,7 @@ public void onPartialResults(Bundle bundle) { callbackContext.sendPluginResult(pluginResult); } } catch (Exception e) { - e.printStackTrace(); - callbackContext.error(e.getMessage()); + exceptionCallback(e); } } @@ -325,8 +358,7 @@ public void onResults(Bundle results) { JSONArray jsonMatches = new JSONArray(matches); callbackContext.success(jsonMatches); } catch (Exception e) { - e.printStackTrace(); - callbackContext.error(e.getMessage()); + exceptionCallback(e); } } @@ -372,12 +404,8 @@ private String getErrorText(int errorCode) { } } - void runBlockWithTryCatch(Runnable runnable) { - try { - runnable.run(); - } catch (Exception e) { - e.printStackTrace(); - callbackContext.error(e.getMessage()); - } + void exceptionCallback(Exception e) { + e.printStackTrace(); + callbackContext.error(e.getMessage()); } }