/// <summary>
        /// Start read sms manager
        /// </summary>
        private void StartReadSmsManager()
        {
            Task readSmsManagerTask = ReadSmsManager.Start(this);

            readSmsManagerTask.AddOnCompleteListener
            (
                new OnCompleteListener
                (
                    "Read Sms Manager Service Started",
                    "Read Sms Manager Service Failed"
                )
            );
        }
        /// <summary>
        /// Sign in Background
        /// </summary>
        private bool SignInBackend()
        {
            Log.Logger.Info(Tag, "signInBackend");
            ClearAccountInfo();

            if (service == null)
            {
                return(false);
            }

            CountDownLatch countDownLatch = new CountDownLatch(1);

            Huawei.Hmf.Tasks.Task task = service.SilentSignIn();
            task.AddOnSuccessListener(new OnSuccessListener(delegate(Java.Lang.Object authHuaweiId)
            {
                Log.Logger.Info(Tag, "silentSignIn success");
                DealSignInResult((AuthHuaweiId)authHuaweiId);
                countDownLatch.CountDown();
            }
                                                            ));

            task.AddOnFailureListener(new OnFailureListener(delegate(Java.Lang.Exception authHuaweiId)
            {
                Log.Logger.Info(Tag, "silentSignIn error");
                countDownLatch.CountDown();
            }
                                                            ));

            try
            {
                countDownLatch.Await(15, TimeUnit.Seconds);
            }
            catch (Java.Lang.InterruptedException e)
            {
                Log.Logger.Info(Tag, "signInBackend catch InterruptedException");
                countDownLatch.CountDown();
            }

            if (TextUtils.IsEmpty(AccessToken))
            {
                return(false);
            }
            else
            {
                return(true);
            }
        }
        /// <summary>
        /// Convert HuaweiTask to System.Threading.Task with Generic return type
        /// </summary>
        /// <typeparam name="TResult">Return type,a Java object</typeparam>
        /// <param name="HuaweiTask">Huawei.Hmf.Tasks.Task class</param>
        /// <returns>System.Threading.Task with wrapped a generic type</returns>
        public static Task <TResult> CastTask <TResult>(this HuaweiTask HuaweiTask) where TResult : Java.Lang.Object
        {
            var tcs = new TaskCompletionSource <TResult>();

            HuaweiTask.AddOnCompleteListener(new HuaweiTaskCompleteListener(
                                                 t =>
            {
                if (t.Exception == null)
                {
                    tcs.TrySetResult(t.Result.JavaCast <TResult>());
                }
                else
                {
                    tcs.TrySetException(t.Exception);
                }
            }));

            return(tcs.Task);
        }
 /// <summary>
 /// Revoking HUAWEI ID Authorization
 /// </summary>
 private void CancelAuthorization()
 {
     try
     {
         Task cancelAuthorizationTask = authManager.CancelAuthorization();
         cancelAuthorizationTask.AddOnCompleteListener
         (
             new OnCompleteListener
             (
                 "Cancel Authorization Success",
                 "Cancel Authorization Failed"
             )
         );
     }
     catch (Exception e)
     {
         Log.InfoFunc(TAG, "Cancel Authorization failed: " + e.Message);
     }
 }
        public static Task CastTask(this HuaweiTask HuaweiTask)
        {
            var tcs = new TaskCompletionSource <bool>();

            HuaweiTask.AddOnCompleteListener(new MyCompleteListener(
                                                 t =>
            {
                if (t.Exception == null)
                {
                    tcs.TrySetResult(false);
                }
                else
                {
                    tcs.TrySetException(t.Exception);
                }
            }
                                                 ));

            return(tcs.Task);
        }
        protected override void OnActivityResult(int requestCode, Result resultCode, Intent data)
        {
            base.OnActivityResult(requestCode, resultCode, data);
            if (requestCode == Constant.RequestSignInLogin)
            {
                //login success
                //get user message by ParseAuthResultFromIntent
                Task authHuaweiIdTask = HuaweiIdAuthManager.ParseAuthResultFromIntent(data);
                if (authHuaweiIdTask.IsSuccessful)
                {
                    AuthHuaweiId huaweiAccount = (AuthHuaweiId)authHuaweiIdTask.Result;
                    Log.InfoFunc(TAG, huaweiAccount.DisplayName + " signIn success ");
                    Log.InfoFunc(TAG, "AccessToken: " + huaweiAccount.AccessToken);

                    ValidateIdToken(huaweiAccount.IdToken);
                }
                else
                {
                    Log.InfoFunc(TAG, "signIn failed: " + (authHuaweiIdTask.Exception).ToString());
                }
            }
            if (requestCode == Constant.RequestSignInLoginCode)
            {
                //login success
                Task authHuaweiIdTask = HuaweiIdAuthManager.ParseAuthResultFromIntent(data);
                if (authHuaweiIdTask.IsSuccessful)
                {
                    AuthHuaweiId huaweiAccount = (AuthHuaweiId)authHuaweiIdTask.Result;
                    Log.InfoFunc(TAG, "signIn get code success.");
                    Log.InfoFunc(TAG, "ServerAuthCode: " + huaweiAccount.AuthorizationCode);

                    /**** english doc:For security reasons, the operation of changing the code to an AT must be performed on your server. The code is only an example and cannot be run. ****/
                    /**********************************************************************************************/
                }
                else
                {
                    Log.InfoFunc(TAG, "signIn get code failed: " + (authHuaweiIdTask.Exception).ToString());
                }
            }
        }
 /// <summary>
 /// Signing Out from HUAWEI ID
 /// </summary>
 private void SignOut()
 {
     try
     {
         Task signOutTask = authManager.SignOut();
         signOutTask.AddOnSuccessListener
         (
             new OnSuccessListener
             (
                 "SignOut Success"
             )
         ).AddOnFailureListener(
             new OnFailureListener
             (
                 "SignOut Failed"
             )
             );
     }
     catch (System.Exception e)
     {
         Log.InfoFunc(TAG, "SignOut Failed: " + e.Message);
     }
 }
 /// <summary>
 /// Silently Signing In With HUAWEI ID
 /// </summary>
 private void SilentSignIn()
 {
     try
     {
         Task silentSignInTask = authManager.SilentSignIn();
         silentSignInTask.AddOnSuccessListener
         (
             new OnSuccessListener
             (
                 "SilentSignIn Success"
             )
         ).AddOnFailureListener(
             new OnFailureListener
             (
                 "SilentSignIn Failed"
             )
             );
     }
     catch (System.Exception e)
     {
         //if Failed use SignIn
         SignIn();
     }
 }
 public void OnComplete(HuaweiTask task)
 => OnCompleteHandler?.Invoke(task);