private void ShowSubscribedDialog()
        {
            if (DownloadManager.IsLogged())
            {
                var dialog = new AlertDialog.Builder(Activity);
                dialog.SetTitle(String.Format(GetString(Resource.String.iap_subscribeTo), Activity.ApplicationInfo.LoadLabel(Activity.PackageManager)));

                string[] items = (from a in _Abbonamenti where a.Prezzo != "" select a.Nome + " " + a.Prezzo).ToArray();

                dialog.SetItems(items, SubscribedDialogClicked);
                dialog.SetNegativeButton(GetString(Resource.String.gen_cancel), delegate { return; });

                dialog.Create();
                dialog.Show().SetDivider();
            }
            else
            {
                Action action = delegate {
                    ShowSubscribedDialog();
                };

                ActivitiesBringe.SetObject("loginSuccess", action);

                var logActivity = new Intent(Activity, typeof(LoginActivity));

                StartActivity(logActivity);
            }
        }
        public void OnItemClick(object sender, AdapterView.ItemClickEventArgs e)
        {
            if (actionMode != null)
            {
                return;
            }

            var item = _Items[e.Position];

            if (item.GetType() == typeof(Download))
            {
                DownloadDetails dialog = new DownloadDetails(this.Activity, item as Download);

                dialog.OpenAction += () =>
                {
                    Intent i = new Intent();
                    i.SetClass(Activity, typeof(ViewerScreen));

                    i.SetAction(Intent.ActionView);

                    i.PutExtra("pubPath", (item as Download).GetLocalPath());
                    Activity.StartActivityForResult(i, 0);

                    dialog.Dismiss();
                };

                dialog.DownloadAction += () =>
                {
                    //var cell = ;
                    //var cell = _GridView.Adapter.get(e.Position);

                    //FileDownloader.DefaultRequestDownload((item as Download).Uri, new DownloadGridItem(Activity, (item as Download)));

                    Download down = (item as Download);
                    //MBDownloadManager.Download(down.Uri.AbsoluteUri, System.IO.Path.GetFileName(down.Uri.LocalPath), down.Titolo);
                    MBDownloadManager.RequestDownload(down.Uri, new VoidNotify());

                    this.PopulateTable();
                    dialog.Dismiss();
                };

                dialog.BuyAction += () =>
                {
                    Task.Run(
                        async() =>
                    {
                        if (!DownloadManager.IsLogged())
                        {
                            Action action = delegate {
                                PopulateTable();
                            };

                            ActivitiesBringe.SetObject("loginSuccess", action);

                            var logActivity = new Intent(Activity, typeof(LoginActivity));
                            StartActivity(logActivity);
                        }
                        else
                        {
                            Download down    = (item as Download);
                            List <string> id = new List <string>()
                            {
                                down.IapID
                            };
                            IReadOnlyList <Product> billProducts = await _billingHandler.QueryInventory(id, ItemType.Product);

                            if (billProducts != null)
                            {
                                var prod = billProducts.FirstOrDefault();
                                if (prod != null && prod.ProductId != null)
                                {
                                    Task billTask = BillProcess(prod);
                                }
                            }
                        }
                    }
                        );

                    dialog.Dismiss();
                };

                dialog.Show();
            }
            else if (item.GetType() == typeof(DownDir))
            {
                Console.WriteLine("click dir" + e.Position);

                DownDir dir = item as DownDir;

                _CurrentDir = dir.Path;
                PopulateTable();
            }
        }
        private async Task LoadDocuments()
        {
            Utils.WriteLog("download PopulateTable");

            if (!CheckBeforeConnect())
            {
                return;
            }

            List <Download> docs = DownloadManager.GetDocuments(_CurrentDir, DataManager.Get <IPreferencesManager>().Preferences.EdicolaOrder);

            List <DownDir> _Directories = DownloadManager.GetDirectory(_CurrentDir);

            //aggiungo celle vuote in modo da tenere separati cartelle e documenti
            int lastLineCol = _Directories.Count - ((int)(_Directories.Count / ColumnNum) * ColumnNum);

            if (ColumnNum > 1 && lastLineCol > 0)
            {
                int n = ColumnNum - lastLineCol;

                for (int i = 0; i < n; i++)
                {
                    DownDir info = new DownDir();
                    _Directories.Add(info);
                }
            }

            //in-app billing
            // prendo i prodotti che non sono stati acquistati e che non hanno permessi dalla lista che mi arriva dal server
            Action <string> buyAction = null;

            _Abbonamenti = new List <Subscription>();

            if (DataManager.Get <ISettingsManager>().Settings.InAppPurchase&& _billingHandler != null)
            {
                var products = docs.Where(p => p.IapID != "" && !p.IapAutorizzato && !p.IapAcquistato && p.Stato == DownloadStato.Download);

                if (products.Count() > 0)
                {
                    //lista degli id dei prodotti per richiedere le info a google
                    List <string> productsIds = new List <string>();

                    foreach (var prod in products)
                    {
                        productsIds.Add(prod.IapID);
                    }

                    //richiesta delle informazioni a google
                    IReadOnlyList <Product> billProducts = await _billingHandler.QueryInventory(productsIds, ItemType.Product);

                    if (billProducts != null)
                    {
                        //per ogni prodotto setto i valori corretti nel download
                        foreach (var billProd in billProducts)
                        {
                            var down = docs.Where(d => d.IapID == billProd.ProductId).FirstOrDefault();
                            if (down != null && down.ID != "")
                            {
                                if (!down.IapAcquistato && !down.IapAutorizzato && down.Stato == DownloadStato.Download)
                                {
                                    down.IapPrezzo = billProd.Price;
                                    down.Stato     = DownloadStato.Buy;
                                }
                            }

                            //tolgo i prodotti corretti dalla lista degli id
                            productsIds.RemoveAll(x => x == billProd.ProductId);
                        }

                        //gli id rimasti li tolgo dai download perché vuol dire che google non li ha riconosciuti
                        foreach (var id in productsIds)
                        {
                            docs.RemoveAll(x => x.IapID == id);
                        }

                        buyAction += (string p) =>
                        {
                            if (!DownloadManager.IsLogged())
                            {
                                Action action = delegate {
                                    PopulateTable();
                                };

                                ActivitiesBringe.SetObject("loginSuccess", action);

                                var logActivity = new Intent(Activity, typeof(LoginActivity));
                                StartActivity(logActivity);
                            }
                            else
                            {
                                var prod = billProducts.Where(x => x.ProductId == p).FirstOrDefault();

                                if (prod == null)
                                {
                                    return;
                                }

                                Task billTask = BillProcess(prod);
                            }
                        };
                    }
                }

                //abbonamenti
                List <string> subIds = new List <string>();
                _Abbonamenti = DownloadManager.GetSubscriptions();

                foreach (var s in _Abbonamenti)
                {
                    subIds.Add(s.IapID);
                }

                if (subIds.Count > 0)
                {
                    IReadOnlyList <Product> billSubscription = await _billingHandler.QueryInventory(subIds, ItemType.Subscription);

                    if (billSubscription != null)
                    {
                        foreach (var billSub in billSubscription)
                        {
                            var sub = _Abbonamenti.Where(d => d.IapID == billSub.ProductId).FirstOrDefault();

                            if (sub != null && sub.IapID != "")
                            {
                                sub.Nome   = billSub.Title.Replace("(" + Activity.ApplicationInfo.LoadLabel(Activity.PackageManager) + ")", "").Trim();
                                sub.Prezzo = billSub.Price;
                            }

                            //tolgo i prodotti corretti dalla lista degli id
                            subIds.RemoveAll(x => x == billSub.ProductId);
                        }

                        //gli id rimasti li tolgo dai download perché vuol dire che google non li ha riconosciuti
                        foreach (var id in subIds)
                        {
                            _Abbonamenti.RemoveAll(x => x.IapID == id);
                        }
                    }
                }
            }

            _Items = new List <Object>();
            _Items.AddRange(_Directories);
            _Items.AddRange(docs);

            var adapter = new DownloadGridAdapter(Activity, _Items);

            adapter.OpenAction += (string p) =>
            {
                Intent i = new Intent();
                i.SetClass(Activity, typeof(ViewerScreen));

                i.SetAction(Intent.ActionView);

                i.PutExtra("pubPath", p);
                Activity.StartActivityForResult(i, 0);
            };

            if (Activity != null)
            {
                Activity.RunOnUiThread(() =>
                {
                    this.SetTitle();
                    StopUpdating();
                    _GridView.Adapter = adapter;
                });
            }

            if (buyAction != null)
            {
                adapter.BuyAction += buyAction;
            }
        }