Skip to content

Commit 75260d4

Browse files
committed
added a method to compute the correct free disk space in a correct way
1 parent 28fd3eb commit 75260d4

File tree

2 files changed

+22
-10
lines changed

2 files changed

+22
-10
lines changed

app/src/main/java/davideag/wearos/ncmonitor/MainActivity.kt

+20-8
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,9 @@ import android.view.View
2828
import kotlinx.android.synthetic.main.activity_main.*
2929
import org.json.JSONException
3030
import org.json.JSONObject
31+
import kotlin.math.roundToInt
3132

3233

33-
const val GB = 1073741824
3434
const val Byte = 1024
3535
const val N_CORES = 4 /* number of cores in your server. 4 for RPi4. */
3636

@@ -142,20 +142,32 @@ class MainActivity : WearableActivity()
142142
// and it has to be specified by the final user using a specific option menu.
143143
// cpu_load_placeholder.text = cpuLoad.toString()
144144

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
147147

148148
cpu_load_placeholder.text = cpuLoad2Digit.toString()
149149
ram_used_placeholder.text = (ramBusy / Byte).toString()
150150
ram_total_placeholder.text = (ramTotal / Byte).toString()
151151
swap_used_placeholder.text = (swapBusy / Byte).toString()
152152
swap_total_placeholder.text = (swapTotal / Byte).toString()
153153

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", "")
159171
}
160172
}
161173
}

app/src/main/res/layout/activity_main.xml

+2-2
Original file line numberDiff line numberDiff line change
@@ -231,12 +231,12 @@
231231
android:id="@+id/disk_text_view"
232232
android:layout_width="wrap_content"
233233
android:layout_height="wrap_content"
234-
android:text="HDD Free: "
234+
android:text="Disk Free: "
235235
android:textAppearance="@android:style/TextAppearance.Material.Medium"
236236
android:textStyle="bold" />
237237

238238
<TextView
239-
android:id="@+id/disk_used_placeholder"
239+
android:id="@+id/disk_free_placeholder"
240240
android:layout_width="wrap_content"
241241
android:layout_height="wrap_content"
242242
android:text="--"

0 commit comments

Comments
 (0)