forked from chromium/chromium
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[sync] Split sync and signin helper class
Currently SyncStatusHelper handles both signin and sync-related tasks. This splits it into two. There will be a follow-up CL to remove the now deprecated methods, as soon as the downstream code has been updated to use the new class. BUG=159203 Review URL: https://chromiumcodereview.appspot.com/12873002 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@188361 0039d316-1c4b-4281-b951-d872f2087c98
- Loading branch information
nyquist@chromium.org
committed
Mar 15, 2013
1 parent
0987a41
commit b6fd2de
Showing
4 changed files
with
186 additions
and
48 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
103 changes: 103 additions & 0 deletions
103
sync/android/java/src/org/chromium/sync/signin/ChromeSigninController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
// Copyright 2013 The Chromium Authors. All rights reserved. | ||
// Use of this source code is governed by a BSD-style license that can be | ||
// found in the LICENSE file. | ||
|
||
package org.chromium.sync.signin; | ||
|
||
import android.accounts.Account; | ||
import android.content.Context; | ||
import android.preference.PreferenceManager; | ||
import android.util.Log; | ||
|
||
import com.google.common.annotations.VisibleForTesting; | ||
|
||
import org.chromium.base.ObserverList; | ||
|
||
public class ChromeSigninController { | ||
public interface Listener { | ||
/** | ||
* Called when the user signs out of Chrome. | ||
*/ | ||
void onClearSignedInUser(); | ||
} | ||
|
||
public static final String TAG = ChromeSigninController.class.getSimpleName(); | ||
|
||
@VisibleForTesting | ||
public static final String SIGNED_IN_ACCOUNT_KEY = "google.services.username"; | ||
|
||
private static final Object LOCK = new Object(); | ||
|
||
private static ChromeSigninController sChromeSigninController; | ||
|
||
private final Context mApplicationContext; | ||
|
||
private final ObserverList<Listener> mListeners = new ObserverList<Listener>(); | ||
|
||
private ChromeSigninController(Context context) { | ||
mApplicationContext = context.getApplicationContext(); | ||
} | ||
|
||
/** | ||
* A factory method for the ChromeSigninController. | ||
* | ||
* @param context the ApplicationContext is retrieved from the context used as an argument. | ||
* @return a singleton instance of the ChromeSigninController | ||
*/ | ||
public static ChromeSigninController get(Context context) { | ||
synchronized (LOCK) { | ||
if (sChromeSigninController == null) { | ||
sChromeSigninController = new ChromeSigninController(context); | ||
} | ||
} | ||
return sChromeSigninController; | ||
} | ||
|
||
public Account getSignedInUser() { | ||
String syncAccountName = getSignedInAccountName(); | ||
if (syncAccountName == null) { | ||
return null; | ||
} | ||
return AccountManagerHelper.createAccountFromName(syncAccountName); | ||
} | ||
|
||
public boolean isSignedIn() { | ||
return getSignedInAccountName() != null; | ||
} | ||
|
||
public void setSignedInAccountName(String accountName) { | ||
PreferenceManager.getDefaultSharedPreferences(mApplicationContext).edit() | ||
.putString(SIGNED_IN_ACCOUNT_KEY, accountName) | ||
.apply(); | ||
} | ||
|
||
public void clearSignedInUser() { | ||
Log.d(TAG, "Clearing user signed in to Chrome"); | ||
setSignedInAccountName(null); | ||
|
||
for (Listener listener : mListeners) { | ||
listener.onClearSignedInUser(); | ||
} | ||
} | ||
|
||
public String getSignedInAccountName() { | ||
return PreferenceManager.getDefaultSharedPreferences(mApplicationContext) | ||
.getString(SIGNED_IN_ACCOUNT_KEY, null); | ||
} | ||
|
||
/** | ||
* Adds a Listener. | ||
* @param listener Listener to add. | ||
*/ | ||
public void addListener(Listener listener) { | ||
mListeners.addObserver(listener); | ||
} | ||
|
||
/** | ||
* Removes a Listener. | ||
* @param listener Listener to remove from the list. | ||
*/ | ||
public void removeListener(Listener listener) { | ||
mListeners.removeObserver(listener); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters