ref: d8ed2aaaf92ae5e71f542805f7c3c6988e78516d
app/src/main/java/xyz/apiote/bimba/czwek/settings/SettingsActivity.kt
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 |
// SPDX-FileCopyrightText: Adam Evyčędo // // SPDX-License-Identifier: AGPL-3.0-or-later package xyz.apiote.bimba.czwek.settings import android.content.Context import android.os.Bundle import android.text.format.DateUtils import android.view.View import android.view.ViewGroup import androidx.activity.enableEdgeToEdge import androidx.appcompat.app.AppCompatActivity import androidx.core.view.ViewCompat import androidx.core.view.WindowInsetsCompat import androidx.core.view.updateLayoutParams import androidx.core.view.updatePadding import androidx.preference.Preference import androidx.preference.PreferenceFragmentCompat import androidx.preference.PreferenceManager import androidx.preference.SwitchPreferenceCompat import androidx.work.ExistingWorkPolicy import androidx.work.OneTimeWorkRequestBuilder import androidx.work.OutOfQuotaPolicy import androidx.work.WorkInfo import androidx.work.WorkManager import xyz.apiote.bimba.czwek.R import java.util.concurrent.ExecutionException class SettingsActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { enableEdgeToEdge() super.onCreate(savedInstanceState) setContentView(R.layout.settings_activity) if (savedInstanceState == null) { supportFragmentManager .beginTransaction() .replace(R.id.settings, SettingsFragment()) .commit() } supportActionBar?.setDisplayHomeAsUpEnabled(true) val root = findViewById<View>(R.id.settings) ViewCompat.setOnApplyWindowInsetsListener(root) { v, windowInsets -> val insets = windowInsets.getInsets(WindowInsetsCompat.Type.systemBars()) v.updatePadding(left = windowInsets.displayCutout?.safeInsetLeft?.takeIf { it > 0 } ?: insets.left) v.updatePadding(right = windowInsets.displayCutout?.safeInsetRight?.takeIf { it > 0 } ?: insets.right) v.updateLayoutParams<ViewGroup.MarginLayoutParams> { topMargin = insets.top } windowInsets } } class SettingsFragment : PreferenceFragmentCompat() { override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) { setPreferencesFromResource(R.xml.root_preferences, rootKey) findPreference<Preference>("download_cities_list")?.setOnPreferenceClickListener { val request = OneTimeWorkRequestBuilder<DownloadCitiesWorker>() .setExpedited(OutOfQuotaPolicy.RUN_AS_NON_EXPEDITED_WORK_REQUEST) .build() WorkManager.getInstance(requireContext()) .enqueueUniqueWork("download_cities", ExistingWorkPolicy.KEEP, request) findPreference<Preference>("download_cities_list")?.isEnabled = false true } if (isWorkScheduled(requireContext(), "download_cities")) { findPreference<Preference>("download_cities_list")?.isEnabled = false } val citiesLastUpdate = PreferenceManager.getDefaultSharedPreferences(requireContext()) .getLong(DownloadCitiesWorker.LAST_UPDATE_KEY, -1) if (citiesLastUpdate > 0) { val lastUpdateTime = DateUtils.getRelativeDateTimeString( context, citiesLastUpdate * DateUtils.SECOND_IN_MILLIS, DateUtils.DAY_IN_MILLIS, DateUtils.WEEK_IN_MILLIS, 0 ) findPreference<SwitchPreferenceCompat>("autoupdate_cities_list")?.summary = getString(R.string.last_update, lastUpdateTime) } } private fun isWorkScheduled(context: Context, name: String): Boolean { val instance = WorkManager.getInstance(context) val statuses = instance.getWorkInfosForUniqueWork(name) try { var running = false val workInfoList = statuses.get() for (workInfo in workInfoList) { val state: WorkInfo.State = workInfo.state running = (state == WorkInfo.State.RUNNING) or (state == WorkInfo.State.ENQUEUED) } return running } catch (e: ExecutionException) { e.printStackTrace() return false } catch (e: InterruptedException) { e.printStackTrace() return false } } } } |