Skip to content

Commit

Permalink
Refactor: cleaned up the battery icon.
Browse files Browse the repository at this point in the history
  • Loading branch information
CreativeCodeCat committed Nov 21, 2024
1 parent dcee7d6 commit c6ee92e
Show file tree
Hide file tree
Showing 10 changed files with 384 additions and 270 deletions.
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("%")
}

}
}

Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ import androidx.biometric.BiometricManager.Authenticators.BIOMETRIC_WEAK
import androidx.biometric.BiometricManager.Authenticators.DEVICE_CREDENTIAL
import androidx.biometric.BiometricPrompt
import androidx.core.content.ContextCompat
import androidx.core.content.res.ResourcesCompat
import androidx.core.os.bundleOf
import androidx.core.view.children
import androidx.fragment.app.Fragment
Expand Down Expand Up @@ -81,6 +80,7 @@ class HomeFragment : Fragment(), View.OnClickListener, View.OnLongClickListener

private var _binding: FragmentHomeBinding? = null
private val binding get() = _binding!!

// Instantiate Colors object
private val colors = Colors()

Expand Down Expand Up @@ -135,7 +135,10 @@ class HomeFragment : Fragment(), View.OnClickListener, View.OnLongClickListener

val timezone = prefs.language.timezone()
val is24HourFormat = DateFormat.is24HourFormat(requireContext())
val best12 = DateFormat.getBestDateTimePattern(timezone, if (prefs.showTimeFormat) "hhmma" else "hhmm").let {
val best12 = DateFormat.getBestDateTimePattern(
timezone,
if (prefs.showTimeFormat) "hhmma" else "hhmm"
).let {
if (!prefs.showTimeFormat) it.removeSuffix(" a") else it
}
Log.d("currentDateTime", best12)
Expand All @@ -150,35 +153,26 @@ class HomeFragment : Fragment(), View.OnClickListener, View.OnLongClickListener

binding.clock.textSize = prefs.clockSize.toFloat()
binding.date.textSize = prefs.dateSize.toFloat()
binding.batteryText.textSize = prefs.batterySize.toFloat()
binding.battery.textSize = prefs.batterySize.toFloat()
binding.homeScreenPager.textSize = prefs.appSize.toFloat()

if(prefs.showBatteryIcon) {
binding.batteryIcon.visibility = View.VISIBLE
val typeface = ResourcesCompat.getFont(requireActivity(), R.font.roboto)
binding.batteryIcon.typeface = typeface
binding.batteryIcon.textSize = prefs.batterySize.toFloat()
}

if (prefs.showBattery) {
binding.batteryLayout.visibility = View.VISIBLE
binding.battery.visibility = View.VISIBLE
}

binding.mainLayout.setBackgroundColor(colors.background(requireContext(), prefs))
if (prefs.followAccentColors) {
val fontColor = getHexFontColor(requireContext(), prefs)
binding.clock.setTextColor(fontColor)
binding.date.setTextColor(fontColor)
binding.batteryIcon.setTextColor(fontColor)
binding.batteryText.setTextColor(fontColor)
binding.battery.setTextColor(fontColor)
binding.setTotalScreenTime.setTextColor(fontColor)
binding.setDefaultLauncher.setTextColor(fontColor)
binding.homeScreenPager.setTextColor(fontColor)
} else {
} else {
binding.clock.setTextColor(colors.accents(requireContext(), prefs, 1))
binding.date.setTextColor(colors.accents(requireContext(), prefs, 1))
binding.batteryIcon.setTextColor(colors.accents(requireContext(), prefs, 1))
binding.batteryText.setTextColor(colors.accents(requireContext(), prefs, 1))
binding.battery.setTextColor(colors.accents(requireContext(), prefs, 1))
binding.setTotalScreenTime.setTextColor(colors.accents(requireContext(), prefs, 2))
binding.setDefaultLauncher.setTextColor(colors.accents(requireContext(), prefs, 2))
binding.homeScreenPager.setTextColor(colors.accents(requireContext(), prefs, 2))
Expand Down Expand Up @@ -252,7 +246,7 @@ class HomeFragment : Fragment(), View.OnClickListener, View.OnLongClickListener
viewModel.resetDefaultLauncherApp(requireContext())
}

R.id.batteryLayout -> {
R.id.battery -> {
openBatteryUsage()
}

Expand Down Expand Up @@ -290,7 +284,7 @@ class HomeFragment : Fragment(), View.OnClickListener, View.OnLongClickListener
binding.date.setOnClickListener(this)
binding.setTotalScreenTime.setOnClickListener(this)
binding.setDefaultLauncher.setOnClickListener(this)
binding.batteryLayout.setOnClickListener(this)
binding.battery.setOnClickListener(this)
}

@RequiresApi(Build.VERSION_CODES.Q)
Expand Down Expand Up @@ -355,6 +349,7 @@ class HomeFragment : Fragment(), View.OnClickListener, View.OnLongClickListener
}
}
}

@SuppressLint("WrongConstant", "PrivateApi")
private fun expandNotificationDrawer(context: Context) {
try {
Expand Down Expand Up @@ -388,6 +383,7 @@ class HomeFragment : Fragment(), View.OnClickListener, View.OnLongClickListener
launchApp(prefs.appShortSwipeUp)
else openDialerApp(requireContext())
}

private fun openSwipeDownApp() {
if (prefs.appShortSwipeDown.activityPackage.isNotEmpty())
launchApp(prefs.appShortSwipeDown)
Expand All @@ -411,6 +407,7 @@ class HomeFragment : Fragment(), View.OnClickListener, View.OnLongClickListener
launchApp(prefs.appLongSwipeUp)
else openDialerApp(requireContext())
}

private fun openLongSwipeDownApp() {
if (prefs.appLongSwipeDown.activityPackage.isNotEmpty())
launchApp(prefs.appLongSwipeDown)
Expand Down Expand Up @@ -521,14 +518,16 @@ class HomeFragment : Fragment(), View.OnClickListener, View.OnLongClickListener
startTime = System.currentTimeMillis()
longSwipeTriggered = false // Reset the flag
}

MotionEvent.ACTION_UP -> {
val endX = motionEvent.x
val endY = motionEvent.y
val endTime = System.currentTimeMillis()
val duration = endTime - startTime
val deltaX = endX - startX
val deltaY = endY - startY
val distance = sqrt((deltaX * deltaX + deltaY * deltaY).toDouble()).toFloat()
val distance =
sqrt((deltaX * deltaX + deltaY * deltaY).toDouble()).toFloat()
val direction: String = if (abs(deltaX) < abs(deltaY)) {
if (deltaY < 0) "up" else "down"
} else {
Expand Down Expand Up @@ -613,14 +612,16 @@ class HomeFragment : Fragment(), View.OnClickListener, View.OnLongClickListener
startTime = System.currentTimeMillis()
longSwipeTriggered = false // Reset the flag
}

MotionEvent.ACTION_UP -> {
val endX = motionEvent.x
val endY = motionEvent.y
val endTime = System.currentTimeMillis()
val duration = endTime - startTime
val deltaX = endX - startX
val deltaY = endY - startY
val distance = sqrt((deltaX * deltaX + deltaY * deltaY).toDouble()).toFloat()
val distance =
sqrt((deltaX * deltaX + deltaY * deltaY).toDouble()).toFloat()
val direction: String = if (abs(deltaX) < abs(deltaY)) {
if (deltaY < 0) "up" else "down"
} else {
Expand Down Expand Up @@ -692,14 +693,17 @@ class HomeFragment : Fragment(), View.OnClickListener, View.OnLongClickListener
Action.OpenApp -> openLongSwipeUpApp()
else -> handleOtherAction(action)
}

"down" -> when (val action = prefs.longSwipeDownAction) {
Action.OpenApp -> openLongSwipeDownApp()
else -> handleOtherAction(action)
}

"left" -> when (val action = prefs.longSwipeLeftAction) {
Action.OpenApp -> openLongSwipeLeftApp()
else -> handleOtherAction(action)
}

"right" -> when (val action = prefs.longSwipeRightAction) {
Action.OpenApp -> openLongSwipeRightApp()
else -> handleOtherAction(action)
Expand Down Expand Up @@ -728,7 +732,8 @@ class HomeFragment : Fragment(), View.OnClickListener, View.OnLongClickListener
}

// Create existingAppView
val existingAppView = layoutInflater.inflate(R.layout.home_app_button, null) as TextView
val existingAppView =
layoutInflater.inflate(R.layout.home_app_button, null) as TextView
existingAppView.apply {
// Set properties of existingAppView
textSize = prefs.appSize.toFloat()
Expand Down Expand Up @@ -758,7 +763,12 @@ class HomeFragment : Fragment(), View.OnClickListener, View.OnLongClickListener
// Set properties of newAppView
textSize = prefs.appSize.toFloat() / 1.5f
id = i
text = formatMillisToHMS(getUsageStats(context, prefs.getHomeAppModel(i).activityPackage))
text = formatMillisToHMS(
getUsageStats(
context,
prefs.getHomeAppModel(i).activityPackage
)
)
setOnTouchListener(getHomeAppsGestureListener(context, this))
setOnClickListener(this@HomeFragment)
if (!prefs.extendHomeAppsArea) {
Expand All @@ -772,7 +782,7 @@ class HomeFragment : Fragment(), View.OnClickListener, View.OnLongClickListener
if (prefs.followAccentColors) {
val fontColor = getHexFontColor(requireContext(), prefs)
setTextColor(fontColor)
} else {
} else {
setTextColor(colors.accents(requireContext(), prefs, 3))
}
}
Expand Down Expand Up @@ -895,17 +905,18 @@ class HomeFragment : Fragment(), View.OnClickListener, View.OnLongClickListener

// Set the text for the page selector corresponding to each page
binding.homeScreenPager.text = pageSelectorTexts.joinToString(" ")
if (prefs.homePagesNum > 1 && prefs.homePagerOn) binding.homeScreenPager.visibility = View.VISIBLE
if (prefs.homePagesNum > 1 && prefs.homePagerOn) binding.homeScreenPager.visibility =
View.VISIBLE
}

private fun handleSwipeLeft(totalPages:Int) {
private fun handleSwipeLeft(totalPages: Int) {
if (currentPage < totalPages - 1) {
currentPage++
updateAppsVisibility(totalPages)
}
}

private fun handleSwipeRight(totalPages:Int) {
private fun handleSwipeRight(totalPages: Int) {
if (currentPage > 0) {
currentPage--
updateAppsVisibility(totalPages)
Expand Down
30 changes: 30 additions & 0 deletions app/src/main/res/drawable/app_battery100.xml
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>
15 changes: 15 additions & 0 deletions app/src/main/res/drawable/app_battery25.xml
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>
Loading

0 comments on commit c6ee92e

Please sign in to comment.