OLD | NEW |
| (Empty) |
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.sync.signin; | |
6 | |
7 import android.accounts.Account; | |
8 import android.content.Context; | |
9 | |
10 import org.chromium.base.ContextUtils; | |
11 import org.chromium.sync.AndroidSyncSettings; | |
12 | |
13 /** | |
14 * Caches the signed-in username in the app prefs. | |
15 */ | |
16 public class ChromeSigninController { | |
17 | |
18 public static final String TAG = "ChromeSigninController"; | |
19 | |
20 // Used by ChromeBackupAgent and for testing. | |
21 public static final String SIGNED_IN_ACCOUNT_KEY = "google.services.username
"; | |
22 | |
23 private static final Object LOCK = new Object(); | |
24 | |
25 private static ChromeSigninController sChromeSigninController; | |
26 | |
27 private final Context mApplicationContext; | |
28 | |
29 private ChromeSigninController(Context context) { | |
30 mApplicationContext = context.getApplicationContext(); | |
31 AndroidSyncSettings.updateAccount(context, getSignedInUser()); | |
32 } | |
33 | |
34 /** | |
35 * A factory method for the ChromeSigninController. | |
36 * | |
37 * @param context the ApplicationContext is retrieved from the context used
as an argument. | |
38 * @return a singleton instance of the ChromeSigninController | |
39 */ | |
40 public static ChromeSigninController get(Context context) { | |
41 synchronized (LOCK) { | |
42 if (sChromeSigninController == null) { | |
43 sChromeSigninController = new ChromeSigninController(context); | |
44 } | |
45 } | |
46 return sChromeSigninController; | |
47 } | |
48 | |
49 public Account getSignedInUser() { | |
50 String syncAccountName = getSignedInAccountName(); | |
51 if (syncAccountName == null) { | |
52 return null; | |
53 } | |
54 return AccountManagerHelper.createAccountFromName(syncAccountName); | |
55 } | |
56 | |
57 public boolean isSignedIn() { | |
58 return getSignedInAccountName() != null; | |
59 } | |
60 | |
61 public void setSignedInAccountName(String accountName) { | |
62 ContextUtils.getAppSharedPreferences().edit() | |
63 .putString(SIGNED_IN_ACCOUNT_KEY, accountName) | |
64 .apply(); | |
65 // TODO(maxbogue): Move this to SigninManager. | |
66 AndroidSyncSettings.updateAccount(mApplicationContext, getSignedInUser()
); | |
67 } | |
68 | |
69 public String getSignedInAccountName() { | |
70 return ContextUtils.getAppSharedPreferences().getString(SIGNED_IN_ACCOUN
T_KEY, null); | |
71 } | |
72 } | |
OLD | NEW |