@ -5,12 +5,15 @@ import java.io.IOException;
import android.accounts.Account ;
import android.accounts.Account ;
import android.accounts.AccountManager ;
import android.accounts.AccountManager ;
import android.accounts.AccountManagerFuture ;
import android.accounts.AccountManagerFuture ;
import android.content.Context ;
import android.os.Bundle ;
import android.os.Bundle ;
import com.google.api.client.googleapis.extensions.android2.auth.GoogleAccountManager ;
import com.google.api.client.googleapis.extensions.android2.auth.GoogleAccountManager ;
import com.timsu.astrid.R ;
import com.todoroo.andlib.service.ContextManager ;
import com.todoroo.andlib.service.ContextManager ;
import com.todoroo.andlib.utility.Preferences ;
import com.todoroo.andlib.utility.Preferences ;
import com.todoroo.astrid.gtasks.GtasksPreferenceService ;
import com.todoroo.astrid.gtasks.GtasksPreferenceService ;
import com.todoroo.astrid.gtasks.api.GoogleTasksException ;
import com.todoroo.astrid.gtasks.api.GtasksService ;
import com.todoroo.astrid.gtasks.api.GtasksService ;
public class GtasksTokenValidator {
public class GtasksTokenValidator {
@ -21,7 +24,7 @@ public class GtasksTokenValidator {
* @param token
* @param token
* @return valid token on success , null on failure
* @return valid token on success , null on failure
* /
* /
public static String validateAuthToken ( String token ) {
public static String validateAuthToken ( Context c , String token ) throws GoogleTasksException {
GoogleAccountManager accountManager = new GoogleAccountManager ( ContextManager . getContext ( ) ) ;
GoogleAccountManager accountManager = new GoogleAccountManager ( ContextManager . getContext ( ) ) ;
GtasksService testService = new GtasksService ( token ) ;
GtasksService testService = new GtasksService ( token ) ;
@ -30,10 +33,9 @@ public class GtasksTokenValidator {
return token ;
return token ;
} catch ( IOException i ) { //If fail, token may have expired -- get a new one and return that
} catch ( IOException i ) { //If fail, token may have expired -- get a new one and return that
String accountName = Preferences . getStringValue ( GtasksPreferenceService . PREF_USER_NAME ) ;
String accountName = Preferences . getStringValue ( GtasksPreferenceService . PREF_USER_NAME ) ;
Account a = accountManager . getAccountByName ( Preferences. getStringValue ( GtasksPreferenceService . PREF_USER_NAME ) ) ;
Account a = accountManager . getAccountByName ( accountName ) ;
if ( a = = null ) {
if ( a = = null ) {
System . err . println ( "Account for name: " + accountName + " not found" ) ;
throw new GoogleTasksException ( c . getString ( R . string . gtasks_error_accountNotFound , accountName ) ) ;
return null ;
}
}
accountManager . invalidateAuthToken ( token ) ;
accountManager . invalidateAuthToken ( token ) ;
@ -49,20 +51,39 @@ public class GtasksTokenValidator {
return token ;
return token ;
} catch ( IOException i2 ) {
} catch ( IOException i2 ) {
i2 . printStackTrace ( ) ;
i2 . printStackTrace ( ) ;
return null ;
String manufacturer = android . os . Build . MANUFACTURER . toLowerCase ( ) ;
if ( ! manufacturer . contains ( "samsung" ) ) { // Try with the notifyAuthFailure set to true in case it was that that broke things
accountManager . invalidateAuthToken ( token ) ;
future = accountManager . manager . getAuthToken ( a , GtasksService . AUTH_TOKEN_TYPE , true , null , null ) ;
try {
if ( future . getResult ( ) . containsKey ( AccountManager . KEY_AUTHTOKEN ) ) {
result = future . getResult ( ) ;
token = result . getString ( AccountManager . KEY_AUTHTOKEN ) ;
testService = new GtasksService ( token ) ;
try {
testService . ping ( ) ;
return token ;
} catch ( IOException i3 ) {
i3 . printStackTrace ( ) ;
throw new GoogleTasksException ( c . getString ( R . string . gtasks_error_authRefresh ) ) ;
}
}
} else {
} else {
System . err . println ( "Future did not have key for authtoken" ) ;
throw new GoogleTasksException ( c . getString ( R . string . gtasks_error_accountManager ) ) ;
}
}
} catch ( Exception e ) {
} catch ( Exception e ) {
e . printStackTrace ( ) ;
throw new GoogleTasksException ( e . getLocalizedMessage ( ) ) ;
return null ;
}
}
} else {
throw new GoogleTasksException ( c . getString ( R . string . gtasks_error_authRefresh ) ) ;
}
}
}
System . err . println ( "Gtasks token validation fell through all logic" ) ;
} else {
return null ;
throw new GoogleTasksException ( c . getString ( R . string . gtasks_error_accountManager ) ) ;
}
} catch ( Exception e ) {
throw new GoogleTasksException ( e . getLocalizedMessage ( ) ) ;
}
}
}
}
}
}