check if installed apk and uninstalled apk have the same Signatures. - Android App

Android examples for App:APK Signature

Description

check if installed apk and uninstalled apk have the same Signatures.

Demo Code


//package com.java2s;
import android.content.Context;

import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.content.pm.Signature;
import android.util.DisplayMetrics;
import android.util.Log;
import java.io.File;

import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;

import java.util.HashSet;

public class Main {
    /**//from www .  ja va  2s.  c  o m
     * check if installed apk and uninstalled apk have the same Signatures.
     * @param context
     * @param packageName installed apk package's name
     * @param archiveFilePath uninstalled apk fill full path
     * @return
     */
    public static boolean isSignatureSame(Context context,
            String packageName, String archiveFilePath) {
        return IsSignaturesSame(
                getInstalledPackageSignatures(context, packageName),
                getUninstallAPKSignatures(archiveFilePath));
    }

    private static boolean IsSignaturesSame(Signature[] s1, Signature[] s2) {
        if (s1 == null) {
            return false;
        }
        if (s2 == null) {
            return false;
        }
        HashSet<Signature> set1 = new HashSet<Signature>();
        for (Signature sig : s1) {
            set1.add(sig);
        }
        HashSet<Signature> set2 = new HashSet<Signature>();
        for (Signature sig : s2) {
            set2.add(sig);
        }
        if (set1.equals(set2)) {
            return true;
        }
        return false;
    }

    private static Signature[] getInstalledPackageSignatures(
            Context context, String packageName) {
        Signature[] signatures = null;
        PackageManager pm = context.getPackageManager();
        try {
            signatures = pm.getPackageInfo(packageName,
                    PackageManager.GET_SIGNATURES).signatures;
        } catch (NameNotFoundException e) {
            e.printStackTrace();
        }
        Log.d("aaaa",
                "getInstalledPackageSignatures " + signatures[0].toString());
        return signatures;
    }

    private static Signature[] getUninstallAPKSignatures(String apkPath) {
        String PATH_PackageParser = "android.content.pm.PackageParser";
        try {
            // apk?????? 
            // ????Package ???, ?????? 
            // ???????????????, apk????? 
            // PackageParser packageParser = new PackageParser(apkPath); 
            Class pkgParserCls = Class.forName(PATH_PackageParser);
            Class[] typeArgs = new Class[1];
            typeArgs[0] = String.class;
            Constructor pkgParserCt = pkgParserCls.getConstructor(typeArgs);
            Object[] valueArgs = new Object[1];
            valueArgs[0] = apkPath;
            Object pkgParser = pkgParserCt.newInstance(valueArgs);
            // ?????????, ??????????????????, ????????? 
            DisplayMetrics metrics = new DisplayMetrics();
            metrics.setToDefaults();
            // PackageParser.Package mPkgInfo = packageParser.parsePackage(new 
            // File(apkPath), apkPath, 
            // metrics, 0); 
            typeArgs = new Class[4];
            typeArgs[0] = File.class;
            typeArgs[1] = String.class;
            typeArgs[2] = DisplayMetrics.class;
            typeArgs[3] = Integer.TYPE;
            Method pkgParser_parsePackageMtd = pkgParserCls
                    .getDeclaredMethod("parsePackage", typeArgs);
            valueArgs = new Object[4];
            valueArgs[0] = new File(apkPath);
            valueArgs[1] = apkPath;
            valueArgs[2] = metrics;
            valueArgs[3] = PackageManager.GET_SIGNATURES;
            Object pkgParserPkg = pkgParser_parsePackageMtd.invoke(
                    pkgParser, valueArgs);

            typeArgs = new Class[2];
            typeArgs[0] = pkgParserPkg.getClass();
            typeArgs[1] = Integer.TYPE;
            Method pkgParser_collectCertificatesMtd = pkgParserCls
                    .getDeclaredMethod("collectCertificates", typeArgs);
            valueArgs = new Object[2];
            valueArgs[0] = pkgParserPkg;
            valueArgs[1] = PackageManager.GET_SIGNATURES;
            pkgParser_collectCertificatesMtd.invoke(pkgParser, valueArgs);
            // ??????????, ?????, ???????, ???????? 
            Field packageInfoFld = pkgParserPkg.getClass()
                    .getDeclaredField("mSignatures");
            Signature[] info = (Signature[]) packageInfoFld
                    .get(pkgParserPkg);
            return info;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
}

Related Tutorials