android: Add a text field to display the devices network address

This commit is contained in:
Colin Edwards
2023-12-21 00:04:17 -06:00
parent 2c851498cc
commit c4c1044427
5 changed files with 70 additions and 8 deletions

View File

@@ -0,0 +1,31 @@
package org.musicpd;
import android.content.Context;
import android.net.ConnectivityManager;
import android.net.LinkAddress;
import android.net.LinkProperties;
import java.net.Inet4Address;
import java.net.InetAddress;
import java.util.List;
public class NetworkUtil {
public static String getDeviceIPV4Address(Context context) {
ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
LinkProperties linkProperties = connectivityManager.getLinkProperties(connectivityManager.getActiveNetwork());
if (linkProperties != null) {
List<LinkAddress> linkAddresses = linkProperties.getLinkAddresses();
for (LinkAddress address : linkAddresses) {
if (!address.getAddress().isLinkLocalAddress() && !address.getAddress().isLoopbackAddress()) {
InetAddress address1 = address.getAddress();
if (address1 instanceof Inet4Address) {
return address1.getHostAddress();
}
}
}
}
return null;
}
}

View File

@@ -213,6 +213,10 @@ public class Settings extends Activity {
if (Preferences.getBoolean(this, Preferences.KEY_PAUSE_ON_HEADPHONES_DISCONNECT, false))
checkbox.setChecked(true);
TextView networkAddressTextView = (TextView) findViewById(R.id.networkAddress);
String deviceIPV4Address = NetworkUtil.getDeviceIPV4Address(this);
networkAddressTextView.setText(deviceIPV4Address);
super.onCreate(savedInstanceState);
}