Search in sources :

Example 36 with NetworkPacket

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

the class NotificationsPlugin method onNotificationRemoved.

@Override
public void onNotificationRemoved(StatusBarNotification statusBarNotification) {
    if (statusBarNotification == null) {
        Log.w(TAG, "onNotificationRemoved: notification is null");
        return;
    }
    String id = getNotificationKeyCompat(statusBarNotification);
    actions.remove(id);
    NetworkPacket np = new NetworkPacket(PACKET_TYPE_NOTIFICATION);
    np.set("id", id);
    np.set("isCancel", true);
    device.sendPacket(np);
    currentNotifications.remove(id);
}
Also used : NetworkPacket(org.kde.kdeconnect.NetworkPacket) SpannableString(android.text.SpannableString)

Example 37 with NetworkPacket

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

the class TelephonyPlugin method callBroadcastReceived.

private void callBroadcastReceived(int state, String phoneNumber) {
    if (isNumberBlocked(phoneNumber))
        return;
    NetworkPacket np = new NetworkPacket(PACKET_TYPE_TELEPHONY);
    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")) {
            String photoUri = contactInfo.get("photoID");
            if (photoUri != null) {
                try {
                    String base64photo = ContactsHelper.photoId64Encoded(context, photoUri);
                    if (!TextUtils.isEmpty(base64photo)) {
                        np.set("phoneThumbnail", base64photo);
                    }
                } catch (Exception e) {
                    Log.e("TelephonyPlugin", "Failed to get contact photo");
                }
            }
        }
    } else {
        np.set("contactName", phoneNumber);
    }
    if (phoneNumber != null) {
        np.set("phoneNumber", phoneNumber);
    }
    switch(state) {
        case TelephonyManager.CALL_STATE_RINGING:
            unmuteRinger();
            np.set("event", "ringing");
            device.sendPacket(np);
            break;
        case // Ongoing call
        TelephonyManager.CALL_STATE_OFFHOOK:
            np.set("event", "talking");
            device.sendPacket(np);
            break;
        case TelephonyManager.CALL_STATE_IDLE:
            if (lastPacket != null) {
                // Resend a cancel of the last event (can either be "ringing" or "talking")
                lastPacket.set("isCancel", "true");
                device.sendPacket(lastPacket);
                if (isMuted) {
                    Timer timer = new Timer();
                    timer.schedule(new TimerTask() {

                        @Override
                        public void run() {
                            unmuteRinger();
                        }
                    }, 500);
                }
                // Emit a missed call notification if needed
                if ("ringing".equals(lastPacket.getString("event", null))) {
                    np.set("event", "missedCall");
                    np.set("phoneNumber", lastPacket.getString("phoneNumber", null));
                    np.set("contactName", lastPacket.getString("contactName", null));
                    device.sendPacket(np);
                }
            }
            break;
    }
    lastPacket = np;
}
Also used : Timer(java.util.Timer) TimerTask(java.util.TimerTask) NetworkPacket(org.kde.kdeconnect.NetworkPacket)

Example 38 with NetworkPacket

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

the class SftpPlugin method onPacketReceived.

@Override
public boolean onPacketReceived(NetworkPacket np) {
    if (np.getBoolean("startBrowsing")) {
        ArrayList<String> paths = new ArrayList<>();
        ArrayList<String> pathNames = new ArrayList<>();
        List<StorageInfo> storageInfoList = SftpSettingsFragment.getStorageInfoList(context, this);
        Collections.sort(storageInfoList, Comparator.comparing(StorageInfo::getUri));
        if (storageInfoList.size() > 0) {
            getPathsAndNamesForStorageInfoList(paths, pathNames, storageInfoList);
        } else {
            NetworkPacket np2 = new NetworkPacket(PACKET_TYPE_SFTP);
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                np2.set("errorMessage", context.getString(R.string.sftp_no_storage_locations_configured));
            } else {
                np2.set("errorMessage", context.getString(R.string.sftp_no_sdcard_detected));
            }
            device.sendPacket(np2);
            return true;
        }
        removeChildren(storageInfoList);
        if (server.start(storageInfoList)) {
            if (preferences != null) {
                preferences.registerOnSharedPreferenceChangeListener(this);
            }
            NetworkPacket np2 = new NetworkPacket(PACKET_TYPE_SFTP);
            // TODO: ip is not used on desktop any more remove both here and from desktop code when nobody ships 1.2.0
            np2.set("ip", server.getLocalIpAddress());
            np2.set("port", server.getPort());
            np2.set("user", SimpleSftpServer.USER);
            np2.set("password", server.getPassword());
            // Kept for compatibility, in case "multiPaths" is not possible or the other end does not support it
            np2.set("path", "/");
            if (paths.size() > 0) {
                np2.set("multiPaths", paths);
                np2.set("pathNames", pathNames);
            }
            device.sendPacket(np2);
            return true;
        }
    }
    return false;
}
Also used : ArrayList(java.util.ArrayList) NetworkPacket(org.kde.kdeconnect.NetworkPacket)

Example 39 with NetworkPacket

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

the class SystemVolumePlugin method sendEnable.

void sendEnable(String name) {
    NetworkPacket np = new NetworkPacket(PACKET_TYPE_SYSTEMVOLUME_REQUEST);
    np.set("enabled", true);
    np.set("name", name);
    device.sendPacket(np);
}
Also used : NetworkPacket(org.kde.kdeconnect.NetworkPacket)

Example 40 with NetworkPacket

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

the class LanLink method receivedNetworkPacket.

private void receivedNetworkPacket(NetworkPacket np) {
    if (np.hasPayloadTransferInfo()) {
        Socket payloadSocket = new Socket();
        try {
            int tcpPort = np.getPayloadTransferInfo().getInt("port");
            InetSocketAddress deviceAddress = (InetSocketAddress) socket.getRemoteSocketAddress();
            payloadSocket.connect(new InetSocketAddress(deviceAddress.getAddress(), tcpPort));
            payloadSocket = SslHelper.convertToSslSocket(context, payloadSocket, getDeviceId(), true, true);
            np.setPayload(new NetworkPacket.Payload(payloadSocket, np.getPayloadSize()));
        } catch (Exception e) {
            try {
                payloadSocket.close();
            } catch (Exception ignored) {
            }
            Log.e("KDE/LanLink", "Exception connecting to payload remote socket", e);
        }
    }
    packageReceived(np);
}
Also used : InetSocketAddress(java.net.InetSocketAddress) NetworkPacket(org.kde.kdeconnect.NetworkPacket) Socket(java.net.Socket) SSLSocket(javax.net.ssl.SSLSocket) ServerSocket(java.net.ServerSocket) SSLHandshakeException(javax.net.ssl.SSLHandshakeException) IOException(java.io.IOException) NotYetConnectedException(java.nio.channels.NotYetConnectedException) SocketTimeoutException(java.net.SocketTimeoutException)

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