<commit_before>package sk.henrichg.phoneprofilesplus;

import android.content.Context;
import android.content.res.TypedArray;
import android.os.AsyncTask;
import android.os.Parcel;
import android.os.Parcelable;
import android.util.AttributeSet;
import android.view.View;

import androidx.preference.DialogPreference;

import java.util.ArrayList;
import java.util.List;

public class MobileCellsPreferenceX extends DialogPreference {

    MobileCellsPreferenceFragmentX fragment;

    String value;
    private String defaultValue;
    String cellFilter;
    int sortCellsBy = 0;
    private boolean savedInstanceState;

    List<MobileCellsData> cellsList;
    List<MobileCellsData> filteredCellsList;

    MobileCellsData registeredCellData;
    boolean registeredCellInTable;
    boolean registeredCellInValue;

    private final Context context;

    static boolean forceStart;

    public MobileCellsPreferenceX(Context context, AttributeSet attrs) {
        super(context, attrs);
        
        this.context = context;
        
        cellsList = new ArrayList<>();
        filteredCellsList = new ArrayList<>();
    }

    @Override
    protected Object onGetDefaultValue(TypedArray ta, int index)
    {
        super.onGetDefaultValue(ta, index);
        return ta.getString(index);
    }

    @Override
    protected void onSetInitialValue(Object defaultValue) {
        value = getPersistedString((String)defaultValue);
        this.defaultValue = (String)defaultValue;
        setSummary();
    }

    private void setSummary() {
        /*if (!ApplicationPreferences.applicationEventMobileCellEnableScannig(context.getApplicationContext())) {
            preference.setSummary(context.getResources().getString(R.string.profile_preferences_device_not_allowed)+
                    ": "+context.getResources().getString(R.string.preference_not_allowed_reason_not_enabled_scanning));
        }
        else {*/
        if (value.isEmpty())
            setSummary(R.string.applications_multiselect_summary_text_not_selected);
        else {
            String[] splits = value.split("\\|");
            String selectedCells = context.getString(R.string.applications_multiselect_summary_text_selected);
            selectedCells = selectedCells + " " + splits.length;
            setSummary(selectedCells);
        }
        //}
        //GlobalGUIRoutines.setPreferenceTitleStyle(preference, false, true, false, false);
    }

    void addCellId(int cellId) {
        String[] splits = value.split("\\|");
        String sCellId = Integer.toString(cellId);
        boolean found = false;
        for (String cell : splits) {
            if (!cell.isEmpty()) {
                if (cell.equals(sCellId)) {
                    found = true;
                    break;
                }
            }
        }
        if (!found) {
            if (!value.isEmpty())
                value = value + "|";
            value = value + sCellId;
        }
    }

    @SuppressWarnings("StringConcatenationInLoop")
    void removeCellId(int cellId) {
        String[] splits = value.split("\\|");
        String sCellId = Integer.toString(cellId);
        value = "";
        for (String cell : splits) {
            if (!cell.isEmpty()) {
                if (!cell.equals(sCellId)) {
                    if (!value.isEmpty())
                        value = value + "|";
                    value = value + cell;
                }
            }
        }
    }

    boolean isCellSelected(int cellId) {
        String[] splits = value.split("\\|");
        String sCellId = Integer.toString(cellId);
        for (String cell : splits) {
            if (cell.equals(sCellId))
                return true;
        }
        return false;
    }

    void setLocationEnableStatus() {
        if (fragment != null)
            fragment.setLocationEnableStatus();
    }

    @SuppressWarnings("SameParameterValue")
    void refreshListView(final boolean forRescan, final int renameCellId)
    {
        if (fragment != null)
            fragment.refreshListView(forRescan, renameCellId);
    }

    void showEditMenu(View view)
    {
        if (fragment != null)
            fragment.showEditMenu(view);
    }

    void persistValue() {
        if (shouldPersist()) {
            //Log.d("MobileCellsPreference.onPositive", "1");
            if (callChangeListener(value)) {
                //Log.d("MobileCellsPreference.onPositive", "2");
                new AsyncTask<Void, Integer, Void>() {
                    List<MobileCellsData> _cellsList;

                    @Override
                    protected void onPreExecute() {
                        super.onPreExecute();
                        _cellsList = new ArrayList<>();
                        _cellsList.addAll(cellsList);
                    }

                    @Override
                    protected Void doInBackground(Void... voids) {
                        DatabaseHandler db = DatabaseHandler.getInstance(context);
                        db.saveMobileCellsList(_cellsList, false, false);
                        return null;
                    }

                    @Override
                    protected void onPostExecute(Void result) {
                        super.onPostExecute(result);
                        persistString(value);
                        setSummary();
                    }
                };

            }
        }
    }

    void resetSummary() {
        if (!savedInstanceState) {
            value = getPersistedString(defaultValue);
            setSummary();
        }
        savedInstanceState = false;
    }

    void setCellNameText(String text) {
        if (fragment != null)
            fragment.setCellNameText(text);
    }

    String getCellNameText() {
        if (fragment != null)
            return  fragment.getCellNameText();
        else
            return null;
    }

    void setCellFilterText(String text) {
        cellFilter = text;
        if (fragment != null)
            fragment.setCellFilterText(text);
    }<commit_msg>Fixed not working save of mobile cells in Cells preference dialog.<commit_after>package sk.henrichg.phoneprofilesplus;

import android.content.Context;
import android.content.res.TypedArray;
import android.os.AsyncTask;
import android.os.Parcel;
import android.os.Parcelable;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;

import androidx.preference.DialogPreference;

import java.util.ArrayList;
import java.util.List;

public class MobileCellsPreferenceX extends DialogPreference {

    MobileCellsPreferenceFragmentX fragment;

    String value;
    private String defaultValue;
    String cellFilter;
    int sortCellsBy = 0;
    private boolean savedInstanceState;

    List<MobileCellsData> cellsList;
    List<MobileCellsData> filteredCellsList;

    MobileCellsData registeredCellData;
    boolean registeredCellInTable;
    boolean registeredCellInValue;

    private final Context context;

    static boolean forceStart;

    public MobileCellsPreferenceX(Context context, AttributeSet attrs) {
        super(context, attrs);
        
        this.context = context;
        
        cellsList = new ArrayList<>();
        filteredCellsList = new ArrayList<>();
    }

    @Override
    protected Object onGetDefaultValue(TypedArray ta, int index)
    {
        super.onGetDefaultValue(ta, index);
        return ta.getString(index);
    }

    @Override
    protected void onSetInitialValue(Object defaultValue) {
        value = getPersistedString((String)defaultValue);
        this.defaultValue = (String)defaultValue;
        setSummary();
    }

    private void setSummary() {
        /*if (!ApplicationPreferences.applicationEventMobileCellEnableScannig(context.getApplicationContext())) {
            preference.setSummary(context.getResources().getString(R.string.profile_preferences_device_not_allowed)+
                    ": "+context.getResources().getString(R.string.preference_not_allowed_reason_not_enabled_scanning));
        }
        else {*/
        if (value.isEmpty())
            setSummary(R.string.applications_multiselect_summary_text_not_selected);
        else {
            String[] splits = value.split("\\|");
            String selectedCells = context.getString(R.string.applications_multiselect_summary_text_selected);
            selectedCells = selectedCells + " " + splits.length;
            setSummary(selectedCells);
        }
        //}
        //GlobalGUIRoutines.setPreferenceTitleStyle(preference, false, true, false, false);
    }

    void addCellId(int cellId) {
        String[] splits = value.split("\\|");
        String sCellId = Integer.toString(cellId);
        boolean found = false;
        for (String cell : splits) {
            if (!cell.isEmpty()) {
                if (cell.equals(sCellId)) {
                    found = true;
                    break;
                }
            }
        }
        if (!found) {
            if (!value.isEmpty())
                value = value + "|";
            value = value + sCellId;
        }
    }

    @SuppressWarnings("StringConcatenationInLoop")
    void removeCellId(int cellId) {
        String[] splits = value.split("\\|");
        String sCellId = Integer.toString(cellId);
        value = "";
        for (String cell : splits) {
            if (!cell.isEmpty()) {
                if (!cell.equals(sCellId)) {
                    if (!value.isEmpty())
                        value = value + "|";
                    value = value + cell;
                }
            }
        }
    }

    boolean isCellSelected(int cellId) {
        String[] splits = value.split("\\|");
        String sCellId = Integer.toString(cellId);
        for (String cell : splits) {
            if (cell.equals(sCellId))
                return true;
        }
        return false;
    }

    void setLocationEnableStatus() {
        if (fragment != null)
            fragment.setLocationEnableStatus();
    }

    @SuppressWarnings("SameParameterValue")
    void refreshListView(final boolean forRescan, final int renameCellId)
    {
        if (fragment != null)
            fragment.refreshListView(forRescan, renameCellId);
    }

    void showEditMenu(View view)
    {
        if (fragment != null)
            fragment.showEditMenu(view);
    }

    void persistValue() {
        Log.e("MobileCellsPreference.onPositive", "xxx");
        if (shouldPersist()) {
            Log.e("MobileCellsPreference.onPositive", "shouldPersist");
            if (callChangeListener(value)) {
                Log.e("MobileCellsPreference.onPositive", "callChangeListener=true");
                new AsyncTask<Void, Integer, Void>() {
                    List<MobileCellsData> _cellsList;

                    @Override
                    protected void onPreExecute() {
                        super.onPreExecute();
                        Log.e("MobileCellsPreference.onPositive", "onPreExecute cellList.size()="+cellsList.size());
                        _cellsList = new ArrayList<>();
                        _cellsList.addAll(cellsList);
                    }

                    @Override
                    protected Void doInBackground(Void... voids) {
                        Log.e("MobileCellsPreference.onPositive", "doInBackground");
                        DatabaseHandler db = DatabaseHandler.getInstance(context);
                        db.saveMobileCellsList(_cellsList, false, false);
                        return null;
                    }

                    @Override
                    protected void onPostExecute(Void result) {
                        super.onPostExecute(result);
                        Log.e("MobileCellsPreference.onPositive", "onPostExecute value="+value);
                        persistString(value);
                        setSummary();
                    }
                }.execute();
            }
        }
    }

    void resetSummary() {
        if (!savedInstanceState) {
            value = getPersistedString(defaultValue);
            setSummary();
        }
        savedInstanceState = false;
    }

    void setCellNameText(String text) {
        if (fragment != null)
            fragment.setCellNameText(text);
    }

    String getCellNameText() {
        if (fragment != null)
            return  fragment.getCellNameText();
        else
            return null;
    }

    void setCellFilterText(String text) {
        cellFilter = text;
        if (fragment != null)
            fragment.setCellFilterText(text);
    }
{'fraction_non_alphanumeric': 0.09706115965051629, 'fraction_numerical': 0.0003177124702144559, 'mean_word_length': 1.8878440366972478, 'pattern_counts': {'":': 2, '<': 17, '<?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': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4744853', 'n_tokens_mistral': 3309, 'n_tokens_neox': 3116, 'n_words': 635}