<commit_before>/*
 * Copyright 2015 75py
 *
 * 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 com.nagopy.android.aplin.model.converter

import android.app.Application
import android.content.Intent
import android.content.pm.ApplicationInfo
import android.content.pm.PackageInfo
import android.content.pm.PackageManager
import android.content.pm.PermissionGroupInfo
import android.os.Build
import com.nagopy.android.aplin.entity.App
import com.nagopy.android.aplin.model.AplinDevicePolicyManager
import com.nagopy.android.aplin.model.IconHelper
import io.realm.Realm
import timber.log.Timber
import javax.inject.Inject
import javax.inject.Singleton

@Singleton
open class AppConverter {

    @Inject
    open lateinit var application: Application

    @Inject
    open lateinit var packageManager: PackageManager

    @Inject
    open lateinit var iconHelper: IconHelper

    @Inject
    open lateinit var aplinDevicePolicyManager: AplinDevicePolicyManager

    @Inject
    constructor() {
    }

    open fun setValues(realm: Realm, app: App, applicationInfo: ApplicationInfo) {
        val params = prepare(realm, applicationInfo)

        try {
            AppParameters.values()
                    .filter { it.targetSdkVersion.contains(Build.VERSION.SDK_INT) }
                    .forEach { param ->
                        param.setValue(app, params)
                    }
        } catch(e: Exception) {
            Timber.e(e, "Error occurred. pkg=%s", applicationInfo.packageName)
            app.removeFromRealmSilently()
        }
    }

    open fun setValue(realm: Realm, app: App, applicationInfo: ApplicationInfo, appParameters: AppParameters) {
        val params = prepare(realm, applicationInfo)

        try {
            appParameters.setValue(app, params)
        } catch(e: Exception) {
            Timber.e(e, "Error occurred. pkg=%s", applicationInfo.packageName)
            app.removeFromRealmSilently()
        }
    }

    fun App.removeFromRealmSilently() {
        try {<commit_msg>Fix #78<commit_after>/*
 * Copyright 2015 75py
 *
 * 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 com.nagopy.android.aplin.model.converter

import android.app.Application
import android.content.Intent
import android.content.pm.ApplicationInfo
import android.content.pm.PackageInfo
import android.content.pm.PackageManager
import android.content.pm.PermissionGroupInfo
import android.os.Build
import com.nagopy.android.aplin.entity.App
import com.nagopy.android.aplin.model.AplinDevicePolicyManager
import com.nagopy.android.aplin.model.IconHelper
import io.realm.Realm
import timber.log.Timber
import javax.inject.Inject
import javax.inject.Singleton

@Singleton
open class AppConverter {

    @Inject
    open lateinit var application: Application

    @Inject
    open lateinit var packageManager: PackageManager

    @Inject
    open lateinit var iconHelper: IconHelper

    @Inject
    open lateinit var aplinDevicePolicyManager: AplinDevicePolicyManager

    @Inject
    constructor() {
    }

    open fun setValues(realm: Realm, app: App, applicationInfo: ApplicationInfo) {
        try {
            val params = prepare(realm, applicationInfo)
            AppParameters.values()
                    .filter { it.targetSdkVersion.contains(Build.VERSION.SDK_INT) }
                    .forEach { param ->
                        param.setValue(app, params)
                    }
        } catch(e: PackageManager.NameNotFoundException) {
            Timber.w(e, "Not found. pkg=%s", applicationInfo.packageName)
            app.removeFromRealmSilently()
        } catch(e: Exception) {
            Timber.e(e, "Error occurred. pkg=%s", applicationInfo.packageName)
            app.removeFromRealmSilently()
        }
    }

    open fun setValue(realm: Realm, app: App, applicationInfo: ApplicationInfo, appParameters: AppParameters) {
        try {
            val params = prepare(realm, applicationInfo)
            appParameters.setValue(app, params)
        } catch(e: PackageManager.NameNotFoundException) {
            Timber.w(e, "Not found. pkg=%s", applicationInfo.packageName)
            app.removeFromRealmSilently()
        } catch(e: Exception) {
            Timber.e(e, "Error occurred. pkg=%s", applicationInfo.packageName)
            app.removeFromRealmSilently()
        }
    }

    fun App.removeFromRealmSilently() {
        try {
{'fraction_non_alphanumeric': 0.08339497041420119, 'fraction_numerical': 0.0040680473372781065, 'mean_word_length': 2.9366812227074237, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '3635975', 'n_tokens_mistral': 1492, 'n_tokens_neox': 1464, 'n_words': 413}