Java Code Examples for android.accounts.AccountManager#newChooseAccountIntent()

The following examples show how to use android.accounts.AccountManager#newChooseAccountIntent() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: SignInActivity.java    From ribot-app-android with Apache License 2.0 5 votes vote down vote up
/***** Private helper methods  *****/

    private void triggerAccountPicker() {
        // We need to use the deprecated method because the other one is only supported in API
        // level 23
        setSignInButtonEnabled(false);
        Intent intent = AccountManager.newChooseAccountIntent(null, null,
                new String[]{ACCOUNT_TYPE_GOOGLE}, true, getString(R.string.text_ribot_account),
                null, null, null);
        startActivityForResult(intent, REQUEST_CODE_ACCOUNT_PICKER);
    }
 
Example 2
Source File: AccountImporter.java    From Android-SingleSignOn with GNU General Public License v3.0 5 votes vote down vote up
public static void pickNewAccount(Activity activity) throws NextcloudFilesAppNotInstalledException,
        AndroidGetAccountsPermissionNotGranted {
    checkAndroidAccountPermissions(activity);
    
    if (appInstalledOrNot(activity)) {
        Intent intent = AccountManager.newChooseAccountIntent(null, null, ACCOUNT_TYPES,
                                                              true, null, null, null, null);
        activity.startActivityForResult(intent, CHOOSE_ACCOUNT_SSO);
    } else {
        throw new NextcloudFilesAppNotInstalledException();
    }
}
 
Example 3
Source File: AccountImporter.java    From Android-SingleSignOn with GNU General Public License v3.0 5 votes vote down vote up
public static void pickNewAccount(Fragment fragment) throws NextcloudFilesAppNotInstalledException,
        AndroidGetAccountsPermissionNotGranted {
    checkAndroidAccountPermissions(fragment.getContext());
    
    if (appInstalledOrNot(fragment.getContext())) {
        Intent intent = AccountManager.newChooseAccountIntent(null, null, ACCOUNT_TYPES,
                                                              true, null, null, null, null);
        fragment.startActivityForResult(intent, CHOOSE_ACCOUNT_SSO);
    } else {
        throw new NextcloudFilesAppNotInstalledException();
    }
}
 
Example 4
Source File: StaleListBroadcastReceiver.java    From opentasks with Apache License 2.0 4 votes vote down vote up
@Override
public void onReceive(Context context, Intent intent)
{
    if (Build.VERSION.SDK_INT < 26)
    {
        // this receiver is Android 8+ only
        return;
    }
    AccountManager accountManager = AccountManager.get(context);
    String authority = AuthorityUtil.taskAuthority(context);
    String description = String.format("Please give %s access to the following account", new ManifestAppName(context).value());
    // request access to each account we don't know yet individually
    for (Intent accountRequestIntent :
            new Mapped<>(
                    account -> AccountManager.newChooseAccountIntent(account, new ArrayList<Account>(singletonList(account)), null,
                            description, null,
                            null, null),
                    new Mapped<>(
                            this::account,
                            new Mapped<>(RowSnapshot::values,
                                    new QueryRowSet<>(
                                            new TaskListsView(authority, context.getContentResolver().acquireContentProviderClient(authority)),
                                            new MultiProjection<>(TaskContract.TaskLists.ACCOUNT_NAME, TaskContract.TaskLists.ACCOUNT_TYPE),
                                            new Not<>(new AnyOf<>(
                                                    new Joined<>(
                                                            new Seq<>(new EqArg<>(TaskContract.TaskLists.ACCOUNT_TYPE, TaskContract.LOCAL_ACCOUNT_TYPE)),
                                                            new Mapped<>(AccountEq::new, new Seq<>(accountManager.getAccounts()))))))))))
    {
        if (Build.VERSION.SDK_INT < 28)
        {
            context.startActivity(accountRequestIntent);
        }
        else
        {
            // on newer Android versions post a notification instead because we can't launch activities from the background anymore
            String notificationDescription = String.format("%s needs your permission", new ManifestAppName(context).value());
            NotificationManager nm = context.getSystemService(NotificationManager.class);
            if (nm != null)
            {
                NotificationChannel errorChannel = new NotificationChannel("provider_messages", "Sync Messages", NotificationManager.IMPORTANCE_HIGH);
                nm.createNotificationChannel(errorChannel);
                Resources.Theme theme = context.getTheme();
                theme.applyStyle(context.getApplicationInfo().theme, true);

                nm.notify("stale_list_broadacast", 0,
                        new Notification.Builder(context, "provider_messages")
                                .setContentText(notificationDescription)
                                .setContentIntent(PendingIntent.getActivity(context, 0, accountRequestIntent, PendingIntent.FLAG_UPDATE_CURRENT))
                                .addAction(new Notification.Action.Builder(null, "Grant",
                                        PendingIntent.getActivity(context, 0, accountRequestIntent, PendingIntent.FLAG_UPDATE_CURRENT)).build())
                                .setColor(new AttributeColor(theme, R.attr.colorPrimary).argb())
                                .setColorized(true)
                                .setSmallIcon(R.drawable.ic_24_opentasks)
                                .build());
            }
        }
    }
}
 
Example 5
Source File: AccountChooser.java    From Android-AccountChooser with Apache License 2.0 2 votes vote down vote up
/**
 * Returns an intent to an {@link Activity} that prompts the user to choose
 * from a list of accounts. The caller will then typically start the
 * activity by calling <code>startActivityWithResult(intent, ...);</code>.
 * <p/>
 * On success the activity returns a Bundle with the account name and type
 * specified using keys {@link AccountManager#KEY_ACCOUNT_NAME} and
 * {@link AccountManager#KEY_ACCOUNT_TYPE}.
 * <p/>
 * The most common case is to call this with one account type, e.g.:
 * <p/>
 * 
 * <pre>
 * newChooseAccountsIntent(null, null, new String[] { &quot;com.google&quot; }, false, null,
 * 		null, null, null);
 * </pre>
 * 
 * @param selectedAccount
 *            if specified, indicates that the
 *            {@link android.accounts.Account} is the currently selected
 *            one, according to the caller's definition of selected.
 * @param allowableAccounts
 *            an optional {@link java.util.ArrayList} of accounts that are
 *            allowed to be shown. If not specified then this field will not
 *            limit the displayed accounts.
 * @param allowableAccountTypes
 *            an optional string array of account types. These are used both
 *            to filter the shown accounts and to filter the list of account
 *            types that are shown when adding an account.
 * @param alwaysPromptForAccount
 *            if set the account chooser screen is always shown, otherwise
 *            it is only shown when there is more than one account from
 *            which to choose
 * @param descriptionOverrideText
 *            if non-null this string is used as the description in the
 *            accounts chooser screen rather than the default
 * @param addAccountAuthTokenType
 *            this string is passed as the {@link AccountManager#addAccount}
 *            authTokenType parameter
 * @param addAccountRequiredFeatures
 *            this string array is passed as the
 *            {@link AccountManager#addAccount} requiredFeatures parameter
 * @param addAccountOptions
 *            This {@link android.os.Bundle} is passed as the
 *            {@link AccountManager#addAccount} options parameter
 * @param context
 *            The Context of the calling app
 * 
 * @return an {@link Intent} that can be used to launch the ChooseAccount
 *         activity flow.
 */
@SuppressLint("NewApi")
public static Intent newChooseAccountIntent(Account selectedAccount,
		ArrayList<Account> allowableAccounts,
		String[] allowableAccountTypes, boolean alwaysPromptForAccount,
		String descriptionOverrideText, String addAccountAuthTokenType,
		String[] addAccountRequiredFeatures, Bundle addAccountOptions,
		Context context) {

	if (Build.VERSION.SDK_INT > Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
		return AccountManager.newChooseAccountIntent(selectedAccount,
				allowableAccounts, allowableAccountTypes,
				alwaysPromptForAccount, descriptionOverrideText,
				addAccountAuthTokenType, addAccountRequiredFeatures,
				addAccountOptions);
	}

	Intent intent = new Intent();
	intent.setClassName(context,
			ChooseTypeAndAccountActivity.class.getName());

	intent.putExtra(
			ChooseTypeAndAccountActivity.EXTRA_ALLOWABLE_ACCOUNTS_ARRAYLIST,
			allowableAccounts);
	intent.putExtra(
			ChooseTypeAndAccountActivity.EXTRA_ALLOWABLE_ACCOUNT_TYPES_STRING_ARRAY,
			allowableAccountTypes);
	intent.putExtra(
			ChooseTypeAndAccountActivity.EXTRA_ADD_ACCOUNT_OPTIONS_BUNDLE,
			addAccountOptions);
	intent.putExtra(ChooseTypeAndAccountActivity.EXTRA_SELECTED_ACCOUNT,
			selectedAccount);
	intent.putExtra(
			ChooseTypeAndAccountActivity.EXTRA_ALWAYS_PROMPT_FOR_ACCOUNT,
			alwaysPromptForAccount);
	intent.putExtra(
			ChooseTypeAndAccountActivity.EXTRA_DESCRIPTION_TEXT_OVERRIDE,
			descriptionOverrideText);
	intent.putExtra(
			ChooseTypeAndAccountActivity.EXTRA_ADD_ACCOUNT_AUTH_TOKEN_TYPE_STRING,
			addAccountAuthTokenType);
	intent.putExtra(
			ChooseTypeAndAccountActivity.EXTRA_ADD_ACCOUNT_REQUIRED_FEATURES_STRING_ARRAY,
			addAccountRequiredFeatures);
	return intent;
}