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 17 package com.android.tv.settings.about; 18 19 import android.content.Context; 20 import android.net.ConnectivityManager; 21 import android.net.LinkProperties; 22 import android.net.Network; 23 import android.net.NetworkCapabilities; 24 import android.util.Log; 25 26 import androidx.preference.Preference; 27 import androidx.preference.PreferenceScreen; 28 29 import com.android.settingslib.core.lifecycle.Lifecycle; 30 import com.android.settingslib.deviceinfo.AbstractWifiMacAddressPreferenceController; 31 32 import java.net.NetworkInterface; 33 import java.net.SocketException; 34 import java.util.Collections; 35 36 /** 37 * Concrete subclass of WIFI MAC address preference controller 38 */ 39 public class MacAddressPreferenceController extends AbstractWifiMacAddressPreferenceController { 40 private static final String TAG = "MacAddressPC"; 41 42 private final ConnectivityManager mConnectivityManager; 43 private Preference mMacAddress; 44 MacAddressPreferenceController(Context context, Lifecycle lifecycle)45 public MacAddressPreferenceController(Context context, Lifecycle lifecycle) { 46 super(context, lifecycle); 47 mConnectivityManager = context.getSystemService(ConnectivityManager.class); 48 } 49 50 @Override displayPreference(PreferenceScreen screen)51 public void displayPreference(PreferenceScreen screen) { 52 mMacAddress = screen.findPreference(getPreferenceKey()); 53 super.displayPreference(screen); 54 } 55 56 @Override updateConnectivity()57 protected void updateConnectivity() { 58 Network activeNetwork = mConnectivityManager.getActiveNetwork(); 59 NetworkCapabilities networkCapabilities = activeNetwork != null 60 ? mConnectivityManager.getNetworkCapabilities(activeNetwork) : null; 61 LinkProperties linkProperties = activeNetwork != null 62 ? mConnectivityManager.getLinkProperties(activeNetwork) : null; 63 if (mMacAddress == null || networkCapabilities == null || linkProperties == null 64 || linkProperties.getInterfaceName() == null 65 || !networkCapabilities.hasTransport(NetworkCapabilities.TRANSPORT_ETHERNET)) { 66 super.updateConnectivity(); 67 return; 68 } 69 70 // Find ethernet mac address from system. 71 try { 72 for (NetworkInterface networkInterface : 73 Collections.list(NetworkInterface.getNetworkInterfaces())) { 74 byte[] mac = networkInterface.getHardwareAddress(); 75 if (mac != null && linkProperties.getInterfaceName().equalsIgnoreCase( 76 networkInterface.getName())) { 77 StringBuilder macString = new StringBuilder(); 78 for (byte b : mac) { 79 macString.append(String.format(macString.length() == 0 80 ? "%02X" : ":%02X", b)); 81 } 82 mMacAddress.setSummary(macString); 83 return; 84 } 85 86 } 87 } catch (SocketException e) { 88 Log.e(TAG, "Unable to list network interfaces", e); 89 } 90 91 super.updateConnectivity(); 92 } 93 } 94