2222
2323package com .onedrive .sdk .authentication ;
2424
25+ import com .microsoft .onedrivesdk .BuildConfig ;
2526import com .onedrive .sdk .concurrency .ICallback ;
2627import com .onedrive .sdk .concurrency .IExecutors ;
2728import com .onedrive .sdk .concurrency .SimpleWaiter ;
3132import com .onedrive .sdk .logger .ILogger ;
3233
3334import android .app .Activity ;
35+ import android .content .Context ;
36+ import android .content .SharedPreferences ;
37+ import android .support .annotation .Nullable ;
3438
3539import java .security .InvalidParameterException ;
3640import java .util .concurrent .atomic .AtomicReference ;
4044 */
4145public class DisambiguationAuthenticator implements IAuthenticator {
4246
47+ /**
48+ * The preferences for this authenticator.
49+ */
50+ private static final String DISAMBIGUATION_AUTHENTICATOR_PREFS = "DisambiguationAuthenticatorPrefs" ;
51+
52+ /**
53+ * The key for the version code
54+ */
55+ public static final String VERSION_CODE_KEY = "versionCode" ;
56+
57+ /**
58+ * The key for the account type
59+ */
60+ public static final String ACCOUNT_TYPE_KEY = "accountType" ;
61+
4362 /**
4463 * The current account info object.
4564 */
@@ -173,31 +192,41 @@ public void failure(final ClientException error2) {
173192 }
174193 };
175194
176- mLogger .logDebug ("Creating disambiguation ui, waiting for user to sign in" );
177- new DisambiguationRequest (mActivity , disambiguationCallback , mLogger ).execute ();
178- disambiguationWaiter .waitForSignal ();
179-
180- //noinspection ThrowableResultOfMethodCallIgnored
181- if (error .get () != null ) {
182- throw error .get ();
195+ AccountType accountType = getAccountTypeInPreferences ();
196+ String accountName = null ;
197+ if (accountType != null ) {
198+ mLogger .logDebug (String .format ("Found saved account information %s type of account" , accountType ));
199+ } else {
200+ mLogger .logDebug ("Creating disambiguation ui, waiting for user to sign in" );
201+ new DisambiguationRequest (mActivity , disambiguationCallback , mLogger ).execute ();
202+ disambiguationWaiter .waitForSignal ();
203+
204+ //noinspection ThrowableResultOfMethodCallIgnored
205+ if (error .get () != null ) {
206+ throw error .get ();
207+ }
208+ final DisambiguationResponse disambiguationResponse = response .get ();
209+ accountType = disambiguationResponse .getAccountType ();
210+ accountName = disambiguationResponse .getAccount ();
183211 }
184- final DisambiguationResponse disambiguationResponse = response . get ();
212+
185213 final IAccountInfo accountInfo ;
186- switch (disambiguationResponse . getAccountType () ) {
214+ switch (accountType ) {
187215 case ActiveDirectory :
188- accountInfo = mADALAuthenticator .login (disambiguationResponse . getAccount () );
216+ accountInfo = mADALAuthenticator .login (accountName );
189217 break ;
190218 case MicrosoftAccount :
191- accountInfo = mMSAAuthenticator .login (disambiguationResponse . getAccount () );
219+ accountInfo = mMSAAuthenticator .login (accountName );
192220 break ;
193221 default :
194222 final UnsupportedOperationException unsupportedOperationException
195223 = new UnsupportedOperationException ("Unrecognized account type "
196- + disambiguationResponse . getAccountType () );
224+ + accountType );
197225 mLogger .logError ("Unrecognized account type" , unsupportedOperationException );
198226 throw unsupportedOperationException ;
199227 }
200228
229+ setAccountTypeInPreferences (accountType );
201230 mAccountInfo .set (accountInfo );
202231 return mAccountInfo .get ();
203232 }
@@ -241,10 +270,17 @@ public synchronized IAccountInfo loginSilent() throws ClientException {
241270 }
242271
243272 mLogger .logDebug ("Starting login silent" );
273+
274+ final AccountType accountType = getAccountTypeInPreferences ();
275+ if (accountType != null ) {
276+ mLogger .logDebug (String .format ("Expecting %s type of account" , accountType ));
277+ }
278+
244279 mLogger .logDebug ("Checking MSA" );
245280 IAccountInfo accountInfo = mMSAAuthenticator .loginSilent ();
246281 if (accountInfo != null ) {
247282 mLogger .logDebug ("Found account info in MSA" );
283+ setAccountTypeInPreferences (accountType );
248284 mAccountInfo .set (accountInfo );
249285 return accountInfo ;
250286 }
@@ -254,8 +290,9 @@ public synchronized IAccountInfo loginSilent() throws ClientException {
254290 mAccountInfo .set (accountInfo );
255291 if (accountInfo != null ) {
256292 mLogger .logDebug ("Found account info in ADAL" );
293+ setAccountTypeInPreferences (accountType );
257294 }
258- return accountInfo ;
295+ return mAccountInfo . get () ;
259296 }
260297
261298 /**
@@ -277,7 +314,7 @@ public void logout(final ICallback<Void> logoutCallback) {
277314 @ Override
278315 public void run () {
279316 logout ();
280- mExecutors .performOnForeground ((Void )null , logoutCallback );
317+ mExecutors .performOnForeground ((Void ) null , logoutCallback );
281318 }
282319 });
283320 }
@@ -296,10 +333,18 @@ public synchronized void logout() throws ClientException {
296333 if (mMSAAuthenticator .getAccountInfo () != null ) {
297334 mLogger .logDebug ("Starting logout of MSA account" );
298335 mMSAAuthenticator .logout ();
299- } else if (mADALAuthenticator .getAccountInfo () != null ) {
336+ }
337+
338+ if (mADALAuthenticator .getAccountInfo () != null ) {
300339 mLogger .logDebug ("Starting logout of ADAL account" );
301340 mADALAuthenticator .logout ();
302341 }
342+
343+ getSharedPreferences ()
344+ .edit ()
345+ .clear ()
346+ .putInt (VERSION_CODE_KEY , BuildConfig .VERSION_CODE )
347+ .commit ();
303348 }
304349
305350 /**
@@ -310,4 +355,41 @@ public synchronized void logout() throws ClientException {
310355 public IAccountInfo getAccountInfo () {
311356 return mAccountInfo .get ();
312357 }
358+
359+ /**
360+ * Gets the shared preferences for this authenticator.
361+ * @return The shared preferences.
362+ */
363+ private SharedPreferences getSharedPreferences () {
364+ return mActivity .getSharedPreferences (DISAMBIGUATION_AUTHENTICATOR_PREFS , Context .MODE_PRIVATE );
365+ }
366+
367+ /**
368+ * Sets the AccountType from SharedPreferences
369+ * @param accountType The account type, can be null
370+ */
371+ private void setAccountTypeInPreferences (@ Nullable final AccountType accountType ) {
372+ if (accountType == null ) {
373+ return ;
374+ }
375+
376+ getSharedPreferences ()
377+ .edit ()
378+ .putString (ACCOUNT_TYPE_KEY , accountType .toString ())
379+ .putInt (VERSION_CODE_KEY , BuildConfig .VERSION_CODE )
380+ .commit ();
381+ }
382+
383+ /**
384+ * Gets the AccountType from SharedPreferences
385+ * @return The account type, null if none was found
386+ */
387+ @ Nullable
388+ private AccountType getAccountTypeInPreferences () {
389+ final String accountTypeString = getSharedPreferences ().getString (ACCOUNT_TYPE_KEY , null );
390+ if (accountTypeString == null ) {
391+ return null ;
392+ }
393+ return AccountType .valueOf (accountTypeString );
394+ }
313395}
0 commit comments