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

Closes #1092 - Fixed contents cut off on smaller screen issue. #1095

Merged
merged 7 commits into from
Oct 25, 2022
Merged
Show file tree
Hide file tree
Changes from 3 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
54 changes: 32 additions & 22 deletions app/src/main/java/protect/card_locker/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.util.Log;
import android.util.TypedValue;
import android.view.GestureDetector;
import android.view.LayoutInflater;
import android.view.Menu;
Expand All @@ -29,6 +31,7 @@
import androidx.appcompat.view.ActionMode;
import androidx.appcompat.widget.SearchView;
import androidx.core.splashscreen.SplashScreen;
import androidx.core.widget.TextViewCompat;
import androidx.recyclerview.widget.RecyclerView;

import com.google.android.material.dialog.MaterialAlertDialogBuilder;
Expand All @@ -55,6 +58,8 @@ public class MainActivity extends CatimaAppCompatActivity implements LoyaltyCard
private static final String TAG = "Catima";
public static final String RESTART_ACTIVITY_INTENT = "restart_activity_intent";

private static final int MEDIUM_SCALE_FACTOR_DIP = 460;

private SQLiteDatabase mDatabase;
private LoyaltyCardCursorAdapter mAdapter;
private ActionMode mCurrentActionMode;
Expand Down Expand Up @@ -178,37 +183,33 @@ public boolean onActionItemClicked(ActionMode inputMode, MenuItem inputItem) {
dialog.show();

return true;
}
else if(inputItem.getItemId() == R.id.action_archive){
} else if (inputItem.getItemId() == R.id.action_archive) {
for (LoyaltyCard loyaltyCard : mAdapter.getSelectedItems()) {
Log.d(TAG, "Archiving card: " + loyaltyCard.id);
DBHelper.updateLoyaltyCardArchiveStatus(mDatabase, loyaltyCard.id,1);
DBHelper.updateLoyaltyCardArchiveStatus(mDatabase, loyaltyCard.id, 1);
updateLoyaltyCardList(false);
inputMode.finish();
invalidateOptionsMenu();
}
return true;
}
else if(inputItem.getItemId() == R.id.action_unarchive){
} else if (inputItem.getItemId() == R.id.action_unarchive) {
for (LoyaltyCard loyaltyCard : mAdapter.getSelectedItems()) {
Log.d(TAG, "Unarchiving card: " + loyaltyCard.id);
DBHelper.updateLoyaltyCardArchiveStatus(mDatabase, loyaltyCard.id,0);
DBHelper.updateLoyaltyCardArchiveStatus(mDatabase, loyaltyCard.id, 0);
updateLoyaltyCardList(false);
inputMode.finish();
invalidateOptionsMenu();
}
return true;
}
else if(inputItem.getItemId() == R.id.action_star){
} else if (inputItem.getItemId() == R.id.action_star) {
for (LoyaltyCard loyaltyCard : mAdapter.getSelectedItems()) {
Log.d(TAG, "Starring card: " + loyaltyCard.id);
DBHelper.updateLoyaltyCardStarStatus(mDatabase, loyaltyCard.id, 1);
updateLoyaltyCardList(false);
inputMode.finish();
}
return true;
}
else if(inputItem.getItemId() == R.id.action_unstar){
} else if (inputItem.getItemId() == R.id.action_unstar) {
for (LoyaltyCard loyaltyCard : mAdapter.getSelectedItems()) {
Log.d(TAG, "Unstarring card: " + loyaltyCard.id);
DBHelper.updateLoyaltyCardStarStatus(mDatabase, loyaltyCard.id, 0);
Expand All @@ -233,15 +234,14 @@ protected void onCreate(Bundle inputSavedInstanceState) {
extractIntentFields(getIntent());
SplashScreen.installSplashScreen(this);
super.onCreate(inputSavedInstanceState);
if(!mArchiveMode) {
if (!mArchiveMode) {
binding = MainActivityBinding.inflate(getLayoutInflater());
setTitle(R.string.app_name);
setContentView(binding.getRoot());
setSupportActionBar(binding.toolbar);
groupsTabLayout = binding.groups;
contentMainBinding = ContentMainBinding.bind(binding.include.getRoot());
}
else{
} else {
archiveActivityBinding = ArchiveActivityBinding.inflate(getLayoutInflater());
setTitle(R.string.archiveList);
setContentView(archiveActivityBinding.getRoot());
Expand All @@ -250,7 +250,7 @@ protected void onCreate(Bundle inputSavedInstanceState) {
contentMainBinding = ContentMainBinding.bind(archiveActivityBinding.include.getRoot());
}

if(mArchiveMode){
if (mArchiveMode) {
ActionBar actionBar = getSupportActionBar();
if (actionBar != null) {
actionBar.setDisplayHomeAsUpEnabled(true);
Expand Down Expand Up @@ -302,7 +302,6 @@ public void onTabReselected(TabLayout.Tab tab) {
mAdapter = new LoyaltyCardCursorAdapter(this, null, this);
mCardList.setAdapter(mAdapter);
registerForContextMenu(mCardList);

mGroup = null;
updateLoyaltyCardList(true);

Expand Down Expand Up @@ -404,7 +403,9 @@ protected void onResume() {
groupsTabLayout.selectTab(tab);
assert tab != null;
mGroup = tab.getTag();
}
} else
scaleScreen();

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For single if statements I kinda get the no-brackets style (although I personally wouldn't use it), but I'd really prefer brackets here. For me personally, it is a lot more readable.

updateLoyaltyCardList(true);
// End of active tab logic

Expand Down Expand Up @@ -592,9 +593,9 @@ public void updateTabGroups(TabLayout groupsTabLayout) {

@Override
public boolean onCreateOptionsMenu(Menu inputMenu) {
if(!mArchiveMode)
if (!mArchiveMode)
getMenuInflater().inflate(R.menu.main_menu, inputMenu);
else{
else {
getMenuInflater().inflate(R.menu.archive_menu, inputMenu);
}

Expand Down Expand Up @@ -632,7 +633,7 @@ public boolean onQueryTextChange(String newText) {
});
}

if(!mArchiveMode) {
if (!mArchiveMode) {
if (DBHelper.getArchivedCardsCount(mDatabase) == 0) {
inputMenu.findItem(R.id.action_archived).setVisible(false);
} else {
Expand Down Expand Up @@ -846,6 +847,16 @@ private void enableActionMode(int inputPosition) {
toggleSelection(inputPosition);
}

private void scaleScreen() {
DisplayMetrics displayMetrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
int screenHeight = displayMetrics.heightPixels;
float mediumSizePx = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,MEDIUM_SCALE_FACTOR_DIP,getResources().getDisplayMetrics());
boolean shouldScaleSmaller = screenHeight < mediumSizePx;

binding.include.welcomeIcon.setVisibility(shouldScaleSmaller ? View.GONE : View.VISIBLE);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Didn't notice this before but it seems this crashes the archive option (you can confirm this by putting 1 card in archive and then clicking the menu in the top right and choosing "Archive"):

E/AndroidRuntime: FATAL EXCEPTION: main
    Process: me.hackerchick.catima.debug, PID: 19126
    java.lang.RuntimeException: Unable to resume activity {me.hackerchick.catima.debug/protect.card_locker.MainActivity}: java.lang.NullPointerException: Attempt to read from field 'protect.card_locker.databinding.ContentMainBinding protect.card_locker.databinding.MainActivityBinding.include' on a null object reference
        at android.app.ActivityThread.performResumeActivity(ActivityThread.java:4824)
        at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:4857)
        at android.app.servertransaction.ResumeActivityItem.execute(ResumeActivityItem.java:54)
        at android.app.servertransaction.ActivityTransactionItem.execute(ActivityTransactionItem.java:45)
        at android.app.servertransaction.TransactionExecutor.executeLifecycleState(TransactionExecutor.java:176)
        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:97)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2253)
        at android.os.Handler.dispatchMessage(Handler.java:106)
        at android.os.Looper.loopOnce(Looper.java:201)
        at android.os.Looper.loop(Looper.java:288)
        at android.app.ActivityThread.main(ActivityThread.java:7870)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:548)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1003)
     Caused by: java.lang.NullPointerException: Attempt to read from field 'protect.card_locker.databinding.ContentMainBinding protect.card_locker.databinding.MainActivityBinding.include' on a null object reference
        at protect.card_locker.MainActivity.scaleScreen(MainActivity.java:857)
        at protect.card_locker.MainActivity.onResume(MainActivity.java:407)
        at android.app.Instrumentation.callActivityOnResume(Instrumentation.java:1488)
        at android.app.Activity.performResume(Activity.java:8197)
        at android.app.ActivityThread.performResumeActivity(ActivityThread.java:4814)
        at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:4857) 
        at android.app.servertransaction.ResumeActivityItem.execute(ResumeActivityItem.java:54) 
        at android.app.servertransaction.ActivityTransactionItem.execute(ActivityTransactionItem.java:45) 
        at android.app.servertransaction.TransactionExecutor.executeLifecycleState(TransactionExecutor.java:176) 
        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:97) 
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2253) 
        at android.os.Handler.dispatchMessage(Handler.java:106) 
        at android.os.Looper.loopOnce(Looper.java:201) 
        at android.os.Looper.loop(Looper.java:288) 
        at android.app.ActivityThread.main(ActivityThread.java:7870) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:548) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1003) 

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ooops.. I didn't think about this scenario. It's now fixed in commit b0008cd

}

private void toggleSelection(int inputPosition) {
mAdapter.toggleSelection(inputPosition);
int count = mAdapter.getSelectedItemCount();
Expand All @@ -864,11 +875,10 @@ private void toggleSelection(int inputPosition) {
boolean hasStarred = false;
boolean hasUnstarred = false;

if(!mArchiveMode) {
if (!mArchiveMode) {
unarchiveItem.setVisible(false);
archiveItem.setVisible(true);
}
else{
} else {
unarchiveItem.setVisible(true);
archiveItem.setVisible(false);
}
Expand Down
56 changes: 52 additions & 4 deletions app/src/main/java/protect/card_locker/ScanActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@
import android.content.ActivityNotFoundException;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.res.Configuration;
import android.graphics.Color;
import android.net.Uri;
import android.os.Bundle;
import android.provider.Settings;
import android.util.DisplayMetrics;
import android.util.Log;
import android.util.TypedValue;
import android.view.KeyEvent;
Expand Down Expand Up @@ -49,13 +51,19 @@ public class ScanActivity extends CatimaAppCompatActivity {
private CustomBarcodeScannerBinding customBarcodeScannerBinding;
private static final String TAG = "Catima";

private static final int MEDIUM_SCALE_FACTOR_DIP = 460;
private static final int COMPAT_SCALE_FACTOR_DIP = 320;

private CaptureManager capture;
private DecoratedBarcodeView barcodeScannerView;

private String cardId;
private String addGroup;
private boolean torch = false;

private int buttonDefaultMinHeight;
private int buttonDefaultMaxHeight;

private ActivityResultLauncher<Intent> manualAddLauncher;
// can't use the pre-made contract because that launches the file manager for image type instead of gallery
private ActivityResultLauncher<Intent> photoPickerLauncher;
Expand Down Expand Up @@ -98,7 +106,7 @@ protected void onCreate(Bundle savedInstanceState) {
captureIntentBundle.putBoolean(Intents.Scan.BEEP_ENABLED, false);
captureIntent.putExtras(captureIntentBundle);
capture.initializeFromIntent(captureIntent, savedInstanceState);

saveDefaultUIValues();
barcodeScannerView.decodeSingle(new BarcodeCallback() {
@Override
public void barcodeResult(BarcodeResult result) {
Expand Down Expand Up @@ -127,6 +135,7 @@ protected void onResume() {
capture.onResume();
if (ContextCompat.checkSelfPermission(this, Manifest.permission.CAMERA) == PackageManager.PERMISSION_GRANTED)
showCameraPermissionMissingText(false);
scaleScreen();
}

@Override
Expand Down Expand Up @@ -163,6 +172,7 @@ public boolean onCreateOptionsMenu(Menu menu) {
return super.onCreateOptionsMenu(menu);
}


@Override
public boolean onOptionsItemSelected(MenuItem item) {
if (item.getItemId() == android.R.id.home) {
Expand Down Expand Up @@ -236,10 +246,48 @@ public void addFromImage(View view) {
private void showCameraPermissionMissingText(boolean show) {
customBarcodeScannerBinding.cameraPermissionDeniedLayout.cameraPermissionDeniedClickableArea.setOnClickListener(show ? v -> {
navigateToSystemPermissionSetting();
} : null);
customBarcodeScannerBinding.background.setBackgroundColor(show ? obtainThemeAttribute(R.attr.colorSurface) : Color.TRANSPARENT);
customBarcodeScannerBinding.cameraPermissionDeniedLayout.getRoot().setVisibility(show ? View.VISIBLE : View.GONE);
} : null);
customBarcodeScannerBinding.cardInputContainer.setBackgroundColor(show ? obtainThemeAttribute(R.attr.colorSurface) : Color.TRANSPARENT);
customBarcodeScannerBinding.cameraPermissionDeniedLayout.getRoot().setVisibility(show ? View.VISIBLE : View.GONE);

}

private void scaleScreen() {
DisplayMetrics displayMetrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
int screenHeight = displayMetrics.heightPixels;
int compatPadding = getResources().getDimensionPixelSize(R.dimen.no_data_compat_padding);
int normalPadding = getResources().getDimensionPixelSize(R.dimen.no_data_padding);
//Converting the scaling factor dps to pixels.
float mediumSizePx = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,MEDIUM_SCALE_FACTOR_DIP,getResources().getDisplayMetrics());
float compatSizePx = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,COMPAT_SCALE_FACTOR_DIP,getResources().getDisplayMetrics());
boolean shouldScaleSmaller = screenHeight < compatSizePx;
boolean shouldScaleMedium = screenHeight < mediumSizePx && !shouldScaleSmaller;
if (shouldScaleMedium) {
customBarcodeScannerBinding.cameraPermissionDeniedLayout.cameraPermissionDeniedIcon.setVisibility(View.GONE);
} else {
int buttonMinHeight = getResources().getDimensionPixelSize(R.dimen.scan_button_min_height);
customBarcodeScannerBinding.cameraPermissionDeniedLayout.cameraPermissionDeniedTitle.setVisibility(shouldScaleSmaller ? View.GONE : View.VISIBLE);
customBarcodeScannerBinding.cameraPermissionDeniedLayout.cameraPermissionDeniedIcon.setVisibility(shouldScaleSmaller ? View.GONE : View.VISIBLE);
customBarcodeScannerBinding.cameraPermissionDeniedLayout.cameraPermissionDeniedMessage.setTextSize(TypedValue.COMPLEX_UNIT_PX, getResources().getDimensionPixelSize(shouldScaleSmaller ? R.dimen.no_data_min_textSize : R.dimen.no_data_max_textSize));
customBarcodeScannerBinding.cameraPermissionDeniedLayout.cameraPermissionDeniedMessage.setPadding(shouldScaleSmaller?compatPadding:normalPadding,shouldScaleSmaller?compatPadding:normalPadding,shouldScaleSmaller?compatPadding:normalPadding,shouldScaleSmaller?compatPadding:normalPadding);
customBarcodeScannerBinding.addFromImage.setTextSize(TypedValue.COMPLEX_UNIT_PX, getResources().getDimensionPixelSize(shouldScaleSmaller ? R.dimen.scan_button_min_textSize : R.dimen.scan_button_max_textSize));
customBarcodeScannerBinding.addManually.setTextSize(TypedValue.COMPLEX_UNIT_PX, getResources().getDimensionPixelSize(shouldScaleSmaller ? R.dimen.scan_button_min_textSize : R.dimen.scan_button_max_textSize));
customBarcodeScannerBinding.addFromImage.setMinimumHeight(shouldScaleSmaller ? getResources().getDimensionPixelSize(R.dimen.scan_button_min_height) : buttonDefaultMinHeight);
customBarcodeScannerBinding.addFromImage.setMinimumWidth(shouldScaleSmaller ? buttonMinHeight : buttonDefaultMinHeight);
customBarcodeScannerBinding.addManually.setMinimumHeight(shouldScaleSmaller ? buttonMinHeight : buttonDefaultMinHeight);
customBarcodeScannerBinding.addManually.setMinimumWidth(shouldScaleSmaller ? buttonMinHeight : buttonDefaultMinHeight);
customBarcodeScannerBinding.addFromImage.setMinHeight(shouldScaleSmaller ? buttonMinHeight : buttonDefaultMinHeight);
customBarcodeScannerBinding.addFromImage.setMinWidth(shouldScaleSmaller ? buttonMinHeight : buttonDefaultMinHeight);
customBarcodeScannerBinding.addManually.setMinHeight(shouldScaleSmaller ? buttonMinHeight : buttonDefaultMinHeight);
customBarcodeScannerBinding.addManually.setMinWidth(shouldScaleSmaller ? buttonMinHeight : buttonDefaultMinHeight);
}

}

private void saveDefaultUIValues() {
buttonDefaultMinHeight = customBarcodeScannerBinding.addFromImage.getMinHeight();
buttonDefaultMaxHeight = customBarcodeScannerBinding.addFromImage.getMinWidth();
}
liutng marked this conversation as resolved.
Show resolved Hide resolved

private int obtainThemeAttribute(int attribute) {
Expand Down
3 changes: 3 additions & 0 deletions app/src/main/res/layout/camera_permission_failed_layout.xml
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,22 @@
android:orientation="vertical">

<ImageView
android:id="@+id/camera_permission_denied_icon"
android:layout_width="match_parent"
android:layout_height="84dp"
android:scaleType="fitCenter"
android:src="@drawable/camera_permission_denied" />

<TextView
android:id="@+id/camera_permission_denied_title"
style="@style/TextAppearance.Material3.HeadlineLarge"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="@string/cameraPermissionDeniedTitle" />

<TextView
android:id="@+id/camera_permission_denied_message"
style="@style/AppTheme.TextView.NoData"
android:layout_width="match_parent"
android:layout_height="wrap_content"
Expand Down
Loading