-
Notifications
You must be signed in to change notification settings - Fork 25.2k
Add local network permission prompt on android 17 #57291
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
Closed
Closed
Changes from 3 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
69 changes: 69 additions & 0 deletions
69
...ve/ReactAndroid/src/main/java/com/facebook/react/devsupport/LocalNetworkPermissionUtil.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,69 @@ | ||
| /* | ||
| * Copyright (c) Meta Platforms, Inc. and affiliates. | ||
| * | ||
| * This source code is licensed under the MIT license found in the | ||
| * LICENSE file in the root directory of this source tree. | ||
| */ | ||
|
|
||
| package com.facebook.react.devsupport | ||
|
|
||
| import android.app.Activity | ||
| import android.content.Context | ||
| import android.content.pm.PackageManager | ||
| import com.facebook.react.modules.core.PermissionAwareActivity | ||
| import com.facebook.react.modules.systeminfo.AndroidInfoHelpers | ||
| import com.facebook.react.util.AndroidVersion | ||
|
|
||
| /** | ||
| * Debug-only helper to request the runtime `ACCESS_LOCAL_NETWORK` permission needed to reach Metro | ||
| * on Android 17 (SDK 37) devices, which gate local-network addresses (the emulator's `10.0.2.2` | ||
| * alias, a device's Wi-Fi/LAN IP) for any app that declares the permission, regardless of targetSdk. | ||
| * Loopback via `adb reverse` (`localhost`) is exempt. | ||
| */ | ||
| internal object LocalNetworkPermissionUtil { | ||
| private const val PERMISSION = "android.permission.ACCESS_LOCAL_NETWORK" | ||
| private const val PERMISSION_REQUEST_CODE = 1 | ||
|
|
||
| /** | ||
| * Invokes [onResolved] once it is safe to connect to Metro: immediately when no permission is | ||
| * needed, or after the user answers the `ACCESS_LOCAL_NETWORK` prompt otherwise. | ||
| */ | ||
| @JvmStatic | ||
| fun requestLocalNetworkAccessIfNeeded(activity: Activity, onResolved: Runnable) { | ||
| if (activity is PermissionAwareActivity && needsLocalNetworkPrompt(activity)) { | ||
| activity.requestPermissions(arrayOf(PERMISSION), PERMISSION_REQUEST_CODE) { _, _, _ -> | ||
| onResolved.run() | ||
| true | ||
| } | ||
| } else { | ||
| onResolved.run() | ||
| } | ||
| } | ||
|
|
||
| /** Whether the `ACCESS_LOCAL_NETWORK` prompt must be shown before reaching the dev server. */ | ||
| private fun needsLocalNetworkPrompt(activity: Activity): Boolean { | ||
| if (!AndroidVersion.isAtLeastSdk37()) return false // enforced by the device, not app targetSdk | ||
| if (!isPermissionDeclared(activity)) return false // debug manifest only | ||
| if (activity.checkSelfPermission(PERMISSION) == PackageManager.PERMISSION_GRANTED) return false | ||
| return !isExemptDevServerHost(activity) // loopback needs no permission | ||
| } | ||
|
|
||
| /** | ||
| * True for loopback dev servers (`localhost` / `127.x` / `::1`, e.g. USB + `adb reverse`), which | ||
| * are exempt. The emulator's `10.0.2.2` is not loopback here and does need the permission. | ||
| */ | ||
| private fun isExemptDevServerHost(context: Context): Boolean { | ||
|
alanjhughes marked this conversation as resolved.
Outdated
|
||
| val host = AndroidInfoHelpers.getServerHost(context).substringBeforeLast(':').trim('[', ']') | ||
| return host == "localhost" || host == "::1" || host.startsWith("127.") | ||
| } | ||
|
|
||
| private fun isPermissionDeclared(activity: Activity): Boolean = | ||
| try { | ||
| activity.packageManager | ||
| .getPackageInfo(activity.packageName, PackageManager.GET_PERMISSIONS) | ||
| .requestedPermissions | ||
| ?.contains(PERMISSION) == true | ||
| } catch (_: PackageManager.NameNotFoundException) { | ||
| false | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.