]> rtime.felk.cvut.cz Git - hornmich/skoda-qr-demo.git/blob - BarCodeScanner/mobile/src/main/java/cz/cvut/fel/dce/barcodescanner/result/AddressBookResultHandler.java
Initial commit
[hornmich/skoda-qr-demo.git] / BarCodeScanner / mobile / src / main / java / cz / cvut / fel / dce / barcodescanner / result / AddressBookResultHandler.java
1 /*
2  * Copyright (C) 2008 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.result;
18
19 import android.app.Activity;
20 import android.telephony.PhoneNumberUtils;
21 import android.text.Spannable;
22 import android.text.SpannableString;
23 import android.text.style.StyleSpan;
24
25 import cz.cvut.fel.dce.barcodescanner.R;
26 import com.google.zxing.client.result.AddressBookParsedResult;
27 import com.google.zxing.client.result.ParsedResult;
28
29 import java.text.DateFormat;
30 import java.text.ParseException;
31 import java.text.SimpleDateFormat;
32 import java.util.Date;
33 import java.util.Locale;
34
35 /**
36  * Handles address book entries.
37  *
38  * @author dswitkin@google.com (Daniel Switkin)
39  */
40 public final class AddressBookResultHandler extends ResultHandler {
41
42   private static final DateFormat[] DATE_FORMATS = {
43     new SimpleDateFormat("yyyyMMdd", Locale.ENGLISH),
44     new SimpleDateFormat("yyyyMMdd'T'HHmmss", Locale.ENGLISH),
45     new SimpleDateFormat("yyyy-MM-dd", Locale.ENGLISH),
46     new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss", Locale.ENGLISH),
47   };
48   static {
49     for (DateFormat format : DATE_FORMATS) {
50       format.setLenient(false);
51     }
52   }
53
54   private static final int[] BUTTON_TEXTS = {
55     R.string.button_add_contact,
56     R.string.button_show_map,
57     R.string.button_dial,
58     R.string.button_email,
59   };
60
61   private final boolean[] fields;
62   private int buttonCount;
63
64   // This takes all the work out of figuring out which buttons/actions should be in which
65   // positions, based on which fields are present in this barcode.
66   private int mapIndexToAction(int index) {
67     if (index < buttonCount) {
68       int count = -1;
69       for (int x = 0; x < MAX_BUTTON_COUNT; x++) {
70         if (fields[x]) {
71           count++;
72         }
73         if (count == index) {
74           return x;
75         }
76       }
77     }
78     return -1;
79   }
80
81   public AddressBookResultHandler(Activity activity, ParsedResult result) {
82     super(activity, result);
83     AddressBookParsedResult addressResult = (AddressBookParsedResult) result;
84     String[] addresses = addressResult.getAddresses();
85     boolean hasAddress = addresses != null && addresses.length > 0 && addresses[0] != null && !addresses[0].isEmpty();
86     String[] phoneNumbers = addressResult.getPhoneNumbers();
87     boolean hasPhoneNumber = phoneNumbers != null && phoneNumbers.length > 0;
88     String[] emails = addressResult.getEmails();
89     boolean hasEmailAddress = emails != null && emails.length > 0;
90
91     fields = new boolean[MAX_BUTTON_COUNT];
92     fields[0] = true; // Add contact is always available
93     fields[1] = hasAddress;
94     fields[2] = hasPhoneNumber;
95     fields[3] = hasEmailAddress;
96
97     buttonCount = 0;
98     for (int x = 0; x < MAX_BUTTON_COUNT; x++) {
99       if (fields[x]) {
100         buttonCount++;
101       }
102     }
103   }
104
105   @Override
106   public int getButtonCount() {
107     return buttonCount;
108   }
109
110   @Override
111   public int getButtonText(int index) {
112     return BUTTON_TEXTS[mapIndexToAction(index)];
113   }
114
115   @Override
116   public void handleButtonPress(int index) {
117     AddressBookParsedResult addressResult = (AddressBookParsedResult) getResult();
118     String[] addresses = addressResult.getAddresses();
119     String address1 = addresses == null || addresses.length < 1 ? null : addresses[0];
120     String[] addressTypes = addressResult.getAddressTypes();
121     String address1Type = addressTypes == null || addressTypes.length < 1 ? null : addressTypes[0];
122     int action = mapIndexToAction(index);
123     switch (action) {
124       case 0:
125         addContact(addressResult.getNames(),
126                    addressResult.getNicknames(),
127                    addressResult.getPronunciation(),
128                    addressResult.getPhoneNumbers(),
129                    addressResult.getPhoneTypes(),
130                    addressResult.getEmails(),
131                    addressResult.getEmailTypes(),
132                    addressResult.getNote(),
133                    addressResult.getInstantMessenger(),
134                    address1,
135                    address1Type,
136                    addressResult.getOrg(),
137                    addressResult.getTitle(),
138                    addressResult.getURLs(),
139                    addressResult.getBirthday(),
140                    addressResult.getGeo());
141         break;
142       case 1:
143         searchMap(address1);
144         break;
145       case 2:
146         dialPhone(addressResult.getPhoneNumbers()[0]);
147         break;
148       case 3:
149         sendEmail(addressResult.getEmails(), null, null, null, null);
150         break;
151       default:
152         break;
153     }
154   }
155
156   private static Date parseDate(String s) {
157     for (DateFormat currentFormat : DATE_FORMATS) {
158       try {
159         return currentFormat.parse(s);
160       } catch (ParseException e) {
161         // continue
162       }
163     }
164     return null;
165   }
166
167   // Overriden so we can hyphenate phone numbers, format birthdays, and bold the name.
168   @Override
169   public CharSequence getDisplayContents() {
170     AddressBookParsedResult result = (AddressBookParsedResult) getResult();
171     StringBuilder contents = new StringBuilder(100);
172     ParsedResult.maybeAppend(result.getNames(), contents);
173     int namesLength = contents.length();
174
175     String pronunciation = result.getPronunciation();
176     if (pronunciation != null && !pronunciation.isEmpty()) {
177       contents.append("\n(");
178       contents.append(pronunciation);
179       contents.append(')');
180     }
181
182     ParsedResult.maybeAppend(result.getTitle(), contents);
183     ParsedResult.maybeAppend(result.getOrg(), contents);
184     ParsedResult.maybeAppend(result.getAddresses(), contents);
185     String[] numbers = result.getPhoneNumbers();
186     if (numbers != null) {
187       for (String number : numbers) {
188         if (number != null) {
189           ParsedResult.maybeAppend(PhoneNumberUtils.formatNumber(number), contents);
190         }
191       }
192     }
193     ParsedResult.maybeAppend(result.getEmails(), contents);
194     ParsedResult.maybeAppend(result.getURLs(), contents);
195
196     String birthday = result.getBirthday();
197     if (birthday != null && !birthday.isEmpty()) {
198       Date date = parseDate(birthday);
199       if (date != null) {
200         ParsedResult.maybeAppend(DateFormat.getDateInstance(DateFormat.MEDIUM).format(date.getTime()), contents);
201       }
202     }
203     ParsedResult.maybeAppend(result.getNote(), contents);
204
205     if (namesLength > 0) {
206       // Bold the full name to make it stand out a bit.
207       Spannable styled = new SpannableString(contents.toString());
208       styled.setSpan(new StyleSpan(android.graphics.Typeface.BOLD), 0, namesLength, 0);
209       return styled;
210     } else {
211       return contents.toString();
212     }
213   }
214
215   @Override
216   public int getDisplayTitle() {
217     return R.string.result_address_book;
218   }
219 }