Small speedups in time-related code
[zxing.git] / android / src / com / google / zxing / client / android / 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 com.google.zxing.client.android.result;
18
19 import android.app.Activity;
20 import android.telephony.PhoneNumberUtils;
21 import android.text.SpannableString;
22 import android.text.Spannable;
23 import android.text.style.StyleSpan;
24 import com.google.zxing.client.android.R;
25 import com.google.zxing.client.result.AddressBookParsedResult;
26 import com.google.zxing.client.result.ParsedResult;
27
28 import java.text.DateFormat;
29 import java.text.ParsePosition;
30 import java.text.SimpleDateFormat;
31 import java.util.Date;
32
33 public final class AddressBookResultHandler extends ResultHandler {
34
35   private static final DateFormat DATE_FORMAT = new SimpleDateFormat("yyyyMMdd");
36
37   private final boolean[] mFields;
38   private int mButtonCount;
39
40   // This takes all the work out of figuring out which buttons/actions should be in which
41   // positions, based on which fields are present in this barcode.
42   private int mapIndexToAction(int index) {
43     if (index < mButtonCount) {
44       int count = -1;
45       for (int x = 0; x < MAX_BUTTON_COUNT; x++) {
46         if (mFields[x]) count++;
47         if (count == index) return x;
48       }
49     }
50     return -1;
51   }
52
53   public AddressBookResultHandler(Activity activity, ParsedResult result) {
54     super(activity, result);
55     AddressBookParsedResult addressResult = (AddressBookParsedResult) result;
56     String address = addressResult.getAddress();
57     boolean hasAddress = address != null && address.length() > 0;
58     String[] phoneNumbers = addressResult.getPhoneNumbers();
59     boolean hasPhoneNumber = phoneNumbers != null && phoneNumbers.length > 0;
60     String[] emails = addressResult.getEmails();
61     boolean hasEmailAddress = emails != null && emails.length > 0;
62
63     mFields = new boolean[MAX_BUTTON_COUNT];
64     mFields[0] = true; // Add contact is always available
65     mFields[1] = hasAddress;
66     mFields[2] = hasPhoneNumber;
67     mFields[3] = hasEmailAddress;
68
69     mButtonCount = 0;
70     for (int x = 0; x < MAX_BUTTON_COUNT; x++) {
71       if (mFields[x]) mButtonCount++;
72     }
73   }
74
75   public int getButtonCount() {
76     return mButtonCount;
77   }
78
79   public int getButtonText(int index) {
80     int action = mapIndexToAction(index);
81     switch (action) {
82       case 0:
83         return R.string.button_add_contact;
84       case 1:
85         return R.string.button_show_map;
86       case 2:
87         return R.string.button_dial;
88       case 3:
89         return R.string.button_email;
90       default:
91         throw new ArrayIndexOutOfBoundsException();
92     }
93   }
94
95   public void handleButtonPress(int index) {
96     AddressBookParsedResult addressResult = (AddressBookParsedResult) mResult;
97     int action = mapIndexToAction(index);
98     switch (action) {
99       case 0:
100         addContact(addressResult.getNames(), addressResult.getPhoneNumbers(),
101             addressResult.getEmails(), addressResult.getNote(),
102             addressResult.getAddress(), addressResult.getOrg(),
103             addressResult.getTitle());
104         break;
105       case 1:
106         String[] names = addressResult.getNames();
107         String title = names != null ? names[0] : null;
108         searchMap(addressResult.getAddress(), title);
109         break;
110       case 2:
111         dialPhone(addressResult.getPhoneNumbers()[0]);
112         break;
113       case 3:
114         sendEmail(addressResult.getEmails()[0], null, null);
115         break;
116       default:
117         break;
118     }
119   }
120
121   // Overriden so we can hyphenate phone numbers, format birthdays, and bold the name.
122   @Override
123   public CharSequence getDisplayContents() {
124     AddressBookParsedResult result = (AddressBookParsedResult) mResult;
125     StringBuffer contents = new StringBuffer();
126     ParsedResult.maybeAppend(result.getNames(), contents);
127     int namesLength = contents.length();
128
129     String pronunciation = result.getPronunciation();
130     if (pronunciation != null && pronunciation.length() > 0) {
131       contents.append("\n(");
132       contents.append(pronunciation);
133       contents.append(")");
134     }
135
136     ParsedResult.maybeAppend(result.getTitle(), contents);
137     ParsedResult.maybeAppend(result.getOrg(), contents);
138     ParsedResult.maybeAppend(result.getAddress(), contents);
139     String[] numbers = result.getPhoneNumbers();
140     if (numbers != null) {
141       for (String number : numbers) {
142         ParsedResult.maybeAppend(PhoneNumberUtils.formatNumber(number), contents);
143       }
144     }
145     ParsedResult.maybeAppend(result.getEmails(), contents);
146     ParsedResult.maybeAppend(result.getURL(), contents);
147
148     String birthday = result.getBirthday();
149     if (birthday != null && birthday.length() > 0) {
150       Date date;
151       synchronized (DATE_FORMAT) {
152         date = DATE_FORMAT.parse(birthday, new ParsePosition(0));
153       }
154       ParsedResult.maybeAppend(DateFormat.getDateInstance().format(date.getTime()), contents);
155     }
156     ParsedResult.maybeAppend(result.getNote(), contents);
157
158     if (namesLength > 0) {
159       // Bold the full name to make it stand out a bit.
160       Spannable styled = new SpannableString(contents.toString());
161       styled.setSpan(new StyleSpan(android.graphics.Typeface.BOLD), 0, namesLength, 0);
162       return styled;
163     } else {
164       return contents.toString();
165     }
166   }
167
168   public int getDisplayTitle() {
169     return R.string.result_address_book;
170   }
171
172 }