mirror of
https://github.com/CatimaLoyalty/Android.git
synced 2026-04-13 20:07:36 -04:00
126 lines
4.5 KiB
Java
126 lines
4.5 KiB
Java
package protect.card_locker;
|
|
|
|
import android.app.Activity;
|
|
import android.content.Context;
|
|
import android.content.Intent;
|
|
import android.graphics.Color;
|
|
import android.util.Log;
|
|
|
|
import java.math.BigDecimal;
|
|
import java.text.NumberFormat;
|
|
import java.text.ParseException;
|
|
import java.util.Calendar;
|
|
import java.util.Currency;
|
|
import java.util.Date;
|
|
import java.util.GregorianCalendar;
|
|
import java.util.Locale;
|
|
|
|
import androidx.core.graphics.ColorUtils;
|
|
|
|
public class Utils {
|
|
private static final String TAG = "Catima";
|
|
|
|
// Activity request codes
|
|
public static final int MAIN_REQUEST = 1;
|
|
public static final int SELECT_BARCODE_REQUEST = 2;
|
|
public static final int BARCODE_SCAN = 3;
|
|
|
|
static final double LUMINANCE_MIDPOINT = 0.5;
|
|
|
|
static public LetterBitmap generateIcon(Context context, String store, Integer backgroundColor) {
|
|
if (store.length() == 0) {
|
|
return null;
|
|
}
|
|
|
|
int tileLetterFontSize = context.getResources().getDimensionPixelSize(R.dimen.tileLetterFontSize);
|
|
int pixelSize = context.getResources().getDimensionPixelSize(R.dimen.cardThumbnailSize);
|
|
|
|
if (backgroundColor == null) {
|
|
backgroundColor = LetterBitmap.getDefaultColor(context, store);
|
|
}
|
|
|
|
return new LetterBitmap(context, store, store,
|
|
tileLetterFontSize, pixelSize, pixelSize, backgroundColor, needsDarkForeground(backgroundColor) ? Color.BLACK : Color.WHITE);
|
|
}
|
|
|
|
static public boolean needsDarkForeground(Integer backgroundColor) {
|
|
return ColorUtils.calculateLuminance(backgroundColor) > LUMINANCE_MIDPOINT;
|
|
}
|
|
|
|
static public BarcodeValues parseSetBarcodeActivityResult(int requestCode, int resultCode, Intent intent) {
|
|
String contents = null;
|
|
String format = null;
|
|
|
|
if (resultCode == Activity.RESULT_OK)
|
|
{
|
|
if (requestCode == Utils.BARCODE_SCAN) {
|
|
Log.i(TAG, "Received barcode information from camera");
|
|
} else if (requestCode == Utils.SELECT_BARCODE_REQUEST) {
|
|
Log.i(TAG, "Received barcode information from typing it");
|
|
} else {
|
|
return new BarcodeValues(null, null);
|
|
}
|
|
|
|
contents = intent.getStringExtra(BarcodeSelectorActivity.BARCODE_CONTENTS);
|
|
format = intent.getStringExtra(BarcodeSelectorActivity.BARCODE_FORMAT);
|
|
}
|
|
|
|
Log.i(TAG, "Read barcode id: " + contents);
|
|
Log.i(TAG, "Read format: " + format);
|
|
|
|
return new BarcodeValues(format, contents);
|
|
}
|
|
|
|
static public Boolean hasExpired(Date expiryDate) {
|
|
// today
|
|
Calendar date = new GregorianCalendar();
|
|
// reset hour, minutes, seconds and millis
|
|
date.set(Calendar.HOUR_OF_DAY, 0);
|
|
date.set(Calendar.MINUTE, 0);
|
|
date.set(Calendar.SECOND, 0);
|
|
date.set(Calendar.MILLISECOND, 0);
|
|
|
|
return expiryDate.before(date.getTime());
|
|
}
|
|
|
|
static public String formatBalance(Context context, BigDecimal value, Currency currency) {
|
|
NumberFormat numberFormat = NumberFormat.getInstance();
|
|
|
|
if (currency == null) {
|
|
numberFormat.setMaximumFractionDigits(0);
|
|
return context.getString(R.string.balancePoints, numberFormat.format(value));
|
|
}
|
|
|
|
NumberFormat currencyFormat = NumberFormat.getCurrencyInstance();
|
|
currencyFormat.setCurrency(currency);
|
|
|
|
return currencyFormat.format(value);
|
|
}
|
|
|
|
static public String formatBalanceWithoutCurrencySymbol(BigDecimal value, Currency currency) {
|
|
NumberFormat numberFormat = NumberFormat.getInstance();
|
|
|
|
if (currency == null) {
|
|
numberFormat.setMaximumFractionDigits(0);
|
|
return numberFormat.format(value);
|
|
}
|
|
|
|
numberFormat.setMinimumFractionDigits(currency.getDefaultFractionDigits());
|
|
numberFormat.setMaximumFractionDigits(currency.getDefaultFractionDigits());
|
|
|
|
return numberFormat.format(value);
|
|
}
|
|
|
|
static public BigDecimal parseCurrencyInUserLocale(String value) throws ParseException, NumberFormatException {
|
|
// BigDecimal only likes to parse in US locale
|
|
// So we have to translate whatever the input was to US locale
|
|
NumberFormat numberInputFormat = NumberFormat.getNumberInstance();
|
|
NumberFormat numberToBigDecimalFormat = NumberFormat.getNumberInstance(Locale.US);
|
|
|
|
// BigDecimal won't understand values like 1,000 instead of 1000
|
|
numberToBigDecimalFormat.setGroupingUsed(false);
|
|
|
|
return new BigDecimal(numberToBigDecimalFormat.format(numberInputFormat.parse(value)));
|
|
}
|
|
}
|