public override void OnReceive(Context context, Intent intent)
        {
            var reference = intent.GetLongExtra(Android.App.DownloadManager.ExtraDownloadId, -1);

            var downloadFile = CrossDownloadManager.Current.Queue.Cast <DownloadFileImplementation> ().FirstOrDefault(f => f.Id == reference);

            if (downloadFile == null)
            {
                return;
            }

            var query = new Android.App.DownloadManager.Query();

            query.SetFilterById(downloadFile.Id);

            try {
                using (var cursor = ((Android.App.DownloadManager)context.GetSystemService(Context.DownloadService)).InvokeQuery(query)) {
                    while (cursor != null && cursor.MoveToNext())
                    {
                        ((DownloadManagerImplementation)CrossDownloadManager.Current).UpdateFileProperties(cursor, downloadFile);
                    }
                    cursor?.Close();
                }
            } catch (Android.Database.Sqlite.SQLiteException) {
                // I lately got an exception that the database was unaccessible ...
            }
        }
Exemplo n.º 2
0
        public static DownloadInfo DownloadInfo(long downId)
        {
            Android.App.DownloadManager.Query query = new Android.App.DownloadManager.Query();

            query.SetFilterById(downId);

            Android.App.DownloadManager downloadManager = (Android.App.DownloadManager)Application.Context.GetSystemService("download");

            ICursor cursor = downloadManager.InvokeQuery(query);

            if (cursor.Count > 0 && cursor.MoveToFirst())
            {
                string uri = cursor.GetString(cursor.GetColumnIndex(Android.App.DownloadManager.ColumnUri));

                DownloadInfo info = new DownloadInfo();
                info.Uri            = uri;
                info.LocalUri       = cursor.GetString(cursor.GetColumnIndex(Android.App.DownloadManager.ColumnLocalUri));
                info.Id             = cursor.GetLong(cursor.GetColumnIndex(Android.App.DownloadManager.ColumnId));
                info.title          = cursor.GetString(cursor.GetColumnIndex(Android.App.DownloadManager.ColumnTitle));
                info.Status         = (DownloadStatus)cursor.GetInt(cursor.GetColumnIndex(Android.App.DownloadManager.ColumnStatus));
                info.ByteDownloaded = cursor.GetLong(cursor.GetColumnIndex(Android.App.DownloadManager.ColumnBytesDownloadedSoFar));
                info.ByteTotal      = cursor.GetLong(cursor.GetColumnIndex(Android.App.DownloadManager.ColumnTotalSizeBytes));

                cursor.Close();
                return(info);
            }

            cursor.Close();

            return(null);
        }
        void StartDownloadWatcher()
        {
            // Create an instance for a runnable-handler
            _downloadWatcherHandler = new Android.OS.Handler();

            // Create a runnable, restarting itself to update every file in the queue
            _downloadWatcherHandlerRunnable = new Java.Lang.Runnable(() => {
                var downloads = Queue.Cast <DownloadFileImplementation>().ToList();

                foreach (var file in downloads)
                {
                    var query = new Android.App.DownloadManager.Query();
                    query.SetFilterById(file.Id);

                    try {
                        using (var cursor = _downloadManager.InvokeQuery(query)) {
                            if (cursor != null && cursor.MoveToNext())
                            {
                                UpdateFileProperties(cursor, file);
                            }
                            else
                            {
                                // This file is not listed in the native download manager anymore. Let's mark it as canceled.
                                Abort(file);
                            }
                            cursor?.Close();
                        }
                    } catch (Android.Database.Sqlite.SQLiteException) {
                        // I lately got an exception that the database was unaccessible ...
                    }
                }

                _downloadWatcherHandler.PostDelayed(_downloadWatcherHandlerRunnable, 1000);
            });

            // Start this playing handler immediately
            _downloadWatcherHandler.PostDelayed(_downloadWatcherHandlerRunnable, 0);
        }
Exemplo n.º 4
0
        public override void OnReceive(Context context, Android.Content.Intent intent)
        {
            if (intent.Action.Equals(Android.App.DownloadManager.ActionDownloadComplete))
            {
                Bundle data        = intent.Extras;
                long   download_id = data.GetLong(Android.App.DownloadManager.ExtraDownloadId);

                Android.App.DownloadManager.Query query = new Android.App.DownloadManager.Query();
                query.SetFilterById(download_id);

                Android.App.DownloadManager downloadManager = (Android.App.DownloadManager)context.GetSystemService("download");

                ICursor c = downloadManager.InvokeQuery(query);

                if (c.MoveToFirst())
                {
                    string title    = c.GetString(c.GetColumnIndex(Android.App.DownloadManager.ColumnTitle));
                    string uri      = c.GetString(c.GetColumnIndex(Android.App.DownloadManager.ColumnUri));
                    string localUri = c.GetString(c.GetColumnIndex(Android.App.DownloadManager.ColumnLocalUri));

                    string fileName = System.IO.Path.GetFileName(localUri);

                    string search = "/pub/";
                    //string url = uri.AbsoluteUri;
                    uri = uri.Substring(uri.IndexOf(search) + search.Length).Trim('/');

                    string outPath = System.IO.Path.Combine(DataManager.Get <ISettingsManager>().Settings.DocPath, uri);
                    //outPath = Path.Combine(outPath, parts[1]).Trim('/');
                    outPath = System.Web.HttpUtility.UrlDecode(outPath);
                    outPath = System.IO.Path.GetDirectoryName(outPath);

                    string filePath = new Uri(localUri).AbsolutePath;
                    new Thread(() =>
                    {
                        try
                        {
                            if (System.IO.Path.GetExtension(localUri) == ".mb")
                            {
                                //outPath = Path.Combine(outPath, fileName.Replace(fileExt, ".mbp"));
                                FileSystemManager.UnzipDocument(filePath, outPath);
                                File.Delete(filePath);
                            }
                            else if (System.IO.Path.GetExtension(localUri) == ".pdf")
                            {
                                outPath = System.IO.Path.Combine(outPath, fileName);
                                if (File.Exists(outPath))
                                {
                                    File.Delete(outPath);
                                }

                                //se la cartella non esiste la creo
                                string dir = System.IO.Path.GetDirectoryName(outPath);

                                if (!Directory.Exists(dir))
                                {
                                    Directory.CreateDirectory(dir);
                                }

                                File.Move(filePath, outPath);
                            }
                        }
                        catch (Exception value)
                        {
                            Console.WriteLine(value);
                        }
                    }).Start();
                }
            }
        }
Exemplo n.º 5
0
        private void StartDownloadWatcher()
        {
            if (_downloadWatcherHandler != null)
            {
                return;
            }

            // Create an instance for a runnable-handler
            _downloadWatcherHandler = new Android.OS.Handler();

            // Create a runnable, restarting itself to update every file in the queue
            _downloadWatcherHandlerRunnable = new Java.Lang.Runnable(() => {
                var queueDownload = Queue.Cast <DownloadFileImplementation>();
                var file          = queueDownload.FirstOrDefault();
                if (file != null)
                {
                    if (file.Status == DownloadFileStatus.PAUSED)
                    {
                        var fileTemp = queueDownload.FirstOrDefault(x => x.Status != DownloadFileStatus.PAUSED);
                        if (fileTemp != null)
                        {
                            file = fileTemp;
                        }
                    }

                    if (file.Status == DownloadFileStatus.INITIALIZED)
                    {
                        string destinationPathName = null;
                        if (PathNameForDownloadedFile != null)
                        {
                            destinationPathName = PathNameForDownloadedFile(file);
                        }
                        file.StartDownload(_downloadManager, destinationPathName, true, NotificationVisibility, IsVisibleInDownloadsUi);
                    }

                    var query = new Android.App.DownloadManager.Query();
                    query.SetFilterById(file.Id);
                    try
                    {
                        using (var cursor = _downloadManager.InvokeQuery(query))
                        {
                            if (cursor != null && cursor.MoveToNext())
                            {
                                UpdateFileProperties(cursor, file);
                            }
                            else
                            {
                                // This file is not listed in the native download manager anymore. Let's mark it as canceled.
                                Abort(file);
                            }
                            cursor?.Close();
                        }
                    }
                    catch (Android.Database.Sqlite.SQLiteException)
                    {
                        // I lately got an exception that the database was unaccessible ...
                    }
                }
                _downloadWatcherHandler?.PostDelayed(_downloadWatcherHandlerRunnable, 1000);
            });
            // Start this playing handler immediately
            _downloadWatcherHandler.Post(_downloadWatcherHandlerRunnable);
        }