From bd82ba77734bb023ba1a49fda3a477ea11b7ca23 Mon Sep 17 00:00:00 2001 From: Steve Johnson Date: Tue, 31 Oct 2023 17:59:21 +0800 Subject: [PATCH] fix: avoid deprecated apis --- .../clash/MetaFeatureSettingsActivity.kt | 133 +++++++++--------- .../kr328/clash/design/ProfilesDesign.kt | 2 +- design/src/main/res/values-ja-rJP/strings.xml | 4 + design/src/main/res/values-ko-rKR/strings.xml | 4 + design/src/main/res/values-ru/strings.xml | 4 + design/src/main/res/values-zh-rHK/strings.xml | 4 + design/src/main/res/values-zh-rTW/strings.xml | 4 + design/src/main/res/values-zh/strings.xml | 4 + design/src/main/res/values/strings.xml | 4 + 9 files changed, 92 insertions(+), 71 deletions(-) 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 38112347..5478f1d0 100644 --- a/app/src/main/java/com/github/kr328/clash/MetaFeatureSettingsActivity.kt +++ b/app/src/main/java/com/github/kr328/clash/MetaFeatureSettingsActivity.kt @@ -1,25 +1,41 @@ 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.net.Uri +import android.os.Bundle import android.provider.OpenableColumns 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.withClash import com.google.android.material.dialog.MaterialAlertDialogBuilder +import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.isActive +import kotlinx.coroutines.launch import kotlinx.coroutines.selects.select +import kotlinx.coroutines.withContext import java.io.File import java.io.FileOutputStream -import kotlin.coroutines.resume 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) } @@ -50,36 +66,17 @@ class MetaFeatureSettingsActivity : BaseActivity() { clearOverride(Clash.OverrideSlot.Persist) } } - finish() } } MetaFeatureSettingsDesign.Request.ImportGeoIp -> { - val intent = Intent(Intent.ACTION_GET_CONTENT) - intent.type = "*/*" - intent.addCategory(Intent.CATEGORY_OPENABLE) - startActivityForResult( - intent, - MetaFeatureSettingsDesign.Request.ImportGeoIp.ordinal - ) + geoipDbImporter.launch("*/*") } MetaFeatureSettingsDesign.Request.ImportGeoSite -> { - val intent = Intent(Intent.ACTION_GET_CONTENT) - intent.type = "*/*" - intent.addCategory(Intent.CATEGORY_OPENABLE) - startActivityForResult( - intent, - MetaFeatureSettingsDesign.Request.ImportGeoSite.ordinal - ) + geositeDbImporter.launch("*/*") } MetaFeatureSettingsDesign.Request.ImportCountry -> { - val intent = Intent(Intent.ACTION_GET_CONTENT) - intent.type = "*/*" - intent.addCategory(Intent.CATEGORY_OPENABLE) - startActivityForResult( - intent, - MetaFeatureSettingsDesign.Request.ImportCountry.ordinal - ) + countryDbImporter.launch("*/*") } } } @@ -87,57 +84,53 @@ class MetaFeatureSettingsActivity : BaseActivity() { } } - public val validDatabaseExtensions = listOf( + private 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)) - { - 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 - } + private suspend fun geoFilesImported(uri: Uri?, importType: MetaFeatureSettingsDesign.Request) { + val cursor: Cursor? = uri?.let { + contentResolver.query(it, null, null, null, null, null) + } + cursor?.use { + if (it.moveToFirst()) { + val columnIndex = it.getColumnIndex(OpenableColumns.DISPLAY_NAME) + val displayName: String = + if (columnIndex != -1) it.getString(columnIndex) else ""; + val ext = "." + displayName.substringAfterLast(".") + + if (!validDatabaseExtensions.contains(ext)) { + MaterialAlertDialogBuilder(this) + .setTitle(R.string.geofile_unknown_db_format) + .setMessage(getString(R.string.geofile_unknown_db_format_message, + validDatabaseExtensions.joinToString("/"))) + .setPositiveButton("OK") { _, _ -> } + .show() + return + } + val outputFileName = when (importType) { + MetaFeatureSettingsDesign.Request.ImportGeoIp -> + "geoip$ext" + MetaFeatureSettingsDesign.Request.ImportGeoSite -> + "geosite$ext" + MetaFeatureSettingsDesign.Request.ImportCountry -> + "country$ext" + else -> "" + } + + withContext(Dispatchers.IO) { val outputFile = File(File(filesDir, "clash"), outputFileName); - contentResolver.openInputStream(uri).use { ins-> - FileOutputStream(outputFile).use { outs-> + 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, getString(R.string.geofile_imported, displayName), + Toast.LENGTH_LONG).show() + return } } - Toast.makeText(this, "Import failed", Toast.LENGTH_LONG).show() + Toast.makeText(this, R.string.geofile_import_failed, Toast.LENGTH_LONG).show() } } \ No newline at end of file diff --git a/design/src/main/java/com/github/kr328/clash/design/ProfilesDesign.kt b/design/src/main/java/com/github/kr328/clash/design/ProfilesDesign.kt index 24bfa40c..7d850af3 100644 --- a/design/src/main/java/com/github/kr328/clash/design/ProfilesDesign.kt +++ b/design/src/main/java/com/github/kr328/clash/design/ProfilesDesign.kt @@ -94,8 +94,8 @@ class ProfilesDesign(context: Context) : Design(context) fun requestUpdateAll() { allUpdating = true; - requests.trySend(Request.UpdateAll) changeUpdateAllButtonStatus() + requests.trySend(Request.UpdateAll) } fun finishUpdateAll() { diff --git a/design/src/main/res/values-ja-rJP/strings.xml b/design/src/main/res/values-ja-rJP/strings.xml index efbb34f0..1fe271ab 100644 --- a/design/src/main/res/values-ja-rJP/strings.xml +++ b/design/src/main/res/values-ja-rJP/strings.xml @@ -239,4 +239,8 @@ Press to import... Import GeoSite Database Import Country Database + Import failed + Unknown Database format + Only %1$s are supported + %1$s imported \ 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 3290535a..c2bfe116 100644 --- a/design/src/main/res/values-ko-rKR/strings.xml +++ b/design/src/main/res/values-ko-rKR/strings.xml @@ -239,4 +239,8 @@ Press to import... Import GeoSite Database Import Country Database + Import failed + Unknown Database format + Only %1$s are supported + %1$s imported \ 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 069fee33..f3dd8e12 100644 --- a/design/src/main/res/values-ru/strings.xml +++ b/design/src/main/res/values-ru/strings.xml @@ -304,4 +304,8 @@ Press to import... Import GeoSite Database Import Country Database + Import failed + Unknown Database format + Only %1$s are supported + %1$s imported diff --git a/design/src/main/res/values-zh-rHK/strings.xml b/design/src/main/res/values-zh-rHK/strings.xml index de2a3080..f7cd5c8c 100644 --- a/design/src/main/res/values-zh-rHK/strings.xml +++ b/design/src/main/res/values-zh-rHK/strings.xml @@ -236,4 +236,8 @@ Press to import... Import GeoSite Database Import Country Database + Import failed + Unknown Database format + Only %1$s are supported + %1$s imported \ 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 fcefb208..78b26c6c 100644 --- a/design/src/main/res/values-zh-rTW/strings.xml +++ b/design/src/main/res/values-zh-rTW/strings.xml @@ -236,4 +236,8 @@ Press to import... Import GeoSite Database Import Country Database + Import failed + Unknown Database format + Only %1$s are supported + %1$s imported diff --git a/design/src/main/res/values-zh/strings.xml b/design/src/main/res/values-zh/strings.xml index 98e27c6b..92437b8c 100644 --- a/design/src/main/res/values-zh/strings.xml +++ b/design/src/main/res/values-zh/strings.xml @@ -239,4 +239,8 @@ Press to import... 导入 GeoSite 数据库 导入 Country 数据库 + Import failed + Unknown Database format + Only %1$s are supported + %1$s imported \ 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 9a9232c0..bfca9884 100644 --- a/design/src/main/res/values/strings.xml +++ b/design/src/main/res/values/strings.xml @@ -304,4 +304,8 @@ Press to import... Import GeoSite Database Import Country Database + Import failed + Unknown Database format + Only %1$s are supported + %1$s imported