-
Notifications
You must be signed in to change notification settings - Fork 457
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
Merged
Merged
Add a banner to ask the user to disable battery optimization when Event cannot be resolved from Push #4845
Changes from 1 commit
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
7deed4c
Add a banner to ask the user to disable battery optimization when Eve…
bmarty 127fa4f
Update screenshots
ElementBot 9956ed6
Use defined const instead of magic numbers.
bmarty 6fbde3d
Avoid using the Activity, and use eventSink instead of lambda in states.
bmarty 4b5eb8e
Use eventSink instead of lambda in states.
bmarty 1197241
Add fallback to ACTION_IGNORE_BATTERY_OPTIMIZATION_SETTINGS if ACTION…
bmarty a9ca08f
Add unit tests on AndroidBatteryOptimization
bmarty d5dfced
Battery optimization banner: update wording.
bmarty cce2f33
Update screenshots
ElementBot 751f7e7
Rename `DoAction` to more specific `RequestDisableOptimizations`
bmarty File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
45 changes: 45 additions & 0 deletions
45
.../kotlin/io/element/android/features/roomlist/impl/components/BatteryOptimizationBanner.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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(), | ||
| ) | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
16 changes: 16 additions & 0 deletions
16
...src/main/kotlin/io/element/android/libraries/push/api/battery/BatteryOptimizationState.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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, | ||
| ) | ||
20 changes: 20 additions & 0 deletions
20
.../kotlin/io/element/android/libraries/push/api/battery/BatteryOptimizationStateProvider.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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, | ||
| ) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
72 changes: 72 additions & 0 deletions
72
...mpl/src/main/kotlin/io/element/android/libraries/push/impl/battery/BatteryOptimization.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 | ||
| 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() | ||
| return try { | ||
| activity.startActivity(intent) | ||
| true | ||
| } catch (exception: ActivityNotFoundException) { | ||
| Timber.w(exception, "Cannot request ignoring battery optimizations.") | ||
| false | ||
| } | ||
| } | ||
| } | ||
66 changes: 66 additions & 0 deletions
66
...ain/kotlin/io/element/android/libraries/push/impl/battery/BatteryOptimizationPresenter.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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() | ||
| } | ||
| } | ||
| } | ||
| ) | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.