Skip to content

Commit 70cc170

Browse files
committed
close foreground on app teminating
1 parent f35b94b commit 70cc170

File tree

5 files changed

+109
-157
lines changed

5 files changed

+109
-157
lines changed

app/build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ android.defaultConfig.vectorDrawables.useSupportLibrary = true
5656
dependencies {
5757
implementation fileTree(include: ['*.jar'], dir: 'libs')
5858
implementation 'androidx.swiperefreshlayout:swiperefreshlayout:1.1.0'
59+
implementation 'androidx.lifecycle:lifecycle-process:2.8.2'
5960
androidTestImplementation('androidx.test.espresso:espresso-core:3.5.1', {
6061
exclude group: 'com.android.support', module: 'support-annotations'
6162
})

app/src/main/kotlin/io/treehouses/remote/MainApplication.kt

Lines changed: 46 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,20 @@ import android.content.Intent
99
import android.content.ServiceConnection
1010
import android.os.Build
1111
import android.os.IBinder
12+
import android.util.Log
1213
import androidx.core.app.NotificationCompat
13-
import androidx.preference.PreferenceManager
14+
import androidx.lifecycle.ProcessLifecycleOwner
1415
import com.parse.Parse
1516
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
1620
import io.treehouses.remote.utils.SaveUtils
1721

1822
class MainApplication : Application() {
1923
var logSent = false
24+
private lateinit var appLifecycleObserver: AppLifecycleObserver
25+
private lateinit var activityLifecycleTracker: AppLifecycleTracker
2026

2127
override fun onCreate() {
2228
super.onCreate()
@@ -27,79 +33,100 @@ class MainApplication : Application() {
2733
terminalList = ArrayList()
2834
tunnelList = ArrayList()
2935
commandList = ArrayList()
30-
Parse.initialize(Parse.Configuration.Builder(this)
36+
Parse.initialize(
37+
Parse.Configuration.Builder(this)
3138
.applicationId(Constants.PARSE_APPLICATION_ID)
3239
.clientKey(null)
3340
.server(Constants.PARSE_URL)
3441
.build()
3542
)
3643
SaveUtils.initCommandsList(applicationContext)
44+
45+
appLifecycleObserver = AppLifecycleObserver()
46+
ProcessLifecycleOwner.get().lifecycle.addObserver(appLifecycleObserver)
47+
48+
activityLifecycleTracker = AppLifecycleTracker()
49+
registerActivityLifecycleCallbacks(activityLifecycleTracker)
3750
}
3851

3952
private val connection = object : ServiceConnection {
40-
4153
override fun onServiceConnected(className: ComponentName, service: IBinder) {
42-
// We've bound to LocalService, cast the IBinder and get LocalService instance
4354
val binder = service as BluetoothChatService.LocalBinder
4455
mChatService = binder.service
45-
// sendBroadcast(Intent().setAction(BLUETOOTH_SERVICE_CONNECTED))
4656
}
4757

4858
override fun onServiceDisconnected(arg0: ComponentName) {
4959
mChatService = null
5060
}
5161
}
5262

53-
fun getCurrentBluetoothService() : BluetoothChatService? {
63+
fun getCurrentBluetoothService(): BluetoothChatService? {
5464
return mChatService
5565
}
5666

5767
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+
}
5971
}
6072

6173
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()
6479
}
6580

81+
val bluetoothIntent = Intent(this, BluetoothChatService::class.java)
82+
stopService(bluetoothIntent)
6683
}
6784

68-
69-
7085
private fun createNotificationChannel() {
7186
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
7287
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 {
7692
description = getString(R.string.bt_notification_description)
7793
lockscreenVisibility = NotificationCompat.VISIBILITY_PRIVATE
7894
}
7995

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
8298
notificationManager.createNotificationChannels(listOf(bluetoothChannel))
8399
}
84100
}
85101

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+
86109
companion object {
87110
const val BLUETOOTH_SERVICE_CONNECTED = "BLUETOOTH_SERVICE_CONNECTED"
111+
88112
@JvmStatic
89113
var terminalList: ArrayList<String>? = null
90114
private set
115+
91116
@JvmStatic
92117
var tunnelList: ArrayList<String>? = null
93118
private set
119+
94120
@JvmStatic
95121
lateinit var commandList: ArrayList<String>
96122
private set
123+
97124
@JvmField
98125
var showLogDialog = true
126+
99127
@JvmField
100128
var ratingDialog = true
101-
lateinit var context:Context
102-
103-
var mChatService : BluetoothChatService? = null
129+
lateinit var context: Context
130+
var mChatService: BluetoothChatService? = null
104131
}
105132
}

0 commit comments

Comments
 (0)