Do not update tile state when unsure

This commit is contained in:
Mygod
2018-12-25 12:57:25 +08:00
parent 36b0284573
commit b2534eb2c0
6 changed files with 35 additions and 25 deletions

View File

@@ -24,9 +24,9 @@ class BluetoothTethering(context: Context) : BluetoothProfile.ServiceListener, A
/**
* Based on: https://android.googlesource.com/platform/packages/apps/Settings/+/78d5efd/src/com/android/settings/TetherSettings.java
*/
val active: Boolean get() {
val pan = pan
return adapter?.state == BluetoothAdapter.STATE_ON && pan != null && isTetheringOn.invoke(pan) as Boolean
val active: Boolean? get() {
val pan = pan ?: return null
return adapter?.state == BluetoothAdapter.STATE_ON && isTetheringOn.invoke(pan) as Boolean
}
init {

View File

@@ -48,7 +48,7 @@ class LocalOnlyHotspotTileService : TetherListeningTileService() {
override fun updateTile() {
qsTile?.run {
state = if (binder?.iface == null) Tile.STATE_INACTIVE else Tile.STATE_ACTIVE
state = if ((binder ?: return).iface == null) Tile.STATE_INACTIVE else Tile.STATE_ACTIVE
icon = tile
label = getText(R.string.tethering_temp_hotspot)
updateTile()

View File

@@ -57,7 +57,7 @@ class RepeaterTileService : KillableTileService() {
private fun updateTile(group: WifiP2pGroup? = binder?.group) {
qsTile?.run {
when (binder?.service?.status) {
when ((binder ?: return).service.status) {
RepeaterService.Status.IDLE -> {
state = Tile.STATE_INACTIVE
label = getText(R.string.title_repeater)
@@ -66,7 +66,7 @@ class RepeaterTileService : KillableTileService() {
state = Tile.STATE_ACTIVE
label = group?.networkName
}
else -> { // STARTING, null or DESTROYED, which should never occur
else -> { // STARTING or DESTROYED, which should never occur
state = Tile.STATE_UNAVAILABLE
label = getText(R.string.title_repeater)
}

View File

@@ -17,7 +17,8 @@ abstract class TetherListeningTileService : KillableTileService() {
override fun onStartListening() {
super.onStartListening()
registerReceiver(receiver, IntentFilter(TetheringManager.ACTION_TETHER_STATE_CHANGED))
tethered = TetheringManager.getTetheredIfaces(registerReceiver(
receiver, IntentFilter(TetheringManager.ACTION_TETHER_STATE_CHANGED))?.extras ?: return)
}
override fun onStopListening() {

View File

@@ -157,7 +157,7 @@ sealed class TetherManager(protected val parent: TetheringFragment) : Manager(),
override val title get() = parent.getString(R.string.tethering_manage_bluetooth)
override val tetherType get() = TetherType.BLUETOOTH
override val type get() = VIEW_TYPE_BLUETOOTH
override val isStarted get() = tethering.active
override val isStarted get() = tethering.active == true
override fun start() = TetheringManager.start(TetheringManager.TETHERING_BLUETOOTH, true, this)
override fun stop() {

View File

@@ -61,8 +61,9 @@ sealed class TetheringTileService : TetherListeningTileService(), TetheringManag
state = Tile.STATE_INACTIVE
icon = tileOff
} else {
val binder = binder ?: return
state = Tile.STATE_ACTIVE
icon = if (interested.all { binder?.isActive(it) == true }) tileOn else tileOff
icon = if (interested.all { binder.isActive(it) }) tileOn else tileOff
}
label = getText(labelString)
updateTile()
@@ -141,29 +142,37 @@ sealed class TetheringTileService : TetherListeningTileService(), TetheringManag
override fun updateTile() {
qsTile?.run {
if (tethering?.active == true) {
state = Tile.STATE_ACTIVE
val interested = interested
icon = if (interested.isNotEmpty() && interested.all { binder?.isActive(it) == true })
tileOn else tileOff
} else {
state = Tile.STATE_INACTIVE
icon = tileOff
when (tethering?.active) {
true -> {
val binder = binder ?: return
state = Tile.STATE_ACTIVE
val interested = interested
icon = if (interested.isNotEmpty() && interested.all { binder.isActive(it) }) tileOn else tileOff
}
false -> {
state = Tile.STATE_INACTIVE
icon = tileOff
}
null -> return
}
label = getText(labelString)
updateTile()
}
}
override fun onClick() = if (tethering?.active == true) {
val binder = binder
if (binder == null) tapPending = true else {
val inactive = interested.filter { !binder.isActive(it) }
if (inactive.isEmpty()) safeInvoker { stop() }
else ContextCompat.startForegroundService(this, Intent(this, TetheringService::class.java)
.putExtra(TetheringService.EXTRA_ADD_INTERFACES, inactive.toTypedArray()))
override fun onClick() = when (tethering?.active) {
true -> {
val binder = binder
if (binder == null) tapPending = true else {
val inactive = interested.filter { !binder.isActive(it) }
if (inactive.isEmpty()) safeInvoker { stop() }
else ContextCompat.startForegroundService(this, Intent(this, TetheringService::class.java)
.putExtra(TetheringService.EXTRA_ADD_INTERFACES, inactive.toTypedArray()))
}
}
} else safeInvoker { start() }
false -> safeInvoker { start() }
else -> tapPending = true
}
}
@Suppress("DEPRECATION")