Home | History | Annotate | Download | only in signin
      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 import android.preference.PreferenceManager;
     10 import android.util.Log;
     11 
     12 import com.google.common.annotations.VisibleForTesting;
     13 
     14 import org.chromium.base.ObserverList;
     15 
     16 public class ChromeSigninController {
     17     public interface Listener {
     18         /**
     19          * Called when the user signs out of Chrome.
     20          */
     21         void onClearSignedInUser();
     22     }
     23 
     24     public static final String TAG = "ChromeSigninController";
     25 
     26     @VisibleForTesting
     27     public static final String SIGNED_IN_ACCOUNT_KEY = "google.services.username";
     28 
     29     private static final Object LOCK = new Object();
     30 
     31     private static ChromeSigninController sChromeSigninController;
     32 
     33     private final Context mApplicationContext;
     34 
     35     private final ObserverList<Listener> mListeners = new ObserverList<Listener>();
     36 
     37     private ChromeSigninController(Context context) {
     38         mApplicationContext = context.getApplicationContext();
     39     }
     40 
     41     /**
     42      * A factory method for the ChromeSigninController.
     43      *
     44      * @param context the ApplicationContext is retrieved from the context used as an argument.
     45      * @return a singleton instance of the ChromeSigninController
     46      */
     47     public static ChromeSigninController get(Context context) {
     48         synchronized (LOCK) {
     49             if (sChromeSigninController == null) {
     50                 sChromeSigninController = new ChromeSigninController(context);
     51             }
     52         }
     53         return sChromeSigninController;
     54     }
     55 
     56     public Account getSignedInUser() {
     57         String syncAccountName = getSignedInAccountName();
     58         if (syncAccountName == null) {
     59             return null;
     60         }
     61         return AccountManagerHelper.createAccountFromName(syncAccountName);
     62     }
     63 
     64     public boolean isSignedIn() {
     65         return getSignedInAccountName() != null;
     66     }
     67 
     68     public void setSignedInAccountName(String accountName) {
     69         PreferenceManager.getDefaultSharedPreferences(mApplicationContext).edit()
     70                 .putString(SIGNED_IN_ACCOUNT_KEY, accountName)
     71                 .apply();
     72     }
     73 
     74     public void clearSignedInUser() {
     75         Log.d(TAG, "Clearing user signed in to Chrome");
     76         setSignedInAccountName(null);
     77 
     78         for (Listener listener : mListeners) {
     79             listener.onClearSignedInUser();
     80         }
     81     }
     82 
     83     public String getSignedInAccountName() {
     84         return PreferenceManager.getDefaultSharedPreferences(mApplicationContext)
     85                 .getString(SIGNED_IN_ACCOUNT_KEY, null);
     86     }
     87 
     88     /**
     89      * Adds a Listener.
     90      * @param listener Listener to add.
     91      */
     92     public void addListener(Listener listener) {
     93         mListeners.addObserver(listener);
     94     }
     95 
     96     /**
     97      * Removes a Listener.
     98      * @param listener Listener to remove from the list.
     99      */
    100     public void removeListener(Listener listener) {
    101         mListeners.removeObserver(listener);
    102     }
    103 }
    104