Skip to content

Commit dff1314

Browse files
zx2c4Alon Blayer-Gat
authored andcommitted
ui: display latest handshake time
Signed-off-by: Jason A. Donenfeld <[email protected]> Signed-off-by: Alon Blayer-Gat <[email protected]>
1 parent 569176c commit dff1314

File tree

1 file changed

+60
-60
lines changed

1 file changed

+60
-60
lines changed

ui/src/main/java/com/wireguard/android/util/QuantityFormatter.kt

Lines changed: 60 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -3,64 +3,64 @@
33
* SPDX-License-Identifier: Apache-2.0
44
*/
55

6-
package com.wireguard.android.util
6+
package com.wireguard.android.util
77

8-
import android.icu.text.ListFormatter
9-
import android.icu.text.MeasureFormat
10-
import android.icu.text.RelativeDateTimeFormatter
11-
import android.icu.util.Measure
12-
import android.icu.util.MeasureUnit
13-
import android.os.Build
14-
import com.wireguard.android.Application
15-
import com.wireguard.android.R
16-
import java.util.Locale
17-
import kotlin.time.Duration.Companion.seconds
18-
19-
object QuantityFormatter {
20-
fun formatBytes(bytes: Long): String {
21-
val context = Application.get().applicationContext
22-
return when {
23-
bytes < 1024 -> context.getString(R.string.transfer_bytes, bytes)
24-
bytes < 1024 * 1024 -> context.getString(R.string.transfer_kibibytes, bytes / 1024.0)
25-
bytes < 1024 * 1024 * 1024 -> context.getString(R.string.transfer_mibibytes, bytes / (1024.0 * 1024.0))
26-
bytes < 1024 * 1024 * 1024 * 1024L -> context.getString(R.string.transfer_gibibytes, bytes / (1024.0 * 1024.0 * 1024.0))
27-
else -> context.getString(R.string.transfer_tibibytes, bytes / (1024.0 * 1024.0 * 1024.0) / 1024.0)
28-
}
29-
}
30-
31-
fun formatEpochAgo(epochMillis: Long): String {
32-
var span = (System.currentTimeMillis() - epochMillis) / 1000
33-
34-
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.N)
35-
return Application.get().applicationContext.getString(R.string.latest_handshake_ago, span.seconds.toString())
36-
37-
if (span <= 0L)
38-
return RelativeDateTimeFormatter.getInstance().format(RelativeDateTimeFormatter.Direction.PLAIN, RelativeDateTimeFormatter.AbsoluteUnit.NOW)
39-
val measureFormat = MeasureFormat.getInstance(Locale.getDefault(), MeasureFormat.FormatWidth.WIDE)
40-
val parts = ArrayList<CharSequence>(4)
41-
if (span >= 24 * 60 * 60L) {
42-
val v = span / (24 * 60 * 60L)
43-
parts.add(measureFormat.format(Measure(v, MeasureUnit.DAY)))
44-
span -= v * (24 * 60 * 60L)
45-
}
46-
if (span >= 60 * 60L) {
47-
val v = span / (60 * 60L)
48-
parts.add(measureFormat.format(Measure(v, MeasureUnit.HOUR)))
49-
span -= v * (60 * 60L)
50-
}
51-
if (span >= 60L) {
52-
val v = span / 60L
53-
parts.add(measureFormat.format(Measure(v, MeasureUnit.MINUTE)))
54-
span -= v * 60L
55-
}
56-
if (span > 0L)
57-
parts.add(measureFormat.format(Measure(span, MeasureUnit.SECOND)))
58-
59-
val joined = if (Build.VERSION.SDK_INT < Build.VERSION_CODES.TIRAMISU)
60-
parts.joinToString()
61-
else
62-
ListFormatter.getInstance(Locale.getDefault(), ListFormatter.Type.UNITS, ListFormatter.Width.SHORT).format(parts)
63-
64-
return Application.get().applicationContext.getString(R.string.latest_handshake_ago, joined)
65-
}
66-
}
8+
import android.icu.text.ListFormatter
9+
import android.icu.text.MeasureFormat
10+
import android.icu.text.RelativeDateTimeFormatter
11+
import android.icu.util.Measure
12+
import android.icu.util.MeasureUnit
13+
import android.os.Build
14+
import com.wireguard.android.Application
15+
import com.wireguard.android.R
16+
import java.util.Locale
17+
import kotlin.time.Duration.Companion.seconds
18+
19+
object QuantityFormatter {
20+
fun formatBytes(bytes: Long): String {
21+
val context = Application.get().applicationContext
22+
return when {
23+
bytes < 1024 -> context.getString(R.string.transfer_bytes, bytes)
24+
bytes < 1024 * 1024 -> context.getString(R.string.transfer_kibibytes, bytes / 1024.0)
25+
bytes < 1024 * 1024 * 1024 -> context.getString(R.string.transfer_mibibytes, bytes / (1024.0 * 1024.0))
26+
bytes < 1024 * 1024 * 1024 * 1024L -> context.getString(R.string.transfer_gibibytes, bytes / (1024.0 * 1024.0 * 1024.0))
27+
else -> context.getString(R.string.transfer_tibibytes, bytes / (1024.0 * 1024.0 * 1024.0) / 1024.0)
28+
}
29+
}
30+
31+
fun formatEpochAgo(epochMillis: Long): String {
32+
var span = (System.currentTimeMillis() - epochMillis) / 1000
33+
34+
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.N)
35+
return Application.get().applicationContext.getString(R.string.latest_handshake_ago, span.seconds.toString())
36+
37+
if (span <= 0L)
38+
return RelativeDateTimeFormatter.getInstance().format(RelativeDateTimeFormatter.Direction.PLAIN, RelativeDateTimeFormatter.AbsoluteUnit.NOW)
39+
val measureFormat = MeasureFormat.getInstance(Locale.getDefault(), MeasureFormat.FormatWidth.WIDE)
40+
val parts = ArrayList<CharSequence>(4)
41+
if (span >= 24 * 60 * 60L) {
42+
val v = span / (24 * 60 * 60L)
43+
parts.add(measureFormat.format(Measure(v, MeasureUnit.DAY)))
44+
span -= v * (24 * 60 * 60L)
45+
}
46+
if (span >= 60 * 60L) {
47+
val v = span / (60 * 60L)
48+
parts.add(measureFormat.format(Measure(v, MeasureUnit.HOUR)))
49+
span -= v * (60 * 60L)
50+
}
51+
if (span >= 60L) {
52+
val v = span / 60L
53+
parts.add(measureFormat.format(Measure(v, MeasureUnit.MINUTE)))
54+
span -= v * 60L
55+
}
56+
if (span > 0L)
57+
parts.add(measureFormat.format(Measure(span, MeasureUnit.SECOND)))
58+
59+
val joined = if (Build.VERSION.SDK_INT < Build.VERSION_CODES.TIRAMISU)
60+
parts.joinToString()
61+
else
62+
ListFormatter.getInstance(Locale.getDefault(), ListFormatter.Type.UNITS, ListFormatter.Width.SHORT).format(parts)
63+
64+
return Application.get().applicationContext.getString(R.string.latest_handshake_ago, joined)
65+
}
66+
}

0 commit comments

Comments
 (0)