From 173d4a27677e34eb8ab4501ad6c5dbd4ca759380 Mon Sep 17 00:00:00 2001 From: Pritimay Sarkar Date: Sun, 4 Feb 2024 14:45:37 +0530 Subject: [PATCH] save dac values and refactor --- app/build.gradle | 2 + .../hpostesting/data/constant/Constants.kt | 2 +- .../presentation/autodac/AutoDacActivity.kt | 6 +- .../presentation/autodac/AutoDacFragment.kt | 25 +++---- .../HemocubeBufferCheckActivity.kt | 6 +- .../calibration/CalibrationActivity.kt | 6 +- .../presentation/deviceinfo/DeviceActivity.kt | 10 +-- .../DeviceProvisionActivity.kt | 6 +- .../diagnostics/DiagnosticsActivity.kt | 8 +-- .../presentation/hemocube/HemocubeActivity.kt | 6 +- .../presentation/trueheme/TrueHemeActivity.kt | 8 +-- .../presentation/trueheme/TrueHemeFragment.kt | 2 + .../hpostesting/AuthInterceptorTest.kt | 65 +++++++++++++++++++ 13 files changed, 108 insertions(+), 44 deletions(-) create mode 100644 app/src/test/java/com/example/hpostesting/AuthInterceptorTest.kt diff --git a/app/build.gradle b/app/build.gradle index a1fb725..0fc5ccb 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -112,6 +112,8 @@ dependencies { androidTestImplementation 'org.mockito:mockito-android:3.12.4' androidTestImplementation 'org.mockito:mockito-inline:3.12.4' androidTestImplementation 'org.mockito:mockito-android:3.12.4' + testImplementation 'org.powermock:powermock-api-mockito2:2.0.9' + testImplementation 'org.powermock:powermock-module-junit4:2.0.9' testImplementation "androidx.arch.core:core-testing:2.2.0" testImplementation 'org.jetbrains.kotlinx:kotlinx-coroutines-test:1.7.1' diff --git a/app/src/main/java/com/example/hpostesting/data/constant/Constants.kt b/app/src/main/java/com/example/hpostesting/data/constant/Constants.kt index 48813c9..d8aed49 100644 --- a/app/src/main/java/com/example/hpostesting/data/constant/Constants.kt +++ b/app/src/main/java/com/example/hpostesting/data/constant/Constants.kt @@ -2,7 +2,7 @@ package com.example.hpostesting.data.constant object Constants { const val ACTION_USB_PERMISSION = "shanmukha.in.sickle_cell.USB_PERMISSION" - const val HOMOCUBE_USB_PERMISSION = "shanmukha.in.sickle_cell_homocube.USB_PERMISSION" + const val HEMOCUBE_USB_PERMISSION = "shanmukha.in.sickle_cell_homocube.USB_PERMISSION" const val BASE_URL = "www.google.com" diff --git a/app/src/main/java/com/example/hpostesting/presentation/autodac/AutoDacActivity.kt b/app/src/main/java/com/example/hpostesting/presentation/autodac/AutoDacActivity.kt index fcef9bf..4ff6e46 100644 --- a/app/src/main/java/com/example/hpostesting/presentation/autodac/AutoDacActivity.kt +++ b/app/src/main/java/com/example/hpostesting/presentation/autodac/AutoDacActivity.kt @@ -145,18 +145,18 @@ class AutoDacActivity: AppCompatActivity() { val mPendingIntent: PendingIntent if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.S) { mPendingIntent = PendingIntent.getBroadcast( - this, 0, Intent(Constants.HOMOCUBE_USB_PERMISSION), PendingIntent.FLAG_MUTABLE + this, 0, Intent(Constants.HEMOCUBE_USB_PERMISSION), PendingIntent.FLAG_MUTABLE ) } else { mPendingIntent = PendingIntent.getBroadcast( this, 0, - Intent(Constants.HOMOCUBE_USB_PERMISSION), + Intent(Constants.HEMOCUBE_USB_PERMISSION), PendingIntent.FLAG_ONE_SHOT or PendingIntent.FLAG_IMMUTABLE ) } - val filter = IntentFilter(Constants.HOMOCUBE_USB_PERMISSION) + val filter = IntentFilter(Constants.HEMOCUBE_USB_PERMISSION) registerReceiver(broadcastReceiver, filter) manager.requestPermission(device, mPendingIntent) } diff --git a/app/src/main/java/com/example/hpostesting/presentation/autodac/AutoDacFragment.kt b/app/src/main/java/com/example/hpostesting/presentation/autodac/AutoDacFragment.kt index 477af58..71f4d2c 100644 --- a/app/src/main/java/com/example/hpostesting/presentation/autodac/AutoDacFragment.kt +++ b/app/src/main/java/com/example/hpostesting/presentation/autodac/AutoDacFragment.kt @@ -12,11 +12,15 @@ import androidx.fragment.app.activityViewModels import androidx.lifecycle.MutableLiveData import com.example.hpostesting.data.constant.Constants import com.example.hpostesting.data.constant.HemoCubeCommands +import com.example.hpostesting.data.model.diagnostics.DiagnosticsData import com.example.hpostesting.data.model.patient.DeviceData import com.example.hpostesting.presentation.UsbServiceListener import com.google.firebase.crashlytics.ktx.crashlytics import com.google.firebase.ktx.Firebase import `in`.sminnovations.hpostesting.databinding.FragmentAutoDacBinding +import java.text.SimpleDateFormat +import java.util.Calendar +import java.util.Locale class AutoDacFragment: Fragment() { @@ -141,22 +145,19 @@ class AutoDacFragment: Fragment() { } if (resultData.contains("#CC")) { + autoDacViewModel.addAutoDacDataToDb( + DiagnosticsData( + deviceId = sharedPreferences.getString(Constants.DEVICE_ID, "").toString(), + deviceData = resultData, + runTime = SimpleDateFormat( + "yyyy-MM-dd HH:mm:ss", Locale.getDefault() + ).format(Calendar.getInstance().time) + ) + ) activity?.runOnUiThread { binding.ivCheck.visibility = View.VISIBLE } } - - if (resultData.contains("END") || fullReadOutput.contains("END")) { -// autoDacViewModel.addAutoDacDataToDb( -// DiagnosticsData( -// deviceId = sharedPreferences.getString(Constants.DEVICE_ID, "").toString(), -// deviceData = resultData, -// runTime = SimpleDateFormat( -// "yyyy-MM-dd HH:mm:ss", Locale.getDefault() -// ).format(Calendar.getInstance().time) -// ) -// ) - } } override fun onUsbError(e: Exception?) { diff --git a/app/src/main/java/com/example/hpostesting/presentation/buffercheck/HemocubeBufferCheckActivity.kt b/app/src/main/java/com/example/hpostesting/presentation/buffercheck/HemocubeBufferCheckActivity.kt index 291ba1c..7372588 100644 --- a/app/src/main/java/com/example/hpostesting/presentation/buffercheck/HemocubeBufferCheckActivity.kt +++ b/app/src/main/java/com/example/hpostesting/presentation/buffercheck/HemocubeBufferCheckActivity.kt @@ -129,18 +129,18 @@ open class HemocubeBufferCheckActivity : AppCompatActivity() { private fun requestUserPermission(manager: UsbManager, device: UsbDevice) { 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 + this, 0, Intent(Constants.HEMOCUBE_USB_PERMISSION), PendingIntent.FLAG_MUTABLE ) } else { PendingIntent.getBroadcast( this, 0, - Intent(Constants.HOMOCUBE_USB_PERMISSION), + Intent(Constants.HEMOCUBE_USB_PERMISSION), PendingIntent.FLAG_ONE_SHOT or PendingIntent.FLAG_IMMUTABLE ) } - val filter = IntentFilter(Constants.HOMOCUBE_USB_PERMISSION) + val filter = IntentFilter(Constants.HEMOCUBE_USB_PERMISSION) registerReceiver(broadcastReceiver, filter) manager.requestPermission(device, mPendingIntent) } diff --git a/app/src/main/java/com/example/hpostesting/presentation/calibration/CalibrationActivity.kt b/app/src/main/java/com/example/hpostesting/presentation/calibration/CalibrationActivity.kt index ec2f1a7..f164a64 100644 --- a/app/src/main/java/com/example/hpostesting/presentation/calibration/CalibrationActivity.kt +++ b/app/src/main/java/com/example/hpostesting/presentation/calibration/CalibrationActivity.kt @@ -144,18 +144,18 @@ class CalibrationActivity: AppCompatActivity() { private fun requestUserPermission(manager: UsbManager, device: UsbDevice) { 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 + this, 0, Intent(Constants.HEMOCUBE_USB_PERMISSION), PendingIntent.FLAG_MUTABLE ) } else { PendingIntent.getBroadcast( this, 0, - Intent(Constants.HOMOCUBE_USB_PERMISSION), + Intent(Constants.HEMOCUBE_USB_PERMISSION), PendingIntent.FLAG_ONE_SHOT or PendingIntent.FLAG_IMMUTABLE ) } - val filter = IntentFilter(Constants.HOMOCUBE_USB_PERMISSION) + val filter = IntentFilter(Constants.HEMOCUBE_USB_PERMISSION) registerReceiver(broadcastReceiver, filter) manager.requestPermission(device, mPendingIntent) } diff --git a/app/src/main/java/com/example/hpostesting/presentation/deviceinfo/DeviceActivity.kt b/app/src/main/java/com/example/hpostesting/presentation/deviceinfo/DeviceActivity.kt index a5d1de8..60db0cd 100644 --- a/app/src/main/java/com/example/hpostesting/presentation/deviceinfo/DeviceActivity.kt +++ b/app/src/main/java/com/example/hpostesting/presentation/deviceinfo/DeviceActivity.kt @@ -24,15 +24,11 @@ import androidx.core.view.get import com.example.hpostesting.data.DataHolder import com.example.hpostesting.data.constant.Constants import com.example.hpostesting.data.constant.LanguageManager -import com.example.hpostesting.presentation.calibration.CalibrationFragment -import com.example.hpostesting.presentation.calibration.CalibrationViewModel -import com.example.hpostesting.presentation.hemocube.HemoCubeViewModel import com.example.hpostesting.presentation.testRight.UsbService 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.ActivityCalibrationBinding import `in`.sminnovations.hpostesting.databinding.ActivityDeviceBinding @Suppress("MemberVisibilityCanBePrivate") @@ -148,18 +144,18 @@ class DeviceActivity : AppCompatActivity() { val mPendingIntent: PendingIntent if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.S) { mPendingIntent = PendingIntent.getBroadcast( - this, 0, Intent(Constants.HOMOCUBE_USB_PERMISSION), PendingIntent.FLAG_MUTABLE + this, 0, Intent(Constants.HEMOCUBE_USB_PERMISSION), PendingIntent.FLAG_MUTABLE ) } else { mPendingIntent = PendingIntent.getBroadcast( this, 0, - Intent(Constants.HOMOCUBE_USB_PERMISSION), + Intent(Constants.HEMOCUBE_USB_PERMISSION), PendingIntent.FLAG_ONE_SHOT or PendingIntent.FLAG_IMMUTABLE ) } - val filter = IntentFilter(Constants.HOMOCUBE_USB_PERMISSION) + val filter = IntentFilter(Constants.HEMOCUBE_USB_PERMISSION) registerReceiver(broadcastReceiver, filter) manager.requestPermission(device, mPendingIntent) } diff --git a/app/src/main/java/com/example/hpostesting/presentation/deviceprovision/DeviceProvisionActivity.kt b/app/src/main/java/com/example/hpostesting/presentation/deviceprovision/DeviceProvisionActivity.kt index 4509bb1..1e316ff 100644 --- a/app/src/main/java/com/example/hpostesting/presentation/deviceprovision/DeviceProvisionActivity.kt +++ b/app/src/main/java/com/example/hpostesting/presentation/deviceprovision/DeviceProvisionActivity.kt @@ -144,18 +144,18 @@ class DeviceProvisionActivity : AppCompatActivity() { private fun requestUserPermission(manager: UsbManager, device: UsbDevice) { 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 + this, 0, Intent(Constants.HEMOCUBE_USB_PERMISSION), PendingIntent.FLAG_MUTABLE ) } else { PendingIntent.getBroadcast( this, 0, - Intent(Constants.HOMOCUBE_USB_PERMISSION), + Intent(Constants.HEMOCUBE_USB_PERMISSION), PendingIntent.FLAG_ONE_SHOT or PendingIntent.FLAG_IMMUTABLE ) } - val filter = IntentFilter(Constants.HOMOCUBE_USB_PERMISSION) + val filter = IntentFilter(Constants.HEMOCUBE_USB_PERMISSION) registerReceiver(broadcastReceiver, filter) manager.requestPermission(device, mPendingIntent) } diff --git a/app/src/main/java/com/example/hpostesting/presentation/diagnostics/DiagnosticsActivity.kt b/app/src/main/java/com/example/hpostesting/presentation/diagnostics/DiagnosticsActivity.kt index 8514b62..c400280 100644 --- a/app/src/main/java/com/example/hpostesting/presentation/diagnostics/DiagnosticsActivity.kt +++ b/app/src/main/java/com/example/hpostesting/presentation/diagnostics/DiagnosticsActivity.kt @@ -23,8 +23,6 @@ import androidx.core.view.get import com.example.hpostesting.data.DataHolder import com.example.hpostesting.data.constant.Constants import com.example.hpostesting.data.constant.LanguageManager -import com.example.hpostesting.presentation.hemocube.HemoCubeFragment -import com.example.hpostesting.presentation.hemocube.HemoCubeViewModel import com.example.hpostesting.presentation.testRight.UsbService import com.hoho.android.usbserial.driver.UsbSerialDriver import com.hoho.android.usbserial.driver.UsbSerialProber @@ -148,18 +146,18 @@ class DiagnosticsActivity : AppCompatActivity() { val mPendingIntent: PendingIntent if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.S) { mPendingIntent = PendingIntent.getBroadcast( - this, 0, Intent(Constants.HOMOCUBE_USB_PERMISSION), PendingIntent.FLAG_MUTABLE + this, 0, Intent(Constants.HEMOCUBE_USB_PERMISSION), PendingIntent.FLAG_MUTABLE ) } else { mPendingIntent = PendingIntent.getBroadcast( this, 0, - Intent(Constants.HOMOCUBE_USB_PERMISSION), + Intent(Constants.HEMOCUBE_USB_PERMISSION), PendingIntent.FLAG_ONE_SHOT or PendingIntent.FLAG_IMMUTABLE ) } - val filter = IntentFilter(Constants.HOMOCUBE_USB_PERMISSION) + val filter = IntentFilter(Constants.HEMOCUBE_USB_PERMISSION) registerReceiver(broadcastReceiver, filter) manager.requestPermission(device, mPendingIntent) } 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 64e5123..cb3d223 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 @@ -130,18 +130,18 @@ open class HemocubeActivity : AppCompatActivity() { val mPendingIntent: PendingIntent if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.S) { mPendingIntent = PendingIntent.getBroadcast( - this, 0, Intent(Constants.HOMOCUBE_USB_PERMISSION), PendingIntent.FLAG_MUTABLE + this, 0, Intent(Constants.HEMOCUBE_USB_PERMISSION), PendingIntent.FLAG_MUTABLE ) } else { mPendingIntent = PendingIntent.getBroadcast( this, 0, - Intent(Constants.HOMOCUBE_USB_PERMISSION), + Intent(Constants.HEMOCUBE_USB_PERMISSION), PendingIntent.FLAG_ONE_SHOT or PendingIntent.FLAG_IMMUTABLE ) } - val filter = IntentFilter(Constants.HOMOCUBE_USB_PERMISSION) + val filter = IntentFilter(Constants.HEMOCUBE_USB_PERMISSION) registerReceiver(broadcastReceiver, filter) manager.requestPermission(device, mPendingIntent) } diff --git a/app/src/main/java/com/example/hpostesting/presentation/trueheme/TrueHemeActivity.kt b/app/src/main/java/com/example/hpostesting/presentation/trueheme/TrueHemeActivity.kt index 465abb7..fb9d678 100644 --- a/app/src/main/java/com/example/hpostesting/presentation/trueheme/TrueHemeActivity.kt +++ b/app/src/main/java/com/example/hpostesting/presentation/trueheme/TrueHemeActivity.kt @@ -127,23 +127,23 @@ class TrueHemeActivity : AppCompatActivity() { } - @SuppressLint("MutableImplicitPendingIntent") + @SuppressLint("MutableImplicitPendingIntent", "UnspecifiedRegisterReceiverFlag") 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( - this, 0, Intent(Constants.HOMOCUBE_USB_PERMISSION), PendingIntent.FLAG_MUTABLE + this, 0, Intent(Constants.HEMOCUBE_USB_PERMISSION), PendingIntent.FLAG_MUTABLE ) } else { mPendingIntent = PendingIntent.getBroadcast( this, 0, - Intent(Constants.HOMOCUBE_USB_PERMISSION), + Intent(Constants.HEMOCUBE_USB_PERMISSION), PendingIntent.FLAG_ONE_SHOT or PendingIntent.FLAG_IMMUTABLE ) } - val filter = IntentFilter(Constants.HOMOCUBE_USB_PERMISSION) + val filter = IntentFilter(Constants.HEMOCUBE_USB_PERMISSION) registerReceiver(broadcastReceiver, filter) manager.requestPermission(device, mPendingIntent) } diff --git a/app/src/main/java/com/example/hpostesting/presentation/trueheme/TrueHemeFragment.kt b/app/src/main/java/com/example/hpostesting/presentation/trueheme/TrueHemeFragment.kt index be89edf..d1a4bf8 100644 --- a/app/src/main/java/com/example/hpostesting/presentation/trueheme/TrueHemeFragment.kt +++ b/app/src/main/java/com/example/hpostesting/presentation/trueheme/TrueHemeFragment.kt @@ -1,5 +1,6 @@ package com.example.hpostesting.presentation.trueheme +import android.annotation.SuppressLint import android.content.Context import android.content.Intent import android.content.SharedPreferences @@ -33,6 +34,7 @@ import kotlin.math.abs import kotlin.math.log10 @Suppress("MemberVisibilityCanBePrivate") +@SuppressLint("SetTextI18n") class TrueHemeFragment : Fragment() { private lateinit var binding: FragmentTruehemeSampleBinding diff --git a/app/src/test/java/com/example/hpostesting/AuthInterceptorTest.kt b/app/src/test/java/com/example/hpostesting/AuthInterceptorTest.kt new file mode 100644 index 0000000..16cf91c --- /dev/null +++ b/app/src/test/java/com/example/hpostesting/AuthInterceptorTest.kt @@ -0,0 +1,65 @@ +package com.example.hpostesting + +// +//@PrepareForTest(Object::class) +//class AuthInterceptorTest { +// +//// @Rule +//// val rule = PowerMockRule() +// +// @Mock +// private lateinit var sharedPreferences: SharedPreferences +// +// @Mock +// private lateinit var chain: Interceptor.Chain +// +// @Mock +// private lateinit var request: okhttp3.Request +// +// @Mock +// private lateinit var response: Response +// +// @Captor +// private lateinit var captor: ArgumentCaptor +// +// private lateinit var authInterceptor: AuthInterceptor +// +// @Before +// fun setup() { +// MockitoAnnotations.initMocks(this) +// authInterceptor = AuthInterceptor(sharedPreferences) +// } +// +// @Test +// fun intercept_withToken_shouldAddAuthorizationHeader() { +// // Arrange +// val accessToken = "fakeAccessToken" +// `when`(sharedPreferences.getString(Constants.ACCESS_TOKEN, null)).thenReturn(accessToken) +// `when`(chain.request()).thenReturn(request) +// `when`(chain.proceed(request)).thenReturn(response) +// +// // Act +// val result = authInterceptor.intercept(chain) +// +// // Assert +// verify(chain).proceed(captor.capture()) +// assertEquals("Bearer $accessToken", captor.value.header("Authorization")) +// assertEquals(response, result) +// } +// +// @Test +// fun intercept_withoutToken_shouldNotAddAuthorizationHeader() { +// // Arrange +// `when`(sharedPreferences.getString(Constants.ACCESS_TOKEN, null)).thenReturn(null) +// `when`(chain.request()).thenReturn(request) +// `when`(chain.proceed(request)).thenReturn(response) +// +// // Act +// val result = authInterceptor.intercept(chain) +// +// // Assert +// verify(chain).proceed(captor.capture()) +// assertEquals(null, captor.value.header("Authorization")) +// assertEquals(response, result) +// } +//} \ No newline at end of file