Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions app/src/main/java/in/testpress/testpress/ui/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
import android.accounts.Account;
import android.accounts.AccountManager;
import android.accounts.OperationCanceledException;
import android.app.ActivityManager;
import android.content.ComponentName;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
Expand Down Expand Up @@ -689,6 +691,9 @@ public void onResume() {
}

public void openEnforceDataActivity(){
if (isEnforceDataActivityOnTop()){
return;
}
this.startActivity(
EnforceDataActivity.Companion.createIntent(
this,
Expand All @@ -701,6 +706,14 @@ public void openEnforceDataActivity(){
);
}

public boolean isEnforceDataActivityOnTop() {
ActivityManager am = (ActivityManager) this.getSystemService(Context.ACTIVITY_SERVICE);
List<ActivityManager.RunningTaskInfo> taskInfo = am.getRunningTasks(1);
ComponentName topActivity = taskInfo.get(0).topActivity;
return topActivity != null && topActivity.getClassName().contains("EnforceDataActivity");
}
Comment on lines +709 to +714

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

The method isEnforceDataActivityOnTop has a few issues that should be addressed:

  • Potential Crash: am.getRunningTasks(1) can return an empty or null list, which would cause an IndexOutOfBoundsException on taskInfo.get(0). You should add a null/empty check for the returned list.
  • Deprecated and Unreliable API: ActivityManager.getRunningTasks() has been deprecated since API level 21 and its use is discouraged for application logic. It might not work as expected on newer Android versions and can even break in future releases. A more reliable approach would be to use ActivityLifecycleCallbacks or a flag within your application to track whether EnforceDataActivity is active.
  • Fragile Class Name Check: Using String.contains() to check the activity's class name is not robust. It's better to use an exact match with the fully qualified class name: EnforceDataActivity.class.getName().

Here's a suggested improvement that addresses the crash and the class name check. I strongly recommend replacing the deprecated API for a more robust long-term solution.

    public boolean isEnforceDataActivityOnTop() {
        ActivityManager am = (ActivityManager) this.getSystemService(Context.ACTIVITY_SERVICE);
        List<ActivityManager.RunningTaskInfo> taskInfo = am.getRunningTasks(1);
        if (taskInfo == null || taskInfo.isEmpty()) {
            return false;
        }
        ComponentName topActivity = taskInfo.get(0).topActivity;
        return topActivity != null && topActivity.getClassName().equals(EnforceDataActivity.class.getName());
    }



public void checkForForceUserData() {
new SafeAsyncTask<CheckPermission>() {
@Override
Expand Down