Android Context Check isDebugCertificateCheck(final Context context)

Here you can find the source of isDebugCertificateCheck(final Context context)

Description

is Debug Certificate Check

License

Apache License

Declaration

private static boolean isDebugCertificateCheck(final Context context) 

Method Source Code

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

import android.content.Context;

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 {
    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 {/*from w w w .ja  v  a2  s .  co  m*/
            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();
    }
}

Related

  1. isApplicationBroughtToBackground( final Context context)
  2. isApplicationBroughtToBackground( final Context context)
  3. isCallable(Context context, String url)
  4. isConnected(Context connext)
  5. isConnected(Context context)
  6. isDebuggable(final Context context, final boolean includeDefaultDebugCertificateCheck)
  7. isExisting(Context context, Uri uri, String selection, String[] args)
  8. isFileExist(Context ctx, String filename)
  9. isFree(Context context)