Skip to content

Commit 0210fb8

Browse files
committed
Code Refactored
1 parent 929614f commit 0210fb8

File tree

5 files changed

+64
-79
lines changed

5 files changed

+64
-79
lines changed

app/src/main/java/github/umer0586/sensorserver/activities/MainActivity.kt

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ import github.umer0586.sensorserver.service.SensorService
2222
import github.umer0586.sensorserver.service.SensorService.LocalBinder
2323
import github.umer0586.sensorserver.service.ServiceBindHelper
2424

25-
class MainActivity : AppCompatActivity(), NavigationBarView.OnItemSelectedListener, ServiceConnection
25+
class MainActivity : AppCompatActivity(), NavigationBarView.OnItemSelectedListener
2626
{
2727

2828
private lateinit var actionBarDrawerToggle: ActionBarDrawerToggle
@@ -61,10 +61,12 @@ class MainActivity : AppCompatActivity(), NavigationBarView.OnItemSelectedListen
6161

6262
serviceBindHelper = ServiceBindHelper(
6363
context = applicationContext,
64-
serviceConnection = this,
65-
service = SensorService::class.java
64+
service = SensorService::class.java,
65+
componentLifecycle = lifecycle
6666
)
67-
lifecycle.addObserver(serviceBindHelper)
67+
68+
serviceBindHelper.onServiceConnected(this::onServiceConnected)
69+
6870

6971

7072
binding.dashboard.viewPager.isUserInputEnabled = false
@@ -109,9 +111,9 @@ class MainActivity : AppCompatActivity(), NavigationBarView.OnItemSelectedListen
109111
}
110112

111113

112-
override fun onServiceConnected(name: ComponentName, service: IBinder)
114+
fun onServiceConnected(binder: IBinder)
113115
{
114-
val localBinder = service as LocalBinder
116+
val localBinder = binder as LocalBinder
115117
sensorService = localBinder.service
116118

117119
sensorService?.let{ setConnectionCountBadge( it.getConnectionCount() ) }
@@ -123,9 +125,6 @@ class MainActivity : AppCompatActivity(), NavigationBarView.OnItemSelectedListen
123125

124126
}
125127

126-
override fun onServiceDisconnected(name: ComponentName)
127-
{
128-
}
129128

130129
override fun onPause()
131130
{
@@ -136,12 +135,6 @@ class MainActivity : AppCompatActivity(), NavigationBarView.OnItemSelectedListen
136135
sensorService?.onConnectionsCountChange(callBack = null)
137136
}
138137

139-
override fun onDestroy()
140-
{
141-
super.onDestroy()
142-
Log.d(TAG, "onDestroy()")
143-
lifecycle.removeObserver(serviceBindHelper)
144-
}
145138

146139
private fun setConnectionCountBadge(totalConnections: Int)
147140
{

app/src/main/java/github/umer0586/sensorserver/activities/TouchScreenActivity.kt

Lines changed: 9 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import github.umer0586.sensorserver.service.ServiceBindHelper
1616
import github.umer0586.sensorserver.util.JsonUtil
1717
import github.umer0586.sensorserver.websocketserver.TouchSensors
1818

19-
class TouchScreenActivity : AppCompatActivity(),ServiceConnection
19+
class TouchScreenActivity : AppCompatActivity()
2020
{
2121
private val TAG = "TouchScreenActivity"
2222
private var sensorService: SensorService? = null
@@ -29,10 +29,15 @@ class TouchScreenActivity : AppCompatActivity(),ServiceConnection
2929

3030
val serviceBindHelper = ServiceBindHelper(
3131
context = applicationContext,
32-
serviceConnection = this,
33-
service = SensorService::class.java
32+
service = SensorService::class.java,
33+
componentLifecycle = lifecycle
3434
)
35-
lifecycle.addObserver(serviceBindHelper)
35+
36+
serviceBindHelper.onServiceConnected { binder ->
37+
38+
val localBinder = binder as SensorService.LocalBinder
39+
sensorService = localBinder.service
40+
}
3641

3742
}
3843

@@ -45,17 +50,5 @@ class TouchScreenActivity : AppCompatActivity(),ServiceConnection
4550
return super.onTouchEvent(event)
4651
}
4752

48-
override fun onServiceConnected(name: ComponentName?, service: IBinder?)
49-
{
50-
Log.d(TAG,"onServiceConnected()")
51-
52-
val localBinder = service as SensorService.LocalBinder
53-
sensorService = localBinder.service
5453

55-
}
56-
57-
override fun onServiceDisconnected(name: ComponentName?)
58-
{
59-
TODO("Not yet implemented")
60-
}
6154
}

app/src/main/java/github/umer0586/sensorserver/fragments/ConnectionsFragment.kt

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ import org.java_websocket.WebSocket
2121
/**
2222
* TODO: functionality to allow user to close all connections (using button in action bar)
2323
*/
24-
class ConnectionsFragment : Fragment(), ServiceConnection
24+
class ConnectionsFragment : Fragment()
2525
{
2626

2727
private var sensorService: SensorService? = null
@@ -56,10 +56,12 @@ class ConnectionsFragment : Fragment(), ServiceConnection
5656

5757
serviceBindHelper = ServiceBindHelper(
5858
context = requireContext(),
59-
serviceConnection = this,
60-
service = SensorService::class.java
59+
service = SensorService::class.java,
60+
componentLifecycle = lifecycle
6161
)
62-
lifecycle.addObserver(serviceBindHelper)
62+
63+
serviceBindHelper.onServiceConnected(this::onServiceConnected)
64+
6365
}
6466

6567
override fun onPause()
@@ -71,15 +73,10 @@ class ConnectionsFragment : Fragment(), ServiceConnection
7173
sensorService?.onConnectionsChange( callBack = null)
7274
}
7375

74-
override fun onDestroy()
75-
{
76-
super.onDestroy()
77-
lifecycle.removeObserver(serviceBindHelper)
78-
}
7976

80-
override fun onServiceConnected(name: ComponentName, service: IBinder)
77+
fun onServiceConnected(binder: IBinder)
8178
{
82-
val localBinder = service as LocalBinder
79+
val localBinder = binder as LocalBinder
8380
sensorService = localBinder.service
8481

8582

@@ -107,10 +104,6 @@ class ConnectionsFragment : Fragment(), ServiceConnection
107104

108105

109106

110-
}
111-
112-
override fun onServiceDisconnected(name: ComponentName)
113-
{
114107
}
115108

116109
override fun onDestroyView() {

app/src/main/java/github/umer0586/sensorserver/fragments/ServerFragment.kt

Lines changed: 12 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,9 @@
11
package github.umer0586.sensorserver.fragments
22

3-
import android.content.ComponentName
43
import android.content.Context
54
import android.content.Intent
6-
import android.content.ServiceConnection
75
import android.net.wifi.WifiManager
86
import android.os.Bundle
9-
import android.os.IBinder
107
import android.util.Log
118
import android.view.LayoutInflater
129
import android.view.View
@@ -28,7 +25,7 @@ import github.umer0586.sensorserver.websocketserver.ServerInfo
2825
import java.net.BindException
2926
import java.net.UnknownHostException
3027

31-
class ServerFragment : Fragment(), ServiceConnection, ServerStateListener
28+
class ServerFragment : Fragment(), ServerStateListener
3229
{
3330

3431
private var sensorService: SensorService? = null
@@ -59,10 +56,18 @@ class ServerFragment : Fragment(), ServiceConnection, ServerStateListener
5956

6057
serviceBindHelper = ServiceBindHelper(
6158
context = requireContext(),
62-
serviceConnection = this,
63-
service = SensorService::class.java
59+
service = SensorService::class.java,
60+
componentLifecycle = lifecycle
6461
)
65-
lifecycle.addObserver(serviceBindHelper)
62+
63+
serviceBindHelper.onServiceConnected { binder ->
64+
65+
val localBinder = binder as LocalBinder
66+
sensorService = localBinder.service
67+
68+
sensorService?.setServerStateListener(this)
69+
sensorService?.checkState() // this callbacks onServerAlreadyRunning()
70+
}
6671

6772
hidePulseAnimation()
6873
hideServerAddress()
@@ -217,25 +222,6 @@ class ServerFragment : Fragment(), ServiceConnection, ServerStateListener
217222
binding.serverAddress.visibility = View.GONE
218223
}
219224

220-
override fun onServiceConnected(name: ComponentName, service: IBinder)
221-
{
222-
val localBinder = service as LocalBinder
223-
sensorService = localBinder.service
224-
225-
sensorService?.setServerStateListener(this)
226-
sensorService?.checkState() // this callbacks onServerAlreadyRunning()
227-
228-
}
229-
230-
override fun onServiceDisconnected(name: ComponentName)
231-
{
232-
}
233-
234-
override fun onDestroy()
235-
{
236-
super.onDestroy()
237-
lifecycle.removeObserver(serviceBindHelper)
238-
}
239225

240226
override fun onDestroyView() {
241227
super.onDestroyView()

app/src/main/java/github/umer0586/sensorserver/service/ServiceBindHelper.kt

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,22 @@ import android.util.Log
1010
import androidx.lifecycle.Lifecycle
1111
import androidx.lifecycle.LifecycleEventObserver
1212
import androidx.lifecycle.LifecycleOwner
13-
import github.umer0586.sensorserver.service.ServiceBindHelper
1413

15-
class ServiceBindHelper(private val context: Context, private val serviceConnection: ServiceConnection, private val service: Class<out Service>
14+
class ServiceBindHelper(private val context: Context,
15+
private val service: Class<out Service>,
16+
componentLifecycle : Lifecycle
1617
) : ServiceConnection, LifecycleEventObserver
1718
{
1819

20+
init
21+
{
22+
// Make this class observe lifecycle of Activity/Fragment
23+
componentLifecycle.addObserver(this)
24+
}
25+
1926

2027
private var bounded = false
28+
private var onServiceConnectedCallBack : ((IBinder) -> Unit)? = null
2129

2230

2331
private fun bindToService()
@@ -38,23 +46,35 @@ class ServiceBindHelper(private val context: Context, private val serviceConnect
3846
}
3947
}
4048

41-
override fun onServiceConnected(name: ComponentName, service: IBinder)
49+
fun onServiceConnected(callBack: ((IBinder) -> Unit)?)
50+
{
51+
onServiceConnectedCallBack = callBack
52+
}
53+
54+
override fun onServiceConnected(name: ComponentName, binder: IBinder)
4255
{
4356
Log.d(TAG, "onServiceConnected()")
4457
bounded = true
45-
serviceConnection.onServiceConnected(name, service)
58+
59+
onServiceConnectedCallBack?.invoke(binder)
60+
4661
}
4762

63+
/* The onServiceDisconnected() method in Android is called when the connection to the service is unexpectedly disconnected,
64+
usually due to a crash or the service being killed by the system.
65+
This allows you to handle the situation and possibly attempt to reestablish the connection.
66+
onServiceDisconnected() method is not called when you explicitly call context.unbindService().
67+
It's only called when the connection to the service is unexpectedly lost, such as when the service process crashes or is killed by the system.*/
4868
override fun onServiceDisconnected(name: ComponentName)
4969
{
5070
Log.d(TAG, "onServiceDisconnected()")
5171
bounded = false
52-
serviceConnection.onServiceDisconnected(name)
72+
5373
}
5474

5575
override fun onStateChanged(source: LifecycleOwner, event: Lifecycle.Event)
5676
{
57-
Log.d(TAG + " : " + source.javaClass.getSimpleName(), event.name)
77+
Log.d(TAG + " : " + source.javaClass.simpleName, event.name)
5878

5979
when(event)
6080
{
@@ -68,6 +88,6 @@ class ServiceBindHelper(private val context: Context, private val serviceConnect
6888

6989
companion object
7090
{
71-
private val TAG: String = ServiceBindHelper::class.java.getSimpleName()
91+
private val TAG: String = ServiceBindHelper::class.java.simpleName
7292
}
7393
}

0 commit comments

Comments
 (0)