コード例 #1
0
        protected override void OnCreate(Bundle bundle)
        {
            base.OnCreate(bundle);

            if (DataManager.Get <ISettingsManager>().Settings.InAppPurchase)
            {
                BillingHandler = new SaneInAppBillingHandler(this, DataManager.Get <ISettingsManager>().Settings.PublicKey);

                try
                {
                    Task.Run(
                        async() => {
                        await BillingHandler.Connect();

                        if (DownloadManager.IsLogged())
                        {
                            //abbonamenti
                            var purch   = await BillingHandler.GetPurchases(ItemType.Subscription);
                            var subList = new List <Dictionary <string, string> >();

                            foreach (var p in purch)
                            {
                                //BillingHandler.ConsumePurchase(p);
                                Dictionary <string, string> data = new Dictionary <string, string>();

                                string orderID = p.OrderId;
                                if (orderID == null || orderID == "")
                                {
                                    orderID = p.PurchaseToken;
                                }

                                data.Add("packageName", this.PackageName);
                                data.Add("orderId", orderID);
                                data.Add("productId", p.ProductId);
                                data.Add("developerPayload", p.DeveloperPayload);
                                data.Add("purchaseTime", p.PurchaseTime.ToString());
                                data.Add("purchaseToken", p.PurchaseToken);
                                data.Add("purchaseState", p.PurchaseState.ToString());

                                subList.Add(data);
                            }

                            var encodeData = Convert.ToBase64String(System.Text.Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(subList)));

                            Notification.CheckSubscriptions(encodeData);
                        }
                    }

                        );
                    //await _billingHandler.Connect();
                }
                catch (InAppBillingException ex)
                {
                    // Thrown if the commection fails for whatever reason (device doesn't support In-App billing, etc.)
                    // All methods (except for Disconnect()) may throw this exception,
                    // handling it is omitted for brevity in the rest of the samples
                    Log.Error("HomeScreen", ex.Message);
                }
            }

            //permessi cartella condivisa
            if (!this.CanAccessExternal())
            {
                var permissions = new string[] { Manifest.Permission.ReadExternalStorage, Manifest.Permission.WriteExternalStorage };
                this.RequestPermissions(permissions, 1);
            }

            //this.SetTheme(Resource.Style.MB);

            SetContentView(Resource.Layout.HomeScreen);


            MenuLabels = new Dictionary <string, string>()
            {
                { "edicola", GetString(Resource.String.menu_Edicola) },
                { "download", GetString(Resource.String.menu_Download) },
                { "impostazioni", GetString(Resource.String.menu_Settings) },
                { "crediti", GetString(Resource.String.menu_Credits) },
                { "ciccio", "ddddddd" }
            };

            _Title = _DrawerTitle = Title = this.AppName();

            _Drawer        = this.FindViewById <DrawerLayout>(Resource.Id.drawer_layout);
            _DrawerList    = this.FindViewById <ListView>(Resource.Id.left_drawer);
            _DrawerContent = this.FindViewById <LinearLayout>(Resource.Id.drawer_content);

            _Drawer.SetBackgroundColor(Color.Transparent.FromHex("eeeeee"));

            //voci menu
            List <string> Sections = new List <string>();

            if (DataManager.Get <ISettingsManager>().Settings.EdicolaEnabled)
            {
                MenuItems.Add("edicola");
                Sections.Add(MenuLabels["edicola"]);
            }

            if (DataManager.Get <ISettingsManager>().Settings.DownloadEnabled)
            {
                MenuItems.Add("download");
                Sections.Add(MenuLabels["download"]);
            }

            if (DataManager.Get <ISettingsManager>().Settings.SettingsEnabled)
            {
                MenuItems.Add("impostazioni");
                Sections.Add(MenuLabels["impostazioni"]);
            }

            MenuItems.Add("crediti");
            Sections.Add(MenuLabels["crediti"]);

            //_DrawerList.Adapter = new ArrayAdapter<string>(this, Resource.Layout.DrawerListItem, Sections);
            _DrawerList.Adapter = new DrawerAdapter <string>(this, Resource.Layout.DrawerListItem, Sections);

            _DrawerList.ItemClick += (sender, args) => ListItemClicked(args.Position);

            _Drawer.SetDrawerShadow(Resource.Drawable.drawer_shadow_light, (int)Android.Views.GravityFlags.Start);

            _DrawerContent.SetBackgroundColor(Color.Transparent.FromHex(DataManager.Get <ISettingsManager>().Settings.MenuFondoColor));
            _DrawerList.SetBackgroundColor(Color.Transparent.FromHex(DataManager.Get <ISettingsManager>().Settings.MenuFondoColor));

            _DrawerList.Divider       = new ColorDrawable(Color.Transparent);
            _DrawerList.DividerHeight = 8;

            _DrawerToggle = new MyActionBarDrawerToggle(this, _Drawer,
                                                        Resource.Drawable.ic_drawer,
                                                        Resource.String.DrawerOpen,
                                                        Resource.String.DrawerClose);


            //Display the current fragments title and update the options menu
            _DrawerToggle.DrawerClosed += (o, args) =>
            {
                this.ActionBar.Title = _Title;
                ActionBar.SetDisplayShowCustomEnabled(true);
                this.InvalidateOptionsMenu();
            };

            //Display the drawer title and update the options menu
            _DrawerToggle.DrawerOpened += (o, args) =>
            {
                this.ActionBar.Title = _DrawerTitle;
                ActionBar.SetDisplayShowCustomEnabled(false);
                this.InvalidateOptionsMenu();
            };

            //Set the drawer lister to be the toggle.
            _Drawer.SetDrawerListener(this._DrawerToggle);


            //if first time you will want to go ahead and click first item.
            if (bundle == null)
            {
                ListItemClicked(0);
            }

            /*string color = "#" + DataManager.Get<ISettingsManager>().Settings.NavigationBarColor;
             * ActionBar.SetBackgroundDrawable(new ColorDrawable(Color.ParseColor(color)));
             *
             * int amId = this.Resources.GetIdentifier("action_context_bar", "id", "android");
             * View view= FindViewById(amId);
             * view.SetBackgroundColor(Color.ParseColor(color));
             */

            ActionBar.SetCustomView(Resource.Layout.CustomActionBar);
            ActionBar.SetDisplayShowCustomEnabled(true);

            ActionBar.SetDisplayHomeAsUpEnabled(true);
            ActionBar.SetHomeButtonEnabled(true);
            //ActionBar.SetDisplayShowHomeEnabled (false);
            ActionBar.SetIcon(Android.Resource.Color.Transparent);

            ActionBar.SetBackgroundDrawable(new ColorDrawable(Color.Transparent.FromHex(DataManager.Get <ISettingsManager>().Settings.NavigationBarColor)));

            //ActionBar.CustomView.Background.Colorize(DataManager.Get<ISettingsManager>().Settings.NavigationBarColor);

            var btnBack = ActionBar.CustomView.FindViewById <Button>(Resource.Id.btnBack);

            btnBack.SetTextColor(Color.Transparent.FromHex(DataManager.Get <ISettingsManager>().Settings.TintColor));

            btnBack.Click += (sender, e) =>
            {
                if (_CurrentItem == "crediti" || _CurrentItem == "impostazioni")
                {
                    _Drawer.OpenDrawer(_DrawerContent);
                }
            };

            //colore titolo
            var titleId = Resources.GetIdentifier("action_bar_title", "id", "android");
            var abTitle = FindViewById <TextView>(titleId);

            abTitle.SetTextColor(Color.Transparent.FromHex(DataManager.Get <ISettingsManager>().Settings.TintColor));

            //se è un'applicazione edicola e non ci sono documenti lo mando direttamente ai downloads
            if (DataManager.Get <ISettingsManager>().Settings.EdicolaEnabled&& DataManager.Get <ISettingsManager>().Settings.DownloadEnabled)
            {
                if (FileSystemManager.ElemetsInBaseDir == 0)
                {
                    //if(/*this.CanAccessExternal() &&*/ FileSystemManager.DocumentsToImport == 0)
                    GoTo(1);
                }
            }

            //coloro l'icona di navigazione
            ColorizeDrawer();
        }
コード例 #2
0
        private async Task BillProcess(Product prod)
        {
            var result = await _billingHandler.BuyProduct(prod);

            var returnData = new Dictionary <string, string>();

            if (result.Result == BillingResult.OK)
            {
                if (result.Purchase != null && result.Data != null)
                {
                    string orderID = "";
                    if (result.Purchase.OrderId == null || result.Purchase.OrderId == "")
                    {
                        orderID = result.Purchase.PurchaseToken;
                    }

                    var data = new Dictionary <string, string>();
                    data.Add("packageName", Activity.PackageName);
                    data.Add("orderId", orderID);
                    data.Add("productId", result.Purchase.ProductId);
                    data.Add("developerPayload", result.Purchase.DeveloperPayload);
                    data.Add("purchaseTime", result.Purchase.PurchaseTime.ToString());
                    data.Add("purchaseToken", result.Purchase.PurchaseToken);
                    data.Add("purchaseState", result.Purchase.PurchaseState.ToString());

                    returnData = Notification.CheckRegisterPurchase(Convert.ToBase64String(System.Text.Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(data))));
                }
            }
            else if (result.Result == BillingResult.ItemAlreadyOwned)
            {
                var purch = await _billingHandler.GetPurchases(ItemType.Product);

                foreach (var p in purch)
                {
                    if (p.ProductId == prod.ProductId)
                    {
                        string orderID = "";
                        if (p.OrderId == null || p.OrderId == "")
                        {
                            orderID = p.PurchaseToken;
                        }

                        var data = new Dictionary <string, string>();
                        data.Add("packageName", Activity.PackageName);
                        data.Add("orderId", orderID);
                        data.Add("productId", p.ProductId);
                        data.Add("developerPayload", p.DeveloperPayload);
                        data.Add("purchaseTime", p.PurchaseTime.ToString());
                        data.Add("purchaseToken", p.PurchaseToken);
                        data.Add("purchaseState", p.PurchaseState.ToString());

                        returnData = Notification.CheckRegisterPurchase(Convert.ToBase64String(System.Text.Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(data))));

                        break;
                    }
                }
            }
            else if (result.Result == BillingResult.UserCancelled)
            {
            }
            else
            {
                returnData["success"]   = "false";
                returnData["errorCode"] = result.Result.ToString();
            }

            if (returnData["success"].ToLower() == "true")
            {
                var sub = _Abbonamenti.Where(d => d.IapID == prod.ProductId).FirstOrDefault();

                if (sub != null && sub.IapID != "") //abbonamento
                {
                    this.PopulateTable();
                }
                else //prodotto
                {
                    Activity.RunOnUiThread(() =>
                    {
                        var uri = new Uri(DataManager.Get <ISettingsManager>().Settings.DownloadUrl + "data/accounts/" + returnData["account"] + "/pub/" + returnData["path"]);
                        MBDownloadManager.RequestDownload(uri, new VoidNotify());

                        this.PopulateTable();
                    });
                }
            }
            else
            {
                string msgError = "";
                switch (returnData["errorCode"])
                {
                case "401":
                    msgError = Activity.GetString(Resource.String.iap_authenticationError);
                    break;

                case "403":
                    msgError = Activity.GetString(Resource.String.iap_unauthorizedUser);
                    break;

                case "402":
                    msgError = Activity.GetString(Resource.String.iap_transactionError);
                    break;

                case "503":
                    msgError = Activity.GetString(Resource.String.gen_tryLater);
                    break;

                case "505":
                    msgError = Activity.GetString(Resource.String.gen_tryLater);
                    break;
                }

                Activity.RunOnUiThread(() =>
                {
                    var alert = new AlertDialog.Builder(Activity);
                    alert.SetTitle(GetString(Resource.String.gen_error));

                    if (msgError != "")
                    {
                        alert.SetMessage(msgError + " [" + returnData["errorCode"] + "]");
                    }
                    else
                    {
                        alert.SetMessage("code: " + returnData["errorCode"]);
                    }

                    alert.SetPositiveButton("Ok", (EventHandler <DialogClickEventArgs>)null);
                    alert.Show().SetDivider();
                });
            }
        }