Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  package com.koushikdutta.ion.conscrypt;
  
  import  android.content.Context;
  
  
 
Created by koush on 7/13/14.
 
 public class ConscryptMiddleware extends SimpleMiddleware {
     final static Object lock = new Object();
     static boolean initialized;
     static boolean success;
     boolean instanceInitialized;
     boolean enabled = true;
 
     public void enable(boolean enabled) {
         this. = enabled;
         if (!enabled) {
              = false;
             .setSSLContext(null);
         }
     }
 
     public static void initialize(Context context) {
         try {
             synchronized () {
                 if ()
                     return;
 
                  = true;
 
                 // GMS Conscrypt is already initialized, from outside ion. Leave it alone.
                 if (Security.getProvider() != null) {
                      = true;
                     return;
                 }
 
                 SSLContext originalDefaultContext = SSLContext.getDefault();
                 SSLSocketFactory originalDefaultSSLSocketFactory = HttpsURLConnection.getDefaultSSLSocketFactory();
                 try {
                     Class<?> providerInstaller = Class.forName("com.google.android.gms.security.ProviderInstaller");
                     Method mInsertProvider = providerInstaller.getDeclaredMethod("installIfNeeded", Context.class);
                     mInsertProvider.invoke(nullcontext);
 
                 } catch (Throwable ignored) {
                     Context gms = context.createPackageContext("com.google.android.gms", Context.CONTEXT_INCLUDE_CODE | Context.CONTEXT_IGNORE_SECURITY);
                     gms
                     .getClassLoader()
                     .loadClass("com.google.android.gms.common.security.ProviderInstallerImpl")
                     .getMethod("insertProvider", Context.class)
                     .invoke(nullcontext);
                 }
 
                 Provider[] providers = Security.getProviders();
                 Provider provider = Security.getProvider();
                 Security.removeProvider();
                 Security.insertProviderAt(providerproviders.length);
                 SSLContext.setDefault(originalDefaultContext);
                 HttpsURLConnection.setDefaultSSLSocketFactory(originalDefaultSSLSocketFactory);
                  = true;
             }
         }
         catch (Exception e) {
         }
     }
 
     private static final String GMS_PROVIDER = "GmsCore_OpenSSL";
 
     public void initialize() {
         initialize();
         if ( && ! && ) {
              = true;
             try {
                 SSLContext sslContext = null;
                 try {
                     sslContext = SSLContext.getInstance("TLS");
                 }
                 catch (Exception e) {
                 }
                 if (sslContext == null)
                     sslContext = SSLContext.getInstance("TLS");
                 sslContext.init(nullnullnull);
                 // only set the SSL context if it is the default SSL context
                 if (.getSSLContext() == AsyncSSLSocketWrapper.getDefaultSSLContext())
                     .setSSLContext(sslContext);
             }
             catch (Exception e) {
            }
        }
    }
    Context context;
    public ConscryptMiddleware(Context contextAsyncSSLSocketMiddleware middleware) {
        this. = middleware;
        this. = context.getApplicationContext();
    }
    @Override
    public Cancellable getSocket(GetSocketData data) {
        if (!) {
            return null;
        }
        // initialize here vs the constructor, or this will potentially block the ui thread.
        initialize();
        return super.getSocket(data);
    }
New to GrepCode? Check out our FAQ X