blob: 68c9f61bc95a6ccc3e171b7a8ebc371597682bb7 [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;
Hugo Hudsonb002f512011-07-15 17:41:12 +010022import com.android.contacts.voicemail.VoicemailPlaybackFragment;
Debashish Chatterjee0cb82242011-07-19 19:29:37 +010023import com.android.contacts.voicemail.VoicemailStatusHelper;
24import com.android.contacts.voicemail.VoicemailStatusHelper.StatusMessage;
25import com.android.contacts.voicemail.VoicemailStatusHelperImpl;
Amith Yamasani8bbe2f22009-03-24 21:24:12 -070026
Hugo Hudsonb002f512011-07-15 17:41:12 +010027import android.app.FragmentManager;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080028import android.app.ListActivity;
29import android.content.ContentResolver;
30import android.content.ContentUris;
31import android.content.Context;
32import android.content.Intent;
33import android.content.res.Resources;
34import android.database.Cursor;
35import android.net.Uri;
36import android.os.Bundle;
37import android.provider.CallLog;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080038import android.provider.CallLog.Calls;
Flavio Lerda9cafe472011-06-08 14:06:13 +010039import android.provider.Contacts.Intents.Insert;
Dmitri Plotnikovd0d776d2009-08-19 17:38:04 -070040import android.provider.ContactsContract.Contacts;
41import android.provider.ContactsContract.PhoneLookup;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080042import android.telephony.PhoneNumberUtils;
43import android.telephony.TelephonyManager;
44import android.text.TextUtils;
Flavio Lerda178eeeb2011-07-11 19:51:40 +010045import android.util.Log;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080046import android.view.KeyEvent;
47import android.view.LayoutInflater;
Flavio Lerda94d7bab2011-07-22 16:52:34 +010048import android.view.Menu;
49import android.view.MenuItem;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080050import android.view.View;
51import android.view.ViewGroup;
52import android.widget.AdapterView;
53import android.widget.BaseAdapter;
54import android.widget.ImageView;
Flavio Lerda9a208cc2011-07-12 21:05:47 +010055import android.widget.ListView;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080056import android.widget.TextView;
57import android.widget.Toast;
58
59import java.util.ArrayList;
60import java.util.List;
61
62/**
63 * Displays the details of a specific call log entry.
Flavio Lerda178eeeb2011-07-11 19:51:40 +010064 * <p>
65 * This activity can be either started with the URI of a single call log entry, or with the
66 * {@link #EXTRA_CALL_LOG_IDS} extra to specify a group of call log entries.
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080067 */
68public class CallDetailActivity extends ListActivity implements
69 AdapterView.OnItemClickListener {
70 private static final String TAG = "CallDetail";
71
Flavio Lerda178eeeb2011-07-11 19:51:40 +010072 /** A long array extra containing ids of call log entries to display. */
Hugo Hudsonb002f512011-07-15 17:41:12 +010073 public static final String EXTRA_CALL_LOG_IDS = "EXTRA_CALL_LOG_IDS";
74 /** If we are started with a voicemail, we'll find the uri to play with this extra. */
75 public static final String EXTRA_VOICEMAIL_URI = "EXTRA_VOICEMAIL_URI";
76 /** If we should immediately start playback of the voicemail, this extra will be set to true. */
77 public static final String EXTRA_VOICEMAIL_START_PLAYBACK = "EXTRA_VOICEMAIL_START_PLAYBACK";
Flavio Lerda178eeeb2011-07-11 19:51:40 +010078
Flavio Lerdaa024c3f2011-06-10 10:47:07 +010079 /** The views representing the details of a phone call. */
Flavio Lerdaafb93bb2011-07-05 14:46:10 +010080 private PhoneCallDetailsViews mPhoneCallDetailsViews;
Flavio Lerda9a208cc2011-07-12 21:05:47 +010081 private CallTypeHelper mCallTypeHelper;
Flavio Lerda178eeeb2011-07-11 19:51:40 +010082 private PhoneNumberHelper mPhoneNumberHelper;
Flavio Lerdaafb93bb2011-07-05 14:46:10 +010083 private PhoneCallDetailsHelper mPhoneCallDetailsHelper;
Flavio Lerdafb63c432011-07-07 17:16:53 +010084 private View mHomeActionView;
85 private ImageView mMainActionView;
Flavio Lerda9cafe472011-06-08 14:06:13 +010086 private ImageView mContactBackgroundView;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080087
88 private String mNumber = null;
Bai Tao09eb04f2010-09-01 15:34:16 +080089 private String mDefaultCountryIso;
Dmitri Plotnikovd0d776d2009-08-19 17:38:04 -070090
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080091 /* package */ LayoutInflater mInflater;
92 /* package */ Resources mResources;
Flavio Lerda9cafe472011-06-08 14:06:13 +010093 /** Helper to load contact photos. */
94 private ContactPhotoManager mContactPhotoManager;
Debashish Chatterjee0cb82242011-07-19 19:29:37 +010095 /** Helper to make async queries to content resolver. */
96 private CallDetailActivityQueryHandler mAsyncQueryHandler;
97 /** Helper to get voicemail status messages. */
98 private VoicemailStatusHelper mVoicemailStatusHelper;
99 // Views related to voicemail status message.
100 private View mStatusMessageView;
101 private TextView mStatusMessageText;
102 private TextView mStatusMessageAction;
Dmitri Plotnikovd0d776d2009-08-19 17:38:04 -0700103
Flavio Lerda94d7bab2011-07-22 16:52:34 +0100104 /** Whether we should show "remove from call log" in the options menu. */
105 private boolean mHasRemoveFromCallLog;
106 /** Whether we should show "edit number before call" in the options menu. */
107 private boolean mHasEditNumberBeforeCall;
108
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800109 static final String[] CALL_LOG_PROJECTION = new String[] {
110 CallLog.Calls.DATE,
111 CallLog.Calls.DURATION,
112 CallLog.Calls.NUMBER,
113 CallLog.Calls.TYPE,
Bai Tao09eb04f2010-09-01 15:34:16 +0800114 CallLog.Calls.COUNTRY_ISO,
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800115 };
Dmitri Plotnikovd0d776d2009-08-19 17:38:04 -0700116
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800117 static final int DATE_COLUMN_INDEX = 0;
118 static final int DURATION_COLUMN_INDEX = 1;
119 static final int NUMBER_COLUMN_INDEX = 2;
120 static final int CALL_TYPE_COLUMN_INDEX = 3;
Bai Tao09eb04f2010-09-01 15:34:16 +0800121 static final int COUNTRY_ISO_COLUMN_INDEX = 4;
Dmitri Plotnikovd0d776d2009-08-19 17:38:04 -0700122
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800123 static final String[] PHONES_PROJECTION = new String[] {
Dmitri Plotnikovd0d776d2009-08-19 17:38:04 -0700124 PhoneLookup._ID,
125 PhoneLookup.DISPLAY_NAME,
126 PhoneLookup.TYPE,
127 PhoneLookup.LABEL,
128 PhoneLookup.NUMBER,
Bai Tao09eb04f2010-09-01 15:34:16 +0800129 PhoneLookup.NORMALIZED_NUMBER,
Daniel Lehmann362654a2011-07-17 14:16:47 -0700130 PhoneLookup.PHOTO_URI,
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800131 };
132 static final int COLUMN_INDEX_ID = 0;
133 static final int COLUMN_INDEX_NAME = 1;
134 static final int COLUMN_INDEX_TYPE = 2;
135 static final int COLUMN_INDEX_LABEL = 3;
136 static final int COLUMN_INDEX_NUMBER = 4;
Bai Tao09eb04f2010-09-01 15:34:16 +0800137 static final int COLUMN_INDEX_NORMALIZED_NUMBER = 5;
Daniel Lehmann362654a2011-07-17 14:16:47 -0700138 static final int COLUMN_INDEX_PHOTO_URI = 6;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800139
140 @Override
141 protected void onCreate(Bundle icicle) {
142 super.onCreate(icicle);
143
144 setContentView(R.layout.call_detail);
145
146 mInflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
147 mResources = getResources();
Dmitri Plotnikovd0d776d2009-08-19 17:38:04 -0700148
Flavio Lerda696e8132011-07-05 19:00:23 +0100149 mPhoneCallDetailsViews = PhoneCallDetailsViews.fromView(getWindow().getDecorView());
Flavio Lerda9a208cc2011-07-12 21:05:47 +0100150 mCallTypeHelper = new CallTypeHelper(getResources(),
Flavio Lerda693d1f82011-07-13 18:20:46 +0100151 getResources().getDrawable(R.drawable.ic_call_incoming_holo_dark),
152 getResources().getDrawable(R.drawable.ic_call_outgoing_holo_dark),
153 getResources().getDrawable(R.drawable.ic_call_missed_holo_dark),
154 getResources().getDrawable(R.drawable.ic_call_voicemail_holo_dark));
Flavio Lerda178eeeb2011-07-11 19:51:40 +0100155 mPhoneNumberHelper = new PhoneNumberHelper(mResources, getVoicemailNumber());
156 mPhoneCallDetailsHelper = new PhoneCallDetailsHelper(this, mResources, mCallTypeHelper,
157 mPhoneNumberHelper);
Debashish Chatterjee0cb82242011-07-19 19:29:37 +0100158 mVoicemailStatusHelper = new VoicemailStatusHelperImpl();
159 mAsyncQueryHandler = new CallDetailActivityQueryHandler(this);
160 mStatusMessageView = findViewById(R.id.voicemail_status);
161 mStatusMessageText = (TextView) findViewById(R.id.voicemail_status_message);
162 mStatusMessageAction = (TextView) findViewById(R.id.voicemail_status_action);
Flavio Lerdafb63c432011-07-07 17:16:53 +0100163 mHomeActionView = findViewById(R.id.action_bar_home);
164 mMainActionView = (ImageView) findViewById(R.id.main_action);
Flavio Lerda9cafe472011-06-08 14:06:13 +0100165 mContactBackgroundView = (ImageView) findViewById(R.id.contact_background);
Bai Tao09eb04f2010-09-01 15:34:16 +0800166 mDefaultCountryIso = ContactsUtils.getCurrentCountryIso(this);
Flavio Lerda9cafe472011-06-08 14:06:13 +0100167 mContactPhotoManager = ContactPhotoManager.getInstance(this);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800168 getListView().setOnItemClickListener(this);
Flavio Lerdafb63c432011-07-07 17:16:53 +0100169 mHomeActionView.setOnClickListener(new View.OnClickListener() {
170 @Override
171 public void onClick(View v) {
172 // We want this to start the call log if this activity was not started from the
173 // call log itself.
174 CallDetailActivity.this.finish();
175 }
176 });
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800177 }
Dmitri Plotnikovd0d776d2009-08-19 17:38:04 -0700178
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800179 @Override
180 public void onResume() {
181 super.onResume();
Flavio Lerda178eeeb2011-07-11 19:51:40 +0100182 updateData(getCallLogEntryUris());
Hugo Hudson757aa552011-07-20 01:15:25 +0100183 optionallyHandleVoicemail();
Hugo Hudsonb002f512011-07-15 17:41:12 +0100184 }
185
186 /**
187 * Handle voicemail playback or hide voicemail ui.
188 * <p>
189 * If the Intent used to start this Activity contains the suitable extras, then start voicemail
190 * playback. If it doesn't, then hide the voicemail ui.
191 */
Hugo Hudson757aa552011-07-20 01:15:25 +0100192 private void optionallyHandleVoicemail() {
Hugo Hudson157dde12011-07-21 23:28:13 +0100193 if (hasVoicemail()) {
194 // Has voicemail: leave the fragment visible. Optionally start the playback.
Hugo Hudson757aa552011-07-20 01:15:25 +0100195 // Do a query to fetch the voicemail status messages.
196 boolean startPlayback = getIntent().getBooleanExtra(
Hugo Hudsonb002f512011-07-15 17:41:12 +0100197 EXTRA_VOICEMAIL_START_PLAYBACK, false);
Hugo Hudson157dde12011-07-21 23:28:13 +0100198 Uri voicemailUri = getIntent().getParcelableExtra(EXTRA_VOICEMAIL_URI);
199 getVoicemailPlaybackFragment().setVoicemailUri(voicemailUri, startPlayback);
Hugo Hudson757aa552011-07-20 01:15:25 +0100200 mAsyncQueryHandler.startVoicemailStatusQuery(voicemailUri);
201 } else {
202 // No voicemail uri: hide the voicemail fragment and the status view.
Hugo Hudson157dde12011-07-21 23:28:13 +0100203 getFragmentManager().beginTransaction().hide(getVoicemailPlaybackFragment()).commit();
Hugo Hudson757aa552011-07-20 01:15:25 +0100204 mStatusMessageView.setVisibility(View.GONE);
Hugo Hudsonb002f512011-07-15 17:41:12 +0100205 }
Flavio Lerda178eeeb2011-07-11 19:51:40 +0100206 }
207
Hugo Hudson157dde12011-07-21 23:28:13 +0100208 private boolean hasVoicemail() {
209 return getIntent().getParcelableExtra(EXTRA_VOICEMAIL_URI) != null;
210 }
211
212 private VoicemailPlaybackFragment getVoicemailPlaybackFragment() {
213 FragmentManager manager = getFragmentManager();
214 return (VoicemailPlaybackFragment) manager.findFragmentById(
215 R.id.voicemail_playback_fragment);
216 }
217
Flavio Lerda178eeeb2011-07-11 19:51:40 +0100218 /**
219 * Returns the list of URIs to show.
220 * <p>
221 * There are two ways the URIs can be provided to the activity: as the data on the intent, or as
222 * a list of ids in the call log added as an extra on the URI.
223 * <p>
224 * If both are available, the data on the intent takes precedence.
225 */
226 private Uri[] getCallLogEntryUris() {
227 Uri uri = getIntent().getData();
228 if (uri != null) {
229 // If there is a data on the intent, it takes precedence over the extra.
230 return new Uri[]{ uri };
231 }
232 long[] ids = getIntent().getLongArrayExtra(EXTRA_CALL_LOG_IDS);
233 Uri[] uris = new Uri[ids.length];
234 for (int index = 0; index < ids.length; ++index) {
235 uris[index] = ContentUris.withAppendedId(Calls.CONTENT_URI_WITH_VOICEMAIL, ids[index]);
236 }
237 return uris;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800238 }
239
240 @Override
241 public boolean onKeyDown(int keyCode, KeyEvent event) {
242 switch (keyCode) {
243 case KeyEvent.KEYCODE_CALL: {
244 // Make sure phone isn't already busy before starting direct call
245 TelephonyManager tm = (TelephonyManager)
246 getSystemService(Context.TELEPHONY_SERVICE);
247 if (tm.getCallState() == TelephonyManager.CALL_STATE_IDLE) {
248 Intent callIntent = new Intent(Intent.ACTION_CALL_PRIVILEGED,
249 Uri.fromParts("tel", mNumber, null));
250 startActivity(callIntent);
251 return true;
252 }
253 }
254 }
Dmitri Plotnikovd0d776d2009-08-19 17:38:04 -0700255
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800256 return super.onKeyDown(keyCode, event);
257 }
Dmitri Plotnikovd0d776d2009-08-19 17:38:04 -0700258
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800259 /**
260 * Update user interface with details of given call.
Dmitri Plotnikovd0d776d2009-08-19 17:38:04 -0700261 *
Flavio Lerda178eeeb2011-07-11 19:51:40 +0100262 * @param callUris URIs into {@link CallLog.Calls} of the calls to be displayed
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800263 */
Flavio Lerda178eeeb2011-07-11 19:51:40 +0100264 private void updateData(final Uri... callUris) {
265 // TODO: All phone calls correspond to the same person, so we can make a single lookup.
266 final int numCalls = callUris.length;
267 final PhoneCallDetails[] details = new PhoneCallDetails[numCalls];
268 try {
269 for (int index = 0; index < numCalls; ++index) {
270 details[index] = getPhoneCallDetailsForUri(callUris[index]);
271 }
272 } catch (IllegalArgumentException e) {
273 // Something went wrong reading in our primary data, so we're going to
274 // bail out and show error to users.
275 Log.w(TAG, "invalid URI starting call details", e);
276 Toast.makeText(this, R.string.toast_call_detail_error,
277 Toast.LENGTH_SHORT).show();
278 finish();
279 return;
280 }
281
282 // We know that all calls are from the same number and the same contact, so pick the first.
283 mNumber = details[0].number.toString();
284 final long personId = details[0].personId;
Daniel Lehmann362654a2011-07-17 14:16:47 -0700285 final Uri photoUri = details[0].photoUri;
Flavio Lerda178eeeb2011-07-11 19:51:40 +0100286
287 // Set the details header, based on the first phone call.
288 mPhoneCallDetailsHelper.setPhoneCallDetails(mPhoneCallDetailsViews,
Flavio Lerda76921902011-07-13 21:11:51 +0100289 details[0], false, false);
Flavio Lerda178eeeb2011-07-11 19:51:40 +0100290
Flavio Lerdacfff16d2011-07-12 23:54:40 +0100291 // Cache the details about the phone number.
292 final Uri numberCallUri = mPhoneNumberHelper.getCallUri(mNumber);
293 final boolean canPlaceCallsTo = mPhoneNumberHelper.canPlaceCallsTo(mNumber);
294 final boolean isVoicemailNumber = mPhoneNumberHelper.isVoicemailNumber(mNumber);
295 final boolean isSipNumber = mPhoneNumberHelper.isSipNumber(mNumber);
296
Flavio Lerda178eeeb2011-07-11 19:51:40 +0100297 // Let user view contact details if they exist, otherwise add option to create new contact
298 // from this number.
299 final Intent mainActionIntent;
300 final int mainActionIcon;
301
302 if (details[0].personId != -1) {
303 Uri personUri = ContentUris.withAppendedId(Contacts.CONTENT_URI, personId);
304 mainActionIntent = new Intent(Intent.ACTION_VIEW, personUri);
305 mainActionIcon = R.drawable.sym_action_view_contact;
Flavio Lerdacfff16d2011-07-12 23:54:40 +0100306 } else if (isVoicemailNumber) {
307 mainActionIntent = null;
308 mainActionIcon = 0;
309 } else if (isSipNumber) {
310 // TODO: This item is currently disabled for SIP addresses, because
311 // the Insert.PHONE extra only works correctly for PSTN numbers.
312 //
313 // To fix this for SIP addresses, we need to:
314 // - define ContactsContract.Intents.Insert.SIP_ADDRESS, and use it here if
315 // the current number is a SIP address
316 // - update the contacts UI code to handle Insert.SIP_ADDRESS by
317 // updating the SipAddress field
318 // and then we can remove the "!isSipNumber" check above.
319 mainActionIntent = null;
320 mainActionIcon = 0;
Flavio Lerda8e403402011-07-20 16:25:49 +0100321 } else if (canPlaceCallsTo) {
Flavio Lerda178eeeb2011-07-11 19:51:40 +0100322 mainActionIntent = new Intent(Intent.ACTION_INSERT_OR_EDIT);
323 mainActionIntent.setType(Contacts.CONTENT_ITEM_TYPE);
324 mainActionIntent.putExtra(Insert.PHONE, mNumber);
325 mainActionIcon = R.drawable.sym_action_add;
Flavio Lerda8e403402011-07-20 16:25:49 +0100326 } else {
327 // If we cannot call the number, when we probably cannot add it as a contact either.
328 // This is usually the case of private, unknown, or payphone numbers.
329 mainActionIntent = null;
330 mainActionIcon = 0;
Flavio Lerda178eeeb2011-07-11 19:51:40 +0100331 }
332
Flavio Lerdacfff16d2011-07-12 23:54:40 +0100333 if (mainActionIntent == null) {
334 mMainActionView.setVisibility(View.INVISIBLE);
335 } else {
336 mMainActionView.setVisibility(View.VISIBLE);
337 mMainActionView.setImageResource(mainActionIcon);
338 mMainActionView.setOnClickListener(new View.OnClickListener() {
339 @Override
340 public void onClick(View v) {
341 startActivity(mainActionIntent);
342 }
343 });
344 }
Flavio Lerda178eeeb2011-07-11 19:51:40 +0100345
346 // Build list of various available actions.
347 final List<ViewEntry> actions = new ArrayList<ViewEntry>();
348
Flavio Lerda178eeeb2011-07-11 19:51:40 +0100349 // This action allows to call the number that places the call.
Flavio Lerdacfff16d2011-07-12 23:54:40 +0100350 if (canPlaceCallsTo) {
Flavio Lerda178eeeb2011-07-11 19:51:40 +0100351 actions.add(new ViewEntry(android.R.drawable.sym_action_call,
352 getString(R.string.menu_callNumber, mNumber),
353 new Intent(Intent.ACTION_CALL_PRIVILEGED, numberCallUri)));
354 }
355
Flavio Lerdacfff16d2011-07-12 23:54:40 +0100356 // This action allows to send an SMS to the number that placed the call.
357 if (mPhoneNumberHelper.canSendSmsTo(mNumber)) {
Flavio Lerda178eeeb2011-07-11 19:51:40 +0100358 Intent smsIntent = new Intent(Intent.ACTION_SENDTO,
359 Uri.fromParts("sms", mNumber, null));
360 actions.add(new ViewEntry(R.drawable.sym_action_sms,
361 getString(R.string.menu_sendTextMessage), smsIntent));
362 }
363
364 // This action deletes all elements in the group from the call log.
Hugo Hudson157dde12011-07-21 23:28:13 +0100365 // We don't have this action for voicemails, because you can just use the trash button.
Flavio Lerda94d7bab2011-07-22 16:52:34 +0100366 mHasRemoveFromCallLog = !hasVoicemail();
367 mHasEditNumberBeforeCall = canPlaceCallsTo && !isSipNumber && !isVoicemailNumber;
Flavio Lerda178eeeb2011-07-11 19:51:40 +0100368
Flavio Lerda94d7bab2011-07-22 16:52:34 +0100369 if (actions.size() != 0) {
370 // Set the actions for this phone number.
371 setListAdapter(new ViewAdapter(this, actions));
372 getListView().setVisibility(View.VISIBLE);
373 } else {
374 getListView().setVisibility(View.GONE);
Hugo Hudson157dde12011-07-21 23:28:13 +0100375 }
Flavio Lerda178eeeb2011-07-11 19:51:40 +0100376
Flavio Lerda178eeeb2011-07-11 19:51:40 +0100377 ListView historyList = (ListView) findViewById(R.id.history);
378 historyList.setAdapter(
379 new CallDetailHistoryAdapter(this, mInflater, mCallTypeHelper, details));
Daniel Lehmann362654a2011-07-17 14:16:47 -0700380 loadContactPhotos(photoUri);
Flavio Lerda178eeeb2011-07-11 19:51:40 +0100381 }
382
383 /** Return the phone call details for a given call log URI. */
384 private PhoneCallDetails getPhoneCallDetailsForUri(Uri callUri) {
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800385 ContentResolver resolver = getContentResolver();
386 Cursor callCursor = resolver.query(callUri, CALL_LOG_PROJECTION, null, null, null);
387 try {
Flavio Lerda178eeeb2011-07-11 19:51:40 +0100388 if (callCursor == null || !callCursor.moveToFirst()) {
389 throw new IllegalArgumentException("Cannot find content: " + callUri);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800390 }
Flavio Lerda178eeeb2011-07-11 19:51:40 +0100391
392 // Read call log specifics.
393 String number = callCursor.getString(NUMBER_COLUMN_INDEX);
394 long date = callCursor.getLong(DATE_COLUMN_INDEX);
395 long duration = callCursor.getLong(DURATION_COLUMN_INDEX);
396 int callType = callCursor.getInt(CALL_TYPE_COLUMN_INDEX);
397 String countryIso = callCursor.getString(COUNTRY_ISO_COLUMN_INDEX);
398 if (TextUtils.isEmpty(countryIso)) {
399 countryIso = mDefaultCountryIso;
400 }
401
402 // Formatted phone number.
403 final CharSequence numberText;
404 // Read contact specifics.
405 CharSequence nameText = "";
406 int numberType = 0;
407 CharSequence numberLabel = "";
408 long personId = -1L;
Daniel Lehmann362654a2011-07-17 14:16:47 -0700409 Uri photoUri = null;
Flavio Lerda178eeeb2011-07-11 19:51:40 +0100410 // If this is not a regular number, there is no point in looking it up in the contacts.
411 if (!mPhoneNumberHelper.canPlaceCallsTo(number)) {
412 numberText = mPhoneNumberHelper.getDisplayNumber(number, null);
413 } else {
414 // Perform a reverse-phonebook lookup to find the contact details.
415 Uri phoneUri = Uri.withAppendedPath(PhoneLookup.CONTENT_FILTER_URI,
416 Uri.encode(number));
417 Cursor phonesCursor = resolver.query(phoneUri, PHONES_PROJECTION, null, null, null);
418 String candidateNumberText = number;
419 try {
420 if (phonesCursor != null && phonesCursor.moveToFirst()) {
421 personId = phonesCursor.getLong(COLUMN_INDEX_ID);
422 nameText = phonesCursor.getString(COLUMN_INDEX_NAME);
Daniel Lehmann362654a2011-07-17 14:16:47 -0700423 String photoUriString = phonesCursor.getString(COLUMN_INDEX_PHOTO_URI);
424 photoUri = photoUriString == null ? null : Uri.parse(photoUriString);
Flavio Lerda178eeeb2011-07-11 19:51:40 +0100425 candidateNumberText = PhoneNumberUtils.formatNumber(
426 phonesCursor.getString(COLUMN_INDEX_NUMBER),
427 phonesCursor.getString(COLUMN_INDEX_NORMALIZED_NUMBER),
428 countryIso);
429 numberType = phonesCursor.getInt(COLUMN_INDEX_TYPE);
430 numberLabel = phonesCursor.getString(COLUMN_INDEX_LABEL);
431 } else {
432 // We could not find this contact in the contacts, just format the phone
433 // number as best as we can. All the other fields will have their default
434 // values.
435 candidateNumberText =
436 PhoneNumberUtils.formatNumber(number, countryIso);
437 }
438 } finally {
439 if (phonesCursor != null) phonesCursor.close();
440 numberText = candidateNumberText;
441 }
442 }
Flavio Lerda9c466372011-07-19 17:38:17 +0100443 return new PhoneCallDetails(number, numberText, countryIso,
Flavio Lerdacb805e82011-07-18 10:31:44 +0100444 new int[]{ callType }, date, duration,
Daniel Lehmann362654a2011-07-17 14:16:47 -0700445 nameText, numberType, numberLabel, personId, photoUri);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800446 } finally {
447 if (callCursor != null) {
448 callCursor.close();
449 }
450 }
451 }
452
Flavio Lerda9cafe472011-06-08 14:06:13 +0100453 /** Load the contact photos and places them in the corresponding views. */
Daniel Lehmann362654a2011-07-17 14:16:47 -0700454 private void loadContactPhotos(Uri photoUri) {
455 mContactPhotoManager.loadPhoto(mContactBackgroundView, photoUri);
Flavio Lerda9cafe472011-06-08 14:06:13 +0100456 }
457
Flavio Lerda696e8132011-07-05 19:00:23 +0100458 private String getVoicemailNumber() {
459 TelephonyManager telephonyManager =
460 (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
461 return telephonyManager.getVoiceMailNumber();
462 }
463
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800464 static final class ViewEntry {
Flavio Lerdaa8956882011-07-09 21:34:38 +0100465 public final int icon;
466 public final String text;
467 public final Intent intent;
468 public final View.OnClickListener action;
469
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800470 public String label = null;
471 public String number = null;
Dmitri Plotnikovd0d776d2009-08-19 17:38:04 -0700472
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800473 public ViewEntry(int icon, String text, Intent intent) {
474 this.icon = icon;
475 this.text = text;
476 this.intent = intent;
Flavio Lerdaa8956882011-07-09 21:34:38 +0100477 this.action = null;
478 }
479
480 public ViewEntry(int icon, String text, View.OnClickListener listener) {
481 this.icon = icon;
482 this.text = text;
483 this.intent = null;
484 this.action = listener;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800485 }
486 }
487
488 static final class ViewAdapter extends BaseAdapter {
Dmitri Plotnikovd0d776d2009-08-19 17:38:04 -0700489
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800490 private final List<ViewEntry> mActions;
Dmitri Plotnikovd0d776d2009-08-19 17:38:04 -0700491
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800492 private final LayoutInflater mInflater;
Dmitri Plotnikovd0d776d2009-08-19 17:38:04 -0700493
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800494 public ViewAdapter(Context context, List<ViewEntry> actions) {
495 mActions = actions;
496 mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
497 }
Dmitri Plotnikovd0d776d2009-08-19 17:38:04 -0700498
Flavio Lerda9cafe472011-06-08 14:06:13 +0100499 @Override
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800500 public int getCount() {
501 return mActions.size();
502 }
503
Flavio Lerda9cafe472011-06-08 14:06:13 +0100504 @Override
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800505 public Object getItem(int position) {
506 return mActions.get(position);
507 }
508
Flavio Lerda9cafe472011-06-08 14:06:13 +0100509 @Override
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800510 public long getItemId(int position) {
511 return position;
512 }
Dmitri Plotnikovd0d776d2009-08-19 17:38:04 -0700513
Flavio Lerda9cafe472011-06-08 14:06:13 +0100514 @Override
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800515 public View getView(int position, View convertView, ViewGroup parent) {
516 // Make sure we have a valid convertView to start with
517 if (convertView == null) {
518 convertView = mInflater.inflate(R.layout.call_detail_list_item, parent, false);
519 }
520
521 // Fill action with icon and text.
522 ViewEntry entry = mActions.get(position);
523 convertView.setTag(entry);
Dmitri Plotnikovd0d776d2009-08-19 17:38:04 -0700524
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800525 ImageView icon = (ImageView) convertView.findViewById(R.id.icon);
526 TextView text = (TextView) convertView.findViewById(android.R.id.text1);
527
528 icon.setImageResource(entry.icon);
529 text.setText(entry.text);
530
531 View line2 = convertView.findViewById(R.id.line2);
532 boolean numberEmpty = TextUtils.isEmpty(entry.number);
533 boolean labelEmpty = TextUtils.isEmpty(entry.label) || numberEmpty;
534 if (labelEmpty && numberEmpty) {
535 line2.setVisibility(View.GONE);
536 } else {
537 line2.setVisibility(View.VISIBLE);
Dmitri Plotnikovd0d776d2009-08-19 17:38:04 -0700538
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800539 TextView label = (TextView) convertView.findViewById(R.id.label);
540 if (labelEmpty) {
541 label.setVisibility(View.GONE);
542 } else {
543 label.setText(entry.label);
544 label.setVisibility(View.VISIBLE);
545 }
546
547 TextView number = (TextView) convertView.findViewById(R.id.number);
548 number.setText(entry.number);
549 }
Dmitri Plotnikovd0d776d2009-08-19 17:38:04 -0700550
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800551 return convertView;
552 }
553 }
Dmitri Plotnikovd0d776d2009-08-19 17:38:04 -0700554
Flavio Lerda9cafe472011-06-08 14:06:13 +0100555 @Override
556 public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800557 // Handle passing action off to correct handler.
558 if (view.getTag() instanceof ViewEntry) {
559 ViewEntry entry = (ViewEntry) view.getTag();
560 if (entry.intent != null) {
561 startActivity(entry.intent);
Flavio Lerdaa8956882011-07-09 21:34:38 +0100562 } else if (entry.action != null) {
563 entry.action.onClick(view);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800564 }
565 }
Dmitri Plotnikovd0d776d2009-08-19 17:38:04 -0700566 }
Dmitri Plotnikov8e86b752010-02-22 17:47:57 -0800567
568 @Override
569 public void startSearch(String initialQuery, boolean selectInitialQuery, Bundle appSearchData,
570 boolean globalSearch) {
571 if (globalSearch) {
572 super.startSearch(initialQuery, selectInitialQuery, appSearchData, globalSearch);
573 } else {
574 ContactsSearchManager.startSearch(this, initialQuery);
575 }
576 }
Debashish Chatterjee0cb82242011-07-19 19:29:37 +0100577
578 protected void updateVoicemailStatusMessage(Cursor statusCursor) {
579 if (statusCursor == null) {
580 mStatusMessageView.setVisibility(View.GONE);
581 return;
582 }
583 final StatusMessage message = getStatusMessage(statusCursor);
584 if (message == null || !message.showInCallDetails()) {
585 mStatusMessageView.setVisibility(View.GONE);
586 return;
587 }
588
589 mStatusMessageView.setVisibility(View.VISIBLE);
590 mStatusMessageText.setText(message.callDetailsMessageId);
591 if (message.actionMessageId != -1) {
592 mStatusMessageAction.setText(message.actionMessageId);
593 }
594 if (message.actionUri != null) {
595 mStatusMessageAction.setClickable(true);
596 mStatusMessageAction.setOnClickListener(new View.OnClickListener() {
597 @Override
598 public void onClick(View v) {
599 startActivity(new Intent(Intent.ACTION_VIEW, message.actionUri));
600 }
601 });
602 } else {
603 mStatusMessageAction.setClickable(false);
604 }
605 }
606
607 private StatusMessage getStatusMessage(Cursor statusCursor) {
608 List<StatusMessage> messages = mVoicemailStatusHelper.getStatusMessages(statusCursor);
609 Log.d(TAG, "Num status messages: " + messages.size());
610 if (messages.size() == 0) {
611 return null;
612 }
613 // There can only be a single status message per source package, so num of messages can
614 // at most be 1.
615 if (messages.size() > 1) {
616 Log.w(TAG, String.format("Expected 1, found (%d) num of status messages." +
617 " Will use the first one.", messages.size()));
618 }
619 return messages.get(0);
620 }
Flavio Lerda94d7bab2011-07-22 16:52:34 +0100621
622 @Override
623 public boolean onCreateOptionsMenu(Menu menu) {
624 getMenuInflater().inflate(R.menu.call_details_options, menu);
625 return true;
626 }
627
628 @Override
629 public boolean onPrepareOptionsMenu(Menu menu) {
630 // This action deletes all elements in the group from the call log.
631 // We don't have this action for voicemails, because you can just use the trash button.
632 menu.findItem(R.id.remove_from_call_log).setVisible(mHasRemoveFromCallLog);
633 menu.findItem(R.id.edit_number_before_call).setVisible(mHasEditNumberBeforeCall);
634 return mHasRemoveFromCallLog || mHasEditNumberBeforeCall;
635 }
636
637 @Override
638 public boolean onMenuItemSelected(int featureId, MenuItem item) {
639 switch (item.getItemId()) {
640 case R.id.remove_from_call_log: {
641 StringBuilder callIds = new StringBuilder();
642 for (Uri callUri : getCallLogEntryUris()) {
643 if (callIds.length() != 0) {
644 callIds.append(",");
645 }
646 callIds.append(ContentUris.parseId(callUri));
647 }
648
649 getContentResolver().delete(Calls.CONTENT_URI_WITH_VOICEMAIL,
650 Calls._ID + " IN (" + callIds + ")", null);
651 // Also close the activity.
652 finish();
653 return true;
654 }
655
656 case R.id.edit_number_before_call:
657 startActivity(
658 new Intent(Intent.ACTION_DIAL, mPhoneNumberHelper.getCallUri(mNumber)));
659 return true;
660
661 default:
662 throw new IllegalArgumentException();
663 }
664 }
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800665}