Fix: intent.resolveActivity(...) returns null (instead used different function, which fixes "No browser found" for all sites if browser is actually present).

This commit is contained in:
acclorite 2024-07-23 15:38:12 +03:00
parent ae6596f237
commit 5bcda4a7d9
10 changed files with 99 additions and 55 deletions

View file

@ -26,6 +26,7 @@
</intent> </intent>
<intent> <intent>
<action android:name="android.intent.action.VIEW" /> <action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<data android:scheme="https" /> <data android:scheme="https" />
</intent> </intent>

View file

@ -1,11 +0,0 @@
package ua.acclorite.book_story.presentation.data
fun String.removeTrailingZero(): String {
if (!this.contains('.'))
return this
return this
.dropLastWhile { it == '0' }
.dropLastWhile { it == '.' }
}
fun Double.removeDigits(digits: Int) = "%.${digits}f".format(this).replace(",", ".")

View file

@ -0,0 +1,34 @@
package ua.acclorite.book_story.presentation.data
import android.content.Intent
import androidx.activity.ComponentActivity
fun String.removeTrailingZero(): String {
if (!this.contains('.'))
return this
return this
.dropLastWhile { it == '0' }
.dropLastWhile { it == '.' }
}
fun Double.removeDigits(digits: Int) = "%.${digits}f".format(this).replace(",", ".")
fun Intent.launchActivity(
activity: ComponentActivity,
openInNewWindow: Boolean = true,
success: (() -> Unit)? = null,
error: () -> Unit
) {
if (openInNewWindow) {
this.flags = Intent.FLAG_ACTIVITY_NEW_TASK
}
try {
activity.baseContext.startActivity(this)
} catch (e: Exception) {
error()
return
}
success?.invoke()
}

View file

@ -2,6 +2,7 @@ package ua.acclorite.book_story.presentation.screens.about.data
import android.content.Intent import android.content.Intent
import android.net.Uri import android.net.Uri
import androidx.activity.ComponentActivity
import androidx.lifecycle.ViewModel import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope import androidx.lifecycle.viewModelScope
import dagger.hilt.android.lifecycle.HiltViewModel import dagger.hilt.android.lifecycle.HiltViewModel
@ -11,6 +12,7 @@ import kotlinx.coroutines.flow.update
import kotlinx.coroutines.launch import kotlinx.coroutines.launch
import ua.acclorite.book_story.R import ua.acclorite.book_story.R
import ua.acclorite.book_story.domain.use_case.CheckForUpdates import ua.acclorite.book_story.domain.use_case.CheckForUpdates
import ua.acclorite.book_story.presentation.data.launchActivity
import javax.inject.Inject import javax.inject.Inject
@HiltViewModel @HiltViewModel
@ -30,14 +32,11 @@ class AboutViewModel @Inject constructor(
Uri.parse(event.page) Uri.parse(event.page)
) )
if (intent.resolveActivity(event.context.packageManager) != null) { intent.launchActivity(event.context as ComponentActivity) {
event.context.startActivity(intent)
return@launch
}
event.noAppsFound() event.noAppsFound()
} }
} }
}
is AboutEvent.OnCheckForUpdates -> { is AboutEvent.OnCheckForUpdates -> {
viewModelScope.launch { viewModelScope.launch {

View file

@ -3,6 +3,7 @@ package ua.acclorite.book_story.presentation.screens.about.nested.license_info.d
import android.content.Context import android.content.Context
import android.content.Intent import android.content.Intent
import android.net.Uri import android.net.Uri
import androidx.activity.ComponentActivity
import androidx.lifecycle.ViewModel import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope import androidx.lifecycle.viewModelScope
import com.mikepenz.aboutlibraries.Libs import com.mikepenz.aboutlibraries.Libs
@ -15,6 +16,7 @@ import kotlinx.coroutines.flow.update
import kotlinx.coroutines.launch import kotlinx.coroutines.launch
import ua.acclorite.book_story.domain.util.OnNavigate import ua.acclorite.book_story.domain.util.OnNavigate
import ua.acclorite.book_story.presentation.data.Screen import ua.acclorite.book_story.presentation.data.Screen
import ua.acclorite.book_story.presentation.data.launchActivity
import javax.inject.Inject import javax.inject.Inject
@HiltViewModel @HiltViewModel
@ -32,16 +34,13 @@ class LicenseInfoViewModel @Inject constructor() : ViewModel() {
Uri.parse(event.page) Uri.parse(event.page)
) )
if (intent.resolveActivity(event.context.packageManager) != null) { intent.launchActivity(event.context as ComponentActivity) {
event.context.startActivity(intent)
return@launch
}
event.noAppsFound() event.noAppsFound()
} }
} }
} }
} }
}
fun init(screen: Screen.About.LicenseInfo, onNavigate: OnNavigate, context: Context) { fun init(screen: Screen.About.LicenseInfo, onNavigate: OnNavigate, context: Context) {
viewModelScope.launch(Dispatchers.IO) { viewModelScope.launch(Dispatchers.IO) {

View file

@ -26,6 +26,7 @@ import ua.acclorite.book_story.domain.use_case.GetFilesFromDevice
import ua.acclorite.book_story.domain.use_case.InsertBook import ua.acclorite.book_story.domain.use_case.InsertBook
import ua.acclorite.book_story.domain.util.Resource import ua.acclorite.book_story.domain.util.Resource
import ua.acclorite.book_story.presentation.data.Screen import ua.acclorite.book_story.presentation.data.Screen
import ua.acclorite.book_story.presentation.data.launchActivity
import javax.inject.Inject import javax.inject.Inject
@OptIn(ExperimentalPermissionsApi::class) @OptIn(ExperimentalPermissionsApi::class)
@ -84,9 +85,11 @@ class BrowseViewModel @Inject constructor(
val uri = Uri.parse("package:${event.activity.packageName}") val uri = Uri.parse("package:${event.activity.packageName}")
val intent = Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS, uri) val intent = Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS, uri)
if (intent.resolveActivity(event.activity.packageManager) != null) { var failure = false
event.activity.startActivity(intent) intent.launchActivity(event.activity) {
} else { failure = true
}
if (failure) {
return return
} }
} }
@ -99,9 +102,11 @@ class BrowseViewModel @Inject constructor(
uri uri
) )
if (intent.resolveActivity(event.activity.packageManager) != null) { var failure = false
event.activity.startActivity(intent) intent.launchActivity(event.activity) {
} else { failure = true
}
if (failure) {
return return
} }
} }

View file

@ -3,6 +3,7 @@ package ua.acclorite.book_story.presentation.screens.help.data
import android.app.SearchManager import android.app.SearchManager
import android.content.Intent import android.content.Intent
import android.net.Uri import android.net.Uri
import androidx.activity.ComponentActivity
import androidx.lifecycle.ViewModel import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope import androidx.lifecycle.viewModelScope
import dagger.hilt.android.lifecycle.HiltViewModel import dagger.hilt.android.lifecycle.HiltViewModel
@ -11,6 +12,7 @@ import kotlinx.coroutines.flow.asStateFlow
import kotlinx.coroutines.flow.update import kotlinx.coroutines.flow.update
import kotlinx.coroutines.launch import kotlinx.coroutines.launch
import ua.acclorite.book_story.presentation.data.Screen import ua.acclorite.book_story.presentation.data.Screen
import ua.acclorite.book_story.presentation.data.launchActivity
import javax.inject.Inject import javax.inject.Inject
@HiltViewModel @HiltViewModel
@ -30,14 +32,11 @@ class HelpViewModel @Inject constructor(
Uri.parse(event.page) Uri.parse(event.page)
) )
if (intent.resolveActivity(event.context.packageManager) != null) { intent.launchActivity(event.context as ComponentActivity) {
event.context.startActivity(intent)
return@launch
}
event.noAppsFound() event.noAppsFound()
} }
} }
}
is HelpEvent.OnSearchInWeb -> { is HelpEvent.OnSearchInWeb -> {
viewModelScope.launch { viewModelScope.launch {
@ -58,14 +57,11 @@ class HelpViewModel @Inject constructor(
"${_state.value.textFieldValue.trim()} filetype:txt OR filetype:pdf" "${_state.value.textFieldValue.trim()} filetype:txt OR filetype:pdf"
) )
if (intent.resolveActivity(event.context.packageManager) != null) { intent.launchActivity(event.context as ComponentActivity) {
event.context.startActivity(intent)
return@launch
}
event.noAppsFound() event.noAppsFound()
} }
} }
}
is HelpEvent.OnUpdateState -> { is HelpEvent.OnUpdateState -> {
_state.update { _state.update {

View file

@ -32,6 +32,7 @@ import ua.acclorite.book_story.domain.use_case.UpdateBooks
import ua.acclorite.book_story.domain.util.OnNavigate import ua.acclorite.book_story.domain.util.OnNavigate
import ua.acclorite.book_story.domain.util.UIText import ua.acclorite.book_story.domain.util.UIText
import ua.acclorite.book_story.presentation.data.Screen import ua.acclorite.book_story.presentation.data.Screen
import ua.acclorite.book_story.presentation.data.launchActivity
import ua.acclorite.book_story.presentation.screens.history.data.HistoryEvent import ua.acclorite.book_story.presentation.screens.history.data.HistoryEvent
import ua.acclorite.book_story.presentation.screens.library.data.LibraryEvent import ua.acclorite.book_story.presentation.screens.library.data.LibraryEvent
import javax.inject.Inject import javax.inject.Inject
@ -336,13 +337,19 @@ class ReaderViewModel @Inject constructor(
"translate: ${event.textToTranslate.trim()}" "translate: ${event.textToTranslate.trim()}"
) )
if (translatorIntent.resolveActivity(event.context.packageManager) != null) { var translatorFailure = false
event.context.startActivity(translatorIntent) translatorIntent.launchActivity(event.context) {
translatorFailure = true
}
if (!translatorFailure) {
return@launch return@launch
} }
if (browserIntent.resolveActivity(event.context.packageManager) != null) { var browserFailure = false
event.context.startActivity(browserIntent) browserIntent.launchActivity(event.context) {
browserFailure = true
}
if (!browserFailure) {
return@launch return@launch
} }
@ -361,8 +368,11 @@ class ReaderViewModel @Inject constructor(
": ${event.textToDefine.trim()}" ": ${event.textToDefine.trim()}"
) )
if (browserIntent.resolveActivity(event.context.packageManager) != null) { var failure = false
event.context.startActivity(browserIntent) browserIntent.launchActivity(event.context) {
failure = true
}
if (!failure) {
return@launch return@launch
} }

View file

@ -13,6 +13,7 @@ import kotlinx.coroutines.Job
import kotlinx.coroutines.delay import kotlinx.coroutines.delay
import kotlinx.coroutines.launch import kotlinx.coroutines.launch
import kotlinx.coroutines.yield import kotlinx.coroutines.yield
import ua.acclorite.book_story.presentation.data.launchActivity
import javax.inject.Inject import javax.inject.Inject
@OptIn(ExperimentalPermissionsApi::class) @OptIn(ExperimentalPermissionsApi::class)
@ -48,9 +49,12 @@ class SettingsViewModel @Inject constructor(
val intent = Intent(Settings.ACTION_APP_NOTIFICATION_SETTINGS) val intent = Intent(Settings.ACTION_APP_NOTIFICATION_SETTINGS)
intent.putExtra(Settings.EXTRA_APP_PACKAGE, event.activity.packageName) intent.putExtra(Settings.EXTRA_APP_PACKAGE, event.activity.packageName)
if (intent.resolveActivity(event.activity.packageManager) != null) { var failure = false
event.activity.startActivity(intent) intent.launchActivity(event.activity) {
} else { failure = true
}
if (failure) {
return return
} }
} }

View file

@ -22,6 +22,7 @@ import kotlinx.coroutines.launch
import kotlinx.coroutines.yield import kotlinx.coroutines.yield
import ua.acclorite.book_story.domain.use_case.CheckForUpdates import ua.acclorite.book_story.domain.use_case.CheckForUpdates
import ua.acclorite.book_story.presentation.data.Screen import ua.acclorite.book_story.presentation.data.Screen
import ua.acclorite.book_story.presentation.data.launchActivity
import javax.inject.Inject import javax.inject.Inject
@OptIn(ExperimentalPermissionsApi::class) @OptIn(ExperimentalPermissionsApi::class)
@ -137,9 +138,11 @@ class StartViewModel @Inject constructor(
val uri = Uri.parse("package:${event.activity.packageName}") val uri = Uri.parse("package:${event.activity.packageName}")
val intent = Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS, uri) val intent = Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS, uri)
if (intent.resolveActivity(event.activity.packageManager) != null) { var failure = false
event.activity.startActivity(intent) intent.launchActivity(event.activity) {
} else { failure = true
}
if (failure) {
return return
} }
} }
@ -152,9 +155,11 @@ class StartViewModel @Inject constructor(
uri uri
) )
if (intent.resolveActivity(event.activity.packageManager) != null) { var failure = false
event.activity.startActivity(intent) intent.launchActivity(event.activity) {
} else { failure = true
}
if (failure) {
return return
} }
} }
@ -203,9 +208,11 @@ class StartViewModel @Inject constructor(
val intent = Intent(Settings.ACTION_APP_NOTIFICATION_SETTINGS) val intent = Intent(Settings.ACTION_APP_NOTIFICATION_SETTINGS)
intent.putExtra(Settings.EXTRA_APP_PACKAGE, event.activity.packageName) intent.putExtra(Settings.EXTRA_APP_PACKAGE, event.activity.packageName)
if (intent.resolveActivity(event.activity.packageManager) != null) { var failure = false
event.activity.startActivity(intent) intent.launchActivity(event.activity) {
} else { failure = true
}
if (failure) {
return return
} }
} }