-
Notifications
You must be signed in to change notification settings - Fork 58
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
172 additions
and
40 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
91 changes: 91 additions & 0 deletions
91
app/src/main/java/de/k3b/android/io/DocumentFileCache.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
/* | ||
* Copyright (c) 2021 by k3b. | ||
* | ||
* This file is part of AndroFotoFinder / #APhotoManager. | ||
* | ||
* This program is free software: you can redistribute it and/or modify it | ||
* under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, but WITHOUT | ||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS | ||
* FOR A PARTICULAR PURPOSE. See the GNU General Public License | ||
* for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License along with | ||
* this program. If not, see <http://www.gnu.org/licenses/> | ||
*/ | ||
package de.k3b.android.io; | ||
|
||
import android.support.v4.provider.DocumentFile; | ||
import android.util.Log; | ||
|
||
import java.io.File; | ||
import java.util.HashMap; | ||
|
||
import de.k3b.io.filefacade.IFile; | ||
import de.k3b.media.PhotoPropertiesUtil; | ||
|
||
public class DocumentFileCache { | ||
private static final String TAG = DocumentFileTranslator.TAG; | ||
|
||
private CurrentFileCache[] currentFileCaches = null; | ||
|
||
/** | ||
* strategyID : IFile.STRATEGY_XXX. | ||
*/ | ||
public CurrentFileCache getCacheStrategy(int strategyID) { | ||
if (currentFileCaches == null) { | ||
currentFileCaches = new CurrentFileCache[IFile.STRATEGY_MAX + 1]; | ||
currentFileCaches[IFile.STRATEGY_INPUT] = new CurrentFileCache(); | ||
currentFileCaches[IFile.STRATEGY_OUTPUT] = new CurrentFileCache(); | ||
currentFileCaches[IFile.STRATEGY_NONE] = null; | ||
} | ||
if (strategyID >= 0 && strategyID < currentFileCaches.length) { | ||
return currentFileCaches[strategyID]; | ||
} | ||
Log.w(TAG, "DocumentFileCache.setCacheStrategy(id=" + strategyID + | ||
") unknow Strategy"); | ||
return currentFileCaches[IFile.STRATEGY_NONE]; | ||
} | ||
|
||
public DocumentFile findFile(DocumentFile parentDoc, File parentFile, String displayName, int strategyID) { | ||
CurrentFileCache currentFileCache = getCacheStrategy(strategyID); | ||
|
||
if (currentFileCache != null) { | ||
if (!parentFile.equals(currentFileCache.lastParentFile)) { | ||
currentFileCache.lastParentFile = parentFile; | ||
currentFileCache.lastChildDocFiles.clear(); | ||
for (DocumentFile childDoc : parentDoc.listFiles()) { | ||
if (childDoc.isFile()) { | ||
String childDocName = childDoc.getName().toLowerCase(); | ||
if (PhotoPropertiesUtil.isImage(childDocName, PhotoPropertiesUtil.IMG_TYPE_ALL | PhotoPropertiesUtil.IMG_TYPE_XMP)) { | ||
currentFileCache.lastChildDocFiles.put(childDocName, childDoc); | ||
} | ||
} | ||
} | ||
|
||
} | ||
|
||
if (PhotoPropertiesUtil.isImage(displayName, PhotoPropertiesUtil.IMG_TYPE_ALL | PhotoPropertiesUtil.IMG_TYPE_XMP)) { | ||
return currentFileCache.lastChildDocFiles.get(displayName.toLowerCase()); | ||
} | ||
} | ||
return parentDoc.findFile(displayName); | ||
} | ||
|
||
public void invalidateParentDirCache(int strategyID) { | ||
CurrentFileCache currentFileCache = getCacheStrategy(strategyID); | ||
if (currentFileCache != null) currentFileCache.lastParentFile = null; | ||
} | ||
|
||
/** | ||
* Mapping from local file name inside lastParentFile to photo-related-DocumentFiles | ||
*/ | ||
private static class CurrentFileCache { | ||
private final HashMap<String, DocumentFile> lastChildDocFiles = new HashMap<>(); | ||
private File lastParentFile = null; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters