<commit_before>package com.thebluealliance.androidclient.fragments.mytba;

import android.app.Activity;
import android.os.Bundle;
import android.preference.CheckBoxPreference;
import android.preference.Preference;
import android.preference.PreferenceFragment;
import android.preference.PreferenceGroup;
import android.preference.PreferenceScreen;
import android.util.Log;
import android.view.View;

import com.thebluealliance.androidclient.Constants;
import com.thebluealliance.androidclient.background.mytba.CreateSubscriptionPanel;
import com.thebluealliance.androidclient.helpers.ModelHelper;
import com.thebluealliance.androidclient.helpers.ModelNotificationFavoriteSettings;
import com.thebluealliance.androidclient.helpers.MyTBAHelper;

import java.util.ArrayList;
import java.util.Set;

/**
 * File created by phil on 8/18/14.
 */
public class NotificationSettingsFragment extends PreferenceFragment {

    public static final String MODEL_KEY = "model_key";
    public static final String MODEL_TYPE = "model_type";
    public static final String SAVED_STATE_BUNDLE = "saved_state_bundle";
    private Bundle savedStateBundle;
    private Bundle initialStateBundle;
    private String modelKey;
    private ModelHelper.MODELS modelType;

    private boolean preferencesLoaded = false;

    public static NotificationSettingsFragment newInstance(String modelKey, ModelHelper.MODELS modelType, Bundle savedStateBundle) {
        NotificationSettingsFragment fragment = new NotificationSettingsFragment();
        Bundle args = new Bundle();
        args.putString(MODEL_KEY, modelKey);
        args.putInt(MODEL_TYPE, modelType.getEnum());
        args.putBundle(SAVED_STATE_BUNDLE, savedStateBundle);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() == null || !getArguments().containsKey(MODEL_KEY)) {
            throw new IllegalArgumentException("NotificationSettingsFragment must be constructed with a model key");
        }
        modelKey = getArguments().getString(MODEL_KEY);
        modelType = ModelHelper.getModelFromEnum(getArguments().getInt(MODEL_TYPE));
        savedStateBundle = getArguments().getBundle(SAVED_STATE_BUNDLE);
    }

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        // Create the preference screen that will hold all the preferences
        PreferenceScreen p = getPreferenceManager().createPreferenceScreen(getActivity());
        this.setPreferenceScreen(p);

        // Create the list of preferences
        new CreateSubscriptionPanel(getActivity(), this, savedStateBundle, modelType).execute(modelKey);

        // Remove padding from the list view
        if(getView() != null) {
            View listView = getView().findViewById(android.R.id.list);
            if (listView != null) {
                listView.setPadding(0, 0, 0, 0);
            }
        }
    }

    public ModelNotificationFavoriteSettings getSettings() {
        ArrayList<String> subscribed = new ArrayList<>();

        PreferenceScreen preferences = getPreferenceScreen();
        // Use recursion to make sure we catch any preferences nested in groups
        writeSettingsFromPreferenceGroupToStringArray(preferences, subscribed);

        // Don't pass the favorite preference to the updater.
        subscribed.remove(MyTBAHelper.getFavoritePreferenceKey());
        Log.d(Constants.LOG_TAG, "notifications: " + subscribed);

        ModelNotificationFavoriteSettings settings = new ModelNotificationFavoriteSettings();
        settings.isFavorite = ((CheckBoxPreference) findPreference(MyTBAHelper.getFavoritePreferenceKey())).isChecked();
        settings.enabledNotifications = subscribed;
        settings.modelKey = modelKey;
        settings.modelType = modelType;

        return settings;
    }

    private void writeSettingsFromPreferenceGroupToStringArray(PreferenceGroup pg, ArrayList<String> strings) {
        for (int i = 0; i < pg.getPreferenceCount(); i++) {
            Preference currentPreference = pg.getPreference(i);
            if (currentPreference instanceof CheckBoxPreference) {
                if (((CheckBoxPreference) currentPreference).isChecked()) {
                    strings.add(currentPreference.getKey());
                }
            } else if (currentPreference instanceof PreferenceGroup) {
                writeSettingsFromPreferenceGroupToStringArray((PreferenceGroup) currentPreference, strings);
            }<commit_msg>Fix possible NPE on mytba fragment (fixes #311)<commit_after>package com.thebluealliance.androidclient.fragments.mytba;

import android.app.Activity;
import android.os.Bundle;
import android.preference.CheckBoxPreference;
import android.preference.Preference;
import android.preference.PreferenceFragment;
import android.preference.PreferenceGroup;
import android.preference.PreferenceScreen;
import android.util.Log;
import android.view.View;

import com.thebluealliance.androidclient.Constants;
import com.thebluealliance.androidclient.background.mytba.CreateSubscriptionPanel;
import com.thebluealliance.androidclient.helpers.ModelHelper;
import com.thebluealliance.androidclient.helpers.ModelNotificationFavoriteSettings;
import com.thebluealliance.androidclient.helpers.MyTBAHelper;

import java.util.ArrayList;
import java.util.Set;

/**
 * File created by phil on 8/18/14.
 */
public class NotificationSettingsFragment extends PreferenceFragment {

    public static final String MODEL_KEY = "model_key";
    public static final String MODEL_TYPE = "model_type";
    public static final String SAVED_STATE_BUNDLE = "saved_state_bundle";
    private Bundle savedStateBundle;
    private Bundle initialStateBundle;
    private String modelKey;
    private ModelHelper.MODELS modelType;

    private boolean preferencesLoaded = false;

    public static NotificationSettingsFragment newInstance(String modelKey, ModelHelper.MODELS modelType, Bundle savedStateBundle) {
        NotificationSettingsFragment fragment = new NotificationSettingsFragment();
        Bundle args = new Bundle();
        args.putString(MODEL_KEY, modelKey);
        args.putInt(MODEL_TYPE, modelType.getEnum());
        args.putBundle(SAVED_STATE_BUNDLE, savedStateBundle);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() == null || !getArguments().containsKey(MODEL_KEY)) {
            throw new IllegalArgumentException("NotificationSettingsFragment must be constructed with a model key");
        }
        modelKey = getArguments().getString(MODEL_KEY);
        modelType = ModelHelper.getModelFromEnum(getArguments().getInt(MODEL_TYPE));
        savedStateBundle = getArguments().getBundle(SAVED_STATE_BUNDLE);
    }

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        // Create the preference screen that will hold all the preferences
        PreferenceScreen p = getPreferenceManager().createPreferenceScreen(getActivity());
        this.setPreferenceScreen(p);

        // Create the list of preferences
        new CreateSubscriptionPanel(getActivity(), this, savedStateBundle, modelType).execute(modelKey);

        // Remove padding from the list view
        if(getView() != null) {
            View listView = getView().findViewById(android.R.id.list);
            if (listView != null) {
                listView.setPadding(0, 0, 0, 0);
            }
        }
    }

    public ModelNotificationFavoriteSettings getSettings() {
        ArrayList<String> subscribed = new ArrayList<>();

        PreferenceScreen preferences = getPreferenceScreen();
        // Use recursion to make sure we catch any preferences nested in groups
        writeSettingsFromPreferenceGroupToStringArray(preferences, subscribed);

        // Don't pass the favorite preference to the updater.
        subscribed.remove(MyTBAHelper.getFavoritePreferenceKey());
        Log.d(Constants.LOG_TAG, "notifications: " + subscribed);

        ModelNotificationFavoriteSettings settings = new ModelNotificationFavoriteSettings();
        Preference fav = findPreference(MyTBAHelper.getFavoritePreferenceKey());
        settings.isFavorite = (fav != null && ((CheckBoxPreference) fav).isChecked());
        settings.enabledNotifications = subscribed;
        settings.modelKey = modelKey;
        settings.modelType = modelType;

        return settings;
    }

    private void writeSettingsFromPreferenceGroupToStringArray(PreferenceGroup pg, ArrayList<String> strings) {
        for (int i = 0; i < pg.getPreferenceCount(); i++) {
            Preference currentPreference = pg.getPreference(i);
            if (currentPreference instanceof CheckBoxPreference) {
                if (((CheckBoxPreference) currentPreference).isChecked()) {
                    strings.add(currentPreference.getKey());
                }
            } else if (currentPreference instanceof PreferenceGroup) {
                writeSettingsFromPreferenceGroupToStringArray((PreferenceGroup) currentPreference, strings);
            }
{'fraction_non_alphanumeric': 0.08353321514235061, 'fraction_numerical': 0.002398581708207321, 'mean_word_length': 3.6689386562804285, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 9, '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': '3639481', 'n_tokens_mistral': 2464, 'n_tokens_neox': 2388, 'n_words': 554}