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.Collections.Generic;
using System.IO; using System.IO;
using System.Linq; using System.Linq;
using System.Runtime.Serialization;
using Android;
using Android.Content; using Android.Content;
using Android.Database; using Android.Database;
using Android.OS; using Android.OS;
using Android.Provider; using Android.Provider;
using Java.IO; using Java.IO;
using KeePassLib.Serialization; using KeePassLib.Serialization;
using Console = System.Console;
namespace keepass2android.Io namespace keepass2android.Io
{ {
@@ -51,7 +54,20 @@ namespace keepass2android.Io
public Stream OpenFileForRead(IOConnectionInfo ioc) public Stream OpenFileForRead(IOConnectionInfo ioc)
{ {
return _ctx.ContentResolver.OpenInputStream(Android.Net.Uri.Parse(ioc.Path)); 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) public IWriteTransaction OpenWriteTransaction(IOConnectionInfo ioc, bool useFileTransaction)
@@ -267,7 +283,26 @@ namespace keepass2android.Io
} }
class AndroidContentWriteTransaction : IWriteTransaction 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; private readonly string _path;
private readonly Context _ctx; private readonly Context _ctx;

View File

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

View File

@@ -403,6 +403,8 @@
<string name="IconVisibilityInfo_Android8_btnSettings">Open settings</string> <string name="IconVisibilityInfo_Android8_btnSettings">Open settings</string>
<string name="DontCare">I don\'t care</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_title">Pre-load database file</string>
<string name="PreloadDatabaseEnabled_summary">Start background loading or downloading of the database file during password entry.</string> <string name="PreloadDatabaseEnabled_summary">Start background loading or downloading of the database file during password entry.</string>

View File

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