-
Notifications
You must be signed in to change notification settings - Fork 455
Add a banner to ask the user to disable battery optimization when Event cannot be resolved from Push #4845
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add a banner to ask the user to disable battery optimization when Event cannot be resolved from Push #4845
Changes from 3 commits
7deed4c
127fa4f
9956ed6
6fbde3d
4b5eb8e
1197241
a9ca08f
d5dfced
cce2f33
751f7e7
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,45 @@ | ||
| /* | ||
| * Copyright 2025 New Vector Ltd. | ||
| * | ||
| * SPDX-License-Identifier: AGPL-3.0-only OR LicenseRef-Element-Commercial | ||
| * Please see LICENSE files in the repository root for full details. | ||
| */ | ||
|
|
||
| package io.element.android.features.roomlist.impl.components | ||
|
|
||
| import androidx.activity.compose.LocalActivity | ||
| import androidx.compose.runtime.Composable | ||
| import androidx.compose.ui.Modifier | ||
| import io.element.android.libraries.designsystem.components.Announcement | ||
| import io.element.android.libraries.designsystem.components.AnnouncementType | ||
| import io.element.android.libraries.designsystem.preview.ElementPreview | ||
| import io.element.android.libraries.designsystem.preview.PreviewsDayNight | ||
| import io.element.android.libraries.push.api.battery.BatteryOptimizationState | ||
| import io.element.android.libraries.push.api.battery.aBatteryOptimizationState | ||
|
|
||
| @Composable | ||
| internal fun BatteryOptimizationBanner( | ||
| state: BatteryOptimizationState, | ||
| modifier: Modifier = Modifier, | ||
| ) { | ||
| val activity = LocalActivity.current | ||
| Announcement( | ||
| modifier = modifier.roomListBannerPadding(), | ||
| // TODO Localazy | ||
| title = "Notification tip", | ||
| description = "To be sure to receive all the notifications, it can help to disable the battery optimization for this application.", | ||
| type = AnnouncementType.Actionable( | ||
| actionText = "Yes, disable", | ||
| onActionClick = { state.openSettings(activity) }, | ||
| onDismissClick = state.dismiss, | ||
| ), | ||
| ) | ||
| } | ||
|
|
||
| @PreviewsDayNight | ||
| @Composable | ||
| internal fun BatteryOptimizationBannerPreview() = ElementPreview { | ||
| BatteryOptimizationBanner( | ||
| state = aBatteryOptimizationState(), | ||
| ) | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| /* | ||
| * Copyright 2025 New Vector Ltd. | ||
| * | ||
| * SPDX-License-Identifier: AGPL-3.0-only OR LicenseRef-Element-Commercial | ||
| * Please see LICENSE files in the repository root for full details. | ||
| */ | ||
|
|
||
| package io.element.android.libraries.push.api.battery | ||
|
|
||
| import android.app.Activity | ||
|
|
||
| data class BatteryOptimizationState( | ||
| val shouldDisplayBanner: Boolean, | ||
| val dismiss: () -> Unit, | ||
| val openSettings: (Activity?) -> Unit, | ||
| ) | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| /* | ||
| * Copyright 2025 New Vector Ltd. | ||
| * | ||
| * SPDX-License-Identifier: AGPL-3.0-only OR LicenseRef-Element-Commercial | ||
| * Please see LICENSE files in the repository root for full details. | ||
| */ | ||
|
|
||
| package io.element.android.libraries.push.api.battery | ||
|
|
||
| import android.app.Activity | ||
|
|
||
| fun aBatteryOptimizationState( | ||
| shouldDisplayBanner: Boolean = false, | ||
| dismiss: () -> Unit = {}, | ||
| openSettings: (Activity?) -> Unit = {}, | ||
| ) = BatteryOptimizationState( | ||
| shouldDisplayBanner = shouldDisplayBanner, | ||
| dismiss = dismiss, | ||
| openSettings = openSettings, | ||
| ) |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,72 @@ | ||
| /* | ||
| * Copyright 2025 New Vector Ltd. | ||
| * | ||
| * SPDX-License-Identifier: AGPL-3.0-only OR LicenseRef-Element-Commercial | ||
| * Please see LICENSE files in the repository root for full details. | ||
| */ | ||
|
|
||
| package io.element.android.libraries.push.impl.battery | ||
|
|
||
| import android.annotation.SuppressLint | ||
| import android.app.Activity | ||
| import android.content.ActivityNotFoundException | ||
| import android.content.Context | ||
| import android.content.Intent | ||
| import android.os.PowerManager | ||
| import android.provider.Settings | ||
| import androidx.core.content.getSystemService | ||
| import androidx.core.net.toUri | ||
| import com.squareup.anvil.annotations.ContributesBinding | ||
| import io.element.android.libraries.di.AppScope | ||
| import io.element.android.libraries.di.ApplicationContext | ||
| import timber.log.Timber | ||
| import javax.inject.Inject | ||
|
|
||
| interface BatteryOptimization { | ||
| /** | ||
| * Tells if the application ignores battery optimizations. | ||
| * | ||
| * Ignoring them allows the app to run in background to make background sync with the homeserver. | ||
| * 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. | ||
| * | ||
| * @return true if battery optimisations are ignored | ||
| */ | ||
| fun isIgnoringBatteryOptimizations(): Boolean | ||
bmarty marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| /** | ||
| * Request the user to disable battery optimizations for this app. | ||
| * This will open the system settings where the user can disable battery optimizations. | ||
| * See https://developer.android.com/training/monitoring-device-state/doze-standby#exemption-cases | ||
| * | ||
| * @param activity The activity from which to start the settings intent. | ||
| * @return true if the intent was successfully started, false if the activity was not found | ||
| */ | ||
| fun requestDisablingBatteryOptimization(activity: Activity?): Boolean | ||
| } | ||
|
|
||
| @ContributesBinding(AppScope::class) | ||
| class AndroidBatteryOptimization @Inject constructor( | ||
| @ApplicationContext | ||
|
Check warning on line 50 in libraries/push/impl/src/main/kotlin/io/element/android/libraries/push/impl/battery/BatteryOptimization.kt
|
||
| private val context: Context, | ||
| ) : BatteryOptimization { | ||
| override fun isIgnoringBatteryOptimizations(): Boolean { | ||
| return context.getSystemService<PowerManager>() | ||
| ?.isIgnoringBatteryOptimizations(context.packageName) == true | ||
| } | ||
|
|
||
| @SuppressLint("BatteryLife") | ||
| override fun requestDisablingBatteryOptimization(activity: Activity?): Boolean { | ||
| activity ?: return false | ||
| val intent = Intent() | ||
| intent.action = Settings.ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS | ||
| intent.data = ("package:" + context.packageName).toUri() | ||
|
Check warning on line 63 in libraries/push/impl/src/main/kotlin/io/element/android/libraries/push/impl/battery/BatteryOptimization.kt
|
||
| return try { | ||
| activity.startActivity(intent) | ||
| true | ||
|
Check warning on line 66 in libraries/push/impl/src/main/kotlin/io/element/android/libraries/push/impl/battery/BatteryOptimization.kt
|
||
| } catch (exception: ActivityNotFoundException) { | ||
| Timber.w(exception, "Cannot request ignoring battery optimizations.") | ||
| false | ||
|
Check warning on line 69 in libraries/push/impl/src/main/kotlin/io/element/android/libraries/push/impl/battery/BatteryOptimization.kt
|
||
| } | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,66 @@ | ||
| /* | ||
| * Copyright 2025 New Vector Ltd. | ||
| * | ||
| * SPDX-License-Identifier: AGPL-3.0-only OR LicenseRef-Element-Commercial | ||
| * Please see LICENSE files in the repository root for full details. | ||
| */ | ||
|
|
||
| package io.element.android.libraries.push.impl.battery | ||
|
|
||
| import androidx.compose.runtime.Composable | ||
| import androidx.compose.runtime.collectAsState | ||
| import androidx.compose.runtime.getValue | ||
| import androidx.compose.runtime.mutableStateOf | ||
| import androidx.compose.runtime.remember | ||
| import androidx.compose.runtime.rememberCoroutineScope | ||
| import androidx.compose.runtime.setValue | ||
| import androidx.lifecycle.compose.LifecycleResumeEffect | ||
| import io.element.android.libraries.architecture.Presenter | ||
| import io.element.android.libraries.push.api.battery.BatteryOptimizationState | ||
| import io.element.android.libraries.push.impl.push.MutableBatteryOptimizationStore | ||
| import io.element.android.libraries.push.impl.store.PushDataStore | ||
| import kotlinx.coroutines.launch | ||
| import javax.inject.Inject | ||
|
|
||
| class BatteryOptimizationPresenter @Inject constructor( | ||
| private val pushDataStore: PushDataStore, | ||
| private val mutableBatteryOptimizationStore: MutableBatteryOptimizationStore, | ||
| private val batteryOptimization: BatteryOptimization, | ||
| ) : Presenter<BatteryOptimizationState> { | ||
| @Composable | ||
| override fun present(): BatteryOptimizationState { | ||
| val coroutineScope = rememberCoroutineScope() | ||
| var isRequestSent by remember { mutableStateOf(false) } | ||
| var localShouldDisplayBanner by remember { mutableStateOf(true) } | ||
| val storeShouldDisplayBanner by pushDataStore.shouldDisplayBatteryOptimizationBannerFlow.collectAsState(initial = false) | ||
| var isSystemIgnoringBatteryOptimizations by remember { | ||
| mutableStateOf(batteryOptimization.isIgnoringBatteryOptimizations()) | ||
| } | ||
|
|
||
| LifecycleResumeEffect(Unit) { | ||
| isSystemIgnoringBatteryOptimizations = batteryOptimization.isIgnoringBatteryOptimizations() | ||
| if (isRequestSent) { | ||
| localShouldDisplayBanner = false | ||
| } | ||
| onPauseOrDispose {} | ||
| } | ||
|
|
||
| return BatteryOptimizationState( | ||
| shouldDisplayBanner = localShouldDisplayBanner && storeShouldDisplayBanner && !isSystemIgnoringBatteryOptimizations, | ||
bmarty marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| dismiss = { | ||
| coroutineScope.launch { | ||
| mutableBatteryOptimizationStore.onOptimizationBannerDismissed() | ||
| } | ||
| }, | ||
| openSettings = { activity -> | ||
| isRequestSent = true | ||
| if (batteryOptimization.requestDisablingBatteryOptimization(activity).not()) { | ||
| // If not able to perform the request, ensure that we do not display the banner again | ||
| coroutineScope.launch { | ||
| mutableBatteryOptimizationStore.onOptimizationBannerDismissed() | ||
| } | ||
| } | ||
| } | ||
| ) | ||
| } | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.