diff --git a/V2rayNG/app/src/main/kotlin/com/v2ray/ang/AppConfig.kt b/V2rayNG/app/src/main/kotlin/com/v2ray/ang/AppConfig.kt index 2e892556..7295a15c 100644 --- a/V2rayNG/app/src/main/kotlin/com/v2ray/ang/AppConfig.kt +++ b/V2rayNG/app/src/main/kotlin/com/v2ray/ang/AppConfig.kt @@ -7,6 +7,7 @@ package com.v2ray.ang object AppConfig { const val ANG_PACKAGE = BuildConfig.APPLICATION_ID const val DIR_ASSETS = "assets" + const val DIR_BACKUPS = "backups" // legacy const val ANG_CONFIG = "ang_config" diff --git a/V2rayNG/app/src/main/kotlin/com/v2ray/ang/ui/AboutActivity.kt b/V2rayNG/app/src/main/kotlin/com/v2ray/ang/ui/AboutActivity.kt index 22baf988..6c1637cc 100644 --- a/V2rayNG/app/src/main/kotlin/com/v2ray/ang/ui/AboutActivity.kt +++ b/V2rayNG/app/src/main/kotlin/com/v2ray/ang/ui/AboutActivity.kt @@ -1,15 +1,27 @@ package com.v2ray.ang.ui +import android.Manifest +import android.content.Intent +import android.os.Build import android.os.Bundle +import androidx.activity.result.contract.ActivityResultContracts +import com.tbruyelle.rxpermissions.RxPermissions +import com.tencent.mmkv.MMKV import com.v2ray.ang.AppConfig import com.v2ray.ang.BuildConfig import com.v2ray.ang.R import com.v2ray.ang.databinding.ActivityAboutBinding +import com.v2ray.ang.extension.toast import com.v2ray.ang.util.SpeedtestUtil import com.v2ray.ang.util.Utils +import com.v2ray.ang.util.ZipUtil +import java.io.File +import java.text.SimpleDateFormat +import java.util.Locale class AboutActivity : BaseActivity() { private lateinit var binding: ActivityAboutBinding + private val extDir by lazy { File(Utils.backupPath(this)) } override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) @@ -19,6 +31,31 @@ class AboutActivity : BaseActivity() { title = getString(R.string.title_about) + binding.tvBackupSummary.text = this.getString(R.string.summary_configuration_backup, extDir) + binding.layoutBackup.setOnClickListener { + backupMMKV() + } + + binding.layoutRestore.setOnClickListener { + val permission = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) { + Manifest.permission.READ_MEDIA_IMAGES + } else { + Manifest.permission.READ_EXTERNAL_STORAGE + } + RxPermissions(this) + .request(permission) + .subscribe { + if (it) { + try { + showFileChooser() + } catch (e: Exception) { + e.printStackTrace() + } + } else + toast(R.string.toast_permission_denied) + } + } + binding.layoutSoureCcode.setOnClickListener { Utils.openUri(this, AppConfig.v2rayNGUrl) } @@ -38,9 +75,79 @@ class AboutActivity : BaseActivity() { "v${BuildConfig.VERSION_NAME} (${SpeedtestUtil.getLibVersion()})".also { binding.tvVersion.text = it } - - } + fun backupMMKV() { + val dateFormated = SimpleDateFormat( + "yyyy-MM-dd-HH-mm-ss", + Locale.getDefault() + ).format(System.currentTimeMillis()) + val folderName = "${getString(R.string.app_name)}_${dateFormated}" + val backupDir = this.cacheDir.absolutePath + "/$folderName" + val outputZipFilePath = extDir.absolutePath + "/$folderName.zip" -} + val count = MMKV.backupAllToDirectory(backupDir) + if (count <= 0) { + toast(R.string.toast_failure) + } + + if (ZipUtil.zipFromFolder(backupDir, outputZipFilePath)) { + toast(R.string.toast_success) + } else { + toast(R.string.toast_failure) + } + } + + fun restoreMMKV(zipFile: File) { + val backupDir = this.cacheDir.absolutePath + "/${System.currentTimeMillis()}" + + if (!ZipUtil.unzipToFolder(zipFile, backupDir)) { + toast(R.string.toast_failure) + } + + val count = MMKV.restoreAllFromDirectory(backupDir) + if (count > 0) { + toast(R.string.toast_success) + } else { + toast(R.string.toast_failure) + } + } + + private fun showFileChooser() { + val intent = Intent(Intent.ACTION_GET_CONTENT) + intent.type = "*/*" + intent.addCategory(Intent.CATEGORY_OPENABLE) + + try { + chooseFile.launch(Intent.createChooser(intent, getString(R.string.title_file_chooser))) + } catch (ex: android.content.ActivityNotFoundException) { + toast(R.string.toast_require_file_manager) + } + } + + private val chooseFile = + registerForActivityResult(ActivityResultContracts.StartActivityForResult()) { + val uri = it.data?.data + if (it.resultCode == RESULT_OK && uri != null) { + try { + try { + val targetFile = + File(this.cacheDir.absolutePath, "${System.currentTimeMillis()}.zip") + contentResolver.openInputStream(uri).use { input -> + targetFile.outputStream().use { fileOut -> + input?.copyTo(fileOut) + } + } + + restoreMMKV(targetFile) + } catch (e: Exception) { + e.printStackTrace() + } + + } catch (e: Exception) { + e.printStackTrace() + toast(e.message.toString()) + } + } + } +} \ No newline at end of file diff --git a/V2rayNG/app/src/main/kotlin/com/v2ray/ang/util/Utils.kt b/V2rayNG/app/src/main/kotlin/com/v2ray/ang/util/Utils.kt index 47153633..0fdc564f 100644 --- a/V2rayNG/app/src/main/kotlin/com/v2ray/ang/util/Utils.kt +++ b/V2rayNG/app/src/main/kotlin/com/v2ray/ang/util/Utils.kt @@ -1,11 +1,8 @@ package com.v2ray.ang.util +import android.content.ClipData import android.content.ClipboardManager import android.content.Context -import android.text.Editable -import android.util.Base64 -import java.util.* -import android.content.ClipData import android.content.Intent import android.content.pm.PackageManager import android.content.res.Configuration.UI_MODE_NIGHT_MASK @@ -14,6 +11,8 @@ import android.net.Uri import android.os.Build import android.os.LocaleList import android.provider.Settings +import android.text.Editable +import android.util.Base64 import android.util.Log import android.util.Patterns import android.webkit.URLUtil @@ -23,9 +22,10 @@ import com.v2ray.ang.AppConfig.ANG_PACKAGE import com.v2ray.ang.BuildConfig import com.v2ray.ang.R import com.v2ray.ang.extension.toast -import java.net.* import com.v2ray.ang.service.V2RayServiceManager import java.io.IOException +import java.net.* +import java.util.* object Utils { @@ -318,6 +318,14 @@ object Utils { return extDir.absolutePath } + fun backupPath(context: Context?): String { + if (context == null) + return "" + val extDir = context.getExternalFilesDir(AppConfig.DIR_BACKUPS) + ?: return context.getDir(AppConfig.DIR_BACKUPS, 0).absolutePath + return extDir.absolutePath + } + fun getDeviceIdForXUDPBaseKey(): String { val androidId = Settings.Secure.ANDROID_ID.toByteArray(charset("UTF-8")) return Base64.encodeToString(androidId.copyOf(32), Base64.NO_PADDING.or(Base64.URL_SAFE)) diff --git a/V2rayNG/app/src/main/kotlin/com/v2ray/ang/util/ZipUtil.kt b/V2rayNG/app/src/main/kotlin/com/v2ray/ang/util/ZipUtil.kt new file mode 100644 index 00000000..5343f6d8 --- /dev/null +++ b/V2rayNG/app/src/main/kotlin/com/v2ray/ang/util/ZipUtil.kt @@ -0,0 +1,102 @@ +package com.v2ray.ang.util + +import java.io.BufferedOutputStream +import java.io.File +import java.io.FileInputStream +import java.io.FileOutputStream +import java.io.IOException +import java.io.InputStream +import java.util.zip.ZipEntry +import java.util.zip.ZipFile +import java.util.zip.ZipOutputStream + +object ZipUtil { + private const val BUFFER_SIZE = 4096 + + @Throws(IOException::class) + fun zipFromFolder(folderPath: String, outputZipFilePath: String): Boolean { + val buffer = ByteArray(BUFFER_SIZE) + + try { + if (folderPath.isEmpty() || outputZipFilePath.isEmpty()) { + return false + } + + val filesToCompress = ArrayList() + val directory = File(folderPath) + if (directory.isDirectory) { + directory.listFiles()?.forEach { + if (it.isFile) { + filesToCompress.add(it.absolutePath) + } + } + } + if (filesToCompress.isEmpty()) { + return false + } + + val zos = ZipOutputStream(FileOutputStream(outputZipFilePath)) + + filesToCompress.forEach { file -> + val ze = ZipEntry(File(file).name) + zos.putNextEntry(ze) + val inputStream = FileInputStream(file) + while (true) { + val len = inputStream.read(buffer) + if (len <= 0) break + zos.write(buffer, 0, len) + } + + inputStream.close() + } + + zos.closeEntry() + zos.close() + } catch (e: Exception) { + e.printStackTrace() + return false + } + return true + } + + @Throws(IOException::class) + fun unzipToFolder(zipFile: File, destDirectory: String): Boolean { + File(destDirectory).run { + if (!exists()) { + mkdirs() + } + } + try { + ZipFile(zipFile).use { zip -> + zip.entries().asSequence().forEach { entry -> + zip.getInputStream(entry).use { input -> + val filePath = destDirectory + File.separator + entry.name + if (!entry.isDirectory) { + // if the entry is a file, extracts it + extractFile(input, filePath) + } else { + // if the entry is a directory, make the directory + val dir = File(filePath) + dir.mkdir() + } + } + } + } + } catch (e: Exception) { + e.printStackTrace() + return false + } + return true + } + + @Throws(IOException::class) + private fun extractFile(inputStream: InputStream, destFilePath: String) { + val bos = BufferedOutputStream(FileOutputStream(destFilePath)) + val bytesIn = ByteArray(BUFFER_SIZE) + var read: Int + while (inputStream.read(bytesIn).also { read = it } != -1) { + bos.write(bytesIn, 0, read) + } + bos.close() + } +} \ No newline at end of file diff --git a/V2rayNG/app/src/main/res/drawable-night/ic_backup_24dp.xml b/V2rayNG/app/src/main/res/drawable-night/ic_backup_24dp.xml new file mode 100644 index 00000000..9f774c85 --- /dev/null +++ b/V2rayNG/app/src/main/res/drawable-night/ic_backup_24dp.xml @@ -0,0 +1,9 @@ + + + diff --git a/V2rayNG/app/src/main/res/drawable-night/ic_restore_24dp.xml b/V2rayNG/app/src/main/res/drawable-night/ic_restore_24dp.xml new file mode 100644 index 00000000..6b372816 --- /dev/null +++ b/V2rayNG/app/src/main/res/drawable-night/ic_restore_24dp.xml @@ -0,0 +1,9 @@ + + + diff --git a/V2rayNG/app/src/main/res/drawable/ic_backup_24dp.xml b/V2rayNG/app/src/main/res/drawable/ic_backup_24dp.xml new file mode 100644 index 00000000..0eca2402 --- /dev/null +++ b/V2rayNG/app/src/main/res/drawable/ic_backup_24dp.xml @@ -0,0 +1,9 @@ + + + diff --git a/V2rayNG/app/src/main/res/drawable/ic_restore_24dp.xml b/V2rayNG/app/src/main/res/drawable/ic_restore_24dp.xml new file mode 100644 index 00000000..12d78b2c --- /dev/null +++ b/V2rayNG/app/src/main/res/drawable/ic_restore_24dp.xml @@ -0,0 +1,9 @@ + + + diff --git a/V2rayNG/app/src/main/res/layout/activity_about.xml b/V2rayNG/app/src/main/res/layout/activity_about.xml index d139ea8d..24f8279a 100644 --- a/V2rayNG/app/src/main/res/layout/activity_about.xml +++ b/V2rayNG/app/src/main/res/layout/activity_about.xml @@ -7,10 +7,86 @@ + + + + + + + + + + + + + + + + + + + + + + + + + android:textAppearance="@style/TextAppearance.AppCompat.Tooltip" /> + android:textAppearance="@style/TextAppearance.AppCompat.Tooltip" /> @@ -89,8 +163,7 @@ android:layout_height="wrap_content" android:paddingStart="16dp" android:text="@string/title_tg_channel" - android:textAppearance="@style/TextAppearance.AppCompat.Tooltip" - android:textColor="@color/color_secondary" /> + android:textAppearance="@style/TextAppearance.AppCompat.Tooltip" /> + android:textAppearance="@style/TextAppearance.AppCompat.Tooltip" /> + android:textAppearance="@style/TextAppearance.AppCompat.Tooltip" /> diff --git a/V2rayNG/app/src/main/res/values-ar/strings.xml b/V2rayNG/app/src/main/res/values-ar/strings.xml index 9386cee1..d317d913 100644 --- a/V2rayNG/app/src/main/res/values-ar/strings.xml +++ b/V2rayNG/app/src/main/res/values-ar/strings.xml @@ -149,6 +149,9 @@ About Source code Telegram channel + Backup configuration + Backup storage location: [%s], It will be cleared when the app is uninstalled + Restore configuration ترقية ترقية، انقر للحصول على التفاصيل (يمكن إزالة التبرع) اشتراكات التحديث التلقائي diff --git a/V2rayNG/app/src/main/res/values-fa/strings.xml b/V2rayNG/app/src/main/res/values-fa/strings.xml index cd893706..bd2d63a1 100644 --- a/V2rayNG/app/src/main/res/values-fa/strings.xml +++ b/V2rayNG/app/src/main/res/values-fa/strings.xml @@ -173,6 +173,9 @@ About Source code Telegram channel + Backup configuration + Backup storage location: [%s], It will be cleared when the app is uninstalled + Restore configuration تبلیغات تبلیغات، برای جزئیات بیشتر کلیک کنید (کمک مالی کنید تا حذف شود) diff --git a/V2rayNG/app/src/main/res/values-ru/strings.xml b/V2rayNG/app/src/main/res/values-ru/strings.xml index ad6095a9..482e258f 100644 --- a/V2rayNG/app/src/main/res/values-ru/strings.xml +++ b/V2rayNG/app/src/main/res/values-ru/strings.xml @@ -175,6 +175,9 @@ О приложении Исходный код Telegram-канал + Backup configuration + Backup storage location: [%s], It will be cleared when the app is uninstalled + Restore configuration Содействие Содействие, нажмите для получения подробной информации (пожертвование может быть удалено) diff --git a/V2rayNG/app/src/main/res/values-vi/strings.xml b/V2rayNG/app/src/main/res/values-vi/strings.xml index 7b890f5e..53b9d800 100644 --- a/V2rayNG/app/src/main/res/values-vi/strings.xml +++ b/V2rayNG/app/src/main/res/values-vi/strings.xml @@ -175,6 +175,9 @@ About Source code Telegram channel + Backup configuration + Backup storage location: [%s], It will be cleared when the app is uninstalled + Restore configuration Quảng bá server Quảng cáo, nhấn để biết thêm (Ủng hộ có thể được gỡ bỏ) diff --git a/V2rayNG/app/src/main/res/values-zh-rCN/strings.xml b/V2rayNG/app/src/main/res/values-zh-rCN/strings.xml index 09dddc4f..93e085d9 100644 --- a/V2rayNG/app/src/main/res/values-zh-rCN/strings.xml +++ b/V2rayNG/app/src/main/res/values-zh-rCN/strings.xml @@ -173,6 +173,9 @@ 关于 源代码 Telegram 频道 + 备份配置 + 备份存储位置: [%s], 卸载App后会备份将被清除 + 还原配置 推广 一些推广,点击查看详情(捐赠可去除) diff --git a/V2rayNG/app/src/main/res/values-zh-rTW/strings.xml b/V2rayNG/app/src/main/res/values-zh-rTW/strings.xml index 6161a75d..56df96a0 100644 --- a/V2rayNG/app/src/main/res/values-zh-rTW/strings.xml +++ b/V2rayNG/app/src/main/res/values-zh-rTW/strings.xml @@ -173,6 +173,9 @@ 關於 原始碼 Telegram 頻道 + 備份配置 + 備份儲存位置: [%s], 卸載App後備份將會清除 + 還原配置 推廣 一些推廣,輕觸以檢視 (捐贈可去除) diff --git a/V2rayNG/app/src/main/res/values/strings.xml b/V2rayNG/app/src/main/res/values/strings.xml index 6b07cb9e..478a4474 100644 --- a/V2rayNG/app/src/main/res/values/strings.xml +++ b/V2rayNG/app/src/main/res/values/strings.xml @@ -186,6 +186,9 @@ About Source code Telegram channel + Backup configuration + Backup storage location: [%s], It will be cleared when the app is uninstalled + Restore configuration Promotion Promotion,click for details(Donation can be removed)