-
Notifications
You must be signed in to change notification settings - Fork 0
/
TerminalFragment.kt
345 lines (293 loc) · 11.2 KB
/
TerminalFragment.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
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
package simple.bluetooth.terminal
import android.app.Activity
import android.bluetooth.BluetoothAdapter
import android.content.ComponentName
import android.content.Context
import android.content.Intent
import android.content.ServiceConnection
import android.os.Bundle
import android.os.IBinder
import android.text.Spannable
import android.text.SpannableStringBuilder
import android.text.method.ScrollingMovementMethod
import android.text.style.ForegroundColorSpan
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentManager
import androidx.fragment.app.activityViewModels
import androidx.lifecycle.Observer
import li.garteroboter.pren.Constants
import li.garteroboter.pren.Constants.FROM_BINARY_START_COMMAND_ESP32
import li.garteroboter.pren.R
import li.garteroboter.pren.qrcode.fragments.GlobalStateViewModel
import simple.bluetooth.terminal.SerialService.SerialBinder
import simple.bluetooth.terminal.TextUtil.HexWatcher
class TerminalFragment : Fragment(), ServiceConnection, SerialListener {
/** ViewModel used for output of information*/
private val globalStateViewModel: GlobalStateViewModel by activityViewModels()
/** ViewModel used for input of information*/
private val terminalStartStopViewModel: TerminalStartStopViewModel by activityViewModels()
private enum class Connected {
False, Pending, True
}
private var deviceAddress: String? = null
private var service: SerialService? = null
private var receiveText: TextView? = null
private var sendText: TextView? = null
private var hexWatcher: HexWatcher? = null
private var connected = Connected.False
private var initialStart = true
private val hexEnabled = false
private var pendingNewline = false
private val newline = TextUtil.newline_crlf
external fun setObjectReferenceAsGlobal(terminalFragment: TerminalFragment?): Boolean
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
retainInstance = true
deviceAddress = requireArguments().getString("device")
Log.d(TAG, String.format("Found device address: %s", deviceAddress))
// Log.i(TAG, "setting Global reference for JNI ")
// setObjectReferenceAsGlobal(this) /* This allows accessing the instance of TerminalFragment from the C++ Layer */
}
override fun onDestroy() {
super.onDestroy()
Log.e(TAG, "onDestroy()")
if (connected != Connected.False) disconnect()
requireActivity().stopService(Intent(activity, SerialService::class.java))
}
override fun onStart() {
super.onStart()
if (service != null) service!!.attach(this) else requireActivity().startService(
Intent(
activity,
SerialService::class.java
)
)
// prevents service destroy on unbind from recreated activity caused by orientation change
}
override fun onStop() {
if (service != null && !requireActivity().isChangingConfigurations) service!!.detach()
super.onStop()
}
override fun onAttach(activity: Activity) {
super.onAttach(activity)
requireActivity().bindService(
Intent(getActivity(), SerialService::class.java),
this,
Context.BIND_AUTO_CREATE
)
}
override fun onDetach() {
try {
requireActivity().unbindService(this)
} catch (ignored: Exception) {
}
super.onDetach()
}
override fun onResume() {
super.onResume()
if (initialStart && service != null) {
initialStart = false
requireActivity().runOnUiThread { connect() }
}
}
override fun onServiceConnected(name: ComponentName, binder: IBinder) {
service = (binder as SerialBinder).service
service?.attach(this)
if (initialStart && isResumed) {
initialStart = false
requireActivity().runOnUiThread { connect() }
}
}
override fun onServiceDisconnected(name: ComponentName) {
service = null
}
/*
* UI
*/
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
return inflater.inflate(R.layout.fragment_terminal, container, false)
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
receiveText =
view.findViewById(R.id.receive_text) // TextView performance decreases with number of spans
receiveText?.setTextColor(resources.getColor(R.color.colorRecieveText)) // set as default color to reduce number of spans
receiveText?.setMovementMethod(ScrollingMovementMethod.getInstance())
sendText = view.findViewById(R.id.send_text)
hexWatcher = HexWatcher(sendText)
hexWatcher!!.enable(hexEnabled)
sendText?.addTextChangedListener(hexWatcher)
sendText?.setHint(if (hexEnabled) "HEX mode" else "")
observeViewModel(view)
}
private fun observeViewModel(view: View) {
setupButtonOnClickListener(view)
terminalStartStopViewModel.getNextCommand().observe(viewLifecycleOwner, Observer { command ->
Log.v(TAG, " startStopViewModel.getNextCommand().observe")
send(command)
})
}
private fun setupButtonOnClickListener(view: View) {
// Hardcoded Start/Stop Signal Buttons. Sends "start" / "stop to ESP32
val sendStartSignalButton = view.findViewById<View>(R.id.bluetooth_send_start)
sendStartSignalButton.setOnClickListener { v: View? ->
send(
Constants.START_COMMAND_ESP32
)
}
val mainButtonsimulateIOError = view.findViewById<View>(R.id.bluetooth_crash_simulate)
mainButtonsimulateIOError.setOnClickListener { v: View? ->
// simulate connection loss for testing
onSerialIoError(java.lang.Exception("Totally a SerialIOError"))
}
val sendBtn = view.findViewById<View>(R.id.send_btn)
sendBtn.setOnClickListener { v: View? ->
send(sendText?.getText().toString())
}
}
/*
* Serial + UI
*/
private fun connect() {
try {
val bluetoothAdapter = BluetoothAdapter.getDefaultAdapter()
val device = bluetoothAdapter.getRemoteDevice(deviceAddress)
status("connecting...")
connected = Connected.Pending
val socket = SerialSocket(requireActivity().applicationContext, device)
service!!.connect(socket)
} catch (e: Exception) {
onSerialConnectError(e)
}
}
private fun disconnect() {
Log.d(TAG, "disconnect()")
connected = Connected.False
service!!.disconnect()
}
fun send(str: String) {
if (connected != Connected.True) {
Log.wtf(TAG, "Tried to send message but not connected!!")
return
}
try {
val msg: String
val data: ByteArray
if (hexEnabled) {
val sb = StringBuilder()
TextUtil.toHexString(sb, TextUtil.fromHexString(str))
TextUtil.toHexString(sb, newline.toByteArray())
msg = sb.toString()
data = TextUtil.fromHexString(msg)
} else {
msg = str
data = (str + newline).toByteArray()
}
val spn = SpannableStringBuilder(
"""
$msg
""".trimIndent()
)
spn.setSpan(
ForegroundColorSpan(resources.getColor(R.color.colorSendText)),
0,
spn.length,
Spannable.SPAN_EXCLUSIVE_EXCLUSIVE
)
receiveText!!.append(spn)
service!!.write(data)
} catch (e: Exception) {
Log.d(TAG, "exception while sending Text in method send(String str)")
onSerialIoError(e)
}
}
private fun receive(data: ByteArray) {
if (hexEnabled) {
receiveText!!.append(
"""
${TextUtil.toHexString(data)}
""".trimIndent()
)
} else {
var msg = String(data)
if (newline == TextUtil.newline_crlf && msg.length > 0) {
// don't show CR as ^M if directly before LF
msg = msg.replace(TextUtil.newline_crlf, TextUtil.newline_lf)
// special handling if CR and LF come in separate fragments
if (pendingNewline && msg[0] == '\n') {
val edt = receiveText!!.editableText
if (edt != null && edt.length > 1) edt.replace(edt.length - 2, edt.length, "")
}
pendingNewline = msg[msg.length - 1] == '\r'
}
val content: CharSequence = TextUtil.toCaretString(msg, newline.isNotEmpty())
if (content.contains(FROM_BINARY_START_COMMAND_ESP32)) {
Log.d(TAG, content.toString())
globalStateViewModel.setDriveState(FROM_BINARY_START_COMMAND_ESP32)
}
receiveText!!.append(content)
}
}
private fun status(str: String) {
val spn = SpannableStringBuilder(
"""
$str
""".trimIndent()
)
spn.setSpan(
ForegroundColorSpan(resources.getColor(R.color.colorStatusText)),
0,
spn.length,
Spannable.SPAN_EXCLUSIVE_EXCLUSIVE
)
receiveText!!.append(spn)
}
/*
* SerialListener
*/
override fun onSerialConnect() {
status("connected")
connected = Connected.True
}
override fun onSerialConnectError(e: Exception) {
status("connection failed: " + e.message)
disconnect()
startRecoverySequence()
}
override fun onSerialRead(data: ByteArray) {
receive(data)
}
override fun onSerialIoError(e: Exception) {
status("connection lost: " + e.message)
disconnect()
startRecoverySequence()
}
private fun startRecoverySequence() {
/** If things really go wrong, that is, we receive some type of Serial error,
* attempt to reconnect. Reconnect by returning back to DevicesFragment.
*
* There, implement a function to automatically re-connect */
Log.e(TAG, "startRecoverySequence")
val fragmentManager: FragmentManager = requireActivity().supportFragmentManager
val result = fragmentManager.popBackStackImmediate("terminal", POP_BACK_STACK_INCLUSIVE)
if (!result) {
Log.e(TAG, "There is nothing to pop")
}
}
companion object {
private const val TAG = "TerminalFragment"
private const val POP_BACK_STACK_INCLUSIVE = 1
// init {
// System.loadLibrary("nanodetncnn")
// }
}
}