1 /* 2 * Copyright (C) 2022 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.android.networkstack.apishim.common; 18 19 import android.net.Network; 20 import android.net.NetworkRequest; 21 import android.net.nsd.NsdManager; 22 import android.net.nsd.NsdManager.DiscoveryListener; 23 import android.net.nsd.NsdManager.RegistrationListener; 24 import android.net.nsd.NsdManager.ResolveListener; 25 import android.net.nsd.NsdServiceInfo; 26 27 import androidx.annotation.NonNull; 28 import androidx.annotation.Nullable; 29 30 import java.net.InetAddress; 31 import java.util.List; 32 import java.util.concurrent.Executor; 33 34 /** Shim for NSD APIs, including {@link android.net.nsd.NsdManager} and 35 * {@link android.net.nsd.NsdServiceInfo}. */ 36 public interface NsdShim { 37 /** 38 * @see NsdServiceInfo#getNetwork() 39 */ 40 @Nullable getNetwork(@onNull NsdServiceInfo serviceInfo)41 Network getNetwork(@NonNull NsdServiceInfo serviceInfo); 42 43 /** 44 * @see NsdServiceInfo#setNetwork(Network) 45 */ setNetwork(@onNull NsdServiceInfo serviceInfo, @Nullable Network network)46 void setNetwork(@NonNull NsdServiceInfo serviceInfo, @Nullable Network network); 47 48 /** 49 * @see NsdManager#registerService(NsdServiceInfo, int, Executor, RegistrationListener) 50 */ registerService(@onNull NsdManager nsdManager, @NonNull NsdServiceInfo serviceInfo, int protocolType, @NonNull Executor executor, @NonNull RegistrationListener listener)51 void registerService(@NonNull NsdManager nsdManager, @NonNull NsdServiceInfo serviceInfo, 52 int protocolType, @NonNull Executor executor, @NonNull RegistrationListener listener) 53 throws UnsupportedApiLevelException; 54 55 /** 56 * @see NsdManager#discoverServices(String, int, Network, Executor, DiscoveryListener) 57 */ discoverServices(@onNull NsdManager nsdManager, @NonNull String serviceType, int protocolType, @Nullable Network network, @NonNull Executor executor, @NonNull DiscoveryListener listener)58 void discoverServices(@NonNull NsdManager nsdManager, @NonNull String serviceType, 59 int protocolType, @Nullable Network network, 60 @NonNull Executor executor, @NonNull DiscoveryListener listener) 61 throws UnsupportedApiLevelException; 62 63 /** 64 * @see NsdManager#resolveService(NsdServiceInfo, Executor, ResolveListener) 65 */ resolveService(@onNull NsdManager nsdManager, @NonNull NsdServiceInfo serviceInfo, @NonNull Executor executor, @NonNull ResolveListener resolveListener)66 void resolveService(@NonNull NsdManager nsdManager, @NonNull NsdServiceInfo serviceInfo, 67 @NonNull Executor executor, @NonNull ResolveListener resolveListener) 68 throws UnsupportedApiLevelException; 69 70 /** 71 * @see NsdManager#discoverServices(String, int, NetworkRequest, Executor, DiscoveryListener) 72 */ discoverServices(@onNull NsdManager nsdManager, @NonNull String serviceType, int protocolType, @Nullable NetworkRequest request, @NonNull Executor executor, @NonNull DiscoveryListener listener)73 void discoverServices(@NonNull NsdManager nsdManager, @NonNull String serviceType, 74 int protocolType, @Nullable NetworkRequest request, 75 @NonNull Executor executor, @NonNull DiscoveryListener listener) 76 throws UnsupportedApiLevelException; 77 78 /** 79 * @see NsdManager#stopServiceResolution(ResolveListener) 80 */ stopServiceResolution(@onNull NsdManager nsdManager, @NonNull ResolveListener resolveListener)81 void stopServiceResolution(@NonNull NsdManager nsdManager, 82 @NonNull ResolveListener resolveListener) throws UnsupportedApiLevelException; 83 84 /** 85 * @see NsdManager#ServiceInfoCallback 86 */ 87 interface ServiceInfoCallbackShim { onServiceInfoCallbackRegistrationFailed(int errorCode)88 default void onServiceInfoCallbackRegistrationFailed(int errorCode) {} onServiceUpdated(@onNull NsdServiceInfo serviceInfo)89 default void onServiceUpdated(@NonNull NsdServiceInfo serviceInfo) {} onServiceLost()90 default void onServiceLost() {} onServiceInfoCallbackUnregistered()91 default void onServiceInfoCallbackUnregistered() {} 92 } 93 94 /** 95 * @see NsdManager#registerServiceInfoCallback 96 */ registerServiceInfoCallback(@onNull NsdManager nsdManager, @NonNull NsdServiceInfo serviceInfo, @NonNull Executor executor, @NonNull ServiceInfoCallbackShim listener)97 default void registerServiceInfoCallback(@NonNull NsdManager nsdManager, 98 @NonNull NsdServiceInfo serviceInfo, @NonNull Executor executor, 99 @NonNull ServiceInfoCallbackShim listener) 100 throws UnsupportedApiLevelException { 101 throw new UnsupportedApiLevelException("Service callback is only supported on U+"); 102 } 103 104 /** 105 * @see NsdManager#unregisterServiceInfoCallback 106 */ unregisterServiceInfoCallback(@onNull NsdManager nsdManager, @NonNull ServiceInfoCallbackShim listener)107 default void unregisterServiceInfoCallback(@NonNull NsdManager nsdManager, 108 @NonNull ServiceInfoCallbackShim listener) 109 throws UnsupportedApiLevelException { 110 throw new UnsupportedApiLevelException("Service callback is only supported on U+"); 111 } 112 113 /** 114 * @see NsdServiceInfo#getHostAddresses() 115 */ 116 @NonNull getHostAddresses(@onNull NsdServiceInfo serviceInfo)117 default List<InetAddress> getHostAddresses(@NonNull NsdServiceInfo serviceInfo) 118 throws UnsupportedApiLevelException { 119 throw new UnsupportedApiLevelException("getHostAddresses is only supported on U+"); 120 } 121 122 /** 123 * @see NsdServiceInfo#setHostAddresses(List<InetAddress>) 124 */ setHostAddresses(@onNull NsdServiceInfo serviceInfo, @NonNull List<InetAddress> addresses)125 default void setHostAddresses(@NonNull NsdServiceInfo serviceInfo, 126 @NonNull List<InetAddress> addresses) throws UnsupportedApiLevelException { 127 throw new UnsupportedApiLevelException("setHostAddresses is only supported on U+"); 128 } 129 } 130