Search in sources :

Example 61 with NetworkPacket

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

the class ContactsPlugin method handleRequestVCardsByUIDs.

private boolean handleRequestVCardsByUIDs(NetworkPacket np) {
    if (!np.has("uids")) {
        Log.e("ContactsPlugin", "handleRequestNamesByUIDs received a malformed packet with no uids key");
        return false;
    }
    List<String> uIDsAsStrings = np.getStringList("uids");
    // Convert to Collection<uIDs> to call getVCardsForContactIDs
    Set<uID> uIDs = new HashSet<>(uIDsAsStrings.size());
    for (String uID : uIDsAsStrings) {
        uIDs.add(new uID(uID));
    }
    Map<uID, VCardBuilder> uIDsToVCards = ContactsHelper.getVCardsForContactIDs(context, uIDs);
    // ContactsHelper.getVCardsForContactIDs(..) is allowed to reply without
    // some of the requested uIDs if they were not in the database, so update our list
    uIDsAsStrings = new ArrayList<>(uIDsToVCards.size());
    NetworkPacket reply = new NetworkPacket(PACKET_TYPE_CONTACTS_RESPONSE_VCARDS);
    // Add the vcards to the packet
    for (uID uID : uIDsToVCards.keySet()) {
        VCardBuilder vcard = uIDsToVCards.get(uID);
        try {
            vcard = this.addVCardMetadata(vcard, uID);
            // Store this as a valid uID
            uIDsAsStrings.add(uID.toString());
            // Add the uid -> vcard pairing to the packet
            reply.set(uID.toString(), vcard.toString());
        } catch (ContactsHelper.ContactNotFoundException e) {
            e.printStackTrace();
        }
    }
    // Add the valid uIDs to the packet
    reply.set("uids", uIDsAsStrings);
    device.sendPacket(reply);
    return true;
}
Also used : ContactsHelper.uID(org.kde.kdeconnect.Helpers.ContactsHelper.uID) ContactsHelper(org.kde.kdeconnect.Helpers.ContactsHelper) VCardBuilder(org.kde.kdeconnect.Helpers.ContactsHelper.VCardBuilder) ContactNotFoundException(org.kde.kdeconnect.Helpers.ContactsHelper.ContactNotFoundException) NetworkPacket(org.kde.kdeconnect.NetworkPacket) HashSet(java.util.HashSet)

Example 62 with NetworkPacket

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

the class ContactsPlugin method handleRequestAllUIDsTimestamps.

/**
 * Return a unique identifier (Contacts.LOOKUP_KEY) for all contacts in the Contacts database
 * <p>
 * The identifiers returned can be used in future requests to get more information
 * about the contact
 *
 * @param np The package containing the request
 * @return true if successfully handled, false otherwise
 */
@SuppressWarnings("SameReturnValue")
private boolean handleRequestAllUIDsTimestamps(@SuppressWarnings("unused") NetworkPacket np) {
    NetworkPacket reply = new NetworkPacket(PACKET_TYPE_CONTACTS_RESPONSE_UIDS_TIMESTAMPS);
    Map<uID, Long> uIDsToTimestamps = ContactsHelper.getAllContactTimestamps(context);
    int contactCount = uIDsToTimestamps.size();
    List<String> uIDs = new ArrayList<>(contactCount);
    for (uID contactID : uIDsToTimestamps.keySet()) {
        Long timestamp = uIDsToTimestamps.get(contactID);
        reply.set(contactID.toString(), timestamp);
        uIDs.add(contactID.toString());
    }
    reply.set("uids", uIDs);
    device.sendPacket(reply);
    return true;
}
Also used : ContactsHelper.uID(org.kde.kdeconnect.Helpers.ContactsHelper.uID) ArrayList(java.util.ArrayList) NetworkPacket(org.kde.kdeconnect.NetworkPacket)

Example 63 with NetworkPacket

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

the class ComposeSendActivity method sendChars.

public void sendChars(CharSequence chars) {
    final NetworkPacket np = new NetworkPacket(MousePadPlugin.PACKET_TYPE_MOUSEPAD_REQUEST);
    np.set("key", chars.toString());
    sendKeyPressPacket(np);
}
Also used : NetworkPacket(org.kde.kdeconnect.NetworkPacket)

Example 64 with NetworkPacket

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

the class SharePlugin method sendUriList.

void sendUriList(final ArrayList<Uri> uriList) {
    CompositeUploadFileJob job;
    if (uploadFileJob == null) {
        job = new CompositeUploadFileJob(device, this.receiveFileJobCallback);
    } else {
        job = uploadFileJob;
    }
    // Read all the data early, as we only have permissions to do it while the activity is alive
    for (Uri uri : uriList) {
        NetworkPacket np = FilesHelper.uriToNetworkPacket(context, uri, PACKET_TYPE_SHARE_REQUEST);
        if (np != null) {
            job.addNetworkPacket(np);
        }
    }
    if (job != uploadFileJob) {
        uploadFileJob = job;
        backgroundJobHandler.runJob(uploadFileJob);
    }
}
Also used : NetworkPacket(org.kde.kdeconnect.NetworkPacket) Uri(android.net.Uri)

Example 65 with NetworkPacket

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

the class SharePlugin method share.

public void share(Intent intent) {
    Bundle extras = intent.getExtras();
    if (extras != null) {
        if (extras.containsKey(Intent.EXTRA_STREAM)) {
            try {
                ArrayList<Uri> uriList;
                if (!Intent.ACTION_SEND.equals(intent.getAction())) {
                    uriList = intent.getParcelableArrayListExtra(Intent.EXTRA_STREAM);
                } else {
                    Uri uri = extras.getParcelable(Intent.EXTRA_STREAM);
                    uriList = new ArrayList<>();
                    uriList.add(uri);
                }
                sendUriList(uriList);
            } catch (Exception e) {
                Log.e("ShareActivity", "Exception");
                e.printStackTrace();
            }
        } else if (extras.containsKey(Intent.EXTRA_TEXT)) {
            String text = extras.getString(Intent.EXTRA_TEXT);
            String subject = extras.getString(Intent.EXTRA_SUBJECT);
            // Hack: Detect shared youtube videos, so we can open them in the browser instead of as text
            if (StringUtils.endsWith(subject, "YouTube")) {
                int index = text.indexOf(": http://youtu.be/");
                if (index > 0) {
                    // Skip ": "
                    text = text.substring(index + 2);
                }
            }
            boolean isUrl;
            try {
                new URL(text);
                isUrl = true;
            } catch (Exception e) {
                isUrl = false;
            }
            NetworkPacket np = new NetworkPacket(SharePlugin.PACKET_TYPE_SHARE_REQUEST);
            if (isUrl) {
                np.set("url", text);
            } else {
                np.set("text", text);
            }
            device.sendPacket(np);
        }
    }
}
Also used : Bundle(android.os.Bundle) NetworkPacket(org.kde.kdeconnect.NetworkPacket) Uri(android.net.Uri) URL(java.net.URL)

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