Fix visual bugs and save icon pack setting

This commit is contained in:
J-Jamet committed 2018-04-13 19:47:56 +02:00
1 parent 15c843fbb9
commit 697e9aa923
8 files changed
+217 -68

No files matched your search

@@ -291,9 +291,9 @@ public class EntryActivity extends LockingHideActivity {
int[] attrs = {R.attr.textColorInverse};
TypedArray ta = getTheme().obtainStyledAttributes(attrs);
int iconColor = ta.getColor(0, Color.WHITE);
App.getDB().getDrawFactory().assignDrawableTo(this, titleIconView, mEntry.getIcon(), true, iconColor);
App.getDB().getDrawFactory().assignDatabaseIconTo(this, titleIconView, mEntry.getIcon(), true, iconColor);
} else {
App.getDB().getDrawFactory().assignDrawableTo(this, titleIconView, mEntry.getIcon());
App.getDB().getDrawFactory().assignDatabaseIconTo(this, titleIconView, mEntry.getIcon());
}
// Assign title text
@@ -21,6 +21,8 @@ package com.kunzisoft.keepass.activities;
import android.app.Activity;
import android.content.Intent;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.support.v7.widget.Toolbar;
@@ -46,7 +48,6 @@ import com.kunzisoft.keepass.database.PwDate;
import com.kunzisoft.keepass.database.PwEntry;
import com.kunzisoft.keepass.database.PwGroup;
import com.kunzisoft.keepass.database.PwGroupId;
import com.kunzisoft.keepass.database.PwIconStandard;
import com.kunzisoft.keepass.database.edit.AddEntry;
import com.kunzisoft.keepass.database.edit.OnFinish;
import com.kunzisoft.keepass.database.edit.RunnableOnFinish;
@@ -168,7 +169,15 @@ public class EntryEditActivity extends LockingHideActivity
mEntry = PwEntry.getInstance(parent);
mIsNew = true;
// Add the default icon
entryIconView.setImageResource(IconPackChooser.getSelectedIconPack(this).getDefaultIconId());
if (IconPackChooser.getSelectedIconPack(this).tintable()) {
// Retrieve the textColor to tint the icon
int[] attrs = {R.attr.textColorInverse};
TypedArray ta = getTheme().obtainStyledAttributes(attrs);
int iconColor = ta.getColor(0, Color.WHITE);
App.getDB().getDrawFactory().assignDefaultDatabaseIconTo(this, entryIconView, true, iconColor);
} else {
App.getDB().getDrawFactory().assignDefaultDatabaseIconTo(this, entryIconView);
}
} else {
UUID uuid = Types.bytestoUUID(uuidBytes);
mEntry = pm.getEntryByUUIDId(uuid);
@@ -382,8 +391,7 @@ public class EntryEditActivity extends LockingHideActivity
newEntry.setTitle(entryTitleView.getText().toString());
if(mSelectedIconID != -1)
// or TODO icon factory newEntry.setIcon(App.getDB().pm.iconFactory.getIcon(mSelectedIconID));
newEntry.setIcon(new PwIconStandard(mSelectedIconID));
newEntry.setIcon(App.getDB().getPwDatabase().getIconFactory().getIcon(mSelectedIconID));
else {
if (mIsNew) {
newEntry.setIcon(App.getDB().getPwDatabase().getIconFactory().getFirstIcon());
@@ -440,7 +448,16 @@ public class EntryEditActivity extends LockingHideActivity
}
protected void fillData() {
App.getDB().getDrawFactory().assignDrawableTo(this, entryIconView, mEntry.getIcon());
if (IconPackChooser.getSelectedIconPack(this).tintable()) {
// Retrieve the textColor to tint the icon
int[] attrs = {R.attr.textColorInverse};
TypedArray ta = getTheme().obtainStyledAttributes(attrs);
int iconColor = ta.getColor(0, Color.WHITE);
App.getDB().getDrawFactory().assignDatabaseIconTo(this, entryIconView, mEntry.getIcon(), true, iconColor);
} else {
App.getDB().getDrawFactory().assignDatabaseIconTo(this, entryIconView, mEntry.getIcon());
}
// Don't start the field reference manager, we want to see the raw ref
mEntry.endToManageFieldReferences();
@@ -75,6 +75,7 @@ public class GroupActivity extends ListNodesActivity
private Toolbar toolbar;
private ImageView iconView;
private AddNodeButtonView addNodeButtonView;
protected boolean addGroupEnabled = false;
@@ -143,6 +144,7 @@ public class GroupActivity extends ListNodesActivity
// Construct main view
setContentView(getLayoutInflater().inflate(R.layout.list_nodes_with_add_button, null));
iconView = findViewById(R.id.icon);
addNodeButtonView = findViewById(R.id.add_node_button);
addNodeButtonView.enableAddGroup(addGroupEnabled);
addNodeButtonView.enableAddEntry(addEntryEnabled);
@@ -413,16 +415,14 @@ public class GroupActivity extends ListNodesActivity
*/
protected void assignGroupIcon() {
if (mCurrentGroup != null) {
ImageView iv = findViewById(R.id.icon);
if (IconPackChooser.getSelectedIconPack(this).tintable()) {
// Retrieve the textColor to tint the icon
int[] attrs = {R.attr.textColorInverse};
TypedArray ta = getTheme().obtainStyledAttributes(attrs);
int iconColor = ta.getColor(0, Color.WHITE);
App.getDB().getDrawFactory().assignDrawableTo(this, iv, mCurrentGroup.getIcon(), true, iconColor);
App.getDB().getDrawFactory().assignDatabaseIconTo(this, iconView, mCurrentGroup.getIcon(), true, iconColor);
} else {
App.getDB().getDrawFactory().assignDrawableTo(this, iv, mCurrentGroup.getIcon());
App.getDB().getDrawFactory().assignDatabaseIconTo(this, iconView, mCurrentGroup.getIcon());
}
}
}
@@ -198,9 +198,9 @@ public class NodeAdapter extends RecyclerView.Adapter<BasicViewHolder> {
iconColor = iconEntryColor;
break;
}
App.getDB().getDrawFactory().assignDrawableTo(context, holder.icon, subNode.getIcon(), true, iconColor);
App.getDB().getDrawFactory().assignDatabaseIconTo(context, holder.icon, subNode.getIcon(), true, iconColor);
} else {
App.getDB().getDrawFactory().assignDrawableTo(context, holder.icon, subNode.getIcon());
App.getDB().getDrawFactory().assignDatabaseIconTo(context, holder.icon, subNode.getIcon());
}
// Assign text
holder.text.setText(subNode.getDisplayTitle());
@@ -41,13 +41,12 @@ import org.apache.commons.collections.map.AbstractReferenceMap;
import org.apache.commons.collections.map.ReferenceMap;
/**
* Factory class who build database icons dynamically, can assign an icon of IconPack, or a custom icon to an ImageView with tint
* Factory class who build database icons dynamically, can assign an icon of IconPack, or a custom icon to an ImageView with a tint
*/
public class IconDrawableFactory {
private static Drawable blank = null;
private static int blankWidth = -1;
private static int blankHeight = -1;
private boolean iconStandardToTint = false;
/** customIconMap
* Cache for icon drawable.
@@ -61,41 +60,115 @@ public class IconDrawableFactory {
*/
private ReferenceMap standardIconMap = new ReferenceMap(AbstractReferenceMap.HARD, AbstractReferenceMap.WEAK);
public void assignDrawableTo(Context context, ImageView iv, PwIcon icon) {
assignDrawableTo(context, iv, icon, false, -1);
}
public void assignDrawableTo(Context context, ImageView iv, PwIcon icon, boolean tint, int tintColor) {
Drawable draw = getIconDrawable(context, icon, tint);
if (iv != null && draw != null) {
iv.setImageDrawable(draw);
if (iconStandardToTint && tint) {
ImageViewCompat.setImageTintList(iv, ColorStateList.valueOf(tintColor));
}
}
iconStandardToTint = false;
/**
* Assign a default database icon to an ImageView
*
* @param context Context to build the drawable
* @param iv ImageView that will host the drawable
*/
public void assignDefaultDatabaseIconTo(Context context, ImageView iv) {
assignDefaultDatabaseIconTo(context, iv, false, Color.WHITE);
}
public Drawable getIconDrawable(Context context, PwIcon icon) {
return getIconDrawable(context, icon, false);
/**
* Assign a default database icon to an ImageView and tint it
*
* @param context Context to build the drawable
* @param iv ImageView that will host the drawable
*/
public void assignDefaultDatabaseIconTo(Context context, ImageView iv, boolean tint, int tintColor) {
assignDrawableTo(context, iv, IconPackChooser.getSelectedIconPack(context).getDefaultIconId(), tint, tintColor);
}
/**
* Assign a database icon to an ImageView
*
* @param context Context to build the drawable
* @param iv ImageView that will host the drawable
* @param icon The icon from the database
*/
public void assignDatabaseIconTo(Context context, ImageView iv, PwIcon icon) {
assignDatabaseIconTo(context, iv, icon, false, Color.WHITE);
}
public Drawable getIconDrawable(Context context, PwIcon icon, boolean tint) {
if (icon instanceof PwIconStandard) {
Drawable sharedDrawable = getIconDrawable(context.getApplicationContext(), (PwIconStandard) icon);
/**
* Assign a database icon to an ImageView and tint it
*
* @param context Context to build the drawable
* @param imageView ImageView that will host the drawable
* @param icon The icon from the database
* @param tint true will tint the drawable with tintColor
* @param tintColor Use this color if tint is true
*/
public void assignDatabaseIconTo(Context context, ImageView imageView, PwIcon icon, boolean tint, int tintColor) {
assignDrawableToImageView(getIconDrawable(context, icon, tint, tintColor),
imageView,
tint,
tintColor);
}
/**
* Assign an image by its resourceId to an ImageView and tint it
*
* @param context Context to build the drawable
* @param imageView ImageView that will host the drawable
* @param iconId iconId from the resources
* @param tint true will tint the drawable with tintColor
* @param tintColor Use this color if tint is true
*/
public void assignDrawableTo(Context context, ImageView imageView, int iconId, boolean tint, int tintColor) {
assignDrawableToImageView(getIconDrawable(context, iconId, tint, tintColor),
imageView,
tint,
tintColor);
}
/**
* Utility method to assign a drawable to an ImageView and tint it
*/
private void assignDrawableToImageView(Drawable drawable, ImageView imageView, boolean tint, int tintColor) {
if (imageView != null && drawable != null) {
imageView.setImageDrawable(drawable);
if (tint) {
iconStandardToTint = true;
assert sharedDrawable.getConstantState() != null;
return sharedDrawable.getConstantState().newDrawable();
// TODO Optimisation for each tint
} else
return sharedDrawable;
ImageViewCompat.setImageTintList(imageView, ColorStateList.valueOf(tintColor));
} else {
ImageViewCompat.setImageTintList(imageView, null);
}
}
}
/**
* Get the drawable icon from cache or build it and add it to the cache if not exists yet
*
* @param context Context to build the drawable
* @param icon The icon from database
* @return The build drawable
*/
public Drawable getIconDrawable(Context context, PwIcon icon) {
return getIconDrawable(context, icon, false, Color.WHITE);
}
/**
* Get the drawable icon from cache or build it and add it to the cache if not exists yet then tint it if needed
*
* @param context Context to build the drawable
* @param icon The icon from database
* @param tint true will tint the drawable with tintColor
* @param tintColor Use this color if tint is true
* @return The build drawable
*/
public Drawable getIconDrawable(Context context, PwIcon icon, boolean tint, int tintColor) {
if (icon instanceof PwIconStandard) {
return getIconDrawable(context.getApplicationContext(), (PwIconStandard) icon, tint, tintColor);
} else {
iconStandardToTint = false;
return getIconDrawable(context, (PwIconCustom) icon);
}
}
/**
* Build a blank drawable
* @param res Resource to build the drawable
*/
private static void initBlank(Resources res) {
if (blank==null) {
blankWidth = (int) res.getDimension(R.dimen.icon_size);
@@ -104,20 +177,80 @@ public class IconDrawableFactory {
blank.setBounds(0, 0, blankWidth, blankHeight);
}
}
private Drawable getIconDrawable(Context context, PwIconStandard icon) {
int resId = IconPackChooser.getSelectedIconPack(context).iconToResId(icon.iconId);
Drawable draw = (Drawable) standardIconMap.get(resId);
if (draw == null) {
draw = ContextCompat.getDrawable(context, resId);
if (draw != null)
standardIconMap.put(resId, draw);
}
return draw;
/**
* Key class to retrieve a Drawable in the cache if it's tinted or not
*/
private class CacheKey {
int resId;
boolean isTint;
int color;
CacheKey(int resId, boolean isTint, int color) {
this.resId = resId;
this.isTint = isTint;
this.color = color;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
CacheKey cacheKey = (CacheKey) o;
if (isTint)
return resId == cacheKey.resId &&
cacheKey.isTint &&
color == cacheKey.color;
else
return resId == cacheKey.resId &&
!cacheKey.isTint;
}
}
/**
* Get the drawable icon from cache or build it and add it to the cache if not exists yet
*
* @param context Context to make drawable
* @param icon Icon from database
* @param isTint Tint the drawable if true
* @param tintColor Use this color if tint is true
* @return The drawable
*/
private Drawable getIconDrawable(Context context, PwIconStandard icon, boolean isTint, int tintColor) {
int resId = IconPackChooser.getSelectedIconPack(context).iconToResId(icon.iconId);
return getIconDrawable(context, resId, isTint, tintColor);
}
/**
* Get the drawable icon from cache or build it and add it to the cache if not exists yet
*
* @param context Context to make drawable
* @param iconId iconId from resources
* @param isTint Tint the drawable if true
* @param tintColor Use this color if tint is true
* @return The drawable
*/
private Drawable getIconDrawable(Context context, int iconId, boolean isTint, int tintColor) {
CacheKey newCacheKey = new CacheKey(iconId, isTint, tintColor);
Drawable draw = (Drawable) standardIconMap.get(newCacheKey);
if (draw == null) {
draw = ContextCompat.getDrawable(context, iconId);
if (draw != null) {
standardIconMap.put(newCacheKey, draw);
}
}
return draw;
}
/**
* Build a custom icon from database
* @param context Context to build the drawable
* @param icon Icon from database
* @return The drawable
*/
private Drawable getIconDrawable(Context context, PwIconCustom icon) {
initBlank(context.getResources());
if (icon == null) {
@@ -147,10 +280,11 @@ public class IconDrawableFactory {
return draw;
}
/** Resize the custom icon to match the built in icons
/**
* Resize the custom icon to match the built in icons
*
* @param bitmap
* @return
* @param bitmap Bitmap to resize
* @return Bitmap resized
*/
private Bitmap resize(Bitmap bitmap) {
int width = bitmap.getWidth();
@@ -24,6 +24,7 @@ import android.util.Log;
import com.kunzisoft.keepass.R;
import com.kunzisoft.keepass.app.App;
import com.kunzisoft.keepass.settings.PreferencesUtil;
import java.util.ArrayList;
import java.util.List;
@@ -36,7 +37,7 @@ public class IconPackChooser {
private static final String TAG = IconPackChooser.class.getName();
private static List<IconPack> iconPackList = new ArrayList<>();
private static IconPack iconPackSelected;
private static IconPack iconPackSelected = null;
private static IconPackChooser sIconPackBuilder;
@@ -71,7 +72,6 @@ public class IconPackChooser {
Log.e(TAG, "Icon packs can't be load, retry with one by default");
addDefaultIconPack(context);
}
iconPackSelected = getDefaultIconPack(context);
}
}
}
@@ -108,17 +108,6 @@ public class IconPackChooser {
}
}
/**
* Get the default IconPack
*
* @param context Context to build the icon pack if not already build
* @return default IconPack
*/
public static IconPack getDefaultIconPack(Context context) {
build(context);
return getIconPackList().get(0);
}
/**
* Get the current IconPack used
*
@@ -128,7 +117,7 @@ public class IconPackChooser {
public static IconPack getSelectedIconPack(Context context) {
build(context);
if (iconPackSelected == null)
return getDefaultIconPack(context);
setSelectedIconPack(PreferencesUtil.getIconPackSelectedId(context));
return iconPackSelected;
}
@@ -22,6 +22,7 @@ package com.kunzisoft.keepass.settings;
import android.content.Context;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;
import android.support.v4.content.ContextCompat;
import com.kunzisoft.keepass.R;
import com.kunzisoft.keepass.database.SortNodeEnum;
@@ -145,6 +146,13 @@ public class PreferencesUtil {
ctx.getResources().getBoolean(R.bool.allow_copy_password_default));
}
public static String getIconPackSelectedId(Context context) {
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
return prefs.getString(
context.getString(R.string.setting_icon_pack_choose_key),
context.getString(R.string.setting_icon_pack_choose_default));
}
/**
* All preference keys associated with education
*/