Search code examples
androidfirebase-authenticationandroid-permissionsuser-permissionsshopify-app

Get the user email firebase auth


Im trying to do something like this

shopify screenshot i have found this pop up dialog in a few apps like shopify app and discord app , once you open sign up or login page dialog pop up with all possible emails.

Now my question how to achieve something like this without asking the user for contact permission.

My current Code is :

private void addAdapterToViews() {
    Account[] accounts = AccountManager.get(getActivity()).getAccounts();
    Set<String> emailSet = new HashSet<String>();
    for (Account account : accounts) {
        if (EMAIL_PATTERN.matcher(account.name).matches()) {
            emailSet.add(account.name);
        }
    }
    binding.autoemail.setAdapter(new ArrayAdapter<String>(getActivity(), android.R.layout.simple_dropdown_item_1line, new ArrayList<String>(emailSet)));
}

private boolean mayRequestContacts() {
    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) {
        return true;
    }
    if (getActivity().checkSelfPermission(READ_CONTACTS) == PackageManager.PERMISSION_GRANTED) {
        return true;
    }
    if (shouldShowRequestPermissionRationale(READ_CONTACTS)) {
        requestPermissions(new String[]{READ_CONTACTS}, REQUEST_READ_CONTACTS);
        Snackbar.make(binding.autoemail, R.string.permission_rationale, Snackbar.LENGTH_INDEFINITE)
                .setAction(android.R.string.ok, new View.OnClickListener() {
                    @Override
                    @TargetApi(Build.VERSION_CODES.M)
                    public void onClick(View v) {
                        requestPermissions(new String[]{READ_CONTACTS}, REQUEST_READ_CONTACTS);
                    }
                });
    } else {
        requestPermissions(new String[]{READ_CONTACTS}, REQUEST_READ_CONTACTS);
    }
    return false;
}

@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions,
                                       @NonNull int[] grantResults) {
    if (requestCode == REQUEST_READ_CONTACTS) {
        if (grantResults.length == 1 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
            addAdapterToViews();
        }
    }
}

This code is working fine for me once the user try to type any letters in email edittext if permission is granted.


Solution

  • Credentials API to retrieve sign-in hints, such as the user's name and email address.

    On Android 6.0 (Marshmallow) and newer, your app does not need to request any device or runtime permissions to retrieve sign-in hints with the Credentials API

    HintRequest hintRequest = new HintRequest.Builder()
            .setHintPickerConfig(new CredentialPickerConfig.Builder()
                    .setShowCancelButton(true)
                    .build())
            .setEmailAddressIdentifierSupported(true)
            .setAccountTypes(IdentityProviders.GOOGLE)
            .build();
    
    PendingIntent intent = mCredentialsClient.getHintPickerIntent(hintRequest);
    try {
        startIntentSenderForResult(intent.getIntentSender(), RC_HINT, null, 0, 0, 0);
    } catch (IntentSender.SendIntentException e) {
        Log.e(TAG, "Could not start hint picker Intent", e);
    }
    

    HintRequest Builder Google Documentation