Skip to content

Commit

Permalink
Merge pull request #2926 from dimagi/jp/CCCT-627-country-code-issue
Browse files Browse the repository at this point in the history
CCCT-627 || Country Code Issue
  • Loading branch information
OrangeAndGreen authored Dec 31, 2024
2 parents b4fe7c6 + 671347b commit 3f840bf
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container,
}
);

binding.countryCode.setText(PhoneNumberHelper.setDefaultCountryCode(getContext()));
binding.connectPrimaryPhoneInput.setOnFocusChangeListener(listener);
binding.countryCode.setOnFocusChangeListener(listener);

Expand Down
35 changes: 20 additions & 15 deletions app/src/org/commcare/utils/PhoneNumberHelper.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
package org.commcare.utils;

import android.app.Activity;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.content.IntentSender;
import android.widget.Toast;

import androidx.activity.result.ActivityResultLauncher;
import androidx.activity.result.IntentSenderRequest;
Expand All @@ -14,18 +11,9 @@
import com.google.android.gms.auth.api.identity.Identity;
import com.google.android.gms.auth.api.identity.SignInClient;
import com.google.android.gms.common.api.ApiException;
import com.google.android.gms.tasks.OnSuccessListener;

import org.commcare.android.database.connect.models.ConnectUserRecord;
import org.commcare.connect.ConnectConstants;
import org.commcare.connect.ConnectManager;
import org.commcare.connect.network.ApiConnectId;
import org.commcare.connect.network.IApiCallback;
import org.commcare.dalvik.R;
import org.javarosa.core.services.Logger;

import java.io.IOException;
import java.io.InputStream;

import java.util.Locale;

import io.michaelrocks.libphonenumber.android.NumberParseException;
Expand Down Expand Up @@ -95,20 +83,37 @@ public static int getCountryCode(Context context) {
return util.getCountryCodeForRegion(locale.getCountry());
}

public static String setDefaultCountryCode(Context context) {
Locale locale = context.getResources().getConfiguration().locale;
PhoneNumberUtil util = getUtil(context);

int code = util.getCountryCodeForRegion(locale.getCountry());

String codeText = "";
if (code > 0) {
codeText = String.format(Locale.getDefault(), "%d", code);
if (!codeText.startsWith("+")) {
codeText = "+" + codeText;
}
}

return codeText;
}

public static void requestPhoneNumberHint(Activity activity) {
GetPhoneNumberHintIntentRequest hintRequest = GetPhoneNumberHintIntentRequest.builder().build();
Identity.getSignInClient(activity).getPhoneNumberHintIntent(hintRequest)
.addOnSuccessListener(pendingIntent -> {
try {
IntentSenderRequest intentSenderRequest = new IntentSenderRequest.Builder(pendingIntent).build();
phoneNumberHintLauncher.launch(intentSenderRequest);
} catch (Exception e) {
} catch (Exception e) {
e.printStackTrace();
}
});
}

public static String handlePhoneNumberPickerResult(int requestCode, int resultCode, Intent intent, Activity activity){
public static String handlePhoneNumberPickerResult(int requestCode, int resultCode, Intent intent, Activity activity) {

if (requestCode == ConnectConstants.CREDENTIAL_PICKER_REQUEST && resultCode == Activity.RESULT_OK) {
SignInClient signInClient = Identity.getSignInClient(activity);
Expand Down

0 comments on commit 3f840bf

Please sign in to comment.