// Path: app/src/main/java/name/caiyao/microreader/MicroApplication.java
// public class MicroApplication extends Application {
// 
//     public static MicroApplication microApplication = null;
// 
//     @Override
//     public void onCreate() {
//         super.onCreate();
// 
//         BugtagsOptions options = new BugtagsOptions.Builder().
//                 trackingLocation(true).//是否获取位置
//                 trackingCrashLog(!BuildConfig.DEBUG).//是否收集crash
//                 trackingConsoleLog(true).//是否收集console log
//                 trackingUserSteps(true).//是否收集用户操作步骤
//                 build();
//         Bugtags.start("9c1b1a3234ceeb5b9c531177a93b65ec", this, Bugtags.BTGInvocationEventNone, options);
//         MobclickAgent.setCatchUncaughtExceptions(false);
//         microApplication = this;
//     }
// 
//     public static Context getContext(){
//         return microApplication;
//     }
// }
// 
// Path: app/src/main/java/name/caiyao/microreader/utils/NetWorkUtil.java
// public class NetWorkUtil {
// 
//     private NetWorkUtil() {}
// 
//     /**
//      * 判断网络是否可用
//      *
//      * @param context
//      * @return
//      */
//     public static boolean isNetWorkAvailable(Context context) {
//         ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
//         NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();
//         return networkInfo != null && networkInfo.isConnected();
//     }
// 
//     /**
//      * 检测wifi是否连接
//      *
//      * @return
//      */
//     public static boolean isWifiConnected(Context context) {
//         ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
//         NetworkInfo networkInfo = cm.getActiveNetworkInfo();
//         return networkInfo != null && networkInfo.getType() == ConnectivityManager.TYPE_WIFI;
//     }
// 
//     /**
//      * 检测3G是否连接
//      *
//      * @return
//      */
//     public static boolean is3gConnected(Context context) {
//         ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
//         NetworkInfo networkInfo = cm.getActiveNetworkInfo();
//         return networkInfo != null && networkInfo.getType() == ConnectivityManager.TYPE_MOBILE;
//     }
// }


import java.io.File;
import java.io.IOException;
import name.caiyao.microreader.MicroApplication;
import name.caiyao.microreader.utils.NetWorkUtil;
import okhttp3.Cache;
import okhttp3.Interceptor;
import okhttp3.OkHttpClient;
import okhttp3.Response;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava.RxJavaCallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;
package name.caiyao.microreader.api.zhihu;



/**
 * Created by 蔡小木 on 2016/3/6 0006.
 */
public class ZhihuRequest {

    private ZhihuRequest() {}

    private static final Interceptor REWRITE_CACHE_CONTROL_INTERCEPTOR = new Interceptor() {
        @Override
        public Response intercept(Chain chain) throws IOException {
            Response originalResponse = chain.proceed(chain.request());
            if (NetWorkUtil.isNetWorkAvailable(MicroApplication.getContext())) {
                int maxAge = 60; // 在线缓存在1分钟内可读取
                return originalResponse.newBuilder()
                        .removeHeader("Pragma")
                        .removeHeader("Cache-Control")
                        .header("Cache-Control", "public, max-age=" + maxAge)
                        .build();
            } else {
                int maxStale = 60 * 60 * 24 * 28; // 离线时缓存保存4周
                return originalResponse.newBuilder()
                        .removeHeader("Pragma")
                        .removeHeader("Cache-Control")
                        .header("Cache-Control", "public, only-if-cached, max-stale=" + maxStale)
                        .build();
            }
        }
    };
// Path: app/src/main/java/name/caiyao/microreader/MicroApplication.java
// public class MicroApplication extends Application {
// 
//     public static MicroApplication microApplication = null;
// 
//     @Override
//     public void onCreate() {
//         super.onCreate();
// 
//         BugtagsOptions options = new BugtagsOptions.Builder().
//                 trackingLocation(true).//是否获取位置
//                 trackingCrashLog(!BuildConfig.DEBUG).//是否收集crash
//                 trackingConsoleLog(true).//是否收集console log
//                 trackingUserSteps(true).//是否收集用户操作步骤
//                 build();
//         Bugtags.start("9c1b1a3234ceeb5b9c531177a93b65ec", this, Bugtags.BTGInvocationEventNone, options);
//         MobclickAgent.setCatchUncaughtExceptions(false);
//         microApplication = this;
//     }
// 
//     public static Context getContext(){
//         return microApplication;
//     }
// }
// 
// Path: app/src/main/java/name/caiyao/microreader/utils/NetWorkUtil.java
// public class NetWorkUtil {
// 
//     private NetWorkUtil() {}
// 
//     /**
//      * 判断网络是否可用
//      *
//      * @param context
//      * @return
//      */
//     public static boolean isNetWorkAvailable(Context context) {
//         ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
//         NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();
//         return networkInfo != null && networkInfo.isConnected();
//     }
// 
//     /**
//      * 检测wifi是否连接
//      *
//      * @return
//      */
//     public static boolean isWifiConnected(Context context) {
//         ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
//         NetworkInfo networkInfo = cm.getActiveNetworkInfo();
//         return networkInfo != null && networkInfo.getType() == ConnectivityManager.TYPE_WIFI;
//     }
// 
//     /**
//      * 检测3G是否连接
//      *
//      * @return
//      */
//     public static boolean is3gConnected(Context context) {
//         ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
//         NetworkInfo networkInfo = cm.getActiveNetworkInfo();
//         return networkInfo != null && networkInfo.getType() == ConnectivityManager.TYPE_MOBILE;
//     }
// }


// Path: app/src/main/java/name/caiyao/microreader/api/zhihu/ZhihuRequest.java
import java.io.File;
import java.io.IOException;
import name.caiyao.microreader.MicroApplication;
import name.caiyao.microreader.utils.NetWorkUtil;
import okhttp3.Cache;
import okhttp3.Interceptor;
import okhttp3.OkHttpClient;
import okhttp3.Response;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava.RxJavaCallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;

package name.caiyao.microreader.api.zhihu;



/**
 * Created by 蔡小木 on 2016/3/6 0006.
 */
public class ZhihuRequest {

    private ZhihuRequest() {}

    private static final Interceptor REWRITE_CACHE_CONTROL_INTERCEPTOR = new Interceptor() {
        @Override
        public Response intercept(Chain chain) throws IOException {
            Response originalResponse = chain.proceed(chain.request());
            if (NetWorkUtil.isNetWorkAvailable(MicroApplication.getContext())) {
                int maxAge = 60; // 在线缓存在1分钟内可读取
                return originalResponse.newBuilder()
                        .removeHeader("Pragma")
                        .removeHeader("Cache-Control")
                        .header("Cache-Control", "public, max-age=" + maxAge)
                        .build();
            } else {
                int maxStale = 60 * 60 * 24 * 28; // 离线时缓存保存4周
                return originalResponse.newBuilder()
                        .removeHeader("Pragma")
                        .removeHeader("Cache-Control")
                        .header("Cache-Control", "public, only-if-cached, max-stale=" + maxStale)
                        .build();
            }
        }
    };
    private static File httpCacheDirectory = new File(MicroApplication.getContext().getCacheDir(), "zhihuCache");