2014-11-16 23:14:06 -08:00
|
|
|
/*
|
2020-08-17 16:17:20 +02:00
|
|
|
* SPDX-FileCopyrightText: 2014 Albert Vaca Cintora <albertvaka@gmail.com>
|
2014-11-16 23:14:06 -08:00
|
|
|
*
|
2020-08-17 16:17:20 +02:00
|
|
|
* SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
|
2014-11-16 23:14:06 -08:00
|
|
|
*/
|
|
|
|
|
2013-09-16 15:24:21 +02:00
|
|
|
package org.kde.kdeconnect.Backends;
|
2013-06-17 12:23:08 +02:00
|
|
|
|
2015-06-20 20:54:39 +05:30
|
|
|
import android.content.Context;
|
|
|
|
|
2023-03-05 22:03:58 +01:00
|
|
|
import androidx.annotation.WorkerThread;
|
|
|
|
|
2015-01-15 22:57:06 -08:00
|
|
|
import org.kde.kdeconnect.Device;
|
2018-03-04 11:31:37 +01:00
|
|
|
import org.kde.kdeconnect.NetworkPacket;
|
2013-06-17 12:23:08 +02:00
|
|
|
|
2013-09-16 17:36:26 +02:00
|
|
|
import java.security.PrivateKey;
|
2013-06-17 12:23:08 +02:00
|
|
|
import java.util.ArrayList;
|
|
|
|
|
|
|
|
|
2013-09-16 17:36:26 +02:00
|
|
|
public abstract class BaseLink {
|
2013-06-17 12:23:08 +02:00
|
|
|
|
2015-06-20 20:54:39 +05:30
|
|
|
protected final Context context;
|
2015-09-11 09:24:35 -07:00
|
|
|
|
2018-03-04 11:31:37 +01:00
|
|
|
public interface PacketReceiver {
|
|
|
|
void onPacketReceived(NetworkPacket np);
|
2015-09-09 03:31:39 -07:00
|
|
|
}
|
|
|
|
|
2014-03-29 01:47:15 +01:00
|
|
|
private final BaseLinkProvider linkProvider;
|
2015-06-25 19:54:37 +05:30
|
|
|
private final String deviceId;
|
2018-03-04 11:31:37 +01:00
|
|
|
private final ArrayList<PacketReceiver> receivers = new ArrayList<>();
|
2013-09-16 17:36:26 +02:00
|
|
|
protected PrivateKey privateKey;
|
2013-07-23 16:11:54 +02:00
|
|
|
|
2016-03-02 11:39:49 -08:00
|
|
|
protected BaseLink(Context context, String deviceId, BaseLinkProvider linkProvider) {
|
2015-09-11 09:24:35 -07:00
|
|
|
this.context = context;
|
2013-07-23 16:11:54 +02:00
|
|
|
this.linkProvider = linkProvider;
|
2013-08-07 10:44:52 +02:00
|
|
|
this.deviceId = deviceId;
|
2013-07-23 16:11:54 +02:00
|
|
|
}
|
|
|
|
|
2015-08-13 14:55:00 +05:30
|
|
|
/* To be implemented by each link for pairing handlers */
|
|
|
|
public abstract String getName();
|
|
|
|
public abstract BasePairingHandler getPairingHandler(Device device, BasePairingHandler.PairingHandlerCallback callback);
|
|
|
|
|
2013-07-23 16:11:54 +02:00
|
|
|
public String getDeviceId() {
|
|
|
|
return deviceId;
|
|
|
|
}
|
|
|
|
|
2013-09-16 17:36:26 +02:00
|
|
|
public void setPrivateKey(PrivateKey key) {
|
|
|
|
privateKey = key;
|
|
|
|
}
|
|
|
|
|
2013-07-23 16:11:54 +02:00
|
|
|
public BaseLinkProvider getLinkProvider() {
|
|
|
|
return linkProvider;
|
|
|
|
}
|
|
|
|
|
2016-03-02 11:39:49 -08:00
|
|
|
//The daemon will periodically destroy unpaired links if this returns false
|
|
|
|
public boolean linkShouldBeKeptAlive() {
|
|
|
|
return false;
|
2013-07-23 17:50:09 +02:00
|
|
|
}
|
2013-06-17 12:23:08 +02:00
|
|
|
|
2018-03-04 11:31:37 +01:00
|
|
|
public void addPacketReceiver(PacketReceiver pr) {
|
2013-06-17 12:23:08 +02:00
|
|
|
receivers.add(pr);
|
|
|
|
}
|
2018-03-04 11:31:37 +01:00
|
|
|
public void removePacketReceiver(PacketReceiver pr) {
|
2013-06-17 12:23:08 +02:00
|
|
|
receivers.remove(pr);
|
|
|
|
}
|
|
|
|
|
2023-03-17 18:56:52 +01:00
|
|
|
//Should be called from a background thread listening for packets
|
|
|
|
protected void packetReceived(NetworkPacket np) {
|
2018-03-04 11:31:37 +01:00
|
|
|
for(PacketReceiver pr : receivers) {
|
|
|
|
pr.onPacketReceived(np);
|
2013-06-17 12:23:08 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-09 00:45:56 +02:00
|
|
|
public void disconnect() {
|
|
|
|
linkProvider.connectionLost(this);
|
|
|
|
}
|
|
|
|
|
2013-10-05 17:25:59 +02:00
|
|
|
//TO OVERRIDE, should be sync
|
2019-07-21 07:01:50 -04:00
|
|
|
@WorkerThread
|
2018-03-04 11:31:37 +01:00
|
|
|
public abstract boolean sendPacket(NetworkPacket np, Device.SendPacketStatusCallback callback);
|
2013-06-17 12:23:08 +02:00
|
|
|
}
|