OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 package org.chromium.components.signin; | 5 package org.chromium.components.signin; |
6 | 6 |
7 import android.accounts.Account; | 7 import android.accounts.Account; |
| 8 import android.content.Context; |
8 | 9 |
9 import org.chromium.base.ContextUtils; | 10 import org.chromium.base.ContextUtils; |
10 | 11 |
11 /** | 12 /** |
12 * Caches the signed-in username in the app prefs. | 13 * Caches the signed-in username in the app prefs. |
13 */ | 14 */ |
14 public class ChromeSigninController { | 15 public class ChromeSigninController { |
15 public static final String TAG = "ChromeSigninController"; | 16 public static final String TAG = "ChromeSigninController"; |
16 | 17 |
17 // Used by ChromeBackupAgent and for testing. | 18 // Used by ChromeBackupAgent and for testing. |
18 public static final String SIGNED_IN_ACCOUNT_KEY = "google.services.username
"; | 19 public static final String SIGNED_IN_ACCOUNT_KEY = "google.services.username
"; |
19 | 20 |
20 private static final Object LOCK = new Object(); | 21 private static final Object LOCK = new Object(); |
21 | 22 |
22 private static ChromeSigninController sChromeSigninController; | 23 private static ChromeSigninController sChromeSigninController; |
23 | 24 |
24 private ChromeSigninController() {} | 25 private final Context mApplicationContext; |
| 26 |
| 27 private ChromeSigninController(Context context) { |
| 28 mApplicationContext = context.getApplicationContext(); |
| 29 } |
25 | 30 |
26 /** | 31 /** |
27 * A factory method for the ChromeSigninController. | 32 * A factory method for the ChromeSigninController. |
28 * | 33 * |
| 34 * @param context the ApplicationContext is retrieved from the context used
as an argument. |
29 * @return a singleton instance of the ChromeSigninController | 35 * @return a singleton instance of the ChromeSigninController |
30 */ | 36 */ |
31 public static ChromeSigninController get() { | 37 public static ChromeSigninController get(Context context) { |
32 synchronized (LOCK) { | 38 synchronized (LOCK) { |
33 if (sChromeSigninController == null) { | 39 if (sChromeSigninController == null) { |
34 sChromeSigninController = new ChromeSigninController(); | 40 sChromeSigninController = new ChromeSigninController(context); |
35 } | 41 } |
36 } | 42 } |
37 return sChromeSigninController; | 43 return sChromeSigninController; |
38 } | 44 } |
39 | 45 |
40 public Account getSignedInUser() { | 46 public Account getSignedInUser() { |
41 String syncAccountName = getSignedInAccountName(); | 47 String syncAccountName = getSignedInAccountName(); |
42 if (syncAccountName == null) { | 48 if (syncAccountName == null) { |
43 return null; | 49 return null; |
44 } | 50 } |
45 return AccountManagerHelper.createAccountFromName(syncAccountName); | 51 return AccountManagerHelper.createAccountFromName(syncAccountName); |
46 } | 52 } |
47 | 53 |
48 public boolean isSignedIn() { | 54 public boolean isSignedIn() { |
49 return getSignedInAccountName() != null; | 55 return getSignedInAccountName() != null; |
50 } | 56 } |
51 | 57 |
52 public void setSignedInAccountName(String accountName) { | 58 public void setSignedInAccountName(String accountName) { |
53 ContextUtils.getAppSharedPreferences() | 59 ContextUtils.getAppSharedPreferences() |
54 .edit() | 60 .edit() |
55 .putString(SIGNED_IN_ACCOUNT_KEY, accountName) | 61 .putString(SIGNED_IN_ACCOUNT_KEY, accountName) |
56 .apply(); | 62 .apply(); |
57 } | 63 } |
58 | 64 |
59 public String getSignedInAccountName() { | 65 public String getSignedInAccountName() { |
60 return ContextUtils.getAppSharedPreferences().getString(SIGNED_IN_ACCOUN
T_KEY, null); | 66 return ContextUtils.getAppSharedPreferences().getString(SIGNED_IN_ACCOUN
T_KEY, null); |
61 } | 67 } |
62 } | 68 } |
OLD | NEW |