<commit_before>package douglas_carlos.museu.feevale.br.museumvisualizr.utils;

import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.os.AsyncTask;
import android.provider.Settings;
import android.util.Log;

import org.json.JSONArray;
import org.json.JSONException;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import java.util.List;

import douglas_carlos.museu.feevale.br.museumvisualizr.VisitsActivity;
import douglas_carlos.museu.feevale.br.museumvisualizr.utils.DBHelper;
import douglas_carlos.museu.feevale.br.museumvisualizr.utils.Visit;

public class Requester extends AsyncTask<Void, Void, Void> {

    private ProgressDialog progress;
    private Context context;
    private String data;

    protected final static String SERVICE_DOMAIN = "museumvisualizr.esy.es";

    public Requester(Context context) {
        this.progress = new ProgressDialog(context);;
        this.context = context;
        this.syncVisits();
        Log.d("JSON", data);
        this.execute();
    }

    private static JSONArray toJsonArray(List<Visit> visits) throws JSONException {
        JSONArray json = new JSONArray();
        for (Visit visit : visits) json.put(visit.toJson());
        return json;
    }

    public void syncVisits(){

        DBHelper helper = new DBHelper(context);
        helper.open();

        List<Visit> visits = Visit.allNotSynced(helper);
        try {

            data = toJsonArray(visits).toString();
            Visit.updateAsSynchronized(helper, visits);

        } catch (JSONException e) {
            e.printStackTrace();
        }

        helper.close();

    }

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        progress.setMessage("Sincronizando...");
        progress.show();
    }

    @Override
    protected Void doInBackground(Void... params) {
        try{

            StringBuilder values = new StringBuilder();
            values.append(URLEncoder.encode("json", "UTF-8"));
            values.append("=");
            values.append(URLEncoder.encode(data, "UTF-8"));

            values.append("&");

            values.append(URLEncoder.encode("android_id", "UTF-8"));
            values.append("=");
            values.append(URLEncoder.encode(Settings.Secure.ANDROID_ID, "UTF-8"));

            URL url = new URL("http://" + SERVICE_DOMAIN + "/sync.php");
            HttpURLConnection conn = (HttpURLConnection) url.openConnection();<commit_msg>checking if the list is synchronized<commit_after>package douglas_carlos.museu.feevale.br.museumvisualizr.utils;

import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.os.AsyncTask;
import android.provider.Settings;
import android.util.Log;
import android.widget.Toast;

import org.json.JSONArray;
import org.json.JSONException;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import java.util.List;

import douglas_carlos.museu.feevale.br.museumvisualizr.VisitsActivity;
import douglas_carlos.museu.feevale.br.museumvisualizr.utils.DBHelper;
import douglas_carlos.museu.feevale.br.museumvisualizr.utils.Visit;

public class Requester extends AsyncTask<Void, Void, Void> {

    private ProgressDialog progress;
    private Context context;
    private String data;

    protected final static String SERVICE_DOMAIN = "museumvisualizr.esy.es";

    public Requester(Context context) {
        this.progress = new ProgressDialog(context);;
        this.context = context;
        this.syncVisits();
    }

    private static JSONArray toJsonArray(List<Visit> visits) throws JSONException {
        JSONArray json = new JSONArray();
        for (Visit visit : visits) json.put(visit.toJson());
        return json;
    }

    public void syncVisits(){

        DBHelper helper = new DBHelper(context);
        helper.open();

        List<Visit> visits = Visit.allNotSynced(helper);

        if(visits.size()>0) {
            try {

                data = toJsonArray(visits).toString();
                Visit.updateAsSynchronized(helper, visits);
                Log.d("JSON", data);
                this.execute();

            } catch (JSONException e) {
                e.printStackTrace();
            }
        }else{
            Toast.makeText(context, "As visitas já estão sincronizadas!", Toast.LENGTH_LONG).show();
        }
        helper.close();

    }

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        progress.setMessage("Sincronizando...");
        progress.show();
    }

    @Override
    protected Void doInBackground(Void... params) {
        try{

            StringBuilder values = new StringBuilder();
            values.append(URLEncoder.encode("json", "UTF-8"));
            values.append("=");
            values.append(URLEncoder.encode(data, "UTF-8"));

            values.append("&");

            values.append(URLEncoder.encode("android_id", "UTF-8"));
            values.append("=");
            values.append(URLEncoder.encode(Settings.Secure.ANDROID_ID, "UTF-8"));

            URL url = new URL("http://" + SERVICE_DOMAIN + "/sync.php");
            HttpURLConnection conn = (HttpURLConnection) url.openConnection();
{'fraction_non_alphanumeric': 0.11635331677899964, 'fraction_numerical': 0.0015963107484923732, 'mean_word_length': 2.913254684247051, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 10, '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': '7253935', 'n_tokens_mistral': 1696, 'n_tokens_neox': 1637, 'n_words': 321}