From 7ad3823f9bb1c9a20418e236be5d43e68aa24c18 Mon Sep 17 00:00:00 2001 From: Zhengyu Peng Date: Mon, 21 Feb 2022 21:18:57 -0500 Subject: [PATCH] update --- .../rookiedev/hexapod/DeviceListActivity.kt | 25 ------ .../com/rookiedev/hexapod/MainActivity.kt | 81 ++++++++++++------- .../app/src/main/res/layout/activity_main.xml | 15 +--- .../app/src/main/res/layout/device_name.xml | 18 ----- .../src/main/res/layout/selected_device.xml | 4 +- 5 files changed, 57 insertions(+), 86 deletions(-) delete mode 100644 software/android/app/src/main/res/layout/device_name.xml diff --git a/software/android/app/src/main/java/com/rookiedev/hexapod/DeviceListActivity.kt b/software/android/app/src/main/java/com/rookiedev/hexapod/DeviceListActivity.kt index 154adc7..c9f7a07 100644 --- a/software/android/app/src/main/java/com/rookiedev/hexapod/DeviceListActivity.kt +++ b/software/android/app/src/main/java/com/rookiedev/hexapod/DeviceListActivity.kt @@ -68,7 +68,6 @@ class DeviceListActivity : Activity() { @SuppressLint("MissingPermission") override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) - // Setup the window setContentView(R.layout.activity_device_list) @@ -77,14 +76,6 @@ class DeviceListActivity : Activity() { // Set result CANCELED in case the user backs out setResult(RESULT_CANCELED) - // Initialize array adapters. One for already paired devices and - // one for newly discovered devices - val pairedDevicesArrayAdapter = ArrayAdapter(this, R.layout.device_list) -// val bluetoothAdapter = BluetoothAdapter(this, R.layout.device_list, ) - - - - // Get the local Bluetooth adapter bluetoothManager = mContext!!.getSystemService(Context.BLUETOOTH_SERVICE) as BluetoothManager @@ -97,22 +88,6 @@ class DeviceListActivity : Activity() { val bluetoothAdapter = BluetoothAdapter(this, deviceList) -// // If there are paired devices, add each one to the ArrayAdapter -// if (pairedDevices.isNotEmpty()) { -// for (device in pairedDevices) { -// -// pairedDevicesArrayAdapter.add( -// """ -// ${device.name} -// ${device.address} -// """.trimIndent() -// ) -// } -// } else { -// val noDevices = "No device" -// pairedDevicesArrayAdapter.add(noDevices) -// } - // Find and set up the ListView for paired devices val pairedListView: ListView = findViewById(R.id.paired_devices) pairedListView.adapter = bluetoothAdapter diff --git a/software/android/app/src/main/java/com/rookiedev/hexapod/MainActivity.kt b/software/android/app/src/main/java/com/rookiedev/hexapod/MainActivity.kt index 2d494c1..a0a3ba9 100644 --- a/software/android/app/src/main/java/com/rookiedev/hexapod/MainActivity.kt +++ b/software/android/app/src/main/java/com/rookiedev/hexapod/MainActivity.kt @@ -1,37 +1,30 @@ package com.rookiedev.hexapod -import android.Manifest import android.app.Activity import android.content.Context import android.content.Intent import android.content.pm.PackageManager import android.net.InetAddresses.isNumericAddress -import android.os.Build import android.os.Bundle import android.text.Editable import android.text.TextWatcher import android.text.method.LinkMovementMethod import android.view.View -import android.widget.AdapterView.OnItemClickListener import android.widget.Button -import android.widget.ListView import android.widget.TextView import android.widget.Toast import androidx.activity.result.contract.ActivityResultContracts -import androidx.annotation.RequiresApi import androidx.appcompat.app.AppCompatActivity import androidx.constraintlayout.widget.ConstraintLayout import androidx.core.app.ActivityCompat import com.google.android.material.tabs.TabLayout import com.google.android.material.textfield.TextInputEditText import com.google.android.material.textfield.TextInputLayout -import org.w3c.dom.Text class MainActivity : AppCompatActivity() { companion object { private const val BLUETOOTH_PERMISSION_CODE = 100 - private const val INTERNET_PERMISSION_CODE = 101 } private val REQUEST_CONNECT_DEVICE_SECURE = 1 @@ -41,6 +34,9 @@ class MainActivity : AppCompatActivity() { private val SHAREDPREFSNAME = "com.rookiedev.hexapod_preferences" private val SHAREDPREFSIP = "IP" private val SHAREDPREFSPORT = "PORT" + private val SHARED_PREFS_TAB = "TAB" + private val SHARED_PREFS_DEVICE_NAME = "DEVICE_NAME" + private val SHARED_PREFS_DEVICE_ADDRESS = "DEVICE_ADDRESS" private var mContext: Context? = null @@ -50,6 +46,8 @@ class MainActivity : AppCompatActivity() { private lateinit var deviceName: TextView private lateinit var deviceAddress: TextView + private lateinit var tabLayout: TabLayout + override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) @@ -63,29 +61,25 @@ class MainActivity : AppCompatActivity() { val ipLayout = findViewById(R.id.ip_input_layout) val portLayout = findViewById(R.id.port_input_layout) - val deviceList = findViewById(R.id.paired_devices) val selectedDevice = findViewById(R.id.selected) - deviceName = findViewById(R.id.textView_device_name) - deviceAddress = findViewById(R.id.textView_device_address) + deviceName = findViewById(R.id.textView_device_name) + deviceAddress = findViewById(R.id.textView_device_address) val sourceLink = findViewById(R.id.textView_github) sourceLink.movementMethod = LinkMovementMethod.getInstance() + tabLayout = findViewById(R.id.tab) - val tabLayout = findViewById(R.id.tab) tabLayout.addOnTabSelectedListener( object : TabLayout.OnTabSelectedListener { - @RequiresApi(Build.VERSION_CODES.S) override fun onTabSelected(tab: TabLayout.Tab?) { if (tab!!.text == "WiFi") { ipLayout.visibility = View.VISIBLE portLayout.visibility = View.VISIBLE - deviceList.visibility = View.GONE selectedDevice.visibility = View.GONE } else if (tab.text == "Bluetooth") { ipLayout.visibility = View.GONE portLayout.visibility = View.GONE - deviceList.visibility = View.VISIBLE selectedDevice.visibility = View.VISIBLE } } @@ -99,7 +93,7 @@ class MainActivity : AppCompatActivity() { ) selectedDevice.setOnClickListener { - checkPermission(Manifest.permission.BLUETOOTH_CONNECT, BLUETOOTH_PERMISSION_CODE) + checkPermission("android.permission.BLUETOOTH_CONNECT", BLUETOOTH_PERMISSION_CODE) val serverIntent = Intent(this, DeviceListActivity::class.java) resultLauncher.launch(serverIntent) // startActivityForResult(serverIntent, REQUEST_CONNECT_DEVICE_INSECURE) @@ -107,6 +101,17 @@ class MainActivity : AppCompatActivity() { readSharedPref() + if (tabLayout.selectedTabPosition == 0){ + ipLayout.visibility = View.VISIBLE + portLayout.visibility = View.VISIBLE + selectedDevice.visibility = View.GONE + } else if (tabLayout.selectedTabPosition == 1){ + checkPermission("android.permission.BLUETOOTH_CONNECT", BLUETOOTH_PERMISSION_CODE) + ipLayout.visibility = View.GONE + portLayout.visibility = View.GONE + selectedDevice.visibility = View.VISIBLE + } + buttonConnect.setOnClickListener { // your code to perform when the user clicks on the button @@ -147,7 +152,7 @@ class MainActivity : AppCompatActivity() { }) } - var resultLauncher = + private var resultLauncher = registerForActivityResult(ActivityResultContracts.StartActivityForResult()) { result -> if (result.resultCode == Activity.RESULT_OK) { // There are no request codes @@ -166,12 +171,12 @@ class MainActivity : AppCompatActivity() { permission ) == PackageManager.PERMISSION_DENIED ) { - // Requesting the permission ActivityCompat.requestPermissions(this@MainActivity, arrayOf(permission), requestCode) } else { - Toast.makeText(this@MainActivity, "Permission already granted", Toast.LENGTH_SHORT) - .show() +// Toast.makeText(this@MainActivity, "Permission already granted", Toast.LENGTH_SHORT) +// .show() + } } @@ -186,23 +191,20 @@ class MainActivity : AppCompatActivity() { super.onRequestPermissionsResult(requestCode, permissions, grantResults) if (requestCode == BLUETOOTH_PERMISSION_CODE) { if (grantResults.isNotEmpty() && grantResults[0] == PackageManager.PERMISSION_GRANTED) { - Toast.makeText(this@MainActivity, "Camera Permission Granted", Toast.LENGTH_SHORT) - .show() - } else { - Toast.makeText(this@MainActivity, "Camera Permission Denied", Toast.LENGTH_SHORT) - .show() - } - } else if (requestCode == INTERNET_PERMISSION_CODE) { - if (grantResults.isNotEmpty() && grantResults[0] == PackageManager.PERMISSION_GRANTED) { - Toast.makeText(this@MainActivity, "Storage Permission Granted", Toast.LENGTH_SHORT) + Toast.makeText(this@MainActivity, "Bluetooth Permission Granted", Toast.LENGTH_SHORT) .show() } else { - Toast.makeText(this@MainActivity, "Storage Permission Denied", Toast.LENGTH_SHORT) + Toast.makeText(this@MainActivity, "Bluetooth Permission Denied", Toast.LENGTH_SHORT) .show() } } } + override fun onPause() { + super.onPause() + saveSharedPref() + } + private fun readSharedPref() { val prefs = getSharedPreferences( @@ -212,6 +214,18 @@ class MainActivity : AppCompatActivity() { // read values from the shared preferences ipInput.setText(prefs.getString(SHAREDPREFSIP, "192.168.1.127")) portInput.setText(prefs.getString(SHAREDPREFSPORT, "1234")) + + val selectedTab = prefs.getString(SHARED_PREFS_TAB, "WiFi") + if (selectedTab == "WiFi"){ + val tab = tabLayout.getTabAt(0) + tab!!.select() + } else if (selectedTab == "Bluetooth") { + val tab = tabLayout.getTabAt(1) + tab!!.select() + } + + deviceName.text = prefs.getString(SHARED_PREFS_DEVICE_NAME, "Click to select a device") + deviceAddress.text = prefs.getString(SHARED_PREFS_DEVICE_ADDRESS, "") } private fun saveSharedPref() { @@ -222,6 +236,15 @@ class MainActivity : AppCompatActivity() { val editor = prefs.edit() editor.putString(SHAREDPREFSIP, ipInput.text.toString()) editor.putString(SHAREDPREFSPORT, portInput.text.toString()) + if (tabLayout.selectedTabPosition == 0){ + editor.putString(SHARED_PREFS_TAB, "WiFi") + } else if (tabLayout.selectedTabPosition == 1){ + editor.putString(SHARED_PREFS_TAB, "Bluetooth") + } + + editor.putString(SHARED_PREFS_DEVICE_NAME, deviceName.text.toString()) + editor.putString(SHARED_PREFS_DEVICE_ADDRESS, deviceAddress.text.toString()) + editor.apply() } } \ No newline at end of file diff --git a/software/android/app/src/main/res/layout/activity_main.xml b/software/android/app/src/main/res/layout/activity_main.xml index f9ad9a8..0d70ef4 100644 --- a/software/android/app/src/main/res/layout/activity_main.xml +++ b/software/android/app/src/main/res/layout/activity_main.xml @@ -37,11 +37,13 @@ app:layout_constraintTop_toBottomOf="@+id/hexapod_logo"> @@ -92,19 +94,6 @@ - - - - \ No newline at end of file diff --git a/software/android/app/src/main/res/layout/selected_device.xml b/software/android/app/src/main/res/layout/selected_device.xml index 0b1dfdc..d530ae6 100644 --- a/software/android/app/src/main/res/layout/selected_device.xml +++ b/software/android/app/src/main/res/layout/selected_device.xml @@ -6,7 +6,9 @@ android:layout_height="wrap_content" android:clickable="true" android:focusable="true" - android:background="?android:attr/selectableItemBackground"> + android:background="?android:attr/selectableItemBackground" + android:paddingTop="24dp" + android:paddingBottom="24dp">