@capgo/capacitor-android-sms-retriever 8.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CapgoCapacitorAndroidSmsRetriever.podspec +17 -0
- package/LICENSE +373 -0
- package/Package.swift +28 -0
- package/README.md +312 -0
- package/android/build.gradle +63 -0
- package/android/src/main/AndroidManifest.xml +2 -0
- package/android/src/main/java/app/capgo/androidsmsretriever/AndroidSmsRetriever.java +137 -0
- package/android/src/main/java/app/capgo/androidsmsretriever/AndroidSmsRetrieverPlugin.java +224 -0
- package/android/src/main/java/app/capgo/androidsmsretriever/AppSignatureHashHelper.java +78 -0
- package/android/src/main/java/app/capgo/androidsmsretriever/SmsBroadcastReceiver.java +79 -0
- package/android/src/main/res/.gitkeep +0 -0
- package/dist/docs.json +423 -0
- package/dist/esm/definitions.d.ts +165 -0
- package/dist/esm/definitions.js +2 -0
- package/dist/esm/definitions.js.map +1 -0
- package/dist/esm/index.d.ts +4 -0
- package/dist/esm/index.js +7 -0
- package/dist/esm/index.js.map +1 -0
- package/dist/esm/web.d.ts +9 -0
- package/dist/esm/web.js +21 -0
- package/dist/esm/web.js.map +1 -0
- package/dist/plugin.cjs.js +35 -0
- package/dist/plugin.cjs.js.map +1 -0
- package/dist/plugin.js +38 -0
- package/dist/plugin.js.map +1 -0
- package/ios/Sources/AndroidSmsRetrieverPlugin/AndroidSmsRetriever.swift +7 -0
- package/ios/Sources/AndroidSmsRetrieverPlugin/AndroidSmsRetrieverPlugin.swift +36 -0
- package/ios/Tests/AndroidSmsRetrieverPluginTests/AndroidSmsRetrieverTests.swift +11 -0
- package/package.json +94 -0
- package/src/definitions.ts +185 -0
- package/src/index.ts +10 -0
- package/src/web.ts +34 -0
package/README.md
ADDED
|
@@ -0,0 +1,312 @@
|
|
|
1
|
+
# @capgo/capacitor-android-sms-retriever
|
|
2
|
+
|
|
3
|
+
<a href="https://capgo.app/"><img src="https://raw.githubusercontent.com/Cap-go/capgo/main/assets/capgo_banner.png" alt="Capgo - Instant updates for Capacitor" /></a>
|
|
4
|
+
|
|
5
|
+
<div align="center">
|
|
6
|
+
<h2><a href="https://capgo.app/?ref=plugin_android_sms_retriever">Get instant updates for your app with Capgo</a></h2>
|
|
7
|
+
<h2><a href="https://capgo.app/consulting/?ref=plugin_android_sms_retriever">Missing a feature? We can build the plugin for you</a></h2>
|
|
8
|
+
</div>
|
|
9
|
+
|
|
10
|
+
Android-only Capacitor plugin for Google Play services SMS Retriever and Phone Number Hint APIs.
|
|
11
|
+
|
|
12
|
+
## Install
|
|
13
|
+
|
|
14
|
+
```bash
|
|
15
|
+
bun add @capgo/capacitor-android-sms-retriever
|
|
16
|
+
bunx cap sync android
|
|
17
|
+
```
|
|
18
|
+
|
|
19
|
+
## Android
|
|
20
|
+
|
|
21
|
+
This plugin does not request SMS permissions. SMS retrieval uses Google Play services and waits for one matching verification SMS for up to five minutes.
|
|
22
|
+
|
|
23
|
+
The verification SMS must include the app hash returned by `getHashString()`.
|
|
24
|
+
|
|
25
|
+
## Usage
|
|
26
|
+
|
|
27
|
+
```typescript
|
|
28
|
+
import { AndroidSmsRetriever } from '@capgo/capacitor-android-sms-retriever';
|
|
29
|
+
|
|
30
|
+
const { hash } = await AndroidSmsRetriever.getHashString();
|
|
31
|
+
console.log('SMS hash:', hash);
|
|
32
|
+
|
|
33
|
+
const received = await AndroidSmsRetriever.addListener('smsReceived', ({ message }) => {
|
|
34
|
+
console.log('Verification SMS:', message);
|
|
35
|
+
});
|
|
36
|
+
|
|
37
|
+
await AndroidSmsRetriever.addListener('smsRetrieverTimeout', () => {
|
|
38
|
+
console.log('SMS Retriever timed out');
|
|
39
|
+
});
|
|
40
|
+
|
|
41
|
+
await AndroidSmsRetriever.startWatch();
|
|
42
|
+
|
|
43
|
+
// Later, if needed:
|
|
44
|
+
await AndroidSmsRetriever.stopWatch();
|
|
45
|
+
await received.remove();
|
|
46
|
+
```
|
|
47
|
+
|
|
48
|
+
```typescript
|
|
49
|
+
const { phoneNumber } = await AndroidSmsRetriever.getPhoneNumber();
|
|
50
|
+
console.log(phoneNumber);
|
|
51
|
+
```
|
|
52
|
+
|
|
53
|
+
## API
|
|
54
|
+
|
|
55
|
+
<docgen-index>
|
|
56
|
+
|
|
57
|
+
* [`startWatch()`](#startwatch)
|
|
58
|
+
* [`stopWatch()`](#stopwatch)
|
|
59
|
+
* [`getHashString()`](#gethashstring)
|
|
60
|
+
* [`getPhoneNumber()`](#getphonenumber)
|
|
61
|
+
* [`addListener('smsReceived', ...)`](#addlistenersmsreceived-)
|
|
62
|
+
* [`addListener('smsRetrieverTimeout', ...)`](#addlistenersmsretrievertimeout-)
|
|
63
|
+
* [`addListener('smsRetrieverError', ...)`](#addlistenersmsretrievererror-)
|
|
64
|
+
* [`removeAllListeners()`](#removealllisteners)
|
|
65
|
+
* [`getPluginVersion()`](#getpluginversion)
|
|
66
|
+
* [Interfaces](#interfaces)
|
|
67
|
+
* [Type Aliases](#type-aliases)
|
|
68
|
+
|
|
69
|
+
</docgen-index>
|
|
70
|
+
|
|
71
|
+
<docgen-api>
|
|
72
|
+
<!--Update the source file JSDoc comments and rerun docgen to update the docs below-->
|
|
73
|
+
|
|
74
|
+
Android-only Capacitor plugin for Google Play services SMS Retriever and Phone Number Hint APIs.
|
|
75
|
+
|
|
76
|
+
### startWatch()
|
|
77
|
+
|
|
78
|
+
```typescript
|
|
79
|
+
startWatch() => Promise<StartWatchResult>
|
|
80
|
+
```
|
|
81
|
+
|
|
82
|
+
Start listening for one verification SMS through Android's SMS Retriever API.
|
|
83
|
+
|
|
84
|
+
Android stops listening automatically when a matching message arrives or after the
|
|
85
|
+
five-minute SMS Retriever timeout. No SMS permissions are required.
|
|
86
|
+
|
|
87
|
+
**Returns:** <code>Promise<<a href="#startwatchresult">StartWatchResult</a>></code>
|
|
88
|
+
|
|
89
|
+
**Since:** 8.0.0
|
|
90
|
+
|
|
91
|
+
--------------------
|
|
92
|
+
|
|
93
|
+
|
|
94
|
+
### stopWatch()
|
|
95
|
+
|
|
96
|
+
```typescript
|
|
97
|
+
stopWatch() => Promise<StopWatchResult>
|
|
98
|
+
```
|
|
99
|
+
|
|
100
|
+
Stop the active SMS Retriever watch, if any.
|
|
101
|
+
|
|
102
|
+
**Returns:** <code>Promise<<a href="#stopwatchresult">StopWatchResult</a>></code>
|
|
103
|
+
|
|
104
|
+
**Since:** 8.0.0
|
|
105
|
+
|
|
106
|
+
--------------------
|
|
107
|
+
|
|
108
|
+
|
|
109
|
+
### getHashString()
|
|
110
|
+
|
|
111
|
+
```typescript
|
|
112
|
+
getHashString() => Promise<GetHashStringResult>
|
|
113
|
+
```
|
|
114
|
+
|
|
115
|
+
Get the 11-character app hash for the installed app signature.
|
|
116
|
+
|
|
117
|
+
Generate this once for the signing key used to distribute the app and append it
|
|
118
|
+
to verification SMS messages.
|
|
119
|
+
|
|
120
|
+
**Returns:** <code>Promise<<a href="#gethashstringresult">GetHashStringResult</a>></code>
|
|
121
|
+
|
|
122
|
+
**Since:** 8.0.0
|
|
123
|
+
|
|
124
|
+
--------------------
|
|
125
|
+
|
|
126
|
+
|
|
127
|
+
### getPhoneNumber()
|
|
128
|
+
|
|
129
|
+
```typescript
|
|
130
|
+
getPhoneNumber() => Promise<GetPhoneNumberResult>
|
|
131
|
+
```
|
|
132
|
+
|
|
133
|
+
Show Android's Phone Number Hint UI and return the user-selected SIM-based phone number.
|
|
134
|
+
|
|
135
|
+
**Returns:** <code>Promise<<a href="#getphonenumberresult">GetPhoneNumberResult</a>></code>
|
|
136
|
+
|
|
137
|
+
**Since:** 8.0.0
|
|
138
|
+
|
|
139
|
+
--------------------
|
|
140
|
+
|
|
141
|
+
|
|
142
|
+
### addListener('smsReceived', ...)
|
|
143
|
+
|
|
144
|
+
```typescript
|
|
145
|
+
addListener(eventName: 'smsReceived', listenerFunc: (event: SmsReceivedEvent) => void) => Promise<PluginListenerHandle>
|
|
146
|
+
```
|
|
147
|
+
|
|
148
|
+
Listen for a verification SMS received after {@link startWatch}.
|
|
149
|
+
|
|
150
|
+
| Param | Type |
|
|
151
|
+
| ------------------ | --------------------------------------------------------------------------------- |
|
|
152
|
+
| **`eventName`** | <code>'smsReceived'</code> |
|
|
153
|
+
| **`listenerFunc`** | <code>(event: <a href="#smsreceivedevent">SmsReceivedEvent</a>) => void</code> |
|
|
154
|
+
|
|
155
|
+
**Returns:** <code>Promise<<a href="#pluginlistenerhandle">PluginListenerHandle</a>></code>
|
|
156
|
+
|
|
157
|
+
**Since:** 8.0.0
|
|
158
|
+
|
|
159
|
+
--------------------
|
|
160
|
+
|
|
161
|
+
|
|
162
|
+
### addListener('smsRetrieverTimeout', ...)
|
|
163
|
+
|
|
164
|
+
```typescript
|
|
165
|
+
addListener(eventName: 'smsRetrieverTimeout', listenerFunc: () => void) => Promise<PluginListenerHandle>
|
|
166
|
+
```
|
|
167
|
+
|
|
168
|
+
Listen for the five-minute SMS Retriever timeout.
|
|
169
|
+
|
|
170
|
+
| Param | Type |
|
|
171
|
+
| ------------------ | ---------------------------------- |
|
|
172
|
+
| **`eventName`** | <code>'smsRetrieverTimeout'</code> |
|
|
173
|
+
| **`listenerFunc`** | <code>() => void</code> |
|
|
174
|
+
|
|
175
|
+
**Returns:** <code>Promise<<a href="#pluginlistenerhandle">PluginListenerHandle</a>></code>
|
|
176
|
+
|
|
177
|
+
**Since:** 8.0.0
|
|
178
|
+
|
|
179
|
+
--------------------
|
|
180
|
+
|
|
181
|
+
|
|
182
|
+
### addListener('smsRetrieverError', ...)
|
|
183
|
+
|
|
184
|
+
```typescript
|
|
185
|
+
addListener(eventName: 'smsRetrieverError', listenerFunc: (event: SmsRetrieverErrorEvent) => void) => Promise<PluginListenerHandle>
|
|
186
|
+
```
|
|
187
|
+
|
|
188
|
+
Listen for SMS Retriever runtime errors after {@link startWatch}.
|
|
189
|
+
|
|
190
|
+
| Param | Type |
|
|
191
|
+
| ------------------ | --------------------------------------------------------------------------------------------- |
|
|
192
|
+
| **`eventName`** | <code>'smsRetrieverError'</code> |
|
|
193
|
+
| **`listenerFunc`** | <code>(event: <a href="#smsretrievererrorevent">SmsRetrieverErrorEvent</a>) => void</code> |
|
|
194
|
+
|
|
195
|
+
**Returns:** <code>Promise<<a href="#pluginlistenerhandle">PluginListenerHandle</a>></code>
|
|
196
|
+
|
|
197
|
+
**Since:** 8.0.0
|
|
198
|
+
|
|
199
|
+
--------------------
|
|
200
|
+
|
|
201
|
+
|
|
202
|
+
### removeAllListeners()
|
|
203
|
+
|
|
204
|
+
```typescript
|
|
205
|
+
removeAllListeners() => Promise<void>
|
|
206
|
+
```
|
|
207
|
+
|
|
208
|
+
Remove all registered listeners for this plugin.
|
|
209
|
+
|
|
210
|
+
**Since:** 8.0.0
|
|
211
|
+
|
|
212
|
+
--------------------
|
|
213
|
+
|
|
214
|
+
|
|
215
|
+
### getPluginVersion()
|
|
216
|
+
|
|
217
|
+
```typescript
|
|
218
|
+
getPluginVersion() => Promise<PluginVersionResult>
|
|
219
|
+
```
|
|
220
|
+
|
|
221
|
+
Get the native Capacitor plugin version.
|
|
222
|
+
|
|
223
|
+
**Returns:** <code>Promise<<a href="#pluginversionresult">PluginVersionResult</a>></code>
|
|
224
|
+
|
|
225
|
+
**Since:** 8.0.0
|
|
226
|
+
|
|
227
|
+
--------------------
|
|
228
|
+
|
|
229
|
+
|
|
230
|
+
### Interfaces
|
|
231
|
+
|
|
232
|
+
|
|
233
|
+
#### StartWatchResult
|
|
234
|
+
|
|
235
|
+
Result returned when SMS Retriever watching starts.
|
|
236
|
+
|
|
237
|
+
| Prop | Type | Description | Since |
|
|
238
|
+
| ------------ | --------------------------------------------------------------------------- | ---------------------------------- | ----- |
|
|
239
|
+
| **`status`** | <code><a href="#smsretrieverstartstatus">SmsRetrieverStartStatus</a></code> | Native status returned by Android. | 8.0.0 |
|
|
240
|
+
|
|
241
|
+
|
|
242
|
+
#### StopWatchResult
|
|
243
|
+
|
|
244
|
+
Result returned when SMS Retriever watching stops.
|
|
245
|
+
|
|
246
|
+
| Prop | Type | Description | Since |
|
|
247
|
+
| ------------ | --------------------------------- | ---------------------------------- | ----- |
|
|
248
|
+
| **`status`** | <code>'SMS_RETRIEVER_DONE'</code> | Native status returned by Android. | 8.0.0 |
|
|
249
|
+
|
|
250
|
+
|
|
251
|
+
#### GetHashStringResult
|
|
252
|
+
|
|
253
|
+
Result returned by {@link AndroidSmsRetrieverPlugin.getHashString}.
|
|
254
|
+
|
|
255
|
+
| Prop | Type | Description | Since |
|
|
256
|
+
| ---------- | ------------------- | ----------------------------------------------------------------------------- | ----- |
|
|
257
|
+
| **`hash`** | <code>string</code> | The 11-character app hash that must be appended to verification SMS messages. | 8.0.0 |
|
|
258
|
+
|
|
259
|
+
|
|
260
|
+
#### GetPhoneNumberResult
|
|
261
|
+
|
|
262
|
+
Result returned by {@link AndroidSmsRetrieverPlugin.getPhoneNumber}.
|
|
263
|
+
|
|
264
|
+
| Prop | Type | Description | Since |
|
|
265
|
+
| ----------------- | ------------------- | ---------------------------------------------------------------------- | ----- |
|
|
266
|
+
| **`phoneNumber`** | <code>string</code> | Phone number selected by the user from Android's Phone Number Hint UI. | 8.0.0 |
|
|
267
|
+
|
|
268
|
+
|
|
269
|
+
#### PluginListenerHandle
|
|
270
|
+
|
|
271
|
+
| Prop | Type |
|
|
272
|
+
| ------------ | ----------------------------------------- |
|
|
273
|
+
| **`remove`** | <code>() => Promise<void></code> |
|
|
274
|
+
|
|
275
|
+
|
|
276
|
+
#### SmsReceivedEvent
|
|
277
|
+
|
|
278
|
+
SMS Retriever message event payload.
|
|
279
|
+
|
|
280
|
+
| Prop | Type | Description | Since |
|
|
281
|
+
| ------------- | ------------------- | ---------------------------------------------------------------------- | ----- |
|
|
282
|
+
| **`message`** | <code>string</code> | Full verification SMS message received by Android's SMS Retriever API. | 8.0.0 |
|
|
283
|
+
|
|
284
|
+
|
|
285
|
+
#### SmsRetrieverErrorEvent
|
|
286
|
+
|
|
287
|
+
SMS Retriever error event payload.
|
|
288
|
+
|
|
289
|
+
| Prop | Type | Description | Since |
|
|
290
|
+
| ------------- | ------------------- | ---------------------------------------------------------- | ----- |
|
|
291
|
+
| **`message`** | <code>string</code> | Error message returned by Android or Google Play services. | 8.0.0 |
|
|
292
|
+
|
|
293
|
+
|
|
294
|
+
#### PluginVersionResult
|
|
295
|
+
|
|
296
|
+
Plugin version payload.
|
|
297
|
+
|
|
298
|
+
| Prop | Type | Description | Since |
|
|
299
|
+
| ------------- | ------------------- | ----------------------------------------------------------- | ----- |
|
|
300
|
+
| **`version`** | <code>string</code> | Version identifier returned by the platform implementation. | 8.0.0 |
|
|
301
|
+
|
|
302
|
+
|
|
303
|
+
### Type Aliases
|
|
304
|
+
|
|
305
|
+
|
|
306
|
+
#### SmsRetrieverStartStatus
|
|
307
|
+
|
|
308
|
+
Status values returned by {@link AndroidSmsRetrieverPlugin.startWatch}.
|
|
309
|
+
|
|
310
|
+
<code>'SMS_RETRIEVER_STARTED' | 'SMS_RETRIEVER_ALREADY_STARTED'</code>
|
|
311
|
+
|
|
312
|
+
</docgen-api>
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
ext {
|
|
2
|
+
junitVersion = project.hasProperty('junitVersion') ? rootProject.ext.junitVersion : '4.13.2'
|
|
3
|
+
androidxAppCompatVersion = project.hasProperty('androidxAppCompatVersion') ? rootProject.ext.androidxAppCompatVersion : '1.7.1'
|
|
4
|
+
androidxJunitVersion = project.hasProperty('androidxJunitVersion') ? rootProject.ext.androidxJunitVersion : '1.3.0'
|
|
5
|
+
androidxEspressoCoreVersion = project.hasProperty('androidxEspressoCoreVersion') ? rootProject.ext.androidxEspressoCoreVersion : '3.7.0'
|
|
6
|
+
playServicesAuthVersion = project.hasProperty('playServicesAuthVersion') ? rootProject.ext.playServicesAuthVersion : '21.5.1'
|
|
7
|
+
playServicesAuthApiPhoneVersion = project.hasProperty('playServicesAuthApiPhoneVersion') ? rootProject.ext.playServicesAuthApiPhoneVersion : '18.3.0'
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
buildscript {
|
|
11
|
+
repositories {
|
|
12
|
+
google()
|
|
13
|
+
mavenCentral()
|
|
14
|
+
}
|
|
15
|
+
dependencies {
|
|
16
|
+
classpath 'com.android.tools.build:gradle:8.13.0'
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
apply plugin: 'com.android.library'
|
|
21
|
+
|
|
22
|
+
android {
|
|
23
|
+
namespace = "app.capgo.androidsmsretriever"
|
|
24
|
+
compileSdk = project.hasProperty('compileSdkVersion') ? rootProject.ext.compileSdkVersion : 36
|
|
25
|
+
defaultConfig {
|
|
26
|
+
minSdkVersion project.hasProperty('minSdkVersion') ? rootProject.ext.minSdkVersion : 24
|
|
27
|
+
targetSdkVersion project.hasProperty('targetSdkVersion') ? rootProject.ext.targetSdkVersion : 36
|
|
28
|
+
versionCode 1
|
|
29
|
+
versionName "1.0"
|
|
30
|
+
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
|
|
31
|
+
}
|
|
32
|
+
buildTypes {
|
|
33
|
+
release {
|
|
34
|
+
minifyEnabled false
|
|
35
|
+
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
lint {
|
|
39
|
+
abortOnError = false
|
|
40
|
+
}
|
|
41
|
+
compileOptions {
|
|
42
|
+
sourceCompatibility JavaVersion.VERSION_21
|
|
43
|
+
targetCompatibility JavaVersion.VERSION_21
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
repositories {
|
|
48
|
+
google()
|
|
49
|
+
mavenCentral()
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
|
|
53
|
+
dependencies {
|
|
54
|
+
implementation fileTree(dir: 'libs', include: ['*.jar'])
|
|
55
|
+
implementation project(':capacitor-android')
|
|
56
|
+
annotationProcessor project(':capacitor-android')
|
|
57
|
+
implementation "androidx.appcompat:appcompat:$androidxAppCompatVersion"
|
|
58
|
+
implementation "com.google.android.gms:play-services-auth:$playServicesAuthVersion"
|
|
59
|
+
implementation "com.google.android.gms:play-services-auth-api-phone:$playServicesAuthApiPhoneVersion"
|
|
60
|
+
testImplementation "junit:junit:$junitVersion"
|
|
61
|
+
androidTestImplementation "androidx.test.ext:junit:$androidxJunitVersion"
|
|
62
|
+
androidTestImplementation "androidx.test.espresso:espresso-core:$androidxEspressoCoreVersion"
|
|
63
|
+
}
|
|
@@ -0,0 +1,137 @@
|
|
|
1
|
+
package app.capgo.androidsmsretriever;
|
|
2
|
+
|
|
3
|
+
import android.content.Context;
|
|
4
|
+
import android.content.IntentFilter;
|
|
5
|
+
import android.os.Build;
|
|
6
|
+
import com.google.android.gms.auth.api.phone.SmsRetriever;
|
|
7
|
+
import com.google.android.gms.auth.api.phone.SmsRetrieverClient;
|
|
8
|
+
import com.google.android.gms.tasks.Task;
|
|
9
|
+
import java.util.ArrayList;
|
|
10
|
+
|
|
11
|
+
public class AndroidSmsRetriever {
|
|
12
|
+
|
|
13
|
+
public interface StartWatchCallback {
|
|
14
|
+
void success(String status);
|
|
15
|
+
void error(String message, Exception exception);
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
public interface SmsRetrieverListener {
|
|
19
|
+
void onSmsReceived(String message);
|
|
20
|
+
void onTimeout();
|
|
21
|
+
void onError(String message);
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
private static final String STATUS_STARTED = "SMS_RETRIEVER_STARTED";
|
|
25
|
+
private static final String STATUS_ALREADY_STARTED = "SMS_RETRIEVER_ALREADY_STARTED";
|
|
26
|
+
|
|
27
|
+
private final Context applicationContext;
|
|
28
|
+
private SmsBroadcastReceiver smsBroadcastReceiver;
|
|
29
|
+
private SmsRetrieverListener listener;
|
|
30
|
+
|
|
31
|
+
public AndroidSmsRetriever(Context context) {
|
|
32
|
+
this.applicationContext = context.getApplicationContext();
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
public void setListener(SmsRetrieverListener listener) {
|
|
36
|
+
this.listener = listener;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
public void startWatch(StartWatchCallback callback) {
|
|
40
|
+
if (smsBroadcastReceiver != null) {
|
|
41
|
+
callback.success(STATUS_ALREADY_STARTED);
|
|
42
|
+
return;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
smsBroadcastReceiver = new SmsBroadcastReceiver();
|
|
46
|
+
smsBroadcastReceiver.setListener(
|
|
47
|
+
new SmsBroadcastReceiver.Listener() {
|
|
48
|
+
@Override
|
|
49
|
+
public void onSmsReceived(String message) {
|
|
50
|
+
if (listener != null) {
|
|
51
|
+
listener.onSmsReceived(message);
|
|
52
|
+
}
|
|
53
|
+
unregisterReceiver();
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
@Override
|
|
57
|
+
public void onTimeout() {
|
|
58
|
+
if (listener != null) {
|
|
59
|
+
listener.onTimeout();
|
|
60
|
+
}
|
|
61
|
+
unregisterReceiver();
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
@Override
|
|
65
|
+
public void onError(String message) {
|
|
66
|
+
if (listener != null) {
|
|
67
|
+
listener.onError(message);
|
|
68
|
+
}
|
|
69
|
+
unregisterReceiver();
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
);
|
|
73
|
+
|
|
74
|
+
SmsRetrieverClient smsRetrieverClient = SmsRetriever.getClient(applicationContext);
|
|
75
|
+
Task<Void> task = smsRetrieverClient.startSmsRetriever();
|
|
76
|
+
|
|
77
|
+
task.addOnSuccessListener((aVoid) -> {
|
|
78
|
+
try {
|
|
79
|
+
registerReceiver();
|
|
80
|
+
callback.success(STATUS_STARTED);
|
|
81
|
+
} catch (Exception exception) {
|
|
82
|
+
unregisterReceiver();
|
|
83
|
+
callback.error("Unable to register SMS Retriever receiver.", exception);
|
|
84
|
+
}
|
|
85
|
+
});
|
|
86
|
+
|
|
87
|
+
task.addOnFailureListener((exception) -> {
|
|
88
|
+
unregisterReceiver();
|
|
89
|
+
callback.error("Unable to start SMS Retriever.", exception);
|
|
90
|
+
});
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
public void stopWatch() {
|
|
94
|
+
unregisterReceiver();
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
public String getHashString() {
|
|
98
|
+
ArrayList<String> appCodes = new AppSignatureHashHelper(applicationContext).getAppSignatures();
|
|
99
|
+
if (appCodes.isEmpty() || appCodes.get(0) == null) {
|
|
100
|
+
throw new IllegalStateException("Unable to find package signature hash.");
|
|
101
|
+
}
|
|
102
|
+
return appCodes.get(0);
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
private void registerReceiver() {
|
|
106
|
+
if (smsBroadcastReceiver == null) {
|
|
107
|
+
throw new IllegalStateException("SMS Retriever receiver is not initialized.");
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
IntentFilter intentFilter = new IntentFilter(SmsRetriever.SMS_RETRIEVED_ACTION);
|
|
111
|
+
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
|
|
112
|
+
applicationContext.registerReceiver(
|
|
113
|
+
smsBroadcastReceiver,
|
|
114
|
+
intentFilter,
|
|
115
|
+
SmsRetriever.SEND_PERMISSION,
|
|
116
|
+
null,
|
|
117
|
+
Context.RECEIVER_EXPORTED
|
|
118
|
+
);
|
|
119
|
+
} else {
|
|
120
|
+
applicationContext.registerReceiver(smsBroadcastReceiver, intentFilter, SmsRetriever.SEND_PERMISSION, null);
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
private void unregisterReceiver() {
|
|
125
|
+
if (smsBroadcastReceiver == null) {
|
|
126
|
+
return;
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
try {
|
|
130
|
+
applicationContext.unregisterReceiver(smsBroadcastReceiver);
|
|
131
|
+
} catch (IllegalArgumentException ignored) {
|
|
132
|
+
// Receiver was already unregistered by Android.
|
|
133
|
+
} finally {
|
|
134
|
+
smsBroadcastReceiver = null;
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
}
|