Files
amnezia-client/client/android/src/org/amnezia/vpn/AmneziaActivity.kt
T

99 lines
2.6 KiB
Kotlin
Raw Normal View History

2023-11-16 20:16:28 +03:00
package org.amnezia.vpn
import android.content.Intent
import android.net.Uri
import java.io.FileNotFoundException
import java.io.FileOutputStream
import java.io.IOException
2022-12-23 17:32:20 +03:00
import org.qtproject.qt.android.bindings.QtActivity
private const val TAG = "AmneziaActivity"
2023-03-29 16:09:46 +03:00
private const val CREATE_FILE_ACTION_CODE = 102
class AmneziaActivity : QtActivity() {
private var tmpFileContentToSave: String = ""
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
if (requestCode == CREATE_FILE_ACTION_CODE && resultCode == RESULT_OK) {
data?.data?.also { uri ->
alterDocument(uri)
2023-03-29 16:09:46 +03:00
}
}
super.onActivityResult(requestCode, resultCode, data)
2023-03-29 16:09:46 +03:00
}
private fun alterDocument(uri: Uri) {
2022-12-23 17:32:20 +03:00
try {
applicationContext.contentResolver.openFileDescriptor(uri, "w")?.use { fd ->
FileOutputStream(fd.fileDescriptor).use { fos ->
fos.write(tmpFileContentToSave.toByteArray())
}
}
} catch (e: FileNotFoundException) {
e.printStackTrace()
} catch (e: IOException) {
2022-12-23 17:32:20 +03:00
e.printStackTrace()
}
tmpFileContentToSave = ""
}
/**
* Methods called by Qt
*/
@Suppress("unused")
fun qtAndroidControllerInitialized() {
Log.v(TAG, "Qt Android controller initialized")
Log.w(TAG, "Not yet implemented")
}
@Suppress("unused")
fun start(vpnConfig: String) {
Log.v(TAG, "Start VPN")
Log.w(TAG, "Not yet implemented")
2022-12-23 17:32:20 +03:00
}
@Suppress("unused")
fun stop() {
Log.v(TAG, "Stop VPN")
Log.w(TAG, "Not yet implemented")
2022-12-23 17:32:20 +03:00
}
@Suppress("unused")
fun saveFile(fileName: String, data: String) {
Log.v(TAG, "Save file $fileName")
// todo: refactor
tmpFileContentToSave = data
2023-03-29 16:09:46 +03:00
val intent = Intent(Intent.ACTION_CREATE_DOCUMENT).apply {
addCategory(Intent.CATEGORY_OPENABLE)
type = "text/*"
putExtra(Intent.EXTRA_TITLE, fileName)
2022-12-23 17:32:20 +03:00
}
startActivityForResult(intent, CREATE_FILE_ACTION_CODE)
2022-12-23 17:32:20 +03:00
}
@Suppress("unused")
fun setNotificationText(title: String, message: String, timerSec: Int) {
Log.v(TAG, "Set notification text")
Log.w(TAG, "Not yet implemented")
2022-12-23 17:32:20 +03:00
}
@Suppress("unused")
fun cleanupLogs() {
Log.v(TAG, "Cleanup logs")
Log.w(TAG, "Not yet implemented")
}
@Suppress("unused")
fun startQrCodeReader() {
Log.v(TAG, "Start camera")
2023-11-21 22:48:52 +03:00
Intent(this, CameraActivity::class.java).also {
startActivity(it)
}
}
}