Skip to content

Commit

Permalink
Add a setting to enable auto play videos on lovelace (#1792)
Browse files Browse the repository at this point in the history
* Add a setting to enable auto play videos on lovelace

* Flip condition
  • Loading branch information
dshokouhi committed Oct 20, 2021
1 parent 7f9e30d commit 5c9a1f5
Show file tree
Hide file tree
Showing 9 changed files with 47 additions and 1 deletion.
Expand Up @@ -43,6 +43,7 @@ class SettingsPresenterImpl @Inject constructor(
"app_lock" -> authenticationUseCase.isLockEnabled()
"crash_reporting" -> prefsRepository.isCrashReporting()
"prioritize_internal" -> urlUseCase.isPrioritizeInternal()
"autoplay_video" -> integrationUseCase.isAutoPlayVideoEnabled()
else -> throw IllegalArgumentException("No boolean found by this key: $key")
}
}
Expand All @@ -56,6 +57,7 @@ class SettingsPresenterImpl @Inject constructor(
"app_lock" -> authenticationUseCase.setLockEnabled(value)
"crash_reporting" -> prefsRepository.setCrashReporting(value)
"prioritize_internal" -> urlUseCase.setPrioritizeInternal(value)
"autoplay_video" -> integrationUseCase.setAutoPlayVideo(value)
else -> throw IllegalArgumentException("No boolean found by this key: $key")
}
}
Expand Down
Expand Up @@ -157,6 +157,7 @@ class WebViewActivity : BaseActivity(), io.homeassistant.companion.android.webvi
private var exoMute: Boolean = true
private var failedConnection = "external"
private var moreInfoEntity = ""
private var currentAutoplay: Boolean = false

@SuppressLint("SetJavaScriptEnabled")
override fun onCreate(savedInstanceState: Bundle?) {
Expand Down Expand Up @@ -217,6 +218,7 @@ class WebViewActivity : BaseActivity(), io.homeassistant.companion.android.webvi

settings.javaScriptEnabled = true
settings.domStorageEnabled = true
settings.mediaPlaybackRequiresUserGesture = !presenter.isAutoPlayVideoEnabled()
settings.userAgentString = USER_AGENT_STRING + " ${Build.MODEL} ${BuildConfig.VERSION_NAME}"
webViewClient = object : WebViewClient() {
override fun onReceivedError(
Expand Down Expand Up @@ -549,6 +551,7 @@ class WebViewActivity : BaseActivity(), io.homeassistant.companion.android.webvi
window.addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON)

currentLang = languagesManager.getCurrentLang()
currentAutoplay = presenter.isAutoPlayVideoEnabled()

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
val webviewPackage = WebViewCompat.getCurrentWebViewPackage(this)
Expand All @@ -571,7 +574,7 @@ class WebViewActivity : BaseActivity(), io.homeassistant.companion.android.webvi

override fun onResume() {
super.onResume()
if (currentLang != languagesManager.getCurrentLang())
if ((currentLang != languagesManager.getCurrentLang()) || currentAutoplay != presenter.isAutoPlayVideoEnabled())
recreate()
if ((!unlocked && !presenter.isLockEnabled()) ||
(!unlocked && presenter.isLockEnabled() && System.currentTimeMillis() < presenter.getSessionExpireMillis())
Expand Down
Expand Up @@ -17,6 +17,7 @@ interface WebViewPresenter {
fun isKeepScreenOnEnabled(): Boolean

fun isLockEnabled(): Boolean
fun isAutoPlayVideoEnabled(): Boolean

fun sessionTimeOut(): Int

Expand Down
Expand Up @@ -141,6 +141,12 @@ class WebViewPresenterImpl @Inject constructor(
}
}

override fun isAutoPlayVideoEnabled(): Boolean {
return runBlocking {
integrationUseCase.isAutoPlayVideoEnabled()
}
}

override fun sessionTimeOut(): Int {
return runBlocking {
integrationUseCase.getSessionTimeOut()
Expand Down
15 changes: 15 additions & 0 deletions app/src/main/res/drawable/ic_baseline_video_settings_24.xml
@@ -0,0 +1,15 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportWidth="24"
android:viewportHeight="24">
<path
android:fillColor="@color/colorAccent"
android:pathData="M3,6h18v5h2V6c0,-1.1 -0.9,-2 -2,-2H3C1.9,4 1,4.9 1,6v12c0,1.1 0.9,2 2,2h9v-2H3V6z"/>
<path
android:fillColor="@color/colorAccent"
android:pathData="M15,12l-6,-4l0,8z"/>
<path
android:fillColor="@color/colorAccent"
android:pathData="M22.71,18.43c0.03,-0.29 0.04,-0.58 0.01,-0.86l1.07,-0.85c0.1,-0.08 0.12,-0.21 0.06,-0.32l-1.03,-1.79c-0.06,-0.11 -0.19,-0.15 -0.31,-0.11L21.23,15c-0.23,-0.17 -0.48,-0.31 -0.75,-0.42l-0.2,-1.36C20.26,13.09 20.16,13 20.03,13h-2.07c-0.12,0 -0.23,0.09 -0.25,0.21l-0.2,1.36c-0.26,0.11 -0.51,0.26 -0.74,0.42l-1.28,-0.5c-0.12,-0.05 -0.25,0 -0.31,0.11l-1.03,1.79c-0.06,0.11 -0.04,0.24 0.06,0.32l1.07,0.86c-0.03,0.29 -0.04,0.58 -0.01,0.86l-1.07,0.85c-0.1,0.08 -0.12,0.21 -0.06,0.32l1.03,1.79c0.06,0.11 0.19,0.15 0.31,0.11l1.27,-0.5c0.23,0.17 0.48,0.31 0.75,0.42l0.2,1.36c0.02,0.12 0.12,0.21 0.25,0.21h2.07c0.12,0 0.23,-0.09 0.25,-0.21l0.2,-1.36c0.26,-0.11 0.51,-0.26 0.74,-0.42l1.28,0.5c0.12,0.05 0.25,0 0.31,-0.11l1.03,-1.79c0.06,-0.11 0.04,-0.24 -0.06,-0.32L22.71,18.43zM19,19.5c-0.83,0 -1.5,-0.67 -1.5,-1.5s0.67,-1.5 1.5,-1.5s1.5,0.67 1.5,1.5S19.83,19.5 19,19.5z"/>
</vector>
2 changes: 2 additions & 0 deletions app/src/main/res/values/strings.xml
Expand Up @@ -621,4 +621,6 @@ like to connect to:</string>
<string name="webview_error_HOST_LOOKUP">Server or proxy hostname lookup failed, please check that the URL is a valid Home Assistant URL.</string>
<string name="prioritize_internal">Prioritize Internal URL</string>
<string name="prioritize_internal_summary">Always try the internal URL first before the external URL. Enable this setting if you typically leave location off.</string>
<string name="autoplay_video">Autoplay Videos</string>
<string name="autoplay_video_summary">Autoplay Videos when lovelace dashboard is active. Enabling this setting may increase data usage unexpectedly, proceed with caution.</string>
</resources>
5 changes: 5 additions & 0 deletions app/src/main/res/xml/preferences.xml
Expand Up @@ -69,6 +69,11 @@
android:icon="@drawable/ic_phone_check"
android:title="@string/keep_screen_on"
android:summary="@string/keep_screen_on_def"/>
<SwitchPreference
android:key="autoplay_video"
android:icon="@drawable/ic_baseline_video_settings_24"
android:title="@string/autoplay_video"
android:summary="@string/autoplay_video_summary" />
<Preference
android:key="nfc_tags"
android:icon="@drawable/ic_nfc"
Expand Down
Expand Up @@ -23,6 +23,9 @@ interface IntegrationRepository {
suspend fun setKeepScreenOnEnabled(enabled: Boolean)
suspend fun isKeepScreenOnEnabled(): Boolean

suspend fun setAutoPlayVideo(enabled: Boolean)
suspend fun isAutoPlayVideoEnabled(): Boolean

suspend fun sessionTimeOut(value: Int)
suspend fun getSessionTimeOut(): Int

Expand Down
Expand Up @@ -52,6 +52,7 @@ class IntegrationRepositoryImpl @Inject constructor(

private const val PREF_SECRET = "secret"

private const val PREF_AUTOPLAY_VIDEO = "autoplay_video"
private const val PREF_FULLSCREEN_ENABLED = "fullscreen_enabled"
private const val PREF_KEEP_SCREEN_ON_ENABLED = "keep_screen_on_enabled"
private const val PREF_SESSION_TIMEOUT = "session_timeout"
Expand Down Expand Up @@ -312,6 +313,14 @@ class IntegrationRepositoryImpl @Inject constructor(
return localStorage.getBoolean(PREF_KEEP_SCREEN_ON_ENABLED)
}

override suspend fun isAutoPlayVideoEnabled(): Boolean {
return localStorage.getBoolean(PREF_AUTOPLAY_VIDEO)
}

override suspend fun setAutoPlayVideo(enabled: Boolean) {
localStorage.putBoolean(PREF_AUTOPLAY_VIDEO, enabled)
}

override suspend fun sessionTimeOut(value: Int) {
localStorage.putInt(PREF_SESSION_TIMEOUT, value)
}
Expand Down

0 comments on commit 5c9a1f5

Please sign in to comment.