From d5a49db7829facf220c2ebc763901844438a09f4 Mon Sep 17 00:00:00 2001 From: Philipp Crocoll Date: Tue, 21 Aug 2018 19:34:22 +0200 Subject: [PATCH] Prevent users from accidentally opening the local backup, closes #479 --- .../Resources/drawable-mdpi/vdots_bright.png | Bin 0 -> 568 bytes .../Resources/drawable-xhdpi/vdots_bright.png | Bin 0 -> 1047 bytes .../Resources/layout/file_row.xml | 2 +- .../Resources/layout/recent_files.xml | 9 ++ .../Resources/values/strings.xml | 1 + .../fileselect/FileDbHelper.cs | 2 +- .../fileselect/FileSelectActivity.cs | 44 +++++--- .../fileselect/FilteredCursor.cs | 104 ++++++++++++++++++ src/keepass2android/keepass2android.csproj | 7 ++ 9 files changed, 150 insertions(+), 19 deletions(-) create mode 100644 src/keepass2android/Resources/drawable-mdpi/vdots_bright.png create mode 100644 src/keepass2android/Resources/drawable-xhdpi/vdots_bright.png create mode 100644 src/keepass2android/fileselect/FilteredCursor.cs diff --git a/src/keepass2android/Resources/drawable-mdpi/vdots_bright.png b/src/keepass2android/Resources/drawable-mdpi/vdots_bright.png new file mode 100644 index 0000000000000000000000000000000000000000..93e2f5ec54aafe88dc10044746d9a6fb5d4f5f07 GIT binary patch literal 568 zcmV-80>}M{P)n zK`auq5gSbxLC0p12wq|?ljYDGI4&^%?foP(JKtS0s!Gx#O9CS`_1l<@bCHOAeRj?r z0Gq%Z@CICo$mu|S{2Qq1*hi&OK~?jr+SH$;sj7Le8~ez_?7j?e4(trkTmZX(hN`K< z5r**j!;po(XRI?ZHDjzZG4)}rGck2ztozQHkaAQV9Zh~;$ zm;kodX|)2JGqV?EMpUVwB<%*?0^JSTeFBb{*?g`;?`8pz2p%`H9C|<-@D(#+62Ysxtj?DS>cD^SJeVV>or!ox2emEb{+9{rXCj`_LCq%O8U6BLC-5Dx zqB0?17qF03M48z_NL`B3MDWZqg*gGdDIlr@oHVn^EK?XWn*{C_V0AZmWnDdJ1D*gE z>$G|bTr#uYWmXSLuSbOg902au(fmFzP=!>gB$e`7XW!=~O=)vYNje|1|Itdps}9tF z)4&DbIM5S5`T$G;&wy8Eru9H&d5-!YBO=RlRH|}N4$46}CXV-4C}GqVTTmRZ7iW*XShpxqDPsF}UbwahY703?FPNSPfk^9E=Kl^dWP z1^F#gxdFPITSWC12&oFbH^(dUeTjHR2i28`r{{X9fLct%GdiemiFo?uK@t%arjv~q zXgR*w6g5v_a!+B*>^(4CfYosD%JNsQ3hM!Iy8(;a)yhGs^1@w{S2)8#CQ}M-ipPE$ zxw+uaM{VKM;kjOm=6VjwLAe~1gK|&~%0cC%=+8$rI>jUV>Ot6dGLwd4J@(Tf>0TX= z{M-vGm*rnQ@QIDJ>rMlZ2yO3_-D#hE-J}t9i(=M2u;7(nMBxiW)U2KYAyuQQC&w%E zy@_~62i2K~XLL}167h@ diff --git a/src/keepass2android/Resources/layout/recent_files.xml b/src/keepass2android/Resources/layout/recent_files.xml index 33c52f02..020a63ec 100644 --- a/src/keepass2android/Resources/layout/recent_files.xml +++ b/src/keepass2android/Resources/layout/recent_files.xml @@ -22,4 +22,13 @@ android:paddingRight="0dp" android:paddingLeft="0dp" /> + + \ No newline at end of file diff --git a/src/keepass2android/Resources/values/strings.xml b/src/keepass2android/Resources/values/strings.xml index 9fc518d7..986cb092 100644 --- a/src/keepass2android/Resources/values/strings.xml +++ b/src/keepass2android/Resources/values/strings.xml @@ -364,6 +364,7 @@ Create a local backup copy after successfully loading a database. Updating local backup... Local backup of %1$s + Show local backups diff --git a/src/keepass2android/fileselect/FileDbHelper.cs b/src/keepass2android/fileselect/FileDbHelper.cs index edd5910d..01c5739d 100644 --- a/src/keepass2android/fileselect/FileDbHelper.cs +++ b/src/keepass2android/fileselect/FileDbHelper.cs @@ -24,7 +24,7 @@ using KeePassLib.Serialization; namespace keepass2android { - /// + /// /// Class to store the recent files in a database /// public class FileDbHelper { diff --git a/src/keepass2android/fileselect/FileSelectActivity.cs b/src/keepass2android/fileselect/FileSelectActivity.cs index 1097c566..31cb2692 100644 --- a/src/keepass2android/fileselect/FileSelectActivity.cs +++ b/src/keepass2android/fileselect/FileSelectActivity.cs @@ -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(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(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(Resource.Id.recent_files).SetAdapter(new MyCursorAdapter(this, filesCursor,App.Kp2a)); + StartManagingCursor(filesCursor); + + FragmentManager.FindFragmentById(Resource.Id.recent_files).SetAdapter(new MyCursorAdapter(this, filesCursor,App.Kp2a)); } diff --git a/src/keepass2android/fileselect/FilteredCursor.cs b/src/keepass2android/fileselect/FilteredCursor.cs new file mode 100644 index 00000000..dbbc21c9 --- /dev/null +++ b/src/keepass2android/fileselect/FilteredCursor.cs @@ -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 _indicesToKeep; + private int _pos; + + protected FilteredCursor(IntPtr javaReference, JniHandleOwnership transfer) : base(javaReference, transfer) + { + } + + public FilteredCursor(ICursor cursor, Predicate filter) : base(cursor) + { + _indicesToKeep = new List(); + 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; } + } + + } +} \ No newline at end of file diff --git a/src/keepass2android/keepass2android.csproj b/src/keepass2android/keepass2android.csproj index 682428e0..4202a813 100644 --- a/src/keepass2android/keepass2android.csproj +++ b/src/keepass2android/keepass2android.csproj @@ -175,6 +175,7 @@ + @@ -1854,6 +1855,12 @@ + + + + + +