Search in sources :

Example 1 with NetworkPacket

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);
}
Also used : NetworkPacket(org.kde.kdeconnect.NetworkPacket)

Example 2 with NetworkPacket

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);
}
Also used : NetworkPacket(org.kde.kdeconnect.NetworkPacket)

Example 3 with NetworkPacket

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;
}
Also used : NetworkPacket(org.kde.kdeconnect.NetworkPacket)

Example 4 with NetworkPacket

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;
}
Also used : SocketTimeoutException(java.net.SocketTimeoutException) InputStreamReader(java.io.InputStreamReader) SSLSocket(javax.net.ssl.SSLSocket) BufferedReader(java.io.BufferedReader) NetworkPacket(org.kde.kdeconnect.NetworkPacket) IOException(java.io.IOException) SSLHandshakeException(javax.net.ssl.SSLHandshakeException) IOException(java.io.IOException) NotYetConnectedException(java.nio.channels.NotYetConnectedException) SocketTimeoutException(java.net.SocketTimeoutException) WorkerThread(androidx.annotation.WorkerThread)

Example 5 with NetworkPacket

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;
    }
}
Also used : InputStream(java.io.InputStream) NetworkPacket(org.kde.kdeconnect.NetworkPacket) Cursor(android.database.Cursor) SuppressLint(android.annotation.SuppressLint) ContentResolver(android.content.ContentResolver) File(java.io.File)

Aggregations

NetworkPacket (org.kde.kdeconnect.NetworkPacket)77 IOException (java.io.IOException)6 SSLSocket (javax.net.ssl.SSLSocket)4 SMSHelper (org.kde.kdeconnect.Helpers.SMSHelper)4 BufferedReader (java.io.BufferedReader)3 InputStreamReader (java.io.InputStreamReader)3 ServerSocket (java.net.ServerSocket)3 Socket (java.net.Socket)3 SocketException (java.net.SocketException)3 ArrayList (java.util.ArrayList)3 Timer (java.util.Timer)3 SuppressLint (android.annotation.SuppressLint)2 Uri (android.net.Uri)2 SmsMessage (android.telephony.SmsMessage)2 SpannableString (android.text.SpannableString)2 InputStream (java.io.InputStream)2 OutputStream (java.io.OutputStream)2 DatagramSocket (java.net.DatagramSocket)2 InetAddress (java.net.InetAddress)2 InetSocketAddress (java.net.InetSocketAddress)2