Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How give the application manifest permissions? How to do it programmatically on Android?

I used this Topic

I try this code but did not work :

PACKAGE_NAME = context.getApplicationContext().getPackageName();

try {
  pi = context.getPackageManager().getPackageInfo(PACKAGE_NAME, PackageManager.GET_PERMISSIONS);
  for (String perm : pi.requestedPermissions) {
    Log.e("Foo", perm);
  }
} catch (Exception e) {
}

But it could not help me. I have the application list, I want to get the permission that used on each of them. How can I handle it?

UPDATE: like the photo, When clicking on "دسترسی ها", I want to get the permission that used in that app.(for example in a telegram: Internet, storage, call, camera,...)

enter image description here

UPDATE 2:

I will share the adapter code for my problem

My Adapter:

class AppViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {



    AppViewHolder(View itemView, Context context, List<App> apps) {
      super(itemView);
      txt_show_permission = itemView.findViewById(R.id.txt_show_permission);



      /*The String Buffer For Permissions*/
      appNameAndPermissions = new StringBuffer();
      PackageManager pm = context.getPackageManager();
      List<ApplicationInfo> packages = pm.getInstalledApplications(PackageManager.GET_META_DATA);
      for (ApplicationInfo applicationInfo : packages) {
        Log.d(TAG, "App: " + applicationInfo.name + " Package: " + applicationInfo.packageName);

        PackageInfo packageInfo = null;
        try {
          packageInfo = pm.getPackageInfo(applicationInfo.packageName, PackageManager.GET_PERMISSIONS);
          appNameAndPermissions.append(packageInfo.packageName + "*******:\n");


          //Get Permissions
          requestedPermissions = packageInfo.requestedPermissions;

          if (requestedPermissions != null) {
            for (int i = 0; i < requestedPermissions.length; i++) {
              Log.d(TAG, requestedPermissions[i]);
              appNameAndPermissions.append(requestedPermissions[i] + "\n");
            }

            appNameAndPermissions.append("\n");
          }

        } catch (PackageManager.NameNotFoundException e) {
          e.printStackTrace();
        }


      }
    }

set On Click Listener On txt_show_permission in onBindViewHolder:

holder.txt_show_permission.setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View v) {

        showDialog(String.valueOf(appNameAndPermissions));

      }
    });

Method for dialog in adapter class:

 public void showDialog(String txtPermission) {
    final Dialog dialog = new Dialog(context);
    dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
    dialog.setCancelable(false);
    dialog.setContentView(R.layout.show_permission_dialog);

    TextView txt_permission = dialog.findViewById(R.id.txt_permission);
    Button btn_ok = dialog.findViewById(R.id.btn_ok);
    txt_permission.setText(txtPermission);

    btn_ok.setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View v) {
        dialog.dismiss();
      }
    });
    dialog.show();

  }
like image 975
Sana Ebadi Avatar asked Oct 17 '22 06:10

Sana Ebadi


1 Answers

  1. You can loop through all the app names and get their permissions and store them in a String Buffer like this: https://stackoverflow.com/a/14672557/10058326

  2. Or since you want permissions to be shown on button click, you can add for each app the code you have tried with the proper app name in a OnButtonClickListener

  3. Or you can extract the relevant permissions from the StringBuffer made earlier each time the button is clicked

EDIT: See these links on how to create a OnItemClickListener for the Recycler View. You can get the position of the row that was clicked and through that get the app name in that row which you can pass to another function. Then write code inside that function to get permissions for the app name passed and display it https://antonioleiva.com/recyclerview-listener/

https://hackernoon.com/android-recyclerview-onitemclicklistener-getadapterposition-a-better-way-3c789baab4db

https://gist.github.com/riyazMuhammad/1c7b1f9fa3065aa5a46f

EDIT 2: Instead of passing appNameAndPermissions to showDialog which contains the whole list, you need to extract permissions of a certain app from the String Buffer. Here's how:

     String app_name = itemView.findViewById(R.id.app_name_text_view).getText().toString();
int indexOfApp = appNameAndPermissions.indexOf(app_name);
int indexOfLastPermission = appNameAndPermissions.indexOf("\n", indexOfApp);
String permissions = appNameAndPermissions.substring(indexOfApp, indexOfLastPermission);
like image 154
Kunj Mehta Avatar answered Oct 31 '22 20:10

Kunj Mehta