<commit_before>package com.nagopy.android.aplin.model

import android.app.Application
import android.content.pm.ApplicationInfo
import android.content.pm.PackageManager
import android.os.Build
import android.os.Handler
import android.os.Looper
import android.os.UserHandle
import com.nagopy.android.aplin.entity.AppEntity
import com.nagopy.android.aplin.model.converter.AppConverter
import com.nagopy.android.aplin.model.preference.SortSetting
import io.realm.Realm
import io.realm.RealmResults
import timber.log.Timber
import javax.inject.Inject
import javax.inject.Singleton

@Singleton
open class Applications
@Inject constructor(
        var application: Application
        , val packageManager: PackageManager
        , val appConverter: AppConverter
        , val sortSetting: SortSetting
) {

    val handler: Handler = Handler(Looper.getMainLooper())

    open fun initialize(func: () -> Unit) {
        Thread().run {
            if (!isLoaded()) {
                refresh()
            }
            handler.post {
                func()
            }
        }
    }

    open fun isLoaded(): Boolean {
        Realm.getInstance(application).use {
            return it.where(AppEntity::class.java).count() > 0
        }
    }

    open fun refresh() {
        val enabledSettingField: FieldReflection<Int> = FieldReflection(ApplicationInfo::class.java, "enabledSetting")
        val realm = Realm.getInstance(application)
        realm.use {
            realm.executeTransaction {
                realm.where(AppEntity::class.java).findAll().clear()
                val allApps = getInstalledApplications()
                allApps.forEach {
                    if (!it.enabled) {
                        // 無効になっていて、かつenabledSettingが3でないアプリは除外する
                        val enabledSetting = enabledSettingField.get(it)
                        if (enabledSetting != PackageManager.COMPONENT_ENABLED_STATE_DISABLED_USER) {
                            Timber.d("skip:" + it.packageName)
                            return@forEach
                        }<commit_msg>Load applications in new thread<commit_after>package com.nagopy.android.aplin.model

import android.app.Application
import android.content.pm.ApplicationInfo
import android.content.pm.PackageManager
import android.os.Build
import android.os.Handler
import android.os.Looper
import android.os.UserHandle
import com.nagopy.android.aplin.entity.AppEntity
import com.nagopy.android.aplin.model.converter.AppConverter
import com.nagopy.android.aplin.model.preference.SortSetting
import io.realm.Realm
import io.realm.RealmResults
import timber.log.Timber
import javax.inject.Inject
import javax.inject.Singleton

@Singleton
open class Applications
@Inject constructor(
        var application: Application
        , val packageManager: PackageManager
        , val appConverter: AppConverter
        , val sortSetting: SortSetting
) {

    val handler: Handler = Handler(Looper.getMainLooper())

    open fun initialize(func: () -> Unit) {
        Thread({
            if (!isLoaded()) {
                refresh()
            }
            handler.post {
                func()
            }
        }).start()
    }

    open fun isLoaded(): Boolean {
        Realm.getInstance(application).use {
            return it.where(AppEntity::class.java).count() > 0
        }
    }

    open fun refresh() {
        val enabledSettingField: FieldReflection<Int> = FieldReflection(ApplicationInfo::class.java, "enabledSetting")
        val realm = Realm.getInstance(application)
        realm.use {
            realm.executeTransaction {
                realm.where(AppEntity::class.java).findAll().clear()
                val allApps = getInstalledApplications()
                allApps.forEach {
                    if (!it.enabled) {
                        // 無効になっていて、かつenabledSettingが3でないアプリは除外する
                        val enabledSetting = enabledSettingField.get(it)
                        if (enabledSetting != PackageManager.COMPONENT_ENABLED_STATE_DISABLED_USER) {
                            Timber.d("skip:" + it.packageName)
                            return@forEach
                        }
{'fraction_non_alphanumeric': 0.0895808383233533, 'fraction_numerical': 0.0009580838323353293, 'mean_word_length': 2.2024539877300615, 'pattern_counts': {'":': 0, '<': 5, '<?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': '6541132', 'n_tokens_mistral': 1127, 'n_tokens_neox': 1103, 'n_words': 209}