Make TetheringRequest parcelable

Make TetheringRequest parcelable so we can pass it via Wifi APIs and map
Soft AP state changes to the correct request.

Bug: 216524590
Test: build
Change-Id: I63a45b14e9abc288b353159dfdcbe96fde485cb7
This commit is contained in:
Quang Anh Luong 2024-02-07 13:41:40 +09:00
commit 1252f7f428
5 changed files with 95 additions and 3 deletions

View file

@ -95,13 +95,16 @@ package android.net {
method public default void onUpstreamChanged(@Nullable android.net.Network);
}
public static class TetheringManager.TetheringRequest {
public static final class TetheringManager.TetheringRequest implements android.os.Parcelable {
method @FlaggedApi("com.android.net.flags.tethering_request_with_soft_ap_config") public int describeContents();
method @Nullable public android.net.LinkAddress getClientStaticIpv4Address();
method public int getConnectivityScope();
method @Nullable public android.net.LinkAddress getLocalIpv4Address();
method public boolean getShouldShowEntitlementUi();
method public int getTetheringType();
method public boolean isExemptFromEntitlementCheck();
method @FlaggedApi("com.android.net.flags.tethering_request_with_soft_ap_config") public void writeToParcel(@NonNull android.os.Parcel, int);
field @FlaggedApi("com.android.net.flags.tethering_request_with_soft_ap_config") @NonNull public static final android.os.Parcelable.Creator<android.net.TetheringManager.TetheringRequest> CREATOR;
}
public static class TetheringManager.TetheringRequest.Builder {

View file

@ -18,6 +18,7 @@ package android.net;
import static android.annotation.SystemApi.Client.MODULE_LIBRARIES;
import android.Manifest;
import android.annotation.FlaggedApi;
import android.annotation.IntDef;
import android.annotation.NonNull;
import android.annotation.Nullable;
@ -28,6 +29,8 @@ import android.content.Context;
import android.os.Bundle;
import android.os.ConditionVariable;
import android.os.IBinder;
import android.os.Parcel;
import android.os.Parcelable;
import android.os.RemoteException;
import android.os.ResultReceiver;
import android.util.ArrayMap;
@ -59,6 +62,14 @@ import java.util.function.Supplier;
*/
@SystemApi
public class TetheringManager {
// TODO : remove this class when udc-mainline-prod is abandoned and android.net.flags.Flags is
// available here
/** @hide */
public static class Flags {
static final String TETHERING_REQUEST_WITH_SOFT_AP_CONFIG =
"com.android.net.flags.tethering_request_with_soft_ap_config";
}
private static final String TAG = TetheringManager.class.getSimpleName();
private static final int DEFAULT_TIMEOUT_MS = 60_000;
private static final long CONNECTOR_POLL_INTERVAL_MILLIS = 200L;
@ -673,14 +684,44 @@ public class TetheringManager {
/**
* Use with {@link #startTethering} to specify additional parameters when starting tethering.
*/
public static class TetheringRequest {
public static final class TetheringRequest implements Parcelable {
/** A configuration set for TetheringRequest. */
private final TetheringRequestParcel mRequestParcel;
private TetheringRequest(final TetheringRequestParcel request) {
private TetheringRequest(@NonNull final TetheringRequestParcel request) {
mRequestParcel = request;
}
private TetheringRequest(@NonNull Parcel in) {
mRequestParcel = in.readParcelable(TetheringRequestParcel.class.getClassLoader());
}
@FlaggedApi(Flags.TETHERING_REQUEST_WITH_SOFT_AP_CONFIG)
@NonNull
public static final Creator<TetheringRequest> CREATOR = new Creator<>() {
@Override
public TetheringRequest createFromParcel(@NonNull Parcel in) {
return new TetheringRequest(in);
}
@Override
public TetheringRequest[] newArray(int size) {
return new TetheringRequest[size];
}
};
@FlaggedApi(Flags.TETHERING_REQUEST_WITH_SOFT_AP_CONFIG)
@Override
public int describeContents() {
return 0;
}
@FlaggedApi(Flags.TETHERING_REQUEST_WITH_SOFT_AP_CONFIG)
@Override
public void writeToParcel(@NonNull Parcel dest, int flags) {
dest.writeParcelable(mRequestParcel, flags);
}
/** Builder used to create TetheringRequest. */
public static class Builder {
private final TetheringRequestParcel mBuilderParcel;

View file

@ -46,3 +46,9 @@ flag {
bug: "308011229"
}
flag {
name: "tethering_request_with_soft_ap_config"
namespace: "android_core_networking"
description: "The flag controls the access for the parcelable TetheringRequest with getSoftApConfiguration/setSoftApConfiguration API"
bug: "216524590"
}

View file

@ -0,0 +1,20 @@
/**
*
* Copyright (C) 2024 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package android.net;
parcelable TetheringManager.TetheringRequest;

View file

@ -71,6 +71,8 @@ import android.telephony.TelephonyManager;
import androidx.test.InstrumentationRegistry;
import androidx.test.runner.AndroidJUnit4;
import com.android.testutils.ParcelUtils;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
@ -235,6 +237,26 @@ public class TetheringManagerTest {
assertFalse(tr2.getShouldShowEntitlementUi());
}
@Test
public void testTetheringRequestParcelable() {
final LinkAddress localAddr = new LinkAddress("192.168.24.5/24");
final LinkAddress clientAddr = new LinkAddress("192.168.24.100/24");
final TetheringRequest unparceled = new TetheringRequest.Builder(TETHERING_USB)
.setStaticIpv4Addresses(localAddr, clientAddr)
.setExemptFromEntitlementCheck(true)
.setShouldShowEntitlementUi(false).build();
final TetheringRequest parceled = ParcelUtils.parcelingRoundTrip(unparceled);
assertEquals(unparceled.getTetheringType(), parceled.getTetheringType());
assertEquals(unparceled.getConnectivityScope(), parceled.getConnectivityScope());
assertEquals(unparceled.getLocalIpv4Address(), parceled.getLocalIpv4Address());
assertEquals(unparceled.getClientStaticIpv4Address(),
parceled.getClientStaticIpv4Address());
assertEquals(unparceled.isExemptFromEntitlementCheck(),
parceled.isExemptFromEntitlementCheck());
assertEquals(unparceled.getShouldShowEntitlementUi(),
parceled.getShouldShowEntitlementUi());
}
@Test
public void testRegisterTetheringEventCallback() throws Exception {
final TestTetheringEventCallback tetherEventCallback =