diff --git a/app/src/main/java/org/shadowice/flocke/andotp/Database/Entry.java b/app/src/main/java/org/shadowice/flocke/andotp/Database/Entry.java index e9cea5bc..16b4f613 100644 --- a/app/src/main/java/org/shadowice/flocke/andotp/Database/Entry.java +++ b/app/src/main/java/org/shadowice/flocke/andotp/Database/Entry.java @@ -142,19 +142,19 @@ public class Entry { try { this.digits = jsonObj.getInt(JSON_DIGITS); - } catch(JSONException e) { + } catch(Exception e) { this.digits = TokenCalculator.TOTP_DEFAULT_DIGITS; } try { this.type = OTPType.valueOf(jsonObj.getString(JSON_TYPE)); - } catch(JSONException e) { + } catch(Exception e) { this.type = DEFAULT_TYPE; } try { this.algorithm = TokenCalculator.HashAlgorithm.valueOf(jsonObj.getString(JSON_ALGORITHM)); - } catch (JSONException e) { + } catch (Exception e) { this.algorithm = TokenCalculator.DEFAULT_ALGORITHM; } @@ -164,13 +164,13 @@ public class Entry { for(int i = 0; i < tagsArray.length(); i++) { this.tags.add(tagsArray.getString(i)); } - } catch (JSONException e) { + } catch (Exception e) { e.printStackTrace(); } try { - this.thumbnail = EntryThumbnail.EntryThumbnails.values()[jsonObj.getInt(JSON_THUMBNAIL)]; - } catch(JSONException e) { + this.thumbnail = EntryThumbnail.EntryThumbnails.valueOf(jsonObj.getString(JSON_THUMBNAIL)); + } catch(Exception e) { this.thumbnail = EntryThumbnail.EntryThumbnails.Default; } } @@ -183,7 +183,7 @@ public class Entry { jsonObj.put(JSON_DIGITS, getDigits()); jsonObj.put(JSON_TYPE, getType().toString()); jsonObj.put(JSON_ALGORITHM, algorithm.toString()); - jsonObj.put(JSON_THUMBNAIL, getThumbnail().ordinal()); + jsonObj.put(JSON_THUMBNAIL, getThumbnail().name()); JSONArray tagsArray = new JSONArray(); for(String tag : tags){ diff --git a/app/src/main/java/org/shadowice/flocke/andotp/Utilities/EntryThumbnail.java b/app/src/main/java/org/shadowice/flocke/andotp/Utilities/EntryThumbnail.java index b650d88a..c7ba0965 100644 --- a/app/src/main/java/org/shadowice/flocke/andotp/Utilities/EntryThumbnail.java +++ b/app/src/main/java/org/shadowice/flocke/andotp/Utilities/EntryThumbnail.java @@ -9,19 +9,34 @@ import android.graphics.drawable.Drawable; import org.shadowice.flocke.andotp.R; public class EntryThumbnail { + private enum AssetType { + Bitmap, + Vector + } + public enum EntryThumbnails { Default(R.mipmap.ic_launcher_round), Github(R.drawable.ic_github); private int resource; + private AssetType assetType; EntryThumbnails(int resource) { this.resource = resource; + this.assetType = AssetType.Vector; + } + + EntryThumbnails(int resource, AssetType assetType) { + this.resource = resource; + this.assetType = assetType; } public int getResource() { return resource; } + public AssetType getAssetType() { + return assetType; + } } public static Bitmap getThumbnailGraphic(Context context, String label, int size, EntryThumbnails thumbnail) { @@ -31,13 +46,17 @@ public class EntryThumbnail { } try { - Drawable drawable = context.getResources().getDrawable(thumbnail.getResource()); - Bitmap bitmap = Bitmap.createBitmap(drawable.getMinimumWidth(), drawable.getMinimumHeight(), Bitmap.Config.ARGB_8888); - Canvas canvas = new Canvas(bitmap); - drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight()); - drawable.draw(canvas); - return bitmap; - } catch(Exception e) { + if(thumbnail.getAssetType() == AssetType.Vector) { + Drawable drawable = context.getResources().getDrawable(thumbnail.getResource()); + Bitmap bitmap = Bitmap.createBitmap(drawable.getMinimumWidth(), drawable.getMinimumHeight(), Bitmap.Config.ARGB_8888); + Canvas canvas = new Canvas(bitmap); + drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight()); + drawable.draw(canvas); + return bitmap; + } else { + return BitmapFactory.decodeResource(context.getResources(), thumbnail.getResource()); + } + } catch (Exception e) { e.printStackTrace(); } diff --git a/app/src/main/java/org/shadowice/flocke/andotp/View/EntriesCardAdapter.java b/app/src/main/java/org/shadowice/flocke/andotp/View/EntriesCardAdapter.java index b894e9c7..e5def43c 100644 --- a/app/src/main/java/org/shadowice/flocke/andotp/View/EntriesCardAdapter.java +++ b/app/src/main/java/org/shadowice/flocke/andotp/View/EntriesCardAdapter.java @@ -284,7 +284,8 @@ public class EntriesCardAdapter extends RecyclerView.Adapter int marginSmall = context.getResources().getDimensionPixelSize(R.dimen.activity_margin_small); int marginMedium = context.getResources().getDimensionPixelSize(R.dimen.activity_margin_medium); - final ThumbnailSelectionAdapter thumbnailAdapter = new ThumbnailSelectionAdapter(context); + int realIndex = getRealIndex(pos); + final ThumbnailSelectionAdapter thumbnailAdapter = new ThumbnailSelectionAdapter(context, entries.get(realIndex).getLabel()); final EditText input = new EditText(context); input.setLayoutParams(new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT)); @@ -306,7 +307,7 @@ public class EntriesCardAdapter extends RecyclerView.Adapter GridView grid = new GridView(context); grid.setAdapter(thumbnailAdapter); grid.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT)); - grid.setColumnWidth(context.getResources().getDimensionPixelSize(R.dimen.card_thumbnail_size_small)); + grid.setColumnWidth(context.getResources().getDimensionPixelSize(R.dimen.card_thumbnail_size)); grid.setNumColumns(GridView.AUTO_FIT); grid.setVerticalSpacing(context.getResources().getDimensionPixelSize(R.dimen.activity_margin_medium)); grid.setHorizontalSpacing(context.getResources().getDimensionPixelSize(R.dimen.activity_margin_medium)); diff --git a/app/src/main/java/org/shadowice/flocke/andotp/View/EntryViewHolder.java b/app/src/main/java/org/shadowice/flocke/andotp/View/EntryViewHolder.java index 40977a34..ed83913c 100644 --- a/app/src/main/java/org/shadowice/flocke/andotp/View/EntryViewHolder.java +++ b/app/src/main/java/org/shadowice/flocke/andotp/View/EntryViewHolder.java @@ -148,6 +148,12 @@ public class EntryViewHolder extends RecyclerView.ViewHolder tags.setTextSize(TypedValue.COMPLEX_UNIT_PT, size - 2); } + public void setThumbnailSize(int size) { + thumbnailImg.getLayoutParams().height = size; + thumbnailImg.getLayoutParams().width = size; + thumbnailImg.requestLayout(); + } + public void setLabelScroll(boolean active) { if (active) { label.setEllipsize(TextUtils.TruncateAt.MARQUEE); diff --git a/app/src/main/java/org/shadowice/flocke/andotp/View/ThumbnailSelectionAdapter.java b/app/src/main/java/org/shadowice/flocke/andotp/View/ThumbnailSelectionAdapter.java index 75f8fc0a..7dc8ee15 100644 --- a/app/src/main/java/org/shadowice/flocke/andotp/View/ThumbnailSelectionAdapter.java +++ b/app/src/main/java/org/shadowice/flocke/andotp/View/ThumbnailSelectionAdapter.java @@ -21,11 +21,12 @@ import java.util.List; public class ThumbnailSelectionAdapter extends BaseAdapter { private Context context; private List items; + private String label = "Example"; - ThumbnailSelectionAdapter(Context context) { + ThumbnailSelectionAdapter(Context context, String label) { items = new ArrayList(EntryThumbnail.EntryThumbnails.values().length); Collections.addAll(items, EntryThumbnail.EntryThumbnails.values()); - + this.label = label; this.context = context; } @@ -60,7 +61,7 @@ public class ThumbnailSelectionAdapter extends BaseAdapter { @NonNull @Override public View getView(int i, View view, @NonNull ViewGroup viewGroup) { - int size = context.getResources().getDimensionPixelSize(R.dimen.card_thumbnail_size_small); + int size = context.getResources().getDimensionPixelSize(R.dimen.card_thumbnail_size); ImageView imageView; if (view == null) { imageView = new ImageView(context); @@ -71,7 +72,7 @@ public class ThumbnailSelectionAdapter extends BaseAdapter { EntryThumbnail.EntryThumbnails thumb = (EntryThumbnail.EntryThumbnails)getItem(i); - imageView.setImageBitmap(EntryThumbnail.getThumbnailGraphic(context, thumb.name(), size, thumb)); + imageView.setImageBitmap(EntryThumbnail.getThumbnailGraphic(context, label, size, thumb)); return imageView; } }