diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 97db999be..e441e910f 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -402,7 +402,7 @@ android:value="@bool/com_samsung_android_icon_container_has_icon_container" /> diff --git a/app/src/main/kotlin/org/koitharu/kotatsu/core/prefs/AppSettings.kt b/app/src/main/kotlin/org/koitharu/kotatsu/core/prefs/AppSettings.kt index 0deade46f..e677c4b13 100644 --- a/app/src/main/kotlin/org/koitharu/kotatsu/core/prefs/AppSettings.kt +++ b/app/src/main/kotlin/org/koitharu/kotatsu/core/prefs/AppSettings.kt @@ -727,6 +727,7 @@ class AppSettings @Inject constructor(@ApplicationContext context: Context) { const val KEY_LINK_MANUAL = "about_help" const val KEY_PROXY_TEST = "proxy_test" const val KEY_OPEN_BROWSER = "open_browser" + const val KEY_HANDLE_LINKS = "handle_links" // old keys are for migration only private const val KEY_IMAGES_PROXY_OLD = "images_proxy" diff --git a/app/src/main/kotlin/org/koitharu/kotatsu/settings/sources/SourcesSettingsFragment.kt b/app/src/main/kotlin/org/koitharu/kotatsu/settings/sources/SourcesSettingsFragment.kt index c4f4e7a83..49ed190bc 100644 --- a/app/src/main/kotlin/org/koitharu/kotatsu/settings/sources/SourcesSettingsFragment.kt +++ b/app/src/main/kotlin/org/koitharu/kotatsu/settings/sources/SourcesSettingsFragment.kt @@ -6,6 +6,7 @@ import android.view.View import androidx.fragment.app.viewModels import androidx.preference.ListPreference import androidx.preference.Preference +import androidx.preference.TwoStatePreference import dagger.hilt.android.AndroidEntryPoint import org.koitharu.kotatsu.R import org.koitharu.kotatsu.core.prefs.AppSettings @@ -50,6 +51,11 @@ class SourcesSettingsFragment : BasePreferenceFragment(R.string.remote_sources) } } } + findPreference(AppSettings.KEY_HANDLE_LINKS)?.let { pref -> + viewModel.isLinksEnabled.observe(viewLifecycleOwner) { + pref.isChecked = it + } + } } override fun onPreferenceTreeClick(preference: Preference): Boolean = when (preference.key) { @@ -58,6 +64,11 @@ class SourcesSettingsFragment : BasePreferenceFragment(R.string.remote_sources) true } + AppSettings.KEY_HANDLE_LINKS -> { + viewModel.setLinksEnabled((preference as TwoStatePreference).isChecked) + true + } + else -> super.onPreferenceTreeClick(preference) } } diff --git a/app/src/main/kotlin/org/koitharu/kotatsu/settings/sources/SourcesSettingsViewModel.kt b/app/src/main/kotlin/org/koitharu/kotatsu/settings/sources/SourcesSettingsViewModel.kt index 164389541..2ac3c53d3 100644 --- a/app/src/main/kotlin/org/koitharu/kotatsu/settings/sources/SourcesSettingsViewModel.kt +++ b/app/src/main/kotlin/org/koitharu/kotatsu/settings/sources/SourcesSettingsViewModel.kt @@ -1,8 +1,16 @@ package org.koitharu.kotatsu.settings.sources +import android.content.ComponentName +import android.content.Context +import android.content.pm.PackageManager +import android.content.pm.PackageManager.COMPONENT_ENABLED_STATE_DEFAULT +import android.content.pm.PackageManager.COMPONENT_ENABLED_STATE_DISABLED +import android.content.pm.PackageManager.COMPONENT_ENABLED_STATE_ENABLED import androidx.lifecycle.viewModelScope import dagger.hilt.android.lifecycle.HiltViewModel +import dagger.hilt.android.qualifiers.ApplicationContext import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.flow.MutableStateFlow import kotlinx.coroutines.flow.SharingStarted import kotlinx.coroutines.flow.stateIn import kotlinx.coroutines.plus @@ -13,8 +21,11 @@ import javax.inject.Inject @HiltViewModel class SourcesSettingsViewModel @Inject constructor( sourcesRepository: MangaSourcesRepository, + @ApplicationContext private val context: Context, ) : BaseViewModel() { + private val linksHandlerActivity = ComponentName(context, "org.koitharu.kotatsu.details.ui.DetailsByLinkActivity") + val enabledSourcesCount = sourcesRepository.observeEnabledSourcesCount() .withErrorHandling() .stateIn(viewModelScope + Dispatchers.Default, SharingStarted.Eagerly, -1) @@ -22,4 +33,20 @@ class SourcesSettingsViewModel @Inject constructor( val availableSourcesCount = sourcesRepository.observeAvailableSourcesCount() .withErrorHandling() .stateIn(viewModelScope + Dispatchers.Default, SharingStarted.Eagerly, -1) + + val isLinksEnabled = MutableStateFlow(isLinksEnabled()) + + fun setLinksEnabled(isEnabled: Boolean) { + context.packageManager.setComponentEnabledSetting( + linksHandlerActivity, + if (isEnabled) COMPONENT_ENABLED_STATE_ENABLED else COMPONENT_ENABLED_STATE_DISABLED, + PackageManager.DONT_KILL_APP, + ) + isLinksEnabled.value = isLinksEnabled() + } + + private fun isLinksEnabled(): Boolean { + val state = context.packageManager.getComponentEnabledSetting(linksHandlerActivity) + return state == COMPONENT_ENABLED_STATE_ENABLED || state == COMPONENT_ENABLED_STATE_DEFAULT + } } diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index ac3d3c952..e8063c02f 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -765,4 +765,6 @@ Max number of backups Delete old backups Automatically delete old backup files to save storage space + Handle links + Handle manga links from external applications (e.g. web browser). You may also need to enable it manually in the application\'s system settings diff --git a/app/src/main/res/xml/pref_sources.xml b/app/src/main/res/xml/pref_sources.xml index d27001849..687502d82 100644 --- a/app/src/main/res/xml/pref_sources.xml +++ b/app/src/main/res/xml/pref_sources.xml @@ -32,4 +32,11 @@ android:summary="@string/disable_nsfw_summary" android:title="@string/disable_nsfw" /> + +