| 1 | // Copyright 2013 The Chromium Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | package org.chromium.chrome.testshell.sync; |
| 6 | |
| 7 | import android.app.AlertDialog; |
| 8 | import android.app.Dialog; |
| 9 | import android.app.DialogFragment; |
| 10 | import android.content.DialogInterface; |
| 11 | import android.os.Bundle; |
| 12 | |
| 13 | import org.chromium.chrome.testshell.R; |
| 14 | import org.chromium.sync.signin.AccountManagerHelper; |
| 15 | |
| 16 | import java.util.List; |
| 17 | |
| 18 | /** |
| 19 | * The fragment to show when the user is given the option to sign in to Chromium. |
| 20 | * |
| 21 | * It lists the available Google accounts on the device and makes the user choose one. |
| 22 | */ |
| 23 | public class AccountChooserFragment extends DialogFragment |
| 24 | implements DialogInterface.OnClickListener { |
| 25 | |
| 26 | private String[] mAccounts; |
| 27 | private int mSelectedAccount; |
| 28 | |
| 29 | @Override |
| 30 | public Dialog onCreateDialog(Bundle savedInstanceState) { |
| 31 | List<String> accountsList = AccountManagerHelper.get(getActivity()).getGoogleAccountNames(); |
| 32 | mAccounts = accountsList.toArray(new String[accountsList.size()]); |
| 33 | return new AlertDialog.Builder(getActivity(), AlertDialog.THEME_HOLO_LIGHT) |
| 34 | .setTitle(R.string.signin_select_account) |
| 35 | .setSingleChoiceItems(mAccounts, mSelectedAccount, this) |
| 36 | .setPositiveButton(R.string.signin_sign_in, this) |
| 37 | .setNegativeButton(R.string.signin_cancel, this) |
| 38 | .create(); |
| 39 | } |
| 40 | |
| 41 | @Override |
| 42 | public void onClick(DialogInterface dialog, int which) { |
| 43 | switch (which) { |
| 44 | case DialogInterface.BUTTON_POSITIVE: { |
| 45 | selectAccount(mAccounts[mSelectedAccount]); |
| 46 | break; |
| 47 | } |
| 48 | case DialogInterface.BUTTON_NEGATIVE: { |
| 49 | dismiss(); |
| 50 | break; |
| 51 | } |
| 52 | default: { |
| 53 | mSelectedAccount = which; |
| 54 | break; |
| 55 | } |
| 56 | } |
| 57 | } |
| 58 | |
| 59 | private void selectAccount(String accountName) { |
| 60 | SyncController.get(getActivity()).signIn(getActivity(), accountName); |
| 61 | } |
| 62 | } |