MetaFeatureSettingsActivity.kt 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. package com.github.kr328.clash
  2. import android.database.Cursor
  3. import android.net.Uri
  4. import android.os.Bundle
  5. import android.provider.OpenableColumns
  6. import android.widget.Toast
  7. import androidx.activity.result.contract.ActivityResultContracts
  8. import com.github.kr328.clash.core.Clash
  9. import com.github.kr328.clash.design.MetaFeatureSettingsDesign
  10. import com.github.kr328.clash.util.clashDir
  11. import com.github.kr328.clash.util.withClash
  12. import com.google.android.material.dialog.MaterialAlertDialogBuilder
  13. import kotlinx.coroutines.Dispatchers
  14. import kotlinx.coroutines.isActive
  15. import kotlinx.coroutines.launch
  16. import kotlinx.coroutines.selects.select
  17. import kotlinx.coroutines.withContext
  18. import java.io.File
  19. import java.io.FileOutputStream
  20. class MetaFeatureSettingsActivity : BaseActivity<MetaFeatureSettingsDesign>() {
  21. override suspend fun main() {
  22. val configuration = withClash { queryOverride(Clash.OverrideSlot.Persist) }
  23. defer {
  24. withClash {
  25. patchOverride(Clash.OverrideSlot.Persist, configuration)
  26. }
  27. }
  28. val design = MetaFeatureSettingsDesign(
  29. this,
  30. configuration
  31. )
  32. setContentDesign(design)
  33. while (isActive) {
  34. select<Unit> {
  35. events.onReceive {
  36. }
  37. design.requests.onReceive {
  38. when (it) {
  39. MetaFeatureSettingsDesign.Request.ResetOverride -> {
  40. if (design.requestResetConfirm()) {
  41. defer {
  42. withClash {
  43. clearOverride(Clash.OverrideSlot.Persist)
  44. }
  45. }
  46. finish()
  47. }
  48. }
  49. MetaFeatureSettingsDesign.Request.ImportGeoIp -> {
  50. val uri = startActivityForResult(
  51. ActivityResultContracts.GetContent(),
  52. "*/*")
  53. geoFilesImported(uri, MetaFeatureSettingsDesign.Request.ImportGeoIp)
  54. }
  55. MetaFeatureSettingsDesign.Request.ImportGeoSite -> {
  56. val uri = startActivityForResult(
  57. ActivityResultContracts.GetContent(),
  58. "*/*")
  59. geoFilesImported(uri, MetaFeatureSettingsDesign.Request.ImportGeoSite)
  60. }
  61. MetaFeatureSettingsDesign.Request.ImportCountry -> {
  62. val uri = startActivityForResult(
  63. ActivityResultContracts.GetContent(),
  64. "*/*")
  65. geoFilesImported(uri, MetaFeatureSettingsDesign.Request.ImportCountry)
  66. }
  67. }
  68. }
  69. }
  70. }
  71. }
  72. private val validDatabaseExtensions = listOf(
  73. ".metadb", ".db", ".dat", ".mmdb"
  74. )
  75. private suspend fun geoFilesImported(uri: Uri?, importType: MetaFeatureSettingsDesign.Request) {
  76. val cursor: Cursor? = uri?.let {
  77. contentResolver.query(it, null, null, null, null, null)
  78. }
  79. cursor?.use {
  80. if (it.moveToFirst()) {
  81. val columnIndex = it.getColumnIndex(OpenableColumns.DISPLAY_NAME)
  82. val displayName: String =
  83. if (columnIndex != -1) it.getString(columnIndex) else "";
  84. val ext = "." + displayName.substringAfterLast(".")
  85. if (!validDatabaseExtensions.contains(ext)) {
  86. MaterialAlertDialogBuilder(this)
  87. .setTitle(R.string.geofile_unknown_db_format)
  88. .setMessage(getString(R.string.geofile_unknown_db_format_message,
  89. validDatabaseExtensions.joinToString("/")))
  90. .setPositiveButton("OK") { _, _ -> }
  91. .show()
  92. return
  93. }
  94. val outputFileName = when (importType) {
  95. MetaFeatureSettingsDesign.Request.ImportGeoIp ->
  96. "geoip$ext"
  97. MetaFeatureSettingsDesign.Request.ImportGeoSite ->
  98. "geosite$ext"
  99. MetaFeatureSettingsDesign.Request.ImportCountry ->
  100. "country$ext"
  101. else -> ""
  102. }
  103. withContext(Dispatchers.IO) {
  104. val outputFile = File(clashDir, outputFileName);
  105. contentResolver.openInputStream(uri).use { ins ->
  106. FileOutputStream(outputFile).use { outs ->
  107. ins?.copyTo(outs)
  108. }
  109. }
  110. }
  111. Toast.makeText(this, getString(R.string.geofile_imported, displayName),
  112. Toast.LENGTH_LONG).show()
  113. return
  114. }
  115. }
  116. Toast.makeText(this, R.string.geofile_import_failed, Toast.LENGTH_LONG).show()
  117. }
  118. }