java 物质主义#05

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java 物质主义#05相关的知识,希望对你有一定的参考价值。

    
    // remove this annotation we cannot recover the original warning
    @SuppressLint("MissingPermission")
    public static Pair<String, String> getCredentials(Context context) {
        String username = Preferences.getUsername(context);
        if (TextUtils.isEmpty(username)) {
            return null;
        }
        AccountManager accountManager = AccountManager.get(context);
        Account[] accounts = accountManager.getAccountsByType(BuildConfig.APPLICATION_ID);
        for (Account account : accounts) {
            if (TextUtils.equals(username, account.name)) {
                return Pair.create(username, accountManager.getPassword(account));
            }
        }
        return null;
    }
    
    //remove this annotation, we cannot recover this suppressed warning
    @SuppressLint("MissingPermission")
    public static void showLogin(Context context, AlertDialogBuilder alertDialogBuilder) {
        Account[] accounts = AccountManager.get(context).getAccountsByType(BuildConfig.APPLICATION_ID);
        if (accounts.length == 0) { // no accounts, ask to login or re-login
            context.startActivity(new Intent(context, LoginActivity.class));
        } else if (!TextUtils.isEmpty(Preferences.getUsername(context))) { // stale account, ask to re-login
            context.startActivity(new Intent(context, LoginActivity.class));
        } else { // logged out, choose from existing accounts to log in
            showAccountChooser(context, alertDialogBuilder, accounts);
        }
    }
    
    // remove this annotation we cannot recover the suppressed warning
    @SuppressLint("MissingPermission")
    public static void registerAccountsUpdatedListener(final Context context) {
        AccountManager.get(context).addOnAccountsUpdatedListener(accounts -> {
            String username = Preferences.getUsername(context);
            if (TextUtils.isEmpty(username)) {
                return;
            }
            for (Account account : accounts) {
                if (TextUtils.equals(account.name, username)) {
                    return;
                }
            }
            Preferences.setUsername(context, null);
        }, null, true);
    }
    
    // remove this annotation, Line 86 and 89 will report two "MissingPermission" warnings.
    @SuppressLint("MissingPermission")
    public static void showAccountChooser(final Context context, AlertDialogBuilder alertDialogBuilder,
                                           Account[] accounts) {
        String username = Preferences.getUsername(context);
        final String[] items = new String[accounts.length];
        int checked = -1;
        for (int i = 0; i < accounts.length; i++) {
            String accountName = accounts[i].name;
            items[i] = accountName;
            if (TextUtils.equals(accountName, username)) {
                checked = i;
            }
        }
        int initialSelection = checked;
        DialogInterface.OnClickListener clickListener = new DialogInterface.OnClickListener() {
            private int selection = initialSelection;

            @Override
            public void onClick(DialogInterface dialog, int which) {
                switch (which) {
                    case DialogInterface.BUTTON_POSITIVE:
                        Preferences.setUsername(context, items[selection]);
                        Toast.makeText(context,
                                context.getString(R.string.welcome, items[selection]),
                                Toast.LENGTH_SHORT)
                                .show();
                        dialog.dismiss();
                        break;
                    case DialogInterface.BUTTON_NEGATIVE:
                        Intent intent = new Intent(context, LoginActivity.class);
                        intent.putExtra(LoginActivity.EXTRA_ADD_ACCOUNT, true);
                        context.startActivity(intent);
                        dialog.dismiss();
                        break;
                    case DialogInterface.BUTTON_NEUTRAL:
                        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP_MR1) {
                            AccountManager.get(context).removeAccount(accounts[selection], null, null, null);
                        } else {
                            //noinspection deprecation
                            AccountManager.get(context).removeAccount(accounts[selection], null, null);
                        }
                        dialog.dismiss();
                        break;
                    default:
                        selection = which;
                        break;
                }
            }
        };
        alertDialogBuilder
                .init(context)
                .setTitle(R.string.choose_account)
                .setSingleChoiceItems(items, checked, clickListener)
                .setPositiveButton(android.R.string.ok, clickListener)
                .setNegativeButton(R.string.add_account, clickListener)
                .setNeutralButton(R.string.remove_account, clickListener)
                .show();
    }

以上是关于java 物质主义#05的主要内容,如果未能解决你的问题,请参考以下文章

java 物质主义#26

java 物质主义#23~25

java 物质主义#10〜#22

java 物质主义#09

java 物质主义#07

java 物质主义#04