2014-11-16 23:14:06 -08:00
|
|
|
/*
|
|
|
|
* Copyright 2014 Albert Vaca Cintora <albertvaka@gmail.com>
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License as
|
|
|
|
* published by the Free Software Foundation; either version 2 of
|
|
|
|
* the License or (at your option) version 3 or any later version
|
|
|
|
* accepted by the membership of KDE e.V. (or its successor approved
|
|
|
|
* by the membership of KDE e.V.), which shall act as a proxy
|
|
|
|
* defined in Section 14 of version 3 of the license.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
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;
|
|
|
|
|
2015-01-15 22:57:06 -08:00
|
|
|
import org.kde.kdeconnect.Device;
|
2013-09-05 01:37:59 +02:00
|
|
|
import org.kde.kdeconnect.NetworkPackage;
|
2013-06-17 12:23:08 +02:00
|
|
|
|
2013-09-16 17:36:26 +02:00
|
|
|
import java.security.PrivateKey;
|
|
|
|
import java.security.PublicKey;
|
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
|
|
|
|
2015-09-09 03:31:39 -07:00
|
|
|
public interface PackageReceiver {
|
|
|
|
void onPackageReceived(NetworkPackage np);
|
|
|
|
}
|
|
|
|
|
2014-03-29 01:47:15 +01:00
|
|
|
private final BaseLinkProvider linkProvider;
|
2015-06-25 19:54:37 +05:30
|
|
|
private final String deviceId;
|
2015-08-10 00:26:58 -07:00
|
|
|
private final ArrayList<PackageReceiver> 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
|
|
|
|
2013-07-23 17:50:09 +02:00
|
|
|
public void addPackageReceiver(PackageReceiver pr) {
|
2013-06-17 12:23:08 +02:00
|
|
|
receivers.add(pr);
|
|
|
|
}
|
2013-07-23 17:50:09 +02:00
|
|
|
public void removePackageReceiver(PackageReceiver pr) {
|
2013-06-17 12:23:08 +02:00
|
|
|
receivers.remove(pr);
|
|
|
|
}
|
|
|
|
|
2013-06-18 03:05:32 +02:00
|
|
|
//Should be called from a background thread listening to packages
|
2013-07-23 17:50:09 +02:00
|
|
|
protected void packageReceived(NetworkPackage np) {
|
|
|
|
for(PackageReceiver pr : receivers) {
|
|
|
|
pr.onPackageReceived(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
|
2016-12-05 22:15:27 +01:00
|
|
|
public abstract boolean sendPackage(NetworkPackage np,Device.SendPackageStatusCallback callback);
|
2016-06-09 13:37:38 +02:00
|
|
|
@Deprecated
|
2016-12-05 22:15:27 +01:00
|
|
|
public abstract boolean sendPackageEncrypted(NetworkPackage np,Device.SendPackageStatusCallback callback, PublicKey key);
|
2013-06-17 12:23:08 +02:00
|
|
|
}
|