<commit_before>package org.ovirt.mobile.movirt.ui;

import android.accounts.Account;
import android.accounts.AccountAuthenticatorActivity;
import android.accounts.AccountManager;
import android.content.ContentResolver;
import android.content.Intent;
import android.text.TextUtils;
import android.view.View;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ProgressBar;
import android.widget.Toast;

import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.Background;
import org.androidannotations.annotations.Bean;
import org.androidannotations.annotations.Click;
import org.androidannotations.annotations.EActivity;
import org.androidannotations.annotations.Receiver;
import org.androidannotations.annotations.SystemService;
import org.androidannotations.annotations.UiThread;
import org.androidannotations.annotations.ViewById;
import org.ovirt.mobile.movirt.Broadcasts;
import org.ovirt.mobile.movirt.R;
import org.ovirt.mobile.movirt.auth.MovirtAuthenticator;
import org.ovirt.mobile.movirt.provider.OVirtContract;
import org.ovirt.mobile.movirt.rest.OVirtClient;
import org.ovirt.mobile.movirt.sync.EventsHandler;
import org.ovirt.mobile.movirt.sync.SyncUtils;

@EActivity(R.layout.authenticator_activity)
public class AuthenticatorActivity extends AccountAuthenticatorActivity {

    @SystemService
    AccountManager accountManager;

    @Bean
    OVirtClient client;

    @ViewById
    EditText txtEndpoint;

    @ViewById
    EditText txtUsername;

    @ViewById
    EditText txtPassword;

    @ViewById
    CheckBox chkAdminPriv;

    @ViewById
    CheckBox chkDisableHttps;

    @ViewById
    CheckBox enforceHttpBasicAuth;

    @ViewById
    ProgressBar authProgress;

    @Bean
    MovirtAuthenticator authenticator;

    @Bean
    SyncUtils syncUtils;

    @Bean
    EventsHandler eventsHandler;

    @AfterViews
    void init() {
        txtEndpoint.setText(authenticator.getApiUrl());
        txtUsername.setText(authenticator.getUserName());
        txtPassword.setText(authenticator.getPassword());

        chkAdminPriv.setChecked(authenticator.hasAdminPermissions());
        chkDisableHttps.setChecked(authenticator.disableHttps());
        enforceHttpBasicAuth.setChecked(authenticator.enforceBasicAuth());

    }

    @Click(R.id.btnCreate)
    void addNew() {
        String endpoint = txtEndpoint.getText().toString();
        String username = txtUsername.getText().toString();
        String password = txtPassword.getText().toString();

        Boolean adminPriv = chkAdminPriv.isChecked();
        Boolean disableHttps = chkDisableHttps.isChecked();
        Boolean enforceHttpBasic = enforceHttpBasicAuth.isChecked();

        finishLogin(endpoint, username, password, adminPriv, disableHttps, enforceHttpBasic);
    }

    @Background
    void finishLogin(String apiUrl, String name, String password, Boolean hasAdminPermissions, Boolean disableHttps, Boolean enforceHttpBasic) {
        boolean endpointChanged = false;
        if (!TextUtils.equals(apiUrl, authenticator.getApiUrl()) ||
                !TextUtils.equals(name, authenticator.getUserName())) {
            endpointChanged = true;
        }

        accountManager.addAccountExplicitly(MovirtAuthenticator.MOVIRT_ACCOUNT, password, null);
        ContentResolver.setSyncAutomatically(MovirtAuthenticator.MOVIRT_ACCOUNT, OVirtContract.CONTENT_AUTHORITY, true);

        setUserData(MovirtAuthenticator.MOVIRT_ACCOUNT, apiUrl, name, password, hasAdminPermissions, disableHttps, enforceHttpBasic);

        changeProgressVisibilityTo(View.VISIBLE);
        String token = "";
        boolean success = true;
        try {
            token = client.login(apiUrl, name, password, disableHttps, hasAdminPermissions);
        } catch (Exception e) {
            showToast("Error logging in: " + e.getMessage());
            success = false;
            return;
        } finally {
            changeProgressVisibilityTo(View.GONE);
            if (success) {
                if (TextUtils.isEmpty(token)) {
                    showToast("Error: the returned token is empty");
                    return;
                } else {
                    showToast("Login successful");
                    if (endpointChanged) {
                        // there is a different set of events and since we are counting only the increments, this ones are not needed anymore
                        eventsHandler.deleteEvents();
                    }

                    syncUtils.triggerRefresh();
                }
            } else {<commit_msg>Added also the ContentResolver.setIsSyncable(MovirtAuthenticator.MOVIRT_ACCOUNT, OVirtContract.CONTENT_AUTHORITY, 1); because on some devices the syncing did not happen<commit_after>package org.ovirt.mobile.movirt.ui;

import android.accounts.Account;
import android.accounts.AccountAuthenticatorActivity;
import android.accounts.AccountManager;
import android.content.ContentResolver;
import android.content.Intent;
import android.text.TextUtils;
import android.view.View;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ProgressBar;
import android.widget.Toast;

import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.Background;
import org.androidannotations.annotations.Bean;
import org.androidannotations.annotations.Click;
import org.androidannotations.annotations.EActivity;
import org.androidannotations.annotations.Receiver;
import org.androidannotations.annotations.SystemService;
import org.androidannotations.annotations.UiThread;
import org.androidannotations.annotations.ViewById;
import org.ovirt.mobile.movirt.Broadcasts;
import org.ovirt.mobile.movirt.R;
import org.ovirt.mobile.movirt.auth.MovirtAuthenticator;
import org.ovirt.mobile.movirt.provider.OVirtContract;
import org.ovirt.mobile.movirt.rest.OVirtClient;
import org.ovirt.mobile.movirt.sync.EventsHandler;
import org.ovirt.mobile.movirt.sync.SyncUtils;

@EActivity(R.layout.authenticator_activity)
public class AuthenticatorActivity extends AccountAuthenticatorActivity {

    @SystemService
    AccountManager accountManager;

    @Bean
    OVirtClient client;

    @ViewById
    EditText txtEndpoint;

    @ViewById
    EditText txtUsername;

    @ViewById
    EditText txtPassword;

    @ViewById
    CheckBox chkAdminPriv;

    @ViewById
    CheckBox chkDisableHttps;

    @ViewById
    CheckBox enforceHttpBasicAuth;

    @ViewById
    ProgressBar authProgress;

    @Bean
    MovirtAuthenticator authenticator;

    @Bean
    SyncUtils syncUtils;

    @Bean
    EventsHandler eventsHandler;

    @AfterViews
    void init() {
        txtEndpoint.setText(authenticator.getApiUrl());
        txtUsername.setText(authenticator.getUserName());
        txtPassword.setText(authenticator.getPassword());

        chkAdminPriv.setChecked(authenticator.hasAdminPermissions());
        chkDisableHttps.setChecked(authenticator.disableHttps());
        enforceHttpBasicAuth.setChecked(authenticator.enforceBasicAuth());

    }

    @Click(R.id.btnCreate)
    void addNew() {
        String endpoint = txtEndpoint.getText().toString();
        String username = txtUsername.getText().toString();
        String password = txtPassword.getText().toString();

        Boolean adminPriv = chkAdminPriv.isChecked();
        Boolean disableHttps = chkDisableHttps.isChecked();
        Boolean enforceHttpBasic = enforceHttpBasicAuth.isChecked();

        finishLogin(endpoint, username, password, adminPriv, disableHttps, enforceHttpBasic);
    }

    @Background
    void finishLogin(String apiUrl, String name, String password, Boolean hasAdminPermissions, Boolean disableHttps, Boolean enforceHttpBasic) {
        boolean endpointChanged = false;
        if (!TextUtils.equals(apiUrl, authenticator.getApiUrl()) ||
                !TextUtils.equals(name, authenticator.getUserName())) {
            endpointChanged = true;
        }

        if (accountManager.getAccountsByType(MovirtAuthenticator.ACCOUNT_TYPE).length == 0) {
            accountManager.addAccountExplicitly(MovirtAuthenticator.MOVIRT_ACCOUNT, password, null);
        }

        ContentResolver.setSyncAutomatically(MovirtAuthenticator.MOVIRT_ACCOUNT, OVirtContract.CONTENT_AUTHORITY, true);
        ContentResolver.setIsSyncable(MovirtAuthenticator.MOVIRT_ACCOUNT, OVirtContract.CONTENT_AUTHORITY, 1);

        setUserData(MovirtAuthenticator.MOVIRT_ACCOUNT, apiUrl, name, password, hasAdminPermissions, disableHttps, enforceHttpBasic);

        changeProgressVisibilityTo(View.VISIBLE);
        String token = "";
        boolean success = true;
        try {
            token = client.login(apiUrl, name, password, disableHttps, hasAdminPermissions);
        } catch (Exception e) {
            showToast("Error logging in: " + e.getMessage());
            success = false;
            return;
        } finally {
            changeProgressVisibilityTo(View.GONE);
            if (success) {
                if (TextUtils.isEmpty(token)) {
                    showToast("Error: the returned token is empty");
                    return;
                } else {
                    showToast("Login successful");
                    if (endpointChanged) {
                        // there is a different set of events and since we are counting only the increments, this ones are not needed anymore
                        eventsHandler.deleteEvents();
                    }

                    syncUtils.triggerRefresh();
                }
            } else {
{'fraction_non_alphanumeric': 0.0849754978625795, 'fraction_numerical': 0.00031279324366593683, 'mean_word_length': 3.293643688451209, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '4266376', 'n_tokens_mistral': 2580, 'n_tokens_neox': 2458, 'n_words': 492}