-
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor: cleaned up the battery icon.
- Loading branch information
1 parent
dcee7d6
commit c6ee92e
Showing
10 changed files
with
384 additions
and
270 deletions.
There are no files selected for viewing
96 changes: 40 additions & 56 deletions
96
app/src/main/java/com/github/droidworksstudio/mlauncher/helper/BatteryReceiver.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,82 +1,66 @@ | ||
package com.github.droidworksstudio.mlauncher.helper | ||
|
||
import android.annotation.SuppressLint | ||
import android.app.Activity | ||
import android.content.BroadcastReceiver | ||
import android.content.Context | ||
import android.content.Intent | ||
import android.os.BatteryManager | ||
import android.widget.TextView | ||
import androidx.appcompat.widget.AppCompatTextView | ||
import androidx.core.content.ContextCompat | ||
import com.github.droidworksstudio.mlauncher.R | ||
import com.github.droidworksstudio.mlauncher.data.Prefs | ||
|
||
class BatteryReceiver : BroadcastReceiver() { | ||
|
||
private lateinit var prefs: Prefs | ||
|
||
/* get current battery percentage */ | ||
private fun batteryPercentage(intent: Intent): Int { | ||
val scale = intent.getIntExtra(BatteryManager.EXTRA_SCALE, -1) | ||
val level = intent.getIntExtra(BatteryManager.EXTRA_LEVEL, -1) | ||
val percentage = level / scale.toFloat() | ||
return (percentage * 100).toInt() | ||
} | ||
|
||
/* get current charging status */ | ||
private fun chargingStatus(intent: Intent): Int { | ||
return intent.getIntExtra(BatteryManager.EXTRA_STATUS, -1) | ||
} | ||
|
||
override fun onReceive(context: Context, intent: Intent) { | ||
prefs = Prefs(context) | ||
/* set battery percentage value to the circular progress bar */ | ||
val batteryLevel = batteryPercentage(intent) | ||
val level: Int = intent.getIntExtra(BatteryManager.EXTRA_LEVEL, -1) | ||
val scale: Int = intent.getIntExtra(BatteryManager.EXTRA_SCALE, -1) | ||
|
||
val contextBattery = context as? Activity | ||
val batteryTextView = (contextBattery)?.findViewById<AppCompatTextView>(R.id.battery) | ||
|
||
val batteryLevel = level * 100 / scale.toFloat() | ||
|
||
/* progress bar animation */ | ||
if ( | ||
chargingStatus(intent) == BatteryManager.BATTERY_STATUS_DISCHARGING || | ||
chargingStatus(intent) == BatteryManager.BATTERY_STATUS_NOT_CHARGING | ||
) { | ||
updateBatteryStatus(context, batteryLevel, isCharging = false) | ||
} else if ( | ||
chargingStatus(intent) == BatteryManager.BATTERY_STATUS_CHARGING || | ||
chargingStatus(intent) == BatteryManager.BATTERY_STATUS_FULL | ||
) { | ||
updateBatteryStatus(context, batteryLevel, isCharging = true) | ||
val batteryDrawable = when { | ||
batteryLevel >= 76 -> ContextCompat.getDrawable( | ||
context, | ||
R.drawable.app_battery100 | ||
) | ||
|
||
batteryLevel >= 51 -> ContextCompat.getDrawable( | ||
context, | ||
R.drawable.app_battery75 | ||
) | ||
|
||
batteryLevel >= 26 -> ContextCompat.getDrawable( | ||
context, | ||
R.drawable.app_battery50 | ||
) | ||
|
||
else -> ContextCompat.getDrawable(context, R.drawable.app_battery25) | ||
} | ||
} | ||
|
||
@SuppressLint("SetTextI18n") | ||
private fun updateBatteryStatus(context: Context, batteryLevel: Int, isCharging: Boolean) { | ||
if (!prefs.showBattery) return | ||
val icon: String | ||
if (isCharging) { | ||
icon = "\uDB80\uDC84" | ||
} else { | ||
icon = when (batteryLevel) { | ||
in 0..10 -> "\uDB80\uDC7A" | ||
in 11..20 -> "\uDB80\uDC7B" | ||
in 21..30 -> "\uDB80\uDC7C" | ||
in 31..40 -> "\uDB80\uDC7D" | ||
in 41..50 -> "\uDB80\uDC7E" | ||
in 51..60 -> "\uDB80\uDC7F" | ||
in 61..70 -> "\uDB80\uDC80" | ||
in 71..80 -> "\uDB80\uDC81" | ||
in 81..90 -> "\uDB80\uDC82" | ||
else -> "\uDB80\uDC79" | ||
batteryDrawable?.let { | ||
// Resize the drawable to match the text size | ||
val textSize = batteryTextView?.textSize?.toInt() | ||
if (prefs.showBatteryIcon) { | ||
textSize?.let { bottom -> it.setBounds(0, 0, textSize, bottom) } | ||
batteryTextView?.setCompoundDrawables(it, null, null, null) | ||
} else { | ||
it.setBounds(0, 0, 0, 0) | ||
batteryTextView?.setCompoundDrawables(null, null, null, null) | ||
} | ||
} | ||
|
||
val contextBattery = context as? Activity | ||
val iconView = (contextBattery)?.findViewById<TextView>(R.id.batteryIcon) | ||
iconView?.text = icon | ||
|
||
val textView = (contextBattery)?.findViewById<TextView>(R.id.batteryText) | ||
textView?.text = "$batteryLevel%" | ||
textView?.setOnClickListener { | ||
val powerUsageIntent = Intent(Intent.ACTION_POWER_USAGE_SUMMARY) | ||
context.startActivity(powerUsageIntent) | ||
var batteryLevelInt = batteryLevel.toInt() | ||
batteryTextView?.text = buildString { | ||
append(batteryLevelInt) | ||
append("%") | ||
} | ||
|
||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
<vector xmlns:android="http://schemas.android.com/apk/res/android" | ||
android:width="24dp" | ||
android:height="24dp" | ||
android:viewportWidth="24" | ||
android:viewportHeight="24"> | ||
<path | ||
android:pathData="M16.542,4.392L14.981,4.392L14.981,2.844A0.499,0.496 0,0 0,14.484 2.35L10.468,2.35A0.5,0.497 0,0 0,9.971 2.844L9.971,4.392L8.408,4.392A1.577,1.567 0,0 0,7.29 4.852l-0.028,0.031a1.574,1.564 0,0 0,-0.437 1.075L6.825,19.641a1.588,1.578 0,0 0,1.583 1.574h8.135a1.574,1.564 0,0 0,1.117 -0.461v0a1.577,1.567 0,0 0,0.463 -1.112L18.122,5.966A1.588,1.578 0,0 0,16.539 4.392ZM8.408,5.313h8.135a0.653,0.649 0,0 1,0.65 0.646L17.193,19.641a0.647,0.643 0,0 1,-0.192 0.461v0A0.65,0.646 0,0 1,16.538 20.291L8.408,20.291A0.653,0.649 0,0 1,7.751 19.641L7.751,5.966A0.649,0.645 0,0 1,7.924 5.526l0.019,-0.017A0.65,0.646 0,0 1,8.406 5.319Z" | ||
android:strokeWidth="0.153984" | ||
android:fillColor="@color/white" /> | ||
|
||
<path | ||
android:pathData="M16.193,9.372V6.712A0.406,0.344 0,0 0,15.792 6.372H9.157A0.411,0.348 0,0 0,8.754 6.712v2.66z" | ||
android:fillColor="@color/green" | ||
android:fillType="evenOdd" /> | ||
|
||
<path | ||
android:pathData="M16.193,9.6l-7.44,-0l0,3l7.44,-0z" | ||
android:fillColor="@color/green" | ||
android:fillType="evenOdd" /> | ||
|
||
<path | ||
android:pathData="M16.193,12.839l-7.44,-0l0,3l7.44,-0z" | ||
android:fillColor="@color/green" | ||
android:fillType="evenOdd" /> | ||
|
||
<path | ||
android:pathData="M16.19,16.065H8.751v2.666a0.408,0.338 0,0 0,0.403 0.334H15.795a0.403,0.334 0,0 0,0.402 -0.334z" | ||
android:fillColor="@color/green" | ||
android:fillType="evenOdd" /> | ||
</vector> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
<vector xmlns:android="http://schemas.android.com/apk/res/android" | ||
android:width="24dp" | ||
android:height="24dp" | ||
android:viewportWidth="24" | ||
android:viewportHeight="24"> | ||
<path | ||
android:pathData="M16.542,4.392L14.981,4.392L14.981,2.844A0.499,0.496 0,0 0,14.484 2.35L10.468,2.35A0.5,0.497 0,0 0,9.971 2.844L9.971,4.392L8.408,4.392A1.577,1.567 0,0 0,7.29 4.852l-0.028,0.031a1.574,1.564 0,0 0,-0.437 1.075L6.825,19.641a1.588,1.578 0,0 0,1.583 1.574h8.135a1.574,1.564 0,0 0,1.117 -0.461v0a1.577,1.567 0,0 0,0.463 -1.112L18.122,5.966A1.588,1.578 0,0 0,16.539 4.392ZM8.408,5.313h8.135a0.653,0.649 0,0 1,0.65 0.646L17.193,19.641a0.647,0.643 0,0 1,-0.192 0.461v0A0.65,0.646 0,0 1,16.538 20.291L8.408,20.291A0.653,0.649 0,0 1,7.751 19.641L7.751,5.966A0.649,0.645 0,0 1,7.924 5.526l0.019,-0.017A0.65,0.646 0,0 1,8.406 5.319Z" | ||
android:strokeWidth="0.153984" | ||
android:fillColor="@color/white" /> | ||
|
||
<path | ||
android:pathData="M16.19,16.065H8.751v2.666a0.408,0.338 0,0 0,0.403 0.334H15.795a0.403,0.334 0,0 0,0.402 -0.334z" | ||
android:fillColor="@color/red" | ||
android:fillType="evenOdd" /> | ||
</vector> |
Oops, something went wrong.