diff --git a/libSafeMobile/src/main/java/com/safemobile/services/TCPhandler.java b/libSafeMobile/src/main/java/com/safemobile/services/TCPhandler.java index bc89785..072dd7a 100644 --- a/libSafeMobile/src/main/java/com/safemobile/services/TCPhandler.java +++ b/libSafeMobile/src/main/java/com/safemobile/services/TCPhandler.java @@ -5,7 +5,6 @@ import android.content.Context; import android.content.Intent; import android.content.IntentFilter; import android.net.ConnectivityManager; -import android.net.NetworkInfo; import android.net.wifi.WifiManager; import com.safemobile.lib.AppParams; @@ -38,21 +37,19 @@ public class TCPhandler implements Runnable { private String leftOver = ""; public static LinkedList msgList; - private volatile int n = 0; public Boolean isConnectionUP = false; public Boolean previousConnectionWasUP = false; - - private Context context; - private boolean isWiFiOn = true; + + private final Context context; + private boolean isWiFiOn; public TCPhandler(Context context, String hostName, int p) { - this.context = context; serverHostname = hostName; port = p; - msgList = new LinkedList(); + msgList = new LinkedList<>(); SM.Debug("---TCPhandler constructor [" + hostName + "," + p + "] ---"); listenThread = new Thread(this, "TCPlisten"); listenThread.start(); // (2) Start the thread. @@ -64,7 +61,6 @@ public class TCPhandler implements Runnable { public void run() { try { previousConnectionWasUP = isConnectionUP; - // try to send something TCPmsgParser._fireonTCPConnectionStatusEvent(isConnectionUP, previousConnectionWasUP); } catch (Exception e) { @@ -72,18 +68,20 @@ public class TCPhandler implements Runnable { } } }, 0, 3000); - - // get WiFi state - ConnectivityManager connManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); - NetworkInfo mWifi = connManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI); - if (mWifi.isConnectedOrConnecting()) - isWiFiOn = true; - + + // get WiFi state + isWiFiOn = isNetworkConnected(); + IntentFilter intentFilter = new IntentFilter(); intentFilter.addAction(WifiManager.NETWORK_STATE_CHANGED_ACTION); context.registerReceiver(mReceived, intentFilter); } + private boolean isNetworkConnected() { + ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); + return cm.getActiveNetworkInfo() != null && cm.getActiveNetworkInfo().isConnected(); + } + @Override public void run() { try { @@ -100,20 +98,19 @@ public class TCPhandler implements Runnable { isConnectionUP = true; triggerTCPConnectionStateEvent(); } - } catch (UnknownHostException e) { - SM.Debug("UnknownHostException", "TCPhandler break:" + e.toString()); + SM.Debug("UnknownHostException", "TCPhandler break:"+ e); } catch (IllegalArgumentException e) { - SM.Debug("IllegalArgumentException", "TCPhandler break:" + e.toString()); + SM.Debug("IllegalArgumentException", "TCPhandler break:"+ e); } catch (IOException e) { - SM.Debug("IOException", "TCPhandler break:" + e.toString()); + SM.Debug("IOException", "TCPhandler break:"+ e); } while (alive) { try { Thread.sleep(3000); } catch (InterruptedException e) { - SM.Debug("TCPhandler Crash1 on sleep:" + e.toString()); + SM.Debug("TCPhandler Crash1 on sleep:"+ e); } while (Boolean.TRUE.equals(isConnectionUP)) { @@ -166,7 +163,7 @@ public class TCPhandler implements Runnable { //end process leftover String data = ""; - n = 0; + int n = 0; byte[] buf = new byte[1024]; // read data into buffer @@ -181,7 +178,6 @@ public class TCPhandler implements Runnable { break; } - byte[] temp = new byte[n]; if (n >= 0) System.arraycopy(buf, 0, temp, 0, n); @@ -190,77 +186,72 @@ public class TCPhandler implements Runnable { data = new String(temp); - //if we have any leftovers from previous message add them - if (leftOver.length() > 1) // avoid case with only one # - { - data = leftOver + data; - leftOver = ""; - } + //if we have any leftovers from previous message add them + if(leftOver.length() > 1) { // avoid case with only one # + data = leftOver+data; + leftOver = ""; + } - //search for overflow message - String[] tempArr = data.split("#"); - if ((tempArr.length == 0) || (tempArr.length == 1)) { - SM.Debug("TCP Client", "incorect messagebuss message=" + data); - continue; - } - //get msg len - int messLen; + //search for overflow message + String[] tempArr = data.split("#"); + if ((tempArr.length == 0) || (tempArr.length == 1)) { + SM.Debug("TCP Client", "incorect messagebuss message=" + data); + continue; + } + //get msg len + int messLen; try { messLen = Integer.parseInt(tempArr[1]); } catch (Exception e) { SM.Debug("TCP Client", "incorect msg len =" + tempArr[1]); continue; - } - //messLen not int - if (messLen == -1) { - continue; - } - char[] temMSG = data.toCharArray(); - if (data.length() != messLen) { - if (messLen > data.length()) { - leftOver = data; // Add by bigu - continue; - } - //perform cut - temMSG = data.substring(0, messLen).toCharArray(); - leftOver = data.substring(messLen, data.length()); - } + } + //messLen not int + if (messLen == -1) { + continue; + } + char[] temMSG = data.toCharArray(); + if (data.length() != messLen) { + //if expected string message is smaller then actual string then exit processing; + if (messLen > data.length()) { + leftOver = data; // Add by bigu + continue; + } + //perform cut + temMSG = data.substring(0,messLen).toCharArray(); + leftOver = data.substring(messLen); + } //decode TCP msg TCPmsg msg = new TCPmsg(temMSG); SM.Debug("������� RX �������", msg.allData); - + if (msg.allData.contains("#92#")) prioritizePongReceived(); msgList.add(msg); - - } catch (Exception ex) { - SM.Debug("TCPHandler", "TCPhandler/run/break:" + ex.toString()); + } catch(Exception ex) { + SM.Debug("TCPHandler", "TCPhandler/run/break:"+ ex); isConnectionUP = false; triggerTCPConnectionStateEvent(); } } - - + // try { Thread.sleep(1000); } catch (InterruptedException e) { - SM.Debug("TCPhandler Crash2 on sleep:" + e.toString()); + SM.Debug("TCPhandler Crash2 on sleep:"+ e); } - //try to restart connection if (alive && isWiFiOn) restartTCP(); - } SM.Debug("=================================="); SM.Debug("TCP listenThread stoped!! alive = false"); SM.Debug("=================================="); } - /** * Create a bypass in order to trigger the ping received event */ @@ -268,33 +259,26 @@ public class TCPhandler implements Runnable { TCPmsgParser._firePONGReceivedEvent(); } - /* Broadcast Received for WiFi Connect/Disconnect */ public BroadcastReceiver mReceived = new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { - final String action = intent.getAction(); - SM.Debug("WIFI STATE", action); - - if (action.equals(WifiManager.NETWORK_STATE_CHANGED_ACTION)) { - NetworkInfo info = intent.getParcelableExtra(WifiManager.EXTRA_NETWORK_INFO); - // close socket if the wifi is disconnecting or disconnected - if (!info.isConnectedOrConnecting()) { + final String action = intent.getAction(); + SM.Debug("WIFI STATE", action); + + if (action.equals(WifiManager.NETWORK_STATE_CHANGED_ACTION)){ + // close socket if the wifi is disconnecting or disconnected + isWiFiOn = isNetworkConnected(); + if (!isWiFiOn) closeSocket(); - isWiFiOn = false; - } else - isWiFiOn = true; - } + } } }; - - - /** - * Send a message through the TCP Socket - * - * @param seqID The messages's sequence id (a number of order) - * @param msg The messages which will be sent + + /** Send a message through the TCP Socket + * @param seqID The messages's sequence ID (a number of order) + * @param msg The messages which will be sent * @return True if the message was sent */ public boolean Write(String seqID, String msg) { @@ -338,52 +322,29 @@ public class TCPhandler implements Runnable { } return false; } - - - - - public void setConnectionIsDown() { - if (input != null) { - try { - input.close(); - } catch (IOException e) { - } finally { - input = null; - } - } - - isConnectionUP = false; - } - + /* Encrypt a string using an encryption algorithm, * in this case TEA */ public static byte[] encryptTEA(String toEncryptData) { - byte[] encryptedByteArray = new byte[]{}; - // no encryption - encryptedByteArray = toEncryptData.getBytes(); - - return encryptedByteArray; + return toEncryptData.getBytes(); } /* Decrypt a string using an encryption algorithm, * in this case TEA */ public static byte[] decryptTEA(byte[] toDecryptData) { - byte[] decryptedByteArray = new byte[]{}; - + byte[] decryptedByteArray; + // no decryption decryptedByteArray = toDecryptData; - return decryptedByteArray; } - public int getPort() { return port; } - + public void updateTCPparameters(String ip, String _port) { - // stop socket try { if (soc != null) @@ -395,9 +356,8 @@ public class TCPhandler implements Runnable { serverHostname = ip; try { port = Integer.parseInt(_port); - } catch (Exception e) { - - } finally { + } catch (Exception ignored) { } + finally { port = 13589; } } @@ -438,23 +398,11 @@ public class TCPhandler implements Runnable { SM.Exception("restartTCP break:" + e.toString()); isConnectionUP = false; } - triggerTCPConnectionStateEvent(); } - public boolean isAlive() { - return alive; - } - - public void setAlive(boolean alive) { - this.alive = alive; - } - - /** - * close Socket when unReachable - */ + /** close Socket when unReachable */ public void closeSocket() { - try { input = null; output = null; @@ -465,17 +413,15 @@ public class TCPhandler implements Runnable { e.printStackTrace(); } } - + public void Stop() { SM.Debug("Stopping TCP", "TCP Connection is stopping on " + AppParams.IP + ":" + port); alive = false; - - - if (mReceived != null) + + if (mReceived!= null) try { context.unregisterReceiver(mReceived); - } catch (Exception ex) {/* receiver not registered //*/} - ; + } catch(Exception ex) {/* receiver not registered //*/} // stop thread if (listenThread != null) { @@ -484,7 +430,6 @@ public class TCPhandler implements Runnable { moribund.interrupt(); } - if (input != null) { try { input.close(); @@ -493,7 +438,7 @@ public class TCPhandler implements Runnable { e.printStackTrace(); } } - + if (output != null) { try { output.close(); @@ -502,15 +447,14 @@ public class TCPhandler implements Runnable { e.printStackTrace(); } } - - if (soc != null) { + + if (soc !=null) { try { soc.close(); soc = null; } catch (IOException e) { - SM.Exception("TCPClient[STOP]", "Stop break:" + e.toString()); + SM.Exception("TCPClient[STOP]", "Stop break:"+ e); } } } - -} +} \ No newline at end of file diff --git a/safeDispatch/release/SafeDispatchMobile_v.8.0.49-release.aab b/safeDispatch/release/SafeDispatchMobile_v.8.0.49-release.aab new file mode 100644 index 0000000..c04a47f Binary files /dev/null and b/safeDispatch/release/SafeDispatchMobile_v.8.0.49-release.aab differ