11package com.ismartcoding.plain.services
22
3-
4- import android.annotation.SuppressLint
5- import android.content.BroadcastReceiver
6- import android.content.Context
73import android.content.Intent
8- import android.content.IntentFilter
94import android.graphics.drawable.Icon
105import android.service.quicksettings.Tile
116import android.service.quicksettings.TileService
12- import com.ismartcoding.lib.channel.sendEvent
7+ import com.ismartcoding.lib.channel.receiveEventHandler
138import 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
1710import 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
2014import com.ismartcoding.plain.preferences.WebPreference
2115import com.ismartcoding.plain.web.HttpServerManager
22- import java.lang.ref.SoftReference
2316
2417class QSTileService : TileService () {
18+ private var httpServerStateListener: ((HttpServerState ) -> Unit )? = null
19+
2520 fun setState (state : Int ) {
2621 if (state == Tile .STATE_INACTIVE ) {
2722 qsTile?.state = Tile .STATE_INACTIVE
@@ -36,86 +31,85 @@ class QSTileService : TileService() {
3631 qsTile?.updateTile()
3732 }
3833
39- @SuppressLint(" UnspecifiedRegisterReceiverFlag" )
4034 override fun onStartListening () {
4135 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+ }
4846 }
4947
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+ }
5174 }
5275
5376 override fun onStopListening () {
5477 super .onStopListening()
5578
56- unregisterReceiver(mMsgReceive)
57- mMsgReceive = null
79+ // Unregister HTTP server state listener
80+ httpServerStateListener = null
5881 }
5982
6083 override fun onClick () {
6184 super .onClick()
6285 when (qsTile.state) {
6386 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)
6797 }
6898 }
6999
70100 Tile .STATE_ACTIVE -> {
101+ // Stop service
102+ qsTile?.state = Tile .STATE_UNAVAILABLE
103+ qsTile?.updateTile()
104+
71105 coIO {
72106 WebPreference .putAsync(this @QSTileService, false )
73107 HttpServerManager .stopServiceAsync(this @QSTileService)
108+ setState(Tile .STATE_INACTIVE )
74109 }
75110 }
76111 }
77112 }
78113
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- }
97114
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- }
121115}
0 commit comments