Java Code Examples for android.app.Application#getCacheDir()

The following examples show how to use android.app.Application#getCacheDir() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: CrashHelper.java    From Android-utils with Apache License 2.0 6 votes vote down vote up
/**
 * 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;
    }
}
 
Example 2
Source File: ApiModule.java    From Dagger2-Sample with MIT License 5 votes vote down vote up
@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);
}
 
Example 3
Source File: DataModule.java    From Pioneer with Apache License 2.0 5 votes vote down vote up
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;
}
 
Example 4
Source File: DebugViewActivity.java    From DebugDrawer with Apache License 2.0 5 votes vote down vote up
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);
}
 
Example 5
Source File: MainActivity.java    From DebugDrawer with Apache License 2.0 5 votes vote down vote up
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);
}
 
Example 6
Source File: BaseSuggestionsModel.java    From Xndroid with GNU General Public License v3.0 5 votes vote down vote up
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();
}
 
Example 7
Source File: DataModule.java    From u2020-mvp with Apache License 2.0 5 votes vote down vote up
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);

}
 
Example 8
Source File: DataModule.java    From Pioneer with Apache License 2.0 5 votes vote down vote up
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;
}
 
Example 9
Source File: DataModule.java    From dagger2-example with MIT License 5 votes vote down vote up
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;
}
 
Example 10
Source File: OkClientFactory.java    From Learning-Resources with MIT License 5 votes vote down vote up
@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();
}
 
Example 11
Source File: OkClientFactory.java    From Learning-Resources with MIT License 5 votes vote down vote up
@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();
}
 
Example 12
Source File: DataModule.java    From u2020 with Apache License 2.0 5 votes vote down vote up
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);
}
 
Example 13
Source File: BookmarkPage.java    From JumpGo with Mozilla Public License 2.0 4 votes vote down vote up
@NonNull
private static File getDefaultIconFile(@NonNull Application application) {
    return new File(application.getCacheDir(), DEFAULT_ICON);
}
 
Example 14
Source File: BookmarkPage.java    From JumpGo with Mozilla Public License 2.0 4 votes vote down vote up
@NonNull
private static File getFaviconFile(@NonNull Application application) {
    return new File(application.getCacheDir(), FOLDER_ICON);
}
 
Example 15
Source File: NetworkModule.java    From Popular-Movies-App with Apache License 2.0 4 votes vote down vote up
@Provides
@Singleton
Cache providesOkHttpCache(Application application) {
    return new Cache(application.getCacheDir(), CACHE_SIZE);
}
 
Example 16
Source File: DataModule.java    From droidconat-2016 with Apache License 2.0 4 votes vote down vote up
@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);
}
 
Example 17
Source File: BookmarkPage.java    From Xndroid with GNU General Public License v3.0 4 votes vote down vote up
@NonNull
private static File getDefaultIconFile(@NonNull Application application) {
    return new File(application.getCacheDir(), DEFAULT_ICON);
}
 
Example 18
Source File: BookmarkPage.java    From Xndroid with GNU General Public License v3.0 4 votes vote down vote up
@NonNull
private static File getFaviconFile(@NonNull Application application) {
    return new File(application.getCacheDir(), FOLDER_ICON);
}
 
Example 19
Source File: ApiModule.java    From AndroidBlueprints with Apache License 2.0 4 votes vote down vote up
@Provides
@Singleton
Cache provideOkHttpCache(Application application) {
    int cacheSize = 10 * 1024 * 1024; // 10 MiB
    return new Cache(application.getCacheDir(), cacheSize);
}
 
Example 20
Source File: FaviconModel.java    From Xndroid with GNU General Public License v3.0 3 votes vote down vote up
/**
 * 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");
}