Prevent users from accidentally opening the local backup, closes #479

This commit is contained in:
Philipp Crocoll
2018-08-21 19:34:22 +02:00
parent 4b393f412c
commit d5a49db782
9 changed files with 150 additions and 19 deletions

Binary file not shown.

After

Width:  |  Height:  |  Size: 568 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.0 KiB

View File

@@ -27,7 +27,7 @@
android:id="@+id/group_name_vdots"
android:layout_width="wrap_content"
android:layout_height="20dp"
android:src="@drawable/vdots"
android:src="@drawable/vdots_bright"
android:gravity="right|bottom"
android:layout_alignParentRight="true"
android:paddingTop="4sp"/>

View File

@@ -22,4 +22,13 @@
android:paddingRight="0dp"
android:paddingLeft="0dp" />
<Switch
android:id="@+id/local_backups_switch"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/show_local_backups"
android:checked="false"
android:textColor="#fff"
android:gravity="left"/>
</LinearLayout>

View File

@@ -364,6 +364,7 @@
<string name="CreateBackups_summary">Create a local backup copy after successfully loading a database.</string>
<string name="UpdatingBackup">Updating local backup...</string>
<string name="LocalBackupOf">Local backup of %1$s</string>
<string name="show_local_backups">Show local backups</string>

View File

@@ -24,7 +24,7 @@ using KeePassLib.Serialization;
namespace keepass2android
{
/// <summary>
/// <summary>
/// Class to store the recent files in a database
/// </summary>
public class FileDbHelper {

View File

@@ -26,6 +26,7 @@ using Android.Views;
using Android.Widget;
using Android.Content.PM;
using Android.Support.V7.App;
using Java.IO;
using KeePassLib.Serialization;
using Keepass2android.Pluginsdk;
using keepass2android.Io;
@@ -154,7 +155,7 @@ namespace keepass2android
};
createNewButton.Click += createNewButtonClick;
/*//CREATE + IMPORT
/*//CREATE + IMPORT
Button createImportButton = (Button)FindViewById(Resource.Id.start_create_import);
createImportButton.Click += (object sender, EventArgs e) =>
{
@@ -168,7 +169,9 @@ namespace keepass2android
};*/
FillData();
FindViewById<Switch>(Resource.Id.local_backups_switch).CheckedChange += (sender, args) => {FillData();};
FillData();
if (savedInstanceState != null)
{
@@ -178,7 +181,7 @@ namespace keepass2android
}
private bool ShowRecentFiles()
private bool ShowRecentFiles()
{
if (!RememberRecentFiles())
{
@@ -280,6 +283,19 @@ namespace keepass2android
{
if (args2.Item.ItemId == remove)
{
if (new LocalFileStorage(App.Kp2a).IsLocalBackup(IOConnectionInfo.FromPath(filename)))
{
try
{
Java.IO.File file = new File(filename);
file.Delete();
}
catch (Exception exception)
{
Kp2aLog.LogUnexpectedError(exception);
}
}
App.Kp2a.FileDbHelper.DeleteFile(filename);
cursor.Requery();
@@ -319,24 +335,18 @@ namespace keepass2android
{
// Get all of the rows from the database and create the item list
ICursor filesCursor = _dbHelper.FetchAllFiles();
StartManagingCursor(filesCursor);
// Create an array to specify the fields we want to display in the list
// (only TITLE)
String[] from = new[] { FileDbHelper.KeyFileFilename };
// and an array of the fields we want to bind those fields to (in this
// case just text1)
int[] to = new[] { Resource.Id.file_filename };
/*
// Now create a simple cursor adapter and set it to display
SimpleCursorAdapter recentFilesAdapter = new SimpleCursorAdapter(this,
Resource.Layout.file_row, filesCursor, from, to);
if (FindViewById<Switch>(Resource.Id.local_backups_switch).Checked == false)
{
var fileStorage = new LocalFileStorage(App.Kp2a);
filesCursor = new FilteredCursor(filesCursor, cursor => !fileStorage.IsLocalBackup(IOConnectionInfo.FromPath(cursor.GetString(1))));
}
recentFilesAdapter.ViewBinder = new MyViewBinder(App.Kp2a);
*/
FragmentManager.FindFragmentById<RecentFilesFragment>(Resource.Id.recent_files).SetAdapter(new MyCursorAdapter(this, filesCursor,App.Kp2a));
StartManagingCursor(filesCursor);
FragmentManager.FindFragmentById<RecentFilesFragment>(Resource.Id.recent_files).SetAdapter(new MyCursorAdapter(this, filesCursor,App.Kp2a));
}

View File

@@ -0,0 +1,104 @@
using System;
using System.Collections.Generic;
using Android.Database;
using Android.Runtime;
namespace keepass2android
{
public class FilteredCursor : CursorWrapper
{
private readonly List<int> _indicesToKeep;
private int _pos;
protected FilteredCursor(IntPtr javaReference, JniHandleOwnership transfer) : base(javaReference, transfer)
{
}
public FilteredCursor(ICursor cursor, Predicate<ICursor> filter) : base(cursor)
{
_indicesToKeep = new List<int>();
int index = 0;
for (cursor.MoveToFirst(); !cursor.IsAfterLast; cursor.MoveToNext())
{
if (filter(cursor))
_indicesToKeep.Add(index);
index++;
}
_pos = -1;
}
public override int Count
{
get
{
return _indicesToKeep.Count;
}
}
public override bool MoveToPosition(int position)
{
if (position >= Count)
{
_pos = Count;
return false;
}
if (position < 0)
{
_pos = -1;
return false;
}
return base.MoveToPosition(_indicesToKeep[position]);
}
public override bool Move(int offset)
{
return MoveToPosition(_pos+offset);
}
public override bool MoveToFirst()
{
return MoveToPosition(0);
}
public override bool MoveToNext()
{
return MoveToPosition(_pos+1);
}
public override bool MoveToLast()
{
return MoveToPosition(Count-1);
}
public override bool MoveToPrevious()
{
return MoveToPosition(_pos-1);
}
public override bool IsAfterLast
{
get { return _pos >= Count; }
}
public override bool IsBeforeFirst
{
get { return _pos < 0; }
}
public override bool IsFirst
{
get { return _pos == 0; }
}
public override bool IsLast
{
get { return _pos == Count-1; }
}
public override int Position
{
get { return _pos; }
}
}
}

View File

@@ -175,6 +175,7 @@
<ItemGroup>
<Compile Include="ChallengeXCKey.cs" />
<Compile Include="EntryActivityClasses\ViewImagePopupItem.cs" />
<Compile Include="fileselect\FilteredCursor.cs" />
<Compile Include="ImageViewActivity.cs" />
<Compile Include="addons\OtpKeyProv\EncodingUtil.cs" />
<Compile Include="addons\OtpKeyProv\OathHotpKeyProv.cs" />
@@ -1854,6 +1855,12 @@
<ItemGroup>
<AndroidResource Include="Resources\drawable\ic_launcher_background_green.xml" />
</ItemGroup>
<ItemGroup>
<AndroidResource Include="Resources\drawable-mdpi\vdots_bright.png" />
</ItemGroup>
<ItemGroup>
<AndroidResource Include="Resources\drawable-xhdpi\vdots_bright.png" />
</ItemGroup>
<Import Project="$(MSBuildExtensionsPath)\Xamarin\Android\Xamarin.Android.CSharp.targets" />
<Import Project="..\packages\Microsoft.Bcl.Build.1.0.14\tools\Microsoft.Bcl.Build.targets" Condition="Exists('..\packages\Microsoft.Bcl.Build.1.0.14\tools\Microsoft.Bcl.Build.targets')" />
<Target Name="EnsureBclBuildImported" BeforeTargets="BeforeBuild" Condition="'$(BclBuildImported)' == ''">