Skip to content

Commit

Permalink
Fix incorrect connection reset on startup
Browse files Browse the repository at this point in the history
  • Loading branch information
erasmospunk committed Apr 16, 2016
1 parent 87da17d commit ef90006
Showing 1 changed file with 26 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@
public class CoinServiceImpl extends Service implements CoinService {
private WalletApplication application;
private Configuration config;
private ConnectivityManager connManager;
private ConnectivityHelper connHelper;
private BroadcastReceiver connectivityReceiver;

@CheckForNull
private ServerClients clients;
Expand Down Expand Up @@ -161,27 +161,25 @@ public class CoinServiceImpl extends Service implements CoinService {
// }
//

private final BroadcastReceiver connectivityReceiver = new BroadcastReceiver() {
private class MyBroadcastReceiver extends BroadcastReceiver {
private final ConnectivityManager connectivityManager;
private boolean hasConnectivity;
private boolean hasStorage = true;
private int currentNetworkType = -1;

public MyBroadcastReceiver(ConnectivityManager connectivityManager) {
this.connectivityManager = connectivityManager;
checkNetworkType();
}

@Override
public void onReceive(final Context context, final Intent intent) {
final String action = intent.getAction();

if (ConnectivityManager.CONNECTIVITY_ACTION.equals(action)) {
hasConnectivity = !intent.getBooleanExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY, false);

NetworkInfo activeInfo = connManager.getActiveNetworkInfo();
boolean isNetworkChanged;
if (activeInfo != null && activeInfo.isConnected()) {
isNetworkChanged = currentNetworkType != activeInfo.getType();
currentNetworkType = activeInfo.getType();
} else {
isNetworkChanged = false;
currentNetworkType = -1;
}
boolean isNetworkChanged = checkNetworkType();
log.info("network is " + (hasConnectivity ? "up" : "down"));
log.info("network type " + (isNetworkChanged ? "changed" : "didn't change"));

Expand All @@ -199,7 +197,20 @@ public void onReceive(final Context context, final Intent intent) {
}
}

// @SuppressLint("Wakelock")
private boolean checkNetworkType() {
boolean isNetworkChanged;
NetworkInfo activeInfo = connectivityManager.getActiveNetworkInfo();
if (activeInfo != null && activeInfo.isConnected()) {
isNetworkChanged = currentNetworkType != activeInfo.getType();
currentNetworkType = activeInfo.getType();
} else {
isNetworkChanged = false;
currentNetworkType = -1;
}
return isNetworkChanged;
}

// @SuppressLint("Wakelock")
private void check(boolean isNetworkChanged) {
Wallet wallet = application.getWallet();
final boolean hasEverything = hasConnectivity && hasStorage && (wallet != null);
Expand All @@ -210,7 +221,7 @@ private void check(boolean isNetworkChanged) {

log.info("Creating coins clients");
clients = getServerClients(wallet);
if (lastAccount != null) clients.startAsync(wallet.getAccount(lastAccount));
// if (lastAccount != null) clients.startAsync(wallet.getAccount(lastAccount));
} else if (hasEverything && isNetworkChanged) {
log.info("Restarting coins clients as network changed");
clients.resetConnections();
Expand Down Expand Up @@ -290,9 +301,10 @@ public void onCreate() {

application = (WalletApplication) getApplication();
config = application.getConfiguration();
connManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
ConnectivityManager connManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
connHelper = getConnectivityHelper(connManager);

connectivityReceiver = new MyBroadcastReceiver(connManager);
final IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction(ConnectivityManager.CONNECTIVITY_ACTION);
intentFilter.addAction(Intent.ACTION_DEVICE_STORAGE_LOW);
Expand Down

0 comments on commit ef90006

Please sign in to comment.