Skip to content

Commit c6ee92e

Browse files
Refactor: cleaned up the battery icon.
1 parent dcee7d6 commit c6ee92e

File tree

10 files changed

+384
-270
lines changed

10 files changed

+384
-270
lines changed
Lines changed: 40 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -1,82 +1,66 @@
11
package com.github.droidworksstudio.mlauncher.helper
22

3-
import android.annotation.SuppressLint
43
import android.app.Activity
54
import android.content.BroadcastReceiver
65
import android.content.Context
76
import android.content.Intent
87
import android.os.BatteryManager
9-
import android.widget.TextView
8+
import androidx.appcompat.widget.AppCompatTextView
9+
import androidx.core.content.ContextCompat
1010
import com.github.droidworksstudio.mlauncher.R
1111
import com.github.droidworksstudio.mlauncher.data.Prefs
1212

1313
class BatteryReceiver : BroadcastReceiver() {
1414

1515
private lateinit var prefs: Prefs
1616

17-
/* get current battery percentage */
18-
private fun batteryPercentage(intent: Intent): Int {
19-
val scale = intent.getIntExtra(BatteryManager.EXTRA_SCALE, -1)
20-
val level = intent.getIntExtra(BatteryManager.EXTRA_LEVEL, -1)
21-
val percentage = level / scale.toFloat()
22-
return (percentage * 100).toInt()
23-
}
24-
25-
/* get current charging status */
26-
private fun chargingStatus(intent: Intent): Int {
27-
return intent.getIntExtra(BatteryManager.EXTRA_STATUS, -1)
28-
}
29-
3017
override fun onReceive(context: Context, intent: Intent) {
3118
prefs = Prefs(context)
32-
/* set battery percentage value to the circular progress bar */
33-
val batteryLevel = batteryPercentage(intent)
19+
val level: Int = intent.getIntExtra(BatteryManager.EXTRA_LEVEL, -1)
20+
val scale: Int = intent.getIntExtra(BatteryManager.EXTRA_SCALE, -1)
21+
22+
val contextBattery = context as? Activity
23+
val batteryTextView = (contextBattery)?.findViewById<AppCompatTextView>(R.id.battery)
24+
25+
val batteryLevel = level * 100 / scale.toFloat()
3426

35-
/* progress bar animation */
36-
if (
37-
chargingStatus(intent) == BatteryManager.BATTERY_STATUS_DISCHARGING ||
38-
chargingStatus(intent) == BatteryManager.BATTERY_STATUS_NOT_CHARGING
39-
) {
40-
updateBatteryStatus(context, batteryLevel, isCharging = false)
41-
} else if (
42-
chargingStatus(intent) == BatteryManager.BATTERY_STATUS_CHARGING ||
43-
chargingStatus(intent) == BatteryManager.BATTERY_STATUS_FULL
44-
) {
45-
updateBatteryStatus(context, batteryLevel, isCharging = true)
27+
val batteryDrawable = when {
28+
batteryLevel >= 76 -> ContextCompat.getDrawable(
29+
context,
30+
R.drawable.app_battery100
31+
)
32+
33+
batteryLevel >= 51 -> ContextCompat.getDrawable(
34+
context,
35+
R.drawable.app_battery75
36+
)
37+
38+
batteryLevel >= 26 -> ContextCompat.getDrawable(
39+
context,
40+
R.drawable.app_battery50
41+
)
42+
43+
else -> ContextCompat.getDrawable(context, R.drawable.app_battery25)
4644
}
47-
}
4845

49-
@SuppressLint("SetTextI18n")
50-
private fun updateBatteryStatus(context: Context, batteryLevel: Int, isCharging: Boolean) {
51-
if (!prefs.showBattery) return
52-
val icon: String
53-
if (isCharging) {
54-
icon = "\uDB80\uDC84"
55-
} else {
56-
icon = when (batteryLevel) {
57-
in 0..10 -> "\uDB80\uDC7A"
58-
in 11..20 -> "\uDB80\uDC7B"
59-
in 21..30 -> "\uDB80\uDC7C"
60-
in 31..40 -> "\uDB80\uDC7D"
61-
in 41..50 -> "\uDB80\uDC7E"
62-
in 51..60 -> "\uDB80\uDC7F"
63-
in 61..70 -> "\uDB80\uDC80"
64-
in 71..80 -> "\uDB80\uDC81"
65-
in 81..90 -> "\uDB80\uDC82"
66-
else -> "\uDB80\uDC79"
46+
batteryDrawable?.let {
47+
// Resize the drawable to match the text size
48+
val textSize = batteryTextView?.textSize?.toInt()
49+
if (prefs.showBatteryIcon) {
50+
textSize?.let { bottom -> it.setBounds(0, 0, textSize, bottom) }
51+
batteryTextView?.setCompoundDrawables(it, null, null, null)
52+
} else {
53+
it.setBounds(0, 0, 0, 0)
54+
batteryTextView?.setCompoundDrawables(null, null, null, null)
6755
}
6856
}
6957

70-
val contextBattery = context as? Activity
71-
val iconView = (contextBattery)?.findViewById<TextView>(R.id.batteryIcon)
72-
iconView?.text = icon
73-
74-
val textView = (contextBattery)?.findViewById<TextView>(R.id.batteryText)
75-
textView?.text = "$batteryLevel%"
76-
textView?.setOnClickListener {
77-
val powerUsageIntent = Intent(Intent.ACTION_POWER_USAGE_SUMMARY)
78-
context.startActivity(powerUsageIntent)
58+
var batteryLevelInt = batteryLevel.toInt()
59+
batteryTextView?.text = buildString {
60+
append(batteryLevelInt)
61+
append("%")
7962
}
63+
8064
}
8165
}
8266

app/src/main/java/com/github/droidworksstudio/mlauncher/ui/HomeFragment.kt

Lines changed: 37 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@ import androidx.biometric.BiometricManager.Authenticators.BIOMETRIC_WEAK
3030
import androidx.biometric.BiometricManager.Authenticators.DEVICE_CREDENTIAL
3131
import androidx.biometric.BiometricPrompt
3232
import androidx.core.content.ContextCompat
33-
import androidx.core.content.res.ResourcesCompat
3433
import androidx.core.os.bundleOf
3534
import androidx.core.view.children
3635
import androidx.fragment.app.Fragment
@@ -81,6 +80,7 @@ class HomeFragment : Fragment(), View.OnClickListener, View.OnLongClickListener
8180

8281
private var _binding: FragmentHomeBinding? = null
8382
private val binding get() = _binding!!
83+
8484
// Instantiate Colors object
8585
private val colors = Colors()
8686

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

136136
val timezone = prefs.language.timezone()
137137
val is24HourFormat = DateFormat.is24HourFormat(requireContext())
138-
val best12 = DateFormat.getBestDateTimePattern(timezone, if (prefs.showTimeFormat) "hhmma" else "hhmm").let {
138+
val best12 = DateFormat.getBestDateTimePattern(
139+
timezone,
140+
if (prefs.showTimeFormat) "hhmma" else "hhmm"
141+
).let {
139142
if (!prefs.showTimeFormat) it.removeSuffix(" a") else it
140143
}
141144
Log.d("currentDateTime", best12)
@@ -150,35 +153,26 @@ class HomeFragment : Fragment(), View.OnClickListener, View.OnLongClickListener
150153

151154
binding.clock.textSize = prefs.clockSize.toFloat()
152155
binding.date.textSize = prefs.dateSize.toFloat()
153-
binding.batteryText.textSize = prefs.batterySize.toFloat()
156+
binding.battery.textSize = prefs.batterySize.toFloat()
154157
binding.homeScreenPager.textSize = prefs.appSize.toFloat()
155158

156-
if(prefs.showBatteryIcon) {
157-
binding.batteryIcon.visibility = View.VISIBLE
158-
val typeface = ResourcesCompat.getFont(requireActivity(), R.font.roboto)
159-
binding.batteryIcon.typeface = typeface
160-
binding.batteryIcon.textSize = prefs.batterySize.toFloat()
161-
}
162-
163159
if (prefs.showBattery) {
164-
binding.batteryLayout.visibility = View.VISIBLE
160+
binding.battery.visibility = View.VISIBLE
165161
}
166162

167163
binding.mainLayout.setBackgroundColor(colors.background(requireContext(), prefs))
168164
if (prefs.followAccentColors) {
169165
val fontColor = getHexFontColor(requireContext(), prefs)
170166
binding.clock.setTextColor(fontColor)
171167
binding.date.setTextColor(fontColor)
172-
binding.batteryIcon.setTextColor(fontColor)
173-
binding.batteryText.setTextColor(fontColor)
168+
binding.battery.setTextColor(fontColor)
174169
binding.setTotalScreenTime.setTextColor(fontColor)
175170
binding.setDefaultLauncher.setTextColor(fontColor)
176171
binding.homeScreenPager.setTextColor(fontColor)
177-
} else {
172+
} else {
178173
binding.clock.setTextColor(colors.accents(requireContext(), prefs, 1))
179174
binding.date.setTextColor(colors.accents(requireContext(), prefs, 1))
180-
binding.batteryIcon.setTextColor(colors.accents(requireContext(), prefs, 1))
181-
binding.batteryText.setTextColor(colors.accents(requireContext(), prefs, 1))
175+
binding.battery.setTextColor(colors.accents(requireContext(), prefs, 1))
182176
binding.setTotalScreenTime.setTextColor(colors.accents(requireContext(), prefs, 2))
183177
binding.setDefaultLauncher.setTextColor(colors.accents(requireContext(), prefs, 2))
184178
binding.homeScreenPager.setTextColor(colors.accents(requireContext(), prefs, 2))
@@ -252,7 +246,7 @@ class HomeFragment : Fragment(), View.OnClickListener, View.OnLongClickListener
252246
viewModel.resetDefaultLauncherApp(requireContext())
253247
}
254248

255-
R.id.batteryLayout -> {
249+
R.id.battery -> {
256250
openBatteryUsage()
257251
}
258252

@@ -290,7 +284,7 @@ class HomeFragment : Fragment(), View.OnClickListener, View.OnLongClickListener
290284
binding.date.setOnClickListener(this)
291285
binding.setTotalScreenTime.setOnClickListener(this)
292286
binding.setDefaultLauncher.setOnClickListener(this)
293-
binding.batteryLayout.setOnClickListener(this)
287+
binding.battery.setOnClickListener(this)
294288
}
295289

296290
@RequiresApi(Build.VERSION_CODES.Q)
@@ -355,6 +349,7 @@ class HomeFragment : Fragment(), View.OnClickListener, View.OnLongClickListener
355349
}
356350
}
357351
}
352+
358353
@SuppressLint("WrongConstant", "PrivateApi")
359354
private fun expandNotificationDrawer(context: Context) {
360355
try {
@@ -388,6 +383,7 @@ class HomeFragment : Fragment(), View.OnClickListener, View.OnLongClickListener
388383
launchApp(prefs.appShortSwipeUp)
389384
else openDialerApp(requireContext())
390385
}
386+
391387
private fun openSwipeDownApp() {
392388
if (prefs.appShortSwipeDown.activityPackage.isNotEmpty())
393389
launchApp(prefs.appShortSwipeDown)
@@ -411,6 +407,7 @@ class HomeFragment : Fragment(), View.OnClickListener, View.OnLongClickListener
411407
launchApp(prefs.appLongSwipeUp)
412408
else openDialerApp(requireContext())
413409
}
410+
414411
private fun openLongSwipeDownApp() {
415412
if (prefs.appLongSwipeDown.activityPackage.isNotEmpty())
416413
launchApp(prefs.appLongSwipeDown)
@@ -521,14 +518,16 @@ class HomeFragment : Fragment(), View.OnClickListener, View.OnLongClickListener
521518
startTime = System.currentTimeMillis()
522519
longSwipeTriggered = false // Reset the flag
523520
}
521+
524522
MotionEvent.ACTION_UP -> {
525523
val endX = motionEvent.x
526524
val endY = motionEvent.y
527525
val endTime = System.currentTimeMillis()
528526
val duration = endTime - startTime
529527
val deltaX = endX - startX
530528
val deltaY = endY - startY
531-
val distance = sqrt((deltaX * deltaX + deltaY * deltaY).toDouble()).toFloat()
529+
val distance =
530+
sqrt((deltaX * deltaX + deltaY * deltaY).toDouble()).toFloat()
532531
val direction: String = if (abs(deltaX) < abs(deltaY)) {
533532
if (deltaY < 0) "up" else "down"
534533
} else {
@@ -613,14 +612,16 @@ class HomeFragment : Fragment(), View.OnClickListener, View.OnLongClickListener
613612
startTime = System.currentTimeMillis()
614613
longSwipeTriggered = false // Reset the flag
615614
}
615+
616616
MotionEvent.ACTION_UP -> {
617617
val endX = motionEvent.x
618618
val endY = motionEvent.y
619619
val endTime = System.currentTimeMillis()
620620
val duration = endTime - startTime
621621
val deltaX = endX - startX
622622
val deltaY = endY - startY
623-
val distance = sqrt((deltaX * deltaX + deltaY * deltaY).toDouble()).toFloat()
623+
val distance =
624+
sqrt((deltaX * deltaX + deltaY * deltaY).toDouble()).toFloat()
624625
val direction: String = if (abs(deltaX) < abs(deltaY)) {
625626
if (deltaY < 0) "up" else "down"
626627
} else {
@@ -692,14 +693,17 @@ class HomeFragment : Fragment(), View.OnClickListener, View.OnLongClickListener
692693
Action.OpenApp -> openLongSwipeUpApp()
693694
else -> handleOtherAction(action)
694695
}
696+
695697
"down" -> when (val action = prefs.longSwipeDownAction) {
696698
Action.OpenApp -> openLongSwipeDownApp()
697699
else -> handleOtherAction(action)
698700
}
701+
699702
"left" -> when (val action = prefs.longSwipeLeftAction) {
700703
Action.OpenApp -> openLongSwipeLeftApp()
701704
else -> handleOtherAction(action)
702705
}
706+
703707
"right" -> when (val action = prefs.longSwipeRightAction) {
704708
Action.OpenApp -> openLongSwipeRightApp()
705709
else -> handleOtherAction(action)
@@ -728,7 +732,8 @@ class HomeFragment : Fragment(), View.OnClickListener, View.OnLongClickListener
728732
}
729733

730734
// Create existingAppView
731-
val existingAppView = layoutInflater.inflate(R.layout.home_app_button, null) as TextView
735+
val existingAppView =
736+
layoutInflater.inflate(R.layout.home_app_button, null) as TextView
732737
existingAppView.apply {
733738
// Set properties of existingAppView
734739
textSize = prefs.appSize.toFloat()
@@ -758,7 +763,12 @@ class HomeFragment : Fragment(), View.OnClickListener, View.OnLongClickListener
758763
// Set properties of newAppView
759764
textSize = prefs.appSize.toFloat() / 1.5f
760765
id = i
761-
text = formatMillisToHMS(getUsageStats(context, prefs.getHomeAppModel(i).activityPackage))
766+
text = formatMillisToHMS(
767+
getUsageStats(
768+
context,
769+
prefs.getHomeAppModel(i).activityPackage
770+
)
771+
)
762772
setOnTouchListener(getHomeAppsGestureListener(context, this))
763773
setOnClickListener(this@HomeFragment)
764774
if (!prefs.extendHomeAppsArea) {
@@ -772,7 +782,7 @@ class HomeFragment : Fragment(), View.OnClickListener, View.OnLongClickListener
772782
if (prefs.followAccentColors) {
773783
val fontColor = getHexFontColor(requireContext(), prefs)
774784
setTextColor(fontColor)
775-
} else {
785+
} else {
776786
setTextColor(colors.accents(requireContext(), prefs, 3))
777787
}
778788
}
@@ -895,17 +905,18 @@ class HomeFragment : Fragment(), View.OnClickListener, View.OnLongClickListener
895905

896906
// Set the text for the page selector corresponding to each page
897907
binding.homeScreenPager.text = pageSelectorTexts.joinToString(" ")
898-
if (prefs.homePagesNum > 1 && prefs.homePagerOn) binding.homeScreenPager.visibility = View.VISIBLE
908+
if (prefs.homePagesNum > 1 && prefs.homePagerOn) binding.homeScreenPager.visibility =
909+
View.VISIBLE
899910
}
900911

901-
private fun handleSwipeLeft(totalPages:Int) {
912+
private fun handleSwipeLeft(totalPages: Int) {
902913
if (currentPage < totalPages - 1) {
903914
currentPage++
904915
updateAppsVisibility(totalPages)
905916
}
906917
}
907918

908-
private fun handleSwipeRight(totalPages:Int) {
919+
private fun handleSwipeRight(totalPages: Int) {
909920
if (currentPage > 0) {
910921
currentPage--
911922
updateAppsVisibility(totalPages)
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<vector xmlns:android="http://schemas.android.com/apk/res/android"
2+
android:width="24dp"
3+
android:height="24dp"
4+
android:viewportWidth="24"
5+
android:viewportHeight="24">
6+
<path
7+
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"
8+
android:strokeWidth="0.153984"
9+
android:fillColor="@color/white" />
10+
11+
<path
12+
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"
13+
android:fillColor="@color/green"
14+
android:fillType="evenOdd" />
15+
16+
<path
17+
android:pathData="M16.193,9.6l-7.44,-0l0,3l7.44,-0z"
18+
android:fillColor="@color/green"
19+
android:fillType="evenOdd" />
20+
21+
<path
22+
android:pathData="M16.193,12.839l-7.44,-0l0,3l7.44,-0z"
23+
android:fillColor="@color/green"
24+
android:fillType="evenOdd" />
25+
26+
<path
27+
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"
28+
android:fillColor="@color/green"
29+
android:fillType="evenOdd" />
30+
</vector>
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<vector xmlns:android="http://schemas.android.com/apk/res/android"
2+
android:width="24dp"
3+
android:height="24dp"
4+
android:viewportWidth="24"
5+
android:viewportHeight="24">
6+
<path
7+
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"
8+
android:strokeWidth="0.153984"
9+
android:fillColor="@color/white" />
10+
11+
<path
12+
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"
13+
android:fillColor="@color/red"
14+
android:fillType="evenOdd" />
15+
</vector>

0 commit comments

Comments
 (0)