diff options
author | Oleg Kibirev <olegk@google.com> | 2023-11-17 18:54:15 +0000 |
---|---|---|
committer | Cherrypicker Worker <android-build-cherrypicker-worker@google.com> | 2023-11-17 19:13:45 +0000 |
commit | 4a548e711ed4c1b80c1d45e648123d76b03546d5 (patch) | |
tree | a2f1811afe73ee7bf7b94a40bea5656b461426c8 | |
parent | 3a97b10e1384feb70ed6965000510ef0daaca37d (diff) | |
download | TvSettings-4a548e711ed4c1b80c1d45e648123d76b03546d5.tar.gz |
Display Mac address of active network interface in Settings/About/Status
This can be a Wifi or ethernet connection
Bug: 286409648
(cherry picked from https://googleplex-android-review.googlesource.com/q/commit:36df96c97ab79f9248b38a1aaa1f265544734932)
Merged-In: I46a25eeee97040c28724830d2a390ef50d623075
Change-Id: I46a25eeee97040c28724830d2a390ef50d623075
3 files changed, 94 insertions, 34 deletions
diff --git a/Settings/src/com/android/tv/settings/about/MacAddressPreferenceController.java b/Settings/src/com/android/tv/settings/about/MacAddressPreferenceController.java new file mode 100644 index 000000000..b8a31dc25 --- /dev/null +++ b/Settings/src/com/android/tv/settings/about/MacAddressPreferenceController.java @@ -0,0 +1,93 @@ +/* + * Copyright (C) 2017 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.android.tv.settings.about; + +import android.content.Context; +import android.net.ConnectivityManager; +import android.net.LinkProperties; +import android.net.Network; +import android.net.NetworkCapabilities; +import android.util.Log; + +import androidx.preference.Preference; +import androidx.preference.PreferenceScreen; + +import com.android.settingslib.core.lifecycle.Lifecycle; +import com.android.settingslib.deviceinfo.AbstractWifiMacAddressPreferenceController; + +import java.net.NetworkInterface; +import java.net.SocketException; +import java.util.Collections; + +/** + * Concrete subclass of WIFI MAC address preference controller + */ +public class MacAddressPreferenceController extends AbstractWifiMacAddressPreferenceController { + private static final String TAG = "MacAddressPC"; + + private final ConnectivityManager mConnectivityManager; + private Preference mMacAddress; + + public MacAddressPreferenceController(Context context, Lifecycle lifecycle) { + super(context, lifecycle); + mConnectivityManager = context.getSystemService(ConnectivityManager.class); + } + + @Override + public void displayPreference(PreferenceScreen screen) { + mMacAddress = screen.findPreference(getPreferenceKey()); + super.displayPreference(screen); + } + + @Override + protected void updateConnectivity() { + Network activeNetwork = mConnectivityManager.getActiveNetwork(); + NetworkCapabilities networkCapabilities = activeNetwork != null + ? mConnectivityManager.getNetworkCapabilities(activeNetwork) : null; + LinkProperties linkProperties = activeNetwork != null + ? mConnectivityManager.getLinkProperties(activeNetwork) : null; + if (mMacAddress == null || networkCapabilities == null || linkProperties == null + || linkProperties.getInterfaceName() == null + || !networkCapabilities.hasTransport(NetworkCapabilities.TRANSPORT_ETHERNET)) { + super.updateConnectivity(); + return; + } + + // Find ethernet mac address from system. + try { + for (NetworkInterface networkInterface : + Collections.list(NetworkInterface.getNetworkInterfaces())) { + byte[] mac = networkInterface.getHardwareAddress(); + if (mac != null && linkProperties.getInterfaceName().equalsIgnoreCase( + networkInterface.getName())) { + StringBuilder macString = new StringBuilder(); + for (byte b : mac) { + macString.append(String.format(macString.length() == 0 + ? "%02X" : ":%02X", b)); + } + mMacAddress.setSummary(macString); + return; + } + + } + } catch (SocketException e) { + Log.e(TAG, "Unable to list network interfaces", e); + } + + super.updateConnectivity(); + } +} diff --git a/Settings/src/com/android/tv/settings/about/StatusFragment.java b/Settings/src/com/android/tv/settings/about/StatusFragment.java index e6d0db050..5c7df850a 100644 --- a/Settings/src/com/android/tv/settings/about/StatusFragment.java +++ b/Settings/src/com/android/tv/settings/about/StatusFragment.java @@ -61,7 +61,7 @@ public class StatusFragment extends PreferenceControllerFragment { controllers.add(new UptimePreferenceController(context, lifecycle)); controllers.add(new BluetoothAddressPreferenceController(context, lifecycle)); controllers.add(new IpAddressPreferenceController(context, lifecycle)); - controllers.add(new WifiMacAddressPreferenceController(context, lifecycle)); + controllers.add(new MacAddressPreferenceController(context, lifecycle)); controllers.add(new ImsStatusPreferenceController(context, lifecycle)); return controllers; diff --git a/Settings/src/com/android/tv/settings/about/WifiMacAddressPreferenceController.java b/Settings/src/com/android/tv/settings/about/WifiMacAddressPreferenceController.java deleted file mode 100644 index f234a2b5f..000000000 --- a/Settings/src/com/android/tv/settings/about/WifiMacAddressPreferenceController.java +++ /dev/null @@ -1,33 +0,0 @@ -/* - * Copyright (C) 2017 The Android Open Source Project - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package com.android.tv.settings.about; - -import android.content.Context; - -import com.android.settingslib.core.lifecycle.Lifecycle; -import com.android.settingslib.deviceinfo.AbstractWifiMacAddressPreferenceController; - -/** - * Concrete subclass of WIFI MAC address preference controller - */ -public class WifiMacAddressPreferenceController extends AbstractWifiMacAddressPreferenceController { - public WifiMacAddressPreferenceController(Context context, Lifecycle lifecycle) { - super(context, lifecycle); - } - - // This space intentionally left blank -} |