diff options
Diffstat (limited to 'Tethering/common/TetheringLib/src/android/net/TetheringManager.java')
| -rw-r--r-- | Tethering/common/TetheringLib/src/android/net/TetheringManager.java | 85 |
1 files changed, 84 insertions, 1 deletions
diff --git a/Tethering/common/TetheringLib/src/android/net/TetheringManager.java b/Tethering/common/TetheringLib/src/android/net/TetheringManager.java index 97fb4974d0..c64da8a8e7 100644 --- a/Tethering/common/TetheringLib/src/android/net/TetheringManager.java +++ b/Tethering/common/TetheringLib/src/android/net/TetheringManager.java @@ -557,6 +557,28 @@ public class TetheringManager { } /** + * Indicates that this tethering connection will provide connectivity beyond this device (e.g., + * global Internet access). + */ + public static final int CONNECTIVITY_SCOPE_GLOBAL = 1; + + /** + * Indicates that this tethering connection will only provide local connectivity. + */ + public static final int CONNECTIVITY_SCOPE_LOCAL = 2; + + /** + * Connectivity scopes for {@link TetheringRequest.Builder#setConnectivityScope}. + * @hide + */ + @Retention(RetentionPolicy.SOURCE) + @IntDef(prefix = "CONNECTIVITY_SCOPE_", value = { + CONNECTIVITY_SCOPE_GLOBAL, + CONNECTIVITY_SCOPE_LOCAL, + }) + public @interface ConnectivityScope {} + + /** * Use with {@link #startTethering} to specify additional parameters when starting tethering. */ public static class TetheringRequest { @@ -579,6 +601,7 @@ public class TetheringManager { mBuilderParcel.staticClientAddress = null; mBuilderParcel.exemptFromEntitlementCheck = false; mBuilderParcel.showProvisioningUi = true; + mBuilderParcel.connectivityScope = getDefaultConnectivityScope(type); } /** @@ -624,7 +647,21 @@ public class TetheringManager { return this; } - /** Build {@link TetheringRequest] with the currently set configuration. */ + /** + * Sets the connectivity scope to be provided by this tethering downstream. + */ + @RequiresPermission(android.Manifest.permission.TETHER_PRIVILEGED) + @NonNull + public Builder setConnectivityScope(@ConnectivityScope int scope) { + if (!checkConnectivityScope(mBuilderParcel.tetheringType, scope)) { + throw new IllegalArgumentException("Invalid connectivity scope " + scope); + } + + mBuilderParcel.connectivityScope = scope; + return this; + } + + /** Build {@link TetheringRequest} with the currently set configuration. */ @NonNull public TetheringRequest build() { return new TetheringRequest(mBuilderParcel); @@ -655,6 +692,12 @@ public class TetheringManager { return mRequestParcel.tetheringType; } + /** Get connectivity type */ + @ConnectivityScope + public int getConnectivityScope() { + return mRequestParcel.connectivityScope; + } + /** Check if exempt from entitlement check. */ public boolean isExemptFromEntitlementCheck() { return mRequestParcel.exemptFromEntitlementCheck; @@ -679,6 +722,26 @@ public class TetheringManager { } /** + * Returns the default connectivity scope for the given tethering type. Usually this is + * CONNECTIVITY_SCOPE_GLOBAL, except for NCM which for historical reasons defaults to local. + * @hide + */ + public static @ConnectivityScope int getDefaultConnectivityScope(int tetheringType) { + return tetheringType != TETHERING_NCM + ? CONNECTIVITY_SCOPE_GLOBAL + : CONNECTIVITY_SCOPE_LOCAL; + } + + /** + * Checks whether the requested connectivity scope is allowed. + * @hide + */ + private static boolean checkConnectivityScope(int type, int scope) { + if (scope == CONNECTIVITY_SCOPE_GLOBAL) return true; + return type == TETHERING_USB || type == TETHERING_ETHERNET || type == TETHERING_NCM; + } + + /** * Get a TetheringRequestParcel from the configuration * @hide */ @@ -940,6 +1003,15 @@ public class TetheringManager { default void onTetheredInterfacesChanged(@NonNull List<String> interfaces) {} /** + * Called when there was a change in the list of local-only interfaces. + * + * <p>This will be called immediately after the callback is registered, and may be called + * multiple times later upon changes. + * @param interfaces The list of 0 or more String of active local-only interface names. + */ + default void onLocalOnlyInterfacesChanged(@NonNull List<String> interfaces) {} + + /** * Called when an error occurred configuring tethering. * * <p>This will be called immediately after the callback is registered if the latest status @@ -1045,6 +1117,7 @@ public class TetheringManager { private final HashMap<String, Integer> mErrorStates = new HashMap<>(); private String[] mLastTetherableInterfaces = null; private String[] mLastTetheredInterfaces = null; + private String[] mLastLocalOnlyInterfaces = null; @Override public void onUpstreamChanged(Network network) throws RemoteException { @@ -1082,6 +1155,14 @@ public class TetheringManager { Collections.unmodifiableList(Arrays.asList(mLastTetheredInterfaces))); } + private synchronized void maybeSendLocalOnlyIfacesChangedCallback( + final TetherStatesParcel newStates) { + if (Arrays.equals(mLastLocalOnlyInterfaces, newStates.localOnlyList)) return; + mLastLocalOnlyInterfaces = newStates.localOnlyList.clone(); + callback.onLocalOnlyInterfacesChanged( + Collections.unmodifiableList(Arrays.asList(mLastLocalOnlyInterfaces))); + } + // Called immediately after the callbacks are registered. @Override public void onCallbackStarted(TetheringCallbackStartedParcel parcel) { @@ -1092,6 +1173,7 @@ public class TetheringManager { sendRegexpsChanged(parcel.config); maybeSendTetherableIfacesChangedCallback(parcel.states); maybeSendTetheredIfacesChangedCallback(parcel.states); + maybeSendLocalOnlyIfacesChangedCallback(parcel.states); callback.onClientsChanged(parcel.tetheredClients); callback.onOffloadStatusChanged(parcel.offloadStatus); }); @@ -1122,6 +1204,7 @@ public class TetheringManager { sendErrorCallbacks(states); maybeSendTetherableIfacesChangedCallback(states); maybeSendTetheredIfacesChangedCallback(states); + maybeSendLocalOnlyIfacesChangedCallback(states); }); } |
