@@ -28,9 +28,9 @@ import android.view.View
28
28
import kotlinx.android.synthetic.main.activity_main.*
29
29
import org.json.JSONException
30
30
import org.json.JSONObject
31
+ import kotlin.math.roundToInt
31
32
32
33
33
- const val GB = 1073741824
34
34
const val Byte = 1024
35
35
const val N_CORES = 4 /* number of cores in your server. 4 for RPi4. */
36
36
@@ -142,20 +142,32 @@ class MainActivity : WearableActivity()
142
142
// and it has to be specified by the final user using a specific option menu.
143
143
// cpu_load_placeholder.text = cpuLoad.toString()
144
144
145
- val cpuLoad3Digit = Math .round (((cpuLoad* 100 )/ N_CORES ) * 1000.0 ) / 1000.0
146
- val cpuLoad2Digit = Math .round (cpuLoad3Digit * 100.0 ) / 100.0
145
+ val cpuLoad3Digit = (((cpuLoad * 100 ) / N_CORES ) * 1000.0 ).roundToInt( ) / 1000.0
146
+ val cpuLoad2Digit = (cpuLoad3Digit * 100.0 ).roundToInt( ) / 100.0
147
147
148
148
cpu_load_placeholder.text = cpuLoad2Digit.toString()
149
149
ram_used_placeholder.text = (ramBusy / Byte ).toString()
150
150
ram_total_placeholder.text = (ramTotal / Byte ).toString()
151
151
swap_used_placeholder.text = (swapBusy / Byte ).toString()
152
152
swap_total_placeholder.text = (swapTotal / Byte ).toString()
153
153
154
- if (diskFree > GB )
155
- disk_used_placeholder.text = (diskFree / GB ).format(2 )
156
- else {
157
- disk_used_placeholder.text = (diskFree / Byte ).format(2 )
158
- disk_unit.text = " MB"
154
+ val p = humanReadableByteCountBin(diskFree)
155
+ disk_free_placeholder.text = p.first
156
+ disk_unit.text = " " + p.second
157
+ }
158
+
159
+ /* This function is used to convert the free disk
160
+ * space in an human friendly measure
161
+ */
162
+ private fun humanReadableByteCountBin (bytes : Double ): Pair <String , String > {
163
+ return when {
164
+ bytes == Double .MIN_VALUE || bytes < 0 -> Pair (" N/A" , " " )
165
+ bytes < 1024L -> Pair (" $bytes " , " B" )
166
+ bytes <= 0xfffccccccccccccL shr 40 -> Pair (" %.1f" .format(bytes / (0x1 shl 10 )), " KiB" )
167
+ bytes <= 0xfffccccccccccccL shr 30 -> Pair (" %.1f" .format(bytes / (0x1 shl 20 )), " MiB" )
168
+ bytes <= 0xfffccccccccccccL shr 20 -> Pair (" %.1f" .format(bytes / (0x1 shl 30 )), " GiB" )
169
+ bytes <= 0xfffccccccccccccL shr 10 -> Pair (" %.1f" .format(bytes / (0x1 shl 40 )), " TiB" )
170
+ else -> Pair (" N/A" , " " )
159
171
}
160
172
}
161
173
}
0 commit comments