mirror of
https://github.com/moonlight-stream/moonlight-android.git
synced 2025-07-19 19:13:03 +00:00
Improve error reporting for incorrect IP address
This commit is contained in:
parent
6557cba307
commit
3c2fd32d1e
@ -1,5 +1,12 @@
|
|||||||
package com.limelight.preferences;
|
package com.limelight.preferences;
|
||||||
|
|
||||||
|
import java.net.Inet4Address;
|
||||||
|
import java.net.InetAddress;
|
||||||
|
import java.net.InterfaceAddress;
|
||||||
|
import java.net.NetworkInterface;
|
||||||
|
import java.net.SocketException;
|
||||||
|
import java.net.UnknownHostException;
|
||||||
|
import java.util.Collections;
|
||||||
import java.util.concurrent.LinkedBlockingQueue;
|
import java.util.concurrent.LinkedBlockingQueue;
|
||||||
|
|
||||||
import com.limelight.computers.ComputerManagerService;
|
import com.limelight.computers.ComputerManagerService;
|
||||||
@ -39,36 +46,84 @@ public class AddComputerManually extends Activity {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
private boolean isWrongSubnetSiteLocalAddress(String address) {
|
||||||
|
try {
|
||||||
|
InetAddress targetAddress = InetAddress.getByName(address);
|
||||||
|
if (!(targetAddress instanceof Inet4Address) || !targetAddress.isSiteLocalAddress()) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
// We have a site-local address. Look for a matching local interface.
|
||||||
|
for (NetworkInterface iface : Collections.list(NetworkInterface.getNetworkInterfaces())) {
|
||||||
|
for (InterfaceAddress addr : iface.getInterfaceAddresses()) {
|
||||||
|
if (!(addr.getAddress() instanceof Inet4Address) || !addr.getAddress().isSiteLocalAddress()) {
|
||||||
|
// Skip non-site-local or non-IPv4 addresses
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
byte[] targetAddrBytes = targetAddress.getAddress();
|
||||||
|
byte[] ifaceAddrBytes = addr.getAddress().getAddress();
|
||||||
|
|
||||||
|
// Compare prefix to ensure it's the same
|
||||||
|
boolean addressMatches = true;
|
||||||
|
for (int i = 0; i < addr.getNetworkPrefixLength(); i++) {
|
||||||
|
if ((ifaceAddrBytes[i / 8] & (1 << (i % 8))) != (targetAddrBytes[i / 8] & (1 << (i % 8)))) {
|
||||||
|
addressMatches = false;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (addressMatches) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Couldn't find a matching interface
|
||||||
|
return true;
|
||||||
|
} catch (SocketException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
return false;
|
||||||
|
} catch (UnknownHostException e) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private void doAddPc(String host) {
|
private void doAddPc(String host) {
|
||||||
String msg;
|
String msg;
|
||||||
boolean finish = false;
|
boolean wrongSiteLocal = false;
|
||||||
|
boolean success;
|
||||||
|
|
||||||
SpinnerDialog dialog = SpinnerDialog.displayDialog(this, getResources().getString(R.string.title_add_pc),
|
SpinnerDialog dialog = SpinnerDialog.displayDialog(this, getResources().getString(R.string.title_add_pc),
|
||||||
getResources().getString(R.string.msg_add_pc), false);
|
getResources().getString(R.string.msg_add_pc), false);
|
||||||
|
|
||||||
if (!managerBinder.addComputerBlocking(host, true)){
|
success = managerBinder.addComputerBlocking(host, true);
|
||||||
msg = getResources().getString(R.string.addpc_fail);
|
if (!success){
|
||||||
}
|
wrongSiteLocal = isWrongSubnetSiteLocalAddress(host);
|
||||||
else {
|
|
||||||
msg = getResources().getString(R.string.addpc_success);
|
|
||||||
finish = true;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
dialog.dismiss();
|
dialog.dismiss();
|
||||||
|
|
||||||
final boolean toastFinish = finish;
|
if (wrongSiteLocal) {
|
||||||
final String toastMsg = msg;
|
Dialog.displayDialog(this, getResources().getString(R.string.conn_error_title), getResources().getString(R.string.addpc_wrong_sitelocal), false);
|
||||||
AddComputerManually.this.runOnUiThread(new Runnable() {
|
}
|
||||||
@Override
|
else if (!success) {
|
||||||
public void run() {
|
Dialog.displayDialog(this, getResources().getString(R.string.conn_error_title), getResources().getString(R.string.addpc_fail), false);
|
||||||
Toast.makeText(AddComputerManually.this, toastMsg, Toast.LENGTH_LONG).show();
|
}
|
||||||
|
else {
|
||||||
|
AddComputerManually.this.runOnUiThread(new Runnable() {
|
||||||
|
@Override
|
||||||
|
public void run() {
|
||||||
|
Toast.makeText(AddComputerManually.this, getResources().getString(R.string.addpc_success), Toast.LENGTH_LONG).show();
|
||||||
|
|
||||||
if (toastFinish && !isFinishing()) {
|
if (!isFinishing()) {
|
||||||
// Close the activity
|
// Close the activity
|
||||||
AddComputerManually.this.finish();
|
AddComputerManually.this.finish();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private void startAddThread() {
|
private void startAddThread() {
|
||||||
|
@ -98,6 +98,7 @@
|
|||||||
<string name="addpc_success">Successfully added computer</string>
|
<string name="addpc_success">Successfully added computer</string>
|
||||||
<string name="addpc_unknown_host">Unable to resolve PC address. Make sure you didn\'t make a typo in the address.</string>
|
<string name="addpc_unknown_host">Unable to resolve PC address. Make sure you didn\'t make a typo in the address.</string>
|
||||||
<string name="addpc_enter_ip">You must enter an IP address</string>
|
<string name="addpc_enter_ip">You must enter an IP address</string>
|
||||||
|
<string name="addpc_wrong_sitelocal">That address doesn\'t look right. You must your router\'s public IP address for streaming over the Internet.</string>
|
||||||
|
|
||||||
<!-- Preferences -->
|
<!-- Preferences -->
|
||||||
<string name="category_basic_settings">Basic Settings</string>
|
<string name="category_basic_settings">Basic Settings</string>
|
||||||
|
Loading…
x
Reference in New Issue
Block a user