Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow fallback to front or back image for thumbnail if icon image is not set #1930

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
import android.graphics.Bitmap;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.os.Handler;
import android.os.Looper;
import android.util.SparseBooleanArray;
import android.util.TypedValue;
import android.view.HapticFeedbackConstants;
Expand Down Expand Up @@ -88,9 +90,13 @@ public void onBindViewHolder(LoyaltyCardListItemViewHolder inputHolder, Cursor i
inputHolder.mDivider.setVisibility(View.GONE);

LoyaltyCard loyaltyCard = LoyaltyCard.toLoyaltyCard(inputCursor);
Bitmap icon = Utils.retrieveCardImage(mContext, loyaltyCard.id, ImageLocationType.icon);
inputHolder.mCardIcon.setContentDescription(loyaltyCard.store);

// Default header at first, real icon will be retrieved later
Utils.setIconOrTextWithBackground(mContext, loyaltyCard, null, inputHolder.mCardIcon, inputHolder.mCardText);

if (mLoyaltyCardListDisplayOptions.showingNameBelowThumbnail() && icon != null) {
ImageLocationType thumbnailImageLocationType = Utils.getThumbnailImageLocationType(mContext, loyaltyCard.id);
if (mLoyaltyCardListDisplayOptions.showingNameBelowThumbnail() && thumbnailImageLocationType != null) {
showDivider = true;
inputHolder.setStoreField(loyaltyCard.store);
} else {
Expand Down Expand Up @@ -122,15 +128,28 @@ public void onBindViewHolder(LoyaltyCardListItemViewHolder inputHolder, Cursor i
inputHolder.setExtraField(inputHolder.mExpiryField, null, null, false);
}

inputHolder.mCardIcon.setContentDescription(loyaltyCard.store);
inputHolder.mIconBackgroundColor = Utils.setIconOrTextWithBackground(mContext, loyaltyCard, icon, inputHolder.mCardIcon, inputHolder.mCardText);

inputHolder.toggleCardStateIcon(loyaltyCard.starStatus != 0, loyaltyCard.archiveStatus != 0, itemSelected(inputCursor.getPosition()));

inputHolder.itemView.setActivated(mSelectedItems.get(inputCursor.getPosition(), false));
applyIconAnimation(inputHolder, inputCursor.getPosition());
applyClickEvents(inputHolder, inputCursor.getPosition());

if (thumbnailImageLocationType != null) {
new Thread() {
@Override
public void run() {
Bitmap icon = Utils.retrieveCardImage(mContext, loyaltyCard.id, thumbnailImageLocationType);

new Handler(Looper.getMainLooper()).post(new Runnable() {
public void run() {
inputHolder.mIconBackgroundColor = Utils.setIconOrTextWithBackground(mContext, loyaltyCard, icon, inputHolder.mCardIcon, inputHolder.mCardText);
inputHolder.toggleCardStateIcon(loyaltyCard.starStatus != 0, loyaltyCard.archiveStatus != 0, itemSelected(inputCursor.getPosition()));
}
});
}
}.start();
}

// Force redraw to fix size not shrinking after data change
inputHolder.mRow.requestLayout();
}
Expand Down
24 changes: 18 additions & 6 deletions app/src/main/java/protect/card_locker/LoyaltyCardViewActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import android.text.style.ForegroundColorSpan;
import android.text.util.Linkify;
import android.util.Log;
import android.util.Pair;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
Expand Down Expand Up @@ -85,6 +86,9 @@ public class LoyaltyCardViewActivity extends CatimaAppCompatActivity implements
String barcodeIdString;
CatimaBarcode format;

Bitmap iconBitmap;
ImageLocationType iconBitmapLocationType;

Bitmap frontImageBitmap;
Bitmap backImageBitmap;

Expand Down Expand Up @@ -302,10 +306,16 @@ public void onStopTrackingTouch(SeekBar seekBar) {
binding.bottomAppBarUpdateBalanceButton.setOnClickListener(view -> showBalanceUpdateDialog());

binding.iconContainer.setOnClickListener(view -> {
if (Utils.retrieveCardImage(this, loyaltyCard.id, ImageLocationType.icon) != null) {
if (iconBitmapLocationType == null) {
Toast.makeText(LoyaltyCardViewActivity.this, R.string.icon_header_click_text, Toast.LENGTH_LONG).show();
} else if (iconBitmapLocationType == ImageLocationType.icon) {
openImageInGallery(ImageType.ICON);
} else if (iconBitmapLocationType == ImageLocationType.front) {
openImageInGallery(ImageType.IMAGE_FRONT);
} else if (iconBitmapLocationType == ImageLocationType.back) {
openImageInGallery(ImageType.IMAGE_BACK);
} else {
Toast.makeText(LoyaltyCardViewActivity.this, R.string.icon_header_click_text, Toast.LENGTH_LONG).show();
throw new IllegalArgumentException("Unknown image type: " + iconBitmapLocationType);
}
});
binding.iconContainer.setOnLongClickListener(view -> {
Expand Down Expand Up @@ -674,7 +684,12 @@ protected void onResume() {
dialog.show();
});

int backgroundHeaderColor = Utils.getHeaderColor(this, loyaltyCard);
Pair<Bitmap, ImageLocationType> bitmapImageLocationTypePair = Utils.getThumbnailWithFallback(this, loyaltyCard.id);
iconBitmap = bitmapImageLocationTypePair.first;
iconBitmapLocationType = bitmapImageLocationTypePair.second;
Utils.setIconOrTextWithBackground(this, loyaltyCard, iconBitmap, binding.iconImage, binding.iconText);

int backgroundHeaderColor = Utils.getHeaderColorFromImage(iconBitmap, Utils.getHeaderColor(this, loyaltyCard));

// Also apply colours to UI elements
int darkenedColor = ColorUtils.blendARGB(backgroundHeaderColor, Color.BLACK, 0.1f);
Expand All @@ -692,9 +707,6 @@ protected void onResume() {
editButtonIcon.setTint(Utils.needsDarkForeground(complementaryColor) ? Color.BLACK : Color.WHITE);
binding.fabEdit.setImageDrawable(editButtonIcon);

Bitmap icon = Utils.retrieveCardImage(this, loyaltyCard.id, ImageLocationType.icon);
Utils.setIconOrTextWithBackground(this, loyaltyCard, icon, binding.iconImage, binding.iconText);

// If the background is very bright, we should use dark icons
backgroundNeedsDarkIcons = Utils.needsDarkForeground(backgroundHeaderColor);

Expand Down
54 changes: 53 additions & 1 deletion app/src/main/java/protect/card_locker/Utils.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import android.text.Spanned;
import android.text.style.ClickableSpan;
import android.util.Log;
import android.util.Pair;
import android.util.TypedValue;
import android.view.MotionEvent;
import android.view.View;
Expand Down Expand Up @@ -619,6 +620,16 @@ public static File retrieveCardImageAsFile(Context context, int loyaltyCardId, I
return retrieveCardImageAsFile(context, getCardImageFileName(loyaltyCardId, type));
}

static public boolean hasCardImage(Context context, String fileName) {
try {
context.openFileInput(fileName);
} catch (FileNotFoundException e) {
return false;
}

return true;
}

static public Bitmap retrieveCardImage(Context context, String fileName) {
FileInputStream in;
try {
Expand All @@ -630,6 +641,10 @@ static public Bitmap retrieveCardImage(Context context, String fileName) {
return BitmapFactory.decodeStream(in);
}

static public boolean hasCardImage(Context context, int loyaltyCardId, ImageLocationType type) {
return hasCardImage(context, getCardImageFileName(loyaltyCardId, type));
}

static public Bitmap retrieveCardImage(Context context, int loyaltyCardId, ImageLocationType type) {
return retrieveCardImage(context, getCardImageFileName(loyaltyCardId, type));
}
Expand Down Expand Up @@ -934,7 +949,7 @@ public static String linkify(final String input) {
* @return background colour
*/
public static int setIconOrTextWithBackground(Context context, LoyaltyCard loyaltyCard, Bitmap icon, ImageView backgroundOrIcon, TextView textWhenNoImage) {
int headerColor = getHeaderColor(context, loyaltyCard);
int headerColor = getHeaderColorFromImage(icon, getHeaderColor(context, loyaltyCard));
backgroundOrIcon.setImageBitmap(icon);
backgroundOrIcon.setBackgroundColor(headerColor);

Expand Down Expand Up @@ -1025,4 +1040,41 @@ public static boolean deviceHasCamera(Context context) {
return false;
}
}

/**
* Check if the card has an image thumbnail and if yes return what type it is
*
* @param context Android context
* @param loyaltyCardId Id of Loyalty Card
* @return ImageLocationType The location type of the thumbnail if it exists, or null if no thumbnail
*/
public static ImageLocationType getThumbnailImageLocationType(Context context, int loyaltyCardId) {
for (ImageLocationType imageLocationType : new ImageLocationType[]{ImageLocationType.icon, ImageLocationType.front, ImageLocationType.back}) {
if (Utils.hasCardImage(context, loyaltyCardId, imageLocationType)) {
return imageLocationType;
}
}

return null;
}

/**
* Retrieve the card thumbnail, falling back to the front and back card image if no thumbnail is set
*
* @param context Android context
* @param loyaltyCardId Id of Loyalty Card
* @return Pair<Bitmap, ImageLocationType> thumbnail and imageLocationType or both null if not set
*/
public static Pair<Bitmap, ImageLocationType> getThumbnailWithFallback(Context context, int loyaltyCardId) {
Bitmap icon;
for (ImageLocationType imageLocationType : new ImageLocationType[]{ ImageLocationType.icon, ImageLocationType.front, ImageLocationType.back }) {
icon = Utils.retrieveCardImage(context, loyaltyCardId, imageLocationType);

if (icon != null) {
return new Pair<>(icon, imageLocationType);
}
}

return new Pair<>(null, null);
}
}
1 change: 0 additions & 1 deletion app/src/main/res/layout/loyalty_card_layout.xml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
android:layout_height="match_parent"
android:contentDescription="@string/thumbnailDescription"
android:scaleType="fitCenter"
android:src="@mipmap/ic_launcher"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
Expand Down
Loading