Example usage for android.accounts AccountManager newChooseAccountIntent

List of usage examples for android.accounts AccountManager newChooseAccountIntent

Introduction

In this page you can find the example usage for android.accounts AccountManager newChooseAccountIntent.

Prototype

static public Intent newChooseAccountIntent(Account selectedAccount, List<Account> allowableAccounts,
        String[] allowableAccountTypes, String descriptionOverrideText, String addAccountAuthTokenType,
        String[] addAccountRequiredFeatures, Bundle addAccountOptions) 

Source Link

Document

Returns an intent to an Activity that prompts the user to choose from a list of accounts.

Usage

From source file:org.comixwall.pffw.MainActivity.java

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    // TODO: Check why main activity is recreated on rotation
    logger.finest("MainActivity onCreate()");

    mMenuItems2Fragments = new HashMap<Integer, Class>() {
        {/*from ww  w.  jav a  2s.co  m*/
            // ATTENTION: Dashboard fragment should never be instantiated using this map
            put(R.id.menuDashboard, Dashboard.class);
            put(R.id.menuNotifications, Notifications.class);
            put(R.id.menuInfoPf, InfoPf.class);
            put(R.id.menuInfoSystem, InfoSystem.class);
            put(R.id.menuInfoHosts, InfoHosts.class);
            put(R.id.menuInfoIfs, InfoIfs.class);
            put(R.id.menuInfoRules, InfoRules.class);
            put(R.id.menuInfoStates, InfoStates.class);
            put(R.id.menuInfoQueues, InfoQueues.class);
            put(R.id.menuStatsGeneral, StatsGeneral.class);
            put(R.id.menuStatsDaily, StatsDaily.class);
            put(R.id.menuStatsHourly, StatsHourly.class);
            put(R.id.menuStatsLive, StatsLive.class);
            put(R.id.menuGraphsInterfaces, GraphsIfs.class);
            put(R.id.menuGraphsTransfer, GraphsTransfer.class);
            put(R.id.menuGraphsStates, GraphsStates.class);
            put(R.id.menuGraphsMbufs, GraphsMbufs.class);
            put(R.id.menuLogsArchives, LogsArchives.class);
            put(R.id.menuLogsLive, LogsLive.class);
        }
    };

    FragmentManager fm = getSupportFragmentManager();

    // ATTENTION: Always load cache from the FragmentManager.
    // Otherwise, the cache survives even after the application is closed by the last Back button press
    cache = (Cache) fm.findFragmentByTag("cache");
    if (cache == null) {
        // create the fragment and data the first time
        cache = new Cache();
        fm.beginTransaction().add(cache, "cache").commit();
    }

    Toolbar toolbar = findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    drawer = findViewById(R.id.drawerLayout);

    toggle = new ActionBarDrawerToggle(this, drawer, toolbar, R.string.navigation_drawer_open,
            R.string.navigation_drawer_close);
    drawer.addDrawerListener(toggle);
    toggle.syncState();

    navigationView = findViewById(R.id.navView);
    navigationView.setNavigationItemSelectedListener(this);

    logFilePickerDialog = new LogFilePickerDialog();

    int result = ContextCompat.checkSelfPermission(this, Manifest.permission.GET_ACCOUNTS);
    if (result == PackageManager.PERMISSION_GRANTED) {
        setUser();
    } else {
        if (ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.GET_ACCOUNTS)) {
            Toast.makeText(this, R.string.account_perms, Toast.LENGTH_LONG).show();
        }
        ActivityCompat.requestPermissions(this, new String[] { Manifest.permission.GET_ACCOUNTS },
                PERMISSION_REQUEST_CODE);
    }

    if (user.equals("Unknown user")) {
        // In Android 8.0 (API level 26), apps can no longer get access to user accounts unless the
        // authenticator owns the accounts or the user grants that access. The GET_ACCOUNTS permission
        // is no longer sufficient. To be granted access to an account, apps should either use
        // AccountManager.newChooseAccountIntent() or an authenticator-specific method. After getting
        // access to accounts, an app can call AccountManager.getAccounts() to access them.
        // @see https://developer.android.com/about/versions/oreo/android-8.0-changes.html#aaad
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            // TODO: Use new String[]{"com.google"} for allowableAccountTypes instead?
            Intent intent = AccountManager.newChooseAccountIntent(null, null, null, null, null, null, null);
            startActivityForResult(intent, PICK_CONTACT_REQUEST);

        }
    }
}