From 17ffe03dbae019bbdbb6b434be4db7ad78fdb44f Mon Sep 17 00:00:00 2001 From: Sarah Jamie Lewis Date: Mon, 28 Nov 2022 13:35:12 -0800 Subject: [PATCH] Fix Android Profile Export Fix #512. --- .../kotlin/im/cwtch/flwtch/MainActivity.kt | 61 +++++++++++-------- lib/views/profilemgrview.dart | 8 +-- 2 files changed, 38 insertions(+), 31 deletions(-) diff --git a/android/app/src/main/kotlin/im/cwtch/flwtch/MainActivity.kt b/android/app/src/main/kotlin/im/cwtch/flwtch/MainActivity.kt index 5ff736d3..a35a3b22 100644 --- a/android/app/src/main/kotlin/im/cwtch/flwtch/MainActivity.kt +++ b/android/app/src/main/kotlin/im/cwtch/flwtch/MainActivity.kt @@ -136,30 +136,36 @@ class MainActivity: FlutterActivity() { "filekey" to this.dlToFileKey )), ErrorLogResult(""));//placeholder; this Result is never actually invoked } else if (requestCode == PREVIEW_EXPORT_REQUEST_CODE) { - val targetPath = intent!!.getData().toString() - val sourcePath = Paths.get(this.exportFromPath); - 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); + try { + val sourcePath = Paths.get(this.exportFromPath); + val targetUri = intent!!.getData(); + 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); + } + } catch (e: Exception) { + Log.d("MainActivity:PREVIEW_EXPORT FAILED", e.toString()); } } 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); + Log.i("MainActivity:EXPORT_PROFILE", "exporting profile: " + srcFile); + try { + val sourcePath = Paths.get(srcFile); + val targetUri = intent!!.getData(); + val os = this.applicationContext.getContentResolver().openOutputStream(targetUri!!); + val bytesWritten = Files.copy(sourcePath, os); + Log.d("MainActivity:EXPORT_PROFILE", "copied " + bytesWritten.toString() + " bytes"); + if (bytesWritten != 0L) { + os?.flush(); + os?.close(); + //Files.delete(sourcePath); + } + } catch (e: Exception) { + Log.d("MainActivity:EXPORT_PROFILE FAILED", e.toString()); } } } @@ -280,6 +286,12 @@ class MainActivity: FlutterActivity() { startActivityForResult(intent, PREVIEW_EXPORT_REQUEST_CODE) } "ExportProfile" -> { + + val profileOnion: String = call.argument("ProfileOnion") ?: "" + val file: String = StringBuilder().append(this.applicationContext.cacheDir).append("/").append(call.argument("file") ?: "").toString() + Log.i("FlwtchWorker", "constructing exported file " + file); + Cwtch.exportProfile(profileOnion,file) + this.exportFromPath = argmap["file"] ?: "" val intent = Intent(Intent.ACTION_CREATE_DOCUMENT).apply { addCategory(Intent.CATEGORY_OPENABLE) @@ -516,12 +528,6 @@ class MainActivity: FlutterActivity() { val v: String = call.argument("Val") ?: "" Cwtch.setServerAttribute(serverOnion, key, v) } - "ExportProfile" -> { - val profileOnion: String = call.argument("ProfileOnion") ?: "" - val file: String = StringBuilder().append(this.applicationContext.cacheDir).append("/").append(call.argument("file") ?: "").toString() - Log.i("FlwtchWorker", "constructing exported file " + file); - Cwtch.exportProfile(profileOnion,file) - } "ImportProfile" -> { val file: String = call.argument("file") ?: "" val pass: String = call.argument("pass") ?: "" @@ -585,6 +591,7 @@ class MainActivity: FlutterActivity() { } } + override fun onDestroy() { super.onDestroy() Log.i("MainActivity.kt", "onDestroy - cancelling all WORKER_TAG and pruning old work") diff --git a/lib/views/profilemgrview.dart b/lib/views/profilemgrview.dart index eccf6db7..92ab20b7 100644 --- a/lib/views/profilemgrview.dart +++ b/lib/views/profilemgrview.dart @@ -358,8 +358,8 @@ class _ProfileMgrViewState extends State { child: TextButton.icon( icon: Icon(CwtchIcons.lock_open_24px, color: Provider.of(context).current().mainTextColor), style: TextButton.styleFrom( - minimumSize: Size(MediaQuery.of(context).size.width * 0.79, 50), - maximumSize: Size(MediaQuery.of(context).size.width * 0.8, 50), + minimumSize: Size(MediaQuery.of(context).size.width * 0.79, 80), + maximumSize: Size(MediaQuery.of(context).size.width * 0.8, 80), shape: RoundedRectangleBorder(borderRadius: BorderRadius.horizontal(left: Radius.circular(180), right: Radius.circular(180))), ), label: Text( @@ -393,8 +393,8 @@ class _ProfileMgrViewState extends State { child: TextButton.icon( icon: Icon(Icons.add, color: Provider.of(context).current().mainTextColor), style: TextButton.styleFrom( - minimumSize: Size(MediaQuery.of(context).size.width * 0.79, 50), - maximumSize: Size(MediaQuery.of(context).size.width * 0.8, 50), + minimumSize: Size(MediaQuery.of(context).size.width * 0.79, 80), + maximumSize: Size(MediaQuery.of(context).size.width * 0.8, 80), shape: RoundedRectangleBorder(borderRadius: BorderRadius.horizontal(left: Radius.circular(180), right: Radius.circular(180))), ), label: Text(