<commit_before>/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

package org.mozilla.focus.search

import android.app.Activity
import android.content.Context
import android.support.v7.preference.PreferenceViewHolder
import android.view.ContextThemeWrapper
import android.util.AttributeSet
import android.widget.CompoundButton
import org.mozilla.focus.R

import java.util.HashSet

class MultiselectSearchEngineListPreference(context: Context, attrs: AttributeSet) :
        SearchEngineListPreference(context, attrs) {

    override val itemResId: Int
        get() = R.layout.search_engine_checkbox_button

    val checkedEngineIds: Set<String>
        get() {
            val engineIdSet = HashSet<String>()

            for (i in 0 until searchEngineGroup!!.childCount) {
                val engineButton = searchEngineGroup!!.getChildAt(i) as CompoundButton
                if (engineButton.isChecked) {
                    engineIdSet.add(engineButton.tag as String)
                }
            }
            return engineIdSet
        }

    override fun onBindViewHolder(holder: PreferenceViewHolder?) {
        super.onBindViewHolder(holder)
        this.bindEngineCheckboxesToMenu()
    }

    override fun updateDefaultItem(defaultButton: CompoundButton) {
        defaultButton.isClickable = false
        // Showing the default engine as disabled requires a StateListDrawable, but since there
        // is no state_clickable and state_enabled seems to require a default drawable state,
        // use state_activated instead to designate the default search engine.
        defaultButton.isActivated = true
    }

    // Whenever an engine is checked or unchecked, we notify the menu
    private fun bindEngineCheckboxesToMenu() {
        for (i in 0 until searchEngineGroup!!.childCount) {
            val engineButton = searchEngineGroup!!.getChildAt(i) as CompoundButton
            engineButton.setOnCheckedChangeListener { _, _ ->
                val context = context
                if (context is ContextThemeWrapper) {
                    if (context.baseContext is Activity) {
                        (context.baseContext as Activity).invalidateOptionsMenu()
                    }
                } else {
                    (context as Activity).invalidateOptionsMenu()
                }
            }
        }
    }

    fun atLeastOneEngineChecked(): Boolean {
        for (i in 0 until searchEngineGroup!!.childCount) {
            val engineButton = searchEngineGroup!!.getChildAt(i) as CompoundButton
            if (engineButton.isChecked) {
                return true
            }
        }
        return false
    }
}<commit_msg>Issue #3254: Use the context casting extension function<commit_after>/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

package org.mozilla.focus.search

import android.content.Context
import android.support.v7.preference.PreferenceViewHolder
import android.util.AttributeSet
import android.widget.CompoundButton
import org.mozilla.focus.R
import org.mozilla.focus.utils.asActivity
import java.util.HashSet

class MultiselectSearchEngineListPreference(context: Context, attrs: AttributeSet) :
        SearchEngineListPreference(context, attrs) {

    override val itemResId: Int
        get() = R.layout.search_engine_checkbox_button

    val checkedEngineIds: Set<String>
        get() {
            val engineIdSet = HashSet<String>()

            for (i in 0 until searchEngineGroup!!.childCount) {
                val engineButton = searchEngineGroup!!.getChildAt(i) as CompoundButton
                if (engineButton.isChecked) {
                    engineIdSet.add(engineButton.tag as String)
                }
            }
            return engineIdSet
        }

    override fun onBindViewHolder(holder: PreferenceViewHolder?) {
        super.onBindViewHolder(holder)
        this.bindEngineCheckboxesToMenu()
    }

    override fun updateDefaultItem(defaultButton: CompoundButton) {
        defaultButton.isClickable = false
        // Showing the default engine as disabled requires a StateListDrawable, but since there
        // is no state_clickable and state_enabled seems to require a default drawable state,
        // use state_activated instead to designate the default search engine.
        defaultButton.isActivated = true
    }

    // Whenever an engine is checked or unchecked, we notify the menu
    private fun bindEngineCheckboxesToMenu() {
        for (i in 0 until searchEngineGroup!!.childCount) {
            val engineButton = searchEngineGroup!!.getChildAt(i) as CompoundButton
            engineButton.setOnCheckedChangeListener { _, _ ->
                val context = context
                context?.asActivity()?.invalidateOptionsMenu()
            }
        }
    }

    fun atLeastOneEngineChecked(): Boolean {
        for (i in 0 until searchEngineGroup!!.childCount) {
            val engineButton = searchEngineGroup!!.getChildAt(i) as CompoundButton
            if (engineButton.isChecked) {
                return true
            }
        }
        return false
    }
}
{'fraction_non_alphanumeric': 0.07116382505559674, 'fraction_numerical': 0.0037064492216456633, 'mean_word_length': 2.431023521932613, 'pattern_counts': {'":': 0, '<': 7, '<?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': '5835357', 'n_tokens_mistral': 1400, 'n_tokens_neox': 1336, 'n_words': 395}