Skip to content

Commit

Permalink
feat: 增加可选数据源;HTTP 请求改用Fuel实现;增加更新日志解析
Browse files Browse the repository at this point in the history
  • Loading branch information
WankkoRee committed Jul 17, 2022
1 parent 19bfac2 commit 9e23512
Show file tree
Hide file tree
Showing 19 changed files with 1,614 additions and 1,205 deletions.
4 changes: 4 additions & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -93,5 +93,9 @@ dependencies {

implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-android:1.5.2'
implementation 'androidx.lifecycle:lifecycle-runtime-ktx:2.4.1'
implementation 'com.github.kittinunf.fuel:fuel:2.3.1'
implementation 'com.github.kittinunf.fuel:fuel-android:2.3.1'
implementation 'com.github.kittinunf.fuel:fuel-gson:2.3.1'
implementation 'com.google.code.gson:gson:2.9.0'
implementation "io.noties.markwon:core:4.6.2"
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,22 @@ import android.content.Intent
import android.net.Uri
import android.os.Build
import android.os.Bundle
import android.util.Log
import android.view.View
import android.view.ViewGroup
import android.widget.Toast
import androidx.appcompat.app.AlertDialog
import androidx.appcompat.app.AppCompatActivity
import androidx.viewpager.widget.PagerAdapter
import cn.wankkoree.xposed.enablewebviewdebugging.BuildConfig
import cn.wankkoree.xposed.enablewebviewdebugging.R
import cn.wankkoree.xposed.enablewebviewdebugging.data.ModuleSP.data_source
import cn.wankkoree.xposed.enablewebviewdebugging.databinding.AdvanceBinding
import cn.wankkoree.xposed.enablewebviewdebugging.databinding.DialogDataSourceBinding
import com.github.kittinunf.fuel.Fuel
import com.github.kittinunf.fuel.gson.responseObject
import com.highcapable.yukihookapi.YukiHookAPI
import com.highcapable.yukihookapi.hook.factory.modulePrefs

class Advance: AppCompatActivity() {
private lateinit var viewBinding: AdvanceBinding
Expand All @@ -23,6 +30,81 @@ class Advance: AppCompatActivity() {
viewBinding = AdvanceBinding.inflate(layoutInflater)
setContentView(viewBinding.root)

viewBinding.advanceSettingDataSource.setOnClickListener {
val dialogBinding = DialogDataSourceBinding.inflate(layoutInflater)
AlertDialog.Builder(this@Advance).apply {
val dataSource = modulePrefs("module").get(data_source)
when (dataSource) {
"https://raw.githubusercontent.com/WankkoRee/EnableWebViewDebugging-Rules/master" -> dialogBinding.dialogDataSource.check(dialogBinding.dialogDataSourceGithub.id)
"https://raw.fastgit.org/WankkoRee/EnableWebViewDebugging-Rules/master" -> dialogBinding.dialogDataSource.check(dialogBinding.dialogDataSourceFastgit.id)
else -> {
dialogBinding.dialogDataSource.check(dialogBinding.dialogDataSourceCustom.id)
dialogBinding.dialogDataSourceCustomValue.visibility = View.VISIBLE
}
}
dialogBinding.dialogDataSourceCustomValue.setText(dataSource)
setView(dialogBinding.root)
}.show().also { dialog ->
dialogBinding.dialogDataSource.setOnCheckedChangeListener { _, checkedId ->
when (checkedId) {
dialogBinding.dialogDataSourceGithub.id -> {
dialogBinding.dialogDataSourceCustomValue.visibility = View.GONE
}
dialogBinding.dialogDataSourceFastgit.id -> {
dialogBinding.dialogDataSourceCustomValue.visibility = View.GONE
}
dialogBinding.dialogDataSourceCustom.id -> {
dialogBinding.dialogDataSourceCustomValue.visibility = View.VISIBLE
}
}
}
dialogBinding.dialogDataSourceTest.setOnClickListener TestEvent@ {
val dataSource = when (dialogBinding.dialogDataSource.checkedRadioButtonId) {
dialogBinding.dialogDataSourceGithub.id -> "https://raw.githubusercontent.com/WankkoRee/EnableWebViewDebugging-Rules/master"
dialogBinding.dialogDataSourceFastgit.id -> "https://raw.fastgit.org/WankkoRee/EnableWebViewDebugging-Rules/master"
dialogBinding.dialogDataSourceCustom.id -> dialogBinding.dialogDataSourceCustomValue.text.toString()
else -> {
toast?.cancel()
toast = Toast.makeText(this@Advance, getString(R.string.unknown_checked_radio_button_id), Toast.LENGTH_SHORT)
toast!!.show()
null
}
} ?: return@TestEvent
Fuel.get("$dataSource/rules/rules.json")
.responseObject<List<String>> { _, _, result ->
result.fold({
toast?.cancel()
toast = Toast.makeText(this@Advance, getString(R.string.available), Toast.LENGTH_SHORT)
toast!!.show()
}, {
Log.e(BuildConfig.APPLICATION_ID, getString(R.string.unavailable), it)
toast?.cancel()
toast = Toast.makeText(this@Advance, getString(R.string.unavailable), Toast.LENGTH_SHORT)
toast!!.show()
})
}
}
dialogBinding.dialogDataSourceCancel.setOnClickListener {
dialog.cancel()
}
dialogBinding.dialogDataSourceSave.setOnClickListener {
when (dialogBinding.dialogDataSource.checkedRadioButtonId) {
dialogBinding.dialogDataSourceGithub.id -> {
modulePrefs("module").put(data_source, "https://raw.githubusercontent.com/WankkoRee/EnableWebViewDebugging-Rules/master")
}
dialogBinding.dialogDataSourceFastgit.id -> {
modulePrefs("module").put(data_source, "https://raw.fastgit.org/WankkoRee/EnableWebViewDebugging-Rules/master")
}
dialogBinding.dialogDataSourceCustom.id -> {
modulePrefs("module").put(data_source, dialogBinding.dialogDataSourceCustomValue.text.toString())
}
}
refresh()
dialog.cancel()
}
}
}

getPrimaryColor(viewBinding.advanceLicenseAuthorIcon.drawable, this@Advance).first.also {
viewBinding.advanceLicenseAuthorCard.backgroundTintList = colorStateSingle((it or 0xff000000.toInt()) and 0x33ffffff)
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) viewBinding.advanceLicenseAuthorCard.outlineSpotShadowColor = it
Expand Down Expand Up @@ -61,6 +143,14 @@ class Advance: AppCompatActivity() {
viewBinding.advanceToolbarBack.setOnClickListener {
finish()
}

refresh()
}

private fun refresh() {
with(modulePrefs("module")) {
viewBinding.advanceSettingDataSourceValue.text = get(data_source)
}
}

class LicenseAdapter(private val items: ArrayList<View>) : PagerAdapter() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,12 @@ fun getPrimaryColor(d: Drawable, context: Context): Triple<Int, Int, Int> {
}

fun Context.getDrawableCompat(resId: Int) = AppCompatResources.getDrawable(this@getDrawableCompat, resId)

fun Long.autoUnitByte() : String = when (this@autoUnitByte) {
in 0 until 1024 -> "${this@autoUnitByte} B"
in 1024 until 1024*1024 -> "${(this@autoUnitByte / 1024.0).round(2)} KB"
in 1024*1024 until 1024*1024*1024 -> "${(this@autoUnitByte / 1048576.0).round(2)} MB"
else -> "${(this@autoUnitByte / 1073741824.0).round(2)} GB"
}

fun Number.round(scale: Int) : String = String.format("%.${scale}f", this@round)
Original file line number Diff line number Diff line change
Expand Up @@ -12,36 +12,36 @@ import androidx.activity.result.contract.ActivityResultContract
import androidx.appcompat.app.AlertDialog
import androidx.appcompat.app.AppCompatActivity
import androidx.appcompat.widget.PopupMenu
import androidx.lifecycle.lifecycleScope
import cn.wankkoree.xposed.enablewebviewdebugging.BuildConfig
import cn.wankkoree.xposed.enablewebviewdebugging.R
import cn.wankkoree.xposed.enablewebviewdebugging.data.AppsSP
import cn.wankkoree.xposed.enablewebviewdebugging.data.ResourcesSP
import cn.wankkoree.xposed.enablewebviewdebugging.data.getSet
import cn.wankkoree.xposed.enablewebviewdebugging.databinding.MainBinding
import cn.wankkoree.xposed.enablewebviewdebugging.http.Http
import com.github.kittinunf.fuel.Fuel
import com.github.kittinunf.fuel.gson.responseObject
import com.google.gson.GsonBuilder
import com.highcapable.yukihookapi.YukiHookAPI.Status.executorName
import com.highcapable.yukihookapi.YukiHookAPI.Status.executorVersion
import com.highcapable.yukihookapi.YukiHookAPI.Status.isModuleActive
import com.highcapable.yukihookapi.YukiHookAPI.Status.isTaiChiModuleActive
import com.highcapable.yukihookapi.hook.factory.modulePrefs
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import io.noties.markwon.Markwon
import java.net.URLEncoder


class Main: AppCompatActivity() {

private lateinit var viewBinding: MainBinding
private var toast: Toast? = null
private val appsResultContract = registerForActivityResult(AppsResultContract()) {
refresh()
}
private lateinit var markdown: Markwon

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
viewBinding = MainBinding.inflate(layoutInflater)
setContentView(viewBinding.root)
markdown = Markwon.create(this@Main)

viewBinding.mainToolbarIcon.setImageDrawable(packageManager.getApplicationIcon(BuildConfig.APPLICATION_ID))
viewBinding.mainVersionText.text = getString(R.string.main_version_text).format("${BuildConfig.VERSION_NAME}-${BuildConfig.BUILD_TYPE}", BuildConfig.VERSION_CODE)
Expand Down Expand Up @@ -154,42 +154,48 @@ class Main: AppCompatActivity() {
}

private fun checkUpdate() {
lifecycleScope.launch(Dispatchers.Main) {
toast?.cancel()
toast = Toast.makeText(this@Main, getString(R.string.checking_for_updates), Toast.LENGTH_SHORT)
toast!!.show()
val latestStr = try {
Http.get("https://api.github.com/repos/WankkoRee/EnableWebViewDebugging/releases/latest")
} catch(e: Exception) {
Log.e(BuildConfig.APPLICATION_ID, getString(R.string.pull_failed).format(getString(R.string.latest_version)), e)
null
}
if (latestStr != null) {
val latest = GsonBuilder().setDateFormat("yyyy-MM-dd'T'HH:mm:ssZ").create().fromJson(latestStr, cn.wankkoree.xposed.enablewebviewdebugging.http.bean.api.github.RepoRelease::class.java)
Regex("^([0-9]+?)-(.+)\$").matchEntire(latest.tag_name)!!.groupValues.also {
val latestCode = it[1].toInt()
val latestName = it[2]
if (latestCode > BuildConfig.VERSION_CODE) {
AlertDialog.Builder(this@Main).apply {
setTitle(R.string.it_is_checked_that_there_is_a_new_version_do_you_want_to_download_it)
setMessage(getString(R.string.latest_version) + ":\n" + getString(R.string.version_format).format(latestName, latestCode) + "\n\n" + getString(R.string.update_log) + ":\n" + latest.body)
setPositiveButton(R.string.confirm) { _, _ ->
startActivity(Intent(Intent.ACTION_VIEW, Uri.parse(latest.html_url)))
toast?.cancel()
toast = Toast.makeText(this@Main, getString(R.string.checking_for_updates), Toast.LENGTH_SHORT)
toast!!.show()
Fuel.get("https://api.github.com/repos/WankkoRee/EnableWebViewDebugging/releases/latest")
.responseObject<cn.wankkoree.xposed.enablewebviewdebugging.http.bean.api.github.RepoRelease>(GsonBuilder().setDateFormat("yyyy-MM-dd'T'HH:mm:ssZ").create()) { _, _, result ->
result.fold({ latest ->
Regex("^([0-9]+?)-(.+)\$").matchEntire(latest.tag_name)!!.groupValues.also {
val latestCode = it[1].toInt()
val latestName = it[2]
if (latestCode > BuildConfig.VERSION_CODE) {
AlertDialog.Builder(this@Main).apply {
setTitle(R.string.it_is_checked_that_there_is_a_new_version_do_you_want_to_download_it)
setMessage("123")
setPositiveButton(R.string.confirm) { _, _ ->
startActivity(Intent(Intent.ACTION_VIEW, Uri.parse(latest.html_url)))
}
setNegativeButton(R.string.cancel) { _, _ -> }
}.show().also { dialog ->
markdown.setMarkdown(
dialog.findViewById(android.R.id.message)!!,
"""## ${getString(R.string.latest_version)}
|
|`${getString(R.string.version_format).format(latestName, latestCode)}`
|
|${latest.body}
""".trimMargin()
)
}
setNegativeButton(R.string.cancel) { _, _ -> }
}.create().show()
} else if (latestCode == BuildConfig.VERSION_CODE) {
toast?.cancel()
toast = Toast.makeText(this@Main, getString(R.string.is_the_latest_version), Toast.LENGTH_SHORT)
toast!!.show()
} else {
toast?.cancel()
toast = Toast.makeText(this@Main, getString(R.string.your_version_is_higher_than_the_latest_version_it_may_be_a_withdrawn_version_or_a_prerelease_version), Toast.LENGTH_SHORT)
toast!!.show()
} else if (latestCode == BuildConfig.VERSION_CODE) {
toast?.cancel()
toast = Toast.makeText(this@Main, getString(R.string.is_the_latest_version), Toast.LENGTH_SHORT)
toast!!.show()
} else {
toast?.cancel()
toast = Toast.makeText(this@Main, getString(R.string.your_version_is_higher_than_the_latest_version_it_may_be_a_withdrawn_version_or_a_prerelease_version), Toast.LENGTH_SHORT)
toast!!.show()
}
}
}
}, { e ->
Log.e(BuildConfig.APPLICATION_ID, getString(R.string.pull_failed).format(getString(R.string.latest_version)), e)
})
}
}
}

class AppsResultContract : ActivityResultContract<Unit, Unit>() {
Expand Down
Loading

0 comments on commit 9e23512

Please sign in to comment.