| 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.accounts.Account; |
| 8 | import android.app.Activity; |
| 9 | import android.app.FragmentManager; |
| 10 | import android.content.Context; |
| 11 | |
| 12 | import org.chromium.base.ThreadUtils; |
| 13 | import org.chromium.chrome.browser.signin.SigninManager; |
| 14 | import org.chromium.chrome.browser.sync.ProfileSyncService; |
| 15 | import org.chromium.sync.notifier.SyncStatusHelper; |
| 16 | import org.chromium.sync.signin.AccountManagerHelper; |
| 17 | |
| 18 | /** |
| 19 | * A helper class for signing in and out of Chromium. |
| 20 | */ |
| 21 | public class SyncController { |
| 22 | |
| 23 | private static SyncController sInstance; |
| 24 | |
| 25 | private final Context mContext; |
| 26 | |
| 27 | private SyncController(Context context) { |
| 28 | mContext = context; |
| 29 | } |
| 30 | |
| 31 | /** |
| 32 | * Retrieve the singleton instance of this class. |
| 33 | * |
| 34 | * @param context the current context. |
| 35 | * @return the singleton instance. |
| 36 | */ |
| 37 | public static SyncController get(Context context) { |
| 38 | ThreadUtils.assertOnUiThread(); |
| 39 | if (sInstance == null) { |
| 40 | sInstance = new SyncController(context.getApplicationContext()); |
| 41 | } |
| 42 | return sInstance; |
| 43 | } |
| 44 | |
| 45 | /** |
| 46 | * Open a dialog that gives the user the option to sign in from a list of available accounts. |
| 47 | * |
| 48 | * @param fragmentManager the FragmentManager. |
| 49 | */ |
| 50 | public static void openSigninDialog(FragmentManager fragmentManager) { |
| 51 | AccountChooserFragment chooserFragment = new AccountChooserFragment(); |
| 52 | chooserFragment.show(fragmentManager, null); |
| 53 | } |
| 54 | |
| 55 | /** |
| 56 | * Open a dialog that gives the user the option to sign out. |
| 57 | * |
| 58 | * @param fragmentManager the FragmentManager. |
| 59 | */ |
| 60 | public static void openSignOutDialog(FragmentManager fragmentManager) { |
| 61 | SignoutFragment signoutFragment = new SignoutFragment(); |
| 62 | signoutFragment.show(fragmentManager, null); |
| 63 | } |
| 64 | |
| 65 | /** |
| 66 | * Trigger Chromium sign in of the given account. |
| 67 | * |
| 68 | * This also ensure that sync setup is not in progress anymore, so sync will start after |
| 69 | * sync initialization has happened. |
| 70 | * |
| 71 | * @param activity the current activity. |
| 72 | * @param accountName the full account name. |
| 73 | */ |
| 74 | public void signIn(Activity activity, String accountName) { |
| 75 | final Account account = AccountManagerHelper.createAccountFromName(accountName); |
| 76 | |
| 77 | // The SigninManager handles most of the sign-in flow, and doFinishSignIn handles the |
| 78 | // Chromium testshell specific details. |
| 79 | SigninManager signinManager = SigninManager.get(mContext); |
| 80 | final boolean passive = false; |
| 81 | signinManager.startSignIn(activity, account, passive, new SigninManager.Observer() { |
| 82 | @Override |
| 83 | public void onSigninComplete() { |
| 84 | ProfileSyncService.get(mContext).setSetupInProgress(false); |
| 85 | // The SigninManager does not control the Android sync state. |
| 86 | SyncStatusHelper.get(mContext).enableAndroidSync(account); |
| 87 | } |
| 88 | |
| 89 | @Override |
| 90 | public void onSigninCancelled() { |
| 91 | } |
| 92 | }); |
| 93 | } |
| 94 | } |