-
-
Notifications
You must be signed in to change notification settings - Fork 3.5k
Expand file tree
/
Copy pathCacheDirUtils.java
More file actions
66 lines (56 loc) · 2.21 KB
/
CacheDirUtils.java
File metadata and controls
66 lines (56 loc) · 2.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
package org.schabi.newpipe.util;
import java.io.File;
import android.content.Context;
import androidx.annotation.NonNull;
public final class CacheDirUtils {
private CacheDirUtils() {
// no instance
}
public static String getExternalAppCacheDirPath(
@NonNull final Context context) {
final File externalCacheDir = context.getExternalCacheDir();
if (null != externalCacheDir) {
// /storage/emulated/0/Android/data/<package_name>/cache/
return externalCacheDir.getAbsolutePath();
}
return null;
}
public static String getInternalAppCacheDirPath(
@NonNull final Context context) {
// always available, never be 'null'
// /data/user/0/<package_name>/cache/
return context.getCacheDir().getAbsolutePath();
}
/**
* Returns the preferred cache directory path for the application.
*
* Prefers the external cache directory when available
* (user-accessible, larger space),
* falls back to the internal private cache directory otherwise
* (always available, more secure).
*
* Typical paths:
* - External: /storage/emulated/0/Android/data/<package_name>/cache/
* - Internal: /data/user/0/<package_name>/cache/
* (or /data/data/<package_name>/cache/ on some devices)
*
* Note: The 'external' and 'internal' cache directories mentioned above
* are Android terms. They are typically located on the device's
* built-in storage and are not related to removable SD/TF cards.
*
* User "Clear Cache" in app settings deletes files in both locations.
*
* @param context used to get the available cache dir
* @return absolute path string, never null
*/
@NonNull
public static String getPreferredAppCacheDirPath(
@NonNull final Context context) {
final String externalCacheDirPath = getExternalAppCacheDirPath(context);
if (null != externalCacheDirPath) {
return externalCacheDirPath;
}
// Internal cache dir should always be available
return getInternalAppCacheDirPath(context);
}
}