Skip to content

Commit

Permalink
close foreground on app teminating
Browse files Browse the repository at this point in the history
  • Loading branch information
Okuro3499 committed Jun 19, 2024
1 parent f35b94b commit 70cc170
Show file tree
Hide file tree
Showing 5 changed files with 109 additions and 157 deletions.
1 change: 1 addition & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ android.defaultConfig.vectorDrawables.useSupportLibrary = true
dependencies {
implementation fileTree(include: ['*.jar'], dir: 'libs')
implementation 'androidx.swiperefreshlayout:swiperefreshlayout:1.1.0'
implementation 'androidx.lifecycle:lifecycle-process:2.8.2'
androidTestImplementation('androidx.test.espresso:espresso-core:3.5.1', {
exclude group: 'com.android.support', module: 'support-annotations'
})
Expand Down
65 changes: 46 additions & 19 deletions app/src/main/kotlin/io/treehouses/remote/MainApplication.kt
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,20 @@ import android.content.Intent
import android.content.ServiceConnection
import android.os.Build
import android.os.IBinder
import android.util.Log
import androidx.core.app.NotificationCompat
import androidx.preference.PreferenceManager
import androidx.lifecycle.ProcessLifecycleOwner
import com.parse.Parse
import io.treehouses.remote.network.BluetoothChatService
import io.treehouses.remote.utils.AppLifecycleObserver
import io.treehouses.remote.utils.AppLifecycleTracker
import io.treehouses.remote.utils.GPSService
import io.treehouses.remote.utils.SaveUtils

class MainApplication : Application() {
var logSent = false
private lateinit var appLifecycleObserver: AppLifecycleObserver
private lateinit var activityLifecycleTracker: AppLifecycleTracker

override fun onCreate() {
super.onCreate()
Expand All @@ -27,79 +33,100 @@ class MainApplication : Application() {
terminalList = ArrayList()
tunnelList = ArrayList()
commandList = ArrayList()
Parse.initialize(Parse.Configuration.Builder(this)
Parse.initialize(
Parse.Configuration.Builder(this)
.applicationId(Constants.PARSE_APPLICATION_ID)
.clientKey(null)
.server(Constants.PARSE_URL)
.build()
)
SaveUtils.initCommandsList(applicationContext)

appLifecycleObserver = AppLifecycleObserver()
ProcessLifecycleOwner.get().lifecycle.addObserver(appLifecycleObserver)

activityLifecycleTracker = AppLifecycleTracker()
registerActivityLifecycleCallbacks(activityLifecycleTracker)
}

private val connection = object : ServiceConnection {

override fun onServiceConnected(className: ComponentName, service: IBinder) {
// We've bound to LocalService, cast the IBinder and get LocalService instance
val binder = service as BluetoothChatService.LocalBinder
mChatService = binder.service
// sendBroadcast(Intent().setAction(BLUETOOTH_SERVICE_CONNECTED))
}

override fun onServiceDisconnected(arg0: ComponentName) {
mChatService = null
}
}

fun getCurrentBluetoothService() : BluetoothChatService? {
fun getCurrentBluetoothService(): BluetoothChatService? {
return mChatService
}

fun startBluetoothService() {
Intent(this, BluetoothChatService::class.java).also { intent -> bindService(intent, connection, Context.BIND_AUTO_CREATE) }
Intent(this, BluetoothChatService::class.java).also { intent ->
bindService(intent, connection, Context.BIND_AUTO_CREATE)
}
}

fun stopBluetoothService() {
if (!PreferenceManager.getDefaultSharedPreferences(this).getBoolean(Constants.KEEP_BLUETOOTH_ALIVE, false)) {
try {unbindService(connection)} catch (e: Exception) {}
try {
unbindService(connection)
mChatService = null
} catch (e: Exception) {
e.printStackTrace()
}

val bluetoothIntent = Intent(this, BluetoothChatService::class.java)
stopService(bluetoothIntent)
}



private fun createNotificationChannel() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
val bluetoothChannel = NotificationChannel(
getString(R.string.bt_notification_ID),
getString(R.string.bt_notification_channel),
NotificationManager.IMPORTANCE_HIGH).apply {
getString(R.string.bt_notification_ID),
getString(R.string.bt_notification_channel),
NotificationManager.IMPORTANCE_HIGH
).apply {
description = getString(R.string.bt_notification_description)
lockscreenVisibility = NotificationCompat.VISIBILITY_PRIVATE
}

// Register the channel with the system
val notificationManager: NotificationManager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
val notificationManager: NotificationManager =
getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
notificationManager.createNotificationChannels(listOf(bluetoothChannel))
}
}

fun stopAllServices() {
stopBluetoothService()
val gpsIntent = Intent(this, GPSService::class.java)
stopService(gpsIntent)
Log.d("MainApplication", "All services stopped")
}

companion object {
const val BLUETOOTH_SERVICE_CONNECTED = "BLUETOOTH_SERVICE_CONNECTED"

@JvmStatic
var terminalList: ArrayList<String>? = null
private set

@JvmStatic
var tunnelList: ArrayList<String>? = null
private set

@JvmStatic
lateinit var commandList: ArrayList<String>
private set

@JvmField
var showLogDialog = true

@JvmField
var ratingDialog = true
lateinit var context:Context

var mChatService : BluetoothChatService? = null
lateinit var context: Context
var mChatService: BluetoothChatService? = null
}
}
Loading

0 comments on commit 70cc170

Please sign in to comment.