/*
 * Copyright (c) 2004-2019, University of Oslo
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 * Redistributions of source code must retain the above copyright notice, this
 * list of conditions and the following disclaimer.
 *
 * Redistributions in binary form must reproduce the above copyright notice,
 * this list of conditions and the following disclaimer in the documentation
 * and/or other materials provided with the distribution.
 * Neither the name of the HISP project nor the names of its contributors may
 * be used to endorse or promote products derived from this software without
 * specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

package org.hisp.dhis.android.core;

import android.content.Context;

import androidx.test.InstrumentationRegistry;

import com.facebook.stetho.okhttp3.StethoInterceptor;

import org.hisp.dhis.android.core.arch.db.access.DatabaseAdapter;
import org.hisp.dhis.android.core.arch.storage.internal.AndroidSecureStore;
import org.hisp.dhis.android.core.arch.storage.internal.Credentials;
import org.hisp.dhis.android.core.arch.storage.internal.CredentialsSecureStoreImpl;
import org.hisp.dhis.android.core.arch.storage.internal.InMemorySecureStore;
import org.hisp.dhis.android.core.arch.storage.internal.ObjectSecureStore;
import org.hisp.dhis.android.core.arch.storage.internal.SecureStore;
import org.hisp.dhis.android.core.maintenance.D2Error;

import java.util.Collections;

import okhttp3.logging.HttpLoggingInterceptor;

public class D2Factory {

    public static D2 forNewDatabase() {
        return forNewDatabaseInternal(new InMemorySecureStore());
    }

    public static D2 forNewDatabaseWithAndroidSecureStore() {
        Context context = InstrumentationRegistry.getTargetContext().getApplicationContext();
        return forNewDatabaseInternal(new AndroidSecureStore(context));
    }

    private static D2 forNewDatabaseInternal(SecureStore secureStore) {
        Context context = InstrumentationRegistry.getTargetContext().getApplicationContext();

        D2Configuration d2Configuration = d2Configuration(context);

        D2Manager.setTestMode(true);
        D2Manager.setTestingSecureStore(secureStore);
        D2 d2 = D2Manager.blockingInstantiateD2(d2Configuration);

        D2Manager.clear();

        return d2;
    }

    public static D2Configuration d2Configuration(Context context) {
        HttpLoggingInterceptor loggingInterceptor = new HttpLoggingInterceptor();
        loggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BASIC);
        return D2Configuration.builder()
                .appVersion("1.0.0")
                .readTimeoutInSeconds(30)
                .connectTimeoutInSeconds(30)
                .writeTimeoutInSeconds(30)
                .networkInterceptors(Collections.singletonList(new StethoInterceptor()))
                .interceptors(Collections.singletonList(loggingInterceptor))
                .context(context)
                .build();
    }

    public static D2 forDatabaseAdapter(DatabaseAdapter databaseAdapter) {
        Context context = InstrumentationRegistry.getTargetContext().getApplicationContext();
        NotClosedObjectsDetector.enableNotClosedObjectsDetection();
        SecureStore secureStore = new InMemorySecureStore();
        ObjectSecureStore<Credentials> credentialsSecureStore = new CredentialsSecureStoreImpl(secureStore);
        try {
            return new D2(
                    RetrofitFactory.retrofit(
                            OkHttpClientFactory.okHttpClient(d2Configuration(context), credentialsSecureStore)),
                    databaseAdapter,
                    context,
                    secureStore,
                    credentialsSecureStore);
        } catch (D2Error d2Error) {
            d2Error.printStackTrace();
            return null;
        }
    }
}
@@ -1766,262 +1766,43 @@
 rch.
-db.access.DatabaseAdapter;%0Aimport org.hisp.dhis.android.core.arch.storage.internal.AndroidSecureStore;%0Aimport org.hisp.dhis.android.core.arch.storage.internal.Credentials;%0Aimport org.hisp.dhis.android.core.arch.storage.internal.CredentialsSecureStoreImpl
+storage.internal.AndroidSecureStore
 ;%0Aim
@@ -1880,83 +1880,8 @@
 re;%0A
-import org.hisp.dhis.android.core.arch.storage.internal.ObjectSecureStore;%0A
 impo
@@ -1948,63 +1948,8 @@
 ore;
-%0Aimport org.hisp.dhis.android.core.maintenance.D2Error;
 %0A%0Aim
@@ -2844,37 +2844,38 @@
 d2;%0A    %7D%0A%0A    p
-ublic
+rivate
  static D2Config
@@ -3480,16 +3480,16 @@
 ontext)%0A
+
         
@@ -3515,874 +3515,6 @@
    %7D
-%0A%0A    public static D2 forDatabaseAdapter(DatabaseAdapter databaseAdapter) %7B%0A        Context context = InstrumentationRegistry.getTargetContext().getApplicationContext();%0A        NotClosedObjectsDetector.enableNotClosedObjectsDetection();%0A        SecureStore secureStore = new InMemorySecureStore();%0A        ObjectSecureStore%3CCredentials%3E credentialsSecureStore = new CredentialsSecureStoreImpl(secureStore);%0A        try %7B%0A            return new D2(%0A                    RetrofitFactory.retrofit(%0A                            OkHttpClientFactory.okHttpClient(d2Configuration(context), credentialsSecureStore)),%0A                    databaseAdapter,%0A                    context,%0A                    secureStore,%0A                    credentialsSecureStore);%0A        %7D catch (D2Error d2Error) %7B%0A            d2Error.printStackTrace();%0A            return null;%0A        %7D%0A    %7D
 %0A%7D

Remove unused D2Factory.fromDatabaseAdapter