<commit_before>package org.odk.collect.android.preferences;

import android.content.SharedPreferences;
import android.preference.Preference;
import android.preference.PreferenceCategory;
import android.preference.PreferenceScreen;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;

import static org.odk.collect.android.preferences.PreferencesActivity.INTENT_KEY_ADMIN_MODE;

class DisabledPreferencesRemover {
    /** A map used to find the parent category of any preference */ // ToDo: find a better way?
    private final Map<Preference, PreferenceCategory> preferencePreferenceCategoryMap;

    private PreferencesActivity pa;

    DisabledPreferencesRemover(PreferencesActivity pa) {
        this.pa = pa;
        preferencePreferenceCategoryMap = createPreferenceToPreferenceCategoryMap();
    }

    private Map<Preference, PreferenceCategory> createPreferenceToPreferenceCategoryMap() {
        final Map<Preference, PreferenceCategory> map = new HashMap<>();
        PreferenceScreen screen = pa.getPreferenceScreen();
        int numPrefs = screen.getPreferenceCount();
        for (int i = 0; i < numPrefs; i++) {
            Preference p = screen.getPreference(i);
            if (p instanceof PreferenceCategory) {
                PreferenceCategory pc = (PreferenceCategory) p;
                for (int j = 0; j < pc.getPreferenceCount(); ++j) {
                    map.put(pc.getPreference(j), pc);
                }
            }
        }
        return map;
    }

    /**
     * Removes any preferences from the category that are excluded by the admin settings,
     * and deletes the entire category if it is then empty.
     *
     * @param adminAndGeneralKeyses one or more AdminAndGeneralKeys objects.
     */
    void remove(AdminAndGeneralKeys... adminAndGeneralKeyses) {
        final boolean adminMode = pa.getIntent().getBooleanExtra(INTENT_KEY_ADMIN_MODE, false);

        final SharedPreferences adminPreferences = pa.getSharedPreferences(
                AdminPreferencesActivity.ADMIN_PREFERENCES, 0);

        for (AdminAndGeneralKeys keys : adminAndGeneralKeyses) {
            final boolean prefAllowed = adminPreferences.getBoolean(keys.adminKey, true);

            if (!prefAllowed && !adminMode) {
                Preference pref = pa.pref(keys.generalKey);
                PreferenceCategory preferenceCategory = preferencePreferenceCategoryMap.get(pref);
                if (preferenceCategory != null && pref != null) { // Neither should ever be null
                    preferenceCategory.removePreference(pref);
                }
            }
        }
    }

    void removeEmptyCategories() {
        final boolean adminMode = pa.getIntent().getBooleanExtra(INTENT_KEY_ADMIN_MODE, false);
        for (PreferenceCategory pc : new HashSet<>(preferencePreferenceCategoryMap.values())) {
            if (pc.getPreferenceCount() == 0 && !adminMode) {
                pa.getPreferenceScreen().removePreference(pc);
            }
        }
    }
}<commit_msg>Fix comments.<commit_after>package org.odk.collect.android.preferences;

import android.content.SharedPreferences;
import android.preference.Preference;
import android.preference.PreferenceCategory;
import android.preference.PreferenceScreen;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;

import static org.odk.collect.android.preferences.PreferencesActivity.INTENT_KEY_ADMIN_MODE;

class DisabledPreferencesRemover {
    /** A map used to find the parent category of any preference */ // ToDo: find a better way?
    private final Map<Preference, PreferenceCategory> preferencePreferenceCategoryMap;

    private PreferencesActivity pa;

    DisabledPreferencesRemover(PreferencesActivity pa) {
        this.pa = pa;
        preferencePreferenceCategoryMap = createPreferenceToPreferenceCategoryMap();
    }

    private Map<Preference, PreferenceCategory> createPreferenceToPreferenceCategoryMap() {
        final Map<Preference, PreferenceCategory> map = new HashMap<>();
        PreferenceScreen screen = pa.getPreferenceScreen();
        int numPrefs = screen.getPreferenceCount();
        for (int i = 0; i < numPrefs; i++) {
            Preference p = screen.getPreference(i);
            if (p instanceof PreferenceCategory) {
                PreferenceCategory pc = (PreferenceCategory) p;
                for (int j = 0; j < pc.getPreferenceCount(); ++j) {
                    map.put(pc.getPreference(j), pc);
                }
            }
        }
        return map;
    }

    /**
     * Removes any preferences from the category that are excluded by the admin settings.
     *
     * @param adminAndGeneralKeyses one or more AdminAndGeneralKeys objects.
     */
    void remove(AdminAndGeneralKeys... adminAndGeneralKeyses) {
        final boolean adminMode = pa.getIntent().getBooleanExtra(INTENT_KEY_ADMIN_MODE, false);

        final SharedPreferences adminPreferences = pa.getSharedPreferences(
                AdminPreferencesActivity.ADMIN_PREFERENCES, 0);

        for (AdminAndGeneralKeys keys : adminAndGeneralKeyses) {
            final boolean prefAllowed = adminPreferences.getBoolean(keys.adminKey, true);

            if (!prefAllowed && !adminMode) {
                Preference pref = pa.pref(keys.generalKey);
                PreferenceCategory preferenceCategory = preferencePreferenceCategoryMap.get(pref);
                if (preferenceCategory != null && pref != null) { // Neither should ever be null
                    preferenceCategory.removePreference(pref);
                }
            }
        }
    }

    /** Deletes all empty PreferenceCategory items. */
    void removeEmptyCategories() {
        final boolean adminMode = pa.getIntent().getBooleanExtra(INTENT_KEY_ADMIN_MODE, false);
        for (PreferenceCategory pc : new HashSet<>(preferencePreferenceCategoryMap.values())) {
            if (pc.getPreferenceCount() == 0 && !adminMode) {
                pa.getPreferenceScreen().removePreference(pc);
            }
        }
    }
}
{'fraction_non_alphanumeric': 0.08717948717948718, 'fraction_numerical': 0.0013234077750206782, 'mean_word_length': 2.819330385344283, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 13, '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': '3961668', 'n_tokens_mistral': 1579, 'n_tokens_neox': 1542, 'n_words': 368}