Let users select thumbnail size

This commit is contained in:
RichyHBM 2017-11-27 21:29:02 +00:00
parent df3ff7536c
commit 43ddf62529
11 changed files with 136 additions and 12 deletions

View file

@ -121,6 +121,7 @@ So make sure you have a **current backup** before switching!
* [Code Parts from Google's Android Samples](https://android.googlesource.com/platform/development/+/master/samples/Vault/src/com/example/android/vault)
* [FloatingActionMenuAndroid](https://github.com/pmahsky/FloatingActionMenuAndroid)
* [LetterBitmap](http://stackoverflow.com/questions/23122088/colored-boxed-with-letters-a-la-gmail)
* [DimensionConverter](https://stackoverflow.com/questions/8343971/how-to-parse-a-dimension-string-and-convert-it-to-a-dimension-value)
## License:
```

View file

@ -431,7 +431,8 @@ public class MainActivity extends BaseActivity
if (key.equals(getString(R.string.settings_key_label_size)) ||
key.equals(getString(R.string.settings_key_tap_to_reveal)) ||
key.equals(getString(R.string.settings_key_label_scroll)) ||
key.equals(getString(R.string.settings_key_thumbnail_visible))) {
key.equals(getString(R.string.settings_key_thumbnail_visible)) ||
key.equals(getString(R.string.settings_key_thumbnail_size))) {
adapter.notifyDataSetChanged();
} else if (key.equals(getString(R.string.settings_key_theme)) ||
key.equals(getString(R.string.settings_key_lang))) {

View file

@ -0,0 +1,81 @@
package org.shadowice.flocke.andotp.Utilities;
import android.util.DisplayMetrics;
import android.util.TypedValue;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class DimensionConverter {
// -- Initialize dimension string to constant lookup.
public static final Map<String, Integer> dimensionConstantLookup = initDimensionConstantLookup();
private static Map<String, Integer> initDimensionConstantLookup() {
Map<String, Integer> m = new HashMap<String, Integer>();
m.put("px", TypedValue.COMPLEX_UNIT_PX);
m.put("dip", TypedValue.COMPLEX_UNIT_DIP);
m.put("dp", TypedValue.COMPLEX_UNIT_DIP);
m.put("sp", TypedValue.COMPLEX_UNIT_SP);
m.put("pt", TypedValue.COMPLEX_UNIT_PT);
m.put("in", TypedValue.COMPLEX_UNIT_IN);
m.put("mm", TypedValue.COMPLEX_UNIT_MM);
return Collections.unmodifiableMap(m);
}
// -- Initialize pattern for dimension string.
private static final Pattern DIMENSION_PATTERN = Pattern.compile("^\\s*(\\d+(\\.\\d+)*)\\s*([a-zA-Z]+)\\s*$");
public static int stringToDimensionPixelSize(String dimension, DisplayMetrics metrics) {
// -- Mimics TypedValue.complexToDimensionPixelSize(int data, DisplayMetrics metrics).
InternalDimension internalDimension = stringToInternalDimension(dimension);
final float value = internalDimension.value;
final float f = TypedValue.applyDimension(internalDimension.unit, value, metrics);
final int res = (int)(f+0.5f);
if (res != 0) return res;
if (value == 0) return 0;
if (value > 0) return 1;
return -1;
}
public static float stringToDimension(String dimension, DisplayMetrics metrics) {
// -- Mimics TypedValue.complexToDimension(int data, DisplayMetrics metrics).
InternalDimension internalDimension = stringToInternalDimension(dimension);
return TypedValue.applyDimension(internalDimension.unit, internalDimension.value, metrics);
}
private static InternalDimension stringToInternalDimension(String dimension) {
// -- Match target against pattern.
Matcher matcher = DIMENSION_PATTERN.matcher(dimension);
if (matcher.matches()) {
// -- Match found.
// -- Extract value.
float value = Float.valueOf(matcher.group(1)).floatValue();
// -- Extract dimension units.
String unit = matcher.group(3).toLowerCase();
// -- Get Android dimension constant.
Integer dimensionUnit = dimensionConstantLookup.get(unit);
if (dimensionUnit == null) {
// -- Invalid format.
throw new NumberFormatException();
} else {
// -- Return valid dimension.
return new InternalDimension(value, dimensionUnit);
}
} else {
// -- Invalid format.
throw new NumberFormatException();
}
}
private static class InternalDimension {
float value;
int unit;
public InternalDimension(float value, int unit) {
this.value = value;
this.unit = unit;
}
}
}

View file

@ -27,6 +27,7 @@ import android.content.SharedPreferences;
import android.os.Environment;
import android.preference.PreferenceManager;
import android.util.Base64;
import android.util.Log;
import org.apache.commons.codec.binary.Hex;
import org.apache.commons.codec.digest.DigestUtils;
@ -335,4 +336,14 @@ public class Settings {
return getBoolean(R.string.settings_key_thumbnail_visible, true);
}
public int getThumbnailSize() {
try {
String dimen = getString(R.string.settings_key_thumbnail_size, context.getResources().getString(R.string.settings_default_thumbnail_size));
Log.d("dimen", dimen);
return DimensionConverter.stringToDimensionPixelSize(dimen, context.getResources().getDisplayMetrics());
} catch(Exception e) {
e.printStackTrace();
return context.getResources().getDimensionPixelSize(R.dimen.card_thumbnail_size);
}
}
}

View file

@ -56,6 +56,7 @@ import android.widget.Toast;
import org.shadowice.flocke.andotp.Database.Entry;
import org.shadowice.flocke.andotp.R;
import org.shadowice.flocke.andotp.Utilities.DatabaseHelper;
import org.shadowice.flocke.andotp.Utilities.DimensionConverter;
import org.shadowice.flocke.andotp.Utilities.EntryThumbnail;
import org.shadowice.flocke.andotp.Utilities.Settings;
import org.shadowice.flocke.andotp.Utilities.TagDialogHelper;
@ -182,6 +183,9 @@ public class EntriesCardAdapter extends RecyclerView.Adapter<EntryViewHolder>
int fontSize = sharedPrefs.getInt(context.getString(R.string.settings_key_label_size), context.getResources().getInteger(R.integer.settings_default_label_size));
entryViewHolder.setLabelSize(fontSize);
int thumbnailSize = settings.getThumbnailSize();
entryViewHolder.setThumbnailSize(thumbnailSize);
entryViewHolder.setLabelScroll(sharedPrefs.getBoolean(context.getString(R.string.settings_key_label_scroll), false));
}
@ -262,8 +266,6 @@ public class EntriesCardAdapter extends RecyclerView.Adapter<EntryViewHolder>
notifyItemChanged(pos);
}
int size = context.getResources().getDimensionPixelSize(R.dimen.card_thumbnail_size);
Entry e = entries.get(realIndex);
e.setLabel(newLabel);
@ -307,7 +309,9 @@ public class EntriesCardAdapter extends RecyclerView.Adapter<EntryViewHolder>
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));
int thumbnailSize = settings.getThumbnailSize();
grid.setColumnWidth(thumbnailSize);
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));
@ -342,7 +346,6 @@ public class EntriesCardAdapter extends RecyclerView.Adapter<EntryViewHolder>
e.printStackTrace();
}
int size = context.getResources().getDimensionPixelSize(R.dimen.card_thumbnail_size);
Entry e = entries.get(realIndex);
e.setThumbnail(thumbnail);

View file

@ -36,6 +36,7 @@ import android.widget.LinearLayout;
import android.widget.TextView;
import org.shadowice.flocke.andotp.R;
import org.shadowice.flocke.andotp.Utilities.DimensionConverter;
import org.shadowice.flocke.andotp.Utilities.EntryThumbnail;
import org.shadowice.flocke.andotp.Utilities.Settings;
import org.shadowice.flocke.andotp.Utilities.Tools;
@ -130,8 +131,8 @@ public class EntryViewHolder extends RecyclerView.ViewHolder
thumbnailImg.setVisibility(settings.getThumbnailVisible() ? View.VISIBLE : View.GONE);
int size = context.getResources().getDimensionPixelSize(R.dimen.card_thumbnail_size);
thumbnailImg.setImageBitmap(EntryThumbnail.getThumbnailGraphic(context, label, size, thumbnail));
int thumbnailSize = settings.getThumbnailSize();
thumbnailImg.setImageBitmap(EntryThumbnail.getThumbnailGraphic(context, label, thumbnailSize, thumbnail));
}
public void showCustomPeriod(int period) {

View file

@ -12,7 +12,9 @@ import android.widget.ImageView;
import android.widget.TextView;
import org.shadowice.flocke.andotp.R;
import org.shadowice.flocke.andotp.Utilities.DimensionConverter;
import org.shadowice.flocke.andotp.Utilities.EntryThumbnail;
import org.shadowice.flocke.andotp.Utilities.Settings;
import java.util.ArrayList;
import java.util.Collections;
@ -22,12 +24,14 @@ public class ThumbnailSelectionAdapter extends BaseAdapter {
private Context context;
private List items;
private String label = "Example";
private Settings settings;
ThumbnailSelectionAdapter(Context context, String label) {
items = new ArrayList(EntryThumbnail.EntryThumbnails.values().length);
Collections.addAll(items, EntryThumbnail.EntryThumbnails.values());
this.label = label;
this.context = context;
settings = new Settings(context);
}
void filter(String filter) {
@ -61,18 +65,18 @@ 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);
int thumbnailSize = settings.getThumbnailSize();
ImageView imageView;
if (view == null) {
imageView = new ImageView(context);
imageView.setLayoutParams(new GridView.LayoutParams(size, size));
imageView.setLayoutParams(new GridView.LayoutParams(thumbnailSize, thumbnailSize));
} else {
imageView = (ImageView) view;
}
EntryThumbnail.EntryThumbnails thumb = (EntryThumbnail.EntryThumbnails)getItem(i);
imageView.setImageBitmap(EntryThumbnail.getThumbnailGraphic(context, label, size, thumb));
imageView.setImageBitmap(EntryThumbnail.getThumbnailGraphic(context, label, thumbnailSize, thumb));
return imageView;
}
}

View file

@ -9,8 +9,7 @@
<dimen name="activity_horizontal_margin">16dp</dimen>
<dimen name="card_corner_radius">0dp</dimen>
<dimen name="card_thumbnail_size">64dp</dimen>
<dimen name="card_thumbnail_size_small">46dp</dimen>
<dimen name="card_thumbnail_size">46dp</dimen>
<!-- The default letter tile text size -->
<dimen name="tile_letter_font_size">33sp</dimen>

View file

@ -16,6 +16,7 @@
<string name="settings_key_label_size" translatable="false">pref_label_size</string>
<string name="settings_key_label_scroll" translatable="false">pref_label_scroll</string>
<string name="settings_key_thumbnail_visible" translatable="false">pref_thumbnail_visible</string>
<string name="settings_key_thumbnail_size" translatable="false">pref_thumbnail_size</string>
<string name="settings_key_backup_ask" translatable="false">pref_backup_ask</string>
<string name="settings_key_backup_directory" translatable="false">pref_backup_directory</string>
@ -38,6 +39,7 @@
<string name="settings_default_lang" translatable="false">system</string>
<string name="settings_default_theme" translatable="false">light</string>
<integer name="settings_default_label_size">8</integer>
<string name="settings_default_thumbnail_size">46dp</string>
<!-- All of the possible tile background colors -->
<array name="letter_tile_colors">
@ -83,6 +85,13 @@
<item>black</item>
</string-array>
<string-array name="settings_values_thumbnail_size" translatable="false">
<item>32dp</item>
<item>46dp</item>
<item>64dp</item>
<item>96dp</item>
</string-array>
<!-- Misc -->
<string-array name="settings_empty_array" translatable="false">
</string-array>

View file

@ -19,6 +19,7 @@
<string name="settings_title_label_size">Label font size</string>
<string name="settings_title_label_scroll">Scroll label</string>
<string name="settings_title_thumbnail_visible_ask">Show thumbnails</string>
<string name="settings_title_thumbnail_size_ask">Thumbnail size</string>
<string name="settings_title_backup_ask">Ask for filename</string>
<string name="settings_title_backup_directory">Backup directory</string>
@ -85,6 +86,13 @@
<item>Black theme</item>
</string-array>
<string-array name="settings_entries_thumbnail_size">
<item>Small</item>
<item>Default</item>
<item>Medium</item>
<item>Large</item>
</string-array>
<!-- PasswordPreference -->
<string name="settings_hint_password">Password</string>
<string name="settings_hint_pin">PIN</string>

View file

@ -71,6 +71,12 @@
android:title="@string/settings_title_thumbnail_visible_ask"
android:defaultValue="true" />
<ListPreference
android:key="@string/settings_key_thumbnail_size"
android:title="@string/settings_title_thumbnail_size_ask"
android:entries="@array/settings_entries_thumbnail_size"
android:entryValues="@array/settings_values_thumbnail_size"
android:defaultValue="@string/settings_default_thumbnail_size" />
</PreferenceCategory>