Ejemplo n.º 1
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))
                {
                    MBDownloadManager2.InstallMbPackage(uri, localUri);
                    MBDownloadManager2.Remove(id);
                }
            }

            cursor.Close();
        }
Ejemplo n.º 2
0
        public static long RequestDownload(Uri uri, string filename, string title, IDownloadUpdated2 notify)
        {
            if (MBDownloadManager2.DefaultManager == null)
            {
                MBDownloadManager2.DefaultManager = new MBDownloadManager2();
            }

            return(MBDownloadManager2.DefaultManager.AddDownload(uri, filename, title, notify));
        }
Ejemplo n.º 3
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.º 4
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.º 5
0
        public long AddDownload(Uri uri, string filename, string title, IDownloadUpdated2 notify)
        {
            long   id   = 0;
            object obj2 = MBDownloadManager2.requestQueue;

            lock (obj2)
            {
                if (MBDownloadManager2.pendingRequests.ContainsKey(uri))
                {
                    return(0);
                }
            }

            if (notify == null)
            {
                throw new ArgumentNullException("notify");
            }
            object obj = MBDownloadManager2.requestQueue;

            lock (obj)
            {
                if (MBDownloadManager2.pendingRequests.ContainsKey(uri))
                {
                    MBDownloadManager2.pendingRequests[uri].Add(notify);
                }
                else
                {
                    List <IDownloadUpdated2> list = new List <IDownloadUpdated2>(4);
                    list.Add(notify);
                    MBDownloadManager2.pendingRequests[uri] = list;

                    id = MBDownloadManager2.Download(uri, filename, title, notify);
                }
            }
            return(id);
        }
Ejemplo n.º 6
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;
                }
            }
        }