diff --git a/app/src/main/java/com/github/kr328/clash/MainApplication.kt b/app/src/main/java/com/github/kr328/clash/MainApplication.kt index 35226aa9..3cdfb5b5 100644 --- a/app/src/main/java/com/github/kr328/clash/MainApplication.kt +++ b/app/src/main/java/com/github/kr328/clash/MainApplication.kt @@ -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.remote.Remote import com.github.kr328.clash.service.util.sendServiceRecreated +import com.github.kr328.clash.util.clashDir import java.io.File import java.io.FileOutputStream import java.io.IOException @@ -38,7 +39,6 @@ class MainApplication : Application() { } private fun extractGeoFiles() { - val clashDir = File(filesDir, "clash") clashDir.mkdirs(); val geoipFile = File(clashDir, "geoip.metadb") diff --git a/app/src/main/java/com/github/kr328/clash/MetaFeatureSettingsActivity.kt b/app/src/main/java/com/github/kr328/clash/MetaFeatureSettingsActivity.kt index 5478f1d0..9708fcd7 100644 --- a/app/src/main/java/com/github/kr328/clash/MetaFeatureSettingsActivity.kt +++ b/app/src/main/java/com/github/kr328/clash/MetaFeatureSettingsActivity.kt @@ -8,6 +8,7 @@ import android.widget.Toast import androidx.activity.result.contract.ActivityResultContracts import com.github.kr328.clash.core.Clash import com.github.kr328.clash.design.MetaFeatureSettingsDesign +import com.github.kr328.clash.util.clashDir import com.github.kr328.clash.util.withClash import com.google.android.material.dialog.MaterialAlertDialogBuilder import kotlinx.coroutines.Dispatchers @@ -20,22 +21,6 @@ import java.io.FileOutputStream class MetaFeatureSettingsActivity : BaseActivity() { - - 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() { val configuration = withClash { queryOverride(Clash.OverrideSlot.Persist) } @@ -70,13 +55,22 @@ class MetaFeatureSettingsActivity : BaseActivity() { } } MetaFeatureSettingsDesign.Request.ImportGeoIp -> { - geoipDbImporter.launch("*/*") + val uri = startActivityForResult( + ActivityResultContracts.GetContent(), + "*/*") + geoFilesImported(uri, MetaFeatureSettingsDesign.Request.ImportGeoIp) } MetaFeatureSettingsDesign.Request.ImportGeoSite -> { - geositeDbImporter.launch("*/*") + val uri = startActivityForResult( + ActivityResultContracts.GetContent(), + "*/*") + geoFilesImported(uri, MetaFeatureSettingsDesign.Request.ImportGeoSite) } MetaFeatureSettingsDesign.Request.ImportCountry -> { - countryDbImporter.launch("*/*") + val uri = startActivityForResult( + ActivityResultContracts.GetContent(), + "*/*") + geoFilesImported(uri, MetaFeatureSettingsDesign.Request.ImportCountry) } } } @@ -119,7 +113,7 @@ class MetaFeatureSettingsActivity : BaseActivity() { } withContext(Dispatchers.IO) { - val outputFile = File(File(filesDir, "clash"), outputFileName); + val outputFile = File(clashDir, outputFileName); contentResolver.openInputStream(uri).use { ins -> FileOutputStream(outputFile).use { outs -> ins?.copyTo(outs) diff --git a/app/src/main/java/com/github/kr328/clash/util/Files.kt b/app/src/main/java/com/github/kr328/clash/util/Files.kt index b684a27a..9b696c8a 100644 --- a/app/src/main/java/com/github/kr328/clash/util/Files.kt +++ b/app/src/main/java/com/github/kr328/clash/util/Files.kt @@ -4,4 +4,7 @@ import android.content.Context import java.io.File val Context.logsDir: File - get() = cacheDir.resolve("logs") \ No newline at end of file + get() = cacheDir.resolve("logs") + +val Context.clashDir: File + get() = filesDir.resolve("clash") \ No newline at end of file