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.ipc.invalidation.external.client.contrib.MultiplexingGcmListener;
15 import org.chromium.base.CommandLine;
16 import org.chromium.base.ObserverList;
17 import org.chromium.base.VisibleForTesting;
18 import org.chromium.sync.SyncSwitches;
20 public class ChromeSigninController {
21 public interface Listener {
23 * Called when the user signs out of Chrome.
25 void onClearSignedInUser();
28 public static final String TAG = "ChromeSigninController";
31 public static final String SIGNED_IN_ACCOUNT_KEY = "google.services.username";
33 private static final Object LOCK = new Object();
35 private static ChromeSigninController sChromeSigninController;
37 private final Context mApplicationContext;
39 private final ObserverList<Listener> mListeners = new ObserverList<Listener>();
41 private boolean mGcmInitialized;
43 private ChromeSigninController(Context context) {
44 mApplicationContext = context.getApplicationContext();
48 * A factory method for the ChromeSigninController.
50 * @param context the ApplicationContext is retrieved from the context used as an argument.
51 * @return a singleton instance of the ChromeSigninController
53 public static ChromeSigninController get(Context context) {
55 if (sChromeSigninController == null) {
56 sChromeSigninController = new ChromeSigninController(context);
59 return sChromeSigninController;
62 public Account getSignedInUser() {
63 String syncAccountName = getSignedInAccountName();
64 if (syncAccountName == null) {
67 return AccountManagerHelper.createAccountFromName(syncAccountName);
70 public boolean isSignedIn() {
71 return getSignedInAccountName() != null;
74 public void setSignedInAccountName(String accountName) {
75 PreferenceManager.getDefaultSharedPreferences(mApplicationContext).edit()
76 .putString(SIGNED_IN_ACCOUNT_KEY, accountName)
80 public void clearSignedInUser() {
81 Log.d(TAG, "Clearing user signed in to Chrome");
82 setSignedInAccountName(null);
84 for (Listener listener : mListeners) {
85 listener.onClearSignedInUser();
89 public String getSignedInAccountName() {
90 return PreferenceManager.getDefaultSharedPreferences(mApplicationContext)
91 .getString(SIGNED_IN_ACCOUNT_KEY, null);
96 * @param listener Listener to add.
98 public void addListener(Listener listener) {
99 mListeners.addObserver(listener);
103 * Removes a Listener.
104 * @param listener Listener to remove from the list.
106 public void removeListener(Listener listener) {
107 mListeners.removeObserver(listener);
111 * Registers for Google Cloud Messaging (GCM) if there is no existing registration.
113 public void ensureGcmIsInitialized() {
114 if (mGcmInitialized) return;
115 mGcmInitialized = true;
116 new AsyncTask<Void, Void, Void>() {
118 protected Void doInBackground(Void... arg0) {
119 if (CommandLine.getInstance().hasSwitch(
120 SyncSwitches.DISABLE_SYNC_GCM_IN_ORDER_TO_TRY_PUSH_API)) {
121 Log.w(TAG, "Sync GCM notifications disabled in order to try Push API!");
125 String regId = MultiplexingGcmListener.initializeGcm(mApplicationContext);
126 if (!regId.isEmpty())
127 Log.d(TAG, "Already registered with GCM");
128 } catch (IllegalStateException exception) {
129 Log.w(TAG, "Application manifest does not correctly configure GCM; "
130 + "sync notifications will not work", exception);
131 } catch (UnsupportedOperationException exception) {
132 Log.w(TAG, "Device does not support GCM; sync notifications will not work",
137 }.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);