allow to pick (existing) custom icons

This commit is contained in:
Philipp Crocoll 2015-09-30 20:43:38 +02:00
parent 89672fe201
commit 1bca643c3f
6 changed files with 96 additions and 44 deletions

View File

@ -30,21 +30,25 @@ namespace keepass2android
private IKp2aApp _app; private IKp2aApp _app;
private readonly String _name; private readonly String _name;
private readonly int _iconId; private readonly int _iconId;
private readonly PwUuid _groupCustomIconId;
internal PwGroup Group; internal PwGroup Group;
internal PwGroup Parent; internal PwGroup Parent;
protected bool DontSave; protected bool DontSave;
readonly Context _ctx; readonly Context _ctx;
public static AddGroup GetInstance(Context ctx, IKp2aApp app, String name, int iconid, PwGroup parent, OnFinish finish, bool dontSave) { public static AddGroup GetInstance(Context ctx, IKp2aApp app, string name, int iconid, PwUuid groupCustomIconId, PwGroup parent, OnFinish finish, bool dontSave) {
return new AddGroup(ctx, app, name, iconid, parent, finish, dontSave); return new AddGroup(ctx, app, name, iconid, groupCustomIconId, parent, finish, dontSave);
} }
private AddGroup(Context ctx, IKp2aApp app, String name, int iconid, PwGroup parent, OnFinish finish, bool dontSave): base(finish) { private AddGroup(Context ctx, IKp2aApp app, String name, int iconid, PwUuid groupCustomIconId, PwGroup parent, OnFinish finish, bool dontSave)
: base(finish)
{
_ctx = ctx; _ctx = ctx;
_name = name; _name = name;
_iconId = iconid; _iconId = iconid;
_groupCustomIconId = groupCustomIconId;
Parent = parent; Parent = parent;
DontSave = dontSave; DontSave = dontSave;
_app = app; _app = app;
@ -57,6 +61,10 @@ namespace keepass2android
StatusLogger.UpdateMessage(UiStringKey.AddingGroup); StatusLogger.UpdateMessage(UiStringKey.AddingGroup);
// Generate new group // Generate new group
Group = new PwGroup(true, true, _name, (PwIcon)_iconId); Group = new PwGroup(true, true, _name, (PwIcon)_iconId);
if (_groupCustomIconId != null)
{
Group.CustomIconUuid = _groupCustomIconId;
}
Parent.AddGroup(Group, true); Parent.AddGroup(Group, true);
// Commit to disk // Commit to disk

View File

@ -74,9 +74,9 @@ namespace keepass2android
db.SearchHelper = new SearchDbHelper(_app); db.SearchHelper = new SearchDbHelper(_app);
// Add a couple default groups // Add a couple default groups
AddGroup internet = AddGroup.GetInstance(_ctx, _app, "Internet", 1, db.KpDatabase.RootGroup, null, true); AddGroup internet = AddGroup.GetInstance(_ctx, _app, "Internet", 1, null, db.KpDatabase.RootGroup, null, true);
internet.Run(); internet.Run();
AddGroup email = AddGroup.GetInstance(_ctx, _app, "eMail", 19, db.KpDatabase.RootGroup, null, true); AddGroup email = AddGroup.GetInstance(_ctx, _app, "eMail", 19, null, db.KpDatabase.RootGroup, null, true);
email.Run(); email.Run();
// Commit changes // Commit changes

View File

@ -153,6 +153,8 @@ namespace keepass2android
{ {
State.EntryInDatabase.IconId = State.ParentGroup.IconId; // Inherit icon from group State.EntryInDatabase.IconId = State.ParentGroup.IconId; // Inherit icon from group
} }
else
State.EntryInDatabase.IconId = PwIcon.Key;
State.EntryInDatabase.CustomIconUuid = State.ParentGroup.CustomIconUuid; State.EntryInDatabase.CustomIconUuid = State.ParentGroup.CustomIconUuid;
/* /*
@ -206,8 +208,7 @@ namespace keepass2android
if (State.SelectedIcon) if (State.SelectedIcon)
{ {
//TODO: custom image App.Kp2a.GetDb().DrawableFactory.AssignDrawableTo(iconButton, Resources, App.Kp2a.GetDb().KpDatabase, (PwIcon)State.SelectedIconId, State.SelectedCustomIconId, false);
iconButton.SetImageResource(Icons.IconToResId(State.SelectedIconId, false));
} }
iconButton.Click += (sender, evt) => { iconButton.Click += (sender, evt) => {
UpdateEntryFromUi(State.Entry); UpdateEntryFromUi(State.Entry);
@ -327,17 +328,11 @@ namespace keepass2android
bool bCreateBackup = (!State.IsNew); bool bCreateBackup = (!State.IsNew);
if(bCreateBackup) newEntry.CreateBackup(null); if(bCreateBackup) newEntry.CreateBackup(null);
if (State.SelectedIcon == false) { if (State.SelectedIcon)
if (State.IsNew) { {
newEntry.IconId = PwIcon.Key;
} else {
// Keep previous icon, if no new one was selected
}
}
else {
newEntry.IconId = State.SelectedIconId; newEntry.IconId = State.SelectedIconId;
newEntry.CustomIconUuid = State.SelectedCustomIconId; newEntry.CustomIconUuid = State.SelectedCustomIconId;
} } //else the State.EntryInDatabase.Icon
/* KPDesktop /* KPDesktop
if(m_cbCustomForegroundColor.Checked) if(m_cbCustomForegroundColor.Checked)
newEntry.ForegroundColor = m_clrForeground; newEntry.ForegroundColor = m_clrForeground;
@ -687,7 +682,7 @@ namespace keepass2android
switch (resultCode) switch (resultCode)
{ {
case (Result)ResultOkIconPicker: case (Result)ResultOkIconPicker:
State.SelectedIconId = (PwIcon) data.Extras.GetInt(IconPickerActivity.KeyIconId); State.SelectedIconId = (PwIcon) data.Extras.GetInt(IconPickerActivity.KeyIconId,(int)PwIcon.Key);
State.SelectedCustomIconId = PwUuid.Zero; State.SelectedCustomIconId = PwUuid.Zero;
String customIconIdString = data.Extras.GetString(IconPickerActivity.KeyCustomIconId); String customIconIdString = data.Extras.GetString(IconPickerActivity.KeyCustomIconId);
if (!String.IsNullOrEmpty(customIconIdString)) if (!String.IsNullOrEmpty(customIconIdString))

View File

@ -133,7 +133,7 @@ namespace keepass2android
RunnableOnFinish task; RunnableOnFinish task;
if (strGroupUuid == null) if (strGroupUuid == null)
{ {
task = AddGroup.GetInstance(this, App.Kp2a, groupName, groupIconId, Group, new RefreshTask(handler, this), false); task = AddGroup.GetInstance(this, App.Kp2a, groupName, groupIconId, groupCustomIconId, Group, new RefreshTask(handler, this), false);
} }
else else
{ {

View File

@ -19,6 +19,7 @@ using System;
using Android.App; using Android.App;
using Android.Content; using Android.Content;
using Android.OS; using Android.OS;
using Android.Runtime;
using Android.Widget; using Android.Widget;
using KeePassLib; using KeePassLib;
using KeePassLib.Utility; using KeePassLib.Utility;
@ -45,6 +46,13 @@ namespace keepass2android
_design = new ActivityDesign(this); _design = new ActivityDesign(this);
} }
protected GroupEditActivity(IntPtr javaReference, JniHandleOwnership transfer)
: base(javaReference, transfer)
{
}
public static void Launch(Activity act, PwGroup parentGroup) public static void Launch(Activity act, PwGroup parentGroup)
{ {
Intent i = new Intent(act, typeof(GroupEditActivity)); Intent i = new Intent(act, typeof(GroupEditActivity));
@ -91,7 +99,8 @@ namespace keepass2android
intent.PutExtra (KeyName, name); intent.PutExtra (KeyName, name);
intent.PutExtra (KeyIconId, _selectedIconId); intent.PutExtra (KeyIconId, _selectedIconId);
intent.PutExtra(KeyCustomIconId, MemUtil.ByteArrayToHexString(_selectedCustomIconId.UuidBytes)); if (_selectedCustomIconId != null)
intent.PutExtra(KeyCustomIconId, MemUtil.ByteArrayToHexString(_selectedCustomIconId.UuidBytes));
if (_groupToEdit != null) if (_groupToEdit != null)
intent.PutExtra(KeyGroupUuid, MemUtil.ByteArrayToHexString(_groupToEdit.Uuid.UuidBytes)); intent.PutExtra(KeyGroupUuid, MemUtil.ByteArrayToHexString(_groupToEdit.Uuid.UuidBytes));
@ -135,10 +144,13 @@ namespace keepass2android
switch ((int)resultCode) switch ((int)resultCode)
{ {
case EntryEditActivity.ResultOkIconPicker: case EntryEditActivity.ResultOkIconPicker:
_selectedIconId = data.Extras.GetInt(IconPickerActivity.KeyIconId); _selectedIconId = data.Extras.GetInt(IconPickerActivity.KeyIconId, (int) PwIcon.Key);
_selectedCustomIconId = PwUuid.Zero; String customIconIdString = data.Extras.GetString(IconPickerActivity.KeyCustomIconId);
if (!String.IsNullOrEmpty(customIconIdString))
_selectedCustomIconId = new PwUuid(MemUtil.HexStringToByteArray(customIconIdString));
ImageButton currIconButton = (ImageButton) FindViewById(Resource.Id.icon_button); ImageButton currIconButton = (ImageButton) FindViewById(Resource.Id.icon_button);
currIconButton.SetImageResource(Icons.IconToResId((PwIcon)_selectedIconId, false)); App.Kp2a.GetDb().DrawableFactory.AssignDrawableTo(currIconButton, Resources, App.Kp2a.GetDb().KpDatabase, (PwIcon) _selectedIconId, _selectedCustomIconId, false);
break; break;
} }
} }

View File

@ -22,6 +22,8 @@ using Android.Graphics;
using Android.OS; using Android.OS;
using Android.Views; using Android.Views;
using Android.Widget; using Android.Widget;
using KeePassLib;
using KeePassLib.Utility;
namespace keepass2android namespace keepass2android
{ {
@ -44,34 +46,44 @@ namespace keepass2android
SetContentView(Resource.Layout.icon_picker); SetContentView(Resource.Layout.icon_picker);
GridView currIconGridView = (GridView)FindViewById(Resource.Id.IconGridView); GridView currIconGridView = (GridView)FindViewById(Resource.Id.IconGridView);
currIconGridView.Adapter = new ImageAdapter(this); currIconGridView.Adapter = new ImageAdapter(this, App.Kp2a.GetDb().KpDatabase);
currIconGridView.ItemClick += (sender, e) => { currIconGridView.ItemClick += (sender, e) =>
{
Intent intent = new Intent(); Intent intent = new Intent();
if (((ImageAdapter) currIconGridView.Adapter).IsCustomIcon(e.Position))
{
intent.PutExtra(KeyCustomIconId,
MemUtil.ByteArrayToHexString(((ImageAdapter) currIconGridView.Adapter).GetCustomIcon(e.Position).Uuid.UuidBytes));
}
else
{
intent.PutExtra(KeyIconId, e.Position); intent.PutExtra(KeyIconId, e.Position);
SetResult((Result)EntryEditActivity.ResultOkIconPicker, intent); }
SetResult((Result)EntryEditActivity.ResultOkIconPicker, intent);
Finish(); Finish();
}; };
} }
public class ImageAdapter : BaseAdapter public class ImageAdapter : BaseAdapter
{ {
readonly IconPickerActivity _act; readonly IconPickerActivity _act;
private readonly PwDatabase _db;
public ImageAdapter(IconPickerActivity act) public ImageAdapter(IconPickerActivity act, PwDatabase db)
{ {
_act = act; _act = act;
_db = db;
} }
public override int Count public override int Count
{ {
get get
{ {
/* Return number of KeePass icons */ return Icons.Count() + _db.CustomIcons.Count;
return Icons.Count();
} }
} }
@ -97,16 +109,41 @@ namespace keepass2android
{ {
currView = convertView; currView = convertView;
} }
TextView tv = (TextView) currView.FindViewById(Resource.Id.icon_text); TextView tv = (TextView) currView.FindViewById(Resource.Id.icon_text);
tv.Text = "" + position;
ImageView iv = (ImageView) currView.FindViewById(Resource.Id.icon_image); ImageView iv = (ImageView) currView.FindViewById(Resource.Id.icon_image);
iv.SetImageResource(Icons.IconToResId((KeePassLib.PwIcon)position, false));
Android.Graphics.PorterDuff.Mode mMode = Android.Graphics.PorterDuff.Mode.SrcAtop; if (position < Icons.Count())
Color color = new Color(189, 189, 189); {
iv.SetColorFilter(color, mMode); tv.Text = "" + position;
iv.SetImageResource(Icons.IconToResId((KeePassLib.PwIcon) position, false));
Android.Graphics.PorterDuff.Mode mMode = Android.Graphics.PorterDuff.Mode.SrcAtop;
Color color = new Color(189, 189, 189);
iv.SetColorFilter(color, mMode);
}
else
{
int pos = position - Icons.Count();
var icon = _db.CustomIcons[pos];
tv.Text = pos.ToString();
iv.SetColorFilter(null);
iv.SetImageBitmap(icon.Image);
}
return currView; return currView;
} }
public bool IsCustomIcon(int position)
{
return position >= Icons.Count();
}
public PwCustomIcon GetCustomIcon(int position)
{
if (!IsCustomIcon(position))
return null;
return _db.CustomIcons[position - Icons.Count()];
}
} }
} }