/*
 * 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 android.net.TrafficStats;
import com.android.volley.AuthFailureError;
import com.android.volley.Request;
import com.android.volley.toolbox.HurlStack;
import com.squareup.okhttp.OkHttpClient;
import org.apache.http.HttpResponse;
import org.gdg.frisbee.android.Const;
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;
import java.util.Map;

/**
 * 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);
    }

    @Override
    public HttpResponse performRequest(Request<?> request, Map<String, String> additionalHeaders) throws IOException, AuthFailureError {
        if(Const.DEVELOPER_MODE) {
            TrafficStats.setThreadStatsTag(0xF00D);
            try {
                HttpResponse res = super.performRequest(request, additionalHeaders);
                return res;
            } finally {
                TrafficStats.clearThreadStatsTag();
            }
        } else {
            return super.performRequest(request, additionalHeaders);
        }
    }
}

@@ -688,16 +688,44 @@
 Stats;%0D%0A
+import android.os.Build;%0D%0A%0D%0A
 import c
@@ -757,24 +757,24 @@
 lureError;%0D%0A
-
 import com.a
@@ -888,126 +888,8 @@
 nt;%0D
-%0Aimport org.apache.http.HttpResponse;%0D%0Aimport org.gdg.frisbee.android.Const;%0D%0Aimport org.gdg.frisbee.android.app.App;%0D
 %0A%0D%0Ai
@@ -961,48 +961,8 @@
 r;%0D%0A
-import javax.net.ssl.X509TrustManager;%0D%0A
 impo
@@ -1108,108 +1108,135 @@
 ava.
-security.cert.CertificateException;%0D%0Aimport java.security.cert.X509Certificate;%0D%0Aimport java.util.Ma
+util.Map;%0D%0A%0D%0Aimport org.apache.http.HttpResponse;%0D%0Aimport org.gdg.frisbee.android.Const;%0D%0Aimport org.gdg.frisbee.android.app.Ap
 p;%0D%0A
@@ -2300,16 +2300,83 @@
 PER_MODE
+ && Build.VERSION.SDK_INT %3E= Build.VERSION_CODES.ICE_CREAM_SANDWICH
 ) %7B%0D%0A   

fix okstack for devices < 14