Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
//License from project: Apache License 

import java.security.MessageDigest;
import java.util.ArrayList;
import java.util.List;

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

import android.content.pm.Signature;

import android.util.Base64;

public class Main {
    private static String mPackageName = null;

    /**
     * Gets the key hash of application package's certificate signature.
     * @since   0.1.1
     * @param   aContext The context from which the package manager is retrieved to get the signature's information.
     * @return   The list of signatures embedded to the application package.
     */
    public static List<String> getKeyHash(Context aContext) {
        try {
            PackageInfo info = aContext.getPackageManager().getPackageInfo(getPackageName(aContext),
                    PackageManager.GET_SIGNATURES);

            List<String> keyHashList = new ArrayList<String>(info.signatures.length);

            for (Signature signature : info.signatures) {
                MessageDigest md = MessageDigest.getInstance("SHA");
                md.update(signature.toByteArray());
                keyHashList.add(Base64.encodeToString(md.digest(), Base64.DEFAULT));
            }
            return keyHashList;
        } catch (Exception e) {
            return null;
        }
    }

    /**
     * Gets application's package name.
     * @since   0.1.0
     * @param   aContext The context from which the package name is retrieved.
     * @return   The package name.
     */
    public static String getPackageName(Context aContext) {
        if (mPackageName == null) {
            mPackageName = aContext.getPackageName();
        }
        return mPackageName;
    }
}