Fix request ignore battery optimizations

This commit is contained in:
世界 2024-03-17 23:58:54 +08:00
parent ae7166a699
commit 662cc8edf9
No known key found for this signature in database
GPG key ID: CD109927C34A63C4

View file

@ -1,5 +1,7 @@
package io.nekohasekai.sfa.ui.main package io.nekohasekai.sfa.ui.main
import android.annotation.SuppressLint
import android.app.Activity
import android.content.Intent import android.content.Intent
import android.net.Uri import android.net.Uri
import android.os.Build import android.os.Build
@ -8,6 +10,7 @@ import android.view.LayoutInflater
import android.view.View import android.view.View
import android.view.ViewGroup import android.view.ViewGroup
import androidx.activity.result.contract.ActivityResultContracts import androidx.activity.result.contract.ActivityResultContracts
import androidx.annotation.RequiresApi
import androidx.core.view.isGone import androidx.core.view.isGone
import androidx.core.view.isVisible import androidx.core.view.isVisible
import androidx.fragment.app.Fragment import androidx.fragment.app.Fragment
@ -32,24 +35,25 @@ import kotlinx.coroutines.withContext
class SettingsFragment : Fragment() { class SettingsFragment : Fragment() {
private var binding: FragmentSettingsBinding? = null private lateinit var binding: FragmentSettingsBinding
override fun onCreateView( override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle? inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?
): View { ): View {
val binding = FragmentSettingsBinding.inflate(inflater, container, false) binding = FragmentSettingsBinding.inflate(inflater, container, false)
this.binding = binding
onCreate() onCreate()
return binding.root return binding.root
} }
@RequiresApi(Build.VERSION_CODES.M)
private val requestIgnoreBatteryOptimizations = registerForActivityResult( private val requestIgnoreBatteryOptimizations = registerForActivityResult(
ActivityResultContracts.StartActivityForResult() ActivityResultContracts.StartActivityForResult()
) { _ -> ) { result ->
lifecycleScope.launch(Dispatchers.IO) { if (result.resultCode == Activity.RESULT_OK) {
reloadSettings() binding.backgroundPermissionCard.isVisible = false
} }
} }
@SuppressLint("BatteryLife")
private fun onCreate() { private fun onCreate() {
val activity = activity as MainActivity? ?: return val activity = activity as MainActivity? ?: return
val binding = binding ?: return val binding = binding ?: return
@ -92,13 +96,16 @@ class SettingsFragment : Fragment() {
binding.dontKillMyAppButton.setOnClickListener { binding.dontKillMyAppButton.setOnClickListener {
it.context.launchCustomTab("https://dontkillmyapp.com/") it.context.launchCustomTab("https://dontkillmyapp.com/")
} }
binding.requestIgnoreBatteryOptimizationsButton.setOnClickListener {
requestIgnoreBatteryOptimizations.launch( if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
Intent( binding.requestIgnoreBatteryOptimizationsButton.setOnClickListener {
android.provider.Settings.ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS, requestIgnoreBatteryOptimizations.launch(
Uri.parse("package:${Application.application.packageName}") Intent(
android.provider.Settings.ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS,
Uri.parse("package:${Application.application.packageName}")
)
) )
) }
} }
binding.configureOverridesButton.setOnClickListener { binding.configureOverridesButton.setOnClickListener {
startActivity(Intent(requireContext(), ProfileOverrideActivity::class.java)) startActivity(Intent(requireContext(), ProfileOverrideActivity::class.java))
@ -140,9 +147,4 @@ class SettingsFragment : Fragment() {
} }
} }
override fun onDestroyView() {
super.onDestroyView()
binding = null
}
} }