mirror of
https://github.com/KDE/kdeconnect-android
synced 2025-09-05 00:25:09 +00:00
Some refactoring on sendpackage to reduce allocations of empty callbacks
This commit is contained in:
@@ -582,42 +582,43 @@ public class Device implements BaseLink.PackageReceiver {
|
||||
}
|
||||
|
||||
public static abstract class SendPackageStatusCallback {
|
||||
protected abstract void onSuccess();
|
||||
protected abstract void onFailure(Throwable e);
|
||||
protected void onProgressChanged(int percent) { }
|
||||
public abstract void onSuccess();
|
||||
public abstract void onFailure(Throwable e);
|
||||
public void onProgressChanged(int percent) { }
|
||||
}
|
||||
|
||||
private boolean success = false;
|
||||
public void sendSuccess() {
|
||||
success = true;
|
||||
onSuccess();
|
||||
}
|
||||
public void sendFailure(Throwable e) {
|
||||
private SendPackageStatusCallback defaultCallback = new SendPackageStatusCallback() {
|
||||
@Override
|
||||
public void onSuccess() { }
|
||||
@Override
|
||||
public void onFailure(Throwable e) {
|
||||
if (e != null) {
|
||||
e.printStackTrace();
|
||||
Log.e("KDE/sendPackage", "Exception: " + e.getMessage());
|
||||
} else {
|
||||
Log.e("KDE/sendPackage", "Unknown (null) exception");
|
||||
}
|
||||
onFailure(e);
|
||||
}
|
||||
public void sendProgress(int percent) { onProgressChanged(percent); }
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
public void sendPackage(NetworkPackage np) {
|
||||
sendPackage(np,new SendPackageStatusCallback() {
|
||||
@Override
|
||||
protected void onSuccess() { }
|
||||
@Override
|
||||
protected void onFailure(Throwable e) { }
|
||||
});
|
||||
sendPackage(np, defaultCallback);
|
||||
}
|
||||
|
||||
public boolean sendPackageBlocking(NetworkPackage np) {
|
||||
return sendPackageBlocking(np, defaultCallback);
|
||||
}
|
||||
|
||||
//Async
|
||||
public void sendPackage(final NetworkPackage np, final SendPackageStatusCallback callback) {
|
||||
new Thread(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
sendPackageBlocking(np, callback);
|
||||
}
|
||||
}).start();
|
||||
}
|
||||
|
||||
hackToMakeRetrocompatiblePacketTypes(np);
|
||||
|
||||
public boolean sendPackageBlocking(final NetworkPackage np, final SendPackageStatusCallback callback) {
|
||||
|
||||
/*
|
||||
if (!m_outgoingCapabilities.contains(np.getType()) && !NetworkPackage.protocolPackageTypes.contains(np.getType())) {
|
||||
@@ -626,37 +627,29 @@ public class Device implements BaseLink.PackageReceiver {
|
||||
}
|
||||
*/
|
||||
|
||||
//Log.e("sendPackage", "Sending package...");
|
||||
//Log.e("sendPackage", np.serialize());
|
||||
hackToMakeRetrocompatiblePacketTypes(np);
|
||||
|
||||
final Throwable backtrace = new Throwable();
|
||||
new Thread(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
|
||||
boolean useEncryption = (protocolVersion < LanLinkProvider.MIN_VERSION_WITH_SSL_SUPPORT && (!np.getType().equals(NetworkPackage.PACKAGE_TYPE_PAIR) && isPaired()));
|
||||
|
||||
//Make a copy to avoid concurrent modification exception if the original list changes
|
||||
for (final BaseLink link : links) {
|
||||
if (link == null) continue; //Since we made a copy, maybe somebody destroyed the link in the meanwhile
|
||||
if (useEncryption) {
|
||||
link.sendPackageEncrypted(np, callback, publicKey);
|
||||
} else {
|
||||
link.sendPackage(np, callback);
|
||||
}
|
||||
if (callback.success) break; //If the link didn't call sendSuccess(), try the next one
|
||||
}
|
||||
|
||||
if (!callback.success) {
|
||||
Log.e("KDE/sendPackage", "No device link (of "+links.size()+" available) could send the package. Package "+np.getType()+" to " + name + " lost!");
|
||||
backtrace.printStackTrace();
|
||||
}
|
||||
boolean useEncryption = (protocolVersion < LanLinkProvider.MIN_VERSION_WITH_SSL_SUPPORT && (!np.getType().equals(NetworkPackage.PACKAGE_TYPE_PAIR) && isPaired()));
|
||||
|
||||
boolean success = false;
|
||||
//Make a copy to avoid concurrent modification exception if the original list changes
|
||||
for (final BaseLink link : links) {
|
||||
if (link == null) continue; //Since we made a copy, maybe somebody destroyed the link in the meanwhile
|
||||
if (useEncryption) {
|
||||
success = link.sendPackageEncrypted(np, callback, publicKey);
|
||||
} else {
|
||||
success = link.sendPackage(np, callback);
|
||||
}
|
||||
}).start();
|
||||
if (success) break; //If the link didn't call sendSuccess(), try the next one
|
||||
}
|
||||
|
||||
if (!success) {
|
||||
Log.e("KDE/sendPackage", "No device link (of "+links.size()+" available) could send the package. Package "+np.getType()+" to " + name + " lost!");
|
||||
}
|
||||
|
||||
return success;
|
||||
|
||||
}
|
||||
|
||||
//
|
||||
// Plugin-related functions
|
||||
//
|
||||
|
Reference in New Issue
Block a user