Add quick settings tile (#7)

* Add quick settings tile
* Launch MainActivity on QS_TILE_PREFERENCES
* Show repeater password on tile
This commit is contained in:
Alex Zhang
2018-02-17 12:20:34 +08:00
committed by Mygod
parent ea823833fd
commit 7ffb3b54ff
4 changed files with 124 additions and 1 deletions

View File

@@ -40,6 +40,15 @@
</service>
<service android:name=".TetheringService">
</service>
<service
android:name=".TileService"
android:icon="@drawable/ic_quick_settings_tile_off"
android:label="@string/app_name"
android:permission="android.permission.BIND_QUICK_SETTINGS_TILE">
<intent-filter>
<action android:name="android.service.quicksettings.action.QS_TILE" />
</intent-filter>
</service>
</application>
</manifest>

View File

@@ -0,0 +1,84 @@
package be.mygod.vpnhotspot
import android.content.ComponentName
import android.content.Context
import android.content.Intent
import android.content.ServiceConnection
import android.graphics.drawable.Icon
import android.os.Build
import android.os.IBinder
import android.service.quicksettings.Tile
import android.support.annotation.RequiresApi
import android.support.v4.content.ContextCompat
import android.support.v4.content.LocalBroadcastManager
import android.service.quicksettings.TileService as BaseTileService
@RequiresApi(Build.VERSION_CODES.N)
class TileService : BaseTileService(), ServiceConnection {
private var binder: RepeaterService.RepeaterBinder? = null
val onStatusChangedReceive = broadcastReceiver { _, _ ->
updateTile()
}
var qsTileState: Int
get() = qsTile.state
set(value) {
qsTile.state = value
when (value) {
Tile.STATE_ACTIVE -> {
qsTile.icon = Icon.createWithResource(application,
R.drawable.ic_quick_settings_tile_on)
qsTile.label = "${getString(R.string.repeater_password)}:\n${binder?.service?.password}"
}
Tile.STATE_INACTIVE -> {
qsTile.icon = Icon.createWithResource(application,
R.drawable.ic_quick_settings_tile_off)
qsTile.label = getString(R.string.app_name)
}
}
qsTile.updateTile()
}
override fun onStartListening() {
super.onStartListening()
bindService(Intent(this, RepeaterService::class.java), this,
Context.BIND_AUTO_CREATE)
}
override fun onStopListening() {
super.onStopListening()
unbindService(this)
}
override fun onClick() {
val binder = binder
when (binder?.service?.status) {
RepeaterService.Status.ACTIVE -> binder.shutdown()
RepeaterService.Status.IDLE -> ContextCompat.startForegroundService(this,
Intent(this, RepeaterService::class.java))
else -> {
}
}
}
override fun onServiceConnected(name: ComponentName?, service: IBinder) {
binder = service as RepeaterService.RepeaterBinder
updateTile()
LocalBroadcastManager.getInstance(this).registerReceiver(onStatusChangedReceive,
intentFilter(RepeaterService.ACTION_STATUS_CHANGED))
}
override fun onServiceDisconnected(name: ComponentName?) {
binder = null
LocalBroadcastManager.getInstance(this).unregisterReceiver(onStatusChangedReceive)
}
fun updateTile() {
qsTileState = when (binder?.service?.status) {
RepeaterService.Status.ACTIVE -> Tile.STATE_ACTIVE
RepeaterService.Status.IDLE -> Tile.STATE_INACTIVE
else -> Tile.STATE_UNAVAILABLE
}
}
}

View File

@@ -0,0 +1,13 @@
<?xml version="1.0" encoding="utf-8"?>
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportWidth="24"
android:viewportHeight="24">
<path
android:fillColor="#fff"
android:pathData="M14.36,10.22 A5.33,5.33,0,1,0,14.36,13.78 L18.22,13.78 L18.22,17.33 L21.78,17.33
L21.78,13.78 L23.56,13.78 L23.56,10.22 Z M9.36,13.78 A1.78,1.78,0,1,1,11.11,12
A1.78,1.78,0,0,1,9.33,13.78 Z" />
</vector>

View File

@@ -0,0 +1,17 @@
<?xml version="1.0" encoding="utf-8"?>
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportWidth="24"
android:viewportHeight="24">
<path
android:fillColor="#fff"
android:pathData="M14.36,10.22 A5.33,5.33,0,1,0,14.36,13.78 L18.22,13.78 L18.22,17.33 L21.78,17.33
L21.78,13.78 L23.56,13.78 L23.56,10.22 Z M9.36,13.78 A1.78,1.78,0,1,1,11.11,12
A1.78,1.78,0,0,1,9.33,13.78 Z" />
<path
android:fillColor="#fff"
android:pathData="M9.33,3.11 A8.89,8.89,0,0,0,4.88,19.69 L5.77,18.15 A7.11,7.11,0,1,1,12.89,18.15
L13.78,19.69 A8.89,8.89,0,0,0,9.33,3.11 Z" />
</vector>