Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 66 additions & 0 deletions app/src/main/java/com/leanbitlab/lwidget/BatteryOptimization.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/*
* Copyright (C) 2026 LeanBitLab
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/

package com.leanbitlab.lwidget

import android.content.Context
import android.content.Intent
import android.net.Uri
import android.os.PowerManager
import android.provider.Settings
import android.util.Log
import android.widget.CompoundButton

private const val TAG = "BatteryOptimization"

internal fun Context.isIgnoringBatteryOptimizations(): Boolean =
(getSystemService(Context.POWER_SERVICE) as PowerManager).isIgnoringBatteryOptimizations(packageName)

/**
* Handles a user toggle of the battery optimization switch. Turning it on asks the system to
* exempt the app (falling back to the settings list); turning it off can only open the settings list.
*/
internal fun Context.onBatteryOptimizationToggled(isChecked: Boolean) {
val isCurrentlyIgnoring = isIgnoringBatteryOptimizations()
if (isChecked && !isCurrentlyIgnoring) {
try {
startActivity(Intent(Settings.ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS).apply {
data = Uri.parse("package:$packageName")
})
} catch (e: Exception) {
Log.w(TAG, "Direct exemption request failed, opening settings list", e)
openBatteryOptimizationSettings()
}
} else if (!isChecked && isCurrentlyIgnoring) {
openBatteryOptimizationSettings()
}
}

private fun Context.openBatteryOptimizationSettings() {
try {
startActivity(Intent(Settings.ACTION_IGNORE_BATTERY_OPTIMIZATION_SETTINGS))
} catch (e: Exception) {
Log.w(TAG, "Failed to open battery optimization settings", e)
}
}

/** Syncs the switch to the current exemption state, then routes user toggles to [onBatteryOptimizationToggled]. */
internal fun Context.bindBatteryOptimizationSwitch(switchOpt: CompoundButton) {
switchOpt.setOnCheckedChangeListener(null)
switchOpt.isChecked = isIgnoringBatteryOptimizations()
switchOpt.setOnCheckedChangeListener { _, isChecked -> onBatteryOptimizationToggled(isChecked) }
}
2 changes: 1 addition & 1 deletion app/src/main/java/com/leanbitlab/lwidget/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -318,7 +318,7 @@ class MainActivity : AppCompatActivity() {
previewContainer.removeAllViews()
previewContainer.addView(view)
} catch (e: Exception) {
e.printStackTrace()
Log.w("MainActivity", "Failed to render live preview", e)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ internal fun MainActivity.setupWeatherSection() {
).setAction(getString(R.string.action_install)) {
try {
CustomTabsIntent.Builder().build().launchUrl(this@setupWeatherSection, android.net.Uri.parse("https://github.com/breezy-weather/breezy-weather/releases"))
} catch (e: Exception) { android.util.Log.e("MainActivity", "Exception caught", e) }
} catch (e: Exception) { android.util.Log.w("MainActivity", "Failed to open BreezyWeather releases page", e) }
}.show()
return@setOnCheckedChangeListener
}
Expand Down
52 changes: 10 additions & 42 deletions app/src/main/java/com/leanbitlab/lwidget/SettingsPermissions.kt
Original file line number Diff line number Diff line change
Expand Up @@ -116,10 +116,10 @@ internal fun MainActivity.checkAllPermissions() {
} else openAppSettings()
}
updatePermissionToggle(R.id.row_perm_data_usage, getString(R.string.section_data_usage), !usageMissing) {
try { startActivity(Intent(android.provider.Settings.ACTION_USAGE_ACCESS_SETTINGS)) } catch (e: Exception) { Log.w("MainActivity", "Failed to launch usage access settings", e) }
openUsageAccessSettings()
}
updatePermissionToggle(R.id.row_perm_screen_time, getString(R.string.section_screen_time), !usageMissing) {
try { startActivity(Intent(android.provider.Settings.ACTION_USAGE_ACCESS_SETTINGS)) } catch (e: Exception) { Log.w("MainActivity", "Failed to launch usage access settings", e) }
openUsageAccessSettings()
}
updatePermissionToggle(R.id.row_perm_weather, getString(R.string.section_weather_condition), !weatherMissing) {
if (weatherMissing && !isAppInstalled("org.breezyweather")) {
Expand Down Expand Up @@ -232,45 +232,13 @@ internal fun MainActivity.setupBatteryOptimizationSection() {
internal fun MainActivity.updateBatteryOptimizationSwitch() {
val row = findViewById<View>(R.id.row_battery_optimization_toggle) ?: return
val switchOpt = row.findViewById<SwitchMaterial>(R.id.row_switch) ?: return
val powerManager = getSystemService(Context.POWER_SERVICE) as android.os.PowerManager
val isIgnoring = if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.M) {
powerManager.isIgnoringBatteryOptimizations(packageName)
} else true

switchOpt.setOnCheckedChangeListener(null)
switchOpt.isChecked = isIgnoring
switchOpt.setOnCheckedChangeListener { _, isChecked ->
if (isChecked) {
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.M) {
val isCurrentlyIgnoring = powerManager.isIgnoringBatteryOptimizations(packageName)
if (!isCurrentlyIgnoring) {
try {
val intent = Intent(android.provider.Settings.ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS).apply {
data = android.net.Uri.parse("package:$packageName")
}
startActivity(intent)
} catch (e: Exception) {
try {
val intent = Intent(android.provider.Settings.ACTION_IGNORE_BATTERY_OPTIMIZATION_SETTINGS)
startActivity(intent)
} catch (ex: Exception) {
ex.printStackTrace()
}
}
}
}
} else {
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.M) {
val isCurrentlyIgnoring = powerManager.isIgnoringBatteryOptimizations(packageName)
if (isCurrentlyIgnoring) {
try {
val intent = Intent(android.provider.Settings.ACTION_IGNORE_BATTERY_OPTIMIZATION_SETTINGS)
startActivity(intent)
} catch (e: Exception) {
e.printStackTrace()
}
}
}
}
bindBatteryOptimizationSwitch(switchOpt)
}

private fun MainActivity.openUsageAccessSettings() {
try {
startActivity(Intent(android.provider.Settings.ACTION_USAGE_ACCESS_SETTINGS))
} catch (e: Exception) {
Log.w("MainActivity", "Failed to launch usage access settings", e)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ internal fun MainActivity.setupDataUsageSection() {
getString(R.string.perm_usage_access_title),
com.google.android.material.snackbar.Snackbar.LENGTH_LONG
).show()
} catch (e: Exception) { android.util.Log.e("MainActivity", "Exception caught", e) }
} catch (e: Exception) { android.util.Log.w("MainActivity", "Failed to show usage access snackbar", e) }
return@setOnCheckedChangeListener
}
if (!checkLimit()) {
Expand Down
46 changes: 3 additions & 43 deletions app/src/main/java/com/leanbitlab/lwidget/SetupActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -119,13 +119,13 @@ class SetupActivity : AppCompatActivity() {
findViewById<MaterialButton>(R.id.btn_grant_data_usage).setOnClickListener {
try {
startActivity(Intent(android.provider.Settings.ACTION_USAGE_ACCESS_SETTINGS))
} catch (e: Exception) { android.util.Log.e("SetupActivity", "Exception caught", e) }
} catch (e: Exception) { android.util.Log.w("SetupActivity", "Failed to open usage access settings", e) }
}

findViewById<MaterialButton>(R.id.btn_grant_screen_time).setOnClickListener {
try {
startActivity(Intent(android.provider.Settings.ACTION_USAGE_ACCESS_SETTINGS))
} catch (e: Exception) { android.util.Log.e("SetupActivity", "Exception caught", e) }
} catch (e: Exception) { android.util.Log.w("SetupActivity", "Failed to open usage access settings", e) }
}

// Weather (Breezy Weather provider)
Expand Down Expand Up @@ -235,46 +235,6 @@ class SetupActivity : AppCompatActivity() {

private fun updateBatteryOptimizationSwitch() {
val switchOpt = findViewById<SwitchMaterial>(R.id.switch_setup_battery_optimization) ?: return
val powerManager = getSystemService(Context.POWER_SERVICE) as android.os.PowerManager
val isIgnoring = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
powerManager.isIgnoringBatteryOptimizations(packageName)
} else true

switchOpt.setOnCheckedChangeListener(null)
switchOpt.isChecked = isIgnoring
switchOpt.setOnCheckedChangeListener { _, isChecked ->
if (isChecked) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
val isCurrentlyIgnoring = powerManager.isIgnoringBatteryOptimizations(packageName)
if (!isCurrentlyIgnoring) {
try {
val intent = Intent(android.provider.Settings.ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS).apply {
data = android.net.Uri.parse("package:$packageName")
}
startActivity(intent)
} catch (e: Exception) {
try {
val intent = Intent(android.provider.Settings.ACTION_IGNORE_BATTERY_OPTIMIZATION_SETTINGS)
startActivity(intent)
} catch (ex: Exception) {
ex.printStackTrace()
}
}
}
}
} else {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
val isCurrentlyIgnoring = powerManager.isIgnoringBatteryOptimizations(packageName)
if (isCurrentlyIgnoring) {
try {
val intent = Intent(android.provider.Settings.ACTION_IGNORE_BATTERY_OPTIMIZATION_SETTINGS)
startActivity(intent)
} catch (e: Exception) {
e.printStackTrace()
}
}
}
}
}
bindBatteryOptimizationSwitch(switchOpt)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ object BreezyWeatherFetcher {
return try {
json.decodeFromString<BreezyGadgetbridgeData>(jsonString)
} catch (e: Exception) {
e.printStackTrace()
android.util.Log.w("BreezyWeatherFetcher", "Failed to parse Breezy weather JSON", e)
null
}
}
Expand Down
Loading