|
package com.webserveis.batterycheck |
|
|
|
import android.content.BroadcastReceiver |
|
import android.content.Context |
|
import android.content.Intent |
|
import android.content.IntentFilter |
|
import android.os.BatteryManager |
|
import android.os.Build |
|
import android.os.Bundle |
|
import android.util.Log |
|
import android.view.Menu |
|
import android.view.MenuItem |
|
import android.widget.Toast |
|
import androidx.appcompat.app.AppCompatActivity |
|
import androidx.appcompat.app.AppCompatDelegate |
|
import androidx.core.content.ContextCompat |
|
import com.google.android.material.snackbar.Snackbar |
|
import kotlinx.android.synthetic.main.activity_main.* |
|
import kotlinx.android.synthetic.main.content_main.* |
|
import kotlin.math.roundToInt |
|
|
|
|
|
/* |
|
https://www.baeldung.com/java-to-kotlin |
|
adb shell dumpsys battery set level 30 |
|
|
|
https://danielme.com/2016/01/23/android-tutorial-broadcast-receiver/ |
|
https://github.com/BharathVishal/AndroidBatteryStats/blob/master/app/src/main/java/androidbatterystats/bharathvishal/com/androidbatterystats/MainActivity.kt |
|
21> ACTION_POWER_SAVE_MODE_CHANGED |
|
*/ |
|
class MainActivity : AppCompatActivity() { |
|
|
|
companion object { |
|
val TAG: String = MainActivity::class.java.simpleName |
|
} |
|
|
|
//private lateinit var batteryPercent: WaveLoadingView |
|
|
|
|
|
override fun onCreate(savedInstanceState: Bundle?) { |
|
super.onCreate(savedInstanceState) |
|
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO) |
|
|
|
setContentView(R.layout.activity_main) |
|
setSupportActionBar(toolbar) |
|
|
|
Log.d(TAG, "onCreate") |
|
|
|
//batteryPercent = findViewById<WaveLoadingView>(R.id.batteryPercent) |
|
|
|
fab.setOnClickListener { view -> |
|
|
|
if (isPlugged(this)) { |
|
Toast.makeText(this@MainActivity, "plugged!", Toast.LENGTH_SHORT).show() |
|
} else { |
|
Toast.makeText(this@MainActivity, "unplugged!", Toast.LENGTH_SHORT).show() |
|
} |
|
|
|
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG) |
|
.setAction("Action", null).show() |
|
} |
|
} |
|
|
|
|
|
override fun onStart() { |
|
super.onStart() |
|
registerReceiver(batteryChangeReceiver, IntentFilter(Intent.ACTION_BATTERY_CHANGED)) |
|
} |
|
|
|
override fun onStop() { |
|
super.onStop() |
|
unregisterReceiver(batteryChangeReceiver) |
|
} |
|
|
|
override fun onCreateOptionsMenu(menu: Menu): Boolean { |
|
// Inflate the menu; this adds items to the action bar if it is present. |
|
menuInflater.inflate(R.menu.menu_main, menu) |
|
return true |
|
} |
|
|
|
override fun onOptionsItemSelected(item: MenuItem): Boolean { |
|
// Handle action bar item clicks here. The action bar will |
|
// automatically handle clicks on the Home/Up button, so long |
|
// as you specify a parent activity in AndroidManifest.xml. |
|
return when (item.itemId) { |
|
R.id.action_battery_settings -> { |
|
val intentBatteryUsage = Intent(Intent.ACTION_POWER_USAGE_SUMMARY) |
|
startActivity(intentBatteryUsage) |
|
true |
|
} |
|
|
|
else -> super.onOptionsItemSelected(item) |
|
} |
|
} |
|
|
|
fun isPlugged(context: Context): Boolean { |
|
val batteryStatus: Intent? = IntentFilter(Intent.ACTION_BATTERY_CHANGED).let { ifilter -> |
|
context.registerReceiver(null, ifilter) |
|
} |
|
|
|
//original |
|
val plugged: Int = batteryStatus?.getIntExtra(BatteryManager.EXTRA_PLUGGED, -1) ?: -1 |
|
return plugged == BatteryManager.BATTERY_PLUGGED_AC || plugged == BatteryManager.BATTERY_PLUGGED_USB || plugged == BatteryManager.BATTERY_PLUGGED_WIRELESS |
|
|
|
} |
|
|
|
private val batteryChangeReceiver = object : BroadcastReceiver() { |
|
override fun onReceive(context: Context?, intent: Intent?) { |
|
|
|
Log.i(TAG, "onReceive $intent") |
|
|
|
/*when (intent?.action) { |
|
Intent.ACTION_POWER_CONNECTED -> handleSomethingHappened() |
|
}*/ |
|
|
|
val level: Int = intent!!.getIntExtra(BatteryManager.EXTRA_LEVEL, -1) |
|
val scale: Int = intent.getIntExtra(BatteryManager.EXTRA_SCALE, -1) |
|
val batteryPct: Float = level / scale.toFloat() |
|
|
|
val batteryInfo: BatteryInfo = BatteryInfo() |
|
batteryInfo.status = intent.getIntExtra(BatteryManager.EXTRA_STATUS, -1) |
|
batteryInfo.level = batteryPct |
|
batteryInfo.chargePlug = intent.getIntExtra(BatteryManager.EXTRA_PLUGGED, -1) |
|
batteryInfo.health = intent.getIntExtra(BatteryManager.EXTRA_HEALTH, BatteryManager.BATTERY_HEALTH_UNKNOWN) |
|
batteryInfo.voltage = intent.getIntExtra(BatteryManager.EXTRA_VOLTAGE, -1).toFloat() |
|
batteryInfo.temperature = intent.getIntExtra(BatteryManager.EXTRA_TEMPERATURE, -1) |
|
batteryInfo.technology = intent.getStringExtra(BatteryManager.EXTRA_TECHNOLOGY) |
|
|
|
Log.d(TAG, "batteryInfo $batteryInfo") |
|
|
|
lblBatteryStatus.text = getBatteryStatusString(batteryInfo.status) |
|
|
|
when (batteryInfo.chargePlug) { |
|
BatteryManager.BATTERY_PLUGGED_WIRELESS -> lblBatteryCharge.text = |
|
getText(R.string.battery_plugged_wireless) |
|
BatteryManager.BATTERY_PLUGGED_AC -> lblBatteryCharge.text = getText(R.string.battery_plugged_ac) |
|
BatteryManager.BATTERY_PLUGGED_USB -> lblBatteryCharge.text = getText(R.string.battery_plugged_usb) |
|
else -> lblBatteryCharge.text = getText(R.string.battery_in_use) |
|
} |
|
|
|
lblBatteryHealth.text = getBatteryHealthString(batteryInfo.health) |
|
|
|
batteryPercent.progressValue = (batteryPct * 100).roundToInt() |
|
batteryPercent.centerTitle = (batteryPct * 100).roundToInt().toString() + " %" |
|
batteryPercent.setAnimDuration(3000L) |
|
|
|
when ((batteryPct * 100).toInt()) { |
|
in 31..100 -> batteryPercent.waveColor = ContextCompat.getColor(context!!, R.color.md_green_600) |
|
in 11..30 -> batteryPercent.waveColor = ContextCompat.getColor(context!!, R.color.md_yellow_500) |
|
in 0..10 -> batteryPercent.waveColor = ContextCompat.getColor(context!!, R.color.md_red_500) |
|
} |
|
|
|
|
|
outputText.text = null |
|
|
|
outputText.append( |
|
String.format( |
|
getText(R.string.battery_terminal_status_label).toString(), |
|
getBatteryStatusString(batteryInfo.status) |
|
) + System.getProperty("line.separator") |
|
) |
|
|
|
outputText.append( |
|
String.format( |
|
getText(R.string.battery_terminal_plugged_label).toString(), |
|
getBatteryPluggedString(batteryInfo.chargePlug) |
|
) + System.getProperty("line.separator") |
|
) |
|
|
|
outputText.append( |
|
String.format( |
|
getText(R.string.battery_terminal_level_label).toString(), |
|
level |
|
) + System.getProperty("line.separator") |
|
) |
|
|
|
outputText.append( |
|
String.format( |
|
getText(R.string.battery_terminal_health_label).toString(), |
|
getBatteryHealthString(batteryInfo.health) |
|
) + System.getProperty("line.separator") |
|
) |
|
|
|
outputText.append( |
|
String.format( |
|
getText(R.string.battery_terminal_voltage_label).toString(), |
|
batteryInfo.voltage |
|
) + System.getProperty("line.separator") |
|
) |
|
|
|
outputText.append( |
|
String.format( |
|
getText(R.string.battery_terminal_temperature_label).toString(), |
|
(batteryInfo.temperature / 10).toFloat() |
|
) + System.getProperty("line.separator") |
|
) |
|
|
|
outputText.append( |
|
String.format( |
|
getText(R.string.battery_terminal_technology_label).toString(), |
|
batteryInfo.technology |
|
) + System.getProperty("line.separator") |
|
) |
|
|
|
} |
|
|
|
} |
|
|
|
private fun getBatteryStatusString(status: Int?): String { |
|
return when (status) { |
|
BatteryManager.BATTERY_STATUS_CHARGING -> getText(R.string.battery_status_charging).toString() |
|
BatteryManager.BATTERY_STATUS_DISCHARGING -> getText(R.string.battery_status_discharging).toString() |
|
BatteryManager.BATTERY_STATUS_FULL -> getText(R.string.battery_status_full).toString() |
|
BatteryManager.BATTERY_STATUS_NOT_CHARGING -> getText(R.string.battery_status_not_charging).toString() |
|
BatteryManager.BATTERY_STATUS_UNKNOWN -> getText(R.string.battery_status_unknown).toString() |
|
else -> "" |
|
} |
|
} |
|
|
|
private fun getBatteryPluggedString(plugged: Int?): String { |
|
return when (plugged) { |
|
BatteryManager.BATTERY_PLUGGED_WIRELESS -> getText(R.string.battery_plugged_wireless).toString() |
|
BatteryManager.BATTERY_PLUGGED_AC -> getText(R.string.battery_plugged_ac).toString() |
|
BatteryManager.BATTERY_PLUGGED_USB -> getText(R.string.battery_plugged_usb).toString() |
|
else -> getText(R.string.battery_plugged_not).toString() |
|
} |
|
} |
|
|
|
private fun getBatteryHealthString(health: Int?): String { |
|
return when (health) { |
|
BatteryManager.BATTERY_HEALTH_GOOD -> getText(R.string.battery_health_good).toString() |
|
BatteryManager.BATTERY_HEALTH_COLD -> getText(R.string.battery_health_cold).toString() |
|
BatteryManager.BATTERY_HEALTH_DEAD -> getText(R.string.battery_health_dead).toString() |
|
BatteryManager.BATTERY_HEALTH_OVERHEAT -> getText(R.string.battery_health_overheat).toString() |
|
BatteryManager.BATTERY_HEALTH_OVER_VOLTAGE -> getText(R.string.battery_health_over_voltage).toString() |
|
BatteryManager.BATTERY_HEALTH_UNSPECIFIED_FAILURE -> getText(R.string.battery_health_unspecified_failure).toString() |
|
else -> getText(R.string.battery_health_unknown).toString() |
|
} |
|
} |
|
|
|
private fun getBatteryCapacity(ctx: Context): Long { |
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { |
|
val mBatteryManager = ctx.getSystemService(Context.BATTERY_SERVICE) as BatteryManager |
|
val chargeCounter = mBatteryManager.getLongProperty(BatteryManager.BATTERY_PROPERTY_CHARGE_COUNTER) |
|
val capacity = mBatteryManager.getLongProperty(BatteryManager.BATTERY_PROPERTY_CAPACITY) |
|
|
|
val currentAvg = mBatteryManager.getLongProperty(BatteryManager.BATTERY_PROPERTY_CURRENT_AVERAGE) |
|
val currentNow: Long = |
|
(mBatteryManager.getLongProperty(BatteryManager.BATTERY_PROPERTY_CURRENT_NOW) / 1000) |
|
|
|
Log.d(TAG, "chargeCounter $chargeCounter") |
|
Log.d(TAG, "capacity $capacity") |
|
Log.d(TAG, "CurrentAvg $currentAvg mah") |
|
Log.d(TAG, "currentNow $currentNow mah") |
|
|
|
return (chargeCounter.toFloat() / capacity.toFloat() * 100f).toLong() |
|
} |
|
|
|
return 0 |
|
} |
|
|
|
|
|
} |