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

fix(network): modernize simplenetworkdetector with updated android apis #736

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

orioonyx
Copy link

@orioonyx orioonyx commented Jan 9, 2025

Related issue

This pull request resolves issue #735.

Summary of changes

This PR updates the SimpleNetworkDetector class to replace deprecated Android APIs with their modern counterparts while ensuring backward compatibility:

  1. For API 29 and above:
    • Replaced getActiveNetworkInfo() with getActiveNetwork() and getNetworkCapabilities().
    • Used NetworkCapabilities.TRANSPORT_* constants to determine network type.
  2. For API 28 and below:
    • Retained the existing logic using getActiveNetworkInfo() and TYPE_* constants to support older devices.

Additional improvements include:

  • Added null checks to handle cases where modern APIs might return null.
  • Simplified and aligned the code for better readability.

Why these changes are important

  • Aligning with modern Android APIs ensures compatibility with Android 10 and newer versions.
  • Maintaining backward compatibility supports older Android versions, benefiting a broader range of users.
  • Improving code readability and structure enhances maintainability for future contributors.

Acknowledgment

Thank you for providing this opportunity to contribute to the project. I have done my best to follow the contribution guidelines and ensure the changes adhere to project standards.

If there are any further improvements or adjustments required, please let me know. I will be more than happy to address any feedback promptly.

Thank you for your time and consideration!

@orioonyx orioonyx requested a review from a team as a code owner January 9, 2025 12:42
Copy link

linux-foundation-easycla bot commented Jan 9, 2025

CLA Signed


The committers listed above are authorized under a signed CLA.

// Determine network type based on transport capabilities
if (capabilities.hasTransport(NetworkCapabilities.TRANSPORT_CELLULAR)) {
return CurrentNetwork.builder(NetworkState.TRANSPORT_CELLULAR)
.subType("") // Additional details can be added
Copy link
Member

Choose a reason for hiding this comment

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

Not possible to do activeNetwork.getSubtypeName() like the older APIs?

Copy link
Author

Choose a reason for hiding this comment

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

Thank you for the question!

Unfortunately, getSubtypeName() is not available in the newer APIs like NetworkCapabilities. The newer APIs focus on transport-level details (e.g., cellular, WiFi) rather than providing specific subtype information like "LTE" or "HSPA."

Thank you for your review and feedback! 😊

Copy link
Member

@marandaneto marandaneto left a comment

Choose a reason for hiding this comment

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

left a question otherwise LGTM

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.

Modernize SimpleNetworkDetector with updated Android network APIs
2 participants