diff --git a/app/src/main/java/com/leanbitlab/lwidget/BatteryOptimization.kt b/app/src/main/java/com/leanbitlab/lwidget/BatteryOptimization.kt new file mode 100644 index 0000000..29041cb --- /dev/null +++ b/app/src/main/java/com/leanbitlab/lwidget/BatteryOptimization.kt @@ -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 . + */ + +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) } +} diff --git a/app/src/main/java/com/leanbitlab/lwidget/MainActivity.kt b/app/src/main/java/com/leanbitlab/lwidget/MainActivity.kt index eb008cb..8fe2ccd 100644 --- a/app/src/main/java/com/leanbitlab/lwidget/MainActivity.kt +++ b/app/src/main/java/com/leanbitlab/lwidget/MainActivity.kt @@ -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) } } diff --git a/app/src/main/java/com/leanbitlab/lwidget/SettingsDisplaySections.kt b/app/src/main/java/com/leanbitlab/lwidget/SettingsDisplaySections.kt index eacd1bd..eef1d4b 100644 --- a/app/src/main/java/com/leanbitlab/lwidget/SettingsDisplaySections.kt +++ b/app/src/main/java/com/leanbitlab/lwidget/SettingsDisplaySections.kt @@ -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 } diff --git a/app/src/main/java/com/leanbitlab/lwidget/SettingsPermissions.kt b/app/src/main/java/com/leanbitlab/lwidget/SettingsPermissions.kt index 8f49c34..8e2c630 100644 --- a/app/src/main/java/com/leanbitlab/lwidget/SettingsPermissions.kt +++ b/app/src/main/java/com/leanbitlab/lwidget/SettingsPermissions.kt @@ -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")) { @@ -232,45 +232,13 @@ internal fun MainActivity.setupBatteryOptimizationSection() { internal fun MainActivity.updateBatteryOptimizationSwitch() { val row = findViewById(R.id.row_battery_optimization_toggle) ?: return val switchOpt = row.findViewById(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) } } diff --git a/app/src/main/java/com/leanbitlab/lwidget/SettingsSystemSections.kt b/app/src/main/java/com/leanbitlab/lwidget/SettingsSystemSections.kt index 607ebd2..5463907 100644 --- a/app/src/main/java/com/leanbitlab/lwidget/SettingsSystemSections.kt +++ b/app/src/main/java/com/leanbitlab/lwidget/SettingsSystemSections.kt @@ -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()) { diff --git a/app/src/main/java/com/leanbitlab/lwidget/SetupActivity.kt b/app/src/main/java/com/leanbitlab/lwidget/SetupActivity.kt index 353d6e3..d30550c 100644 --- a/app/src/main/java/com/leanbitlab/lwidget/SetupActivity.kt +++ b/app/src/main/java/com/leanbitlab/lwidget/SetupActivity.kt @@ -119,13 +119,13 @@ class SetupActivity : AppCompatActivity() { findViewById(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(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) @@ -235,46 +235,6 @@ class SetupActivity : AppCompatActivity() { private fun updateBatteryOptimizationSwitch() { val switchOpt = findViewById(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) } } diff --git a/app/src/main/java/com/leanbitlab/lwidget/weather/BreezyWeatherFetcher.kt b/app/src/main/java/com/leanbitlab/lwidget/weather/BreezyWeatherFetcher.kt index db40ce6..fc39990 100644 --- a/app/src/main/java/com/leanbitlab/lwidget/weather/BreezyWeatherFetcher.kt +++ b/app/src/main/java/com/leanbitlab/lwidget/weather/BreezyWeatherFetcher.kt @@ -35,7 +35,7 @@ object BreezyWeatherFetcher { return try { json.decodeFromString(jsonString) } catch (e: Exception) { - e.printStackTrace() + android.util.Log.w("BreezyWeatherFetcher", "Failed to parse Breezy weather JSON", e) null } }