From 5475f2dd74a0a8e8f797f72eafa8d866bb204ad9 Mon Sep 17 00:00:00 2001 From: Quang Anh Luong Date: Wed, 4 Dec 2024 12:28:29 +0900 Subject: [PATCH] Add stopTethering(TetheringRequest) stubs Add a new API TetheringManager.stopTethering(TetheringRequest) to allow apps to stop tethering for a specific request. This CL only provides the API stubs. Implementation will be added in a future CL. Bug: 216524590 Test: atest TetheringManagerTest Change-Id: If00a7dc5d328dac620fb69c384378c36e99e1856 --- .../TetheringLib/api/system-current.txt | 8 ++++ .../src/android/net/TetheringManager.java | 46 +++++++++++++++++++ .../tethering/cts/TetheringManagerTest.java | 13 ++++++ 3 files changed, 67 insertions(+) diff --git a/Tethering/common/TetheringLib/api/system-current.txt b/Tethering/common/TetheringLib/api/system-current.txt index 0e859569b5..3e637e7fd5 100644 --- a/Tethering/common/TetheringLib/api/system-current.txt +++ b/Tethering/common/TetheringLib/api/system-current.txt @@ -36,6 +36,7 @@ package android.net { method @RequiresPermission(anyOf={android.Manifest.permission.TETHER_PRIVILEGED, android.Manifest.permission.WRITE_SETTINGS}) public void startTethering(@NonNull android.net.TetheringManager.TetheringRequest, @NonNull java.util.concurrent.Executor, @NonNull android.net.TetheringManager.StartTetheringCallback); method @RequiresPermission(anyOf={android.Manifest.permission.TETHER_PRIVILEGED, android.Manifest.permission.WRITE_SETTINGS}) public void stopAllTethering(); method @RequiresPermission(anyOf={android.Manifest.permission.TETHER_PRIVILEGED, android.Manifest.permission.WRITE_SETTINGS}) public void stopTethering(int); + method @FlaggedApi("com.android.net.flags.tethering_with_soft_ap_config") @RequiresPermission(anyOf={android.Manifest.permission.TETHER_PRIVILEGED, android.Manifest.permission.WRITE_SETTINGS}) public void stopTethering(@NonNull android.net.TetheringManager.TetheringRequest, @NonNull java.util.concurrent.Executor, @NonNull android.net.TetheringManager.StopTetheringCallback); method @RequiresPermission(anyOf={android.Manifest.permission.TETHER_PRIVILEGED, android.Manifest.permission.ACCESS_NETWORK_STATE}) public void unregisterTetheringEventCallback(@NonNull android.net.TetheringManager.TetheringEventCallback); field @Deprecated public static final String ACTION_TETHER_STATE_CHANGED = "android.net.conn.TETHER_STATE_CHANGED"; field public static final int CONNECTIVITY_SCOPE_GLOBAL = 1; // 0x1 @@ -54,6 +55,7 @@ package android.net { field public static final int TETHERING_WIFI_P2P = 3; // 0x3 field public static final int TETHER_ERROR_DHCPSERVER_ERROR = 12; // 0xc field public static final int TETHER_ERROR_DISABLE_FORWARDING_ERROR = 9; // 0x9 + field @FlaggedApi("com.android.net.flags.tethering_with_soft_ap_config") public static final int TETHER_ERROR_DUPLICATE_REQUEST = 18; // 0x12 field public static final int TETHER_ERROR_ENABLE_FORWARDING_ERROR = 8; // 0x8 field public static final int TETHER_ERROR_ENTITLEMENT_UNKNOWN = 13; // 0xd field public static final int TETHER_ERROR_IFACE_CFG_ERROR = 10; // 0xa @@ -66,6 +68,7 @@ package android.net { field public static final int TETHER_ERROR_TETHER_IFACE_ERROR = 6; // 0x6 field public static final int TETHER_ERROR_UNAVAIL_IFACE = 4; // 0x4 field public static final int TETHER_ERROR_UNKNOWN_IFACE = 1; // 0x1 + field @FlaggedApi("com.android.net.flags.tethering_with_soft_ap_config") public static final int TETHER_ERROR_UNKNOWN_REQUEST = 17; // 0x11 field public static final int TETHER_ERROR_UNKNOWN_TYPE = 16; // 0x10 field public static final int TETHER_ERROR_UNSUPPORTED = 3; // 0x3 field public static final int TETHER_ERROR_UNTETHER_IFACE_ERROR = 7; // 0x7 @@ -83,6 +86,11 @@ package android.net { method public default void onTetheringStarted(); } + @FlaggedApi("com.android.net.flags.tethering_with_soft_ap_config") public static interface TetheringManager.StopTetheringCallback { + method public default void onStopTetheringFailed(int); + method public default void onStopTetheringSucceeded(); + } + public static interface TetheringManager.TetheringEventCallback { method public default void onClientsChanged(@NonNull java.util.Collection); method public default void onError(@NonNull String, int); diff --git a/Tethering/common/TetheringLib/src/android/net/TetheringManager.java b/Tethering/common/TetheringLib/src/android/net/TetheringManager.java index 6d6eb82f60..4c342abcaf 100644 --- a/Tethering/common/TetheringLib/src/android/net/TetheringManager.java +++ b/Tethering/common/TetheringLib/src/android/net/TetheringManager.java @@ -258,10 +258,20 @@ public class TetheringManager { TETHER_ERROR_INTERNAL_ERROR, TETHER_ERROR_NO_CHANGE_TETHERING_PERMISSION, TETHER_ERROR_UNKNOWN_TYPE, + TETHER_ERROR_DUPLICATE_REQUEST, }) public @interface StartTetheringError { } + /** @hide */ + @Retention(RetentionPolicy.SOURCE) + @IntDef(value = { + TETHER_ERROR_NO_ERROR, + TETHER_ERROR_UNKNOWN_REQUEST, + }) + public @interface StopTetheringError { + } + public static final int TETHER_ERROR_NO_ERROR = 0; public static final int TETHER_ERROR_UNKNOWN_IFACE = 1; public static final int TETHER_ERROR_SERVICE_UNAVAIL = 2; @@ -279,6 +289,10 @@ public class TetheringManager { public static final int TETHER_ERROR_NO_CHANGE_TETHERING_PERMISSION = 14; public static final int TETHER_ERROR_NO_ACCESS_TETHERING_PERMISSION = 15; public static final int TETHER_ERROR_UNKNOWN_TYPE = 16; + @FlaggedApi(Flags.FLAG_TETHERING_WITH_SOFT_AP_CONFIG) + public static final int TETHER_ERROR_UNKNOWN_REQUEST = 17; + @FlaggedApi(Flags.FLAG_TETHERING_WITH_SOFT_AP_CONFIG) + public static final int TETHER_ERROR_DUPLICATE_REQUEST = 18; /** @hide */ @Retention(RetentionPolicy.SOURCE) @@ -1116,6 +1130,24 @@ public class TetheringManager { default void onTetheringFailed(@StartTetheringError final int error) {} } + /** + * Callback for use with {@link #stopTethering} to find out whether stop tethering succeeded. + */ + @FlaggedApi(Flags.FLAG_TETHERING_WITH_SOFT_AP_CONFIG) + public interface StopTetheringCallback { + /** + * Called when tethering has been successfully stopped. + */ + default void onStopTetheringSucceeded() {} + + /** + * Called when starting tethering failed. + * + * @param error The error that caused the failure. + */ + default void onStopTetheringFailed(@StopTetheringError final int error) {} + } + /** * Starts tethering and runs tether provisioning for the given type if needed. If provisioning * fails, stopTethering will be called automatically. @@ -1203,6 +1235,20 @@ public class TetheringManager { })); } + /** + * Stops tethering for the given request. Operation will fail with + * {@link #TETHER_ERROR_UNKNOWN_REQUEST} if there is no request that matches it. + */ + @RequiresPermission(anyOf = { + android.Manifest.permission.TETHER_PRIVILEGED, + android.Manifest.permission.WRITE_SETTINGS + }) + @FlaggedApi(Flags.FLAG_TETHERING_WITH_SOFT_AP_CONFIG) + public void stopTethering(@NonNull TetheringRequest request, + @NonNull final Executor executor, @NonNull final StopTetheringCallback callback) { + throw new UnsupportedOperationException(); + } + /** * Callback for use with {@link #getLatestTetheringEntitlementResult} to find out whether * entitlement succeeded. diff --git a/tests/cts/tethering/src/android/tethering/cts/TetheringManagerTest.java b/tests/cts/tethering/src/android/tethering/cts/TetheringManagerTest.java index 54ecc49bea..e0e22e5edd 100644 --- a/tests/cts/tethering/src/android/tethering/cts/TetheringManagerTest.java +++ b/tests/cts/tethering/src/android/tethering/cts/TetheringManagerTest.java @@ -95,6 +95,7 @@ import java.util.ArrayList; import java.util.Arrays; import java.util.List; import java.util.concurrent.CompletableFuture; +import java.util.concurrent.Executor; import java.util.concurrent.LinkedBlockingQueue; import java.util.concurrent.TimeUnit; import java.util.function.Consumer; @@ -444,6 +445,18 @@ public class TetheringManagerTest { } } + @Test + public void testStopTetheringRequest() throws Exception { + TetheringRequest request = new TetheringRequest.Builder(TETHERING_WIFI).build(); + Executor executor = Runnable::run; + TetheringManager.StopTetheringCallback callback = + new TetheringManager.StopTetheringCallback() {}; + try { + mTM.stopTethering(request, executor, callback); + fail("stopTethering should throw UnsupportedOperationException"); + } catch (UnsupportedOperationException expect) { } + } + @Test public void testEnableTetheringPermission() throws Exception { final StartTetheringCallback startTetheringCallback = new StartTetheringCallback();