Skip to content

Commit

Permalink
check if open as default is enabled
Browse files Browse the repository at this point in the history
  • Loading branch information
DatL4g committed Mar 18, 2024
1 parent 33d9852 commit 6509d91
Show file tree
Hide file tree
Showing 5 changed files with 119 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import dev.datlag.burningseries.model.common.safeCast
import dev.datlag.burningseries.shared.App
import dev.datlag.burningseries.shared.SharedRes
import dev.datlag.burningseries.shared.common.lifecycle.LocalLifecycleOwner
import dev.datlag.burningseries.shared.other.DomainVerifier
import dev.datlag.burningseries.shared.ui.*
import dev.datlag.burningseries.shared.ui.custom.Permission
import dev.datlag.burningseries.shared.ui.navigation.NavHostComponent
Expand Down Expand Up @@ -63,6 +64,7 @@ class MainActivity : AppCompatActivity() {

Kast.setup(this)
SmallIcon = R.drawable.ic_launcher_foreground
DomainVerifier.verify(this)

setContent {
CompositionLocalProvider(
Expand Down Expand Up @@ -119,6 +121,18 @@ class MainActivity : AppCompatActivity() {
Kast.dispose()
}

override fun onStart() {
super.onStart()

DomainVerifier.verify(this)
}

override fun onResume() {
super.onResume()

DomainVerifier.verify(this)
}

override fun dispatchKeyEvent(event: KeyEvent?): Boolean {
return (KeyEventDispatcher.invoke(event) ?: false) || super.dispatchKeyEvent(event)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package dev.datlag.burningseries.shared.other

import android.content.Context
import android.content.Intent
import android.content.pm.verify.domain.DomainVerificationManager
import android.content.pm.verify.domain.DomainVerificationUserState
import android.net.Uri
import android.os.Build
import android.provider.Settings
import androidx.annotation.ChecksSdkIntAtLeast
import androidx.core.content.ContextCompat
import kotlinx.coroutines.flow.MutableStateFlow

data object DomainVerifier {

@ChecksSdkIntAtLeast(api = Build.VERSION_CODES.S)
val supported: Boolean = Build.VERSION.SDK_INT >= Build.VERSION_CODES.S
val verified: MutableStateFlow<Boolean> = MutableStateFlow(false)

@Suppress("NewApi")
fun verify(context: Context) {
if (supported) {
val manager = ContextCompat.getSystemService(context, DomainVerificationManager::class.java)
val userState = manager?.getDomainVerificationUserState(context.packageName)
val unapprovedDomains = userState?.hostToStateMap?.filterValues { it == DomainVerificationUserState.DOMAIN_STATE_NONE }

unapprovedDomains?.let { count ->
verified.compareAndSet(false, count.isEmpty())
}
}
}

@Suppress("NewApi")
fun enable(context: Context) {
if (supported) {
val intent = Intent(
Settings.ACTION_APP_OPEN_BY_DEFAULT_SETTINGS,
Uri.parse("package:${context.packageName}")
)
ContextCompat.startActivity(context, intent, null)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,24 @@ import androidx.compose.foundation.Image
import androidx.compose.foundation.layout.*
import androidx.compose.foundation.lazy.grid.LazyGridScope
import androidx.compose.material3.*
import androidx.compose.runtime.SideEffect
import androidx.compose.runtime.getValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.ColorFilter
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.text.SpanStyle
import androidx.compose.ui.text.buildAnnotatedString
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.text.withStyle
import androidx.compose.ui.unit.dp
import dev.datlag.burningseries.model.Release
import dev.datlag.burningseries.shared.SharedRes
import dev.datlag.burningseries.shared.common.header
import dev.datlag.burningseries.shared.common.lifecycle.collectAsStateWithLifecycle
import dev.datlag.burningseries.shared.common.openInBrowser
import dev.datlag.burningseries.shared.other.DomainVerifier
import dev.datlag.burningseries.shared.rememberIsTv
import dev.icerock.moko.resources.compose.painterResource
import dev.icerock.moko.resources.compose.stringResource
import kotlinx.coroutines.flow.StateFlow
Expand Down Expand Up @@ -59,8 +66,51 @@ actual fun LazyGridScope.DeviceContent(release: StateFlow<Release?>, onDeviceRea
Text(text = stringResource(SharedRes.strings.github))
}
}
} else if (!reachable) {
Text(text = stringResource(SharedRes.strings.enable_custom_dns))
} else {
val context = LocalContext.current
val verified by DomainVerifier.verified.collectAsStateWithLifecycle()

SideEffect {
DomainVerifier.verify(context)
}

if (!rememberIsTv() && !verified && DomainVerifier.supported) {
Column(
modifier = Modifier.padding(top = 16.dp),
verticalArrangement = Arrangement.spacedBy(8.dp)
) {
Text(
text = stringResource(SharedRes.strings.open_domains_title),
style = MaterialTheme.typography.headlineLarge,
fontWeight = FontWeight.Bold
)
Text(
text = buildAnnotatedString {
withStyle(
SpanStyle(
fontWeight = FontWeight.Bold
)
) {
append(stringResource(SharedRes.strings.open_domains_text_1))
}
appendLine()
append(stringResource(SharedRes.strings.open_domains_text_2))
appendLine()
append(stringResource(SharedRes.strings.open_domains_text_3))
}
)
Button(
modifier = Modifier.fillMaxWidth(),
onClick = {
DomainVerifier.enable(context)
}
) {
Text(text = stringResource(SharedRes.strings.enable))
}
}
} else if (!reachable) {
Text(text = stringResource(SharedRes.strings.enable_custom_dns))
}
}
}
}
5 changes: 5 additions & 0 deletions app/shared/src/commonMain/resources/MR/base/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -87,4 +87,9 @@
<string name="pulz_subtitle">Gaming and E-Sport content</string>
<string name="aniflow">AniFlow</string>
<string name="aniflow_subtitle">Sync your Anime watch progress with AniList or MyAnimeList. (Currently under development)</string>
<string name="enable">Enable</string>
<string name="open_domains_title">Open Links</string>
<string name="open_domains_text_1">You are not protected against fake domains right now!</string>
<string name="open_domains_text_2">Additionally you can view Burning-Series search results directly in this app then.</string>
<string name="open_domains_text_3">Make sure to check the option to open supported links and add every link below.</string>
</resources>
5 changes: 5 additions & 0 deletions app/shared/src/commonMain/resources/MR/de/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -87,4 +87,9 @@
<string name="pulz_subtitle">Gaming und E-Sport Infos</string>
<string name="aniflow">AniFlow</string>
<string name="aniflow_subtitle">Synchronisier deinen Anime Fortschritt mit AniList oder MyAnimeList. (Momentan in der Entwicklung)</string>
<string name="enable">Aktivieren</string>
<string name="open_domains_title">Links öffnen</string>
<string name="open_domains_text_1">Du bist momentan nicht vor Fake-Seiten geschützt!</string>
<string name="open_domains_text_2">Zusätzlich kannst du dann Burning-Series Suchergebnisse in der App sehen.</string>
<string name="open_domains_text_3">Aktiviere einfach die Option unterstützte Links zu öffnen und füge diese darunter hinzu.</string>
</resources>

0 comments on commit 6509d91

Please sign in to comment.