use TextClock instead of custom solution

This commit is contained in:
Josia Pietsch 2025-01-22 02:17:46 +01:00
parent 785e024ddb
commit 6d385e4e87
Signed by: jrpie
GPG key ID: E70B571D66986A2D
2 changed files with 11 additions and 37 deletions

View file

@ -21,8 +21,6 @@ import de.jrpie.android.launcher.actions.LauncherAction
import de.jrpie.android.launcher.databinding.HomeBinding import de.jrpie.android.launcher.databinding.HomeBinding
import de.jrpie.android.launcher.preferences.LauncherPreferences import de.jrpie.android.launcher.preferences.LauncherPreferences
import de.jrpie.android.launcher.ui.tutorial.TutorialActivity import de.jrpie.android.launcher.ui.tutorial.TutorialActivity
import java.text.SimpleDateFormat
import java.util.Date
import java.util.Locale import java.util.Locale
import java.util.Timer import java.util.Timer
import kotlin.concurrent.fixedRateTimer import kotlin.concurrent.fixedRateTimer
@ -65,8 +63,6 @@ class HomeActivity : UIObject, AppCompatActivity(),
private lateinit var mDetector: GestureDetectorCompat private lateinit var mDetector: GestureDetectorCompat
// timers
private var clockTimer = Timer()
override fun onCreate(savedInstanceState: Bundle?) { override fun onCreate(savedInstanceState: Bundle?) {
super<AppCompatActivity>.onCreate(savedInstanceState) super<AppCompatActivity>.onCreate(savedInstanceState)
@ -100,30 +96,24 @@ class HomeActivity : UIObject, AppCompatActivity(),
} }
private fun updateClock() { private fun initClock() {
clockTimer.cancel()
val locale = Locale.getDefault() val locale = Locale.getDefault()
val dateVisible = LauncherPreferences.clock().dateVisible() val dateVisible = LauncherPreferences.clock().dateVisible()
val timeVisible = LauncherPreferences.clock().timeVisible() val timeVisible = LauncherPreferences.clock().timeVisible()
var dateFMT = "yyyy-MM-dd" var dateFMT = "yyyy-MM-dd"
var timeFMT = "HH:mm" var timeFMT = "HH:mm"
val period = 100L
if (LauncherPreferences.clock().showSeconds()) { if (LauncherPreferences.clock().showSeconds()) {
timeFMT += ":ss" timeFMT += ":ss"
} }
/*
I thought about adding an option to show microseconds as well ( timeFMT += ".SSS" ).
However setting period ot 1L (or even 10L) causes high CPU load,
so that doesn't seem to be a good idea.
*/
if (LauncherPreferences.clock().localized()) { if (LauncherPreferences.clock().localized()) {
dateFMT = android.text.format.DateFormat.getBestDateTimePattern(locale, dateFMT) dateFMT = android.text.format.DateFormat.getBestDateTimePattern(locale, dateFMT)
timeFMT = android.text.format.DateFormat.getBestDateTimePattern(locale, timeFMT) timeFMT = android.text.format.DateFormat.getBestDateTimePattern(locale, timeFMT)
} }
var upperFormat = SimpleDateFormat(dateFMT, locale) var upperFormat = dateFMT
var lowerFormat = SimpleDateFormat(timeFMT, locale) var lowerFormat = timeFMT
var upperVisible = dateVisible var upperVisible = dateVisible
var lowerVisible = timeVisible var lowerVisible = timeVisible
@ -138,21 +128,10 @@ class HomeActivity : UIObject, AppCompatActivity(),
binding.homeUpperView.setTextColor(LauncherPreferences.clock().color()) binding.homeUpperView.setTextColor(LauncherPreferences.clock().color())
binding.homeLowerView.setTextColor(LauncherPreferences.clock().color()) binding.homeLowerView.setTextColor(LauncherPreferences.clock().color())
binding.homeLowerView.format24Hour = lowerFormat
clockTimer = fixedRateTimer("clockTimer", true, 0L, period) { binding.homeUpperView.format24Hour = upperFormat
this@HomeActivity.runOnUiThread { binding.homeLowerView.format12Hour = lowerFormat
if (lowerVisible) { binding.homeUpperView.format12Hour = upperFormat
val t = lowerFormat.format(Date())
if (binding.homeLowerView.text != t)
binding.homeLowerView.text = t
}
if (upperVisible) {
val d = upperFormat.format(Date())
if (binding.homeUpperView.text != d)
binding.homeUpperView.text = d
}
}
}
} }
override fun getTheme(): Resources.Theme { override fun getTheme(): Resources.Theme {
@ -172,12 +151,7 @@ class HomeActivity : UIObject, AppCompatActivity(),
edgeWidth = LauncherPreferences.enabled_gestures().edgeSwipeEdgeWidth() / 100f edgeWidth = LauncherPreferences.enabled_gestures().edgeSwipeEdgeWidth() / 100f
updateClock() initClock()
}
override fun onPause() {
super.onPause()
clockTimer.cancel()
} }
override fun onDestroy() { override fun onDestroy() {

View file

@ -10,7 +10,7 @@
android:fitsSystemWindows="true" android:fitsSystemWindows="true"
tools:context=".ui.HomeActivity"> tools:context=".ui.HomeActivity">
<TextView <TextClock
android:id="@+id/home_upper_view" android:id="@+id/home_upper_view"
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="wrap_content"
@ -23,7 +23,7 @@
app:layout_constraintVertical_bias="0.45" app:layout_constraintVertical_bias="0.45"
tools:text="2024-12-24" /> tools:text="2024-12-24" />
<TextView <TextClock
android:id="@+id/home_lower_view" android:id="@+id/home_lower_view"
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="wrap_content"