Import / Export for Android

This commit is contained in:
Sarah Jamie Lewis 2022-03-10 13:29:28 -08:00
parent 6237032716
commit 8a3867b5b3
4 changed files with 42 additions and 13 deletions

View File

@ -46,7 +46,7 @@
<!--Needed to run in background (lol)-->
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
<!--Meeded to check if activity is foregrounded or if messages from the service should be queued-->
<!--Needed to check if activity is foregrounded or if messages from the service should be queued-->
<uses-permission android:name="android.permission.GET_TASKS" />
<queries>

View File

@ -1,6 +1,7 @@
package im.cwtch.flwtch
import android.app.*
import android.os.Environment
import android.content.Context
import android.content.Intent
import android.graphics.BitmapFactory
@ -430,12 +431,13 @@ class FlwtchWorker(context: Context, parameters: WorkerParameters) :
}
"ExportProfile" -> {
val profileOnion = (a.get("ProfileOnion") as? String) ?: ""
val file = (a.get("file") as? String) ?: ""
Cwtch.exportProfile(profileOnion, file)
val file = StringBuilder().append(this.applicationContext.cacheDir).append("/").append((a.get("file") as? String) ?: "").toString()
Log.i("FlwtchWorker", "constructing exported file " + file);
Cwtch.exportProfile(profileOnion,file)
}
"ImportProfile" -> {
val file = (a.get("file") as? String) ?: ""
val password = (a.get("pass") as? String) ?: ""
val pass = (a.get("pass") as? String) ?: ""
return Result.success(Data.Builder().putString("result", Cwtch.importProfile(file, pass)).build());
}
else -> {

View File

@ -62,6 +62,7 @@ class MainActivity: FlutterActivity() {
// "Download to..." prompt extra arguments
private val FILEPICKER_REQUEST_CODE = 234
private val PREVIEW_EXPORT_REQUEST_CODE = 235
private val PROFILE_EXPORT_REQUEST_CODE = 236
private var dlToProfile = ""
private var dlToHandle = ""
private var dlToFileKey = ""
@ -110,8 +111,6 @@ class MainActivity: FlutterActivity() {
)), ErrorLogResult(""));//placeholder; this Result is never actually invoked
} else if (requestCode == PREVIEW_EXPORT_REQUEST_CODE) {
val targetPath = intent!!.getData().toString()
var srcFile = File(this.exportFromPath)
Log.i("MainActivity:PREVIEW_EXPORT", "exporting previewed file")
val sourcePath = Paths.get(this.exportFromPath);
val targetUri = Uri.parse(targetPath);
val os = this.applicationContext.getContentResolver().openOutputStream(targetUri);
@ -122,6 +121,20 @@ class MainActivity: FlutterActivity() {
os?.close();
//Files.delete(sourcePath);
}
} else if (requestCode == PROFILE_EXPORT_REQUEST_CODE ) {
val targetPath = intent!!.getData().toString()
val srcFile = StringBuilder().append(this.applicationContext.cacheDir).append("/").append(this.exportFromPath).toString();
Log.i("MainActivity:PREVIEW_EXPORT", "exporting previewed file " + srcFile);
val sourcePath = Paths.get(srcFile);
val targetUri = Uri.parse(targetPath);
val os = this.applicationContext.getContentResolver().openOutputStream(targetUri);
val bytesWritten = Files.copy(sourcePath, os);
Log.d("MainActivity:PREVIEW_EXPORT", "copied " + bytesWritten.toString() + " bytes");
if (bytesWritten != 0L) {
os?.flush();
os?.close();
//Files.delete(sourcePath);
}
}
}
@ -211,6 +224,14 @@ class MainActivity: FlutterActivity() {
}
startActivityForResult(intent, PREVIEW_EXPORT_REQUEST_CODE)
return
} else if (call.method == "ExportProfile") {
this.exportFromPath = argmap["file"] ?: ""
val intent = Intent(Intent.ACTION_CREATE_DOCUMENT).apply {
addCategory(Intent.CATEGORY_OPENABLE)
type = "application/gzip"
putExtra(Intent.EXTRA_TITLE, argmap["file"])
}
startActivityForResult(intent, PROFILE_EXPORT_REQUEST_CODE)
}
// ...otherwise fallthru to a normal ffi method call (and return the result using the result callback)

View File

@ -297,13 +297,19 @@ class _AddEditProfileViewState extends State<AddEditProfileView> {
message: AppLocalizations.of(context)!.exportProfileTooltip,
child: ElevatedButton.icon(
onPressed: () {
showCreateFilePicker(context).then((name) {
if (name != null) {
Provider.of<FlwtchState>(context, listen: false).cwtch.ExportProfile(ctrlrOnion.value.text, name);
final snackBar = SnackBar(content: Text(AppLocalizations.of(context)!.fileSavedTo + " " + name));
ScaffoldMessenger.of(context).showSnackBar(snackBar);
}
});
if (Platform.isAndroid) {
Provider.of<FlwtchState>(context, listen: false).cwtch.ExportProfile(ctrlrOnion.value.text, ctrlrOnion.value.text + ".tar.gz");
final snackBar = SnackBar(content: Text(AppLocalizations.of(context)!.fileSavedTo + " " + ctrlrOnion.value.text + ".tar.gz"));
ScaffoldMessenger.of(context).showSnackBar(snackBar);
} else {
showCreateFilePicker(context).then((name) {
if (name != null) {
Provider.of<FlwtchState>(context, listen: false).cwtch.ExportProfile(ctrlrOnion.value.text, name);
final snackBar = SnackBar(content: Text(AppLocalizations.of(context)!.fileSavedTo + " " + name));
ScaffoldMessenger.of(context).showSnackBar(snackBar);
}
});
}
},
icon: Icon(Icons.import_export),
label: Text(AppLocalizations.of(context)!.exportProfile),