Added broadcast receiver and listener to toggle usb connected/disconnected icon in action bar.
This commit is contained in:
@@ -1,8 +1,12 @@
|
||||
package com.example.hpos.data
|
||||
|
||||
import androidx.lifecycle.MutableLiveData
|
||||
import com.example.hpos.data.model.TestRightDeviceConstants
|
||||
|
||||
object DataHolder {
|
||||
// var usbConnected: Boolean = false
|
||||
val usbConnected = MutableLiveData(false)
|
||||
|
||||
var isStoragePermissionGranted = false
|
||||
var isAppFolderCreated = false
|
||||
var appFolderPath = ""
|
||||
|
||||
@@ -1,10 +1,20 @@
|
||||
package com.example.hpos.presentation
|
||||
|
||||
import android.content.BroadcastReceiver
|
||||
import android.content.Context
|
||||
import android.content.Intent
|
||||
import android.content.IntentFilter
|
||||
import android.hardware.usb.UsbManager
|
||||
import android.os.Bundle
|
||||
import android.view.Gravity
|
||||
import android.view.Menu
|
||||
import android.widget.Toast
|
||||
import androidx.appcompat.app.AppCompatActivity
|
||||
import androidx.core.content.ContextCompat
|
||||
import androidx.core.view.get
|
||||
import androidx.lifecycle.ViewModelProvider
|
||||
import com.example.hpos.R
|
||||
import com.example.hpos.data.DataHolder
|
||||
import com.example.hpos.databinding.ActivityMainBinding
|
||||
import com.example.hpos.presentation.testRight.TestRightActivity
|
||||
import com.example.hpos.util.MyViewModelFactory
|
||||
@@ -17,8 +27,23 @@ class MainActivity : AppCompatActivity()
|
||||
private lateinit var binding: ActivityMainBinding
|
||||
private lateinit var viewModel: MainViewModel
|
||||
|
||||
private var myMenu: Menu? = null
|
||||
|
||||
private val TAG = "MainActivity"
|
||||
|
||||
private var mUsbReceiver: BroadcastReceiver = object : BroadcastReceiver() {
|
||||
override fun onReceive(context: Context?, intent: Intent) {
|
||||
val action = intent.action
|
||||
if (UsbManager.ACTION_USB_DEVICE_DETACHED == action) {
|
||||
DataHolder.usbConnected.value = false
|
||||
// Toast.makeText(context, "Detached", Toast.LENGTH_SHORT).show()
|
||||
} else if (UsbManager.ACTION_USB_DEVICE_ATTACHED == action) {
|
||||
DataHolder.usbConnected.value = true
|
||||
// Toast.makeText(context, "Attached", Toast.LENGTH_SHORT).show()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
override fun onCreate(savedInstanceState: Bundle?) {
|
||||
super.onCreate(savedInstanceState)
|
||||
binding = ActivityMainBinding.inflate(layoutInflater)
|
||||
@@ -32,6 +57,11 @@ class MainActivity : AppCompatActivity()
|
||||
// supportActionBar?.setDisplayHomeAsUpEnabled(true)
|
||||
|
||||
setupListeners()
|
||||
|
||||
val filter = IntentFilter()
|
||||
filter.addAction(UsbManager.ACTION_USB_DEVICE_DETACHED)
|
||||
filter.addAction(UsbManager.ACTION_USB_DEVICE_ATTACHED)
|
||||
registerReceiver(mUsbReceiver, filter)
|
||||
}
|
||||
|
||||
private fun setupListeners() {
|
||||
@@ -44,7 +74,22 @@ class MainActivity : AppCompatActivity()
|
||||
binding.cvItem2.setOnClickListener {
|
||||
Toast.makeText(this, "To be Implemented", Toast.LENGTH_SHORT).show()
|
||||
}
|
||||
|
||||
DataHolder.usbConnected.observe(this){
|
||||
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)
|
||||
Toast.makeText(this, "USB NOT CONNECTED", Toast.LENGTH_SHORT).show()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
override fun onCreateOptionsMenu(menu: Menu?): Boolean {
|
||||
menuInflater.inflate(R.menu.my_menu, menu)
|
||||
myMenu = menu
|
||||
DataHolder.usbConnected.value = DataHolder.usbConnected.value
|
||||
return true
|
||||
}
|
||||
|
||||
}
|
||||
@@ -8,6 +8,7 @@ import android.hardware.usb.UsbManager
|
||||
import android.os.Bundle
|
||||
import android.os.IBinder
|
||||
import android.util.Log
|
||||
import android.view.Gravity
|
||||
import android.view.Menu
|
||||
import android.view.MenuItem
|
||||
import android.widget.Toast
|
||||
@@ -23,6 +24,7 @@ import com.example.hpos.util.MyViewModelFactory
|
||||
import com.hoho.android.usbserial.driver.UsbSerialDriver
|
||||
import com.hoho.android.usbserial.driver.UsbSerialProber
|
||||
|
||||
|
||||
class TestRightActivity : AppCompatActivity() {
|
||||
|
||||
private lateinit var binding: ActivityTestRightBinding
|
||||
@@ -79,27 +81,21 @@ class TestRightActivity : AppCompatActivity() {
|
||||
setSupportActionBar(binding.myToolbar)
|
||||
supportActionBar?.setDisplayHomeAsUpEnabled(true)
|
||||
|
||||
// Connected or not connected icon with livetime status using live data
|
||||
|
||||
// Todo: uncomment it
|
||||
// connectUsb(false)
|
||||
setupListener()
|
||||
|
||||
connectUsb(false)
|
||||
}
|
||||
|
||||
override fun onCreateOptionsMenu(menu: Menu?): Boolean {
|
||||
menuInflater.inflate(R.menu.my_menu, menu)
|
||||
myMenu = menu
|
||||
// menu?.get(0)?.icon = ContextCompat.getDrawable(this, R.drawable.ic_baseline_circle_24)
|
||||
return true
|
||||
}
|
||||
|
||||
override fun onOptionsItemSelected(item: MenuItem) = when (item.itemId) {
|
||||
R.id.action_usb -> {
|
||||
Toast.makeText(this, "USB Connected", Toast.LENGTH_SHORT).show()
|
||||
myMenu?.get(0)?.icon = ContextCompat.getDrawable(this, R.drawable.ic_baseline_usb_off_24)
|
||||
true
|
||||
private fun setupListener() {
|
||||
DataHolder.usbConnected.observe(this){
|
||||
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)
|
||||
// val myToast = Toast.makeText(this, "USB is Detached", Toast.LENGTH_SHORT)
|
||||
Toast.makeText(this, "USB is Detached", Toast.LENGTH_SHORT).show()
|
||||
}
|
||||
}
|
||||
else -> {super.onOptionsItemSelected(item)}
|
||||
}
|
||||
|
||||
// private fun testing() {
|
||||
@@ -178,6 +174,32 @@ class TestRightActivity : AppCompatActivity() {
|
||||
}
|
||||
}
|
||||
|
||||
fun onUsbError(msg: String) {
|
||||
Toast.makeText(this, msg, Toast.LENGTH_SHORT).show()
|
||||
// Snackbar.make(binding.clParent, msg, Snackbar.LENGTH_LONG)
|
||||
// .setAction("CLOSE") { Toast.makeText(this, "Will soon", Toast.LENGTH_SHORT).show() }
|
||||
// .setActionTextColor(resources.getColor(R.color.white))
|
||||
// .show()
|
||||
|
||||
if (!isFinishing)
|
||||
onBackPressed()
|
||||
}
|
||||
|
||||
override fun onCreateOptionsMenu(menu: Menu?): Boolean {
|
||||
menuInflater.inflate(R.menu.my_menu, menu)
|
||||
myMenu = menu
|
||||
return true
|
||||
}
|
||||
|
||||
// override fun onOptionsItemSelected(item: MenuItem) = when (item.itemId) {
|
||||
// R.id.action_usb -> {
|
||||
//// Toast.makeText(this, "USB Connected", Toast.LENGTH_SHORT).show()
|
||||
//// myMenu?.get(0)?.icon = ContextCompat.getDrawable(this, R.drawable.ic_baseline_usb_off_24)
|
||||
// true
|
||||
// }
|
||||
// else -> {super.onOptionsItemSelected(item)}
|
||||
// }
|
||||
|
||||
override fun onDestroy() {
|
||||
super.onDestroy()
|
||||
if (viewModel.isServiceConnected) {
|
||||
@@ -187,18 +209,5 @@ class TestRightActivity : AppCompatActivity() {
|
||||
}
|
||||
}
|
||||
|
||||
fun onUsbError(msg: String) {
|
||||
Toast.makeText(this, msg, Toast.LENGTH_SHORT).show()
|
||||
// Snackbar.make(binding.flMain, msg, Snackbar.LENGTH_LONG)
|
||||
// .setAction("CLOSE", object : View.OnClickListener {
|
||||
// override fun onClick(p0: View?) {
|
||||
// TODO("Not yet implemented")
|
||||
// }
|
||||
// })
|
||||
// .setActionTextColor(resources.getColor(R.color.holo_red_light))
|
||||
// .show()
|
||||
if (!isFinishing)
|
||||
onBackPressed()
|
||||
}
|
||||
|
||||
}
|
||||
@@ -6,6 +6,7 @@
|
||||
xmlns:tools="http://schemas.android.com/tools">
|
||||
|
||||
<androidx.constraintlayout.widget.ConstraintLayout
|
||||
android:id="@+id/cl_parent"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent">
|
||||
|
||||
|
||||
Reference in New Issue
Block a user