adding a setting for toggling 24 hour format

This commit is contained in:
tibbi 2018-03-12 22:11:21 +01:00
parent 68ee5a40c6
commit 2243e9561f
11 changed files with 77 additions and 19 deletions

View File

@ -41,7 +41,7 @@ android {
}
dependencies {
implementation 'com.simplemobiletools:commons:3.15.12'
implementation 'com.simplemobiletools:commons:3.15.14'
implementation 'com.facebook.stetho:stetho:1.5.0'
implementation 'com.android.support.constraint:constraint-layout:1.0.2'
implementation 'com.shawnlin:number-picker:2.4.6'

View File

@ -24,6 +24,7 @@ class SettingsActivity : SimpleActivity() {
setupUseEnglish()
setupAvoidWhatsNew()
setupPreventPhoneFromSleeping()
setupHourFormat()
setupSundayFirst()
setupShowSeconds()
setupDisplayOtherTimeZones()
@ -76,6 +77,14 @@ class SettingsActivity : SimpleActivity() {
}
}
private fun setupHourFormat() {
settings_hour_format.isChecked = config.use24HourFormat
settings_hour_format_holder.setOnClickListener {
settings_hour_format.toggle()
config.use24HourFormat = settings_hour_format.isChecked
}
}
private fun setupSundayFirst() {
settings_sunday_first.isChecked = config.isSundayFirst
settings_sunday_first_holder.setOnClickListener {

View File

@ -73,7 +73,7 @@ class WidgetDateTimeConfigureActivity : SimpleActivity() {
private fun updateCurrentDateTime() {
val calendar = Calendar.getInstance()
config_time.text = getPassedSeconds().getFormattedTime(false)
config_time.text = getFormattedTime(getPassedSeconds(), false, false).toString()
config_date.text = getFormattedDate(calendar)
if (config.useTextShadow) {

View File

@ -24,7 +24,7 @@ class EditAlarmDialog(val activity: SimpleActivity, val alarm: Alarm, val callba
view.apply {
edit_alarm_time.setOnClickListener {
TimePickerDialog(context, context.getDialogTheme(), timeSetListener, alarm.timeInMinutes / 60, alarm.timeInMinutes % 60, context.config.use24hourFormat).show()
TimePickerDialog(context, context.getDialogTheme(), timeSetListener, alarm.timeInMinutes / 60, alarm.timeInMinutes % 60, context.config.use24HourFormat).show()
}
edit_alarm_sound.colorLeftDrawable(textColor)

View File

@ -10,6 +10,8 @@ import android.content.Context
import android.content.Intent
import android.media.RingtoneManager
import android.net.Uri
import android.text.SpannableString
import android.text.style.RelativeSizeSpan
import android.widget.Toast
import com.simplemobiletools.clock.R
import com.simplemobiletools.clock.helpers.*
@ -173,3 +175,24 @@ fun Context.scheduleNextWidgetUpdate() {
else -> alarmManager.set(AlarmManager.RTC, triggerAtMillis, pendingIntent)
}
}
fun Context.getFormattedTime(passedSeconds: Int, showSeconds: Boolean, makeAmPmSmaller: Boolean): SpannableString {
val use24HourFormat = config.use24HourFormat
val hours = (passedSeconds / 3600) % 24
val minutes = (passedSeconds / 60) % 60
val seconds = passedSeconds % 60
return if (!use24HourFormat) {
val appendable = getString(if (hours >= 12) R.string.p_m else R.string.a_m)
val newHours = if (hours == 0 || hours == 12) 12 else hours % 12
val formattedTime = "${formatTime(showSeconds, use24HourFormat, newHours, minutes, seconds)} $appendable"
val spannableTime = SpannableString(formattedTime)
val amPmMultiplier = if (makeAmPmSmaller) 0.4f else 1f
spannableTime.setSpan(RelativeSizeSpan(amPmMultiplier), spannableTime.length - 5, spannableTime.length, 0)
spannableTime
} else {
val formattedTime = formatTime(showSeconds, use24HourFormat, hours, minutes, seconds)
SpannableString(formattedTime)
}
}

View File

@ -6,17 +6,3 @@ fun Int.formatAlarmTime(): String {
val format = "%02d:%02d"
return String.format(format, hours, minutes)
}
fun Int.getFormattedTime(showSeconds: Boolean): String {
val hours = (this / 3600) % 24
val minutes = (this / 60) % 60
val seconds = this % 60
var format = "%02d:%02d"
return if (showSeconds) {
format += ":%02d"
String.format(format, hours, minutes, seconds)
} else {
String.format(format, hours, minutes)
}
}

View File

@ -76,7 +76,11 @@ class ClockFragment : Fragment() {
val hours = (passedSeconds / 3600) % 24
val minutes = (passedSeconds / 60) % 60
val seconds = passedSeconds % 60
view.clock_time.text = passedSeconds.getFormattedTime(context!!.config.showSeconds)
view.clock_time.text = context!!.getFormattedTime(passedSeconds, context!!.config.showSeconds, true)
if (!context!!.config.use24HourFormat) {
view.clock_time.textSize = resources.getDimension(R.dimen.clock_text_size_smaller) / resources.displayMetrics.density
}
if (seconds == 0) {
if (hours == 0 && minutes == 0) {

View File

@ -51,6 +51,18 @@ fun getPassedSeconds(): Int {
return ((calendar.timeInMillis + offset) / 1000).toInt()
}
fun formatTime(showSeconds: Boolean, use24HourFormat: Boolean, hours: Int, minutes: Int, seconds: Int): String {
val hoursFormat = if (use24HourFormat) "%02d" else "%01d"
var format = "$hoursFormat:%02d"
return if (showSeconds) {
format += ":%02d"
String.format(format, hours, minutes, seconds)
} else {
String.format(format, hours, minutes)
}
}
fun getAllTimeZones() = arrayListOf(
MyTimeZone(1, "GMT-11:00 Midway", "Pacific/Midway"),
MyTimeZone(2, "GMT-10:00 Honolulu", "Pacific/Honolulu"),

View File

@ -45,7 +45,7 @@ class MyWidgetDateTimeProvider : AppWidgetProvider() {
private fun updateTexts(context: Context, views: RemoteViews) {
val calendar = Calendar.getInstance()
views.apply {
setText(R.id.widget_time, getPassedSeconds().getFormattedTime(false))
setText(R.id.widget_time, context.getFormattedTime(getPassedSeconds(), false, false).toString())
setText(R.id.widget_date, context.getFormattedDate(calendar))
}
}

View File

@ -102,6 +102,29 @@
</RelativeLayout>
<RelativeLayout
android:id="@+id/settings_hour_format_holder"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/medium_margin"
android:background="?attr/selectableItemBackground"
android:paddingBottom="@dimen/activity_margin"
android:paddingLeft="@dimen/normal_margin"
android:paddingRight="@dimen/normal_margin"
android:paddingTop="@dimen/activity_margin">
<com.simplemobiletools.commons.views.MySwitchCompat
android:id="@+id/settings_hour_format"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@null"
android:clickable="false"
android:paddingLeft="@dimen/medium_margin"
android:paddingStart="@dimen/medium_margin"
android:text="@string/use_24_hour_time_format"/>
</RelativeLayout>
<RelativeLayout
android:id="@+id/settings_sunday_first_holder"
android:layout_width="match_parent"

View File

@ -7,6 +7,7 @@
<dimen name="min_widget_width">110dp</dimen>
<dimen name="clock_text_size">70sp</dimen>
<dimen name="clock_text_size_smaller">60sp</dimen>
<dimen name="alarm_text_size">46sp</dimen>
<dimen name="stopwatch_text_size">60sp</dimen>
<dimen name="widget_time_text_size">50sp</dimen>