1 /* 2 * Copyright (C) 2017 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 package com.android.car.settings.wifi.details; 17 18 import android.car.drivingstate.CarUxRestrictions; 19 import android.content.Context; 20 import android.net.LinkAddress; 21 import android.net.NetworkUtils; 22 23 import com.android.car.settings.common.FragmentController; 24 25 import java.net.Inet4Address; 26 import java.net.InetAddress; 27 import java.net.UnknownHostException; 28 29 /** 30 * Shows info about Wifi subnet. 31 */ 32 public class WifiSubnetPreferenceController extends 33 WifiDetailsBasePreferenceController<WifiDetailsPreference> { 34 WifiSubnetPreferenceController(Context context, String preferenceKey, FragmentController fragmentController, CarUxRestrictions uxRestrictions)35 public WifiSubnetPreferenceController(Context context, String preferenceKey, 36 FragmentController fragmentController, CarUxRestrictions uxRestrictions) { 37 super(context, preferenceKey, fragmentController, uxRestrictions); 38 } 39 40 @Override getPreferenceType()41 protected Class<WifiDetailsPreference> getPreferenceType() { 42 return WifiDetailsPreference.class; 43 } 44 45 @Override updateState(WifiDetailsPreference preference)46 protected void updateState(WifiDetailsPreference preference) { 47 String subnet = null; 48 49 for (LinkAddress addr : getWifiInfoProvider().getLinkProperties().getLinkAddresses()) { 50 if (addr.getAddress() instanceof Inet4Address) { 51 subnet = ipv4PrefixLengthToSubnetMask(addr.getPrefixLength()); 52 } 53 } 54 55 if (subnet == null) { 56 preference.setVisible(false); 57 } else { 58 preference.setDetailText(subnet); 59 preference.setVisible(true); 60 } 61 } 62 ipv4PrefixLengthToSubnetMask(int prefixLength)63 private static String ipv4PrefixLengthToSubnetMask(int prefixLength) { 64 try { 65 InetAddress all = InetAddress.getByAddress( 66 new byte[]{(byte) 255, (byte) 255, (byte) 255, (byte) 255}); 67 return NetworkUtils.getNetworkPart(all, prefixLength).getHostAddress(); 68 } catch (UnknownHostException e) { 69 return null; 70 } 71 } 72 } 73