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

View File

@ -74,9 +74,9 @@ namespace keepass2android
db.SearchHelper = new SearchDbHelper(_app);
// 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();
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();
// Commit changes

View File

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

View File

@ -133,7 +133,7 @@ namespace keepass2android
RunnableOnFinish task;
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
{

View File

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

View File

@ -22,6 +22,8 @@ using Android.Graphics;
using Android.OS;
using Android.Views;
using Android.Widget;
using KeePassLib;
using KeePassLib.Utility;
namespace keepass2android
{
@ -44,34 +46,44 @@ namespace keepass2android
SetContentView(Resource.Layout.icon_picker);
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);
SetResult((Result)EntryEditActivity.ResultOkIconPicker, intent);
}
SetResult((Result)EntryEditActivity.ResultOkIconPicker, intent);
Finish();
};
Finish();
};
}
public class ImageAdapter : BaseAdapter
{
readonly IconPickerActivity _act;
private readonly PwDatabase _db;
public ImageAdapter(IconPickerActivity act)
public ImageAdapter(IconPickerActivity act, PwDatabase db)
{
_act = act;
_db = db;
}
public override int Count
{
get
{
/* Return number of KeePass icons */
return Icons.Count();
return Icons.Count() + _db.CustomIcons.Count;
}
}
@ -97,16 +109,41 @@ namespace keepass2android
{
currView = convertView;
}
TextView tv = (TextView) currView.FindViewById(Resource.Id.icon_text);
tv.Text = "" + position;
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;
Color color = new Color(189, 189, 189);
iv.SetColorFilter(color, mMode);
if (position < Icons.Count())
{
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;
}
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()];
}
}
}