Stop using LocalBroadcastManager

This commit is contained in:
Mygod
2018-05-09 15:56:37 -07:00
parent 1695295eb3
commit e72dad8790
8 changed files with 83 additions and 62 deletions

View File

@@ -5,16 +5,15 @@ import android.content.Context
import android.content.Intent
import android.content.ServiceConnection
import android.graphics.drawable.Icon
import android.net.wifi.p2p.WifiP2pGroup
import android.os.IBinder
import android.service.quicksettings.Tile
import android.service.quicksettings.TileService
import android.support.annotation.RequiresApi
import android.support.v4.content.ContextCompat
import android.support.v4.content.LocalBroadcastManager
@RequiresApi(24)
class RepeaterTileService : TileService(), ServiceConnection {
private val statusListener = broadcastReceiver { _, _ -> updateTile() }
private val tileOff by lazy { Icon.createWithResource(application, R.drawable.ic_quick_settings_tile_off) }
private val tileOn by lazy { Icon.createWithResource(application, R.drawable.ic_quick_settings_tile_on) }
@@ -41,18 +40,20 @@ class RepeaterTileService : TileService(), ServiceConnection {
}
override fun onServiceConnected(name: ComponentName?, service: IBinder) {
binder = service as RepeaterService.RepeaterBinder
updateTile()
LocalBroadcastManager.getInstance(this).registerReceiver(statusListener,
intentFilter(RepeaterService.ACTION_STATUS_CHANGED))
val binder = service as RepeaterService.RepeaterBinder
this.binder = binder
binder.statusChanged[this] = { updateTile() }
binder.groupChanged[this] = this::updateTile
}
override fun onServiceDisconnected(name: ComponentName?) {
binder = null
LocalBroadcastManager.getInstance(this).unregisterReceiver(statusListener)
val binder = binder ?: return
this.binder = null
binder.statusChanged -= this
binder.groupChanged -= this
}
private fun updateTile() {
private fun updateTile(group: WifiP2pGroup? = binder?.service?.group) {
val qsTile = qsTile ?: return
when (binder?.service?.status) {
RepeaterService.Status.IDLE -> {
@@ -68,7 +69,7 @@ class RepeaterTileService : TileService(), ServiceConnection {
RepeaterService.Status.ACTIVE -> {
qsTile.state = Tile.STATE_ACTIVE
qsTile.icon = tileOn
qsTile.label = binder?.ssid
qsTile.label = group?.networkName
}
null -> {
qsTile.state = Tile.STATE_UNAVAILABLE