Skip to content

Commit baf9b3b

Browse files
Merge pull request #146 from appdevforall/feat/ADFA-4578-server-state-observers
ADFA-4578 (slice 2): observe ServerStateRepository; drop the public server fields
2 parents e4d76d7 + 5471d76 commit baf9b3b

11 files changed

Lines changed: 29 additions & 39 deletions

File tree

‎controller/app/src/main/java/org/iiab/controller/DashboardFragment.java‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -478,7 +478,6 @@ private void renderStatus(DashboardStatus status) {
478478
currentSystemState = evaluateSystemState(isMainServerAlive);
479479

480480
if (getActivity() instanceof MainActivity) {
481-
((MainActivity) getActivity()).currentSystemState = currentSystemState;
482481
((MainActivity) getActivity()).updateUIColorsAndVisibility();
483482
}
484483

‎controller/app/src/main/java/org/iiab/controller/DeployFragment.java‎

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -274,9 +274,6 @@ public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceStat
274274
new Thread(() -> {
275275
boolean isAlive = pingUrl("http://localhost:8085/home");
276276
checkInternetAccess();
277-
if (getActivity() instanceof MainActivity) {
278-
((MainActivity) getActivity()).isServerAlive = isAlive;
279-
}
280277
if (isAdded() && getActivity() != null) {
281278
getActivity().runOnUiThread(this::updateDynamicButtons);
282279
}
@@ -561,7 +558,7 @@ public void updateDynamicButtons() {
561558
MainActivity mainAct = (MainActivity) getActivity();
562559
if (mainAct == null || !isAdded()) return;
563560

564-
boolean isServerRunning = mainAct.isServerAlive;
561+
boolean isServerRunning = ServerStateRepository.get().current().alive;
565562
final File iiabRootDir = new File(requireContext().getFilesDir(), "rootfs");
566563
final File debianRootfs = new File(iiabRootDir, "installed-rootfs/iiab");
567564
final File backupsDir = new File(iiabRootDir, "backups");

‎controller/app/src/main/java/org/iiab/controller/MainActivity.java‎

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -70,10 +70,8 @@ public class MainActivity extends AppCompatActivity implements TerminalControlle
7070
private TabLayout tabLayout;
7171
private ViewPager2 viewPager;
7272
private TextView versionFooter;
73-
public boolean isServerAlive = false;
7473
private long serverUpSinceMs = 0L; // ADFA-4466: for server_stopped uptime bucket
7574
public boolean isNegotiating = false;
76-
public DashboardFragment.SystemState currentSystemState = DashboardFragment.SystemState.NONE;
7775
public Boolean targetServerState = null;
7876
public String serverTransitionText = "";
7977
public UsageFragment usageFragment;
@@ -134,15 +132,16 @@ public void invalidateModuleStateTrust() {
134132
* otherwise). server_stopped carries a coarse uptime bucket, never an exact duration.
135133
*/
136134
private void updateServerAlive(boolean nowAlive) {
137-
if (nowAlive && !isServerAlive) {
135+
boolean wasAlive = ServerStateRepository.get().current().alive;
136+
if (nowAlive && !wasAlive) {
138137
serverUpSinceMs = System.currentTimeMillis();
139138
org.iiab.controller.analytics.AnalyticsClient.with(this).logServerStarted();
140-
} else if (!nowAlive && isServerAlive) {
139+
} else if (!nowAlive && wasAlive) {
141140
long uptime = serverUpSinceMs > 0L ? System.currentTimeMillis() - serverUpSinceMs : -1L;
142141
serverUpSinceMs = 0L;
143142
org.iiab.controller.analytics.AnalyticsClient.with(this).logServerStopped(uptime);
144143
}
145-
isServerAlive = nowAlive;
144+
// The repository is updated by the poll (checkServerStatus) right after this.
146145
}
147146

148147
public boolean isModuleStateTrusted() {
@@ -408,7 +407,7 @@ public boolean onTouch(View v, MotionEvent event) {
408407

409408
// --- QR Share Button Logic ---
410409
btnShareQr.setOnClickListener(v -> {
411-
if (!isServerAlive) {
410+
if (!ServerStateRepository.get().current().alive) {
412411
if (viewPager != null) {
413412
viewPager.setCurrentItem(1, true);
414413
}
@@ -617,7 +616,7 @@ public void onClick(View view) {
617616
}
618617

619618
public void handleBrowseContentClick(View v) {
620-
if (!isServerAlive) {
619+
if (!ServerStateRepository.get().current().alive) {
621620
Snackbar.make(v, R.string.qr_error_no_server, Snackbar.LENGTH_LONG).show();
622621
return;
623622
}
@@ -689,7 +688,7 @@ public void handleServerLaunchClick(View v) {
689688

690689
File rootfsDir = new File(getFilesDir(), "rootfs/installed-rootfs/iiab");
691690

692-
if (!isServerAlive) {
691+
if (!ServerStateRepository.get().current().alive) {
693692
addToLog(getString(R.string.log_server_booting_native));
694693
// kernel & uptime
695694
createFakeSysData(rootfsDir);
@@ -727,7 +726,7 @@ public void onError(String error) {
727726

728727
// Fallback for Oppo/Xiaomi: Notify user if server fails to start
729728
new Handler(android.os.Looper.getMainLooper()).postDelayed(() -> {
730-
if (targetServerState != null && !isServerAlive) {
729+
if (targetServerState != null && !ServerStateRepository.get().current().alive) {
731730
Snackbar.make(v, R.string.termux_stuck_warning, Snackbar.LENGTH_LONG).show();
732731
}
733732
}, getResources().getInteger(R.integer.server_snackbar_delay_ms));
@@ -795,23 +794,19 @@ private void checkServerStatus() {
795794

796795
// ADFA-4578: evaluate + publish the SystemState at app level (every poll),
797796
// so every tab reflects the server live instead of only after visiting the
798-
// Dashboard. Mirror it onto currentSystemState for the current (field) readers.
799797
final DashboardFragment.SystemState sysState = SystemStateEvaluator.evaluate(MainActivity.this, localAlive);
800798
ServerStateRepository.get().post(ServerState.of(localAlive, sysState));
801799

802800
// STATE MACHINE: Has the target state been reached?
803-
if (targetServerState != null && isServerAlive == targetServerState) {
801+
if (targetServerState != null && ServerStateRepository.get().current().alive == targetServerState) {
804802
targetServerState = null; // Transition complete!
805803
timeoutHandler.removeCallbacks(timeoutRunnable); // Cancel safety net
806804
if (usageFragment != null) runOnUiThread(() -> usageFragment.stopBtnProgress());
807805
}
808806

809807
currentTargetUrl = localAlive ? "http://localhost:8085/home" : null;
810808

811-
runOnUiThread(() -> {
812-
currentSystemState = sysState;
813-
updateUIColorsAndVisibility();
814-
});
809+
runOnUiThread(this::updateUIColorsAndVisibility);
815810
});
816811
}
817812

‎controller/app/src/main/java/org/iiab/controller/SyncFragment.java‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -213,8 +213,7 @@ public boolean isShareMode() {
213213
// --- ShareHost (ADFA-4506) ---------------------------------------------
214214
@Override
215215
public boolean isServerAlive() {
216-
MainActivity a = (MainActivity) getActivity();
217-
return a != null && a.isServerAlive;
216+
return ServerStateRepository.get().current().alive;
218217
}
219218

220219
@Override

‎controller/app/src/main/java/org/iiab/controller/UsageFragment.java‎

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,7 @@ public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceStat
178178

179179
btnServerControl.setOnClickListener(v -> {
180180
// --- Intercept based on State Machine ---
181-
DashboardFragment.SystemState state = mainActivity.currentSystemState;
181+
DashboardFragment.SystemState state = ServerStateRepository.get().current().systemState;
182182
boolean isFullyInstalled = (state == DashboardFragment.SystemState.ONLINE || state == DashboardFragment.SystemState.OFFLINE);
183183

184184
if (!isFullyInstalled) {
@@ -189,8 +189,8 @@ public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceStat
189189

190190
if (mainActivity.targetServerState != null) return;
191191

192-
mainActivity.serverTransitionText = !mainActivity.isServerAlive ? getString(R.string.server_booting) : getString(R.string.server_shutting_down);
193-
mainActivity.targetServerState = !mainActivity.isServerAlive;
192+
mainActivity.serverTransitionText = !ServerStateRepository.get().current().alive ? getString(R.string.server_booting) : getString(R.string.server_shutting_down);
193+
mainActivity.targetServerState = !ServerStateRepository.get().current().alive;
194194

195195
updateUIColorsAndVisibility();
196196
btnServerControl.startProgress();
@@ -239,7 +239,7 @@ public void updateUIColorsAndVisibility() {
239239

240240
// Explore Button
241241
button_browse_content.setVisibility(View.VISIBLE);
242-
if (!mainActivity.isServerAlive) {
242+
if (!ServerStateRepository.get().current().alive) {
243243
button_browse_content.setEnabled(true);
244244
button_browse_content.setBackgroundTintList(ContextCompat.getColorStateList(requireContext(), R.color.btn_explore_disabled));
245245
button_browse_content.setAlpha(1.0f);
@@ -255,7 +255,7 @@ public void updateUIColorsAndVisibility() {
255255
}
256256

257257
// Server Control Logic
258-
DashboardFragment.SystemState state = mainActivity.currentSystemState;
258+
DashboardFragment.SystemState state = ServerStateRepository.get().current().systemState;
259259
boolean isFullyInstalled = (state == DashboardFragment.SystemState.ONLINE || state == DashboardFragment.SystemState.OFFLINE);
260260

261261
if (!isFullyInstalled) {
@@ -271,7 +271,7 @@ public void updateUIColorsAndVisibility() {
271271
} else {
272272
// SYSTEM READY: Normal behavior
273273
btnServerControl.setAlpha(1.0f);
274-
if (mainActivity.isServerAlive) {
274+
if (ServerStateRepository.get().current().alive) {
275275
btnServerControl.setText(R.string.stop_server);
276276
btnServerControl.setBackgroundTintList(ContextCompat.getColorStateList(requireContext(), R.color.btn_danger));
277277
} else {

‎controller/app/src/main/java/org/iiab/controller/backup/presentation/BackupController.java‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ public void registerLaunchers() {
119119
private void bindBackupButtonLogic() {
120120
if (btnAdvancedBackup == null) return;
121121
btnAdvancedBackup.setOnClickListener(v -> {
122-
if (mainAct.isServerAlive) {
122+
if (org.iiab.controller.ServerStateRepository.get().current().alive) {
123123
Snackbar.make(v, R.string.install_msg_server_running_lock, Snackbar.LENGTH_LONG).show();
124124
return;
125125
}
@@ -426,7 +426,7 @@ private void refreshRestoreButtonLogic() {
426426
MainActivity mainAct = (MainActivity) fragment.getActivity();
427427
if (mainAct == null || btnAdvancedRestore == null) return;
428428

429-
if (mainAct.isServerAlive) {
429+
if (org.iiab.controller.ServerStateRepository.get().current().alive) {
430430
btnAdvancedRestore.setAlpha(0.5f);
431431
btnAdvancedRestore.setOnClickListener(v -> Snackbar.make(v, R.string.install_msg_server_running_lock, Snackbar.LENGTH_LONG).show());
432432
return;
@@ -438,7 +438,7 @@ private void refreshRestoreButtonLogic() {
438438
} else {
439439
btnAdvancedRestore.setAlpha(1.0f);
440440
btnAdvancedRestore.setOnClickListener(v -> {
441-
if (mainAct.isServerAlive) {
441+
if (org.iiab.controller.ServerStateRepository.get().current().alive) {
442442
Snackbar.make(v, R.string.install_msg_server_running_lock, Snackbar.LENGTH_LONG).show();
443443
return;
444444
}

‎controller/app/src/main/java/org/iiab/controller/install/presentation/InstallController.java‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ public void bind(MainActivity mainAct, File debianRootfs, File iiabRootDir,
8080
private void bindInstallButtonLogic() {
8181
btnFastInstall.setOnClickListener(v -> {
8282
// 1. Main Lock: Server On
83-
if (mainAct.isServerAlive) {
83+
if (org.iiab.controller.ServerStateRepository.get().current().alive) {
8484
Snackbar.make(v, R.string.install_msg_server_running_lock, Snackbar.LENGTH_LONG).show();
8585
return;
8686
}
@@ -188,7 +188,7 @@ private void evaluateLaunchButton() {
188188
if (hasSelections) {
189189
btnLaunchInstall.setOnClickListener(v -> {
190190
MainActivity mainAct = (MainActivity) fragment.getActivity();
191-
if (mainAct != null && mainAct.isServerAlive) {
191+
if (mainAct != null && org.iiab.controller.ServerStateRepository.get().current().alive) {
192192
Snackbar.make(v, R.string.install_msg_server_running_lock, Snackbar.LENGTH_LONG).show();
193193
return;
194194
}
@@ -294,7 +294,7 @@ public void verifyInstallationState(JSONObject jsonVars) {
294294
boolean pingState = isMainServerAlive && host.pingUrl("http://localhost:8085/" + module.endpoint);
295295

296296
MainActivity mainAct = (MainActivity) fragment.getActivity();
297-
boolean isRunning = mainAct != null && mainAct.isServerAlive;
297+
boolean isRunning = mainAct != null && org.iiab.controller.ServerStateRepository.get().current().alive;
298298
boolean isTrusted = mainAct != null && mainAct.isModuleStateTrusted();
299299

300300
boolean isConfirmedInstalled;

‎controller/app/src/main/java/org/iiab/controller/install/presentation/PlannerController.java‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ public void createModulesGrid() {
104104

105105
boolean isServerRunning = false;
106106
if (fragment.getActivity() instanceof MainActivity) {
107-
isServerRunning = ((MainActivity) fragment.getActivity()).isServerAlive;
107+
isServerRunning = org.iiab.controller.ServerStateRepository.get().current().alive;
108108
}
109109

110110
String termuxArch = host.getTermuxArch();

‎controller/app/src/main/java/org/iiab/controller/install/presentation/ResetDeleteController.java‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ private void bindResetButtonLogic() {
6868
return;
6969
}
7070

71-
if (mainAct.isServerAlive) {
71+
if (org.iiab.controller.ServerStateRepository.get().current().alive) {
7272
Snackbar.make(v, R.string.install_msg_server_running_lock, Snackbar.LENGTH_LONG).show();
7373
return;
7474
}
@@ -101,7 +101,7 @@ private void bindResetButtonLogic() {
101101

102102
private void bindDeleteButtonLogic() {
103103
btnFastDelete.setOnClickListener(v -> {
104-
if (mainAct.isServerAlive) {
104+
if (org.iiab.controller.ServerStateRepository.get().current().alive) {
105105
Snackbar.make(v, R.string.install_msg_server_running_lock, Snackbar.LENGTH_LONG).show();
106106
return;
107107
}

‎controller/app/src/main/java/org/iiab/controller/sync/presentation/ReceiveHost.java‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
package org.iiab.controller.sync.presentation;
1414

1515
public interface ReceiveHost {
16-
/** True when the embedded IIAB server is running (MainActivity.isServerAlive). */
16+
/** True when the embedded IIAB server is running (via the app-level ServerStateRepository). */
1717
boolean isServerAlive();
1818

1919
/** ADFA-4496 pre-flight: true when the phantom-process monitor is NOT active. */

0 commit comments

Comments
 (0)