Android Context Check isDebuggable(final Context context, final boolean includeDefaultDebugCertificateCheck)

Here you can find the source of isDebuggable(final Context context, final boolean includeDefaultDebugCertificateCheck)

Description

Check whether the application is debuggable.

License

Apache License

Parameter

Parameter Description
context Context

Return

true when the app is debuggable, otherwise false

Declaration

public static boolean isDebuggable(final Context context,
        final boolean includeDefaultDebugCertificateCheck) 

Method Source Code

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

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 {
    /**//from   w  w  w.  ja v a 2  s.  c  o  m
     * 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();
    }
}

Related

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