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/>.
|
|
|
|
*/
|
|
|
|
|
2016-01-11 14:58:42 -05:00
|
|
|
using System;
|
2013-02-23 11:43:42 -05:00
|
|
|
using System.Collections.Generic;
|
2015-12-21 15:23:28 -05:00
|
|
|
using Android.App;
|
|
|
|
using Android.Content;
|
|
|
|
using Android.Content.PM;
|
2013-02-23 11:43:42 -05:00
|
|
|
using Android.Widget;
|
|
|
|
using Android.Graphics.Drawables;
|
|
|
|
using Android.Content.Res;
|
|
|
|
using KeePassLib;
|
|
|
|
using Android.Graphics;
|
2015-12-21 15:23:28 -05:00
|
|
|
using Android.Preferences;
|
2013-02-23 11:43:42 -05:00
|
|
|
|
|
|
|
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
|
|
|
{
|
2015-12-21 15:23:28 -05:00
|
|
|
private static Drawable _blank;
|
2013-06-15 06:40:01 -04:00
|
|
|
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
|
|
|
|
*/
|
2015-12-21 15:23:28 -05:00
|
|
|
private readonly Dictionary<int/*icon key*/, Drawable> _standardIconMap = new Dictionary<int, Drawable>();
|
2015-12-21 23:42:02 -05:00
|
|
|
|
|
|
|
public void AssignDrawableTo(ImageView iv, Context context, PwDatabase db, PwIcon icon, PwUuid customIconId, bool forGroup)
|
2013-02-23 11:43:42 -05:00
|
|
|
{
|
2015-12-21 15:23:28 -05:00
|
|
|
Drawable draw = GetIconDrawable (context, db, icon, customIconId, forGroup);
|
|
|
|
if (draw != null)
|
2015-12-21 23:42:02 -05:00
|
|
|
{
|
|
|
|
draw = draw.Mutate();
|
2015-12-21 15:23:28 -05:00
|
|
|
iv.SetImageDrawable(draw);
|
2015-12-21 23:42:02 -05:00
|
|
|
}
|
|
|
|
|
2015-12-21 15:23:28 -05:00
|
|
|
else
|
2016-01-11 14:58:42 -05:00
|
|
|
Kp2aLog.LogUnexpectedError(new Exception("icon not found : " + icon));
|
2013-02-23 11:43:42 -05:00
|
|
|
}
|
|
|
|
|
2015-12-21 15:23:28 -05:00
|
|
|
public Drawable GetIconDrawable (Context context, PwDatabase db, PwIcon icon, PwUuid customIconId, bool forGroup)
|
2013-02-23 11:43:42 -05:00
|
|
|
{
|
2015-12-21 15:23:28 -05:00
|
|
|
if ((customIconId != null) && (!customIconId.Equals(PwUuid.Zero))) {
|
|
|
|
return GetIconDrawable (context, db, customIconId);
|
2013-02-23 11:43:42 -05:00
|
|
|
}
|
2015-12-21 15:23:28 -05:00
|
|
|
return GetIconDrawable (context, icon, forGroup);
|
2013-02-23 11:43:42 -05:00
|
|
|
}
|
2013-06-19 13:44:35 -04:00
|
|
|
|
2015-12-21 15:23:28 -05:00
|
|
|
public bool IsWhiteIconSet
|
|
|
|
{
|
|
|
|
get
|
|
|
|
{
|
|
|
|
var context = Application.Context;
|
|
|
|
string packageName = PreferenceManager.GetDefaultSharedPreferences(Application.Context).GetString("IconSetKey", context.PackageName);
|
|
|
|
//assume that at the momemt only the built in icons are white
|
|
|
|
return packageName == context.PackageName;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private static void InitBlank(Context context)
|
2013-02-23 11:43:42 -05:00
|
|
|
{
|
2013-06-15 06:40:01 -04:00
|
|
|
if (_blank == null) {
|
2015-12-21 15:23:28 -05:00
|
|
|
_blank = context.Resources.GetDrawable (Resource.Drawable.ic99_blank);
|
2013-06-15 06:40:01 -04:00
|
|
|
_blankWidth = _blank.IntrinsicWidth;
|
|
|
|
_blankHeight = _blank.IntrinsicHeight;
|
2013-02-23 11:43:42 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-12-21 15:23:28 -05:00
|
|
|
public Drawable GetIconDrawable (Context context, PwIcon icon, bool forGroup)
|
2013-02-23 11:43:42 -05:00
|
|
|
{
|
2015-12-21 15:23:28 -05:00
|
|
|
//int resId = DefaultIcons.IconToResId (icon, forGroup);
|
|
|
|
int dictKey = GetDictionaryKey(icon, forGroup);
|
2013-02-23 11:43:42 -05:00
|
|
|
|
|
|
|
Drawable draw;
|
2015-12-21 15:23:28 -05:00
|
|
|
if (!_standardIconMap.TryGetValue(dictKey, out draw))
|
2013-02-23 11:43:42 -05:00
|
|
|
{
|
2015-12-21 15:23:28 -05:00
|
|
|
string packageName = PreferenceManager.GetDefaultSharedPreferences(Application.Context).GetString("IconSetKey", context.PackageName);
|
|
|
|
|
|
|
|
Resources res = context.PackageManager.GetResourcesForApplication(packageName);
|
|
|
|
|
|
|
|
try
|
|
|
|
{
|
|
|
|
int resId = GetResourceId(res, dictKey, packageName);
|
|
|
|
if (resId == 0)
|
|
|
|
draw = context.Resources.GetDrawable(Resource.Drawable.ic99_blank);
|
|
|
|
else
|
|
|
|
draw = res.GetDrawable(resId);
|
|
|
|
|
|
|
|
}
|
|
|
|
catch (System.Exception e)
|
|
|
|
{
|
2016-01-11 14:58:42 -05:00
|
|
|
Kp2aLog.LogUnexpectedError(e);
|
2015-12-21 15:23:28 -05:00
|
|
|
draw = context.Resources.GetDrawable(Resource.Drawable.ic99_blank);
|
|
|
|
}
|
2013-02-23 11:43:42 -05:00
|
|
|
|
2015-12-21 15:23:28 -05:00
|
|
|
}
|
|
|
|
_standardIconMap[dictKey] = draw;
|
2013-02-23 11:43:42 -05:00
|
|
|
return draw;
|
|
|
|
}
|
2015-12-21 15:23:28 -05:00
|
|
|
|
|
|
|
private const int FolderOffset = 10000;
|
|
|
|
|
|
|
|
private int GetResourceId(Resources res, int dictKey, string packageName)
|
|
|
|
{
|
|
|
|
bool forFolder = dictKey >= FolderOffset;
|
|
|
|
int iconId = dictKey%FolderOffset;
|
|
|
|
var name = (forFolder? "icf" : "ic") + iconId.ToString("D2");
|
|
|
|
int resId = res.GetIdentifier(name, "drawable", packageName);
|
|
|
|
if ((forFolder) && (resId == 0)) //if no explicit folder icon was found, retry a normal icon
|
|
|
|
{
|
|
|
|
name = "ic" + iconId.ToString("D2");
|
|
|
|
resId = res.GetIdentifier(name, "drawable", packageName);
|
|
|
|
}
|
|
|
|
//return what we found, may still be 0
|
|
|
|
return resId;
|
|
|
|
}
|
|
|
|
|
|
|
|
private int GetDictionaryKey(PwIcon icon, bool forGroup)
|
|
|
|
{
|
|
|
|
|
|
|
|
return ((int)icon) + (forGroup ? FolderOffset : 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
public Drawable GetIconDrawable(Context context, PwDatabase db, PwUuid icon)
|
2013-02-23 11:43:42 -05:00
|
|
|
{
|
2015-12-21 15:23:28 -05:00
|
|
|
InitBlank (context);
|
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
|
|
|
}
|
|
|
|
|
2015-12-21 15:23:28 -05:00
|
|
|
draw = new BitmapDrawable(context.Resources, bitmap);
|
2013-06-15 06:40:01 -04:00
|
|
|
_customIconMap[icon] = draw;
|
2013-02-23 11:43:42 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
return draw;
|
|
|
|
}
|
2015-10-03 00:53:45 -04:00
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2015-12-21 23:42:02 -05:00
|
|
|
|
2013-02-23 11:43:42 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|