<commit_before>package org.fossasia.pslab.fragment;

import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.CardView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.SeekBar;
import android.widget.Spinner;
import android.widget.Toast;

import org.fossasia.pslab.R;
import org.fossasia.pslab.communication.ScienceLab;
import org.fossasia.pslab.communication.peripherals.I2C;
import org.fossasia.pslab.communication.sensors.BH1750;
import org.fossasia.pslab.communication.sensors.TSL2561;
import org.fossasia.pslab.others.ScienceLabCommon;

import java.io.IOException;
import java.util.ArrayList;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

public class LuxMeterFragmentConfig extends Fragment {
    private BH1750 bh1750;
    private TSL2561 tsl2561;
    private final int highLimitMax = 10000;
    private final int updatePeriodMax = 900;
    private final int updatePeriodMin = 100;
    private static int selectedSensor = 0; //0 for built in and 1 for BH1750
    private int highValue = 0;
    private int updatePeriodValue = 100;
    private Unbinder unbinder;

    @BindView(R.id.lux_hight_limit_text)
    EditText highLimit;
    @BindView(R.id.lux_update_period_text)
    EditText updatePeriod;
    @BindView(R.id.cardview_gain_range)
    CardView gainRangeCard;
    @BindView(R.id.spinner_lux_sensor_gain)
    Spinner gainValue;

    private static ScienceLab scienceLab;

    public static LuxMeterFragmentConfig newInstance() {
        return new LuxMeterFragmentConfig();
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_lux_meter_config, container, false);
        unbinder = ButterKnife.bind(this, view);
        final SeekBar highLimitSeek = (SeekBar) view.findViewById(R.id.lux_hight_limit_seekbar);
        final SeekBar updatePeriodSeek = (SeekBar) view.findViewById(R.id.lux_update_period_seekbar);
        final Spinner selectSensor = (Spinner) view.findViewById(R.id.spinner_select_light);

        highLimitSeek.setMax(highLimitMax);
        updatePeriodSeek.setMax(updatePeriodMax);

        highLimit.setText(String.format("%d", highValue));
        highLimitSeek.setProgress(highValue);
        updatePeriod.setText(String.format("%d", updatePeriodValue));
        updatePeriodSeek.setProgress(Integer.valueOf(updatePeriod.getText().toString()) - updatePeriodMin);

        gainValue.setEnabled(false);
        highLimitSeek.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                highValue = progress;
                highLimit.setText(String.format("%d", progress));
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {
                //do nothing
            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {
                //do nothing
            }
        });

        highLimit.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                try {
                    if (!hasFocus) {
                        String stringValue = highLimit.getText().toString();
                        int value = 0;
                        try {
                            value = Integer.parseInt(stringValue);
                        } catch (NumberFormatException e) {
                            e.printStackTrace();
                        }
                        if (value > highLimitMax)
                            highLimitSeek.setProgress(highLimitMax);
                        else if (value < 0)
                            highLimitSeek.setProgress(0);
                        else
                            highLimitSeek.setProgress(value);
                    }
                } catch (NullPointerException e) {
                    e.printStackTrace();
                }
            }
        });

        updatePeriodSeek.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                updatePeriodValue = progress + updatePeriodMin;
                updatePeriod.setText(String.format("%d", updatePeriodValue));
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {
                //do nothing
            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {
                //do nothing
            }
        });

        updatePeriod.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                try {
                    if (!hasFocus) {
                        String stringValue = updatePeriod.getText().toString();
                        int value = 100;
                        try {
                            value = Integer.parseInt(stringValue);
                        } catch (NumberFormatException e) {
                            e.printStackTrace();
                        }
                        if (value > updatePeriodMax)
                            updatePeriodSeek.setProgress(updatePeriodMax + 100);
                        else if (value < updatePeriodMin)
                            updatePeriodSeek.setProgress(updatePeriodMin - 100);
                        else
                            updatePeriodSeek.setProgress(value - updatePeriodMin);
                    }<commit_msg>Lux Meter configurations now saving state (#1236)<commit_after>package org.fossasia.pslab.fragment;

import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.CardView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.SeekBar;
import android.widget.Spinner;
import android.widget.Toast;

import org.fossasia.pslab.R;
import org.fossasia.pslab.communication.ScienceLab;
import org.fossasia.pslab.communication.peripherals.I2C;
import org.fossasia.pslab.communication.sensors.BH1750;
import org.fossasia.pslab.communication.sensors.TSL2561;
import org.fossasia.pslab.others.ScienceLabCommon;

import java.io.IOException;
import java.util.ArrayList;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

public class LuxMeterFragmentConfig extends Fragment {

    private static final String FRAG_CONFIG = "LuxMeterConfig";

    private BH1750 bh1750;
    private TSL2561 tsl2561;
    private final int highLimitMax = 10000;
    private final int updatePeriodMax = 900;
    private final int updatePeriodMin = 100;
    private static int selectedSensor = 0; //0 for built in and 1 for BH1750
    private int highValue;
    private int updatePeriodValue;
    private Unbinder unbinder;

    @BindView(R.id.lux_hight_limit_text)
    EditText highLimit;
    @BindView(R.id.lux_update_period_text)
    EditText updatePeriod;
    @BindView(R.id.cardview_gain_range)
    CardView gainRangeCard;
    @BindView(R.id.spinner_lux_sensor_gain)
    Spinner gainValue;

    private static ScienceLab scienceLab;

    public static LuxMeterFragmentConfig newInstance() {
        return new LuxMeterFragmentConfig();
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_lux_meter_config, container, false);
        unbinder = ButterKnife.bind(this, view);
        final SharedPreferences settings = getActivity().getSharedPreferences(FRAG_CONFIG, Context.MODE_PRIVATE);
        highValue = settings.getInt("HighValue", 2000);
        updatePeriodValue = settings.getInt("UpdatePeriodValue", 100);
        final SeekBar highLimitSeek = (SeekBar) view.findViewById(R.id.lux_hight_limit_seekbar);
        final SeekBar updatePeriodSeek = (SeekBar) view.findViewById(R.id.lux_update_period_seekbar);
        final Spinner selectSensor = (Spinner) view.findViewById(R.id.spinner_select_light);

        highLimitSeek.setMax(highLimitMax);
        updatePeriodSeek.setMax(updatePeriodMax);

        highLimit.setText(String.format("%d", highValue));
        highLimitSeek.setProgress(highValue - updatePeriodMin);
        updatePeriod.setText(String.format("%d", updatePeriodValue));
        updatePeriodSeek.setProgress(Integer.valueOf(updatePeriod.getText().toString()) - updatePeriodMin);

        gainValue.setEnabled(false);
        highLimitSeek.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                highValue = progress;
                highLimit.setText(String.format("%d", progress));
                SharedPreferences.Editor editor = settings.edit();
                editor.putInt("HighValue", highValue);
                editor.apply();
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {
                //do nothing
            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {
                //do nothing
            }
        });

        highLimit.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                try {
                    if (!hasFocus) {
                        String stringValue = highLimit.getText().toString();
                        int value = 0;
                        try {
                            value = Integer.parseInt(stringValue);
                        } catch (NumberFormatException e) {
                            e.printStackTrace();
                        }
                        if (value > highLimitMax) {
                            highLimitSeek.setProgress(highLimitMax);
                            value = highLimitMax;
                        }
                        else if (value < 0) {
                            highLimitSeek.setProgress(0);
                            value = 0;
                        }
                        else
                            highLimitSeek.setProgress(value);
                        SharedPreferences.Editor editor = settings.edit();
                        editor.putInt("HighValue", value);
                        editor.apply();
                    }
                } catch (NullPointerException e) {
                    e.printStackTrace();
                }
            }
        });

        updatePeriodSeek.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                updatePeriodValue = progress + updatePeriodMin;
                updatePeriod.setText(String.format("%d", updatePeriodValue));
                SharedPreferences.Editor editor = settings.edit();
                editor.putInt("UpdatePeriodValue", updatePeriodValue);
                editor.apply();
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {
                //do nothing
            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {
                //do nothing
            }
        });

        updatePeriod.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                try {
                    if (!hasFocus) {
                        String stringValue = updatePeriod.getText().toString();
                        int value = 100;
                        try {
                            value = Integer.parseInt(stringValue);
                        } catch (NumberFormatException e) {
                            e.printStackTrace();
                        }
                        if (value > updatePeriodMax + 100) {
                            updatePeriodSeek.setProgress(updatePeriodMax);
                            value = updatePeriodMax + 100;
                        }
                        else if (value < updatePeriodMin) {
                            updatePeriodSeek.setProgress(updatePeriodMin - 100);
                            value = updatePeriodMin;
                        }
                        else
                            updatePeriodSeek.setProgress(value - updatePeriodMin);
                        SharedPreferences.Editor editor = settings.edit();
                        editor.putInt("UpdatePeriodValue", value);
                        editor.apply();
                    }
{'fraction_non_alphanumeric': 0.07804114490161002, 'fraction_numerical': 0.009913536076326774, 'mean_word_length': 1.884755966458826, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, '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': '5327261', 'n_tokens_mistral': 3548, 'n_tokens_neox': 3271, 'n_words': 659}