mirror of
https://github.com/KDE/kdeconnect-android
synced 2025-08-30 13:47:41 +00:00
Fixed bugs with file transfers
Finding open ports was not working (socked became closed after first try) AsyncTask on Device's sendPackage was blocking execution when stuck sending
This commit is contained in:
@@ -1,10 +1,11 @@
|
|||||||
<?xml version="1.0" encoding="UTF-8"?>
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
<module external.linked.project.path="$MODULE_DIR$" external.system.id="GRADLE" type="JAVA_MODULE" version="4">
|
<module external.linked.project.path="$MODULE_DIR$" external.root.project.path="$MODULE_DIR$/.." external.system.id="GRADLE" type="JAVA_MODULE" version="4">
|
||||||
<component name="FacetManager">
|
<component name="FacetManager">
|
||||||
<facet type="android" name="Android">
|
<facet type="android" name="Android">
|
||||||
<configuration>
|
<configuration>
|
||||||
<option name="SELECTED_BUILD_VARIANT" value="Debug" />
|
<option name="SELECTED_BUILD_VARIANT" value="Debug" />
|
||||||
<option name="ASSEMBLE_TASK_NAME" value="assembleDebug" />
|
<option name="ASSEMBLE_TASK_NAME" value="assembleDebug" />
|
||||||
|
<option name="COMPILE_JAVA_TASK_NAME" value="compileDebug" />
|
||||||
<option name="ASSEMBLE_TEST_TASK_NAME" value="assembleTest" />
|
<option name="ASSEMBLE_TEST_TASK_NAME" value="assembleTest" />
|
||||||
<option name="SOURCE_GEN_TASK_NAME" value="generateDebugSources" />
|
<option name="SOURCE_GEN_TASK_NAME" value="generateDebugSources" />
|
||||||
<option name="ALLOW_USER_CONFIGURATION" value="false" />
|
<option name="ALLOW_USER_CONFIGURATION" value="false" />
|
||||||
@@ -60,7 +61,6 @@
|
|||||||
<excludeFolder url="file://$MODULE_DIR$/build/bundles" />
|
<excludeFolder url="file://$MODULE_DIR$/build/bundles" />
|
||||||
<excludeFolder url="file://$MODULE_DIR$/build/classes" />
|
<excludeFolder url="file://$MODULE_DIR$/build/classes" />
|
||||||
<excludeFolder url="file://$MODULE_DIR$/build/dependency-cache" />
|
<excludeFolder url="file://$MODULE_DIR$/build/dependency-cache" />
|
||||||
<excludeFolder url="file://$MODULE_DIR$/build/exploded-bundles" />
|
|
||||||
<excludeFolder url="file://$MODULE_DIR$/build/incremental" />
|
<excludeFolder url="file://$MODULE_DIR$/build/incremental" />
|
||||||
<excludeFolder url="file://$MODULE_DIR$/build/libs" />
|
<excludeFolder url="file://$MODULE_DIR$/build/libs" />
|
||||||
<excludeFolder url="file://$MODULE_DIR$/build/manifests" />
|
<excludeFolder url="file://$MODULE_DIR$/build/manifests" />
|
||||||
@@ -70,10 +70,10 @@
|
|||||||
<orderEntry type="jdk" jdkName="Android 4.3 Platform" jdkType="Android SDK" />
|
<orderEntry type="jdk" jdkName="Android 4.3 Platform" jdkType="Android SDK" />
|
||||||
<orderEntry type="sourceFolder" forTests="false" />
|
<orderEntry type="sourceFolder" forTests="false" />
|
||||||
<orderEntry type="library" exported="" name="android-support-v4" level="project" />
|
<orderEntry type="library" exported="" name="android-support-v4" level="project" />
|
||||||
<orderEntry type="library" exported="" name="mina-core-2.0.7" level="project" />
|
|
||||||
<orderEntry type="library" exported="" name="slf4j-api-1.6.6" level="project" />
|
|
||||||
<orderEntry type="library" exported="" name="support-v4-18.0.0" level="project" />
|
<orderEntry type="library" exported="" name="support-v4-18.0.0" level="project" />
|
||||||
<orderEntry type="library" exported="" name="ComAndroidSupportAppcompatV71800.aar" level="project" />
|
<orderEntry type="library" exported="" name="ComAndroidSupportAppcompatV71800.aar" level="project" />
|
||||||
|
<orderEntry type="library" exported="" name="slf4j-api-1.6.6" level="project" />
|
||||||
|
<orderEntry type="library" exported="" name="mina-core-2.0.7" level="project" />
|
||||||
</component>
|
</component>
|
||||||
</module>
|
</module>
|
||||||
|
|
||||||
|
@@ -50,40 +50,47 @@ public class LanLink extends BaseLink {
|
|||||||
|
|
||||||
try {
|
try {
|
||||||
|
|
||||||
final ServerSocket server = new ServerSocket();
|
ServerSocket candidateServer = null;
|
||||||
boolean success = false;
|
boolean success = false;
|
||||||
int tcpPort = 1764;
|
int tcpPort = 1739;
|
||||||
while(!success) {
|
while(!success) {
|
||||||
try {
|
try {
|
||||||
server.bind(new InetSocketAddress(tcpPort));
|
candidateServer = new ServerSocket();
|
||||||
|
candidateServer.bind(new InetSocketAddress(tcpPort));
|
||||||
success = true;
|
success = true;
|
||||||
} catch(Exception e) {
|
} catch(Exception e) {
|
||||||
|
Log.e("LanLink", "Exception openning serversocket: "+e);
|
||||||
tcpPort++;
|
tcpPort++;
|
||||||
|
if (tcpPort >= 1764) return new JSONObject();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
JSONObject payloadTransferInfo = new JSONObject();
|
JSONObject payloadTransferInfo = new JSONObject();
|
||||||
payloadTransferInfo.put("port", tcpPort);
|
payloadTransferInfo.put("port", tcpPort);
|
||||||
|
|
||||||
|
final ServerSocket server = candidateServer;
|
||||||
new Thread(new Runnable() {
|
new Thread(new Runnable() {
|
||||||
@Override
|
@Override
|
||||||
public void run() {
|
public void run() {
|
||||||
//TODO: Timeout when waiting for a connection and close the socket
|
//TODO: Timeout when waiting for a connection and close the socket
|
||||||
|
OutputStream socket = null;
|
||||||
try {
|
try {
|
||||||
OutputStream socket = server.accept().getOutputStream();
|
socket = server.accept().getOutputStream();
|
||||||
byte[] buffer = new byte[2048];
|
byte[] buffer = new byte[4096];
|
||||||
int bytesRead;
|
int bytesRead;
|
||||||
Log.e("LanLink","Beginning to send payload");
|
Log.e("LanLink","Beginning to send payload");
|
||||||
while ((bytesRead = stream.read(buffer)) != -1) {
|
while ((bytesRead = stream.read(buffer)) != -1) {
|
||||||
Log.e("ok",""+bytesRead);
|
//Log.e("ok",""+bytesRead);
|
||||||
socket.write(buffer, 0, bytesRead);
|
socket.write(buffer, 0, bytesRead);
|
||||||
}
|
}
|
||||||
socket.close();
|
|
||||||
server.close();
|
|
||||||
Log.e("LanLink","Finished sending payload");
|
Log.e("LanLink","Finished sending payload");
|
||||||
} catch(Exception e) {
|
} catch(Exception e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
Log.e("LanLink", "Exception with payload upload socket");
|
Log.e("LanLink", "Exception with payload upload socket");
|
||||||
|
} finally {
|
||||||
|
if (socket != null) {
|
||||||
|
try { socket.close(); } catch(Exception e) { }
|
||||||
|
}
|
||||||
|
try { server.close(); } catch(Exception e) { }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}).start();
|
}).start();
|
||||||
@@ -118,18 +125,23 @@ public class LanLink extends BaseLink {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean sendPackageEncrypted(NetworkPackage np, PublicKey key) {
|
public boolean sendPackageEncrypted(NetworkPackage np, PublicKey key) {
|
||||||
|
|
||||||
if (session == null) {
|
if (session == null) {
|
||||||
Log.e("LanLink","sendPackage failed: not yet connected");
|
Log.e("LanLink","sendPackage failed: not yet connected");
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
|
|
||||||
if (np.hasPayload()) {
|
if (np.hasPayload()) {
|
||||||
JSONObject transferInfo = sendPayload(np.getPayload());
|
JSONObject transferInfo = sendPayload(np.getPayload());
|
||||||
np.setPayloadTransferInfo(transferInfo);
|
np.setPayloadTransferInfo(transferInfo);
|
||||||
}
|
}
|
||||||
|
|
||||||
np.encrypt(key);
|
np.encrypt(key);
|
||||||
|
|
||||||
session.write(np.serialize());
|
session.write(np.serialize());
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
|
@@ -65,10 +65,14 @@ public class LanLinkProvider extends BaseLinkProvider {
|
|||||||
LanLink prevLink = nioSessions.get(session.getId());
|
LanLink prevLink = nioSessions.get(session.getId());
|
||||||
|
|
||||||
if (np.getType().equals(NetworkPackage.PACKAGE_TYPE_IDENTITY)) {
|
if (np.getType().equals(NetworkPackage.PACKAGE_TYPE_IDENTITY)) {
|
||||||
|
|
||||||
String myId = NetworkPackage.createIdentityPackage(context).getString("deviceId");
|
String myId = NetworkPackage.createIdentityPackage(context).getString("deviceId");
|
||||||
if (np.getString("deviceId").equals(myId)) {
|
if (np.getString("deviceId").equals(myId)) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//Log.e("LanLinkProvider", "Identity package received from "+np.getString("deviceName"));
|
||||||
|
|
||||||
LanLink link = new LanLink(session, np.getString("deviceId"), LanLinkProvider.this);
|
LanLink link = new LanLink(session, np.getString("deviceId"), LanLinkProvider.this);
|
||||||
nioSessions.put(session.getId(),link);
|
nioSessions.put(session.getId(),link);
|
||||||
addLink(np, link);
|
addLink(np, link);
|
||||||
@@ -90,33 +94,23 @@ public class LanLinkProvider extends BaseLinkProvider {
|
|||||||
|
|
||||||
//Log.e("LanLinkProvider", "Udp message received (" + message.getClass() + ") " + message.toString());
|
//Log.e("LanLinkProvider", "Udp message received (" + message.getClass() + ") " + message.toString());
|
||||||
|
|
||||||
NetworkPackage np = null;
|
|
||||||
|
|
||||||
try {
|
try {
|
||||||
//We should receive a string thanks to the TextLineCodecFactory filter
|
//We should receive a string thanks to the TextLineCodecFactory filter
|
||||||
String theMessage = (String) message;
|
String theMessage = (String) message;
|
||||||
np = NetworkPackage.unserialize(theMessage);
|
final NetworkPackage identityPackage = NetworkPackage.unserialize(theMessage);
|
||||||
} catch (Exception e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
Log.e("LanLinkProvider", "Could not unserialize package");
|
|
||||||
}
|
|
||||||
|
|
||||||
if (np != null) {
|
if (!identityPackage.getType().equals(NetworkPackage.PACKAGE_TYPE_IDENTITY)) {
|
||||||
|
|
||||||
final NetworkPackage identityPackage = np;
|
|
||||||
if (!np.getType().equals(NetworkPackage.PACKAGE_TYPE_IDENTITY)) {
|
|
||||||
Log.e("LanLinkProvider", "1 Expecting an identity package");
|
Log.e("LanLinkProvider", "1 Expecting an identity package");
|
||||||
return;
|
return;
|
||||||
} else {
|
} else {
|
||||||
String myId = NetworkPackage.createIdentityPackage(context).getString("deviceId");
|
String myId = NetworkPackage.createIdentityPackage(context).getString("deviceId");
|
||||||
if (np.getString("deviceId").equals(myId)) {
|
if (identityPackage.getString("deviceId").equals(myId)) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Log.i("LanLinkProvider", "Identity package received, creating link");
|
Log.i("LanLinkProvider", "Identity package received, creating link");
|
||||||
|
|
||||||
try {
|
|
||||||
final InetSocketAddress address = (InetSocketAddress) udpSession.getRemoteAddress();
|
final InetSocketAddress address = (InetSocketAddress) udpSession.getRemoteAddress();
|
||||||
|
|
||||||
final NioSocketConnector connector = new NioSocketConnector();
|
final NioSocketConnector connector = new NioSocketConnector();
|
||||||
@@ -129,7 +123,7 @@ public class LanLinkProvider extends BaseLinkProvider {
|
|||||||
);
|
);
|
||||||
connector.getSessionConfig().setKeepAlive(true);
|
connector.getSessionConfig().setKeepAlive(true);
|
||||||
|
|
||||||
int tcpPort = np.getInt("tcpPort",port);
|
int tcpPort = identityPackage.getInt("tcpPort",port);
|
||||||
ConnectFuture future = connector.connect(new InetSocketAddress(address.getAddress(), tcpPort));
|
ConnectFuture future = connector.connect(new InetSocketAddress(address.getAddress(), tcpPort));
|
||||||
future.addListener(new IoFutureListener<IoFuture>() {
|
future.addListener(new IoFutureListener<IoFuture>() {
|
||||||
@Override
|
@Override
|
||||||
@@ -149,12 +143,11 @@ public class LanLinkProvider extends BaseLinkProvider {
|
|||||||
});
|
});
|
||||||
|
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
Log.e("LanLinkProvider","Exception!!");
|
Log.e("LanLinkProvider","Exception receiving udp package!!");
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
|
||||||
};
|
};
|
||||||
|
|
||||||
private void addLink(NetworkPackage identityPackage, LanLink link) {
|
private void addLink(NetworkPackage identityPackage, LanLink link) {
|
||||||
|
@@ -419,30 +419,34 @@ public class Device implements BaseLink.PackageReceiver {
|
|||||||
|
|
||||||
public boolean sendPackage(final NetworkPackage np) {
|
public boolean sendPackage(final NetworkPackage np) {
|
||||||
|
|
||||||
new AsyncTask<Void,Void,Void>() {
|
new Thread(new Runnable() {
|
||||||
@Override
|
@Override
|
||||||
protected Void doInBackground(Void... voids) {
|
public void run() {
|
||||||
|
|
||||||
boolean useEncryption = (!np.getType().equals(NetworkPackage.PACKAGE_TYPE_PAIR) && isPaired());
|
boolean useEncryption = (!np.getType().equals(NetworkPackage.PACKAGE_TYPE_PAIR) && isPaired());
|
||||||
|
|
||||||
|
//Log.e("sendPackage", "Sending...");
|
||||||
|
|
||||||
for(BaseLink link : links) {
|
for(BaseLink link : links) {
|
||||||
|
|
||||||
if (useEncryption) {
|
if (useEncryption) {
|
||||||
if (link.sendPackageEncrypted(np, publicKey)) return null;
|
if (link.sendPackageEncrypted(np, publicKey)) {
|
||||||
|
//Log.e("sendPackage", "Sent");
|
||||||
|
return;
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
if (link.sendPackage(np)) return null;
|
if (link.sendPackage(np)) {
|
||||||
|
//Log.e("sendPackage", "Sent");
|
||||||
|
return;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Log.e("sendPackage","Error: Package could not be sent ("+links.size()+" links available)");
|
Log.e("sendPackage","Error: Package could not be sent ("+links.size()+" links available)");
|
||||||
|
|
||||||
return null;
|
|
||||||
|
|
||||||
}
|
}
|
||||||
}.execute();
|
}).start();
|
||||||
|
|
||||||
//TODO: Detect when unable to send a package and try again somehow
|
|
||||||
|
|
||||||
return !links.isEmpty();
|
return !links.isEmpty();
|
||||||
}
|
}
|
||||||
|
@@ -150,7 +150,7 @@ public class NetworkPackage {
|
|||||||
}
|
}
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
Log.e("NetworkPackage", "Unserialization exception");
|
Log.e("NetworkPackage", "Unserialization exception unserializing "+s);
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -1,5 +1,5 @@
|
|||||||
<?xml version="1.0" encoding="UTF-8"?>
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
<module external.linked.project.path="$MODULE_DIR$" external.system.id="GRADLE" type="JAVA_MODULE" version="4">
|
<module external.linked.project.path="$MODULE_DIR$" external.root.project.path="$MODULE_DIR$" external.system.id="GRADLE" type="JAVA_MODULE" version="4">
|
||||||
<component name="NewModuleRootManager" inherit-compiler-output="true">
|
<component name="NewModuleRootManager" inherit-compiler-output="true">
|
||||||
<exclude-output />
|
<exclude-output />
|
||||||
<content url="file://$MODULE_DIR$">
|
<content url="file://$MODULE_DIR$">
|
||||||
|
Reference in New Issue
Block a user