<commit_before>package exh.ui.lock

import android.content.Context
import android.support.v7.preference.Preference
import android.text.InputType
import android.util.AttributeSet
import com.afollestad.materialdialogs.MaterialDialog
import eu.kanade.tachiyomi.R
import eu.kanade.tachiyomi.data.preference.PreferencesHelper
import rx.Observable
import rx.android.schedulers.AndroidSchedulers
import rx.schedulers.Schedulers
import uy.kohesive.injekt.injectLazy
import java.math.BigInteger
import java.security.SecureRandom

class LockPreference @JvmOverloads constructor(context: Context, attrs: AttributeSet? = null) :
        Preference(context, attrs) {

    val secureRandom by lazy { SecureRandom() }

    val prefs: PreferencesHelper by injectLazy()

    override fun onAttached() {
        super.onAttached()
        updateSummary()
    }

    fun updateSummary() {
        if(lockEnabled(prefs)) {
            summary = "Application is locked"
        } else {
            summary = "Application is not locked, tap to lock"
        }
    }

    override fun onClick() {
        super.onClick()
        if(!notifyLockSecurity(context)) {
            MaterialDialog.Builder(context)
                    .title("Lock application")
                    .content("Enter a pin to lock the application. Enter nothing to disable the pin lock.")
                    .inputRangeRes(0, 10, R.color.material_red_500)
                    .inputType(InputType.TYPE_CLASS_NUMBER)
                    .input("", "", { _, c ->
                        val progressDialog = MaterialDialog.Builder(context)
                                .title("Saving password")
                                .progress(true, 0)
                                .cancelable(false)
                                .show()
                        Observable.fromCallable {
                            savePassword(c.toString())
                        }.observeOn(Schedulers.computation())
                                .subscribeOn(AndroidSchedulers.mainThread())
                                .subscribe {
                                    progressDialog.dismiss()
                                    updateSummary()
                                }
                    })
                    .negativeText("Cancel")
                    .autoDismiss(true)
                    .cancelable(true)
                    .canceledOnTouchOutside(true)
                    .show()
        }
    }

    fun savePassword(password: String) {
        val salt: String?
        val hash: String?
        val length: Int
        if(password.isEmpty()) {
            salt = null
            hash = null
            length = -1
        } else {
            salt = BigInteger(130, secureRandom).toString(32)<commit_msg>Fix lock preference observing on wrong thread.<commit_after>package exh.ui.lock

import android.content.Context
import android.support.v7.preference.Preference
import android.text.InputType
import android.util.AttributeSet
import com.afollestad.materialdialogs.MaterialDialog
import eu.kanade.tachiyomi.R
import eu.kanade.tachiyomi.data.preference.PreferencesHelper
import rx.Observable
import rx.android.schedulers.AndroidSchedulers
import rx.schedulers.Schedulers
import uy.kohesive.injekt.injectLazy
import java.math.BigInteger
import java.security.SecureRandom

class LockPreference @JvmOverloads constructor(context: Context, attrs: AttributeSet? = null) :
        Preference(context, attrs) {

    val secureRandom by lazy { SecureRandom() }

    val prefs: PreferencesHelper by injectLazy()

    override fun onAttached() {
        super.onAttached()
        updateSummary()
    }

    fun updateSummary() {
        if(lockEnabled(prefs)) {
            summary = "Application is locked"
        } else {
            summary = "Application is not locked, tap to lock"
        }
    }

    override fun onClick() {
        super.onClick()
        if(!notifyLockSecurity(context)) {
            MaterialDialog.Builder(context)
                    .title("Lock application")
                    .content("Enter a pin to lock the application. Enter nothing to disable the pin lock.")
                    .inputRangeRes(0, 10, R.color.material_red_500)
                    .inputType(InputType.TYPE_CLASS_NUMBER)
                    .input("", "", { _, c ->
                        val progressDialog = MaterialDialog.Builder(context)
                                .title("Saving password")
                                .progress(true, 0)
                                .cancelable(false)
                                .show()
                        Observable.fromCallable {
                            savePassword(c.toString())
                        }.subscribeOn(Schedulers.computation())
                                .observeOn(AndroidSchedulers.mainThread())
                                .subscribe {
                                    progressDialog.dismiss()
                                    updateSummary()
                                }
                    })
                    .negativeText("Cancel")
                    .autoDismiss(true)
                    .cancelable(true)
                    .canceledOnTouchOutside(true)
                    .show()
        }
    }

    fun savePassword(password: String) {
        val salt: String?
        val hash: String?
        val length: Int
        if(password.isEmpty()) {
            salt = null
            hash = null
            length = -1
        } else {
            salt = BigInteger(130, secureRandom).toString(32)
{'fraction_non_alphanumeric': 0.08355698693862945, 'fraction_numerical': 0.0050098407586330294, 'mean_word_length': 1.5248419150858175, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1207873', 'n_tokens_mistral': 1404, 'n_tokens_neox': 1331, 'n_words': 287}