Skip to content
This repository has been archived by the owner on Dec 18, 2023. It is now read-only.

Commit

Permalink
Merge pull request #8 from Bruno-Furtado/feature-phone-number-listener
Browse files Browse the repository at this point in the history
Listener to get the phone number.
  • Loading branch information
Bruno Tortato Furtado committed Mar 9, 2019
2 parents 113f390 + 710cbbb commit cc42e71
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ final class PhoneNumberHelper {

private static final String ACTIVITY_NULL_ERROR_TYPE = "ACTIVITY_NULL_ERROR_TYPE";
private static final String ACTIVITY_RESULT_NOOK_ERROR_TYPE = "ACTIVITY_RESULT_NOOK_ERROR_TYPE";
private static final String CONNECTION_SUSPENENDED_ERROR_TYPE = "CONNECTION_SUSPENENDED_ERROR_TYPE";
private static final String CONNECTION_SUSPENDED_ERROR_TYPE = "CONNECTION_SUSPENDED_ERROR_TYPE";
private static final String CONNECTION_FAILED_ERROR_TYPE = "CONNECTION_FAILED_ERROR_TYPE";
private static final String SEND_INTENT_ERROR_TYPE = "SEND_INTENT_ERROR_TYPE";

Expand All @@ -37,6 +37,7 @@ final class PhoneNumberHelper {

private GoogleApiClient mGoogleApiClient;
private Promise mPromise;
private Listener mListener;


PhoneNumberHelper() { }
Expand All @@ -47,25 +48,33 @@ ActivityEventListener getActivityEventListener() {
return mActivityEventListener;
}

void setListener(@NonNull final Listener listener) {
mListener = listener;
}

void requestPhoneNumber(@NonNull final Context context, final Activity activity, final Promise promise) {
if (promise == null) {
callAndResetListener();
return;
}

mPromise = promise;

if (!GooglePlayServicesHelper.isAvailable(context)) {
promiseReject(GooglePlayServicesHelper.UNAVAILABLE_ERROR_TYPE, GooglePlayServicesHelper.UNAVAILABLE_ERROR_MESSAGE);
callAndResetListener();
return;
}

if (!GooglePlayServicesHelper.hasSupportedVersion(context)) {
promiseReject(GooglePlayServicesHelper.UNSUPORTED_VERSION_ERROR_TYPE, GooglePlayServicesHelper.UNSUPORTED_VERSION_ERROR_MESSAGE);
callAndResetListener();
return;
}

if (activity == null) {
promiseReject(ACTIVITY_NULL_ERROR_TYPE, ACTIVITY_NULL_ERROR_MESSAGE);
callAndResetListener();
return;
}

Expand All @@ -83,6 +92,7 @@ void requestPhoneNumber(@NonNull final Context context, final Activity activity,
REQUEST_PHONE_NUMBER_REQUEST_CODE, null, 0, 0, 0);
} catch (IntentSender.SendIntentException e) {
promiseReject(SEND_INTENT_ERROR_TYPE, SEND_INTENT_ERROR_MESSAGE);
callAndResetListener();
}
}

Expand All @@ -108,6 +118,13 @@ private GoogleApiClient getGoogleApiClient(@NonNull final Context context, final
return mGoogleApiClient;
}

private void callAndResetListener() {
if (mListener != null) {
mListener.phoneNumberResultReceived();
mListener = null;
}
}

//endregion

//region - Promises
Expand Down Expand Up @@ -136,14 +153,16 @@ public void onConnected(@Nullable Bundle bundle) { }

@Override
public void onConnectionSuspended(int i) {
promiseReject(CONNECTION_SUSPENENDED_ERROR_TYPE, CONNECTION_SUSPENENDED_ERROR_MESSAGE);
promiseReject(CONNECTION_SUSPENDED_ERROR_TYPE, CONNECTION_SUSPENENDED_ERROR_MESSAGE);
callAndResetListener();
}
};

private final GoogleApiClient.OnConnectionFailedListener mApiClientOnConnectionFailedListener = new GoogleApiClient.OnConnectionFailedListener() {
@Override
public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {
promiseReject(CONNECTION_FAILED_ERROR_TYPE, CONNECTION_FAILED_ERROR_MESSAGE);
callAndResetListener();
}
};

Expand All @@ -157,14 +176,26 @@ public void onActivityResult(Activity activity, int requestCode, int resultCode,
final Credential credential = data.getParcelableExtra(Credential.EXTRA_KEY);
final String phoneNumber = credential.getId();
promiseResolve(phoneNumber);
callAndResetListener();
return;
}
}

promiseReject(ACTIVITY_RESULT_NOOK_ERROR_TYPE, ACTIVITY_RESULT_NOOK_ERROR_MESSAGE);
callAndResetListener();
}
};

//endregion

//region - Classes

public interface Listener {

void phoneNumberResultReceived();

}

//endregion

}
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package me.furtado.smsretriever;

import android.app.Activity;
import android.content.Context;
import android.support.annotation.NonNull;

import com.facebook.react.bridge.ActivityEventListener;
Expand All @@ -17,11 +16,7 @@ final class RNSmsRetrieverModule extends ReactContextBaseJavaModule {

RNSmsRetrieverModule(@NonNull final ReactApplicationContext context) {
super(context);

mSmsHelper = new SmsHelper(context);

final ActivityEventListener eventListener = mPhoneNumberHelper.getActivityEventListener();
context.addActivityEventListener(eventListener);
}

//region - ReactContextBaseJavaModule
Expand All @@ -39,8 +34,19 @@ public String getName() {
@SuppressWarnings("unused")
@ReactMethod
public void requestPhoneNumber(final Promise promise) {
final Context context = getReactApplicationContext();
final ReactApplicationContext context = getReactApplicationContext();
final Activity activity = getCurrentActivity();
final ActivityEventListener eventListener = mPhoneNumberHelper.getActivityEventListener();

context.addActivityEventListener(eventListener);

mPhoneNumberHelper.setListener(new PhoneNumberHelper.Listener() {
@Override
public void phoneNumberResultReceived() {
context.removeActivityEventListener(eventListener);
}
});

mPhoneNumberHelper.requestPhoneNumber(context, activity, promise);
}

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "react-native-sms-retriever",
"version": "1.0.2",
"version": "1.0.3",
"description": "Android SMS Retriever API for React Native",
"homepage": "https://github.com/Bruno-Furtado/react-native-sms-retriever",
"readme": "https://github.com/Bruno-Furtado/react-native-sms-retriever#readme",
Expand Down

0 comments on commit cc42e71

Please sign in to comment.