Skip to content
This repository has been archived by the owner on Mar 24, 2021. It is now read-only.

Commit

Permalink
frame: use thermal for battery icon
Browse files Browse the repository at this point in the history
  • Loading branch information
valish committed Jan 15, 2020
1 parent e75e3c8 commit a12a8c4
Showing 1 changed file with 6 additions and 28 deletions.
34 changes: 6 additions & 28 deletions frame/app/src/main/java/ai/comma/plus/frame/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ import ai.comma.openpilot.cereal.Log.UiLayoutState
import android.net.wifi.WifiManager
import android.net.NetworkInfo
import android.os.*
import android.os.BatteryManager.BATTERY_PROPERTY_CAPACITY
import android.telephony.PhoneStateListener
import android.telephony.ServiceState
import android.telephony.SignalStrength
Expand Down Expand Up @@ -93,7 +92,6 @@ class MainActivity : Activity(), NewDestinationReceiverDelegate, OffroadNavigati
var uiLayoutReceiver: UiLayoutReceiver? = null

var networkMonitor: NetworkMonitor? = null
var batteryMonitor: BatteryMonitor? = null
var pandaConnectionMonitor: PandaConnectionMonitor? = null
var lastStarted: Boolean = false
var satelliteCount: Int = -1
Expand Down Expand Up @@ -254,6 +252,9 @@ class MainActivity : Activity(), NewDestinationReceiverDelegate, OffroadNavigati
log.thermal.freeSpace,
log.thermal.pa0,
log.thermal.thermalStatus.toString());
onBatteryChange(
log.thermal.batteryPercent.toInt(),
log.thermal.batteryStatus.toString())
}
}
}
Expand Down Expand Up @@ -622,13 +623,6 @@ class MainActivity : Activity(), NewDestinationReceiverDelegate, OffroadNavigati
uiLayoutReceiver = UiLayoutReceiver(this)
registerReceiver(uiLayoutReceiver, UiLayoutReceiver.uiLayoutIntentFilter)

batteryMonitor = BatteryMonitor()
val batteryIntentFilter = IntentFilter(Intent.ACTION_BATTERY_CHANGED)
registerReceiver(batteryMonitor, batteryIntentFilter)
val batteryManager = getSystemService(Context.BATTERY_SERVICE) as BatteryManager
val status = if (batteryManager.isCharging) BatteryManager.BATTERY_STATUS_CHARGING else BatteryManager.BATTERY_STATUS_NOT_CHARGING
onBatteryChange(batteryManager.getIntProperty(BATTERY_PROPERTY_CAPACITY), 100, status)

val telephonyManager = getSystemService(Context.TELEPHONY_SERVICE) as TelephonyManager
telephonyManager.listen(CellStateListener(), PhoneStateListener.LISTEN_SIGNAL_STRENGTHS or PhoneStateListener.LISTEN_SERVICE_STATE)

Expand Down Expand Up @@ -666,7 +660,6 @@ class MainActivity : Activity(), NewDestinationReceiverDelegate, OffroadNavigati
override fun onDestroy() {
unregisterReceiver(newDestinationReceiver)
unregisterReceiver(networkMonitor)
unregisterReceiver(batteryMonitor)

pandaConnectionMonitor?.destroy()

Expand Down Expand Up @@ -715,12 +708,10 @@ class MainActivity : Activity(), NewDestinationReceiverDelegate, OffroadNavigati
networkTypeText?.text = networkType
}

fun onBatteryChange(level: Int, scale: Int, status: Int) {
val pct = 100 * (level / (scale * 1.0))

val isCharging = status == BatteryManager.BATTERY_STATUS_CHARGING || status == BatteryManager.BATTERY_STATUS_FULL
val batteryPctRound = if (pct > 0) (Math.ceil(pct / 25) * 25).toInt() else 0
fun onBatteryChange(level: Int, status: String) {
val isCharging = status == "Charging"
val suffix = if (isCharging) "_charging" else ""
val batteryPctRound = if (level > 0) (Math.ceil(level.toDouble() / 25) * 25).toInt() else 0
val iconAsset = "indicator_battery_${batteryPctRound}${suffix}"
val iconId = resources.getIdentifier(iconAsset, "drawable", packageName)
val iconBattery = resources.getDrawable(iconId, null);
Expand Down Expand Up @@ -749,19 +740,6 @@ class MainActivity : Activity(), NewDestinationReceiverDelegate, OffroadNavigati
}
}

inner class BatteryMonitor : BroadcastReceiver() {
override fun onReceive(context: Context?, intent: Intent?) {
when (intent?.action) {
Intent.ACTION_BATTERY_CHANGED -> {
val level = intent.getIntExtra(BatteryManager.EXTRA_LEVEL, -1)
val scale = intent.getIntExtra(BatteryManager.EXTRA_SCALE, -1)
val status = intent.getIntExtra(BatteryManager.EXTRA_STATUS, -1)
onBatteryChange(level, scale, status)
}
}
}
}

inner class CellStateListener : PhoneStateListener() {
var lastServiceState: ServiceState? = null
override fun onSignalStrengthsChanged(signalStrength: SignalStrength?) {
Expand Down

0 comments on commit a12a8c4

Please sign in to comment.