package com.valtech.contactsync;

import android.accounts.*;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;

public class Authenticator extends AbstractAccountAuthenticator {
  private final Context context;

  public Authenticator(Context context) {
    super(context);
    this.context = context;
  }

  @Override
  public Bundle editProperties(AccountAuthenticatorResponse response, String accountType) {
    throw new UnsupportedOperationException();
  }

  @Override
  public Bundle addAccount(AccountAuthenticatorResponse response, String accountType, String authTokenType, String[] requiredFeatures, Bundle options) throws NetworkErrorException {
    Bundle result = new Bundle();
    Intent i = new Intent(context, SignInActivity.class);
    i.putExtra(AccountManager.KEY_ACCOUNT_AUTHENTICATOR_RESPONSE, response);
    result.putParcelable(AccountManager.KEY_INTENT, i);
    return result;
  }

  @Override
  public Bundle confirmCredentials(AccountAuthenticatorResponse response, Account account, Bundle options) throws NetworkErrorException {
    return null;
  }

  @Override
  public Bundle getAuthToken(AccountAuthenticatorResponse response, Account account, String authTokenType, Bundle options) throws NetworkErrorException {
    Bundle bundle = new Bundle();
    bundle.putString(AccountManager.KEY_ACCOUNT_NAME, account.name);
    bundle.putString(AccountManager.KEY_ACCOUNT_TYPE, context.getString(R.string.account_type));
    bundle.putString(AccountManager.KEY_AUTHTOKEN, AccountManager.get(context).getPassword(account));
    return bundle;
  }

  @Override
  public String getAuthTokenLabel(String authTokenType) {
    throw new UnsupportedOperationException();
  }

  @Override
  public Bundle updateCredentials(AccountAuthenticatorResponse response, Account account, String authTokenType, Bundle options) throws NetworkErrorException {
    throw new UnsupportedOperationException();
  }

  @Override
  public Bundle hasFeatures(AccountAuthenticatorResponse response, Account account, String[] features) throws NetworkErrorException {
    throw new UnsupportedOperationException();
  }
}

@@ -142,16 +142,41 @@
 .Bundle;
+%0Aimport android.util.Log;
 %0A%0Apublic
@@ -249,28 +249,139 @@
 ate 
-final Context contex
+static final String TAG = Authenticator.class.getSimpleName();%0A%0A  private final Context context;%0A  private final ApiClient apiClien
 t;%0A%0A
@@ -470,16 +470,54 @@
 ontext;%0A
+    this.apiClient = new ApiClient();%0A
   %7D%0A%0A  @
@@ -625,49 +625,19 @@
     
-throw new UnsupportedOperationException()
+return null
 ;%0A  
@@ -841,246 +841,36 @@
     
-Bundle result = new Bundle();%0A    Intent i = new Intent(context, SignInActivity.class);%0A    i.putExtra(AccountManager.KEY_ACCOUNT_AUTHENTICATOR_RESPONSE, response);%0A    result.putParcelable(AccountManager.KEY_INTENT, i);%0A    return result
+return startSignIn(response)
 ;%0A  
@@ -1218,38 +1218,286 @@
     
-Bundle bundle = new Bundle();%0A
+Log.i(TAG, %22Fetching new access token.%22);%0A    Bundle bundle = new Bundle();%0A%0A    try %7B%0A      String refreshToken = AccountManager.get(context).getPassword(account);%0A      String accessToken = apiClient.getAccessToken(refreshToken);%0A      Log.i(TAG, %22Got new access token.%22);%0A%0A  
     
@@ -1561,16 +1561,18 @@
 .name);%0A
+  
     bund
@@ -1660,24 +1660,26 @@
 type));%0A    
+  
 bundle.putSt
@@ -1717,59 +1717,368 @@
 EN, 
-AccountManager.get(context).getPassword(account)
+accessToken);%0A%0A      return bundle;%0A    %7D catch (ApiClient.InvalidGrantException e) %7B%0A      Log.i(TAG, %22Refresh token invalid.%22);%0A      return startSignIn(response);%0A    %7D catch (Exception e) %7B%0A      Log.e(TAG, %22Unknown error when fetching access token.%22, e);%0A      bundle.putInt(AccountManager.KEY_ERROR_CODE, AccountManager.ERROR_CODE_BAD_AUTHENTICATION
 );%0A
+  
     
@@ -2092,16 +2092,22 @@
 bundle;%0A
+    %7D%0A
   %7D%0A%0A  @
@@ -2181,49 +2181,19 @@
     
-throw new UnsupportedOperationException()
+return null
 ;%0A  
@@ -2374,49 +2374,19 @@
     
-throw new UnsupportedOperationException()
+return null
 ;%0A  
@@ -2542,49 +2542,338 @@
     
-throw new UnsupportedOperationException()
+return null;%0A  %7D%0A%0A  private Bundle startSignIn(AccountAuthenticatorResponse response) %7B%0A    Bundle result = new Bundle();%0A    Intent i = new Intent(context, SignInActivity.class);%0A    i.putExtra(AccountManager.KEY_ACCOUNT_AUTHENTICATOR_RESPONSE, response);%0A    result.putParcelable(AccountManager.KEY_INTENT, i);%0A    return result
 ;%0A  

Make getAuthToken() fetch new AT instead of returning RT