Example usage for com.squareup.okhttp CacheControl.Builder onlyIfCached

List of usage examples for com.squareup.okhttp CacheControl.Builder onlyIfCached

Introduction

In this page you can find the example usage for com.squareup.okhttp CacheControl.Builder onlyIfCached.

Prototype

boolean onlyIfCached

To view the source code for com.squareup.okhttp CacheControl.Builder onlyIfCached.

Click Source Link

Usage

From source file:retrofit.MagnetCall.java

License:Apache License

private CacheControl getCacheControl(CacheOptions options) {
    if (null != options) {
        if (Boolean.TRUE == options.isAlwaysUseCacheIfOffline() || options.getMaxCacheAge() > 0) {
            CacheControl.Builder builder = new CacheControl.Builder();
            // Always return from cache if offline
            if (Boolean.TRUE == options.isAlwaysUseCacheIfOffline()) {
                if (ConnectivityManager.getInstance()
                        .getConnectivityStatus() == ConnectivityManager.TYPE_NOT_CONNECTED) {
                    builder.onlyIfCached().maxAge(Integer.MAX_VALUE, TimeUnit.SECONDS).build();
                }//from   w  w  w .  j  a  v a2  s. c o m
            }
            if (options.getMaxCacheAge() > 0) { // Return from cache if it's not expired
                builder.maxAge(options.getMaxCacheAge(), TimeUnit.SECONDS);
            }

            return builder.build();
        }
    } else {

    }

    return null;
}