-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathMainActivity.kt
executable file
·257 lines (209 loc) · 7.95 KB
/
MainActivity.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
package ru.roundeasy.ktping
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.os.Handler
import android.os.Message
import android.widget.EditText
import android.widget.TableRow
import android.widget.TextView
import android.widget.Toast
import androidx.appcompat.app.AlertDialog
import kotlinx.android.synthetic.main.activity_main.*
import java.lang.ref.WeakReference
import java.util.regex.Matcher
import java.util.regex.Pattern
import java.text.SimpleDateFormat
import java.util.*
import kotlin.math.roundToLong
/**
* A Main Activity class
*
* This class contains the main logic of the app
*/
class MainActivity : AppCompatActivity() {
private val pingRegex = "(?:\\[(?<ts>[0-9.]+)] )?(?<size>[0-9]+) bytes from (?<ip>[0-9.]+): icmp_seq=(?<seq>[0-9]+) ttl=(?<ttl>[0-9]+)(?: time=(?<rtt>[0-9.]+) (?<rttmetric>\\w+))?"
private val outerClass = WeakReference<MainActivity>(this)
private var mHandlerThread = MyHandler(outerClass)
private var mThread: Thread? = null
private var isThreadRunning = false
private var errorMessage = ""
val queue: Queue<String> = ArrayDeque<String>()
companion object {
private const val START = 100
private const val STOP = 101
private const val PING = 102
}
object Params {
var server = "8.8.8.8"
var size = 16
var interval = 1.0
var count = 5
}
private var params = Params
private fun showSettingsPopup() {
val builder = AlertDialog.Builder(this)
val inflater = layoutInflater
val dialogLayout = inflater.inflate(R.layout.alert_dialog_settings, null)
val serverText = dialogLayout.findViewById<EditText>(R.id.addressText)
val sizeText = dialogLayout.findViewById<EditText>(R.id.sizeText)
val intervalText = dialogLayout.findViewById<EditText>(R.id.intervalText)
val countText = dialogLayout.findViewById<EditText>(R.id.countText)
serverText.setText(params.server)
sizeText.setText(params.size.toString())
intervalText.setText(params.interval.toString())
countText.setText(params.count.toString())
builder.setView(dialogLayout)
builder.setPositiveButton("OK") {
_, _ ->
params.server = serverText.text.toString()
params.size = sizeText.text.toString().toInt()
params.interval = intervalText.text.toString().toDouble()
params.count = countText.text.toString().toInt()
syncServerText()
}
builder.show()
}
private class MyHandler(private val outerClass: WeakReference<MainActivity>) : Handler() {
override fun handleMessage(msg: Message) {
super.handleMessage(msg)
when {
msg.what == PING -> outerClass.get()?.updateText()
msg.what == STOP -> outerClass.get()?.togglePing(false)
msg.what == START -> outerClass.get()?.togglePing(true)
}
}
}
private fun parsePingString(s: String): Matcher {
val re = Pattern.compile(
pingRegex,
Pattern.CASE_INSENSITIVE.or(Pattern.DOTALL))
return re.matcher(s)
}
private fun updateText() {
if (queue.size == 0) {
return
}
val res = parsePingString(queue.remove())
if (!res.find()) {
return
}
val row = TableRow(this)
row.layoutParams = TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT)
val rowView = layoutInflater.inflate(R.layout.result_row, tableLayout, false)
val tvTimestamp = rowView.findViewById(R.id.tvTimestamp) as? TextView
val tvSize = rowView.findViewById(R.id.tvSize) as? TextView
val tvTarget = rowView.findViewById(R.id.tvTarget) as? TextView
val tvSeqN = rowView.findViewById(R.id.tvSeqN) as? TextView
val tvTtl = rowView.findViewById(R.id.tvTtl) as? TextView
val tvRtt = rowView.findViewById(R.id.tvRtt) as? TextView
var dateTimeStr = res.group(1)
try {
val sdf = SimpleDateFormat(getString(R.string.datetime_format), Locale.US)
val netDate = Date((dateTimeStr.toDouble()*1000).roundToLong())
dateTimeStr = sdf.format(netDate)
} finally {
tvTimestamp?.text = dateTimeStr
}
tvSize?.text = res.group(2)
tvTarget?.text =res.group(3)
tvSeqN?.text = res.group(4)
tvTtl?.text = res.group(5)
val rttConcat = if (res.group(6) != null)
resources.getString(R.string.format_rtt, res.group(6), res.group(7))
else resources.getString(R.string.value_na)
tvRtt?.text = rttConcat
tableLayout.addView(rowView, 1)
}
internal inner class PingProcess : Runnable {
override fun run() {
val cmd = mutableListOf("ping", "-D")
if (params.size > 0) {
cmd.addAll(arrayOf("-s", params.size.toString()))
}
if (params.interval > 0) {
cmd.addAll(arrayOf("-i", params.interval.toString()))
}
if (params.count > 0) {
cmd.addAll(arrayOf("-c", params.count.toString()))
}
cmd.add(params.server)
val builder = ProcessBuilder()
builder.command(cmd)
val process = builder.start()
val stdInput = process.inputStream.bufferedReader()
val messageStart = Message()
messageStart.what = START
mHandlerThread.sendMessage(messageStart)
while (isThreadRunning) {
val currentStr = try {
stdInput.readLine()
} catch (e: IllegalStateException) {
break
} ?: break
pingRegex.toRegex().find(currentStr) ?: continue
queue.add(currentStr)
val messagePing = Message()
messagePing.what = PING
mHandlerThread.sendMessage(messagePing)
}
if (isThreadRunning) {
errorMessage = try {
process.errorStream.bufferedReader().readLine()
} catch (e: IllegalStateException) {
"IllegalStateException"
} catch (e: NullPointerException) {
"NullPointerException"
}
}
val messageStop = Message()
messageStop.what = STOP
mHandlerThread.sendMessage(messageStop)
process.destroy()
}
}
private fun togglePing(on: Boolean) {
if (errorMessage != "") {
Toast.makeText(this, errorMessage, Toast.LENGTH_SHORT).show()
}
if (on) {
pingButton.text = resources.getString(R.string.btn_stop)
} else {
mThread = null
pingButton.text = resources.getString(R.string.btn_start)
}
errorMessage = ""
pingButton.isClickable = true
}
private fun triggerTogglePing() {
pingButton.isClickable = false
val doEnable = mThread == null
isThreadRunning = doEnable
if (doEnable) {
mThread = Thread(PingProcess())
mThread?.start()
}
}
private fun initTableView() {
tableLayout.removeAllViews()
val headerRow = layoutInflater.inflate(R.layout.result_row, tableLayout, false)
tableLayout.addView(headerRow)
}
private fun syncServerText() {
settingsButton.text = params.server
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
syncServerText()
initTableView()
clearButton.setOnClickListener {
initTableView()
}
pingButton.setOnClickListener {
triggerTogglePing()
}
settingsButton.setOnClickListener {
showSettingsPopup()
}
}
}