<commit_before>package com.sensirion.smartgadget.view.dashboard;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.res.AssetManager;
import android.graphics.Typeface;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.annotation.UiThread;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;

import com.sensirion.smartgadget.R;
import com.sensirion.smartgadget.peripheral.rht_sensor.RHTSensorFacade;
import com.sensirion.smartgadget.peripheral.rht_sensor.RHTSensorListener;
import com.sensirion.smartgadget.peripheral.rht_sensor.internal.RHTInternalSensorManager;
import com.sensirion.smartgadget.utils.Converter;
import com.sensirion.smartgadget.utils.DeviceModel;
import com.sensirion.smartgadget.utils.Settings;
import com.sensirion.smartgadget.utils.view.ParentFragment;
import com.sensirion.smartgadget.view.MainActivity;
import com.sensirion.smartgadget.view.dashboard.adapter.ConnectedDeviceAdapter;

import java.text.NumberFormat;
import java.util.List;
import java.util.Locale;

import butterknife.BindView;
import butterknife.BindBool;
import butterknife.BindString;
import butterknife.ButterKnife;

public class DashboardFragment extends ParentFragment implements RHTSensorListener {

    private static final String TAG = DashboardFragment.class.getSimpleName();

    //BUTTON STATES
    private static final int BUTTON_STATE_TEMPERATURE = 0;
    private static final int DEFAULT_STATE_BUTTON = BUTTON_STATE_TEMPERATURE;
    private static final int BUTTON_STATE_HUMIDITY = 1;
    private static final int BUTTON_STATE_DEW_POINT = 2;
    private static final int BUTTON_STATE_HEAT_INDEX = 3;

    //BUTTON VALUE FORMAT
    private static final String BUTTON_VALUE_FORMAT = "%s%s %s";

    //VIEWS
    @BindView(R.id.dashboard_connected_device_nested_list_view)
    ListView mConnectedDeviceView;
    @BindView(R.id.dashboard_temperature_button)
    Button mTemperatureButton;
    @BindView(R.id.dashboard_humidity_button)
    Button mHumidityButton;
    @BindView(R.id.dashboard_dew_point_button)
    Button mDewPointButton;
    @BindView(R.id.dashboard_heat_index_button)
    Button mHeatIndexButton;
    @BindView(R.id.dashboard_temperature_value)
    TextView mTemperatureValueTextView;
    @BindView(R.id.dashboard_humidity_value)
    TextView mHumidityValueTextView;
    @BindView(R.id.dashboard_dew_point_value)
    TextView mDewPointValueTextView;
    @BindView(R.id.dashboard_heat_index_value)
    TextView mHeatIndexValueTextView;

    //Extracted attributes from the XML
    @BindBool(R.bool.is_tablet)
    boolean IS_TABLET;
    @BindString(R.string.unit_fahrenheit)
    String FAHRENHEIT_UNIT;
    @BindString(R.string.unit_celsius)
    String CELSIUS_UNIT;
    @BindString(R.string.unit_humidity)
    String HUMIDITY_UNIT;
    @BindString(R.string.label_empty_t)
    String EMPTY_TEMPERATURE_LABEL;
    @BindString(R.string.label_empty_rh)
    String EMPTY_HUMIDITY_LABEL;
    @BindString(R.string.label_empty_heat_index)
    String EMPTY_HEAT_INDEX_LABEL;
    @BindString(R.string.typeface_condensed)
    String TYPEFACE_CONDENSED_LOCATION;
    @BindString(R.string.typeface_bold)
    String TYPEFACE_BOLD_LOCATION;

    //Temperature state
    private boolean mIsFahrenheit;

    //Connection Adapter
    private ConnectedDeviceAdapter mConnectedDeviceAdapter;

    private void updateViewForSelectedTemperatureUnit() {
        mIsFahrenheit = Settings.getInstance().isTemperatureUnitFahrenheit(getContext());
    }

    private void updateListView() {
        if (isAdded()) {
            Log.i(TAG, "updateListView()");
            final List<DeviceModel> connectedDevices = RHTSensorFacade.getInstance().getConnectedSensors();
            final ConnectedDeviceAdapter adapter = ((ConnectedDeviceAdapter) mConnectedDeviceView.getAdapter());
            final Activity parent = getParent();
            if (parent == null) {
                Log.e(TAG, "updateListView() -> Received a null activity.");
            } else {
                parent.runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        adapter.update(connectedDevices);
                    }
                });
            }
        }
    }

    @Override
    public View onCreateView(@NonNull final LayoutInflater inflater,
                             @Nullable final ViewGroup container,
                             @Nullable final Bundle savedInstanceState) {
        Log.i(TAG, "OnCreateView()");
        final View view = inflater.inflate(R.layout.fragment_dashboard, container, false);
        ButterKnife.bind(this, view);
        return view;
    }

    @Override
    public void onViewCreated(@NonNull final View view,
                              @Nullable final Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        Log.i(TAG, "onViewCreated()");
        final AssetManager assets = getContext().getAssets();
        final Typeface typefaceNormal = Typeface.createFromAsset(assets, TYPEFACE_CONDENSED_LOCATION);
        final Typeface typefaceBold = Typeface.createFromAsset(assets, TYPEFACE_BOLD_LOCATION);
        initButtons(typefaceNormal, typefaceBold);
        initListView();
    }

    @Override
    public void onResume() {
        super.onResume();
        RHTSensorFacade.getInstance().registerListener(this);
        updateViewForSelectedTemperatureUnit();
        updateListView();
    }

    @Override
    public void onPause() {
        super.onPause();
        RHTSensorFacade.getInstance().unregisterListener(this);
        resetViewValues();
    }

    @UiThread
    @SuppressLint("CommitPrefEdits")
    //Using apply instead of commit can cause a Parallel writing Exception.
    private void initButtons(@NonNull final Typeface typefaceNormal,
                             @NonNull final Typeface typefaceBold) {

        mTemperatureValueTextView.setTypeface(typefaceBold);
        mHumidityValueTextView.setTypeface(typefaceBold);
        mDewPointValueTextView.setTypeface(typefaceBold);
        mHeatIndexValueTextView.setTypeface(typefaceBold);

        final SharedPreferences prefs =
                PreferenceManager.getDefaultSharedPreferences(getContext().getApplicationContext());

        mTemperatureButton.setTypeface(typefaceNormal);
        prefs.edit().putInt(String.valueOf(mTemperatureButton.getId()), BUTTON_STATE_TEMPERATURE).commit();
        addButtonListener(mTemperatureButton);

        mHumidityButton.setTypeface(typefaceNormal);
        prefs.edit().putInt(String.valueOf(mHumidityButton.getId()), BUTTON_STATE_HUMIDITY).commit();
        addButtonListener(mHumidityButton);

        mDewPointButton.setTypeface(typefaceNormal);
        prefs.edit().putInt(String.valueOf(mDewPointButton.getId()), BUTTON_STATE_DEW_POINT).commit();
        addButtonListener(mDewPointButton);

        mHeatIndexButton.setTypeface(typefaceNormal);
        prefs.edit().putInt(String.valueOf(mHeatIndexButton.getId()), BUTTON_STATE_HEAT_INDEX).commit();
        addButtonListener(mHeatIndexButton);
    }

    private void initListView() {
        mConnectedDeviceAdapter = new ConnectedDeviceAdapter(getContext().getApplicationContext());

        mConnectedDeviceView.setAdapter(mConnectedDeviceAdapter);
        mConnectedDeviceView.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(@NonNull final AdapterView<?> adapterView,
                                    @NonNull final View arg1,
                                    final int position,
                                    final long arg3) {
                final String clickedAddress = mConnectedDeviceAdapter.getItem(position).getAddress();
                final String selectedAddress = Settings.getInstance().getSelectedAddress();
                if (selectedAddress == null || selectedAddress.equals(clickedAddress)) {
                    return;
                }
                Settings.getInstance().setSelectedAddress(clickedAddress);
                mConnectedDeviceAdapter.notifyDataSetChanged();
                resetViewValues();
                RHTSensorFacade.getInstance().notifyCachedSensorData(DashboardFragment.this);
            }
        });
        updateListView();
    }

    private void addButtonListener(@NonNull final Button button) {
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (IS_TABLET) {
                    final MainActivity activity = (MainActivity) getParent();
                    if (activity == null) {
                        Log.e(TAG, "addButtonListener.onClick -> Cannot toogle menu with a null activity.");
                    } else {
                        activity.toggleTabletMenu();
                    }
                }
            }
        });
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public void onGadgetConnectionChanged(@Nullable final String deviceAddress,
                                          final boolean deviceIsConnected) {
        if (isAdded()) {
            updateListView();
            if (deviceIsConnected) {
                Log.i(TAG, String.format(
                                "onGadgetConnectionChanged() -> Sensor with address %s was connected.",
                                deviceAddress
                        )
                );
            } else {
                resetViewValues();
                Log.i(TAG, String.format(
                                "onGadgetConnectionChanged() -> Sensor with address %s was disconnected.",
                                deviceAddress
                        )
                );
            }
        }
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public void onNewRHTSensorData(final float temperature,
                                   final float relativeHumidity,
                                   @Nullable final String deviceAddress) {
        if (deviceAddress == null) {
            updateViewValues(temperature, relativeHumidity, RHTInternalSensorManager.INTERNAL_SENSOR_ADDRESS);
        } else {
            updateViewValues(temperature, relativeHumidity, deviceAddress);
        }<commit_msg>Dashboard: Fix displaying -- when a gadget disconnects<commit_after>package com.sensirion.smartgadget.view.dashboard;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.res.AssetManager;
import android.graphics.Typeface;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.annotation.UiThread;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;

import com.sensirion.smartgadget.R;
import com.sensirion.smartgadget.peripheral.rht_sensor.RHTSensorFacade;
import com.sensirion.smartgadget.peripheral.rht_sensor.RHTSensorListener;
import com.sensirion.smartgadget.peripheral.rht_sensor.internal.RHTInternalSensorManager;
import com.sensirion.smartgadget.utils.Converter;
import com.sensirion.smartgadget.utils.DeviceModel;
import com.sensirion.smartgadget.utils.Settings;
import com.sensirion.smartgadget.utils.view.ParentFragment;
import com.sensirion.smartgadget.view.MainActivity;
import com.sensirion.smartgadget.view.dashboard.adapter.ConnectedDeviceAdapter;

import java.text.NumberFormat;
import java.util.List;
import java.util.Locale;

import butterknife.BindView;
import butterknife.BindBool;
import butterknife.BindString;
import butterknife.ButterKnife;

public class DashboardFragment extends ParentFragment implements RHTSensorListener {

    private static final String TAG = DashboardFragment.class.getSimpleName();

    //BUTTON STATES
    private static final int BUTTON_STATE_TEMPERATURE = 0;
    private static final int DEFAULT_STATE_BUTTON = BUTTON_STATE_TEMPERATURE;
    private static final int BUTTON_STATE_HUMIDITY = 1;
    private static final int BUTTON_STATE_DEW_POINT = 2;
    private static final int BUTTON_STATE_HEAT_INDEX = 3;

    //BUTTON VALUE FORMAT
    private static final String BUTTON_VALUE_FORMAT = "%s%s %s";

    //VIEWS
    @BindView(R.id.dashboard_connected_device_nested_list_view)
    ListView mConnectedDeviceView;
    @BindView(R.id.dashboard_temperature_button)
    Button mTemperatureButton;
    @BindView(R.id.dashboard_humidity_button)
    Button mHumidityButton;
    @BindView(R.id.dashboard_dew_point_button)
    Button mDewPointButton;
    @BindView(R.id.dashboard_heat_index_button)
    Button mHeatIndexButton;
    @BindView(R.id.dashboard_temperature_value)
    TextView mTemperatureValueTextView;
    @BindView(R.id.dashboard_humidity_value)
    TextView mHumidityValueTextView;
    @BindView(R.id.dashboard_dew_point_value)
    TextView mDewPointValueTextView;
    @BindView(R.id.dashboard_heat_index_value)
    TextView mHeatIndexValueTextView;

    //Extracted attributes from the XML
    @BindBool(R.bool.is_tablet)
    boolean IS_TABLET;
    @BindString(R.string.unit_fahrenheit)
    String FAHRENHEIT_UNIT;
    @BindString(R.string.unit_celsius)
    String CELSIUS_UNIT;
    @BindString(R.string.unit_humidity)
    String HUMIDITY_UNIT;
    @BindString(R.string.label_empty_t)
    String EMPTY_TEMPERATURE_LABEL;
    @BindString(R.string.label_empty_rh)
    String EMPTY_HUMIDITY_LABEL;
    @BindString(R.string.label_empty_heat_index)
    String EMPTY_HEAT_INDEX_LABEL;
    @BindString(R.string.typeface_condensed)
    String TYPEFACE_CONDENSED_LOCATION;
    @BindString(R.string.typeface_bold)
    String TYPEFACE_BOLD_LOCATION;

    //Temperature state
    private boolean mIsFahrenheit;

    //Connection Adapter
    private ConnectedDeviceAdapter mConnectedDeviceAdapter;

    private void updateViewForSelectedTemperatureUnit() {
        mIsFahrenheit = Settings.getInstance().isTemperatureUnitFahrenheit(getContext());
    }

    private void updateListView() {
        if (isAdded()) {
            Log.i(TAG, "updateListView()");
            final List<DeviceModel> connectedDevices = RHTSensorFacade.getInstance().getConnectedSensors();
            final ConnectedDeviceAdapter adapter = ((ConnectedDeviceAdapter) mConnectedDeviceView.getAdapter());
            final Activity parent = getParent();
            if (parent == null) {
                Log.e(TAG, "updateListView() -> Received a null activity.");
            } else {
                parent.runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        adapter.update(connectedDevices);
                    }
                });
            }
        }
    }

    @Override
    public View onCreateView(@NonNull final LayoutInflater inflater,
                             @Nullable final ViewGroup container,
                             @Nullable final Bundle savedInstanceState) {
        Log.i(TAG, "OnCreateView()");
        final View view = inflater.inflate(R.layout.fragment_dashboard, container, false);
        ButterKnife.bind(this, view);
        return view;
    }

    @Override
    public void onViewCreated(@NonNull final View view,
                              @Nullable final Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        Log.i(TAG, "onViewCreated()");
        final AssetManager assets = getContext().getAssets();
        final Typeface typefaceNormal = Typeface.createFromAsset(assets, TYPEFACE_CONDENSED_LOCATION);
        final Typeface typefaceBold = Typeface.createFromAsset(assets, TYPEFACE_BOLD_LOCATION);
        initButtons(typefaceNormal, typefaceBold);
        initListView();
    }

    @Override
    public void onResume() {
        super.onResume();
        resetViewValues();
        RHTSensorFacade.getInstance().registerListener(this);
        updateViewForSelectedTemperatureUnit();
        updateListView();
    }

    @Override
    public void onPause() {
        super.onPause();
        RHTSensorFacade.getInstance().unregisterListener(this);
    }

    @UiThread
    @SuppressLint("CommitPrefEdits")
    //Using apply instead of commit can cause a Parallel writing Exception.
    private void initButtons(@NonNull final Typeface typefaceNormal,
                             @NonNull final Typeface typefaceBold) {

        mTemperatureValueTextView.setTypeface(typefaceBold);
        mHumidityValueTextView.setTypeface(typefaceBold);
        mDewPointValueTextView.setTypeface(typefaceBold);
        mHeatIndexValueTextView.setTypeface(typefaceBold);

        final SharedPreferences prefs =
                PreferenceManager.getDefaultSharedPreferences(getContext().getApplicationContext());

        mTemperatureButton.setTypeface(typefaceNormal);
        prefs.edit().putInt(String.valueOf(mTemperatureButton.getId()), BUTTON_STATE_TEMPERATURE).commit();
        addButtonListener(mTemperatureButton);

        mHumidityButton.setTypeface(typefaceNormal);
        prefs.edit().putInt(String.valueOf(mHumidityButton.getId()), BUTTON_STATE_HUMIDITY).commit();
        addButtonListener(mHumidityButton);

        mDewPointButton.setTypeface(typefaceNormal);
        prefs.edit().putInt(String.valueOf(mDewPointButton.getId()), BUTTON_STATE_DEW_POINT).commit();
        addButtonListener(mDewPointButton);

        mHeatIndexButton.setTypeface(typefaceNormal);
        prefs.edit().putInt(String.valueOf(mHeatIndexButton.getId()), BUTTON_STATE_HEAT_INDEX).commit();
        addButtonListener(mHeatIndexButton);
    }

    private void initListView() {
        mConnectedDeviceAdapter = new ConnectedDeviceAdapter(getContext().getApplicationContext());

        mConnectedDeviceView.setAdapter(mConnectedDeviceAdapter);
        mConnectedDeviceView.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(@NonNull final AdapterView<?> adapterView,
                                    @NonNull final View arg1,
                                    final int position,
                                    final long arg3) {
                final String clickedAddress = mConnectedDeviceAdapter.getItem(position).getAddress();
                final String selectedAddress = Settings.getInstance().getSelectedAddress();
                if (selectedAddress == null || selectedAddress.equals(clickedAddress)) {
                    return;
                }
                Settings.getInstance().setSelectedAddress(clickedAddress);
                mConnectedDeviceAdapter.notifyDataSetChanged();
                resetViewValues();
                RHTSensorFacade.getInstance().notifyCachedSensorData(DashboardFragment.this);
            }
        });
        updateListView();
    }

    private void addButtonListener(@NonNull final Button button) {
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (IS_TABLET) {
                    final MainActivity activity = (MainActivity) getParent();
                    if (activity == null) {
                        Log.e(TAG, "addButtonListener.onClick -> Cannot toogle menu with a null activity.");
                    } else {
                        activity.toggleTabletMenu();
                    }
                }
            }
        });
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public void onGadgetConnectionChanged(@Nullable final String deviceAddress,
                                          final boolean deviceIsConnected) {
        if (isAdded()) {
            updateListView();
            if (deviceIsConnected) {
                Log.i(TAG, String.format(
                                "onGadgetConnectionChanged() -> Sensor with address %s was connected.",
                                deviceAddress
                        )
                );
            } else {
                resetViewValues();
                RHTSensorFacade.getInstance().notifyCachedSensorData(DashboardFragment.this);
                Log.i(TAG, String.format(
                                "onGadgetConnectionChanged() -> Sensor with address %s was disconnected.",
                                deviceAddress
                        )
                );
            }
        }
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public void onNewRHTSensorData(final float temperature,
                                   final float relativeHumidity,
                                   @Nullable final String deviceAddress) {
        if (deviceAddress == null) {
            updateViewValues(temperature, relativeHumidity, RHTInternalSensorManager.INTERNAL_SENSOR_ADDRESS);
        } else {
            updateViewValues(temperature, relativeHumidity, deviceAddress);
        }
{'fraction_non_alphanumeric': 0.0829444110715771, 'fraction_numerical': 0.0005545030266623539, 'mean_word_length': 2.702018474170373, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 14, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '262390', 'n_tokens_mistral': 5867, 'n_tokens_neox': 5724, 'n_words': 1017}