Skip to content

NetworkUtils: getNetworkType() broken on Android 12+ due to READ_PHONE_STATE requirement #6891

Description

@chrisdebian

Problem

`NetworkUtils.getNetworkType()` always returns `UNKNOWN` for cellular connections on Android 12+ (API 31).

The method was written using `TelephonyManager.networkType`, which requires the `READ_PHONE_STATE` dangerous permission on API 31+. Because the app does not declare this permission, the cellular detection block was commented out with `// TODO for Android 12+ request permission from user is mandatory`, leaving the method unconditionally returning `UNKNOWN` for all non-WiFi connections.

Impact

`getNetworkType()` is called only from `DeviceInfoUtil.getConnectionType()`, which feeds crash reports and the feedback dialog. Cellular users' connection type appears as `UNKNOWN` rather than `CELLULAR` in diagnostics.

Fix

Replace `TelephonyManager.networkType` with `ConnectivityManager.getNetworkCapabilities()` (API 21+, no dangerous permission required). This correctly identifies WiFi vs. cellular. Generation detection (2G/3G/4G) cannot be recovered without `READ_PHONE_STATE`, but since all non-WiFi paths already returned `UNKNOWN`, there is no regression.

The fix also removes the large commented-out block, removes the unused `TelephonyManager` dependency from `getNetworkType()`, and re-enables three previously `@Ignore`d unit tests.

I have a fix ready and can open a PR.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions