More reckless refactoring and code style tweaks -- mostly adding braces around condit...
[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]) {
47           count++;
48         }
49         if (count == index) {
50           return x;
51         }
52       }
53     }
54     return -1;
55   }
56
57   public AddressBookResultHandler(Activity activity, ParsedResult result) {
58     super(activity, result);
59     AddressBookParsedResult addressResult = (AddressBookParsedResult) result;
60     String address = addressResult.getAddress();
61     boolean hasAddress = address != null && address.length() > 0;
62     String[] phoneNumbers = addressResult.getPhoneNumbers();
63     boolean hasPhoneNumber = phoneNumbers != null && phoneNumbers.length > 0;
64     String[] emails = addressResult.getEmails();
65     boolean hasEmailAddress = emails != null && emails.length > 0;
66
67     mFields = new boolean[MAX_BUTTON_COUNT];
68     mFields[0] = true; // Add contact is always available
69     mFields[1] = hasAddress;
70     mFields[2] = hasPhoneNumber;
71     mFields[3] = hasEmailAddress;
72
73     mButtonCount = 0;
74     for (int x = 0; x < MAX_BUTTON_COUNT; x++) {
75       if (mFields[x]) {
76         mButtonCount++;
77       }
78     }
79   }
80
81   @Override
82   public int getButtonCount() {
83     return mButtonCount;
84   }
85
86   @Override
87   public int getButtonText(int index) {
88     int action = mapIndexToAction(index);
89     switch (action) {
90       case 0:
91         return R.string.button_add_contact;
92       case 1:
93         return R.string.button_show_map;
94       case 2:
95         return R.string.button_dial;
96       case 3:
97         return R.string.button_email;
98       default:
99         throw new ArrayIndexOutOfBoundsException();
100     }
101   }
102
103   @Override
104   public void handleButtonPress(int index) {
105     AddressBookParsedResult addressResult = (AddressBookParsedResult) mResult;
106     int action = mapIndexToAction(index);
107     switch (action) {
108       case 0:
109         addContact(addressResult.getNames(), addressResult.getPhoneNumbers(),
110             addressResult.getEmails(), addressResult.getNote(),
111             addressResult.getAddress(), addressResult.getOrg(),
112             addressResult.getTitle());
113         break;
114       case 1:
115         String[] names = addressResult.getNames();
116         String title = names != null ? names[0] : null;
117         searchMap(addressResult.getAddress(), title);
118         break;
119       case 2:
120         dialPhone(addressResult.getPhoneNumbers()[0]);
121         break;
122       case 3:
123         sendEmail(addressResult.getEmails()[0], null, null);
124         break;
125       default:
126         break;
127     }
128   }
129
130   // Overriden so we can hyphenate phone numbers, format birthdays, and bold the name.
131   @Override
132   public CharSequence getDisplayContents() {
133     AddressBookParsedResult result = (AddressBookParsedResult) mResult;
134     StringBuffer contents = new StringBuffer();
135     ParsedResult.maybeAppend(result.getNames(), contents);
136     int namesLength = contents.length();
137
138     String pronunciation = result.getPronunciation();
139     if (pronunciation != null && pronunciation.length() > 0) {
140       contents.append("\n(");
141       contents.append(pronunciation);
142       contents.append(")");
143     }
144
145     ParsedResult.maybeAppend(result.getTitle(), contents);
146     ParsedResult.maybeAppend(result.getOrg(), contents);
147     ParsedResult.maybeAppend(result.getAddress(), contents);
148     String[] numbers = result.getPhoneNumbers();
149     if (numbers != null) {
150       for (String number : numbers) {
151         ParsedResult.maybeAppend(PhoneNumberUtils.formatNumber(number), contents);
152       }
153     }
154     ParsedResult.maybeAppend(result.getEmails(), contents);
155     ParsedResult.maybeAppend(result.getURL(), contents);
156
157     String birthday = result.getBirthday();
158     if (birthday != null && birthday.length() > 0) {
159       Date date;
160       synchronized (DATE_FORMAT) {
161         date = DATE_FORMAT.parse(birthday, new ParsePosition(0));
162       }
163       ParsedResult.maybeAppend(DateFormat.getDateInstance().format(date.getTime()), contents);
164     }
165     ParsedResult.maybeAppend(result.getNote(), contents);
166
167     if (namesLength > 0) {
168       // Bold the full name to make it stand out a bit.
169       Spannable styled = new SpannableString(contents.toString());
170       styled.setSpan(new StyleSpan(android.graphics.Typeface.BOLD), 0, namesLength, 0);
171       return styled;
172     } else {
173       return contents.toString();
174     }
175   }
176
177   @Override
178   public int getDisplayTitle() {
179     return R.string.result_address_book;
180   }
181
182 }