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