Skip to content

Commit

Permalink
Screensaver: Ensure text is always full visible
Browse files Browse the repository at this point in the history
Signed-off-by: mueller-ma <[email protected]>
  • Loading branch information
mueller-ma committed Jul 18, 2024
1 parent 753c895 commit 721c8cc
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 6 deletions.
1 change: 1 addition & 0 deletions mobile/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -334,6 +334,7 @@
android:exported="true"
android:label="@string/app_name"
android:icon="@drawable/ic_openhab_appicon_24dp"
android:configChanges="orientation|screenSize"
android:permission="android.permission.BIND_DREAM_SERVICE">
<intent-filter>
<action android:name="android.service.dreams.DreamService" />
Expand Down
42 changes: 36 additions & 6 deletions mobile/src/main/java/org/openhab/habdroid/ui/DayDream.kt
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@
package org.openhab.habdroid.ui

import android.animation.ObjectAnimator
import android.content.res.Configuration
import android.graphics.Rect
import android.os.Handler
import android.os.Looper
import android.service.dreams.DreamService
import android.util.Log
import android.view.View
Expand Down Expand Up @@ -41,6 +45,7 @@ import org.openhab.habdroid.util.getStringOrNull
class DayDream : DreamService(), CoroutineScope {
private val job = Job()
override val coroutineContext: CoroutineContext get() = Dispatchers.Main + job
private var moveTextJob: Job? = null
private lateinit var textView: TextView
private lateinit var wrapper: LinearLayout
private lateinit var container: FrameLayout
Expand All @@ -64,34 +69,50 @@ class DayDream : DreamService(), CoroutineScope {
launch {
item?.let { listenForTextItem(it) }
}
launch {
moveText()
}
}

override fun onConfigurationChanged(newConfig: Configuration) {
super.onConfigurationChanged(newConfig)
moveTextIfRequired()
}

private suspend fun listenForTextItem(item: String) {
ConnectionFactory.waitForInitialization()
val connection = ConnectionFactory.primaryUsableConnection?.connection ?: return

moveText()
textView.text = try {
ItemClient.loadItem(connection, item)?.state?.asString.orEmpty()
} catch (e: HttpClient.HttpException) {
getString(R.string.screensaver_error_loading_item, item)
}
moveTextIfRequired()

ItemClient.listenForItemChange(this, connection, item) { _, payload ->
val state = payload.getString("value")
Log.d(TAG, "Got state by event: $state")
textView.text = state
moveTextIfRequired()
}
}

private suspend fun moveText() {
private fun moveText() {
moveTextJob?.cancel()
wrapper.fadeOut()
wrapper.moveViewToRandomPosition(container)
wrapper.fadeIn()
delay(if (BuildConfig.DEBUG) 10.seconds else 1.minutes)
moveText()
moveTextJob = launch {
delay(if (BuildConfig.DEBUG) 10.seconds else 1.minutes)
moveText()
}
}

private fun moveTextIfRequired() {
Handler(Looper.getMainLooper()).post {
if (!textView.isFullyVisible()) {
moveText()
}
}
}

private fun View.moveViewToRandomPosition(container: FrameLayout) {
Expand Down Expand Up @@ -122,6 +143,15 @@ class DayDream : DreamService(), CoroutineScope {
animator.start()
}

private fun View.isFullyVisible(): Boolean {
val rect = Rect()
val isVisible = this.getGlobalVisibleRect(rect)
val viewHeight = this.height
val viewWidth = this.width

return isVisible && rect.height() == viewHeight && rect.width() == viewWidth
}

override fun onDetachedFromWindow() {
super.onDetachedFromWindow()
job.cancel()
Expand Down

0 comments on commit 721c8cc

Please sign in to comment.