mirror of
https://github.com/bitfireAT/davx5-ose
synced 2024-07-23 11:39:15 +00:00
BatteryOptimizationIntroFragment: use contract instead of onActivityResult (#444)
* Using result launcher Signed-off-by: Arnau Mora <arnyminerz@proton.me> * Minor re-ordering --------- Signed-off-by: Arnau Mora <arnyminerz@proton.me> Co-authored-by: Ricki Hirner <hirner@bitfire.at>
This commit is contained in:
parent
599c905610
commit
5ae70cb5d0
|
@ -5,6 +5,7 @@
|
|||
package at.bitfire.davdroid.ui.intro
|
||||
|
||||
import android.annotation.SuppressLint
|
||||
import android.app.Application
|
||||
import android.content.Context
|
||||
import android.content.Intent
|
||||
import android.net.Uri
|
||||
|
@ -14,12 +15,13 @@ import android.os.PowerManager
|
|||
import android.view.LayoutInflater
|
||||
import android.view.View
|
||||
import android.view.ViewGroup
|
||||
import androidx.activity.result.contract.ActivityResultContract
|
||||
import androidx.core.content.getSystemService
|
||||
import androidx.databinding.ObservableBoolean
|
||||
import androidx.fragment.app.Fragment
|
||||
import androidx.fragment.app.viewModels
|
||||
import androidx.lifecycle.AndroidViewModel
|
||||
import androidx.lifecycle.MutableLiveData
|
||||
import androidx.lifecycle.ViewModel
|
||||
import at.bitfire.davdroid.App
|
||||
import at.bitfire.davdroid.BuildConfig
|
||||
import at.bitfire.davdroid.R
|
||||
|
@ -34,7 +36,6 @@ import dagger.hilt.InstallIn
|
|||
import dagger.hilt.android.AndroidEntryPoint
|
||||
import dagger.hilt.android.components.ActivityComponent
|
||||
import dagger.hilt.android.lifecycle.HiltViewModel
|
||||
import dagger.hilt.android.qualifiers.ApplicationContext
|
||||
import dagger.multibindings.IntoSet
|
||||
import org.apache.commons.text.WordUtils
|
||||
import java.util.*
|
||||
|
@ -43,12 +44,13 @@ import javax.inject.Inject
|
|||
@AndroidEntryPoint
|
||||
class BatteryOptimizationsFragment: Fragment() {
|
||||
|
||||
companion object {
|
||||
const val REQUEST_IGNORE_BATTERY_OPTIMIZATIONS = 0
|
||||
}
|
||||
|
||||
val model by viewModels<Model>()
|
||||
|
||||
private val ignoreBatteryOptimizationsResultLauncher =
|
||||
registerForActivityResult(IgnoreBatteryOptimizationsContract) {
|
||||
model.checkWhitelisted()
|
||||
}
|
||||
|
||||
|
||||
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View {
|
||||
val binding = IntroBatteryOptimizationsBinding.inflate(inflater, container, false)
|
||||
|
@ -58,10 +60,7 @@ class BatteryOptimizationsFragment: Fragment() {
|
|||
model.shouldBeWhitelisted.observe(viewLifecycleOwner) { shouldBeWhitelisted ->
|
||||
@SuppressLint("BatteryLife")
|
||||
if (shouldBeWhitelisted && !model.isWhitelisted.value!!)
|
||||
startActivityForResult(Intent(
|
||||
android.provider.Settings.ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS,
|
||||
Uri.parse("package:" + BuildConfig.APPLICATION_ID)
|
||||
), REQUEST_IGNORE_BATTERY_OPTIMIZATIONS)
|
||||
ignoreBatteryOptimizationsResultLauncher.launch(BuildConfig.APPLICATION_ID)
|
||||
}
|
||||
binding.batteryText.text = getString(R.string.intro_battery_text, getString(R.string.app_name))
|
||||
|
||||
|
@ -78,12 +77,6 @@ class BatteryOptimizationsFragment: Fragment() {
|
|||
return binding.root
|
||||
}
|
||||
|
||||
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
|
||||
super.onActivityResult(requestCode, resultCode, data)
|
||||
if (requestCode == REQUEST_IGNORE_BATTERY_OPTIMIZATIONS)
|
||||
model.checkWhitelisted()
|
||||
}
|
||||
|
||||
override fun onResume() {
|
||||
super.onResume()
|
||||
model.checkWhitelisted()
|
||||
|
@ -92,9 +85,9 @@ class BatteryOptimizationsFragment: Fragment() {
|
|||
|
||||
@HiltViewModel
|
||||
class Model @Inject constructor(
|
||||
@ApplicationContext val context: Context,
|
||||
application: Application,
|
||||
val settings: SettingsManager
|
||||
): ViewModel() {
|
||||
): AndroidViewModel(application) {
|
||||
|
||||
companion object {
|
||||
|
||||
|
@ -163,7 +156,7 @@ class BatteryOptimizationsFragment: Fragment() {
|
|||
}
|
||||
|
||||
fun checkWhitelisted() {
|
||||
val whitelisted = isWhitelisted(context)
|
||||
val whitelisted = isWhitelisted(getApplication())
|
||||
isWhitelisted.value = whitelisted
|
||||
shouldBeWhitelisted.value = whitelisted
|
||||
|
||||
|
@ -175,6 +168,21 @@ class BatteryOptimizationsFragment: Fragment() {
|
|||
}
|
||||
|
||||
|
||||
@SuppressLint("BatteryLife")
|
||||
object IgnoreBatteryOptimizationsContract: ActivityResultContract<String, Unit?>() {
|
||||
override fun createIntent(context: Context, input: String): Intent {
|
||||
return Intent(
|
||||
android.provider.Settings.ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS,
|
||||
Uri.parse("package:$input")
|
||||
)
|
||||
}
|
||||
|
||||
override fun parseResult(resultCode: Int, intent: Intent?): Unit? {
|
||||
return null
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@Module
|
||||
@InstallIn(ActivityComponent::class)
|
||||
abstract class BatteryOptimizationsFragmentModule {
|
||||
|
@ -187,17 +195,17 @@ class BatteryOptimizationsFragment: Fragment() {
|
|||
): IntroFragmentFactory {
|
||||
|
||||
override fun getOrder(context: Context) =
|
||||
// show fragment when:
|
||||
// 1. DAVx5 is not whitelisted yet and "don't show anymore" has not been clicked, and/or
|
||||
// 2a. evil manufacturer AND
|
||||
// 2b. "don't show anymore" has not been clicked
|
||||
if (
|
||||
(!Model.isWhitelisted(context) && settingsManager.getBooleanOrNull(HINT_BATTERY_OPTIMIZATIONS) != false) ||
|
||||
(Model.manufacturerWarning && settingsManager.getBooleanOrNull(HINT_AUTOSTART_PERMISSION) != false)
|
||||
)
|
||||
100
|
||||
else
|
||||
IntroFragmentFactory.DONT_SHOW
|
||||
// show fragment when:
|
||||
// 1. DAVx5 is not whitelisted yet and "don't show anymore" has not been clicked, and/or
|
||||
// 2a. evil manufacturer AND
|
||||
// 2b. "don't show anymore" has not been clicked
|
||||
if (
|
||||
(!Model.isWhitelisted(context) && settingsManager.getBooleanOrNull(HINT_BATTERY_OPTIMIZATIONS) != false) ||
|
||||
(Model.manufacturerWarning && settingsManager.getBooleanOrNull(HINT_AUTOSTART_PERMISSION) != false)
|
||||
)
|
||||
100
|
||||
else
|
||||
IntroFragmentFactory.DONT_SHOW
|
||||
|
||||
override fun create() = BatteryOptimizationsFragment()
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue