// Path: app/src/main/java/io/github/krtkush/marsexplorer/RESTClients/RestClientConstants.java
// public class  RestClientConstants {
// 
//     // Strings related to NASA's API
//     private static final String nasaApiEndPoint = "https://api.nasa.gov/";
//     private static final String nasaApiServiceName = "mars-photos/";
//     private static final String nasaApiVersion = "api/v1/rovers/";
//     public static final String nasaApiBaseUrl = nasaApiEndPoint + nasaApiServiceName
//             + nasaApiVersion;
// 
//     // Strings related to Mars Weather's API
//     private static final String marsWeatherEndPoint = "http://cab.inta-csic.es/";
//     private static final String marsWeatherAddress = "rems/wp-content/plugins/marsweather-widget/";
//     public static final String marsWeatherBaseUrl = marsWeatherEndPoint + marsWeatherAddress;
// 
//     // Custom headers
//     public static final String responseCachingFlagHeader = "ApplyResponseCache";
//     public static final String offlineCachingFlagHeader = "ApplyOfflineCache";
// 
//     // Cache directory name
//     public static final String apiResponsesCache = "apiResponses";
// }
// 
// Path: app/src/main/java/io/github/krtkush/marsexplorer/UtilityMethods.java
// public class UtilityMethods {
// 
//     /**
//      * Method to detect network connection on the device
//      * @return
//      */
//     public static boolean isNetworkAvailable() {
// 
//         ConnectivityManager connectivityManager =
//                 (ConnectivityManager) MarsExplorerApplication.getApplicationInstance()
//                         .getSystemService(MarsExplorerApplication.getApplicationInstance()
//                                 .CONNECTIVITY_SERVICE);
// 
//         return connectivityManager.getActiveNetworkInfo() != null
//                 && connectivityManager.getActiveNetworkInfo().isConnectedOrConnecting();
//     }
// 
//     /**
//      * It's usually very important for websites to track where their traffic is coming from.
//      * We make sure to let them know that we are sending them users by setting the referrer when
//      * launching our Custom Tab.
//      * @return The value of the Referrer Intent.
//      */
//     public static String customTabReferrerString() {
// 
//         int URI_ANDROID_APP_SCHEME;
// 
//         // Prepare for SDK version compatibility problems.
//         if(Build.VERSION.SDK_INT < 22)
//             URI_ANDROID_APP_SCHEME = 1<<1;
//         else
//             URI_ANDROID_APP_SCHEME = Intent.URI_ANDROID_APP_SCHEME;
// 
//         return URI_ANDROID_APP_SCHEME + "//" + MarsExplorerApplication.getApplicationInstance()
//                 .getPackageName();
//     }
// 
//     /**
//      * @return The Referrer intent key.
//      */
//     public static String customTabReferrerKey() {
// 
//         String EXTRA_REFERRER;
// 
//         // Prepare for SDK version compatibility problems.
//         if(Build.VERSION.SDK_INT < 17)
//             EXTRA_REFERRER = "android.intent.extra.REFERRER";
//         else
//             EXTRA_REFERRER = Intent.EXTRA_REFERRER;
// 
//         return EXTRA_REFERRER;
//     }
// }


import java.io.IOException;
import io.github.krtkush.marsexplorer.RESTClients.RestClientConstants;
import io.github.krtkush.marsexplorer.UtilityMethods;
import okhttp3.Interceptor;
import okhttp3.Request;
import okhttp3.Response;
import timber.log.Timber;
package io.github.krtkush.marsexplorer.RESTClients.Interceptors;



/**
 * Created by kartikeykushwaha on 08/06/16.
 */

/**
 * Interceptor to cache data and maintain it for four weeks.
 *
 * If the device is offline, stale (at most four weeks old) response is fetched from the cache.
 */
public class OfflineResponseCacheInterceptor implements Interceptor {
    @Override
    public Response intercept(Chain chain) throws IOException {

        Request request = chain.request();
        if(Boolean.valueOf(request.header(RestClientConstants.offlineCachingFlagHeader))) {
            Timber.i("Offline cache applied");
// Path: app/src/main/java/io/github/krtkush/marsexplorer/RESTClients/RestClientConstants.java
// public class  RestClientConstants {
// 
//     // Strings related to NASA's API
//     private static final String nasaApiEndPoint = "https://api.nasa.gov/";
//     private static final String nasaApiServiceName = "mars-photos/";
//     private static final String nasaApiVersion = "api/v1/rovers/";
//     public static final String nasaApiBaseUrl = nasaApiEndPoint + nasaApiServiceName
//             + nasaApiVersion;
// 
//     // Strings related to Mars Weather's API
//     private static final String marsWeatherEndPoint = "http://cab.inta-csic.es/";
//     private static final String marsWeatherAddress = "rems/wp-content/plugins/marsweather-widget/";
//     public static final String marsWeatherBaseUrl = marsWeatherEndPoint + marsWeatherAddress;
// 
//     // Custom headers
//     public static final String responseCachingFlagHeader = "ApplyResponseCache";
//     public static final String offlineCachingFlagHeader = "ApplyOfflineCache";
// 
//     // Cache directory name
//     public static final String apiResponsesCache = "apiResponses";
// }
// 
// Path: app/src/main/java/io/github/krtkush/marsexplorer/UtilityMethods.java
// public class UtilityMethods {
// 
//     /**
//      * Method to detect network connection on the device
//      * @return
//      */
//     public static boolean isNetworkAvailable() {
// 
//         ConnectivityManager connectivityManager =
//                 (ConnectivityManager) MarsExplorerApplication.getApplicationInstance()
//                         .getSystemService(MarsExplorerApplication.getApplicationInstance()
//                                 .CONNECTIVITY_SERVICE);
// 
//         return connectivityManager.getActiveNetworkInfo() != null
//                 && connectivityManager.getActiveNetworkInfo().isConnectedOrConnecting();
//     }
// 
//     /**
//      * It's usually very important for websites to track where their traffic is coming from.
//      * We make sure to let them know that we are sending them users by setting the referrer when
//      * launching our Custom Tab.
//      * @return The value of the Referrer Intent.
//      */
//     public static String customTabReferrerString() {
// 
//         int URI_ANDROID_APP_SCHEME;
// 
//         // Prepare for SDK version compatibility problems.
//         if(Build.VERSION.SDK_INT < 22)
//             URI_ANDROID_APP_SCHEME = 1<<1;
//         else
//             URI_ANDROID_APP_SCHEME = Intent.URI_ANDROID_APP_SCHEME;
// 
//         return URI_ANDROID_APP_SCHEME + "//" + MarsExplorerApplication.getApplicationInstance()
//                 .getPackageName();
//     }
// 
//     /**
//      * @return The Referrer intent key.
//      */
//     public static String customTabReferrerKey() {
// 
//         String EXTRA_REFERRER;
// 
//         // Prepare for SDK version compatibility problems.
//         if(Build.VERSION.SDK_INT < 17)
//             EXTRA_REFERRER = "android.intent.extra.REFERRER";
//         else
//             EXTRA_REFERRER = Intent.EXTRA_REFERRER;
// 
//         return EXTRA_REFERRER;
//     }
// }


// Path: app/src/main/java/io/github/krtkush/marsexplorer/RESTClients/Interceptors/OfflineResponseCacheInterceptor.java
import java.io.IOException;
import io.github.krtkush.marsexplorer.RESTClients.RestClientConstants;
import io.github.krtkush.marsexplorer.UtilityMethods;
import okhttp3.Interceptor;
import okhttp3.Request;
import okhttp3.Response;
import timber.log.Timber;

package io.github.krtkush.marsexplorer.RESTClients.Interceptors;



/**
 * Created by kartikeykushwaha on 08/06/16.
 */

/**
 * Interceptor to cache data and maintain it for four weeks.
 *
 * If the device is offline, stale (at most four weeks old) response is fetched from the cache.
 */
public class OfflineResponseCacheInterceptor implements Interceptor {
    @Override
    public Response intercept(Chain chain) throws IOException {

        Request request = chain.request();
        if(Boolean.valueOf(request.header(RestClientConstants.offlineCachingFlagHeader))) {
            Timber.i("Offline cache applied");
            if(!UtilityMethods.isNetworkAvailable()) {