1010
1111import java .util .HashMap ;
1212import java .util .Map ;
13+ import java .util .concurrent .Callable ;
1314
1415import bolts .Continuation ;
1516import bolts .Task ;
1617
1718/** package */ class ParseAuthenticationManager {
1819
1920 private final Object lock = new Object ();
20- private final Map <String , ParseAuthenticationProvider > authenticationProviders = new HashMap <>();
21+ private final Map <String , AuthenticationCallback > callbacks = new HashMap <>();
2122 private final ParseCurrentUserController controller ;
2223
2324 public ParseAuthenticationManager (ParseCurrentUserController controller ) {
2425 this .controller = controller ;
2526 }
2627
27- public void register (ParseAuthenticationProvider provider ) {
28- final String authType = provider .getAuthType ();
28+ public void register (final String authType , AuthenticationCallback callback ) {
2929 if (authType == null ) {
3030 throw new IllegalArgumentException ("Invalid authType: " + null );
3131 }
3232
3333 synchronized (lock ) {
34- if (authenticationProviders .containsKey (authType )) {
35- throw new IllegalStateException ("Another " + authType + " provider was already registered : "
36- + authenticationProviders .get (authType ));
34+ if (this . callbacks .containsKey (authType )) {
35+ throw new IllegalStateException ("Callback already registered for < " + authType + "> : "
36+ + this . callbacks .get (authType ));
3737 }
38- authenticationProviders . put (provider . getAuthType (), provider );
38+ this . callbacks . put (authType , callback );
3939 }
4040
41- if (provider instanceof AnonymousAuthenticationProvider ) {
41+ if (ParseAnonymousUtils . AUTH_TYPE . equals ( authType ) ) {
4242 // There's nothing to synchronize
4343 return ;
4444 }
4545
46- // Synchronize the current user with the auth provider .
46+ // Synchronize the current user with the auth callback .
4747 controller .getAsync (false ).onSuccessTask (new Continuation <ParseUser , Task <Void >>() {
4848 @ Override
4949 public Task <Void > then (Task <ParseUser > task ) throws Exception {
@@ -56,24 +56,35 @@ public Task<Void> then(Task<ParseUser> task) throws Exception {
5656 });
5757 }
5858
59- public Task <Void > restoreAuthenticationAsync (String authType , Map <String , String > authData ) {
60- ParseAuthenticationProvider provider ;
59+ public Task <Boolean > restoreAuthenticationAsync (String authType , final Map <String , String > authData ) {
60+ final AuthenticationCallback callback ;
6161 synchronized (lock ) {
62- provider = authenticationProviders .get (authType );
62+ callback = this . callbacks .get (authType );
6363 }
64- if (provider == null ) {
65- return Task .forResult (null );
64+ if (callback == null ) {
65+ return Task .forResult (true );
6666 }
67- return provider .restoreAuthenticationInBackground (authData );
67+ return Task .call (new Callable <Boolean >() {
68+ @ Override
69+ public Boolean call () throws Exception {
70+ return callback .onRestore (authData );
71+ }
72+ }, ParseExecutors .io ());
6873 }
6974
7075 public Task <Void > deauthenticateAsync (String authType ) {
71- ParseAuthenticationProvider provider ;
76+ final AuthenticationCallback callback ;
7277 synchronized (lock ) {
73- provider = authenticationProviders .get (authType );
78+ callback = this . callbacks .get (authType );
7479 }
75- if (provider != null ) {
76- return provider .deauthenticateInBackground ();
80+ if (callback != null ) {
81+ return Task .call (new Callable <Void >() {
82+ @ Override
83+ public Void call () throws Exception {
84+ callback .onRestore (null );
85+ return null ;
86+ }
87+ }, ParseExecutors .io ());
7788 }
7889 return Task .forResult (null );
7990 }
0 commit comments