blob: cce48dd9277e65bdc0963fbee0401fc025b635e3 [file] [log] [blame]
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -08001/*
2 * Copyright (C) 2009 The Android Open Source Project
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
17package com.android.contacts;
18
Flavio Lerda9a208cc2011-07-12 21:05:47 +010019import com.android.contacts.calllog.CallDetailHistoryAdapter;
20import com.android.contacts.calllog.CallTypeHelper;
Flavio Lerda178eeeb2011-07-11 19:51:40 +010021import com.android.contacts.calllog.PhoneNumberHelper;
Amith Yamasani8bbe2f22009-03-24 21:24:12 -070022
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080023import android.app.ListActivity;
24import android.content.ContentResolver;
25import android.content.ContentUris;
26import android.content.Context;
27import android.content.Intent;
28import android.content.res.Resources;
29import android.database.Cursor;
30import android.net.Uri;
31import android.os.Bundle;
32import android.provider.CallLog;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080033import android.provider.CallLog.Calls;
Flavio Lerda9cafe472011-06-08 14:06:13 +010034import android.provider.Contacts.Intents.Insert;
Dmitri Plotnikovd0d776d2009-08-19 17:38:04 -070035import android.provider.ContactsContract.Contacts;
36import android.provider.ContactsContract.PhoneLookup;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080037import android.telephony.PhoneNumberUtils;
38import android.telephony.TelephonyManager;
39import android.text.TextUtils;
Flavio Lerda178eeeb2011-07-11 19:51:40 +010040import android.util.Log;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080041import android.view.KeyEvent;
42import android.view.LayoutInflater;
43import android.view.View;
44import android.view.ViewGroup;
45import android.widget.AdapterView;
46import android.widget.BaseAdapter;
47import android.widget.ImageView;
Flavio Lerda9a208cc2011-07-12 21:05:47 +010048import android.widget.ListView;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080049import android.widget.TextView;
50import android.widget.Toast;
51
52import java.util.ArrayList;
53import java.util.List;
54
55/**
56 * Displays the details of a specific call log entry.
Flavio Lerda178eeeb2011-07-11 19:51:40 +010057 * <p>
58 * This activity can be either started with the URI of a single call log entry, or with the
59 * {@link #EXTRA_CALL_LOG_IDS} extra to specify a group of call log entries.
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080060 */
61public class CallDetailActivity extends ListActivity implements
62 AdapterView.OnItemClickListener {
63 private static final String TAG = "CallDetail";
64
Flavio Lerda178eeeb2011-07-11 19:51:40 +010065 /** A long array extra containing ids of call log entries to display. */
66 public static final String EXTRA_CALL_LOG_IDS = "com.android.contacts.CALL_LOG_IDS";
67
Flavio Lerdaa024c3f2011-06-10 10:47:07 +010068 /** The views representing the details of a phone call. */
Flavio Lerdaafb93bb2011-07-05 14:46:10 +010069 private PhoneCallDetailsViews mPhoneCallDetailsViews;
Flavio Lerda9a208cc2011-07-12 21:05:47 +010070 private CallTypeHelper mCallTypeHelper;
Flavio Lerda178eeeb2011-07-11 19:51:40 +010071 private PhoneNumberHelper mPhoneNumberHelper;
Flavio Lerdaafb93bb2011-07-05 14:46:10 +010072 private PhoneCallDetailsHelper mPhoneCallDetailsHelper;
Flavio Lerdafb63c432011-07-07 17:16:53 +010073 private View mHomeActionView;
74 private ImageView mMainActionView;
Flavio Lerda9cafe472011-06-08 14:06:13 +010075 private ImageView mContactBackgroundView;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080076
77 private String mNumber = null;
Bai Tao09eb04f2010-09-01 15:34:16 +080078 private String mDefaultCountryIso;
Dmitri Plotnikovd0d776d2009-08-19 17:38:04 -070079
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080080 /* package */ LayoutInflater mInflater;
81 /* package */ Resources mResources;
Flavio Lerda9cafe472011-06-08 14:06:13 +010082 /** Helper to load contact photos. */
83 private ContactPhotoManager mContactPhotoManager;
Dmitri Plotnikovd0d776d2009-08-19 17:38:04 -070084
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080085 static final String[] CALL_LOG_PROJECTION = new String[] {
86 CallLog.Calls.DATE,
87 CallLog.Calls.DURATION,
88 CallLog.Calls.NUMBER,
89 CallLog.Calls.TYPE,
Bai Tao09eb04f2010-09-01 15:34:16 +080090 CallLog.Calls.COUNTRY_ISO,
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080091 };
Dmitri Plotnikovd0d776d2009-08-19 17:38:04 -070092
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080093 static final int DATE_COLUMN_INDEX = 0;
94 static final int DURATION_COLUMN_INDEX = 1;
95 static final int NUMBER_COLUMN_INDEX = 2;
96 static final int CALL_TYPE_COLUMN_INDEX = 3;
Bai Tao09eb04f2010-09-01 15:34:16 +080097 static final int COUNTRY_ISO_COLUMN_INDEX = 4;
Dmitri Plotnikovd0d776d2009-08-19 17:38:04 -070098
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080099 static final String[] PHONES_PROJECTION = new String[] {
Dmitri Plotnikovd0d776d2009-08-19 17:38:04 -0700100 PhoneLookup._ID,
101 PhoneLookup.DISPLAY_NAME,
102 PhoneLookup.TYPE,
103 PhoneLookup.LABEL,
104 PhoneLookup.NUMBER,
Bai Tao09eb04f2010-09-01 15:34:16 +0800105 PhoneLookup.NORMALIZED_NUMBER,
Flavio Lerda9cafe472011-06-08 14:06:13 +0100106 PhoneLookup.PHOTO_ID,
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800107 };
108 static final int COLUMN_INDEX_ID = 0;
109 static final int COLUMN_INDEX_NAME = 1;
110 static final int COLUMN_INDEX_TYPE = 2;
111 static final int COLUMN_INDEX_LABEL = 3;
112 static final int COLUMN_INDEX_NUMBER = 4;
Bai Tao09eb04f2010-09-01 15:34:16 +0800113 static final int COLUMN_INDEX_NORMALIZED_NUMBER = 5;
Flavio Lerda9cafe472011-06-08 14:06:13 +0100114 static final int COLUMN_INDEX_PHOTO_ID = 6;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800115
116 @Override
117 protected void onCreate(Bundle icicle) {
118 super.onCreate(icicle);
119
120 setContentView(R.layout.call_detail);
121
122 mInflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
123 mResources = getResources();
Dmitri Plotnikovd0d776d2009-08-19 17:38:04 -0700124
Flavio Lerda696e8132011-07-05 19:00:23 +0100125 mPhoneCallDetailsViews = PhoneCallDetailsViews.fromView(getWindow().getDecorView());
Flavio Lerda9a208cc2011-07-12 21:05:47 +0100126 mCallTypeHelper = new CallTypeHelper(getResources(),
Flavio Lerda693d1f82011-07-13 18:20:46 +0100127 getResources().getDrawable(R.drawable.ic_call_incoming_holo_dark),
128 getResources().getDrawable(R.drawable.ic_call_outgoing_holo_dark),
129 getResources().getDrawable(R.drawable.ic_call_missed_holo_dark),
130 getResources().getDrawable(R.drawable.ic_call_voicemail_holo_dark));
Flavio Lerda178eeeb2011-07-11 19:51:40 +0100131 mPhoneNumberHelper = new PhoneNumberHelper(mResources, getVoicemailNumber());
132 mPhoneCallDetailsHelper = new PhoneCallDetailsHelper(this, mResources, mCallTypeHelper,
133 mPhoneNumberHelper);
Flavio Lerdafb63c432011-07-07 17:16:53 +0100134 mHomeActionView = findViewById(R.id.action_bar_home);
135 mMainActionView = (ImageView) findViewById(R.id.main_action);
Flavio Lerda9cafe472011-06-08 14:06:13 +0100136 mContactBackgroundView = (ImageView) findViewById(R.id.contact_background);
Bai Tao09eb04f2010-09-01 15:34:16 +0800137 mDefaultCountryIso = ContactsUtils.getCurrentCountryIso(this);
Flavio Lerda9cafe472011-06-08 14:06:13 +0100138 mContactPhotoManager = ContactPhotoManager.getInstance(this);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800139 getListView().setOnItemClickListener(this);
Flavio Lerdafb63c432011-07-07 17:16:53 +0100140 mHomeActionView.setOnClickListener(new View.OnClickListener() {
141 @Override
142 public void onClick(View v) {
143 // We want this to start the call log if this activity was not started from the
144 // call log itself.
145 CallDetailActivity.this.finish();
146 }
147 });
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800148 }
Dmitri Plotnikovd0d776d2009-08-19 17:38:04 -0700149
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800150 @Override
151 public void onResume() {
152 super.onResume();
Flavio Lerda178eeeb2011-07-11 19:51:40 +0100153 updateData(getCallLogEntryUris());
154 }
155
156 /**
157 * Returns the list of URIs to show.
158 * <p>
159 * There are two ways the URIs can be provided to the activity: as the data on the intent, or as
160 * a list of ids in the call log added as an extra on the URI.
161 * <p>
162 * If both are available, the data on the intent takes precedence.
163 */
164 private Uri[] getCallLogEntryUris() {
165 Uri uri = getIntent().getData();
166 if (uri != null) {
167 // If there is a data on the intent, it takes precedence over the extra.
168 return new Uri[]{ uri };
169 }
170 long[] ids = getIntent().getLongArrayExtra(EXTRA_CALL_LOG_IDS);
171 Uri[] uris = new Uri[ids.length];
172 for (int index = 0; index < ids.length; ++index) {
173 uris[index] = ContentUris.withAppendedId(Calls.CONTENT_URI_WITH_VOICEMAIL, ids[index]);
174 }
175 return uris;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800176 }
177
178 @Override
179 public boolean onKeyDown(int keyCode, KeyEvent event) {
180 switch (keyCode) {
181 case KeyEvent.KEYCODE_CALL: {
182 // Make sure phone isn't already busy before starting direct call
183 TelephonyManager tm = (TelephonyManager)
184 getSystemService(Context.TELEPHONY_SERVICE);
185 if (tm.getCallState() == TelephonyManager.CALL_STATE_IDLE) {
186 Intent callIntent = new Intent(Intent.ACTION_CALL_PRIVILEGED,
187 Uri.fromParts("tel", mNumber, null));
188 startActivity(callIntent);
189 return true;
190 }
191 }
192 }
Dmitri Plotnikovd0d776d2009-08-19 17:38:04 -0700193
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800194 return super.onKeyDown(keyCode, event);
195 }
Dmitri Plotnikovd0d776d2009-08-19 17:38:04 -0700196
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800197 /**
198 * Update user interface with details of given call.
Dmitri Plotnikovd0d776d2009-08-19 17:38:04 -0700199 *
Flavio Lerda178eeeb2011-07-11 19:51:40 +0100200 * @param callUris URIs into {@link CallLog.Calls} of the calls to be displayed
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800201 */
Flavio Lerda178eeeb2011-07-11 19:51:40 +0100202 private void updateData(final Uri... callUris) {
203 // TODO: All phone calls correspond to the same person, so we can make a single lookup.
204 final int numCalls = callUris.length;
205 final PhoneCallDetails[] details = new PhoneCallDetails[numCalls];
206 try {
207 for (int index = 0; index < numCalls; ++index) {
208 details[index] = getPhoneCallDetailsForUri(callUris[index]);
209 }
210 } catch (IllegalArgumentException e) {
211 // Something went wrong reading in our primary data, so we're going to
212 // bail out and show error to users.
213 Log.w(TAG, "invalid URI starting call details", e);
214 Toast.makeText(this, R.string.toast_call_detail_error,
215 Toast.LENGTH_SHORT).show();
216 finish();
217 return;
218 }
219
220 // We know that all calls are from the same number and the same contact, so pick the first.
221 mNumber = details[0].number.toString();
222 final long personId = details[0].personId;
223 final long photoId = details[0].photoId;
224
225 // Set the details header, based on the first phone call.
226 mPhoneCallDetailsHelper.setPhoneCallDetails(mPhoneCallDetailsViews,
Flavio Lerda76921902011-07-13 21:11:51 +0100227 details[0], false, false);
Flavio Lerda178eeeb2011-07-11 19:51:40 +0100228
Flavio Lerdacfff16d2011-07-12 23:54:40 +0100229 // Cache the details about the phone number.
230 final Uri numberCallUri = mPhoneNumberHelper.getCallUri(mNumber);
231 final boolean canPlaceCallsTo = mPhoneNumberHelper.canPlaceCallsTo(mNumber);
232 final boolean isVoicemailNumber = mPhoneNumberHelper.isVoicemailNumber(mNumber);
233 final boolean isSipNumber = mPhoneNumberHelper.isSipNumber(mNumber);
234
Flavio Lerda178eeeb2011-07-11 19:51:40 +0100235 // Let user view contact details if they exist, otherwise add option to create new contact
236 // from this number.
237 final Intent mainActionIntent;
238 final int mainActionIcon;
239
240 if (details[0].personId != -1) {
241 Uri personUri = ContentUris.withAppendedId(Contacts.CONTENT_URI, personId);
242 mainActionIntent = new Intent(Intent.ACTION_VIEW, personUri);
243 mainActionIcon = R.drawable.sym_action_view_contact;
Flavio Lerdacfff16d2011-07-12 23:54:40 +0100244 } else if (isVoicemailNumber) {
245 mainActionIntent = null;
246 mainActionIcon = 0;
247 } else if (isSipNumber) {
248 // TODO: This item is currently disabled for SIP addresses, because
249 // the Insert.PHONE extra only works correctly for PSTN numbers.
250 //
251 // To fix this for SIP addresses, we need to:
252 // - define ContactsContract.Intents.Insert.SIP_ADDRESS, and use it here if
253 // the current number is a SIP address
254 // - update the contacts UI code to handle Insert.SIP_ADDRESS by
255 // updating the SipAddress field
256 // and then we can remove the "!isSipNumber" check above.
257 mainActionIntent = null;
258 mainActionIcon = 0;
Flavio Lerda178eeeb2011-07-11 19:51:40 +0100259 } else {
260 mainActionIntent = new Intent(Intent.ACTION_INSERT_OR_EDIT);
261 mainActionIntent.setType(Contacts.CONTENT_ITEM_TYPE);
262 mainActionIntent.putExtra(Insert.PHONE, mNumber);
263 mainActionIcon = R.drawable.sym_action_add;
264 }
265
Flavio Lerdacfff16d2011-07-12 23:54:40 +0100266 if (mainActionIntent == null) {
267 mMainActionView.setVisibility(View.INVISIBLE);
268 } else {
269 mMainActionView.setVisibility(View.VISIBLE);
270 mMainActionView.setImageResource(mainActionIcon);
271 mMainActionView.setOnClickListener(new View.OnClickListener() {
272 @Override
273 public void onClick(View v) {
274 startActivity(mainActionIntent);
275 }
276 });
277 }
Flavio Lerda178eeeb2011-07-11 19:51:40 +0100278
279 // Build list of various available actions.
280 final List<ViewEntry> actions = new ArrayList<ViewEntry>();
281
Flavio Lerda178eeeb2011-07-11 19:51:40 +0100282 // This action allows to call the number that places the call.
Flavio Lerdacfff16d2011-07-12 23:54:40 +0100283 if (canPlaceCallsTo) {
Flavio Lerda178eeeb2011-07-11 19:51:40 +0100284 actions.add(new ViewEntry(android.R.drawable.sym_action_call,
285 getString(R.string.menu_callNumber, mNumber),
286 new Intent(Intent.ACTION_CALL_PRIVILEGED, numberCallUri)));
287 }
288
Flavio Lerdacfff16d2011-07-12 23:54:40 +0100289 // This action allows to send an SMS to the number that placed the call.
290 if (mPhoneNumberHelper.canSendSmsTo(mNumber)) {
Flavio Lerda178eeeb2011-07-11 19:51:40 +0100291 Intent smsIntent = new Intent(Intent.ACTION_SENDTO,
292 Uri.fromParts("sms", mNumber, null));
293 actions.add(new ViewEntry(R.drawable.sym_action_sms,
294 getString(R.string.menu_sendTextMessage), smsIntent));
295 }
296
297 // This action deletes all elements in the group from the call log.
298 actions.add(new ViewEntry(android.R.drawable.ic_menu_close_clear_cancel,
299 getString(R.string.recentCalls_removeFromRecentList),
300 new View.OnClickListener() {
301 @Override
302 public void onClick(View v) {
303 StringBuilder callIds = new StringBuilder();
304 for (Uri callUri : callUris) {
305 if (callIds.length() != 0) {
306 callIds.append(",");
307 }
308 callIds.append(ContentUris.parseId(callUri));
309 }
310
311 getContentResolver().delete(Calls.CONTENT_URI_WITH_VOICEMAIL,
312 Calls._ID + " IN (" + callIds + ")", null);
313 finish();
314 }
315 }));
316
Flavio Lerdacfff16d2011-07-12 23:54:40 +0100317 if (canPlaceCallsTo && !isSipNumber && !isVoicemailNumber) {
Flavio Lerda178eeeb2011-07-11 19:51:40 +0100318 // "Edit the number before calling" is only available for PSTN numbers.
319 actions.add(new ViewEntry(android.R.drawable.sym_action_call,
320 getString(R.string.recentCalls_editNumberBeforeCall),
321 new Intent(Intent.ACTION_DIAL, numberCallUri)));
322 }
323
324 // Set the actions for this phone number.
325 setListAdapter(new ViewAdapter(this, actions));
326
327 ListView historyList = (ListView) findViewById(R.id.history);
328 historyList.setAdapter(
329 new CallDetailHistoryAdapter(this, mInflater, mCallTypeHelper, details));
330 loadContactPhotos(photoId);
331 }
332
333 /** Return the phone call details for a given call log URI. */
334 private PhoneCallDetails getPhoneCallDetailsForUri(Uri callUri) {
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800335 ContentResolver resolver = getContentResolver();
336 Cursor callCursor = resolver.query(callUri, CALL_LOG_PROJECTION, null, null, null);
337 try {
Flavio Lerda178eeeb2011-07-11 19:51:40 +0100338 if (callCursor == null || !callCursor.moveToFirst()) {
339 throw new IllegalArgumentException("Cannot find content: " + callUri);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800340 }
Flavio Lerda178eeeb2011-07-11 19:51:40 +0100341
342 // Read call log specifics.
343 String number = callCursor.getString(NUMBER_COLUMN_INDEX);
344 long date = callCursor.getLong(DATE_COLUMN_INDEX);
345 long duration = callCursor.getLong(DURATION_COLUMN_INDEX);
346 int callType = callCursor.getInt(CALL_TYPE_COLUMN_INDEX);
347 String countryIso = callCursor.getString(COUNTRY_ISO_COLUMN_INDEX);
348 if (TextUtils.isEmpty(countryIso)) {
349 countryIso = mDefaultCountryIso;
350 }
351
352 // Formatted phone number.
353 final CharSequence numberText;
354 // Read contact specifics.
355 CharSequence nameText = "";
356 int numberType = 0;
357 CharSequence numberLabel = "";
358 long personId = -1L;
359 long photoId = 0L;
360 // If this is not a regular number, there is no point in looking it up in the contacts.
361 if (!mPhoneNumberHelper.canPlaceCallsTo(number)) {
362 numberText = mPhoneNumberHelper.getDisplayNumber(number, null);
363 } else {
364 // Perform a reverse-phonebook lookup to find the contact details.
365 Uri phoneUri = Uri.withAppendedPath(PhoneLookup.CONTENT_FILTER_URI,
366 Uri.encode(number));
367 Cursor phonesCursor = resolver.query(phoneUri, PHONES_PROJECTION, null, null, null);
368 String candidateNumberText = number;
369 try {
370 if (phonesCursor != null && phonesCursor.moveToFirst()) {
371 personId = phonesCursor.getLong(COLUMN_INDEX_ID);
372 nameText = phonesCursor.getString(COLUMN_INDEX_NAME);
373 photoId = phonesCursor.getLong(COLUMN_INDEX_PHOTO_ID);
374 candidateNumberText = PhoneNumberUtils.formatNumber(
375 phonesCursor.getString(COLUMN_INDEX_NUMBER),
376 phonesCursor.getString(COLUMN_INDEX_NORMALIZED_NUMBER),
377 countryIso);
378 numberType = phonesCursor.getInt(COLUMN_INDEX_TYPE);
379 numberLabel = phonesCursor.getString(COLUMN_INDEX_LABEL);
380 } else {
381 // We could not find this contact in the contacts, just format the phone
382 // number as best as we can. All the other fields will have their default
383 // values.
384 candidateNumberText =
385 PhoneNumberUtils.formatNumber(number, countryIso);
386 }
387 } finally {
388 if (phonesCursor != null) phonesCursor.close();
389 numberText = candidateNumberText;
390 }
391 }
392 return new PhoneCallDetails(number, numberText, new int[]{ callType }, date, duration,
393 nameText, numberType, numberLabel, personId, photoId);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800394 } finally {
395 if (callCursor != null) {
396 callCursor.close();
397 }
398 }
399 }
400
Flavio Lerda9cafe472011-06-08 14:06:13 +0100401 /** Load the contact photos and places them in the corresponding views. */
402 private void loadContactPhotos(final long photoId) {
Flavio Lerdafb63c432011-07-07 17:16:53 +0100403 mContactPhotoManager.loadPhoto(mContactBackgroundView, photoId);
Flavio Lerda9cafe472011-06-08 14:06:13 +0100404 }
405
Flavio Lerda696e8132011-07-05 19:00:23 +0100406 private String getVoicemailNumber() {
407 TelephonyManager telephonyManager =
408 (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
409 return telephonyManager.getVoiceMailNumber();
410 }
411
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800412 static final class ViewEntry {
Flavio Lerdaa8956882011-07-09 21:34:38 +0100413 public final int icon;
414 public final String text;
415 public final Intent intent;
416 public final View.OnClickListener action;
417
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800418 public String label = null;
419 public String number = null;
Dmitri Plotnikovd0d776d2009-08-19 17:38:04 -0700420
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800421 public ViewEntry(int icon, String text, Intent intent) {
422 this.icon = icon;
423 this.text = text;
424 this.intent = intent;
Flavio Lerdaa8956882011-07-09 21:34:38 +0100425 this.action = null;
426 }
427
428 public ViewEntry(int icon, String text, View.OnClickListener listener) {
429 this.icon = icon;
430 this.text = text;
431 this.intent = null;
432 this.action = listener;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800433 }
434 }
435
436 static final class ViewAdapter extends BaseAdapter {
Dmitri Plotnikovd0d776d2009-08-19 17:38:04 -0700437
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800438 private final List<ViewEntry> mActions;
Dmitri Plotnikovd0d776d2009-08-19 17:38:04 -0700439
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800440 private final LayoutInflater mInflater;
Dmitri Plotnikovd0d776d2009-08-19 17:38:04 -0700441
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800442 public ViewAdapter(Context context, List<ViewEntry> actions) {
443 mActions = actions;
444 mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
445 }
Dmitri Plotnikovd0d776d2009-08-19 17:38:04 -0700446
Flavio Lerda9cafe472011-06-08 14:06:13 +0100447 @Override
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800448 public int getCount() {
449 return mActions.size();
450 }
451
Flavio Lerda9cafe472011-06-08 14:06:13 +0100452 @Override
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800453 public Object getItem(int position) {
454 return mActions.get(position);
455 }
456
Flavio Lerda9cafe472011-06-08 14:06:13 +0100457 @Override
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800458 public long getItemId(int position) {
459 return position;
460 }
Dmitri Plotnikovd0d776d2009-08-19 17:38:04 -0700461
Flavio Lerda9cafe472011-06-08 14:06:13 +0100462 @Override
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800463 public View getView(int position, View convertView, ViewGroup parent) {
464 // Make sure we have a valid convertView to start with
465 if (convertView == null) {
466 convertView = mInflater.inflate(R.layout.call_detail_list_item, parent, false);
467 }
468
469 // Fill action with icon and text.
470 ViewEntry entry = mActions.get(position);
471 convertView.setTag(entry);
Dmitri Plotnikovd0d776d2009-08-19 17:38:04 -0700472
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800473 ImageView icon = (ImageView) convertView.findViewById(R.id.icon);
474 TextView text = (TextView) convertView.findViewById(android.R.id.text1);
475
476 icon.setImageResource(entry.icon);
477 text.setText(entry.text);
478
479 View line2 = convertView.findViewById(R.id.line2);
480 boolean numberEmpty = TextUtils.isEmpty(entry.number);
481 boolean labelEmpty = TextUtils.isEmpty(entry.label) || numberEmpty;
482 if (labelEmpty && numberEmpty) {
483 line2.setVisibility(View.GONE);
484 } else {
485 line2.setVisibility(View.VISIBLE);
Dmitri Plotnikovd0d776d2009-08-19 17:38:04 -0700486
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800487 TextView label = (TextView) convertView.findViewById(R.id.label);
488 if (labelEmpty) {
489 label.setVisibility(View.GONE);
490 } else {
491 label.setText(entry.label);
492 label.setVisibility(View.VISIBLE);
493 }
494
495 TextView number = (TextView) convertView.findViewById(R.id.number);
496 number.setText(entry.number);
497 }
Dmitri Plotnikovd0d776d2009-08-19 17:38:04 -0700498
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800499 return convertView;
500 }
501 }
Dmitri Plotnikovd0d776d2009-08-19 17:38:04 -0700502
Flavio Lerda9cafe472011-06-08 14:06:13 +0100503 @Override
504 public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800505 // Handle passing action off to correct handler.
506 if (view.getTag() instanceof ViewEntry) {
507 ViewEntry entry = (ViewEntry) view.getTag();
508 if (entry.intent != null) {
509 startActivity(entry.intent);
Flavio Lerdaa8956882011-07-09 21:34:38 +0100510 } else if (entry.action != null) {
511 entry.action.onClick(view);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800512 }
513 }
Dmitri Plotnikovd0d776d2009-08-19 17:38:04 -0700514 }
Dmitri Plotnikov8e86b752010-02-22 17:47:57 -0800515
516 @Override
517 public void startSearch(String initialQuery, boolean selectInitialQuery, Bundle appSearchData,
518 boolean globalSearch) {
519 if (globalSearch) {
520 super.startSearch(initialQuery, selectInitialQuery, appSearchData, globalSearch);
521 } else {
522 ContactsSearchManager.startSearch(this, initialQuery);
523 }
524 }
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800525}