539: fix for #533, fix for #510 + LoyaltyCardViewActivity crashing on returning from day/night theme change r=TheLastProject a=Kethen

please review, fixes #533, and avoids a crash from loading LoyaltyCardViewActivity, switch out to settings and switch day/night mode, switch back, which fixes #510 as well

Co-authored-by: Katharine <kwchuiaa@connect.ust.hk>
This commit is contained in:
bors[bot]
2021-10-27 10:55:51 +00:00
committed by GitHub
4 changed files with 46 additions and 22 deletions

View File

@@ -48,7 +48,7 @@ public class LoyaltyCardCursorAdapter extends BaseCursorAdapter<LoyaltyCardCurso
mSelectedItems = new SparseBooleanArray();
mAnimationItemsIndex = new SparseBooleanArray();
mDarkModeEnabled = MainActivity.isDarkModeEnabled(inputContext);
mDarkModeEnabled = Utils.isDarkModeEnabled(inputContext);
swapCursor(mCursor);
}

View File

@@ -190,11 +190,15 @@ public class LoyaltyCardViewActivity extends CatimaAppCompatActivity implements
Log.d(TAG, "View activity: id=" + loyaltyCardId);
}
private Drawable getDotIcon(boolean active) {
private Drawable getDotIcon(boolean active, boolean darkMode) {
Drawable unwrappedIcon = AppCompatResources.getDrawable(this, active ? R.drawable.active_dot : R.drawable.inactive_dot);
assert unwrappedIcon != null;
Drawable wrappedIcon = DrawableCompat.wrap(unwrappedIcon);
DrawableCompat.setTint(wrappedIcon, ContextCompat.getColor(getApplicationContext(), R.color.iconColor));
if (darkMode){
DrawableCompat.setTint(wrappedIcon, Color.WHITE);
}else{
DrawableCompat.setTint(wrappedIcon, Color.BLACK);
}
return wrappedIcon;
}
@@ -212,6 +216,17 @@ public class LoyaltyCardViewActivity extends CatimaAppCompatActivity implements
return wrappedIcon;
}
private void setCenterGuideline(int zoomLevel) {
float scale = zoomLevel / 100f;
if (format != null && format.isSquare()) {
centerGuideline.setGuidelinePercent(0.75f * scale);
} else {
centerGuideline.setGuidelinePercent(0.5f * scale);
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
@@ -254,21 +269,19 @@ public class LoyaltyCardViewActivity extends CatimaAppCompatActivity implements
barcodeScaler.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
if (!fromUser){
Log.d(TAG, "non user triggered onProgressChanged, ignoring, progress is " + progress);
return;
}
Log.d(TAG, "Progress is " + progress);
Log.d(TAG, "Max is " + barcodeScaler.getMax());
float scale = (float) progress / (float) barcodeScaler.getMax();
Log.d(TAG, "Scaling to " + scale);
if(isFullscreen){
loyaltyCard.zoomLevel = progress;
db.updateLoyaltyCardZoomLevel(loyaltyCardId, loyaltyCard.zoomLevel);
}
loyaltyCard.zoomLevel = progress;
db.updateLoyaltyCardZoomLevel(loyaltyCardId, loyaltyCard.zoomLevel);
if (format != null && format.isSquare()) {
centerGuideline.setGuidelinePercent(0.75f * scale);
} else {
centerGuideline.setGuidelinePercent(0.5f * scale);
}
setCenterGuideline(loyaltyCard.zoomLevel);
drawMainImage(mainImageIndex, true);
}
@@ -545,10 +558,11 @@ public class LoyaltyCardViewActivity extends CatimaAppCompatActivity implements
dotIndicator.removeAllViews();
if (imageTypes.size() >= 2) {
dots = new ImageView[imageTypes.size()];
boolean darkMode = Utils.isDarkModeEnabled(getApplicationContext());
for (int i = 0; i < imageTypes.size(); i++) {
dots[i] = new ImageView(this);
dots[i].setImageDrawable(getDotIcon(false));
dots[i].setImageDrawable(getDotIcon(false, darkMode));
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
params.setMargins(8, 0, 8, 0);
@@ -736,8 +750,9 @@ public class LoyaltyCardViewActivity extends CatimaAppCompatActivity implements
}
if (dots != null) {
boolean darkMode = Utils.isDarkModeEnabled(getApplicationContext());
for (int i = 0; i < dots.length; i++) {
dots[i].setImageDrawable(getDotIcon(i == index));
dots[i].setImageDrawable(getDotIcon(i == index, darkMode));
}
}
@@ -795,6 +810,7 @@ public class LoyaltyCardViewActivity extends CatimaAppCompatActivity implements
drawMainImage(mainImageIndex, true);
barcodeScaler.setProgress(loyaltyCard.zoomLevel);
setCenterGuideline(loyaltyCard.zoomLevel);
// Hide maximize and show minimize button and scaler
maximizeButton.setVisibility(View.GONE);
@@ -831,7 +847,7 @@ public class LoyaltyCardViewActivity extends CatimaAppCompatActivity implements
Log.d(TAG, "Move out of fullscreen");
// Reset center guideline
barcodeScaler.setProgress(100);
setCenterGuideline(100);
drawMainImage(mainImageIndex, true);

View File

@@ -602,13 +602,6 @@ public class MainActivity extends CatimaAppCompatActivity implements LoyaltyCard
updateLoyaltyCardList();
}
protected static boolean isDarkModeEnabled(Context inputContext)
{
Configuration config = inputContext.getResources().getConfiguration();
int currentNightMode = config.uiMode & Configuration.UI_MODE_NIGHT_MASK;
return (currentNightMode == Configuration.UI_MODE_NIGHT_YES);
}
@Override
public boolean onDown(MotionEvent e) {
return false;

View File

@@ -38,6 +38,7 @@ import java.util.GregorianCalendar;
import java.util.Locale;
import java.util.Map;
import androidx.appcompat.app.AppCompatDelegate;
import androidx.core.graphics.ColorUtils;
import androidx.exifinterface.media.ExifInterface;
import protect.card_locker.preferences.Settings;
@@ -401,4 +402,18 @@ public class Utils {
static public long getUnixTime() {
return System.currentTimeMillis() / 1000;
}
static public boolean isDarkModeEnabled(Context inputContext)
{
int nightModeSetting = new Settings(inputContext).getTheme();
if (nightModeSetting == AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM) {
Configuration config = inputContext.getResources().getConfiguration();
int currentNightMode = config.uiMode & Configuration.UI_MODE_NIGHT_MASK;
return (currentNightMode == Configuration.UI_MODE_NIGHT_YES);
}else if (nightModeSetting == AppCompatDelegate.MODE_NIGHT_YES){
return true;
}else{
return false;
}
}
}