Skip to content

Commit

Permalink
Separate unrelated UI updates from updateRemotes
Browse files Browse the repository at this point in the history
  • Loading branch information
slowscript committed Sep 14, 2022
1 parent 2a7d946 commit 05bb56d
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 19 deletions.
5 changes: 5 additions & 0 deletions app/src/main/java/slowscript/warpinator/LocalBroadcasts.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ public class LocalBroadcasts {
public static final String ACTION_UPDATE_REMOTES = "update_remotes";
public static final String ACTION_UPDATE_TRANSFERS = "update_transfers";
public static final String ACTION_UPDATE_TRANSFER = "update_transfer";
public static final String ACTION_UPDATE_NETWORK = "update_network";
public static final String ACTION_DISPLAY_MESSAGE = "display_message";
public static final String ACTION_DISPLAY_TOAST = "display_toast";
public static final String ACTION_CLOSE_ALL = "close_all";
Expand All @@ -17,6 +18,10 @@ public static void updateRemotes(Context ctx) {
LocalBroadcastManager.getInstance(ctx).sendBroadcast(new Intent(ACTION_UPDATE_REMOTES));
}

public static void updateNetworkState(Context ctx) {
LocalBroadcastManager.getInstance(ctx).sendBroadcast(new Intent(ACTION_UPDATE_NETWORK));
}

public static void updateTransfers(Context ctx, String remote) {
Intent intent = new Intent(ACTION_UPDATE_TRANSFERS);
intent.putExtra("remote", remote);
Expand Down
10 changes: 10 additions & 0 deletions app/src/main/java/slowscript/warpinator/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -97,9 +97,11 @@ protected void onResume() {
startMainService();
});
updateRemoteList();
updateNetworkStateUI();

IntentFilter f = new IntentFilter();
f.addAction(LocalBroadcasts.ACTION_UPDATE_REMOTES);
f.addAction(LocalBroadcasts.ACTION_UPDATE_NETWORK);
f.addAction(LocalBroadcasts.ACTION_DISPLAY_MESSAGE);
f.addAction(LocalBroadcasts.ACTION_DISPLAY_TOAST);
f.addAction(LocalBroadcasts.ACTION_CLOSE_ALL);
Expand Down Expand Up @@ -161,6 +163,11 @@ private void updateRemoteList() {
recyclerView.post(() -> {
adapter.notifyDataSetChanged();
layoutNotFound.setVisibility(MainService.remotes.size() == 0 ? View.VISIBLE : View.INVISIBLE);
});
}

private void updateNetworkStateUI() {
runOnUiThread(() -> {
if (MainService.svc != null)
txtNoNetwork.setVisibility(MainService.svc.gotNetwork() ? View.GONE : View.VISIBLE);
if (Server.current != null)
Expand All @@ -180,6 +187,9 @@ public void onReceive(Context c, Intent intent) {
case LocalBroadcasts.ACTION_UPDATE_REMOTES:
updateRemoteList();
break;
case LocalBroadcasts.ACTION_UPDATE_NETWORK:
updateNetworkStateUI();
break;
case LocalBroadcasts.ACTION_DISPLAY_MESSAGE:
String title = intent.getStringExtra("title");
String msg = intent.getStringExtra("msg");
Expand Down
33 changes: 17 additions & 16 deletions app/src/main/java/slowscript/warpinator/MainService.java
Original file line number Diff line number Diff line change
Expand Up @@ -51,27 +51,28 @@ public class MainService extends Service {
static long reconnectTime = 30_000;
static long autoStopTime = 60_000;

public SharedPreferences prefs;
public int runningTransfers = 0;
public boolean networkAvailable = true;
public boolean networkAvailable = false;
public boolean apOn = false;
int notifId = 1300;
String lastIP = null;

public static MainService svc;
public static HashMap<String, Remote> remotes = new HashMap<>();
public static ArrayList<String> remotesOrder = new ArrayList<>();
public NotificationManagerCompat notificationMgr;
NotificationCompat.Builder notifBuilder = null;
Server server;
Timer timer;
SharedPreferences prefs;
ExecutorService executor = Executors.newCachedThreadPool();
Process logcatProcess;
WifiManager.MulticastLock lock;
ConnectivityManager connMgr;
ConnectivityManager.NetworkCallback networkCallback;
BroadcastReceiver apStateChangeReceiver;
TimerTask autoStopTask;
public NotificationManagerCompat notificationMgr;

private NotificationCompat.Builder notifBuilder = null;
private Server server;
private Timer timer;
private Process logcatProcess;
private WifiManager.MulticastLock lock;
private ConnectivityManager connMgr;
private ConnectivityManager.NetworkCallback networkCallback;
private BroadcastReceiver apStateChangeReceiver;
private TimerTask autoStopTask;

@Nullable
@Override
Expand Down Expand Up @@ -235,14 +236,14 @@ private void listenOnNetworkChanges() {
public void onAvailable(@NonNull Network network) {
Log.d(TAG, "New network");
networkAvailable = true;
LocalBroadcasts.updateRemotes(MainService.this);
LocalBroadcasts.updateNetworkState(MainService.this);
onNetworkChanged();
}
@Override
public void onLost(@NonNull Network network) {
Log.d(TAG, "Network lost");
networkAvailable = false;
LocalBroadcasts.updateRemotes(MainService.this);
LocalBroadcasts.updateNetworkState(MainService.this);
}
@Override
public void onLinkPropertiesChanged(@NonNull Network network, @NonNull LinkProperties linkProperties) {
Expand All @@ -257,12 +258,12 @@ public void onReceive(Context context, Intent intent) {
if (apState % 10 == WifiManager.WIFI_STATE_ENABLED) {
Log.d(TAG, "AP was enabled");
apOn = true;
LocalBroadcasts.updateRemotes(MainService.this);
LocalBroadcasts.updateNetworkState(MainService.this);
onNetworkChanged();
} else if (apState % 10 == WifiManager.WIFI_STATE_DISABLED) {
Log.d(TAG, "AP was disabled");
apOn = false;
LocalBroadcasts.updateRemotes(MainService.this);
LocalBroadcasts.updateNetworkState(MainService.this);
}
}
};
Expand Down
4 changes: 2 additions & 2 deletions app/src/main/java/slowscript/warpinator/Server.java
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ public void Start() {
CertServer.Start(port);
new Thread(this::startMDNS).start();
svc.prefs.registerOnSharedPreferenceChangeListener(preferenceChangeListener);
LocalBroadcasts.updateRemotes(svc);
LocalBroadcasts.updateNetworkState(svc);
}

public void Stop() {
Expand All @@ -101,7 +101,7 @@ public void Stop() {
svc.prefs.unregisterOnSharedPreferenceChangeListener(preferenceChangeListener);
if (gServer != null)
gServer.shutdownNow();
LocalBroadcasts.updateRemotes(svc);
LocalBroadcasts.updateNetworkState(svc);
Log.i(TAG, "--- Server stopped");
}

Expand Down
10 changes: 10 additions & 0 deletions app/src/main/java/slowscript/warpinator/ShareActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,9 @@ protected void onResume() {
if (adapter == null) // If onCreate did not finish (nothing to share)
return;
updateRemotes();
updateNetworkStateUI();
IntentFilter f = new IntentFilter(LocalBroadcasts.ACTION_UPDATE_REMOTES);
f.addAction(LocalBroadcasts.ACTION_UPDATE_NETWORK);
f.addAction(LocalBroadcasts.ACTION_DISPLAY_MESSAGE);
f.addAction(LocalBroadcasts.ACTION_DISPLAY_TOAST);
f.addAction(LocalBroadcasts.ACTION_CLOSE_ALL);
Expand All @@ -168,6 +170,9 @@ public void onReceive(Context context, Intent intent) {
case LocalBroadcasts.ACTION_UPDATE_REMOTES:
updateRemotes();
break;
case LocalBroadcasts.ACTION_UPDATE_NETWORK:
updateNetworkStateUI();
break;
case LocalBroadcasts.ACTION_DISPLAY_MESSAGE:
String title = intent.getStringExtra("title");
String msg = intent.getStringExtra("msg");
Expand All @@ -190,6 +195,11 @@ private void updateRemotes() {
recyclerView.post(() -> {
adapter.notifyDataSetChanged();
layoutNotFound.setVisibility(MainService.remotes.size() == 0 ? View.VISIBLE : View.INVISIBLE);
});
}

private void updateNetworkStateUI() {
runOnUiThread(() -> {
if (MainService.svc != null)
txtNoNetwork.setVisibility(MainService.svc.gotNetwork() ? View.GONE : View.VISIBLE);
if (Server.current != null)
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/java/slowscript/warpinator/Transfer.java
Original file line number Diff line number Diff line change
Expand Up @@ -312,7 +312,7 @@ public void prepareReceive() {
//Show in UI
if (remoteUUID.equals(TransfersActivity.topmostRemote))
LocalBroadcasts.updateTransfers(svc, remoteUUID);
else if (svc.server.notifyIncoming && !autoAccept) { //Notification
else if (Server.current.notifyIncoming && !autoAccept) { //Notification
Intent intent = new Intent(svc, TransfersActivity.class);
intent.putExtra("remote", remoteUUID);
int immutable = Build.VERSION.SDK_INT >= Build.VERSION_CODES.M ? PendingIntent.FLAG_IMMUTABLE : 0;
Expand Down

0 comments on commit 05bb56d

Please sign in to comment.