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

import android.annotation.SuppressLint;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.SharedPreferences;
import android.content.pm.PackageManager;
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.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;

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.external.RHTHumigadgetSensorManager;
import com.sensirion.smartgadget.utils.Settings;
import com.sensirion.smartgadget.utils.view.ParentListFragment;
import com.sensirion.smartgadget.utils.view.SectionAdapter;
import com.sensirion.smartgadget.view.MainActivity;
import com.sensirion.smartgadget.view.device_management.ScanDeviceFragment;
import com.sensirion.smartgadget.view.glossary.GlossaryFragment;
import com.sensirion.smartgadget.view.preference.adapter.PreferenceAdapter;

import java.util.Calendar;

import butterknife.BindString;
import butterknife.ButterKnife;

import static android.content.pm.PackageManager.NameNotFoundException;

public class SmartgadgetPreferenceFragment extends ParentListFragment implements RHTSensorListener {

    // Class name
    @NonNull
    private static final String TAG = SmartgadgetPreferenceFragment.class.getSimpleName();

    // XML resources
    @BindString(R.string.app_name)
    String APP_NAME;
    @BindString(R.string.app_platform)
    String APP_PLATFORM;
    @BindString(R.string.txt_about_url)
    String APP_ABOUT_URL;
    @BindString(R.string.txt_about_char_copyright)
    String ABOUT_CHAR_COPYRIGHT;
    @BindString(R.string.typeface_condensed)
    String CONDENSED_TYPEFACE;
    @BindString(R.string.typeface_bold)
    String BOLD_TYPEFACE;
    @BindString(R.string.label_season)
    String SEASON_PREFERENCE_LABEL;
    @BindString(R.string.label_smart_gadgets)
    String DEVICES_PREFERENCE_LABEL;
    @BindString(R.string.key_pref_season)
    String KEY_PREFERENCE_SEASON;
    @BindString(R.string.label_glossary)
    String GLOSSARY_PREFERENCE_LABEL;
    @BindString(R.string.about_sensirion_ag)
    String ABOUT_SENSIRION_AG;
    @BindString(R.string.header_connections)
    String CONNECTION_HEADER;
    @BindString(R.string.key_pref_temp_unit)
    String TEMPERATURE_PREFERENCE_KEY;
    @BindString(R.string.label_temperature_unit)
    String TEMPERATURE_PREFERENCE_LABEL;
    @BindString(R.string.header_user_prefs)
    String USER_PREFERENCES_HEADER;
    @BindString(R.string.label_about)
    String ABOUT_PREFERENCE_LABEL;
    @BindString(R.string.header_app_information)
    String APP_INFORMATION_HEADER;

    // Layout Adapters
    @Nullable
    private SectionAdapter mSectionAdapter;
    @Nullable
    private PreferenceAdapter mConnectionsAdapter;
    @Nullable
    private PreferenceAdapter mUserPreferencesAdapter;

    // Last printed Toast
    @Nullable
    private Toast mLastAboutToast = null;

    @Override
    public void onResume() {
        super.onResume();
        Log.d(TAG, "onResume() -> Refreshing number of devices.");
        if (mConnectionsAdapter == null) {
            initPreferencesList();
        }
        mConnectionsAdapter.clear();
        refreshPreferenceAdapter();
        refreshUserPreferenceAdapter();
        setListAdapter(mSectionAdapter);
        RHTSensorFacade.getInstance().registerListener(this);
    }

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

    @Override
    @NonNull
    public View onCreateView(@NonNull final LayoutInflater inflater,
                             @Nullable final ViewGroup container,
                             @Nullable final Bundle savedInstanceState) {
        final View view = inflater.inflate(R.layout.fragment_smartgadget_list, container, false);
        ButterKnife.bind(this, view);
        initPreferencesList();
        return view;
    }

    private void initPreferencesList() {
        mSectionAdapter = new SectionAdapter() {
            @NonNull
            @Override
            protected View getHeaderView(@NonNull final String caption,
                                         final int itemIndex,
                                         @Nullable final View convertView,
                                         @Nullable final ViewGroup parent) {
                TextView listItemHeader = (TextView) convertView;
                if (listItemHeader == null) {
                    final AssetManager assets = getContext().getAssets();
                    final Typeface typefaceBold = Typeface.createFromAsset(assets, BOLD_TYPEFACE);
                    listItemHeader = (TextView) View.inflate(getParent(), R.layout.listitem_scan_header, null);
                    listItemHeader.setTypeface(typefaceBold);
                }
                listItemHeader.setText(caption);
                return listItemHeader;
            }
        };
        initConnectionPreferenceAdapter();
        initUserPreferenceAdapter();
        if (mConnectionsAdapter == null) {
            Log.e(TAG, "initPreferencesList -> Connection adapter can't be null");
            return;
        }
        if (mUserPreferencesAdapter == null) {
            Log.e(TAG, "initPreferencesList -> User preferences adapter can't be null");
            return;
        }
        mSectionAdapter.addSectionToAdapter(CONNECTION_HEADER, mConnectionsAdapter);
        mSectionAdapter.addSectionToAdapter(USER_PREFERENCES_HEADER, mUserPreferencesAdapter);
        mSectionAdapter.addSectionToAdapter(APP_INFORMATION_HEADER, getAppInformationAdapter());
    }

    /**
     * ************************************************************************
     * *********************** CONNECTION PREFERENCES *************************
     * ************************************************************************
     */

    private void initConnectionPreferenceAdapter() {
        final AssetManager assets = getContext().getAssets();
        final Typeface typefaceCondensed = Typeface.createFromAsset(assets, CONDENSED_TYPEFACE);
        mConnectionsAdapter = new PreferenceAdapter(typefaceCondensed);
        refreshPreferenceAdapter();
    }

    private void refreshPreferenceAdapter() {
        if (mConnectionsAdapter == null) {
            Log.e(TAG, "refreshPreferenceAdapter -> Connection adapter can't be null");
            return;
        }
        final String title = getConnectedDevicesTitle();
        final View.OnClickListener clickListener = new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (RHTHumigadgetSensorManager.getInstance().bluetoothIsEnabled(getContext())) {
                    getListView().setVisibility(View.GONE);
                    final MainActivity mainActivity = (MainActivity) getParent();
                    if (mainActivity == null) {
                        Log.e(TAG, "refreshPreferenceAdapter.onClick -> getParent() returned null");
                    } else {
                        mainActivity.changeFragment(new ScanDeviceFragment());
                    }
                }
                Log.w(TAG, "initConnectionPreferences -> Bluetooth has to be active in" +
                        " order to scan for new devices.");
                RHTHumigadgetSensorManager.getInstance().requestEnableBluetooth(getParent());
            }
        };
        mConnectionsAdapter.addPreference(title, null, clickListener);
    }

    @NonNull
    private String getConnectedDevicesTitle() {
        final int numberConnectedGadgets = RHTHumigadgetSensorManager.getInstance().getConnectedDevicesCount();
        if (numberConnectedGadgets == 0) {
            return DEVICES_PREFERENCE_LABEL;
        }
        return String.format("%s (%d)", DEVICES_PREFERENCE_LABEL, numberConnectedGadgets);
    }


    /**
     * ************************************************************************
     * *************************** USER PREFERENCES ***************************<commit_msg>SGA-94 Fix Bug related to multiple entries in Connections Section<commit_after>package com.sensirion.smartgadget.view.preference;

import android.annotation.SuppressLint;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.SharedPreferences;
import android.content.pm.PackageManager;
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.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;

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.external.RHTHumigadgetSensorManager;
import com.sensirion.smartgadget.utils.Settings;
import com.sensirion.smartgadget.utils.view.ParentListFragment;
import com.sensirion.smartgadget.utils.view.SectionAdapter;
import com.sensirion.smartgadget.view.MainActivity;
import com.sensirion.smartgadget.view.device_management.ScanDeviceFragment;
import com.sensirion.smartgadget.view.glossary.GlossaryFragment;
import com.sensirion.smartgadget.view.preference.adapter.PreferenceAdapter;

import java.util.Calendar;

import butterknife.BindString;
import butterknife.ButterKnife;

import static android.content.pm.PackageManager.NameNotFoundException;

public class SmartgadgetPreferenceFragment extends ParentListFragment implements RHTSensorListener {

    // Class name
    @NonNull
    private static final String TAG = SmartgadgetPreferenceFragment.class.getSimpleName();

    // XML resources
    @BindString(R.string.app_name)
    String APP_NAME;
    @BindString(R.string.app_platform)
    String APP_PLATFORM;
    @BindString(R.string.txt_about_url)
    String APP_ABOUT_URL;
    @BindString(R.string.txt_about_char_copyright)
    String ABOUT_CHAR_COPYRIGHT;
    @BindString(R.string.typeface_condensed)
    String CONDENSED_TYPEFACE;
    @BindString(R.string.typeface_bold)
    String BOLD_TYPEFACE;
    @BindString(R.string.label_season)
    String SEASON_PREFERENCE_LABEL;
    @BindString(R.string.label_smart_gadgets)
    String DEVICES_PREFERENCE_LABEL;
    @BindString(R.string.key_pref_season)
    String KEY_PREFERENCE_SEASON;
    @BindString(R.string.label_glossary)
    String GLOSSARY_PREFERENCE_LABEL;
    @BindString(R.string.about_sensirion_ag)
    String ABOUT_SENSIRION_AG;
    @BindString(R.string.header_connections)
    String CONNECTION_HEADER;
    @BindString(R.string.key_pref_temp_unit)
    String TEMPERATURE_PREFERENCE_KEY;
    @BindString(R.string.label_temperature_unit)
    String TEMPERATURE_PREFERENCE_LABEL;
    @BindString(R.string.header_user_prefs)
    String USER_PREFERENCES_HEADER;
    @BindString(R.string.label_about)
    String ABOUT_PREFERENCE_LABEL;
    @BindString(R.string.header_app_information)
    String APP_INFORMATION_HEADER;

    // Layout Adapters
    @Nullable
    private SectionAdapter mSectionAdapter;
    @Nullable
    private PreferenceAdapter mConnectionsAdapter;
    @Nullable
    private PreferenceAdapter mUserPreferencesAdapter;

    // Last printed Toast
    @Nullable
    private Toast mLastAboutToast = null;

    @Override
    public void onResume() {
        super.onResume();
        Log.d(TAG, "onResume() -> Refreshing number of devices.");
        if (mConnectionsAdapter == null) {
            initPreferencesList();
        }
        refreshPreferenceAdapter();
        refreshUserPreferenceAdapter();
        setListAdapter(mSectionAdapter);
        RHTSensorFacade.getInstance().registerListener(this);
    }

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

    @Override
    @NonNull
    public View onCreateView(@NonNull final LayoutInflater inflater,
                             @Nullable final ViewGroup container,
                             @Nullable final Bundle savedInstanceState) {
        final View view = inflater.inflate(R.layout.fragment_smartgadget_list, container, false);
        ButterKnife.bind(this, view);
        initPreferencesList();
        return view;
    }

    private void initPreferencesList() {
        mSectionAdapter = new SectionAdapter() {
            @NonNull
            @Override
            protected View getHeaderView(@NonNull final String caption,
                                         final int itemIndex,
                                         @Nullable final View convertView,
                                         @Nullable final ViewGroup parent) {
                TextView listItemHeader = (TextView) convertView;
                if (listItemHeader == null) {
                    final AssetManager assets = getContext().getAssets();
                    final Typeface typefaceBold = Typeface.createFromAsset(assets, BOLD_TYPEFACE);
                    listItemHeader = (TextView) View.inflate(getParent(), R.layout.listitem_scan_header, null);
                    listItemHeader.setTypeface(typefaceBold);
                }
                listItemHeader.setText(caption);
                return listItemHeader;
            }
        };
        initConnectionPreferenceAdapter();
        initUserPreferenceAdapter();
        if (mConnectionsAdapter == null) {
            Log.e(TAG, "initPreferencesList -> Connection adapter can't be null");
            return;
        }
        if (mUserPreferencesAdapter == null) {
            Log.e(TAG, "initPreferencesList -> User preferences adapter can't be null");
            return;
        }
        mSectionAdapter.addSectionToAdapter(CONNECTION_HEADER, mConnectionsAdapter);
        mSectionAdapter.addSectionToAdapter(USER_PREFERENCES_HEADER, mUserPreferencesAdapter);
        mSectionAdapter.addSectionToAdapter(APP_INFORMATION_HEADER, getAppInformationAdapter());
    }

    /**
     * ************************************************************************
     * *********************** CONNECTION PREFERENCES *************************
     * ************************************************************************
     */

    private void initConnectionPreferenceAdapter() {
        final AssetManager assets = getContext().getAssets();
        final Typeface typefaceCondensed = Typeface.createFromAsset(assets, CONDENSED_TYPEFACE);
        mConnectionsAdapter = new PreferenceAdapter(typefaceCondensed);
        refreshPreferenceAdapter();
    }

    private void refreshPreferenceAdapter() {
        if (mConnectionsAdapter == null) {
            Log.e(TAG, "refreshPreferenceAdapter -> Connection adapter can't be null");
            return;
        }
        final String title = getConnectedDevicesTitle();
        final View.OnClickListener clickListener = new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (RHTHumigadgetSensorManager.getInstance().bluetoothIsEnabled(getContext())) {
                    getListView().setVisibility(View.GONE);
                    final MainActivity mainActivity = (MainActivity) getParent();
                    if (mainActivity == null) {
                        Log.e(TAG, "refreshPreferenceAdapter.onClick -> getParent() returned null");
                    } else {
                        mainActivity.changeFragment(new ScanDeviceFragment());
                    }
                    return;
                }
                Log.w(TAG, "initConnectionPreferences -> Bluetooth has to be active in" +
                        " order to scan for new devices.");
                RHTHumigadgetSensorManager.getInstance().requestEnableBluetooth(getParent());
            }
        };
        mConnectionsAdapter.clear();
        mConnectionsAdapter.addPreference(title, null, clickListener);
        mConnectionsAdapter.notifyDataSetChanged();
    }

    @NonNull
    private String getConnectedDevicesTitle() {
        final int numberConnectedGadgets = RHTHumigadgetSensorManager.getInstance().getConnectedDevicesCount();
        if (numberConnectedGadgets == 0) {
            return DEVICES_PREFERENCE_LABEL;
        }
        return String.format("%s (%d)", DEVICES_PREFERENCE_LABEL, numberConnectedGadgets);
    }


    /**
     * ************************************************************************
     * *************************** USER PREFERENCES ***************************
{'fraction_non_alphanumeric': 0.11538907572770497, 'fraction_numerical': 0.00023193784065870347, 'mean_word_length': 3.0811642214860386, 'pattern_counts': {'":': 0, '<': 3, '<?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': 12, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5940905', 'n_tokens_mistral': 4649, 'n_tokens_neox': 4499, 'n_words': 805}