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.LinkProperties; 22 23 import com.android.car.settings.common.FragmentController; 24 import com.android.net.module.util.Inet4AddressUtils; 25 26 import java.net.Inet4Address; 27 28 /** 29 * Shows info about Wifi subnet. 30 */ 31 public class WifiSubnetPreferenceController extends 32 WifiDetailsBasePreferenceController<WifiDetailsPreference> { 33 WifiSubnetPreferenceController(Context context, String preferenceKey, FragmentController fragmentController, CarUxRestrictions uxRestrictions)34 public WifiSubnetPreferenceController(Context context, String preferenceKey, 35 FragmentController fragmentController, CarUxRestrictions uxRestrictions) { 36 super(context, preferenceKey, fragmentController, uxRestrictions); 37 } 38 39 @Override getPreferenceType()40 protected Class<WifiDetailsPreference> getPreferenceType() { 41 return WifiDetailsPreference.class; 42 } 43 44 @Override updateState(WifiDetailsPreference preference)45 protected void updateState(WifiDetailsPreference preference) { 46 LinkProperties linkProperties = getWifiInfoProvider().getLinkProperties(); 47 String subnet = null; 48 if (linkProperties != null) { 49 for (LinkAddress addr : linkProperties.getLinkAddresses()) { 50 if (addr.getAddress() instanceof Inet4Address) { 51 subnet = ipv4PrefixLengthToSubnetMask(addr.getPrefixLength()); 52 } 53 } 54 } 55 56 if (subnet == null) { 57 preference.setVisible(false); 58 } else { 59 preference.setDetailText(subnet); 60 preference.setVisible(true); 61 } 62 } 63 ipv4PrefixLengthToSubnetMask(int prefixLength)64 private static String ipv4PrefixLengthToSubnetMask(int prefixLength) { 65 try { 66 return Inet4AddressUtils.getPrefixMaskAsInet4Address(prefixLength).getHostAddress(); 67 } catch (IllegalArgumentException e) { 68 return null; 69 } 70 } 71 } 72