0

I'm creating a function which retrieves the installed apps of the device, the package of the app and the permissions each app has. The problem is that I want to put all of this into a JSON file and I do not know how to do it, here it is what I programmed, but it doesn't work, please, if someone know something, help, it is very important for me! Thanks in advance.

public static void installedApps(Context context) throws IOException{

    PackageManager pm = context.getPackageManager();
    List<ApplicationInfo> packagelist = pm.getInstalledApplications(PackageManager.GET_META_DATA);

    CharSequence app;
    String pckge;
    String[] permissions;

    for (ApplicationInfo applicationInfo : packagelist){

        app = applicationInfo.loadLabel(context.getPackageManager());
        pckge = applicationInfo.packageName;

        JSONObject json = new JSONObject();

        Log.d("test", "App: "+ applicationInfo.loadLabel(context.getPackageManager()) +", Package: " + applicationInfo.packageName);

        try{
            PackageInfo packageInfo = pm.getPackageInfo(applicationInfo.packageName, PackageManager.GET_PERMISSIONS);

            //Get Permissions
            String[] requestedPermissions = packageInfo.requestedPermissions;

            if(requestedPermissions != null){
                for (int i = 0; i < requestedPermissions.length; i++){

                    Log.d("test", requestedPermissions[i]);

                    permissions = requestedPermissions;

                    JSONArray jsonArray = new JSONArray();

                    try{
                        json.put("app", app);
                        json.put("package", pckge);

                        jsonArray.put(Integer.parseInt("permission"), permissions);


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


                }
            }
            //primero cargar en un array todos los datos, y pasarlos a un json llamandolos
        }catch (PackageManager.NameNotFoundException e){
            e.printStackTrace();
        }

    }
}

2 Answers2

0

I would probably go for something like this:

public static void installedApps(Context context) {
    PackageManager pm = context.getPackageManager();
    List<ApplicationInfo> packagelist = pm.getInstalledApplications(PackageManager.GET_META_DATA);
    JSONObject resp = new JSONObject();

    for (ApplicationInfo applicationInfo : packagelist) {

        CharSequence app = applicationInfo.loadLabel(context.getPackageManager());
        String pckge = applicationInfo.packageName;
        JSONObject json = new JSONObject();

        try {
            //Get Permissions
            PackageInfo packageInfo = pm.getPackageInfo(applicationInfo.packageName, PackageManager.GET_PERMISSIONS);
            String[] requestedPermissions = packageInfo.requestedPermissions;
            if (requestedPermissions != null) {
                json.put("app", app);
                json.put("package", pckge);
                JSONArray jsonArray = new JSONArray();
                for (String requestedPermission : requestedPermissions) {
                    jsonArray.put(requestedPermission);
                }
                json.put("permission", jsonArray);
                resp.put(applicationInfo.name, json); // something to identify each app
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    // return resp.toString() or store in file or whatever you want to do
}
fhomovc
  • 301
  • 3
  • 15
  • Thanks, I managed to do it, now the problem I'm receiving is that when i want to store the json i receive this: W/System.err: java.io.FileNotFoundException: /storage/emulated/0/app.json (Permission denied), I want to create a json file and write it, could you pls help me? – Cesar Sanchez Apr 09 '19 at 18:07
  • Sure, I would need to see the code so you could post a different question to get an answer there. Meanwhile, check https://stackoverflow.com/questions/37819550/java-io-filenotfoundexception-storage-emulated-0-new-file-txt-open-failed-ea or https://developer.android.com/training/data-storage/files for common problems with reading/writing files. I'm pretty sure it's related to some permissions missing, but without a code sample it's difficult to tell. – fhomovc Apr 09 '19 at 18:14
  • Hey, thanks for answering, this is the code File file = new File(Environment.getExternalStorageDirectory() + File.separator + "app.json"); try(FileWriter File = new FileWriter(file)){ File.write(json.toString()); File.flush(); }catch (IOException e){ e.printStackTrace(); } I'm struggling so hard, so if you could help me it would be perfect!! – Cesar Sanchez Apr 09 '19 at 18:15
  • Again, this should be discussed in another question but I'll give some general tips: add `` in your manifest, implement runtime permissions https://developer.android.com/training/permissions/requesting.html or or you can manually enable the storage permission by going to settings>apps> "your_app_name" >click on it >then click permissions> then enable the storage (use this only for debugging) – fhomovc Apr 09 '19 at 18:28
0

I am petty sure there is a problem with this line,

permissions = requestedPermissions; 

...

jsonArray.put(Integer.parseInt("permission"), permissions); 

since you define the permissions array at the top, you will see the final requestPermission entry adding to jsonArray over and over packageList.size() times.

bring permission to inside the loop. it will solve your problem.

Sankha Karunasekara
  • 1,636
  • 18
  • 19
  • I managed to do that, the problem now is that when I debug the program, it always show me this in the json: {"app":"Filter Provider","package":"com.samsung.android.provider.filterprovider","permissions":"[Ljava.lang.String;@e64a639"}. The problem is that I do not want the Ljava.lang.String, I want a list, do you know anything about this issue? – Cesar Sanchez Apr 09 '19 at 17:25
  • 1
    @CesarSanchez `permissions` is as `String[]` and you are adding it as an Object, that's why it internally calls `toString()` method which returns the string you are seeing. See my answer, try `for (String requestedPermission : requestedPermissions) { jsonArray.put(requestedPermission);}` and it should work – fhomovc Apr 09 '19 at 17:59
  • 1
    Sure. This is what happens when to try to convert an array directly in to a string. If you are using java 8 try, String.join(",", permissions); or something to convert a string array in to String. Then you will be fine. – Sankha Karunasekara Apr 09 '19 at 18:01
  • @cesarSanchez if you got it working don't forget to accept the answer. – Sankha Karunasekara Apr 09 '19 at 18:07
  • @lakshans Thanks, I managed to do it, now the problem I'm receiving is that when i want to store the json i receive this: W/System.err: java.io.FileNotFoundException: /storage/emulated/0/app.json (Permission denied), I want to create a json file and write it, could you pls help me? – Cesar Sanchez Apr 09 '19 at 18:12
  • You can find the answer to your problem in different questions in stack overflow try this one. https://stackoverflow.com/questions/41724540/write-data-in-json-file-in-right-form-java – Sankha Karunasekara Apr 09 '19 at 18:32