mobile connection restores on wifi
This commit is contained in:
parent
49d15e1064
commit
2a1517526d
@ -6,6 +6,7 @@ import android.content.Intent;
|
||||
import android.content.IntentFilter;
|
||||
import android.net.ConnectivityManager;
|
||||
import android.net.wifi.WifiManager;
|
||||
import android.util.Log;
|
||||
|
||||
import com.safemobile.lib.AppParams;
|
||||
import com.safemobile.lib.SM;
|
||||
@ -24,6 +25,7 @@ import java.util.TimerTask;
|
||||
|
||||
|
||||
public class TCPhandler implements Runnable {
|
||||
private final String TAG = "TCPhandler";
|
||||
private boolean alive = true;
|
||||
|
||||
public String serverHostname;
|
||||
@ -56,18 +58,22 @@ public class TCPhandler implements Runnable {
|
||||
|
||||
// create timer to check socket status
|
||||
Timer timer = new Timer();
|
||||
timer.scheduleAtFixedRate(new TimerTask() {
|
||||
@Override
|
||||
public void run() {
|
||||
try {
|
||||
previousConnectionWasUP = isConnectionUP;
|
||||
// try to send something
|
||||
TCPmsgParser._fireonTCPConnectionStatusEvent(isConnectionUP, previousConnectionWasUP);
|
||||
} catch (Exception e) {
|
||||
SM.Exception("TIMERException", e.toString());
|
||||
try {
|
||||
timer.scheduleAtFixedRate(new TimerTask() {
|
||||
@Override
|
||||
public void run() {
|
||||
try {
|
||||
previousConnectionWasUP = isConnectionUP;
|
||||
// try to send something
|
||||
TCPmsgParser._fireonTCPConnectionStatusEvent(isConnectionUP, previousConnectionWasUP);
|
||||
} catch (Exception e) {
|
||||
SM.Exception("TIMERException", e.toString());
|
||||
}
|
||||
}
|
||||
}
|
||||
}, 0, 3000);
|
||||
}, 0, 3000);
|
||||
} catch(Exception e) {
|
||||
Log.v("Exception", e.getMessage());
|
||||
}
|
||||
|
||||
// get WiFi state
|
||||
isWiFiOn = isNetworkConnected();
|
||||
@ -104,6 +110,8 @@ public class TCPhandler implements Runnable {
|
||||
SM.Debug("IllegalArgumentException", "TCPhandler break:"+ e);
|
||||
} catch (IOException e) {
|
||||
SM.Debug("IOException", "TCPhandler break:"+ e);
|
||||
} catch (Exception e) {
|
||||
Log.v("Exception", e.getMessage());
|
||||
}
|
||||
|
||||
while (alive) {
|
||||
@ -111,6 +119,8 @@ public class TCPhandler implements Runnable {
|
||||
Thread.sleep(3000);
|
||||
} catch (InterruptedException e) {
|
||||
SM.Debug("TCPhandler Crash1 on sleep:"+ e);
|
||||
} catch (Exception e) {
|
||||
Log.v("Exception", e.getMessage());
|
||||
}
|
||||
|
||||
while (Boolean.TRUE.equals(isConnectionUP)) {
|
||||
@ -242,6 +252,8 @@ public class TCPhandler implements Runnable {
|
||||
Thread.sleep(1000);
|
||||
} catch (InterruptedException e) {
|
||||
SM.Debug("TCPhandler Crash2 on sleep:"+ e);
|
||||
} catch (Exception e) {
|
||||
Log.v("Exception", e.getMessage());
|
||||
}
|
||||
//try to restart connection
|
||||
if (alive && isWiFiOn)
|
||||
@ -350,6 +362,7 @@ public class TCPhandler implements Runnable {
|
||||
if (soc != null)
|
||||
soc.close();
|
||||
} catch (IOException e1) {
|
||||
Log.v("IOException", e1.getMessage());
|
||||
e1.printStackTrace();
|
||||
}
|
||||
|
||||
@ -371,14 +384,15 @@ public class TCPhandler implements Runnable {
|
||||
|
||||
private void restartTCP() {
|
||||
try {
|
||||
Log.v(TAG, "restartTCP");
|
||||
isConnectionUP = false;
|
||||
previousConnectionWasUP = false;
|
||||
|
||||
SM.Debug("Restarting TCP...ip:" + serverHostname + ":" + port);
|
||||
soc = new Socket();
|
||||
soc.connect(new InetSocketAddress(serverHostname, port), 5000);
|
||||
|
||||
soc.connect(new InetSocketAddress(serverHostname, port), 50000);
|
||||
input = new DataInputStream(soc.getInputStream());
|
||||
|
||||
//output stream
|
||||
output = new DataOutputStream(soc.getOutputStream());
|
||||
|
||||
@ -386,17 +400,11 @@ public class TCPhandler implements Runnable {
|
||||
isConnectionUP = true;
|
||||
}
|
||||
|
||||
} catch (UnknownHostException e) {
|
||||
SM.Exception("restartTCP break:" + e.toString());
|
||||
} catch (NullPointerException | IOException e) {
|
||||
SM.Exception("restartTCP break:" + e);
|
||||
isConnectionUP = false;
|
||||
} catch (IllegalArgumentException e) {
|
||||
SM.Debug("IllegalArgumentException", "restartTCP break:" + e.toString());
|
||||
} catch (IOException e) {
|
||||
SM.Exception("restartTCP break:" + e.toString());
|
||||
isConnectionUP = false;
|
||||
} catch (NullPointerException e) {
|
||||
SM.Exception("restartTCP break:" + e.toString());
|
||||
isConnectionUP = false;
|
||||
SM.Debug("IllegalArgumentException", "restartTCP break:" + e);
|
||||
}
|
||||
triggerTCPConnectionStateEvent();
|
||||
}
|
||||
@ -410,6 +418,7 @@ public class TCPhandler implements Runnable {
|
||||
soc.close();
|
||||
soc = null;
|
||||
} catch (IOException e) {
|
||||
Log.v("IOException", e.getMessage());
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
@ -107,7 +107,6 @@ public class TcpAudioClient implements Runnable {
|
||||
recv = soc.getInputStream();
|
||||
writer = soc.getOutputStream();
|
||||
setConnOK(true);
|
||||
|
||||
} catch (IOException e) {
|
||||
SM.Debug("break:" + e);
|
||||
}
|
||||
|
@ -10,6 +10,7 @@ import android.graphics.Typeface;
|
||||
import android.os.Bundle;
|
||||
import android.os.Handler;
|
||||
import android.os.Looper;
|
||||
import android.util.Log;
|
||||
import android.view.View;
|
||||
import android.widget.ArrayAdapter;
|
||||
import android.widget.Button;
|
||||
@ -45,6 +46,7 @@ public class MessagesActivity extends Activity {
|
||||
/**
|
||||
* Called when the activity is first created.
|
||||
*/
|
||||
private final String TAG = "MessagesActivity";
|
||||
|
||||
private MessagesGridViewAdapter mAdapter;
|
||||
private ConversationGridViewAdapter convAdapter;
|
||||
@ -138,6 +140,7 @@ public class MessagesActivity extends Activity {
|
||||
|
||||
// Implement On Item click listener
|
||||
gridView.setOnItemClickListener((arg0, arg1, position, arg3) -> {
|
||||
Log.v(TAG, "onItemClick " + LASTMESSAGES);
|
||||
if (LASTMESSAGES) {
|
||||
// get position
|
||||
Msg item = mAdapter.getItem(position);
|
||||
@ -196,6 +199,7 @@ public class MessagesActivity extends Activity {
|
||||
imageButtonBack.setOnClickListener(v -> {
|
||||
// set LastMessage conversation type
|
||||
LASTMESSAGES = true;
|
||||
Log.v(TAG, "lastmessages: true");
|
||||
// refresh Grid
|
||||
getLastSMS();
|
||||
if (AppParams.DEMO)
|
||||
@ -388,6 +392,7 @@ public class MessagesActivity extends Activity {
|
||||
listLastMessages.add(new Msg(sentVehicle, txt, Calendar.getInstance().getTime(), sendSMSSeqID));
|
||||
}
|
||||
SM.Debug("time: " + timeGMT + " | " + Calendar.getInstance().getTime());
|
||||
Log.v(TAG, "lastmessages: false");
|
||||
LASTMESSAGES = false;
|
||||
|
||||
updateResultsInUi();
|
||||
@ -627,6 +632,7 @@ public class MessagesActivity extends Activity {
|
||||
setScId(item.sc_id);
|
||||
SM.Debug("Selected scId: " + getScId());
|
||||
// set Conversation type
|
||||
Log.v(TAG, "lastmessages: false");
|
||||
LASTMESSAGES = false;
|
||||
// call get SMS
|
||||
getSMS4unit(getScId());
|
||||
|
@ -73,6 +73,7 @@ import android.content.pm.PackageManager;
|
||||
import android.content.res.AssetManager;
|
||||
import android.content.res.Configuration;
|
||||
import android.content.res.Resources;
|
||||
import android.net.ConnectivityManager;
|
||||
import android.net.Uri;
|
||||
import android.os.AsyncTask;
|
||||
import android.os.Build;
|
||||
@ -101,6 +102,7 @@ import androidx.core.app.NotificationManagerCompat;
|
||||
|
||||
public class TabLayoutActivity extends AbstractSDParentActivity {
|
||||
|
||||
private final String TAG = "TabLayoutActivity";
|
||||
/* Misc */
|
||||
private Resources res;
|
||||
private Context context;
|
||||
@ -179,6 +181,9 @@ public class TabLayoutActivity extends AbstractSDParentActivity {
|
||||
private static final String SETTINGS = "Setup";
|
||||
private static final String ABOUT = "SafeMobile";
|
||||
|
||||
private final Handler uiHandler = new Handler(Looper.getMainLooper());
|
||||
|
||||
|
||||
// default app language
|
||||
public static final String DATABASE_LANGUAGE = "en"; // database language : en, de, tr, ro or empty
|
||||
|
||||
@ -602,6 +607,7 @@ public class TabLayoutActivity extends AbstractSDParentActivity {
|
||||
Vehicle vehicle = getVehicle4Imei(getImei());
|
||||
if (vehicle != null)
|
||||
getMessageActivity().setScId(vehicle.sc_id);
|
||||
Log.v(TAG, "lastmessages: false");
|
||||
getMessageActivity().LASTMESSAGES = false;
|
||||
getMessageActivity().getLastSMS();
|
||||
|
||||
@ -694,6 +700,19 @@ public class TabLayoutActivity extends AbstractSDParentActivity {
|
||||
|
||||
@Override
|
||||
public void whenBackPressed(AppParams.ActivityResult result) {
|
||||
try {
|
||||
prepareForLogOut(result);
|
||||
|
||||
finish();
|
||||
android.os.Process.killProcess(android.os.Process.myPid());
|
||||
System.exit(0);
|
||||
} catch (Exception e) {
|
||||
Log.v(TAG, e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
private void prepareForLogOut(AppParams.ActivityResult result) {
|
||||
Log.v(TAG, "prepareForLogOut");
|
||||
if (tcpTimer != null) {
|
||||
tcpTimer.cancel();
|
||||
tcpTimer.purge();
|
||||
@ -721,10 +740,6 @@ public class TabLayoutActivity extends AbstractSDParentActivity {
|
||||
|
||||
getIntent().putExtra("result", result);
|
||||
setResult(RESULT_OK, getIntent()); //-> used for exit
|
||||
|
||||
finish();
|
||||
android.os.Process.killProcess(android.os.Process.myPid());
|
||||
System.exit(0);
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -2037,8 +2052,50 @@ public class TabLayoutActivity extends AbstractSDParentActivity {
|
||||
|
||||
@Override
|
||||
public void onTCPConnectionDown(boolean previousWasConnectionUp) {
|
||||
Timer timer = new Timer();
|
||||
timer.schedule(new TimerTask() {
|
||||
@Override
|
||||
public void run() {
|
||||
boolean isNetworkConnected = isNetworkConnected();
|
||||
Log.v(TAG, "isNetworkConnected: " + isNetworkConnected);
|
||||
if (isNetworkConnected) {
|
||||
if (tcp != null && !tcp.isConnectionUP) {
|
||||
tcp.Stop();
|
||||
// if (tcpParser != null)
|
||||
// tcpParser.clearMsgList();
|
||||
tcp = null;
|
||||
|
||||
uiHandler.post(() -> {
|
||||
if (myService != null) {
|
||||
myService.stopTCPConnection();
|
||||
|
||||
|
||||
myService.recreateTCPConnection();
|
||||
|
||||
getApplicationContext().bindService(new Intent(context, TCPService.class), serviceConnection, Context.BIND_AUTO_CREATE);
|
||||
if (getRadioActivity() != null)
|
||||
getRadioActivity().onCreate(getRadioActivity().savedInstanceState);
|
||||
if (getMessageActivity() != null)
|
||||
getMessageActivity().onCreate(getMessageActivity().getSavedInstanceState());
|
||||
if (getRecordingsActivity() != null)
|
||||
getRecordingsActivity().onCreate(getRecordingsActivity().getSavedInstanceState());
|
||||
if (getAlarmActivity() != null)
|
||||
getAlarmActivity().onCreate(getAlarmActivity().getSavedInstanceState());
|
||||
|
||||
uiHandler.post(initTCPRUN);
|
||||
}
|
||||
SM.Debug("RECREATE TCP", "IP: " + AppParams.IP + " | Port: " + AppParams.PORT);
|
||||
});
|
||||
}
|
||||
|
||||
this.cancel();
|
||||
}
|
||||
}
|
||||
}, 0, 100);
|
||||
|
||||
prepareForLogOut(AppParams.ActivityResult.tcpDown);
|
||||
// execute logout
|
||||
whenBackPressed(AppParams.ActivityResult.tcpDown);
|
||||
// whenBackPressed(AppParams.ActivityResult.tcpDown);
|
||||
|
||||
// send a broadcast
|
||||
notifyBroadcast(OperationCodes.TCP_CONNECTION_DOWN + "");
|
||||
@ -2062,6 +2119,19 @@ public class TabLayoutActivity extends AbstractSDParentActivity {
|
||||
});
|
||||
}
|
||||
|
||||
// Create runnable for posting
|
||||
final Runnable initTCPRUN = () -> {
|
||||
// start thread to add listener
|
||||
SM.Debug("##### initTCPRUN");
|
||||
if (!AppParams.DEMO)
|
||||
tcpInit();
|
||||
};
|
||||
|
||||
private boolean isNetworkConnected() {
|
||||
ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
|
||||
return cm.getActiveNetworkInfo() != null && cm.getActiveNetworkInfo().isConnected();
|
||||
}
|
||||
|
||||
// Create runnable for posting
|
||||
final Runnable updateResultsAlarm = () -> updateResultsAlarmInUi("realpha");
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user