2 * Copyright (C) 2014 ZXing authors
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 package cz.cvut.fel.dce.barcodescanner.encode;
19 import android.telephony.PhoneNumberUtils;
21 import java.util.Iterator;
22 import java.util.List;
29 final class VCardTelDisplayFormatter implements Formatter {
31 private final List<Map<String,Set<String>>> metadataForIndex;
33 VCardTelDisplayFormatter() {
37 VCardTelDisplayFormatter(List<Map<String,Set<String>>> metadataForIndex) {
38 this.metadataForIndex = metadataForIndex;
42 public CharSequence format(CharSequence value, int index) {
43 value = PhoneNumberUtils.formatNumber(value.toString());
44 Map<String,Set<String>> metadata =
45 metadataForIndex == null || metadataForIndex.size() <= index ? null : metadataForIndex.get(index);
46 value = formatMetadata(value, metadata);
50 private static CharSequence formatMetadata(CharSequence value, Map<String,Set<String>> metadata) {
51 if (metadata == null || metadata.isEmpty()) {
54 StringBuilder withMetadata = new StringBuilder();
55 for (Map.Entry<String,Set<String>> metadatum : metadata.entrySet()) {
56 Set<String> values = metadatum.getValue();
57 if (values == null || values.isEmpty()) {
60 Iterator<String> valuesIt = values.iterator();
61 withMetadata.append(valuesIt.next());
62 while (valuesIt.hasNext()) {
63 withMetadata.append(',').append(valuesIt.next());
66 if (withMetadata.length() > 0) {
67 withMetadata.append(' ');
69 withMetadata.append(value);