From 6ed96393d57a36275c2144baa6eea6f4427655a0 Mon Sep 17 00:00:00 2001 From: Branden Archer Date: Wed, 31 Jan 2018 22:13:39 -0500 Subject: [PATCH] Change layout of single card view This changes the layout when viewing a single card. The new layout will have a colored area at the top which displays the first letter of the company, and after that the barcode. The note will be displayed on the card list screen and is presently omitted here. As this layout is experimental, the layout for editing a card is not yet updated. To achieve this the card viewing activity is split into two separate classes, where one handles viewing and one handles editing. When the view layout finalizes the edit icon can be updated as time allows. --- app/src/main/AndroidManifest.xml | 7 + .../protect/card_locker/LetterBitmap.java | 106 ++--- .../card_locker/LoyaltyCardCursorAdapter.java | 5 +- .../card_locker/LoyaltyCardEditActivity.java | 401 ++++++++++++++++++ .../card_locker/LoyaltyCardViewActivity.java | 349 ++------------- .../protect/card_locker/MainActivity.java | 3 +- ...ity.xml => loyalty_card_edit_activity.xml} | 0 .../res/layout/loyalty_card_view_layout.xml | 93 ++++ app/src/main/res/values/dimens.xml | 4 +- app/src/main/res/values/styles.xml | 5 + .../LoyaltyCardViewActivityTest.java | 70 ++- .../protect/card_locker/MainActivityTest.java | 4 +- 12 files changed, 657 insertions(+), 390 deletions(-) create mode 100644 app/src/main/java/protect/card_locker/LoyaltyCardEditActivity.java rename app/src/main/res/layout/{loyalty_card_view_activity.xml => loyalty_card_edit_activity.xml} (100%) create mode 100644 app/src/main/res/layout/loyalty_card_view_layout.xml diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 7306a766f..264f0fe05 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -35,6 +35,13 @@ + diff --git a/app/src/main/java/protect/card_locker/LetterBitmap.java b/app/src/main/java/protect/card_locker/LetterBitmap.java index e8046c57d..a15fbbaa3 100644 --- a/app/src/main/java/protect/card_locker/LetterBitmap.java +++ b/app/src/main/java/protect/card_locker/LetterBitmap.java @@ -26,81 +26,74 @@ class LetterBitmap * The number of available tile colors */ private static final int NUM_OF_TILE_COLORS = 8; - /** - * The {@link TextPaint} used to draw the letter onto the tile + * The letter bitmap */ - private final TextPaint mPaint = new TextPaint(); + private final Bitmap mBitmap; /** - * The bounds that enclose the letter + * The background color of the letter bitmap */ - private final Rect mBounds = new Rect(); - /** - * The {@link Canvas} to draw on - */ - private final Canvas mCanvas = new Canvas(); - /** - * The first char of the name being displayed - */ - private final char[] mFirstChar = new char[1]; - - /** - * The background colors of the tile - */ - private final TypedArray mColors; - /** - * The font size used to display the letter - */ - private final int mTileLetterFontSize; - /** - * The default image to display - */ - private final Bitmap mDefaultBitmap; + private final Integer mColor; /** * Constructor for LetterTileProvider * * @param context The {@link Context} to use + * @param displayName The name used to create the letter for the tile + * @param key The key used to generate the background color for the tile + * @param tileLetterFontSize The font size used to display the letter + * @param width The desired width of the tile + * @param height The desired height of the tile */ - public LetterBitmap(Context context) + public LetterBitmap(Context context, String displayName, String key, int tileLetterFontSize, int width, int height) { final Resources res = context.getResources(); - mPaint.setTypeface(Typeface.create("sans-serif-light", Typeface.BOLD)); - mPaint.setColor(Color.WHITE); - mPaint.setTextAlign(Paint.Align.CENTER); - mPaint.setAntiAlias(true); + TextPaint paint = new TextPaint(); + paint.setTypeface(Typeface.create("sans-serif-light", Typeface.BOLD)); + paint.setColor(Color.WHITE); + paint.setTextAlign(Paint.Align.CENTER); + paint.setAntiAlias(true); - mColors = res.obtainTypedArray(R.array.letter_tile_colors); - mTileLetterFontSize = res.getDimensionPixelSize(R.dimen.tile_letter_font_size); + TypedArray colors = res.obtainTypedArray(R.array.letter_tile_colors); + mColor = pickColor(key, colors); + colors.recycle(); - mDefaultBitmap = BitmapFactory.decodeResource(res, android.R.drawable.sym_def_app_icon); + mBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888); + String firstChar = displayName.substring(0, 1); + + final Canvas c = new Canvas(); + c.setBitmap(mBitmap); + c.drawColor(mColor); + + char [] firstCharArray = new char[1]; + firstCharArray[0] = firstChar.toUpperCase().charAt(0); + paint.setTextSize(tileLetterFontSize); + + // The bounds that enclose the letter + Rect bounds = new Rect(); + + paint.getTextBounds(firstCharArray, 0, 1, bounds); + c.drawText(firstCharArray, 0, 1, width / 2.0f, height / 2.0f + + (bounds.bottom - bounds.top) / 2.0f, paint); } /** - * @param displayName The name used to create the letter for the tile - * @param key The key used to generate the background color for the tile - * @param width The desired width of the tile - * @param height The desired height of the tile * @return A {@link Bitmap} that contains a letter used in the English * alphabet or digit, if there is no letter or digit available, a * default image is shown instead */ - public Bitmap getLetterTile(String displayName, String key, int width, int height) + public Bitmap getLetterTile() { - final Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888); - String firstChar = displayName.substring(0, 1); + return mBitmap; + } - final Canvas c = mCanvas; - c.setBitmap(bitmap); - c.drawColor(pickColor(key)); - - mFirstChar[0] = firstChar.toUpperCase().charAt(0); - mPaint.setTextSize(mTileLetterFontSize); - mPaint.getTextBounds(mFirstChar, 0, 1, mBounds); - c.drawText(mFirstChar, 0, 1, width / 2.0f, height / 2.0f - + (mBounds.bottom - mBounds.top) / 2.0f, mPaint); - return bitmap; + /** + * @return background color used for letter title. + */ + public int getBackgroundColor() + { + return mColor; } /** @@ -108,18 +101,11 @@ class LetterBitmap * @return A new or previously chosen color for key used as the * tile background color */ - private int pickColor(String key) + private int pickColor(String key, TypedArray colors) { // String.hashCode() is not supposed to change across java versions, so // this should guarantee the same key always maps to the same color final int color = Math.abs(key.hashCode()) % NUM_OF_TILE_COLORS; - try - { - return mColors.getColor(color, Color.BLACK); - } - finally - { - mColors.recycle(); - } + return colors.getColor(color, Color.BLACK); } } \ No newline at end of file diff --git a/app/src/main/java/protect/card_locker/LoyaltyCardCursorAdapter.java b/app/src/main/java/protect/card_locker/LoyaltyCardCursorAdapter.java index 32e4f3797..8c8968b7f 100644 --- a/app/src/main/java/protect/card_locker/LoyaltyCardCursorAdapter.java +++ b/app/src/main/java/protect/card_locker/LoyaltyCardCursorAdapter.java @@ -52,8 +52,9 @@ class LoyaltyCardCursorAdapter extends CursorAdapter String cardIdText = String.format(cardIdFormat, cardIdLabel, loyaltyCard.cardId); cardIdField.setText(cardIdText); - LetterBitmap letterBitmap = new LetterBitmap(context); + int tileLetterFontSize = context.getResources().getDimensionPixelSize(R.dimen.tileLetterFontSize); int pixelSize = context.getResources().getDimensionPixelSize(R.dimen.cardThumbnailSize); - thumbnail.setImageBitmap(letterBitmap.getLetterTile(loyaltyCard.store, loyaltyCard.store, pixelSize, pixelSize)); + LetterBitmap letterBitmap = new LetterBitmap(context, loyaltyCard.store, loyaltyCard.store, tileLetterFontSize, pixelSize, pixelSize); + thumbnail.setImageBitmap(letterBitmap.getLetterTile()); } } diff --git a/app/src/main/java/protect/card_locker/LoyaltyCardEditActivity.java b/app/src/main/java/protect/card_locker/LoyaltyCardEditActivity.java new file mode 100644 index 000000000..70393dc9f --- /dev/null +++ b/app/src/main/java/protect/card_locker/LoyaltyCardEditActivity.java @@ -0,0 +1,401 @@ +package protect.card_locker; + +import android.app.Activity; +import android.content.DialogInterface; +import android.content.Intent; +import android.os.Build; +import android.os.Bundle; +import android.support.design.widget.Snackbar; +import android.support.v7.app.ActionBar; +import android.support.v7.app.AlertDialog; +import android.support.v7.app.AppCompatActivity; +import android.support.v7.widget.Toolbar; +import android.util.Log; +import android.view.Menu; +import android.view.MenuItem; +import android.view.View; +import android.view.ViewTreeObserver; +import android.widget.Button; +import android.widget.EditText; +import android.widget.ImageView; +import android.widget.TextView; +import android.widget.Toast; + +import com.google.zxing.BarcodeFormat; +import com.google.zxing.integration.android.IntentIntegrator; +import com.google.zxing.integration.android.IntentResult; + +public class LoyaltyCardEditActivity extends AppCompatActivity +{ + private static final String TAG = "CardLocker"; + + private static final int SELECT_BARCODE_REQUEST = 1; + + EditText storeFieldEdit; + TextView storeFieldView; + EditText noteFieldEdit; + TextView noteFieldView; + TextView cardIdFieldView; + View cardIdDivider; + View cardIdTableRow; + TextView barcodeTypeField; + ImageView barcodeImage; + View barcodeImageLayout; + View barcodeCaptureLayout; + + Button captureButton; + Button enterButton; + + int loyaltyCardId; + boolean updateLoyaltyCard; + + DBHelper db; + + private void extractIntentFields(Intent intent) + { + final Bundle b = intent.getExtras(); + loyaltyCardId = b != null ? b.getInt("id") : 0; + updateLoyaltyCard = b != null && b.getBoolean("update", false); + + Log.d(TAG, "View activity: id=" + loyaltyCardId + + ", updateLoyaltyCard=" + Boolean.toString(updateLoyaltyCard)); + } + + @Override + protected void onCreate(Bundle savedInstanceState) + { + super.onCreate(savedInstanceState); + + setContentView(R.layout.loyalty_card_edit_activity); + Toolbar toolbar = findViewById(R.id.toolbar); + setSupportActionBar(toolbar); + ActionBar actionBar = getSupportActionBar(); + if(actionBar != null) + { + actionBar.setDisplayHomeAsUpEnabled(true); + } + + extractIntentFields(getIntent()); + + db = new DBHelper(this); + + storeFieldEdit = (EditText) findViewById(R.id.storeNameEdit); + storeFieldView = (TextView) findViewById(R.id.storeNameView); + noteFieldEdit = (EditText) findViewById(R.id.noteEdit); + noteFieldView = (TextView) findViewById(R.id.noteView); + cardIdFieldView = (TextView) findViewById(R.id.cardIdView); + cardIdDivider = findViewById(R.id.cardIdDivider); + cardIdTableRow = findViewById(R.id.cardIdTableRow); + barcodeTypeField = (TextView) findViewById(R.id.barcodeType); + barcodeImage = (ImageView) findViewById(R.id.barcode); + barcodeImageLayout = findViewById(R.id.barcodeLayout); + barcodeCaptureLayout = findViewById(R.id.barcodeCaptureLayout); + + captureButton = (Button) findViewById(R.id.captureButton); + enterButton = (Button) findViewById(R.id.enterButton); + } + + @Override + public void onNewIntent(Intent intent) + { + Log.i(TAG, "Received new intent"); + extractIntentFields(intent); + + // Reset these fields, so they are re-populated in onResume(). + storeFieldEdit.setText(""); + noteFieldEdit.setText(""); + cardIdFieldView.setText(""); + barcodeTypeField.setText(""); + } + + @Override + public void onResume() + { + super.onResume(); + + Log.i(TAG, "To view card: " + loyaltyCardId); + + if(updateLoyaltyCard) + { + final LoyaltyCard loyaltyCard = db.getLoyaltyCard(loyaltyCardId); + if(loyaltyCard == null) + { + Log.w(TAG, "Could not lookup loyalty card " + loyaltyCardId); + Toast.makeText(this, R.string.noCardExistsError, Toast.LENGTH_LONG).show(); + finish(); + return; + } + + if(storeFieldEdit.getText().length() == 0) + { + storeFieldEdit.setText(loyaltyCard.store); + storeFieldView.setText(loyaltyCard.store); + } + + if(noteFieldEdit.getText().length() == 0) + { + noteFieldEdit.setText(loyaltyCard.note); + noteFieldView.setText(loyaltyCard.note); + } + + if(cardIdFieldView.getText().length() == 0) + { + cardIdFieldView.setText(loyaltyCard.cardId); + } + + if(barcodeTypeField.getText().length() == 0) + { + barcodeTypeField.setText(loyaltyCard.barcodeType); + } + + if(updateLoyaltyCard) + { + setTitle(R.string.editCardTitle); + + storeFieldView.setVisibility(View.GONE); + noteFieldView.setVisibility(View.GONE); + } + else + { + barcodeCaptureLayout.setVisibility(View.GONE); + captureButton.setVisibility(View.GONE); + setTitle(R.string.viewCardTitle); + + storeFieldEdit.setVisibility(View.GONE); + noteFieldEdit.setVisibility(View.GONE); + } + } + else + { + setTitle(R.string.addCardTitle); + + storeFieldView.setVisibility(View.GONE); + noteFieldView.setVisibility(View.GONE); + } + + if(cardIdFieldView.getText().length() > 0 && barcodeTypeField.getText().length() > 0) + { + String formatString = barcodeTypeField.getText().toString(); + final BarcodeFormat format = BarcodeFormat.valueOf(formatString); + final String cardIdString = cardIdFieldView.getText().toString(); + + if(barcodeImage.getHeight() == 0) + { + Log.d(TAG, "ImageView size is not known known at start, waiting for load"); + // The size of the ImageView is not yet available as it has not + // yet been drawn. Wait for it to be drawn so the size is available. + barcodeImage.getViewTreeObserver().addOnGlobalLayoutListener( + new ViewTreeObserver.OnGlobalLayoutListener() + { + @Override + public void onGlobalLayout() + { + if (Build.VERSION.SDK_INT < 16) + { + barcodeImage.getViewTreeObserver().removeGlobalOnLayoutListener(this); + } + else + { + barcodeImage.getViewTreeObserver().removeOnGlobalLayoutListener(this); + } + + Log.d(TAG, "ImageView size now known"); + new BarcodeImageWriterTask(barcodeImage, cardIdString, format).execute(); + } + }); + } + else + { + Log.d(TAG, "ImageView size known known, creating barcode"); + new BarcodeImageWriterTask(barcodeImage, cardIdString, format).execute(); + } + + barcodeImageLayout.setVisibility(View.VISIBLE); + } + + View.OnClickListener captureCallback = new View.OnClickListener() + { + @Override + public void onClick(View v) + { + IntentIntegrator integrator = new IntentIntegrator(LoyaltyCardEditActivity.this); + integrator.setDesiredBarcodeFormats(BarcodeSelectorActivity.SUPPORTED_BARCODE_TYPES); + + String prompt = getResources().getString(R.string.scanCardBarcode); + integrator.setPrompt(prompt); + integrator.initiateScan(); + } + }; + + captureButton.setOnClickListener(captureCallback); + + enterButton.setOnClickListener(new View.OnClickListener() + { + @Override + public void onClick(View v) + { + Intent i = new Intent(getApplicationContext(), BarcodeSelectorActivity.class); + + String cardId = cardIdFieldView.getText().toString(); + if(cardId.length() > 0) + { + final Bundle b = new Bundle(); + b.putString("initialCardId", cardId); + i.putExtras(b); + } + + startActivityForResult(i, SELECT_BARCODE_REQUEST); + } + }); + + if(cardIdFieldView.getText().length() > 0) + { + cardIdDivider.setVisibility(View.VISIBLE); + cardIdTableRow.setVisibility(View.VISIBLE); + enterButton.setText(R.string.editCard); + } + else + { + cardIdDivider.setVisibility(View.GONE); + cardIdTableRow.setVisibility(View.GONE); + enterButton.setText(R.string.enterCard); + } + } + + private void doSave() + { + String store = storeFieldEdit.getText().toString(); + String note = noteFieldEdit.getText().toString(); + String cardId = cardIdFieldView.getText().toString(); + String barcodeType = barcodeTypeField.getText().toString(); + + if(store.isEmpty()) + { + Snackbar.make(storeFieldEdit, R.string.noStoreError, Snackbar.LENGTH_LONG).show(); + return; + } + + if(cardId.isEmpty() || barcodeType.isEmpty()) + { + Snackbar.make(cardIdFieldView, R.string.noCardIdError, Snackbar.LENGTH_LONG).show(); + return; + } + + if(updateLoyaltyCard) + { + db.updateLoyaltyCard(loyaltyCardId, store, note, cardId, barcodeType); + Log.i(TAG, "Updated " + loyaltyCardId + " to " + cardId); + } + else + { + loyaltyCardId = (int)db.insertLoyaltyCard(store, note, cardId, barcodeType); + } + + finish(); + } + + @Override + public boolean onCreateOptionsMenu(Menu menu) + { + if(updateLoyaltyCard) + { + getMenuInflater().inflate(R.menu.card_update_menu, menu); + } + else + { + getMenuInflater().inflate(R.menu.card_add_menu, menu); + } + + return super.onCreateOptionsMenu(menu); + } + + @Override + public boolean onOptionsItemSelected(MenuItem item) + { + int id = item.getItemId(); + + switch(id) + { + case android.R.id.home: + finish(); + break; + + case R.id.action_delete: + AlertDialog.Builder builder = new AlertDialog.Builder(this); + builder.setTitle(R.string.deleteTitle); + builder.setMessage(R.string.deleteConfirmation); + builder.setPositiveButton(R.string.confirm, new DialogInterface.OnClickListener() + { + @Override + public void onClick(DialogInterface dialog, int which) + { + Log.e(TAG, "Deleting card: " + loyaltyCardId); + + DBHelper db = new DBHelper(LoyaltyCardEditActivity.this); + db.deleteLoyaltyCard(loyaltyCardId); + + ShortcutHelper.removeShortcut(LoyaltyCardEditActivity.this, loyaltyCardId); + + finish(); + dialog.dismiss(); + } + }); + builder.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() + { + @Override + public void onClick(DialogInterface dialog, int which) + { + dialog.dismiss(); + } + }); + AlertDialog dialog = builder.create(); + dialog.show(); + + return true; + + case R.id.action_save: + doSave(); + return true; + } + + return super.onOptionsItemSelected(item); + } + + @Override + public void onActivityResult(int requestCode, int resultCode, Intent intent) + { + String contents = null; + String format = null; + + IntentResult result = + IntentIntegrator.parseActivityResult(requestCode, resultCode, intent); + if (result != null) + { + Log.i(TAG, "Received barcode information from capture"); + contents = result.getContents(); + format = result.getFormatName(); + } + + if(requestCode == SELECT_BARCODE_REQUEST && resultCode == Activity.RESULT_OK) + { + Log.i(TAG, "Received barcode information from capture"); + + contents = intent.getStringExtra(BarcodeSelectorActivity.BARCODE_CONTENTS); + format = intent.getStringExtra(BarcodeSelectorActivity.BARCODE_FORMAT); + } + + if(contents != null && contents.isEmpty() == false && + format != null && format.isEmpty() == false) + { + Log.i(TAG, "Read barcode id: " + contents); + Log.i(TAG, "Read format: " + format); + + TextView cardIdView = (TextView)findViewById(R.id.cardIdView); + cardIdView.setText(contents); + + final TextView barcodeTypeField = (TextView) findViewById(R.id.barcodeType); + barcodeTypeField.setText(format); + onResume(); + } + } +} diff --git a/app/src/main/java/protect/card_locker/LoyaltyCardViewActivity.java b/app/src/main/java/protect/card_locker/LoyaltyCardViewActivity.java index 03bb5656d..d0940a0c6 100644 --- a/app/src/main/java/protect/card_locker/LoyaltyCardViewActivity.java +++ b/app/src/main/java/protect/card_locker/LoyaltyCardViewActivity.java @@ -2,11 +2,9 @@ package protect.card_locker; import android.app.Activity; -import android.content.Context; import android.content.DialogInterface; import android.content.Intent; import android.content.pm.ActivityInfo; -import android.content.res.Configuration; import android.os.Build; import android.os.Bundle; import android.support.design.widget.Snackbar; @@ -22,56 +20,31 @@ import android.view.ViewTreeObserver; import android.view.Window; import android.view.WindowManager; import android.widget.Button; -import android.widget.CheckBox; import android.widget.EditText; import android.widget.ImageView; import android.widget.TextView; import android.widget.Toast; import com.google.zxing.BarcodeFormat; -import com.google.zxing.integration.android.IntentIntegrator; -import com.google.zxing.integration.android.IntentResult; public class LoyaltyCardViewActivity extends AppCompatActivity { private static final String TAG = "CardLocker"; - private static final int SELECT_BARCODE_REQUEST = 1; - - EditText storeFieldEdit; - TextView storeFieldView; - EditText noteFieldEdit; - TextView noteFieldView; TextView cardIdFieldView; - View cardIdDivider; - View cardIdTableRow; - TextView barcodeTypeField; ImageView barcodeImage; - View barcodeImageLayout; - View barcodeCaptureLayout; - - Button captureButton; - Button enterButton; - + ImageView storeLogo; + View collapsingToolbarLayout; int loyaltyCardId; - boolean updateLoyaltyCard; - boolean viewLoyaltyCard; - boolean rotationEnabled; - DBHelper db; private void extractIntentFields(Intent intent) { final Bundle b = intent.getExtras(); loyaltyCardId = b != null ? b.getInt("id") : 0; - updateLoyaltyCard = b != null && b.getBoolean("update", false); - viewLoyaltyCard = b != null && b.getBoolean("view", false); - - Log.d(TAG, "View activity: id=" + loyaltyCardId - + ", updateLoyaltyCard=" + Boolean.toString(updateLoyaltyCard) - + ", viewLoyaltyCard=" + Boolean.toString(viewLoyaltyCard)); + Log.d(TAG, "View activity: id=" + loyaltyCardId); } @Override @@ -79,8 +52,11 @@ public class LoyaltyCardViewActivity extends AppCompatActivity { super.onCreate(savedInstanceState); - setContentView(R.layout.loyalty_card_view_activity); - Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); + extractIntentFields(getIntent()); + + setContentView(R.layout.loyalty_card_view_layout); + + Toolbar toolbar = findViewById(R.id.toolbar); setSupportActionBar(toolbar); ActionBar actionBar = getSupportActionBar(); if(actionBar != null) @@ -88,24 +64,12 @@ public class LoyaltyCardViewActivity extends AppCompatActivity actionBar.setDisplayHomeAsUpEnabled(true); } - extractIntentFields(getIntent()); - db = new DBHelper(this); - storeFieldEdit = (EditText) findViewById(R.id.storeNameEdit); - storeFieldView = (TextView) findViewById(R.id.storeNameView); - noteFieldEdit = (EditText) findViewById(R.id.noteEdit); - noteFieldView = (TextView) findViewById(R.id.noteView); - cardIdFieldView = (TextView) findViewById(R.id.cardIdView); - cardIdDivider = findViewById(R.id.cardIdDivider); - cardIdTableRow = findViewById(R.id.cardIdTableRow); - barcodeTypeField = (TextView) findViewById(R.id.barcodeType); - barcodeImage = (ImageView) findViewById(R.id.barcode); - barcodeImageLayout = findViewById(R.id.barcodeLayout); - barcodeCaptureLayout = findViewById(R.id.barcodeCaptureLayout); - - captureButton = (Button) findViewById(R.id.captureButton); - enterButton = (Button) findViewById(R.id.enterButton); + cardIdFieldView = findViewById(R.id.cardIdView); + barcodeImage = findViewById(R.id.barcode); + storeLogo = findViewById(R.id.storeLogo); + collapsingToolbarLayout = findViewById(R.id.collapsingToolbarLayout); } @Override @@ -113,12 +77,6 @@ public class LoyaltyCardViewActivity extends AppCompatActivity { Log.i(TAG, "Received new intent"); extractIntentFields(intent); - - // Reset these fields, so they are re-populated in onResume(). - storeFieldEdit.setText(""); - noteFieldEdit.setText(""); - cardIdFieldView.setText(""); - barcodeTypeField.setText(""); } @Override @@ -128,90 +86,44 @@ public class LoyaltyCardViewActivity extends AppCompatActivity Log.i(TAG, "To view card: " + loyaltyCardId); - if(viewLoyaltyCard) + // The brightness value is on a scale from [0, ..., 1], where + // '1' is the brightest. We attempt to maximize the brightness + // to help barcode readers scan the barcode. + Window window = getWindow(); + if(window != null) { - // The brightness value is on a scale from [0, ..., 1], where - // '1' is the brightest. We attempt to maximize the brightness - // to help barcode readers scan the barcode. - Window window = getWindow(); - if(window != null) - { - WindowManager.LayoutParams attributes = window.getAttributes(); - attributes.screenBrightness = 1F; - window.setAttributes(attributes); - } + WindowManager.LayoutParams attributes = window.getAttributes(); + attributes.screenBrightness = 1F; + window.setAttributes(attributes); } - if(updateLoyaltyCard || viewLoyaltyCard) + final LoyaltyCard loyaltyCard = db.getLoyaltyCard(loyaltyCardId); + if(loyaltyCard == null) { - final LoyaltyCard loyaltyCard = db.getLoyaltyCard(loyaltyCardId); - if(loyaltyCard == null) - { - Log.w(TAG, "Could not lookup loyalty card " + loyaltyCardId); - Toast.makeText(this, R.string.noCardExistsError, Toast.LENGTH_LONG).show(); - finish(); - return; - } - - if(storeFieldEdit.getText().length() == 0) - { - storeFieldEdit.setText(loyaltyCard.store); - storeFieldView.setText(loyaltyCard.store); - } - - if(noteFieldEdit.getText().length() == 0) - { - noteFieldEdit.setText(loyaltyCard.note); - noteFieldView.setText(loyaltyCard.note); - } - - if(cardIdFieldView.getText().length() == 0) - { - cardIdFieldView.setText(loyaltyCard.cardId); - } - - if(barcodeTypeField.getText().length() == 0) - { - barcodeTypeField.setText(loyaltyCard.barcodeType); - } - - if(updateLoyaltyCard) - { - setTitle(R.string.editCardTitle); - - storeFieldView.setVisibility(View.GONE); - noteFieldView.setVisibility(View.GONE); - } - else - { - barcodeCaptureLayout.setVisibility(View.GONE); - captureButton.setVisibility(View.GONE); - setTitle(R.string.viewCardTitle); - - storeFieldEdit.setVisibility(View.GONE); - noteFieldEdit.setVisibility(View.GONE); - } - } - else - { - setTitle(R.string.addCardTitle); - - storeFieldView.setVisibility(View.GONE); - noteFieldView.setVisibility(View.GONE); + Log.w(TAG, "Could not lookup loyalty card " + loyaltyCardId); + Toast.makeText(this, R.string.noCardExistsError, Toast.LENGTH_LONG).show(); + finish(); + return; } - if(cardIdFieldView.getText().length() > 0 && barcodeTypeField.getText().length() > 0) - { - String formatString = barcodeTypeField.getText().toString(); - final BarcodeFormat format = BarcodeFormat.valueOf(formatString); - final String cardIdString = cardIdFieldView.getText().toString(); + String formatString = loyaltyCard.barcodeType; + final BarcodeFormat format = BarcodeFormat.valueOf(formatString); + final String cardIdString = loyaltyCard.cardId; - if(barcodeImage.getHeight() == 0) - { - Log.d(TAG, "ImageView size is not known known at start, waiting for load"); - // The size of the ImageView is not yet available as it has not - // yet been drawn. Wait for it to be drawn so the size is available. - barcodeImage.getViewTreeObserver().addOnGlobalLayoutListener( + cardIdFieldView.setText(loyaltyCard.cardId); + + int cardViewLetterFontSize = getResources().getDimensionPixelSize(R.dimen.cardViewLetterFontSize); + int pixelSize = getResources().getDimensionPixelSize(R.dimen.cardThumbnailSizeLarge); + LetterBitmap letterBitmap = new LetterBitmap(this, loyaltyCard.store, loyaltyCard.store, cardViewLetterFontSize, pixelSize, pixelSize); + storeLogo.setImageBitmap(letterBitmap.getLetterTile()); + collapsingToolbarLayout.setBackgroundColor(letterBitmap.getBackgroundColor()); + + if(barcodeImage.getHeight() == 0) + { + Log.d(TAG, "ImageView size is not known known at start, waiting for load"); + // The size of the ImageView is not yet available as it has not + // yet been drawn. Wait for it to be drawn so the size is available. + barcodeImage.getViewTreeObserver().addOnGlobalLayoutListener( new ViewTreeObserver.OnGlobalLayoutListener() { @Override @@ -230,112 +142,19 @@ public class LoyaltyCardViewActivity extends AppCompatActivity new BarcodeImageWriterTask(barcodeImage, cardIdString, format).execute(); } }); - } - else - { - Log.d(TAG, "ImageView size known known, creating barcode"); - new BarcodeImageWriterTask(barcodeImage, cardIdString, format).execute(); - } - - barcodeImageLayout.setVisibility(View.VISIBLE); - } - - View.OnClickListener captureCallback = new View.OnClickListener() - { - @Override - public void onClick(View v) - { - IntentIntegrator integrator = new IntentIntegrator(LoyaltyCardViewActivity.this); - integrator.setDesiredBarcodeFormats(BarcodeSelectorActivity.SUPPORTED_BARCODE_TYPES); - - String prompt = getResources().getString(R.string.scanCardBarcode); - integrator.setPrompt(prompt); - integrator.initiateScan(); - } - }; - - captureButton.setOnClickListener(captureCallback); - - enterButton.setOnClickListener(new View.OnClickListener() - { - @Override - public void onClick(View v) - { - Intent i = new Intent(getApplicationContext(), BarcodeSelectorActivity.class); - - String cardId = cardIdFieldView.getText().toString(); - if(cardId.length() > 0) - { - final Bundle b = new Bundle(); - b.putString("initialCardId", cardId); - i.putExtras(b); - } - - startActivityForResult(i, SELECT_BARCODE_REQUEST); - } - }); - - if(cardIdFieldView.getText().length() > 0) - { - cardIdDivider.setVisibility(View.VISIBLE); - cardIdTableRow.setVisibility(View.VISIBLE); - enterButton.setText(R.string.editCard); } else { - cardIdDivider.setVisibility(View.GONE); - cardIdTableRow.setVisibility(View.GONE); - enterButton.setText(R.string.enterCard); - } - } - - private void doSave() - { - String store = storeFieldEdit.getText().toString(); - String note = noteFieldEdit.getText().toString(); - String cardId = cardIdFieldView.getText().toString(); - String barcodeType = barcodeTypeField.getText().toString(); - - if(store.isEmpty()) - { - Snackbar.make(storeFieldEdit, R.string.noStoreError, Snackbar.LENGTH_LONG).show(); - return; + Log.d(TAG, "ImageView size known known, creating barcode"); + new BarcodeImageWriterTask(barcodeImage, cardIdString, format).execute(); } - if(cardId.isEmpty() || barcodeType.isEmpty()) - { - Snackbar.make(cardIdFieldView, R.string.noCardIdError, Snackbar.LENGTH_LONG).show(); - return; - } - - if(updateLoyaltyCard) - { - db.updateLoyaltyCard(loyaltyCardId, store, note, cardId, barcodeType); - Log.i(TAG, "Updated " + loyaltyCardId + " to " + cardId); - } - else - { - loyaltyCardId = (int)db.insertLoyaltyCard(store, note, cardId, barcodeType); - } - - finish(); } @Override public boolean onCreateOptionsMenu(Menu menu) { - if(viewLoyaltyCard) - { - getMenuInflater().inflate(R.menu.card_view_menu, menu); - } - else if(updateLoyaltyCard) - { - getMenuInflater().inflate(R.menu.card_update_menu, menu); - } - else - { - getMenuInflater().inflate(R.menu.card_add_menu, menu); - } + getMenuInflater().inflate(R.menu.card_view_menu, menu); rotationEnabled = true; @@ -353,40 +172,8 @@ public class LoyaltyCardViewActivity extends AppCompatActivity finish(); break; - case R.id.action_delete: - AlertDialog.Builder builder = new AlertDialog.Builder(this); - builder.setTitle(R.string.deleteTitle); - builder.setMessage(R.string.deleteConfirmation); - builder.setPositiveButton(R.string.confirm, new DialogInterface.OnClickListener() - { - @Override - public void onClick(DialogInterface dialog, int which) - { - Log.e(TAG, "Deleting card: " + loyaltyCardId); - - DBHelper db = new DBHelper(LoyaltyCardViewActivity.this); - db.deleteLoyaltyCard(loyaltyCardId); - - ShortcutHelper.removeShortcut(LoyaltyCardViewActivity.this, loyaltyCardId); - - finish(); - dialog.dismiss(); - } - }); - builder.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() - { - @Override - public void onClick(DialogInterface dialog, int which) - { - dialog.dismiss(); - } - }); - AlertDialog dialog = builder.create(); - dialog.show(); - - return true; case R.id.action_edit: - Intent intent = new Intent(getApplicationContext(), LoyaltyCardViewActivity.class); + Intent intent = new Intent(getApplicationContext(), LoyaltyCardEditActivity.class); Bundle bundle = new Bundle(); bundle.putInt("id", loyaltyCardId); bundle.putBoolean("update", true); @@ -410,50 +197,8 @@ public class LoyaltyCardViewActivity extends AppCompatActivity } rotationEnabled = !rotationEnabled; return true; - - case R.id.action_save: - doSave(); - return true; } return super.onOptionsItemSelected(item); } - - @Override - public void onActivityResult(int requestCode, int resultCode, Intent intent) - { - String contents = null; - String format = null; - - IntentResult result = - IntentIntegrator.parseActivityResult(requestCode, resultCode, intent); - if (result != null) - { - Log.i(TAG, "Received barcode information from capture"); - contents = result.getContents(); - format = result.getFormatName(); - } - - if(requestCode == SELECT_BARCODE_REQUEST && resultCode == Activity.RESULT_OK) - { - Log.i(TAG, "Received barcode information from capture"); - - contents = intent.getStringExtra(BarcodeSelectorActivity.BARCODE_CONTENTS); - format = intent.getStringExtra(BarcodeSelectorActivity.BARCODE_FORMAT); - } - - if(contents != null && contents.isEmpty() == false && - format != null && format.isEmpty() == false) - { - Log.i(TAG, "Read barcode id: " + contents); - Log.i(TAG, "Read format: " + format); - - TextView cardIdView = (TextView)findViewById(R.id.cardIdView); - cardIdView.setText(contents); - - final TextView barcodeTypeField = (TextView) findViewById(R.id.barcodeType); - barcodeTypeField.setText(format); - onResume(); - } - } } \ No newline at end of file diff --git a/app/src/main/java/protect/card_locker/MainActivity.java b/app/src/main/java/protect/card_locker/MainActivity.java index 1fcc445ff..39bd3f492 100644 --- a/app/src/main/java/protect/card_locker/MainActivity.java +++ b/app/src/main/java/protect/card_locker/MainActivity.java @@ -96,7 +96,6 @@ public class MainActivity extends AppCompatActivity i.setAction(""); final Bundle b = new Bundle(); b.putInt("id", loyaltyCard.id); - b.putBoolean("view", true); i.putExtras(b); ShortcutHelper.updateShortcuts(MainActivity.this, loyaltyCard, i); @@ -153,7 +152,7 @@ public class MainActivity extends AppCompatActivity if (id == R.id.action_add) { - Intent i = new Intent(getApplicationContext(), LoyaltyCardViewActivity.class); + Intent i = new Intent(getApplicationContext(), LoyaltyCardEditActivity.class); startActivity(i); return true; } diff --git a/app/src/main/res/layout/loyalty_card_view_activity.xml b/app/src/main/res/layout/loyalty_card_edit_activity.xml similarity index 100% rename from app/src/main/res/layout/loyalty_card_view_activity.xml rename to app/src/main/res/layout/loyalty_card_edit_activity.xml diff --git a/app/src/main/res/layout/loyalty_card_view_layout.xml b/app/src/main/res/layout/loyalty_card_view_layout.xml new file mode 100644 index 000000000..7ab5423bb --- /dev/null +++ b/app/src/main/res/layout/loyalty_card_view_layout.xml @@ -0,0 +1,93 @@ + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/app/src/main/res/values/dimens.xml b/app/src/main/res/values/dimens.xml index aed951295..ac36b9748 100644 --- a/app/src/main/res/values/dimens.xml +++ b/app/src/main/res/values/dimens.xml @@ -17,10 +17,12 @@ 18sp 46dp + 200dp 8dp 16dp - 33sp + 33sp + 100sp diff --git a/app/src/main/res/values/styles.xml b/app/src/main/res/values/styles.xml index c4d5ff2e8..d7c6cc296 100644 --- a/app/src/main/res/values/styles.xml +++ b/app/src/main/res/values/styles.xml @@ -17,6 +17,11 @@ +