Skip to content

Commit

Permalink
Enhanced UI & Functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
yuroyami committed Dec 1, 2022
1 parent b250e99 commit ee6618c
Show file tree
Hide file tree
Showing 11 changed files with 453 additions and 252 deletions.
6 changes: 1 addition & 5 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ android {
jvmTarget = '1.8'
}
buildFeatures {
viewBinding true /* Replacing findViewById by viewbinding generated classes */
compose true /* Enabling Jetpack Compose to replace classic views with composeviews */
}
lintOptions {
Expand Down Expand Up @@ -78,17 +77,14 @@ dependencies {
implementation 'androidx.lifecycle:lifecycle-viewmodel-ktx:2.6.0-alpha03'
implementation 'androidx.lifecycle:lifecycle-runtime-ktx:2.6.0-alpha03'

/* Miscellaneous Utils */
implementation 'com.blankj:utilcodex:1.31.1'

/*------------------- Jetpack Compose related libraries ----------------- */
/* Jetpack Compose BOM chooses libraries' versions for us so we don't have to specify them */
implementation platform('androidx.compose:compose-bom:2022.11.00')

/* Now we move onto declaring Jetpack Compose libraries one by one */
/* It's worth noting that there are only a few mandatory classes which are foundation and UI */
/* Material3 is built on top of them, so, you can include foundation and UI instead */
implementation 'androidx.compose.material3:material3' //Material3 + Foundation + UI (core)
implementation 'androidx.compose.material3:material3:1.1.0-alpha02' //Material3 + Foundation + UI (core)
implementation "com.google.android.material:compose-theme-adapter-3:1.1.0" //Theme adapter

implementation 'androidx.compose.ui:ui-tooling-preview'
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
android:theme="@style/Theme.splash">

<activity
android:name=".ComposeActivity"
android:name=".ui.ComposeActivity"
android:configChanges="orientation|keyboardHidden|screenSize"
android:exported="true"
android:imeOptions="flagNoExtractUi"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package app.utils
package app.logic

object Constants {

Expand Down
81 changes: 81 additions & 0 deletions app/src/main/kotlin/app/logic/Panel.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
package app.logic

import androidx.compose.runtime.mutableStateListOf
import androidx.compose.runtime.mutableStateOf
import app.logic.PingyUtils.minPing
import java.lang.Thread.sleep

/** Wrapper Model class for a single Ping graph panel and its current parameters */
class Panel(
val ip: String, /* Address to ping */
val h: Float, /* Screen height, in pixels */
val w: Float, /* Scree, width, in pixels */) {

/** List of all recent pings */
val pings = mutableStateListOf<Int>()

/** Last Received Ping */
var lastping = mutableStateOf(0)

/** Last Received Ping time */
var lastpingms = System.currentTimeMillis()

/** Pinging Parameters */
var packetsize = 32 /* Packet size for the pinging (Minimum is 32, default is 64) */
var interval = 50L /* Interval of pinging, in milliseconds, it is by default 50ms */
var mode = 0 /* Mode of pinging */
var isPinging = true /* Defines whether this panel is showing and doing pings or not */

/** UI-related parameters */
var pingWidth = mutableStateOf(3) /* Ping line width in pixels, default is 4 */
var pingLimit = mutableStateOf(1000) /* The highest ping that the panel shows */
var angleOfAttack = mutableStateOf(8.0f) /* How the panel zooms on smaller amounts, exponentially */
var pingStock = mutableStateOf(if (h > w) h else w) /* Max pings that the panel can remember */
var landMarks = mutableStateListOf(25f, 50f, 100f, 200f, 500f) /* Line marks on the panel */
var expanded = mutableStateOf(true) /* Whether the panel is showing info */
var panelHeight = mutableStateOf(h/5f) /* panel height, in px */

/** For Statistics */
var pingsSent = mutableStateOf(0) /* Overall sent pings */
var pingsLost = mutableStateOf(0) /* Lost pings */
var lowestPing = mutableStateOf(0) /* Lowest recorded ping */
var highestPing = mutableStateOf(0) /* Highest recorded ping */

init {
/** Launching two threads for each panel is better than using coroutines, performance-wise */
Thread {
while (true) {
if (isPinging) {
try {
val p = PingyUtils.pingIcmp(ip, packetsize).toInt()
lastpingms = System.currentTimeMillis()
lastping.value = p
pingsSent.value += 1
if (p < 0) pingsLost.value += 1;
lowestPing.value = pings.minPing()
highestPing.value = pings.max()
} catch (_: Exception) {
sleep(50)
}
}
}
}.start()

Thread {
while (true) {
if (isPinging) {
val p = if (System.currentTimeMillis() - lastpingms > 1000) {
-1
} else {
lastping.value
}
pings.add(p)
if (pings.size > pingStock.value) {
pings.removeFirst()
}
sleep(interval)
}
}
}.start()
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package app.utils
package app.logic

import android.annotation.TargetApi
import android.app.Activity
Expand All @@ -20,41 +20,46 @@ import java.sql.Timestamp

object PingyUtils {


/** This function is used to calculate the ICMP Ping to a certain server or end host.
* This is basically the most important function in our entire app **/
fun pingIcmp(host: String, packet: Int): Double {
var result = 0.13
val result: Double
try {
val pingprocess: Process? =
Runtime.getRuntime().exec("/system/bin/ping -c 1 -w 1 -s $packet $host")
//Reading the Output with BufferedReader
val bufferedReader = BufferedReader(InputStreamReader(pingprocess?.inputStream))
//Parsing the result in a string variable.
val logger: StringBuilder = StringBuilder()
var line: String? = ""
while (line != null) {
line = bufferedReader.readLine()
logger.append(line + "\n")
}
val pingoutput = logger.toString()

//Now reading what we have in pingResult and storing it as an Int value.
val p = Runtime.getRuntime().exec("/system/bin/ping -c 1 -w 1 -s $packet $host")
val i = p.inputStream
val s = i.reader().readText()
result = when {
pingoutput.contains("100% packet loss") -> {
0.0
s.contains("100% packet loss") -> {
-1.0
}
else -> {
((pingoutput.substringAfter("time=").substringBefore(" ms").trim()
((s.substringAfter("time=").substringBefore(" ms").trim()
.toDouble()))
}
}
} catch (e: Exception) {
e.printStackTrace()
} catch (_: Exception) {
return -1.0
}
return result
}

/** Looks for the smallest minimum ping in a ping list, except for -1 which equals an undefined ping.
* We use our own implementation of 'List().min()' because Kotlin's min() will return -1 if it exists
* in the list, while our implementation should ignore a -1 if found */
fun List<Int>.minPing(): Int {
var min = 1000
for (p in this) {
if (p > 0) {
if (p < min) {
min = p
}
}
}
return min
}



/** This function is used to print/format the timestamp used in determining video values
* @param seconds Unix epoch timestamp in seconds
***/
Expand Down
12 changes: 12 additions & 0 deletions app/src/main/kotlin/app/logic/ScreenDimensions.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package app.logic

import kotlin.properties.Delegates

class ScreenDimensions {

var w_dp by Delegates.notNull<Float>()
var w_px by Delegates.notNull<Float>()

var h_dp by Delegates.notNull<Float>()
var h_px by Delegates.notNull<Float>()
}
Loading

0 comments on commit ee6618c

Please sign in to comment.