无法通过AWS Cognito“ ForgotPasswordHandler”回调更新UI

时间:2019-04-04 21:27:01

标签: java android amazon-cognito

我无法从AWS ForgotPasswordHandler回调中更新UI。重置密码时将调用onSuccess方法,但是该代码块中的代码没有影响。

我将AWS Cognito与我的android应用程序一起用于注册,登录模块。对于密码重置,AWS提供了回叫功能ForgotPasswordHandler,在发送代码后,用户需要提供电子邮件以获取代码。用户需要输入验证码和新密码。发送验证码,验证码并设置新密码是通过名为ForgotPasswordContinuation的对象完成的。当我单击获取代码并关闭活动以通过电子邮件阅读代码并返回活动时,ForgotPasswordContinuation状态丢失,并且我无法将验证码和新密码发送到服务器。我获得代码时使用的方法是调用回调的getResetCode函数,并通过后台线程保存ForgotPasswordContinuation的状态。再次打开活动时,我取回ForgotPasswordContinuation对象并发送新的验证码和密码。一切正常,回调onSuccess被调用,以确保密码已成功更改。当我尝试在此finish()中调用onSuccess时,它没有任何影响,并且活动没有被破坏。我把代码放在主线程UI,处理程序上运行,但是没有运气。我该如何运作?我尝试将微调器隐藏在回调中并完成活动,但没有结果。肯定会调用onSuccess,因为登录时可以看到onSuccess方法中的日志文本。另外,如果我没有关闭该活动并从浏览器中读取确认代码,然后输入代码和新密码,也不会退出获取代码屏幕,那么onSuccess可以正常工作。仅当我尝试退出活动以打开gmail并读取确认代码(当我回来输入新密码和代码并执行然后调用onSuccess却被调用,但其中的代码没有影响,才发生问题)微调框被隐藏,finish()调用也不起作用。

    GifImageView rotateLoading;
    TextView tryagain;
    private ForgotPasswordContinuation resultContinuation;
    Context context = ForgotPasswordActivity.this;
    private LinearLayout getCodeLayout,confirmationLayout;
    TextView forgotPassText;
    BackgroundService myServiceObject;
    private boolean verified = false;

    private ServiceConnection connection = new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName className, IBinder service) {
            BackgroundService.LocalBinder localBinder = (BackgroundService.LocalBinder) service;
            myServiceObject = localBinder.getService();
        }
        @Override
        public void onServiceDisconnected(ComponentName arg0) {
        }
    };


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);


        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);

        setContentView(R.layout.activity_forgot_password);

        Intent intent = new Intent(context, BackgroundService.class);
        context.startService(intent);
        context.bindService(intent, connection, Context.BIND_AUTO_CREATE);


        tryagain = findViewById(R.id.tryagain);
        forgotPassText = findViewById(R.id.forgotPassText);
        getCodeLayout = findViewById(R.id.getCodeLayout);
        confirmationLayout = findViewById(R.id.confirmationLayout);
        rotateLoading = findViewById(R.id.rotateloading);

        tryagain.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                updateVariable(context,"LaunchScreen","0");
                getCodeLayout.setVisibility(View.VISIBLE);
                confirmationLayout.setVisibility(View.GONE);
                forgotPassText.setText("Enter the email address associated with your account");
            }
        });

        if(getVariable(context,"LaunchScreen").equals("3")) {
            getCodeLayout.setVisibility(View.GONE);
            confirmationLayout.setVisibility(View.VISIBLE);
            forgotPassText.setText("Enter verification code and set new password");
        }else {
            getCodeLayout.setVisibility(View.VISIBLE);
            confirmationLayout.setVisibility(View.GONE);
            forgotPassText.setText("Email verification");
        }

        final EditText editTextUsername = findViewById(R.id.editTextUsername);

        if(!getVariable(context,"email").equals("0")){
            editTextUsername.setText(getVariable(context,"email"));
        }

        Button buttonGetCode = findViewById(R.id.buttonGetCode);
        buttonGetCode.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                if(getInputText(editTextUsername).isEmpty()){
                    Utils.showAlertMessage(context, "Please enter email");
                }else{
                    if(Utils.isEmailValid(String.valueOf(editTextUsername.getText()))){
                        rotateLoading.setVisibility(View.VISIBLE);

                        CognitoSettings cognitoSettings = new CognitoSettings(ForgotPasswordActivity.this);
                        CognitoUser thisUser = cognitoSettings.getUserPool().getUser(String.valueOf(editTextUsername.getText()));

                        updateVariable(context,"email",String.valueOf(editTextUsername.getText()));

                        Utils.showLog(ForgotPasswordActivity.this,"calling forgot password to get confirmation code....");

                        thisUser.forgotPasswordInBackground(callback);
                    }else {
                        Utils.showAlertMessage(context, "Please enter correct email");
                    }
                }
            }
        });


        final EditText editTextCode = findViewById(R.id.editTextCode);
        final EditText editTextNewPassword = findViewById(R.id.editTextNewPassword);

        Button buttonResetPassword = findViewById(R.id.buttonResetPassword);
        buttonResetPassword.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                if(getInputText(editTextCode).isEmpty()|| getInputText(editTextNewPassword).isEmpty()){
                    Utils.showAlertMessage(context, "Please enter both verification code and new password");
                }else{
                    if(getInputText(editTextNewPassword).length()<6){
                        Utils.showAlertMessage(context, "Password must be at least 6 characters");
                    }else {
                        rotateLoading.setVisibility(View.VISIBLE);

                        Utils.showLog(ForgotPasswordActivity.this,"got code & password, setting continuation object....");

                        if(myServiceObject.getContinuationObject() !=null){
                            Log.e("Teeeeesssttt","myServiceObject.getContinuationObject() !=null");
                            resultContinuation = myServiceObject.getContinuationObject();
                        }

                        resultContinuation.setPassword(String.valueOf(editTextNewPassword.getText()));
                        resultContinuation.setVerificationCode(String.valueOf(editTextCode.getText()));

                        Utils.showLog(ForgotPasswordActivity.this,"got code & password, calling continueTask()....");

                        // Let the forgot password process continue
                        resultContinuation.continueTask();
                    }
                }
            }
        });
    }

    private ForgotPasswordHandler callback = new ForgotPasswordHandler() {
        @Override
        public void onSuccess() {
            /*Forgotten password process completed successfully
             new password has been successfully set*/
            Utils.showToast(ForgotPasswordActivity.this,"Password changed successfully");

            verified = true;


            Handler h = new Handler(context.getMainLooper());
            h.post(new Runnable() {
                @Override
                public void run() {
                    if(rotateLoading.getVisibility() == View.VISIBLE){
                        rotateLoading.setVisibility(View.GONE);
                    }
                }
            });

        }

        @Override
        public void getResetCode(ForgotPasswordContinuation continuation) {


            Utils.showLog(ForgotPasswordActivity.this,"in getResetCode....");

             /* A code will be sent
             , use the "continuation" object to continue with the forgot password process*/
            // This will indicate where the code was sent
            CognitoUserCodeDeliveryDetails codeSentHere = continuation.getParameters();

            Utils.showToast(ForgotPasswordActivity.this,"Code sent here: " + codeSentHere.getDestination()+". Don't forget to check SPAM folder");
            forgotPassText.setText("Enter verification code and set new password");
            // create field so we can use the continuation object in our reset password button
            resultContinuation = continuation;

            myServiceObject.setContinuationObject(continuation);

            getCodeLayout.setVisibility(View.GONE);
            confirmationLayout.setVisibility(View.VISIBLE);

            Handler h = new Handler(context.getMainLooper());
            h.post(new Runnable() {
                @Override
                public void run() {
                    if(rotateLoading.getVisibility() == View.VISIBLE){
                        rotateLoading.setVisibility(View.GONE);
                    }
                }
            });

        }

        public void onFailure(final Exception exception) {

            // Forgot password processing failed

            Handler h = new Handler(context.getMainLooper());
            h.post(new Runnable() {
                @Override
                public void run() {
                    if(rotateLoading.getVisibility() == View.VISIBLE){
                        rotateLoading.setVisibility(View.GONE);
                    }

                    String errorParts[] = exception.getMessage().split("\\(");
                    Utils.showAlertMessage(ForgotPasswordActivity.this,errorParts[0]);
                }
            });
        }
    };

    @Override
    protected void onStop() {
        super.onStop();

        Log.e("Txt","onStop");

        try {
            unbindService(connection);
        }catch (IllegalArgumentException e){
            e.printStackTrace();
        }

        if(confirmationLayout.getVisibility() == View.VISIBLE){
            if(verified){
                updateVariable(context,"LaunchScreen","0");
            }else {
                updateVariable(context,"LaunchScreen","3"); //  enter code and reset password
            }
        }else if(getCodeLayout.getVisibility() == View.VISIBLE) {
            // get code layout
            Intent mainIntent = new Intent(context,LoginActivity.class);
            startActivity(mainIntent);
        }
    }

onSuccess方法调用我想隐藏微调器并完成活动,但是它没有执行该代码,因此无济于事。

0 个答案:

没有答案