// Path: app/src/main/java/io/github/droidkaigi/confsched/api/RequestInterceptor.java
// @Singleton
// public class RequestInterceptor implements Interceptor {
// 
//     final ConnectivityManager connectivityManager;
// 
//     @Inject
//     public RequestInterceptor(ConnectivityManager connectivityManager) {
//         this.connectivityManager = connectivityManager;
//     }
// 
//     @Override
//     public Response intercept(Chain chain) throws IOException {
//         Request.Builder r = chain.request().newBuilder();
//         if (isConnected()) {
//             int maxAge = 2 * 60;
//             r.addHeader("cache-control", "public, max-age=" + maxAge);
//         } else {
//             int maxStale = 30 * 24 * 60 * 60; // 30 days
//             r.addHeader("cache-control", "public, only-if-cached, max-stale=" + maxStale);
//         }
// 
//         return chain.proceed(r.build());
//     }
// 
//     protected boolean isConnected() {
//         NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();
//         return networkInfo != null && networkInfo.isConnectedOrConnecting();
//     }
// 
// }


import com.google.android.gms.analytics.GoogleAnalytics;
import com.google.android.gms.analytics.Tracker;
import com.github.gfx.android.orma.AccessThreadConstraint;
import com.github.gfx.android.orma.migration.ManualStepMigration;
import android.app.Application;
import android.content.Context;
import android.content.SharedPreferences;
import android.net.ConnectivityManager;
import android.support.annotation.NonNull;
import java.io.File;
import javax.inject.Singleton;
import dagger.Module;
import dagger.Provides;
import io.github.droidkaigi.confsched.BuildConfig;
import io.github.droidkaigi.confsched.activity.ActivityNavigator;
import io.github.droidkaigi.confsched.api.RequestInterceptor;
import io.github.droidkaigi.confsched.model.OrmaDatabase;
import okhttp3.Cache;
import okhttp3.Interceptor;
import okhttp3.OkHttpClient;
import rx.subscriptions.CompositeSubscription;
    @Singleton
    @Provides
    public Tracker provideGoogleAnalyticsTracker(Context context) {
        GoogleAnalytics ga = GoogleAnalytics.getInstance(context);
        Tracker tracker = ga.newTracker(BuildConfig.GA_TRACKING_ID);
        tracker.enableAdvertisingIdCollection(true);
        tracker.enableExceptionReporting(true);
        return tracker;
    }

    @Provides
    public ConnectivityManager provideConnectivityManager(Context context) {
        return (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
    }

    @Singleton
    @Provides
    public OkHttpClient provideHttpClient(Context context, Interceptor interceptor) {
        File cacheDir = new File(context.getCacheDir(), CACHE_FILE_NAME);
        Cache cache = new Cache(cacheDir, MAX_CACHE_SIZE);

        OkHttpClient.Builder c = new OkHttpClient.Builder()
                .cache(cache)
                .addInterceptor(interceptor);

        return c.build();
    }

    @Provides
    public Interceptor provideRequestInterceptor(ConnectivityManager connectivityManager) {
// Path: app/src/main/java/io/github/droidkaigi/confsched/api/RequestInterceptor.java
// @Singleton
// public class RequestInterceptor implements Interceptor {
// 
//     final ConnectivityManager connectivityManager;
// 
//     @Inject
//     public RequestInterceptor(ConnectivityManager connectivityManager) {
//         this.connectivityManager = connectivityManager;
//     }
// 
//     @Override
//     public Response intercept(Chain chain) throws IOException {
//         Request.Builder r = chain.request().newBuilder();
//         if (isConnected()) {
//             int maxAge = 2 * 60;
//             r.addHeader("cache-control", "public, max-age=" + maxAge);
//         } else {
//             int maxStale = 30 * 24 * 60 * 60; // 30 days
//             r.addHeader("cache-control", "public, only-if-cached, max-stale=" + maxStale);
//         }
// 
//         return chain.proceed(r.build());
//     }
// 
//     protected boolean isConnected() {
//         NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();
//         return networkInfo != null && networkInfo.isConnectedOrConnecting();
//     }
// 
// }


// Path: app/src/main/java/io/github/droidkaigi/confsched/di/AppModule.java
import com.google.android.gms.analytics.GoogleAnalytics;
import com.google.android.gms.analytics.Tracker;
import com.github.gfx.android.orma.AccessThreadConstraint;
import com.github.gfx.android.orma.migration.ManualStepMigration;
import android.app.Application;
import android.content.Context;
import android.content.SharedPreferences;
import android.net.ConnectivityManager;
import android.support.annotation.NonNull;
import java.io.File;
import javax.inject.Singleton;
import dagger.Module;
import dagger.Provides;
import io.github.droidkaigi.confsched.BuildConfig;
import io.github.droidkaigi.confsched.activity.ActivityNavigator;
import io.github.droidkaigi.confsched.api.RequestInterceptor;
import io.github.droidkaigi.confsched.model.OrmaDatabase;
import okhttp3.Cache;
import okhttp3.Interceptor;
import okhttp3.OkHttpClient;
import rx.subscriptions.CompositeSubscription;

    @Singleton
    @Provides
    public Tracker provideGoogleAnalyticsTracker(Context context) {
        GoogleAnalytics ga = GoogleAnalytics.getInstance(context);
        Tracker tracker = ga.newTracker(BuildConfig.GA_TRACKING_ID);
        tracker.enableAdvertisingIdCollection(true);
        tracker.enableExceptionReporting(true);
        return tracker;
    }

    @Provides
    public ConnectivityManager provideConnectivityManager(Context context) {
        return (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
    }

    @Singleton
    @Provides
    public OkHttpClient provideHttpClient(Context context, Interceptor interceptor) {
        File cacheDir = new File(context.getCacheDir(), CACHE_FILE_NAME);
        Cache cache = new Cache(cacheDir, MAX_CACHE_SIZE);

        OkHttpClient.Builder c = new OkHttpClient.Builder()
                .cache(cache)
                .addInterceptor(interceptor);

        return c.build();
    }

    @Provides
    public Interceptor provideRequestInterceptor(ConnectivityManager connectivityManager) {
        return new RequestInterceptor(connectivityManager);