package com.valtech.contactsync;

import android.accounts.Account;
import android.accounts.AccountAuthenticatorActivity;
import android.accounts.AccountManager;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.Window;
import android.webkit.CookieManager;
import android.webkit.WebView;
import android.webkit.WebViewClient;

import java.util.HashMap;

public class SignInActivity extends AccountAuthenticatorActivity {
  private static final String TAG = SignInActivity.class.getSimpleName();
  private static final int SYNC_INTERVAL = 4 * 3600 * 1000;

  private ApiClient apiClient = new ApiClient();

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    WebView webView = new WebView(this);
    WebViewClient client = new IdpWebViewClient();
    webView.getSettings().setJavaScriptEnabled(true);
    webView.getSettings().setSaveFormData(false);
    webView.setWebViewClient(client);
    CookieManager.getInstance().removeAllCookie();
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    setContentView(webView);

    webView.loadUrl(apiClient.getAuthorizeUrl(), new HashMap<String, String>() {{
      put("X-Idp-Client-Type", "native");
    }});
  }

  private class IdpWebViewClient extends WebViewClient {
    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
      return url.startsWith("vidp://");
    }

    @Override
    public void onPageStarted(WebView view, String url, Bitmap favicon) {
      if (!url.startsWith("vidp://")) {
        super.onPageStarted(view, url, favicon);
        return;
      }

      // clear cookies as the sign in step is now done
      CookieManager.getInstance().removeAllCookie();

      Uri uri = Uri.parse(url);
      final String code = uri.getQueryParameter("code");

      new SignInTask().execute(code);
    }
  }

  private class SignInTask extends AsyncTask<String, Void, Bundle> {
    @Override
    protected Bundle doInBackground(String... params) {
      ApiClient.TokenResponse tokenResponse = apiClient.getAccessTokenAndRefreshToken(params[0]);
      ApiClient.UserInfoResponse userInfoResponse = apiClient.getUserInfoMeResource(tokenResponse.accessToken);

      Log.i(TAG, "Signing in as user " + userInfoResponse.email + " in country " + userInfoResponse.countryCode);

      AccountManager accountManager = AccountManager.get(SignInActivity.this);
      Account account = new Account(userInfoResponse.email, getString(R.string.account_type));
      accountManager.addAccountExplicitly(account, tokenResponse.refreshToken, null);

//      ContentResolver.setIsSyncable(account, ContactsContract.AUTHORITY, 1);
//      ContentResolver.setSyncAutomatically(account, ContactsContract.AUTHORITY, true);

      //ContentResolver.addPeriodicSync(account, getString(R.string.account_authority), null, SYNC_INTERVAL);
//
//      Bundle settings = new Bundle();
//      settings.putBoolean(ContentResolver.SYNC_EXTRAS_MANUAL, true);
//      settings.putBoolean(ContentResolver.SYNC_EXTRAS_EXPEDITED, true);
//      ContentResolver.requestSync(account, getString(R.string.account_authority), settings);

      Bundle result = new Bundle();
      result.putString(AccountManager.KEY_ACCOUNT_NAME, userInfoResponse.email);
      result.putString(AccountManager.KEY_ACCOUNT_TYPE, getString(R.string.account_type));
      return result;
    }

    @Override
    protected void onPostExecute(Bundle result) {
      SignInActivity.this.setAccountAuthenticatorResult(result);
      SignInActivity.this.finish();
    }
  }
}

@@ -146,32 +146,72 @@
 AccountManager;%0A
+import android.content.ContentResolver;%0A
 import android.g
@@ -305,16 +305,58 @@
 Bundle;%0A
+import android.provider.ContactsContract;%0A
 import a
@@ -677,68 +677,8 @@
 e();
-%0A  private static final int SYNC_INTERVAL = 4 * 3600 * 1000;
 %0A%0A  
@@ -2621,16 +2621,32 @@
 );%0A     
+ boolean added =
  account
@@ -2723,477 +2723,623 @@
 );%0A%0A
-//
       
-ContentResolver.setIsSyncable(account, ContactsContract.AUTHORITY, 1);%0A//
+if (!added) %7B%0A        accountManager.setPassword(account, tokenResponse.refreshToken);%0A%0A
       
-ContentResolver.setSyncAutomatically(account, ContactsContract.AUTHORITY, true);%0A%0A      //ContentResolver.addPeriodicSync(account, getString(R.string.account_authority), null, SYNC_INTERVAL);%0A//%0A//      Bundle settings = new Bundle();%0A//      settings.putBoolean(ContentResolver.SYNC_EXTRAS_MANUAL, tru
+  // need to test getting an access token from the new refresh token to clear the%0A        // %22sign in error%22 notification%0A        try %7B%0A          String accessToken = accountManager.blockingGetAuthToken(account, %22access_token%22, true);%0A          accountManager.invalidateAuthToken(account.type, accessToken);%0A        %7D catch (Exception e) %7B%0A          Log.e(TAG, %22Could not get access token from new refresh token.%22, 
 e);%0A
-//
       
-settings.putBoolean(ContentResolver.SYNC_EXTRAS_EXPEDITED, true
+  %7D%0A      %7D%0A%0A      ContentResolver.setIsSyncable(account, ContactsContract.AUTHORITY, 1
 );%0A
-//
     
@@ -3360,76 +3360,70 @@
 ver.
-requestSync(account, getString(R.string.account_authority), settings
+setSyncAutomatically(account, ContactsContract.AUTHORITY, true
 );%0A%0A

Update password on existing account if the account already exists when signing in