package me.devsaki.hentoid.util.network;

import android.util.SparseArray;

import java.io.IOException;
import java.util.concurrent.TimeUnit;

import me.devsaki.hentoid.core.HentoidApp;
import okhttp3.Cache;
import okhttp3.Interceptor;
import okhttp3.OkHttpClient;
import okhttp3.Request;

/**
 * Manages a single instance of OkHttpClient per timeout delay
 */
@SuppressWarnings("squid:S3077")
// https://stackoverflow.com/questions/11639746/what-is-the-point-of-making-the-singleton-instance-volatile-while-using-double-l
public class OkHttpClientSingleton {

    private static volatile SparseArray<OkHttpClient> instance = new SparseArray<>();


    private OkHttpClientSingleton() {
    }

    public static OkHttpClient getInstance() {
        return getInstance(HttpHelper.DEFAULT_REQUEST_TIMEOUT);
    }

    public static OkHttpClient getInstance(int connectTimeout, int ioTimeout) {
        int key = (connectTimeout * 100) + ioTimeout;
        if (null == OkHttpClientSingleton.instance.get(key)) {
            synchronized (OkHttpClientSingleton.class) {
                if (null == OkHttpClientSingleton.instance.get(key)) {

                    int CACHE_SIZE = 2 * 1024 * 1024; // 2 MB

                    OkHttpClient.Builder clientBuilder = new OkHttpClient.Builder()
                            .addInterceptor(OkHttpClientSingleton::rewriteUserAgentInterceptor)
                            .connectTimeout(connectTimeout, TimeUnit.MILLISECONDS)
                            .readTimeout(ioTimeout, TimeUnit.MILLISECONDS)
                            .writeTimeout(ioTimeout, TimeUnit.MILLISECONDS)
                            .cache(new Cache(HentoidApp.getInstance().getCacheDir(), CACHE_SIZE));


                    OkHttpClientSingleton.instance.put(key, clientBuilder.build());
                }
            }
        }
        return OkHttpClientSingleton.instance.get(key);
    }

    public static OkHttpClient getInstance(int timeoutMs) {
        if (null == OkHttpClientSingleton.instance.get(timeoutMs)) {
            synchronized (OkHttpClientSingleton.class) {
                if (null == OkHttpClientSingleton.instance.get(timeoutMs)) {

                    int CACHE_SIZE = 2 * 1024 * 1024; // 2 MB

                    OkHttpClient.Builder clientBuilder = new OkHttpClient.Builder()
                            .addInterceptor(OkHttpClientSingleton::rewriteUserAgentInterceptor)
                            .connectTimeout(timeoutMs, TimeUnit.MILLISECONDS)
                            .readTimeout(timeoutMs, TimeUnit.MILLISECONDS)
                            .writeTimeout(timeoutMs, TimeUnit.MILLISECONDS)
                            .cache(new Cache(HentoidApp.getInstance().getCacheDir(), CACHE_SIZE));


                    OkHttpClientSingleton.instance.put(timeoutMs, clientBuilder.build());
                }
            }
        }
        return OkHttpClientSingleton.instance.get(timeoutMs);
    }

    private static okhttp3.Response rewriteUserAgentInterceptor(Interceptor.Chain chain) throws IOException {
        Request.Builder builder = chain.request().newBuilder();
        // If not specified, all requests are done with the device's mobile user-agent, without the Hentoid string
        if (null == chain.request().header("User-Agent") && null == chain.request().header("user-agent"))
            builder.header(HttpHelper.HEADER_USER_AGENT, HttpHelper.getMobileUserAgent(false, true));
        return chain.proceed(builder.build());
    }
}

@@ -2409,86 +2409,8 @@
 or)%0A
-                            .connectTimeout(timeoutMs, TimeUnit.MILLISECONDS)%0A
     

Revert to default connect timeout for all non-fast connections