get Package Archive Info via reflection - Android java.lang.reflect

Android examples for java.lang.reflect:Package

Description

get Package Archive Info via reflection

Demo Code


//package com.java2s;

import android.content.pm.PackageInfo;

import android.util.DisplayMetrics;
import android.util.Log;
import java.io.File;

import java.lang.reflect.Constructor;

import java.lang.reflect.Method;

public class Main {
    @SuppressWarnings("unchecked")
    private static PackageInfo getPackageArchiveInfo(
            String archiveFilePath, int flags) {
        // Workaround for
        // https://code.google.com/p/android/issues/detail?id=9151#c8
        try {/*www .ja va2 s  .  c om*/
            Class packageParserClass = Class
                    .forName("android.content.pm.PackageParser");
            Class[] innerClasses = packageParserClass.getDeclaredClasses();
            Class packageParserPackageClass = null;
            for (Class innerClass : innerClasses) {
                if (0 == innerClass.getName().compareTo(
                        "android.content.pm.PackageParser$Package")) {
                    packageParserPackageClass = innerClass;
                    break;
                }
            }
            Constructor packageParserConstructor = packageParserClass
                    .getConstructor(String.class);
            Method parsePackageMethod = packageParserClass
                    .getDeclaredMethod("parsePackage", File.class,
                            String.class, DisplayMetrics.class, int.class);
            Method collectCertificatesMethod = packageParserClass
                    .getDeclaredMethod("collectCertificates",
                            packageParserPackageClass, int.class);
            Method generatePackageInfoMethod = packageParserClass
                    .getDeclaredMethod("generatePackageInfo",
                            packageParserPackageClass, int[].class,
                            int.class, long.class, long.class);
            packageParserConstructor.setAccessible(true);
            parsePackageMethod.setAccessible(true);
            collectCertificatesMethod.setAccessible(true);
            generatePackageInfoMethod.setAccessible(true);
            Object packageParser = packageParserConstructor
                    .newInstance(archiveFilePath);
            DisplayMetrics metrics = new DisplayMetrics();
            metrics.setToDefaults();
            final File sourceFile = new File(archiveFilePath);
            Object pkg = parsePackageMethod.invoke(packageParser,
                    sourceFile, archiveFilePath, metrics, 0);
            if (pkg == null) {
                return null;
            }
            if ((flags & android.content.pm.PackageManager.GET_SIGNATURES) != 0) {
                collectCertificatesMethod.invoke(packageParser, pkg, 0);
            }
            return (PackageInfo) generatePackageInfoMethod.invoke(null,
                    pkg, null, flags, 0, 0);
        } catch (Exception e) {
            Log.e("Signature Monitor",
                    "android.content.pm.PackageParser reflection failed: "
                            + e.toString());
        }
        return null;
    }
}

Related Tutorials