Skip to content

Commit 568eae6

Browse files
authored
Merge pull request #20 from Pantrist-dev/migrate-cap-3
feat: migrate to capacitor 3
2 parents d41df6a + 09b7f85 commit 568eae6

File tree

15 files changed

+144
-144
lines changed

15 files changed

+144
-144
lines changed

CapacitorCommunitySpeechRecognition.podspec

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,6 @@
88
s.author = 'Priyank Patel <[email protected]>'
99
s.source = { :git => 'https://github.com/capacitor-community/speech-recognition', :tag => s.version.to_s }
1010
s.source_files = 'ios/Plugin/**/*.{swift,h,m,c,cc,mm,cpp}'
11-
s.ios.deployment_target = '11.0'
11+
s.ios.deployment_target = '12.0'
1212
s.dependency 'Capacitor'
1313
end

README.md

+1-3
Original file line numberDiff line numberDiff line change
@@ -78,9 +78,7 @@ No configuration required for this plugin
7878
## Usage
7979

8080
```typescript
81-
import { Plugins } from "@capacitor/core";
82-
83-
const { SpeechRecognition } = Plugins;
81+
import { SpeechRecognition } from "@capacitor-community/speech-recognition";
8482

8583
/**
8684
* This method will check if speech recognition feature is available on the device.

android/build.gradle

+10-9
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
ext {
2-
junitVersion = project.hasProperty('junitVersion') ? rootProject.ext.junitVersion : '4.12'
3-
androidxJunitVersion = project.hasProperty('androidxJunitVersion') ? rootProject.ext.androidxJunitVersion : '1.1.1'
4-
androidxEspressoCoreVersion = project.hasProperty('androidxEspressoCoreVersion') ? rootProject.ext.androidxEspressoCoreVersion : '3.2.0'
2+
junitVersion = project.hasProperty('junitVersion') ? rootProject.ext.junitVersion : '4.13'
3+
androidxAppCompatVersion = project.hasProperty('androidxAppCompatVersion') ? rootProject.ext.androidxAppCompatVersion : '1.2.0'
4+
androidxJunitVersion = project.hasProperty('androidxJunitVersion') ? rootProject.ext.androidxJunitVersion : '1.1.2'
5+
androidxEspressoCoreVersion = project.hasProperty('androidxEspressoCoreVersion') ? rootProject.ext.androidxEspressoCoreVersion : '3.3.0'
56
}
67

78
buildscript {
@@ -10,20 +11,20 @@ buildscript {
1011
jcenter()
1112
}
1213
dependencies {
13-
classpath 'com.android.tools.build:gradle:3.6.1'
14+
classpath 'com.android.tools.build:gradle:4.2.1'
1415
}
1516
}
1617

1718
apply plugin: 'com.android.library'
1819

1920
android {
20-
compileSdkVersion project.hasProperty('compileSdkVersion') ? rootProject.ext.compileSdkVersion : 29
21+
compileSdkVersion project.hasProperty('compileSdkVersion') ? rootProject.ext.compileSdkVersion : 30
2122
defaultConfig {
2223
minSdkVersion project.hasProperty('minSdkVersion') ? rootProject.ext.minSdkVersion : 21
23-
targetSdkVersion project.hasProperty('targetSdkVersion') ? rootProject.ext.targetSdkVersion : 29
24+
targetSdkVersion project.hasProperty('targetSdkVersion') ? rootProject.ext.targetSdkVersion : 30
2425
versionCode 1
25-
versionName "1.0"
26-
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
26+
versionName '1.0'
27+
testInstrumentationRunner 'androidx.test.runner.AndroidJUnitRunner'
2728
}
2829
buildTypes {
2930
release {
@@ -46,10 +47,10 @@ repositories {
4647
mavenCentral()
4748
}
4849

49-
5050
dependencies {
5151
implementation fileTree(dir: 'libs', include: ['*.jar'])
5252
implementation project(':capacitor-android')
53+
implementation "androidx.appcompat:appcompat:$androidxAppCompatVersion"
5354
testImplementation "junit:junit:$junitVersion"
5455
androidTestImplementation "androidx.test.ext:junit:$androidxJunitVersion"
5556
androidTestImplementation "androidx.test.espresso:espresso-core:$androidxEspressoCoreVersion"

android/src/main/java/com/getcapacitor/community/speechrecognition/Constants.java

+13-13
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,17 @@
33
import android.Manifest;
44

55
public interface Constants {
6-
int REQUEST_CODE_PERMISSION = 2001;
7-
int REQUEST_CODE_SPEECH = 2002;
8-
String IS_RECOGNITION_AVAILABLE = "isRecognitionAvailable";
9-
String START_LISTENING = "startListening";
10-
String STOP_LISTENING = "stopListening";
11-
String GET_SUPPORTED_LANGUAGES = "getSupportedLanguages";
12-
String HAS_PERMISSION = "hasPermission";
13-
String REQUEST_PERMISSION = "requestPermission";
14-
int MAX_RESULTS = 5;
15-
String NOT_AVAILABLE = "Speech recognition service is not available.";
16-
String MISSING_PERMISSION = "Missing permission";
17-
String RECORD_AUDIO_PERMISSION = Manifest.permission.RECORD_AUDIO;
18-
String ERROR = "Could not get list of languages";
6+
int REQUEST_CODE_PERMISSION = 2001;
7+
int REQUEST_CODE_SPEECH = 2002;
8+
String IS_RECOGNITION_AVAILABLE = "isRecognitionAvailable";
9+
String START_LISTENING = "startListening";
10+
String STOP_LISTENING = "stopListening";
11+
String GET_SUPPORTED_LANGUAGES = "getSupportedLanguages";
12+
String HAS_PERMISSION = "hasPermission";
13+
String REQUEST_PERMISSION = "requestPermission";
14+
int MAX_RESULTS = 5;
15+
String NOT_AVAILABLE = "Speech recognition service is not available.";
16+
String MISSING_PERMISSION = "Missing permission";
17+
String RECORD_AUDIO_PERMISSION = Manifest.permission.RECORD_AUDIO;
18+
String ERROR = "Could not get list of languages";
1919
}

android/src/main/java/com/getcapacitor/community/speechrecognition/Receiver.java

+29-31
Original file line numberDiff line numberDiff line change
@@ -5,51 +5,49 @@
55
import android.content.Intent;
66
import android.os.Bundle;
77
import android.speech.RecognizerIntent;
8-
98
import com.getcapacitor.JSArray;
109
import com.getcapacitor.JSObject;
1110
import com.getcapacitor.PluginCall;
12-
1311
import java.util.List;
1412

1513
public class Receiver extends BroadcastReceiver implements Constants {
14+
public static final String TAG = "Receiver";
1615

17-
public static final String TAG = "Receiver";
16+
private List<String> supportedLanguagesList;
17+
private String languagePref;
18+
private PluginCall call;
1819

19-
private List<String> supportedLanguagesList;
20-
private String languagePref;
21-
private PluginCall call;
20+
public Receiver(PluginCall call) {
21+
super();
22+
this.call = call;
23+
}
2224

23-
public Receiver(PluginCall call) {
24-
super();
25+
@Override
26+
public void onReceive(Context context, Intent intent) {
27+
Bundle extras = getResultExtras(true);
2528

26-
this.call = call;
29+
if (extras.containsKey(RecognizerIntent.EXTRA_LANGUAGE_PREFERENCE)) {
30+
languagePref =
31+
extras.getString(RecognizerIntent.EXTRA_LANGUAGE_PREFERENCE);
2732
}
2833

29-
@Override
30-
public void onReceive(Context context, Intent intent) {
31-
Bundle extras = getResultExtras(true);
32-
33-
if (extras.containsKey(RecognizerIntent.EXTRA_LANGUAGE_PREFERENCE)) {
34-
languagePref = extras.getString(RecognizerIntent.EXTRA_LANGUAGE_PREFERENCE);
35-
}
36-
37-
if (extras.containsKey(RecognizerIntent.EXTRA_SUPPORTED_LANGUAGES)) {
38-
supportedLanguagesList = extras.getStringArrayList(RecognizerIntent.EXTRA_SUPPORTED_LANGUAGES);
34+
if (extras.containsKey(RecognizerIntent.EXTRA_SUPPORTED_LANGUAGES)) {
35+
supportedLanguagesList =
36+
extras.getStringArrayList(RecognizerIntent.EXTRA_SUPPORTED_LANGUAGES);
3937

40-
JSArray languagesList = new JSArray(supportedLanguagesList);
41-
call.success(new JSObject().put("languages", languagesList));
42-
return;
43-
}
44-
45-
call.error(ERROR);
38+
JSArray languagesList = new JSArray(supportedLanguagesList);
39+
call.resolve(new JSObject().put("languages", languagesList));
40+
return;
4641
}
4742

48-
public List<String> getSupportedLanguages() {
49-
return supportedLanguagesList;
50-
}
43+
call.reject(ERROR);
44+
}
5145

52-
public String getLanguagePreference() {
53-
return languagePref;
54-
}
46+
public List<String> getSupportedLanguages() {
47+
return supportedLanguagesList;
48+
}
49+
50+
public String getLanguagePreference() {
51+
return languagePref;
52+
}
5553
}

android/src/main/java/com/getcapacitor/community/speechrecognition/SpeechRecognition.java

+24-19
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,11 @@
1111
import androidx.annotation.Nullable;
1212
import com.getcapacitor.JSArray;
1313
import com.getcapacitor.JSObject;
14-
import com.getcapacitor.NativePlugin;
1514
import com.getcapacitor.Plugin;
1615
import com.getcapacitor.PluginCall;
1716
import com.getcapacitor.PluginMethod;
17+
import com.getcapacitor.annotation.CapacitorPlugin;
18+
import com.getcapacitor.annotation.Permission;
1819
import com.orhanobut.logger.AndroidLogAdapter;
1920
import com.orhanobut.logger.BuildConfig;
2021
import com.orhanobut.logger.Logger;
@@ -24,9 +25,13 @@
2425
import java.util.concurrent.locks.ReentrantLock;
2526
import org.json.JSONArray;
2627

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+
}
3035
)
3136
public class SpeechRecognition extends Plugin implements Constants {
3237
public static final String TAG = "SpeechRecognition";
@@ -77,18 +82,18 @@ public void available(PluginCall call) {
7782
boolean val = isSpeechRecognitionAvailable();
7883
JSObject result = new JSObject();
7984
result.put("available", val);
80-
call.success(result);
85+
call.resolve(result);
8186
}
8287

8388
@PluginMethod
8489
public void start(PluginCall call) {
8590
if (!isSpeechRecognitionAvailable()) {
86-
call.error(NOT_AVAILABLE);
91+
call.unavailable(NOT_AVAILABLE);
8792
return;
8893
}
8994

9095
if (!hasAudioPermissions(RECORD_AUDIO_PERMISSION)) {
91-
call.error(MISSING_PERMISSION);
96+
call.reject(MISSING_PERMISSION);
9297
return;
9398
}
9499

@@ -108,7 +113,7 @@ public void stop(final PluginCall call) {
108113
try {
109114
stopListening();
110115
} catch (Exception ex) {
111-
call.error(ex.getLocalizedMessage());
116+
call.reject(ex.getLocalizedMessage());
112117
}
113118
}
114119

@@ -121,7 +126,7 @@ public void getSupportedLanguages(PluginCall call) {
121126
List<String> supportedLanguages = languageReceiver.getSupportedLanguages();
122127
if (supportedLanguages != null) {
123128
JSONArray languages = new JSONArray(supportedLanguages);
124-
call.success(new JSObject().put("languages", languages));
129+
call.resolve(new JSObject().put("languages", languages));
125130
return;
126131
}
127132

@@ -146,7 +151,7 @@ public void getSupportedLanguages(PluginCall call) {
146151

147152
@PluginMethod
148153
public void hasPermission(PluginCall call) {
149-
call.success(
154+
call.resolve(
150155
new JSObject()
151156
.put("permission", hasAudioPermissions(RECORD_AUDIO_PERMISSION))
152157
);
@@ -162,9 +167,9 @@ public void requestPermission(PluginCall call) {
162167
new String[] { RECORD_AUDIO_PERMISSION },
163168
REQUEST_CODE_PERMISSION
164169
);
165-
call.success();
170+
call.resolve();
166171
} else {
167-
call.success();
172+
call.resolve();
168173
}
169174
}
170175
}
@@ -190,12 +195,12 @@ protected void handleOnActivityResult(
190195
);
191196
JSObject result = new JSObject();
192197
result.put("matches", new JSArray(matchesList));
193-
savedCall.success(result);
198+
savedCall.resolve(result);
194199
} catch (Exception ex) {
195-
savedCall.error(ex.getMessage());
200+
savedCall.reject(ex.getMessage());
196201
}
197202
} else {
198-
savedCall.error(Integer.toString(requestCode));
203+
savedCall.reject(Integer.toString(requestCode));
199204
}
200205

201206
SpeechRecognition.this.lock.lock();
@@ -273,7 +278,7 @@ private void beginListening(
273278
speechRecognizer.startListening(intent);
274279
SpeechRecognition.this.listening(true);
275280
} catch (Exception ex) {
276-
call.error(ex.getMessage());
281+
call.reject(ex.getMessage());
277282
} finally {
278283
SpeechRecognition.this.lock.unlock();
279284
}
@@ -330,7 +335,7 @@ public void onError(int error) {
330335
String errorMssg = getErrorText(error);
331336

332337
if (this.call != null) {
333-
call.error(errorMssg);
338+
call.reject(errorMssg);
334339
}
335340
}
336341

@@ -344,12 +349,12 @@ public void onResults(Bundle results) {
344349
JSArray jsArray = new JSArray(matches);
345350

346351
if (this.call != null) {
347-
this.call.success(
352+
this.call.resolve(
348353
new JSObject().put("status", "success").put("matches", jsArray)
349354
);
350355
}
351356
} catch (Exception ex) {
352-
this.call.success(
357+
this.call.resolve(
353358
new JSObject()
354359
.put("status", "error")
355360
.put("message", ex.getMessage())

ios/Plugin.xcodeproj/project.pbxproj

+4-4
Original file line numberDiff line numberDiff line change
@@ -375,7 +375,7 @@
375375
GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE;
376376
GCC_WARN_UNUSED_FUNCTION = YES;
377377
GCC_WARN_UNUSED_VARIABLE = YES;
378-
IPHONEOS_DEPLOYMENT_TARGET = 11.0;
378+
IPHONEOS_DEPLOYMENT_TARGET = 12.0;
379379
MTL_ENABLE_DEBUG_INFO = YES;
380380
ONLY_ACTIVE_ARCH = YES;
381381
SDKROOT = iphoneos;
@@ -429,7 +429,7 @@
429429
GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE;
430430
GCC_WARN_UNUSED_FUNCTION = YES;
431431
GCC_WARN_UNUSED_VARIABLE = YES;
432-
IPHONEOS_DEPLOYMENT_TARGET = 11.0;
432+
IPHONEOS_DEPLOYMENT_TARGET = 12.0;
433433
MTL_ENABLE_DEBUG_INFO = NO;
434434
SDKROOT = iphoneos;
435435
SWIFT_OPTIMIZATION_LEVEL = "-Owholemodule";
@@ -452,7 +452,7 @@
452452
DYLIB_INSTALL_NAME_BASE = "@rpath";
453453
INFOPLIST_FILE = Plugin/Info.plist;
454454
INSTALL_PATH = "$(LOCAL_LIBRARY_DIR)/Frameworks";
455-
IPHONEOS_DEPLOYMENT_TARGET = 11.0;
455+
IPHONEOS_DEPLOYMENT_TARGET = 12.0;
456456
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks $(FRAMEWORK_SEARCH_PATHS)\n$(FRAMEWORK_SEARCH_PATHS)\n$(FRAMEWORK_SEARCH_PATHS)";
457457
ONLY_ACTIVE_ARCH = YES;
458458
PRODUCT_BUNDLE_IDENTIFIER = com.getcapacitor.Plugin;
@@ -477,7 +477,7 @@
477477
DYLIB_INSTALL_NAME_BASE = "@rpath";
478478
INFOPLIST_FILE = Plugin/Info.plist;
479479
INSTALL_PATH = "$(LOCAL_LIBRARY_DIR)/Frameworks";
480-
IPHONEOS_DEPLOYMENT_TARGET = 11.0;
480+
IPHONEOS_DEPLOYMENT_TARGET = 12.0;
481481
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks $(FRAMEWORK_SEARCH_PATHS)";
482482
ONLY_ACTIVE_ARCH = NO;
483483
PRODUCT_BUNDLE_IDENTIFIER = com.getcapacitor.Plugin;

0 commit comments

Comments
 (0)