2
0
mirror of https://github.com/KDE/kdeconnect-android synced 2025-08-22 01:51:47 +00:00
Albert Vaca Cintora 5c0c190f5a Add DeviceInfo class
It contains all the properties we need to instantiate a Device: id, name,
type, cert, capabilities and protocol version. Before, we had a mix of
passing those around as arguments or passing identity packets (ie: json).
This simplifies the Device class quite a bit.

Now, the BaseLink subclasses need to implement getDeviceInfo() interface
that returns a DeviceInfo, which is what we will pass around and eventually
use to instantiate a Device.

This means that identity packets are an implementation detail of the
LanLinkProvider and that other implementations could get the DeviceInfo
in a different way.
2023-06-20 10:27:03 +02:00

72 lines
2.1 KiB
Java

/*
* SPDX-FileCopyrightText: 2014 Albert Vaca Cintora <albertvaka@gmail.com>
*
* SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
*/
package org.kde.kdeconnect.Backends;
import android.content.Context;
import androidx.annotation.NonNull;
import androidx.annotation.WorkerThread;
import org.kde.kdeconnect.Device;
import org.kde.kdeconnect.DeviceInfo;
import org.kde.kdeconnect.NetworkPacket;
import java.io.IOException;
import java.util.ArrayList;
public abstract class BaseLink {
public interface PacketReceiver {
void onPacketReceived(@NonNull NetworkPacket np);
}
protected final Context context;
private final BaseLinkProvider linkProvider;
private final ArrayList<PacketReceiver> receivers = new ArrayList<>();
protected BaseLink(@NonNull Context context, @NonNull BaseLinkProvider linkProvider) {
this.context = context;
this.linkProvider = linkProvider;
}
/* To be implemented by each link for pairing handlers */
public abstract String getName();
public abstract DeviceInfo getDeviceInfo();
public String getDeviceId() {
return getDeviceInfo().id;
}
public BaseLinkProvider getLinkProvider() {
return linkProvider;
}
public void addPacketReceiver(@NonNull PacketReceiver pr) {
receivers.add(pr);
}
public void removePacketReceiver(@NonNull PacketReceiver pr) {
receivers.remove(pr);
}
//Should be called from a background thread listening for packets
public void packetReceived(@NonNull NetworkPacket np) {
for(PacketReceiver pr : receivers) {
pr.onPacketReceived(np);
}
}
public void disconnect() {
linkProvider.onConnectionLost(this);
}
//TO OVERRIDE, should be sync. If sendPayloadFromSameThread is false, it should only block to send the packet but start a separate thread to send the payload.
@WorkerThread
public abstract boolean sendPacket(@NonNull NetworkPacket np, @NonNull Device.SendPacketStatusCallback callback, boolean sendPayloadFromSameThread) throws IOException;
}