コード例 #1
0
        private bool CheckBeforeConnect()
        {
            Uri host = new Uri(DataManager.Get <ISettingsManager>().Settings.DownloadUrl);

            if (!Reachability.IsHostReachable("http://" + host.Host))
            {
                if (Activity == null)
                {
                    return(false);
                }

                Activity.RunOnUiThread(() =>
                {
                    StopUpdating();

                    var alert = new AlertDialog.Builder(Activity);
                    alert.SetTitle(GetString(Resource.String.gen_error));
                    alert.SetMessage(GetString(Resource.String.gen_serverNotReachable));
                    alert.SetPositiveButton("Ok", (EventHandler <DialogClickEventArgs>)null);
                    alert.Show().SetDivider();
                });
                return(false);
            }

            //se siamo nella cartella principale controllo la compatibilità con la versione dell'app
            var verResult = DownloadManager.CheckAppVersion(2);

            if (!verResult.Success)
            {
                Activity.RunOnUiThread(() =>
                {
                    StopUpdating();
                    var alert = new AlertDialog.Builder(Activity);
                    alert.SetCancelable(false);
                    alert.SetTitle(GetString(Resource.String.gen_error));
                    alert.SetMessage(GetString(Resource.String.gen_appVersion));
                    alert.SetPositiveButton(GetString(Resource.String.gen_downApp), delegate
                    {
                        var callIntent = new Intent(Intent.ActionView);

                        Android.Net.Uri uri = Android.Net.Uri.Parse(verResult.Link);
                        callIntent.SetData(uri);
                        this.StartActivity(callIntent);

                        //va all'edicola
                        var home = Activity as HomeScreen;
                        home.GoTo(0);
                    });

                    alert.SetNegativeButton(GetString(Resource.String.gen_cancel), delegate
                    {
                        //va all'edicola
                        var home = Activity as HomeScreen;
                        home.GoTo(0);
                    });

                    alert.Show().SetDivider();
                });

                this.IsUpdating = false;
                return(false);
            }

            //controllo utente disattivato
            var result = DownloadManager.CheckUser();

            if (!result.Success)
            {
                if (result.ErrBloccante)
                {
                    Activity.RunOnUiThread(() =>
                    {
                        StopUpdating();
                        var alert = new AlertDialog.Builder(Activity);
                        alert.SetTitle(GetString(Resource.String.gen_error));
                        alert.SetMessage(result.Message);
                        alert.SetPositiveButton("Ok", (EventHandler <DialogClickEventArgs>)null);
                        alert.Show().SetDivider();

                        var adapter       = new DownloadGridAdapter(Activity, new List <Object>());
                        _GridView.Adapter = adapter;
                        StopUpdating();
                    });

                    this.IsUpdating = false;
                    return(false);
                }
            }

            return(true);
        }
コード例 #2
0
        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;
            }
        }