Check controls while locked setting when using panel (#4047)

- When using the device controls panel instead of individual/native controls on Android 14, also check the setting for using controls while the device is locked and if so block the panel (as the frontend doesn't have a read only mode).
This commit is contained in:
Joris Pelgröm 2023-12-13 20:47:55 +01:00 committed by GitHub
parent 5d779a27aa
commit df902803ab
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -1,15 +1,36 @@
package io.homeassistant.companion.android.controls
import android.annotation.SuppressLint
import android.app.KeyguardManager
import android.os.Bundle
import android.service.controls.ControlsProviderService
import android.util.Log
import androidx.activity.compose.setContent
import androidx.appcompat.app.AppCompatActivity
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.padding
import androidx.compose.material.Icon
import androidx.compose.material.MaterialTheme
import androidx.compose.material.Text
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.Lock
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.unit.dp
import androidx.core.content.getSystemService
import androidx.lifecycle.lifecycleScope
import com.google.accompanist.themeadapter.material.MdcTheme
import dagger.hilt.android.AndroidEntryPoint
import io.homeassistant.companion.android.common.data.prefs.PrefsRepository
import io.homeassistant.companion.android.common.data.servers.ServerManager
import io.homeassistant.companion.android.webview.WebViewActivity
import kotlinx.coroutines.launch
import javax.inject.Inject
import io.homeassistant.companion.android.common.R as commonR
@AndroidEntryPoint
class HaControlsPanelActivity : AppCompatActivity() {
@ -22,13 +43,25 @@ class HaControlsPanelActivity : AppCompatActivity() {
private var launched = false
@SuppressLint("InlinedApi") // This activity will only be launched on Android 14+
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setShowWhenLocked(true)
if (!serverManager.isRegistered()) {
finish()
return
}
val disallowLocked =
intent?.hasExtra(ControlsProviderService.EXTRA_LOCKSCREEN_ALLOW_TRIVIAL_CONTROLS) != true ||
intent?.getBooleanExtra(ControlsProviderService.EXTRA_LOCKSCREEN_ALLOW_TRIVIAL_CONTROLS, false) != true
val keyguardManager = getSystemService<KeyguardManager>()
val isLocked = keyguardManager?.isKeyguardLocked ?: true
if (disallowLocked && isLocked) {
setContent { LockedPanelView() }
return
}
lifecycleScope.launch {
val serverId = prefsRepository.getControlsPanelServer() ?: serverManager.getServer()?.id
val path = prefsRepository.getControlsPanelPath()
@ -54,4 +87,25 @@ class HaControlsPanelActivity : AppCompatActivity() {
super.onPause()
if (launched) finish()
}
@Composable
fun LockedPanelView() {
MdcTheme {
Column(
modifier = Modifier.fillMaxSize(),
horizontalAlignment = Alignment.CenterHorizontally,
verticalArrangement = Arrangement.Center
) {
Icon(
imageVector = Icons.Default.Lock,
contentDescription = null
)
Text(
text = stringResource(commonR.string.tile_auth_required),
style = MaterialTheme.typography.h6,
modifier = Modifier.padding(top = 16.dp)
)
}
}
}
}