chore: code cleanup

This commit is contained in:
Steve Johnson 2023-11-02 10:21:25 +08:00
parent ad471f3837
commit 546378b9c4
3 changed files with 19 additions and 22 deletions

View File

@ -7,6 +7,7 @@ import com.github.kr328.clash.common.compat.currentProcessName
import com.github.kr328.clash.common.log.Log import com.github.kr328.clash.common.log.Log
import com.github.kr328.clash.remote.Remote import com.github.kr328.clash.remote.Remote
import com.github.kr328.clash.service.util.sendServiceRecreated import com.github.kr328.clash.service.util.sendServiceRecreated
import com.github.kr328.clash.util.clashDir
import java.io.File import java.io.File
import java.io.FileOutputStream import java.io.FileOutputStream
import java.io.IOException import java.io.IOException
@ -38,7 +39,6 @@ class MainApplication : Application() {
} }
private fun extractGeoFiles() { private fun extractGeoFiles() {
val clashDir = File(filesDir, "clash")
clashDir.mkdirs(); clashDir.mkdirs();
val geoipFile = File(clashDir, "geoip.metadb") val geoipFile = File(clashDir, "geoip.metadb")

View File

@ -8,6 +8,7 @@ import android.widget.Toast
import androidx.activity.result.contract.ActivityResultContracts import androidx.activity.result.contract.ActivityResultContracts
import com.github.kr328.clash.core.Clash import com.github.kr328.clash.core.Clash
import com.github.kr328.clash.design.MetaFeatureSettingsDesign import com.github.kr328.clash.design.MetaFeatureSettingsDesign
import com.github.kr328.clash.util.clashDir
import com.github.kr328.clash.util.withClash import com.github.kr328.clash.util.withClash
import com.google.android.material.dialog.MaterialAlertDialogBuilder import com.google.android.material.dialog.MaterialAlertDialogBuilder
import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.Dispatchers
@ -20,22 +21,6 @@ import java.io.FileOutputStream
class MetaFeatureSettingsActivity : BaseActivity<MetaFeatureSettingsDesign>() { class MetaFeatureSettingsActivity : BaseActivity<MetaFeatureSettingsDesign>() {
private val geoipDbImporter = registerForActivityResult(ActivityResultContracts.GetContent()){
launch{
geoFilesImported(it, MetaFeatureSettingsDesign.Request.ImportGeoIp)
}
}
private val geositeDbImporter = registerForActivityResult(ActivityResultContracts.GetContent()){
launch{
geoFilesImported(it, MetaFeatureSettingsDesign.Request.ImportGeoSite)
}
}
private val countryDbImporter = registerForActivityResult(ActivityResultContracts.GetContent()){
launch{
geoFilesImported(it, MetaFeatureSettingsDesign.Request.ImportCountry)
}
}
override suspend fun main() { override suspend fun main() {
val configuration = withClash { queryOverride(Clash.OverrideSlot.Persist) } val configuration = withClash { queryOverride(Clash.OverrideSlot.Persist) }
@ -70,13 +55,22 @@ class MetaFeatureSettingsActivity : BaseActivity<MetaFeatureSettingsDesign>() {
} }
} }
MetaFeatureSettingsDesign.Request.ImportGeoIp -> { MetaFeatureSettingsDesign.Request.ImportGeoIp -> {
geoipDbImporter.launch("*/*") val uri = startActivityForResult(
ActivityResultContracts.GetContent(),
"*/*")
geoFilesImported(uri, MetaFeatureSettingsDesign.Request.ImportGeoIp)
} }
MetaFeatureSettingsDesign.Request.ImportGeoSite -> { MetaFeatureSettingsDesign.Request.ImportGeoSite -> {
geositeDbImporter.launch("*/*") val uri = startActivityForResult(
ActivityResultContracts.GetContent(),
"*/*")
geoFilesImported(uri, MetaFeatureSettingsDesign.Request.ImportGeoSite)
} }
MetaFeatureSettingsDesign.Request.ImportCountry -> { MetaFeatureSettingsDesign.Request.ImportCountry -> {
countryDbImporter.launch("*/*") val uri = startActivityForResult(
ActivityResultContracts.GetContent(),
"*/*")
geoFilesImported(uri, MetaFeatureSettingsDesign.Request.ImportCountry)
} }
} }
} }
@ -119,7 +113,7 @@ class MetaFeatureSettingsActivity : BaseActivity<MetaFeatureSettingsDesign>() {
} }
withContext(Dispatchers.IO) { withContext(Dispatchers.IO) {
val outputFile = File(File(filesDir, "clash"), outputFileName); val outputFile = File(clashDir, outputFileName);
contentResolver.openInputStream(uri).use { ins -> contentResolver.openInputStream(uri).use { ins ->
FileOutputStream(outputFile).use { outs -> FileOutputStream(outputFile).use { outs ->
ins?.copyTo(outs) ins?.copyTo(outs)

View File

@ -4,4 +4,7 @@ import android.content.Context
import java.io.File import java.io.File
val Context.logsDir: File val Context.logsDir: File
get() = cacheDir.resolve("logs") get() = cacheDir.resolve("logs")
val Context.clashDir: File
get() = filesDir.resolve("clash")