<commit_before>package swevoq.ebread.com.Chat.View.Settings;

import android.content.Context;
import android.content.Intent;
import android.os.Environment;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.Spinner;
import android.widget.Switch;
import android.widget.TextView;
import android.widget.Toast;

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

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;

import swevoq.ebread.com.Chat.Model.Chat.TextMessage;
import swevoq.ebread.com.Chat.Model.Settings.VoiceSettings;
import swevoq.ebread.com.Chat.Presenter.Settings.VoiceSettingsPresenter;
import swevoq.ebread.com.Chat.View.Utility.ChatListActivity;
import swevoq.ebread.com.Libraries.FATTSLib.FATTSPlayer;
import swevoq.ebread.com.Libraries.FATTSLib.FATTSServices;
import swevoq.ebread.com.R;

public class VoiceSettingsActivity extends AppCompatActivity {
    private VoiceSettingsPresenter presenter;
    private Context context;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_voice_settings);
        context = this;
        ActionBar actionBar = getSupportActionBar();
        actionBar.setTitle("Impostazioni voce di sintesi");
        presenter = new VoiceSettingsPresenter();
        VoiceSettings usersVoiceSettings = presenter.getVoiceSettings(this);
        FATTSServices service= new FATTSServices(this);

        final Spinner defaultVoiceSpinner = (Spinner)findViewById(R.id.defaultVoiceSpinner);
        String[] voiceList;
        ArrayAdapter<CharSequence> voiceSpinnerAdapter;
        //Prova a leggere le voci disponibili da JSON. In caso di errori, legge una lista predefinita.
        try{
            //Popolo un nuovo file JSON
            service.getVoices();
            //Leggo il file JSON
            InputStream input = new FileInputStream(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS) + "/voices.json");
            int size=input.available();
            byte[] buffer=new byte[size];
            input.read(buffer);
            input.close();
            String voicesJson = new String(buffer, "UTF-8");
            JSONObject voices=new JSONObject(voicesJson);

            JSONArray voiceArray = voices.getJSONArray("voices");
            voiceList= new String[voiceArray.length()];
            for(int i=0; i<voiceArray.length(); i++){
                JSONObject v=voiceArray.getJSONObject(i);
                voiceList[i]=v.getString("id");
            }

            voiceSpinnerAdapter= new ArrayAdapter<CharSequence>(this, android.R.layout.simple_spinner_item, voiceList);
        } catch(Exception e){
            voiceSpinnerAdapter = android.widget.ArrayAdapter.createFromResource(this, R.array.voices_array, android.R.layout.simple_spinner_item);
        }
        voiceSpinnerAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        defaultVoiceSpinner.setAdapter(voiceSpinnerAdapter);
        defaultVoiceSpinner.setSelection(voiceSpinnerAdapter.getPosition(usersVoiceSettings.getVoiceName()));

        final Spinner languageVoiceSpinner = (Spinner)findViewById(R.id.languageVoiceSpinner);
        String[] langList;
        ArrayAdapter<CharSequence> languageVoiceSpinnerAdapter;
        //Come sopra per le voci, ma stavolta leggo le lingue
        try{
            //Popolo un nuovo file JSON
            service.getLanguages();
            //Leggo il file JSON
            InputStream input = new FileInputStream(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS) + "/langs.json");
            int size=input.available();
            byte[] buffer=new byte[size];
            input.read(buffer);
            input.close();
            String langsJson = new String(buffer, "UTF-8");
            JSONObject langs=new JSONObject(langsJson);

            JSONArray langArray = langs.getJSONArray("locales");
            langList= new String[langArray.length()];
            for (int i=0; i<langArray.length(); i++) {
                langList[i]=langArray.getString(i);
            }

            languageVoiceSpinnerAdapter= new ArrayAdapter<CharSequence>(this, android.R.layout.simple_spinner_item, langList);
        } catch(Exception e){
            languageVoiceSpinnerAdapter = android.widget.ArrayAdapter.createFromResource(this, R.array.voices_languages_array, android.R.layout.simple_spinner_item);
        }
        languageVoiceSpinnerAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        languageVoiceSpinner.setAdapter(languageVoiceSpinnerAdapter);
        languageVoiceSpinner.setSelection(languageVoiceSpinnerAdapter.getPosition(usersVoiceSettings.getVoiceLanguage()));

        defaultVoiceSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                try{
                    //Metodo brutto fatto velocemente ma dovrebbe funzionare
                    InputStream input = new FileInputStream(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS) + "/voices.json");
                    int size=input.available();
                    byte[] buffer=new byte[size];
                    input.read(buffer);
                    input.close();
                    String voicesJson = new String(buffer, "UTF-8");
                    JSONObject voices=new JSONObject(voicesJson);
                    JSONArray voiceArray = voices.getJSONArray("voices");

                    input = new FileInputStream(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS) + "/langs.json");
                    size=input.available();
                    buffer=new byte[size];
                    input.read(buffer);
                    input.close();
                    String langsJson = new String(buffer, "UTF-8");
                    JSONObject langs=new JSONObject(langsJson);
                    JSONArray langArray = langs.getJSONArray("locales");

                    String currentVoice=defaultVoiceSpinner.getSelectedItem().toString();
                    for(int i=0; i<voiceArray.length(); i++){
                        JSONObject v=voiceArray.getJSONObject(i);
                        if(currentVoice==v.getString("id")){
                            JSONArray l=v.getJSONArray("locales");
                            JSONObject a=l.getJSONObject(0);
                            String ll=a.toString();
                            for (int j=0; j<langArray.length(); j++) {
                                if (ll == langArray.getString(j)) {
                                    languageVoiceSpinner.setSelection(j);
                                }
                            }
                        }
                    }

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

            @Override
            public void onNothingSelected(AdapterView<?> parent) {}
        });

        final Spinner speedVoiceSpinner = (Spinner)findViewById(R.id.speedVoiceSpinner);
        ArrayAdapter<CharSequence> speedVoiceSpinnerAdapter = android.widget.ArrayAdapter.createFromResource(this, R.array.voices_speed_array, android.R.layout.simple_spinner_item);
        speedVoiceSpinnerAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        speedVoiceSpinner.setAdapter(speedVoiceSpinnerAdapter);
        if(usersVoiceSettings.getVoiceRate() == 1.5)
            speedVoiceSpinner.setSelection(speedVoiceSpinnerAdapter.getPosition("lento"));
        else if(usersVoiceSettings.getVoiceRate() == 1.0)
            speedVoiceSpinner.setSelection(speedVoiceSpinnerAdapter.getPosition("normale"));
        else if(usersVoiceSettings.getVoiceRate() == 0.7)
            speedVoiceSpinner.setSelection(speedVoiceSpinnerAdapter.getPosition("veloce"));
        else if(usersVoiceSettings.getVoiceRate() == 0.4)
            speedVoiceSpinner.setSelection(speedVoiceSpinnerAdapter.getPosition("super veloce"));

<commit_msg>Not bugged anymore<commit_after>package swevoq.ebread.com.Chat.View.Settings;

import android.content.Context;
import android.content.Intent;
import android.os.Environment;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.Spinner;
import android.widget.Switch;
import android.widget.TextView;
import android.widget.Toast;

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

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;

import swevoq.ebread.com.Chat.Model.Chat.TextMessage;
import swevoq.ebread.com.Chat.Model.Settings.VoiceSettings;
import swevoq.ebread.com.Chat.Presenter.Settings.VoiceSettingsPresenter;
import swevoq.ebread.com.Chat.View.Utility.ChatListActivity;
import swevoq.ebread.com.Libraries.FATTSLib.FATTSPlayer;
import swevoq.ebread.com.Libraries.FATTSLib.FATTSServices;
import swevoq.ebread.com.R;

public class VoiceSettingsActivity extends AppCompatActivity {
    private VoiceSettingsPresenter presenter;
    private Context context;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_voice_settings);
        context = this;
        ActionBar actionBar = getSupportActionBar();
        actionBar.setTitle("Impostazioni voce di sintesi");
        presenter = new VoiceSettingsPresenter();
        VoiceSettings usersVoiceSettings = presenter.getVoiceSettings(this);
        FATTSServices service= new FATTSServices(this);

        final Spinner defaultVoiceSpinner = (Spinner)findViewById(R.id.defaultVoiceSpinner);
        String[] voiceList;
        ArrayAdapter<CharSequence> voiceSpinnerAdapter;
        //Prova a leggere le voci disponibili da JSON. In caso di errori, legge una lista predefinita.
        try{
            //Popolo un nuovo file JSON
            service.getVoices();
            //Leggo il file JSON
            InputStream input = new FileInputStream(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS) + "/voices.json");
            int size=input.available();
            byte[] buffer=new byte[size];
            input.read(buffer);
            input.close();
            String voicesJson = new String(buffer, "UTF-8");
            JSONObject voices=new JSONObject(voicesJson);

            JSONArray voiceArray = voices.getJSONArray("voices");
            voiceList= new String[voiceArray.length()];
            for(int i=0; i<voiceArray.length(); i++){
                JSONObject v=voiceArray.getJSONObject(i);
                voiceList[i]=v.getString("id");
            }

            voiceSpinnerAdapter= new ArrayAdapter<CharSequence>(this, android.R.layout.simple_spinner_item, voiceList);
        } catch(Exception e){
            voiceSpinnerAdapter = android.widget.ArrayAdapter.createFromResource(this, R.array.voices_array, android.R.layout.simple_spinner_item);
        }
        voiceSpinnerAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        defaultVoiceSpinner.setAdapter(voiceSpinnerAdapter);
        defaultVoiceSpinner.setSelection(voiceSpinnerAdapter.getPosition(usersVoiceSettings.getVoiceName()));

        final Spinner languageVoiceSpinner = (Spinner)findViewById(R.id.languageVoiceSpinner);
        String[] langList;
        ArrayAdapter<CharSequence> languageVoiceSpinnerAdapter;
        //Come sopra per le voci, ma stavolta leggo le lingue
        try{
            //Popolo un nuovo file JSON
            service.getLanguages();
            //Leggo il file JSON
            InputStream input = new FileInputStream(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS) + "/langs.json");
            int size=input.available();
            byte[] buffer=new byte[size];
            input.read(buffer);
            input.close();
            String langsJson = new String(buffer, "UTF-8");
            JSONObject langs=new JSONObject(langsJson);

            JSONArray langArray = langs.getJSONArray("locales");
            langList= new String[langArray.length()];
            for (int i=0; i<langArray.length(); i++) {
                langList[i]=langArray.getString(i);
            }

            languageVoiceSpinnerAdapter= new ArrayAdapter<CharSequence>(this, android.R.layout.simple_spinner_item, langList);
        } catch(Exception e){
            languageVoiceSpinnerAdapter = android.widget.ArrayAdapter.createFromResource(this, R.array.voices_languages_array, android.R.layout.simple_spinner_item);
        }
        languageVoiceSpinnerAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        languageVoiceSpinner.setAdapter(languageVoiceSpinnerAdapter);
        languageVoiceSpinner.setSelection(languageVoiceSpinnerAdapter.getPosition(usersVoiceSettings.getVoiceLanguage()));

        defaultVoiceSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                try{
                    //Metodo brutto fatto velocemente ma dovrebbe funzionare
                    InputStream input = new FileInputStream(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS) + "/voices.json");
                    int size=input.available();
                    byte[] buffer=new byte[size];
                    input.read(buffer);
                    input.close();
                    String voicesJson = new String(buffer, "UTF-8");
                    JSONObject voices=new JSONObject(voicesJson);
                    JSONArray voiceArray = voices.getJSONArray("voices");

                    input = new FileInputStream(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS) + "/langs.json");
                    size=input.available();
                    buffer=new byte[size];
                    input.read(buffer);
                    input.close();
                    String langsJson = new String(buffer, "UTF-8");
                    JSONObject langs=new JSONObject(langsJson);
                    JSONArray langArray = langs.getJSONArray("locales");

                    position=defaultVoiceSpinner.getSelectedItemPosition();
                    String currentVoice=defaultVoiceSpinner.getItemAtPosition(position).toString();
                    for(int i=0; i<voiceArray.length(); i++) {
                        JSONObject v = voiceArray.getJSONObject(i);
                        String voice = v.getString("id");
                        if (currentVoice.equals(voice)) {
                            JSONArray a = v.getJSONArray("locales");
                            String ll = a.getString(0);
                            //Toast.makeText(context,ll,Toast.LENGTH_SHORT).show();
                            for (int j = 0; j < langArray.length(); j++) {
                                if (ll.equals(langArray.getString(j))) {
                                    languageVoiceSpinner.setSelection(j);
                                }
                            }
                        }
                    }
                }catch(Exception e){e.printStackTrace();}
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {}
        });

        final Spinner speedVoiceSpinner = (Spinner)findViewById(R.id.speedVoiceSpinner);
        ArrayAdapter<CharSequence> speedVoiceSpinnerAdapter = android.widget.ArrayAdapter.createFromResource(this, R.array.voices_speed_array, android.R.layout.simple_spinner_item);
        speedVoiceSpinnerAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        speedVoiceSpinner.setAdapter(speedVoiceSpinnerAdapter);
        if(usersVoiceSettings.getVoiceRate() == 1.5)
            speedVoiceSpinner.setSelection(speedVoiceSpinnerAdapter.getPosition("lento"));
        else if(usersVoiceSettings.getVoiceRate() == 1.0)
            speedVoiceSpinner.setSelection(speedVoiceSpinnerAdapter.getPosition("normale"));
        else if(usersVoiceSettings.getVoiceRate() == 0.7)
            speedVoiceSpinner.setSelection(speedVoiceSpinnerAdapter.getPosition("veloce"));
        else if(usersVoiceSettings.getVoiceRate() == 0.4)
            speedVoiceSpinner.setSelection(speedVoiceSpinnerAdapter.getPosition("super veloce"));


{'fraction_non_alphanumeric': 0.0948054973212206, 'fraction_numerical': 0.002212904728627999, 'mean_word_length': 2.691530524505589, 'pattern_counts': {'":': 0, '<': 25, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 5, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3638041', 'n_tokens_mistral': 4600, 'n_tokens_neox': 4403, 'n_words': 762}