Ejemplo n.º 1
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);
        }
Ejemplo n.º 2
0
        public static DownloadInfo DownloadInfo(string uri)
        {
            Android.App.DownloadManager.Query query = new Android.App.DownloadManager.Query();

            //query.SetFilterByStatus(DownloadStatus.Pending | DownloadStatus.Running | DownloadStatus.Paused);

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

            ICursor cursor = downloadManager.InvokeQuery(query);

            while (cursor.MoveToNext())
            {
                string url = cursor.GetString(cursor.GetColumnIndex(Android.App.DownloadManager.ColumnUri));

                if (url == uri)
                {
                    DownloadInfo info = new DownloadInfo();
                    info.Uri            = url;
                    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);
        }
Ejemplo n.º 3
0
        public static void InstallFinishedMB()
        {
            Android.App.DownloadManager.Query query = new Android.App.DownloadManager.Query();

            query.SetFilterByStatus(DownloadStatus.Successful);

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

            ICursor cursor = downloadManager.InvokeQuery(query);

            while (cursor.MoveToNext())
            {
                string localUri = cursor.GetString(cursor.GetColumnIndex(Android.App.DownloadManager.ColumnLocalUri));
                string uri      = cursor.GetString(cursor.GetColumnIndex(Android.App.DownloadManager.ColumnUri));
                long   id       = cursor.GetLong(cursor.GetColumnIndex(Android.App.DownloadManager.ColumnId));
                string filePath = new Uri(localUri).AbsolutePath;

                if (File.Exists(filePath))
                {
                    MBDownloadManager.InstallMbPackage(uri, localUri);
                    downloadManager.Remove(id);
                    //MBDownloadManager.FinishDownload(new Uri(uri), true);
                }
            }

            cursor.Close();
        }
Ejemplo n.º 4
0
        static void Download(Uri uri)
        {
            string fileName = Path.GetFileName(uri.LocalPath);

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

            Android.App.DownloadManager.Request request = new Android.App.DownloadManager.Request(Android.Net.Uri.Parse(uri.AbsoluteUri));

            //Restrict the types of networks over which this download may proceed.
            request.SetAllowedNetworkTypes(Android.App.DownloadNetwork.Wifi | Android.App.DownloadNetwork.Mobile);
            //Set whether this download may proceed over a roaming connection.
            request.SetAllowedOverRoaming(false);
            //Set the title of this download, to be displayed in notifications (if enabled).
            //request.SetTitle(title);
            request.SetVisibleInDownloadsUi(false);

            //request.SetNotificationVisibility(DownloadVisibility.Hidden);
            #if DEBUG
            request.SetNotificationVisibility(DownloadVisibility.Visible);
            #else
            request.SetNotificationVisibility(DownloadVisibility.Hidden);
            #endif

            request.SetDestinationInExternalFilesDir(Application.Context, Android.OS.Environment.DirectoryDownloads, fileName);

            //Enqueue a new download and same the referenceId
            var id = downloadManager.Enqueue(request);

            MBDownloadManager.DownloadMonitor(id);
        }
Ejemplo n.º 5
0
        public static void RemoveAll()
        {
            try
            {
                Android.App.DownloadManager downloadManager = (Android.App.DownloadManager)Application.Context.GetSystemService("download");

                Android.App.DownloadManager.Query query = new Android.App.DownloadManager.Query();
                //query.SetFilterById(_downloadReference);
                //query.SetFilterByStatus(DownloadStatus.Pending | DownloadStatus.Running | DownloadStatus.Paused | DownloadStatus.Successful);

                ICursor cursor = downloadManager.InvokeQuery(query);

                while (cursor.MoveToNext())
                {
                    long id = cursor.GetLong(cursor.GetColumnIndex(Android.App.DownloadManager.ColumnId));
                    downloadManager.Remove(id);
                }

                MBDownloadManager.fileUriers = 0;
            }
            catch (Exception ex)
            {
                string tag = TAG + " - RemoveAll";
                Log.Error(tag, ex.Message);
            }
        }
        public void StartDownload(Android.App.DownloadManager downloadManager, string destinationPathName,
                                  bool allowedOverMetered, DownloadVisibility notificationVisibility, bool isVisibleInDownloadsUi)
        {
            using (var downloadUrl = Uri.Parse(Url))
                using (var request = new Android.App.DownloadManager.Request(downloadUrl))
                {
                    if (Headers != null)
                    {
                        foreach (var header in Headers)
                        {
                            request.AddRequestHeader(header.Key, header.Value);
                        }
                    }

                    if (destinationPathName != null)
                    {
                        var file        = new Java.IO.File(destinationPathName);
                        var uriPathFile = Android.Net.Uri.FromFile(file);
                        request.SetDestinationUri(uriPathFile);
                        //if (file.Exists())
                        //{
                        //    file.Delete();
                        //}
                    }
                    request.SetVisibleInDownloadsUi(isVisibleInDownloadsUi);
                    request.SetAllowedOverMetered(allowedOverMetered);
                    request.SetNotificationVisibility(notificationVisibility);
                    Id = downloadManager.Enqueue(request);
                }
        }
Ejemplo n.º 7
0
        static void NotifyDownloadListeners()
        {
            lock (requestQueue)
            {
                foreach (var quri in queuedUpdates)
                {
                    var list = pendingRequests[quri];
                    pendingRequests.Remove(quri);

                    DownloadInfo down = MBDownloadManager.DownloadInfo(quri.AbsoluteUri);

                    if (down != null)
                    {
                        Android.App.DownloadManager downloadManager = (Android.App.DownloadManager)Application.Context.GetSystemService("download");
                        downloadManager.Remove(down.Id);

                        MBDownloadManager.RegisterDownload(quri.ToString(), down.LocalUri);
                    }

                    foreach (var pr in list)
                    {
                        try
                        {
                            pr.DownloadCompleted(quri.AbsoluteUri, ""); // this is the bit that should be on the UiThread
                        }
                        catch (Exception e)
                        {
                            Log.Error("MWC", e.Message);
                        }
                    }
                }

                queuedUpdates.Clear();
            }
        }
Ejemplo n.º 8
0
        private static void FinishDownload(Uri uri, bool result)
        {
            Interlocked.Decrement(ref MBDownloadManager.fileUriers);
            Console.WriteLine("DECREMENT " + MBDownloadManager.fileUriers);

            bool   flag2 = false;
            object obj   = MBDownloadManager.requestQueue;

            lock (obj)
            {
                if (result)
                {
                    MBDownloadManager.queuedUpdates.Add(uri);
                    if (MBDownloadManager.queuedUpdates.Count == 1)
                    {
                        flag2 = true;
                    }
                }
                else
                {
                    MBDownloadManager.pendingRequests.Remove(uri);

                    DownloadInfo down = MBDownloadManager.DownloadInfo(uri.AbsoluteUri);

                    if (down != null)
                    {
                        Android.App.DownloadManager downloadManager = (Android.App.DownloadManager)Application.Context.GetSystemService("download");
                        downloadManager.Remove(down.Id);
                    }
                }

                if (MBDownloadManager.requestQueue.Count > 0)
                {
                    uri = MBDownloadManager.requestQueue.Pop();
                    if (uri == null)
                    {
                        Console.Error.WriteLine("Dropping request {0} because url is null", uri);
                        MBDownloadManager.pendingRequests.Remove(uri);
                        uri = null;
                    }
                }
                else
                {
                    uri = null;
                }
            }

            if (flag2)
            {
                //FileDownloader.nsDispatcher.BeginInvokeOnMainThread(new NSAction(FileDownloader.NotifyDownloadListeners));
                NotifyDownloadListeners();
            }

            if (uri != null)
            {
                Interlocked.Increment(ref MBDownloadManager.fileUriers);
                MBDownloadManager.StartFileDownload(uri);
            }
        }
Ejemplo n.º 9
0
        public static Native GetManager(this Context context)
        {
            if (downloadManager == null || downloadManager.Handle == IntPtr.Zero)
            {
                downloadManager = (Native)context.GetSystemService(Context.DownloadService);
            }

            return(downloadManager);
        }
Ejemplo n.º 10
0
        public DownloadManagerImplementation()
        {
            _queue = new List <IDownloadFile> ();

            _downloadManager = (Android.App.DownloadManager)Application.Context.GetSystemService(Context.DownloadService);

            // Add all items to the Queue that are pending, paused or running
            LoopOnDownloads(new Action <ICursor> (cursor => ReinitializeFile(cursor)));

            // Check sequentially if parameters for any of the registered downloads changed
            StartDownloadWatcher();
        }
 private void OnDownloadAvailable(YoutubeEntry feed, VideoInfo videoInfo, MediaType mediaType)
 {
     Controls.Title.Text = videoInfo.Title;
     Dm = (DownloadManager) Controls.Activity.BaseContext.GetSystemService(Context.DownloadService);
     var fileName = DownloadHelper.GetLegalPath(videoInfo.Title) + videoInfo.VideoExtension;
     var request =
         new DownloadManager.Request(Android.Net.Uri.Parse(videoInfo.DownloadUri.ToString()));
     request.SetAllowedNetworkTypes(DownloadNetwork.Wifi | DownloadNetwork.Mobile)
        .AddRequestHeader("User-Agent", "Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.97 Safari/537.11")
        .SetShowRunningNotification(true)
        .SetAllowedOverRoaming(true)
        .SetTitle(videoInfo.Title)
        .SetDescription(fileName)
        .SetDestinationInExternalPublicDir(Android.OS.Environment.DirectoryDownloads, fileName);
     Enqueue = Dm.Enqueue(request);
 }
Ejemplo n.º 12
0
        public static void RemoveAll()
        {
            Android.App.DownloadManager downloadManager = (Android.App.DownloadManager)Application.Context.GetSystemService("download");

            Android.App.DownloadManager.Query query = new Android.App.DownloadManager.Query();
            //query.SetFilterById(_downloadReference);
            query.SetFilterByStatus(DownloadStatus.Pending | DownloadStatus.Running | DownloadStatus.Paused);

            ICursor cursor = downloadManager.InvokeQuery(query);

            while (cursor.MoveToNext())
            {
                long id = cursor.GetLong(cursor.GetColumnIndex(Android.App.DownloadManager.ColumnId));
                downloadManager.Remove(id);
            }
        }
Ejemplo n.º 13
0
        private void Download(string Url, string filename)
        {
            Android.Net.Uri contentUri            = Android.Net.Uri.Parse(Url);
            Android.App.DownloadManager.Request r = new Android.App.DownloadManager.Request(contentUri);
            r.SetDestinationInExternalPublicDir(Android.OS.Environment.DirectoryDownloads, filename);
            r.AllowScanningByMediaScanner();
            r.SetNotificationVisibility(Android.App.DownloadVisibility.VisibleNotifyCompleted);
            Android.App.DownloadManager dm = (Android.App.DownloadManager)Xamarin.Forms.Forms.Context.GetSystemService(Android.Content.Context.DownloadService);

            dm.Enqueue(r);

            var localFolder = Android.OS.Environment.DirectoryDownloads;
            var MyFilePath  = $"file://{localFolder}/{Filename}";

            Toast.MakeText(this, MyFilePath, ToastLength.Long).Show();
        }
Ejemplo n.º 14
0
        public void Download(string uri, string filename)
        {
            Android.Net.Uri contentUri = Android.Net.Uri.Parse(uri);

            Android.App.DownloadManager.Request r = new Android.App.DownloadManager.Request(contentUri);


            r.SetDestinationInExternalPublicDir(Android.OS.Environment.DirectoryDownloads, filename);

            r.AllowScanningByMediaScanner();
            r.SetShowRunningNotification(true);
            r.SetNotificationVisibility(Android.App.DownloadVisibility.VisibleNotifyCompleted);
            r.SetVisibleInDownloadsUi(true);

            Android.App.DownloadManager dm = (Android.App.DownloadManager)Xamarin.Forms.Forms.Context.GetSystemService(Android.Content.Context.DownloadService);
            // dm.NotifyAll();
            dm.Enqueue(r);
        }
Ejemplo n.º 15
0
        public void Download(string uri, string filename)
        {
            var contentUri   = Android.Net.Uri.Parse(uri);
            var r            = new Android.App.DownloadManager.Request(contentUri);
            var downloadpath = Android.OS.Environment.GetExternalStoragePublicDirectory(Android.OS.Environment.DirectoryDownloads).AbsolutePath;

            var file = new File(Path.Combine(downloadpath, filename));

            if (file.Exists())
            {
                return;
            }
            r.SetDestinationInExternalPublicDir(Android.OS.Environment.DirectoryDownloads, filename);
            r.AllowScanningByMediaScanner();
            r.SetNotificationVisibility(DownloadVisibility.VisibleNotifyCompleted);
            dm      = (Android.App.DownloadManager)Forms.Context.GetSystemService(Context.DownloadService);
            _fileId = dm.Enqueue(r);
        }
Ejemplo n.º 16
0
        public static void Remove(long id)
        {
            Android.App.DownloadManager downloadManager = (Android.App.DownloadManager)Application.Context.GetSystemService("download");

            var info = MBDownloadManager2.DownloadInfo(id);

            var uri = new Uri(info.Uri);

            downloadManager.Remove(id);

            if (pendingRequests != null && pendingRequests.ContainsKey(uri))
            {
                pendingRequests.Remove(uri);

                if (queuedUpdates.Contains(uri))
                {
                    queuedUpdates.Remove(uri);
                }
            }
        }
Ejemplo n.º 17
0
        public void StartDownload(Android.App.DownloadManager downloadManager, string destinationPathName, bool allowedOverMetered)
        {
            using (var downloadUrl = Uri.Parse(Url))
                using (var request = new Android.App.DownloadManager.Request(downloadUrl)) {
                    if (Headers != null)
                    {
                        foreach (var header in Headers)
                        {
                            request.AddRequestHeader(header.Key, header.Value);
                        }
                    }

                    if (destinationPathName != null)
                    {
                        request.SetDestinationUri(Uri.FromFile(new Java.IO.File(destinationPathName)));
                    }

                    request.SetAllowedOverMetered(allowedOverMetered);

                    Id = downloadManager.Enqueue(request);
                }
        }
Ejemplo n.º 18
0
        static long Download(Uri uri, string filename, string title, IDownloadUpdated2 notify)
        {
            Android.App.DownloadManager downloadManager = (Android.App.DownloadManager)Application.Context.GetSystemService("download");

            Android.App.DownloadManager.Request request = new Android.App.DownloadManager.Request(Android.Net.Uri.Parse(uri.AbsoluteUri));

            //Restrict the types of networks over which this download may proceed.
            request.SetAllowedNetworkTypes(Android.App.DownloadNetwork.Wifi | Android.App.DownloadNetwork.Mobile);
            //Set whether this download may proceed over a roaming connection.
            request.SetAllowedOverRoaming(false);
            //Set the title of this download, to be displayed in notifications (if enabled).
            request.SetTitle(title);
            request.SetVisibleInDownloadsUi(false);

            //request.SetNotificationVisibility(DownloadVisibility.Hidden);
                        #if DEBUG
            request.SetNotificationVisibility(DownloadVisibility.Visible);
                        #else
            request.SetNotificationVisibility(DownloadVisibility.Hidden);
                        #endif

            request.SetDestinationInExternalFilesDir(Application.Context, Android.OS.Environment.DirectoryDownloads, filename);

            //Enqueue a new download and same the referenceId
            var id = downloadManager.Enqueue(request);

            ThreadPool.QueueUserWorkItem(state =>
            {
                MBDownloadManager2.DownloadMonitor(id);
            });

            /*new Thread(() =>
             * {
             *      MBDownloadManager2.DownloadMonitor(id);
             *
             * }).Start();*/

            return(id);
        }
Ejemplo n.º 19
0
        private static void DownloadMonitor(long downId)
        {
            bool downloading = true;

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

            while (downloading)
            {
                DownloadInfo down = MBDownloadManager2.DownloadInfo(downId);

                if (down == null)
                {
                    break;
                }

                Uri uri = new Uri(down.Uri);

                switch (down.Status)
                {
                case DownloadStatus.Running:
                case DownloadStatus.Pending:
                    if (MBDownloadManager2.pendingRequests.ContainsKey(uri))
                    {
                        List <IDownloadUpdated2> list = MBDownloadManager2.pendingRequests[uri];
                        var perc = (int)((down.ByteDownloaded * 100L) / down.ByteTotal);
                        try
                        {
                            foreach (IDownloadUpdated2 current2 in list)
                            {
                                current2.ProgressChanged(perc);
                            }
                        }
                        catch (Exception value)
                        {
                            Console.WriteLine(value);
                        }
                    }
                    break;

                case DownloadStatus.Successful:
                    try
                    {
                        MBDownloadManager2.InstallMbPackage(down.Uri, down.LocalUri);
                        downloadManager.Remove(down.Id);

                        downloading = false;

                        MBDownloadManager2.RegisterDownload(uri.ToString(), down.LocalUri);

                        MBDownloadManager2.FinishDownload(uri, true);
                    }
                    catch (Exception value)
                    {
                        Console.WriteLine(value);
                        MBDownloadManager2.FinishDownload(uri, false);
                    }
                    break;

                default:
                    break;
                }
            }
        }
Ejemplo n.º 20
0
        } = true;                                                // true is the default behavior from Android DownloadManagerApi

        public DownloadManagerImplementation()
        {
            _queue           = new List <IDownloadFile>();
            _downloadManager = (Android.App.DownloadManager)Android.App.Application.Context.GetSystemService(Context.DownloadService);
            //StartDownloadWatcher();
        }
Ejemplo n.º 21
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();
                }
            }
        }