codec2_talkie/codec2talkie/src/main/java/com/radio/codec2talkie/protocol/ProtocolFactory.java

123 wiersze
3.9 KiB
Java
Czysty Zwykły widok Historia

package com.radio.codec2talkie.protocol;
2021-10-12 13:53:01 +00:00
import android.content.Context;
import android.content.SharedPreferences;
2023-07-02 08:12:53 +00:00
import androidx.annotation.NonNull;
2021-10-12 13:53:01 +00:00
import androidx.preference.PreferenceManager;
2022-08-11 13:40:06 +00:00
import com.radio.codec2talkie.settings.SettingsWrapper;
2021-10-12 13:53:01 +00:00
public class ProtocolFactory {
public enum ProtocolType {
2021-01-31 13:08:07 +00:00
RAW("RAW"),
2022-07-31 11:05:35 +00:00
HDLC("HDLC"),
2021-01-31 13:08:07 +00:00
KISS("KISS"),
KISS_BUFFERED("KISS BUF"),
KISS_PARROT("KISS RPT"),
FREEDV("FREEDV");
2021-01-31 13:08:07 +00:00
private final String _name;
2021-01-31 13:08:07 +00:00
2021-02-06 14:34:30 +00:00
ProtocolType(String name) {
2021-01-31 13:08:07 +00:00
_name = name;
}
2023-07-02 08:12:53 +00:00
@NonNull
2021-01-31 13:08:07 +00:00
@Override
public String toString() {
return _name;
}
2022-06-26 14:58:22 +00:00
}
2022-07-04 19:18:49 +00:00
public static ProtocolType getBaseProtocolType(Context context) {
SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context);
ProtocolFactory.ProtocolType protocolType;
2022-08-10 17:46:13 +00:00
// Use HDLC instead of KISS for the sound modem as we are the modem
2022-08-11 13:40:06 +00:00
if (SettingsWrapper.isSoundModemEnabled(sharedPreferences)) {
if (SettingsWrapper.isFreeDvSoundModemModulation(sharedPreferences)) {
protocolType = ProtocolType.FREEDV;
} else {
protocolType = ProtocolFactory.ProtocolType.HDLC;
}
2022-08-11 13:40:06 +00:00
} else if (SettingsWrapper.isKissProtocolEnabled(sharedPreferences)) {
if (SettingsWrapper.isKissParrotEnabled(sharedPreferences)) {
2022-07-04 19:18:49 +00:00
protocolType = ProtocolFactory.ProtocolType.KISS_PARROT;
}
2022-08-11 13:40:06 +00:00
else if (SettingsWrapper.isKissBufferedModeEnabled(sharedPreferences)) {
2022-07-04 19:18:49 +00:00
protocolType = ProtocolFactory.ProtocolType.KISS_BUFFERED;
}
else {
protocolType = ProtocolFactory.ProtocolType.KISS;
}
} else {
protocolType = ProtocolFactory.ProtocolType.RAW;
}
return protocolType;
}
2023-12-09 11:33:32 +00:00
public static Protocol create(Context context) {
2021-10-12 13:53:01 +00:00
SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context);
2022-07-04 19:18:49 +00:00
ProtocolType protocolType = getBaseProtocolType(context);
2022-08-11 13:40:06 +00:00
boolean recordingEnabled = SettingsWrapper.isCodec2RecorderEnabled(sharedPreferences);
boolean scramblingEnabled = SettingsWrapper.isKissScramblerEnabled(sharedPreferences);
String scramblingKey = SettingsWrapper.getKissScramblerKey(sharedPreferences);
boolean aprsEnabled = SettingsWrapper.isAprsEnabled(sharedPreferences);
2022-08-19 17:34:39 +00:00
boolean aprsIsEnabled = SettingsWrapper.isAprsIsEnabled(sharedPreferences);
2022-08-14 12:57:05 +00:00
boolean freedvEnabled = SettingsWrapper.isFreeDvSoundModemModulation(sharedPreferences);
2021-10-12 13:53:01 +00:00
2022-08-14 12:57:05 +00:00
// "root" protocol
Protocol proto;
switch (protocolType) {
case KISS:
proto = new Kiss();
break;
2021-01-30 19:32:25 +00:00
case KISS_BUFFERED:
proto = new KissBuffered();
break;
case KISS_PARROT:
proto = new KissParrot();
break;
2022-07-31 11:05:35 +00:00
case HDLC:
2022-07-31 12:01:27 +00:00
proto = new Hdlc(sharedPreferences);
2022-07-31 11:05:35 +00:00
break;
case FREEDV:
2022-08-14 12:57:05 +00:00
proto = new Freedv();
break;
case RAW:
default:
proto = new Raw();
break;
}
2022-07-29 14:54:24 +00:00
if (scramblingEnabled) {
proto = new Scrambler(proto, scramblingKey);
}
if (aprsEnabled) {
proto = new Ax25(proto);
}
2022-08-14 12:57:05 +00:00
if (!freedvEnabled) {
if (recordingEnabled) {
2023-12-09 11:33:32 +00:00
proto = new Recorder(proto, sharedPreferences);
2022-08-14 12:57:05 +00:00
}
2023-12-09 11:33:32 +00:00
proto = new AudioFrameAggregator(proto, sharedPreferences);
proto = new AudioCodec2(proto, sharedPreferences);
2022-08-14 12:57:05 +00:00
}
2022-06-29 19:23:02 +00:00
if (aprsEnabled) {
2022-08-19 17:34:39 +00:00
if (aprsIsEnabled) {
proto = new AprsIs(proto);
}
2022-06-29 19:23:02 +00:00
proto = new Aprs(proto);
}
return proto;
}
}