Skip to content

Commit 5981a90

Browse files
committed
[duplicated subtitle] Introduce CacheDirUtils for app cache directory selection.
- This commit introduces `CacheDirUtils` to centralize application cache directory selection logic. - The preferred cache directory path is now initialized in 'App.onCreate()' and passed to `SubtitleDeduplicator`, instead of relying on 'StateSaver.init().'
1 parent d3f7945 commit 5981a90

2 files changed

Lines changed: 72 additions & 0 deletions

File tree

app/src/main/java/org/schabi/newpipe/App.kt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,12 +34,14 @@ import org.schabi.newpipe.extractor.services.youtube.extractors.YoutubeStreamExt
3434
import org.schabi.newpipe.ktx.hasAssignableCause
3535
import org.schabi.newpipe.settings.NewPipeSettings
3636
import org.schabi.newpipe.util.BridgeStateSaverInitializer
37+
import org.schabi.newpipe.util.CacheDirUtils
3738
import org.schabi.newpipe.util.Localization
3839
import org.schabi.newpipe.util.ServiceHelper
3940
import org.schabi.newpipe.util.StateSaver
4041
import org.schabi.newpipe.util.image.ImageStrategy
4142
import org.schabi.newpipe.util.image.PreferredImageQuality
4243
import org.schabi.newpipe.util.potoken.PoTokenProviderImpl
44+
import org.schabi.newpipe.util.subtitle.SubtitleDeduplicator
4345

4446
/*
4547
* Copyright (C) Hans-Christoph Steiner 2016 <hans@eds.org>
@@ -93,6 +95,8 @@ open class App :
9395
.getInt(getString(R.string.last_used_preferences_version), -1)
9496
isFirstRun = lastUsedPrefVersion == -1
9597

98+
val appCacheDirPath = CacheDirUtils.getPreferredAppCacheDirPath(this)
99+
96100
// Initialize settings first because other initializations can use its values
97101
NewPipeSettings.initSettings(this)
98102

@@ -124,6 +128,8 @@ open class App :
124128
configureRxJavaErrorHandler()
125129

126130
YoutubeStreamExtractor.setPoTokenProvider(PoTokenProviderImpl)
131+
132+
SubtitleDeduplicator.setCacheDirPath(appCacheDirPath)
127133
}
128134

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

0 commit comments

Comments
 (0)