|
1 | 1 | package com.webileapps.safeguard;
|
2 | 2 |
|
3 | 3 | import android.Manifest;
|
| 4 | +import android.app.Activity; |
4 | 5 | import android.app.ActivityManager;
|
5 | 6 | import android.app.AlertDialog;
|
6 | 7 | import android.content.Context;
|
@@ -344,52 +345,64 @@ private void handleCallStateChange(int state) {
|
344 | 345 | }
|
345 | 346 |
|
346 | 347 | private void showNextDialog(Context context) {
|
347 |
| - if (isShowingDialog || dialogQueue.isEmpty()) { |
348 |
| - return; |
349 |
| - } |
| 348 | + if (isShowingDialog || dialogQueue.isEmpty()) return; |
350 | 349 |
|
351 |
| - isShowingDialog = true; |
352 |
| - SecurityDialogInfo dialogInfo = dialogQueue.remove(0); |
| 350 | + // Ensure this runs on UI thread |
| 351 | + new Handler(Looper.getMainLooper()).post(() -> { |
| 352 | + if (!(context instanceof Activity)) { |
| 353 | + return; |
| 354 | + } |
353 | 355 |
|
354 |
| - // Use dialog options directly from config |
355 |
| - String title = dialogInfo.isCritical ? config.getCriticalDialogTitle() : config.getWarningDialogTitle(); |
356 |
| - String positiveButton = dialogInfo.isCritical ? config.getCriticalDialogPositiveButton() : config.getWarningDialogPositiveButton(); |
357 |
| - String negativeButton = dialogInfo.isCritical ? config.getCriticalDialogNegativeButton() : config.getWarningDialogNegativeButton(); |
| 356 | + Activity activity = (Activity) context; |
358 | 357 |
|
359 |
| - AlertDialog.Builder builder = new AlertDialog.Builder(context) |
360 |
| - .setTitle(title) |
361 |
| - .setMessage(dialogInfo.message) |
362 |
| - .setPositiveButton(positiveButton, (dialogInterface, which) -> { |
363 |
| - dialogInterface.dismiss(); |
364 |
| - if (dialogInfo.isCritical) { |
365 |
| - System.exit(0); |
366 |
| - } else { |
367 |
| - if (dialogInfo.onResponse != null) { |
368 |
| - dialogInfo.onResponse.accept(true); |
369 |
| - } |
| 358 | + // Check if activity is in valid state |
| 359 | + if (activity.isFinishing() || activity.isDestroyed()) { |
| 360 | + return; |
| 361 | + } |
| 362 | + |
| 363 | + isShowingDialog = true; |
| 364 | + SecurityDialogInfo dialogInfo = dialogQueue.remove(0); |
| 365 | + |
| 366 | + String title = dialogInfo.isCritical ? config.getCriticalDialogTitle() : config.getWarningDialogTitle(); |
| 367 | + String positiveButton = dialogInfo.isCritical ? config.getCriticalDialogPositiveButton() : config.getWarningDialogPositiveButton(); |
| 368 | + String negativeButton = dialogInfo.isCritical ? config.getCriticalDialogNegativeButton() : config.getWarningDialogNegativeButton(); |
| 369 | + |
| 370 | + AlertDialog.Builder builder = new AlertDialog.Builder(activity) |
| 371 | + .setTitle(title) |
| 372 | + .setMessage(dialogInfo.message) |
| 373 | + .setPositiveButton(positiveButton, (dialogInterface, which) -> { |
| 374 | + dialogInterface.dismiss(); |
| 375 | + if (dialogInfo.isCritical) { |
| 376 | + System.exit(0); |
| 377 | + } else { |
| 378 | + if (dialogInfo.onResponse != null) { |
| 379 | + dialogInfo.onResponse.accept(true); |
| 380 | + } |
| 381 | + isShowingDialog = false; |
| 382 | + showNextDialog(activity); |
| 383 | + } |
| 384 | + }) |
| 385 | + .setCancelable(!dialogInfo.isCritical); |
| 386 | + |
| 387 | + if (negativeButton != null && !negativeButton.isEmpty()) { |
| 388 | + builder.setNegativeButton(negativeButton, (dialogInterface, which) -> { |
| 389 | + dialogInterface.dismiss(); |
370 | 390 | isShowingDialog = false;
|
371 |
| - showNextDialog(context); |
| 391 | + showNextDialog(activity); |
| 392 | + }); |
| 393 | + } |
| 394 | + |
| 395 | + AlertDialog dialog = builder.create(); |
| 396 | + |
| 397 | + dialog.setOnDismissListener(dialogInterface -> { |
| 398 | + if (!dialogInfo.isCritical) { |
| 399 | + isShowingDialog = false; |
| 400 | + showNextDialog(activity); |
372 | 401 | }
|
373 |
| - }) |
374 |
| - .setCancelable(!dialogInfo.isCritical); |
375 |
| - if (negativeButton != null && !negativeButton.isEmpty()) { |
376 |
| - builder.setNegativeButton(negativeButton, (dialogInterface, which) -> { |
377 |
| - dialogInterface.dismiss(); |
378 |
| - // Optionally handle negative button click here if needed |
379 |
| - isShowingDialog = false; |
380 |
| - showNextDialog(context); |
381 | 402 | });
|
382 |
| - } |
383 |
| - AlertDialog dialog = builder.create(); |
384 | 403 |
|
385 |
| - dialog.setOnDismissListener(dialogInterface -> { |
386 |
| - if (!dialogInfo.isCritical) { |
387 |
| - isShowingDialog = false; |
388 |
| - showNextDialog(context); |
389 |
| - } |
| 404 | + dialog.show(); |
390 | 405 | });
|
391 |
| - |
392 |
| - dialog.show(); |
393 | 406 | }
|
394 | 407 |
|
395 | 408 | public void showSecurityDialog(Context context, String message, boolean isCritical, Consumer<Boolean> onResponse) {
|
|
0 commit comments