Skip to content

Commit

Permalink
Bug fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
dkrivoruchko committed Dec 11, 2022
1 parent 0de7353 commit a7f8a88
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -67,14 +67,18 @@ class NotificationHelper(context: Context) {
.putExtra(android.provider.Settings.EXTRA_APP_PACKAGE, applicationContext.packageName)

fun showForegroundNotification(service: Service, notificationType: NotificationType) {
XLog.d(getLog("showForegroundNotification", "Service:${service.hashCode()}, NotificationType: $notificationType"))
if (currentNotificationType != notificationType) {
val notification = getForegroundNotification(notificationType)
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
XLog.d(getLog("showForegroundNotification", "service.startForeground.Q: Service:${service.hashCode()}, NotificationType: $notificationType"))
service.startForeground(notificationType.id, notification, ServiceInfo.FOREGROUND_SERVICE_TYPE_MEDIA_PROJECTION)
} else {
XLog.d(getLog("showForegroundNotification", "service.startForeground: Service:${service.hashCode()}, NotificationType: $notificationType"))
service.startForeground(notificationType.id, notification)
}
currentNotificationType = notificationType
XLog.d(getLog("showForegroundNotification", "service.startForeground: Service:${service.hashCode()}, currentNotificationType: $currentNotificationType"))
}
}

Expand Down Expand Up @@ -135,11 +139,12 @@ class NotificationHelper(context: Context) {
)

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
val notificationChannel = notificationManager.getNotificationChannel(CHANNEL_ERROR)!!
builder
.setSound(notificationChannel.sound)
.setPriority(notificationChannel.importance)
.setVibrate(notificationChannel.vibrationPattern)
notificationManager.getNotificationChannel(CHANNEL_ERROR)?.let { notificationChannel ->
builder
.setSound(notificationChannel.sound)
.setPriority(notificationChannel.importance)
.setVibrate(notificationChannel.vibrationPattern)
}
}

notificationManager.notify(NotificationType.ERROR.id, builder.build())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -122,11 +122,11 @@ class SettingsAdvancedFragment : Fragment(R.layout.fragment_settings_advanced) {
maxLength = 5,
waitForPositiveButton = false
) { dialog, text ->
val isValid = text.length in 4..5 && text.toString().toInt() in 1025..65535
val isValid = text.length in 4..5 && (text.toString().toIntOrNull() ?: -1) in 1025..65535
dialog.setActionButtonEnabled(WhichButton.POSITIVE, isValid)
}
positiveButton(android.R.string.ok) { dialog ->
val newValue = dialog.getInputField().text?.toString()?.toInt() ?: serverPort
val newValue = dialog.getInputField().text?.toString()?.toIntOrNull() ?: serverPort
if (serverPort != newValue)
viewLifecycleOwner.lifecycleScope.launchWhenCreated { mjpegSettings.setServerPort(newValue) }
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,18 +109,17 @@ class SettingsImageFragment : Fragment(R.layout.fragment_settings_image) {
.positiveButton(android.R.string.ok) { dialog ->
DialogSettingsCropBinding.bind(dialog.getCustomView()).apply {
viewLifecycleOwner.lifecycleScope.launchWhenCreated {
val newTopCrop = tietDialogSettingsCropTop.text.toString().toInt()
val newBottomCrop = tietDialogSettingsCropBottom.text.toString().toInt()
val newLeftCrop = tietDialogSettingsCropLeft.text.toString().toInt()
val newRightCrop = tietDialogSettingsCropRight.text.toString().toInt()
val newTopCrop = tietDialogSettingsCropTop.text.toString().toIntOrNull() ?: topCrop
val newBottomCrop = tietDialogSettingsCropBottom.text.toString().toIntOrNull() ?: bottomCrop
val newLeftCrop = tietDialogSettingsCropLeft.text.toString().toIntOrNull() ?: leftCrop
val newRightCrop = tietDialogSettingsCropRight.text.toString().toIntOrNull() ?: rightCrop

if (newTopCrop != topCrop) mjpegSettings.setImageCropTop(newTopCrop)
if (newBottomCrop != bottomCrop) mjpegSettings.setImageCropBottom(newBottomCrop)
if (newLeftCrop != leftCrop) mjpegSettings.setImageCropLeft(newLeftCrop)
if (newRightCrop != rightCrop) mjpegSettings.setImageCropRight(newRightCrop)

val newImageCrop =
newTopCrop + newBottomCrop + newLeftCrop + newRightCrop != 0
val newImageCrop = newTopCrop + newBottomCrop + newLeftCrop + newRightCrop != 0
binding.cbFragmentSettingsCropImage.isChecked = newImageCrop
mjpegSettings.setImageCrop(newImageCrop)
}
Expand Down Expand Up @@ -194,9 +193,7 @@ class SettingsImageFragment : Fragment(R.layout.fragment_settings_image) {
DialogSettingsResizeBinding.bind(dialog.getCustomView()).apply {
val newResizeFactor = tietDialogSettingsResize.text.toString().toInt()
if (newResizeFactor != resizeFactor)
viewLifecycleOwner.lifecycleScope.launchWhenCreated {
mjpegSettings.setResizeFactor(newResizeFactor)
}
viewLifecycleOwner.lifecycleScope.launchWhenCreated { mjpegSettings.setResizeFactor(newResizeFactor) }
}
}
.negativeButton(android.R.string.cancel)
Expand All @@ -212,8 +209,7 @@ class SettingsImageFragment : Fragment(R.layout.fragment_settings_image) {
addTextChangedListener(SimpleTextWatcher { text ->
val isValid = text.length in 2..3 && (text.toString().toIntOrNull() ?: -1) in 10..150
this@Dialog.setActionButtonEnabled(WhichButton.POSITIVE, isValid)
val newResizeFactor =
(if (isValid) text.toString().toInt() else resizeFactor) / 100f
val newResizeFactor = (if (isValid) text.toString().toInt() else resizeFactor) / 100f

tvDialogSettingsResizeResult.text = resizePictureSizeString.format(
(screenBounds.width() * newResizeFactor).toInt(),
Expand Down Expand Up @@ -284,11 +280,11 @@ class SettingsImageFragment : Fragment(R.layout.fragment_settings_image) {
maxLength = 2,
waitForPositiveButton = false
) { dialog, text ->
val isValid = text.length in 1..2 && text.toString().toInt() in 1..60
val isValid = text.length in 1..2 && (text.toString().toIntOrNull() ?: -1) in 1..60
dialog.setActionButtonEnabled(WhichButton.POSITIVE, isValid)
}
positiveButton(android.R.string.ok) { dialog ->
val newValue = dialog.getInputField().text?.toString()?.toInt() ?: maxFPS
val newValue = dialog.getInputField().text?.toString()?.toIntOrNull() ?: maxFPS
if (maxFPS != newValue)
viewLifecycleOwner.lifecycleScope.launchWhenCreated { mjpegSettings.setMaxFPS(newValue) }
}
Expand Down Expand Up @@ -320,11 +316,11 @@ class SettingsImageFragment : Fragment(R.layout.fragment_settings_image) {
maxLength = 3,
waitForPositiveButton = false
) { dialog, text ->
val isValid = text.length in 2..3 && text.toString().toInt() in 10..100
val isValid = text.length in 2..3 && (text.toString().toIntOrNull() ?: -1) in 10..100
dialog.setActionButtonEnabled(WhichButton.POSITIVE, isValid)
}
positiveButton(android.R.string.ok) { dialog ->
val newValue = dialog.getInputField().text?.toString()?.toInt() ?: jpegQuality
val newValue = dialog.getInputField().text?.toString()?.toIntOrNull() ?: jpegQuality
if (jpegQuality != newValue)
viewLifecycleOwner.lifecycleScope.launchWhenCreated { mjpegSettings.setJpegQuality(newValue) }
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,7 @@ class MjpegStateMachine(

private var slowClients: List<MjpegClient> = emptyList()

@Synchronized
private fun checkForSlowClients(clients: List<MjpegClient>) {
val currentSlowConnections = clients.filter { it.isSlowConnection }.toList()
if (slowClients.containsAll(currentSlowConnections).not()) onSlowConnectionDetected()
Expand All @@ -248,10 +249,16 @@ class MjpegStateMachine(
}
}

private fun releaseWakeLock() {
synchronized(this) {
if (wakeLock?.isHeld == true) wakeLock?.release()
wakeLock = null
}
}

override fun destroy() {
XLog.d(getLog("destroy"))
wakeLock?.release()
wakeLock = null
releaseWakeLock()

sendEvent(InternalEvent.Destroy)
try {
Expand All @@ -268,8 +275,7 @@ class MjpegStateMachine(

private fun onError(appError: AppError) {
XLog.e(getLog("onError", "AppError: $appError"))
wakeLock?.release()
wakeLock = null
releaseWakeLock()
sendEvent(InternalEvent.ComponentError(appError))
}

Expand All @@ -281,8 +287,7 @@ class MjpegStateMachine(
streamState.mediaProjection?.stop()
}

wakeLock?.release()
wakeLock = null
releaseWakeLock()

return streamState.copy(mediaProjection = null, bitmapCapture = null)
}
Expand Down Expand Up @@ -359,11 +364,14 @@ class MjpegStateMachine(
bitmapCapture.start()

if (appSettings.keepAwakeFlow.first()) {
@Suppress("DEPRECATION")
@SuppressLint("WakelockTimeout")
wakeLock = powerManager.newWakeLock(
PowerManager.SCREEN_DIM_WAKE_LOCK or PowerManager.ACQUIRE_CAUSES_WAKEUP, "ScreenStream::StreamingTag"
).apply { acquire() }
synchronized(this) {
@Suppress("DEPRECATION")
@SuppressLint("WakelockTimeout")
wakeLock = powerManager.newWakeLock(
PowerManager.SCREEN_DIM_WAKE_LOCK or PowerManager.ACQUIRE_CAUSES_WAKEUP, "ScreenStream::StreamingTag"
)
.apply { acquire() }
}
}

return streamState.copy(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ class NetworkHelper(context: Context) {
if (netList.isNotEmpty()) return Collections.enumeration(netList)
return@forEach
}
} catch (ex: SocketException) {
} catch (ex: Exception) {
XLog.e(getLog("getNetworkInterfacesWithFallBack.getByIndex#$index:", ex.toString()))
}
}
Expand All @@ -63,7 +63,7 @@ class NetworkHelper(context: Context) {
if (networkInterfaceIndexed != null) netList.add(networkInterfaceIndexed)
}

} catch (ex: SocketException) {
} catch (ex: Exception) {
XLog.e(getLog("getNetworkInterfacesWithFallBack.commonName#$commonName:", ex.toString()))
}
}
Expand Down

0 comments on commit a7f8a88

Please sign in to comment.