Convert some fun to Context extensions

This commit is contained in:
Benoit Marty 2022-05-05 11:57:54 +02:00
parent 00ad9ccc2a
commit 66b32a74d5
5 changed files with 10 additions and 11 deletions

View File

@ -31,7 +31,7 @@ class TestBatteryOptimization @Inject constructor(
) : TroubleshootTest(R.string.settings_troubleshoot_test_battery_title) {
override fun perform(activityResultLauncher: ActivityResultLauncher<Intent>) {
if (isIgnoringBatteryOptimizations(context)) {
if (context.isIgnoringBatteryOptimizations()) {
description = stringProvider.getString(R.string.settings_troubleshoot_test_battery_success)
status = TestStatus.SUCCESS
quickFix = null

View File

@ -43,21 +43,20 @@ import im.vector.app.features.notifications.NotificationUtils
* This user option appears on Android M but Android O enforces its usage and kills apps not
* authorised by the user to run in background.
*
* @param context the context
* @return true if battery optimisations are ignored
*/
fun isIgnoringBatteryOptimizations(context: Context): Boolean {
fun Context.isIgnoringBatteryOptimizations(): Boolean {
// no issue before Android M, battery optimisations did not exist
return Build.VERSION.SDK_INT < Build.VERSION_CODES.M ||
context.getSystemService<PowerManager>()?.isIgnoringBatteryOptimizations(context.packageName) == true
getSystemService<PowerManager>()?.isIgnoringBatteryOptimizations(packageName) == true
}
fun isAirplaneModeOn(context: Context): Boolean {
return Settings.Global.getInt(context.contentResolver, Settings.Global.AIRPLANE_MODE_ON, 0) != 0
fun Context.isAirplaneModeOn(): Boolean {
return Settings.Global.getInt(contentResolver, Settings.Global.AIRPLANE_MODE_ON, 0) != 0
}
fun isAnimationDisabled(context: Context): Boolean {
return Settings.Global.getFloat(context.contentResolver, Settings.Global.ANIMATOR_DURATION_SCALE, 1f) == 0f
fun Context.isAnimationDisabled(): Boolean {
return Settings.Global.getFloat(contentResolver, Settings.Global.ANIMATOR_DURATION_SCALE, 1f) == 0f
}
/**

View File

@ -218,7 +218,7 @@ class PopupAlertManager @Inject constructor(
if (!alert.isLight) {
clearLightStatusBar()
}
val noAnimation = !animate || isAnimationDisabled(activity)
val noAnimation = !animate || activity.isAnimationDisabled()
alert.weakCurrentActivity = WeakReference(activity)
val alerter = Alerter.create(activity, alert.layoutRes)

View File

@ -204,7 +204,7 @@ class VectorSettingsNotificationPreferenceFragment @Inject constructor(
// Important, Battery optim white listing is needed in this mode;
// Even if using foreground service with foreground notif, it stops to work
// in doze mode for certain devices :/
if (!isIgnoringBatteryOptimizations(requireContext())) {
if (!requireContext().isIgnoringBatteryOptimizations()) {
requestDisablingBatteryOptimization(requireActivity(), batteryStartForActivityResult)
}
}

View File

@ -54,7 +54,7 @@ class SyncStateView @JvmOverloads constructor(context: Context, attrs: Attribute
views.syncStateProgressBar.isVisible = newState is SyncState.Running && newState.afterPause
if (newState == SyncState.NoNetwork) {
val isAirplaneModeOn = isAirplaneModeOn(context)
val isAirplaneModeOn = context.isAirplaneModeOn()
views.syncStateNoNetwork.isVisible = isAirplaneModeOn.not()
views.syncStateNoNetworkAirplane.isVisible = isAirplaneModeOn
} else {