Skip to content

Commit

Permalink
#169: FileCache automatically Disabled, if there are saf-Write operat…
Browse files Browse the repository at this point in the history
…ions; Enabled on listFiles or listDirs
  • Loading branch information
k3b committed Apr 26, 2021
1 parent 87a2f9f commit 2b2ef95
Show file tree
Hide file tree
Showing 10 changed files with 152 additions and 127 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2017-2020 by k3b.
* Copyright (c) 2017-2022 by k3b.
*
* This file is part of AndroFotoFinder / #APhotoManager.
*
Expand Down Expand Up @@ -278,7 +278,7 @@ private static SelectedFiles getSelectedFiles(String dbgContext, Context ctx, In

if (result == null) {
String path = IntentUtil.getFilePath(ctx, IntentUtil.getUri(intent));
String fileNames[] = SelectedFiles.getFileNameList(path);
String[] fileNames = SelectedFiles.getFileNameList(path);
Long[] ids = null;
int itemCount = (fileNames != null) ? fileNames.length : 0;

Expand Down Expand Up @@ -849,7 +849,7 @@ public void afterGrant(FilePermissionActivity activity) {
return false;
}

if (FileFacade.debugLogFacade) {
if (FileFacade.debugLogSAFFacade) {
Log.i(FilePermissionActivity.TAG, "PhotoPropertiesEditActivity.execExifUpdate.do");
}

Expand Down
194 changes: 97 additions & 97 deletions app/src/main/java/de/k3b/android/androFotoFinder/SettingsActivity.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2015-2020 by k3b.
* Copyright (c) 2015-2021 by k3b.
*
* This file is part of AndroFotoFinder / #APhotoManager.
*
Expand Down Expand Up @@ -63,100 +63,7 @@ public class SettingsActivity extends PreferenceActivity {
private ListPreference mediaUpdateStrategyPreference;
private ListPreference themePreference; // #21: Support to change locale at runtime

private int INSTALL_REQUEST_CODE = 1927;

@Override
protected void onCreate(final Bundle savedInstanceState) {
LocalizedActivity.fixThemeAndLocale(this); // #21: Support to change locale at runtime
super.onCreate(savedInstanceState);

if (Global.debugEnabled) {
// todo create junit integration tests with arabic locale from this.
StringFormatResourceTests.test(this);
}

final Intent intent = getIntent();
if (Global.debugEnabled && (intent != null)){
Log.d(Global.LOG_CONTEXT, "SettingsActivity onCreate " + intent.toUri(Intent.URI_INTENT_SCHEME));
}

if (FileFacade.debugLogFacade) {
this.addPreferencesFromResource(R.xml.preferences_saf169_test);
findPreference("debugClearSafCache").setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {
@Override
public boolean onPreferenceClick(Preference preference) {
DocumentFileTranslator.clearCache();
Toast.makeText(SettingsActivity.this, "debugClearSafCache",
Toast.LENGTH_LONG).show();
finish();
return true; // do close
}
});
}

this.addPreferencesFromResource(R.xml.preferences);
prefsInstance = PreferenceManager
.getDefaultSharedPreferences(this);
global2Prefs(this.getApplication());

// #21: Support to change locale at runtime
defaultLocalePreference =
(ListPreference) findPreference(Global.PREF_KEY_USER_LOCALE);
defaultLocalePreference.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {
@Override
public boolean onPreferenceChange(Preference preference, Object newValue) {
setLanguage((String) newValue);
LocalizedActivity.recreate(SettingsActivity.this);
return true; // change is allowed
}
});

themePreference = (ListPreference) findPreference(UserTheme.PREF_KEY_USER_THEME);
themePreference.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {
@Override
public boolean onPreferenceChange(Preference preference, Object newValue) {
setUserTheme((String) newValue);
LocalizedActivity.recreate(SettingsActivity.this);
return true; // change is allowed
}
});
mediaUpdateStrategyPreference =
(ListPreference) findPreference("mediaUpdateStrategy");
mediaUpdateStrategyPreference.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {
@Override
public boolean onPreferenceChange(Preference preference, Object newValue) {
LibGlobal.mediaUpdateStrategy = (String) newValue;
setPrefSummayFromKey(LibGlobal.mediaUpdateStrategy, mediaUpdateStrategyPreference, R.array.pref_media_update_strategy_names);
return true;
}
});
setPrefSummayFromKey(LibGlobal.mediaUpdateStrategy, mediaUpdateStrategyPreference, R.array.pref_media_update_strategy_names);

findPreference("debugClearLog").setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {
@Override
public boolean onPreferenceClick(Preference preference) {
onDebugClearLogCat();
return false; // donot close
}
});
findPreference("debugSaveLog").setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {
@Override
public boolean onPreferenceClick(Preference preference) {
onDebugSaveLogCat();
return false; // donot close
}
});
findPreference("translate").setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {
@Override
public boolean onPreferenceClick(Preference preference) {
onTranslate();
return false; // donot close
}
});

// #21: Support to change locale at runtime
updateSummary();
}
private final int INSTALL_REQUEST_CODE = 1927;

public static void global2Prefs(Context context) {
fixDefaults(context, null, null);
Expand Down Expand Up @@ -190,7 +97,7 @@ public static void global2Prefs(Context context) {
prefs.putBoolean("xmp_file_schema_long", LibGlobal.preferLongXmpFormat);

prefs.putBoolean("mapsForgeEnabled", Global.mapsForgeEnabled);
prefs.putBoolean("debugLogFacade", FileFacade.debugLogFacade);
prefs.putBoolean("debugLogFacade", FileFacade.debugLogSAFFacade);
if (Global.allow_emulate_ao10) {
prefs.putBoolean(PREF_KEY_USE_MEDIA_IMAGE_DB_REPLACEMENT, Global.useAo10MediaImageDbReplacement);
}
Expand Down Expand Up @@ -267,7 +174,7 @@ public static void prefs2Global(Context context) {
LibGlobal.preferLongXmpFormat = getPref(prefs, "xmp_file_schema_long", LibGlobal.preferLongXmpFormat);

Global.mapsForgeEnabled = getPref(prefs, "mapsForgeEnabled", Global.mapsForgeEnabled);
FileFacade.debugLogFacade = getPref(prefs, "debugLogFacade", FileFacade.debugLogFacade);
FileFacade.debugLogSAFFacade = getPref(prefs, "debugLogFacade", FileFacade.debugLogSAFFacade);


boolean useAo10MediaImageDbReplacement = Global.useAo10MediaImageDbReplacement;
Expand Down Expand Up @@ -318,6 +225,99 @@ public static void prefs2Global(Context context) {
fixDefaults(context, previousCacheRoot, previousMapsForgeDir);
}

@Override
protected void onCreate(final Bundle savedInstanceState) {
LocalizedActivity.fixThemeAndLocale(this); // #21: Support to change locale at runtime
super.onCreate(savedInstanceState);

if (Global.debugEnabled) {
// todo create junit integration tests with arabic locale from this.
StringFormatResourceTests.test(this);
}

final Intent intent = getIntent();
if (Global.debugEnabled && (intent != null)) {
Log.d(Global.LOG_CONTEXT, "SettingsActivity onCreate " + intent.toUri(Intent.URI_INTENT_SCHEME));
}

if (FileFacade.debugLogSAFFacade) {
this.addPreferencesFromResource(R.xml.preferences_saf169_test);
findPreference("debugClearSafCache").setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {
@Override
public boolean onPreferenceClick(Preference preference) {
DocumentFileTranslator.clearCache();
Toast.makeText(SettingsActivity.this, "debugClearSafCache",
Toast.LENGTH_LONG).show();
finish();
return true; // do close
}
});
}

this.addPreferencesFromResource(R.xml.preferences);
prefsInstance = PreferenceManager
.getDefaultSharedPreferences(this);
global2Prefs(this.getApplication());

// #21: Support to change locale at runtime
defaultLocalePreference =
(ListPreference) findPreference(Global.PREF_KEY_USER_LOCALE);
defaultLocalePreference.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {
@Override
public boolean onPreferenceChange(Preference preference, Object newValue) {
setLanguage((String) newValue);
LocalizedActivity.recreate(SettingsActivity.this);
return true; // change is allowed
}
});

themePreference = (ListPreference) findPreference(UserTheme.PREF_KEY_USER_THEME);
themePreference.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {
@Override
public boolean onPreferenceChange(Preference preference, Object newValue) {
setUserTheme((String) newValue);
LocalizedActivity.recreate(SettingsActivity.this);
return true; // change is allowed
}
});
mediaUpdateStrategyPreference =
(ListPreference) findPreference("mediaUpdateStrategy");
mediaUpdateStrategyPreference.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {
@Override
public boolean onPreferenceChange(Preference preference, Object newValue) {
LibGlobal.mediaUpdateStrategy = (String) newValue;
setPrefSummayFromKey(LibGlobal.mediaUpdateStrategy, mediaUpdateStrategyPreference, R.array.pref_media_update_strategy_names);
return true;
}
});
setPrefSummayFromKey(LibGlobal.mediaUpdateStrategy, mediaUpdateStrategyPreference, R.array.pref_media_update_strategy_names);

findPreference("debugClearLog").setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {
@Override
public boolean onPreferenceClick(Preference preference) {
onDebugClearLogCat();
return false; // donot close
}
});
findPreference("debugSaveLog").setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {
@Override
public boolean onPreferenceClick(Preference preference) {
onDebugSaveLogCat();
return false; // donot close
}
});
findPreference("translate").setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {
@Override
public boolean onPreferenceClick(Preference preference) {
onTranslate();
return false; // donot close
}
});

// #21: Support to change locale at runtime
updateSummary();
}

@Override
public void onPause() {
prefs2Global(this);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,7 @@ private boolean cmdMoveOrCopyWithDestDirPicker(final boolean move, final String
public void afterGrant(FilePermissionActivity activity) {
// does not work in frag,emt
mFileCommands.setContext(activity);
if (FileFacade.debugLogFacade) {
if (FileFacade.debugLogSAFFacade) {
Log.i(FileFacade.LOG_TAG, this.getClass().getSimpleName()
+ ": afterGrant " + activity + "-" + this);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -731,7 +731,7 @@ public void afterGrant(FilePermissionActivity activity) {
return false;
}

if (FileFacade.debugLogFacade) {
if (FileFacade.debugLogSAFFacade) {
Log.i(FilePermissionActivity.TAG, "PhotoPropertiesEditActivity.execExifUpdate.do");
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -307,7 +307,7 @@ public void afterGrant(FilePermissionActivity activity) {
return false;
}

if (FileFacade.debugLogFacade) {
if (FileFacade.debugLogSAFFacade) {
Log.i(FilePermissionActivity.TAG, "AndroidFileCommands.cmdDeleteFileWithQuestion.do");
}

Expand Down
31 changes: 19 additions & 12 deletions app/src/main/java/de/k3b/android/io/AndroidFileFacade.java
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ public String toString() {
@Override
public IFile convert(String dbgContext, File file) {
final IFile result = new AndroidFileFacade(file);
if (debugLogFacade) {
if (debugLogSAFFacade) {
Log.i(LOG_TAG, dbgContext + " convert => " + result);
}
return result;
Expand Down Expand Up @@ -259,23 +259,31 @@ public boolean mkdirs() {

@Override
public IFile[] listFiles() {
reEnableCache();
enableCache("listFiles", true);
final DocumentFile androidFile = getAndroidFile(false);
if (androidFile != null) {
return get(androidFile.listFiles());
}
return new IFile[0];
}

private void reEnableCache() {
if (!Global.android_DocumentFile_find_cache) {
Global.android_DocumentFile_find_cache = true;
invalidateParentDirCache();
private void enableCache(String dbgContext, boolean enable) {
if (enable != Global.android_DocumentFile_find_cache) {
if (FileFacade.debugLogSAFFacade) {
Log.i(FileFacade.LOG_TAG, this.getClass().getSimpleName()
+ " " + dbgContext + ": enableCache(" + enable + ")");
}

Global.android_DocumentFile_find_cache = enable;

if (enable) {
invalidateParentDirCache();
}
}
}

public IFile[] listDirs() {
reEnableCache();
enableCache("listDirs", true);
List<IFile> found = new ArrayList<>();
final DocumentFile androidFile = getAndroidFile(false);
if (androidFile != null) {
Expand Down Expand Up @@ -315,20 +323,19 @@ public OutputStream openOutputStream() throws FileNotFoundException {
context = "openOutputStream create new ";

}
if (FileFacade.debugLogFacade) {
if (FileFacade.debugLogSAFFacade) {
Log.i(LOG_TAG, context + this);
}
OutputStream result = documentFileTranslator.createOutputStream(androidFile);
Global.android_DocumentFile_find_cache = false;
invalidateParentDirCache();
enableCache("openOutputStream", false);
return result;
}

@Override
public InputStream openInputStream() throws FileNotFoundException {
String context = "openInputStream ";
if ((readUri != null)) {
if (debugLogFacade) {
if (debugLogSAFFacade) {
Log.i(LOG_TAG, context + this + " for uri " + readUri);
}
return documentFileTranslator.openInputStream(readUri);
Expand All @@ -341,7 +348,7 @@ public InputStream openInputStream() throws FileNotFoundException {
Log.w(LOG_TAG, msg);
getAndroidFile(true); // allow debugger to step in
throw new FileNotFoundException(msg);
} else if (debugLogFacade) {
} else if (debugLogSAFFacade) {
Log.i(LOG_TAG, context + this + " for uri " + androidFile.getUri());
}
return resultInputStream;
Expand Down
17 changes: 15 additions & 2 deletions app/src/main/java/de/k3b/android/io/DocumentFileCache.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,15 @@
*/
package de.k3b.android.io;

import android.util.Log;

import androidx.documentfile.provider.DocumentFile;

import java.io.File;
import java.util.HashMap;

import de.k3b.android.androFotoFinder.Global;
import de.k3b.io.filefacade.FileFacade;
import de.k3b.media.PhotoPropertiesUtil;

public class DocumentFileCache {
Expand Down Expand Up @@ -59,11 +62,21 @@ public DocumentFile findFile(DocumentFile parentDoc, File parentFile, String dis
}

if (PhotoPropertiesUtil.isImage(displayName, PhotoPropertiesUtil.IMG_TYPE_ALL | PhotoPropertiesUtil.IMG_TYPE_XMP)) {
return currentFileCache.lastChildDocFiles.get(displayName.toLowerCase());
return logFindFileResult(parentFile, currentFileCache.lastChildDocFiles.get(displayName.toLowerCase()));
}
}
}
return parentDoc.findFile(displayName);
return logFindFileResult(parentFile, parentDoc.findFile(displayName));
}

private DocumentFile logFindFileResult(File parentFile, DocumentFile documentFile) {
if (FileFacade.debugLogSAFFacade) {
Log.i(FileFacade.LOG_TAG, this.getClass().getSimpleName()
+ ".findFile(" + parentFile + ",cache="
+ Global.android_DocumentFile_find_cache
+ ") ==> " + documentFile);
}
return documentFile;
}

public void invalidateParentDirCache() {
Expand Down
Loading

0 comments on commit 2b2ef95

Please sign in to comment.