Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;

import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Arrays;

import android.content.Context;
import android.content.pm.PackageManager;

public class Main {
    public static void unRevokePermission(String packageName, String permission, Context ctx) {
        String[] rPerms = getRevokedPerms(packageName, ctx);
        if (rPerms == null)
            rPerms = new String[0];
        ArrayList<String> revokedPerms = new ArrayList<String>();
        revokedPerms.addAll(Arrays.asList(rPerms));
        if (revokedPerms.contains(permission)) {
            revokedPerms.remove(permission);
            String[] permsToRevoke = new String[revokedPerms.size()];
            revokedPerms.toArray(permsToRevoke);
            setRevokedPermissions(packageName, permsToRevoke, ctx);
        }
    }

    public static String[] getRevokedPerms(String packageName, Context ctx) {
        String[] revokedPerms = null;
        PackageManager pkgManager = ctx.getPackageManager();
        Method getRevokedPermissions;
        try {
            getRevokedPermissions = pkgManager.getClass().getMethod("getRevokedPermissions",
                    java.lang.String.class);
            Object[] params = new Object[] { packageName };
            revokedPerms = (String[]) getRevokedPermissions.invoke(pkgManager, params);
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return revokedPerms;
    }

    public static void setRevokedPermissions(String packageName, String[] permissions, Context ctx) {
        PackageManager pkgManager = ctx.getPackageManager();
        Method setRevokedPermissions;

        try {
            setRevokedPermissions = pkgManager.getClass().getMethod("setRevokedPermissions", java.lang.String.class,
                    String[].class);
            Object[] params = new Object[] { packageName, permissions };
            setRevokedPermissions.invoke(pkgManager, params);
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}