2

Hello I must be tired as I just can't seem to make this work, in my mind it makes sense. I have a String[] full of Android app names that I am trying to filter out before I write the results to a JSONArray.

 public static JSONArray appInfo = new JSONArray();
 public static String[] ignoreList = {"Google", "Maps", "Settings", "Phone", "Chrome", "Photos", "Duo", "YouTube", "Gmail", "Contacts", "Drive", "Clock", "Messages", "Files", "Calculator", "File manager", "Samsung", "Gallery", "LastPass", "OnePlus", "Weather"};

public void listApps(Context c) {

    PackageManager pm = c.getPackageManager();
    Intent main = new Intent(Intent.ACTION_MAIN, null);
    main.addCategory(Intent.CATEGORY_LAUNCHER);
    List<ResolveInfo> packages = pm.queryIntentActivities(main, 0);


    for (ResolveInfo resolve_info : packages) {
        try {
            String package_name = resolve_info.activityInfo.packageName;
            String app = (String) pm.getApplicationLabel(pm.getApplicationInfo(package_name, PackageManager.GET_META_DATA));

            for (String tempStr : ignoreList) {
                if (!app.contains(tempStr)) {
                    JSONObject json = new JSONObject(); // Create temp json object with app info.
                    json.put("app", app);
                    appInfo.put(json);
                }
            }

        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    try {
        Log.d("Debug", appInfo.toString(4));
    } catch (JSONException e) {
        e.printStackTrace();
    }

}

If I was to check;

if (!app.contains("Google")) {

etc.. Then my JSONArray would contain no app names with "Google" but when I iterate over the ignoreList I get the following JSON out which is from the ignoreList not the remaining items that are not in the ignorList? Also it keeps repeating the objects? Any help would be very welcomed.

    {
        "app": "Chrome"
    },
    {
        "app": "Chrome"
    },
    {
        "app": "Chrome"
    },
    {
        "app": "Chrome"
    },
    {
        "app": "Chrome"
    },
    {
        "app": "Chrome"
    },
    {
        "app": "Chrome"
    },
    {
        "app": "Chrome"
    },
    {
        "app": "Chrome"
    },
    {
        "app": "Chrome"
    },
    {
        "app": "Chrome"
    },
    {
        "app": "Chrome"
    },
    {
        "app": "Chrome"
    },
    {
        "app": "Chrome"
    },
    {
        "app": "Chrome"
    },
    {
        "app": "Chrome"
    },
    {
        "app": "Chrome"
    },
    {
        "app": "Chrome"
    },
    {
        "app": "Chrome"
    },
    {
        "app": "Chrome"
    },
    {
        "app": "Phone"
    },
    {
        "app": "Phone"
    },
    {
        "app": "Phone"
    },
    {
        "app": "Phone"
    },
    {
        "app": "Phone"
    },
    {
        "app": "Phone"
    },
    {
        "app": "Phone"
    },
    {
        "app": "Phone"
    },
    {
        "app": "Phone"
    },
    {
        "app": "Phone"
    },
    {
        "app": "Phone"
    },
    {
        "app": "Phone"
    },
    {
        "app": "Phone"
    },
    {
        "app": "Phone"
    },
    {
        "app": "Phone"
    },
    {
        "app": "Phone"
    },
    {
        "app": "Phone"
    },
    {
        "app": "Phone"
    },
    {
        "app": "Phone"
    },
    {
        "app": "Phone"
    },
    {
        "app": "Settings"
    },
    {
        "app": "Settings"
    },
    {
        "app": "Settings"
    },
    {
        "app": "Settings"
    },
    {
        "app": "Settings"
    },
    {
        "app": "Settings"
    },
    {
        "app": "Settings"
    },
    {
        "app": "Settings"
    },
    {
        "app": "Settings"
    },
    {
        "app": "Settings"
    },
    {
        "app": "Settings"
    },
    {
        "app": "Settings"
    },
    {
        "app": "Settings"
    },
    {
        "app": "Settings"
    },
    {
        "app": "Settings"
    },
    {
        "app": "Settings"
    },
    {
        "app": "Settings"
    },
    {
        "app": "Settings"
    },
    {
        "app": "Settings"
    },
    {
        "app": "Settings"
    },
    {
        "app": "Google Play Store"
    },
    {
        "app": "Google Play Store"
    },
    {
        "app": "Google Play Store"
    },
    {
        "app": "Google Play Store"
    },
    {
        "app": "Google Play Store"
    },
    {
        "app": "Google Play Store"
    },
    {
        "app": "Google Play Store"
    },
    {
        "app": "Google Play Store"
    },
    {
        "app": "Google Play Store"
    },
    {
        "app": "Google Play Store"
    },
    {
        "app": "Google Play Store"
    },
    {
        "app": "Google Play Store"
    },
    {
        "app": "Google Play Store"
    },
    {
        "app": "Google Play Store"
    },
    {
        "app": "Google Play Store"
    },
    {
        "app": "Google Play Store"
    },
    {
        "app": "Google Play Store"
    },
    {
        "app": "Google Play Store"
    },
    {
        "app": "Google Play Store"
    },
    {
        "app": "Google Play Store"
    },
    {
        "app": "Drive"
    },
    {
        "app": "Drive"
    },
    {
        "app": "Drive"
    },
    {
        "app": "Drive"
    },
    {
        "app": "Drive"
    },
    {
        "app": "Drive"
    },
    {
        "app": "Drive"
    },
    {
        "app": "Drive"
    },
    {
        "app": "Drive"
    },
    {
        "app": "Drive"
    },
    {
        "app": "Drive"
    },
    {
        "app": "Drive"
    },
    {
        "app": "Drive"
    },
    {
        "app": "Drive"
    },
    {
        "app": "Drive"
    },
    {
        "app": "Drive"
    },
    {
        "app": "Drive"
    },
    {
        "app": "Drive"
    },
    {
        "app": "Drive"
    },
    {
        "app": "Drive"
    },
    {
        "app": "Maps"
    },
    {
        "app": "Maps"
    },
    {
        "app": "Maps"
    },
    {
        "app": "Maps"
    }

1 Answer 1

1

Now you have two issues:

  1. Repetition issue: because you don't break the for loop whenever you add your app to the JSON list, so you continue iterating the ignoreList and add more instances of the app to the JSON list. to avoid that you need to add a break statement.

  2. Adding apps that should be ignored: this is because you are adding the app to the JSON list before comparing every instance from the ignoreList the app.. it looks vague and also to me :) so I will explain with example.

Example:

Consider your app = "Settings", and now you are comparing it to the list using the for loop, so first you compare it to "Google", it doesn't equal it, so add it to the JSON list, the same applies when you compare it to "Maps", and so on until you compare it to "Settings", at this time it won't be added to the JSON list, but you already added it multiple times before you reach to "Settings", so you need to wait until you finish your entire iterations before adding the app to the JSON list. I used a boolean to achieve that.

public void listApps(Context c) {

    PackageManager pm = c.getPackageManager();
    Intent main = new Intent(Intent.ACTION_MAIN, null);
    main.addCategory(Intent.CATEGORY_LAUNCHER);
    List<ResolveInfo> packages = pm.queryIntentActivities(main, 0);

    for (ResolveInfo resolve_info : packages) {
        try {
            String package_name = resolve_info.activityInfo.packageName;
            String app = (String) pm.getApplicationLabel(pm.getApplicationInfo(package_name, PackageManager.GET_META_DATA));
            boolean isIgnored = false;

            for (String tempStr : ignoreList) {

                if (app.toLowerCase().contains(tempStr.toLowerCase())) {
                    isIgnored = true;
                    break; // to avoid unneeded iterations
                }
            }
            if (!isIgnored) {
                JSONObject json = new JSONObject(); // Create temp json object with app info.
                json.put("app", app);
                appInfo.put(json);
            }


        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    try {
        Log.d("Debug", appInfo.toString(4));

    } catch (JSONException e) {
        e.printStackTrace();
    }

}
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you so much for the detailed response and taking the time to answer, it makes perfect sense.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.