Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

(#172) Storage Access Framework Integration #292

Merged
merged 200 commits into from
Oct 31, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
200 commits
Select commit Hold shift + click to select a range
a59c485
Update README.md
K1rakishou Jul 15, 2019
3fb6063
Update README.md
K1rakishou Jul 15, 2019
72270df
Update README.md
K1rakishou Jul 15, 2019
48ca131
Update README.md
K1rakishou Jul 15, 2019
1604d02
Update README.md
K1rakishou Jul 15, 2019
ce880e8
Introduce travis CI
K1rakishou Jul 15, 2019
3ab6b25
Update README.md
K1rakishou Jul 15, 2019
c12c686
CI apk uploading
K1rakishou Jul 16, 2019
568197c
Merge remote-tracking branch 'origin/multi-feature' into multi-feature
K1rakishou Jul 16, 2019
725f752
Trigger CI build
K1rakishou Jul 16, 2019
abbf9ad
Trigger CI build
K1rakishou Jul 16, 2019
40e95ca
Merge branch 'multi-feature' into dev
K1rakishou Jul 19, 2019
a031496
Merge pull request #21 from K1rakishou/dev
K1rakishou Jul 19, 2019
6019ab1
Update README.md
K1rakishou Jul 20, 2019
e42adbd
(#172) SAF base API implementation
K1rakishou Aug 10, 2019
f537fdf
(#172) Couple of little code improvements
K1rakishou Aug 10, 2019
b08d175
(#172) Comments and more File API methods
K1rakishou Aug 11, 2019
d26cdcf
(#172) Settings exporting/importing now works with SAF
K1rakishou Aug 11, 2019
9d61e02
(#172) Image saving now works (with bugs) with SAF
K1rakishou Aug 11, 2019
05606fc
(#172) SAF fixes:
K1rakishou Aug 17, 2019
29cce2a
(#172) Image saving now works with SAF!
K1rakishou Aug 17, 2019
ff93a34
(#172) Remove fileManager.fromAbstractFile() method, add AbstractFile…
K1rakishou Aug 18, 2019
37c3a83
(#172) ImageSaving now works with both RawFiles and ExternalFiles (vi…
K1rakishou Aug 18, 2019
b61d619
(#172) Add comments. Remove duplicate name() method
K1rakishou Aug 18, 2019
2b692ab
(#172) Comments, refactoring and bug fixes. ImageSaving now work with…
K1rakishou Aug 18, 2019
a75df6c
(#172) Fixes and refactoring
K1rakishou Aug 18, 2019
11bde35
(#172) Add comments
K1rakishou Aug 24, 2019
0b5de59
(#172) Add local threads location setting (which is selected by the u…
K1rakishou Aug 24, 2019
9946068
(#172) Threads should now work with SAF (not tested yet)
K1rakishou Aug 24, 2019
b4f288b
(#172) Make most of the methods in AbstractFile immutable because it …
K1rakishou Aug 24, 2019
3e4f8a7
(#172) Tests for write to a SAF file, checking it's length and then r…
K1rakishou Aug 24, 2019
4044b72
(#172) Lots of improvements/fixes etc
K1rakishou Aug 25, 2019
941a3d2
(#172) Fix FileCacheDownloader bug where the output file wouldn't be …
K1rakishou Aug 25, 2019
b2878be
(#172) Return back lost SaveLocationController
K1rakishou Aug 25, 2019
1269cb0
(#172) Merge with dev branch
K1rakishou Aug 25, 2019
478e3a7
(#172) Remove empty files
K1rakishou Aug 25, 2019
6772d78
(#172) Fix grammar mistakes
K1rakishou Aug 25, 2019
a38b337
(#172) Fix media scanner scanning file with disabled setting
K1rakishou Aug 25, 2019
3e79291
(#172) Remove testMethod when choosing local threads location (since …
K1rakishou Aug 25, 2019
c0492b7
(#172) Revert strict mode back
K1rakishou Aug 25, 2019
b297828
(#172) Merge branch 'cheese-multi-feature' into (#172)-storage-access…
K1rakishou Aug 31, 2019
98f45e7
(#172) Attempt to implement migration from one local threads base dir…
K1rakishou Sep 1, 2019
177412e
Update travis.yml
K1rakishou Sep 8, 2019
33e0fa9
Update build script, trigger CI build
K1rakishou Sep 8, 2019
260200b
Make gradlew executable, trigger CI build
K1rakishou Sep 8, 2019
58ba49b
Fix gradle build command, trigger CI build
K1rakishou Sep 8, 2019
ddbee83
Attempt to fix it again, trigger CI build, turn off email notifications
K1rakishou Sep 8, 2019
9200627
Attempt to combine scripts into one, trigger CI build
K1rakishou Sep 8, 2019
974b8b3
Update upload_apk script, trigger CI build
K1rakishou Sep 8, 2019
49cca30
Remove pip install --upgrade pip command, trigger CI build
K1rakishou Sep 8, 2019
525ea3e
Use baseUrl for python scripts that is being passed from the travis.y…
K1rakishou Sep 8, 2019
fdd4de4
Fix lint warning, attempt to stop travis build upon script error
K1rakishou Sep 8, 2019
d9eb1c1
Ok, it works, now it should do everything up until apk uploading
K1rakishou Sep 8, 2019
daf5f8e
Trigger CI build, should finally see a built apk on the apk server
K1rakishou Sep 14, 2019
e2f8ded
Update upload_apk script, trigger CI build
K1rakishou Sep 14, 2019
efcc269
use pip3, trigger CI build
K1rakishou Sep 14, 2019
82b6df6
install python3-pip, trigger CI build
K1rakishou Sep 14, 2019
0fe50d9
add getLastTenCommits gradle task, update upload_apk script, trigger …
K1rakishou Sep 14, 2019
a1830fe
update upload_apk script, trigger CI build
K1rakishou Sep 14, 2019
40dc90b
use python3 to run the script instead of python2, trigger CI build
K1rakishou Sep 14, 2019
b4dad88
trigger CI build
K1rakishou Sep 14, 2019
d6943ef
use python 3.6, trigger CI build
K1rakishou Sep 14, 2019
c51d6f1
trigger CI build
K1rakishou Sep 14, 2019
2a95972
move before_install commands into install commands, trigger CI build
K1rakishou Sep 14, 2019
4ed813d
another attempt to use python 3.6, trigger CI build
K1rakishou Sep 14, 2019
de3fe27
update upload_apk script, trigger CI build
K1rakishou Sep 14, 2019
06e395b
update upload_apk script, trigger CI build
K1rakishou Sep 14, 2019
37393b2
update upload_apk script, trigger CI build
K1rakishou Sep 14, 2019
09cbd94
update upload_apk script, trigger CI build
K1rakishou Sep 14, 2019
c6e8bf9
update upload_apk script, trigger CI build
K1rakishou Sep 14, 2019
90c58d3
update upload_apk script, trigger CI build
K1rakishou Sep 14, 2019
63aa5c9
update upload_apk script, trigger CI build
K1rakishou Sep 14, 2019
ea2c642
update upload_apk script, trigger CI build
K1rakishou Sep 14, 2019
fed21f1
update upload_apk script, trigger CI build
K1rakishou Sep 14, 2019
58eb4ac
update upload_apk script, trigger CI build
K1rakishou Sep 14, 2019
f709fa8
update upload_apk script, trigger CI build
K1rakishou Sep 14, 2019
04e2933
update upload_apk script, trigger CI build
K1rakishou Sep 14, 2019
7ac62ac
update upload_apk script, trigger CI build
K1rakishou Sep 14, 2019
e6b696b
update upload_apk script, trigger CI build
K1rakishou Sep 14, 2019
383d434
update upload_apk script, trigger CI build
K1rakishou Sep 14, 2019
afbab46
update upload_apk script, trigger CI build
K1rakishou Sep 14, 2019
9ca3785
update upload_apk script, trigger CI build
K1rakishou Sep 14, 2019
0f4351f
update upload_apk script, trigger CI build
K1rakishou Sep 14, 2019
8b31959
update upload_apk script, trigger CI build
K1rakishou Sep 14, 2019
a51c9a9
update upload_apk script, trigger CI build
K1rakishou Sep 14, 2019
56de0d2
update upload_apk script, trigger CI build
K1rakishou Sep 14, 2019
7500c01
update upload_apk script, trigger CI build
K1rakishou Sep 14, 2019
07d69e1
update upload_apk script, trigger CI build
K1rakishou Sep 15, 2019
e7c8054
update upload_apk script, trigger CI build
K1rakishou Sep 15, 2019
2eef0be
update upload_apk script, trigger CI build
K1rakishou Sep 15, 2019
89917a6
update upload_apk script, trigger CI build
K1rakishou Sep 15, 2019
e631e08
update upload_apk script, trigger CI build
K1rakishou Sep 15, 2019
d22d436
trigger CI build
K1rakishou Sep 15, 2019
e30da58
Trigger CI build
K1rakishou Sep 15, 2019
da8f651
trigger CI build
K1rakishou Sep 15, 2019
770656a
add getVersionCode gradle task, trigger CI build
K1rakishou Sep 15, 2019
0cff1d3
trigger CI build
K1rakishou Sep 15, 2019
55a43b1
trigger CI build
K1rakishou Sep 15, 2019
27702ec
trigger CI build
K1rakishou Sep 15, 2019
b75531c
trigger CI build
K1rakishou Sep 15, 2019
8bf82ca
trigger CI build
K1rakishou Sep 15, 2019
01cf302
trigger CI build
K1rakishou Sep 15, 2019
e4b0b9f
use getLatestCommit, trigger CI build
K1rakishou Sep 15, 2019
f10fc3e
trigger CI build
K1rakishou Sep 15, 2019
7cb6939
trigger CI build
K1rakishou Sep 15, 2019
3a85370
trigger CI build
K1rakishou Sep 15, 2019
aea3ecd
trigger CI build
K1rakishou Sep 15, 2019
4d5a28b
trigger CI build
K1rakishou Sep 15, 2019
28b6e4a
trigger CI build
K1rakishou Sep 15, 2019
29fcac3
trigger CI build
K1rakishou Sep 15, 2019
8512269
update upload_apk script, trigger CI build
K1rakishou Sep 15, 2019
8c46a0b
trigger CI build
K1rakishou Sep 15, 2019
53558ee
trigger CI build
K1rakishou Sep 15, 2019
fbb99b0
trigger CI build
K1rakishou Sep 15, 2019
5ed0a65
trigger CI build
K1rakishou Sep 15, 2019
c945f7a
trigger CI build
K1rakishou Sep 15, 2019
d5b07fe
trigger CI build
K1rakishou Sep 15, 2019
781b4fc
trigger CI build
K1rakishou Sep 15, 2019
974a9a4
trigger CI build
K1rakishou Sep 15, 2019
f9aeb7a
trigger CI build
K1rakishou Sep 15, 2019
63b13b3
trigger CI build
K1rakishou Sep 15, 2019
10809f3
trigger CI build
K1rakishou Sep 15, 2019
4c97a45
trigger CI build
K1rakishou Sep 15, 2019
f331d42
trigger CI build
K1rakishou Sep 15, 2019
34a1f35
trigger CI build
K1rakishou Sep 15, 2019
a11afb6
test commit 1
K1rakishou Sep 16, 2019
d81e177
test commit 1
K1rakishou Sep 18, 2019
1e4c8bf
test commit 2
K1rakishou Sep 18, 2019
ec29342
test commit 3
K1rakishou Sep 18, 2019
caf2b41
test commit 0
K1rakishou Sep 18, 2019
a89723e
test commit 1
K1rakishou Sep 18, 2019
4e04624
test commit 2
K1rakishou Sep 18, 2019
1e99d9b
test commit 3
K1rakishou Sep 18, 2019
3119f4c
test commit 4
K1rakishou Sep 18, 2019
57bd380
test commit 5
K1rakishou Sep 18, 2019
d8cc0f8
upload_apk scripts fixes
K1rakishou Sep 18, 2019
6dc0265
change branch name
K1rakishou Sep 18, 2019
0a80dc1
update upload_apk script
K1rakishou Sep 18, 2019
c7f815f
Change branch name back to multi-feature
K1rakishou Sep 18, 2019
0b66bd2
upload_apk script fixes
K1rakishou Sep 18, 2019
0f1451a
upload_apk script fixes
K1rakishou Sep 18, 2019
551fe3b
shit works!
K1rakishou Sep 18, 2019
d923fef
test commit 1
K1rakishou Sep 18, 2019
d92492c
test commit 2
K1rakishou Sep 18, 2019
59cce6b
test commit 3
K1rakishou Sep 18, 2019
ee19bef
Fix git log command would include a commit with hash that we are pass…
K1rakishou Sep 18, 2019
1c92139
test commit 1
K1rakishou Sep 18, 2019
15db792
test commit 2
K1rakishou Sep 18, 2019
f31fcd1
Check whether a branch exists before doing the uploading
K1rakishou Sep 18, 2019
0acfc14
Merge pull request #22 from K1rakishou/test-my-multi-feature
K1rakishou Sep 18, 2019
5fb53df
test commit 1
K1rakishou Sep 18, 2019
20be69f
test commit 2
K1rakishou Sep 18, 2019
c6f0fd0
remove --first-parent
K1rakishou Sep 18, 2019
2fcfb9f
remove --first-parent, fix upload_apk script
K1rakishou Sep 18, 2019
98e2315
upload_apk script improvements
K1rakishou Sep 18, 2019
c55a9bc
Merge branch 'my-multi-feature' into test-my-miltifeature-branch
K1rakishou Sep 18, 2019
857bc4b
test commit 3
K1rakishou Sep 18, 2019
56e1406
test commit 4
K1rakishou Sep 18, 2019
ceb1021
update upload_apk script
K1rakishou Sep 18, 2019
1b27db6
Merge branch 'my-multi-feature' into test-my-miltifeature-branch
K1rakishou Sep 18, 2019
fc1bfd3
update upload_apk script
K1rakishou Sep 18, 2019
6db6d0d
Merge pull request #23 from K1rakishou/test-my-miltifeature-branch
K1rakishou Sep 18, 2019
342b7b6
Update gradle to use apk name with versionNameSuffix that depends on …
K1rakishou Sep 19, 2019
31ea72c
Merge remote-tracking branch 'origin/multi-feature' into my-multi-fea…
K1rakishou Sep 19, 2019
425928d
Use ISO datetime for commits
K1rakishou Sep 19, 2019
cd3a908
test commit 1
K1rakishou Sep 19, 2019
ceeb4ec
test commit 2
K1rakishou Sep 19, 2019
06ca9f1
test commit 3
K1rakishou Sep 19, 2019
db134bf
Merge pull request #24 from K1rakishou/test-my-multi-feature
K1rakishou Sep 19, 2019
acf04ae
Pass secret key via travis secure variables
K1rakishou Sep 19, 2019
8bed1ff
Merge remote-tracking branch 'origin/multi-feature' into my-multi-fea…
K1rakishou Sep 19, 2019
d054039
Update upload_apk script
K1rakishou Sep 19, 2019
79fb0d0
test commit 1
K1rakishou Sep 19, 2019
89bcfd9
test commit 2
K1rakishou Sep 19, 2019
6338da8
test commit 3
K1rakishou Sep 19, 2019
ee240fd
Disable ApkUpdater for dev builds for now (it doesn't work with the a…
K1rakishou Sep 19, 2019
8acb726
Merge pull request #25 from K1rakishou/test-my-multi-feature
K1rakishou Sep 19, 2019
9dff30c
Add comments
K1rakishou Sep 20, 2019
3f69a6f
Remove redundant line
K1rakishou Sep 20, 2019
15aa2b1
Add one important comment
K1rakishou Sep 20, 2019
d410a80
Update README.md
K1rakishou Sep 20, 2019
80fe48d
Update README.md
K1rakishou Sep 20, 2019
25262f2
Update upload_apk script, trigger CI build
K1rakishou Sep 21, 2019
bfc8f51
Merge remote-tracking branch 'origin/multi-feature' into travis-ci-in…
K1rakishou Sep 21, 2019
0706523
Remove redundant line in travis script
K1rakishou Sep 21, 2019
4861305
(#172) Migrate to a separate (home made) library that handles SAF bul…
K1rakishou Oct 19, 2019
b2ce0b9
(#172) Significantly improved speed of local threads downloading
K1rakishou Oct 20, 2019
fcde52d
(#172) Fix a bug where the downloading thread won't be marked as stop…
K1rakishou Oct 20, 2019
49b1f69
(#172) SAF integration is completed
K1rakishou Oct 26, 2019
5fac46a
(#172) Move all strings to resources
K1rakishou Oct 26, 2019
1eb7521
(#172) Update Fuck-Storage-Access-Framework to the latest release ver…
K1rakishou Oct 27, 2019
24d792d
(#172) Use generic base newBaseDirectoryFile when calling it from the…
K1rakishou Oct 27, 2019
2b30039
(#172) Merge remote-tracking branch 'remotes/origin/multi-feature' in…
K1rakishou Oct 27, 2019
8c2f6a6
(#172) Merge with develop
K1rakishou Oct 27, 2019
97a7b2b
(#172) Fix compilation error
K1rakishou Oct 27, 2019
0184fab
(#172) Fix image saving related bugs, like file separators filtered i…
K1rakishou Oct 27, 2019
ff2d6c1
(#172) Fix ImageLoaderV2 setting bitmap into a request field (haha)
K1rakishou Oct 27, 2019
5fe98c2
(#172) Add a warning alert dialog that will be shown when the user at…
K1rakishou Oct 27, 2019
4dba625
(#172) Remove merge conflict marker from travis.yml
K1rakishou Oct 27, 2019
8e90850
(#172) Set the CURRENT_EXPORT_SETTINGS_VERSION to what it should be (5)
K1rakishou Oct 27, 2019
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions Kuroba/app/build.gradle
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
apply plugin: 'kotlin-kapt'

android {
// !!! IMPORTANT !!!
Expand Down Expand Up @@ -72,6 +75,10 @@ android {
targetCompatibility JavaVersion.VERSION_1_8
}

kotlinOptions {
freeCompilerArgs = ["-Xallow-result-return-type"]
}

lintOptions {
abortOnError false
}
Expand Down Expand Up @@ -215,6 +222,10 @@ dependencies {
implementation 'com.vdurmont:emoji-java:5.1.0'
implementation 'io.reactivex.rxjava2:rxjava:2.2.12'
implementation 'io.reactivex.rxjava2:rxandroid:2.1.1'
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
implementation 'com.github.K1rakishou:Fuck-Storage-Access-Framework:v1.0-alpha'

testImplementation 'junit:junit:4.12'
}

//========================================================
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,10 @@
import com.github.adamantcheese.chan.ui.theme.ThemeHelper;
import com.github.adamantcheese.chan.utils.AndroidUtils;
import com.github.adamantcheese.chan.utils.Logger;
import com.github.k1rakishou.fsaf.FileChooser;
import com.github.k1rakishou.fsaf.callback.FSAFActivityCallbacks;

import org.jetbrains.annotations.NotNull;

import java.io.PrintWriter;
import java.io.StringWriter;
Expand All @@ -73,7 +77,10 @@
import static com.github.adamantcheese.chan.Chan.inject;
import static com.github.adamantcheese.chan.utils.AndroidUtils.getApplicationLabel;

public class StartActivity extends AppCompatActivity implements NfcAdapter.CreateNdefMessageCallback {
public class StartActivity
extends AppCompatActivity
implements NfcAdapter.CreateNdefMessageCallback,
FSAFActivityCallbacks {
private static final String TAG = "StartActivity";

private static final String STATE_KEY = "chan_state";
Expand All @@ -94,15 +101,14 @@ public class StartActivity extends AppCompatActivity implements NfcAdapter.Creat

@Inject
DatabaseManager databaseManager;

@Inject
WatchManager watchManager;

@Inject
SiteResolver siteResolver;

@Inject
SiteService siteService;
@Inject
FileChooser fileChooser;

@Override
protected void onCreate(Bundle savedInstanceState) {
Expand All @@ -115,6 +121,8 @@ protected void onCreate(Bundle savedInstanceState) {

Chan.injector().instance(ThemeHelper.class).setupContext(this);

fileChooser.setCallbacks(this);

imagePickDelegate = new ImagePickDelegate(this);
runtimePermissionsHelper = new RuntimePermissionsHelper(this);
updateManager = new UpdateManager(this);
Expand Down Expand Up @@ -534,6 +542,8 @@ protected void onDestroy() {
return;
}

fileChooser.removeCallbacks();

// TODO: clear whole stack?
stackTop().onHide();
stackTop().onDestroy();
Expand All @@ -544,7 +554,13 @@ protected void onDestroy() {
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);

imagePickDelegate.onActivityResult(requestCode, resultCode, data);
if (fileChooser.onActivityResult(requestCode, resultCode, data)) {
return;
}

if (imagePickDelegate.onActivityResult(requestCode, resultCode, data)) {
return;
}
}

private Controller stackTop() {
Expand Down Expand Up @@ -581,4 +597,9 @@ public void restartApp() {

Runtime.getRuntime().exit(0);
}

@Override
public void fsafStartActivityForResult(@NotNull Intent intent, int requestCode) {
startActivityForResult(intent, requestCode);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,13 @@

import com.github.adamantcheese.chan.core.settings.ChanSettings;
import com.github.adamantcheese.chan.utils.Logger;
import com.github.k1rakishou.fsaf.FileManager;
import com.github.k1rakishou.fsaf.file.AbstractFile;
import com.github.k1rakishou.fsaf.file.FileSegment;
import com.github.k1rakishou.fsaf.file.RawFile;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
Expand All @@ -38,10 +43,11 @@ public class CacheHandler {
private static final String TAG = "CacheHandler";
//1GB for prefetching, so that entire threads can be loaded at once more easily, otherwise 100MB is plenty
private static final long FILE_CACHE_DISK_SIZE = (ChanSettings.autoLoadThreadImages.get() ? 1000 : 100) * 1024 * 1024;
private static final String CACHE_EXTENSION = "cache";

private final ExecutorService pool = Executors.newSingleThreadExecutor();

private final File directory;
private final FileManager fileManager;
private final RawFile cacheDirFile;

/**
* An estimation of the current size of the directory. Used to check if trim must be run
Expand All @@ -50,22 +56,49 @@ public class CacheHandler {
private AtomicLong size = new AtomicLong();
private AtomicBoolean trimRunning = new AtomicBoolean(false);

public CacheHandler(File directory) {
this.directory = directory;
public CacheHandler(FileManager fileManager, RawFile cacheDirFile) {
this.fileManager = fileManager;
this.cacheDirFile = cacheDirFile;

createDirectories();
backgroundRecalculateSize();
}

@MainThread
public boolean exists(String key) {
return get(key).exists();
return fileManager.exists(get(key));
}

@MainThread
public File get(String key) {
public RawFile get(String key) {
createDirectories();
return new File(directory, String.valueOf(key.hashCode()));

String fileName = String.format(
"%s.%s",
// We need extension here because AbstractFile expects all file names to have
// extensions
String.valueOf(key.hashCode()), CACHE_EXTENSION);

return (RawFile) cacheDirFile
.clone(new FileSegment(fileName));
}

public File randomCacheFile() throws IOException {
createDirectories();

File cacheDir = new File(cacheDirFile.getFullPath());
File newFile = new File(cacheDir, String.valueOf(System.nanoTime()));

while (newFile.exists()) {
newFile = new File(cacheDir, String.valueOf(System.nanoTime()));
}

if (!newFile.createNewFile()) {
throw new IOException("Could not create new file in cache directory, newFile = "
+ newFile.getAbsolutePath());
}

return newFile;
}

@MainThread
Expand Down Expand Up @@ -94,11 +127,11 @@ protected void fileWasAdded(long fileLen) {
public void clearCache() {
Logger.d(TAG, "Clearing cache");

if (directory.exists() && directory.isDirectory()) {
for (File file : directory.listFiles()) {
if (!file.delete()) {
if (fileManager.exists(cacheDirFile) && fileManager.isDirectory(cacheDirFile)) {
for (AbstractFile file : fileManager.listFiles(cacheDirFile)) {
if (!fileManager.delete(file)) {
Logger.d(TAG, "Could not delete cache file while clearing cache " +
file.getName());
fileManager.getName(file));
}
}
}
Expand All @@ -108,11 +141,8 @@ public void clearCache() {

@MainThread
public void createDirectories() {
if (!directory.exists()) {
if (!directory.mkdirs()) {
Logger.e(TAG, "Unable to create file cache dir " +
directory.getAbsolutePath());
}
if (!fileManager.exists(cacheDirFile) && fileManager.create(cacheDirFile) == null) {
throw new RuntimeException("Unable to create file cache dir " + cacheDirFile.getFullPath());
}
}

Expand All @@ -125,62 +155,61 @@ private void backgroundRecalculateSize() {
private void recalculateSize() {
long calculatedSize = 0;

File[] files = directory.listFiles();
if (files != null) {
for (File file : files) {
calculatedSize += file.length();
}
List<AbstractFile> files = fileManager.listFiles(cacheDirFile);
for (AbstractFile file : files) {
calculatedSize += fileManager.getLength(file);
}

size.set(calculatedSize);
}

@WorkerThread
private void trim() {
File[] directoryFiles = directory.listFiles();
List<AbstractFile> directoryFiles = fileManager.listFiles(cacheDirFile);

// Don't try to trim empty directories or just one file in it.
if (directoryFiles == null || directoryFiles.length <= 1) {
if (directoryFiles.size() <= 1) {
return;
}

// Get all files with their last modified times.
List<Pair<File, Long>> files = new ArrayList<>(directoryFiles.length);
for (File file : directoryFiles) {
files.add(new Pair<>(file, file.lastModified()));
List<Pair<AbstractFile, Long>> files = new ArrayList<>(directoryFiles.size());
for (AbstractFile file : directoryFiles) {
files.add(new Pair<>(file, fileManager.lastModified(file)));
}

// Sort by oldest first.
Collections.sort(files, (o1, o2) -> Long.signum(o1.second - o2.second));

//Pre-trim based on time, trash anything older than 6 hours
List<Pair<File, Long>> removed = new ArrayList<>();
for (Pair<File, Long> fileLongPair : files) {
List<Pair<AbstractFile, Long>> removed = new ArrayList<>();
for (Pair<AbstractFile, Long> fileLongPair : files) {
if (fileLongPair.second + 6 * 60 * 60 * 1000 < System.currentTimeMillis()) {
Logger.d(TAG, "Delete for trim " + fileLongPair.first.getAbsolutePath());
if (!fileLongPair.first.delete()) {
Logger.d(TAG, "Delete for trim " + fileLongPair.first.getFullPath());
if (!fileManager.delete(fileLongPair.first)) {
Logger.e(TAG, "Failed to delete cache file for trim");
}
removed.add(fileLongPair);
} else break; //only because we sorted earlier
}
for (Pair<File, Long> deleted : removed) {
for (Pair<AbstractFile, Long> deleted : removed) {
files.remove(deleted);
}
recalculateSize();

// Trim as long as the directory size exceeds the threshold (note that oldest is still first)
long workingSize = size.get();
for (int i = 0; workingSize >= FILE_CACHE_DISK_SIZE; i++) {
File file = files.get(i).first;
AbstractFile file = files.get(i).first;
K1rakishou marked this conversation as resolved.
Show resolved Hide resolved

Logger.d(TAG, "Delete for trim " + file.getAbsolutePath());
workingSize -= file.length();
Logger.d(TAG, "Delete for trim " + file.getFullPath());
workingSize -= fileManager.getLength(file);

if (!file.delete()) {
if (!fileManager.delete(file)) {
Logger.e(TAG, "Failed to delete cache file for trim");
}
}

recalculateSize();
}
}
Loading