예제 #1
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();
            }
        }
예제 #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
                {
                }
            });
        }
예제 #3
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);
            }
        }
예제 #4
0
        public void OnItemClick(object sender, AdapterView.ItemClickEventArgs e)
        {
            if (actionMode != null)
            {
                return;
            }

            var item = _Items[e.Position];

            if (item is Pubblicazione)
            {
                Console.WriteLine("click thumb" + e.Position);

                Pubblicazione doc = item as Pubblicazione;

                //se sta aggiornando non apro il documento
                var down = _downloads.Where(d => d.ID == doc.ID).FirstOrDefault();

                if (down != null)
                {
                    DownloadInfo downInfo = MBDownloadManager.DownloadInfo(down.Uri.AbsoluteUri);

                    if (downInfo != null && downInfo.Id != 0 && downInfo.Uri != "")
                    {
                        return;
                    }
                }

                if (DateTime.Now > doc.DataScadenza && !doc.IsPDF && doc.DataScadenza != DateTime.MinValue)
                {
                    var alert = new AlertDialog.Builder(Activity);
                    alert.SetTitle(GetString(Resource.String.gen_error));
                    alert.SetMessage(GetString(Resource.String.pub_expired));
                    alert.SetPositiveButton("Ok", (EventHandler <DialogClickEventArgs>)null);
                    alert.Show().SetDivider();
                    return;
                }

                Intent i = new Intent();
                i.SetClass(Activity, typeof(ViewerScreen));

                //i.SetFlags(ActivityFlags.NewTask);

                i.SetAction(Intent.ActionView);

                i.PutExtra("pubPath", doc.Path);
                Activity.StartActivityForResult(i, 0);
                //Activity.StartActivity(i);
            }
            else if (item is DirectoryInfo)
            {
                Console.WriteLine("click dir" + e.Position);

                DirectoryInfo dir = item as DirectoryInfo;

                _CurrentDir = dir;
                PopulateTable();
            }
        }
예제 #5
0
        public void Initialize()
        {
            if (DownloadUri != null && DownloadUri.AbsolutePath != "")
            {
                DownloadInfo down = MBDownloadManager.DownloadInfo(DownloadUri.AbsoluteUri);

                if (down != null && down.Id != 0 && down.Uri != "")
                {
                    MBDownloadManager.UpdateNotify(DownloadUri, this);

                    this.DownloadStato = DownloadStato.Downloading;
                }
            }
            else
            {
                this.DownloadStato = DownloadStato.NoUpdate;
            }

            SetUIState();
        }
예제 #6
0
        public void ConfigureForDownload()
        {
            //pulsante download
            EventHandler onDownClick = (object sender, EventArgs e) =>
            {
                this.Stato = DownloadStato.Downloading;

                //_downloadReference = MBDownloadManager2.Download(_download.Uri, System.IO.Path.GetFileName(_download.Uri.LocalPath), _download.Titolo, this);
                //_downloadReference = MBDownloadManager2.RequestDownload(DownloadUri, System.IO.Path.GetFileName(DownloadUri.LocalPath), this.Title, this);

                MBDownloadManager.RequestDownload(DownloadUri, this);
                SetUIState();

                Log.Info(this.Title, "Inizio download");
            };

            _btnDownload.Click -= onDownClick;
            _btnDownload.Click += onDownClick;

            //pulsante ferma
            EventHandler onStopClick = (object sender, EventArgs e) =>
            {
                //if(_downloadReference != 0)
                {
                    MBDownloadManager.StopDownload(DownloadUri);
                    //_downloadReference = 0;

                    this.Stato = InitStato;
                    SetUIState();
                }
            };

            _btnStop.Click -= onStopClick;
            _btnStop.Click += onStopClick;

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

            if (down != null && down.Id != 0 && down.Uri != "")
            {
                //_downloadReference = down.Id;
                MBDownloadManager.UpdateNotify(DownloadUri, this);

                if (down.Status == DownloadStatus.Running || down.Status == DownloadStatus.Pending)
                {
                    _prgDownload.Progress = 0;
                }
                else if (down.Status == DownloadStatus.Successful)
                {
                    string filePath = new Uri(down.LocalUri).AbsolutePath;
                    if (File.Exists(filePath))
                    {
                        _prgDownload.Progress = 100;
                    }
                }

                this.Stato = DownloadStato.Downloading;
            }
            else if (MBDownloadManager.IsWaiting(DownloadUri))
            {
                _prgDownload.Progress = 0;
                this.Stato            = DownloadStato.Downloading;

                MBDownloadManager.UpdateNotify(DownloadUri, this);
            }

            _btnBuy.SetText(this.Prezzo, TextView.BufferType.Normal);

            SetUIState();
        }
예제 #7
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;
                }
            }
        }
예제 #8
0
        protected override void OnCreate(Bundle bundle)
        {
            base.OnCreate(bundle);

            this.Window.RequestFeature(WindowFeatures.NoTitle);

            SetContentView(Resource.Layout.DownloadDetails);

            this.SetTitle(_download.Titolo);

            TextView  txtTitolo     = FindViewById <TextView>(Resource.Id.txtTitolo);
            TextView  txtAutore     = FindViewById <TextView>(Resource.Id.txtAutore);
            TextView  txtPubblicato = FindViewById <TextView>(Resource.Id.txtPubblicato);
            TextView  txtScadenza   = FindViewById <TextView>(Resource.Id.txtScadenza);
            TextView  txtDimensione = FindViewById <TextView>(Resource.Id.txtDimensione);
            ImageView imgCover      = FindViewById <ImageView>(Resource.Id.imgCover);
            Button    btnOpen       = FindViewById <Button>(Resource.Id.btnOpen);
            Button    btnDownload   = FindViewById <Button>(Resource.Id.btnDownload);
            Button    btnBuy        = FindViewById <Button>(Resource.Id.btnBuy);
            TextView  txtPrezzo     = FindViewById <TextView>(Resource.Id.txtPrezzo);

            txtTitolo.Text = _download.Titolo;
            txtAutore.Text = _download.Autore.Trim();
            txtPrezzo.Text = "";

            if (_download.Tipo == "pdf")
            {
                imgCover.SetImageResource(Resource.Drawable.pdf_icon);
            }
            else
            {
                string key = _download.ImageKey;
                var    uri = new Uri(DataManager.Get <ISettingsManager>().Settings.DownloadUrl +
                                     "services/edicola_services.php?action=pubCover&zip=" + _download.RelativePath +
                                     "&img=copertina.jpg&key=" + key +
                                     "&app=" + DataManager.Get <ISettingsManager>().Settings.AppId);

                imgCover.Tag = uri.ToString();

                //Koush.UrlImageViewHelper.SetUrlDrawable (imgCover, uri.AbsoluteUri);

                MBImageLoader.DisplayNetworkImage(uri, imgCover, new PointF(360, 360));
            }

            if (_download.DataPubblicazione != DateTime.MinValue)
            {
                txtPubblicato.Text = string.Format(Context.GetString(Resource.String.pub_publishedOn), _download.DataPubblicazione.ToString("dd-MM-yyyy"));
            }
            else
            {
                txtPubblicato.Visibility = ViewStates.Gone;
            }

            if (_download.DataScadenza != DateTime.MinValue)
            {
                txtScadenza.Text = string.Format(Context.GetString(Resource.String.pub_expireOn), _download.DataScadenza.ToString("dd-MM-yyyy"));
            }
            else
            {
                txtScadenza.Visibility = ViewStates.Gone;
            }

            txtDimensione.Text = Utils.FormatBytes(_download.Dimensione);

            if (DataManager.Get <ISettingsManager>().Settings.InAppPurchase&& _download.IapID != "")
            {
                if (_download.IapAcquistato)
                {
                    txtPrezzo.Text = string.Format(Context.GetString(Resource.String.iap_purchaseDate), _download.IapDataAcquisto.ToString("dd-MM-yyyy"));
                }
            }

            //pulsante apri
            btnOpen.SetTextColor(Color.Transparent.FromHex(DataManager.Get <ISettingsManager>().Settings.ButtonColor));
            btnOpen.Text   = Context.GetString(Resource.String.pub_open);
            btnOpen.Click += delegate
            {
                if (OpenAction != null)
                {
                    OpenAction();
                }
            };

            //pulsante download
            btnDownload.SetTextColor(Color.Transparent.FromHex(DataManager.Get <ISettingsManager>().Settings.ButtonColor));
            btnDownload.Text   = Context.GetString(Resource.String.pub_download);
            btnDownload.Click += delegate
            {
                if (DownloadAction != null)
                {
                    DownloadAction();
                }
            };

            //pulsante buy
            btnBuy.SetTextColor(Color.Transparent.FromHex(DataManager.Get <ISettingsManager>().Settings.ButtonColor));

            btnBuy.Text   = _download.IapPrezzo;
            btnBuy.Click += delegate
            {
                if (BuyAction != null)
                {
                    BuyAction();
                }
            };

            GradientDrawable gd = new GradientDrawable();

            gd.SetColor(Color.Transparent.FromHex("ffffff"));
            gd.SetCornerRadius(5);
            gd.SetStroke(1, Color.Transparent.FromHex(DataManager.Get <ISettingsManager>().Settings.ButtonColor));
            btnBuy.Background = gd;

            /*if(FileDownloader.IsWaiting(_download.Uri))
             * {
             *      _download.Stato = DownloadStato.Downloading;
             * }*/
            DownloadInfo down = MBDownloadManager.DownloadInfo(_download.Uri.AbsoluteUri);

            if (down != null && (down.Status == DownloadStatus.Running || down.Status == DownloadStatus.Pending))
            {
                _download.Stato = DownloadStato.Downloading;
            }

            //in base allo stato del download configuro i pulsanti
            switch (_download.Stato)
            {
            case DownloadStato.Download:
                btnDownload.Text   = Context.GetString(Resource.String.pub_download);
                btnOpen.Visibility = ViewStates.Invisible;
                btnBuy.Visibility  = ViewStates.Gone;
                btnBuy.Enabled     = false;
                break;

            case DownloadStato.NoUpdate:
                btnDownload.Visibility = ViewStates.Invisible;
                btnOpen.Visibility     = ViewStates.Visible;
                btnBuy.Visibility      = ViewStates.Gone;
                btnBuy.Enabled         = false;
                break;

            case DownloadStato.Update:
                btnDownload.Text   = Context.GetString(Resource.String.down_update);
                btnOpen.Visibility = ViewStates.Visible;
                btnBuy.Visibility  = ViewStates.Gone;
                btnBuy.Enabled     = false;
                break;

            case DownloadStato.Downloading:
                btnDownload.Text    = Context.GetString(Resource.String.down_wait);
                btnDownload.Enabled = false;
                btnOpen.Visibility  = ViewStates.Invisible;
                btnBuy.Visibility   = ViewStates.Gone;
                btnBuy.Enabled      = false;
                break;

            case DownloadStato.Expired:
                btnDownload.Text    = Context.GetString(Resource.String.pub_expired);
                btnDownload.Enabled = false;
                btnOpen.Visibility  = ViewStates.Invisible;
                btnBuy.Visibility   = ViewStates.Gone;
                btnBuy.Enabled      = false;
                break;

            case DownloadStato.Buy:
                btnDownload.Visibility = ViewStates.Gone;
                btnDownload.Enabled    = false;
                btnOpen.Visibility     = ViewStates.Invisible;
                btnBuy.Visibility      = ViewStates.Visible;
                btnBuy.Enabled         = true;
                break;

            default:
                break;
            }
        }