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
|
2014-01-26 08:27:27 -05:00
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
2013-02-23 11:43:42 -05:00
|
|
|
(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.Collections.Generic;
|
|
|
|
using Android.Widget;
|
|
|
|
using Android.Graphics.Drawables;
|
|
|
|
using Android.Content.Res;
|
|
|
|
using KeePassLib;
|
|
|
|
using Android.Graphics;
|
|
|
|
|
|
|
|
namespace keepass2android
|
|
|
|
{
|
2013-06-15 16:02:48 -04:00
|
|
|
/// <summary>
|
|
|
|
/// Factory to create password icons
|
|
|
|
/// </summary>
|
2013-06-14 00:14:50 -04:00
|
|
|
public class DrawableFactory: IDrawableFactory
|
2013-02-23 11:43:42 -05:00
|
|
|
{
|
2013-06-15 06:40:01 -04:00
|
|
|
private static Drawable _blank;
|
|
|
|
private static int _blankWidth = -1;
|
|
|
|
private static int _blankHeight = -1;
|
2013-02-23 11:43:42 -05:00
|
|
|
|
|
|
|
/** customIconMap
|
|
|
|
* Cache for icon drawable.
|
|
|
|
* Keys: UUID, Values: Drawables
|
|
|
|
*/
|
2013-06-15 06:40:01 -04:00
|
|
|
private readonly Dictionary<PwUuid, Drawable> _customIconMap = new Dictionary<PwUuid, Drawable>(new PwUuidEqualityComparer());
|
2013-02-23 11:43:42 -05:00
|
|
|
|
|
|
|
/** standardIconMap
|
|
|
|
* Cache for icon drawable.
|
|
|
|
* Keys: Integer, Values: Drawables
|
|
|
|
*/
|
2013-06-15 06:40:01 -04:00
|
|
|
private readonly Dictionary<int/*resId*/, Drawable> _standardIconMap = new Dictionary<int, Drawable>();
|
2013-02-23 11:43:42 -05:00
|
|
|
|
2015-09-13 06:33:24 -04:00
|
|
|
public void AssignDrawableTo (ImageView iv, Resources res, PwDatabase db, PwIcon icon, PwUuid customIconId, bool forGroup)
|
2013-02-23 11:43:42 -05:00
|
|
|
{
|
2015-09-13 06:33:24 -04:00
|
|
|
Drawable draw = GetIconDrawable (res, db, icon, customIconId, forGroup);
|
2013-02-23 11:43:42 -05:00
|
|
|
iv.SetImageDrawable (draw);
|
|
|
|
}
|
|
|
|
|
2015-09-13 06:33:24 -04:00
|
|
|
public Drawable GetIconDrawable (Resources res, PwDatabase db, PwIcon icon, PwUuid customIconId, bool forGroup)
|
2013-02-23 11:43:42 -05:00
|
|
|
{
|
2013-11-12 22:13:45 -05:00
|
|
|
if (!customIconId.Equals(PwUuid.Zero)) {
|
2013-06-15 06:40:01 -04:00
|
|
|
return GetIconDrawable (res, db, customIconId);
|
2013-02-23 11:43:42 -05:00
|
|
|
}
|
2015-09-13 06:33:24 -04:00
|
|
|
return GetIconDrawable (res, icon, forGroup);
|
2013-02-23 11:43:42 -05:00
|
|
|
}
|
2013-06-19 13:44:35 -04:00
|
|
|
|
2013-06-19 15:31:18 -04:00
|
|
|
private static void InitBlank (Resources res)
|
2013-02-23 11:43:42 -05:00
|
|
|
{
|
2013-06-15 06:40:01 -04:00
|
|
|
if (_blank == null) {
|
|
|
|
_blank = res.GetDrawable (Resource.Drawable.ic99_blank);
|
|
|
|
_blankWidth = _blank.IntrinsicWidth;
|
|
|
|
_blankHeight = _blank.IntrinsicHeight;
|
2013-02-23 11:43:42 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-13 06:33:24 -04:00
|
|
|
public Drawable GetIconDrawable (Resources res, PwIcon icon, bool forGroup)
|
2013-02-23 11:43:42 -05:00
|
|
|
{
|
2015-09-13 06:33:24 -04:00
|
|
|
int resId = Icons.IconToResId (icon, forGroup);
|
2013-02-23 11:43:42 -05:00
|
|
|
|
|
|
|
Drawable draw;
|
2013-06-15 06:40:01 -04:00
|
|
|
if (!_standardIconMap.TryGetValue(resId, out draw))
|
2013-02-23 11:43:42 -05:00
|
|
|
{
|
|
|
|
draw = res.GetDrawable(resId);
|
2013-06-15 06:40:01 -04:00
|
|
|
_standardIconMap[resId] = draw;
|
2013-02-23 11:43:42 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
return draw;
|
|
|
|
}
|
|
|
|
|
2013-06-15 06:40:01 -04:00
|
|
|
public Drawable GetIconDrawable (Resources res, PwDatabase db, PwUuid icon)
|
2013-02-23 11:43:42 -05:00
|
|
|
{
|
2013-06-15 06:40:01 -04:00
|
|
|
InitBlank (res);
|
2013-11-12 22:13:45 -05:00
|
|
|
if (icon.Equals(PwUuid.Zero)) {
|
2013-06-15 06:40:01 -04:00
|
|
|
return _blank;
|
2013-02-23 11:43:42 -05:00
|
|
|
}
|
2013-06-15 06:40:01 -04:00
|
|
|
Drawable draw;
|
|
|
|
if (!_customIconMap.TryGetValue(icon, out draw))
|
2013-02-23 11:43:42 -05:00
|
|
|
{
|
|
|
|
Bitmap bitmap = db.GetCustomIcon(icon);
|
|
|
|
|
|
|
|
// Could not understand custom icon
|
|
|
|
if (bitmap == null) {
|
2013-06-15 06:40:01 -04:00
|
|
|
return _blank;
|
2013-02-23 11:43:42 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
bitmap = resize (bitmap);
|
2013-06-18 15:12:34 -04:00
|
|
|
|
|
|
|
draw = new BitmapDrawable(res, bitmap);
|
2013-06-15 06:40:01 -04:00
|
|
|
_customIconMap[icon] = draw;
|
2013-02-23 11:43:42 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
return draw;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Resize the custom icon to match the built in icons
|
|
|
|
* @param bitmap
|
|
|
|
* @return
|
|
|
|
*/
|
|
|
|
private Bitmap resize (Bitmap bitmap)
|
|
|
|
{
|
|
|
|
int width = bitmap.Width;
|
|
|
|
int height = bitmap.Height;
|
|
|
|
|
2013-06-15 06:40:01 -04:00
|
|
|
if (width == _blankWidth && height == _blankHeight) {
|
2013-02-23 11:43:42 -05:00
|
|
|
return bitmap;
|
|
|
|
}
|
|
|
|
|
2013-06-15 06:40:01 -04:00
|
|
|
return Bitmap.CreateScaledBitmap (bitmap, _blankWidth, _blankHeight, true);
|
2013-02-23 11:43:42 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
public void Clear ()
|
|
|
|
{
|
2013-06-15 06:40:01 -04:00
|
|
|
_standardIconMap.Clear ();
|
|
|
|
_customIconMap.Clear ();
|
2013-02-23 11:43:42 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|