/*
 * Copyright 2013 The GDG Frisbee Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * 	http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.gdg.frisbee.android.api;

import com.android.volley.toolbox.HurlStack;
import com.squareup.okhttp.OkHttpClient;
import org.gdg.frisbee.android.app.App;

import javax.net.ssl.SSLContext;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.security.GeneralSecurityException;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;

/**
 * GDG Aachen
 * org.gdg.frisbee.android.api
 * <p/>
 * User: maui
 * Date: 27.05.13
 * Time: 02:57
 */
public class OkStack extends HurlStack {

    @Override
    protected HttpURLConnection createConnection(URL url) throws IOException {
        OkHttpClient client = new OkHttpClient();
        SSLContext sslContext;
        try {
            TrustManager[] trustAllCerts = new TrustManager[] { new GdgTrustManager(App.getInstance().getApplicationContext()) };

            sslContext = SSLContext.getInstance("TLS");
            sslContext.init(null, trustAllCerts, new java.security.SecureRandom());
        } catch (GeneralSecurityException e) {
            throw new AssertionError(); // The system has no TLS. Just give up.
        }
        client.setSslSocketFactory(sslContext.getSocketFactory());
        return client.open(url);
    }
}

@@ -665,85 +665,277 @@
 ort 
-com.android.volley.toolbox.HurlStack;%0D%0Aimport com.squareup.okhttp.OkHttpClien
+android.net.TrafficStats;%0D%0Aimport com.android.volley.AuthFailureError;%0D%0Aimport com.android.volley.Request;%0D%0Aimport com.android.volley.toolbox.HurlStack;%0D%0Aimport com.squareup.okhttp.OkHttpClient;%0D%0Aimport org.apache.http.HttpResponse;%0D%0Aimport org.gdg.frisbee.android.Cons
 t;%0D%0A
@@ -1314,16 +1314,39 @@
 ficate;%0D
+%0Aimport java.util.Map;%0D
 %0A%0D%0A/**%0D%0A
@@ -2216,12 +2216,589 @@
 %0D%0A    %7D%0D
+%0A%0D%0A    @Override%0D%0A    public HttpResponse performRequest(Request%3C?%3E request, Map%3CString, String%3E additionalHeaders) throws IOException, AuthFailureError %7B%0D%0A        if(Const.DEVELOPER_MODE) %7B%0D%0A            TrafficStats.setThreadStatsTag(0xF00D);%0D%0A            try %7B%0D%0A                HttpResponse res = super.performRequest(request, additionalHeaders);%0D%0A                return res;%0D%0A            %7D finally %7B%0D%0A                TrafficStats.clearThreadStatsTag();%0D%0A            %7D%0D%0A        %7D else %7B%0D%0A            return super.performRequest(request, additionalHeaders);%0D%0A        %7D%0D%0A    %7D%0D
 %0A%7D%0D%0A

Allow Network Statistics Profiling when Const.DEVELOPER_MODE is set to true