From 91db03bc473e92623128a6fe67ee16e711851941 Mon Sep 17 00:00:00 2001 From: RichyHBM Date: Sun, 26 Nov 2017 11:50:44 +0000 Subject: [PATCH] Remove bitmap from entry class --- .../andotp/Activities/MainActivity.java | 8 --- .../flocke/andotp/Database/Entry.java | 5 -- .../andotp/Utilities/DatabaseHelper.java | 4 -- .../andotp/Utilities/EntryThumbnail.java | 10 +--- .../andotp/View/EntriesCardAdapter.java | 50 +++++++++++-------- .../flocke/andotp/View/EntryViewHolder.java | 7 ++- .../View/ThumbnailSelectionAdapter.java | 47 ++++++++++------- app/src/main/res/drawable/ic_github.xml | 11 ++-- app/src/main/res/layout/component_card.xml | 2 +- .../layout/component_thumbnail_selection.xml | 18 ------- 10 files changed, 74 insertions(+), 88 deletions(-) delete mode 100644 app/src/main/res/layout/component_thumbnail_selection.xml diff --git a/app/src/main/java/org/shadowice/flocke/andotp/Activities/MainActivity.java b/app/src/main/java/org/shadowice/flocke/andotp/Activities/MainActivity.java index 69a0bdc8..08bcc610 100644 --- a/app/src/main/java/org/shadowice/flocke/andotp/Activities/MainActivity.java +++ b/app/src/main/java/org/shadowice/flocke/andotp/Activities/MainActivity.java @@ -209,10 +209,6 @@ public class MainActivity extends BaseActivity int digits = Integer.parseInt(digitsInput.getText().toString()); Entry e = new Entry(type, secret, period, digits, label, algorithm, tagsAdapter.getActiveTags()); - - int size = getResources().getDimensionPixelSize(R.dimen.card_thumbnail_size); - e.setThumbnailImage(EntryThumbnail.getThumbnailGraphic(MainActivity.this, label, size, e.getThumbnail())); - e.updateOTP(); adapter.addEntry(e); adapter.saveEntries(); @@ -459,10 +455,6 @@ public class MainActivity extends BaseActivity if(result.getContents() != null) { try { Entry e = new Entry(result.getContents()); - - int size = getResources().getDimensionPixelSize(R.dimen.card_thumbnail_size); - e.setThumbnailImage(EntryThumbnail.getThumbnailGraphic(MainActivity.this, e.getLabel(), size, e.getThumbnail())); - e.updateOTP(); adapter.addEntry(e); adapter.saveEntries(); 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 0585c97b..e9cea5bc 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 @@ -66,7 +66,6 @@ public class Entry { private long last_update = 0; public List tags = new ArrayList<>(); private EntryThumbnail.EntryThumbnails thumbnail = EntryThumbnail.EntryThumbnails.Default; - private Bitmap thumbnailImage; public Entry(){} @@ -239,10 +238,6 @@ public class Entry { public void setTags(List tags) { this.tags = tags; } - public Bitmap getThumbnailImage() { return thumbnailImage; } - - public void setThumbnailImage(Bitmap thumbnailImage) { this.thumbnailImage = thumbnailImage; } - public EntryThumbnail.EntryThumbnails getThumbnail() { return thumbnail; } public void setThumbnail( EntryThumbnail.EntryThumbnails value) { thumbnail = value; } diff --git a/app/src/main/java/org/shadowice/flocke/andotp/Utilities/DatabaseHelper.java b/app/src/main/java/org/shadowice/flocke/andotp/Utilities/DatabaseHelper.java index f6913a6e..9d14c2c4 100644 --- a/app/src/main/java/org/shadowice/flocke/andotp/Utilities/DatabaseHelper.java +++ b/app/src/main/java/org/shadowice/flocke/andotp/Utilities/DatabaseHelper.java @@ -98,10 +98,6 @@ public class DatabaseHelper { for (int i = 0; i < json.length(); i++) { Entry entry = new Entry(json.getJSONObject(i)); - - int size = context.getResources().getDimensionPixelSize(R.dimen.card_thumbnail_size); - entry.setThumbnailImage(EntryThumbnail.getThumbnailGraphic(context, entry.getLabel(), size, entry.getThumbnail())); - entries.add(entry); } } catch (Exception error) { 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 7853bc25..b650d88a 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 @@ -5,15 +5,9 @@ import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.graphics.Canvas; import android.graphics.drawable.Drawable; -import android.graphics.drawable.PictureDrawable; - -import com.larvalabs.svgandroid.SVG; -import com.larvalabs.svgandroid.SVGBuilder; import org.shadowice.flocke.andotp.R; -import java.io.IOException; - public class EntryThumbnail { public enum EntryThumbnails { Default(R.mipmap.ic_launcher_round), @@ -38,9 +32,9 @@ public class EntryThumbnail { try { Drawable drawable = context.getResources().getDrawable(thumbnail.getResource()); - Bitmap bitmap = Bitmap.createBitmap(size, size, Bitmap.Config.ARGB_8888); + Bitmap bitmap = Bitmap.createBitmap(drawable.getMinimumWidth(), drawable.getMinimumHeight(), Bitmap.Config.ARGB_8888); Canvas canvas = new Canvas(bitmap); - drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight()); + drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight()); drawable.draw(canvas); return bitmap; } catch(Exception e) { 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 5fa9bed2..6abdc6cc 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 @@ -37,12 +37,14 @@ import android.view.MenuInflater; import android.view.MenuItem; import android.view.View; import android.view.ViewGroup; +import android.widget.AbsListView; import android.widget.AdapterView; import android.widget.ArrayAdapter; import android.widget.EditText; import android.widget.Filter; import android.widget.Filterable; import android.widget.FrameLayout; +import android.widget.GridView; import android.widget.LinearLayout; import android.widget.ListView; import android.widget.TextView; @@ -160,7 +162,7 @@ public class EntriesCardAdapter extends RecyclerView.Adapter public void onBindViewHolder(EntryViewHolder entryViewHolder, int i) { Entry entry = displayedEntries.get(i); - entryViewHolder.updateValues(entry.getLabel(), entry.getCurrentOTP(), entry.getTags(), entry.getThumbnailImage()); + entryViewHolder.updateValues(entry.getLabel(), entry.getCurrentOTP(), entry.getTags(), entry.getThumbnail()); if (entry.hasNonDefaultPeriod()) { entryViewHolder.showCustomPeriod(entry.getPeriod()); @@ -261,7 +263,6 @@ public class EntriesCardAdapter extends RecyclerView.Adapter Entry e = entries.get(realIndex); e.setLabel(newLabel); - e.setThumbnailImage(EntryThumbnail.getThumbnailGraphic(context, e.getLabel(), size, e.getThumbnail())); DatabaseHelper.saveDatabase(context, entries); } @@ -280,42 +281,49 @@ 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); - ListView list = new ListView(context); - list.setAdapter(new ThumbnailSelectionAdapter(context)); - list.setDividerHeight(0); + GridView grid = new GridView(context); + grid.setAdapter(new ThumbnailSelectionAdapter(context)); + grid.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT)); + 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)); + grid.setStretchMode(GridView.STRETCH_COLUMN_WIDTH); - list.setOnItemClickListener(new AdapterView.OnItemClickListener() { + FrameLayout container = new FrameLayout(context); + container.setPaddingRelative(marginMedium, marginSmall, marginMedium, 0); + container.addView(grid); + + final AlertDialog alert = builder.setTitle(R.string.menu_popup_change_image) + .setView(container) + .setNegativeButton(android.R.string.cancel, new DialogInterface.OnClickListener() { + @Override + public void onClick(DialogInterface dialogInterface, int i) {} + }) + .create(); + + grid.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView parent, View view, int position, long id) { int realIndex = getRealIndex(pos); - TextView v = (TextView) view.findViewById(R.id.thumbnail_selection_text); EntryThumbnail.EntryThumbnails thumbnail = EntryThumbnail.EntryThumbnails.Default; try { - thumbnail = EntryThumbnail.EntryThumbnails.valueOf(v.getText().toString()); + thumbnail = EntryThumbnail.EntryThumbnails.values()[position]; } catch (Exception e) { e.printStackTrace(); } int size = context.getResources().getDimensionPixelSize(R.dimen.card_thumbnail_size); Entry e = entries.get(realIndex); - e.setThumbnailImage(EntryThumbnail.getThumbnailGraphic(context, e.getLabel(), size, thumbnail)); + e.setThumbnail(thumbnail); + DatabaseHelper.saveDatabase(context, entries); notifyItemChanged(pos); + alert.cancel(); } }); - FrameLayout container = new FrameLayout(context); - container.setPaddingRelative(marginMedium, marginSmall, marginMedium, 0); - container.addView(list); - - builder.setTitle(R.string.menu_popup_change_image) - .setView(container) - .setNegativeButton(android.R.string.cancel, new DialogInterface.OnClickListener() { - @Override - public void onClick(DialogInterface dialogInterface, int i) {} - }) - .create() - .show(); + alert.show(); } 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 356966f4..40977a34 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 @@ -36,6 +36,7 @@ import android.widget.LinearLayout; import android.widget.TextView; import org.shadowice.flocke.andotp.R; +import org.shadowice.flocke.andotp.Utilities.EntryThumbnail; import org.shadowice.flocke.andotp.Utilities.Settings; import org.shadowice.flocke.andotp.Utilities.Tools; import org.shadowice.flocke.andotp.View.ItemTouchHelper.ItemTouchHelperViewHolder; @@ -106,7 +107,7 @@ public class EntryViewHolder extends RecyclerView.ViewHolder }); } - public void updateValues(String label, String token, List tags, Bitmap thumbnail) { + public void updateValues(String label, String token, List tags, EntryThumbnail.EntryThumbnails thumbnail) { Settings settings = new Settings(context); this.label.setText(label); @@ -128,7 +129,9 @@ public class EntryViewHolder extends RecyclerView.ViewHolder } thumbnailImg.setVisibility(settings.getThumbnailVisible() ? View.VISIBLE : View.GONE); - thumbnailImg.setImageBitmap(thumbnail); + + int size = context.getResources().getDimensionPixelSize(R.dimen.card_thumbnail_size); + thumbnailImg.setImageBitmap(EntryThumbnail.getThumbnailGraphic(context, label, size, thumbnail)); } public void showCustomPeriod(int period) { 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 2c975a8b..06bb304c 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 @@ -6,6 +6,8 @@ import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.ArrayAdapter; +import android.widget.BaseAdapter; +import android.widget.GridView; import android.widget.ImageView; import android.widget.TextView; @@ -14,37 +16,46 @@ import org.shadowice.flocke.andotp.Utilities.EntryThumbnail; import java.util.ArrayList; -public class ThumbnailSelectionAdapter extends ArrayAdapter { +public class ThumbnailSelectionAdapter extends BaseAdapter { private Context context; - public ThumbnailSelectionAdapter(Context context) { - super(context, R.layout.component_thumbnail_selection, new ArrayList()); + ThumbnailSelectionAdapter(Context context) { this.context = context; + } - for (EntryThumbnail.EntryThumbnails thumb : EntryThumbnail.EntryThumbnails.values()) { - add(thumb); - } + @Override + public int getCount() { + return EntryThumbnail.EntryThumbnails.values().length; + } + + @Override + public Object getItem(int i) { + if(i >= EntryThumbnail.EntryThumbnails.values().length) + return EntryThumbnail.EntryThumbnails.Default; + else + return EntryThumbnail.EntryThumbnails.values()[i]; + } + + @Override + public long getItemId(int i) { + return EntryThumbnail.EntryThumbnails.values()[i].ordinal(); } @NonNull @Override public View getView(int i, View view, @NonNull ViewGroup viewGroup) { - View newView; + int size = context.getResources().getDimensionPixelSize(R.dimen.card_thumbnail_size); + ImageView imageView; if (view == null) { - newView = LayoutInflater.from(context).inflate(R.layout.component_thumbnail_selection , viewGroup, false); - } else{ - newView = view; + imageView = new ImageView(context); + imageView.setLayoutParams(new GridView.LayoutParams(size, size)); + } else { + imageView = (ImageView) view; } - ImageView imageView = (ImageView) newView.findViewById(R.id.thumbnail_selection_icon); - TextView textView = (TextView) newView.findViewById(R.id.thumbnail_selection_text); + EntryThumbnail.EntryThumbnails thumb = (EntryThumbnail.EntryThumbnails)getItem(i); - EntryThumbnail.EntryThumbnails thumb = getItem(i); - - int size = context.getResources().getDimensionPixelSize(R.dimen.card_thumbnail_size); imageView.setImageBitmap(EntryThumbnail.getThumbnailGraphic(context, thumb.name(), size, thumb)); - textView.setText(thumb.name()); - - return newView; + return imageView; } } diff --git a/app/src/main/res/drawable/ic_github.xml b/app/src/main/res/drawable/ic_github.xml index c409fa1f..3b739907 100644 --- a/app/src/main/res/drawable/ic_github.xml +++ b/app/src/main/res/drawable/ic_github.xml @@ -1,4 +1,9 @@ - - + + diff --git a/app/src/main/res/layout/component_card.xml b/app/src/main/res/layout/component_card.xml index ee9891dc..7cb8e33e 100644 --- a/app/src/main/res/layout/component_card.xml +++ b/app/src/main/res/layout/component_card.xml @@ -17,7 +17,7 @@ android:id="@+id/thumbnailImg" android:layout_width="@dimen/card_thumbnail_size" android:layout_height="@dimen/card_thumbnail_size" - android:layout_marginEnd="@dimen/activity_margin_small" + android:layout_marginEnd="@dimen/activity_margin" android:src="@mipmap/ic_launcher"/> - - - - - - - -