Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ class SettingsPresenterImpl @Inject constructor(
return@runBlocking when (key) {
"fullscreen" -> integrationUseCase.isFullScreenEnabled()
"keep_screen_on" -> integrationUseCase.isKeepScreenOnEnabled()
"pinch_to_zoom" -> integrationUseCase.isPinchToZoomEnabled()
"app_lock" -> authenticationUseCase.isLockEnabled()
"crash_reporting" -> prefsRepository.isCrashReporting()
"prioritize_internal" -> urlUseCase.isPrioritizeInternal()
Expand All @@ -61,6 +62,7 @@ class SettingsPresenterImpl @Inject constructor(
when (key) {
"fullscreen" -> integrationUseCase.setFullScreenEnabled(value)
"keep_screen_on" -> integrationUseCase.setKeepScreenOnEnabled(value)
"pinch_to_zoom" -> integrationUseCase.setPinchToZoomEnabled(value)
"app_lock" -> authenticationUseCase.setLockEnabled(value)
"crash_reporting" -> prefsRepository.setCrashReporting(value)
"prioritize_internal" -> urlUseCase.setPrioritizeInternal(value)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,7 @@ class WebViewActivity : BaseActivity(), io.homeassistant.companion.android.webvi

settings.javaScriptEnabled = true
settings.domStorageEnabled = true
settings.displayZoomControls = false
settings.mediaPlaybackRequiresUserGesture = !presenter.isAutoPlayVideoEnabled()
settings.userAgentString = USER_AGENT_STRING + " ${Build.MODEL} ${BuildConfig.VERSION_NAME}"
webViewClient = object : WebViewClient() {
Expand All @@ -265,6 +266,7 @@ class WebViewActivity : BaseActivity(), io.homeassistant.companion.android.webvi
}

override fun onPageFinished(view: WebView?, url: String?) {
enablePinchToZoom()
if (moreInfoEntity != "" && view?.progress == 100 && isConnected) {
ioScope.launch {
val owner = "onPageFinished:$moreInfoEntity"
Expand Down Expand Up @@ -657,6 +659,8 @@ class WebViewActivity : BaseActivity(), io.homeassistant.companion.android.webvi
binding.blurView.setBlurEnabled(false)
}

enablePinchToZoom()

if (presenter.isKeepScreenOnEnabled())
window.addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON)
else
Expand Down Expand Up @@ -1311,4 +1315,33 @@ class WebViewActivity : BaseActivity(), io.homeassistant.companion.android.webvi

return super.dispatchKeyEvent(event)
}

private fun enablePinchToZoom() {
// Enable pinch to zoom
webView.getSettings().setBuiltInZoomControls(presenter.isPinchToZoomEnabled())
// Use idea from https://github.com/home-assistant/iOS/pull/1472 to filter viewport
val pinchToZoom = if (presenter.isPinchToZoomEnabled()) "true" else "false"
webView.evaluateJavascript(
"""
if (typeof viewport === 'undefined') {
var viewport = document.querySelector('meta[name="viewport"]');
if (viewport != null && typeof original_elements === 'undefined') {
var original_elements = viewport['content'];
}
}
if (viewport != null) {
if ($pinchToZoom) {
const ignoredBits = ['user-scalable', 'minimum-scale', 'maximum-scale'];
let elements = viewport['content'].split(',').filter(contentItem => {
return ignoredBits.every(ignoredBit => !contentItem.includes(ignoredBit));
});
elements.push('user-scalable=yes');
viewport['content'] = elements.join(',');
} else {
viewport['content'] = original_elements;
}
}
"""
) {}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ interface WebViewPresenter {

fun isKeepScreenOnEnabled(): Boolean

fun isPinchToZoomEnabled(): Boolean

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,12 @@ class WebViewPresenterImpl @Inject constructor(
}
}

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

override fun isLockEnabled(): Boolean {
return runBlocking {
authenticationUseCase.isLockEnabled()
Expand Down
8 changes: 8 additions & 0 deletions app/src/main/res/drawable/ic_gesture_pinch.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<!-- drawable/gesture_pinch.xml -->
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:height="24dp"
android:width="24dp"
android:viewportWidth="24"
android:viewportHeight="24">
<path android:fillColor="@color/colorAccent" android:pathData="M13,9A1,1 0 0,1 14,8A1,1 0 0,1 15,9V13.47L16.21,13.6L21.15,15.8C21.67,16.04 22,16.56 22,17.14V21.5C21.97,22.32 21.32,22.97 20.5,23H14C13.62,23 13.26,22.85 13,22.57L8.1,18.37L8.84,17.6C9.03,17.39 9.3,17.28 9.58,17.28H9.8L13,19V9M14,5C15.42,5 16.74,5.76 17.45,7C18.56,8.9 17.91,11.35 16,12.46V11.23C16.64,10.67 17,9.85 17,9A3,3 0 0,0 14,6A3,3 0 0,0 11,9C11,9.85 11.36,10.67 12,11.23V12.46C10.77,11.75 10,10.43 10,9A4,4 0 0,1 14,5M4,9L7,12H5V15H3V12H1L4,9M4,7L1,4H3V1H5V4H7L4,7M9,14C9.73,14 10.41,14.19 11,14.54V15.76C10.47,15.29 9.77,15 9,15A3,3 0 0,0 6,18C6,19 6.5,19.87 7.22,20.42L9.31,22H9A4,4 0 0,1 5,18A4,4 0 0,1 9,14Z" />
</vector>
5 changes: 5 additions & 0 deletions app/src/main/res/xml/preferences.xml
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,11 @@
android:icon="@drawable/ic_phone_check"
android:title="@string/keep_screen_on"
android:summary="@string/keep_screen_on_def"/>
<SwitchPreference
android:key="pinch_to_zoom"
android:icon="@drawable/ic_gesture_pinch"
android:title="@string/pinch_to_zoom"
android:summary="@string/pinch_to_zoom_def"/>
<SwitchPreference
android:key="autoplay_video"
android:icon="@drawable/ic_baseline_video_settings_24"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ interface IntegrationRepository {
suspend fun setKeepScreenOnEnabled(enabled: Boolean)
suspend fun isKeepScreenOnEnabled(): Boolean

suspend fun setPinchToZoomEnabled(enabled: Boolean)
suspend fun isPinchToZoomEnabled(): Boolean

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ class IntegrationRepositoryImpl @Inject constructor(
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_PINCH_TO_ZOOM_ENABLED = "pinch_to_zoom_enabled"
private const val PREF_SESSION_TIMEOUT = "session_timeout"
private const val PREF_SESSION_EXPIRE = "session_expire"
private const val PREF_SEC_WARNING_NEXT = "sec_warning_last"
Expand Down Expand Up @@ -340,6 +341,14 @@ class IntegrationRepositoryImpl @Inject constructor(
return localStorage.getBoolean(PREF_KEEP_SCREEN_ON_ENABLED)
}

override suspend fun setPinchToZoomEnabled(enabled: Boolean) {
localStorage.putBoolean(PREF_PINCH_TO_ZOOM_ENABLED, enabled)
}

override suspend fun isPinchToZoomEnabled(): Boolean {
return localStorage.getBoolean(PREF_PINCH_TO_ZOOM_ENABLED)
}

override suspend fun isAutoPlayVideoEnabled(): Boolean {
return localStorage.getBoolean(PREF_AUTOPLAY_VIDEO)
}
Expand Down
2 changes: 2 additions & 0 deletions common/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,8 @@
<string name="irreversible">This action is irreversible</string>
<string name="keep_screen_on_def">Do not lock screen when Lovelace dashboard is active</string>
<string name="keep_screen_on">Keep screen On</string>
<string name="pinch_to_zoom_def">Allow Pinch-to-zoom gesture to zoom app window</string>
<string name="pinch_to_zoom">Pinch To Zoom</string>
<string name="label_attribute">Attribute:</string>
<string name="label_dynamic_data">Data:</string>
<string name="label_entity_id">Entity ID:</string>
Expand Down