下面列出了android.app.Application#getCacheDir ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* Init crash log cache directory.
*
* @param application application
* @param crashDirPath crash log file cache directory
*/
private static void initCacheDir(Application application, final String crashDirPath) {
if (StringUtils.isSpace(crashDirPath)) {
dir = null;
} else {
dir = crashDirPath.endsWith(FILE_SEP) ? crashDirPath : crashDirPath + FILE_SEP;
}
if (Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())
&& application.getExternalCacheDir() != null) {
// defaultDir: /android/data/< package name >/cache/crash/...
defaultDir = application.getExternalCacheDir() + FILE_SEP + "crash" + FILE_SEP;
} else {
// defaultDir: /data/data/< package name >/cache/crash/...
defaultDir = application.getCacheDir() + FILE_SEP + "crash" + FILE_SEP;
}
}
@Provides
@Singleton
Cache provideCache(Application application) {
long cacheSize = 10 * 1024 * 1024; // 10 MB
File httpCacheDirectory = new File(application.getCacheDir(), "http-cache");
return new Cache(httpCacheDirectory, cacheSize);
}
static OkHttpClient createOkHttpClient(Application app) {
OkHttpClient client = new OkHttpClient();
// Install an HTTP cache in the application cache directory.
File cacheDir = new File(app.getCacheDir(), "http");
Cache cache = new Cache(cacheDir, DISK_CACHE_SIZE);
client.setCache(cache);
return client;
}
BaseSuggestionsModel(@NonNull Application application, @NonNull String encoding) {
mEncoding = encoding;
mLanguage = getLanguage();
File suggestionsCache = new File(application.getCacheDir(), "suggestion_responses");
mHttpClient = new OkHttpClient.Builder()
.cache(new Cache(suggestionsCache, FileUtils.megabytesToBytes(1)))
.addNetworkInterceptor(REWRITE_CACHE_CONTROL_INTERCEPTOR)
.build();
mCacheControl = new CacheControl.Builder().maxStale(1, TimeUnit.DAYS).build();
}
static OkHttpClient.Builder createOkHttpClient(Application app) {
// Install an HTTP cache in the application cache directory.
File cacheDir = new File(app.getCacheDir(), "http");
Cache cache = new Cache(cacheDir, DISK_CACHE_SIZE);
return new OkHttpClient.Builder()
.connectTimeout(10, TimeUnit.SECONDS)
.readTimeout(10, TimeUnit.SECONDS)
.writeTimeout(10, TimeUnit.SECONDS)
.cache(cache);
}
@NonNull
public static OkHttpClient provideOkHttpClient(Application app) {
// Install an HTTP cache in the application cache directory.
File cacheDir = new File(app.getCacheDir(), "http");
Cache cache = new Cache(cacheDir, DISK_CACHE_SIZE);
OkHttpClient.Builder builder = new OkHttpClient.Builder()
.cache(cache);
if (BuildConfig.DEBUG) {
HttpLoggingInterceptor loggingInterceptor = new HttpLoggingInterceptor();
loggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
builder.interceptors().add(loggingInterceptor);
}
return builder.build();
}
@NonNull
public static OkHttpClient provideOkHttpClient(Application app) {
// Install an HTTP cache in the application cache directory.
File cacheDir = new File(app.getCacheDir(), "http");
Cache cache = new Cache(cacheDir, DISK_CACHE_SIZE);
OkHttpClient.Builder builder = new OkHttpClient.Builder()
.cache(cache);
if (BuildConfig.DEBUG) {
HttpLoggingInterceptor loggingInterceptor = new HttpLoggingInterceptor();
loggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
builder.interceptors().add(loggingInterceptor);
}
return builder.build();
}
static OkHttpClient.Builder createOkHttpClient(Application app) {
// Install an HTTP cache in the application cache directory.
File cacheDir = new File(app.getCacheDir(), "http");
Cache cache = new Cache(cacheDir, DISK_CACHE_SIZE);
return new OkHttpClient.Builder()
.cache(cache);
}
public static OkHttpClient createOkHttpClient(Application app) {
OkHttpClient client = new OkHttpClient();
// Install an HTTP cache in the application cache directory.
try {
File cacheDir = new File(app.getCacheDir(), "http");
Cache cache = new Cache(cacheDir, DISK_CACHE_SIZE);
client.setCache(cache);
} catch(IOException e) {
Log.e("DatModule", "Unable to install disk cache.", e);
}
return client;
}
static OkHttpClient createOkHttpClient(Application app) {
OkHttpClient client = new OkHttpClient();
// Install an HTTP cache in the application cache directory.
File cacheDir = new File(app.getCacheDir(), "http");
Cache cache = new Cache(cacheDir, DISK_CACHE_SIZE);
client.setCache(cache);
return client;
}
private static OkHttpClient.Builder createOkHttpClientBuilder(Application app) {
// Install an HTTP cache in the application cache directory.
File cacheDir = new File(app.getCacheDir(), "okhttp3-cache");
Cache cache = new Cache(cacheDir, DISK_CACHE_SIZE);
return new OkHttpClient.Builder()
.cache(cache)
.addInterceptor(LogsModule.chuckInterceptor(app))
.addInterceptor(NetworkQualityModule.interceptor(app))
.readTimeout(10, TimeUnit.SECONDS)
.writeTimeout(10, TimeUnit.SECONDS)
.connectTimeout(10, TimeUnit.SECONDS);
}
private static OkHttpClient.Builder createOkHttpClientBuilder(Application app) {
// Install an HTTP cache in the application cache directory.
File cacheDir = new File(app.getCacheDir(), "okhttp3");
Cache cache = new Cache(cacheDir, DISK_CACHE_SIZE);
return new OkHttpClient.Builder()
.cache(cache)
.addInterceptor(LogsModule.chuckInterceptor(app))
.addInterceptor(NetworkQualityModule.interceptor(app))
.readTimeout(10, TimeUnit.SECONDS)
.writeTimeout(10, TimeUnit.SECONDS)
.connectTimeout(10, TimeUnit.SECONDS);
}
@Provides
@Singleton
Cache provideOkHttpCache(Application application) {
int cacheSize = 10 * 1024 * 1024; // 10 MiB
return new Cache(application.getCacheDir(), cacheSize);
}
@NonNull
private static File getFaviconFile(@NonNull Application application) {
return new File(application.getCacheDir(), FOLDER_ICON);
}
@NonNull
private static File getDefaultIconFile(@NonNull Application application) {
return new File(application.getCacheDir(), DEFAULT_ICON);
}
@Provides @Singleton OkHttpClient.Builder provideOkHttpClientBuilder(Application app) {
File cacheDir = new File(app.getCacheDir(), "http");
Cache cache = new Cache(cacheDir, DISK_CACHE_SIZE);
return new OkHttpClient.Builder().cache(cache);
}
@Provides
@Singleton
Cache providesOkHttpCache(Application application) {
return new Cache(application.getCacheDir(), CACHE_SIZE);
}
@NonNull
private static File getFaviconFile(@NonNull Application application) {
return new File(application.getCacheDir(), FOLDER_ICON);
}
@NonNull
private static File getDefaultIconFile(@NonNull Application application) {
return new File(application.getCacheDir(), DEFAULT_ICON);
}
/**
* Creates the cache file for the favicon
* image. File name will be in the form of
* [hash of URI host].png
*
* @param app the context needed to retrieve the
* cache directory.
* @param uri the URI to use as a unique identifier.
* @return a valid cache file.
*/
@WorkerThread
@NonNull
public static File getFaviconCacheFile(@NonNull Application app, @NonNull Uri uri) {
FaviconUtils.assertUriSafe(uri);
String hash = String.valueOf(uri.getHost().hashCode());
return new File(app.getCacheDir(), hash + ".png");
}