Skip to content

Commit

Permalink
Option to disable link handling #1149
Browse files Browse the repository at this point in the history
  • Loading branch information
Koitharu committed Nov 24, 2024
1 parent e65a3b4 commit c993488
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 1 deletion.
2 changes: 1 addition & 1 deletion app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -402,7 +402,7 @@
android:value="@bool/com_samsung_android_icon_container_has_icon_container" />

<activity-alias
android:name="org.koitharu.kotatsu.details.ui.DetailsBYLinkActivity"
android:name="org.koitharu.kotatsu.details.ui.DetailsByLinkActivity"
android:exported="true"
android:targetActivity="org.koitharu.kotatsu.details.ui.DetailsActivity">

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -50,6 +51,11 @@ class SourcesSettingsFragment : BasePreferenceFragment(R.string.remote_sources)
}
}
}
findPreference<TwoStatePreference>(AppSettings.KEY_HANDLE_LINKS)?.let { pref ->
viewModel.isLinksEnabled.observe(viewLifecycleOwner) {
pref.isChecked = it
}
}
}

override fun onPreferenceTreeClick(preference: Preference): Boolean = when (preference.key) {
Expand All @@ -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)
}
}
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -13,13 +21,32 @@ 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)

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
}
}
2 changes: 2 additions & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -765,4 +765,6 @@
<string name="max_backups_count">Max number of backups</string>
<string name="delete_old_backups">Delete old backups</string>
<string name="delete_old_backups_summary">Automatically delete old backup files to save storage space</string>
<string name="handle_links">Handle links</string>
<string name="handle_links_summary">Handle manga links from external applications (e.g. web browser). You may also need to enable it manually in the application\'s system settings</string>
</resources>
7 changes: 7 additions & 0 deletions app/src/main/res/xml/pref_sources.xml
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,11 @@
android:summary="@string/disable_nsfw_summary"
android:title="@string/disable_nsfw" />

<SwitchPreferenceCompat
android:key="handle_links"
android:persistent="false"
android:summary="@string/handle_links_summary"
android:title="@string/handle_links"
app:allowDividerAbove="true" />

</androidx.preference.PreferenceScreen>

0 comments on commit c993488

Please sign in to comment.