Skip to content

Commit

Permalink
Bug fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
dkrivoruchko committed Sep 6, 2022
1 parent e3ee332 commit 2fdb8cf
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 26 deletions.
14 changes: 7 additions & 7 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ android {
applicationId = "info.dvkr.screenstream"
minSdkVersion(21)
targetSdkVersion(33)
versionCode = 31000
versionName = "3.10.0"
versionCode = 31001
versionName = "3.10.1"

// Don't forget to update xml/locales_config.xml
resConfigs 'en', 'ar', 'ca', 'de', 'es', 'fa', 'fr-rFR', 'gl', 'hi', 'it', 'ja', 'nl', 'pl', 'pt-rBR', 'ru', 'sk', 'uk', 'zh', 'zh-rTW'
Expand Down Expand Up @@ -98,13 +98,13 @@ dependencies {
implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:1.6.4")
implementation("org.jetbrains.kotlinx:kotlinx-coroutines-android:1.6.4")

implementation("androidx.core:core-ktx:1.9.0-beta01")
implementation("androidx.activity:activity-ktx:1.6.0-beta01")
implementation("androidx.core:core-ktx:1.9.0-rc01")
implementation("androidx.activity:activity-ktx:1.6.0-rc01")
implementation("androidx.fragment:fragment-ktx:1.5.2")
implementation("androidx.appcompat:appcompat:1.6.0-beta01")
implementation("androidx.constraintlayout:constraintlayout:2.1.4")
implementation("androidx.recyclerview:recyclerview:1.2.1")
implementation("com.google.android.material:material:1.7.0-beta01")
implementation("com.google.android.material:material:1.7.0-rc01")
implementation("androidx.window:window:1.0.0")

implementation("androidx.navigation:navigation-fragment-ktx:2.5.1")
Expand All @@ -128,8 +128,8 @@ dependencies {
firebaseImplementation("com.google.android.play:app-update-ktx:2.0.0") {
exclude group: "com.android.support"
}
firebaseImplementation("com.google.firebase:firebase-analytics:21.1.0")
firebaseImplementation("com.google.firebase:firebase-crashlytics:18.2.12")
firebaseImplementation("com.google.firebase:firebase-analytics:21.1.1")
firebaseImplementation("com.google.firebase:firebase-crashlytics:18.2.13")
firebaseImplementation("com.google.android.gms:play-services-ads:21.1.0")

// debugImplementation("com.squareup.leakcanary:leakcanary-android:2.9.1")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,22 @@ class ForegroundService : Service() {
@Volatile
var isRunning: Boolean = false

@JvmStatic
fun getForegroundServiceIntent(context: Context): Intent = Intent(context, ForegroundService::class.java)

@JvmStatic
fun startForeground(context: Context, intent: Intent) {
XLog.e(getLog("startForeground"))
XLog.d(getLog("startForeground", intent.extras?.toString()))
runCatching { ContextCompat.startForegroundService(context, intent) }
.onFailure { XLog.e(getLog("startForeground", "Failed to start Foreground Service"), it) }
}

@JvmStatic
fun startService(context: Context, intent: Intent) {
XLog.d(getLog("startService", intent.extras?.toString()))
runCatching { context.startService(intent) }
.onFailure { XLog.e(getLog("startService", "Failed to start Service"), it) }
}
}

internal object ForegroundServiceBinder : Binder() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import android.content.ServiceConnection
import android.graphics.drawable.Icon
import android.os.Build
import android.os.IBinder
import android.os.RemoteException
import android.service.quicksettings.Tile
import android.service.quicksettings.TileService
import androidx.annotation.RequiresApi
Expand Down Expand Up @@ -53,24 +54,35 @@ class TileActionService : TileService() {
serviceConnection = object : ServiceConnection {
override fun onServiceConnected(name: ComponentName?, binder: IBinder) {
XLog.d(this@TileActionService.getLog("onServiceConnected"))
coroutineScope?.cancel()
coroutineScope = CoroutineScope(Job() + Dispatchers.Main.immediate).apply {
launch(CoroutineName("TileActionService.ServiceMessageFlow")) {
(binder as ForegroundService.ForegroundServiceBinder).serviceMessageFlow.onEach { serviceMessage ->
XLog.d(this@TileActionService.getLog("onServiceMessage", "$serviceMessage"))
when (serviceMessage) {
is ServiceMessage.ServiceState -> {
isStreaming = serviceMessage.isStreaming; updateTile()
}
is ServiceMessage.FinishActivity -> {
isStreaming = false; updateTile()

try {
val foregroundServiceBinder = binder as ForegroundService.ForegroundServiceBinder

coroutineScope?.cancel()
coroutineScope = CoroutineScope(Job() + Dispatchers.Main.immediate)
coroutineScope!!.launch {
foregroundServiceBinder.serviceMessageFlow
.onEach { serviceMessage ->
XLog.d(this@TileActionService.getLog("onServiceMessage", "$serviceMessage"))
when (serviceMessage) {
is ServiceMessage.ServiceState -> {
isStreaming = serviceMessage.isStreaming; updateTile()
}
is ServiceMessage.FinishActivity -> {
isStreaming = false; updateTile()
}
else -> Unit
}
else -> Unit
}
}
.catch { cause -> XLog.e(this@TileActionService.getLog("onServiceMessage"), cause) }
.catch { cause ->
XLog.e(this@TileActionService.getLog("onServiceConnected.serviceMessageFlow: $cause"))
XLog.e(this@TileActionService.getLog("onServiceConnected.serviceMessageFlow"), cause)
}
.collect()
}
} catch (cause: RemoteException) {
XLog.e(this@TileActionService.getLog("onServiceConnected", "Failed to bind"), cause)
return
}
isBound = true
IntentAction.GetServiceState.sendToAppService(this@TileActionService)
Expand All @@ -83,7 +95,8 @@ class TileActionService : TileService() {
isBound = false
}
}
serviceConnection?.let { bindService(ForegroundService.getForegroundServiceIntent(this), it, Context.BIND_AUTO_CREATE) }

bindService(ForegroundService.getForegroundServiceIntent(this), serviceConnection!!, Context.BIND_AUTO_CREATE)
} else {
isStreaming = false
updateTile()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,10 @@ internal sealed class IntentAction : Parcelable {
AppActivity.getAppActivityIntent(context).putExtra(EXTRA_PARCELABLE, this@IntentAction)

internal fun sendToAppService(context: Context) =
ForegroundService.startForeground(context, this.toAppServiceIntent(context))
if (ForegroundService.isRunning)
ForegroundService.startService(context, this.toAppServiceIntent(context))
else
ForegroundService.startForeground(context, this.toAppServiceIntent(context))

@Parcelize object GetServiceState : IntentAction()
@Parcelize object StartStream : IntentAction()
Expand Down
2 changes: 1 addition & 1 deletion data/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ dependencies {
implementation("org.jetbrains.kotlin:kotlin-reflect:1.7.10")

//noinspection KtxExtensionAvailable
implementation("androidx.core:core:1.9.0-beta01")
implementation("androidx.core:core:1.9.0-rc01")
implementation("androidx.window:window:1.0.0")

implementation("io.ktor:ktor-server-cio:2.1.0")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,7 @@ class AppStateMachineImpl(
try {
runBlocking(coroutineScope.coroutineContext) { withTimeout(1000) { httpServer.destroy().await() } }
} catch (cause: Throwable) {
XLog.e(getLog("destroy"), cause)
XLog.e(getLog("destroy", cause.toString()))
}
broadcastHelper.stopListening()
connectivityHelper.stopListening()
Expand Down

0 comments on commit 2fdb8cf

Please sign in to comment.