添加设备
This commit is contained in:
@@ -0,0 +1,40 @@
|
||||
package com.example.defenseapplication.adapter
|
||||
|
||||
import android.view.LayoutInflater
|
||||
import android.view.View
|
||||
import android.view.ViewGroup
|
||||
import android.widget.ImageView
|
||||
import android.widget.TextView
|
||||
import androidx.recyclerview.widget.RecyclerView
|
||||
import com.example.defenseapplication.R
|
||||
|
||||
data class Device(
|
||||
val name: String,
|
||||
val icon: String
|
||||
)
|
||||
class AddDeviceAdapter(private val devices: List<Device>, private val onItemClick: (Device) -> Unit) :
|
||||
RecyclerView.Adapter<AddDeviceAdapter.ViewHolder>() {
|
||||
|
||||
inner class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
|
||||
val ivIcon: ImageView = itemView.findViewById(R.id.ivDeviceIcon2)
|
||||
val tvName: TextView = itemView.findViewById(R.id.tvDeviceName2) // 注意:ID 要和布局中一致
|
||||
}
|
||||
|
||||
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
|
||||
val view = LayoutInflater.from(parent.context).inflate(R.layout.item_device2, parent, false)
|
||||
return ViewHolder(view)
|
||||
}
|
||||
|
||||
override fun onBindViewHolder(holder: ViewHolder, position: Int) {
|
||||
val device = devices[position]
|
||||
holder.tvName.text = device.name
|
||||
// 根据 icon 字符串加载对应图片(假设 icon 是本地 drawable 的名称)
|
||||
val resId = holder.itemView.context.resources.getIdentifier(device.icon, "drawable", holder.itemView.context.packageName)
|
||||
if (resId != 0) holder.ivIcon.setImageResource(resId) else holder.ivIcon.setImageResource(R.drawable.img)
|
||||
|
||||
// 设置点击事件
|
||||
holder.itemView.setOnClickListener { onItemClick(device) }
|
||||
}
|
||||
|
||||
override fun getItemCount(): Int = devices.size
|
||||
}
|
||||
@@ -40,11 +40,11 @@ class DeviceAdapter(
|
||||
fun bind(item: DeviceUi) {
|
||||
binding.tvDeviceName.text = item.name
|
||||
if (item.isOnline) {
|
||||
binding.tvDeviceStatus.text = "\u25CF 正在运行"
|
||||
binding.tvDeviceStatus.setTextColor(binding.root.context.getColor(R.color.green))
|
||||
binding.tvDeviceName.text = "\u25CF 正在运行"
|
||||
binding.tvDeviceName.setTextColor(binding.root.context.getColor(R.color.green))
|
||||
} else {
|
||||
binding.tvDeviceStatus.text = "\u25CF 离线"
|
||||
binding.tvDeviceStatus.setTextColor(binding.root.context.getColor(R.color.red))
|
||||
binding.tvDeviceName.text = "\u25CF 离线"
|
||||
binding.tvDeviceName.setTextColor(binding.root.context.getColor(R.color.red))
|
||||
}
|
||||
binding.root.setOnClickListener {
|
||||
onItemClick(item)
|
||||
|
||||
@@ -1,21 +1,52 @@
|
||||
package com.example.defenseapplication.home
|
||||
|
||||
import android.content.Intent
|
||||
import android.os.Bundle
|
||||
import androidx.activity.enableEdgeToEdge
|
||||
import android.view.View
|
||||
import android.widget.Toast
|
||||
import androidx.appcompat.app.AppCompatActivity
|
||||
import androidx.core.view.ViewCompat
|
||||
import androidx.core.view.WindowInsetsCompat
|
||||
import com.example.defenseapplication.R
|
||||
import androidx.recyclerview.widget.GridLayoutManager
|
||||
import com.example.defenseapplication.adapter.AddDeviceAdapter
|
||||
import com.example.defenseapplication.adapter.Device
|
||||
import com.example.defenseapplication.databinding.AddDeviceActivityBinding
|
||||
|
||||
class AddDeviceActivity : AppCompatActivity() {
|
||||
|
||||
private lateinit var binding: AddDeviceActivityBinding
|
||||
|
||||
override fun onCreate(savedInstanceState: Bundle?) {
|
||||
super.onCreate(savedInstanceState)
|
||||
enableEdgeToEdge()
|
||||
setContentView(R.layout.add_device_activity)
|
||||
ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main)) { v, insets ->
|
||||
val systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars())
|
||||
v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom)
|
||||
insets
|
||||
binding = AddDeviceActivityBinding.inflate(layoutInflater)
|
||||
setContentView(binding.root)
|
||||
|
||||
binding.ivBack.setOnClickListener {
|
||||
finish()
|
||||
}
|
||||
|
||||
setupDeviceList()
|
||||
}
|
||||
|
||||
private fun setupDeviceList() {
|
||||
val devices = listOf(
|
||||
Device("智能挂式防御机", "device1"),
|
||||
Device("智能防御机", "device2")
|
||||
)
|
||||
|
||||
if (devices.isEmpty()) {
|
||||
binding.emptyState.visibility = View.VISIBLE
|
||||
binding.rvDevices.visibility = View.GONE
|
||||
} else {
|
||||
binding.emptyState.visibility = View.GONE
|
||||
binding.rvDevices.visibility = View.VISIBLE
|
||||
|
||||
binding.rvDevices.layoutManager = GridLayoutManager(this, 2)
|
||||
// 传入点击回调
|
||||
binding.rvDevices.adapter = AddDeviceAdapter(devices) { device ->
|
||||
// 跳转到 AddFamilyActivity(选择Wi-Fi页面)
|
||||
val intent = Intent(this, AddFamilyActivity::class.java)
|
||||
intent.putExtra("device_name", device.name)
|
||||
startActivity(intent)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,158 @@
|
||||
package com.example.defenseapplication.home
|
||||
|
||||
import android.Manifest
|
||||
import android.content.BroadcastReceiver
|
||||
import android.content.Context
|
||||
import android.content.Intent
|
||||
import android.content.IntentFilter
|
||||
import android.content.pm.PackageManager
|
||||
import android.net.wifi.ScanResult
|
||||
import android.net.wifi.WifiManager
|
||||
import android.os.Bundle
|
||||
import android.text.Editable
|
||||
import android.text.TextWatcher
|
||||
import android.view.Gravity
|
||||
import android.view.LayoutInflater
|
||||
import android.view.ViewGroup
|
||||
import android.widget.PopupWindow
|
||||
import androidx.appcompat.app.AppCompatActivity
|
||||
import androidx.core.app.ActivityCompat
|
||||
import androidx.recyclerview.widget.LinearLayoutManager
|
||||
import com.example.defenseapplication.R
|
||||
import com.example.defenseapplication.adapter.WifiListAdapter
|
||||
import com.example.defenseapplication.databinding.AddFamilyActivityBinding
|
||||
import com.example.defenseapplication.databinding.DialogWifiListBinding
|
||||
|
||||
class AddFamilyActivity : AppCompatActivity() {
|
||||
|
||||
private lateinit var binding: AddFamilyActivityBinding
|
||||
private lateinit var wifiManager: WifiManager
|
||||
private lateinit var wifiScanReceiver: BroadcastReceiver
|
||||
private var popupWindow: PopupWindow? = null
|
||||
|
||||
override fun onCreate(savedInstanceState: Bundle?) {
|
||||
super.onCreate(savedInstanceState)
|
||||
binding = AddFamilyActivityBinding.inflate(layoutInflater)
|
||||
setContentView(binding.root)
|
||||
|
||||
binding.ivBack.setOnClickListener {
|
||||
finish()
|
||||
}
|
||||
|
||||
binding.btnSelectWifi.setOnClickListener {
|
||||
showWifiListDialog()
|
||||
}
|
||||
|
||||
binding.etWifiName.addTextChangedListener(object : TextWatcher {
|
||||
override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {}
|
||||
|
||||
override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {}
|
||||
|
||||
override fun afterTextChanged(s: Editable?) {
|
||||
updateBottomButtonVisibility()
|
||||
}
|
||||
})
|
||||
|
||||
binding.btnConfirm.setOnClickListener {
|
||||
// 确认添加设备逻辑
|
||||
}
|
||||
|
||||
wifiManager = applicationContext.getSystemService(Context.WIFI_SERVICE) as WifiManager
|
||||
|
||||
wifiScanReceiver = object : BroadcastReceiver() {
|
||||
override fun onReceive(context: Context, intent: Intent) {
|
||||
val success = intent.getBooleanExtra(WifiManager.EXTRA_RESULTS_UPDATED, false)
|
||||
if (success) {
|
||||
updateWifiList()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
val deviceName = intent.getStringExtra("device_name")
|
||||
|
||||
binding.btnConfirm.setOnClickListener {
|
||||
val intent=Intent(this, SetNameActivity::class.java)
|
||||
startActivity( intent)
|
||||
}
|
||||
}
|
||||
|
||||
private fun showWifiListDialog() {
|
||||
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION)
|
||||
!= PackageManager.PERMISSION_GRANTED ||
|
||||
ActivityCompat.checkSelfPermission(this, Manifest.permission.CHANGE_WIFI_STATE)
|
||||
!= PackageManager.PERMISSION_GRANTED) {
|
||||
ActivityCompat.requestPermissions(this,
|
||||
arrayOf(Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.CHANGE_WIFI_STATE),
|
||||
1001)
|
||||
return
|
||||
}
|
||||
|
||||
if (!wifiManager.isWifiEnabled) {
|
||||
wifiManager.isWifiEnabled = true
|
||||
}
|
||||
|
||||
wifiManager.startScan()
|
||||
|
||||
val dialogBinding = DialogWifiListBinding.inflate(LayoutInflater.from(this))
|
||||
popupWindow = PopupWindow(
|
||||
dialogBinding.root,
|
||||
ViewGroup.LayoutParams.MATCH_PARENT,
|
||||
ViewGroup.LayoutParams.WRAP_CONTENT
|
||||
).apply {
|
||||
isFocusable = true
|
||||
setBackgroundDrawable(null)
|
||||
setOnDismissListener {
|
||||
binding.maskView.visibility = android.view.View.GONE
|
||||
}
|
||||
showAtLocation(window.decorView, Gravity.BOTTOM, 0, 0)
|
||||
}
|
||||
|
||||
binding.maskView.visibility = android.view.View.VISIBLE
|
||||
|
||||
dialogBinding.rvWifiList.layoutManager = LinearLayoutManager(this@AddFamilyActivity)
|
||||
|
||||
val wifiList = getWifiList()
|
||||
dialogBinding.rvWifiList.adapter = WifiListAdapter(wifiList) { wifiName ->
|
||||
binding.etWifiName.setText(wifiName)
|
||||
popupWindow?.dismiss()
|
||||
}
|
||||
}
|
||||
|
||||
private fun updateBottomButtonVisibility() {
|
||||
val wifiName = binding.etWifiName.text.toString().trim()
|
||||
binding.bottomButtonLayout.visibility =
|
||||
if (wifiName.isNotEmpty()) android.view.View.VISIBLE else android.view.View.GONE
|
||||
}
|
||||
|
||||
private fun getWifiList(): List<String> {
|
||||
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION)
|
||||
!= PackageManager.PERMISSION_GRANTED) {
|
||||
return emptyList()
|
||||
}
|
||||
val results: List<ScanResult> = wifiManager.scanResults
|
||||
return results.map { it.SSID }.distinct()
|
||||
}
|
||||
|
||||
private fun updateWifiList() {
|
||||
popupWindow?.contentView?.findViewById<androidx.recyclerview.widget.RecyclerView>(R.id.rvWifiList)?.let { recyclerView ->
|
||||
val wifiList = getWifiList()
|
||||
recyclerView.adapter = WifiListAdapter(wifiList) { wifiName ->
|
||||
binding.etWifiName.setText(wifiName)
|
||||
popupWindow?.dismiss()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
override fun onStart() {
|
||||
super.onStart()
|
||||
val intentFilter = IntentFilter()
|
||||
intentFilter.addAction(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION)
|
||||
registerReceiver(wifiScanReceiver, intentFilter)
|
||||
}
|
||||
|
||||
override fun onStop() {
|
||||
super.onStop()
|
||||
unregisterReceiver(wifiScanReceiver)
|
||||
popupWindow?.dismiss()
|
||||
}
|
||||
}
|
||||
@@ -70,6 +70,14 @@ class HomeMenuFragment : Fragment() {
|
||||
val intent = Intent(requireContext(), MessageActivity::class.java)
|
||||
startActivity(intent)
|
||||
}
|
||||
binding.add.setOnClickListener {
|
||||
val intent = Intent(requireContext(), AddDeviceActivity::class.java)
|
||||
startActivity(intent)
|
||||
}
|
||||
binding.addFamily.setOnClickListener {
|
||||
val intent = Intent(requireContext(), AddFamilyActivity::class.java)
|
||||
startActivity(intent)
|
||||
}
|
||||
}
|
||||
|
||||
private fun initView() {
|
||||
|
||||
@@ -0,0 +1,25 @@
|
||||
package com.example.defenseapplication.home
|
||||
|
||||
import android.os.Bundle
|
||||
import androidx.appcompat.app.AppCompatActivity
|
||||
import com.example.defenseapplication.databinding.SetNameActivityBinding
|
||||
|
||||
class SetNameActivity : AppCompatActivity() {
|
||||
|
||||
private lateinit var binding: SetNameActivityBinding
|
||||
|
||||
override fun onCreate(savedInstanceState: Bundle?) {
|
||||
super.onCreate(savedInstanceState)
|
||||
binding = SetNameActivityBinding.inflate(layoutInflater)
|
||||
setContentView(binding.root)
|
||||
|
||||
binding.ivBack.setOnClickListener {
|
||||
finish()
|
||||
}
|
||||
|
||||
binding.btnConfirm.setOnClickListener {
|
||||
val deviceName = binding.etName.text.toString().trim()
|
||||
finish()
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user