Add a lot more QS tiles

Fix #53. Also fix Bluetooth connection leaks.
This commit is contained in:
Mygod
2018-12-25 11:37:36 +08:00
parent 365287202e
commit 426b93226d
15 changed files with 481 additions and 163 deletions

View File

@@ -0,0 +1,54 @@
package be.mygod.vpnhotspot.manage
import android.content.ComponentName
import android.content.Context
import android.content.Intent
import android.content.ServiceConnection
import android.graphics.drawable.Icon
import android.os.IBinder
import android.service.quicksettings.Tile
import androidx.annotation.RequiresApi
import androidx.core.content.ContextCompat
import be.mygod.vpnhotspot.LocalOnlyHotspotService
import be.mygod.vpnhotspot.R
import be.mygod.vpnhotspot.util.stopAndUnbind
@RequiresApi(26)
class LocalOnlyHotspotTileService : TetherListeningTileService(), ServiceConnection {
private val tile by lazy { Icon.createWithResource(application, R.drawable.ic_device_wifi_tethering) }
private var binder: LocalOnlyHotspotService.Binder? = null
override fun onStartListening() {
super.onStartListening()
bindService(Intent(this, LocalOnlyHotspotService::class.java), this, Context.BIND_AUTO_CREATE)
}
override fun onStopListening() {
stopAndUnbind(this)
super.onStopListening()
}
override fun onClick() {
val binder = binder
if (binder?.iface != null) binder.stop()
else ContextCompat.startForegroundService(this, Intent(this, LocalOnlyHotspotService::class.java))
}
override fun onServiceConnected(name: ComponentName?, service: IBinder) {
binder = service as LocalOnlyHotspotService.Binder
updateTile()
}
override fun onServiceDisconnected(name: ComponentName?) {
binder = null
}
override fun updateTile() {
qsTile?.run {
state = if (binder?.iface == null) Tile.STATE_INACTIVE else Tile.STATE_ACTIVE
icon = tile
label = getText(R.string.tethering_temp_hotspot)
updateTile()
}
}
}