.. | .. |
---|
40 | 40 | import java.net.URL;
|
---|
41 | 41 | import java.net.URLConnection;
|
---|
42 | 42 | import java.util.ArrayList;
|
---|
| 43 | +import java.net.Inet4Address;
|
---|
| 44 | +import android.net.RouteInfo;
|
---|
43 | 45 |
|
---|
44 | 46 | import static android.view.WindowManager.LayoutParams.FLAG_FULLSCREEN;
|
---|
45 | 47 | import static android.view.WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON;
|
---|
.. | .. |
---|
71 | 73 | private boolean mReadyToGetEthInfo;
|
---|
72 | 74 | private boolean mStop;
|
---|
73 | 75 |
|
---|
| 76 | + private ConnectivityManager mConnectivityManager;
|
---|
74 | 77 | private EthernetManager mEthManager;
|
---|
75 | 78 | private BroadcastReceiver mReceiver;
|
---|
76 | 79 | private String mIfaceName;
|
---|
.. | .. |
---|
151 | 154 | ControlButtonUtil.initControlButtonView(this);
|
---|
152 | 155 | findViewById(R.id.btn_Pass).setVisibility(View.INVISIBLE);
|
---|
153 | 156 | findViewById(R.id.btn_Fail).setVisibility(View.INVISIBLE);
|
---|
| 157 | +
|
---|
| 158 | + mConnectivityManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
|
---|
154 | 159 |
|
---|
155 | 160 | mEthManager = (EthernetManager) getSystemService(Context.ETHERNET_SERVICE);
|
---|
156 | 161 | if (null == mEthManager) {
|
---|
.. | .. |
---|
242 | 247 | }
|
---|
243 | 248 | IpAssignment mode = ipConfiguration.getIpAssignment();
|
---|
244 | 249 | if (mode == IpAssignment.DHCP || mode == IpAssignment.UNASSIGNED) {
|
---|
245 | | - getEthInfoFromDhcp();
|
---|
| 250 | + getEthInfoFromDhcp_V1();
|
---|
246 | 251 | } else if (mode == IpAssignment.STATIC) {
|
---|
247 | 252 | getEthInfoFromStaticIp();
|
---|
248 | 253 | }
|
---|
249 | 254 | }
|
---|
250 | 255 |
|
---|
| 256 | + public static String intToNetmask(int prefixLength) {
|
---|
| 257 | + if (prefixLength < 0 || prefixLength > 32) {
|
---|
| 258 | + throw new IllegalArgumentException("Prefix length must be between 0 and 32");
|
---|
| 259 | + }
|
---|
| 260 | + StringBuilder netmask = new StringBuilder();
|
---|
| 261 | + int[] parts = new int[4];
|
---|
| 262 | + // 计算每一部分的值
|
---|
| 263 | + for (int i = 0; i < 4; i++) {
|
---|
| 264 | + if (prefixLength >= 8) {
|
---|
| 265 | + parts[i] = 255;
|
---|
| 266 | + prefixLength -= 8;
|
---|
| 267 | + } else if (prefixLength > 0) {
|
---|
| 268 | + parts[i] = 256 - (1 << (8 - prefixLength));
|
---|
| 269 | + prefixLength = 0;
|
---|
| 270 | + } else {
|
---|
| 271 | + parts[i] = 0;
|
---|
| 272 | + }
|
---|
| 273 | + }
|
---|
| 274 | + // 构建点分十进制字符串
|
---|
| 275 | + for (int i = 0; i < 4; i++) {
|
---|
| 276 | + netmask.append(parts[i]);
|
---|
| 277 | + if (i < 3) {
|
---|
| 278 | + netmask.append(".");
|
---|
| 279 | + }
|
---|
| 280 | + }
|
---|
| 281 | + return netmask.toString();
|
---|
| 282 | + }
|
---|
| 283 | +
|
---|
| 284 | + private void getEthInfoFromDhcp_V1() {
|
---|
| 285 | + int network = ConnectivityManager.TYPE_ETHERNET;
|
---|
| 286 | + String mMacPreference = mConnectivityManager.getNetworkInfo(network).getExtraInfo();
|
---|
| 287 | +
|
---|
| 288 | + final LinkProperties linkProperties = mConnectivityManager.getLinkProperties(network);
|
---|
| 289 | + for (LinkAddress linkAddress : linkProperties.getLinkAddresses()) { //set ipaddress.
|
---|
| 290 | + if (linkAddress.getAddress() instanceof Inet4Address) {
|
---|
| 291 | + String mgetHostAddress = linkAddress.getAddress().getHostAddress();
|
---|
| 292 | + String netMask = intToNetmask(linkAddress.getPrefixLength());
|
---|
| 293 | + Log.v(TAG, "mgetHostAddress " + mgetHostAddress);
|
---|
| 294 | + Log.v(TAG, "netMask " + netMask);
|
---|
| 295 | + mEthIpAddress = mgetHostAddress;
|
---|
| 296 | + mEthNetmask = netMask;
|
---|
| 297 | + break;
|
---|
| 298 | + }
|
---|
| 299 | + }
|
---|
| 300 | +
|
---|
| 301 | + String dns1 = null;
|
---|
| 302 | + String dns2 = null;
|
---|
| 303 | + int dnsCount = 0;
|
---|
| 304 | + for (InetAddress inetAddress:linkProperties.getDnsServers()) {
|
---|
| 305 | + if (0 == dnsCount) {
|
---|
| 306 | + dns1 = inetAddress.getHostAddress().toString();
|
---|
| 307 | + if (inetAddress instanceof Inet4Address) {
|
---|
| 308 | + dns1 = inetAddress.getHostAddress().toString();
|
---|
| 309 | + } else {
|
---|
| 310 | + dns1 = "0.0.0.0";
|
---|
| 311 | + }
|
---|
| 312 | + }
|
---|
| 313 | + if (1 == dnsCount) {
|
---|
| 314 | + dns2 = inetAddress.getHostAddress().toString();
|
---|
| 315 | + if (inetAddress instanceof Inet4Address) {
|
---|
| 316 | + dns2 = inetAddress.getHostAddress().toString();
|
---|
| 317 | + } else {
|
---|
| 318 | + dns2 = "0.0.0.0";
|
---|
| 319 | + }
|
---|
| 320 | + }
|
---|
| 321 | + dnsCount++;
|
---|
| 322 | + }
|
---|
| 323 | +
|
---|
| 324 | + mEthdns1 = dns1;
|
---|
| 325 | + if (dnsCount > 1) {
|
---|
| 326 | + mEthdns2 = dns2;
|
---|
| 327 | + } else {
|
---|
| 328 | + dns2 = "0.0.0.0";
|
---|
| 329 | + mEthdns2 = dns2;
|
---|
| 330 | + }
|
---|
| 331 | +
|
---|
| 332 | + String Route = null;
|
---|
| 333 | + for (RouteInfo route:linkProperties.getRoutes()) {
|
---|
| 334 | + if (route.isIPv4Default()) {
|
---|
| 335 | + Route = route.getGateway().toString();
|
---|
| 336 | + break;
|
---|
| 337 | + }
|
---|
| 338 | + }
|
---|
| 339 | + if (Route != null) {
|
---|
| 340 | + String[] RouteStr = Route.split("/");
|
---|
| 341 | + mEthGateway = RouteStr[1];
|
---|
| 342 | + } else {
|
---|
| 343 | + mEthGateway = "0.0.0.0";
|
---|
| 344 | + }
|
---|
| 345 | + Log.v(TAG, "mIfaceName " + mIfaceName);
|
---|
| 346 | + Log.v(TAG, "mEthIpAddress " + mEthIpAddress);
|
---|
| 347 | + Log.v(TAG, "mEthNetmask " + mEthNetmask);
|
---|
| 348 | + Log.v(TAG, "mEthGateway " + mEthGateway);
|
---|
| 349 | + Log.v(TAG, "mEthdns1 " + mEthdns1);
|
---|
| 350 | + Log.v(TAG, "mEthdns2 " + mEthdns2);
|
---|
| 351 | +
|
---|
| 352 | + }
|
---|
| 353 | +
|
---|
251 | 354 | public void getEthInfoFromDhcp() throws Exception {
|
---|
252 | 355 | boolean isSdkAfterO = Build.VERSION.SDK_INT >= ConfigUtil.ANDROID_SDK_VERSION_P;
|
---|
253 | 356 | String tempIpInfo;
|
---|