Skip to content

Commit

Permalink
Phone number hint API update (#36)
Browse files Browse the repository at this point in the history
* fix: Android dependencies

Increased targetSdkVerison up to 28. Changed package name in MainActivity

Changed applicationId in buildGradle.
Names now match manifest names

* feat: Api updated

Provided usage of new Android API for phone nuber hint

* chore: returned example orig code

* fix: build fix

Increased compileSdkVersion, added targetSdkVersion

Replaced depracted jscenter repository with mavenCentral

* chore: lastResilt assign fix

* fix: changelog update

Version code increased

* chore: Added comment
  • Loading branch information
feduke-nukem authored Feb 27, 2023
1 parent 06a23f6 commit 90a42b3
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 23 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
# Changelog
## 3.0.0
* Breaking: Upgraded phone number hint API. Now may require updating Google Services on the device.


## 2.1.1

Expand Down
11 changes: 7 additions & 4 deletions android/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ buildscript {
ext.kotlin_version = '1.3.50'
repositories {
google()
jcenter()
mavenCentral()
}

dependencies {
Expand All @@ -17,21 +17,22 @@ buildscript {
rootProject.allprojects {
repositories {
google()
jcenter()
mavenCentral()
}
}

apply plugin: 'com.android.library'
apply plugin: 'kotlin-android'

android {
compileSdkVersion 28
compileSdkVersion 31

sourceSets {
main.java.srcDirs += 'src/main/kotlin'
}
defaultConfig {
minSdkVersion 19
targetSdkVersion 31
}
lintOptions {
disable 'InvalidPackage'
Expand All @@ -40,6 +41,8 @@ android {

dependencies {
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
implementation 'com.google.android.gms:play-services-auth:17.0.0'
implementation 'com.google.android.gms:play-services-auth:20.3.0'
implementation 'com.google.android.gms:play-services-auth-api-phone:17.4.0'
implementation "androidx.activity:activity:1.5.1"
implementation "androidx.fragment:fragment:1.5.4"
}
40 changes: 22 additions & 18 deletions android/src/main/kotlin/ru/surfstudio/otp_autofill/OTPPlugin.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ import android.app.Activity
import android.content.Context
import android.content.Intent
import android.content.IntentFilter
import androidx.activity.result.IntentSenderRequest.*
import androidx.annotation.NonNull;
import com.google.android.gms.auth.api.credentials.Credential
import com.google.android.gms.auth.api.credentials.Credentials
import com.google.android.gms.auth.api.credentials.HintRequest
import com.google.android.gms.auth.api.identity.GetPhoneNumberHintIntentRequest
import com.google.android.gms.auth.api.identity.Identity
import com.google.android.gms.auth.api.phone.SmsRetriever

import io.flutter.embedding.engine.plugins.FlutterPlugin
Expand Down Expand Up @@ -45,6 +45,7 @@ public class OTPPlugin : FlutterPlugin, MethodCallHandler, PluginRegistry.Activi
private var smsUserConsentBroadcastReceiver: SmsUserConsentReceiver? = null
private var smsRetrieverBroadcastReceiver: SmsRetrieverReceiver? = null
private var activity: Activity? = null
private val request = GetPhoneNumberHintIntentRequest.builder().build()

override fun onAttachedToEngine(@NonNull flutterPluginBinding: FlutterPlugin.FlutterPluginBinding) {
context = flutterPluginBinding.applicationContext
Expand Down Expand Up @@ -93,21 +94,23 @@ public class OTPPlugin : FlutterPlugin, MethodCallHandler, PluginRegistry.Activi

private fun showNumberHint(result: Result) {
lastResult = result
if (activity != null) {
val hintRequest = HintRequest.Builder()
.setPhoneNumberIdentifierSupported(true)
.build()
val credentialsClient = Credentials.getClient(activity!!)
val intent = credentialsClient.getHintPickerIntent(hintRequest)
activity!!.startIntentSenderForResult(
intent.intentSender,
credentialPickerRequest,
null, 0, 0, 0
)
}


if(activity == null) return

// if activity is not null will build 'show hint' intent
// on success will start showing hint
Identity.getSignInClient(activity!!)
.getPhoneNumberHintIntent(request)
.addOnSuccessListener { res ->
res.intentSender
val request = Builder(res).build()

activity!!.startIntentSenderForResult(request.intentSender, credentialPickerRequest,
null, 0, 0, 0)
}
}


override fun onDetachedFromEngine(@NonNull binding: FlutterPlugin.FlutterPluginBinding) {
channel.setMethodCallHandler(null)
}
Expand All @@ -124,8 +127,9 @@ public class OTPPlugin : FlutterPlugin, MethodCallHandler, PluginRegistry.Activi
// Consent denied. User can type OTC manually.
}
credentialPickerRequest -> if (resultCode == Activity.RESULT_OK && data != null) {
val credential = data.getParcelableExtra<Credential>(Credential.EXTRA_KEY)
lastResult?.success(credential?.id)
val phoneNumber =
Identity.getSignInClient(context!!).getPhoneNumberFromIntent(data)
lastResult?.success(phoneNumber)
}
}
return true
Expand Down
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: otp_autofill
version: 2.1.1
version: 3.0.0
description: Android implementation of OTP autofill with using User Consent API and Retriever API. Add possibility to receive OTP code from another input.
repository: "https://github.com/surfstudio/flutter-otp-autofill"
issue_tracker: "https://github.com/surfstudio/flutter-otp-autofill/issues"
Expand Down

0 comments on commit 90a42b3

Please sign in to comment.