|
| 1 | +package com.radio.codec2talkie.protocol; |
| 2 | + |
| 3 | +import android.content.Context; |
| 4 | +import android.content.SharedPreferences; |
| 5 | + |
| 6 | +import com.radio.codec2talkie.protocol.message.TextMessage; |
| 7 | +import com.radio.codec2talkie.protocol.position.Position; |
| 8 | +import com.radio.codec2talkie.settings.PreferenceKeys; |
| 9 | +import com.radio.codec2talkie.tools.TextTools; |
| 10 | +import com.radio.codec2talkie.transport.Transport; |
| 11 | + |
| 12 | +import java.io.IOException; |
| 13 | +import java.nio.ByteBuffer; |
| 14 | + |
| 15 | +public class CustomDataPrefix implements Protocol { |
| 16 | + |
| 17 | + private final Protocol _childProtocol; |
| 18 | + private ProtocolCallback _parentProtocolCallback; |
| 19 | + private final byte[] _bytePrefix; |
| 20 | + |
| 21 | + public CustomDataPrefix(Protocol childProtocol, SharedPreferences sharedPreferences) { |
| 22 | + _childProtocol = childProtocol; |
| 23 | + String prefix = sharedPreferences.getString(PreferenceKeys.CUSTOM_PREFIX, ""); |
| 24 | + _bytePrefix = TextTools.hexStringToByteArray(prefix); |
| 25 | + } |
| 26 | + |
| 27 | + @Override |
| 28 | + public void initialize(Transport transport, Context context, ProtocolCallback protocolCallback) throws IOException { |
| 29 | + _parentProtocolCallback = protocolCallback; |
| 30 | + _childProtocol.initialize(transport, context, _protocolCallback); |
| 31 | + } |
| 32 | + |
| 33 | + @Override |
| 34 | + public int getPcmAudioRecordBufferSize() { |
| 35 | + return -1; |
| 36 | + } |
| 37 | + |
| 38 | + @Override |
| 39 | + public void sendCompressedAudio(String src, String dst, byte[] frame) throws IOException { |
| 40 | + byte[] prefixedData = ByteBuffer.allocate(_bytePrefix.length + frame.length) |
| 41 | + .put(_bytePrefix) |
| 42 | + .put(frame) |
| 43 | + .array(); |
| 44 | + _childProtocol.sendCompressedAudio(src, dst, prefixedData); |
| 45 | + } |
| 46 | + |
| 47 | + @Override |
| 48 | + public void sendTextMessage(TextMessage textMessage) throws IOException { |
| 49 | + _childProtocol.sendTextMessage(textMessage); |
| 50 | + } |
| 51 | + |
| 52 | + @Override |
| 53 | + public void sendPcmAudio(String src, String dst, short[] pcmFrame) throws IOException { |
| 54 | + _childProtocol.sendPcmAudio(src, dst, pcmFrame); |
| 55 | + } |
| 56 | + |
| 57 | + @Override |
| 58 | + public void sendData(String src, String dst, String path, byte[] dataPacket) throws IOException { |
| 59 | + byte[] prefixedData = ByteBuffer.allocate(_bytePrefix.length + dataPacket.length) |
| 60 | + .put(_bytePrefix) |
| 61 | + .put(dataPacket) |
| 62 | + .array(); |
| 63 | + _childProtocol.sendData(src, dst, path, prefixedData); |
| 64 | + } |
| 65 | + |
| 66 | + @Override |
| 67 | + public boolean receive() throws IOException { |
| 68 | + return _childProtocol.receive(); |
| 69 | + } |
| 70 | + |
| 71 | + @Override |
| 72 | + public void sendPosition(Position position) throws IOException { |
| 73 | + _childProtocol.sendPosition(position); |
| 74 | + } |
| 75 | + |
| 76 | + @Override |
| 77 | + public void flush() throws IOException { |
| 78 | + _childProtocol.flush(); |
| 79 | + } |
| 80 | + |
| 81 | + @Override |
| 82 | + public void close() { |
| 83 | + _childProtocol.close(); |
| 84 | + } |
| 85 | + |
| 86 | + ProtocolCallback _protocolCallback = new ProtocolCallback() { |
| 87 | + @Override |
| 88 | + protected void onReceivePosition(Position position) { |
| 89 | + _parentProtocolCallback.onReceivePosition(position); |
| 90 | + } |
| 91 | + |
| 92 | + @Override |
| 93 | + protected void onReceivePcmAudio(String src, String dst, short[] pcmFrame) { |
| 94 | + _parentProtocolCallback.onReceivePcmAudio(src, dst, pcmFrame); |
| 95 | + } |
| 96 | + |
| 97 | + @Override |
| 98 | + protected void onReceiveCompressedAudio(String src, String dst, byte[] audioFrame) { |
| 99 | + _parentProtocolCallback.onReceiveCompressedAudio(src, dst, audioFrame); |
| 100 | + } |
| 101 | + |
| 102 | + @Override |
| 103 | + protected void onReceiveTextMessage(TextMessage textMessage) { |
| 104 | + _parentProtocolCallback.onReceiveTextMessage(textMessage); |
| 105 | + } |
| 106 | + |
| 107 | + @Override |
| 108 | + protected void onReceiveData(String src, String dst, String path, byte[] data) { |
| 109 | + _parentProtocolCallback.onReceiveData(src, dst, path, data); |
| 110 | + } |
| 111 | + |
| 112 | + @Override |
| 113 | + protected void onReceiveSignalLevel(short rssi, short snr) { |
| 114 | + _parentProtocolCallback.onReceiveSignalLevel(rssi, snr); |
| 115 | + } |
| 116 | + |
| 117 | + @Override |
| 118 | + protected void onReceiveTelemetry(int batVoltage) { |
| 119 | + _parentProtocolCallback.onReceiveTelemetry(batVoltage); |
| 120 | + } |
| 121 | + |
| 122 | + @Override |
| 123 | + protected void onReceiveLog(String logData) { |
| 124 | + _parentProtocolCallback.onReceiveLog(logData); |
| 125 | + } |
| 126 | + |
| 127 | + @Override |
| 128 | + protected void onTransmitPcmAudio(String src, String dst, short[] frame) { |
| 129 | + _parentProtocolCallback.onTransmitPcmAudio(src, dst, frame); |
| 130 | + } |
| 131 | + |
| 132 | + @Override |
| 133 | + protected void onTransmitCompressedAudio(String src, String dst, byte[] frame) { |
| 134 | + _parentProtocolCallback.onTransmitCompressedAudio(src, dst, frame); |
| 135 | + } |
| 136 | + |
| 137 | + @Override |
| 138 | + protected void onTransmitTextMessage(TextMessage textMessage) { |
| 139 | + _parentProtocolCallback.onTransmitTextMessage(textMessage); |
| 140 | + } |
| 141 | + |
| 142 | + @Override |
| 143 | + protected void onTransmitPosition(Position position) { |
| 144 | + _parentProtocolCallback.onTransmitPosition(position); |
| 145 | + } |
| 146 | + |
| 147 | + @Override |
| 148 | + protected void onTransmitData(String src, String dst, String path, byte[] data) { |
| 149 | + _parentProtocolCallback.onTransmitData(src, dst, path, data); |
| 150 | + } |
| 151 | + |
| 152 | + @Override |
| 153 | + protected void onTransmitLog(String logData) { |
| 154 | + _parentProtocolCallback.onTransmitLog(logData); |
| 155 | + } |
| 156 | + |
| 157 | + @Override |
| 158 | + protected void onProtocolRxError() { |
| 159 | + _parentProtocolCallback.onProtocolRxError(); |
| 160 | + } |
| 161 | + |
| 162 | + @Override |
| 163 | + protected void onProtocolTxError() { |
| 164 | + _parentProtocolCallback.onProtocolTxError(); |
| 165 | + } |
| 166 | + }; |
| 167 | +} |
0 commit comments