diff --git a/README.md b/README.md index 0cbf2c7..b7b50ef 100644 --- a/README.md +++ b/README.md @@ -40,6 +40,9 @@ yarn add react-native-otp-verify-remastered ```javascript import RNOtpVerify from 'react-native-otp-verify-remastered'; + +// THIS PACKAGE ONLY FOR ANDROID + getHash = () => RNOtpVerify.getHash() .then(console.log) diff --git a/android/src/main/java/com/faizal/OtpVerify/RNOtpVerifyModule.java b/android/src/main/java/com/faizal/OtpVerify/RNOtpVerifyModule.java index bd9cdbd..79717ab 100644 --- a/android/src/main/java/com/faizal/OtpVerify/RNOtpVerifyModule.java +++ b/android/src/main/java/com/faizal/OtpVerify/RNOtpVerifyModule.java @@ -22,8 +22,20 @@ import java.util.ArrayList; -public class RNOtpVerifyModule extends ReactContextBaseJavaModule implements LifecycleEventListener { +import android.app.Activity; +import android.app.PendingIntent; +import android.content.Intent; +import com.facebook.react.bridge.ActivityEventListener; +import com.google.android.gms.auth.api.Auth; +import com.google.android.gms.auth.api.credentials.Credential; +import com.google.android.gms.auth.api.credentials.HintRequest; +import com.google.android.gms.common.api.GoogleApiClient; + +public class RNOtpVerifyModule extends ReactContextBaseJavaModule implements LifecycleEventListener, ActivityEventListener { private static final String TAG = RNOtpVerifyModule.class.getSimpleName(); + private static final int RESOLVE_HINT = 10001; + private GoogleApiClient apiClient; + private Promise requestHintCallback; private final ReactApplicationContext reactContext; private BroadcastReceiver mReceiver; private boolean isReceiverRegistered = false; @@ -34,6 +46,11 @@ public RNOtpVerifyModule(ReactApplicationContext reactContext) { mReceiver = new OtpBroadcastReceiver(reactContext); getReactApplicationContext().addLifecycleEventListener(this); registerReceiverIfNecessary(mReceiver); + + reactContext.addActivityEventListener(this); + apiClient = new GoogleApiClient.Builder(reactContext) + .addApi(Auth.CREDENTIALS_API) + .build(); } @Override @@ -41,6 +58,27 @@ public String getName() { return "RNOtpVerify"; } + @ReactMethod + public void requestHint(Promise promise) { + Activity currentActivity = getCurrentActivity(); + requestHintCallback = promise; + + + if (currentActivity == null) { + requestHintCallback.reject("No Activity Found", "Current Activity Null."); + return; + } + try { + HintRequest hintRequest = new HintRequest.Builder().setPhoneNumberIdentifierSupported(true).build(); + PendingIntent intent = Auth.CredentialsApi.getHintPickerIntent(apiClient, hintRequest); + + currentActivity.startIntentSenderForResult(intent.getIntentSender(), RESOLVE_HINT, null, 0, 0, 0); + + } catch (Exception e) { + requestHintCallback.reject(e); + } + } + @ReactMethod public void getOtp(Promise promise) { requestOtp(promise); @@ -123,6 +161,22 @@ public void onHostDestroy() { unregisterReceiver(mReceiver); } + @Override + public void onActivityResult(Activity activity, int requestCode, int resultCode, Intent data) { + if (requestCode == RESOLVE_HINT) { + if (resultCode == Activity.RESULT_OK) { + Credential credential = data.getParcelableExtra(Credential.EXTRA_KEY); + // credential.getId(); <-- will need to process phone number string + requestHintCallback.resolve(credential.getId()); + } + } + } + + @Override + public void onNewIntent(Intent intent) { + + } + @ReactMethod public void addListener(String eventName) { // Keep: Required for RN built in Event Emitter Calls. diff --git a/dist/index.d.ts b/dist/index.d.ts index 341d439..847b2e2 100644 --- a/dist/index.d.ts +++ b/dist/index.d.ts @@ -1,6 +1,7 @@ interface OtpVerify { getOtp: () => Promise; getHash: () => Promise; + requestHint: () => Promise; addListener: (handler: (value: string) => any) => import("react-native").EmitterSubscription; removeListener: () => void; } diff --git a/dist/index.js b/dist/index.js index ccef21b..98dc319 100644 --- a/dist/index.js +++ b/dist/index.js @@ -5,6 +5,7 @@ var RNOtpVerify = react_native_1.NativeModules.RNOtpVerify; var OtpVerify = { getOtp: RNOtpVerify === null || RNOtpVerify === void 0 ? void 0 : RNOtpVerify.getOtp, getHash: RNOtpVerify === null || RNOtpVerify === void 0 ? void 0 : RNOtpVerify.getHash, + requestHint: RNOtpVerify === null || RNOtpVerify === void 0 ? void 0 : RNOtpVerify.requestHint, addListener: function (handler) { return react_native_1.DeviceEventEmitter .addListener('com.faizalshap.otpVerify:otpReceived', handler); diff --git a/index.ts b/index.ts index 3061fda..dd13731 100644 --- a/index.ts +++ b/index.ts @@ -5,6 +5,7 @@ const RNOtpVerify = NativeModules.RNOtpVerify; interface OtpVerify { getOtp: () => Promise; getHash: () => Promise; + requestHint: () => Promise; addListener: (handler: (value: string) => any) => import("react-native").EmitterSubscription; removeListener: () => void; } @@ -12,7 +13,7 @@ interface OtpVerify { const OtpVerify: OtpVerify = { getOtp: RNOtpVerify?.getOtp, getHash: RNOtpVerify?.getHash, - + requestHint: RNOtpVerify?.requestHint, addListener: (handler) => DeviceEventEmitter .addListener('com.faizalshap.otpVerify:otpReceived', handler),