Skip to content

Commit

Permalink
all: smoother foreground services (fixes #2059) (#2060)
Browse files Browse the repository at this point in the history
Co-authored-by: dogi <[email protected]>
  • Loading branch information
Okuro3499 and dogi authored Jul 9, 2024
1 parent d046600 commit 5bcc47e
Show file tree
Hide file tree
Showing 6 changed files with 131 additions and 171 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.6.1', {
exclude group: 'com.android.support', module: 'support-annotations'
})
Expand Down
69 changes: 50 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,21 @@ 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
// private var bluetoothService: BluetoothChatService? = null

override fun onCreate() {
super.onCreate()
Expand All @@ -27,79 +34,103 @@ 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? {
if (mChatService == null) {
mChatService = 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
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -77,14 +77,16 @@ open class BaseBluetoothChatService @JvmOverloads constructor(handler: Handler?
}

protected fun startNotification() {
val disconnectIntent = Intent(DISCONNECT_ACTION)
val disconnectPendingIntent: PendingIntent = PendingIntent.getBroadcast(this, 0, disconnectIntent, FLAG_IMMUTABLE)
context?.let {
val disconnectIntent = Intent(DISCONNECT_ACTION)
val disconnectPendingIntent = PendingIntent.getBroadcast(this, 0, disconnectIntent, FLAG_IMMUTABLE)

val onClickIntent = Intent(this, InitialActivity::class.java)
val pendingClickIntent = PendingIntent.getActivity(this, 0, onClickIntent, FLAG_IMMUTABLE)
val onClickIntent = Intent(this, InitialActivity::class.java)
val pendingClickIntent = PendingIntent.getActivity(this, 0, onClickIntent, FLAG_IMMUTABLE)

val notificationBuilder: NotificationCompat.Builder = NotificationCompat.Builder(this, getString(R.string.bt_notification_ID))
val notification: Notification = notificationBuilder.setOngoing(true)
val notificationBuilder: NotificationCompat.Builder =
NotificationCompat.Builder(this, getString(R.string.bt_notification_ID))
val notification: Notification = notificationBuilder.setOngoing(true)
.setContentTitle("Treehouses Remote is currently running")
.setContentText("Connected to ${mDevice?.name}")
.setPriority(NotificationCompat.PRIORITY_HIGH)
Expand All @@ -93,14 +95,15 @@ open class BaseBluetoothChatService @JvmOverloads constructor(handler: Handler?
.setContentIntent(pendingClickIntent)
.addAction(R.drawable.bluetooth, "Disconnect", disconnectPendingIntent)
.build()
ServiceCompat.startForeground(this,2,
notification,
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
ServiceInfo.FOREGROUND_SERVICE_TYPE_CONNECTED_DEVICE
} else {
0
}
)
ServiceCompat.startForeground(this, 2,
notification,
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
ServiceInfo.FOREGROUND_SERVICE_TYPE_CONNECTED_DEVICE
} else {
0
}
)
}
}

/**
Expand Down
Loading

0 comments on commit 5bcc47e

Please sign in to comment.