From d4aa1cb3971da96c524cfba2325752a9102f466a Mon Sep 17 00:00:00 2001 From: erinn Date: Wed, 29 Sep 2021 17:16:00 -0700 Subject: [PATCH] android download notification, also fix updatemessageflags didnt work on android --- .../kotlin/im/cwtch/flwtch/FlwtchWorker.kt | 49 +++++++++++++++++++ .../kotlin/im/cwtch/flwtch/MainActivity.kt | 6 +-- lib/cwtch/gomobile.dart | 2 +- lib/widgets/filebubble.dart | 2 +- 4 files changed, 54 insertions(+), 5 deletions(-) diff --git a/android/app/src/main/kotlin/im/cwtch/flwtch/FlwtchWorker.kt b/android/app/src/main/kotlin/im/cwtch/flwtch/FlwtchWorker.kt index b5727649..d18bb0d9 100644 --- a/android/app/src/main/kotlin/im/cwtch/flwtch/FlwtchWorker.kt +++ b/android/app/src/main/kotlin/im/cwtch/flwtch/FlwtchWorker.kt @@ -60,6 +60,7 @@ class FlwtchWorker(context: Context, parameters: WorkerParameters) : if (Cwtch.startCwtch(appDir, torPath) != 0.toLong()) return Result.failure() Log.i("FlwtchWorker.kt", "startCwtch success, starting coroutine AppbusEvent loop...") + val downloadIDs = mutableMapOf() while(true) { val evt = MainActivity.AppbusEvent(Cwtch.getAppBusEvent()) if (evt.EventType == "NewMessageFromPeer" || evt.EventType == "NewMessageFromGroup") { @@ -97,10 +98,46 @@ class FlwtchWorker(context: Context, parameters: WorkerParameters) : .build() notificationManager.notify(getNotificationID(data.getString("ProfileOnion"), handle), newNotification) } + } else if (evt.EventType == "FileDownloadProgressUpdate") { + try { + val data = JSONObject(evt.Data); + val fileKey = data.getString("FileKey"); + val title = data.getString("NameSuggestion"); + val progress = data.getString("Progress").toInt(); + val progressMax = data.getString("FileSizeInChunks").toInt(); + if (!downloadIDs.containsKey(fileKey)) { + downloadIDs.put(fileKey, downloadIDs.count()); + } + var dlID = downloadIDs.get(fileKey); + if (dlID == null) { + dlID = 0; + } + val channelId = + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { + createDownloadNotificationChannel(fileKey, fileKey) + } else { + // If earlier version channel ID is not used + // https://developer.android.com/reference/android/support/v4/app/NotificationCompat.Builder.html#NotificationCompat.Builder(android.content.Context) + "" + }; + val newNotification = NotificationCompat.Builder(applicationContext, channelId) + .setOngoing(true) + .setContentTitle("Downloading")//todo: translate + .setContentText(title) + .setSmallIcon(android.R.drawable.stat_sys_download) + .setProgress(progressMax, progress, false) + .setSound(null) + //.setSilent(true) + .build(); + notificationManager.notify(dlID, newNotification); + } catch (e: Exception) { + Log.i("FlwtchWorker->FileDownloadProgressUpdate", e.toString() + " :: " + e.getStackTrace()); + } } else if (evt.EventType == "FileDownloaded") { Log.i("FlwtchWorker", "file downloaded!"); val data = JSONObject(evt.Data); val tempFile = data.getString("TempFile"); + val fileKey = data.getString("FileKey"); if (tempFile != "") { val filePath = data.getString("FilePath"); Log.i("FlwtchWorker", "moving "+tempFile+" to "+filePath); @@ -115,6 +152,9 @@ class FlwtchWorker(context: Context, parameters: WorkerParameters) : Files.delete(sourcePath); } } + if (downloadIDs.containsKey(fileKey)) { + notificationManager.cancel(downloadIDs.get(fileKey)?:0); + } } Intent().also { intent -> @@ -310,6 +350,15 @@ class FlwtchWorker(context: Context, parameters: WorkerParameters) : return channelId } + @RequiresApi(Build.VERSION_CODES.O) + private fun createDownloadNotificationChannel(channelId: String, channelName: String): String{ + val chan = NotificationChannel(channelId, channelName, NotificationManager.IMPORTANCE_LOW) + chan.lightColor = Color.MAGENTA + chan.lockscreenVisibility = Notification.VISIBILITY_PRIVATE + notificationManager.createNotificationChannel(chan) + return channelId + } + companion object { const val KEY_METHOD = "KEY_METHOD" const val KEY_ARGS = "KEY_ARGS" 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 8c7d1f47..5ae7408b 100644 --- a/android/app/src/main/kotlin/im/cwtch/flwtch/MainActivity.kt +++ b/android/app/src/main/kotlin/im/cwtch/flwtch/MainActivity.kt @@ -60,6 +60,7 @@ class MainActivity: FlutterActivity() { private var dlToHandle = "" private var dlToFileKey = "" + // handles clicks received from outside the app (ie, notifications) override fun onNewIntent(intent: Intent) { super.onNewIntent(intent) if (notificationClickChannel == null || intent.extras == null) return @@ -81,6 +82,7 @@ class MainActivity: FlutterActivity() { } } + // handles return values from the system file picker override fun onActivityResult(requestCode: Int, result: Int, intent: Intent?) { if (intent == null || intent!!.getData() == null) { Log.i("MainActivity:onActivityResult", "user canceled activity"); @@ -89,15 +91,13 @@ class MainActivity: FlutterActivity() { val filePath = intent!!.getData().toString(); val manifestPath = StringBuilder().append(this.applicationContext.cacheDir).append("/").append(this.dlToFileKey).toString(); - Log.i("onActivityResult", "got download path: " + filePath); - Log.i("onActivityResult", "got manifest path: " + manifestPath); handleCwtch(MethodCall("DownloadFile", mapOf( "ProfileOnion" to this.dlToProfile, "handle" to this.dlToHandle, "filepath" to filePath, "manifestpath" to manifestPath, "filekey" to this.dlToFileKey - )), ErrorLogResult(""));//placeholder; result is never actually invoked + )), ErrorLogResult(""));//placeholder; this Result is never actually invoked } override fun configureFlutterEngine(@NonNull flutterEngine: FlutterEngine) { diff --git a/lib/cwtch/gomobile.dart b/lib/cwtch/gomobile.dart index 7bab3150..bf73813e 100644 --- a/lib/cwtch/gomobile.dart +++ b/lib/cwtch/gomobile.dart @@ -198,7 +198,7 @@ class CwtchGomobile implements Cwtch { @override void UpdateMessageFlags(String profile, String handle, int index, int flags) { print("gomobile.dart UpdateMessageFlags " + index.toString()); - cwtchPlatform.invokeMethod("UpdateMessageFlags", {"profile": profile, "contact": handle, "index": index, "flags": flags}); + cwtchPlatform.invokeMethod("UpdateMessageFlags", {"profile": profile, "contact": handle, "midx": index, "flags": flags}); } @override diff --git a/lib/widgets/filebubble.dart b/lib/widgets/filebubble.dart index 49aef579..e58f2400 100644 --- a/lib/widgets/filebubble.dart +++ b/lib/widgets/filebubble.dart @@ -98,7 +98,7 @@ class FileBubbleState extends State { wdgDecorations = Text('Checking download status...' + '\u202F'); Provider.of(context, listen: false).cwtch.CheckDownloadStatus(Provider.of(context, listen: false).onion, widget.fileKey()); } else { - wdgDecorations = Text('Saved to: ' + (path??"null") + '\u202F'); + wdgDecorations = Text('Saved to: ' + path + '\u202F'); } } else { wdgDecorations = Center(