Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 4a97abad authored by Remi NGUYEN VAN's avatar Remi NGUYEN VAN
Browse files

Add shims for NSD

The shims will be used for new T APIs in NsdManager and NsdServiceInfo

Bug: 190249673
Test: atest CtsNetTestCases:NsdManagerTest
Change-Id: Ic1380035a60516b138651f48b11782522463f5ea
parent eef90cfe
Loading
Loading
Loading
Loading
+62 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2022 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 com.android.networkstack.apishim.api29;

import android.net.Network;
import android.net.nsd.NsdManager;
import android.net.nsd.NsdServiceInfo;
import android.os.Build;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.RequiresApi;

import com.android.networkstack.apishim.common.NsdShim;
import com.android.networkstack.apishim.common.UnsupportedApiLevelException;

/**
 * Implementation of {@link NsdShim}.
 */
@RequiresApi(Build.VERSION_CODES.Q)
public class NsdShimImpl implements NsdShim {

    /**
     * Get a new instance of {@link NsdShim}.
     */
    public static NsdShim newInstance() {
        return new NsdShimImpl();
    }

    @Nullable
    @Override
    public Network getNetwork(@NonNull NsdServiceInfo serviceInfo) {
        // NsdServiceInfo has no Network before T
        return null;
    }

    @Override
    public void setNetwork(@NonNull NsdServiceInfo serviceInfo, @Nullable Network network) {
        // No-op: NsdServiceInfo has no Network before T
    }

    @Override
    public void discoverServices(@NonNull NsdManager nsdManager, @NonNull String serviceType,
            int protocolType, @Nullable Network network,
            @NonNull NsdManager.DiscoveryListener listener) throws UnsupportedApiLevelException {
        throw new UnsupportedApiLevelException("Discover on network is only supported on T+");
    }
}
+24 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2022 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 com.android.networkstack.apishim.api31;

/**
 * Implementation of {@link com.android.networkstack.apishim.common.NsdShim}.
 */
public class NsdShimImpl extends com.android.networkstack.apishim.api29.NsdShimImpl {
    // Inherit everything from API29 shim
}
+67 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2022 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 com.android.networkstack.apishim;

import android.net.Network;
import android.net.nsd.NsdManager;
import android.net.nsd.NsdServiceInfo;
import android.os.Build;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.RequiresApi;

import com.android.modules.utils.build.SdkLevel;
import com.android.networkstack.apishim.common.NsdShim;
import com.android.networkstack.apishim.common.UnsupportedApiLevelException;

/**
 * Implementation of {@link com.android.networkstack.apishim.common.NsdShim}.
 */
@RequiresApi(Build.VERSION_CODES.TIRAMISU)
public class NsdShimImpl extends com.android.networkstack.apishim.api31.NsdShimImpl {

    /**
     * Get a new instance of {@link NsdShim}.
     */
    @RequiresApi(Build.VERSION_CODES.Q)
    public static NsdShim newInstance() {
        if (SdkLevel.isAtLeastT()) {
            return new NsdShimImpl();
        } else {
            return new com.android.networkstack.apishim.api31.NsdShimImpl();
        }
    }

    @Nullable
    @Override
    public Network getNetwork(@NonNull NsdServiceInfo serviceInfo) {
        return serviceInfo.getNetwork();
    }

    @Override
    public void setNetwork(@NonNull NsdServiceInfo serviceInfo, @Nullable Network network) {
        serviceInfo.setNetwork(network);
    }

    @Override
    public void discoverServices(@NonNull NsdManager nsdManager, @NonNull String serviceType,
            int protocolType, @Nullable Network network,
            @NonNull NsdManager.DiscoveryListener listener) throws UnsupportedApiLevelException {
        nsdManager.discoverServices(serviceType, protocolType, network, listener);
    }
}
+46 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2022 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 com.android.networkstack.apishim.common;

import android.net.Network;
import android.net.nsd.NsdManager;
import android.net.nsd.NsdServiceInfo;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

/** Shim for NSD APIs, including {@link android.net.nsd.NsdManager} and
 * {@link android.net.nsd.NsdServiceInfo}. */
public interface NsdShim {
    /**
     * @see NsdServiceInfo#getNetwork()
     */
    @Nullable
    Network getNetwork(@NonNull NsdServiceInfo serviceInfo);

    /**
     * @see NsdServiceInfo#setNetwork(Network)
     */
    void setNetwork(@NonNull NsdServiceInfo serviceInfo, @Nullable Network network);

    /**
     * @see NsdManager#discoverServices(String, int, Network, NsdManager.DiscoveryListener)
     */
    void discoverServices(@NonNull NsdManager nsdManager, @NonNull String serviceType,
            int protocolType, @Nullable Network network,
            @NonNull NsdManager.DiscoveryListener listener) throws UnsupportedApiLevelException;
}