Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.Signature;

import java.io.ByteArrayInputStream;

import java.security.cert.CertificateException;
import java.security.cert.CertificateFactory;
import java.security.cert.X509Certificate;
import javax.security.auth.x500.X500Principal;

public class Main {
    /**
     * Check whether the application is debuggable.
     * The first check is to see what the PackageManager reports.
     * If wanted, an additional check can be performed by looking at the certificate.
     * The default auto-generated certificate has the DN 'CN=Android Debug,O=Android,C=US',
     * as described at http://developer.android.com/tools/publishing/app-signing.html#debugmode
     * If the app's DN matches this default, it is probably using the debug certificate.
     *
     * @param context Context
     * @return true when the app is debuggable, otherwise false
     */
    public static boolean isDebuggable(final Context context, final boolean includeDefaultDebugCertificateCheck) {
        boolean debuggable = (0 != (context.getApplicationInfo().flags &= ApplicationInfo.FLAG_DEBUGGABLE));
        if (!debuggable && includeDefaultDebugCertificateCheck) {
            debuggable = isDebugCertificateCheck(context);
        }
        return debuggable;
    }

    private static boolean isDebugCertificateCheck(final Context context) {
        final X500Principal DEBUG_CERTIFICATE_DN = new X500Principal("CN=Android Debug,O=Android,C=US");
        boolean debuggable = false;

        try {
            Signature[] signatures = getSignatures(context);

            for (int i = 0; i < signatures.length; i++) {
                X509Certificate certificate = generateX509CertificateFromSignature(signatures[i]);
                debuggable = certificate.getSubjectX500Principal().equals(DEBUG_CERTIFICATE_DN);
                if (debuggable) {
                    return true;
                }
            }
        } catch (PackageManager.NameNotFoundException e) {
            // package not found - debuggable = false
        } catch (CertificateException e) {
            // certificate factory non-instantiable - debuggable = false
        }
        return false;
    }

    private static Signature[] getSignatures(final Context context) throws PackageManager.NameNotFoundException {
        PackageInfo packageInfo = context.getPackageManager().getPackageInfo(getPackageName(context),
                PackageManager.GET_SIGNATURES);
        Signature[] signatures = packageInfo.signatures;
        return signatures;
    }

    private static X509Certificate generateX509CertificateFromSignature(final Signature signature)
            throws CertificateException {
        CertificateFactory certificateFactory = CertificateFactory.getInstance("X.509");
        ByteArrayInputStream inputStream = new ByteArrayInputStream(signature.toByteArray());
        X509Certificate certificate = (X509Certificate) certificateFactory.generateCertificate(inputStream);
        return certificate;
    }

    /**
     * Get the application package name.
     *
     * @param context Context
     * @return package name
     */
    public static String getPackageName(final Context context) {
        return context.getPackageName();
    }
}