Debian Codegraph @

minidns-android21/src/main/java/org/minidns/dnsserverlookup/android21/AndroidUsingLinkProperties.java

raw
1 /*
2 * Copyright 2015-2020 the original author or authors
3 *
4 * This software is licensed under the Apache License, Version 2.0,
5 * the GNU Lesser General Public License version 2 or later ("LGPL")
6 * and the WTFPL.
7 * You may choose either license to govern your use of this software only
8 * upon the condition that you accept all of the terms of either
9 * the Apache License 2.0, the LGPL 2.1+ or the WTFPL.
10 */
11 package org.minidns.dnsserverlookup.android21;
12
13 import android.annotation.TargetApi;
14 import android.content.Context;
15 import android.net.ConnectivityManager;
16 import android.net.LinkProperties;
17 import android.net.Network;
18 import android.net.RouteInfo;
19 import android.os.Build;
20
21 import java.net.InetAddress;
22 import java.util.ArrayList;
23 import java.util.List;
24
25 import org.minidns.DnsClient;
26 import org.minidns.dnsserverlookup.AbstractDnsServerLookupMechanism;
27 import org.minidns.dnsserverlookup.AndroidUsingExec;
28
29 /**
30 * A DNS server lookup mechanism using Android's Link Properties method available on Android API 21 or higher. Use
31 * {@link #setup(Context)} to setup this mechanism.
32 * <p>
33 * Requires the ACCESS_NETWORK_STATE permission.
34 * </p>
35 */
36 public class AndroidUsingLinkProperties extends AbstractDnsServerLookupMechanism {
37
38 private final ConnectivityManager connectivityManager;
39
40 /**
41 * Setup this DNS server lookup mechanism. You need to invoke this method only once, ideally before you do your
42 * first DNS lookup.
43 *
44 * @param context a Context instance.
45 * @return the instance of the newly setup mechanism
46 */
47 public static AndroidUsingLinkProperties setup(Context context) {
48 AndroidUsingLinkProperties androidUsingLinkProperties = new AndroidUsingLinkProperties(context);
49 DnsClient.addDnsServerLookupMechanism(androidUsingLinkProperties);
50 return androidUsingLinkProperties;
51 }
52
53 public AndroidUsingLinkProperties(Context context) {
54 super(AndroidUsingLinkProperties.class.getSimpleName(), AndroidUsingExec.PRIORITY - 1);
55 connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
56 }
57
58 @Override
59 public boolean isAvailable() {
60 return Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP;
61 }
62
63 @TargetApi(Build.VERSION_CODES.M)
64 private List<String> getDnsServerAddressesOfActiveNetwork() {
65 if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) {
66 return null;
67 }
68
69 // ConnectivityManager.getActiveNetwork() is API 23.
70 Network activeNetwork = connectivityManager.getActiveNetwork();
71 if (activeNetwork == null) {
72 return null;
73 }
74
75 LinkProperties linkProperties = connectivityManager.getLinkProperties(activeNetwork);
76 if (linkProperties == null) {
77 return null;
78 }
79
80 List<InetAddress> dnsServers = linkProperties.getDnsServers();
81 return toListOfStrings(dnsServers);
82 }
83
84 @Override
85 @TargetApi(21)
86 public List<String> getDnsServerAddresses() {
87 // First, try the API 23 approach using ConnectivityManager.getActiveNetwork().
88 List<String> servers = getDnsServerAddressesOfActiveNetwork();
89 if (servers != null) {
90 return servers;
91 }
92
93 Network[] networks = connectivityManager.getAllNetworks();
94 if (networks == null) {
95 return null;
96 }
97
98 servers = new ArrayList<>(networks.length * 2);
99 for (Network network : networks) {
100 LinkProperties linkProperties = connectivityManager.getLinkProperties(network);
101 if (linkProperties == null) {
102 continue;
103 }
104
105 // Prioritize the DNS servers of links which have a default route
106 if (hasDefaultRoute(linkProperties)) {
107 servers.addAll(0, toListOfStrings(linkProperties.getDnsServers()));
108 } else {
109 servers.addAll(toListOfStrings(linkProperties.getDnsServers()));
110 }
111 }
112
113 if (servers.isEmpty()) {
114 return null;
115 }
116
117 return servers;
118 }
119
120 @TargetApi(Build.VERSION_CODES.LOLLIPOP)
121 private static boolean hasDefaultRoute(LinkProperties linkProperties) {
122 for (RouteInfo route : linkProperties.getRoutes()) {
123 if (route.isDefaultRoute()) {
124 return true;
125 }
126 }
127 return false;
128 }
129
130 }