Search in sources :

Example 11 with NetworkPackage

use of org.kde.kdeconnect.NetworkPackage in project kdeconnect-android by KDE.

the class MprisPlugin method requestPlayerList.

private void requestPlayerList() {
    NetworkPackage np = new NetworkPackage(PACKAGE_TYPE_MPRIS_REQUEST);
    np.set("requestPlayerList", true);
    device.sendPackage(np);
}
Also used : NetworkPackage(org.kde.kdeconnect.NetworkPackage)

Example 12 with NetworkPackage

use of org.kde.kdeconnect.NetworkPackage in project kdeconnect-android by KDE.

the class MousePadPlugin method sendSingleHold.

public void sendSingleHold() {
    NetworkPackage np = new NetworkPackage(PACKAGE_TYPE_MOUSEPAD_REQUEST);
    np.set("singlehold", true);
    device.sendPackage(np);
}
Also used : NetworkPackage(org.kde.kdeconnect.NetworkPackage)

Example 13 with NetworkPackage

use of org.kde.kdeconnect.NetworkPackage in project kdeconnect-android by KDE.

the class MousePadPlugin method sendDoubleClick.

public void sendDoubleClick() {
    NetworkPackage np = new NetworkPackage(PACKAGE_TYPE_MOUSEPAD_REQUEST);
    np.set("doubleclick", true);
    device.sendPackage(np);
}
Also used : NetworkPackage(org.kde.kdeconnect.NetworkPackage)

Example 14 with NetworkPackage

use of org.kde.kdeconnect.NetworkPackage in project kdeconnect-android by KDE.

the class LanLinkProvider method udpPacketReceived.

//I've received their broadcast and should connect to their TCP socket and send my identity.
void udpPacketReceived(DatagramPacket packet) throws Exception {
    final InetAddress address = packet.getAddress();
    try {
        String message = new String(packet.getData(), StringsHelper.UTF8);
        final NetworkPackage identityPackage = NetworkPackage.unserialize(message);
        final String deviceId = identityPackage.getString("deviceId");
        if (!identityPackage.getType().equals(NetworkPackage.PACKAGE_TYPE_IDENTITY)) {
            Log.e("KDE/LanLinkProvider", "Expecting an UDP identity package");
            return;
        } else {
            String myId = DeviceHelper.getDeviceId(context);
            if (deviceId.equals(myId)) {
                //Ignore my own broadcast
                return;
            }
        }
        if (identityPackage.getInt("protocolVersion") >= MIN_VERSION_WITH_NEW_PORT_SUPPORT && identityPackage.getInt("tcpPort") < MIN_PORT) {
            Log.w("KDE/LanLinkProvider", "Ignoring a udp broadcast from legacy port because it comes from a device which knows about the new port.");
            return;
        }
        Log.i("KDE/LanLinkProvider", "Broadcast identity package received from " + identityPackage.getString("deviceName"));
        int tcpPort = identityPackage.getInt("tcpPort", MIN_PORT);
        SocketFactory socketFactory = SocketFactory.getDefault();
        Socket socket = socketFactory.createSocket(address, tcpPort);
        configureSocket(socket);
        OutputStream out = socket.getOutputStream();
        NetworkPackage myIdentity = NetworkPackage.createIdentityPackage(context);
        out.write(myIdentity.serialize().getBytes());
        out.flush();
        identityPackageReceived(identityPackage, socket, LanLink.ConnectionStarted.Remotely);
    } catch (Exception e) {
        Log.e("KDE/LanLinkProvider", "Cannot connect to " + address);
        e.printStackTrace();
        if (!reverseConnectionBlackList.contains(address)) {
            Log.w("KDE/LanLinkProvider", "Blacklisting " + address);
            reverseConnectionBlackList.add(address);
            new Timer().schedule(new TimerTask() {

                @Override
                public void run() {
                    reverseConnectionBlackList.remove(address);
                }
            }, 5 * 1000);
            // Try to cause a reverse connection
            onNetworkChange();
        }
    }
}
Also used : Timer(java.util.Timer) TimerTask(java.util.TimerTask) SocketFactory(javax.net.SocketFactory) OutputStream(java.io.OutputStream) NetworkPackage(org.kde.kdeconnect.NetworkPackage) InetAddress(java.net.InetAddress) Socket(java.net.Socket) SSLSocket(javax.net.ssl.SSLSocket) DatagramSocket(java.net.DatagramSocket) ServerSocket(java.net.ServerSocket) SocketException(java.net.SocketException) IOException(java.io.IOException)

Example 15 with NetworkPackage

use of org.kde.kdeconnect.NetworkPackage in project kdeconnect-android by KDE.

the class LanLink method reset.

//Returns the old socket
public Socket reset(final Socket newSocket, ConnectionStarted connectionSource) throws IOException {
    Socket 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(new Runnable() {

        @Override
        public void run() {
            try {
                BufferedReader reader = new BufferedReader(new InputStreamReader(newSocket.getInputStream(), StringsHelper.UTF8));
                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;
                    }
                    NetworkPackage np = NetworkPackage.unserialize(packet);
                    receivedNetworkPackage(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) {
                    callback.linkDisconnected(LanLink.this);
                }
            }
        }
    }).start();
    return oldSocket;
}
Also used : SocketTimeoutException(java.net.SocketTimeoutException) InputStreamReader(java.io.InputStreamReader) BufferedReader(java.io.BufferedReader) NetworkPackage(org.kde.kdeconnect.NetworkPackage) IOException(java.io.IOException) Socket(java.net.Socket) SSLSocket(javax.net.ssl.SSLSocket) ServerSocket(java.net.ServerSocket) IOException(java.io.IOException) NotYetConnectedException(java.nio.channels.NotYetConnectedException) SocketTimeoutException(java.net.SocketTimeoutException)

Aggregations

NetworkPackage (org.kde.kdeconnect.NetworkPackage)41 IOException (java.io.IOException)4 ArrayList (java.util.ArrayList)4 SocketException (java.net.SocketException)3 Uri (android.net.Uri)2 BufferedReader (java.io.BufferedReader)2 File (java.io.File)2 InputStreamReader (java.io.InputStreamReader)2 DatagramSocket (java.net.DatagramSocket)2 InetAddress (java.net.InetAddress)2 ServerSocket (java.net.ServerSocket)2 Socket (java.net.Socket)2 Timer (java.util.Timer)2 TimerTask (java.util.TimerTask)2 Cipher (javax.crypto.Cipher)2 SSLSocket (javax.net.ssl.SSLSocket)2 JSONArray (org.json.JSONArray)2 Notification (android.app.Notification)1 ContentResolver (android.content.ContentResolver)1 Intent (android.content.Intent)1