1 /* 2 * Copyright (C) 2025 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.settings.network.ethernet 18 19 import android.content.Context 20 import android.net.EthernetManager 21 import android.net.IpConfiguration 22 import android.net.LinkProperties 23 import android.net.StaticIpConfiguration 24 import android.widget.ImageView 25 import androidx.lifecycle.Lifecycle 26 import androidx.lifecycle.LifecycleEventObserver 27 import androidx.lifecycle.LifecycleOwner 28 import androidx.preference.Preference 29 import androidx.preference.PreferenceFragmentCompat 30 import androidx.preference.PreferenceScreen 31 import com.android.settings.R 32 import com.android.settings.widget.EntityHeaderController 33 import com.android.settingslib.core.AbstractPreferenceController 34 import com.android.settingslib.widget.LayoutPreference 35 36 class EthernetInterfaceDetailsController( 37 context: Context, 38 private val fragment: PreferenceFragmentCompat, 39 private val preferenceId: String, 40 private val lifecycle: Lifecycle, 41 ) : 42 AbstractPreferenceController(context), 43 EthernetInterface.EthernetInterfaceStateListener, 44 LifecycleEventObserver { 45 private val KEY_HEADER = "ethernet_details" 46 47 private val ethernetManager = context.getSystemService(EthernetManager::class.java) 48 private val ethernetInterface = 49 EthernetTrackerImpl.getInstance(context).getInterface(preferenceId) 50 51 private lateinit var entityHeaderController: EntityHeaderController 52 53 private var ipAddressPref: Preference? = null 54 55 init { 56 lifecycle.addObserver(this) 57 } 58 isAvailablenull59 override fun isAvailable(): Boolean { 60 return true 61 } 62 getPreferenceKeynull63 override fun getPreferenceKey(): String? { 64 return KEY_HEADER 65 } 66 onStateChangednull67 override fun onStateChanged(source: LifecycleOwner, event: Lifecycle.Event) { 68 when (event) { 69 Lifecycle.Event.ON_START -> { 70 ethernetInterface?.registerListener(this) 71 } 72 73 Lifecycle.Event.ON_STOP -> { 74 ethernetInterface?.unregisterListener(this) 75 } 76 77 else -> {} 78 } 79 } 80 displayPreferencenull81 override fun displayPreference(screen: PreferenceScreen) { 82 val headerPref: LayoutPreference? = screen.findPreference(KEY_HEADER) 83 84 entityHeaderController = 85 EntityHeaderController.newInstance( 86 fragment.getActivity(), 87 fragment, 88 headerPref?.findViewById(R.id.entity_header), 89 ) 90 91 val iconView: ImageView? = headerPref?.findViewById(R.id.entity_header_icon) 92 93 iconView?.setScaleType(ImageView.ScaleType.CENTER_INSIDE) 94 95 if (entityHeaderController != null) { 96 entityHeaderController 97 .setLabel("Ethernet") 98 .setSummary( 99 if (ethernetInterface?.getInterfaceState() == EthernetManager.STATE_LINK_UP) { 100 mContext.getString(R.string.network_connected) 101 } else { 102 mContext.getString(R.string.network_disconnected) 103 } 104 ) 105 .setSecondSummary("") 106 .setIcon(mContext.getDrawable(R.drawable.ic_settings_ethernet)) 107 .done(true /* rebind */) 108 } 109 110 ipAddressPref = screen.findPreference<Preference>("ethernet_ip_address") 111 112 if (ethernetInterface?.getInterfaceState() == EthernetManager.STATE_LINK_UP) { 113 initializeIpDetails() 114 } 115 } 116 interfaceUpdatednull117 override fun interfaceUpdated() { 118 entityHeaderController?.setSummary( 119 if (ethernetInterface?.getInterfaceState() == EthernetManager.STATE_LINK_UP) { 120 mContext.getString(R.string.network_connected) 121 } else { 122 mContext.getString(R.string.network_disconnected) 123 } 124 ) 125 initializeIpDetails() 126 } 127 initializeIpDetailsnull128 private fun initializeIpDetails() { 129 val ipConfiguration: IpConfiguration? = ethernetInterface?.getConfiguration() 130 val linkProperties: LinkProperties? = ethernetInterface?.getLinkProperties() 131 132 if (ipConfiguration?.getIpAssignment() == IpConfiguration.IpAssignment.STATIC) { 133 val staticIp: StaticIpConfiguration? = ipConfiguration?.getStaticIpConfiguration() 134 ipAddressPref?.setSummary(staticIp?.getIpAddress().toString()) 135 } else { 136 val addresses = linkProperties?.getAddresses() 137 ipAddressPref?.setSummary(addresses?.first().toString()) 138 } 139 } 140 } 141