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,57 @@ 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+         try  {
294+             val  downloadManager =  context.getSystemService(Context .DOWNLOAD_SERVICE ) as  DownloadManager 
295+             val  headersJsonObject =  JSONObject (header)
296+             val  headersMap =  mutableMapOf<String , String >()
297+             for  (key in  headersJsonObject.keys()) {
298+                 headersMap[key] =  headersJsonObject.getString(key)
299+             }
300+             val  uri =  Uri .parse(url)
301+ 
302+             val  request =  DownloadManager .Request (uri).apply  {
303+                 setTitle(" Downloading $fileName " 
304+                 setDescription(" File is being downloaded..." 
305+                 setDestinationInExternalPublicDir(Environment .DIRECTORY_DOWNLOADS , fileName)
306+                 setNotificationVisibility(DownloadManager .Request .VISIBILITY_VISIBLE_NOTIFY_COMPLETED )
307+ 
308+                 for  ((key, value) in  headersMap) {
309+                    addRequestHeader(key, value)
310+                 }
311+             }
312+             //  Queue the download
313+             downloadManager.enqueue(request)
314+             callback(Result .success(" Download started successfully for: $fileName " 
315+         } catch  (e:  Exception ) {
316+             callback(Result .failure(NotificationsError (
317+                 code =  " DOWNLOAD_ERROR" 
318+                 message =  e.message ? :  " Unknown error occurred during file download" 
319+             )))
320+         }
321+     }
322+ }
323+ 
286324/* * A Flutter plugin for the Zulip app's ad-hoc needs. */ 
287325//  @Keep is needed because this class is used only
288326//  from ZulipShimPlugin, via reflection.
289327@Keep
290- class  ZulipPlugin  : FlutterPlugin  {  //  TODO ActivityAware too? 
328+ class  ZulipPlugin  : FlutterPlugin  {
291329    private  var  notificationHost:  AndroidNotificationHost ?  =  null 
330+     private  var  downloadHost:  DownloadHost ?  =  null 
292331
293332    override  fun  onAttachedToEngine (binding :  FlutterPlugin .FlutterPluginBinding ) {
294333        Log .d(TAG , " Attaching to Flutter engine." 
295334        notificationHost =  AndroidNotificationHost (binding.applicationContext)
335+         downloadHost =  DownloadHost (binding.applicationContext)
336+ 
296337        AndroidNotificationHostApi .setUp(binding.binaryMessenger, notificationHost)
338+         DownloadManagerHostApi .setUp(binding.binaryMessenger, downloadHost)
297339    }
298340
299341    override  fun  onDetachedFromEngine (binding :  FlutterPlugin .FlutterPluginBinding ) {
@@ -302,6 +344,9 @@ class ZulipPlugin : FlutterPlugin { // TODO ActivityAware too?
302344            return 
303345        }
304346        AndroidNotificationHostApi .setUp(binding.binaryMessenger, null )
347+         DownloadManagerHostApi .setUp(binding.binaryMessenger, null )
348+ 
305349        notificationHost =  null 
350+         downloadHost =  null 
306351    }
307- }
352+ }
0 commit comments