use of org.kde.kdeconnect.NetworkPacket in project kdeconnect-android by KDE.
the class BluetoothPairingHandler method rejectPairing.
@Override
public void rejectPairing() {
hidePairingNotification();
mPairStatus = PairStatus.NotPaired;
NetworkPacket np = new NetworkPacket(NetworkPacket.PACKET_TYPE_PAIR);
np.set("pair", false);
mDevice.sendPacket(np);
}
use of org.kde.kdeconnect.NetworkPacket in project kdeconnect-android by KDE.
the class BluetoothPairingHandler method unpair.
@Override
public void unpair() {
mPairStatus = PairStatus.NotPaired;
NetworkPacket np = new NetworkPacket(NetworkPacket.PACKET_TYPE_PAIR);
np.set("pair", false);
mDevice.sendPacket(np);
}
use of org.kde.kdeconnect.NetworkPacket in project kdeconnect-android by KDE.
the class BluetoothPairingHandler method createPairPacket.
// @Override
private NetworkPacket createPairPacket() {
NetworkPacket np = new NetworkPacket(NetworkPacket.PACKET_TYPE_PAIR);
np.set("pair", true);
return np;
}
use of org.kde.kdeconnect.NetworkPacket in project kdeconnect-android by KDE.
the class LanLink method reset.
// Returns the old socket
public SSLSocket reset(final SSLSocket newSocket, ConnectionStarted connectionSource) throws IOException {
SSLSocket oldSocket = socket;
socket = newSocket;
this.connectionSource = connectionSource;
if (oldSocket != null) {
// This should cancel the readThread
oldSocket.close();
}
// Log.e("LanLink", "Start listening");
// Create a thread to take care of incoming data for the new socket
new Thread(() -> {
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(newSocket.getInputStream(), Charsets.UTF_8));
while (true) {
String packet;
try {
packet = reader.readLine();
} catch (SocketTimeoutException e) {
continue;
}
if (packet == null) {
throw new IOException("End of stream");
}
if (packet.isEmpty()) {
continue;
}
NetworkPacket np = NetworkPacket.unserialize(packet);
receivedNetworkPacket(np);
}
} catch (Exception e) {
Log.i("LanLink", "Socket closed: " + newSocket.hashCode() + ". Reason: " + e.getMessage());
// Wait a bit because we might receive a new socket meanwhile
try {
Thread.sleep(300);
} catch (InterruptedException ignored) {
}
boolean thereIsaANewSocket = (newSocket != socket);
if (!thereIsaANewSocket) {
Log.i("LanLink", "Socket closed and there's no new socket, disconnecting device");
callback.linkDisconnected(LanLink.this);
}
}
}).start();
return oldSocket;
}
use of org.kde.kdeconnect.NetworkPacket in project kdeconnect-android by KDE.
the class FilesHelper method uriToNetworkPacket.
// Create the network package from the URI
public static NetworkPacket uriToNetworkPacket(final Context context, final Uri uri, String type) {
try {
ContentResolver cr = context.getContentResolver();
InputStream inputStream = cr.openInputStream(uri);
NetworkPacket np = new NetworkPacket(type);
String filename = null;
long size = -1;
Long lastModified = null;
if (uri.getScheme().equals("file")) {
try {
File mFile = new File(uri.getPath());
filename = mFile.getName();
size = mFile.length();
lastModified = mFile.lastModified();
} catch (NullPointerException e) {
Log.e(LOG_TAG, "Received bad file URI", e);
}
} else {
// Since we used Intent.CATEGORY_OPENABLE, these two columns are the only ones we are
// guaranteed to have: https://developer.android.com/reference/android/provider/OpenableColumns
String[] proj = { OpenableColumns.SIZE, OpenableColumns.DISPLAY_NAME };
try (Cursor cursor = cr.query(uri, proj, null, null, null)) {
int nameColumnIndex = cursor.getColumnIndexOrThrow(MediaStore.MediaColumns.DISPLAY_NAME);
int sizeColumnIndex = cursor.getColumnIndexOrThrow(MediaStore.MediaColumns.SIZE);
cursor.moveToFirst();
filename = cursor.getString(nameColumnIndex);
// not local to the device)
if (!cursor.isNull(sizeColumnIndex)) {
size = cursor.getLong(sizeColumnIndex);
}
lastModified = getLastModifiedTime(context, uri);
} catch (Exception e) {
Log.e(LOG_TAG, "Problem getting file information", e);
}
}
if (filename != null) {
np.set("filename", filename);
} else {
// It would be very surprising if this happens
Log.e(LOG_TAG, "Unable to read filename");
}
if (lastModified != null) {
np.set("lastModified", lastModified);
} else {
// This would not be too surprising, and probably means we need to improve
// FilesHelper.getLastModifiedTime
Log.w(LOG_TAG, "Unable to read file last modified time");
}
np.setPayload(new NetworkPacket.Payload(inputStream, size));
return np;
} catch (Exception e) {
Log.e(LOG_TAG, "Exception creating network packet", e);
return null;
}
}
Aggregations