66import android .util .Log ;
77
88import org .sofwerx .torgi .BuildConfig ;
9+ import org .sofwerx .torgi .Config ;
910import org .w3c .dom .Document ;
1011import org .xml .sax .InputSource ;
1112import org .xml .sax .SAXException ;
1213
1314import java .io .ByteArrayInputStream ;
1415import java .io .IOException ;
1516import java .io .StringWriter ;
17+ import java .io .UnsupportedEncodingException ;
1618import java .text .ParseException ;
1719import java .text .SimpleDateFormat ;
1820import java .util .Date ;
21+ import java .util .Locale ;
1922
2023import javax .xml .parsers .DocumentBuilder ;
2124import javax .xml .parsers .DocumentBuilderFactory ;
3639public class SosIpcTransceiver extends BroadcastReceiver {
3740 public final static String TAG = "SosIpc" ;
3841 public final static String SOFWERX_LINK_PLACEHOLDER = "http://www.sofwerx.org/placeholder" ; //this is used as a placeholder where a URL should be provided for a new standard or feature
39- private final static SimpleDateFormat dateFormatISO8601 = new SimpleDateFormat ("yyyy-MM-dd'T'HH:mm:ss.SSSXXX" );
42+ private final static SimpleDateFormat dateFormatISO8601 = new SimpleDateFormat ("yyyy-MM-dd'T'HH:mm:ss.SSSXXX" , Locale . US );
4043 public static final String ACTION_SOS = "org.sofwerx.ogc.ACTION_SOS" ;
4144 private static final String EXTRA_PAYLOAD = "SOS" ;
4245 private static final String EXTRA_ORIGIN = "src" ;
46+ public static final String ACTION_SQAN_BROADCAST = "org.sofwerx.sqan.pkt" ;
47+ private final static String SQAN_PACKET_BYTES = "bytes" ;
48+ private final static String SQAN_PACKET_ORIGIN = "src" ;
49+ private final static String SQAN_PACKET_CHANNEL = "channel" ;
50+ private final static String SQAN_CHANNEL = "torgi" ;
4351 private SosMessageListener listener ;
4452
4553 public SosIpcTransceiver (SosMessageListener listener ) {
@@ -49,12 +57,28 @@ public SosIpcTransceiver(SosMessageListener listener) {
4957 @ Override
5058 public void onReceive (Context context , Intent intent ) {
5159 if ((context != null ) && (intent != null )) {
52- if (ACTION_SOS .equals (intent .getAction ())) {
60+ String action = intent .getAction ();
61+ if (ACTION_SOS .equalsIgnoreCase (action )) {
5362 String origin = intent .getStringExtra (EXTRA_ORIGIN );
5463 if (!BuildConfig .APPLICATION_ID .equalsIgnoreCase (origin ))
5564 onMessageReceived (context , origin , intent .getStringExtra (EXTRA_PAYLOAD ));
65+ } else if (ACTION_SQAN_BROADCAST .equalsIgnoreCase (action )) { //forward traffic from SqAN
66+ String origin = intent .getStringExtra (EXTRA_ORIGIN );
67+ if (!BuildConfig .APPLICATION_ID .equalsIgnoreCase (origin )) {
68+ String channel = intent .getStringExtra (SQAN_PACKET_CHANNEL );
69+ if (SQAN_CHANNEL .equalsIgnoreCase (channel )) { //only handle TORGI channel broadcasts
70+ try {
71+ byte [] bytes = intent .getByteArrayExtra (SQAN_PACKET_BYTES );
72+ if (bytes != null ) {
73+ String payload = new String (bytes ,"UTF-8" );
74+ onMessageReceived (context ,"sqan.torgi" ,payload );
75+ }
76+ } catch (UnsupportedEncodingException ignore ) {
77+ }
78+ }
79+ }
5680 } else
57- Log .e (TAG , "Unexpected action message received: " + intent . getAction () );
81+ Log .e (TAG , "Unexpected action message received: " + action );
5882 }
5983 }
6084
@@ -146,8 +170,22 @@ private void broadcast(Context context, String sosOperation) {
146170 Intent intent = new Intent (ACTION_SOS );
147171 intent .putExtra (EXTRA_ORIGIN , BuildConfig .APPLICATION_ID );
148172 intent .putExtra (EXTRA_PAYLOAD ,sosOperation );
149-
150173 context .sendBroadcast (intent );
174+
175+ //Broadcast of SqAN as well
176+ if (Config .isSqAnBroadcastEnabled (context )) {
177+ Log .d (TAG ,"Broadcasting over SqAN as well" );
178+ try {
179+ byte [] bytes = sosOperation .getBytes ("UTF-8" );
180+ Intent sqanIntent = new Intent (ACTION_SQAN_BROADCAST );
181+ sqanIntent .putExtra (EXTRA_ORIGIN , BuildConfig .APPLICATION_ID );
182+ sqanIntent .putExtra (SQAN_PACKET_BYTES , bytes );
183+ sqanIntent .putExtra (SQAN_PACKET_CHANNEL , SQAN_CHANNEL );
184+ context .sendBroadcast (sqanIntent );
185+ } catch (UnsupportedEncodingException ignore ) {
186+ }
187+ }
188+
151189 Log .d (TAG ,"Broadcast: " +sosOperation );
152190 }
153191
0 commit comments