Display a warning if the active connection is metered. Find a proper MAC address for pairing (might require re-pair). Fix a possible infinite loop in the NvControl code.

This commit is contained in:
Cameron Gutman 2013-11-11 18:42:07 -05:00
parent 83ad55e436
commit 77786f9693
3 changed files with 71 additions and 21 deletions

View File

@ -9,7 +9,7 @@
android:targetSdkVersion="19" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.RECORD_AUDIO" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<application
android:largeHeap="true"

View File

@ -12,6 +12,8 @@ import java.util.concurrent.TimeUnit;
import org.xmlpull.v1.XmlPullParserException;
import android.content.Context;
import android.net.ConnectivityManager;
import android.view.Surface;
import android.widget.Toast;
@ -39,27 +41,49 @@ public class NvConnection {
}
public static String getMacAddressString() throws SocketException {
Enumeration<NetworkInterface> ifaceList = NetworkInterface.getNetworkInterfaces();
Enumeration<NetworkInterface> ifaceList;
NetworkInterface selectedIface = null;
while (ifaceList.hasMoreElements()) {
// First look for a WLAN interface (since those generally aren't removable)
ifaceList = NetworkInterface.getNetworkInterfaces();
while (selectedIface == null && ifaceList.hasMoreElements()) {
NetworkInterface iface = ifaceList.nextElement();
/* Look for the first non-loopback interface to use as the MAC address.
* We don't require the interface to be up to avoid having to repair when
* connecting over different interfaces */
if (!iface.isLoopback()) {
byte[] macAddress = iface.getHardwareAddress();
if (macAddress != null && macAddress.length == 6) {
StringBuilder addrStr = new StringBuilder();
for (int i = 0; i < macAddress.length; i++) {
addrStr.append(String.format("%02x", macAddress[i]));
if (i != macAddress.length - 1) {
addrStr.append(':');
}
}
return addrStr.toString();
if (iface.getName().startsWith("wlan")) {
selectedIface = iface;
}
}
// If we didn't find that, look for an Ethernet interface
ifaceList = NetworkInterface.getNetworkInterfaces();
while (selectedIface == null && ifaceList.hasMoreElements()) {
NetworkInterface iface = ifaceList.nextElement();
if (iface.getName().startsWith("eth")) {
selectedIface = iface;
}
}
// Now just find something
ifaceList = NetworkInterface.getNetworkInterfaces();
if (ifaceList.hasMoreElements()) {
selectedIface = ifaceList.nextElement();
}
if (selectedIface == null) {
return null;
}
byte[] macAddress = selectedIface.getHardwareAddress();
if (macAddress != null && macAddress.length == 6) {
StringBuilder addrStr = new StringBuilder();
for (int i = 0; i < macAddress.length; i++) {
addrStr.append(String.format("%02x", macAddress[i]));
if (i != macAddress.length - 1) {
addrStr.append(':');
}
}
return addrStr.toString();
}
return null;
@ -93,6 +117,8 @@ public class NvConnection {
new Thread(new Runnable() {
@Override
public void run() {
checkDataConnection();
try {
host = InetAddress.getByName(host).getHostAddress();
} catch (UnknownHostException e) {
@ -122,6 +148,14 @@ public class NvConnection {
}).start();
}
private void checkDataConnection()
{
ConnectivityManager mgr = (ConnectivityManager) activity.getSystemService(Context.CONNECTIVITY_SERVICE);
if (mgr.isActiveNetworkMetered()) {
displayToast("Warning: Your active network connection is metered!");
}
}
public void sendMouseMove(final short deltaX, final short deltaY)
{
if (inputStream == null)

View File

@ -327,9 +327,17 @@ public class NvControl {
int offset = 0;
do
{
offset = in.read(header, offset, header.length - offset);
int bytesRead = in.read(header, offset, header.length - offset);
if (bytesRead < 0) {
break;
}
offset += bytesRead;
} while (offset != header.length);
if (offset != header.length) {
throw new IOException("Socket closed prematurely");
}
ByteBuffer bb = ByteBuffer.wrap(header).order(ByteOrder.LITTLE_ENDIAN);
type = bb.getShort();
@ -342,8 +350,16 @@ public class NvControl {
offset = 0;
do
{
offset = in.read(payload, offset, payload.length - offset);
int bytesRead = in.read(payload, offset, payload.length - offset);
if (bytesRead < 0) {
break;
}
offset += bytesRead;
} while (offset != payload.length);
if (offset != payload.length) {
throw new IOException("Socket closed prematurely");
}
}
}