fixed battery issue
This commit is contained in:
@@ -1,5 +1,8 @@
|
||||
package `in`.sminnovations.attendanceapp.main
|
||||
|
||||
import android.content.Intent
|
||||
import android.content.IntentFilter
|
||||
import android.os.BatteryManager
|
||||
import android.os.Bundle
|
||||
import android.view.WindowManager
|
||||
import androidx.activity.ComponentActivity
|
||||
@@ -12,7 +15,11 @@ import `in`.sminnovations.attendanceapp.ui.theme.SMIAttendanceTheme
|
||||
class MainAppActivity : ComponentActivity() {
|
||||
override fun onCreate(savedInstanceState: Bundle?) {
|
||||
super.onCreate(savedInstanceState)
|
||||
window.addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON)
|
||||
if (shouldKeepScreenOn()) {
|
||||
window.addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON)
|
||||
} else {
|
||||
window.clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON)
|
||||
}
|
||||
enableEdgeToEdge()
|
||||
setContent {
|
||||
SMIAttendanceTheme {
|
||||
@@ -20,4 +27,16 @@ class MainAppActivity : ComponentActivity() {
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private fun shouldKeepScreenOn(): Boolean {
|
||||
val batteryStatus = registerReceiver(
|
||||
null,
|
||||
IntentFilter(Intent.ACTION_BATTERY_CHANGED)
|
||||
) ?: return false
|
||||
val level = batteryStatus.getIntExtra(BatteryManager.EXTRA_LEVEL, -1)
|
||||
val scale = batteryStatus.getIntExtra(BatteryManager.EXTRA_SCALE, -1)
|
||||
val isCharging = batteryStatus.getIntExtra(BatteryManager.EXTRA_PLUGGED, -1) > 0
|
||||
val batteryPct = level * 100 / scale
|
||||
return batteryPct > 50 || isCharging
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user