Skip to content

Commit d7e0cc0

Browse files
author
LeanBitLab
committed
feat: add battery settings and bump version
1 parent ca4f111 commit d7e0cc0

6 files changed

Lines changed: 158 additions & 2 deletions

File tree

‎app/build.gradle.kts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ android {
1515
applicationId = "com.leanbitlab.lwidget"
1616
minSdk = 26
1717
targetSdk = 35
18-
versionCode = 17
19-
versionName = "2.2"
18+
versionCode = 18
19+
versionName = "2.3"
2020
}
2121

2222
testOptions {

‎app/src/main/AndroidManifest.xml‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
1212
<uses-permission android:name="android.permission.FOREGROUND_SERVICE_HEALTH" />
1313
<uses-permission android:name="android.permission.POST_NOTIFICATIONS" />
14+
<uses-permission android:name="android.permission.REQUEST_IGNORE_BATTERY_OPTIMIZATIONS" />
1415

1516
<!-- Breezy Weather Local Data Sharing Permission -->
1617
<uses-permission android:name="org.breezyweather.READ_PROVIDER" />

‎app/src/main/java/com/leanbitlab/lwidget/MainActivity.kt‎

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -321,6 +321,8 @@ class MainActivity : AppCompatActivity() {
321321
checkAllPermissions()
322322
// Force a full widget update every time the app is opened
323323
updateWidget()
324+
// Update battery optimization switch state
325+
updateBatteryOptimizationSwitch()
324326
}
325327

326328
override fun onRequestPermissionsResult(requestCode: Int, permissions: Array<out String>, grantResults: IntArray) {
@@ -860,6 +862,7 @@ class MainActivity : AppCompatActivity() {
860862
setupStepsSection()
861863
setupScreenTimeSection()
862864
setupKeepAliveSection()
865+
setupBatteryOptimizationSection()
863866
setupEventsAndTasksSections()
864867
setupThemeSection(colorOptions)
865868

@@ -1244,6 +1247,57 @@ class MainActivity : AppCompatActivity() {
12441247
}
12451248
}
12461249
}
1250+
private fun setupBatteryOptimizationSection() {
1251+
val row = findViewById<View>(R.id.row_battery_optimization_toggle)
1252+
val tvTitle = row.findViewById<TextView>(R.id.row_label)
1253+
tvTitle.text = "Disable Battery Optimization"
1254+
updateBatteryOptimizationSwitch()
1255+
}
1256+
private fun updateBatteryOptimizationSwitch() {
1257+
val row = findViewById<View>(R.id.row_battery_optimization_toggle) ?: return
1258+
val switchOpt = row.findViewById<SwitchMaterial>(R.id.row_switch) ?: return
1259+
val powerManager = getSystemService(Context.POWER_SERVICE) as android.os.PowerManager
1260+
val isIgnoring = if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.M) {
1261+
powerManager.isIgnoringBatteryOptimizations(packageName)
1262+
} else true
1263+
1264+
switchOpt.setOnCheckedChangeListener(null)
1265+
switchOpt.isChecked = isIgnoring
1266+
switchOpt.setOnCheckedChangeListener { _, isChecked ->
1267+
if (isChecked) {
1268+
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.M) {
1269+
val isCurrentlyIgnoring = powerManager.isIgnoringBatteryOptimizations(packageName)
1270+
if (!isCurrentlyIgnoring) {
1271+
try {
1272+
val intent = Intent(android.provider.Settings.ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS).apply {
1273+
data = android.net.Uri.parse("package:$packageName")
1274+
}
1275+
startActivity(intent)
1276+
} catch (e: Exception) {
1277+
try {
1278+
val intent = Intent(android.provider.Settings.ACTION_IGNORE_BATTERY_OPTIMIZATION_SETTINGS)
1279+
startActivity(intent)
1280+
} catch (ex: Exception) {
1281+
ex.printStackTrace()
1282+
}
1283+
}
1284+
}
1285+
}
1286+
} else {
1287+
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.M) {
1288+
val isCurrentlyIgnoring = powerManager.isIgnoringBatteryOptimizations(packageName)
1289+
if (isCurrentlyIgnoring) {
1290+
try {
1291+
val intent = Intent(android.provider.Settings.ACTION_IGNORE_BATTERY_OPTIMIZATION_SETTINGS)
1292+
startActivity(intent)
1293+
} catch (e: Exception) {
1294+
e.printStackTrace()
1295+
}
1296+
}
1297+
}
1298+
}
1299+
}
1300+
}
12471301
private fun setupEventsAndTasksSections() {
12481302
// Events
12491303
val eventsSwitch = bindFoldedSection(

‎app/src/main/java/com/leanbitlab/lwidget/SetupActivity.kt‎

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,11 +131,13 @@ class SetupActivity : AppCompatActivity() {
131131

132132
updateButtons()
133133
updatePermissionButtons()
134+
updateBatteryOptimizationSwitch()
134135
}
135136

136137
override fun onResume() {
137138
super.onResume()
138139
updatePermissionButtons()
140+
updateBatteryOptimizationSwitch()
139141
}
140142

141143
private fun updateButtons() {
@@ -230,4 +232,49 @@ class SetupActivity : AppCompatActivity() {
230232
startActivity(Intent(this, MainActivity::class.java))
231233
finish()
232234
}
235+
236+
private fun updateBatteryOptimizationSwitch() {
237+
val switchOpt = findViewById<SwitchMaterial>(R.id.switch_setup_battery_optimization) ?: return
238+
val powerManager = getSystemService(Context.POWER_SERVICE) as android.os.PowerManager
239+
val isIgnoring = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
240+
powerManager.isIgnoringBatteryOptimizations(packageName)
241+
} else true
242+
243+
switchOpt.setOnCheckedChangeListener(null)
244+
switchOpt.isChecked = isIgnoring
245+
switchOpt.setOnCheckedChangeListener { _, isChecked ->
246+
if (isChecked) {
247+
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
248+
val isCurrentlyIgnoring = powerManager.isIgnoringBatteryOptimizations(packageName)
249+
if (!isCurrentlyIgnoring) {
250+
try {
251+
val intent = Intent(android.provider.Settings.ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS).apply {
252+
data = android.net.Uri.parse("package:$packageName")
253+
}
254+
startActivity(intent)
255+
} catch (e: Exception) {
256+
try {
257+
val intent = Intent(android.provider.Settings.ACTION_IGNORE_BATTERY_OPTIMIZATION_SETTINGS)
258+
startActivity(intent)
259+
} catch (ex: Exception) {
260+
ex.printStackTrace()
261+
}
262+
}
263+
}
264+
}
265+
} else {
266+
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
267+
val isCurrentlyIgnoring = powerManager.isIgnoringBatteryOptimizations(packageName)
268+
if (isCurrentlyIgnoring) {
269+
try {
270+
val intent = Intent(android.provider.Settings.ACTION_IGNORE_BATTERY_OPTIMIZATION_SETTINGS)
271+
startActivity(intent)
272+
} catch (e: Exception) {
273+
e.printStackTrace()
274+
}
275+
}
276+
}
277+
}
278+
}
279+
}
233280
}

‎app/src/main/res/layout/activity_main.xml‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1502,6 +1502,21 @@
15021502
android:layout_width="match_parent"
15031503
android:layout_height="wrap_content"/>
15041504

1505+
<include layout="@layout/settings_toggle_row"
1506+
android:id="@+id/row_battery_optimization_toggle"
1507+
android:layout_width="match_parent"
1508+
android:layout_height="wrap_content"/>
1509+
1510+
<TextView
1511+
android:layout_width="match_parent"
1512+
android:layout_height="wrap_content"
1513+
android:text="Note: Highly recommended to disable battery optimization so the widget updates and touch actions work reliably after device restart."
1514+
android:textSize="12sp"
1515+
android:textColor="?attr/colorOnSurfaceVariant"
1516+
android:paddingStart="16dp"
1517+
android:paddingEnd="16dp"
1518+
android:layout_marginBottom="8dp"/>
1519+
15051520
</LinearLayout>
15061521
</LinearLayout>
15071522
</com.google.android.material.card.MaterialCardView>

‎app/src/main/res/layout/activity_setup.xml‎

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,45 @@
139139
android:layout_height="wrap_content"/>
140140
</LinearLayout>
141141
</com.google.android.material.card.MaterialCardView>
142+
143+
<com.google.android.material.card.MaterialCardView
144+
style="@style/Widget.Material3.CardView.Outlined"
145+
android:layout_width="match_parent"
146+
android:layout_height="wrap_content"
147+
android:layout_marginTop="16dp"
148+
app:cardBackgroundColor="?attr/colorSurfaceContainer">
149+
<LinearLayout
150+
android:layout_width="match_parent"
151+
android:layout_height="wrap_content"
152+
android:orientation="horizontal"
153+
android:padding="16dp"
154+
android:gravity="center_vertical">
155+
<LinearLayout
156+
android:layout_width="0dp"
157+
android:layout_height="wrap_content"
158+
android:layout_weight="1"
159+
android:orientation="vertical">
160+
<TextView
161+
android:layout_width="wrap_content"
162+
android:layout_height="wrap_content"
163+
android:text="Disable Battery Optimization"
164+
android:textSize="16sp"
165+
android:textColor="?attr/colorOnSurface"
166+
android:textStyle="bold"/>
167+
<TextView
168+
android:layout_width="wrap_content"
169+
android:layout_height="wrap_content"
170+
android:text="Prevent Android from pausing the widget updates and touch actions"
171+
android:textSize="12sp"
172+
android:textColor="?attr/colorOnSurfaceVariant"
173+
android:layout_marginTop="4dp"/>
174+
</LinearLayout>
175+
<com.google.android.material.switchmaterial.SwitchMaterial
176+
android:id="@+id/switch_setup_battery_optimization"
177+
android:layout_width="wrap_content"
178+
android:layout_height="wrap_content"/>
179+
</LinearLayout>
180+
</com.google.android.material.card.MaterialCardView>
142181
</LinearLayout>
143182
</ScrollView>
144183

0 commit comments

Comments
 (0)