List all listening network interfaces

This commit is contained in:
edr
2020-05-13 21:36:33 +02:00
parent 9b5857e2c8
commit b2df26e13b
9 changed files with 134 additions and 87 deletions

View File

@@ -4,6 +4,7 @@
android:versionCode="2"
android:versionName="0.2" >
<uses-permission android:required="true" android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:required="true" android:name="android.permission.INTERNET"/>
<uses-permission android:required="true" android:name="android.permission.RECORD_AUDIO"/>
<uses-permission android:required="true" android:name="android.permission.ACCESS_WIFI_STATE"/>

View File

@@ -18,20 +18,24 @@ package de.rochefort.childmonitor;
import java.io.IOException;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import android.app.Activity;
import android.content.Context;
import android.media.AudioRecord;
import android.media.MediaRecorder;
import android.net.ConnectivityManager;
import android.net.LinkAddress;
import android.net.Network;
import android.net.NetworkInfo;
import android.net.nsd.NsdManager;
import android.net.nsd.NsdServiceInfo;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.os.Bundle;
import android.text.format.Formatter;
import android.util.Log;
import android.widget.TextView;
@@ -138,23 +142,47 @@ public class MonitorActivity extends Activity {
}
}).start();
final TextView addressText = (TextView) findViewById(R.id.address);
final TextView addressText = findViewById(R.id.address);
// Use the application context to get WifiManager, to avoid leak before Android 5.1
final WifiManager wifiManager =
(WifiManager) getApplicationContext().getSystemService(WIFI_SERVICE);
final WifiInfo info = wifiManager.getConnectionInfo();
final int address = info.getIpAddress();
if(address != 0) {
@SuppressWarnings("deprecation")
final String ipAddress = Formatter.formatIpAddress(address);
addressText.setText(ipAddress);
List<String> listenAddresses = getListenAddresses();
if(!listenAddresses.isEmpty()) {
StringBuilder sb = new StringBuilder();
for (int i = 0; i < listenAddresses.size(); i++) {
String listenAddress = listenAddresses.get(i);
sb.append(listenAddress);
if (i != listenAddresses.size() -1) {
sb.append("\n\n");
}
}
addressText.setText(sb.toString());
} else {
addressText.setText(R.string.wifiNotConnected);
addressText.setText(R.string.notConnected);
}
}
private List<String> getListenAddresses() {
String service = Context.CONNECTIVITY_SERVICE;
ConnectivityManager cm = (ConnectivityManager)getSystemService(service);
List<String> listenAddresses = new ArrayList<>();
if (cm != null) {
for (Network network : cm.getAllNetworks()) {
NetworkInfo networkInfo = cm.getNetworkInfo(network);
boolean connected = networkInfo.isConnected();
if (connected) {
List<LinkAddress> linkAddresses = cm.getLinkProperties(network).getLinkAddresses();
for (LinkAddress linkAddress : linkAddresses) {
InetAddress address = linkAddress.getAddress();
if (!address.isLinkLocalAddress() && !address.isLoopbackAddress()) {
listenAddresses.add(address.getHostAddress() + " (" + networkInfo.getTypeName() + ")");
}
}
}
}
}
return listenAddresses;
}
@Override
protected void onDestroy() {
Log.i(TAG, "ChildMonitor stop");

View File

@@ -20,76 +20,98 @@
android:textSize="25sp" />
<Space
android:layout_width="match_parent"
android:layout_height="15dip" />
android:layout_width="match_parent"
android:layout_height="15dip" />
<TextView
android:id="@+id/serviceTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/serviceTitle"
android:textSize="20sp" />
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true">
<TextView
android:id="@+id/textService"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/loading"
android:textSize="20sp" />
<LinearLayout
android:id="@+id/LinearLayout2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:keepScreenOn="true"
tools:context="de.rochefort.childmonitor.MonitorActivity" >
<TextView
android:id="@+id/serviceDescription"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/serviceDescription" />
<TextView
android:id="@+id/serviceTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/serviceTitle"
android:textSize="20sp" />
<Space
android:layout_width="match_parent"
android:layout_height="15dip" />
<TextView
android:id="@+id/textService"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/loading"
android:textSize="20sp" />
<TextView
android:id="@+id/addressTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/addressTitle"
android:textSize="20sp" />
<TextView
android:id="@+id/serviceDescription"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/serviceDescription" />
<TextView
android:id="@+id/address"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=""
android:textSize="20sp"/>
<Space
android:layout_width="match_parent"
android:layout_height="15dip" />
<TextView
android:id="@+id/portTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/portTitle"
android:textSize="20sp" />
<Space
android:layout_width="match_parent"
android:layout_height="15dip" />
<TextView
android:id="@+id/port"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/loading"
android:textSize="20sp"/>
<TextView
android:id="@+id/portTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/portTitle"
android:textSize="20sp" />
<TextView
android:id="@+id/addressDescription"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/addressDescription" />
<TextView
android:id="@+id/port"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/loading"
android:textSize="20sp"/>
<Space
android:layout_width="match_parent"
android:layout_height="15dip" />
<Space
android:layout_width="match_parent"
android:layout_height="15dip" />
<TextView
android:id="@+id/textStatus"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=""
android:textSize="20sp" />
<TextView
android:id="@+id/addressTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/addressTitle"
android:textSize="20sp" />
<Space
android:layout_width="match_parent"
android:layout_height="15dip" />
<TextView
android:id="@+id/address"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text=""
android:textSize="15sp"/>
<Space
android:layout_width="match_parent"
android:layout_height="15dip" />
<TextView
android:id="@+id/textStatus"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=""
android:textSize="20sp" />
</LinearLayout>
</ScrollView>
</LinearLayout>

View File

@@ -20,10 +20,9 @@
<string name="connectedTo">Verbunden mit:</string>
<string name="disconnected">Getrennt</string>
<string name="listening">Lausche...</string>
<string name="addressTitle">Adresse:</string>
<string name="addressTitle">Adressen:</string>
<string name="portTitle">Port:</string>
<string name="addressDescription">Adresse und Port zum Koppeln mit Eltern</string>
<string name="wifiNotConnected">Nicht mit WLAN-Netzwerk verbunden</string>
<string name="notConnected">Mit keinem Netzwerk verbunden</string>
<string name="enterAddressInstructions">Gib die IP-Adresse und den Port des Kind-Gerätes ein und klicke auf Verbinden</string>
<string name="connect">Verbinden</string>
<string name="exampleAddress">192.168.1.2</string>

View File

@@ -22,8 +22,7 @@
<string name="listening">リスニング中...</string>
<string name="addressTitle">アドレス:</string>
<string name="portTitle">ポート:</string>
<string name="addressDescription">親機がペアにする先のアドレスとポート</string>
<string name="wifiNotConnected">Wi-Fi ネットワークに接続されていません</string>
<string name="notConnected">ネットワークに接続されていません</string>
<string name="enterAddressInstructions">子機の IP アドレスとポートを入力して、接続をクリックしてください</string>
<string name="connect">接続</string>
<string name="exampleAddress">192.168.1.2</string>

View File

@@ -20,10 +20,9 @@
<string name="connectedTo">Verbonden met:</string>
<string name="disconnected">Verbinding verbroken</string>
<string name="listening">Bezig met luisteren...</string>
<string name="addressTitle">Adres:</string>
<string name="addressTitle">Adressen:</string>
<string name="portTitle">Poort:</string>
<string name="addressDescription">Adres en poort waar de ouder mee moet verbinden</string>
<string name="wifiNotConnected">Niet verbonden met een Wi-Fi-netwerk</string>
<string name="notConnected">Niet verbonden met een netwerk</string>
<string name="enterAddressInstructions">Voer het IP-adres en de poort van een kindapparaat in en druk op Verbinden</string>
<string name="connect">Verbinden</string>
<string name="exampleAddress">192.168.1.2</string>

View File

@@ -20,10 +20,9 @@
<string name="connectedTo">Connected To:</string>
<string name="disconnected">Disconnected</string>
<string name="listening">Listening...</string>
<string name="addressTitle">Address:</string>
<string name="addressTitle">Addresses:</string>
<string name="portTitle">Port:</string>
<string name="addressDescription">Address and port that parent must pair to</string>
<string name="wifiNotConnected">Not connected to a Wi-Fi network</string>
<string name="notConnected">Not connected to any network</string>
<string name="enterAddressInstructions">Enter the IP Address and port of a child and click Connect</string>
<string name="connect">Connect</string>
<string name="exampleAddress">192.168.1.2</string>