11package  com.zulip.flutter 
22
33import  android.annotation.SuppressLint 
4+ import  android.app.DownloadManager 
45import  android.content.ContentUris 
56import  android.content.ContentValues 
67import  android.content.Context 
78import  android.content.Intent 
89import  android.media.AudioAttributes 
10+ import  android.media.MediaScannerConnection 
911import  android.net.Uri 
1012import  android.os.Build 
1113import  android.os.Bundle 
@@ -19,7 +21,7 @@ import androidx.core.app.NotificationCompat
1921import  androidx.core.app.NotificationManagerCompat 
2022import  androidx.core.graphics.drawable.IconCompat 
2123import  io.flutter.embedding.engine.plugins.FlutterPlugin 
22- 
24+ import   org.json.JSONObject 
2325private  const  val  TAG  =  " ZulipPlugin" 
2426
2527fun  toAndroidPerson (person :  Person ): androidx.core.app.Person  {
@@ -283,17 +285,50 @@ private class AndroidNotificationHost(val context: Context)
283285    }
284286}
285287
288+ /* * Host class for handling downloads via DownloadManager */ 
289+ class  DownloadHost (private  val  context :  Context ) : DownloadManagerHostApi {
290+ 
291+     /* * Downloads a file from the given URL and saves it to the specified filename in the Downloads folder. */ 
292+     override  fun  downloadFile (url :  String , fileName :  String , header :  String , callback :  (Result <String >) ->  Unit ) {
293+         val  downloadManager =  context.getSystemService(Context .DOWNLOAD_SERVICE ) as  DownloadManager 
294+         val  headersJsonObject =  JSONObject (header)
295+         val  headersMap =  mutableMapOf<String , String >()
296+         for  (key in  headersJsonObject.keys()) {
297+             headersMap[key] =  headersJsonObject.getString(key)
298+         }
299+         val  uri =  Uri .parse(url)
300+ 
301+         val  request =  DownloadManager .Request (uri).apply  {
302+             setTitle(" Downloading $fileName " 
303+             setDescription(" File is being downloaded..." 
304+             setDestinationInExternalPublicDir(Environment .DIRECTORY_DOWNLOADS , fileName)
305+             setNotificationVisibility(DownloadManager .Request .VISIBILITY_VISIBLE_NOTIFY_COMPLETED )
306+ 
307+             for  ((key, value) in  headersMap) {
308+                 addRequestHeader(key, value)
309+             }
310+         }
311+         //  Queue the download
312+         downloadManager.enqueue(request)
313+         callback(Result .success(" Download started successfully for: $fileName " 
314+     }
315+ }
316+ 
286317/* * A Flutter plugin for the Zulip app's ad-hoc needs. */ 
287318//  @Keep is needed because this class is used only
288319//  from ZulipShimPlugin, via reflection.
289320@Keep
290- class  ZulipPlugin  : FlutterPlugin  {  //  TODO ActivityAware too? 
321+ class  ZulipPlugin  : FlutterPlugin  {
291322    private  var  notificationHost:  AndroidNotificationHost ?  =  null 
323+     private  var  downloadHost:  DownloadHost ?  =  null 
292324
293325    override  fun  onAttachedToEngine (binding :  FlutterPlugin .FlutterPluginBinding ) {
294326        Log .d(TAG , " Attaching to Flutter engine." 
295327        notificationHost =  AndroidNotificationHost (binding.applicationContext)
328+         downloadHost =  DownloadHost (binding.applicationContext)
329+ 
296330        AndroidNotificationHostApi .setUp(binding.binaryMessenger, notificationHost)
331+         DownloadManagerHostApi .setUp(binding.binaryMessenger, downloadHost)
297332    }
298333
299334    override  fun  onDetachedFromEngine (binding :  FlutterPlugin .FlutterPluginBinding ) {
@@ -302,6 +337,9 @@ class ZulipPlugin : FlutterPlugin { // TODO ActivityAware too?
302337            return 
303338        }
304339        AndroidNotificationHostApi .setUp(binding.binaryMessenger, null )
340+         DownloadManagerHostApi .setUp(binding.binaryMessenger, null )
341+ 
305342        notificationHost =  null 
343+         downloadHost =  null 
306344    }
307- }
345+ }
0 commit comments