skip to Main Content

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.

4

Answers


  1. Chosen as BEST ANSWER

    So far what i have found out is using google service to get all of the user emails without any permission with the following code :

    private static final int RC_SIGN_IN = 100;
    

    Inside OnCreate() or Button click :

    AuthUI.SignInIntentBuilder builder = AuthUI.getInstance().createSignInIntentBuilder();
    
    if (getSelectedTosUrl() != null && getSelectedPrivacyPolicyUrl() != null) {
        builder.setTosAndPrivacyPolicyUrls(getSelectedTosUrl(), getSelectedPrivacyPolicyUrl());
    }
    builder.build();
    startActivityForResult(builder.build(), RC_SIGN_IN);
    

    And to get results

     @Nullable
        private String getSelectedPrivacyPolicyUrl() {
    
            return null;
        }
    
        @Nullable
        private String getSelectedTosUrl() {
    
            return null;
        }
    
        @Override
        public void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
            super.onActivityResult(requestCode, resultCode, data);
            if (requestCode == RC_SIGN_IN) {
                handleSignInResponse(resultCode, data);
            }
        }
    
        private void handleSignInResponse(int resultCode, @Nullable Intent data) {
            IdpResponse response = IdpResponse.fromResultIntent(data);
    
            // Successfully signed in
            if (resultCode == RESULT_OK) {
    //            startSignedInActivity(response);
    //            finish();
                Log.e(TAG, String.valueOf(response));
    
            } else {
                // Sign in failed
                if (response == null) {
                    // User pressed back button
    //                showSnackbar(R.string.sign_in_cancelled);
                    Log.e(TAG, "Sign-in error: failed");
    
                    return;
                }
    
                if (response.getError().getErrorCode() == ErrorCodes.NO_NETWORK) {
    //                showSnackbar(R.string.no_internet_connection);
                    Log.e(TAG, "Sign-in error: no net");
                    return;
                }
    
    //            showSnackbar(R.string.unknown_error);
                Log.e(TAG, "Sign-in error: ", response.getError());
            }
        }
    

    But my problem is that the user email dialog open in pre made sign in activty, but i would like to use my own. Any tips ? here is the source google library


  2. its not possible to get access user confidential information (like email id ) without runtime Permissions.

    Login or Signup to reply.
  3. Maybe this is not what you are looking for, but I really recommend that you take a look to Authentication with Firebase and the helper library FirebaseUI. They have all that boilerplate already implemented and it is really easy to config, customize and setup.

    Login or Signup to reply.
  4. 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

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search