├── app
├── .gitignore
├── src
│ └── main
│ │ ├── res
│ │ ├── resources.properties
│ │ ├── values
│ │ │ ├── strings.xml
│ │ │ ├── themes.xml
│ │ │ └── resources.xml
│ │ ├── xml
│ │ │ └── locales_config.xml
│ │ ├── mipmap-anydpi
│ │ │ ├── ic_launcher.xml
│ │ │ └── ic_launcher_round.xml
│ │ ├── drawable
│ │ │ ├── ico_back.xml
│ │ │ ├── ic_launcher_background.xml
│ │ │ ├── ic_launcher_foreground.xml
│ │ │ └── ico_settings.xml
│ │ ├── values-night
│ │ │ └── themes.xml
│ │ ├── values-en
│ │ │ └── strings.xml
│ │ ├── values-es
│ │ │ └── strings.xml
│ │ └── layout
│ │ │ ├── activity_main.xml
│ │ │ └── activity_settings.xml
│ │ ├── ic_launcher-playstore.png
│ │ ├── java
│ │ └── dubrowgn
│ │ │ └── wattz
│ │ │ ├── Extensions.kt
│ │ │ ├── PeriodicTask.kt
│ │ │ ├── PrettyFormat.kt
│ │ │ ├── BootReceiver.kt
│ │ │ ├── RadioLayout.kt
│ │ │ ├── BatterySnapshot.kt
│ │ │ ├── Battery.kt
│ │ │ ├── SettingsActivity.kt
│ │ │ ├── MainActivity.kt
│ │ │ └── StatusService.kt
│ │ └── AndroidManifest.xml
├── proguard-rules.pro
└── build.gradle.kts
├── readme
├── home.png
├── main.png
└── settings.png
├── fastlane
└── metadata
│ └── android
│ └── en-US
│ ├── changelogs
│ ├── 15.txt
│ ├── 14.txt
│ ├── 1.txt
│ ├── 17.txt
│ ├── 3.txt
│ ├── 4.txt
│ ├── 16.txt
│ ├── 20.txt
│ ├── 18.txt
│ ├── 2.txt
│ ├── 0.txt
│ ├── 10.txt
│ ├── 7.txt
│ ├── 12.txt
│ ├── 19.txt
│ ├── 8.txt
│ ├── 5.txt
│ ├── 13.txt
│ ├── 11.txt
│ ├── 9.txt
│ └── 6.txt
│ ├── images
│ ├── phoneScreenshots
│ │ ├── home.png
│ │ ├── main.png
│ │ └── settings.png
│ └── icon.png
│ ├── short_description.txt
│ └── full_description.txt
├── gradle
└── wrapper
│ ├── gradle-wrapper.jar
│ └── gradle-wrapper.properties
├── .gitignore
├── settings.gradle.kts
├── gradle.properties
├── LICENSE
├── readme.md
├── gradlew.bat
└── gradlew
/app/.gitignore:
--------------------------------------------------------------------------------
1 | /build
--------------------------------------------------------------------------------
/app/src/main/res/resources.properties:
--------------------------------------------------------------------------------
1 | unqualifiedResLocale=en-US
--------------------------------------------------------------------------------
/app/src/main/res/values/strings.xml:
--------------------------------------------------------------------------------
1 | ../values-en/strings.xml
--------------------------------------------------------------------------------
/readme/home.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/dubrowgn/wattz/HEAD/readme/home.png
--------------------------------------------------------------------------------
/readme/main.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/dubrowgn/wattz/HEAD/readme/main.png
--------------------------------------------------------------------------------
/fastlane/metadata/android/en-US/changelogs/15.txt:
--------------------------------------------------------------------------------
1 | • Reduce app APK size by ~58%
2 |
3 |
--------------------------------------------------------------------------------
/readme/settings.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/dubrowgn/wattz/HEAD/readme/settings.png
--------------------------------------------------------------------------------
/fastlane/metadata/android/en-US/changelogs/14.txt:
--------------------------------------------------------------------------------
1 | • Add native support for Android 14
2 |
--------------------------------------------------------------------------------
/fastlane/metadata/android/en-US/changelogs/1.txt:
--------------------------------------------------------------------------------
1 | * Add 'time to charged' to notification text
2 |
--------------------------------------------------------------------------------
/fastlane/metadata/android/en-US/changelogs/17.txt:
--------------------------------------------------------------------------------
1 | • Use static version info for F-Droid
2 |
3 |
--------------------------------------------------------------------------------
/fastlane/metadata/android/en-US/images/phoneScreenshots/home.png:
--------------------------------------------------------------------------------
1 | ../../../../../../readme/home.png
--------------------------------------------------------------------------------
/fastlane/metadata/android/en-US/images/phoneScreenshots/main.png:
--------------------------------------------------------------------------------
1 | ../../../../../../readme/main.png
--------------------------------------------------------------------------------
/fastlane/metadata/android/en-US/changelogs/3.txt:
--------------------------------------------------------------------------------
1 | * Internal changes to support release on F-Droid
2 |
--------------------------------------------------------------------------------
/fastlane/metadata/android/en-US/changelogs/4.txt:
--------------------------------------------------------------------------------
1 | * Lower the minimum required Android version to 9 (Pie)
2 |
--------------------------------------------------------------------------------
/fastlane/metadata/android/en-US/images/phoneScreenshots/settings.png:
--------------------------------------------------------------------------------
1 | ../../../../../../readme/settings.png
--------------------------------------------------------------------------------
/fastlane/metadata/android/en-US/changelogs/16.txt:
--------------------------------------------------------------------------------
1 | • Fix highlight color on Samsung phones in dark mode
2 |
3 |
--------------------------------------------------------------------------------
/fastlane/metadata/android/en-US/changelogs/20.txt:
--------------------------------------------------------------------------------
1 | • Add Spanish language translations (contributed by frdarko)
2 |
--------------------------------------------------------------------------------
/gradle/wrapper/gradle-wrapper.jar:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/dubrowgn/wattz/HEAD/gradle/wrapper/gradle-wrapper.jar
--------------------------------------------------------------------------------
/app/src/main/ic_launcher-playstore.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/dubrowgn/wattz/HEAD/app/src/main/ic_launcher-playstore.png
--------------------------------------------------------------------------------
/fastlane/metadata/android/en-US/changelogs/18.txt:
--------------------------------------------------------------------------------
1 | • Show plug type information when charging
2 | • Reduce APK weight by ~28%
3 |
4 |
--------------------------------------------------------------------------------
/fastlane/metadata/android/en-US/changelogs/2.txt:
--------------------------------------------------------------------------------
1 | * Start indicator service on boot
2 | * Run indicator service in its own process
3 |
--------------------------------------------------------------------------------
/fastlane/metadata/android/en-US/short_description.txt:
--------------------------------------------------------------------------------
1 | A real time battery power consumption and charging indicator in your status bar
2 |
--------------------------------------------------------------------------------
/fastlane/metadata/android/en-US/images/icon.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/dubrowgn/wattz/HEAD/fastlane/metadata/android/en-US/images/icon.png
--------------------------------------------------------------------------------
/fastlane/metadata/android/en-US/changelogs/0.txt:
--------------------------------------------------------------------------------
1 | Initial Release
2 | * Battery current status bar indicator
3 | * In-app detailed battery metrics
4 |
--------------------------------------------------------------------------------
/fastlane/metadata/android/en-US/changelogs/10.txt:
--------------------------------------------------------------------------------
1 | • Reduce delay when populating values on app start
2 | • Improve consistency between reported values
3 |
--------------------------------------------------------------------------------
/fastlane/metadata/android/en-US/changelogs/7.txt:
--------------------------------------------------------------------------------
1 | * Add "Charging Since" field to main app
2 | * Only show "Time to Full" field in main app if actually charging
3 |
--------------------------------------------------------------------------------
/fastlane/metadata/android/en-US/changelogs/12.txt:
--------------------------------------------------------------------------------
1 | • Fix an issue that resulted in stale temperature and voltage values - https://github.com/dubrowgn/wattz/issues/24
2 |
--------------------------------------------------------------------------------
/fastlane/metadata/android/en-US/changelogs/19.txt:
--------------------------------------------------------------------------------
1 | • Add charge level information
2 | • Suppress potential crash when starting status indicator on newer Android devices
3 |
4 |
--------------------------------------------------------------------------------
/fastlane/metadata/android/en-US/changelogs/8.txt:
--------------------------------------------------------------------------------
1 | * Fix an issue where the app would incorrectly reset the "Charging Since" timestamp - https://github.com/dubrowgn/wattz/issues/10
2 |
--------------------------------------------------------------------------------
/fastlane/metadata/android/en-US/changelogs/5.txt:
--------------------------------------------------------------------------------
1 | * Add work around for devices which report incorrect battery current units (e.g. Samsung devices) - https://github.com/dubrowgn/wattz/issues/5
2 |
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 | *.apk
2 | *.iml
3 |
4 | .DS_Store
5 | .gradle
6 | .idea
7 |
8 | /app/debug
9 | /app/release
10 | /build
11 | /captures
12 | .externalNativeBuild
13 | .cxx
14 | local.properties
15 |
--------------------------------------------------------------------------------
/fastlane/metadata/android/en-US/changelogs/13.txt:
--------------------------------------------------------------------------------
1 | • Add units setting for the status bar indicator - https://github.com/dubrowgn/wattz/issues/15, https://github.com/dubrowgn/wattz/issues/16, https://github.com/dubrowgn/wattz/issues/28
2 |
--------------------------------------------------------------------------------
/fastlane/metadata/android/en-US/changelogs/11.txt:
--------------------------------------------------------------------------------
1 | • Simplify settings view
2 | • Calculate status icon size using display density
3 | • Improve consistency between settings view and status icon
4 | • Immediately update after plug/unplug event
5 |
--------------------------------------------------------------------------------
/app/src/main/res/xml/locales_config.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
--------------------------------------------------------------------------------
/fastlane/metadata/android/en-US/changelogs/9.txt:
--------------------------------------------------------------------------------
1 | * Overhauled main app screen.
2 | * Added workaround settings screen, for phones with incorrect BatteryManager implementations.
3 | * Energy is now reported in Wh, in addition to Ah.
4 | * Fixed an issue where the status indicator and app could show different power values.
5 |
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-anydpi/ic_launcher.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-anydpi/ic_launcher_round.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
--------------------------------------------------------------------------------
/app/src/main/java/dubrowgn/wattz/Extensions.kt:
--------------------------------------------------------------------------------
1 | package dubrowgn.wattz
2 |
3 | fun Double?.div(v: Double?) : Double? {
4 | if (v == null)
5 | return null
6 |
7 | return this?.div(v)
8 | }
9 |
10 | fun Double?.times(v: Double?) : Double? {
11 | if (v == null)
12 | return null
13 |
14 | return this?.times(v)
15 | }
16 |
--------------------------------------------------------------------------------
/gradle/wrapper/gradle-wrapper.properties:
--------------------------------------------------------------------------------
1 | distributionBase=GRADLE_USER_HOME
2 | distributionPath=wrapper/dists
3 | distributionSha256Sum=4159b938ec734a8388ce03f52aa8f3c7ed0d31f5438622545de4f83a89b79788
4 | distributionUrl=https\://services.gradle.org/distributions/gradle-8.0-bin.zip
5 | networkTimeout=10000
6 | zipStoreBase=GRADLE_USER_HOME
7 | zipStorePath=wrapper/dists
8 |
--------------------------------------------------------------------------------
/fastlane/metadata/android/en-US/changelogs/6.txt:
--------------------------------------------------------------------------------
1 | * Add work around for devices which incorrectly report "0 seconds to charged" when not charging - https://github.com/dubrowgn/wattz/issues/5, https://github.com/dubrowgn/wattz/issues/8
2 | * Add work around for devices which always report false for "is charging" - https://github.com/dubrowgn/wattz/issues/5, https://github.com/dubrowgn/wattz/issues/8
3 |
--------------------------------------------------------------------------------
/app/src/main/res/drawable/ico_back.xml:
--------------------------------------------------------------------------------
1 |
6 |
7 |
8 |
--------------------------------------------------------------------------------
/settings.gradle.kts:
--------------------------------------------------------------------------------
1 | pluginManagement {
2 | repositories {
3 | gradlePluginPortal()
4 | google()
5 | mavenCentral()
6 | }
7 | }
8 | dependencyResolutionManagement {
9 | repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
10 | repositories {
11 | google()
12 | mavenCentral()
13 | }
14 | }
15 | rootProject.name = "wattz"
16 | include("app")
17 |
--------------------------------------------------------------------------------
/app/src/main/res/drawable/ic_launcher_background.xml:
--------------------------------------------------------------------------------
1 |
2 |
7 |
10 |
11 |
--------------------------------------------------------------------------------
/app/src/main/java/dubrowgn/wattz/PeriodicTask.kt:
--------------------------------------------------------------------------------
1 | package dubrowgn.wattz
2 |
3 | import android.os.Handler
4 |
5 | class PeriodicTask(callback: () -> Unit, intervalMs: Long) {
6 | private val ticker = Handler()
7 | private val callback = callback
8 | private val runnable = Runnable { start() }
9 | private val intervalMs = intervalMs
10 |
11 | fun stop() {
12 | ticker.removeCallbacks(runnable)
13 | }
14 |
15 | private fun tick() {
16 | callback()
17 | ticker.postDelayed(runnable, intervalMs)
18 | }
19 |
20 | fun start() {
21 | stop()
22 | tick()
23 | }
24 | }
--------------------------------------------------------------------------------
/app/src/main/res/values/themes.xml:
--------------------------------------------------------------------------------
1 |
2 | #ffffff
3 | #212121
4 | #000000
5 |
12 |
13 |
--------------------------------------------------------------------------------
/app/src/main/res/drawable/ic_launcher_foreground.xml:
--------------------------------------------------------------------------------
1 |
6 |
10 |
13 |
14 |
15 |
--------------------------------------------------------------------------------
/app/src/main/java/dubrowgn/wattz/PrettyFormat.kt:
--------------------------------------------------------------------------------
1 | package dubrowgn.wattz
2 |
3 | import kotlin.math.absoluteValue
4 |
5 | fun fmt(v: Double?): String {
6 | if (v == null)
7 | return "- "
8 |
9 | if (v.absoluteValue >= 10.0)
10 | return "%.0f".format(v)
11 |
12 | return "%.1f".format(v)
13 | }
14 |
15 | fun fmtSeconds(seconds: Double?): String {
16 | if (seconds == null)
17 | return ""
18 |
19 | var secs = seconds.toInt()
20 | if (secs < 60)
21 | return "${secs}s"
22 |
23 | var mins = secs / 60
24 | secs %= 60
25 | if (mins < 60)
26 | return "${mins}m ${secs}s"
27 |
28 | val hrs = mins / 60
29 | mins %= 60
30 | return "${hrs}h ${mins}m ${secs}s"
31 | }
32 |
--------------------------------------------------------------------------------
/app/src/main/res/values-night/themes.xml:
--------------------------------------------------------------------------------
1 |
2 | #121212
3 | #e0e0e0
4 | #ffffff
5 |
13 |
14 |
--------------------------------------------------------------------------------
/app/proguard-rules.pro:
--------------------------------------------------------------------------------
1 | # Add project specific ProGuard rules here.
2 | # You can control the set of applied configuration files using the
3 | # proguardFiles setting in build.gradle.
4 | #
5 | # For more details, see
6 | # http://developer.android.com/guide/developing/tools/proguard.html
7 |
8 | # If your project uses WebView with JS, uncomment the following
9 | # and specify the fully qualified class name to the JavaScript interface
10 | # class:
11 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview {
12 | # public *;
13 | #}
14 |
15 | # Uncomment this to preserve the line number information for
16 | # debugging stack traces.
17 | #-keepattributes SourceFile,LineNumberTable
18 |
19 | # If you keep the line number information, uncomment this to
20 | # hide the original source file name.
21 | #-renamesourcefileattribute SourceFile
--------------------------------------------------------------------------------
/app/src/main/res/values/resources.xml:
--------------------------------------------------------------------------------
1 |
2 | #0080ff
3 | #005cbc
4 | #ffffff
5 |
6 | 32sp
7 | 20sp
8 | 16sp
9 | 8sp
10 |
11 |
19 |
20 |
--------------------------------------------------------------------------------
/fastlane/metadata/android/en-US/full_description.txt:
--------------------------------------------------------------------------------
1 | Wattz is a simple battery indicator. It shows one of several real time battery metrics right in your status bar. Open the app to see detailed battery metrics and and edit your settings.
2 |
3 | Notification permissions are required to show the indicator in the status bar. Please open the app once to automatically start the indicator service.
4 |
5 | This app respects your privacy!
6 | • No unnecessary permissions
7 | • No ads
8 | • No collection of user data of any kind
9 | • No sharing data with third parties
10 |
11 | Detailed battery metrics include:
12 | • Power (watts)
13 | • Current (amps)
14 | • Voltage (volts)
15 | • Energy Level (watt-hours and amp-hours)
16 | • Temperature (celsius)
17 | • Charge Level (percent)
18 | • Is Charging (yes/no)
19 | • Charging Since (date/time)
20 | • Time to Full Charge (duration)
21 |
--------------------------------------------------------------------------------
/gradle.properties:
--------------------------------------------------------------------------------
1 | # Project-wide Gradle settings.
2 | # IDE (e.g. Android Studio) users:
3 | # Gradle settings configured through the IDE *will override*
4 | # any settings specified in this file.
5 | # For more details on how to configure your build environment visit
6 | # http://www.gradle.org/docs/current/userguide/build_environment.html
7 |
8 | # Specifies the JVM arguments used for the daemon process.
9 | # The setting is particularly useful for tweaking memory settings.
10 | org.gradle.jvmargs=-Xmx2048m -Dfile.encoding=UTF-8
11 |
12 | # AndroidX package structure to make it clearer which packages are bundled with the
13 | # Android operating system, and which are packaged with your app"s APK
14 | # https://developer.android.com/topic/libraries/support-library/androidx-rn
15 | android.useAndroidX=true
16 |
17 | # Kotlin code style for this project: "official" or "obsolete":
18 | kotlin.code.style=official
19 |
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | MIT License
2 |
3 | Copyright (c) 2022 Dustin Brown
4 |
5 | Permission is hereby granted, free of charge, to any person obtaining a copy
6 | of this software and associated documentation files (the "Software"), to deal
7 | in the Software without restriction, including without limitation the rights
8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 | copies of the Software, and to permit persons to whom the Software is
10 | furnished to do so, subject to the following conditions:
11 |
12 | The above copyright notice and this permission notice shall be included in all
13 | copies or substantial portions of the Software.
14 |
15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 | SOFTWARE.
22 |
--------------------------------------------------------------------------------
/app/build.gradle.kts:
--------------------------------------------------------------------------------
1 | plugins {
2 | id("com.android.application")
3 | id("org.jetbrains.kotlin.android")
4 | }
5 |
6 | android {
7 | compileSdk = 34
8 |
9 | defaultConfig {
10 | applicationId = "dubrowgn.wattz"
11 | minSdk = 28 // BatteryManager.computeChargeTimeRemaining()
12 | // work around unused library resources
13 | resourceConfigurations.addAll(listOf("anydpi", "en", "es"))
14 | targetSdk = 34
15 | versionCode = 20
16 | versionName = "1.20"
17 | }
18 |
19 | buildTypes {
20 | release {
21 | isMinifyEnabled = true
22 | isShrinkResources = true
23 | proguardFiles(getDefaultProguardFile("proguard-android-optimize.txt"), "proguard-rules.pro")
24 | }
25 | }
26 | compileOptions {
27 | sourceCompatibility = JavaVersion.VERSION_17
28 | targetCompatibility = JavaVersion.VERSION_17
29 | }
30 | kotlinOptions {
31 | jvmTarget = "17"
32 | }
33 | namespace = "dubrowgn.wattz"
34 | }
35 |
36 | dependencies {
37 | implementation("androidx.core:core-ktx:1.12.0")
38 | implementation("androidx.constraintlayout:constraintlayout:2.1.4")
39 | }
40 |
--------------------------------------------------------------------------------
/app/src/main/res/drawable/ico_settings.xml:
--------------------------------------------------------------------------------
1 |
6 |
7 |
8 |
--------------------------------------------------------------------------------
/app/src/main/java/dubrowgn/wattz/BootReceiver.kt:
--------------------------------------------------------------------------------
1 | package dubrowgn.wattz
2 |
3 | import android.content.BroadcastReceiver
4 | import android.content.Context
5 | import android.content.Intent
6 | import android.util.Log
7 |
8 | class BootReceiver : BroadcastReceiver() {
9 | private fun debug(msg: String) {
10 | Log.d(this::class.java.name, msg)
11 | }
12 | private fun error(msg: String) {
13 | Log.e(this::class.java.name, msg)
14 | }
15 |
16 | override fun onReceive(context: Context?, intent: Intent?) {
17 | debug("onReceive()")
18 |
19 | if (context == null) {
20 | error("context is null")
21 | return
22 | }
23 |
24 | if (intent == null) {
25 | error("intent is null")
26 | return
27 | }
28 |
29 | if (intent.action != Intent.ACTION_BOOT_COMPLETED) {
30 | error("received non-boot action '${intent.action}'")
31 | return
32 | }
33 |
34 | debug("starting status service...")
35 | try {
36 | context.startForegroundService(Intent(context, StatusService::class.java))
37 | } catch (e: Exception) {
38 | error("Failed to start StatusService: ${e.message}")
39 | }
40 | }
41 | }
--------------------------------------------------------------------------------
/app/src/main/res/values-en/strings.xml:
--------------------------------------------------------------------------------
1 |
2 | Wattz
3 | Back
4 | Battery
5 | Charging
6 | Charging Since
7 | Charge Level
8 | Current
9 | Energy
10 | fully charged
11 | -
12 | Status Bar Indicator
13 | no
14 | Power
15 | Settings
16 | Temperature
17 | Time to Full Charge
18 | Voltage
19 | Units
20 | yes
21 |
22 |
23 | Power Scalar Workaround
24 | When not plugged in, change this scalar until \u0022Power\u0022 is greater than 0W, but less than about 10W.
25 | Charging Workaround
26 | When not plugged in, enable this toggle if \u0022Charging\u0022 says \u0022yes\u0022.
27 | Workarounds
28 | Many phones do not correctly implement the Android BatteryManager interface, and will report incorrect battery metrics by default. If needed, use the following settings to workaround implementation defects on your phone.
29 |
--------------------------------------------------------------------------------
/app/src/main/java/dubrowgn/wattz/RadioLayout.kt:
--------------------------------------------------------------------------------
1 | package dubrowgn.wattz
2 |
3 | import android.content.Context
4 | import android.util.AttributeSet
5 | import android.util.Log
6 | import android.view.View
7 | import android.widget.RadioButton
8 | import androidx.constraintlayout.widget.ConstraintLayout
9 |
10 | typealias checkChangedHandler = (id: Int) -> Unit
11 |
12 | class RadioLayout : ConstraintLayout {
13 | constructor(context: Context) : super(context)
14 | constructor(context: Context, attrs: AttributeSet) : super(context, attrs)
15 | constructor(context: Context, attrs: AttributeSet, defStyleAttr: Int) : super(context, attrs, defStyleAttr)
16 |
17 | private var checkedId: Int = -1
18 | var checkChangedCallback: checkChangedHandler? = null
19 | private val radios = ArrayList()
20 | private var updateInProgress = false
21 |
22 | fun check(id: Int) {
23 | activateRadio(findViewById(id))
24 | }
25 |
26 | val checkedRadioButtonId : Int get() = checkedId
27 |
28 | private fun activateRadio(radio: RadioButton) {
29 | if (updateInProgress || checkedId == radio.id)
30 | return
31 |
32 | // suppress recursive updates
33 | updateInProgress = true
34 | checkedId = radio.id
35 | for (r in radios) {
36 | r.isChecked = false
37 | }
38 | radio.isChecked = true
39 | updateInProgress = false
40 |
41 | checkChangedCallback?.invoke(checkedId)
42 | }
43 |
44 | override fun onViewAdded(view: View?) {
45 | super.onViewAdded(view)
46 |
47 | val rb = view as? RadioButton
48 | if (rb != null) {
49 | radios.add(rb)
50 | rb.setOnCheckedChangeListener { _, _ -> activateRadio(rb) }
51 | }
52 | }
53 | }
54 |
--------------------------------------------------------------------------------
/app/src/main/AndroidManifest.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
17 |
18 |
21 |
22 |
23 |
24 |
25 |
26 |
29 |
30 |
34 |
35 |
36 |
37 |
38 |
43 |
44 |
45 |
--------------------------------------------------------------------------------
/app/src/main/res/values-es/strings.xml:
--------------------------------------------------------------------------------
1 |
2 | Wattz
3 | Atrás
4 | Batería
5 | Cargando
6 | Cargando desde
7 | Nivel de carga
8 | Corriente
9 | Energía
10 | Completamente cargado
11 | -
12 | Indicador de barra de estado
13 | No
14 | Potencia
15 | Ajustes
16 | Temperatura
17 | Tiempo hasta completar la carga
18 | Voltaje
19 | Unidades
20 | Sí
21 |
22 |
23 | Escala de potencia alternativa
24 | Cuando no esté enchufado, cambie esta unidad de escala hasta que \u0022Potencia\u0022 sea mayor de 0W, pero menor de 10W.
25 | Solución alternativa de carga
26 | Cuando no esté enchufado, activar si \u0022Cargando\u0022 dice \u0022Sí\u0022.
27 | Soluciones alternativas
28 | Muchos teléfonos no implementan correctamente la interfaz de \u0022Android BatteryManager\u0022 y reportarán métricas de batería incorrectas de forma predeterminada. Si es necesario, use la siguiente configuración para solucionar los defectos de implementación en el teléfono.
29 |
30 |
--------------------------------------------------------------------------------
/app/src/main/java/dubrowgn/wattz/BatterySnapshot.kt:
--------------------------------------------------------------------------------
1 | package dubrowgn.wattz
2 |
3 |
4 | class BatterySnapshot(
5 | // configuration
6 | private val currentScalar: Double,
7 | private val invertCurrent: Boolean,
8 |
9 | // data
10 | val chargeTimeRemainingRaw: Long,
11 | val currentRaw: Long?,
12 | val energyRaw: Long?,
13 | val level: Double?,
14 | val isChargingRaw: Boolean,
15 | val plugType: PlugType?,
16 | val tempRaw: Long?,
17 | val voltsRaw: Long?,
18 | ) {
19 | private fun fromMicros(v: Double?) : Double? {
20 | return v?.div(1_000_000.0)
21 | }
22 |
23 | private fun fromMillis(v: Double?) : Double? {
24 | return v?.div(1_000.0)
25 | }
26 |
27 | val microamps : Double? get() {
28 | val sign = if (invertCurrent) 1.0 else -1.0
29 | return currentRaw?.times(currentScalar)?.times(sign)
30 | }
31 | val milliamps : Double? get() = microamps?.div(1_000.0)
32 | val amps : Double? get() = fromMicros(microamps)
33 |
34 | val millivolts : Double? get() = voltsRaw?.toDouble()
35 | val volts : Double? get() = fromMillis(millivolts)
36 |
37 | val milliwatts : Double? get() = milliamps.times(volts)
38 | val watts : Double? get() = amps.times(volts)
39 |
40 | val energyAmpHours : Double? get() = fromMicros(energyRaw?.toDouble())
41 | val energyWattHours : Double? get() = volts?.times(energyAmpHours)
42 |
43 | val levelPercent : Double? get() = level?.times(100.0)
44 |
45 | val celsius : Double? get() = tempRaw?.toDouble()?.div(10.0)
46 |
47 | // Some devices always report false for isCharging, so fall back to battery current detection
48 | val charging : Boolean get() {
49 | if (isChargingRaw)
50 | return true
51 |
52 | val ma = milliamps
53 | return ma != null && ma < 1.0
54 | }
55 |
56 | val secondsUntilCharged: Double? get() {
57 | // Some devices incorrectly report "0 seconds to full" when not charging,
58 | // so ensure we are actually charging first.
59 | if (!charging)
60 | return null
61 |
62 | val ms = chargeTimeRemainingRaw
63 | if (ms == -1L)
64 | return null
65 |
66 | return fromMillis(ms.toDouble())
67 | }
68 | }
69 |
--------------------------------------------------------------------------------
/readme.md:
--------------------------------------------------------------------------------
1 | # Wattz
2 |
3 | [
](https://play.google.com/store/apps/details?id=dubrowgn.wattz)
4 | [
](https://f-droid.org/packages/dubrowgn.wattz/)
5 |
6 | *Wattz* is a simple battery indicator. It shows one of several real time battery
7 | metrics right in your status bar. Open the app to see detailed battery metrics
8 | and and edit your settings.
9 |
10 | Notification permissions are required to show the indicator in the status bar.
11 | Please open the app once to automatically start the indicator service.
12 |
13 | This app respects your privacy!
14 | * No unnecessary permissions
15 | * No ads
16 | * No collection of user data of any kind
17 | * No sharing data with third parties
18 |
19 | Detailed battery metrics include:
20 | * Power (watts)
21 | * Current (amps)
22 | * Voltage (volts)
23 | * Energy Level (watt-hours and amp-hours)
24 | * Temperature (celsius)
25 | * Charge Level (percent)
26 | * Is Charging (yes/no)
27 | * Charging Since (date/time)
28 | * Time to Full Charge (duration)
29 |
30 | ## Screenshots
31 |
32 | **Status Bar**
33 |
34 |
35 |
36 | **App View**
37 |
38 |
39 |
40 | **Settings View**
41 |
42 |
43 |
44 | ## FAQ
45 |
46 | 1. Why does my phone always show `0W`?
47 |
48 | Many phones, especially Samsungs, don't follow the BatteryManager spec. Try changing "Power Scalar Workaround" in the settings view.
49 |
50 | 2. Why does my external power meter show different power numbers than *Wattz*?
51 |
52 | Wattz can only measure power coming into or out of the battery management system. Your external meter is measuring this plus any power your phone is using in addition to that.
53 |
54 | 3. Why isn't the indicator showing up in my status bar?
55 |
56 | *Wattz* needs notification permissions on newer Android phones in order to show the indicator. Make sure to open *Wattz* at least once, where it should prompt you to grant it permissions. Otherwise, check the Android app settings to ensure *Wattz* has notification permissions.
57 |
58 | Keep in mind, Android can revoke these permissions at any time without telling you, so you may need to re-enable them periodically.
59 |
--------------------------------------------------------------------------------
/app/src/main/java/dubrowgn/wattz/Battery.kt:
--------------------------------------------------------------------------------
1 | package dubrowgn.wattz
2 |
3 | import android.app.Activity
4 | import android.content.Context
5 | import android.content.Intent
6 | import android.content.IntentFilter
7 | import android.os.BatteryManager
8 |
9 | enum class PlugType {
10 | Ac,
11 | Dock,
12 | Unknown,
13 | Usb,
14 | Wireless;
15 |
16 | companion object {
17 | fun fromRaw(plugType: Int?): PlugType? {
18 | return when (plugType) {
19 | null, 0 -> null
20 | BatteryManager.BATTERY_PLUGGED_AC -> Ac
21 | BatteryManager.BATTERY_PLUGGED_DOCK -> Dock
22 | BatteryManager.BATTERY_PLUGGED_USB -> Usb
23 | BatteryManager.BATTERY_PLUGGED_WIRELESS -> Wireless
24 | else -> Unknown
25 | }
26 | }
27 | }
28 | }
29 |
30 | class Battery(private val ctx: Context) {
31 | private val mgr = ctx.getSystemService(Activity.BATTERY_SERVICE) as BatteryManager
32 |
33 | // configuration
34 | var currentScalar = 1.0
35 | var invertCurrent = false
36 |
37 | private fun prop(id: Int): Long? {
38 | val v = mgr.getLongProperty(id)
39 | if (v == Long.MIN_VALUE)
40 | return null
41 |
42 | return v
43 | }
44 |
45 | private fun prop(id: String): Long? {
46 | val intent = ctx.registerReceiver(null, IntentFilter(Intent.ACTION_BATTERY_CHANGED))
47 | if (intent?.hasExtra(id) != true)
48 | return null
49 |
50 | val v = intent.getIntExtra(id, Int.MIN_VALUE)
51 | if (v == Int.MIN_VALUE)
52 | return null
53 |
54 | return v.toLong()
55 | }
56 |
57 | fun snapshot(): BatterySnapshot {
58 | val levelScale = prop(BatteryManager.EXTRA_SCALE)?.toDouble()
59 | val level = prop(BatteryManager.EXTRA_LEVEL)?.toDouble()
60 |
61 | return BatterySnapshot(
62 | chargeTimeRemainingRaw = mgr.computeChargeTimeRemaining(),
63 | currentRaw = prop(BatteryManager.BATTERY_PROPERTY_CURRENT_NOW),
64 | currentScalar = currentScalar,
65 | energyRaw = prop(BatteryManager.BATTERY_PROPERTY_CHARGE_COUNTER),
66 | level = level.div(levelScale),
67 | invertCurrent = invertCurrent,
68 | isChargingRaw = mgr.isCharging,
69 | plugType = PlugType.fromRaw(prop(BatteryManager.EXTRA_PLUGGED)?.toInt()),
70 | tempRaw = prop(BatteryManager.EXTRA_TEMPERATURE),
71 | voltsRaw = prop(BatteryManager.EXTRA_VOLTAGE),
72 | )
73 | }
74 | }
75 |
--------------------------------------------------------------------------------
/gradlew.bat:
--------------------------------------------------------------------------------
1 | @rem
2 | @rem Copyright 2015 the original author or authors.
3 | @rem
4 | @rem Licensed under the Apache License, Version 2.0 (the "License");
5 | @rem you may not use this file except in compliance with the License.
6 | @rem You may obtain a copy of the License at
7 | @rem
8 | @rem https://www.apache.org/licenses/LICENSE-2.0
9 | @rem
10 | @rem Unless required by applicable law or agreed to in writing, software
11 | @rem distributed under the License is distributed on an "AS IS" BASIS,
12 | @rem WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | @rem See the License for the specific language governing permissions and
14 | @rem limitations under the License.
15 | @rem
16 |
17 | @if "%DEBUG%"=="" @echo off
18 | @rem ##########################################################################
19 | @rem
20 | @rem Gradle startup script for Windows
21 | @rem
22 | @rem ##########################################################################
23 |
24 | @rem Set local scope for the variables with windows NT shell
25 | if "%OS%"=="Windows_NT" setlocal
26 |
27 | set DIRNAME=%~dp0
28 | if "%DIRNAME%"=="" set DIRNAME=.
29 | @rem This is normally unused
30 | set APP_BASE_NAME=%~n0
31 | set APP_HOME=%DIRNAME%
32 |
33 | @rem Resolve any "." and ".." in APP_HOME to make it shorter.
34 | for %%i in ("%APP_HOME%") do set APP_HOME=%%~fi
35 |
36 | @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
37 | set DEFAULT_JVM_OPTS="-Xmx64m" "-Xms64m"
38 |
39 | @rem Find java.exe
40 | if defined JAVA_HOME goto findJavaFromJavaHome
41 |
42 | set JAVA_EXE=java.exe
43 | %JAVA_EXE% -version >NUL 2>&1
44 | if %ERRORLEVEL% equ 0 goto execute
45 |
46 | echo.
47 | echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
48 | echo.
49 | echo Please set the JAVA_HOME variable in your environment to match the
50 | echo location of your Java installation.
51 |
52 | goto fail
53 |
54 | :findJavaFromJavaHome
55 | set JAVA_HOME=%JAVA_HOME:"=%
56 | set JAVA_EXE=%JAVA_HOME%/bin/java.exe
57 |
58 | if exist "%JAVA_EXE%" goto execute
59 |
60 | echo.
61 | echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME%
62 | echo.
63 | echo Please set the JAVA_HOME variable in your environment to match the
64 | echo location of your Java installation.
65 |
66 | goto fail
67 |
68 | :execute
69 | @rem Setup the command line
70 |
71 | set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar
72 |
73 |
74 | @rem Execute Gradle
75 | "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %*
76 |
77 | :end
78 | @rem End local scope for the variables with windows NT shell
79 | if %ERRORLEVEL% equ 0 goto mainEnd
80 |
81 | :fail
82 | rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of
83 | rem the _cmd.exe /c_ return code!
84 | set EXIT_CODE=%ERRORLEVEL%
85 | if %EXIT_CODE% equ 0 set EXIT_CODE=1
86 | if not ""=="%GRADLE_EXIT_CONSOLE%" exit %EXIT_CODE%
87 | exit /b %EXIT_CODE%
88 |
89 | :mainEnd
90 | if "%OS%"=="Windows_NT" endlocal
91 |
92 | :omega
93 |
--------------------------------------------------------------------------------
/app/src/main/java/dubrowgn/wattz/SettingsActivity.kt:
--------------------------------------------------------------------------------
1 | package dubrowgn.wattz
2 |
3 | import android.annotation.SuppressLint
4 | import android.app.Activity
5 | import android.content.BroadcastReceiver
6 | import android.content.Context
7 | import android.content.Intent
8 | import android.content.IntentFilter
9 | import android.os.Bundle
10 | import android.util.Log
11 | import android.view.View
12 | import android.widget.RadioGroup
13 | import android.widget.Switch
14 | import android.widget.TextView
15 |
16 | const val settingsName = "settings"
17 | const val settingsUpdateInd = "$namespace.settings-update-ind"
18 |
19 | class SettingsActivity : Activity() {
20 | private val batteryReceiver = BatteryDataReceiver()
21 |
22 | private lateinit var charging: TextView
23 | private lateinit var currentScalar: RadioGroup
24 | private lateinit var indicatorUnits: RadioLayout
25 | @SuppressLint("UseSwitchCompatOrMaterialCode")
26 | private lateinit var invertCurrent: Switch
27 | private lateinit var power: TextView
28 |
29 | private fun debug(msg: String) {
30 | Log.d(this::class.java.name, msg)
31 | }
32 |
33 | inner class BatteryDataReceiver : BroadcastReceiver() {
34 | override fun onReceive(context: Context?, intent: Intent?) {
35 | debug("BatteryDataReceiver.onReceive()")
36 |
37 | if (intent == null)
38 | return
39 |
40 | val ind = getString(R.string.indeterminate)
41 |
42 | charging.text = intent.getStringExtra("charging") ?: ind
43 | power.text = intent.getStringExtra("power") ?: ind
44 | }
45 | }
46 |
47 | private fun loadPrefs() {
48 | val settings = getSharedPreferences(settingsName, MODE_PRIVATE)
49 | currentScalar.check(
50 | when (settings.getFloat("currentScalar", -1f)) {
51 | 1000f -> R.id.currentScalar1000
52 | .001f -> R.id.currentScalar0_001
53 | else -> R.id.currentScalar1
54 | }
55 | )
56 | indicatorUnits.check(
57 | when (settings.getString("indicatorUnits", null)) {
58 | "A" -> R.id.indicatorA
59 | "Ah" -> R.id.indicatorAh
60 | "C" -> R.id.indicatorC
61 | "V" -> R.id.indicatorV
62 | "Wh" -> R.id.indicatorWh
63 | "%" -> R.id.indicatorPerc
64 | else -> R.id.indicatorW
65 | }
66 | )
67 | invertCurrent.isChecked = settings.getBoolean("invertCurrent", false)
68 | }
69 |
70 | @SuppressLint("ApplySharedPref")
71 | private fun onChange() {
72 | getSharedPreferences(settingsName, MODE_PRIVATE)
73 | .edit()
74 | .putBoolean("invertCurrent", invertCurrent.isChecked)
75 | .putFloat(
76 | "currentScalar",
77 | when(currentScalar.checkedRadioButtonId) {
78 | R.id.currentScalar1000 -> 1000f
79 | R.id.currentScalar0_001 -> 0.001f
80 | else -> 1f
81 | }
82 | )
83 | .putString(
84 | "indicatorUnits",
85 | when (indicatorUnits.checkedRadioButtonId) {
86 | R.id.indicatorA -> "A"
87 | R.id.indicatorAh -> "Ah"
88 | R.id.indicatorC -> "C"
89 | R.id.indicatorV -> "V"
90 | R.id.indicatorWh -> "Wh"
91 | R.id.indicatorPerc -> "%"
92 | else -> "W"
93 | }
94 | )
95 | .commit()
96 |
97 | sendBroadcast(Intent().setPackage(packageName).setAction(settingsUpdateInd))
98 | }
99 |
100 | override fun onCreate(savedInstanceState: Bundle?) {
101 | debug("onCreate()")
102 |
103 | super.onCreate(savedInstanceState)
104 |
105 | setContentView(R.layout.activity_settings)
106 |
107 | charging = findViewById(R.id.charging)
108 | currentScalar = findViewById(R.id.currentScalar)
109 | indicatorUnits = findViewById(R.id.indicatorUnits)
110 | invertCurrent = findViewById(R.id.invertCurrent)
111 | power = findViewById(R.id.power)
112 |
113 | loadPrefs()
114 |
115 | currentScalar.setOnCheckedChangeListener { _, _ -> onChange() }
116 | indicatorUnits.checkChangedCallback = { _ -> onChange() }
117 | invertCurrent.setOnCheckedChangeListener { _, _ -> onChange() }
118 | }
119 |
120 | override fun onPause() {
121 | debug("onPause()")
122 |
123 | unregisterReceiver(batteryReceiver)
124 |
125 | super.onPause()
126 | }
127 |
128 | override fun onResume() {
129 | debug("onResume()")
130 |
131 | super.onResume()
132 |
133 | registerReceiver(batteryReceiver, IntentFilter(batteryDataResp), RECEIVER_NOT_EXPORTED)
134 | sendBroadcast(Intent().setPackage(packageName).setAction(batteryDataReq))
135 | }
136 |
137 | override fun onDestroy() {
138 | debug("onDestroy()")
139 |
140 | super.onDestroy()
141 | }
142 |
143 | fun onBack(view: View) {
144 | finish()
145 | }
146 | }
147 |
--------------------------------------------------------------------------------
/app/src/main/java/dubrowgn/wattz/MainActivity.kt:
--------------------------------------------------------------------------------
1 | package dubrowgn.wattz
2 |
3 | import android.Manifest.permission
4 | import android.app.Activity
5 | import android.app.ActivityManager
6 | import android.content.BroadcastReceiver
7 | import android.content.Context
8 | import android.content.Intent
9 | import android.content.IntentFilter
10 | import android.content.pm.PackageManager
11 | import android.os.Bundle
12 | import android.util.Log
13 | import android.view.View
14 | import android.widget.TextView
15 |
16 | const val namespace = "dubrowgn.wattz"
17 | const val batteryDataReq = "$namespace.battery-data-req"
18 | const val batteryDataResp = "$namespace.battery-data-resp"
19 | const val intervalMs = 1_250L
20 | const val noteChannelId = "$namespace.status"
21 | const val noteId = 1
22 |
23 | class MainActivity : Activity() {
24 | private val batteryReceiver = BatteryDataReceiver()
25 |
26 | private lateinit var chargeLevel: TextView
27 | private lateinit var charging: TextView
28 | private lateinit var chargingSince: TextView
29 | private lateinit var current: TextView
30 | private lateinit var energy: TextView
31 | private lateinit var power: TextView
32 | private lateinit var temperature: TextView
33 | private lateinit var timeToFullCharge: TextView
34 | private lateinit var voltage: TextView
35 |
36 | private fun debug(msg: String) {
37 | Log.d(this::class.java.name, msg)
38 | }
39 |
40 | enum class Perm { Granted, Denied, NotAsked }
41 |
42 | private fun getPerm(name: String) : Perm {
43 | val settings = getSharedPreferences(settingsName, MODE_PRIVATE)
44 | val perm =
45 | if (checkSelfPermission(name) == PackageManager.PERMISSION_GRANTED)
46 | Perm.Granted
47 | else if (settings.getBoolean("${name}_ASKED", false))
48 | Perm.Denied
49 | else
50 | Perm.NotAsked
51 |
52 | debug("getPerm($name) = $perm")
53 |
54 | return perm
55 | }
56 |
57 | private fun requestPerm(name: String) {
58 | debug("requestPerm($name)")
59 | getSharedPreferences(settingsName, MODE_PRIVATE)
60 | .edit()
61 | .putBoolean("${name}_ASKED", true)
62 | .apply()
63 | requestPermissions(arrayOf(name), 0)
64 | }
65 |
66 | inner class BatteryDataReceiver : BroadcastReceiver() {
67 | override fun onReceive(context: Context?, intent: Intent?) {
68 | debug("BatteryDataReceiver.onReceive()")
69 |
70 | if (intent == null)
71 | return
72 |
73 | val ind = getString(R.string.indeterminate)
74 |
75 | chargeLevel.text = intent.getStringExtra("chargeLevel") ?: ind
76 | charging.text = intent.getStringExtra("charging") ?: ind
77 | chargingSince.text = intent.getStringExtra("chargingSince") ?: ind
78 | current.text = intent.getStringExtra("current") ?: ind
79 | energy.text = intent.getStringExtra("energy") ?: ind
80 | power.text = intent.getStringExtra("power") ?: ind
81 | temperature.text = intent.getStringExtra("temperature") ?: ind
82 | timeToFullCharge.text = intent.getStringExtra("timeToFullCharge") ?: ind
83 | voltage.text = intent.getStringExtra("voltage") ?: ind
84 | }
85 | }
86 |
87 | private fun serviceRunning(): Boolean {
88 | val serviceName = StatusService::class.java.name
89 | val activityManager = getSystemService(ACTIVITY_SERVICE) as ActivityManager
90 | for (info in activityManager.getRunningServices(Int.MAX_VALUE)) {
91 | if (info.service.className == serviceName) {
92 | return true
93 | }
94 | }
95 |
96 | return false
97 | }
98 |
99 | private fun initUi() {
100 | setContentView(R.layout.activity_main)
101 |
102 | chargeLevel = findViewById(R.id.chargeLevel)
103 | charging = findViewById(R.id.charging)
104 | chargingSince = findViewById(R.id.chargingSince)
105 | current = findViewById(R.id.current)
106 | energy = findViewById(R.id.energy)
107 | power = findViewById(R.id.power)
108 | temperature = findViewById(R.id.temperature)
109 | timeToFullCharge = findViewById(R.id.timeToFullCharge)
110 | voltage = findViewById(R.id.voltage)
111 | }
112 |
113 |
114 | private fun init() {
115 | if (getPerm(permission.POST_NOTIFICATIONS) == Perm.NotAsked)
116 | requestPerm(permission.POST_NOTIFICATIONS)
117 |
118 | if (!serviceRunning())
119 | startForegroundService(Intent(this, StatusService::class.java))
120 |
121 | initUi()
122 | }
123 |
124 | override fun onCreate(savedInstanceState: Bundle?) {
125 | debug("onCreate()")
126 |
127 | super.onCreate(savedInstanceState)
128 |
129 | init()
130 | }
131 |
132 | override fun onDestroy() {
133 | debug("onDestroy()")
134 |
135 | super.onDestroy()
136 | }
137 |
138 | override fun onPause() {
139 | debug("onPause()")
140 |
141 | unregisterReceiver(batteryReceiver)
142 |
143 | super.onPause()
144 | }
145 |
146 | override fun onResume() {
147 | debug("onResume()")
148 |
149 | super.onResume()
150 |
151 | registerReceiver(batteryReceiver, IntentFilter(batteryDataResp), RECEIVER_NOT_EXPORTED)
152 | sendBroadcast(Intent().setPackage(packageName).setAction(batteryDataReq))
153 | }
154 |
155 | fun onSettings(view: View) {
156 | startActivity(Intent(this, SettingsActivity::class.java))
157 | }
158 | }
159 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/activity_main.xml:
--------------------------------------------------------------------------------
1 |
2 |
7 |
8 |
22 |
23 |
36 |
37 |
43 |
44 |
51 |
52 |
61 |
62 |
69 |
70 |
79 |
80 |
87 |
88 |
97 |
98 |
105 |
106 |
115 |
116 |
123 |
124 |
133 |
134 |
141 |
142 |
151 |
152 |
159 |
160 |
169 |
170 |
177 |
178 |
187 |
188 |
195 |
196 |
205 |
206 |
--------------------------------------------------------------------------------
/app/src/main/java/dubrowgn/wattz/StatusService.kt:
--------------------------------------------------------------------------------
1 | package dubrowgn.wattz
2 |
3 | import android.app.*
4 | import android.content.BroadcastReceiver
5 | import android.content.Context
6 | import android.content.Intent
7 | import android.content.IntentFilter
8 | import android.graphics.*
9 | import android.graphics.drawable.Icon
10 | import android.os.IBinder
11 | import android.util.Log
12 | import java.time.LocalDateTime
13 | import java.time.ZonedDateTime
14 | import java.time.format.DateTimeFormatter
15 |
16 |
17 | class StatusService : Service() {
18 | private lateinit var battery: Battery
19 | private val dateFmt = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")
20 | private var indicatorUnits: String? = null
21 | private lateinit var noteBuilder: Notification.Builder
22 | private lateinit var noteMgr: NotificationManager
23 | private var pluggedInAt: ZonedDateTime? = null
24 | private lateinit var snapshot: BatterySnapshot
25 | private val task = PeriodicTask({ update() }, intervalMs)
26 |
27 | private fun debug(msg: String) {
28 | Log.d(this::class.java.name, msg)
29 | }
30 |
31 | private inner class MsgReceiver : BroadcastReceiver() {
32 | override fun onReceive(context: Context, intent: Intent) {
33 | when (intent.action) {
34 | batteryDataReq -> updateData()
35 | settingsUpdateInd -> {
36 | loadSettings()
37 | update()
38 | }
39 | Intent.ACTION_POWER_CONNECTED -> {
40 | pluggedInAt = ZonedDateTime.now()
41 | update()
42 | }
43 | Intent.ACTION_POWER_DISCONNECTED -> {
44 | pluggedInAt = null
45 | update()
46 | }
47 | Intent.ACTION_SCREEN_OFF -> task.stop()
48 | Intent.ACTION_SCREEN_ON -> task.start()
49 | }
50 | }
51 | }
52 |
53 | private fun loadSettings() {
54 | val settings = getSharedPreferences(settingsName, MODE_MULTI_PROCESS)
55 | battery.currentScalar = settings.getFloat("currentScalar", 1f).toDouble()
56 | battery.invertCurrent = settings.getBoolean("invertCurrent", false)
57 | indicatorUnits = settings.getString("indicatorUnits", null);
58 | }
59 |
60 | private fun init() {
61 | battery = Battery(applicationContext)
62 | snapshot = battery.snapshot()
63 |
64 | noteMgr = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
65 | noteMgr.createNotificationChannel(
66 | NotificationChannel(
67 | noteChannelId,
68 | "Power Status",
69 | NotificationManager.IMPORTANCE_DEFAULT
70 | ).apply {
71 | description = "Continuously displays current battery power consumption"
72 | }
73 | )
74 |
75 | val noteIntent = PendingIntent.getActivity(
76 | this,
77 | 0,
78 | Intent(this, MainActivity::class.java).apply {
79 | flags = Intent.FLAG_ACTIVITY_SINGLE_TOP
80 | },
81 | PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_IMMUTABLE
82 | )
83 |
84 | val ind = getString(R.string.indeterminate)
85 | noteBuilder = Notification.Builder(this, noteChannelId)
86 | .setContentTitle("Battery Draw: $ind W")
87 | .setSmallIcon(renderIcon(ind, "W"))
88 | .setContentIntent(noteIntent)
89 | .setOnlyAlertOnce(true)
90 |
91 | registerReceiver(
92 | MsgReceiver(),
93 | IntentFilter().apply {
94 | addAction(batteryDataReq)
95 | addAction(settingsUpdateInd)
96 | addAction(Intent.ACTION_POWER_CONNECTED)
97 | addAction(Intent.ACTION_POWER_DISCONNECTED)
98 | addAction(Intent.ACTION_SCREEN_OFF)
99 | addAction(Intent.ACTION_SCREEN_ON)
100 | },
101 | RECEIVER_NOT_EXPORTED,
102 | )
103 | }
104 |
105 | override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
106 | debug("onStartCommand()")
107 |
108 | super.onStartCommand(intent, flags, startId)
109 |
110 | init()
111 | loadSettings()
112 | task.start()
113 |
114 | try {
115 | startForeground(noteId, noteBuilder.build())
116 | } catch (e: Exception) {
117 | error("Failed to foreground StatusService: ${e.message}")
118 | }
119 |
120 | return START_STICKY;
121 | }
122 |
123 | override fun onDestroy() {
124 | debug("onDestroy()")
125 |
126 | super.onDestroy()
127 | }
128 |
129 | override fun onBind(intent: Intent?): IBinder? {
130 | return null
131 | }
132 |
133 | private fun renderIcon(value: String, unit: String): Icon {
134 | val density = resources.displayMetrics.density
135 | val w = (48f * density).toInt()
136 | val bitmap = Bitmap.createBitmap(w, w, Bitmap.Config.ALPHA_8)
137 | val canvas = Canvas(bitmap)
138 |
139 | val textSize = 28f * density
140 | val paint = Paint()
141 | paint.textSize = textSize
142 | paint.typeface = Typeface.DEFAULT_BOLD
143 | paint.style = Paint.Style.FILL
144 | paint.color = Color.WHITE
145 | paint.textAlign = Paint.Align.CENTER
146 |
147 | canvas.drawText(value, w / 2f, w / 2f, paint)
148 | canvas.drawText(unit, w / 2f, w.toFloat(), paint)
149 |
150 | return Icon.createWithBitmap(bitmap)
151 | }
152 |
153 | private fun updateData() {
154 | val plugType = snapshot.plugType?.name?.lowercase()
155 | val indeterminate = getString(R.string.indeterminate)
156 | val fullyCharged = getString(R.string.fullyCharged)
157 | val no = getString(R.string.no)
158 | val yes = getString(R.string.yes)
159 |
160 | val intent = Intent()
161 | .setPackage(packageName)
162 | .setAction(batteryDataResp)
163 | .putExtra("charging",
164 | when (snapshot.charging) {
165 | true -> if (plugType == null) yes else "$yes ($plugType)"
166 | false -> no
167 | }
168 | )
169 | .putExtra("chargeLevel", fmt(snapshot.levelPercent) + "%")
170 | .putExtra("chargingSince",
171 | when (val pluggedInAt = pluggedInAt) {
172 | null -> indeterminate
173 | else -> LocalDateTime
174 | .ofInstant(pluggedInAt.toInstant(), pluggedInAt.zone)
175 | .format(dateFmt)
176 | }
177 | )
178 | .putExtra("current", fmt(snapshot.amps) + "A")
179 | .putExtra("energy",
180 | "${fmt(snapshot.energyWattHours)}Wh (${fmt(snapshot.energyAmpHours)}Ah)"
181 | )
182 | .putExtra("power", fmt(snapshot.watts) + "W")
183 | .putExtra("temperature", fmt(snapshot.celsius) + "°C")
184 | .putExtra("timeToFullCharge",
185 | when (val seconds = snapshot.secondsUntilCharged) {
186 | null -> indeterminate
187 | 0.0 -> fullyCharged
188 | else -> fmtSeconds(seconds)
189 | }
190 | )
191 | .putExtra("voltage", fmt(snapshot.volts) + "V")
192 |
193 | applicationContext.sendBroadcast(intent)
194 | }
195 |
196 | private fun update() {
197 | debug("update()")
198 |
199 | snapshot = battery.snapshot()
200 |
201 | val txtLabel = when (indicatorUnits) {
202 | "A" -> getString(R.string.current)
203 | "Ah" -> getString(R.string.energy)
204 | "C" -> getString(R.string.temperature)
205 | "V" -> getString(R.string.voltage)
206 | "Wh" -> getString(R.string.energy)
207 | "%" -> getString(R.string.chargeLevel)
208 | else -> getString(R.string.power)
209 | }
210 | val txtValue = fmt( when (indicatorUnits) {
211 | "A" -> snapshot.amps
212 | "Ah" -> snapshot.energyAmpHours
213 | "C" -> snapshot.celsius
214 | "V" -> snapshot.volts
215 | "Wh" -> snapshot.energyWattHours
216 | "%" -> snapshot.levelPercent
217 | else -> snapshot.watts
218 | })
219 | val txtUnits = when (indicatorUnits) {
220 | "C" -> "°C"
221 | else -> indicatorUnits ?: "W"
222 | }
223 |
224 | noteBuilder
225 | .setContentTitle("${getString(R.string.battery)} ${txtLabel}: ${txtValue}${txtUnits}")
226 | .setSmallIcon(renderIcon(txtValue, txtUnits))
227 |
228 | noteBuilder.setContentText(
229 | when(val seconds = snapshot.secondsUntilCharged) {
230 | null -> ""
231 | 0.0 -> "fully charged"
232 | else -> "${fmtSeconds(seconds)} until full charge"
233 | }
234 | )
235 |
236 | noteMgr.notify(noteId, noteBuilder.build())
237 |
238 | updateData()
239 | }
240 | }
241 |
--------------------------------------------------------------------------------
/gradlew:
--------------------------------------------------------------------------------
1 | #!/bin/sh
2 |
3 | #
4 | # Copyright © 2015-2021 the original authors.
5 | #
6 | # Licensed under the Apache License, Version 2.0 (the "License");
7 | # you may not use this file except in compliance with the License.
8 | # You may obtain a copy of the License at
9 | #
10 | # https://www.apache.org/licenses/LICENSE-2.0
11 | #
12 | # Unless required by applicable law or agreed to in writing, software
13 | # distributed under the License is distributed on an "AS IS" BASIS,
14 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 | # See the License for the specific language governing permissions and
16 | # limitations under the License.
17 | #
18 |
19 | ##############################################################################
20 | #
21 | # Gradle start up script for POSIX generated by Gradle.
22 | #
23 | # Important for running:
24 | #
25 | # (1) You need a POSIX-compliant shell to run this script. If your /bin/sh is
26 | # noncompliant, but you have some other compliant shell such as ksh or
27 | # bash, then to run this script, type that shell name before the whole
28 | # command line, like:
29 | #
30 | # ksh Gradle
31 | #
32 | # Busybox and similar reduced shells will NOT work, because this script
33 | # requires all of these POSIX shell features:
34 | # * functions;
35 | # * expansions «$var», «${var}», «${var:-default}», «${var+SET}»,
36 | # «${var#prefix}», «${var%suffix}», and «$( cmd )»;
37 | # * compound commands having a testable exit status, especially «case»;
38 | # * various built-in commands including «command», «set», and «ulimit».
39 | #
40 | # Important for patching:
41 | #
42 | # (2) This script targets any POSIX shell, so it avoids extensions provided
43 | # by Bash, Ksh, etc; in particular arrays are avoided.
44 | #
45 | # The "traditional" practice of packing multiple parameters into a
46 | # space-separated string is a well documented source of bugs and security
47 | # problems, so this is (mostly) avoided, by progressively accumulating
48 | # options in "$@", and eventually passing that to Java.
49 | #
50 | # Where the inherited environment variables (DEFAULT_JVM_OPTS, JAVA_OPTS,
51 | # and GRADLE_OPTS) rely on word-splitting, this is performed explicitly;
52 | # see the in-line comments for details.
53 | #
54 | # There are tweaks for specific operating systems such as AIX, CygWin,
55 | # Darwin, MinGW, and NonStop.
56 | #
57 | # (3) This script is generated from the Groovy template
58 | # https://github.com/gradle/gradle/blob/HEAD/subprojects/plugins/src/main/resources/org/gradle/api/internal/plugins/unixStartScript.txt
59 | # within the Gradle project.
60 | #
61 | # You can find Gradle at https://github.com/gradle/gradle/.
62 | #
63 | ##############################################################################
64 |
65 | # Attempt to set APP_HOME
66 |
67 | # Resolve links: $0 may be a link
68 | app_path=$0
69 |
70 | # Need this for daisy-chained symlinks.
71 | while
72 | APP_HOME=${app_path%"${app_path##*/}"} # leaves a trailing /; empty if no leading path
73 | [ -h "$app_path" ]
74 | do
75 | ls=$( ls -ld "$app_path" )
76 | link=${ls#*' -> '}
77 | case $link in #(
78 | /*) app_path=$link ;; #(
79 | *) app_path=$APP_HOME$link ;;
80 | esac
81 | done
82 |
83 | # This is normally unused
84 | # shellcheck disable=SC2034
85 | APP_BASE_NAME=${0##*/}
86 | APP_HOME=$( cd "${APP_HOME:-./}" && pwd -P ) || exit
87 |
88 | # Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
89 | DEFAULT_JVM_OPTS='"-Xmx64m" "-Xms64m"'
90 |
91 | # Use the maximum available, or set MAX_FD != -1 to use that value.
92 | MAX_FD=maximum
93 |
94 | warn () {
95 | echo "$*"
96 | } >&2
97 |
98 | die () {
99 | echo
100 | echo "$*"
101 | echo
102 | exit 1
103 | } >&2
104 |
105 | # OS specific support (must be 'true' or 'false').
106 | cygwin=false
107 | msys=false
108 | darwin=false
109 | nonstop=false
110 | case "$( uname )" in #(
111 | CYGWIN* ) cygwin=true ;; #(
112 | Darwin* ) darwin=true ;; #(
113 | MSYS* | MINGW* ) msys=true ;; #(
114 | NONSTOP* ) nonstop=true ;;
115 | esac
116 |
117 | CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar
118 |
119 |
120 | # Determine the Java command to use to start the JVM.
121 | if [ -n "$JAVA_HOME" ] ; then
122 | if [ -x "$JAVA_HOME/jre/sh/java" ] ; then
123 | # IBM's JDK on AIX uses strange locations for the executables
124 | JAVACMD=$JAVA_HOME/jre/sh/java
125 | else
126 | JAVACMD=$JAVA_HOME/bin/java
127 | fi
128 | if [ ! -x "$JAVACMD" ] ; then
129 | die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME
130 |
131 | Please set the JAVA_HOME variable in your environment to match the
132 | location of your Java installation."
133 | fi
134 | else
135 | JAVACMD=java
136 | which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
137 |
138 | Please set the JAVA_HOME variable in your environment to match the
139 | location of your Java installation."
140 | fi
141 |
142 | # Increase the maximum file descriptors if we can.
143 | if ! "$cygwin" && ! "$darwin" && ! "$nonstop" ; then
144 | case $MAX_FD in #(
145 | max*)
146 | # In POSIX sh, ulimit -H is undefined. That's why the result is checked to see if it worked.
147 | # shellcheck disable=SC3045
148 | MAX_FD=$( ulimit -H -n ) ||
149 | warn "Could not query maximum file descriptor limit"
150 | esac
151 | case $MAX_FD in #(
152 | '' | soft) :;; #(
153 | *)
154 | # In POSIX sh, ulimit -n is undefined. That's why the result is checked to see if it worked.
155 | # shellcheck disable=SC3045
156 | ulimit -n "$MAX_FD" ||
157 | warn "Could not set maximum file descriptor limit to $MAX_FD"
158 | esac
159 | fi
160 |
161 | # Collect all arguments for the java command, stacking in reverse order:
162 | # * args from the command line
163 | # * the main class name
164 | # * -classpath
165 | # * -D...appname settings
166 | # * --module-path (only if needed)
167 | # * DEFAULT_JVM_OPTS, JAVA_OPTS, and GRADLE_OPTS environment variables.
168 |
169 | # For Cygwin or MSYS, switch paths to Windows format before running java
170 | if "$cygwin" || "$msys" ; then
171 | APP_HOME=$( cygpath --path --mixed "$APP_HOME" )
172 | CLASSPATH=$( cygpath --path --mixed "$CLASSPATH" )
173 |
174 | JAVACMD=$( cygpath --unix "$JAVACMD" )
175 |
176 | # Now convert the arguments - kludge to limit ourselves to /bin/sh
177 | for arg do
178 | if
179 | case $arg in #(
180 | -*) false ;; # don't mess with options #(
181 | /?*) t=${arg#/} t=/${t%%/*} # looks like a POSIX filepath
182 | [ -e "$t" ] ;; #(
183 | *) false ;;
184 | esac
185 | then
186 | arg=$( cygpath --path --ignore --mixed "$arg" )
187 | fi
188 | # Roll the args list around exactly as many times as the number of
189 | # args, so each arg winds up back in the position where it started, but
190 | # possibly modified.
191 | #
192 | # NB: a `for` loop captures its iteration list before it begins, so
193 | # changing the positional parameters here affects neither the number of
194 | # iterations, nor the values presented in `arg`.
195 | shift # remove old arg
196 | set -- "$@" "$arg" # push replacement arg
197 | done
198 | fi
199 |
200 | # Collect all arguments for the java command;
201 | # * $DEFAULT_JVM_OPTS, $JAVA_OPTS, and $GRADLE_OPTS can contain fragments of
202 | # shell script including quotes and variable substitutions, so put them in
203 | # double quotes to make sure that they get re-expanded; and
204 | # * put everything else in single quotes, so that it's not re-expanded.
205 |
206 | set -- \
207 | "-Dorg.gradle.appname=$APP_BASE_NAME" \
208 | -classpath "$CLASSPATH" \
209 | org.gradle.wrapper.GradleWrapperMain \
210 | "$@"
211 |
212 | # Stop when "xargs" is not available.
213 | if ! command -v xargs >/dev/null 2>&1
214 | then
215 | die "xargs is not available"
216 | fi
217 |
218 | # Use "xargs" to parse quoted args.
219 | #
220 | # With -n1 it outputs one arg per line, with the quotes and backslashes removed.
221 | #
222 | # In Bash we could simply go:
223 | #
224 | # readarray ARGS < <( xargs -n1 <<<"$var" ) &&
225 | # set -- "${ARGS[@]}" "$@"
226 | #
227 | # but POSIX shell has neither arrays nor command substitution, so instead we
228 | # post-process each arg (as a line of input to sed) to backslash-escape any
229 | # character that might be a shell metacharacter, then use eval to reverse
230 | # that process (while maintaining the separation between arguments), and wrap
231 | # the whole thing up as a single "set" statement.
232 | #
233 | # This will of course break if any of these variables contains a newline or
234 | # an unmatched quote.
235 | #
236 |
237 | eval "set -- $(
238 | printf '%s\n' "$DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS" |
239 | xargs -n1 |
240 | sed ' s~[^-[:alnum:]+,./:=@_]~\\&~g; ' |
241 | tr '\n' ' '
242 | )" '"$@"'
243 |
244 | exec "$JAVACMD" "$@"
245 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/activity_settings.xml:
--------------------------------------------------------------------------------
1 |
2 |
8 |
9 |
13 |
14 |
28 |
29 |
42 |
43 |
55 |
56 |
63 |
64 |
73 |
74 |
84 |
85 |
96 |
97 |
107 |
108 |
118 |
119 |
129 |
130 |
140 |
141 |
151 |
152 |
153 |
154 |
166 |
167 |
177 |
178 |
185 |
186 |
195 |
196 |
203 |
204 |
213 |
214 |
221 |
222 |
232 |
233 |
243 |
244 |
251 |
252 |
259 |
260 |
267 |
268 |
269 |
276 |
277 |
287 |
288 |
298 |
299 |
300 |
--------------------------------------------------------------------------------