From b6fcddfd5dab9b32456b79cfd21d4d2013b97dc1 Mon Sep 17 00:00:00 2001 From: paulhu Date: Tue, 15 Dec 2020 20:24:39 +0800 Subject: [PATCH] Add DnsResolverServiceManager Add DnsResolverServiceManager to obtain android.net.IDnsResolver IBinder for incoming connectivity service mainline. Bug: 170598012 Test: atest FrameworksNetTests Change-Id: Ibd7a678ceaaa550ff3d8eff1d0c4d3a5073b5527 --- core/api/system-current.txt | 4 ++ .../net/DnsResolverServiceManager.java | 63 +++++++++++++++++++ 2 files changed, 67 insertions(+) create mode 100644 core/java/android/net/DnsResolverServiceManager.java diff --git a/core/api/system-current.txt b/core/api/system-current.txt index de8e18046a39a..6333b5f024f8b 100644 --- a/core/api/system-current.txt +++ b/core/api/system-current.txt @@ -6006,6 +6006,10 @@ package android.net { method @Deprecated public void onUpstreamChanged(@Nullable android.net.Network); } + public class DnsResolverServiceManager { + method @NonNull @RequiresPermission(android.net.NetworkStack.PERMISSION_MAINLINE_NETWORK_STACK) public static android.os.IBinder getService(@NonNull android.content.Context); + } + public class EthernetManager { method @NonNull @RequiresPermission(anyOf={android.Manifest.permission.NETWORK_STACK, android.net.NetworkStack.PERMISSION_MAINLINE_NETWORK_STACK}) public android.net.EthernetManager.TetheredInterfaceRequest requestTetheredInterface(@NonNull java.util.concurrent.Executor, @NonNull android.net.EthernetManager.TetheredInterfaceCallback); } diff --git a/core/java/android/net/DnsResolverServiceManager.java b/core/java/android/net/DnsResolverServiceManager.java new file mode 100644 index 0000000000000..15973224f10b5 --- /dev/null +++ b/core/java/android/net/DnsResolverServiceManager.java @@ -0,0 +1,63 @@ +/* + * Copyright (C) 2020 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; + +import android.annotation.NonNull; +import android.annotation.RequiresPermission; +import android.annotation.SystemApi; +import android.content.Context; +import android.os.IBinder; +import android.os.ServiceManager; + +import java.util.Objects; + +/** + * Provides a way to obtain the DnsResolver binder objects. + * + * @hide + */ +@SystemApi +public class DnsResolverServiceManager { + /** + * Name to retrieve a {@link android.net.IDnsResolver} IBinder. + */ + private static final String DNS_RESOLVER_SERVICE = "dnsresolver"; + + private DnsResolverServiceManager() {} + + /** + * Get an {@link IBinder} representing the DnsResolver stable AIDL interface + * + * @param context the context for permission check. + * @return {@link android.net.IDnsResolver} IBinder. + */ + @NonNull + @RequiresPermission(NetworkStack.PERMISSION_MAINLINE_NETWORK_STACK) + public static IBinder getService(@NonNull final Context context) { + Objects.requireNonNull(context); + context.enforceCallingOrSelfPermission(NetworkStack.PERMISSION_MAINLINE_NETWORK_STACK, + "DnsResolverServiceManager"); + try { + return ServiceManager.getServiceOrThrow(DNS_RESOLVER_SERVICE); + } catch (ServiceManager.ServiceNotFoundException e) { + // Catch ServiceManager#ServiceNotFoundException and rethrow IllegalStateException + // because ServiceManager#ServiceNotFoundException is @hide so that it can't be listed + // on the system api. Thus, rethrow IllegalStateException if dns resolver service cannot + // be found. + throw new IllegalStateException("Cannot find dns resolver service."); + } + } +}