From dc52b143358b0feda9d2ba8be72f91bc26e916b2 Mon Sep 17 00:00:00 2001 From: vsuryakumar Date: Mon, 23 Jan 2023 23:02:39 +0530 Subject: [PATCH] added permission support for USB & established a connection using service --- app/src/main/AndroidManifest.xml | 25 +-- .../example/refactoredapp/MyApplication.kt | 5 + .../example/refactoredapp/data/Constants.kt | 4 + .../example/refactoredapp/data/DataHolder.kt | 2 + .../refactoredapp/data/model/DenovixData.kt | 8 - .../data/model/TestRightCommands.kt | 19 +++ .../data/model/TestRightDeviceConstants.kt | 8 + .../presentation/MainActivity.kt | 29 +--- .../presentation/MainViewModel.kt | 3 +- .../refactoredapp/presentation/RVAdapter.kt | 7 +- .../TestRight/TestRightActivity.kt | 28 ---- .../TestRight/TestRightExpReference.kt | 37 ----- .../TestRight/TestRightViewModel.kt | 8 - .../presentation/TestRight/UsbService.kt | 12 -- .../presentation/UsbServiceListener.kt | 9 ++ .../testRight/TestRightActivity.kt | 149 ++++++++++++++++++ .../testRight/TestRightExpReference.kt | 137 ++++++++++++++++ .../TestRightExpSample.kt | 3 +- .../TestRightResults.kt | 4 +- .../testRight/TestRightViewModel.kt | 16 ++ .../presentation/testRight/UsbService.kt | 105 ++++++++++++ .../presentation/utils/MyDialogListener.kt | 6 + .../presentation/utils/UIUtils.kt | 21 +++ .../refactoredapp/util/MyViewModelFactory.kt | 5 +- .../main/res/layout/activity_test_right.xml | 2 +- .../fragment_test_right_exp_reference.xml | 14 +- .../layout/fragment_test_right_exp_sample.xml | 4 +- .../layout/fragment_test_right_results.xml | 4 +- app/src/main/res/values-night/themes.xml | 6 +- app/src/main/res/values/strings.xml | 11 +- app/src/main/res/values/styles.xml | 6 + 31 files changed, 541 insertions(+), 156 deletions(-) delete mode 100644 app/src/main/java/com/example/refactoredapp/data/model/DenovixData.kt create mode 100644 app/src/main/java/com/example/refactoredapp/data/model/TestRightCommands.kt create mode 100644 app/src/main/java/com/example/refactoredapp/data/model/TestRightDeviceConstants.kt delete mode 100644 app/src/main/java/com/example/refactoredapp/presentation/TestRight/TestRightActivity.kt delete mode 100644 app/src/main/java/com/example/refactoredapp/presentation/TestRight/TestRightExpReference.kt delete mode 100644 app/src/main/java/com/example/refactoredapp/presentation/TestRight/TestRightViewModel.kt delete mode 100644 app/src/main/java/com/example/refactoredapp/presentation/TestRight/UsbService.kt create mode 100644 app/src/main/java/com/example/refactoredapp/presentation/UsbServiceListener.kt create mode 100644 app/src/main/java/com/example/refactoredapp/presentation/testRight/TestRightActivity.kt create mode 100644 app/src/main/java/com/example/refactoredapp/presentation/testRight/TestRightExpReference.kt rename app/src/main/java/com/example/refactoredapp/presentation/{TestRight => testRight}/TestRightExpSample.kt (89%) rename app/src/main/java/com/example/refactoredapp/presentation/{TestRight => testRight}/TestRightResults.kt (93%) create mode 100644 app/src/main/java/com/example/refactoredapp/presentation/testRight/TestRightViewModel.kt create mode 100644 app/src/main/java/com/example/refactoredapp/presentation/testRight/UsbService.kt create mode 100644 app/src/main/java/com/example/refactoredapp/presentation/utils/MyDialogListener.kt create mode 100644 app/src/main/java/com/example/refactoredapp/presentation/utils/UIUtils.kt diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index e1bcd15..d61a2ac 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -17,34 +17,39 @@ android:supportsRtl="true" android:theme="@style/Theme.RefactoredApp" tools:targetApi="31"> + + android:exported="false" /> + android:exported="true" + android:theme="@style/AppTheme.NoActionBar" + android:noHistory="true"> + + - + android:name=".presentation.testRight.TestRightActivity" + android:exported="false"> - - + + + \ No newline at end of file diff --git a/app/src/main/java/com/example/refactoredapp/MyApplication.kt b/app/src/main/java/com/example/refactoredapp/MyApplication.kt index 4a1dec4..1cf950b 100644 --- a/app/src/main/java/com/example/refactoredapp/MyApplication.kt +++ b/app/src/main/java/com/example/refactoredapp/MyApplication.kt @@ -1,6 +1,10 @@ package com.example.refactoredapp import android.app.Application +import android.graphics.Color +import android.graphics.PorterDuff +import android.view.View +import android.widget.Button import com.example.refactoredapp.data.model.SampleDetails class MyApplication : Application() { @@ -8,4 +12,5 @@ class MyApplication : Application() { private val sampleDetails = SampleDetails("Change it later"); fun getSampleDetails() : SampleDetails = sampleDetails // fun setSampleDetails(sample: SampleDetails) {sampleDetails = sample} + } \ No newline at end of file diff --git a/app/src/main/java/com/example/refactoredapp/data/Constants.kt b/app/src/main/java/com/example/refactoredapp/data/Constants.kt index a713240..cc08539 100644 --- a/app/src/main/java/com/example/refactoredapp/data/Constants.kt +++ b/app/src/main/java/com/example/refactoredapp/data/Constants.kt @@ -2,4 +2,8 @@ package com.example.refactoredapp.data object Constants { const val ACTION_USB_PERMISSION = "shanmukha.in.sickle_cell.USB_PERMISSION" + + const val WRITE_TIMEOUT_MILLIS = 60000 // 1 min + const val READ_TIMEOUT_MILLIS = 60000 // 1 min + } \ No newline at end of file diff --git a/app/src/main/java/com/example/refactoredapp/data/DataHolder.kt b/app/src/main/java/com/example/refactoredapp/data/DataHolder.kt index db67692..c9b30e8 100644 --- a/app/src/main/java/com/example/refactoredapp/data/DataHolder.kt +++ b/app/src/main/java/com/example/refactoredapp/data/DataHolder.kt @@ -3,4 +3,6 @@ package com.example.refactoredapp.data object DataHolder { var isStoragePermissionGranted = false var isAppFolderCreated = false + + var isReferenceTaken = false } \ No newline at end of file diff --git a/app/src/main/java/com/example/refactoredapp/data/model/DenovixData.kt b/app/src/main/java/com/example/refactoredapp/data/model/DenovixData.kt deleted file mode 100644 index 8ecde3e..0000000 --- a/app/src/main/java/com/example/refactoredapp/data/model/DenovixData.kt +++ /dev/null @@ -1,8 +0,0 @@ -package com.example.refactoredapp.data.model - -data class DenovixData( - val serialNumber: Int, - val nameSample: String, - val ratio: Double, - val result: String -) diff --git a/app/src/main/java/com/example/refactoredapp/data/model/TestRightCommands.kt b/app/src/main/java/com/example/refactoredapp/data/model/TestRightCommands.kt new file mode 100644 index 0000000..7f830cc --- /dev/null +++ b/app/src/main/java/com/example/refactoredapp/data/model/TestRightCommands.kt @@ -0,0 +1,19 @@ +package com.example.refactoredapp.data.model + +//class TestRightCmdConstants { +// +// companion object { +// const val readCommand = "read\r" +// const val autosetCommand = "autoset\r" +// const val printCommand = "print\r" +// const val runCommand = "run\r" +// } +// +//} + +enum class TestRightCommands(val command: String) { + read("read\r"), + autoset("autoset\r"), + print("print 239 2719\r"), + print_all("print\r"), +} \ No newline at end of file diff --git a/app/src/main/java/com/example/refactoredapp/data/model/TestRightDeviceConstants.kt b/app/src/main/java/com/example/refactoredapp/data/model/TestRightDeviceConstants.kt new file mode 100644 index 0000000..5ae67d8 --- /dev/null +++ b/app/src/main/java/com/example/refactoredapp/data/model/TestRightDeviceConstants.kt @@ -0,0 +1,8 @@ +package com.example.refactoredapp.data.model + +data class TestRightDeviceConstants( + val a: String, + val b: String, + val c: String, + val d: String +) diff --git a/app/src/main/java/com/example/refactoredapp/presentation/MainActivity.kt b/app/src/main/java/com/example/refactoredapp/presentation/MainActivity.kt index cbcb092..219644f 100644 --- a/app/src/main/java/com/example/refactoredapp/presentation/MainActivity.kt +++ b/app/src/main/java/com/example/refactoredapp/presentation/MainActivity.kt @@ -1,40 +1,13 @@ package com.example.refactoredapp.presentation -import android.Manifest -import android.app.PendingIntent -import android.content.Context import android.content.Intent -import android.content.pm.PackageManager -import android.hardware.usb.UsbDevice -import android.hardware.usb.UsbManager -import android.net.Uri -import android.os.Build import android.os.Bundle -import android.os.Environment -import android.provider.Settings -import android.util.Log -import android.view.View -import android.widget.AdapterView -import android.widget.ArrayAdapter import android.widget.Toast -import androidx.activity.result.contract.ActivityResultContracts -import androidx.annotation.RequiresApi import androidx.appcompat.app.AppCompatActivity -import androidx.core.content.ContextCompat import androidx.lifecycle.ViewModelProvider -import com.example.refactoredapp.R -import com.example.refactoredapp.data.Constants import com.example.refactoredapp.databinding.ActivityMainBinding -import com.example.refactoredapp.presentation.TestRight.TestRightActivity -import com.example.refactoredapp.util.MyUtils +import com.example.refactoredapp.presentation.testRight.TestRightActivity import com.example.refactoredapp.util.MyViewModelFactory -import com.hoho.android.usbserial.driver.UsbSerialDriver -import com.hoho.android.usbserial.driver.UsbSerialPort -import com.hoho.android.usbserial.driver.UsbSerialProber -import com.hoho.android.usbserial.util.SerialInputOutputManager -import java.io.File -import java.lang.Exception -import java.nio.charset.StandardCharsets class MainActivity : AppCompatActivity() diff --git a/app/src/main/java/com/example/refactoredapp/presentation/MainViewModel.kt b/app/src/main/java/com/example/refactoredapp/presentation/MainViewModel.kt index 5537588..3cae34d 100644 --- a/app/src/main/java/com/example/refactoredapp/presentation/MainViewModel.kt +++ b/app/src/main/java/com/example/refactoredapp/presentation/MainViewModel.kt @@ -3,7 +3,6 @@ package com.example.refactoredapp.presentation import androidx.lifecycle.MutableLiveData import androidx.lifecycle.ViewModel import androidx.lifecycle.viewModelScope -import com.example.refactoredapp.data.model.DenovixData import com.example.refactoredapp.data.model.DeviceType import com.example.refactoredapp.domain.ParseCSV import kotlinx.coroutines.Dispatchers @@ -15,7 +14,7 @@ class MainViewModel(private val parseDenovix: ParseCSV, private val parseTestRig var device = DeviceType.TestRight var isStoragePermission = MutableLiveData(false) - val resultsLiveData = MutableLiveData>() +// val resultsLiveData = MutableLiveData>() fun parseCSV(file: File){ viewModelScope.launch(Dispatchers.IO) { diff --git a/app/src/main/java/com/example/refactoredapp/presentation/RVAdapter.kt b/app/src/main/java/com/example/refactoredapp/presentation/RVAdapter.kt index ec32d50..93ee357 100644 --- a/app/src/main/java/com/example/refactoredapp/presentation/RVAdapter.kt +++ b/app/src/main/java/com/example/refactoredapp/presentation/RVAdapter.kt @@ -1,16 +1,15 @@ package com.example.refactoredapp.presentation -import com.example.refactoredapp.data.model.DenovixData - import android.view.LayoutInflater import android.view.View import android.view.ViewGroup import androidx.recyclerview.widget.RecyclerView import com.example.refactoredapp.R +import com.example.refactoredapp.data.model.SampleDetails class RVAdapter : RecyclerView.Adapter() { - private val dataset = ArrayList() + private val dataset = ArrayList() class ViewHolder(view: View) : RecyclerView.ViewHolder(view) { @@ -31,7 +30,7 @@ class RVAdapter : RecyclerView.Adapter() { return dataset.size } - fun updateDataSet(newDataSet: ArrayList) { + fun updateDataSet(newDataSet: ArrayList) { dataset.clear() dataset.addAll(newDataSet) notifyDataSetChanged() diff --git a/app/src/main/java/com/example/refactoredapp/presentation/TestRight/TestRightActivity.kt b/app/src/main/java/com/example/refactoredapp/presentation/TestRight/TestRightActivity.kt deleted file mode 100644 index 48f3eac..0000000 --- a/app/src/main/java/com/example/refactoredapp/presentation/TestRight/TestRightActivity.kt +++ /dev/null @@ -1,28 +0,0 @@ -package com.example.refactoredapp.presentation.TestRight - -import androidx.appcompat.app.AppCompatActivity -import android.os.Bundle -import androidx.lifecycle.ViewModelProvider -import com.example.refactoredapp.R -import com.example.refactoredapp.databinding.ActivityMainBinding -import com.example.refactoredapp.databinding.ActivityTestRightBinding -import com.example.refactoredapp.util.MyViewModelFactory - -class TestRightActivity : AppCompatActivity() { - - private lateinit var binding: ActivityTestRightBinding - private lateinit var viewModel: TestRightViewModel - - override fun onCreate(savedInstanceState: Bundle?) { - super.onCreate(savedInstanceState) - binding = ActivityTestRightBinding.inflate(layoutInflater) - setContentView(binding.root) - viewModel = ViewModelProvider(this, MyViewModelFactory(this.applicationContext))[TestRightViewModel::class.java] - - if (!viewModel.isReferenceDone) - supportFragmentManager.beginTransaction().replace(binding.flMain.id, TestRightExpReference()).commit() - else - supportFragmentManager.beginTransaction().replace(binding.flMain.id, TestRightExpSample()).commit() - } - -} \ No newline at end of file diff --git a/app/src/main/java/com/example/refactoredapp/presentation/TestRight/TestRightExpReference.kt b/app/src/main/java/com/example/refactoredapp/presentation/TestRight/TestRightExpReference.kt deleted file mode 100644 index df84863..0000000 --- a/app/src/main/java/com/example/refactoredapp/presentation/TestRight/TestRightExpReference.kt +++ /dev/null @@ -1,37 +0,0 @@ -package com.example.refactoredapp.presentation.TestRight - -import android.os.Bundle -import androidx.fragment.app.Fragment -import android.view.LayoutInflater -import android.view.View -import android.view.ViewGroup -import androidx.fragment.app.activityViewModels -import com.example.refactoredapp.R -import com.example.refactoredapp.databinding.FragmentTestRightExpReferenceBinding -import com.example.refactoredapp.databinding.FragmentTestRightResultsBinding - -class TestRightExpReference : Fragment() { - - private lateinit var binding: FragmentTestRightExpReferenceBinding - private val viewModel: TestRightViewModel by activityViewModels() - - override fun onCreateView( - inflater: LayoutInflater, container: ViewGroup?, - savedInstanceState: Bundle? - ): View { - // Inflate the layout for this fragment - binding = FragmentTestRightExpReferenceBinding.inflate(inflater, container, false) - return binding.root - } - - override fun onViewCreated(view: View, savedInstanceState: Bundle?) { - super.onViewCreated(view, savedInstanceState) - setupListeners() - } - - private fun setupListeners() { - binding.btnSetReference.setOnClickListener { - - } - } -} \ No newline at end of file diff --git a/app/src/main/java/com/example/refactoredapp/presentation/TestRight/TestRightViewModel.kt b/app/src/main/java/com/example/refactoredapp/presentation/TestRight/TestRightViewModel.kt deleted file mode 100644 index 5c3ac29..0000000 --- a/app/src/main/java/com/example/refactoredapp/presentation/TestRight/TestRightViewModel.kt +++ /dev/null @@ -1,8 +0,0 @@ -package com.example.refactoredapp.presentation.TestRight - -import androidx.lifecycle.ViewModel - -class TestRightViewModel : ViewModel() { - - var isReferenceDone = false -} \ No newline at end of file diff --git a/app/src/main/java/com/example/refactoredapp/presentation/TestRight/UsbService.kt b/app/src/main/java/com/example/refactoredapp/presentation/TestRight/UsbService.kt deleted file mode 100644 index 9a7da5a..0000000 --- a/app/src/main/java/com/example/refactoredapp/presentation/TestRight/UsbService.kt +++ /dev/null @@ -1,12 +0,0 @@ -package com.example.refactoredapp.presentation.TestRight - -import android.app.Service -import android.content.Intent -import android.os.IBinder - -class UsbService : Service() { - - override fun onBind(intent: Intent): IBinder { - TODO("Return the communication channel to the service.") - } -} \ No newline at end of file diff --git a/app/src/main/java/com/example/refactoredapp/presentation/UsbServiceListener.kt b/app/src/main/java/com/example/refactoredapp/presentation/UsbServiceListener.kt new file mode 100644 index 0000000..20c5c8d --- /dev/null +++ b/app/src/main/java/com/example/refactoredapp/presentation/UsbServiceListener.kt @@ -0,0 +1,9 @@ +package com.example.refactoredapp.presentation + +interface UsbServiceListener { + fun onUsbConnect() + fun onUsbConnectError(e: Exception?) + fun onUsbRead(data: ByteArray?) + fun onUsbRead() + fun onUsbIoError(e: Exception?) +} \ No newline at end of file diff --git a/app/src/main/java/com/example/refactoredapp/presentation/testRight/TestRightActivity.kt b/app/src/main/java/com/example/refactoredapp/presentation/testRight/TestRightActivity.kt new file mode 100644 index 0000000..504b651 --- /dev/null +++ b/app/src/main/java/com/example/refactoredapp/presentation/testRight/TestRightActivity.kt @@ -0,0 +1,149 @@ +package com.example.refactoredapp.presentation.testRight + +import android.app.PendingIntent +import android.content.* +import android.hardware.usb.UsbDevice +import android.hardware.usb.UsbDeviceConnection +import android.hardware.usb.UsbManager +import android.os.Bundle +import android.os.IBinder +import android.util.Log +import android.widget.Toast +import androidx.appcompat.app.AppCompatActivity +import androidx.lifecycle.ViewModelProvider +import com.example.refactoredapp.data.Constants +import com.example.refactoredapp.databinding.ActivityTestRightBinding +import com.example.refactoredapp.util.MyViewModelFactory +import com.hoho.android.usbserial.driver.UsbSerialDriver +import com.hoho.android.usbserial.driver.UsbSerialProber + +class TestRightActivity : AppCompatActivity() { + + private lateinit var binding: ActivityTestRightBinding + private lateinit var viewModel: TestRightViewModel + + private lateinit var mDriver: UsbSerialDriver + private var mConnection: UsbDeviceConnection? = null + lateinit var mService: UsbService + + private val TAG = "TestRightActivity" + + private val broadcastReceiver = object : BroadcastReceiver() { + override fun onReceive(context: Context, intent: Intent) { + + synchronized(this) { + val device: UsbDevice? = intent.getParcelableExtra(UsbManager.EXTRA_DEVICE) + + if (intent.getBooleanExtra(UsbManager.EXTRA_PERMISSION_GRANTED, false)) { + device?.apply { + connectUsb(true) + } + } else { + onUsbError("permission denied for device") + } + } + + } + } + + private val connection = object : ServiceConnection { + override fun onServiceConnected(className: ComponentName, service: IBinder) { + val binder = service as UsbService.UsbServiceBinder + mService = binder.getService() + viewModel.isServiceConnected = true + mConnection.let { mService.connect(mDriver, mConnection!!) } + moveToNext() + } + + override fun onServiceDisconnected(arg0: ComponentName) { + viewModel.isServiceConnected = false + } + } + + override fun onCreate(savedInstanceState: Bundle?) { + super.onCreate(savedInstanceState) + binding = ActivityTestRightBinding.inflate(layoutInflater) + setContentView(binding.root) + viewModel = ViewModelProvider(this, MyViewModelFactory(this.applicationContext))[TestRightViewModel::class.java] + + connectUsb(false); + +// if(connectUsb(false)) { +// moveToNext() +// } else { +// onBackPressed() +// } + + } + + private fun connectUsb(permissionGranted: Boolean) { + val manager = getSystemService(Context.USB_SERVICE) as UsbManager + val availableDrivers = UsbSerialProber.getDefaultProber().findAllDrivers(manager) + + if (availableDrivers.isEmpty()) { + onUsbError("No Device is Connected") + } else { + mDriver = availableDrivers[0] + mConnection = manager.openDevice(mDriver.device) + + if (mConnection == null){ + requestUserPermission(manager, mDriver.device) + } else { + setupService() + } + } + } + + /* + * Request user permission. The response will be received in the BroadcastReceiver + */ + private fun requestUserPermission(manager: UsbManager, device: UsbDevice) { + Log.d(TAG, String.format("requestUserPermissions", device.vendorId, device.productId)) + + val mPendingIntent = PendingIntent.getBroadcast( + this, + 0, + Intent(Constants.ACTION_USB_PERMISSION), + 0 + ) + + val filter = IntentFilter(Constants.ACTION_USB_PERMISSION) + registerReceiver(broadcastReceiver, filter) + manager.requestPermission(device, mPendingIntent) + } + + private fun setupService() { + val intent = Intent(this, UsbService::class.java) +// startService(intent) + bindService(intent, connection, Context.BIND_AUTO_CREATE) + } + + private fun moveToNext(){ +// setupService() + if (supportFragmentManager.isDestroyed) + return + + if (!viewModel.isReferenceDone) + supportFragmentManager.beginTransaction() + .replace(binding.flMain.id, TestRightExpReference()).commit() + else + supportFragmentManager.beginTransaction() + .replace(binding.flMain.id, TestRightExpSample()).commit() + } + + override fun onDestroy() { + super.onDestroy() + if (viewModel.isServiceConnected) { + mService.disconnect() + unbindService(connection) + viewModel.isServiceConnected = false + } + } + + fun onUsbError(msg: String) { + Toast.makeText(this, msg, Toast.LENGTH_SHORT).show() + if (!isFinishing) + onBackPressed() + } + +} \ No newline at end of file diff --git a/app/src/main/java/com/example/refactoredapp/presentation/testRight/TestRightExpReference.kt b/app/src/main/java/com/example/refactoredapp/presentation/testRight/TestRightExpReference.kt new file mode 100644 index 0000000..5f8afc7 --- /dev/null +++ b/app/src/main/java/com/example/refactoredapp/presentation/testRight/TestRightExpReference.kt @@ -0,0 +1,137 @@ +package com.example.refactoredapp.presentation.testRight + +import android.os.Bundle +import android.os.Handler +import android.util.Log +import android.view.LayoutInflater +import android.view.View +import android.view.ViewGroup +import androidx.fragment.app.Fragment +import androidx.fragment.app.activityViewModels +import com.example.refactoredapp.R +import com.example.refactoredapp.data.DataHolder +import com.example.refactoredapp.data.model.TestRightCommands +import com.example.refactoredapp.databinding.FragmentTestRightExpReferenceBinding +import com.example.refactoredapp.presentation.UsbServiceListener +import com.example.refactoredapp.presentation.utils.MyDialogListener +import com.example.refactoredapp.presentation.utils.UIUtils + + +class TestRightExpReference : Fragment() { + + private lateinit var binding: FragmentTestRightExpReferenceBinding + private val viewModel: TestRightViewModel by activityViewModels() + + private val TAG = "TestRightExpReference" + + var fullString: String = "" + + override fun onCreateView( + inflater: LayoutInflater, container: ViewGroup?, + savedInstanceState: Bundle? + ): View { + // Inflate the layout for this fragment + binding = FragmentTestRightExpReferenceBinding.inflate(inflater, container, false) + return binding.root + } + + override fun onViewCreated(view: View, savedInstanceState: Bundle?) { + super.onViewCreated(view, savedInstanceState) + setupListeners() + + if (DataHolder.isReferenceTaken) + parentFragmentManager.beginTransaction().replace(R.id.fl_main, TestRightExpSample()) + .commit() + + } + + private fun setupListeners() { + binding.btnSetReference.setOnClickListener { + UIUtils().createAlertDialog( + requireContext(), + getString(R.string.have_you_placed), + getString(R.string.please_place), + getString(R.string.yes), + getString(R.string.no), + object : MyDialogListener { + override fun onClickBtnOne() { + startTakingReference() + } + + override fun onClickBtnTwo() {} + }) + } + + (activity as TestRightActivity).mService.addListener( + object : UsbServiceListener { + override fun onUsbConnect() { + Log.d(TAG, "onUsbConnect() called") + } + + override fun onUsbConnectError(e: Exception?) { + } + + override fun onUsbRead(data: ByteArray?) { +// Log.d(TAG, "onUsbRead(data) call back called") + data?.let { + val stringData = String(it) +// Log.d(TAG, stringData) + fullString += stringData + + if(stringData.contains("OK")){ + viewModel.mapValues(fullString) + } + } +// val stringData = String(data!!) +// Log.d(TAG, stringData) + } + + override fun onUsbRead() { + Log.d(TAG, "onSerialRead() call back called") + } + override fun onUsbIoError(e: Exception?) { + Log.e(TAG, "onUsbIoError() called -> $e") + } + } + ) + + binding.btnRead.setOnClickListener { +// Log.d(TAG, "clicked on text") +// val mService = (activity as TestRightActivity).mService +// mService.read() + Log.d(TAG, "FULL STRING IS -> $fullString") + + } + } + + private fun startTakingReference() { + val mService = (activity as TestRightActivity).mService + mService.eventDrivenWrite(TestRightCommands.print_all) + } + +// private fun startTakingReference() { +// val mService = (activity as TestRightActivity).mService +// mService.write(TestRightCommands.print) +// +// val handler = Handler() +// handler.postDelayed(Runnable { // Do something after 5s = 5000ms +// mService.read() +// }, 10000) +// +// } + + override fun onDestroy() { + super.onDestroy() + (activity as TestRightActivity).mService.removeListener() + } + +// fun Button.disable() { +// background.setColorFilter(Color.GRAY, PorterDuff.Mode.MULTIPLY) +// isClickable = false +// } +// +// fun Button.enable() { +// background.setColorFilter(Color.BLUE, PorterDuff.Mode.MULTIPLY) +// isClickable = true +// } +} \ No newline at end of file diff --git a/app/src/main/java/com/example/refactoredapp/presentation/TestRight/TestRightExpSample.kt b/app/src/main/java/com/example/refactoredapp/presentation/testRight/TestRightExpSample.kt similarity index 89% rename from app/src/main/java/com/example/refactoredapp/presentation/TestRight/TestRightExpSample.kt rename to app/src/main/java/com/example/refactoredapp/presentation/testRight/TestRightExpSample.kt index 2bef13f..bc165db 100644 --- a/app/src/main/java/com/example/refactoredapp/presentation/TestRight/TestRightExpSample.kt +++ b/app/src/main/java/com/example/refactoredapp/presentation/testRight/TestRightExpSample.kt @@ -1,4 +1,4 @@ -package com.example.refactoredapp.presentation.TestRight +package com.example.refactoredapp.presentation.testRight import android.os.Bundle import androidx.fragment.app.Fragment @@ -6,7 +6,6 @@ import android.view.LayoutInflater import android.view.View import android.view.ViewGroup import androidx.fragment.app.activityViewModels -import com.example.refactoredapp.R import com.example.refactoredapp.databinding.FragmentTestRightExpReferenceBinding class TestRightExpSample : Fragment() { diff --git a/app/src/main/java/com/example/refactoredapp/presentation/TestRight/TestRightResults.kt b/app/src/main/java/com/example/refactoredapp/presentation/testRight/TestRightResults.kt similarity index 93% rename from app/src/main/java/com/example/refactoredapp/presentation/TestRight/TestRightResults.kt rename to app/src/main/java/com/example/refactoredapp/presentation/testRight/TestRightResults.kt index 8bb025a..a483d34 100644 --- a/app/src/main/java/com/example/refactoredapp/presentation/TestRight/TestRightResults.kt +++ b/app/src/main/java/com/example/refactoredapp/presentation/testRight/TestRightResults.kt @@ -1,16 +1,14 @@ -package com.example.refactoredapp.presentation.TestRight +package com.example.refactoredapp.presentation.testRight import android.os.Bundle import androidx.fragment.app.Fragment import android.view.LayoutInflater import android.view.View import android.view.ViewGroup -import android.widget.AdapterView import android.widget.ArrayAdapter import androidx.fragment.app.activityViewModels import com.example.refactoredapp.R import com.example.refactoredapp.databinding.FragmentTestRightResultsBinding -import com.example.refactoredapp.util.MyUtils class TestRightResults : Fragment() { diff --git a/app/src/main/java/com/example/refactoredapp/presentation/testRight/TestRightViewModel.kt b/app/src/main/java/com/example/refactoredapp/presentation/testRight/TestRightViewModel.kt new file mode 100644 index 0000000..a534d67 --- /dev/null +++ b/app/src/main/java/com/example/refactoredapp/presentation/testRight/TestRightViewModel.kt @@ -0,0 +1,16 @@ +package com.example.refactoredapp.presentation.testRight + +import androidx.lifecycle.MutableLiveData +import androidx.lifecycle.ViewModel + +class TestRightViewModel : ViewModel() { + + var isReferenceDone = false + var isServiceConnected = false + val isUsbConnected = MutableLiveData(false) + + fun mapValues(fullString: String) { + TODO("Not yet implemented") + } + +} \ No newline at end of file diff --git a/app/src/main/java/com/example/refactoredapp/presentation/testRight/UsbService.kt b/app/src/main/java/com/example/refactoredapp/presentation/testRight/UsbService.kt new file mode 100644 index 0000000..2ab17c1 --- /dev/null +++ b/app/src/main/java/com/example/refactoredapp/presentation/testRight/UsbService.kt @@ -0,0 +1,105 @@ +package com.example.refactoredapp.presentation.testRight + +import android.app.Service +import android.content.Intent +import android.hardware.usb.UsbDeviceConnection +import android.os.Binder +import android.os.IBinder +import android.util.Log +import com.example.refactoredapp.data.Constants +import com.example.refactoredapp.data.model.TestRightCommands +import com.example.refactoredapp.presentation.UsbServiceListener +import com.hoho.android.usbserial.driver.UsbSerialDriver +import com.hoho.android.usbserial.driver.UsbSerialPort +import com.hoho.android.usbserial.util.SerialInputOutputManager +import java.io.IOException + + +class UsbService : Service() { + + // Binder to be given to clients + private val binder = UsbServiceBinder() + + private lateinit var mPort: UsbSerialPort + + private var listener: UsbServiceListener? = null + + private var isUsbConnected = false + + private val TAG = "UsbService" + + inner class UsbServiceBinder : Binder() { + // Return this instance of UsbService so clients can call public methods + fun getService(): UsbService = this@UsbService + } + + override fun onBind(intent: Intent): IBinder { + Log.d("surya", "onBind() called") + return binder + } + + fun connect(driver: UsbSerialDriver, connection: UsbDeviceConnection) { + + mPort = driver.ports[0] // Most devices have just one port (port 0) + mPort.open(connection) + mPort.setParameters(115200, 8, UsbSerialPort.STOPBITS_1, UsbSerialPort.PARITY_NONE) + + isUsbConnected = true + Log.d(TAG, "My Usb Connected ${mPort.driver}") + } + + fun disconnect() { + if(isUsbConnected){ + mPort.close() + isUsbConnected = false; + Log.d(TAG, "My Usb disconnected:: ${mPort.driver}") + } + } + + fun addListener(listener: UsbServiceListener){ + this.listener = listener + if(isUsbConnected) + listener.onUsbConnect() + } + + fun removeListener(){ + this.listener = null + } + + fun eventDrivenWrite(command: TestRightCommands) { + val usbIoManager = SerialInputOutputManager(mPort, + object : SerialInputOutputManager.Listener{ + override fun onNewData(data: ByteArray?) { + listener?.onUsbRead(data) + } + + override fun onRunError(e: Exception?) { + Log.e(TAG, "onRunError() called inside eventDrivenWrite()") + } + + }) + usbIoManager.start(); + + try { + mPort.write(command.command.toByteArray(), Constants.WRITE_TIMEOUT_MILLIS) + } catch (e: IOException) { + listener?.onUsbIoError(e) + } + } + + fun write(command: TestRightCommands){ + mPort.write(command.command.toByteArray(), Constants.WRITE_TIMEOUT_MILLIS) + } + + fun read(){ + val byteArray = ByteArray(3700) + val len = mPort.read(byteArray, Constants.READ_TIMEOUT_MILLIS) + + Log.d(TAG, "length is $len") + val byteArrayCropped = ByteArray(len) + System.arraycopy(byteArray, 0, byteArrayCropped, 0, len) + + listener?.onUsbRead(byteArrayCropped) + } + +} \ No newline at end of file diff --git a/app/src/main/java/com/example/refactoredapp/presentation/utils/MyDialogListener.kt b/app/src/main/java/com/example/refactoredapp/presentation/utils/MyDialogListener.kt new file mode 100644 index 0000000..29d491f --- /dev/null +++ b/app/src/main/java/com/example/refactoredapp/presentation/utils/MyDialogListener.kt @@ -0,0 +1,6 @@ +package com.example.refactoredapp.presentation.utils + +interface MyDialogListener { + fun onClickBtnOne() + fun onClickBtnTwo() +} \ No newline at end of file diff --git a/app/src/main/java/com/example/refactoredapp/presentation/utils/UIUtils.kt b/app/src/main/java/com/example/refactoredapp/presentation/utils/UIUtils.kt new file mode 100644 index 0000000..ec8d36d --- /dev/null +++ b/app/src/main/java/com/example/refactoredapp/presentation/utils/UIUtils.kt @@ -0,0 +1,21 @@ +package com.example.refactoredapp.presentation.utils + +import android.content.Context +import com.google.android.material.dialog.MaterialAlertDialogBuilder + +class UIUtils { + + fun createAlertDialog(context: Context, title: String, subtitle: String, btnOneText: String, btnTwoText: String, listener: MyDialogListener){ + MaterialAlertDialogBuilder(context) + .setTitle(title) + .setMessage(subtitle) + .setNegativeButton(btnOneText) { _, _ -> + listener.onClickBtnOne() + } + .setPositiveButton(btnTwoText) { _, _ -> + listener.onClickBtnTwo() + } + .show() + } + +} \ No newline at end of file diff --git a/app/src/main/java/com/example/refactoredapp/util/MyViewModelFactory.kt b/app/src/main/java/com/example/refactoredapp/util/MyViewModelFactory.kt index 05d8e7f..49afe7d 100644 --- a/app/src/main/java/com/example/refactoredapp/util/MyViewModelFactory.kt +++ b/app/src/main/java/com/example/refactoredapp/util/MyViewModelFactory.kt @@ -3,13 +3,10 @@ package com.example.refactoredapp.util import android.content.Context import androidx.lifecycle.ViewModel import androidx.lifecycle.ViewModelProvider -import com.example.refactoredapp.data.RepositoryImpl import com.example.refactoredapp.domain.ParseDenovixUseCase import com.example.refactoredapp.domain.ParseTestRightCSVUseCase -import com.example.refactoredapp.presentation.MainActivity import com.example.refactoredapp.presentation.MainViewModel -import com.example.refactoredapp.presentation.TestRight.TestRightActivity -import com.example.refactoredapp.presentation.TestRight.TestRightViewModel +import com.example.refactoredapp.presentation.testRight.TestRightViewModel class MyViewModelFactory(private val context: Context) : ViewModelProvider.Factory { diff --git a/app/src/main/res/layout/activity_test_right.xml b/app/src/main/res/layout/activity_test_right.xml index 63eef54..4eab4f3 100644 --- a/app/src/main/res/layout/activity_test_right.xml +++ b/app/src/main/res/layout/activity_test_right.xml @@ -9,7 +9,7 @@ android:id="@+id/fl_main" android:layout_width="match_parent" android:layout_height="match_parent" - tools:context=".presentation.TestRight.TestRightActivity"> + tools:context=".presentation.testRight.TestRightActivity"> diff --git a/app/src/main/res/layout/fragment_test_right_exp_reference.xml b/app/src/main/res/layout/fragment_test_right_exp_reference.xml index 0981239..9179c30 100644 --- a/app/src/main/res/layout/fragment_test_right_exp_reference.xml +++ b/app/src/main/res/layout/fragment_test_right_exp_reference.xml @@ -2,7 +2,7 @@ + tools:context=".presentation.testRight.TestRightExpReference"> + +