Streamline contact encoding and avoid encoding a line break

git-svn-id: https://zxing.googlecode.com/svn/trunk@925 59b500cc-1b3d-0410-9834-0bbf25fbcc57
This commit is contained in:
srowen 2009-05-01 17:25:42 +00:00
parent e168d0c3c6
commit fb2813d1ea
2 changed files with 33 additions and 16 deletions

View file

@ -103,38 +103,42 @@ public final class QRCodeEncoder {
} else if (type.equals(Contents.Type.CONTACT)) { } else if (type.equals(Contents.Type.CONTACT)) {
Bundle bundle = intent.getBundleExtra(Intents.Encode.DATA); Bundle bundle = intent.getBundleExtra(Intents.Encode.DATA);
if (bundle != null) { if (bundle != null) {
mContents = "MECARD:"; StringBuilder newContents = new StringBuilder();
mDisplayContents = ""; StringBuilder newDisplayContents = new StringBuilder();
newContents.append("MECARD:");
String name = bundle.getString(Contacts.Intents.Insert.NAME); String name = bundle.getString(Contacts.Intents.Insert.NAME);
if (name != null && name.length() > 0) { if (name != null && name.length() > 0) {
mContents = "N:" + name + ';'; newContents.append("N:").append(name).append(';');
mDisplayContents += name; newDisplayContents.append(name);
} }
String address = bundle.getString(Contacts.Intents.Insert.POSTAL); String address = bundle.getString(Contacts.Intents.Insert.POSTAL);
if (address != null && address.length() > 0) { if (address != null && address.length() > 0) {
mContents += "ADR:" + address + ';'; newContents.append("ADR:").append(address).append(';');
mDisplayContents += '\n' + address; newDisplayContents.append('\n').append(address);
} }
for (int x = 0; x < Contents.PHONE_KEYS.length; x++) { for (int x = 0; x < Contents.PHONE_KEYS.length; x++) {
String phone = bundle.getString(Contents.PHONE_KEYS[x]); String phone = bundle.getString(Contents.PHONE_KEYS[x]);
if (phone != null && phone.length() > 0) { if (phone != null && phone.length() > 0) {
mContents += "TEL:" + phone + ';'; newContents.append("TEL:").append(phone).append(';');
mDisplayContents += '\n' + PhoneNumberUtils.formatNumber(phone); newDisplayContents.append('\n').append(PhoneNumberUtils.formatNumber(phone));
} }
} }
for (int x = 0; x < Contents.EMAIL_KEYS.length; x++) { for (int x = 0; x < Contents.EMAIL_KEYS.length; x++) {
String email = bundle.getString(Contents.EMAIL_KEYS[x]); String email = bundle.getString(Contents.EMAIL_KEYS[x]);
if (email != null && email.length() > 0) { if (email != null && email.length() > 0) {
mContents += "EMAIL:" + email + ';'; newContents.append("EMAIL:").append(email).append(';');
mDisplayContents += '\n' + email; newDisplayContents.append('\n').append(email);
} }
} }
// Make sure we've encoded at least one field. // Make sure we've encoded at least one field.
if (mDisplayContents.length() > 0) { if (newDisplayContents.length() > 0) {
mContents += ";"; newContents.append(';');
mContents = newContents.toString();
mDisplayContents = newDisplayContents.toString();
mTitle = mActivity.getString(R.string.contents_contact); mTitle = mActivity.getString(R.string.contents_contact);
} else { } else {
mContents = null; mContents = null;
mDisplayContents = null;
} }
} }
} else if (type.equals(Contents.Type.LOCATION)) { } else if (type.equals(Contents.Type.LOCATION)) {
@ -167,6 +171,7 @@ public final class QRCodeEncoder {
mPixelResolution = pixelResolution; mPixelResolution = pixelResolution;
} }
@Override
public void run() { public void run() {
try { try {
ByteMatrix result = new MultiFormatWriter().encode(mContents, BarcodeFormat.QR_CODE, ByteMatrix result = new MultiFormatWriter().encode(mContents, BarcodeFormat.QR_CODE,

View file

@ -146,7 +146,7 @@ public final class ShareActivity extends Activity {
// Don't require a name to be present, this contact might be just a phone number. // Don't require a name to be present, this contact might be just a phone number.
if (name != null && name.length() > 0) { if (name != null && name.length() > 0) {
bundle.putString(Contacts.Intents.Insert.NAME, name); bundle.putString(Contacts.Intents.Insert.NAME, massageContactData(name));
} }
contactCursor.close(); contactCursor.close();
@ -157,7 +157,7 @@ public final class ShareActivity extends Activity {
while (phonesCursor.moveToNext()) { while (phonesCursor.moveToNext()) {
String number = phonesCursor.getString(PHONES_NUMBER_COLUMN); String number = phonesCursor.getString(PHONES_NUMBER_COLUMN);
if (foundPhone < Contents.PHONE_KEYS.length) { if (foundPhone < Contents.PHONE_KEYS.length) {
bundle.putString(Contents.PHONE_KEYS[foundPhone], number); bundle.putString(Contents.PHONE_KEYS[foundPhone], massageContactData(number));
foundPhone++; foundPhone++;
} }
} }
@ -176,13 +176,13 @@ public final class ShareActivity extends Activity {
switch (kind) { switch (kind) {
case Contacts.KIND_EMAIL: case Contacts.KIND_EMAIL:
if (foundEmail < Contents.EMAIL_KEYS.length) { if (foundEmail < Contents.EMAIL_KEYS.length) {
bundle.putString(Contents.EMAIL_KEYS[foundEmail], data); bundle.putString(Contents.EMAIL_KEYS[foundEmail], massageContactData(data));
foundEmail++; foundEmail++;
} }
break; break;
case Contacts.KIND_POSTAL: case Contacts.KIND_POSTAL:
if (!foundPostal) { if (!foundPostal) {
bundle.putString(Contacts.Intents.Insert.POSTAL, data); bundle.putString(Contacts.Intents.Insert.POSTAL, massageContactData(data));
foundPostal = true; foundPostal = true;
} }
break; break;
@ -198,4 +198,16 @@ public final class ShareActivity extends Activity {
} }
} }
private static String massageContactData(String data) {
// For now -- make sure we don't put newlines in shared contact data. It messes up
// any known encoding of contact data. Replace with space.
if (data.indexOf('\n') >= 0) {
data = data.replace("\n", " ");
}
if (data.indexOf('\r') >= 0) {
data = data.replace("\r", " ");
}
return data;
}
} }