handle network swap #32

Merged
CiufudeanDani merged 2 commits from SD-230 into develop 2022-05-03 08:27:34 +00:00
Showing only changes of commit 6fda464ed8 - Show all commits

View File

@ -74,6 +74,9 @@ import android.content.res.AssetManager;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.net.ConnectivityManager;
import android.net.Network;
import android.net.NetworkCapabilities;
import android.net.NetworkRequest;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Build;
@ -571,7 +574,66 @@ public class TabLayoutActivity extends AbstractSDParentActivity {
/* Create Service and bind to it */
getApplicationContext().bindService(new Intent(this, TCPService.class), serviceConnection, Context.BIND_AUTO_CREATE);
ConnectivityManager connectivityManager =
(ConnectivityManager) this.getSystemService(Context.CONNECTIVITY_SERVICE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N)
connectivityManager.registerDefaultNetworkCallback(networkCallback);
else {
NetworkRequest request = new NetworkRequest.Builder()
.addCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET).build();
connectivityManager.registerNetworkCallback(request, networkCallback);
cristi marked this conversation as resolved
Review

This is done on both branches, should be outside of if.

This is done on both branches, should be outside of `if`.
Review

On first branch it has one parameter and in the second one has two.

On first branch it has one parameter and in the second one has two.
}
}
private final ConnectivityManager.NetworkCallback networkCallback = new ConnectivityManager.NetworkCallback() {
@Override
public void onAvailable(Network network) {
// network available
Log.v(TAG, "network available");
Log.v(TAG, "tcp: " + (tcp != null));
// if (tcp == null)
// getApplicationContext().bindService(new Intent(context, TCPService.class), serviceConnection, Context.BIND_AUTO_CREATE);
// if (myService != null)
// tcp = myService.getTCPConnection();
if (tcp != null) {
if (tcp.isConnectionUP) {
tcp.isConnectionUP = false;
tcp.Stop();
}
// tcp = null;
uiHandler.post(() -> {
Log.v(TAG, "myService: " + (myService != null));
if (myService != null) {
myService.stopTCPConnection();
Review

Is this related to tcp.Stop() above? Is it independent? Why?

Is this related to `tcp.Stop()` above? Is it independent? Why?
myService.recreateTCPConnection();
getApplicationContext().bindService(new Intent(context, TCPService.class), serviceConnection, Context.BIND_AUTO_CREATE);
if (getRadioActivity() != null)
getRadioActivity().onCreate(getRadioActivity().savedInstanceState);
cristi marked this conversation as resolved
Review

Why is here savedInstanceState and getSavedInstanceState on the others?

Why is here `savedInstanceState` and `getSavedInstanceState` on the others?
Review

Because savedInstanceState is the name of the variable and we didn't have a getter. Now I've created one and I'm using it.

Because savedInstanceState is the name of the variable and we didn't have a getter. Now I've created one and I'm using it.
if (getMessageActivity() != null)
getMessageActivity().onCreate(getMessageActivity().getSavedInstanceState());
if (getRecordingsActivity() != null)
getRecordingsActivity().onCreate(getRecordingsActivity().getSavedInstanceState());
if (getAlarmActivity() != null)
getAlarmActivity().onCreate(getAlarmActivity().getSavedInstanceState());
uiHandler.post(initTCPRUN);
}
});
}
}
@Override
public void onLost(Network network) {
// network unavailable
Log.v(TAG, "network unavailable");
}
};
/**
* Broadcast Received for notifications
@ -2098,7 +2160,7 @@ public class TabLayoutActivity extends AbstractSDParentActivity {
// whenBackPressed(AppParams.ActivityResult.tcpDown);
// send a broadcast
notifyBroadcast(OperationCodes.TCP_CONNECTION_DOWN + "");
// notifyBroadcast(OperationCodes.TCP_CONNECTION_DOWN + "");
}
@Override