Implement connected devices (buggy debug version)
This commit is contained in:
@@ -7,10 +7,17 @@ import android.content.ServiceConnection
|
||||
import android.databinding.BaseObservable
|
||||
import android.databinding.Bindable
|
||||
import android.databinding.DataBindingUtil
|
||||
import android.net.wifi.p2p.WifiP2pDevice
|
||||
import android.os.Bundle
|
||||
import android.os.IBinder
|
||||
import android.support.v4.content.ContextCompat
|
||||
import android.support.v7.app.AppCompatActivity
|
||||
import android.support.v7.widget.DefaultItemAnimator
|
||||
import android.support.v7.widget.LinearLayoutManager
|
||||
import android.support.v7.widget.RecyclerView
|
||||
import android.view.LayoutInflater
|
||||
import android.view.ViewGroup
|
||||
import be.mygod.vpnhotspot.databinding.ClientBinding
|
||||
import be.mygod.vpnhotspot.databinding.MainActivityBinding
|
||||
|
||||
class MainActivity : AppCompatActivity(), ServiceConnection {
|
||||
@@ -37,23 +44,66 @@ class MainActivity : AppCompatActivity(), ServiceConnection {
|
||||
}
|
||||
}
|
||||
|
||||
val running get() = binder?.status == HotspotService.Status.ACTIVE
|
||||
val ssid: String @Bindable get() = if (running) binder!!.service.group.networkName else ""
|
||||
val password: String @Bindable get() = if (running) binder!!.service.group.passphrase else ""
|
||||
val clients @Bindable get() = if (running) binder!!.service.clients else null
|
||||
|
||||
fun onStatusChanged() {
|
||||
notifyPropertyChanged(BR.switchEnabled)
|
||||
notifyPropertyChanged(BR.serviceStarted)
|
||||
onGroupChanged()
|
||||
onClientsChanged()
|
||||
}
|
||||
fun onBinderChanged() {
|
||||
onStatusChanged()
|
||||
fun onGroupChanged() {
|
||||
notifyPropertyChanged(BR.ssid)
|
||||
notifyPropertyChanged(BR.password)
|
||||
}
|
||||
fun onClientsChanged() = adapter.fetchClients()
|
||||
}
|
||||
|
||||
class ClientViewHolder(val binding: ClientBinding) : RecyclerView.ViewHolder(binding.root)
|
||||
inner class ClientAdapter : RecyclerView.Adapter<ClientViewHolder>() {
|
||||
private var owner: WifiP2pDevice? = null
|
||||
private lateinit var clients: MutableCollection<WifiP2pDevice>
|
||||
|
||||
fun fetchClients() {
|
||||
val binder = binder!!
|
||||
if (data.running) {
|
||||
owner = binder.service.group.owner
|
||||
clients = binder.service.clients
|
||||
} else owner = null
|
||||
notifyDataSetChanged() // recreate everything
|
||||
}
|
||||
|
||||
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int) =
|
||||
ClientViewHolder(ClientBinding.inflate(LayoutInflater.from(parent.context)))
|
||||
|
||||
override fun onBindViewHolder(holder: ClientViewHolder, position: Int) {
|
||||
holder.binding.device = when (position) {
|
||||
0 -> owner
|
||||
else -> clients.elementAt(position - 1)
|
||||
}
|
||||
holder.binding.executePendingBindings()
|
||||
}
|
||||
|
||||
override fun getItemCount() = if (owner == null) 0 else 1 + clients.size
|
||||
}
|
||||
|
||||
private lateinit var binding: MainActivityBinding
|
||||
private val data = Data()
|
||||
private val adapter = ClientAdapter()
|
||||
private var binder: HotspotService.HotspotBinder? = null
|
||||
|
||||
override fun onCreate(savedInstanceState: Bundle?) {
|
||||
super.onCreate(savedInstanceState)
|
||||
binding = DataBindingUtil.setContentView(this, R.layout.main_activity)
|
||||
binding.data = data
|
||||
binding.clients.layoutManager = LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false)
|
||||
val animator = DefaultItemAnimator()
|
||||
animator.supportsChangeAnimations = false // prevent fading-in/out when rebinding
|
||||
binding.clients.itemAnimator = animator
|
||||
binding.clients.adapter = adapter
|
||||
}
|
||||
|
||||
override fun onStart() {
|
||||
@@ -70,11 +120,12 @@ class MainActivity : AppCompatActivity(), ServiceConnection {
|
||||
val binder = service as HotspotService.HotspotBinder
|
||||
binder.data = data
|
||||
this.binder = binder
|
||||
data.onBinderChanged()
|
||||
data.onStatusChanged()
|
||||
}
|
||||
|
||||
override fun onServiceDisconnected(name: ComponentName?) {
|
||||
binder?.data = null
|
||||
binder = null
|
||||
data.onStatusChanged()
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user