BarCode as separate class
This commit is contained in:
parent
3324f83b0e
commit
e829548924
8 changed files with 108 additions and 68 deletions
|
@ -9,6 +9,7 @@ import com.squareup.spoon.Spoon;
|
|||
|
||||
import org.ligi.passandroid.helper.BarcodeDecoder;
|
||||
import org.ligi.passandroid.injections.FixedPassListPassStore;
|
||||
import org.ligi.passandroid.model.BarCode;
|
||||
import org.ligi.passandroid.model.Pass;
|
||||
import org.ligi.passandroid.model.PassImpl;
|
||||
import org.ligi.passandroid.ui.FullscreenBarcodeActivity;
|
||||
|
@ -71,8 +72,7 @@ public class TheFullscreenBarcodeActivity extends BaseIntegration<FullscreenBarc
|
|||
|
||||
private void testWithBarcodeFormat(BarcodeFormat format) {
|
||||
final PassImpl pass = new PassImpl();
|
||||
pass.setBarcodeFormat(format);
|
||||
pass.setBarcodeMessage(BARCODE_MESSAGE);
|
||||
pass.setBarCode(new BarCode(format, BARCODE_MESSAGE));
|
||||
App.getPassStore().setCurrentPass(pass);
|
||||
getActivity();
|
||||
onView(withId(R.id.fullscreen_image)).check(matches(isDisplayed()));
|
||||
|
|
|
@ -4,9 +4,11 @@ import android.annotation.TargetApi;
|
|||
import android.test.suitebuilder.annotation.MediumTest;
|
||||
|
||||
import com.google.common.base.Optional;
|
||||
import com.google.zxing.BarcodeFormat;
|
||||
|
||||
import org.joda.time.DateTime;
|
||||
import org.ligi.passandroid.injections.FixedPassListPassStore;
|
||||
import org.ligi.passandroid.model.BarCode;
|
||||
import org.ligi.passandroid.model.Pass;
|
||||
import org.ligi.passandroid.model.PassImpl;
|
||||
import org.ligi.passandroid.ui.PassViewActivity;
|
||||
|
@ -37,6 +39,7 @@ public class ThePassViewActivity extends BaseIntegration<PassViewActivity> {
|
|||
final ArrayList<Pass> list = new ArrayList<Pass>() {{
|
||||
act_pass = new PassImpl();
|
||||
act_pass.setDescription("foo");
|
||||
act_pass.setBarCode(new BarCode(BarcodeFormat.QR_CODE, "foo"));
|
||||
add(act_pass);
|
||||
}};
|
||||
|
||||
|
|
|
@ -20,7 +20,6 @@ import java.nio.charset.Charset;
|
|||
import java.util.ArrayList;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
import java.util.Locale;
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
|
@ -90,20 +89,10 @@ public class AppleStylePassReader {
|
|||
|
||||
try {
|
||||
final JSONObject barcode_json = pass_json.getJSONObject("barcode");
|
||||
|
||||
pass.setBarcodeFormat(BarcodeFormat.QR_CODE); // DEFAULT
|
||||
|
||||
pass.setBarcodeMessage(barcode_json.getString("message"));
|
||||
|
||||
final String barcodeFormatString = barcode_json.getString("format");
|
||||
|
||||
if (barcodeFormatString.contains("417")) {
|
||||
pass.setBarcodeFormat(BarcodeFormat.PDF_417);
|
||||
}
|
||||
|
||||
if (barcodeFormatString.toUpperCase(Locale.ENGLISH).contains("AZTEC")) {
|
||||
pass.setBarcodeFormat(BarcodeFormat.AZTEC);
|
||||
}
|
||||
final BarcodeFormat barcodeFormat = BarCode.getFormatFromString(barcodeFormatString);
|
||||
pass.setBarCode(new BarCode(barcodeFormat, barcode_json.getString("message")));
|
||||
|
||||
// TODO should check a bit more with barcode here - this can be dangerous
|
||||
|
||||
|
|
58
src/main/java/org/ligi/passandroid/model/BarCode.java
Normal file
58
src/main/java/org/ligi/passandroid/model/BarCode.java
Normal file
|
@ -0,0 +1,58 @@
|
|||
package org.ligi.passandroid.model;
|
||||
|
||||
import android.graphics.Bitmap;
|
||||
|
||||
import com.google.zxing.BarcodeFormat;
|
||||
|
||||
import org.ligi.passandroid.Tracker;
|
||||
import org.ligi.passandroid.helper.BarcodeHelper;
|
||||
import org.ligi.tracedroid.logging.Log;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.util.Locale;
|
||||
|
||||
public class BarCode implements Serializable {
|
||||
private final BarcodeFormat barcodeFormat;
|
||||
private final String barcodeMessage;
|
||||
|
||||
public BarCode(BarcodeFormat barcodeFormat, String barcodeMessage) {
|
||||
this.barcodeFormat = barcodeFormat;
|
||||
this.barcodeMessage = barcodeMessage;
|
||||
}
|
||||
|
||||
public Bitmap getBitmap(final int size) {
|
||||
if (barcodeMessage == null) {
|
||||
// no message -> no barcode
|
||||
Tracker.get().trackException("No Barcode in pass - strange", false);
|
||||
return null;
|
||||
}
|
||||
|
||||
if (barcodeFormat == null) {
|
||||
Log.w("Barcode format is null - fallback to QR");
|
||||
Tracker.get().trackException("Barcode format is null - fallback to QR", false);
|
||||
BarcodeHelper.generateBarCodeBitmap(barcodeMessage, BarcodeFormat.QR_CODE, size);
|
||||
}
|
||||
|
||||
return BarcodeHelper.generateBarCodeBitmap(barcodeMessage, barcodeFormat, size);
|
||||
|
||||
}
|
||||
|
||||
public BarcodeFormat getFormat() {
|
||||
return barcodeFormat;
|
||||
}
|
||||
|
||||
public static BarcodeFormat getFormatFromString(String format) {
|
||||
if (format.contains("417")) {
|
||||
return BarcodeFormat.PDF_417;
|
||||
}
|
||||
|
||||
if (format.toUpperCase(Locale.ENGLISH).contains("AZTEC")) {
|
||||
return BarcodeFormat.AZTEC;
|
||||
}
|
||||
|
||||
return BarcodeFormat.QR_CODE;
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
|
@ -3,7 +3,6 @@ package org.ligi.passandroid.model;
|
|||
import android.graphics.Bitmap;
|
||||
|
||||
import com.google.common.base.Optional;
|
||||
import com.google.zxing.BarcodeFormat;
|
||||
|
||||
import org.joda.time.DateTime;
|
||||
|
||||
|
@ -34,10 +33,6 @@ public interface Pass extends Serializable {
|
|||
|
||||
boolean isValid();
|
||||
|
||||
BarcodeFormat getBarcodeFormat();
|
||||
|
||||
Bitmap getBarcodeBitmap(final int size);
|
||||
|
||||
Optional<Bitmap> getIconBitmap();
|
||||
|
||||
Optional<Bitmap> getThumbnailImage();
|
||||
|
@ -59,4 +54,6 @@ public interface Pass extends Serializable {
|
|||
Optional<String> getOrganisation();
|
||||
|
||||
Optional<String> getSource();
|
||||
|
||||
Optional<BarCode> getBarCode();
|
||||
}
|
||||
|
|
|
@ -4,12 +4,9 @@ import android.graphics.Bitmap;
|
|||
import android.graphics.BitmapFactory;
|
||||
|
||||
import com.google.common.base.Optional;
|
||||
import com.google.zxing.BarcodeFormat;
|
||||
|
||||
import org.joda.time.DateTime;
|
||||
import org.ligi.axt.AXT;
|
||||
import org.ligi.passandroid.Tracker;
|
||||
import org.ligi.passandroid.helper.BarcodeHelper;
|
||||
import org.ligi.tracedroid.logging.Log;
|
||||
|
||||
import java.io.File;
|
||||
|
@ -22,8 +19,9 @@ public class PassImpl implements Pass, Serializable {
|
|||
private Optional<String> organisation = Optional.absent();
|
||||
private String type;
|
||||
private boolean valid = true; // be positive
|
||||
private String barcodeMessage;
|
||||
private BarcodeFormat barcodeFormat;
|
||||
|
||||
private Optional<BarCode> barCode = Optional.absent();
|
||||
|
||||
private int backGroundColor;
|
||||
private int foregroundColor;
|
||||
private String description;
|
||||
|
@ -75,10 +73,6 @@ public class PassImpl implements Pass, Serializable {
|
|||
return valid;
|
||||
}
|
||||
|
||||
@Override
|
||||
public BarcodeFormat getBarcodeFormat() {
|
||||
return barcodeFormat;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getType() {
|
||||
|
@ -159,23 +153,6 @@ public class PassImpl implements Pass, Serializable {
|
|||
return getBitmapFromOptionalString(logoBitmapFile);
|
||||
}
|
||||
|
||||
public Bitmap getBarcodeBitmap(final int size) {
|
||||
if (barcodeMessage == null) {
|
||||
// no message -> no barcode
|
||||
Tracker.get().trackException("No Barcode in pass - strange", false);
|
||||
return null;
|
||||
}
|
||||
|
||||
if (barcodeFormat == null) {
|
||||
Log.w("Barcode format is null - fallback to QR");
|
||||
Tracker.get().trackException("Barcode format is null - fallback to QR", false);
|
||||
BarcodeHelper.generateBarCodeBitmap(barcodeMessage, BarcodeFormat.QR_CODE, size);
|
||||
}
|
||||
|
||||
return BarcodeHelper.generateBarCodeBitmap(barcodeMessage, barcodeFormat, size);
|
||||
|
||||
}
|
||||
|
||||
|
||||
public String getTypeNotNull() {
|
||||
if (type == null) {
|
||||
|
@ -209,14 +186,6 @@ public class PassImpl implements Pass, Serializable {
|
|||
valid = false;
|
||||
}
|
||||
|
||||
public void setBarcodeFormat(BarcodeFormat barcodeFormat) {
|
||||
this.barcodeFormat = barcodeFormat;
|
||||
}
|
||||
|
||||
public void setBarcodeMessage(String barcodeMessage) {
|
||||
this.barcodeMessage = barcodeMessage;
|
||||
}
|
||||
|
||||
public void setRelevantDate(Optional<DateTime> relevantDaterele) {
|
||||
this.relevantDate = relevantDaterele;
|
||||
}
|
||||
|
@ -282,4 +251,13 @@ public class PassImpl implements Pass, Serializable {
|
|||
}
|
||||
return Optional.absent();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Optional<BarCode> getBarCode() {
|
||||
return barCode;
|
||||
}
|
||||
|
||||
public void setBarCode(BarCode barCode) {
|
||||
this.barCode = Optional.fromNullable(barCode);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -28,7 +28,7 @@ public class FullscreenBarcodeActivity extends PassViewActivityBase {
|
|||
|
||||
setBestFittingOrientationForBarCode();
|
||||
|
||||
iv.setImageBitmap(optionalPass.get().getBarcodeBitmap(smallestSize));
|
||||
iv.setImageBitmap(optionalPass.get().getBarCode().get().getBitmap(smallestSize));
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -46,7 +46,7 @@ public class FullscreenBarcodeActivity extends PassViewActivityBase {
|
|||
*/
|
||||
private void setBestFittingOrientationForBarCode() {
|
||||
|
||||
if (optionalPass.get().getBarcodeFormat() == BarcodeFormat.PDF_417) {
|
||||
if (optionalPass.get().getBarCode().get().getFormat() == BarcodeFormat.PDF_417) {
|
||||
switch (getRequestedOrientation()) {
|
||||
|
||||
case ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE:
|
||||
|
|
|
@ -3,6 +3,7 @@ package org.ligi.passandroid.ui;
|
|||
import android.app.AlertDialog;
|
||||
import android.content.DialogInterface;
|
||||
import android.content.Intent;
|
||||
import android.graphics.Bitmap;
|
||||
import android.os.Bundle;
|
||||
import android.support.v4.app.NavUtils;
|
||||
import android.support.v4.app.TaskStackBuilder;
|
||||
|
@ -15,6 +16,8 @@ import android.view.WindowManager;
|
|||
import android.widget.ImageView;
|
||||
import android.widget.TextView;
|
||||
|
||||
import com.google.common.base.Optional;
|
||||
|
||||
import org.ligi.axt.AXT;
|
||||
import org.ligi.passandroid.App;
|
||||
import org.ligi.passandroid.R;
|
||||
|
@ -30,7 +33,6 @@ import butterknife.OnClick;
|
|||
|
||||
public class PassViewActivity extends PassViewActivityBase {
|
||||
|
||||
|
||||
@OnClick(R.id.barcode_img)
|
||||
void onBarcodeClick() {
|
||||
AXT.at(this).startCommonIntent().activityFromClass(FullscreenBarcodeActivity.class);
|
||||
|
@ -88,23 +90,25 @@ public class PassViewActivity extends PassViewActivityBase {
|
|||
|
||||
ButterKnife.inject(this);
|
||||
|
||||
barcode_img.setImageBitmap(pass.getBarcodeBitmap(AXT.at(getWindowManager()).getSmallestSide() / 3));
|
||||
if (pass.getBarCode().isPresent()) {
|
||||
|
||||
if (pass.getLogoBitmap().isPresent()) {
|
||||
logo_img.setImageBitmap(pass.getLogoBitmap().get());
|
||||
logo_img.setVisibility(View.VISIBLE);
|
||||
barcode_img.setVisibility(View.VISIBLE);
|
||||
} else {
|
||||
logo_img.setVisibility(View.GONE);
|
||||
barcode_img.setVisibility(View.GONE);
|
||||
}
|
||||
|
||||
if (pass.getBarCode().isPresent()) {
|
||||
final int smallestSide = AXT.at(getWindowManager()).getSmallestSide();
|
||||
final Bitmap bitmap = pass.getBarCode().get().getBitmap(smallestSide / 3);
|
||||
setBitmapSafe(barcode_img, Optional.fromNullable(bitmap));
|
||||
} else {
|
||||
setBitmapSafe(barcode_img, Optional.<Bitmap>absent());
|
||||
}
|
||||
setBitmapSafe(logo_img, pass.getLogoBitmap());
|
||||
|
||||
logo_img.setBackgroundColor(pass.getBackGroundColor());
|
||||
|
||||
if (pass.getThumbnailImage().isPresent()) {
|
||||
thumbnail_img.setImageBitmap(pass.getThumbnailImage().get());
|
||||
thumbnail_img.setVisibility(View.VISIBLE);
|
||||
} else {
|
||||
thumbnail_img.setVisibility(View.GONE);
|
||||
}
|
||||
setBitmapSafe(thumbnail_img, pass.getThumbnailImage());
|
||||
|
||||
if (findViewById(R.id.map_container) != null) {
|
||||
if (!(pass.getLocations().size() > 0 && PassbookMapsFacade.init(this))) {
|
||||
|
@ -135,6 +139,17 @@ public class PassViewActivity extends PassViewActivityBase {
|
|||
PassVisualizer.visualize(this, pass, contentView);
|
||||
}
|
||||
|
||||
private static void setBitmapSafe(ImageView imageView, Optional<Bitmap> bitmapOptional) {
|
||||
|
||||
if (bitmapOptional.isPresent()) {
|
||||
imageView.setImageBitmap(bitmapOptional.get());
|
||||
imageView.setVisibility(View.VISIBLE);
|
||||
} else {
|
||||
imageView.setVisibility(View.GONE);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private String getPassDebugInfo(Pass pass) {
|
||||
|
||||
String result = ""; // TODO bring back sth like passbook.getPlainJsonString();
|
||||
|
|
Loading…
Reference in a new issue