mirror of
https://github.com/wgtunnel/android.git
synced 2026-07-03 14:07:49 +02:00
ffa7a207fb
Added basic kernel support to allow users to switch between userspace and kernel wireguard Improved location disclosure flow to only show once once per app install Fix airplane mode bug Improve database migration testing Fix auto-tunneling permission bug. Lint Closes #67 Closes #43
67 lines
2.1 KiB
Kotlin
67 lines
2.1 KiB
Kotlin
package com.zaneschepke.wireguardautotunnel.util
|
|
|
|
import android.content.ContentValues
|
|
import android.content.Context
|
|
import android.os.Build
|
|
import android.os.Environment
|
|
import android.provider.MediaStore
|
|
import android.provider.MediaStore.MediaColumns
|
|
import com.zaneschepke.wireguardautotunnel.Constants
|
|
import java.io.File
|
|
import java.io.OutputStream
|
|
import java.time.Instant
|
|
import java.util.zip.ZipEntry
|
|
import java.util.zip.ZipOutputStream
|
|
|
|
object FileUtils {
|
|
private const val ZIP_FILE_MIME_TYPE = "application/zip"
|
|
|
|
private fun createDownloadsFileOutputStream(
|
|
context: Context,
|
|
fileName: String,
|
|
mimeType: String = Constants.ALLOWED_FILE_TYPES
|
|
): OutputStream? {
|
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
|
|
val resolver = context.contentResolver
|
|
val contentValues =
|
|
ContentValues().apply {
|
|
put(MediaColumns.DISPLAY_NAME, fileName)
|
|
put(MediaColumns.MIME_TYPE, mimeType)
|
|
put(MediaColumns.RELATIVE_PATH, Environment.DIRECTORY_DOWNLOADS)
|
|
}
|
|
val uri = resolver.insert(MediaStore.Downloads.EXTERNAL_CONTENT_URI, contentValues)
|
|
if (uri != null) {
|
|
return resolver.openOutputStream(uri)
|
|
}
|
|
} else {
|
|
val target =
|
|
File(
|
|
Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS),
|
|
fileName
|
|
)
|
|
return target.outputStream()
|
|
}
|
|
return null
|
|
}
|
|
|
|
fun saveFilesToZip(
|
|
context: Context,
|
|
files: List<File>
|
|
) {
|
|
val zipOutputStream = createDownloadsFileOutputStream(
|
|
context,
|
|
"wg-export_${Instant.now().epochSecond}.zip",
|
|
ZIP_FILE_MIME_TYPE
|
|
)
|
|
ZipOutputStream(zipOutputStream).use { zos ->
|
|
files.forEach { file ->
|
|
val entry = ZipEntry(file.name)
|
|
zos.putNextEntry(entry)
|
|
if (file.isFile) {
|
|
file.inputStream().use { fis -> fis.copyTo(zos) }
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|