1
1
package com.ismartcoding.plain.services
2
2
3
-
4
- import android.annotation.SuppressLint
5
- import android.content.BroadcastReceiver
6
- import android.content.Context
7
3
import android.content.Intent
8
- import android.content.IntentFilter
9
4
import android.graphics.drawable.Icon
10
5
import android.service.quicksettings.Tile
11
6
import android.service.quicksettings.TileService
12
- import com.ismartcoding.lib.channel.sendEvent
7
+ import com.ismartcoding.lib.channel.receiveEventHandler
13
8
import com.ismartcoding.lib.helpers.CoroutinesHelper.coIO
14
- import com.ismartcoding.lib.isTPlus
15
- import com.ismartcoding.plain.BuildConfig
16
- import com.ismartcoding.plain.Constants
9
+ import com.ismartcoding.lib.logcat.LogCat
17
10
import com.ismartcoding.plain.R
18
- import com.ismartcoding.plain.enums.QSTileServiceAction
19
- import com.ismartcoding.plain.events.StartHttpServerEvent
11
+ import com.ismartcoding.plain.TempData
12
+ import com.ismartcoding.plain.enums.HttpServerState
13
+ import com.ismartcoding.plain.events.HttpServerStateChangedEvent
20
14
import com.ismartcoding.plain.preferences.WebPreference
21
15
import com.ismartcoding.plain.web.HttpServerManager
22
- import java.lang.ref.SoftReference
23
16
24
17
class QSTileService : TileService () {
18
+ private var httpServerStateListener: ((HttpServerState ) -> Unit )? = null
19
+
25
20
fun setState (state : Int ) {
26
21
if (state == Tile .STATE_INACTIVE ) {
27
22
qsTile?.state = Tile .STATE_INACTIVE
@@ -36,86 +31,85 @@ class QSTileService : TileService() {
36
31
qsTile?.updateTile()
37
32
}
38
33
39
- @SuppressLint(" UnspecifiedRegisterReceiverFlag" )
40
34
override fun onStartListening () {
41
35
super .onStartListening()
42
- setState(Tile .STATE_INACTIVE )
43
- mMsgReceive = ReceiveMessageHandler (this )
44
- if (isTPlus()) {
45
- registerReceiver(mMsgReceive, IntentFilter (Constants .BROADCAST_ACTION_ACTIVITY ), Context .RECEIVER_EXPORTED )
46
- } else {
47
- registerReceiver(mMsgReceive, IntentFilter (Constants .BROADCAST_ACTION_ACTIVITY ))
36
+
37
+ // Register HTTP server state listener
38
+ httpServerStateListener = { state ->
39
+ when (state) {
40
+ HttpServerState .ON -> setState(Tile .STATE_ACTIVE )
41
+ HttpServerState .OFF -> setState(Tile .STATE_INACTIVE )
42
+ HttpServerState .STARTING -> setState(Tile .STATE_INACTIVE )
43
+ HttpServerState .STOPPING -> setState(Tile .STATE_INACTIVE )
44
+ HttpServerState .ERROR -> setState(Tile .STATE_INACTIVE )
45
+ }
48
46
}
49
47
50
- sendMsg(this , Constants .BROADCAST_ACTION_SERVICE , QSTileServiceAction .MSG_REGISTER_CLIENT .value, " " )
48
+ // Listen for HTTP server state changes
49
+ receiveEventHandler<HttpServerStateChangedEvent > { event ->
50
+ httpServerStateListener?.invoke(event.state)
51
+ }
52
+
53
+ // Check current server state
54
+ coIO {
55
+ try {
56
+ // First check if webEnabled is true in TempData
57
+ if (TempData .webEnabled) {
58
+ val checkResult = HttpServerManager .checkServerAsync()
59
+ if (checkResult.websocket && checkResult.http) {
60
+ setState(Tile .STATE_ACTIVE )
61
+ } else {
62
+ // Service should be running but isn't responding
63
+ LogCat .d(" Web service enabled but not responding, setting inactive state" )
64
+ setState(Tile .STATE_INACTIVE )
65
+ }
66
+ } else {
67
+ setState(Tile .STATE_INACTIVE )
68
+ }
69
+ } catch (e: Exception ) {
70
+ LogCat .e(" Failed to check server state: ${e.message} " )
71
+ setState(Tile .STATE_INACTIVE )
72
+ }
73
+ }
51
74
}
52
75
53
76
override fun onStopListening () {
54
77
super .onStopListening()
55
78
56
- unregisterReceiver(mMsgReceive)
57
- mMsgReceive = null
79
+ // Unregister HTTP server state listener
80
+ httpServerStateListener = null
58
81
}
59
82
60
83
override fun onClick () {
61
84
super .onClick()
62
85
when (qsTile.state) {
63
86
Tile .STATE_INACTIVE -> {
64
- coIO {
65
- WebPreference .putAsync(this @QSTileService, true )
66
- sendEvent(StartHttpServerEvent ())
87
+ // Start the service directly
88
+ qsTile?.state = Tile .STATE_UNAVAILABLE
89
+ qsTile?.updateTile()
90
+
91
+ // Launch the app with unlockAndRun
92
+ unlockAndRun {
93
+ val intent = Intent (this , Class .forName(" com.ismartcoding.plain.ui.MainActivity" ))
94
+ intent.addFlags(Intent .FLAG_ACTIVITY_NEW_TASK )
95
+ intent.putExtra(" start_web_service" , true )
96
+ startActivity(intent)
67
97
}
68
98
}
69
99
70
100
Tile .STATE_ACTIVE -> {
101
+ // Stop service
102
+ qsTile?.state = Tile .STATE_UNAVAILABLE
103
+ qsTile?.updateTile()
104
+
71
105
coIO {
72
106
WebPreference .putAsync(this @QSTileService, false )
73
107
HttpServerManager .stopServiceAsync(this @QSTileService)
108
+ setState(Tile .STATE_INACTIVE )
74
109
}
75
110
}
76
111
}
77
112
}
78
113
79
- private var mMsgReceive: BroadcastReceiver ? = null
80
-
81
- private class ReceiveMessageHandler (context : QSTileService ) : BroadcastReceiver() {
82
- internal var mReference: SoftReference <QSTileService > = SoftReference (context)
83
- override fun onReceive (ctx : Context ? , intent : Intent ? ) {
84
- val context = mReference.get()
85
- when (intent?.getIntExtra(" key" , 0 )) {
86
- QSTileServiceAction .MSG_STATE_RUNNING .value -> {
87
- context?.setState(Tile .STATE_ACTIVE )
88
- }
89
-
90
- QSTileServiceAction .MSG_STATE_NOT_RUNNING .value -> {
91
- context?.setState(Tile .STATE_INACTIVE )
92
- }
93
-
94
- QSTileServiceAction .MSG_STATE_START_SUCCESS .value -> {
95
- context?.setState(Tile .STATE_ACTIVE )
96
- }
97
114
98
- QSTileServiceAction .MSG_STATE_START_FAILURE .value -> {
99
- context?.setState(Tile .STATE_INACTIVE )
100
- }
101
-
102
- QSTileServiceAction .MSG_STATE_STOP_SUCCESS .value -> {
103
- context?.setState(Tile .STATE_INACTIVE )
104
- }
105
- }
106
- }
107
- }
108
-
109
- private fun sendMsg (ctx : Context , action : String , what : Int , content : String ) {
110
- try {
111
- val intent = Intent ()
112
- intent.action = action
113
- intent.`package` = BuildConfig .APPLICATION_ID
114
- intent.putExtra(" key" , what)
115
- intent.putExtra(" content" , content)
116
- ctx.sendBroadcast(intent)
117
- } catch (e: Exception ) {
118
- e.printStackTrace()
119
- }
120
- }
121
115
}
0 commit comments