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.
5 package org.chromium.sync.signin;
7 import android.accounts.Account;
8 import android.content.Context;
9 import android.os.AsyncTask;
10 import android.preference.PreferenceManager;
11 import android.util.Log;
13 import com.google.common.annotations.VisibleForTesting;
14 import com.google.ipc.invalidation.external.client.contrib.MultiplexingGcmListener;
16 import org.chromium.base.ObserverList;
18 public class ChromeSigninController {
19 public interface Listener {
21 * Called when the user signs out of Chrome.
23 void onClearSignedInUser();
26 public static final String TAG = "ChromeSigninController";
29 public static final String SIGNED_IN_ACCOUNT_KEY = "google.services.username";
31 private static final Object LOCK = new Object();
33 private static ChromeSigninController sChromeSigninController;
35 private final Context mApplicationContext;
37 private final ObserverList<Listener> mListeners = new ObserverList<Listener>();
39 private boolean mGcmInitialized;
41 private ChromeSigninController(Context context) {
42 mApplicationContext = context.getApplicationContext();
46 * A factory method for the ChromeSigninController.
48 * @param context the ApplicationContext is retrieved from the context used as an argument.
49 * @return a singleton instance of the ChromeSigninController
51 public static ChromeSigninController get(Context context) {
53 if (sChromeSigninController == null) {
54 sChromeSigninController = new ChromeSigninController(context);
57 return sChromeSigninController;
60 public Account getSignedInUser() {
61 String syncAccountName = getSignedInAccountName();
62 if (syncAccountName == null) {
65 return AccountManagerHelper.createAccountFromName(syncAccountName);
68 public boolean isSignedIn() {
69 return getSignedInAccountName() != null;
72 public void setSignedInAccountName(String accountName) {
73 PreferenceManager.getDefaultSharedPreferences(mApplicationContext).edit()
74 .putString(SIGNED_IN_ACCOUNT_KEY, accountName)
78 public void clearSignedInUser() {
79 Log.d(TAG, "Clearing user signed in to Chrome");
80 setSignedInAccountName(null);
82 for (Listener listener : mListeners) {
83 listener.onClearSignedInUser();
87 public String getSignedInAccountName() {
88 return PreferenceManager.getDefaultSharedPreferences(mApplicationContext)
89 .getString(SIGNED_IN_ACCOUNT_KEY, null);
94 * @param listener Listener to add.
96 public void addListener(Listener listener) {
97 mListeners.addObserver(listener);
101 * Removes a Listener.
102 * @param listener Listener to remove from the list.
104 public void removeListener(Listener listener) {
105 mListeners.removeObserver(listener);
109 * Registers for Google Cloud Messaging (GCM) if there is no existing registration.
111 public void ensureGcmIsInitialized() {
112 if (mGcmInitialized) return;
113 mGcmInitialized = true;
114 new AsyncTask<Void, Void, Void>() {
116 protected Void doInBackground(Void... arg0) {
118 String regId = MultiplexingGcmListener.initializeGcm(mApplicationContext);
119 if (!regId.isEmpty())
120 Log.d(TAG, "Already registered with GCM");
121 } catch (IllegalStateException exception) {
122 Log.w(TAG, "Application manifest does not correctly configure GCM; "
123 + "sync notifications will not work", exception);
124 } catch (UnsupportedOperationException exception) {
125 Log.w(TAG, "Device does not support GCM; sync notifications will not work",
130 }.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);