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 fd1f0164c7..0bc7684a7a 100644 --- a/app/src/main/java/com/github/kr328/clash/MetaFeatureSettingsActivity.kt +++ b/app/src/main/java/com/github/kr328/clash/MetaFeatureSettingsActivity.kt @@ -1,10 +1,23 @@ package com.github.kr328.clash +import android.R +import android.annotation.SuppressLint +import android.content.Context +import android.content.DialogInterface +import android.content.Intent +import android.database.Cursor +import android.provider.OpenableColumns +import android.widget.Toast import com.github.kr328.clash.core.Clash import com.github.kr328.clash.design.MetaFeatureSettingsDesign import com.github.kr328.clash.util.withClash +import com.google.android.material.dialog.MaterialAlertDialogBuilder import kotlinx.coroutines.isActive import kotlinx.coroutines.selects.select +import java.io.File +import java.io.FileOutputStream +import kotlin.coroutines.resume + class MetaFeatureSettingsActivity : BaseActivity() { override suspend fun main() { @@ -41,9 +54,90 @@ class MetaFeatureSettingsActivity : BaseActivity() { finish() } } + MetaFeatureSettingsDesign.Request.ImportGeoIp -> { + val intent = Intent(Intent.ACTION_GET_CONTENT) + intent.type = "*/*" + intent.addCategory(Intent.CATEGORY_OPENABLE) + startActivityForResult( + intent, + MetaFeatureSettingsDesign.Request.ImportGeoIp.ordinal + ) + } + MetaFeatureSettingsDesign.Request.ImportGeoSite -> { + val intent = Intent(Intent.ACTION_GET_CONTENT) + intent.type = "*/*" + intent.addCategory(Intent.CATEGORY_OPENABLE) + startActivityForResult( + intent, + MetaFeatureSettingsDesign.Request.ImportGeoSite.ordinal + ) + } + MetaFeatureSettingsDesign.Request.ImportCountry -> { + val intent = Intent(Intent.ACTION_GET_CONTENT) + intent.type = "*/*" + intent.addCategory(Intent.CATEGORY_OPENABLE) + startActivityForResult( + intent, + MetaFeatureSettingsDesign.Request.ImportCountry.ordinal + ) + } + } + } + } + } + } + + public val validDatabaseExtensions = listOf( + ".metadb", ".db", ".dat", ".mmdb" + ) + @SuppressLint("Range") + override fun onActivityResult(requestCode: Int, resultCode: Int, resultData: Intent?) { + super.onActivityResult(requestCode, resultCode, resultData) + if(resultCode == RESULT_OK) { + val uri = resultData?.data + val cursor: Cursor? = uri?.let { + contentResolver.query(it, null, null, null, null, null) + } + cursor?.use { + if (it.moveToFirst()) { + val displayName: String = + it.getString(it.getColumnIndex(OpenableColumns.DISPLAY_NAME)) + val ext = "." + displayName.substringAfterLast(".") + if(!validDatabaseExtensions.contains(ext)) + { + val dialog = MaterialAlertDialogBuilder(this) + .setTitle("Unknown Database Format") + .setMessage("Only ${validDatabaseExtensions.joinToString("/")} are supported") + .setPositiveButton("OK"){ _, _ -> } + .show() + return + } + val outputFileName = when (requestCode) { + MetaFeatureSettingsDesign.Request.ImportGeoIp.ordinal -> + "geoip$ext" + MetaFeatureSettingsDesign.Request.ImportGeoSite.ordinal -> + "geosite$ext" + MetaFeatureSettingsDesign.Request.ImportCountry.ordinal -> + "country$ext" + else -> "" + } + if(outputFileName.isEmpty()) + { + Toast.makeText(this, "Bad request", Toast.LENGTH_LONG).show() + return + } + + val outputFile = File(File(filesDir, "clash"), outputFileName); + contentResolver.openInputStream(uri).use { ins-> + FileOutputStream(outputFile).use { outs-> + ins?.copyTo(outs) + } } + Toast.makeText(this, "$displayName imported", Toast.LENGTH_LONG).show() + return } } } + Toast.makeText(this, "Import failed", Toast.LENGTH_LONG).show() } } \ No newline at end of file diff --git a/design/src/main/java/com/github/kr328/clash/design/MetaFeatureSettingsDesign.kt b/design/src/main/java/com/github/kr328/clash/design/MetaFeatureSettingsDesign.kt index de928ded76..099bf9a2a4 100644 --- a/design/src/main/java/com/github/kr328/clash/design/MetaFeatureSettingsDesign.kt +++ b/design/src/main/java/com/github/kr328/clash/design/MetaFeatureSettingsDesign.kt @@ -15,7 +15,7 @@ class MetaFeatureSettingsDesign( configuration: ConfigurationOverride ) : Design(context) { enum class Request { - ResetOverride + ResetOverride, ImportGeoIp, ImportGeoSite, ImportCountry } private val binding = DesignSettingsMetaFeatureBinding @@ -198,6 +198,36 @@ class MetaFeatureSettingsDesign( ) sniffer.listener?.onChanged() + + category(R.string.geox_files) + + clickable ( + title = R.string.import_geoip_file, + summary = R.string.press_to_import, + ){ + clicked { + requests.trySend(Request.ImportGeoIp) + } + } + + clickable ( + title = R.string.import_geosite_file, + summary = R.string.press_to_import, + ){ + clicked { + requests.trySend(Request.ImportGeoSite) + } + } + + clickable ( + title = R.string.import_country_file, + summary = R.string.press_to_import, + ){ + clicked { + requests.trySend(Request.ImportCountry) + } + } + /* category(R.string.geox_url_setting) diff --git a/design/src/main/res/values-ja-rJP/strings.xml b/design/src/main/res/values-ja-rJP/strings.xml index ff349bd486..efbb34f0b8 100644 --- a/design/src/main/res/values-ja-rJP/strings.xml +++ b/design/src/main/res/values-ja-rJP/strings.xml @@ -234,4 +234,9 @@ GeoIP URL MMDB URL Geosite URL + Geo Files + Import GeoIP Database + Press to import... + Import GeoSite Database + Import Country Database \ No newline at end of file diff --git a/design/src/main/res/values-ko-rKR/strings.xml b/design/src/main/res/values-ko-rKR/strings.xml index b5381f6fdb..3290535a82 100644 --- a/design/src/main/res/values-ko-rKR/strings.xml +++ b/design/src/main/res/values-ko-rKR/strings.xml @@ -234,4 +234,9 @@ GeoIP Url MMDB Url Geosite Url + Geo Files + Import GeoIP Database + Press to import... + Import GeoSite Database + Import Country Database \ No newline at end of file diff --git a/design/src/main/res/values-ru/strings.xml b/design/src/main/res/values-ru/strings.xml index e81ba0c4d7..069fee33b7 100644 --- a/design/src/main/res/values-ru/strings.xml +++ b/design/src/main/res/values-ru/strings.xml @@ -299,4 +299,9 @@ https://raw.githubusercontent.com/Loyalsoldier/v2ray-rules-dat/release/geoip.dat https://raw.githubusercontent.com/Loyalsoldier/geoip/release/Country.mmdb https://raw.githubusercontent.com/Loyalsoldier/v2ray-rules-dat/release/geosite.dat + Geo Files + Import GeoIP Database + Press to import... + Import GeoSite Database + Import Country Database diff --git a/design/src/main/res/values-zh-rHK/strings.xml b/design/src/main/res/values-zh-rHK/strings.xml index 3ac6d9ee8d..de2a308037 100644 --- a/design/src/main/res/values-zh-rHK/strings.xml +++ b/design/src/main/res/values-zh-rHK/strings.xml @@ -231,4 +231,9 @@ Geosite Url Prefer h3 Port Whitelist + Geo Files + Import GeoIP Database + Press to import... + Import GeoSite Database + Import Country Database \ No newline at end of file diff --git a/design/src/main/res/values-zh-rTW/strings.xml b/design/src/main/res/values-zh-rTW/strings.xml index dee570032f..fcefb20852 100644 --- a/design/src/main/res/values-zh-rTW/strings.xml +++ b/design/src/main/res/values-zh-rTW/strings.xml @@ -231,4 +231,9 @@ Geosite Url Prefer h3 Port Whitelist + Geo Files + Import GeoIP Database + Press to import... + Import GeoSite Database + Import Country Database diff --git a/design/src/main/res/values-zh/strings.xml b/design/src/main/res/values-zh/strings.xml index 8fc03d6dee..98e27c6bca 100644 --- a/design/src/main/res/values-zh/strings.xml +++ b/design/src/main/res/values-zh/strings.xml @@ -234,4 +234,9 @@ Geosite Url Prefer h3 Port Whitelist + Geo Files + 导入 GeoIP 数据库 + Press to import... + 导入 GeoSite 数据库 + 导入 Country 数据库 \ No newline at end of file diff --git a/design/src/main/res/values/strings.xml b/design/src/main/res/values/strings.xml index ba76f44b9b..9a9232c082 100644 --- a/design/src/main/res/values/strings.xml +++ b/design/src/main/res/values/strings.xml @@ -299,4 +299,9 @@ https://raw.githubusercontent.com/MetaCubeX/meta-rules-dat/release/geoip.dat https://raw.githubusercontent.com/MetaCubeX/meta-rules-dat/release/country.mmdb https://raw.githubusercontent.com/MetaCubeX/meta-rules-dat/release/geosite.dat + Geo Files + Import GeoIP Database + Press to import... + Import GeoSite Database + Import Country Database