-
-
Notifications
You must be signed in to change notification settings - Fork 214
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
First attempt at Network State Change listening
- Revised some of the data used for ListenIfAdapter; - NetworkInterfaceTester is a class that report change on network state, it's still a WIP, it is now used to update the Spinner in relation to the available network interfaces;
- Loading branch information
Showing
4 changed files
with
274 additions
and
162 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,9 @@ | ||
/* | ||
* DroidVNC-NG ListenIfAdapter. | ||
* | ||
* Author: Christian Beier <[email protected] | ||
* Author: elluisian <[email protected]> | ||
* | ||
* Copyright (C) 2020 Kitchen Armor. | ||
* Copyright (C) 2020 Christian Beier. | ||
* | ||
* You can redistribute and/or modify this program under the terms of the | ||
* GNU General Public License version 2 as published by the Free Software | ||
|
@@ -20,6 +20,10 @@ | |
*/ | ||
package net.christianbeier.droidvnc_ng; | ||
|
||
|
||
import java.net.NetworkInterface; | ||
import java.util.ArrayList; | ||
|
||
import android.content.res.Resources; | ||
import android.content.Context; | ||
import android.util.Log; | ||
|
@@ -30,118 +34,41 @@ | |
import android.widget.Spinner; | ||
import android.view.LayoutInflater; | ||
|
||
import java.util.regex.Matcher; | ||
import java.util.regex.Pattern; | ||
import java.net.NetworkInterface; | ||
import java.util.ArrayList; | ||
|
||
|
||
public class ListenIfAdapter extends ArrayAdapter<ListenIfAdapter.NetworkInterfaceData> { | ||
// Class representing the data to show | ||
static class NetworkInterfaceData { | ||
// This represents the "Any" option | ||
public static NetworkInterfaceData ANY; | ||
|
||
|
||
private NetworkInterface nic; | ||
private String techName; | ||
private String friendlyName; | ||
|
||
|
||
public NetworkInterfaceData(ListenIfAdapter lia) { | ||
this(null, lia); | ||
} | ||
|
||
|
||
public NetworkInterfaceData(NetworkInterface nic, ListenIfAdapter lia) { | ||
this.nic = nic; | ||
|
||
if (nic == null) { | ||
this.techName = "0.0.0.0"; | ||
this.friendlyName = lia.getStringForAny(); | ||
|
||
} else { | ||
this.techName = this.nic.getName(); | ||
String[] ifNameInfo = lia.extractIfInfo(this.techName); | ||
if (ifNameInfo == null) { // No info found, consider "techName" as friendly | ||
this.friendlyName = this.techName; | ||
|
||
} else { | ||
this.friendlyName = lia.getFriendlyStringForInterface(ifNameInfo[0]); | ||
if (ifNameInfo.length > 1) { | ||
this.friendlyName += " " + ifNameInfo[1]; | ||
} | ||
} | ||
} | ||
} | ||
|
||
|
||
public static NetworkInterfaceData getAnyOption(ListenIfAdapter lia) { | ||
if (ANY == null) { | ||
ANY = new NetworkInterfaceData(lia); | ||
} | ||
return ANY; | ||
} | ||
|
||
|
||
public String getName() { | ||
return this.techName; | ||
} | ||
|
||
|
||
public String getFriendlyName() { | ||
return this.friendlyName; | ||
} | ||
|
||
|
||
public String toString() { | ||
return this.friendlyName + " (" + this.techName + ")"; | ||
} | ||
} | ||
|
||
|
||
|
||
|
||
|
||
|
||
public class ListenIfAdapter extends ArrayAdapter<NetworkInterfaceTester.NetIfData> implements NetworkInterfaceTester.OnNetworkStateChangedListener { | ||
// This adapter uses the ViewHolder pattern | ||
private static class ViewHolder { | ||
public TextView txtLabel; | ||
} | ||
|
||
// Data to be shown with the adapter | ||
private ArrayList<NetworkInterfaceData> data; | ||
private ArrayList<NetworkInterfaceTester.NetIfData> data; | ||
private int dataSize; | ||
|
||
|
||
// Some context data for "easy retrieval" | ||
private Context mContext; | ||
private Resources mResources; | ||
private LayoutInflater mInflater; | ||
|
||
|
||
|
||
|
||
public ListenIfAdapter(ArrayList<NetworkInterface> ifs, Context context) { | ||
public ListenIfAdapter(NetworkInterfaceTester nit, Context context) { | ||
super(context, R.layout.spinner_row, R.id.spinner_text); | ||
|
||
this.mContext = context; | ||
this.mResources = this.mContext.getResources(); | ||
this.mInflater = LayoutInflater.from(this.mContext); | ||
|
||
this.data = new ArrayList<>(); | ||
this.data.add(NetworkInterfaceData.getAnyOption(this)); | ||
for (NetworkInterface nic : ifs) { | ||
this.data.add(new NetworkInterfaceData(nic, this)); | ||
} | ||
this.dataSize = this.data.size(); | ||
nit.addOnNetworkStateChangedListener(this); | ||
this.onNetworkStateChanged(nit, null, false); | ||
} | ||
|
||
|
||
|
||
public int getItemPositionByIfName(String ifName) { | ||
int i = 0; | ||
for (NetworkInterfaceData nid : this.data) { | ||
for (NetworkInterfaceTester.NetIfData nid : this.data) { | ||
if (nid.getName().equals(ifName)) { | ||
return i; | ||
} | ||
|
@@ -156,7 +83,7 @@ public int getItemPositionByIfName(String ifName) { | |
|
||
@Override | ||
public View getView(int position, View convertView, ViewGroup parent) { | ||
if (convertView == null) { // Check if view must be recreated, using the famous ViewHolder pattern | ||
if (convertView == null) { // Check if view must be recreated using the famous ViewHolder pattern | ||
convertView = this.mInflater.inflate(R.layout.spinner_row, parent, false); | ||
|
||
ViewHolder vh = new ViewHolder(); | ||
|
@@ -165,99 +92,31 @@ public View getView(int position, View convertView, ViewGroup parent) { | |
} | ||
|
||
ViewHolder vh = (ViewHolder)convertView.getTag(); | ||
NetworkInterfaceData nid = this.getItem(position); | ||
NetworkInterfaceTester.NetIfData nid = this.getItem(position); | ||
vh.txtLabel.setText(nid.toString()); | ||
|
||
return convertView; | ||
} | ||
|
||
|
||
@Override | ||
public NetworkInterfaceData getItem(int position) { | ||
public NetworkInterfaceTester.NetIfData getItem(int position) { | ||
if (0 <= position && position < this.getCount()) { | ||
return this.data.get(position); | ||
} | ||
return null; | ||
} | ||
|
||
|
||
|
||
@Override | ||
public int getCount() { | ||
return this.dataSize; | ||
} | ||
|
||
|
||
|
||
|
||
private String getStringForAny() { | ||
return this.mResources.getString(R.string.main_activity_settings_listenif_spin_any); | ||
} | ||
|
||
|
||
/** | ||
* Since the majority of the interfaces have standard Linux names, this method tries to get a possible friendly name. | ||
* Special thanks go to the pages: | ||
* - https://stackoverflow.com/questions/33747787/android-networkinterface-what-are-the-meanings-of-names-of-networkinterface | ||
* - https://stackoverflow.com/questions/47488435/meaning-of-network-interface-rmnet-ipa0 | ||
* for some of the obscure interface names | ||
* @param ifName name (technical) of the interface | ||
* @return if known, a friendly name for the interface, otherwise, null | ||
*/ | ||
private String getFriendlyStringForInterface(String ifName) { | ||
String ifFriendlyName = null; // Null if no friendly name is known | ||
|
||
if (ifName.equals("eth")) { | ||
ifFriendlyName = this.mResources.getString(R.string.main_activity_settings_listenif_spin_eth); | ||
|
||
} else if (ifName.equals("wlan")) { | ||
ifFriendlyName = this.mResources.getString(R.string.main_activity_settings_listenif_spin_wifi); | ||
|
||
} else if (ifName.equals("lo")) { | ||
ifFriendlyName = this.mResources.getString(R.string.main_activity_settings_listenif_spin_lpback); | ||
|
||
} else if (ifName.equals("tun")) { | ||
ifFriendlyName = this.mResources.getString(R.string.main_activity_settings_listenif_spin_tun); | ||
|
||
} else if (ifName.equals("dummy")) { | ||
ifFriendlyName = this.mResources.getString(R.string.main_activity_settings_listenif_spin_dum); | ||
|
||
} else if (ifName.equals("rmnet_data")) { | ||
ifFriendlyName = this.mResources.getString(R.string.main_activity_settings_listenif_spin_rmndata); | ||
|
||
} else if (ifName.equals("rmnet_ipa")) { | ||
ifFriendlyName = this.mResources.getString(R.string.main_activity_settings_listenif_spin_rmnipa); | ||
|
||
} | ||
|
||
|
||
return ifFriendlyName; | ||
} | ||
|
||
|
||
|
||
|
||
/* | ||
* This method is used to extract the name and the number of the particular interface | ||
* from a full interface name (e.g. tun0 becomes "tun" and "0"). | ||
* Null if no info was found (id est, interface is not known) | ||
*/ | ||
private static final String IFS_REGEX = "(eth|wlan|tun|dummy|rmnet_data|rmnet_ipa)([0-9+])"; | ||
|
||
private String[] extractIfInfo(String ifName) { | ||
Pattern pt = Pattern.compile(IFS_REGEX, Pattern.CASE_INSENSITIVE); | ||
Matcher mc = pt.matcher(ifName); | ||
|
||
if (mc.matches()) { | ||
return new String[] { mc.group(1), mc.group(2) }; | ||
} | ||
|
||
// If not found, check for loopback | ||
if (ifName.equalsIgnoreCase("lo")) { | ||
return new String[] { "lo" }; | ||
} | ||
|
||
// No useful info was found | ||
return null; | ||
public void onNetworkStateChanged(NetworkInterfaceTester nit, NetworkInterface iface, boolean enabled) { | ||
this.data = nit.getAvailableInterfaces(); | ||
this.dataSize = this.data.size(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.