11
11
import androidx .annotation .Nullable ;
12
12
import com .getcapacitor .JSArray ;
13
13
import com .getcapacitor .JSObject ;
14
- import com .getcapacitor .NativePlugin ;
15
14
import com .getcapacitor .Plugin ;
16
15
import com .getcapacitor .PluginCall ;
17
16
import com .getcapacitor .PluginMethod ;
17
+ import com .getcapacitor .annotation .CapacitorPlugin ;
18
+ import com .getcapacitor .annotation .Permission ;
18
19
import com .orhanobut .logger .AndroidLogAdapter ;
19
20
import com .orhanobut .logger .BuildConfig ;
20
21
import com .orhanobut .logger .Logger ;
24
25
import java .util .concurrent .locks .ReentrantLock ;
25
26
import org .json .JSONArray ;
26
27
27
- @ NativePlugin (
28
- permissions = { Manifest .permission .RECORD_AUDIO },
29
- requestCodes = { SpeechRecognition .REQUEST_CODE_SPEECH }
28
+ @ CapacitorPlugin (
29
+ permissions = {
30
+ @ Permission (
31
+ strings = { Manifest .permission .RECORD_AUDIO },
32
+ alias = "record_audio"
33
+ ),
34
+ }
30
35
)
31
36
public class SpeechRecognition extends Plugin implements Constants {
32
37
public static final String TAG = "SpeechRecognition" ;
@@ -77,18 +82,18 @@ public void available(PluginCall call) {
77
82
boolean val = isSpeechRecognitionAvailable ();
78
83
JSObject result = new JSObject ();
79
84
result .put ("available" , val );
80
- call .success (result );
85
+ call .resolve (result );
81
86
}
82
87
83
88
@ PluginMethod
84
89
public void start (PluginCall call ) {
85
90
if (!isSpeechRecognitionAvailable ()) {
86
- call .error (NOT_AVAILABLE );
91
+ call .unavailable (NOT_AVAILABLE );
87
92
return ;
88
93
}
89
94
90
95
if (!hasAudioPermissions (RECORD_AUDIO_PERMISSION )) {
91
- call .error (MISSING_PERMISSION );
96
+ call .reject (MISSING_PERMISSION );
92
97
return ;
93
98
}
94
99
@@ -108,7 +113,7 @@ public void stop(final PluginCall call) {
108
113
try {
109
114
stopListening ();
110
115
} catch (Exception ex ) {
111
- call .error (ex .getLocalizedMessage ());
116
+ call .reject (ex .getLocalizedMessage ());
112
117
}
113
118
}
114
119
@@ -121,7 +126,7 @@ public void getSupportedLanguages(PluginCall call) {
121
126
List <String > supportedLanguages = languageReceiver .getSupportedLanguages ();
122
127
if (supportedLanguages != null ) {
123
128
JSONArray languages = new JSONArray (supportedLanguages );
124
- call .success (new JSObject ().put ("languages" , languages ));
129
+ call .resolve (new JSObject ().put ("languages" , languages ));
125
130
return ;
126
131
}
127
132
@@ -146,7 +151,7 @@ public void getSupportedLanguages(PluginCall call) {
146
151
147
152
@ PluginMethod
148
153
public void hasPermission (PluginCall call ) {
149
- call .success (
154
+ call .resolve (
150
155
new JSObject ()
151
156
.put ("permission" , hasAudioPermissions (RECORD_AUDIO_PERMISSION ))
152
157
);
@@ -162,9 +167,9 @@ public void requestPermission(PluginCall call) {
162
167
new String [] { RECORD_AUDIO_PERMISSION },
163
168
REQUEST_CODE_PERMISSION
164
169
);
165
- call .success ();
170
+ call .resolve ();
166
171
} else {
167
- call .success ();
172
+ call .resolve ();
168
173
}
169
174
}
170
175
}
@@ -190,12 +195,12 @@ protected void handleOnActivityResult(
190
195
);
191
196
JSObject result = new JSObject ();
192
197
result .put ("matches" , new JSArray (matchesList ));
193
- savedCall .success (result );
198
+ savedCall .resolve (result );
194
199
} catch (Exception ex ) {
195
- savedCall .error (ex .getMessage ());
200
+ savedCall .reject (ex .getMessage ());
196
201
}
197
202
} else {
198
- savedCall .error (Integer .toString (requestCode ));
203
+ savedCall .reject (Integer .toString (requestCode ));
199
204
}
200
205
201
206
SpeechRecognition .this .lock .lock ();
@@ -273,7 +278,7 @@ private void beginListening(
273
278
speechRecognizer .startListening (intent );
274
279
SpeechRecognition .this .listening (true );
275
280
} catch (Exception ex ) {
276
- call .error (ex .getMessage ());
281
+ call .reject (ex .getMessage ());
277
282
} finally {
278
283
SpeechRecognition .this .lock .unlock ();
279
284
}
@@ -330,7 +335,7 @@ public void onError(int error) {
330
335
String errorMssg = getErrorText (error );
331
336
332
337
if (this .call != null ) {
333
- call .error (errorMssg );
338
+ call .reject (errorMssg );
334
339
}
335
340
}
336
341
@@ -344,12 +349,12 @@ public void onResults(Bundle results) {
344
349
JSArray jsArray = new JSArray (matches );
345
350
346
351
if (this .call != null ) {
347
- this .call .success (
352
+ this .call .resolve (
348
353
new JSObject ().put ("status" , "success" ).put ("matches" , jsArray )
349
354
);
350
355
}
351
356
} catch (Exception ex ) {
352
- this .call .success (
357
+ this .call .resolve (
353
358
new JSObject ()
354
359
.put ("status" , "error" )
355
360
.put ("message" , ex .getMessage ())
0 commit comments