Streamline contact encoding and avoid encoding a line break
authorsrowen <srowen@59b500cc-1b3d-0410-9834-0bbf25fbcc57>
Fri, 1 May 2009 17:25:42 +0000 (17:25 +0000)
committersrowen <srowen@59b500cc-1b3d-0410-9834-0bbf25fbcc57>
Fri, 1 May 2009 17:25:42 +0000 (17:25 +0000)
git-svn-id: http://zxing.googlecode.com/svn/trunk@925 59b500cc-1b3d-0410-9834-0bbf25fbcc57

android/src/com/google/zxing/client/android/QRCodeEncoder.java
android/src/com/google/zxing/client/android/ShareActivity.java

index 4dd1777..1fca976 100755 (executable)
@@ -103,38 +103,42 @@ public final class QRCodeEncoder {
     } else if (type.equals(Contents.Type.CONTACT)) {
       Bundle bundle = intent.getBundleExtra(Intents.Encode.DATA);
       if (bundle != null) {
-        mContents = "MECARD:";
-        mDisplayContents = "";
+        StringBuilder newContents = new StringBuilder();
+        StringBuilder newDisplayContents = new StringBuilder();
+        newContents.append("MECARD:");
         String name = bundle.getString(Contacts.Intents.Insert.NAME);
         if (name != null && name.length() > 0) {
-          mContents = "N:" + name + ';';
-          mDisplayContents += name;
+          newContents.append("N:").append(name).append(';');
+          newDisplayContents.append(name);
         }
         String address = bundle.getString(Contacts.Intents.Insert.POSTAL);
         if (address != null && address.length() > 0) {
-          mContents += "ADR:" + address + ';';
-          mDisplayContents += '\n' + address;
+          newContents.append("ADR:").append(address).append(';');
+          newDisplayContents.append('\n').append(address);
         }
         for (int x = 0; x < Contents.PHONE_KEYS.length; x++) {
           String phone = bundle.getString(Contents.PHONE_KEYS[x]);
           if (phone != null && phone.length() > 0) {
-            mContents += "TEL:" + phone + ';';
-            mDisplayContents += '\n' + PhoneNumberUtils.formatNumber(phone);
+            newContents.append("TEL:").append(phone).append(';');
+            newDisplayContents.append('\n').append(PhoneNumberUtils.formatNumber(phone));
           }
         }
         for (int x = 0; x < Contents.EMAIL_KEYS.length; x++) {
           String email = bundle.getString(Contents.EMAIL_KEYS[x]);
           if (email != null && email.length() > 0) {
-            mContents += "EMAIL:" + email + ';';
-            mDisplayContents += '\n' + email;
+            newContents.append("EMAIL:").append(email).append(';');
+            newDisplayContents.append('\n').append(email);
           }
         }
         // Make sure we've encoded at least one field.
-        if (mDisplayContents.length() > 0) {
-          mContents += ";";
+        if (newDisplayContents.length() > 0) {
+          newContents.append(';');
+          mContents = newContents.toString();
+          mDisplayContents = newDisplayContents.toString();
           mTitle = mActivity.getString(R.string.contents_contact);
         } else {
           mContents = null;
+          mDisplayContents = null;
         }
       }
     } else if (type.equals(Contents.Type.LOCATION)) {
@@ -167,6 +171,7 @@ public final class QRCodeEncoder {
       mPixelResolution = pixelResolution;
     }
 
+    @Override
     public void run() {
       try {
         ByteMatrix result = new MultiFormatWriter().encode(mContents, BarcodeFormat.QR_CODE,
index a0e48ae..70b161e 100755 (executable)
@@ -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.
       if (name != null && name.length() > 0) {
-        bundle.putString(Contacts.Intents.Insert.NAME, name);
+        bundle.putString(Contacts.Intents.Insert.NAME, massageContactData(name));
       }
       contactCursor.close();
 
@@ -157,7 +157,7 @@ public final class ShareActivity extends Activity {
         while (phonesCursor.moveToNext()) {
           String number = phonesCursor.getString(PHONES_NUMBER_COLUMN);
           if (foundPhone < Contents.PHONE_KEYS.length) {
-            bundle.putString(Contents.PHONE_KEYS[foundPhone], number);
+            bundle.putString(Contents.PHONE_KEYS[foundPhone], massageContactData(number));
             foundPhone++;
           }
         }
@@ -176,13 +176,13 @@ public final class ShareActivity extends Activity {
           switch (kind) {
             case Contacts.KIND_EMAIL:
               if (foundEmail < Contents.EMAIL_KEYS.length) {
-                bundle.putString(Contents.EMAIL_KEYS[foundEmail], data);
+                bundle.putString(Contents.EMAIL_KEYS[foundEmail], massageContactData(data));
                 foundEmail++;
               }
               break;
             case Contacts.KIND_POSTAL:
               if (!foundPostal) {
-                bundle.putString(Contacts.Intents.Insert.POSTAL, data);
+                bundle.putString(Contacts.Intents.Insert.POSTAL, massageContactData(data));
                 foundPostal = true;
               }
               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;
+  }
+
 }