From 3aed6c2aad3632b439a8bdf1a3d84857aeeb7ac4 Mon Sep 17 00:00:00 2001 From: kunfei Date: Sun, 23 Feb 2020 11:17:01 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E4=BC=98=E5=8C=96=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../io/legado/app/ui/about/AboutActivity.kt | 15 ++--------- .../io/legado/app/ui/about/AboutFragment.kt | 27 ++++++++++--------- .../io/legado/app/ui/about/DonateFragment.kt | 21 ++++----------- .../legado/app/ui/book/read/TextActionMenu.kt | 3 ++- .../io/legado/app/utils/ContextExtensions.kt | 19 +++++++++++++ 5 files changed, 42 insertions(+), 43 deletions(-) diff --git a/app/src/main/java/io/legado/app/ui/about/AboutActivity.kt b/app/src/main/java/io/legado/app/ui/about/AboutActivity.kt index 18e3bcdb9..33587d0a2 100644 --- a/app/src/main/java/io/legado/app/ui/about/AboutActivity.kt +++ b/app/src/main/java/io/legado/app/ui/about/AboutActivity.kt @@ -1,14 +1,12 @@ package io.legado.app.ui.about -import android.content.Intent -import android.net.Uri import android.os.Bundle import android.view.Menu import android.view.MenuItem import io.legado.app.R import io.legado.app.base.BaseActivity +import io.legado.app.utils.openUrl import io.legado.app.utils.shareText -import org.jetbrains.anko.toast class AboutActivity : BaseActivity(R.layout.activity_about) { @@ -28,7 +26,7 @@ class AboutActivity : BaseActivity(R.layout.activity_about) { override fun onCompatOptionsItemSelected(item: MenuItem): Boolean { when (item.itemId) { - R.id.menu_scoring -> openIntent("market://details?id=$packageName") + R.id.menu_scoring -> openUrl("market://details?id=$packageName") R.id.menu_share_it -> shareText( "App Share", getString(R.string.app_share_description) @@ -37,13 +35,4 @@ class AboutActivity : BaseActivity(R.layout.activity_about) { return super.onCompatOptionsItemSelected(item) } - private fun openIntent(address: String) { - try { - val intent = Intent(Intent.ACTION_VIEW) - intent.data = Uri.parse(address) - startActivity(intent) - } catch (e: Exception) { - toast(R.string.can_not_open) - } - } } diff --git a/app/src/main/java/io/legado/app/ui/about/AboutFragment.kt b/app/src/main/java/io/legado/app/ui/about/AboutFragment.kt index 8579a0783..365381fbd 100644 --- a/app/src/main/java/io/legado/app/ui/about/AboutFragment.kt +++ b/app/src/main/java/io/legado/app/ui/about/AboutFragment.kt @@ -11,6 +11,7 @@ import io.legado.app.App import io.legado.app.R import io.legado.app.lib.dialogs.alert import io.legado.app.ui.widget.dialog.TextDialog +import io.legado.app.utils.openUrl import io.legado.app.utils.sendToClip import io.legado.app.utils.toast @@ -41,14 +42,14 @@ class AboutFragment : PreferenceFragmentCompat() { override fun onPreferenceTreeClick(preference: Preference?): Boolean { when (preference?.key) { - "contributors" -> openIntent(Intent.ACTION_VIEW, R.string.contributors_url) + "contributors" -> openUrl(R.string.contributors_url) "update_log" -> showUpdateLog() - "check_update" -> openIntent(Intent.ACTION_VIEW, R.string.latest_release_url) - "mail" -> openIntent(Intent.ACTION_SENDTO, "mailto:kunfei.ge@gmail.com") - "git" -> openIntent(Intent.ACTION_VIEW, R.string.this_github_url) - "home_page" -> openIntent(Intent.ACTION_VIEW, R.string.home_page_url) - "license" -> openIntent(Intent.ACTION_VIEW, licenseUrl) - "disclaimer" -> openIntent(Intent.ACTION_VIEW, disclaimerUrl) + "check_update" -> openUrl(R.string.latest_release_url) + "mail" -> sendMail() + "git" -> openUrl(R.string.this_github_url) + "home_page" -> openUrl(R.string.home_page_url) + "license" -> requireContext().openUrl(licenseUrl) + "disclaimer" -> requireContext().openUrl(disclaimerUrl) "qq" -> showQqGroups() "gzGzh" -> requireContext().sendToClip("开源阅读软件") } @@ -56,17 +57,17 @@ class AboutFragment : PreferenceFragmentCompat() { } @Suppress("SameParameterValue") - private fun openIntent(intentName: String, @StringRes addressID: Int) { - openIntent(intentName, getString(addressID)) + private fun openUrl(@StringRes addressID: Int) { + requireContext().openUrl(getString(addressID)) } - private fun openIntent(intentName: String, address: String) { + private fun sendMail() { try { - val intent = Intent(intentName) - intent.data = Uri.parse(address) + val intent = Intent(Intent.ACTION_SENDTO) + intent.data = Uri.parse("mailto:kunfei.ge@gmail.com") startActivity(intent) } catch (e: Exception) { - toast(R.string.can_not_open) + toast(e.localizedMessage ?: "Error") } } diff --git a/app/src/main/java/io/legado/app/ui/about/DonateFragment.kt b/app/src/main/java/io/legado/app/ui/about/DonateFragment.kt index 08daf95ab..0270b3f77 100644 --- a/app/src/main/java/io/legado/app/ui/about/DonateFragment.kt +++ b/app/src/main/java/io/legado/app/ui/about/DonateFragment.kt @@ -9,8 +9,8 @@ import androidx.preference.Preference import androidx.preference.PreferenceFragmentCompat import io.legado.app.R import io.legado.app.utils.ACache +import io.legado.app.utils.openUrl import io.legado.app.utils.sendToClip -import io.legado.app.utils.toast import org.jetbrains.anko.longToast import java.net.URLEncoder @@ -32,10 +32,10 @@ class DonateFragment : PreferenceFragmentCompat() { override fun onPreferenceTreeClick(preference: Preference?): Boolean { when (preference?.key) { - "wxZsm" -> openIntent(Intent.ACTION_VIEW, wxZsRwmUrl) - "zfbHbRwm" -> openIntent(Intent.ACTION_VIEW, zfbHbRwmUrl) - "zfbSkRwm" -> openIntent(Intent.ACTION_VIEW, zfbSkRwmUrl) - "qqSkRwm" -> openIntent(Intent.ACTION_VIEW, qqSkRwmUrl) + "wxZsm" -> requireContext().openUrl(wxZsRwmUrl) + "zfbHbRwm" -> requireContext().openUrl(zfbHbRwmUrl) + "zfbSkRwm" -> requireContext().openUrl(zfbSkRwmUrl) + "qqSkRwm" -> requireContext().openUrl(qqSkRwmUrl) "zfbSk" -> aliDonate(requireContext()) "zfbHbSsm" -> getZfbHb(requireContext()) "gzGzh" -> requireContext().sendToClip("开源阅读软件") @@ -43,17 +43,6 @@ class DonateFragment : PreferenceFragmentCompat() { return super.onPreferenceTreeClick(preference) } - @Suppress("SameParameterValue") - private fun openIntent(intentName: String, address: String) { - try { - val intent = Intent(intentName) - intent.data = Uri.parse(address) - startActivity(intent) - } catch (e: Exception) { - toast(R.string.can_not_open) - } - } - private fun getZfbHb(context: Context) { requireContext().sendToClip("537954522") context.longToast("高级功能已开启\n红包码已复制\n支付宝首页搜索“537954522” 立即领红包") diff --git a/app/src/main/java/io/legado/app/ui/book/read/TextActionMenu.kt b/app/src/main/java/io/legado/app/ui/book/read/TextActionMenu.kt index 1b29cb097..c7044b2ae 100644 --- a/app/src/main/java/io/legado/app/ui/book/read/TextActionMenu.kt +++ b/app/src/main/java/io/legado/app/ui/book/read/TextActionMenu.kt @@ -85,7 +85,8 @@ class TextActionMenu(private val context: Context, private val callBack: CallBac intent.data = Uri.parse(callBack.selectedText) context.startActivity(intent) } catch (e: Exception) { - context.toast(R.string.can_not_open) + e.printStackTrace() + context.toast(e.localizedMessage ?: "ERROR") } } else -> item.intent?.let { diff --git a/app/src/main/java/io/legado/app/utils/ContextExtensions.kt b/app/src/main/java/io/legado/app/utils/ContextExtensions.kt index 25c74ec23..5b266a343 100644 --- a/app/src/main/java/io/legado/app/utils/ContextExtensions.kt +++ b/app/src/main/java/io/legado/app/utils/ContextExtensions.kt @@ -6,6 +6,7 @@ import android.content.res.ColorStateList import android.content.res.Configuration import android.graphics.Bitmap import android.graphics.drawable.Drawable +import android.net.Uri import android.os.BatteryManager import android.provider.Settings import androidx.annotation.ColorRes @@ -152,4 +153,22 @@ fun Context.getBettery(): Int { val iFilter = IntentFilter(Intent.ACTION_BATTERY_CHANGED) val batteryStatus = registerReceiver(null, iFilter) return batteryStatus?.getIntExtra(BatteryManager.EXTRA_LEVEL, -1) ?: -1 +} + +fun Context.openUrl(url: String) { + val intent = Intent(Intent.ACTION_VIEW) + intent.data = Uri.parse(url) + if (intent.resolveActivity(packageManager) != null) { + try { + startActivity(intent) + } catch (e: Exception) { + toast(e.localizedMessage ?: "open url error") + } + } else { + try { + startActivity(Intent.createChooser(intent, "请选择浏览器")) + } catch (e: Exception) { + toast(e.localizedMessage ?: "open url error") + } + } } \ No newline at end of file