<commit_before>/*
 * ProgrammingActivity.kt
 *
 * Copyright 2016 <NAME> (<EMAIL>)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package net.thauvin.erik.android.tesremoteprogrammer

import android.Manifest
import android.content.Context
import android.content.res.ColorStateList
import android.graphics.Color
import android.graphics.Typeface
import android.os.Bundle
import android.support.design.widget.Snackbar
import android.support.v7.app.AppCompatActivity
import android.text.InputFilter
import android.text.InputType
import android.text.TextUtils
import android.util.TypedValue
import android.view.Gravity.*
import android.view.ViewManager
import android.view.inputmethod.EditorInfo
import android.view.inputmethod.InputMethodManager
import android.widget.EditText
import com.lb.auto_fit_textview.AutoResizeTextView
import net.thauvin.erik.android.tesremoteprogrammer.filters.AlphaFilter
import net.thauvin.erik.android.tesremoteprogrammer.filters.MinMaxFilter
import net.thauvin.erik.android.tesremoteprogrammer.filters.NumberFilter
import net.thauvin.erik.android.tesremoteprogrammer.models.Option
import net.thauvin.erik.android.tesremoteprogrammer.models.Params
import net.thauvin.erik.android.tesremoteprogrammer.util.Dtmf
import net.thauvin.erik.android.tesremoteprogrammer.util.isDKS
import net.thauvin.erik.android.tesremoteprogrammer.util.isLinear
import net.thauvin.erik.android.tesremoteprogrammer.widget.ScrollAwareFABBehavior
import org.jetbrains.anko.*
import org.jetbrains.anko.custom.ankoView
import org.jetbrains.anko.design.coordinatorLayout
import org.jetbrains.anko.design.floatingActionButton
import org.jetbrains.anko.design.textInputLayout
import org.jetbrains.anko.support.v4.nestedScrollView
import permissions.dispatcher.NeedsPermission
import permissions.dispatcher.RuntimePermissions
import java.util.*

@RuntimePermissions
class ProgrammingActivity : AppCompatActivity(), AnkoLogger {

    inline fun ViewManager.autofitTextView(theme: Int = 0, init: AutoResizeTextView.() -> Unit) = ankoView({ AutoResizeTextView(it) }, theme, init)

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        val params: Params = intent.extras.getParcelable("net.thauvin.erik.android.tesremoteprogrammer.models.Params")
        val option: Option = intent.extras.getParcelable("net.thauvin.erik.android.tesremoteprogrammer.models.Option")
        val fields = arrayListOf<EditText>()

        coordinatorLayout {
            // option title
            autofitTextView {
                padding = dip(20)
                text = option.title
                setTextSize(TypedValue.COMPLEX_UNIT_DIP, 20f)
                singleLine = true
                maxLines = 1
                typeface = Typeface.DEFAULT_BOLD
                isFocusableInTouchMode = true
                ellipsize = TextUtils.TruncateAt.END
            }.lparams(width = matchParent, height = matchParent)

            nestedScrollView {
                topPadding = dip(50)
                bottomPadding = dip(30)
                horizontalPadding = dip(20)

                verticalLayout {
                    lparams(width = matchParent, height = wrapContent)

                    // fields
                    val it = option.fields.iterator()
                    while (it.hasNext()) {
                        val field = it.next()

                        textInputLayout {
                            horizontalPadding = dip(40)
                            lparams(width = matchParent)

                            val inputFilters: ArrayList<InputFilter> = ArrayList()

                            val editText = editText() {
                                hint = field!!.hint

                                if (field.alpha) {
                                    if (params.type.isDKS()) {
                                        inputType = InputType.TYPE_CLASS_TEXT or InputType.TYPE_TEXT_FLAG_CAP_CHARACTERS
                                        inputFilters.add(AlphaFilter(Dtmf.DKS_EXTRAS))
                                    } else if (params.type.isLinear()) {
                                        inputType = InputType.TYPE_CLASS_TEXT or InputType.TYPE_TEXT_FLAG_CAP_WORDS
                                        inputFilters.add(AlphaFilter(Dtmf.LINEAR_EXTRAS))
                                    }
                                } else {
                                    inputType = InputType.TYPE_CLASS_PHONE
                                    inputFilters.add(NumberFilter(field.digits, if (field.alt) params.alt else empty))
                                    if (field.max != -1 && field.min != -1) {
                                        inputFilters.add(
                                                MinMaxFilter(
                                                        field.min,
                                                        field.max,
                                                        field.size,
                                                        params.type.isDKS() || field.zeros))
                                    }
                                }

                                if (field.size != -1) {
                                    inputFilters.add(InputFilter.LengthFilter(field.size))
                                }

                                if (inputFilters.isNotEmpty()) {
                                    filters = inputFilters.toTypedArray()
                                }

                                if (!it.hasNext()) {
                                    imeOptions = EditorInfo.IME_ACTION_DONE
                                    setOnEditorActionListener { v, id, event ->
                                        if (id == EditorInfo.IME_ACTION_DONE) {
                                            clearFocus()
                                            val imm = getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
                                            imm.hideSoftInputFromWindow(windowToken, 0)
                                            true
                                        } else {
                                            false
                                        }
                                    }
                                }
                            }
                            fields.add(editText)
                        }
                    }
                }
            }.lparams(width = matchParent, height = matchParent)

            // dialpad FAB
            if (!option.nosteps) {
                floatingActionButton {
                    imageResource = R.drawable.ic_menu_dialpad_lt

                    if (!option.nodial) {
                        backgroundTintList = ColorStateList.valueOf(Color.GRAY)
                    }

                    onClick {
                        if (validateFields(params.type, fields, option)) {
                            val dtmf = Dtmf.build(params.type, params.master, params.ack, option, fields)
                            if (Dtmf.validate(dtmf, "${MainActivity.PAUSE}${params.ack}${params.alt}")) {
                                val begin = if (params.begin.isNotBlank()) {
                                    "${params.begin}${MainActivity.PAUSE}"
                                } else {
                                    ""
                                }

                                val end = if (params.end.isNotBlank()) {
                                    "${MainActivity.PAUSE}${params.end}"
                                } else {
                                    ""
                                }

                                startActivity<StepsActivity>(
                                        StepsActivity.EXTRA_STEPS to "$begin$dtmf$end".split(','))
                            } else {
                                Snackbar.make(this@coordinatorLayout, getString(R.string.error_invalid_dtmf, dtmf),
                                        Snackbar.LENGTH_LONG).show()
                            }
                        } else {
                            Snackbar.make(this@coordinatorLayout, R.string.error_invalid_field,
                                    Snackbar.LENGTH_LONG).show()
                        }
                    }
                }.lparams(width = wrapContent, height = wrapContent) {
                    if (option.nodial) {
                        gravity = BOTTOM or END
                        rightMargin = dip(16)
                    } else {
                        gravity = BOTTOM or START
                        leftMargin = dip(16)
                    }
<commit_msg>Added empty string val.<commit_after>/*
 * ProgrammingActivity.kt
 *
 * Copyright 2016 <NAME> (<EMAIL>)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package net.thauvin.erik.android.tesremoteprogrammer

import android.Manifest
import android.content.Context
import android.content.res.ColorStateList
import android.graphics.Color
import android.graphics.Typeface
import android.os.Bundle
import android.support.design.widget.Snackbar
import android.support.v7.app.AppCompatActivity
import android.text.InputFilter
import android.text.InputType
import android.text.TextUtils
import android.util.TypedValue
import android.view.Gravity.*
import android.view.ViewManager
import android.view.inputmethod.EditorInfo
import android.view.inputmethod.InputMethodManager
import android.widget.EditText
import com.lb.auto_fit_textview.AutoResizeTextView
import net.thauvin.erik.android.tesremoteprogrammer.filters.AlphaFilter
import net.thauvin.erik.android.tesremoteprogrammer.filters.MinMaxFilter
import net.thauvin.erik.android.tesremoteprogrammer.filters.NumberFilter
import net.thauvin.erik.android.tesremoteprogrammer.models.Option
import net.thauvin.erik.android.tesremoteprogrammer.models.Params
import net.thauvin.erik.android.tesremoteprogrammer.util.Dtmf
import net.thauvin.erik.android.tesremoteprogrammer.util.isDKS
import net.thauvin.erik.android.tesremoteprogrammer.util.isLinear
import net.thauvin.erik.android.tesremoteprogrammer.widget.ScrollAwareFABBehavior
import org.jetbrains.anko.*
import org.jetbrains.anko.custom.ankoView
import org.jetbrains.anko.design.coordinatorLayout
import org.jetbrains.anko.design.floatingActionButton
import org.jetbrains.anko.design.textInputLayout
import org.jetbrains.anko.support.v4.nestedScrollView
import permissions.dispatcher.NeedsPermission
import permissions.dispatcher.RuntimePermissions
import java.util.*

@RuntimePermissions
class ProgrammingActivity : AppCompatActivity(), AnkoLogger {
    val empty = ""

    inline fun ViewManager.autofitTextView(theme: Int = 0, init: AutoResizeTextView.() -> Unit) = ankoView({ AutoResizeTextView(it) }, theme, init)

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        val params: Params = intent.extras.getParcelable("net.thauvin.erik.android.tesremoteprogrammer.models.Params")
        val option: Option = intent.extras.getParcelable("net.thauvin.erik.android.tesremoteprogrammer.models.Option")
        val fields = arrayListOf<EditText>()

        coordinatorLayout {
            // option title
            autofitTextView {
                padding = dip(20)
                text = option.title
                setTextSize(TypedValue.COMPLEX_UNIT_DIP, 20f)
                singleLine = true
                maxLines = 1
                typeface = Typeface.DEFAULT_BOLD
                isFocusableInTouchMode = true
                ellipsize = TextUtils.TruncateAt.END
            }.lparams(width = matchParent, height = matchParent)

            nestedScrollView {
                topPadding = dip(50)
                bottomPadding = dip(30)
                horizontalPadding = dip(20)

                verticalLayout {
                    lparams(width = matchParent, height = wrapContent)

                    // fields
                    val it = option.fields.iterator()
                    while (it.hasNext()) {
                        val field = it.next()

                        textInputLayout {
                            horizontalPadding = dip(40)
                            lparams(width = matchParent)

                            val inputFilters: ArrayList<InputFilter> = ArrayList()

                            val editText = editText() {
                                hint = field!!.hint

                                if (field.alpha) {
                                    if (params.type.isDKS()) {
                                        inputType = InputType.TYPE_CLASS_TEXT or InputType.TYPE_TEXT_FLAG_CAP_CHARACTERS
                                        inputFilters.add(AlphaFilter(Dtmf.DKS_EXTRAS))
                                    } else if (params.type.isLinear()) {
                                        inputType = InputType.TYPE_CLASS_TEXT or InputType.TYPE_TEXT_FLAG_CAP_WORDS
                                        inputFilters.add(AlphaFilter(Dtmf.LINEAR_EXTRAS))
                                    }
                                } else {
                                    inputType = InputType.TYPE_CLASS_PHONE
                                    inputFilters.add(NumberFilter(field.digits, if (field.alt) params.alt else empty))
                                    if (field.max != -1 && field.min != -1) {
                                        inputFilters.add(
                                                MinMaxFilter(
                                                        field.min,
                                                        field.max,
                                                        field.size,
                                                        params.type.isDKS() || field.zeros))
                                    }
                                }

                                if (field.size != -1) {
                                    inputFilters.add(InputFilter.LengthFilter(field.size))
                                }

                                if (inputFilters.isNotEmpty()) {
                                    filters = inputFilters.toTypedArray()
                                }

                                if (!it.hasNext()) {
                                    imeOptions = EditorInfo.IME_ACTION_DONE
                                    setOnEditorActionListener { v, id, event ->
                                        if (id == EditorInfo.IME_ACTION_DONE) {
                                            clearFocus()
                                            val imm = getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
                                            imm.hideSoftInputFromWindow(windowToken, 0)
                                            true
                                        } else {
                                            false
                                        }
                                    }
                                }
                            }
                            fields.add(editText)
                        }
                    }
                }
            }.lparams(width = matchParent, height = matchParent)

            // dialpad FAB
            if (!option.nosteps) {
                floatingActionButton {
                    imageResource = R.drawable.ic_menu_dialpad_lt

                    if (!option.nodial) {
                        backgroundTintList = ColorStateList.valueOf(Color.GRAY)
                    }

                    onClick {
                        if (validateFields(params.type, fields, option)) {
                            val dtmf = Dtmf.build(params.type, params.master, params.ack, option, fields)
                            if (Dtmf.validate(dtmf, "${MainActivity.PAUSE}${params.ack}${params.alt}")) {
                                val begin = if (params.begin.isNotBlank()) {
                                    "${params.begin}${MainActivity.PAUSE}"
                                } else {
                                    empty
                                }

                                val end = if (params.end.isNotBlank()) {
                                    "${MainActivity.PAUSE}${params.end}"
                                } else {
                                    empty
                                }

                                startActivity<StepsActivity>(
                                        StepsActivity.EXTRA_STEPS to "$begin$dtmf$end".split(','))
                            } else {
                                Snackbar.make(this@coordinatorLayout,
                                        getString(R.string.error_invalid_dtmf, dtmf),
                                        Snackbar.LENGTH_LONG).show()
                            }
                        } else {
                            Snackbar.make(this@coordinatorLayout, R.string.error_invalid_field,
                                    Snackbar.LENGTH_LONG).show()
                        }
                    }
                }.lparams(width = wrapContent, height = wrapContent) {
                    if (option.nodial) {
                        gravity = BOTTOM or END
                        rightMargin = dip(16)
                    } else {
                        gravity = BOTTOM or START
                        leftMargin = dip(16)
                    }

{'fraction_non_alphanumeric': 0.0733243606998654, 'fraction_numerical': 0.0034454912516823687, 'mean_word_length': 1.2394213381555155, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 119, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3637618', 'n_tokens_mistral': 4401, 'n_tokens_neox': 4180, 'n_words': 793}