@@ -9,14 +9,20 @@ import android.content.Intent
9
9
import android.content.ServiceConnection
10
10
import android.os.Build
11
11
import android.os.IBinder
12
+ import android.util.Log
12
13
import androidx.core.app.NotificationCompat
13
- import androidx.preference.PreferenceManager
14
+ import androidx.lifecycle.ProcessLifecycleOwner
14
15
import com.parse.Parse
15
16
import io.treehouses.remote.network.BluetoothChatService
17
+ import io.treehouses.remote.utils.AppLifecycleObserver
18
+ import io.treehouses.remote.utils.AppLifecycleTracker
19
+ import io.treehouses.remote.utils.GPSService
16
20
import io.treehouses.remote.utils.SaveUtils
17
21
18
22
class MainApplication : Application () {
19
23
var logSent = false
24
+ private lateinit var appLifecycleObserver: AppLifecycleObserver
25
+ private lateinit var activityLifecycleTracker: AppLifecycleTracker
20
26
21
27
override fun onCreate () {
22
28
super .onCreate()
@@ -27,79 +33,100 @@ class MainApplication : Application() {
27
33
terminalList = ArrayList ()
28
34
tunnelList = ArrayList ()
29
35
commandList = ArrayList ()
30
- Parse .initialize(Parse .Configuration .Builder (this )
36
+ Parse .initialize(
37
+ Parse .Configuration .Builder (this )
31
38
.applicationId(Constants .PARSE_APPLICATION_ID )
32
39
.clientKey(null )
33
40
.server(Constants .PARSE_URL )
34
41
.build()
35
42
)
36
43
SaveUtils .initCommandsList(applicationContext)
44
+
45
+ appLifecycleObserver = AppLifecycleObserver ()
46
+ ProcessLifecycleOwner .get().lifecycle.addObserver(appLifecycleObserver)
47
+
48
+ activityLifecycleTracker = AppLifecycleTracker ()
49
+ registerActivityLifecycleCallbacks(activityLifecycleTracker)
37
50
}
38
51
39
52
private val connection = object : ServiceConnection {
40
-
41
53
override fun onServiceConnected (className : ComponentName , service : IBinder ) {
42
- // We've bound to LocalService, cast the IBinder and get LocalService instance
43
54
val binder = service as BluetoothChatService .LocalBinder
44
55
mChatService = binder.service
45
- // sendBroadcast(Intent().setAction(BLUETOOTH_SERVICE_CONNECTED))
46
56
}
47
57
48
58
override fun onServiceDisconnected (arg0 : ComponentName ) {
49
59
mChatService = null
50
60
}
51
61
}
52
62
53
- fun getCurrentBluetoothService () : BluetoothChatService ? {
63
+ fun getCurrentBluetoothService (): BluetoothChatService ? {
54
64
return mChatService
55
65
}
56
66
57
67
fun startBluetoothService () {
58
- Intent (this , BluetoothChatService ::class .java).also { intent -> bindService(intent, connection, Context .BIND_AUTO_CREATE ) }
68
+ Intent (this , BluetoothChatService ::class .java).also { intent ->
69
+ bindService(intent, connection, Context .BIND_AUTO_CREATE )
70
+ }
59
71
}
60
72
61
73
fun stopBluetoothService () {
62
- if (! PreferenceManager .getDefaultSharedPreferences(this ).getBoolean(Constants .KEEP_BLUETOOTH_ALIVE , false )) {
63
- try {unbindService(connection)} catch (e: Exception ) {}
74
+ try {
75
+ unbindService(connection)
76
+ mChatService = null
77
+ } catch (e: Exception ) {
78
+ e.printStackTrace()
64
79
}
65
80
81
+ val bluetoothIntent = Intent (this , BluetoothChatService ::class .java)
82
+ stopService(bluetoothIntent)
66
83
}
67
84
68
-
69
-
70
85
private fun createNotificationChannel () {
71
86
if (Build .VERSION .SDK_INT >= Build .VERSION_CODES .O ) {
72
87
val bluetoothChannel = NotificationChannel (
73
- getString(R .string.bt_notification_ID),
74
- getString(R .string.bt_notification_channel),
75
- NotificationManager .IMPORTANCE_HIGH ).apply {
88
+ getString(R .string.bt_notification_ID),
89
+ getString(R .string.bt_notification_channel),
90
+ NotificationManager .IMPORTANCE_HIGH
91
+ ).apply {
76
92
description = getString(R .string.bt_notification_description)
77
93
lockscreenVisibility = NotificationCompat .VISIBILITY_PRIVATE
78
94
}
79
95
80
- // Register the channel with the system
81
- val notificationManager : NotificationManager = getSystemService(Context .NOTIFICATION_SERVICE ) as NotificationManager
96
+ val notificationManager : NotificationManager =
97
+ getSystemService(Context .NOTIFICATION_SERVICE ) as NotificationManager
82
98
notificationManager.createNotificationChannels(listOf (bluetoothChannel))
83
99
}
84
100
}
85
101
102
+ fun stopAllServices () {
103
+ stopBluetoothService()
104
+ val gpsIntent = Intent (this , GPSService ::class .java)
105
+ stopService(gpsIntent)
106
+ Log .d(" MainApplication" , " All services stopped" )
107
+ }
108
+
86
109
companion object {
87
110
const val BLUETOOTH_SERVICE_CONNECTED = " BLUETOOTH_SERVICE_CONNECTED"
111
+
88
112
@JvmStatic
89
113
var terminalList: ArrayList <String >? = null
90
114
private set
115
+
91
116
@JvmStatic
92
117
var tunnelList: ArrayList <String >? = null
93
118
private set
119
+
94
120
@JvmStatic
95
121
lateinit var commandList: ArrayList <String >
96
122
private set
123
+
97
124
@JvmField
98
125
var showLogDialog = true
126
+
99
127
@JvmField
100
128
var ratingDialog = true
101
- lateinit var context: Context
102
-
103
- var mChatService : BluetoothChatService ? = null
129
+ lateinit var context: Context
130
+ var mChatService: BluetoothChatService ? = null
104
131
}
105
132
}
0 commit comments