Improved printable string

pull/214/head
M66B 11 months ago
parent d95aebfcc0
commit 95aed14c59

@ -232,7 +232,7 @@ public class AdapterMedia extends RecyclerView.Adapter<AdapterMedia.ViewHolder>
BinaryBitmap bBitmap = new BinaryBitmap(new HybridBinarizer(source)); BinaryBitmap bBitmap = new BinaryBitmap(new HybridBinarizer(source));
MultiFormatReader reader = new MultiFormatReader(); MultiFormatReader reader = new MultiFormatReader();
Result result = reader.decode(bBitmap); Result result = reader.decode(bBitmap);
args.putString("barcode_text", Helper.getPrintableString(result.getText())); args.putString("barcode_text", Helper.getPrintableString(result.getText(), false));
args.putString("barcode_format", result.getBarcodeFormat().name()); args.putString("barcode_format", result.getBarcodeFormat().name());
} catch (NotFoundException ex) { } catch (NotFoundException ex) {
Log.w(ex); Log.w(ex);

@ -2177,28 +2177,29 @@ public class Helper {
return df.format(sign * bytes / Math.pow(unit, exp)) + " " + pre + "B"; return df.format(sign * bytes / Math.pow(unit, exp)) + " " + pre + "B";
} }
static boolean isPrintableChar(char c) { static boolean isPrintableChar(int codepoint) {
Character.UnicodeBlock block = Character.UnicodeBlock.of(c); Character.UnicodeBlock block = Character.UnicodeBlock.of(codepoint);
if (block == null || block == Character.UnicodeBlock.SPECIALS) if (block == null || block == Character.UnicodeBlock.SPECIALS)
return false; return false;
return !Character.isISOControl(c); return !Character.isISOControl(codepoint);
} }
// https://issuetracker.google.com/issues/37054851 // https://issuetracker.google.com/issues/37054851
static String getPrintableString(String value) { static String getPrintableString(String value, boolean debug) {
if (TextUtils.isEmpty(value)) if (TextUtils.isEmpty(value))
return value; return value;
StringBuilder result = new StringBuilder(); StringBuilder result = new StringBuilder();
for (int i = 0; i < value.length(); i++) { for (int i = 0; i < value.length(); ) {
char kar = value.charAt(i); int codepoint = value.codePointAt(i);
if (kar == '\n') if (debug && codepoint == 10)
result.append('|'); result.append('|');
else if (kar == ' ') else if (debug && codepoint == 32)
result.append('_'); result.append('_');
else if (!Helper.isPrintableChar(kar) || kar == '\u00a0') else if (!Helper.isPrintableChar(codepoint) || codepoint == 160)
result.append('{').append(Integer.toHexString(kar)).append('}'); result.append('{').append(Integer.toHexString(codepoint)).append('}');
else else
result.append(kar); result.append(Character.toChars(codepoint));
i += Character.charCount(codepoint);
} }
return result.toString(); return result.toString();
} }

@ -625,9 +625,9 @@ public class Log {
if (element instanceof Long) if (element instanceof Long)
elements[i] = element + " (0x" + Long.toHexString((Long) element) + ")"; elements[i] = element + " (0x" + Long.toHexString((Long) element) + ")";
else if (element instanceof Spanned) else if (element instanceof Spanned)
elements[i] = "(span:" + Helper.getPrintableString(element.toString()) + ")"; elements[i] = "(span:" + Helper.getPrintableString(element.toString(), true) + ")";
else else
elements[i] = (element == null ? "<null>" : Helper.getPrintableString(element.toString())); elements[i] = (element == null ? "<null>" : Helper.getPrintableString(element.toString(), true));
} }
value = TextUtils.join(",", elements); value = TextUtils.join(",", elements);
if (length > 10) if (length > 10)
@ -636,7 +636,7 @@ public class Log {
} else if (v instanceof Long) } else if (v instanceof Long)
value = v + " (0x" + Long.toHexString((Long) v) + ")"; value = v + " (0x" + Long.toHexString((Long) v) + ")";
else if (v instanceof Spanned) else if (v instanceof Spanned)
value = "(span:" + Helper.getPrintableString(v.toString()) + ")"; value = "(span:" + Helper.getPrintableString(v.toString(), true) + ")";
else if (v instanceof Bundle) else if (v instanceof Bundle)
value = "{" + TextUtils.join(" ", getExtras((Bundle) v)) + "}"; value = "{" + TextUtils.join(" ", getExtras((Bundle) v)) + "}";

Loading…
Cancel
Save