Update MainActivity.kt

master
Zhengyu Peng 3 years ago
parent 4e226c2cbc
commit fa31a36d9d

@ -33,24 +33,30 @@ class MainActivity : AppCompatActivity() {
private const val SHARED_PREFS_TAB = "TAB" private const val SHARED_PREFS_TAB = "TAB"
private const val SHARED_PREFS_DEVICE_NAME = "DEVICE_NAME" private const val SHARED_PREFS_DEVICE_NAME = "DEVICE_NAME"
private const val SHARED_PREFS_DEVICE_ADDRESS = "DEVICE_ADDRESS" private const val SHARED_PREFS_DEVICE_ADDRESS = "DEVICE_ADDRESS"
private const val TAB_WIFI = "WiFi"
private const val TAB_BLUETOOTH = "Bluetooth"
} }
private var mContext: Context? = null private var mContext: Context? = null
private lateinit var tabLayout: TabLayout
private lateinit var ipInput: TextInputEditText private lateinit var ipInput: TextInputEditText
private lateinit var portInput: TextInputEditText private lateinit var portInput: TextInputEditText
private lateinit var btDeviceName: TextView private lateinit var btDeviceName: TextView
private lateinit var btDeviceMac: TextView private lateinit var btDeviceMac: TextView
private lateinit var tabLayout: TabLayout
override fun onCreate(savedInstanceState: Bundle?) { override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState) super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main) setContentView(R.layout.activity_main)
mContext = applicationContext mContext = applicationContext
// Tab layout
tabLayout = findViewById(R.id.tab)
// TCP // TCP
val ipLayout = findViewById<TextInputLayout>(R.id.ip_input_layout) val ipLayout = findViewById<TextInputLayout>(R.id.ip_input_layout)
val portLayout = findViewById<TextInputLayout>(R.id.port_input_layout) val portLayout = findViewById<TextInputLayout>(R.id.port_input_layout)
@ -62,22 +68,25 @@ class MainActivity : AppCompatActivity() {
btDeviceName = findViewById(R.id.textView_device_name) btDeviceName = findViewById(R.id.textView_device_name)
btDeviceMac = findViewById(R.id.textView_device_mac) btDeviceMac = findViewById(R.id.textView_device_mac)
// Connect button
val connectButton = findViewById<Button>(R.id.button_connect)
val buttonConnect = findViewById<Button>(R.id.button_connect) // GitHub link
val sourceLink = findViewById<TextView>(R.id.textView_github) val sourceLink = findViewById<TextView>(R.id.textView_github)
sourceLink.movementMethod = LinkMovementMethod.getInstance() sourceLink.movementMethod = LinkMovementMethod.getInstance()
tabLayout = findViewById(R.id.tab) // Read preference
readSharedPref()
// Tab select listener
tabLayout.addOnTabSelectedListener( tabLayout.addOnTabSelectedListener(
object : TabLayout.OnTabSelectedListener { object : TabLayout.OnTabSelectedListener {
override fun onTabSelected(tab: TabLayout.Tab?) { override fun onTabSelected(tab: TabLayout.Tab?) {
if (tab!!.text == "WiFi") { if (tab!!.text == TAB_WIFI) {
ipLayout.visibility = View.VISIBLE ipLayout.visibility = View.VISIBLE
portLayout.visibility = View.VISIBLE portLayout.visibility = View.VISIBLE
selectedDevice.visibility = View.GONE selectedDevice.visibility = View.GONE
} else if (tab.text == "Bluetooth") { } else if (tab.text == TAB_BLUETOOTH) {
ipLayout.visibility = View.GONE ipLayout.visibility = View.GONE
portLayout.visibility = View.GONE portLayout.visibility = View.GONE
selectedDevice.visibility = View.VISIBLE selectedDevice.visibility = View.VISIBLE
@ -100,7 +109,7 @@ class MainActivity : AppCompatActivity() {
// startActivityForResult(serverIntent, REQUEST_CONNECT_DEVICE_INSECURE) // startActivityForResult(serverIntent, REQUEST_CONNECT_DEVICE_INSECURE)
} }
readSharedPref()
if (tabLayout.selectedTabPosition == 0) { if (tabLayout.selectedTabPosition == 0) {
ipLayout.visibility = View.VISIBLE ipLayout.visibility = View.VISIBLE
@ -113,7 +122,7 @@ class MainActivity : AppCompatActivity() {
selectedDevice.visibility = View.VISIBLE selectedDevice.visibility = View.VISIBLE
} }
buttonConnect.setOnClickListener { connectButton.setOnClickListener {
// your code to perform when the user clicks on the button // your code to perform when the user clicks on the button
// Toast.makeText(this@MainActivity, "You clicked me.", Toast.LENGTH_SHORT).show() // Toast.makeText(this@MainActivity, "You clicked me.", Toast.LENGTH_SHORT).show()
@ -123,7 +132,7 @@ class MainActivity : AppCompatActivity() {
) { ) {
saveSharedPref() saveSharedPref()
val intent = Intent(this, ControlActivity::class.java).apply { val intent = Intent(this, ControlActivity::class.java).apply {
putExtra("interface", "WiFi") putExtra("interface", TAB_WIFI)
putExtra("ip", ipInput.text.toString()) putExtra("ip", ipInput.text.toString())
putExtra("port", portInput.text.toString()) putExtra("port", portInput.text.toString())
} }
@ -137,7 +146,7 @@ class MainActivity : AppCompatActivity() {
if (btDeviceMac.text.isNotBlank()) { if (btDeviceMac.text.isNotBlank()) {
saveSharedPref() saveSharedPref()
val intent = Intent(this, ControlActivity::class.java).apply { val intent = Intent(this, ControlActivity::class.java).apply {
putExtra("interface", "Bluetooth") putExtra("interface", TAB_BLUETOOTH)
putExtra("mac", btDeviceMac.text.toString()) putExtra("mac", btDeviceMac.text.toString())
} }
startActivity(intent) startActivity(intent)
@ -232,11 +241,11 @@ class MainActivity : AppCompatActivity() {
ipInput.setText(prefs.getString(SHARED_PREFS_IP, "192.168.1.127")) ipInput.setText(prefs.getString(SHARED_PREFS_IP, "192.168.1.127"))
portInput.setText(prefs.getString(SHARED_PREFS_PORT, "1234")) portInput.setText(prefs.getString(SHARED_PREFS_PORT, "1234"))
val selectedTab = prefs.getString(SHARED_PREFS_TAB, "WiFi") val selectedTab = prefs.getString(SHARED_PREFS_TAB, TAB_WIFI)
if (selectedTab == "WiFi") { if (selectedTab == TAB_WIFI) {
val tab = tabLayout.getTabAt(0) val tab = tabLayout.getTabAt(0)
tab!!.select() tab!!.select()
} else if (selectedTab == "Bluetooth") { } else if (selectedTab == TAB_BLUETOOTH) {
val tab = tabLayout.getTabAt(1) val tab = tabLayout.getTabAt(1)
tab!!.select() tab!!.select()
} }
@ -254,9 +263,9 @@ class MainActivity : AppCompatActivity() {
editor.putString(SHARED_PREFS_IP, ipInput.text.toString()) editor.putString(SHARED_PREFS_IP, ipInput.text.toString())
editor.putString(SHARED_PREFS_PORT, portInput.text.toString()) editor.putString(SHARED_PREFS_PORT, portInput.text.toString())
if (tabLayout.selectedTabPosition == 0) { if (tabLayout.selectedTabPosition == 0) {
editor.putString(SHARED_PREFS_TAB, "WiFi") editor.putString(SHARED_PREFS_TAB, TAB_WIFI)
} else if (tabLayout.selectedTabPosition == 1) { } else if (tabLayout.selectedTabPosition == 1) {
editor.putString(SHARED_PREFS_TAB, "Bluetooth") editor.putString(SHARED_PREFS_TAB, TAB_BLUETOOTH)
} }
editor.putString(SHARED_PREFS_DEVICE_NAME, btDeviceName.text.toString()) editor.putString(SHARED_PREFS_DEVICE_NAME, btDeviceName.text.toString())

Loading…
Cancel
Save