Skip to content

Commit 87ff48f

Browse files
committed
Alert dialog issue fixed
1 parent 7bbc68f commit 87ff48f

File tree

1 file changed

+51
-38
lines changed

1 file changed

+51
-38
lines changed

protect/src/main/java/com/webileapps/safeguard/SecurityChecker.java

Lines changed: 51 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.webileapps.safeguard;
22

33
import android.Manifest;
4+
import android.app.Activity;
45
import android.app.ActivityManager;
56
import android.app.AlertDialog;
67
import android.content.Context;
@@ -344,52 +345,64 @@ private void handleCallStateChange(int state) {
344345
}
345346

346347
private void showNextDialog(Context context) {
347-
if (isShowingDialog || dialogQueue.isEmpty()) {
348-
return;
349-
}
348+
if (isShowingDialog || dialogQueue.isEmpty()) return;
350349

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+
}
353355

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;
358357

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();
370390
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);
372401
}
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);
381402
});
382-
}
383-
AlertDialog dialog = builder.create();
384403

385-
dialog.setOnDismissListener(dialogInterface -> {
386-
if (!dialogInfo.isCritical) {
387-
isShowingDialog = false;
388-
showNextDialog(context);
389-
}
404+
dialog.show();
390405
});
391-
392-
dialog.show();
393406
}
394407

395408
public void showSecurityDialog(Context context, String message, boolean isCritical, Consumer<Boolean> onResponse) {

0 commit comments

Comments
 (0)