2023-11-16 20:16:28 +03:00
|
|
|
package org.amnezia.vpn
|
2022-12-08 11:51:28 +03:00
|
|
|
|
|
|
|
|
import android.content.Intent
|
|
|
|
|
import android.net.Uri
|
2023-11-17 15:10:11 +03:00
|
|
|
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
|
2022-12-08 11:51:28 +03:00
|
|
|
|
2023-11-17 15:10:11 +03:00
|
|
|
private const val TAG = "AmneziaActivity"
|
2023-03-29 16:09:46 +03:00
|
|
|
|
2023-11-17 15:10:11 +03:00
|
|
|
private const val CAMERA_ACTION_CODE = 101
|
|
|
|
|
private const val CREATE_FILE_ACTION_CODE = 102
|
2022-12-08 11:51:28 +03:00
|
|
|
|
2023-11-17 15:10:11 +03:00
|
|
|
class AmneziaActivity : QtActivity() {
|
2023-03-16 12:13:55 +03:00
|
|
|
|
|
|
|
|
private var tmpFileContentToSave: String = ""
|
2023-02-17 16:38:44 +03:00
|
|
|
|
|
|
|
|
private fun startQrCodeActivity() {
|
|
|
|
|
val intent = Intent(this, CameraActivity::class.java)
|
|
|
|
|
startActivityForResult(intent, CAMERA_ACTION_CODE)
|
2022-12-23 17:32:20 +03:00
|
|
|
}
|
|
|
|
|
|
2023-11-17 15:10:11 +03:00
|
|
|
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
|
|
|
}
|
|
|
|
|
}
|
2023-11-17 15:10:11 +03:00
|
|
|
super.onActivityResult(requestCode, resultCode, data)
|
2023-03-29 16:09:46 +03:00
|
|
|
}
|
|
|
|
|
|
2023-11-17 15:10:11 +03:00
|
|
|
private fun alterDocument(uri: Uri) {
|
2022-12-23 17:32:20 +03:00
|
|
|
try {
|
2023-11-17 15:10:11 +03:00
|
|
|
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()
|
|
|
|
|
}
|
2022-12-08 11:51:28 +03:00
|
|
|
|
2023-11-17 15:10:11 +03:00
|
|
|
tmpFileContentToSave = ""
|
2022-12-08 11:51:28 +03:00
|
|
|
}
|
|
|
|
|
|
2023-11-17 15:10:11 +03:00
|
|
|
/**
|
|
|
|
|
* Methods called by Qt
|
|
|
|
|
*/
|
|
|
|
|
@Suppress("unused")
|
|
|
|
|
fun qtAndroidControllerInitialized() {
|
|
|
|
|
Log.v(TAG, "Qt Android controller initialized")
|
|
|
|
|
Log.w(TAG, "Not yet implemented")
|
2022-12-08 11:51:28 +03:00
|
|
|
}
|
|
|
|
|
|
2023-11-17 15:10:11 +03:00
|
|
|
@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
|
|
|
}
|
|
|
|
|
|
2023-11-17 15:10:11 +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
|
|
|
}
|
|
|
|
|
|
2023-11-17 15:10:11 +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
|
|
|
|
2023-11-17 15:10:11 +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
|
|
|
}
|
|
|
|
|
|
2023-11-17 15:10:11 +03:00
|
|
|
startActivityForResult(intent, CREATE_FILE_ACTION_CODE)
|
2022-12-23 17:32:20 +03:00
|
|
|
}
|
|
|
|
|
|
2023-11-17 15:10:11 +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
|
|
|
}
|
|
|
|
|
|
2023-11-17 15:10:11 +03:00
|
|
|
@Suppress("unused")
|
|
|
|
|
fun cleanupLogs() {
|
|
|
|
|
Log.v(TAG, "Cleanup logs")
|
|
|
|
|
Log.w(TAG, "Not yet implemented")
|
2023-03-16 12:13:55 +03:00
|
|
|
}
|
|
|
|
|
|
2023-11-17 15:10:11 +03:00
|
|
|
@Suppress("unused")
|
|
|
|
|
fun startQrCodeReader() {
|
|
|
|
|
Log.v(TAG, "Start camera")
|
|
|
|
|
startQrCodeActivity()
|
2022-12-08 11:51:28 +03:00
|
|
|
}
|
|
|
|
|
}
|