keepass2android/src/keepass2android/PwGroupListAdapter.cs

177 lines
4.8 KiB
C#
Raw Normal View History

2013-02-23 11:43:42 -05:00
/*
This file is part of Keepass2Android, Copyright 2013 Philipp Crocoll. This file is based on Keepassdroid, Copyright Brian Pellin.
Keepass2Android is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 2 of the License, or
(at your option) any later version.
Keepass2Android is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Keepass2Android. If not, see <http://www.gnu.org/licenses/>.
*/
using System;
using System.Collections.Generic;
using System.Globalization;
2013-02-23 11:43:42 -05:00
using Android.Content;
using Android.Views;
using Android.Widget;
using Android.Preferences;
using KeePassLib;
using keepass2android.view;
namespace keepass2android
{
public class PwGroupListAdapter : BaseAdapter
{
private readonly GroupBaseActivity _act;
private readonly PwGroup _group;
private List<PwGroup> _groupsForViewing;
private List<PwEntry> _entriesForViewing;
2013-02-23 11:43:42 -05:00
private readonly ISharedPreferences _prefs;
2013-02-23 11:43:42 -05:00
public PwGroupListAdapter(GroupBaseActivity act, PwGroup group) {
_act = act;
_group = group;
_prefs = PreferenceManager.GetDefaultSharedPreferences(act);
2013-02-23 11:43:42 -05:00
FilterAndSort();
2013-02-23 11:43:42 -05:00
}
public override void NotifyDataSetChanged() {
base.NotifyDataSetChanged();
FilterAndSort();
2013-02-23 11:43:42 -05:00
}
public override void NotifyDataSetInvalidated() {
base.NotifyDataSetInvalidated();
FilterAndSort();
2013-02-23 11:43:42 -05:00
}
private void FilterAndSort() {
_entriesForViewing = new List<PwEntry>();
2013-02-23 11:43:42 -05:00
foreach (PwEntry entry in _group.Entries)
2013-02-23 11:43:42 -05:00
{
_entriesForViewing.Add(entry);
2013-02-23 11:43:42 -05:00
}
bool sortLists = _prefs.GetBoolean(_act.GetString(Resource.String.sort_key), _act.Resources.GetBoolean(Resource.Boolean.sort_default));
2013-02-23 11:43:42 -05:00
if ( sortLists )
{
_groupsForViewing = new List<PwGroup>(_group.Groups);
_groupsForViewing.Sort( (x, y) => { return String.Compare (x.Name, y.Name, true); });
_entriesForViewing.Sort( (x, y) =>
2013-02-23 11:43:42 -05:00
{
String nameX = x.Strings.ReadSafe(PwDefs.TitleField);
String nameY = y.Strings.ReadSafe(PwDefs.TitleField);
if (nameX.ToLower() != nameY.ToLower())
return String.Compare(nameX, nameY, StringComparison.OrdinalIgnoreCase);
else
{
if (PwDefs.IsTanEntry(x) && PwDefs.IsTanEntry(y))
{
//compare the user name fields (=TAN index)
String userX = x.Strings.ReadSafe(PwDefs.UserNameField);
String userY = y.Strings.ReadSafe(PwDefs.UserNameField);
if (userX != userY)
{
try
{
return int.Parse(userX).CompareTo(int.Parse(userY));
}
catch (Exception)
{
//ignore
}
return String.Compare(userX, userY, StringComparison.OrdinalIgnoreCase);
}
}
//use creation time for non-tan entries:
return x.CreationTime.CompareTo(y.CreationTime);
}
}
2013-02-23 11:43:42 -05:00
);
} else {
_groupsForViewing = new List<PwGroup>(_group.Groups);
2013-02-23 11:43:42 -05:00
}
}
public override int Count
{
get{
return _groupsForViewing.Count + _entriesForViewing.Count;
2013-02-23 11:43:42 -05:00
}
}
public override Java.Lang.Object GetItem(int position) {
return position;
}
public override long GetItemId(int position) {
return position;
}
public override View GetView(int position, View convertView, ViewGroup parent) {
int size = _groupsForViewing.Count;
2013-02-23 11:43:42 -05:00
if ( position < size ) {
return CreateGroupView(position, convertView);
2013-02-23 11:43:42 -05:00
} else {
return CreateEntryView(position - size, convertView);
2013-02-23 11:43:42 -05:00
}
}
private View CreateGroupView(int position, View convertView) {
PwGroup g = _groupsForViewing[position];
2013-02-23 11:43:42 -05:00
PwGroupView gv;
if (convertView == null || !(convertView is PwGroupView)) {
gv = PwGroupView.GetInstance(_act, g);
2013-02-23 11:43:42 -05:00
}
else {
gv = (PwGroupView) convertView;
gv.ConvertView(g);
2013-02-23 11:43:42 -05:00
}
return gv;
}
private PwEntryView CreateEntryView(int position, View convertView) {
PwEntry entry = _entriesForViewing[position];
2013-02-23 11:43:42 -05:00
PwEntryView ev;
if (convertView == null || !(convertView is PwEntryView)) {
ev = PwEntryView.GetInstance(_act, entry, position);
2013-02-23 11:43:42 -05:00
}
else {
ev = (PwEntryView) convertView;
ev.ConvertView(entry, position);
2013-02-23 11:43:42 -05:00
}
return ev;
}
}
}