diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 11d8e5e..e23cb28 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -13,8 +13,11 @@ + + + + + when (deviceType) { + Constants.DEVICE_TYPE_HOMOCUBE -> { + val intent = Intent(applicationContext, HemocubeActivity::class.java) + intent.putExtra(Constants.BLOOD_COLLECTING_METHOD, bloodCollectingMethod) + startActivity(intent) + } + + Constants.DEVICE_TYPE_TEST_RIGHT -> { + val intent = Intent(applicationContext, TestRightActivity::class.java) + intent.putExtra(Constants.BLOOD_COLLECTING_METHOD, bloodCollectingMethod) + startActivity(intent) + } + } + } + } +} \ No newline at end of file diff --git a/app/src/main/java/com/example/hpostesting/presentation/KitScanActivity.kt b/app/src/main/java/com/example/hpostesting/presentation/KitScanActivity.kt index 25640d7..1e150fb 100644 --- a/app/src/main/java/com/example/hpostesting/presentation/KitScanActivity.kt +++ b/app/src/main/java/com/example/hpostesting/presentation/KitScanActivity.kt @@ -10,8 +10,6 @@ import androidx.appcompat.app.AppCompatActivity import com.example.hpostesting.data.DataHolder import com.example.hpostesting.data.constant.Constants import com.example.hpostesting.presentation.dashboard.DashboardActivity -import com.example.hpostesting.presentation.hemocube.HemocubeActivity -import com.example.hpostesting.presentation.testRight.TestRightActivity import com.journeyapps.barcodescanner.ScanContract import com.journeyapps.barcodescanner.ScanIntentResult import com.journeyapps.barcodescanner.ScanOptions @@ -107,20 +105,8 @@ class KitScanActivity : AppCompatActivity() { } private fun moveToNext() { - - DataHolder.deviceType.observe(this) { deviceType -> - when (deviceType) { - Constants.DEVICE_TYPE_HOMOCUBE -> { - val i = Intent(applicationContext, HemocubeActivity::class.java) - startActivity(i) - } - - Constants.DEVICE_TYPE_TEST_RIGHT -> { - val i = Intent(applicationContext, TestRightActivity::class.java) - startActivity(i) - } - } - } + val i = Intent(applicationContext, BloodCollectingMethodActivity::class.java) + startActivity(i) } private fun startScanningNow() { diff --git a/app/src/main/java/com/example/hpostesting/presentation/hemocube/HemoCubeFragment.kt b/app/src/main/java/com/example/hpostesting/presentation/hemocube/HemoCubeFragment.kt index 54e5345..334346d 100644 --- a/app/src/main/java/com/example/hpostesting/presentation/hemocube/HemoCubeFragment.kt +++ b/app/src/main/java/com/example/hpostesting/presentation/hemocube/HemoCubeFragment.kt @@ -21,6 +21,7 @@ import com.example.hpostesting.presentation.UsbServiceListener import com.example.hpostesting.presentation.dashboard.DashboardActivity import com.example.hpostesting.presentation.utils.MyDialogListener import com.example.hpostesting.presentation.utils.UIUtils +import com.google.android.material.dialog.MaterialAlertDialogBuilder import com.google.firebase.firestore.FirebaseFirestore import com.google.firebase.firestore.ktx.firestore import com.google.firebase.ktx.Firebase @@ -43,7 +44,6 @@ class HemoCubeFragment : Fragment() { private var resultData: String = "" private var isUsingExistingBuffer = false private var isTestOngoing = false - private var bloodGroup: String = "" override fun onCreateView( inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle? ): View { @@ -242,8 +242,6 @@ class HemoCubeFragment : Fragment() { resultData += fullReadOutput.toString() when { - stringData.contains("ERROR 500") -> showError500Dialog() - stringData.contains("ERROR 501") -> showError501Dialog() stringData.contains("#Buffer Completed") -> showStartSampleDialog() stringData.contains("#Sample Completed") -> { activity?.runOnUiThread { @@ -272,38 +270,6 @@ class HemoCubeFragment : Fragment() { } } - private fun showError500Dialog() { - UIUtils.createAlertDialog(requireContext(), - "BUFFER ERROR", - "Do you want to continue with existing buffer?", - getString(R.string.noo), - "Yes", - object : MyDialogListener { - override fun onClickNegativeButton() {} - - override fun onClickPositiveButton() { - listenToHemoCube() - startBufferProcess() - } - }) - } - - private fun showError501Dialog() { - UIUtils.createAlertDialog(requireContext(), - "SAMPLE ERROR", - "Do you want to continue with Sample?", - getString(R.string.noo), - "Yes", - object : MyDialogListener { - override fun onClickNegativeButton() {} - - override fun onClickPositiveButton() { - listenToHemoCube() - startSampleProcess() - } - }) - } - private fun showStartSampleDialog() { activity?.runOnUiThread { UIUtils.createAlertDialog(requireContext(), @@ -324,62 +290,176 @@ class HemoCubeFragment : Fragment() { private fun handleValidResult(validString: String, fullReadOutput: String) { try { val result = validString.split(" ") - if (result.size == 8) { - val deviceSerialNo = result[2] - val led1BufferForDevice = if (isUsingExistingBuffer) { - sharedPreferences.getString(Constants.BUFFER_VALUE_1, "")?.toDoubleOrNull() - } else { - result[3].toDoubleOrNull() - } - val led2BufferForDevice = if (isUsingExistingBuffer) { - sharedPreferences.getString(Constants.BUFFER_VALUE_2, "")?.toDoubleOrNull() - } else { - result[4].toDoubleOrNull() - } - val version = BuildConfig.VERSION_NAME - val led1Sample = result[5].toDoubleOrNull() - val led2Sample = result[6].toDoubleOrNull() - val led1Average = log10(led1BufferForDevice?.div(led1Sample!!) ?: 0.0) - val led2Average = log10(led2BufferForDevice?.div(led2Sample!!) ?: 0.0) - val deviceRatio = led1Average / led2Average - if (led1BufferForDevice!! > 20000 && led1BufferForDevice < 24000 && led2BufferForDevice!! > 20000 && led2BufferForDevice < 24000) { - if (led1Sample!! < led1BufferForDevice && led1Sample < 24000 && led2Sample!! < led2BufferForDevice && led2Sample < 24000) { - if (deviceRatio < 0.08) { - if (deviceRatio > 1.07) { + if (result.size != 8) return - } - } - } - } - DataHolder.hemoCubeTestData?.apply { - deviceId = deviceSerialNo - led1Buffer = led1BufferForDevice - led2Buffer = led2BufferForDevice - appVersion = version - this.led1Sample = led1Sample - this.led2Sample = led2Sample - this.led1Average = led1Average - this.led2Average = led2Average - this.deviceRatio = deviceRatio - this.calculatedRatio = calculateRatio(deviceRatio) - this.resultData = fullReadOutput - if (!isBufferValueAvailable()) { - with(sharedPreferences.edit()) { - putString(Constants.BUFFER_VALUE_1, led1Buffer.toString()) - putString(Constants.BUFFER_VALUE_2, led2Buffer.toString()) - apply() - } - } - hemoCubeViewModel.uploadHemoCubeResultToDatabase( - isOnline, true, sharedPreferences.getString(Constants.KIT_NUMBER, "") + val deviceSerialNo = result[2] + val led1BufferForDevice = + getLedBufferValue(isUsingExistingBuffer, Constants.BUFFER_VALUE_1, result[3]) + val led2BufferForDevice = + getLedBufferValue(isUsingExistingBuffer, Constants.BUFFER_VALUE_2, result[4]) + val version = BuildConfig.VERSION_NAME + val led1Sample = result[5].toDoubleOrNull() + val led2Sample = result[6].toDoubleOrNull() + + val led1Average = calculateAverage(led1BufferForDevice, led1Sample) + val led2Average = calculateAverage(led2BufferForDevice, led2Sample) + val deviceRatio = led1Average / led2Average + + val isBufferValid = validateBuffer(led1BufferForDevice, led2BufferForDevice) + val isSampleValid = + validateSample(led1Sample, led1BufferForDevice, led2Sample, led2BufferForDevice) + + when { + !isBufferValid -> showError( + "Buffer error", + "Check for Empty curved, disconnected and reconnect the device, and please retest this sample again with a new blank" + ) + + !isSampleValid -> showError( + "Sample error", + "Check for Empty curved, disconnected and reconnect the device, and please retest this sample again with a new blank" + ) + + !validateBloodVolume(led1Average, led2Average) -> { + showError( + "Low Blood Volume", + "Please retest this sample with a higher volume of blood" ) } + + !validateHighBloodVolume(led1Average, led2Average) -> { + showError( + "High Blood Volume", + "Please retest this sample by diluting the sample in the buffer" + ) + } + + else -> processValidData( + deviceSerialNo, + led1BufferForDevice, + led2BufferForDevice, + version, + led1Sample, + led2Sample, + led1Average, + led2Average, + deviceRatio, + fullReadOutput + ) } } catch (_: Exception) { - + // handle exception or log it if necessary } + } + private fun getLedBufferValue( + usingExistingBuffer: Boolean, bufferConstant: String, resultValue: String + ): Double? { + return if (usingExistingBuffer) { + sharedPreferences.getString(bufferConstant, "")?.toDoubleOrNull() + } else { + resultValue.toDoubleOrNull() + } + } + + private fun calculateAverage(bufferForDevice: Double?, sample: Double?): Double { + return log10(bufferForDevice?.div(sample ?: 1.0) ?: 0.0) + } + + private fun validateBuffer(led1Buffer: Double?, led2Buffer: Double?): Boolean { + return led1Buffer!! in 20001.0..23999.0 && led2Buffer!! in 20001.0..23999.0 + } + + private fun validateSample( + sample1: Double?, buffer1: Double?, sample2: Double?, buffer2: Double? + ): Boolean { + return sample1 != null && sample1 < buffer1!! && sample1 < 24000 && sample2 != null && sample2 < buffer2!! && sample2 < 24000 + } + + private fun validateBloodVolume(average1: Double, average2: Double): Boolean { + return average1 > 0.08 && average2 > 0.08 + } + + private fun validateHighBloodVolume(average1: Double, average2: Double): Boolean { + return average1 < 1.07 && average2 < 1.07 + } + + private fun showError(title: String, message: String) { + customErrorDialogBox(title, message) + } + + private fun processValidData( + deviceId: String, + led1Buffer: Double?, + led2Buffer: Double?, + appVersion: String, + led1Sample: Double?, + led2Sample: Double?, + led1Average: Double, + led2Average: Double, + deviceRatio: Double, + resultData: String + ) { + val threshold = + if ((activity as HemocubeActivity).bloodCollectingMethod == Constants.FINGER_PRICK_BLOOD) 0.05 else 0.06 + + if (led1Average > threshold) { + DataHolder.hemoCubeTestData?.apply { + this.deviceId = deviceId + this.led1Buffer = led1Buffer + this.led2Buffer = led2Buffer + this.appVersion = appVersion + this.led1Sample = led1Sample + this.led2Sample = led2Sample + this.led1Average = led1Average + this.led2Average = led2Average + this.deviceRatio = deviceRatio + this.calculatedRatio = calculateRatio(deviceRatio) + this.resultData = resultData + + if (!isBufferValueAvailable()) { + with(sharedPreferences.edit()) { + putString(Constants.BUFFER_VALUE_1, led1Buffer.toString()) + putString(Constants.BUFFER_VALUE_2, led2Buffer.toString()) + apply() + } + } + hemoCubeViewModel.uploadHemoCubeResultToDatabase( + isOnline, true, sharedPreferences.getString(Constants.KIT_NUMBER, "") + ) + } + } else { + showError( + "Low Blood Volume", + "Please repeat the test with 10 µl of blood added to 1 ml of buffer" + ) + } + } + + private fun customErrorDialogBox(title: String, message: String) { + activity?.runOnUiThread { + MaterialAlertDialogBuilder(requireContext()).setTitle(title).setMessage(message) + .setPositiveButton("OK") { dialog, _ -> + if (title == "Buffer error" || title == "Sample error") { + with(sharedPreferences.edit()) { + putString(Constants.BUFFER_VALUE_1, "") + putString(Constants.BUFFER_VALUE_2, "") + apply() + } + finishTestingActivity() + } else { + finishTestingActivity() + } + dialog.dismiss() + }.show() + } + } + + private fun finishTestingActivity() { + val i = Intent(requireContext().applicationContext, DashboardActivity::class.java) + startActivity(i) + (activity as HemocubeActivity).finish() } private fun showToast(message: String) { diff --git a/app/src/main/java/com/example/hpostesting/presentation/hemocube/HemocubeActivity.kt b/app/src/main/java/com/example/hpostesting/presentation/hemocube/HemocubeActivity.kt index 6bb9efb..01f9d66 100644 --- a/app/src/main/java/com/example/hpostesting/presentation/hemocube/HemocubeActivity.kt +++ b/app/src/main/java/com/example/hpostesting/presentation/hemocube/HemocubeActivity.kt @@ -3,7 +3,6 @@ package com.example.hpostesting.presentation.hemocube import android.app.PendingIntent import android.content.BroadcastReceiver import android.content.ComponentName -import android.content.ContentValues.TAG import android.content.Context import android.content.Intent import android.content.IntentFilter @@ -11,29 +10,23 @@ import android.content.ServiceConnection import android.hardware.usb.UsbDevice import android.hardware.usb.UsbDeviceConnection import android.hardware.usb.UsbManager -import androidx.appcompat.app.AppCompatActivity import android.os.Bundle import android.os.IBinder import android.util.Log import android.view.Menu import android.widget.Toast import androidx.activity.viewModels +import androidx.appcompat.app.AppCompatActivity import androidx.core.content.ContextCompat import androidx.core.view.get import com.example.hpostesting.data.DataHolder import com.example.hpostesting.data.constant.Constants -import com.example.hpostesting.presentation.testRight.TestRightExpReference -import com.example.hpostesting.presentation.testRight.TestRightExpSample -import com.example.hpostesting.presentation.testRight.TestRightViewModel import com.example.hpostesting.presentation.testRight.UsbService -import com.google.android.gms.location.FusedLocationProviderClient -import com.google.android.gms.location.LocationCallback import com.hoho.android.usbserial.driver.UsbSerialDriver import com.hoho.android.usbserial.driver.UsbSerialProber import dagger.hilt.android.AndroidEntryPoint import `in`.sminnovations.hpostesting.R import `in`.sminnovations.hpostesting.databinding.ActivityHemocubeBinding -import `in`.sminnovations.hpostesting.databinding.ActivityMainBinding @AndroidEntryPoint class HemocubeActivity : AppCompatActivity() { @@ -47,6 +40,8 @@ class HemocubeActivity : AppCompatActivity() { private val TAG = "HemoCube" + var bloodCollectingMethod = "" + private val broadcastReceiver = object : BroadcastReceiver() { override fun onReceive(context: Context, intent: Intent) { @@ -84,6 +79,7 @@ class HemocubeActivity : AppCompatActivity() { super.onCreate(savedInstanceState) binding = ActivityHemocubeBinding.inflate(layoutInflater) setContentView(binding.root) + bloodCollectingMethod = intent.getStringExtra(Constants.BLOOD_COLLECTING_METHOD).toString() setSupportActionBar(binding.myToolbar) supportActionBar?.setDisplayHomeAsUpEnabled(true) setupListener() @@ -91,12 +87,14 @@ class HemocubeActivity : AppCompatActivity() { } private fun setupListener() { - DataHolder.usbConnected.observe(this){ + DataHolder.usbConnected.observe(this) { Log.d("USB OBSERVE", "HemoCube called -> $it") - if (it){ - myMenu?.get(0)?.icon = ContextCompat.getDrawable(this, R.drawable.ic_baseline_usb_24) + if (it) { + myMenu?.get(0)?.icon = + ContextCompat.getDrawable(this, R.drawable.ic_baseline_usb_24) } else { - myMenu?.get(0)?.icon = ContextCompat.getDrawable(this, R.drawable.ic_baseline_usb_off_24) + myMenu?.get(0)?.icon = + ContextCompat.getDrawable(this, R.drawable.ic_baseline_usb_off_24) Toast.makeText(this, "Device is Disconnected", Toast.LENGTH_SHORT).show() } } @@ -123,16 +121,15 @@ class HemocubeActivity : AppCompatActivity() { private fun requestUserPermission(manager: UsbManager, device: UsbDevice) { - val mPendingIntent: PendingIntent - if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.S) { - mPendingIntent = PendingIntent.getBroadcast( + val mPendingIntent: PendingIntent = if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.S) { + PendingIntent.getBroadcast( this, 0, Intent(Constants.HOMOCUBE_USB_PERMISSION), PendingIntent.FLAG_MUTABLE ) } else { - mPendingIntent = PendingIntent.getBroadcast( + PendingIntent.getBroadcast( this, 0, Intent(Constants.HOMOCUBE_USB_PERMISSION), @@ -169,6 +166,7 @@ class HemocubeActivity : AppCompatActivity() { menuInflater.inflate(R.menu.my_menu, menu) return true } + override fun onDestroy() { super.onDestroy() if (viewModel.isServiceConnected) { diff --git a/app/src/main/res/layout/activity_blood_collecting_method.xml b/app/src/main/res/layout/activity_blood_collecting_method.xml new file mode 100644 index 0000000..f37dd2b --- /dev/null +++ b/app/src/main/res/layout/activity_blood_collecting_method.xml @@ -0,0 +1,74 @@ + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/app/src/main/res/layout/activity_kit_scan.xml b/app/src/main/res/layout/activity_kit_scan.xml index aa84355..a177111 100644 --- a/app/src/main/res/layout/activity_kit_scan.xml +++ b/app/src/main/res/layout/activity_kit_scan.xml @@ -4,7 +4,7 @@ xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" - tools:context=".presentation.KitScanActivity"> + tools:context="com.example.hpostesting.presentation.KitScanActivity"> Test Completed Select Blood Group All registered user are tested successfully + Venous Blood + Finger Prick Blood + Select blood collecting method \ No newline at end of file