From c6d09c7aa17d9f16ebc47a128c5eb3d5d944aa56 Mon Sep 17 00:00:00 2001 From: kunfei Date: Sun, 13 Oct 2019 15:42:42 +0800 Subject: [PATCH] up --- app/src/main/AndroidManifest.xml | 29 ++++----- .../app/receiver/ReceivingSharedActivity.kt | 61 +++++++++++++++++++ .../app/ui/book/search/SearchActivity.kt | 19 +----- .../io/legado/app/ui/config/ConfigFragment.kt | 29 +++++++++ 4 files changed, 107 insertions(+), 31 deletions(-) create mode 100644 app/src/main/java/io/legado/app/receiver/ReceivingSharedActivity.kt diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 3e9c54ca2..e2f33b7c6 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -58,20 +58,7 @@ android:windowSoftInputMode="adjustResize|stateHidden" /> - - - - - - - - - - - - + @@ -97,6 +84,20 @@ + + + + + + + + + + + + diff --git a/app/src/main/java/io/legado/app/receiver/ReceivingSharedActivity.kt b/app/src/main/java/io/legado/app/receiver/ReceivingSharedActivity.kt new file mode 100644 index 000000000..807002ef9 --- /dev/null +++ b/app/src/main/java/io/legado/app/receiver/ReceivingSharedActivity.kt @@ -0,0 +1,61 @@ +package io.legado.app.receiver + +import android.content.Intent +import android.os.Build +import android.os.Bundle +import android.os.PersistableBundle +import androidx.appcompat.app.AppCompatActivity +import io.legado.app.ui.book.search.SearchActivity +import io.legado.app.ui.main.MainActivity +import org.jetbrains.anko.startActivity + +class ReceivingSharedActivity : AppCompatActivity() { + + private val receivingType = "text/plain" + + override fun onCreate(savedInstanceState: Bundle?, persistentState: PersistableBundle?) { + super.onCreate(savedInstanceState, persistentState) + initIntent() + } + + private fun initIntent() { + if (Intent.ACTION_SEND == intent.action && intent.type == receivingType) { + intent.getStringExtra(Intent.EXTRA_TEXT)?.let { + if (openUrl(it)) { + startActivity(Pair("key", it)) + } + finish() + } + } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M + && Intent.ACTION_PROCESS_TEXT == intent.action + && intent.type == receivingType + ) { + intent.getStringExtra(Intent.EXTRA_PROCESS_TEXT)?.let { + if (openUrl(it)) { + startActivity(Pair("key", it)) + } + finish() + } + } + } + + private fun openUrl(text: String): Boolean { + if (text.isBlank()) { + return false + } + val urls = text.split("\\s".toRegex()).dropLastWhile { it.isEmpty() }.toTypedArray() + val result = StringBuilder() + for (url in urls) { + if (url.matches("http.+".toRegex())) + result.append("\n").append(url.trim { it <= ' ' }) + } + return if (result.length > 1) { + val intent = Intent() + intent.setClass(this@ReceivingSharedActivity, MainActivity::class.java) + this.startActivity(intent) + false + } else { + true + } + } +} \ No newline at end of file diff --git a/app/src/main/java/io/legado/app/ui/book/search/SearchActivity.kt b/app/src/main/java/io/legado/app/ui/book/search/SearchActivity.kt index 0812b6706..03b6755b3 100644 --- a/app/src/main/java/io/legado/app/ui/book/search/SearchActivity.kt +++ b/app/src/main/java/io/legado/app/ui/book/search/SearchActivity.kt @@ -1,7 +1,5 @@ package io.legado.app.ui.book.search -import android.content.Intent -import android.os.Build import android.os.Bundle import androidx.appcompat.widget.SearchView import androidx.lifecycle.LiveData @@ -129,21 +127,8 @@ class SearchActivity : VMBaseActivity(R.layout.activity_book_se } private fun initIntent() { - if (Intent.ACTION_SEND == intent.action && intent.type == "text/plain") { - intent.getStringExtra(Intent.EXTRA_TEXT)?.let { - search_view.setQuery(it, true) - } - } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M - && Intent.ACTION_PROCESS_TEXT == intent.action - && intent.type == "text/plain" - ) { - intent.getStringExtra(Intent.EXTRA_PROCESS_TEXT)?.let { - search_view.setQuery(it, true) - } - } else { - intent.getStringExtra("key")?.let { - search_view.setQuery(it, true) - } + intent.getStringExtra("key")?.let { + search_view.setQuery(it, true) } } diff --git a/app/src/main/java/io/legado/app/ui/config/ConfigFragment.kt b/app/src/main/java/io/legado/app/ui/config/ConfigFragment.kt index fd957e433..f01bd6079 100644 --- a/app/src/main/java/io/legado/app/ui/config/ConfigFragment.kt +++ b/app/src/main/java/io/legado/app/ui/config/ConfigFragment.kt @@ -1,6 +1,8 @@ package io.legado.app.ui.config +import android.content.ComponentName import android.content.SharedPreferences +import android.content.pm.PackageManager import android.os.Bundle import android.view.View import androidx.preference.ListPreference @@ -10,6 +12,7 @@ import io.legado.app.App import io.legado.app.R import io.legado.app.help.BookHelp import io.legado.app.lib.theme.ATH +import io.legado.app.ui.book.search.SearchActivity import io.legado.app.utils.LogUtils import io.legado.app.utils.getPrefString @@ -17,6 +20,12 @@ import io.legado.app.utils.getPrefString class ConfigFragment : PreferenceFragmentCompat(), Preference.OnPreferenceChangeListener, SharedPreferences.OnSharedPreferenceChangeListener { + private val packageManager = App.INSTANCE.packageManager + private val componentName = ComponentName( + App.INSTANCE, + SearchActivity::class.java.name + ) + override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) { addPreferencesFromResource(R.xml.pref_config) bindPreferenceSummaryToValue(findPreference("downloadPath")) @@ -41,6 +50,9 @@ class ConfigFragment : PreferenceFragmentCompat(), Preference.OnPreferenceChange when (key) { "downloadPath" -> BookHelp.upDownloadPath() "recordLog" -> LogUtils.upLevel() + "process_text" -> sharedPreferences?.let { + setProcessTextEnable(it.getBoolean("process_text", true)) + } } } @@ -77,4 +89,21 @@ class ConfigFragment : PreferenceFragmentCompat(), Preference.OnPreferenceChange } } + private fun isProcessTextEnabled(): Boolean { + return packageManager.getComponentEnabledSetting(componentName) != PackageManager.COMPONENT_ENABLED_STATE_DISABLED + } + + private fun setProcessTextEnable(enable: Boolean) { + if (enable) { + packageManager.setComponentEnabledSetting( + componentName, + PackageManager.COMPONENT_ENABLED_STATE_ENABLED, PackageManager.DONT_KILL_APP + ) + } else { + packageManager.setComponentEnabledSetting( + componentName, + PackageManager.COMPONENT_ENABLED_STATE_DISABLED, PackageManager.DONT_KILL_APP + ) + } + } } \ No newline at end of file