<commit_before>package ru.dyatel.tsuschedule.updater

import android.app.Activity
import android.app.AlertDialog
import android.app.PendingIntent
import android.app.ProgressDialog
import android.content.Context
import android.content.Intent
import android.support.v4.app.NotificationCompat
import android.support.v7.widget.DividerItemDecoration
import android.support.v7.widget.LinearLayoutManager
import com.mikepenz.fastadapter.FastAdapter
import com.mikepenz.fastadapter.adapters.ItemAdapter
import hirondelle.date4j.DateTime
import kotlinx.coroutines.experimental.android.UI
import kotlinx.coroutines.experimental.async
import kotlinx.coroutines.experimental.launch
import org.jetbrains.anko.frameLayout
import org.jetbrains.anko.indeterminateProgressDialog
import org.jetbrains.anko.intentFor
import org.jetbrains.anko.leftPadding
import org.jetbrains.anko.matchParent
import org.jetbrains.anko.notificationManager
import org.jetbrains.anko.progressDialog
import org.jetbrains.anko.recyclerview.v7.recyclerView
import org.jetbrains.anko.rightPadding
import org.jetbrains.anko.topPadding
import ru.dyatel.tsuschedule.BadGroupException
import ru.dyatel.tsuschedule.BuildConfig
import ru.dyatel.tsuschedule.INTENT_TYPE
import ru.dyatel.tsuschedule.INTENT_TYPE_UPDATE
import ru.dyatel.tsuschedule.MainActivity
import ru.dyatel.tsuschedule.NOTIFICATION_CHANNEL_UPDATES
import ru.dyatel.tsuschedule.NOTIFICATION_UPDATE
import ru.dyatel.tsuschedule.R
import ru.dyatel.tsuschedule.database.database
import ru.dyatel.tsuschedule.handle
import ru.dyatel.tsuschedule.layout.ChangelogItem
import ru.dyatel.tsuschedule.layout.DIM_DIALOG_SIDE_PADDING
import ru.dyatel.tsuschedule.utilities.Validator
import ru.dyatel.tsuschedule.utilities.download
import ru.dyatel.tsuschedule.utilities.schedulePreferences
import java.io.File
import java.net.URL
import java.util.TimeZone

class Updater(activity: Activity) {

    private val context: Context = activity
    private val preferences = context.schedulePreferences

    private val changelogDao = activity.database.changelogs

    private val api = UpdaterApi()

    fun fetchUpdate(): ReleaseToken? {
        val allowPrerelease = preferences.allowPrerelease

        api.setTimeout(preferences.connectionTimeout)

        val releases = api.getReleases()
        changelogDao.save(releases)

        val update = releases
                .sortedByDescending { it.release }
                .firstOrNull { !it.prerelease || allowPrerelease }
                ?.takeIf { Release.CURRENT < it.release }

        preferences.lastRelease = update?.url
        return update
    }

    fun installUpdate(file: File) {
        val uri = UpdateFileProvider.getUriForFile(context, file)
        val intent = Intent(Intent.ACTION_INSTALL_PACKAGE, uri)
                .putExtra(Intent.EXTRA_NOT_UNKNOWN_SOURCE, true)
        context.startActivity(intent)
    }

    fun handleMigration() {
        val lastUsedVersion = preferences.lastUsedVersion ?: -1
        if (lastUsedVersion == BuildConfig.VERSION_CODE) {
            return
        }

        if (lastUsedVersion <= 11) {
            try {
                preferences.group
                        ?.let { Validator.validateGroup(it) }
                        ?.let { preferences.addGroup(it) }
            } catch (e: BadGroupException) {
            }
        }
        preferences.lastUsedVersion = BuildConfig.VERSION_CODE

        if (lastUsedVersion != -1) {
            showChangelog()
        }
    }

    fun checkUpdatesInBackground() {
        val now = DateTime.now(TimeZone.getDefault())

        val scheduled = preferences.lastAutoupdate?.plusDays(3)
        if (scheduled?.gt(now) == true) {<commit_msg>Show changelog only if it was supported before<commit_after>package ru.dyatel.tsuschedule.updater

import android.app.Activity
import android.app.AlertDialog
import android.app.PendingIntent
import android.app.ProgressDialog
import android.content.Context
import android.content.Intent
import android.support.v4.app.NotificationCompat
import android.support.v7.widget.DividerItemDecoration
import android.support.v7.widget.LinearLayoutManager
import com.mikepenz.fastadapter.FastAdapter
import com.mikepenz.fastadapter.adapters.ItemAdapter
import hirondelle.date4j.DateTime
import kotlinx.coroutines.experimental.android.UI
import kotlinx.coroutines.experimental.async
import kotlinx.coroutines.experimental.launch
import org.jetbrains.anko.frameLayout
import org.jetbrains.anko.indeterminateProgressDialog
import org.jetbrains.anko.intentFor
import org.jetbrains.anko.leftPadding
import org.jetbrains.anko.matchParent
import org.jetbrains.anko.notificationManager
import org.jetbrains.anko.progressDialog
import org.jetbrains.anko.recyclerview.v7.recyclerView
import org.jetbrains.anko.rightPadding
import org.jetbrains.anko.topPadding
import ru.dyatel.tsuschedule.BadGroupException
import ru.dyatel.tsuschedule.BuildConfig
import ru.dyatel.tsuschedule.INTENT_TYPE
import ru.dyatel.tsuschedule.INTENT_TYPE_UPDATE
import ru.dyatel.tsuschedule.MainActivity
import ru.dyatel.tsuschedule.NOTIFICATION_CHANNEL_UPDATES
import ru.dyatel.tsuschedule.NOTIFICATION_UPDATE
import ru.dyatel.tsuschedule.R
import ru.dyatel.tsuschedule.database.database
import ru.dyatel.tsuschedule.handle
import ru.dyatel.tsuschedule.layout.ChangelogItem
import ru.dyatel.tsuschedule.layout.DIM_DIALOG_SIDE_PADDING
import ru.dyatel.tsuschedule.utilities.Validator
import ru.dyatel.tsuschedule.utilities.download
import ru.dyatel.tsuschedule.utilities.schedulePreferences
import java.io.File
import java.net.URL
import java.util.TimeZone

class Updater(activity: Activity) {

    private val context: Context = activity
    private val preferences = context.schedulePreferences

    private val changelogDao = activity.database.changelogs

    private val api = UpdaterApi()

    fun fetchUpdate(): ReleaseToken? {
        val allowPrerelease = preferences.allowPrerelease

        api.setTimeout(preferences.connectionTimeout)

        val releases = api.getReleases()
        changelogDao.save(releases)

        val update = releases
                .sortedByDescending { it.release }
                .firstOrNull { !it.prerelease || allowPrerelease }
                ?.takeIf { Release.CURRENT < it.release }

        preferences.lastRelease = update?.url
        return update
    }

    fun installUpdate(file: File) {
        val uri = UpdateFileProvider.getUriForFile(context, file)
        val intent = Intent(Intent.ACTION_INSTALL_PACKAGE, uri)
                .putExtra(Intent.EXTRA_NOT_UNKNOWN_SOURCE, true)
        context.startActivity(intent)
    }

    fun handleMigration() {
        val lastUsedVersion = preferences.lastUsedVersion ?: -1
        if (lastUsedVersion == BuildConfig.VERSION_CODE) {
            return
        }

        if (lastUsedVersion <= 11) {
            try {
                preferences.group
                        ?.let { Validator.validateGroup(it) }
                        ?.let { preferences.addGroup(it) }
            } catch (e: BadGroupException) {
            }
        }
        preferences.lastUsedVersion = BuildConfig.VERSION_CODE

        if (lastUsedVersion >= 15) {
            showChangelog()
        }
    }

    fun checkUpdatesInBackground() {
        val now = DateTime.now(TimeZone.getDefault())

        val scheduled = preferences.lastAutoupdate?.plusDays(3)
        if (scheduled?.gt(now) == true) {
{'fraction_non_alphanumeric': 0.07991940899932841, 'fraction_numerical': 0.0028206850235057085, 'mean_word_length': 3.4936632468316233, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 4, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1084128', 'n_tokens_mistral': 2241, 'n_tokens_neox': 2192, 'n_words': 381}