<commit_before>package com.rehivetech.beeeon.network.authentication;

import android.Manifest;
import android.accounts.Account;
import android.accounts.AccountManager;
import android.annotation.TargetApi;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.net.http.SslError;
import android.os.AsyncTask;
import android.os.Build;
import android.support.annotation.RequiresPermission;
import android.util.Log;
import android.view.View;
import android.webkit.SslErrorHandler;
import android.webkit.WebResourceError;
import android.webkit.WebResourceRequest;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Toast;

import com.google.android.gms.auth.GoogleAuthException;
import com.google.android.gms.auth.GoogleAuthUtil;
import com.google.android.gms.auth.UserRecoverableAuthException;
import com.google.android.gms.common.AccountPicker;
import com.rehivetech.beeeon.R;
import com.rehivetech.beeeon.exception.AppException;
import com.rehivetech.beeeon.gui.activity.LoginActivity;
import com.rehivetech.beeeon.gui.activity.WebAuthActivity;
import com.rehivetech.beeeon.util.Utils;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import static android.R.attr.description;

public class GoogleAuthProvider implements IAuthProvider {
	private static final String TAG = GoogleAuthProvider.class.getSimpleName();

	// This ID must be unique amongst all providers
	public static final int PROVIDER_ID = 201;

	private static final String PROVIDER_NAME = "google";

	private static final String PARAMETER_TOKEN = "token";

	private static final String AUTH_INTENT_DATA_TOKEN = "token";

	private static final String AUTH_INTENT_DATA_EMAIL = "email";

	private static final String SCOPE = "oauth2:https://www.googleapis.com/auth/userinfo.profile https://www.googleapis.com/auth/userinfo.email";

	private static Map<String, String> mParameters = new HashMap<>();

	private String mEmail = "";

	@Override
	public boolean isDemo() {
		return false;
	}

	@Override
	public String getProviderName() {
		return PROVIDER_NAME;
	}

	@Override
	public Map<String, String> getParameters() {
		return mParameters;
	}

	@Override
	public String getPrimaryParameter() {
		return mEmail;
	}

	@Override
	public void setPrimaryParameter(String parameter) {
		mEmail = parameter;
	}

	@Override
	public boolean loadAuthIntent(Intent data) {
		String email = data.getStringExtra(AUTH_INTENT_DATA_EMAIL);
		if (email != null)
			mEmail = email;

		String token = data.getStringExtra(AUTH_INTENT_DATA_TOKEN);
		if (token == null)
			return false;

		mParameters.put(PARAMETER_TOKEN, token);
		return true;
	}

	@Override
	public void prepareAuth(final LoginActivity activity) {
		if (!Utils.isGooglePlayServicesAvailable(activity))
			webloginAuth(activity);
		else
			androidAuth(activity);
	}

	/**
	 * Helper for invalidating Google authentication token.
	 *
	 * @param context app context
	 */
	public void invalidateToken(Context context) {
		try {
			GoogleAuthUtil.clearToken(context, mParameters.get(PARAMETER_TOKEN));
		} catch (GoogleAuthException | IOException e) {
			e.printStackTrace();
		}
	}

	/**
	 * Method mine users account names
	 *
	 * @return array of names to choose
	 */
	@RequiresPermission(Manifest.permission.GET_ACCOUNTS)
	private String[] getAccountNames(Context context) {
		AccountManager mAccountManager = AccountManager.get(context);
		Account[] accounts = mAccountManager.getAccountsByType(GoogleAuthUtil.GOOGLE_ACCOUNT_TYPE);
		String[] names = new String[accounts.length];
		for (int i = 0; i < names.length; i++) {
			names[i] = accounts[i].name;
		}
		return names;
	}

	private void webloginAuth(final LoginActivity activity) {
		Log.d(TAG, "Start webloginAuth");

		final Intent intent = new Intent(activity, WebAuthActivity.class);
		intent.putExtra(WebAuthActivity.EXTRA_PROVIDER_ID, PROVIDER_ID);

		// Start activity and let user login via web
		activity.startActivityForResult(intent, PROVIDER_ID);
	}

	private void androidAuth(final LoginActivity activity) {
		Log.d(TAG, "Start androidAuth");

		String[] accounts = this.getAccountNames(activity);
		if (!mEmail.isEmpty()) {
			// Check if this e-mail still exists on this device
			boolean found = false;
			for (String account : accounts) {
				if (account.equalsIgnoreCase(mEmail)) {
					found = true;
					break;
				}
			}

			// If this email was not found, delete it and let user choose the one again
			if (!found)
				mEmail = "";
		}

		if (mEmail.isEmpty()) {

			Log.d(TAG, String.format("Found number of accounts on this device: %d", accounts.length));

			if (accounts.length == 1) {
				// Set the only one email account used
				mEmail = accounts[0];
			} else {
				// Start activity and let user choose the email account to use
				Intent intent = AccountPicker.newChooseAccountIntent(null, null, new String[]{"com.google"}, false, null, null, null, null);
				activity.startActivityForResult(intent, PROVIDER_ID);
				return; // we need to finish initialization in activity's onResult
			}
		}

		if (!mEmail.isEmpty()) {
			new Thread(new Runnable() {
				@Override
				public void run() {
					try {
						// Load token from Google server and save it
						String token = GoogleAuthUtil.getToken(activity, new Account(mEmail, "user"), SCOPE);

						Intent data = new Intent();
						data.putExtra(AUTH_INTENT_DATA_TOKEN, token);

						// Remember also primary email this provider was started with
						data.putExtra(AUTH_INTENT_DATA_EMAIL, mEmail);

						// Report success to caller
						activity.onActivityResult(PROVIDER_ID, IAuthProvider.RESULT_AUTH, data);
					} catch (UserRecoverableAuthException e) {
						Intent intent = e.getIntent();
						if (intent != null) {
							activity.startActivityForResult(intent, PROVIDER_ID);
						}
					} catch (GoogleAuthException | IOException e) {
						// Return error to caller
						activity.onActivityResult(PROVIDER_ID, IAuthProvider.RESULT_ERROR, null);
					}<commit_msg>Roll back old getToken method, to fix google login problem<commit_after>package com.rehivetech.beeeon.network.authentication;

import android.Manifest;
import android.accounts.Account;
import android.accounts.AccountManager;
import android.annotation.TargetApi;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.net.http.SslError;
import android.os.AsyncTask;
import android.os.Build;
import android.support.annotation.RequiresPermission;
import android.util.Log;
import android.view.View;
import android.webkit.SslErrorHandler;
import android.webkit.WebResourceError;
import android.webkit.WebResourceRequest;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Toast;

import com.google.android.gms.auth.GoogleAuthException;
import com.google.android.gms.auth.GoogleAuthUtil;
import com.google.android.gms.auth.UserRecoverableAuthException;
import com.google.android.gms.common.AccountPicker;
import com.rehivetech.beeeon.R;
import com.rehivetech.beeeon.exception.AppException;
import com.rehivetech.beeeon.gui.activity.LoginActivity;
import com.rehivetech.beeeon.gui.activity.WebAuthActivity;
import com.rehivetech.beeeon.util.Utils;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import static android.R.attr.description;

public class GoogleAuthProvider implements IAuthProvider {
	private static final String TAG = GoogleAuthProvider.class.getSimpleName();

	// This ID must be unique amongst all providers
	public static final int PROVIDER_ID = 201;

	private static final String PROVIDER_NAME = "google";

	private static final String PARAMETER_TOKEN = "token";

	private static final String AUTH_INTENT_DATA_TOKEN = "token";

	private static final String AUTH_INTENT_DATA_EMAIL = "email";

	private static final String SCOPE = "oauth2:https://www.googleapis.com/auth/userinfo.profile https://www.googleapis.com/auth/userinfo.email";

	private static Map<String, String> mParameters = new HashMap<>();

	private String mEmail = "";

	@Override
	public boolean isDemo() {
		return false;
	}

	@Override
	public String getProviderName() {
		return PROVIDER_NAME;
	}

	@Override
	public Map<String, String> getParameters() {
		return mParameters;
	}

	@Override
	public String getPrimaryParameter() {
		return mEmail;
	}

	@Override
	public void setPrimaryParameter(String parameter) {
		mEmail = parameter;
	}

	@Override
	public boolean loadAuthIntent(Intent data) {
		String email = data.getStringExtra(AUTH_INTENT_DATA_EMAIL);
		if (email != null)
			mEmail = email;

		String token = data.getStringExtra(AUTH_INTENT_DATA_TOKEN);
		if (token == null)
			return false;

		mParameters.put(PARAMETER_TOKEN, token);
		return true;
	}

	@Override
	public void prepareAuth(final LoginActivity activity) {
		if (!Utils.isGooglePlayServicesAvailable(activity))
			webloginAuth(activity);
		else
			androidAuth(activity);
	}

	/**
	 * Helper for invalidating Google authentication token.
	 *
	 * @param context app context
	 */
	public void invalidateToken(Context context) {
		try {
			GoogleAuthUtil.clearToken(context, mParameters.get(PARAMETER_TOKEN));
		} catch (GoogleAuthException | IOException e) {
			e.printStackTrace();
		}
	}

	/**
	 * Method mine users account names
	 *
	 * @return array of names to choose
	 */
	@RequiresPermission(Manifest.permission.GET_ACCOUNTS)
	private String[] getAccountNames(Context context) {
		AccountManager mAccountManager = AccountManager.get(context);
		Account[] accounts = mAccountManager.getAccountsByType(GoogleAuthUtil.GOOGLE_ACCOUNT_TYPE);
		String[] names = new String[accounts.length];
		for (int i = 0; i < names.length; i++) {
			names[i] = accounts[i].name;
		}
		return names;
	}

	private void webloginAuth(final LoginActivity activity) {
		Log.d(TAG, "Start webloginAuth");

		final Intent intent = new Intent(activity, WebAuthActivity.class);
		intent.putExtra(WebAuthActivity.EXTRA_PROVIDER_ID, PROVIDER_ID);

		// Start activity and let user login via web
		activity.startActivityForResult(intent, PROVIDER_ID);
	}

	private void androidAuth(final LoginActivity activity) {
		Log.d(TAG, "Start androidAuth");

		String[] accounts = this.getAccountNames(activity);
		if (!mEmail.isEmpty()) {
			// Check if this e-mail still exists on this device
			boolean found = false;
			for (String account : accounts) {
				if (account.equalsIgnoreCase(mEmail)) {
					found = true;
					break;
				}
			}

			// If this email was not found, delete it and let user choose the one again
			if (!found)
				mEmail = "";
		}

		if (mEmail.isEmpty()) {

			Log.d(TAG, String.format("Found number of accounts on this device: %d", accounts.length));

			if (accounts.length == 1) {
				// Set the only one email account used
				mEmail = accounts[0];
			} else {
				// Start activity and let user choose the email account to use
				Intent intent = AccountPicker.newChooseAccountIntent(null, null, new String[]{"com.google"}, false, null, null, null, null);
				activity.startActivityForResult(intent, PROVIDER_ID);
				return; // we need to finish initialization in activity's onResult
			}
		}

		if (!mEmail.isEmpty()) {
			new Thread(new Runnable() {
				@Override
				public void run() {
					try {
						// Load token from Google server and save it
						String token = GoogleAuthUtil.getToken(activity, mEmail, SCOPE);

						Intent data = new Intent();
						data.putExtra(AUTH_INTENT_DATA_TOKEN, token);

						// Remember also primary email this provider was started with
						data.putExtra(AUTH_INTENT_DATA_EMAIL, mEmail);

						// Report success to caller
						activity.onActivityResult(PROVIDER_ID, IAuthProvider.RESULT_AUTH, data);
					} catch (UserRecoverableAuthException e) {
						Intent intent = e.getIntent();
						if (intent != null) {
							activity.startActivityForResult(intent, PROVIDER_ID);
						}
					} catch (GoogleAuthException | IOException e) {
						// Return error to caller
						activity.onActivityResult(PROVIDER_ID, IAuthProvider.RESULT_ERROR, null);
					}
{'fraction_non_alphanumeric': 0.09970578620464204, 'fraction_numerical': 0.0011441647597254005, 'mean_word_length': 4.353018372703412, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 9, 'https://': 4, 'lorem ipsum': 0, 'www.': 4, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5715518', 'n_tokens_mistral': 4102, 'n_tokens_neox': 3618, 'n_words': 949}