Search in sources :

Example 56 with NetworkPacket

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

the class SMSPlugin method smsBroadcastReceivedDeprecated.

/**
 * Deliver an old-style SMS packet in response to a new message arriving
 *
 * For backwards-compatibility with long-lived distro packages, this method needs to exist in
 * order to support older desktop apps. However, note that it should no longer be used
 *
 * This comment is being written 30 August 2018. Distros will likely be running old versions for
 * many years to come...
 *
 * @param messages Ordered list of parts of the message body which should be combined into a single message
 */
@Deprecated
private void smsBroadcastReceivedDeprecated(ArrayList<SmsMessage> messages) {
    if (BuildConfig.DEBUG) {
        if (!(messages.size() > 0)) {
            throw new AssertionError("This method requires at least one message");
        }
    }
    NetworkPacket np = new NetworkPacket(PACKET_TYPE_TELEPHONY);
    np.set("event", "sms");
    StringBuilder messageBody = new StringBuilder();
    for (int index = 0; index < messages.size(); index++) {
        messageBody.append(messages.get(index).getMessageBody());
    }
    np.set("messageBody", messageBody.toString());
    String phoneNumber = messages.get(0).getOriginatingAddress();
    if (isNumberBlocked(phoneNumber))
        return;
    int permissionCheck = ContextCompat.checkSelfPermission(context, Manifest.permission.READ_CONTACTS);
    if (permissionCheck == PackageManager.PERMISSION_GRANTED) {
        Map<String, String> contactInfo = ContactsHelper.phoneNumberLookup(context, phoneNumber);
        if (contactInfo.containsKey("name")) {
            np.set("contactName", contactInfo.get("name"));
        }
        if (contactInfo.containsKey("photoID")) {
            np.set("phoneThumbnail", ContactsHelper.photoId64Encoded(context, contactInfo.get("photoID")));
        }
    }
    if (phoneNumber != null) {
        np.set("phoneNumber", phoneNumber);
    }
    device.sendPacket(np);
}
Also used : NetworkPacket(org.kde.kdeconnect.NetworkPacket) SuppressLint(android.annotation.SuppressLint)

Example 57 with NetworkPacket

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

the class SMSPlugin method handleRequestAllConversations.

/**
 * Respond to a request for all conversations
 * <p>
 * Send one packet of type PACKET_TYPE_SMS_MESSAGE with the first message in all conversations
 */
private boolean handleRequestAllConversations(NetworkPacket packet) {
    Map<SMSHelper.ThreadID, SMSHelper.Message> conversations = SMSHelper.getConversations(this.context);
    // Prepare the mostRecentTimestamp counter based on these messages, since they are the most
    // recent in every conversation
    mostRecentTimestampLock.lock();
    for (SMSHelper.Message message : conversations.values()) {
        if (message.date > mostRecentTimestamp) {
            mostRecentTimestamp = message.date;
        }
    }
    mostRecentTimestampLock.unlock();
    NetworkPacket reply = constructBulkMessagePacket(conversations.values());
    device.sendPacket(reply);
    return true;
}
Also used : SmsMessage(android.telephony.SmsMessage) SMSHelper(org.kde.kdeconnect.Helpers.SMSHelper) NetworkPacket(org.kde.kdeconnect.NetworkPacket)

Example 58 with NetworkPacket

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

the class NotificationsPlugin method attachIcon.

private void attachIcon(NetworkPacket np, Bitmap appIcon) {
    ByteArrayOutputStream outStream = new ByteArrayOutputStream();
    appIcon.compress(Bitmap.CompressFormat.PNG, 90, outStream);
    byte[] bitmapData = outStream.toByteArray();
    np.setPayload(new NetworkPacket.Payload(bitmapData));
    np.set("payloadHash", getChecksum(bitmapData));
}
Also used : NetworkPacket(org.kde.kdeconnect.NetworkPacket) ByteArrayOutputStream(java.io.ByteArrayOutputStream)

Example 59 with NetworkPacket

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

the class NotificationsPlugin method sendNotification.

private void sendNotification(StatusBarNotification statusBarNotification) {
    Notification notification = statusBarNotification.getNotification();
    if ((notification.flags & Notification.FLAG_FOREGROUND_SERVICE) != 0 || (notification.flags & Notification.FLAG_ONGOING_EVENT) != 0 || (notification.flags & Notification.FLAG_LOCAL_ONLY) != 0 || // The notification that groups other notifications
    (notification.flags & NotificationCompat.FLAG_GROUP_SUMMARY) != 0) {
        // This is not a notification we want!
        return;
    }
    if (!appDatabase.isEnabled(statusBarNotification.getPackageName())) {
        return;
    // we don't want notification from this app
    }
    String key = getNotificationKeyCompat(statusBarNotification);
    String packageName = statusBarNotification.getPackageName();
    String appName = AppsHelper.appNameLookup(context, packageName);
    if ("com.facebook.orca".equals(packageName) && (statusBarNotification.getId() == 10012) && "Messenger".equals(appName) && notification.tickerText == null) {
        // HACK: Hide weird Facebook empty "Messenger" notification that is actually not shown in the phone
        return;
    }
    if ("com.android.systemui".equals(packageName) && "low_battery".equals(statusBarNotification.getTag())) {
        // HACK: Android low battery notification are posted again every few seconds. Ignore them, as we already have a battery indicator.
        return;
    }
    if ("org.kde.kdeconnect_tp".equals(packageName)) {
        // Don't send our own notifications
        return;
    }
    NetworkPacket np = new NetworkPacket(PACKET_TYPE_NOTIFICATION);
    boolean isUpdate = currentNotifications.contains(key);
    // If it's an update, the other end should have the icon already: no need to extract it and create the payload again
    if (!isUpdate) {
        currentNotifications.add(key);
        Bitmap appIcon = extractIcon(statusBarNotification, notification);
        if (appIcon != null && !appDatabase.getPrivacy(packageName, AppDatabase.PrivacyOptions.BLOCK_IMAGES)) {
            attachIcon(np, appIcon);
        }
    }
    np.set("actions", extractActions(notification, key));
    np.set("id", key);
    np.set("onlyOnce", (notification.flags & NotificationCompat.FLAG_ONLY_ALERT_ONCE) != 0);
    np.set("isClearable", statusBarNotification.isClearable());
    np.set("appName", StringUtils.defaultString(appName, packageName));
    np.set("time", Long.toString(statusBarNotification.getPostTime()));
    if (!appDatabase.getPrivacy(packageName, AppDatabase.PrivacyOptions.BLOCK_CONTENTS)) {
        RepliableNotification rn = extractRepliableNotification(statusBarNotification);
        if (rn != null) {
            np.set("requestReplyId", rn.id);
            pendingIntents.put(rn.id, rn);
        }
        np.set("ticker", getTickerText(notification));
        Pair<String, String> conversation = extractConversation(notification);
        if (conversation.first != null) {
            np.set("title", conversation.first);
        } else {
            np.set("title", extractStringFromExtra(getExtras(notification), NotificationCompat.EXTRA_TITLE));
        }
        np.set("text", extractText(notification, conversation));
    }
    device.sendPacket(np);
}
Also used : Bitmap(android.graphics.Bitmap) NetworkPacket(org.kde.kdeconnect.NetworkPacket) SpannableString(android.text.SpannableString) Notification(android.app.Notification) StatusBarNotification(android.service.notification.StatusBarNotification)

Example 60 with NetworkPacket

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

the class PhotoPlugin method sendCancel.

void sendCancel() {
    NetworkPacket np = new NetworkPacket(PACKET_TYPE_PHOTO);
    np.set("cancel", true);
    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