コード例 #1
0
 /// <summary>
 /// The run checks.
 /// </summary>
 private void RunChecks()
 {
     while (this.pendingChecks.Any())
     {
         LicenseValidator validator = this.pendingChecks.Dequeue();
         try
         {
             Debug.WriteLine("Calling CheckLicense on service for " + validator.GetPackageName());
             this.licensingService.CheckLicense(
                 validator.GetNumberUsedOnce(), validator.GetPackageName(), new ResultListener(validator, this));
             this.checksInProgress.Add(validator);
         }
         catch (RemoteException e)
         {
             Debug.WriteLine("RemoteException in CheckLicense call. " + e.Message);
             this.HandleServiceConnectionError(validator);
         }
     }
 }
            /// <summary>
            /// Initializes a new instance of the <see cref="ResultListener"/> class.
            /// </summary>
            /// <param name="validator">
            /// The validator.
            /// </param>
            /// <param name="checker">
            /// The checker.
            /// </param>
            public ResultListener(LicenseValidator validator, LicenseChecker checker)
            {
                this.checker = checker;
                this.licenseValidator = validator;
                this.onTimeout = delegate
                    {
                        Debug.WriteLine("License check timed out.");

                        this.checker.HandleServiceConnectionError(this.licenseValidator);
                        this.checker.FinishCheck(this.licenseValidator);
                    };
                this.StartTimeout();
            }
 /// <summary>
 /// The finish check.
 /// </summary>
 /// <param name="validator">
 /// The validator.
 /// </param>
 private void FinishCheck(LicenseValidator validator)
 {
     lock (this.locker)
     {
         this.checksInProgress.Remove(validator);
         if (this.checksInProgress.Any())
         {
             this.CleanupService();
         }
     }
 }
        /// <summary>
        /// Generates policy response for service connection errors, as a result of disconnections or timeouts.
        /// </summary>
        /// <param name="validator">
        /// The validator.
        /// </param>
        private void HandleServiceConnectionError(LicenseValidator validator)
        {
            lock (this.locker)
            {
                this.policy.ProcessServerResponse(PolicyServerResponse.Retry, null);

                if (this.policy.AllowAccess())
                {
                    validator.GetCallback().Allow(PolicyServerResponse.Retry);
                }
                else
                {
                    validator.GetCallback().DontAllow(PolicyServerResponse.Retry);
                }
            }
        }
        /// <summary>
        /// Checks if the user should have access to the app. Binds the service if necessary.
        /// </summary>
        /// <param name="callback">
        /// The callback.
        /// </param>
        public void CheckAccess(ILicenseCheckerCallback callback)
        {
            lock (this.locker)
            {
                // If we have a valid recent LICENSED response, we can skip asking Market/Play.
                if (this.policy.AllowAccess())
                {
                    System.Diagnostics.Debug.WriteLine("Using cached license response");
                    callback.Allow(PolicyServerResponse.Licensed);
                }
                else
                {
                    var validator = new LicenseValidator(
                        this.policy, 
                        new NullDeviceLimiter(), 
                        callback, 
                        GenerateNumberUsedOnce(), 
                        this.packageName, 
                        this.versionCode);

                    if (this.licensingService == null)
                    {
                        try
                        {
                            var i = new Intent(LicensingServiceIntentString);
                            i.SetPackage(LicensingServicePackageString);

                            if (this.context.BindService(i, this, Bind.AutoCreate))
                            {
                                this.pendingChecks.Enqueue(validator);
                            }
                            else
                            {
                                Debug.WriteLine("Could not bind to service.");
                                this.HandleServiceConnectionError(validator);
                            }
                        }
                        catch (Java.Lang.SecurityException)
                        {
                            callback.ApplicationError(CallbackErrorCode.MissingPermission);
                        }
                        catch (Exception ex)
                        {
                            Debug.WriteLine(ex.StackTrace);
                        }
                    }
                    else
                    {
                        this.pendingChecks.Enqueue(validator);
                        this.RunChecks();
                    }
                }
            }
        }