improve message when storage access framework access permission is revoked

This commit is contained in:
Philipp Crocoll
2018-04-02 19:05:32 +02:00
parent d67b8b8298
commit e42d8b8eeb
4 changed files with 45 additions and 4 deletions

View File

@@ -2,12 +2,15 @@ using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.Serialization;
using Android;
using Android.Content;
using Android.Database;
using Android.OS;
using Android.Provider;
using Java.IO;
using KeePassLib.Serialization;
using Console = System.Console;
namespace keepass2android.Io
{
@@ -50,9 +53,22 @@ namespace keepass2android.Io
}
public Stream OpenFileForRead(IOConnectionInfo ioc)
{
try
{
return _ctx.ContentResolver.OpenInputStream(Android.Net.Uri.Parse(ioc.Path));
}
catch (Exception e)
{
if (e.Message.Contains("requires that you obtain access using ACTION_OPEN_DOCUMENT"))
{
//looks like permission was revoked.
throw new DocumentAccessRevokedException();
}
throw;
}
}
public IWriteTransaction OpenWriteTransaction(IOConnectionInfo ioc, bool useFileTransaction)
{
@@ -267,6 +283,25 @@ namespace keepass2android.Io
}
public class DocumentAccessRevokedException : Exception
{
public DocumentAccessRevokedException()
{
}
public DocumentAccessRevokedException(string message) : base(message)
{
}
public DocumentAccessRevokedException(string message, Exception innerException) : base(message, innerException)
{
}
protected DocumentAccessRevokedException(SerializationInfo info, StreamingContext context) : base(info, context)
{
}
}
class AndroidContentWriteTransaction : IWriteTransaction
{
private readonly string _path;

View File

@@ -63,6 +63,7 @@ namespace keepass2android
AskDeletePermanentlyItems,
AskDeletePermanentlyItemsNoRecycle,
InOfflineMode,
DocumentAccessRevoked,
DuplicateTitle,
TemplateTitle_IdCard,
TemplateField_IdCard_Name,

View File

@@ -403,6 +403,8 @@
<string name="IconVisibilityInfo_Android8_btnSettings">Open settings</string>
<string name="DontCare">I don\'t care</string>
<string name="DocumentAccessRevoked">The file is no longer accessible to Keepass2Android. Either it was removed or the access permissions have been revoked. Please use re-open the file, e.g. using Change database.</string>
<string name="PreloadDatabaseEnabled_title">Pre-load database file</string>
<string name="PreloadDatabaseEnabled_summary">Start background loading or downloading of the database file during password entry.</string>

View File

@@ -714,6 +714,9 @@ namespace keepass2android
string errorMessage = e.Message;
if (e is OfflineModeException)
errorMessage = GetResourceString(UiStringKey.InOfflineMode);
if (e is DocumentAccessRevokedException)
errorMessage = GetResourceString(UiStringKey.DocumentAccessRevoked);
return errorMessage;
}