Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Resolved OTP issue #2923

Merged
merged 3 commits into from
Dec 31, 2024
Merged

Resolved OTP issue #2923

merged 3 commits into from
Dec 31, 2024

Conversation

j13panchal
Copy link

Solve OTP issue

Summary

Feature Flag

Product Description

PR Checklist

  • If I think the PR is high risk, "High Risk" label is set
  • I have confidence that this PR will not introduce a regression for the reasons below
  • Do we need to enhance manual QA test coverage ? If yes, "QA Note" label is set correctly
  • Does the PR introduce any major changes worth communicating ? If yes, "Release Note" label is set and a "Release Note" is specified in PR description.

Automated test coverage

Safety story

Solve OTP issue
Copy link

coderabbitai bot commented Dec 13, 2024

📝 Walkthrough

Walkthrough

The changes in the CustomOtpView class focus on enhancing the functionality related to OTP (One-Time Password) input handling. Key modifications include commenting out the InputFilter that limited the input length of EditText, which allows for more flexible character input. A new OnKeyListener has been introduced to manage character deletion; when the delete key is pressed, it clears the current EditText and shifts focus to the previous field if applicable. The TextWatcher has been updated to effectively manage input by overwriting existing values when multiple characters are entered, ensuring only the last character remains. Additionally, the afterTextChanged method has been modified to prevent recursive calls using a flag to track self-initiated changes. The setOtp method has been adjusted to clear focus from all EditText fields after setting the OTP, enhancing user experience. Two new interfaces, OtpCompleteListener and OnOtpChangedListener, have also been added to facilitate better handling of OTP completion and changes.

Possibly related PRs

Suggested reviewers

  • OrangeAndGreen

Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR. (Experiment)
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 3

🧹 Outside diff range and nitpick comments (2)
app/src/org/commcare/views/connect/CustomOtpView.java (2)

100-112: Consider additional keyboard event handling

The deletion handling looks good, but consider these edge cases:

  1. Hardware keyboard events
  2. Different Android keyboard implementations
  3. Input Method Editor (IME) specific events

Consider adding a comment explaining the deletion behavior and testing with different keyboard types.


Line range hint 1-241: Consider security implications of OTP handling

While the implementation improvements are good, consider these security aspects:

  1. Add input sanitization to prevent injection attacks
  2. Implement clipboard handling policy for sensitive OTP data
  3. Consider auto-clear functionality after a timeout
  4. Add visual feedback for remaining time if OTP is time-sensitive

Would you like assistance in implementing any of these security enhancements?

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 169247b and a4bf545.

📒 Files selected for processing (1)
  • app/src/org/commcare/views/connect/CustomOtpView.java (5 hunks)
🔇 Additional comments (1)
app/src/org/commcare/views/connect/CustomOtpView.java (1)

83-83: Document the rationale for removing the input length filter

While removing the length filter enables more flexible input handling (e.g., paste operations), this change should be documented to explain the design decision and potential implications.

Let's check if this change is consistent across the codebase:

✅ Verification successful

Removing input length filter is intentional and safe

The removal of the input length filter in CustomOtpView is consistent with the codebase as:

  1. The DecimalWidget's length filter (15 chars) serves a different purpose for decimal input
  2. The CustomOtpView still maintains input integrity through:
    • InputType.TYPE_CLASS_NUMBER restriction
    • TextWatcher implementation for handling input validation
  3. No other OTP implementations are affected by this change
🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Search for similar OTP input implementations that might need the same update
rg -l "InputFilter\.LengthFilter.*1.*" --type java

Length of output: 160


Script:

#!/bin/bash
# Let's check the context of LengthFilter usage in DecimalWidget.java
rg "InputFilter\.LengthFilter.*1.*" -B 2 -A 2 app/src/org/commcare/views/widgets/DecimalWidget.java

# Also check if there are any other OTP-related files
rg -l "OTP|otp" --type java

Length of output: 875


Script:

#!/bin/bash
# Let's check how CustomOtpView is used in other OTP-related files
rg "CustomOtpView" -B 2 -A 2 app/src/org/commcare/fragments/connectId/ConnectIdPhoneVerificationFragmnet.java app/src/org/commcare/fragments/connectId/ConnectIdUserDeactivateOTPVerificationFragment.java

# Also check the implementation details around the commented line
rg "setFilters|TextWatcher|addTextChangedListener" -B 2 -A 2 app/src/org/commcare/views/connect/CustomOtpView.java

Length of output: 926

Comment on lines +115 to +116
private boolean isSelfTriggered = false;

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

Ensure thread safety of the isSelfTriggered flag

The isSelfTriggered flag could potentially cause issues in multi-threaded scenarios. Consider using AtomicBoolean or synchronization.

-    private boolean isSelfTriggered = false;
+    private final AtomicBoolean isSelfTriggered = new AtomicBoolean(false);

Also applies to: 123-124

Comment on lines 139 to 142
} else if (s.length() > 1) {
// If more than one character, replace with the latest input
editText.setText(String.valueOf(s.charAt(s.length() - 1)));
// editText.setSelection(1); // Move cursor to the end
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

Add input validation for numeric input

Since InputType.TYPE_CLASS_NUMBER is set, we should validate that the input is actually numeric to prevent potential crashes.

 } else if (s.length() > 1) {
-    editText.setText(String.valueOf(s.charAt(s.length() - 1)));
+    char lastChar = s.charAt(s.length() - 1);
+    if (Character.isDigit(lastChar)) {
+        editText.setText(String.valueOf(lastChar));
+    } else {
+        editText.setText("");
+    }
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
} else if (s.length() > 1) {
// If more than one character, replace with the latest input
editText.setText(String.valueOf(s.charAt(s.length() - 1)));
// editText.setSelection(1); // Move cursor to the end
} else if (s.length() > 1) {
// If more than one character, replace with the latest input
char lastChar = s.charAt(s.length() - 1);
if (Character.isDigit(lastChar)) {
editText.setText(String.valueOf(lastChar));
} else {
editText.setText("");
}
// editText.setSelection(1); // Move cursor to the end

Comment on lines +236 to 239
editText.setText("");
}
editText.clearFocus(); // Ensure no field remains focused
}
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Enhance setOtp method with validation and notification

Consider these improvements:

  1. Validate that input contains only digits
  2. Notify listeners when OTP is set programmatically
 public void setOtp(String otp) {
     if (otp.length() > digitCount) {
         throw new IllegalArgumentException("OTP length exceeds the digit count");
     }
+    if (!otp.matches("\\d*")) {
+        throw new IllegalArgumentException("OTP must contain only digits");
+    }
 
     for (int i = 0; i < digitCount; i++) {
         EditText editText = (EditText) getChildAt(i);
         if (i < otp.length()) {
             editText.setText(String.valueOf(otp.charAt(i)));
         } else {
             editText.setText("");
         }
         editText.clearFocus();
     }
+    // Notify listeners
+    if (otpChangedListener != null) {
+        otpChangedListener.onOtpChanged(otp);
+    }
+    if (otp.length() == digitCount && otpCompleteListener != null) {
+        otpCompleteListener.onOtpComplete(otp);
+    }
 }

Committable suggestion skipped: line range outside the PR's diff.

@OrangeAndGreen OrangeAndGreen merged commit b4fe7c6 into connect_qa Dec 31, 2024
1 of 2 checks passed
@OrangeAndGreen OrangeAndGreen deleted the jp/issue-otp-view branch December 31, 2024 17:31
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants