feat: 优化代码

pull/115/head
kunfei 5 years ago
parent 76fec52adb
commit 3aed6c2aad
  1. 15
      app/src/main/java/io/legado/app/ui/about/AboutActivity.kt
  2. 27
      app/src/main/java/io/legado/app/ui/about/AboutFragment.kt
  3. 21
      app/src/main/java/io/legado/app/ui/about/DonateFragment.kt
  4. 3
      app/src/main/java/io/legado/app/ui/book/read/TextActionMenu.kt
  5. 19
      app/src/main/java/io/legado/app/utils/ContextExtensions.kt

@ -1,14 +1,12 @@
package io.legado.app.ui.about package io.legado.app.ui.about
import android.content.Intent
import android.net.Uri
import android.os.Bundle import android.os.Bundle
import android.view.Menu import android.view.Menu
import android.view.MenuItem import android.view.MenuItem
import io.legado.app.R import io.legado.app.R
import io.legado.app.base.BaseActivity import io.legado.app.base.BaseActivity
import io.legado.app.utils.openUrl
import io.legado.app.utils.shareText import io.legado.app.utils.shareText
import org.jetbrains.anko.toast
class AboutActivity : BaseActivity(R.layout.activity_about) { class AboutActivity : BaseActivity(R.layout.activity_about) {
@ -28,7 +26,7 @@ class AboutActivity : BaseActivity(R.layout.activity_about) {
override fun onCompatOptionsItemSelected(item: MenuItem): Boolean { override fun onCompatOptionsItemSelected(item: MenuItem): Boolean {
when (item.itemId) { 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( R.id.menu_share_it -> shareText(
"App Share", "App Share",
getString(R.string.app_share_description) getString(R.string.app_share_description)
@ -37,13 +35,4 @@ class AboutActivity : BaseActivity(R.layout.activity_about) {
return super.onCompatOptionsItemSelected(item) 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)
}
}
} }

@ -11,6 +11,7 @@ import io.legado.app.App
import io.legado.app.R import io.legado.app.R
import io.legado.app.lib.dialogs.alert import io.legado.app.lib.dialogs.alert
import io.legado.app.ui.widget.dialog.TextDialog 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.sendToClip
import io.legado.app.utils.toast import io.legado.app.utils.toast
@ -41,14 +42,14 @@ class AboutFragment : PreferenceFragmentCompat() {
override fun onPreferenceTreeClick(preference: Preference?): Boolean { override fun onPreferenceTreeClick(preference: Preference?): Boolean {
when (preference?.key) { when (preference?.key) {
"contributors" -> openIntent(Intent.ACTION_VIEW, R.string.contributors_url) "contributors" -> openUrl(R.string.contributors_url)
"update_log" -> showUpdateLog() "update_log" -> showUpdateLog()
"check_update" -> openIntent(Intent.ACTION_VIEW, R.string.latest_release_url) "check_update" -> openUrl(R.string.latest_release_url)
"mail" -> openIntent(Intent.ACTION_SENDTO, "mailto:kunfei.ge@gmail.com") "mail" -> sendMail()
"git" -> openIntent(Intent.ACTION_VIEW, R.string.this_github_url) "git" -> openUrl(R.string.this_github_url)
"home_page" -> openIntent(Intent.ACTION_VIEW, R.string.home_page_url) "home_page" -> openUrl(R.string.home_page_url)
"license" -> openIntent(Intent.ACTION_VIEW, licenseUrl) "license" -> requireContext().openUrl(licenseUrl)
"disclaimer" -> openIntent(Intent.ACTION_VIEW, disclaimerUrl) "disclaimer" -> requireContext().openUrl(disclaimerUrl)
"qq" -> showQqGroups() "qq" -> showQqGroups()
"gzGzh" -> requireContext().sendToClip("开源阅读软件") "gzGzh" -> requireContext().sendToClip("开源阅读软件")
} }
@ -56,17 +57,17 @@ class AboutFragment : PreferenceFragmentCompat() {
} }
@Suppress("SameParameterValue") @Suppress("SameParameterValue")
private fun openIntent(intentName: String, @StringRes addressID: Int) { private fun openUrl(@StringRes addressID: Int) {
openIntent(intentName, getString(addressID)) requireContext().openUrl(getString(addressID))
} }
private fun openIntent(intentName: String, address: String) { private fun sendMail() {
try { try {
val intent = Intent(intentName) val intent = Intent(Intent.ACTION_SENDTO)
intent.data = Uri.parse(address) intent.data = Uri.parse("mailto:kunfei.ge@gmail.com")
startActivity(intent) startActivity(intent)
} catch (e: Exception) { } catch (e: Exception) {
toast(R.string.can_not_open) toast(e.localizedMessage ?: "Error")
} }
} }

@ -9,8 +9,8 @@ import androidx.preference.Preference
import androidx.preference.PreferenceFragmentCompat import androidx.preference.PreferenceFragmentCompat
import io.legado.app.R import io.legado.app.R
import io.legado.app.utils.ACache import io.legado.app.utils.ACache
import io.legado.app.utils.openUrl
import io.legado.app.utils.sendToClip import io.legado.app.utils.sendToClip
import io.legado.app.utils.toast
import org.jetbrains.anko.longToast import org.jetbrains.anko.longToast
import java.net.URLEncoder import java.net.URLEncoder
@ -32,10 +32,10 @@ class DonateFragment : PreferenceFragmentCompat() {
override fun onPreferenceTreeClick(preference: Preference?): Boolean { override fun onPreferenceTreeClick(preference: Preference?): Boolean {
when (preference?.key) { when (preference?.key) {
"wxZsm" -> openIntent(Intent.ACTION_VIEW, wxZsRwmUrl) "wxZsm" -> requireContext().openUrl(wxZsRwmUrl)
"zfbHbRwm" -> openIntent(Intent.ACTION_VIEW, zfbHbRwmUrl) "zfbHbRwm" -> requireContext().openUrl(zfbHbRwmUrl)
"zfbSkRwm" -> openIntent(Intent.ACTION_VIEW, zfbSkRwmUrl) "zfbSkRwm" -> requireContext().openUrl(zfbSkRwmUrl)
"qqSkRwm" -> openIntent(Intent.ACTION_VIEW, qqSkRwmUrl) "qqSkRwm" -> requireContext().openUrl(qqSkRwmUrl)
"zfbSk" -> aliDonate(requireContext()) "zfbSk" -> aliDonate(requireContext())
"zfbHbSsm" -> getZfbHb(requireContext()) "zfbHbSsm" -> getZfbHb(requireContext())
"gzGzh" -> requireContext().sendToClip("开源阅读软件") "gzGzh" -> requireContext().sendToClip("开源阅读软件")
@ -43,17 +43,6 @@ class DonateFragment : PreferenceFragmentCompat() {
return super.onPreferenceTreeClick(preference) 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) { private fun getZfbHb(context: Context) {
requireContext().sendToClip("537954522") requireContext().sendToClip("537954522")
context.longToast("高级功能已开启\n红包码已复制\n支付宝首页搜索“537954522” 立即领红包") context.longToast("高级功能已开启\n红包码已复制\n支付宝首页搜索“537954522” 立即领红包")

@ -85,7 +85,8 @@ class TextActionMenu(private val context: Context, private val callBack: CallBac
intent.data = Uri.parse(callBack.selectedText) intent.data = Uri.parse(callBack.selectedText)
context.startActivity(intent) context.startActivity(intent)
} catch (e: Exception) { } catch (e: Exception) {
context.toast(R.string.can_not_open) e.printStackTrace()
context.toast(e.localizedMessage ?: "ERROR")
} }
} }
else -> item.intent?.let { else -> item.intent?.let {

@ -6,6 +6,7 @@ import android.content.res.ColorStateList
import android.content.res.Configuration import android.content.res.Configuration
import android.graphics.Bitmap import android.graphics.Bitmap
import android.graphics.drawable.Drawable import android.graphics.drawable.Drawable
import android.net.Uri
import android.os.BatteryManager import android.os.BatteryManager
import android.provider.Settings import android.provider.Settings
import androidx.annotation.ColorRes import androidx.annotation.ColorRes
@ -153,3 +154,21 @@ fun Context.getBettery(): Int {
val batteryStatus = registerReceiver(null, iFilter) val batteryStatus = registerReceiver(null, iFilter)
return batteryStatus?.getIntExtra(BatteryManager.EXTRA_LEVEL, -1) ?: -1 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")
}
}
}
Loading…
Cancel
Save