@@ -15,6 +15,8 @@ import android.service.quicksettings.Tile
15
15
import android.service.quicksettings.TileService
16
16
import android.util.Log
17
17
import au.id.colby.nfcquicksettings.R.*
18
+ import java.util.Timer
19
+ import kotlin.concurrent.fixedRateTimer
18
20
19
21
/* *
20
22
* A custom Quick Settings tile for NFC.
@@ -24,6 +26,7 @@ import au.id.colby.nfcquicksettings.R.*
24
26
*/
25
27
class NfcTileService : TileService () {
26
28
private val TAG = " NfcTileService"
29
+ private var updateTimer: Timer ? = null
27
30
28
31
/* *
29
32
* Called when this tile moves into a listening state.
@@ -34,18 +37,34 @@ class NfcTileService : TileService() {
34
37
override fun onStartListening () {
35
38
super .onStartListening()
36
39
Log .d(TAG , " onStartListening" )
37
- if (qsTile == null ) {
38
- Log .i(TAG , " qsTile is null; won't update at this time." )
39
- return
40
- }
41
40
val adapter: NfcAdapter ? = NfcAdapter .getDefaultAdapter(this )
42
- qsTile.state = if (adapter == null ) Tile .STATE_UNAVAILABLE else
43
- if (adapter.isEnabled) Tile .STATE_ACTIVE else Tile .STATE_INACTIVE
44
- if (Build .VERSION .SDK_INT >= Build .VERSION_CODES .Q ) qsTile.subtitle = getText(
45
- if (adapter == null ) string.tile_subtitle_unavailable else
46
- if (adapter.isEnabled) string.tile_subtitle_active else string.tile_subtitle_inactive
47
- )
48
- qsTile.updateTile()
41
+ updateTimer = fixedRateTimer(" default" , false , 0L , 500 ) {
42
+ Log .d(TAG , " updateTimer" )
43
+ qsTile?.apply {
44
+ Log .d(TAG , " Updating tile" )
45
+ state = if (adapter == null ) Tile .STATE_INACTIVE else
46
+ if (adapter.isEnabled) Tile .STATE_ACTIVE else Tile .STATE_INACTIVE
47
+ if (Build .VERSION .SDK_INT >= Build .VERSION_CODES .Q ) subtitle = getText(
48
+ if (adapter == null ) string.tile_subtitle_unavailable else
49
+ if (adapter.isEnabled) string.tile_subtitle_active else string.tile_subtitle_inactive
50
+ )
51
+ updateTile()
52
+ }
53
+ }
54
+ }
55
+
56
+ /* *
57
+ * Called when this tile moves out of the listening state.
58
+ *
59
+ * This override cancels the update timer, if any is running.
60
+ */
61
+ override fun onStopListening () {
62
+ Log .d(TAG , " onStopListening" )
63
+ updateTimer?.apply {
64
+ Log .d(TAG , " Cancelling update timer" )
65
+ cancel()
66
+ }
67
+ super .onStopListening()
49
68
}
50
69
51
70
/* *
0 commit comments