Ejemplo n.º 1
0
        private static void InstallMbPackage(string uri, string localUri)
        {
            Uri qUri = new Uri(uri);

            localUri = Uri.UnescapeDataString(localUri);
            uri      = Uri.UnescapeDataString(uri);

            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).LocalPath;

            try
            {
                if (System.IO.Path.GetExtension(localUri) == ".mb")
                {
                    //outPath = Path.Combine(outPath, fileName.Replace(fileExt, ".mbp"));
                    FileSystemManager.UnzipDocument(filePath, outPath);
                    File.Delete(filePath);
                    //ImageLoader.Instance.ClearDiskCache();
                    //ImageLoader.Instance.ClearMemoryCache();
                    MBImageLoader.RemoveFromCache(qUri.ToString());
                }
                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);
                }

                MBDownloadManager.FinishDownload(qUri, true);
            }
            catch (Exception value)
            {
                Console.WriteLine(value);
                MBDownloadManager.FinishDownload(qUri, false);
            }
        }
Ejemplo n.º 2
0
        public static void StopDownload(Uri uri)
        {
            ThreadPool.QueueUserWorkItem(delegate
            {
                if (pendingRequests.ContainsKey(uri))
                {
                    pendingRequests.Remove(uri);
                }

                if (queuedUpdates.Contains(uri))
                {
                    queuedUpdates.Remove(uri);
                }

                requestQueue = new Stack <Uri>(requestQueue.Where(i => i != uri).Reverse());

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

                if (down != null)
                {
                    MBDownloadManager.FinishDownload(uri, false);
                }
                else
                {
                }
            });
        }
Ejemplo n.º 3
0
        private static void DownloadMonitor(long downId)
        {
            //bool downloading = true;
            //Android.App.DownloadManager downloadManager = (Android.App.DownloadManager)Application.Context.GetSystemService("download");

            DownloadInfo down = MBDownloadManager.DownloadInfo(downId);

            while (down != null)
            {
                down = MBDownloadManager.DownloadInfo(downId);

                if (down == null)
                {
                    break;
                }

                Uri uri = new Uri(down.Uri);

                if (!MBDownloadManager.pendingRequests.ContainsKey(uri))
                {
                    break;
                }

                switch (down.Status)
                {
                case DownloadStatus.Running:
                case DownloadStatus.Pending:
                    if (MBDownloadManager.pendingRequests.ContainsKey(uri))
                    {
                        List <IDownloadUpdated2> list = MBDownloadManager.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
                    {
                        MBDownloadManager.InstallMbPackage(down.Uri, down.LocalUri);
                        //downloadManager.Remove(down.Id);

                        //downloading = false;

                        //MBDownloadManager.RegisterDownload(uri.ToString(), down.LocalUri);

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

                default:
                    break;
                }
            }
        }