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.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
|
|
|
|
2013-06-15 16:02:48 -04:00
|
|
|
public void AssignDrawableTo (ImageView iv, Resources res, PwDatabase db, PwIcon icon, PwUuid customIconId)
|
2013-02-23 11:43:42 -05:00
|
|
|
{
|
2013-06-15 06:40:01 -04:00
|
|
|
Drawable draw = GetIconDrawable (res, db, icon, customIconId);
|
2013-02-23 11:43:42 -05:00
|
|
|
iv.SetImageDrawable (draw);
|
|
|
|
}
|
|
|
|
|
2013-06-15 06:40:01 -04:00
|
|
|
public Drawable GetIconDrawable (Resources res, PwDatabase db, PwIcon icon, PwUuid customIconId)
|
2013-02-23 11:43:42 -05:00
|
|
|
{
|
2013-06-15 06:40:01 -04:00
|
|
|
if (customIconId != PwUuid.Zero) {
|
|
|
|
return GetIconDrawable (res, db, customIconId);
|
2013-02-23 11:43:42 -05:00
|
|
|
}
|
2013-06-15 06:40:01 -04:00
|
|
|
return GetIconDrawable (res, icon);
|
2013-02-23 11:43:42 -05:00
|
|
|
}
|
2013-06-15 06:40:01 -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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-15 06:40:01 -04:00
|
|
|
public Drawable GetIconDrawable (Resources res, PwIcon icon)
|
2013-02-23 11:43:42 -05:00
|
|
|
{
|
2013-06-15 06:40:01 -04:00
|
|
|
int resId = Icons.IconToResId (icon);
|
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-02-23 11:43:42 -05:00
|
|
|
if (icon == 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-15 06:40:01 -04:00
|
|
|
draw = BitmapDrawableCompat.GetBitmapDrawable (res, bitmap);
|
|
|
|
_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
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|