]> rtime.felk.cvut.cz Git - hornmich/skoda-qr-demo.git/blob - BarCodeScanner/mobile/src/main/java/cz/cvut/fel/dce/barcodescanner/encode/VCardTelDisplayFormatter.java
Initial commit
[hornmich/skoda-qr-demo.git] / BarCodeScanner / mobile / src / main / java / cz / cvut / fel / dce / barcodescanner / encode / VCardTelDisplayFormatter.java
1 /*
2  * Copyright (C) 2014 ZXing authors
3  *
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
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 package cz.cvut.fel.dce.barcodescanner.encode;
18
19 import android.telephony.PhoneNumberUtils;
20
21 import java.util.Iterator;
22 import java.util.List;
23 import java.util.Map;
24 import java.util.Set;
25
26 /**
27  * @author Sean Owen
28  */
29 final class VCardTelDisplayFormatter implements Formatter {
30
31   private final List<Map<String,Set<String>>> metadataForIndex;
32
33   VCardTelDisplayFormatter() {
34     this(null);
35   }
36
37   VCardTelDisplayFormatter(List<Map<String,Set<String>>> metadataForIndex) {
38     this.metadataForIndex = metadataForIndex;
39   }
40
41   @Override
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);
47     return value;
48   }
49
50   private static CharSequence formatMetadata(CharSequence value, Map<String,Set<String>> metadata) {
51     if (metadata == null || metadata.isEmpty()) {
52       return value;
53     }
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()) {
58         continue;
59       }
60       Iterator<String> valuesIt = values.iterator();
61       withMetadata.append(valuesIt.next());
62       while (valuesIt.hasNext()) {
63         withMetadata.append(',').append(valuesIt.next());
64       }
65     }
66     if (withMetadata.length() > 0) {
67       withMetadata.append(' ');
68     }
69     withMetadata.append(value);
70     return withMetadata;
71   }
72
73 }