The new Android client, featuring:
[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 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         searchMap(addressResult.getAddress());
104         break;
105       case 2:
106         dialPhone(addressResult.getPhoneNumbers()[0]);
107         break;
108       case 3:
109         sendEmail(addressResult.getEmails()[0], null, null);
110         break;
111       default:
112         break;
113     }
114   }
115
116   // Overriden so we can hyphenate phone numbers, format birthdays, and bold the name.
117   @Override
118   public CharSequence getDisplayContents() {
119     AddressBookParsedResult result = (AddressBookParsedResult) mResult;
120     StringBuffer contents = new StringBuffer();
121     ParsedResult.maybeAppend(result.getNames(), contents);
122     int namesLength = contents.length();
123
124     ParsedResult.maybeAppend(result.getTitle(), contents);
125     ParsedResult.maybeAppend(result.getOrg(), contents);
126     ParsedResult.maybeAppend(result.getAddress(), contents);
127     String[] numbers = result.getPhoneNumbers();
128     if (numbers != null) {
129       for (String number : numbers) {
130         ParsedResult.maybeAppend(PhoneNumberUtils.formatNumber(number), contents);
131       }
132     }
133     ParsedResult.maybeAppend(result.getEmails(), contents);
134     ParsedResult.maybeAppend(result.getURL(), contents);
135
136     String birthday = result.getBirthday();
137     if (birthday != null && birthday.length() > 0) {
138       SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd");
139       Date date = format.parse(birthday, new ParsePosition(0));
140       ParsedResult.maybeAppend(DateFormat.getDateInstance().format(date.getTime()), contents);
141     }
142     ParsedResult.maybeAppend(result.getNote(), contents);
143
144     if (namesLength > 0) {
145       // Bold the full name to make it stand out a bit.
146       SpannableString styled = new SpannableString(contents.toString());
147       styled.setSpan(new StyleSpan(android.graphics.Typeface.BOLD), 0, namesLength, 0);
148       return styled;
149     } else {
150       return contents.toString();
151     }
152   }
153
154   public int getDisplayTitle() {
155     return R.string.result_address_book;
156   }
157
158 }