Skip to content

Commit

Permalink
support importing local geofile
Browse files Browse the repository at this point in the history
  • Loading branch information
stevejohnson7 committed Oct 29, 2023
1 parent ec41029 commit 574fba8
Show file tree
Hide file tree
Showing 9 changed files with 160 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -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<MetaFeatureSettingsDesign>() {
override suspend fun main() {
Expand Down Expand Up @@ -41,9 +54,90 @@ class MetaFeatureSettingsActivity : BaseActivity<MetaFeatureSettingsDesign>() {
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()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class MetaFeatureSettingsDesign(
configuration: ConfigurationOverride
) : Design<MetaFeatureSettingsDesign.Request>(context) {
enum class Request {
ResetOverride
ResetOverride, ImportGeoIp, ImportGeoSite, ImportCountry
}

private val binding = DesignSettingsMetaFeatureBinding
Expand Down Expand Up @@ -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)
Expand Down
5 changes: 5 additions & 0 deletions design/src/main/res/values-ja-rJP/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -234,4 +234,9 @@
<string name="geox_geoip">GeoIP URL</string>
<string name="geox_mmdb">MMDB URL</string>
<string name="geox_geosite">Geosite URL</string>
<string name="geox_files" >Geo Files</string>
<string name="import_geoip_file">Import GeoIP Database</string>
<string name="press_to_import">Press to import...</string>
<string name="import_geosite_file">Import GeoSite Database</string>
<string name="import_country_file">Import Country Database</string>
</resources>
5 changes: 5 additions & 0 deletions design/src/main/res/values-ko-rKR/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -234,4 +234,9 @@
<string name="geox_geoip">GeoIP Url</string>
<string name="geox_mmdb">MMDB Url</string>
<string name="geox_geosite">Geosite Url</string>
<string name="geox_files" >Geo Files</string>
<string name="import_geoip_file">Import GeoIP Database</string>
<string name="press_to_import">Press to import...</string>
<string name="import_geosite_file">Import GeoSite Database</string>
<string name="import_country_file">Import Country Database</string>
</resources>
5 changes: 5 additions & 0 deletions design/src/main/res/values-ru/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -299,4 +299,9 @@
<string name="geoip_url" translatable="false">https://raw.githubusercontent.com/Loyalsoldier/v2ray-rules-dat/release/geoip.dat</string>
<string name="mmdb_url" translatable="false">https://raw.githubusercontent.com/Loyalsoldier/geoip/release/Country.mmdb</string>
<string name="geosite_url" translatable="false">https://raw.githubusercontent.com/Loyalsoldier/v2ray-rules-dat/release/geosite.dat</string>
<string name="geox_files" >Geo Files</string>
<string name="import_geoip_file">Import GeoIP Database</string>
<string name="press_to_import">Press to import...</string>
<string name="import_geosite_file">Import GeoSite Database</string>
<string name="import_country_file">Import Country Database</string>
</resources>
5 changes: 5 additions & 0 deletions design/src/main/res/values-zh-rHK/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -231,4 +231,9 @@
<string name="geox_geosite">Geosite Url</string>
<string name="prefer_h3">Prefer h3</string>
<string name="port_whitelist">Port Whitelist</string>
<string name="geox_files" >Geo Files</string>
<string name="import_geoip_file">Import GeoIP Database</string>
<string name="press_to_import">Press to import...</string>
<string name="import_geosite_file">Import GeoSite Database</string>
<string name="import_country_file">Import Country Database</string>
</resources>
5 changes: 5 additions & 0 deletions design/src/main/res/values-zh-rTW/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -231,4 +231,9 @@
<string name="geox_geosite">Geosite Url</string>
<string name="prefer_h3">Prefer h3</string>
<string name="port_whitelist">Port Whitelist</string>
<string name="geox_files" >Geo Files</string>
<string name="import_geoip_file">Import GeoIP Database</string>
<string name="press_to_import">Press to import...</string>
<string name="import_geosite_file">Import GeoSite Database</string>
<string name="import_country_file">Import Country Database</string>
</resources>
5 changes: 5 additions & 0 deletions design/src/main/res/values-zh/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -234,4 +234,9 @@
<string name="geox_geosite">Geosite Url</string>
<string name="prefer_h3">Prefer h3</string>
<string name="port_whitelist">Port Whitelist</string>
<string name="geox_files" >Geo Files</string>
<string name="import_geoip_file">导入 GeoIP 数据库</string>
<string name="press_to_import">Press to import...</string>
<string name="import_geosite_file">导入 GeoSite 数据库</string>
<string name="import_country_file">导入 Country 数据库</string>
</resources>
5 changes: 5 additions & 0 deletions design/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -299,4 +299,9 @@
<string name="geoip_url" translatable="false">https://raw.githubusercontent.com/MetaCubeX/meta-rules-dat/release/geoip.dat</string>
<string name="mmdb_url" translatable="false">https://raw.githubusercontent.com/MetaCubeX/meta-rules-dat/release/country.mmdb</string>
<string name="geosite_url" translatable="false">https://raw.githubusercontent.com/MetaCubeX/meta-rules-dat/release/geosite.dat</string>
<string name="geox_files" >Geo Files</string>
<string name="import_geoip_file">Import GeoIP Database</string>
<string name="press_to_import">Press to import...</string>
<string name="import_geosite_file">Import GeoSite Database</string>
<string name="import_country_file">Import Country Database</string>
</resources>

0 comments on commit 574fba8

Please sign in to comment.