Skip to content

Commit b2e5f32

Browse files
fix(ui): prevent battery dialog dismissal when settings fail to open
* Show an error message if settings cannot be opened * Manual handle the positive button click to ensure the dialog stays visible if there's an error Signed-off-by: Josh <josh.t.richards@gmail.com>
1 parent df14182 commit b2e5f32

1 file changed

Lines changed: 19 additions & 3 deletions

File tree

app/src/main/java/com/owncloud/android/ui/activity/SyncedFoldersActivity.kt

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -849,22 +849,38 @@ class SyncedFoldersActivity :
849849
}
850850

851851
private fun showBatteryOptimizationDialog() {
852+
// Only show dialog if activity is in resumed state (prevents crashes)
852853
if (!lifecycle.currentState.isAtLeast(Lifecycle.State.RESUMED)) {
853854
Log_OC.w(TAG, "Activity not resumed, skipping battery dialog")
854855
return
855856
}
856857

858+
// Build the dialog with NO automatic positive button listener
857859
val dialog = MaterialAlertDialogBuilder(this, R.style.Theme_ownCloud_Dialog)
858860
.setTitle(R.string.battery_optimization_title)
859861
.setMessage(R.string.battery_optimization_message)
860-
.setPositiveButton(R.string.battery_optimization_disable) { _, _ ->
861-
BatteryOptimizationHelper.openBatteryOptimizationSettings(this)
862-
}
862+
.setPositiveButton(R.string.battery_optimization_disable, null) // Custom handler set below
863863
.setNeutralButton(R.string.battery_optimization_close, null)
864864
.setIcon(R.drawable.ic_battery_alert)
865865

866+
viewThemeUtils.dialog.colorMaterialAlertDialogBackground(this, dialog)
867+
866868
val alertDialog = dialog.show()
867869

870+
// Try to open battery optimization settings. If it fails, stay open for visibillity.
871+
alertDialog.getButton(AlertDialog.BUTTON_POSITIVE).setOnClickListener {
872+
val success = BatteryOptimizationHelper.openBatteryOptimizationSettings(this)
873+
if (success) {
874+
// We dismiss here because we successfully sent the user to Settings.
875+
// If they come back without changing settings, the dialog stays gone
876+
// until the next time showBatteryOptimizationDialogIfNeeded() runs.
877+
alertDialog.dismiss()
878+
} else {
879+
showSnackMessage(getString(R.string.unable_to_open_battery_optimization_settings))
880+
}
881+
}
882+
883+
// Consistently style the dialog's buttons
868884
viewThemeUtils.platform.colorTextButtons(
869885
alertDialog.getButton(AlertDialog.BUTTON_POSITIVE),
870886
alertDialog.getButton(AlertDialog.BUTTON_NEUTRAL)

0 commit comments

Comments
 (0)