use internal directory for offline caching. this reduces the likelihood of data loss. Users need to disable and then re-enable file caching to make sure all files are cached in the new directory. closes #83 https://github.com/PhilippC/keepass2android/issues/83
This commit is contained in:
@@ -255,7 +255,8 @@ namespace keepass2android.Io
|
||||
if (ioc.IsLocalFile())
|
||||
{
|
||||
bool requiresPermission = !(ioc.Path.StartsWith(activity.Activity.FilesDir.CanonicalPath)
|
||||
|| ioc.Path.StartsWith(IoUtil.GetInternalDirectory(activity.Activity).CanonicalPath));
|
||||
|| ioc.Path.StartsWith(IoUtil.GetInternalDirectory(activity.Activity).CanonicalPath)
|
||||
|| ioc.Path.StartsWith(IoUtil.GetInternalDirectory(activity.Activity).CanonicalPath));
|
||||
|
||||
var extDirectory = activity.Activity.GetExternalFilesDir(null);
|
||||
if ((extDirectory != null) && (ioc.Path.StartsWith(extDirectory.CanonicalPath)))
|
||||
|
||||
@@ -65,22 +65,28 @@ namespace keepass2android.Io
|
||||
|
||||
protected readonly OfflineSwitchableFileStorage _cachedStorage;
|
||||
private readonly ICacheSupervisor _cacheSupervisor;
|
||||
private readonly string _streamCacheDir;
|
||||
private readonly string _legacyCacheDir;
|
||||
private readonly string _cacheDir;
|
||||
|
||||
public CachingFileStorage(IFileStorage cachedStorage, string cacheDir, ICacheSupervisor cacheSupervisor)
|
||||
public CachingFileStorage(IFileStorage cachedStorage, Context cacheDirContext, ICacheSupervisor cacheSupervisor)
|
||||
{
|
||||
_cachedStorage = new OfflineSwitchableFileStorage(cachedStorage);
|
||||
_cacheSupervisor = cacheSupervisor;
|
||||
_streamCacheDir = cacheDir + Java.IO.File.Separator + "OfflineCache" + Java.IO.File.Separator;
|
||||
if (!Directory.Exists(_streamCacheDir))
|
||||
Directory.CreateDirectory(_streamCacheDir);
|
||||
_legacyCacheDir = cacheDirContext.CacheDir.Path + Java.IO.File.Separator + "OfflineCache" + Java.IO.File.Separator;
|
||||
if (!Directory.Exists(_legacyCacheDir))
|
||||
Directory.CreateDirectory(_legacyCacheDir);
|
||||
|
||||
}
|
||||
_cacheDir = IoUtil.GetInternalDirectory(cacheDirContext).Path + Java.IO.File.Separator + "OfflineCache" + Java.IO.File.Separator;
|
||||
if (!Directory.Exists(_cacheDir))
|
||||
Directory.CreateDirectory(_cacheDir);
|
||||
|
||||
}
|
||||
|
||||
public void ClearCache()
|
||||
{
|
||||
IoUtil.DeleteDir(new Java.IO.File(_streamCacheDir), true);
|
||||
}
|
||||
IoUtil.DeleteDir(new Java.IO.File(_legacyCacheDir), true);
|
||||
IoUtil.DeleteDir(new Java.IO.File(_cacheDir), true);
|
||||
}
|
||||
|
||||
public IEnumerable<string> SupportedProtocols { get { return _cachedStorage.SupportedProtocols; } }
|
||||
|
||||
@@ -105,7 +111,11 @@ namespace keepass2android.Io
|
||||
{
|
||||
SHA256Managed sha256 = new SHA256Managed();
|
||||
string iocAsHexString = MemUtil.ByteArrayToHexString(sha256.ComputeHash(Encoding.Unicode.GetBytes(ioc.Path.ToCharArray())))+".cache";
|
||||
return _streamCacheDir + iocAsHexString;
|
||||
if (File.Exists(_legacyCacheDir + iocAsHexString))
|
||||
return _legacyCacheDir + iocAsHexString;
|
||||
|
||||
return _cacheDir + iocAsHexString;
|
||||
|
||||
}
|
||||
|
||||
public bool IsCached(IOConnectionInfo ioc)
|
||||
|
||||
@@ -4,6 +4,7 @@ using KeePassLib.Serialization;
|
||||
using OtpKeyProv;
|
||||
using keepass2android.Io;
|
||||
using System.Xml;
|
||||
using Android.Content;
|
||||
|
||||
namespace keepass2android.addons.OtpKeyProv
|
||||
{
|
||||
@@ -28,8 +29,8 @@ namespace keepass2android.addons.OtpKeyProv
|
||||
}
|
||||
|
||||
|
||||
public OtpAuxCachingFileStorage(IFileStorage cachedStorage, string cacheDir, IOtpAuxCacheSupervisor cacheSupervisor)
|
||||
: base(cachedStorage, cacheDir, cacheSupervisor)
|
||||
public OtpAuxCachingFileStorage(IFileStorage cachedStorage, Context context, IOtpAuxCacheSupervisor cacheSupervisor)
|
||||
: base(cachedStorage, context, cacheSupervisor)
|
||||
{
|
||||
_cacheSupervisor = cacheSupervisor;
|
||||
}
|
||||
|
||||
@@ -541,7 +541,7 @@ namespace keepass2android
|
||||
|
||||
if (DatabaseCacheEnabled && allowCache)
|
||||
{
|
||||
fileStorage = new CachingFileStorage(innerFileStorage, Application.Context.CacheDir.Path, this);
|
||||
fileStorage = new CachingFileStorage(innerFileStorage, Application.Context, this);
|
||||
}
|
||||
else
|
||||
{
|
||||
@@ -820,7 +820,7 @@ namespace keepass2android
|
||||
|
||||
public void ClearOfflineCache()
|
||||
{
|
||||
new CachingFileStorage(new LocalFileStorage(this), Application.Context.CacheDir.Path, this).ClearCache();
|
||||
new CachingFileStorage(new LocalFileStorage(this), Application.Context, this).ClearCache();
|
||||
}
|
||||
|
||||
public IFileStorage GetFileStorage(string protocolId)
|
||||
@@ -844,7 +844,7 @@ namespace keepass2android
|
||||
|
||||
if (DatabaseCacheEnabled)
|
||||
{
|
||||
return new OtpAuxCachingFileStorage(innerFileStorage, Application.Context.CacheDir.Path, new OtpAuxCacheSupervisor(this));
|
||||
return new OtpAuxCachingFileStorage(innerFileStorage, Application.Context, new OtpAuxCacheSupervisor(this));
|
||||
}
|
||||
else
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user