Search in sources :

Example 26 with NetworkPacket

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

the class MprisReceiverPlugin method sendPlayerList.

private void sendPlayerList() {
    NetworkPacket np = new NetworkPacket(PACKET_TYPE_MPRIS);
    np.set("playerList", players.keySet());
    device.sendPacket(np);
}
Also used : NetworkPacket(org.kde.kdeconnect.NetworkPacket)

Example 27 with NetworkPacket

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

the class SMSPlugin method onPacketReceived.

@Override
public boolean onPacketReceived(NetworkPacket np) {
    long subID;
    switch(np.getType()) {
        case PACKET_TYPE_SMS_REQUEST_CONVERSATIONS:
            return this.handleRequestAllConversations(np);
        case PACKET_TYPE_SMS_REQUEST_CONVERSATION:
            return this.handleRequestSingleConversation(np);
        case PACKET_TYPE_SMS_REQUEST:
            String textMessage = np.getString("messageBody");
            subID = np.getLong("subID", -1);
            List<SMSHelper.Address> addressList = SMSHelper.jsonArrayToAddressList(np.getJSONArray("addresses"));
            if (addressList == null) {
                // If the List of Address is null, then the SMS_REQUEST packet is
                // most probably from the older version of the desktop app.
                addressList = new ArrayList<>();
                addressList.add(new SMSHelper.Address(np.getString("phoneNumber")));
            }
            List<SMSHelper.Attachment> attachedFiles = SMSHelper.jsonArrayToAttachmentsList(np.getJSONArray("attachments"));
            SmsMmsUtils.sendMessage(context, textMessage, attachedFiles, addressList, (int) subID);
            break;
        case TelephonyPlugin.PACKET_TYPE_TELEPHONY_REQUEST:
            if (np.getBoolean("sendSms")) {
                String phoneNo = np.getString("phoneNumber");
                String sms = np.getString("messageBody");
                subID = np.getLong("subID", -1);
                try {
                    SmsManager smsManager = subID == -1 ? SmsManager.getDefault() : SmsManager.getSmsManagerForSubscriptionId((int) subID);
                    ArrayList<String> parts = smsManager.divideMessage(sms);
                    // If this message turns out to fit in a single SMS, sendMultipartTextMessage
                    // properly handles that case
                    smsManager.sendMultipartTextMessage(phoneNo, null, parts, null, null);
                // TODO: Notify other end
                } catch (Exception e) {
                    // TODO: Notify other end
                    Log.e("SMSPlugin", "Exception", e);
                }
            }
            break;
        case PACKET_TYPE_SMS_REQUEST_ATTACHMENT:
            long partID = np.getLong("part_id");
            String uniqueIdentifier = np.getString("unique_identifier");
            NetworkPacket networkPacket = SmsMmsUtils.partIdToMessageAttachmentPacket(context, partID, uniqueIdentifier, PACKET_TYPE_SMS_ATTACHMENT_FILE);
            if (networkPacket != null) {
                device.sendPacket(networkPacket);
            }
            break;
    }
    return true;
}
Also used : SMSHelper(org.kde.kdeconnect.Helpers.SMSHelper) NetworkPacket(org.kde.kdeconnect.NetworkPacket) JSONException(org.json.JSONException) SmsManager(android.telephony.SmsManager)

Example 28 with NetworkPacket

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

the class SMSPlugin method constructBulkMessagePacket.

/**
 * Construct a proper packet of PACKET_TYPE_SMS_MESSAGE from the passed messages
 *
 * @param messages Messages to include in the packet
 * @return NetworkPacket of type PACKET_TYPE_SMS_MESSAGE
 */
private static NetworkPacket constructBulkMessagePacket(Collection<SMSHelper.Message> messages) {
    NetworkPacket reply = new NetworkPacket(PACKET_TYPE_SMS_MESSAGE);
    JSONArray body = new JSONArray();
    for (SMSHelper.Message message : messages) {
        try {
            JSONObject json = message.toJSONObject();
            body.put(json);
        } catch (JSONException e) {
            Log.e("Conversations", "Error serializing message", e);
        }
    }
    reply.set("messages", body);
    reply.set("version", SMS_MESSAGE_PACKET_VERSION);
    return reply;
}
Also used : JSONObject(org.json.JSONObject) JSONArray(org.json.JSONArray) SMSHelper(org.kde.kdeconnect.Helpers.SMSHelper) JSONException(org.json.JSONException) NetworkPacket(org.kde.kdeconnect.NetworkPacket)

Example 29 with NetworkPacket

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

the class SendKeystrokesToHostActivity method sendKeys.

private void sendKeys(Device deviceId) {
    String toSend;
    if (binding.textToSend.getText() != null && (toSend = binding.textToSend.getText().toString().trim()).length() > 0) {
        final NetworkPacket np = new NetworkPacket(MousePadPlugin.PACKET_TYPE_MOUSEPAD_REQUEST);
        np.set("key", toSend);
        BackgroundService.RunWithPlugin(this, deviceId.getDeviceId(), MousePadPlugin.class, plugin -> plugin.sendKeyboardPacket(np));
        Toast.makeText(getApplicationContext(), getString(R.string.sendkeystrokes_sent_text, toSend, deviceId.getName()), Toast.LENGTH_SHORT).show();
    }
}
Also used : NetworkPacket(org.kde.kdeconnect.NetworkPacket)

Example 30 with NetworkPacket

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

the class PresenterPlugin method sendNext.

public void sendNext() {
    NetworkPacket np = new NetworkPacket(PACKET_TYPE_MOUSEPAD_REQUEST);
    np.set("specialKey", SpecialKeysMap.get(KeyEvent.KEYCODE_PAGE_DOWN));
    device.sendPacket(np);
}
Also used : NetworkPacket(org.kde.kdeconnect.NetworkPacket)

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