|
|
|
|
@@ -1,10 +1,7 @@
|
|
|
|
|
package com.example.hpostesting.presentation
|
|
|
|
|
|
|
|
|
|
import android.Manifest
|
|
|
|
|
import android.content.BroadcastReceiver
|
|
|
|
|
import android.content.Context
|
|
|
|
|
import android.content.Intent
|
|
|
|
|
import android.content.IntentFilter
|
|
|
|
|
import android.content.*
|
|
|
|
|
import android.content.pm.PackageManager
|
|
|
|
|
import android.hardware.usb.UsbManager
|
|
|
|
|
import android.location.Location
|
|
|
|
|
@@ -21,11 +18,7 @@ import com.example.hpostesting.data.DataHolder
|
|
|
|
|
import com.example.hpostesting.data.constant.Constants
|
|
|
|
|
import com.example.hpostesting.data.model.patient.UserData
|
|
|
|
|
import com.example.hpostesting.data.model.test.TestType
|
|
|
|
|
import com.google.android.gms.location.FusedLocationProviderClient
|
|
|
|
|
import com.google.android.gms.location.LocationCallback
|
|
|
|
|
import com.google.android.gms.location.LocationRequest
|
|
|
|
|
import com.google.android.gms.location.LocationResult
|
|
|
|
|
import com.google.android.gms.location.LocationServices
|
|
|
|
|
import com.google.android.gms.location.*
|
|
|
|
|
import com.hoho.android.usbserial.driver.UsbSerialProber
|
|
|
|
|
import `in`.sminnovations.hpostesting.R
|
|
|
|
|
import `in`.sminnovations.hpostesting.databinding.ActivityMainBinding
|
|
|
|
|
@@ -33,22 +26,18 @@ import `in`.sminnovations.hpostesting.databinding.ActivityMainBinding
|
|
|
|
|
class MainActivity : AppCompatActivity() {
|
|
|
|
|
|
|
|
|
|
private lateinit var binding: ActivityMainBinding
|
|
|
|
|
|
|
|
|
|
private var myMenu: Menu? = null
|
|
|
|
|
|
|
|
|
|
private val TAG = "MainActivity"
|
|
|
|
|
|
|
|
|
|
private lateinit var fusedLocationClient: FusedLocationProviderClient
|
|
|
|
|
private lateinit var locationCallback: LocationCallback
|
|
|
|
|
|
|
|
|
|
private var mUsbReceiver: BroadcastReceiver = object : BroadcastReceiver() {
|
|
|
|
|
private val usbReceiver = object : BroadcastReceiver() {
|
|
|
|
|
override fun onReceive(context: Context?, intent: Intent) {
|
|
|
|
|
val action = intent.action
|
|
|
|
|
if (UsbManager.ACTION_USB_DEVICE_DETACHED == action) {
|
|
|
|
|
DataHolder.usbConnected.value = false
|
|
|
|
|
} else if (UsbManager.ACTION_USB_DEVICE_ATTACHED == action) {
|
|
|
|
|
DataHolder.usbConnected.value = true
|
|
|
|
|
when (intent.action) {
|
|
|
|
|
UsbManager.ACTION_USB_DEVICE_DETACHED -> DataHolder.usbConnected.value = false
|
|
|
|
|
UsbManager.ACTION_USB_DEVICE_ATTACHED -> DataHolder.usbConnected.value = true
|
|
|
|
|
}
|
|
|
|
|
checkAndUpdateUsbConnection()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@@ -72,26 +61,21 @@ class MainActivity : AppCompatActivity() {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
val filter = IntentFilter().apply {
|
|
|
|
|
val usbFilter = IntentFilter().apply {
|
|
|
|
|
addAction(UsbManager.ACTION_USB_DEVICE_DETACHED)
|
|
|
|
|
addAction(UsbManager.ACTION_USB_DEVICE_ATTACHED)
|
|
|
|
|
}
|
|
|
|
|
registerReceiver(mUsbReceiver, filter)
|
|
|
|
|
registerReceiver(usbReceiver, usbFilter)
|
|
|
|
|
|
|
|
|
|
DataHolder.deviceType.observe(this) { deviceType ->
|
|
|
|
|
when (deviceType) {
|
|
|
|
|
Constants.DEVICE_TYPE_HOMOCUBE -> {
|
|
|
|
|
binding.cvItem1.visibility = View.VISIBLE
|
|
|
|
|
binding.cvItem3.visibility = View.VISIBLE
|
|
|
|
|
binding.cvItem2.visibility = View.GONE
|
|
|
|
|
binding.cvItem4.visibility = View.GONE
|
|
|
|
|
Toast.makeText(this, "HomoCube Connected", Toast.LENGTH_SHORT).show()
|
|
|
|
|
DataHolder.usbConnected.value = true
|
|
|
|
|
DataHolder.deviceType.postValue(Constants.DEVICE_TYPE_HOMOCUBE)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
"Test" -> {
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
with(binding) {
|
|
|
|
|
cvItem1.visibility =
|
|
|
|
|
if (deviceType == Constants.DEVICE_TYPE_HOMOCUBE) View.VISIBLE else View.GONE
|
|
|
|
|
cvItem2.visibility =
|
|
|
|
|
if (deviceType == Constants.DEVICE_TYPE_TEST_RIGHT) View.VISIBLE else View.GONE
|
|
|
|
|
cvItem3.visibility = View.VISIBLE
|
|
|
|
|
cvItem4.visibility = View.VISIBLE
|
|
|
|
|
DataHolder.usbConnected.value = true
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
@@ -99,40 +83,59 @@ class MainActivity : AppCompatActivity() {
|
|
|
|
|
private fun checkAndUpdateUsbConnection() {
|
|
|
|
|
val availableDrivers = UsbSerialProber.getDefaultProber()
|
|
|
|
|
.findAllDrivers(getSystemService(Context.USB_SERVICE) as UsbManager)
|
|
|
|
|
if (availableDrivers.isNotEmpty() && availableDrivers[0].device.productId == Constants.DEVICE_PRODUCT_ID && availableDrivers[0].device.vendorId == Constants.DEVICE_VENDOR_ID) {
|
|
|
|
|
Log.d(
|
|
|
|
|
TAG,
|
|
|
|
|
"Device matched -> product = ${availableDrivers[0].device.productId}, vendor = ${availableDrivers[0].device.vendorId}"
|
|
|
|
|
)
|
|
|
|
|
// Log.d("mariya", availableDrivers[0].device.toString())
|
|
|
|
|
if (availableDrivers.isNotEmpty() && availableDrivers[0].device.productId == Constants.HOMO_CUBE_ID && availableDrivers[0].device.vendorId == Constants.VENDOR_ID) {
|
|
|
|
|
Log.e("HomoCube", "product1")
|
|
|
|
|
binding.cvItem1.visibility = View.VISIBLE
|
|
|
|
|
binding.cvItem3.visibility = View.VISIBLE
|
|
|
|
|
binding.cvItem2.visibility = View.GONE
|
|
|
|
|
binding.cvItem4.visibility = View.GONE
|
|
|
|
|
Toast.makeText(this, "HomoCube Connected", Toast.LENGTH_SHORT).show()
|
|
|
|
|
DataHolder.usbConnected.value = true
|
|
|
|
|
DataHolder.deviceType.postValue(Constants.DEVICE_TYPE_HOMOCUBE)
|
|
|
|
|
} else if (availableDrivers.isNotEmpty() && availableDrivers[0].device.productId == Constants.DEVICE_PRODUCT_ID && availableDrivers[0].device.vendorId == Constants.DEVICE_VENDOR_ID) {
|
|
|
|
|
Log.d(
|
|
|
|
|
TAG,
|
|
|
|
|
"Device matched -> product = ${availableDrivers[0].device.productId}, vendor = ${availableDrivers[0].device.vendorId}"
|
|
|
|
|
)
|
|
|
|
|
DataHolder.usbConnected.value = true
|
|
|
|
|
} else if (availableDrivers.isNotEmpty()) {
|
|
|
|
|
Log.d(
|
|
|
|
|
TAG,
|
|
|
|
|
"Device NOT matched -> product = ${availableDrivers[0].device.productId}, vendor = ${availableDrivers[0].device.vendorId}"
|
|
|
|
|
)
|
|
|
|
|
Toast.makeText(this, "Device Not Compatible", Toast.LENGTH_SHORT).show()
|
|
|
|
|
DataHolder.usbConnected.value = false
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
|
|
val deviceType = when {
|
|
|
|
|
availableDrivers.isNotEmpty() -> {
|
|
|
|
|
val device = availableDrivers[0].device
|
|
|
|
|
when {
|
|
|
|
|
device.productId == Constants.HOMO_CUBE_ID && device.vendorId == Constants.VENDOR_ID -> {
|
|
|
|
|
binding.cvItem1.visibility = View.VISIBLE
|
|
|
|
|
binding.cvItem3.visibility = View.VISIBLE
|
|
|
|
|
binding.cvItem2.visibility = View.GONE
|
|
|
|
|
binding.cvItem4.visibility = View.GONE
|
|
|
|
|
DataHolder.deviceType.postValue(Constants.DEVICE_TYPE_HOMOCUBE)
|
|
|
|
|
Constants.DEVICE_TYPE_HOMOCUBE
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
device.productId == Constants.DEVICE_PRODUCT_ID && device.vendorId == Constants.DEVICE_VENDOR_ID -> {
|
|
|
|
|
Log.d(
|
|
|
|
|
TAG,
|
|
|
|
|
"Device matched -> product = ${device.productId}, vendor = ${device.vendorId}"
|
|
|
|
|
)
|
|
|
|
|
binding.cvItem1.visibility = View.VISIBLE
|
|
|
|
|
binding.cvItem3.visibility = View.VISIBLE
|
|
|
|
|
binding.cvItem2.visibility = View.VISIBLE
|
|
|
|
|
binding.cvItem4.visibility = View.VISIBLE
|
|
|
|
|
DataHolder.deviceType.postValue(Constants.DEVICE_TYPE_TEST_RIGHT)
|
|
|
|
|
Constants.DEVICE_TYPE_TEST_RIGHT
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
else -> {
|
|
|
|
|
Log.d(
|
|
|
|
|
TAG,
|
|
|
|
|
"Device NOT matched -> product = ${device.productId}, vendor = ${device.vendorId}"
|
|
|
|
|
)
|
|
|
|
|
val message =
|
|
|
|
|
if (device.productId == Constants.DEVICE_PRODUCT_ID) "Device Not Compatible" else "Device Not Connected"
|
|
|
|
|
Toast.makeText(this, message, Toast.LENGTH_SHORT).show()
|
|
|
|
|
DataHolder.usbConnected.value = false
|
|
|
|
|
null
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
else -> {
|
|
|
|
|
Log.d(TAG, "Device NOT Connected")
|
|
|
|
|
Toast.makeText(this, "Device Not Connected", Toast.LENGTH_SHORT).show()
|
|
|
|
|
DataHolder.usbConnected.value = false
|
|
|
|
|
null
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
deviceType?.let {
|
|
|
|
|
DataHolder.usbConnected.value = true
|
|
|
|
|
DataHolder.deviceType.postValue(it)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private fun setupListeners() {
|
|
|
|
|
|