Improved logic

This commit is contained in:
mohamedkaif356
2023-07-25 13:42:29 +05:30
parent e2e9c28efb
commit ebf212d33a
4 changed files with 69 additions and 65 deletions

View File

@@ -14,7 +14,7 @@ android {
namespace 'in.sminnovations.hpostesting' namespace 'in.sminnovations.hpostesting'
defaultConfig { defaultConfig {
applicationId "in.sminnovations.hpostesting.prod" applicationId "in.sminnovations.hpostesting"
minSdk 21 minSdk 21
targetSdk 33 targetSdk 33
versionCode 2 versionCode 2

View File

@@ -23,6 +23,7 @@ object Constants {
const val DEVICE_PRODUCT_ID = 24597 const val DEVICE_PRODUCT_ID = 24597
const val DEVICE_TYPE_HOMOCUBE = "HOMOCUBE" const val DEVICE_TYPE_HOMOCUBE = "HOMOCUBE"
const val DEVICE_TYPE_TEST_RIGHT = "TEST_RIGHT"
const val DEVICE_VENDOR_ID = 1027 const val DEVICE_VENDOR_ID = 1027
const val HOMO_CUBE_ID = 29987 const val HOMO_CUBE_ID = 29987

View File

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

View File

@@ -43,7 +43,7 @@ class HemoCubeViewModel@Inject constructor(
if (isOnline) { if (isOnline) {
try { try {
addResultTestToDb() addResultTestToDb()
} catch (exception: Exception) { } catch (_: Exception) {
} }
} else { } else {
testDetails?.testTime = SimpleDateFormat( testDetails?.testTime = SimpleDateFormat(