blob: e9a693ee9fd407ab75634a32c053a2d9f7b27bf4 [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
Flavio Lerdad44e83a2011-07-24 23:10:17 +010027import android.app.ActionBar;
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 ImageView mMainActionView;
Flavio Lerda9cafe472011-06-08 14:06:13 +010085 private ImageView mContactBackgroundView;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080086
87 private String mNumber = null;
Bai Tao09eb04f2010-09-01 15:34:16 +080088 private String mDefaultCountryIso;
Dmitri Plotnikovd0d776d2009-08-19 17:38:04 -070089
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080090 /* package */ LayoutInflater mInflater;
91 /* package */ Resources mResources;
Flavio Lerda9cafe472011-06-08 14:06:13 +010092 /** Helper to load contact photos. */
93 private ContactPhotoManager mContactPhotoManager;
Debashish Chatterjee0cb82242011-07-19 19:29:37 +010094 /** Helper to make async queries to content resolver. */
95 private CallDetailActivityQueryHandler mAsyncQueryHandler;
96 /** Helper to get voicemail status messages. */
97 private VoicemailStatusHelper mVoicemailStatusHelper;
98 // Views related to voicemail status message.
99 private View mStatusMessageView;
100 private TextView mStatusMessageText;
101 private TextView mStatusMessageAction;
Dmitri Plotnikovd0d776d2009-08-19 17:38:04 -0700102
Flavio Lerda94d7bab2011-07-22 16:52:34 +0100103 /** Whether we should show "remove from call log" in the options menu. */
104 private boolean mHasRemoveFromCallLog;
105 /** Whether we should show "edit number before call" in the options menu. */
106 private boolean mHasEditNumberBeforeCall;
107
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800108 static final String[] CALL_LOG_PROJECTION = new String[] {
109 CallLog.Calls.DATE,
110 CallLog.Calls.DURATION,
111 CallLog.Calls.NUMBER,
112 CallLog.Calls.TYPE,
Bai Tao09eb04f2010-09-01 15:34:16 +0800113 CallLog.Calls.COUNTRY_ISO,
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800114 };
Dmitri Plotnikovd0d776d2009-08-19 17:38:04 -0700115
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800116 static final int DATE_COLUMN_INDEX = 0;
117 static final int DURATION_COLUMN_INDEX = 1;
118 static final int NUMBER_COLUMN_INDEX = 2;
119 static final int CALL_TYPE_COLUMN_INDEX = 3;
Bai Tao09eb04f2010-09-01 15:34:16 +0800120 static final int COUNTRY_ISO_COLUMN_INDEX = 4;
Dmitri Plotnikovd0d776d2009-08-19 17:38:04 -0700121
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800122 static final String[] PHONES_PROJECTION = new String[] {
Dmitri Plotnikovd0d776d2009-08-19 17:38:04 -0700123 PhoneLookup._ID,
124 PhoneLookup.DISPLAY_NAME,
125 PhoneLookup.TYPE,
126 PhoneLookup.LABEL,
127 PhoneLookup.NUMBER,
Bai Tao09eb04f2010-09-01 15:34:16 +0800128 PhoneLookup.NORMALIZED_NUMBER,
Daniel Lehmann362654a2011-07-17 14:16:47 -0700129 PhoneLookup.PHOTO_URI,
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800130 };
131 static final int COLUMN_INDEX_ID = 0;
132 static final int COLUMN_INDEX_NAME = 1;
133 static final int COLUMN_INDEX_TYPE = 2;
134 static final int COLUMN_INDEX_LABEL = 3;
135 static final int COLUMN_INDEX_NUMBER = 4;
Bai Tao09eb04f2010-09-01 15:34:16 +0800136 static final int COLUMN_INDEX_NORMALIZED_NUMBER = 5;
Daniel Lehmann362654a2011-07-17 14:16:47 -0700137 static final int COLUMN_INDEX_PHOTO_URI = 6;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800138
139 @Override
140 protected void onCreate(Bundle icicle) {
141 super.onCreate(icicle);
142
143 setContentView(R.layout.call_detail);
144
145 mInflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
146 mResources = getResources();
Dmitri Plotnikovd0d776d2009-08-19 17:38:04 -0700147
Flavio Lerda696e8132011-07-05 19:00:23 +0100148 mPhoneCallDetailsViews = PhoneCallDetailsViews.fromView(getWindow().getDecorView());
Flavio Lerda40569562011-07-22 21:51:29 +0100149 mCallTypeHelper = new CallTypeHelper(getResources(), mInflater);
Flavio Lerda178eeeb2011-07-11 19:51:40 +0100150 mPhoneNumberHelper = new PhoneNumberHelper(mResources, getVoicemailNumber());
Flavio Lerda40569562011-07-22 21:51:29 +0100151 mPhoneCallDetailsHelper = new PhoneCallDetailsHelper(mResources, mCallTypeHelper,
Flavio Lerda178eeeb2011-07-11 19:51:40 +0100152 mPhoneNumberHelper);
Debashish Chatterjee0cb82242011-07-19 19:29:37 +0100153 mVoicemailStatusHelper = new VoicemailStatusHelperImpl();
154 mAsyncQueryHandler = new CallDetailActivityQueryHandler(this);
155 mStatusMessageView = findViewById(R.id.voicemail_status);
156 mStatusMessageText = (TextView) findViewById(R.id.voicemail_status_message);
157 mStatusMessageAction = (TextView) findViewById(R.id.voicemail_status_action);
Flavio Lerdafb63c432011-07-07 17:16:53 +0100158 mMainActionView = (ImageView) findViewById(R.id.main_action);
Flavio Lerda9cafe472011-06-08 14:06:13 +0100159 mContactBackgroundView = (ImageView) findViewById(R.id.contact_background);
Bai Tao09eb04f2010-09-01 15:34:16 +0800160 mDefaultCountryIso = ContactsUtils.getCurrentCountryIso(this);
Flavio Lerda9cafe472011-06-08 14:06:13 +0100161 mContactPhotoManager = ContactPhotoManager.getInstance(this);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800162 getListView().setOnItemClickListener(this);
Flavio Lerdad44e83a2011-07-24 23:10:17 +0100163 configureActionBar();
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800164 }
Dmitri Plotnikovd0d776d2009-08-19 17:38:04 -0700165
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800166 @Override
167 public void onResume() {
168 super.onResume();
Flavio Lerda178eeeb2011-07-11 19:51:40 +0100169 updateData(getCallLogEntryUris());
Hugo Hudson757aa552011-07-20 01:15:25 +0100170 optionallyHandleVoicemail();
Hugo Hudsonb002f512011-07-15 17:41:12 +0100171 }
172
173 /**
174 * Handle voicemail playback or hide voicemail ui.
175 * <p>
176 * If the Intent used to start this Activity contains the suitable extras, then start voicemail
177 * playback. If it doesn't, then hide the voicemail ui.
178 */
Hugo Hudson757aa552011-07-20 01:15:25 +0100179 private void optionallyHandleVoicemail() {
Hugo Hudson157dde12011-07-21 23:28:13 +0100180 if (hasVoicemail()) {
Hugo Hudsona9ad6942011-07-29 17:06:04 +0100181 // Has voicemail: add the voicemail fragment. Add suitable arguments to set the uri
182 // to play and optionally start the playback.
Hugo Hudson757aa552011-07-20 01:15:25 +0100183 // Do a query to fetch the voicemail status messages.
Hugo Hudsona9ad6942011-07-29 17:06:04 +0100184 VoicemailPlaybackFragment playbackFragment = new VoicemailPlaybackFragment();
185 Bundle fragmentArguments = new Bundle();
Hugo Hudson157dde12011-07-21 23:28:13 +0100186 Uri voicemailUri = getIntent().getParcelableExtra(EXTRA_VOICEMAIL_URI);
Hugo Hudsona9ad6942011-07-29 17:06:04 +0100187 fragmentArguments.putParcelable(EXTRA_VOICEMAIL_URI, voicemailUri);
188 if (getIntent().getBooleanExtra(EXTRA_VOICEMAIL_START_PLAYBACK, false)) {
189 fragmentArguments.putBoolean(EXTRA_VOICEMAIL_START_PLAYBACK, true);
190 }
191 playbackFragment.setArguments(fragmentArguments);
192 getFragmentManager().beginTransaction()
193 .add(R.id.voicemail_container, playbackFragment).commit();
Hugo Hudson757aa552011-07-20 01:15:25 +0100194 mAsyncQueryHandler.startVoicemailStatusQuery(voicemailUri);
195 } else {
Hugo Hudsona9ad6942011-07-29 17:06:04 +0100196 // No voicemail uri: hide the status view.
Hugo Hudson757aa552011-07-20 01:15:25 +0100197 mStatusMessageView.setVisibility(View.GONE);
Hugo Hudsonb002f512011-07-15 17:41:12 +0100198 }
Flavio Lerda178eeeb2011-07-11 19:51:40 +0100199 }
200
Hugo Hudson157dde12011-07-21 23:28:13 +0100201 private boolean hasVoicemail() {
202 return getIntent().getParcelableExtra(EXTRA_VOICEMAIL_URI) != null;
203 }
204
Flavio Lerda178eeeb2011-07-11 19:51:40 +0100205 /**
206 * Returns the list of URIs to show.
207 * <p>
208 * There are two ways the URIs can be provided to the activity: as the data on the intent, or as
209 * a list of ids in the call log added as an extra on the URI.
210 * <p>
211 * If both are available, the data on the intent takes precedence.
212 */
213 private Uri[] getCallLogEntryUris() {
214 Uri uri = getIntent().getData();
215 if (uri != null) {
216 // If there is a data on the intent, it takes precedence over the extra.
217 return new Uri[]{ uri };
218 }
219 long[] ids = getIntent().getLongArrayExtra(EXTRA_CALL_LOG_IDS);
220 Uri[] uris = new Uri[ids.length];
221 for (int index = 0; index < ids.length; ++index) {
222 uris[index] = ContentUris.withAppendedId(Calls.CONTENT_URI_WITH_VOICEMAIL, ids[index]);
223 }
224 return uris;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800225 }
226
227 @Override
228 public boolean onKeyDown(int keyCode, KeyEvent event) {
229 switch (keyCode) {
230 case KeyEvent.KEYCODE_CALL: {
231 // Make sure phone isn't already busy before starting direct call
232 TelephonyManager tm = (TelephonyManager)
233 getSystemService(Context.TELEPHONY_SERVICE);
234 if (tm.getCallState() == TelephonyManager.CALL_STATE_IDLE) {
235 Intent callIntent = new Intent(Intent.ACTION_CALL_PRIVILEGED,
236 Uri.fromParts("tel", mNumber, null));
237 startActivity(callIntent);
238 return true;
239 }
240 }
241 }
Dmitri Plotnikovd0d776d2009-08-19 17:38:04 -0700242
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800243 return super.onKeyDown(keyCode, event);
244 }
Dmitri Plotnikovd0d776d2009-08-19 17:38:04 -0700245
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800246 /**
247 * Update user interface with details of given call.
Dmitri Plotnikovd0d776d2009-08-19 17:38:04 -0700248 *
Flavio Lerda178eeeb2011-07-11 19:51:40 +0100249 * @param callUris URIs into {@link CallLog.Calls} of the calls to be displayed
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800250 */
Flavio Lerda178eeeb2011-07-11 19:51:40 +0100251 private void updateData(final Uri... callUris) {
252 // TODO: All phone calls correspond to the same person, so we can make a single lookup.
253 final int numCalls = callUris.length;
254 final PhoneCallDetails[] details = new PhoneCallDetails[numCalls];
255 try {
256 for (int index = 0; index < numCalls; ++index) {
257 details[index] = getPhoneCallDetailsForUri(callUris[index]);
258 }
259 } catch (IllegalArgumentException e) {
260 // Something went wrong reading in our primary data, so we're going to
261 // bail out and show error to users.
262 Log.w(TAG, "invalid URI starting call details", e);
263 Toast.makeText(this, R.string.toast_call_detail_error,
264 Toast.LENGTH_SHORT).show();
265 finish();
266 return;
267 }
268
269 // We know that all calls are from the same number and the same contact, so pick the first.
270 mNumber = details[0].number.toString();
271 final long personId = details[0].personId;
Daniel Lehmann362654a2011-07-17 14:16:47 -0700272 final Uri photoUri = details[0].photoUri;
Flavio Lerda178eeeb2011-07-11 19:51:40 +0100273
274 // Set the details header, based on the first phone call.
275 mPhoneCallDetailsHelper.setPhoneCallDetails(mPhoneCallDetailsViews,
Flavio Lerda40569562011-07-22 21:51:29 +0100276 details[0], false, false, true);
Flavio Lerda178eeeb2011-07-11 19:51:40 +0100277
Flavio Lerdacfff16d2011-07-12 23:54:40 +0100278 // Cache the details about the phone number.
279 final Uri numberCallUri = mPhoneNumberHelper.getCallUri(mNumber);
280 final boolean canPlaceCallsTo = mPhoneNumberHelper.canPlaceCallsTo(mNumber);
281 final boolean isVoicemailNumber = mPhoneNumberHelper.isVoicemailNumber(mNumber);
282 final boolean isSipNumber = mPhoneNumberHelper.isSipNumber(mNumber);
283
Flavio Lerda178eeeb2011-07-11 19:51:40 +0100284 // Let user view contact details if they exist, otherwise add option to create new contact
285 // from this number.
286 final Intent mainActionIntent;
287 final int mainActionIcon;
288
289 if (details[0].personId != -1) {
290 Uri personUri = ContentUris.withAppendedId(Contacts.CONTENT_URI, personId);
291 mainActionIntent = new Intent(Intent.ACTION_VIEW, personUri);
292 mainActionIcon = R.drawable.sym_action_view_contact;
Flavio Lerdacfff16d2011-07-12 23:54:40 +0100293 } else if (isVoicemailNumber) {
294 mainActionIntent = null;
295 mainActionIcon = 0;
296 } else if (isSipNumber) {
297 // TODO: This item is currently disabled for SIP addresses, because
298 // the Insert.PHONE extra only works correctly for PSTN numbers.
299 //
300 // To fix this for SIP addresses, we need to:
301 // - define ContactsContract.Intents.Insert.SIP_ADDRESS, and use it here if
302 // the current number is a SIP address
303 // - update the contacts UI code to handle Insert.SIP_ADDRESS by
304 // updating the SipAddress field
305 // and then we can remove the "!isSipNumber" check above.
306 mainActionIntent = null;
307 mainActionIcon = 0;
Flavio Lerda8e403402011-07-20 16:25:49 +0100308 } else if (canPlaceCallsTo) {
Flavio Lerda178eeeb2011-07-11 19:51:40 +0100309 mainActionIntent = new Intent(Intent.ACTION_INSERT_OR_EDIT);
310 mainActionIntent.setType(Contacts.CONTENT_ITEM_TYPE);
311 mainActionIntent.putExtra(Insert.PHONE, mNumber);
312 mainActionIcon = R.drawable.sym_action_add;
Flavio Lerda8e403402011-07-20 16:25:49 +0100313 } else {
314 // If we cannot call the number, when we probably cannot add it as a contact either.
315 // This is usually the case of private, unknown, or payphone numbers.
316 mainActionIntent = null;
317 mainActionIcon = 0;
Flavio Lerda178eeeb2011-07-11 19:51:40 +0100318 }
319
Flavio Lerdacfff16d2011-07-12 23:54:40 +0100320 if (mainActionIntent == null) {
321 mMainActionView.setVisibility(View.INVISIBLE);
322 } else {
323 mMainActionView.setVisibility(View.VISIBLE);
324 mMainActionView.setImageResource(mainActionIcon);
325 mMainActionView.setOnClickListener(new View.OnClickListener() {
326 @Override
327 public void onClick(View v) {
328 startActivity(mainActionIntent);
329 }
330 });
331 }
Flavio Lerda178eeeb2011-07-11 19:51:40 +0100332
333 // Build list of various available actions.
334 final List<ViewEntry> actions = new ArrayList<ViewEntry>();
335
Flavio Lerda178eeeb2011-07-11 19:51:40 +0100336 // This action allows to call the number that places the call.
Flavio Lerdacfff16d2011-07-12 23:54:40 +0100337 if (canPlaceCallsTo) {
Flavio Lerda178eeeb2011-07-11 19:51:40 +0100338 actions.add(new ViewEntry(android.R.drawable.sym_action_call,
339 getString(R.string.menu_callNumber, mNumber),
340 new Intent(Intent.ACTION_CALL_PRIVILEGED, numberCallUri)));
341 }
342
Flavio Lerdacfff16d2011-07-12 23:54:40 +0100343 // This action allows to send an SMS to the number that placed the call.
344 if (mPhoneNumberHelper.canSendSmsTo(mNumber)) {
Flavio Lerda178eeeb2011-07-11 19:51:40 +0100345 Intent smsIntent = new Intent(Intent.ACTION_SENDTO,
346 Uri.fromParts("sms", mNumber, null));
347 actions.add(new ViewEntry(R.drawable.sym_action_sms,
348 getString(R.string.menu_sendTextMessage), smsIntent));
349 }
350
351 // This action deletes all elements in the group from the call log.
Hugo Hudson157dde12011-07-21 23:28:13 +0100352 // We don't have this action for voicemails, because you can just use the trash button.
Flavio Lerda94d7bab2011-07-22 16:52:34 +0100353 mHasRemoveFromCallLog = !hasVoicemail();
354 mHasEditNumberBeforeCall = canPlaceCallsTo && !isSipNumber && !isVoicemailNumber;
Flavio Lerda178eeeb2011-07-11 19:51:40 +0100355
Flavio Lerda94d7bab2011-07-22 16:52:34 +0100356 if (actions.size() != 0) {
357 // Set the actions for this phone number.
358 setListAdapter(new ViewAdapter(this, actions));
359 getListView().setVisibility(View.VISIBLE);
360 } else {
361 getListView().setVisibility(View.GONE);
Hugo Hudson157dde12011-07-21 23:28:13 +0100362 }
Flavio Lerda178eeeb2011-07-11 19:51:40 +0100363
Flavio Lerda178eeeb2011-07-11 19:51:40 +0100364 ListView historyList = (ListView) findViewById(R.id.history);
365 historyList.setAdapter(
366 new CallDetailHistoryAdapter(this, mInflater, mCallTypeHelper, details));
Daniel Lehmann362654a2011-07-17 14:16:47 -0700367 loadContactPhotos(photoUri);
Flavio Lerda178eeeb2011-07-11 19:51:40 +0100368 }
369
370 /** Return the phone call details for a given call log URI. */
371 private PhoneCallDetails getPhoneCallDetailsForUri(Uri callUri) {
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800372 ContentResolver resolver = getContentResolver();
373 Cursor callCursor = resolver.query(callUri, CALL_LOG_PROJECTION, null, null, null);
374 try {
Flavio Lerda178eeeb2011-07-11 19:51:40 +0100375 if (callCursor == null || !callCursor.moveToFirst()) {
376 throw new IllegalArgumentException("Cannot find content: " + callUri);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800377 }
Flavio Lerda178eeeb2011-07-11 19:51:40 +0100378
379 // Read call log specifics.
380 String number = callCursor.getString(NUMBER_COLUMN_INDEX);
381 long date = callCursor.getLong(DATE_COLUMN_INDEX);
382 long duration = callCursor.getLong(DURATION_COLUMN_INDEX);
383 int callType = callCursor.getInt(CALL_TYPE_COLUMN_INDEX);
384 String countryIso = callCursor.getString(COUNTRY_ISO_COLUMN_INDEX);
385 if (TextUtils.isEmpty(countryIso)) {
386 countryIso = mDefaultCountryIso;
387 }
388
389 // Formatted phone number.
390 final CharSequence numberText;
391 // Read contact specifics.
392 CharSequence nameText = "";
393 int numberType = 0;
394 CharSequence numberLabel = "";
395 long personId = -1L;
Daniel Lehmann362654a2011-07-17 14:16:47 -0700396 Uri photoUri = null;
Flavio Lerda178eeeb2011-07-11 19:51:40 +0100397 // If this is not a regular number, there is no point in looking it up in the contacts.
398 if (!mPhoneNumberHelper.canPlaceCallsTo(number)) {
399 numberText = mPhoneNumberHelper.getDisplayNumber(number, null);
400 } else {
401 // Perform a reverse-phonebook lookup to find the contact details.
402 Uri phoneUri = Uri.withAppendedPath(PhoneLookup.CONTENT_FILTER_URI,
403 Uri.encode(number));
404 Cursor phonesCursor = resolver.query(phoneUri, PHONES_PROJECTION, null, null, null);
405 String candidateNumberText = number;
406 try {
407 if (phonesCursor != null && phonesCursor.moveToFirst()) {
408 personId = phonesCursor.getLong(COLUMN_INDEX_ID);
409 nameText = phonesCursor.getString(COLUMN_INDEX_NAME);
Daniel Lehmann362654a2011-07-17 14:16:47 -0700410 String photoUriString = phonesCursor.getString(COLUMN_INDEX_PHOTO_URI);
411 photoUri = photoUriString == null ? null : Uri.parse(photoUriString);
Flavio Lerda178eeeb2011-07-11 19:51:40 +0100412 candidateNumberText = PhoneNumberUtils.formatNumber(
413 phonesCursor.getString(COLUMN_INDEX_NUMBER),
414 phonesCursor.getString(COLUMN_INDEX_NORMALIZED_NUMBER),
415 countryIso);
416 numberType = phonesCursor.getInt(COLUMN_INDEX_TYPE);
417 numberLabel = phonesCursor.getString(COLUMN_INDEX_LABEL);
418 } else {
419 // We could not find this contact in the contacts, just format the phone
420 // number as best as we can. All the other fields will have their default
421 // values.
422 candidateNumberText =
423 PhoneNumberUtils.formatNumber(number, countryIso);
424 }
425 } finally {
426 if (phonesCursor != null) phonesCursor.close();
427 numberText = candidateNumberText;
428 }
429 }
Flavio Lerda9c466372011-07-19 17:38:17 +0100430 return new PhoneCallDetails(number, numberText, countryIso,
Flavio Lerdacb805e82011-07-18 10:31:44 +0100431 new int[]{ callType }, date, duration,
Daniel Lehmann362654a2011-07-17 14:16:47 -0700432 nameText, numberType, numberLabel, personId, photoUri);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800433 } finally {
434 if (callCursor != null) {
435 callCursor.close();
436 }
437 }
438 }
439
Flavio Lerda9cafe472011-06-08 14:06:13 +0100440 /** Load the contact photos and places them in the corresponding views. */
Daniel Lehmann362654a2011-07-17 14:16:47 -0700441 private void loadContactPhotos(Uri photoUri) {
442 mContactPhotoManager.loadPhoto(mContactBackgroundView, photoUri);
Flavio Lerda9cafe472011-06-08 14:06:13 +0100443 }
444
Flavio Lerda696e8132011-07-05 19:00:23 +0100445 private String getVoicemailNumber() {
446 TelephonyManager telephonyManager =
447 (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
448 return telephonyManager.getVoiceMailNumber();
449 }
450
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800451 static final class ViewEntry {
Flavio Lerdaa8956882011-07-09 21:34:38 +0100452 public final int icon;
453 public final String text;
454 public final Intent intent;
455 public final View.OnClickListener action;
456
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800457 public String label = null;
458 public String number = null;
Dmitri Plotnikovd0d776d2009-08-19 17:38:04 -0700459
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800460 public ViewEntry(int icon, String text, Intent intent) {
461 this.icon = icon;
462 this.text = text;
463 this.intent = intent;
Flavio Lerdaa8956882011-07-09 21:34:38 +0100464 this.action = null;
465 }
466
467 public ViewEntry(int icon, String text, View.OnClickListener listener) {
468 this.icon = icon;
469 this.text = text;
470 this.intent = null;
471 this.action = listener;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800472 }
473 }
474
475 static final class ViewAdapter extends BaseAdapter {
Dmitri Plotnikovd0d776d2009-08-19 17:38:04 -0700476
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800477 private final List<ViewEntry> mActions;
Dmitri Plotnikovd0d776d2009-08-19 17:38:04 -0700478
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800479 private final LayoutInflater mInflater;
Dmitri Plotnikovd0d776d2009-08-19 17:38:04 -0700480
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800481 public ViewAdapter(Context context, List<ViewEntry> actions) {
482 mActions = actions;
483 mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
484 }
Dmitri Plotnikovd0d776d2009-08-19 17:38:04 -0700485
Flavio Lerda9cafe472011-06-08 14:06:13 +0100486 @Override
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800487 public int getCount() {
488 return mActions.size();
489 }
490
Flavio Lerda9cafe472011-06-08 14:06:13 +0100491 @Override
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800492 public Object getItem(int position) {
493 return mActions.get(position);
494 }
495
Flavio Lerda9cafe472011-06-08 14:06:13 +0100496 @Override
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800497 public long getItemId(int position) {
498 return position;
499 }
Dmitri Plotnikovd0d776d2009-08-19 17:38:04 -0700500
Flavio Lerda9cafe472011-06-08 14:06:13 +0100501 @Override
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800502 public View getView(int position, View convertView, ViewGroup parent) {
503 // Make sure we have a valid convertView to start with
504 if (convertView == null) {
505 convertView = mInflater.inflate(R.layout.call_detail_list_item, parent, false);
506 }
507
508 // Fill action with icon and text.
509 ViewEntry entry = mActions.get(position);
510 convertView.setTag(entry);
Dmitri Plotnikovd0d776d2009-08-19 17:38:04 -0700511
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800512 ImageView icon = (ImageView) convertView.findViewById(R.id.icon);
513 TextView text = (TextView) convertView.findViewById(android.R.id.text1);
514
515 icon.setImageResource(entry.icon);
516 text.setText(entry.text);
517
518 View line2 = convertView.findViewById(R.id.line2);
519 boolean numberEmpty = TextUtils.isEmpty(entry.number);
520 boolean labelEmpty = TextUtils.isEmpty(entry.label) || numberEmpty;
521 if (labelEmpty && numberEmpty) {
522 line2.setVisibility(View.GONE);
523 } else {
524 line2.setVisibility(View.VISIBLE);
Dmitri Plotnikovd0d776d2009-08-19 17:38:04 -0700525
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800526 TextView label = (TextView) convertView.findViewById(R.id.label);
527 if (labelEmpty) {
528 label.setVisibility(View.GONE);
529 } else {
530 label.setText(entry.label);
531 label.setVisibility(View.VISIBLE);
532 }
533
534 TextView number = (TextView) convertView.findViewById(R.id.number);
535 number.setText(entry.number);
536 }
Dmitri Plotnikovd0d776d2009-08-19 17:38:04 -0700537
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800538 return convertView;
539 }
540 }
Dmitri Plotnikovd0d776d2009-08-19 17:38:04 -0700541
Flavio Lerda9cafe472011-06-08 14:06:13 +0100542 @Override
543 public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800544 // Handle passing action off to correct handler.
545 if (view.getTag() instanceof ViewEntry) {
546 ViewEntry entry = (ViewEntry) view.getTag();
547 if (entry.intent != null) {
548 startActivity(entry.intent);
Flavio Lerdaa8956882011-07-09 21:34:38 +0100549 } else if (entry.action != null) {
550 entry.action.onClick(view);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800551 }
552 }
Dmitri Plotnikovd0d776d2009-08-19 17:38:04 -0700553 }
Dmitri Plotnikov8e86b752010-02-22 17:47:57 -0800554
555 @Override
556 public void startSearch(String initialQuery, boolean selectInitialQuery, Bundle appSearchData,
557 boolean globalSearch) {
558 if (globalSearch) {
559 super.startSearch(initialQuery, selectInitialQuery, appSearchData, globalSearch);
560 } else {
561 ContactsSearchManager.startSearch(this, initialQuery);
562 }
563 }
Debashish Chatterjee0cb82242011-07-19 19:29:37 +0100564
565 protected void updateVoicemailStatusMessage(Cursor statusCursor) {
566 if (statusCursor == null) {
567 mStatusMessageView.setVisibility(View.GONE);
568 return;
569 }
570 final StatusMessage message = getStatusMessage(statusCursor);
571 if (message == null || !message.showInCallDetails()) {
572 mStatusMessageView.setVisibility(View.GONE);
573 return;
574 }
575
576 mStatusMessageView.setVisibility(View.VISIBLE);
577 mStatusMessageText.setText(message.callDetailsMessageId);
578 if (message.actionMessageId != -1) {
579 mStatusMessageAction.setText(message.actionMessageId);
580 }
581 if (message.actionUri != null) {
582 mStatusMessageAction.setClickable(true);
583 mStatusMessageAction.setOnClickListener(new View.OnClickListener() {
584 @Override
585 public void onClick(View v) {
586 startActivity(new Intent(Intent.ACTION_VIEW, message.actionUri));
587 }
588 });
589 } else {
590 mStatusMessageAction.setClickable(false);
591 }
592 }
593
594 private StatusMessage getStatusMessage(Cursor statusCursor) {
595 List<StatusMessage> messages = mVoicemailStatusHelper.getStatusMessages(statusCursor);
596 Log.d(TAG, "Num status messages: " + messages.size());
597 if (messages.size() == 0) {
598 return null;
599 }
600 // There can only be a single status message per source package, so num of messages can
601 // at most be 1.
602 if (messages.size() > 1) {
603 Log.w(TAG, String.format("Expected 1, found (%d) num of status messages." +
604 " Will use the first one.", messages.size()));
605 }
606 return messages.get(0);
607 }
Flavio Lerda94d7bab2011-07-22 16:52:34 +0100608
609 @Override
610 public boolean onCreateOptionsMenu(Menu menu) {
611 getMenuInflater().inflate(R.menu.call_details_options, menu);
612 return true;
613 }
614
615 @Override
616 public boolean onPrepareOptionsMenu(Menu menu) {
617 // This action deletes all elements in the group from the call log.
618 // We don't have this action for voicemails, because you can just use the trash button.
619 menu.findItem(R.id.remove_from_call_log).setVisible(mHasRemoveFromCallLog);
620 menu.findItem(R.id.edit_number_before_call).setVisible(mHasEditNumberBeforeCall);
621 return mHasRemoveFromCallLog || mHasEditNumberBeforeCall;
622 }
623
624 @Override
625 public boolean onMenuItemSelected(int featureId, MenuItem item) {
626 switch (item.getItemId()) {
627 case R.id.remove_from_call_log: {
628 StringBuilder callIds = new StringBuilder();
629 for (Uri callUri : getCallLogEntryUris()) {
630 if (callIds.length() != 0) {
631 callIds.append(",");
632 }
633 callIds.append(ContentUris.parseId(callUri));
634 }
635
636 getContentResolver().delete(Calls.CONTENT_URI_WITH_VOICEMAIL,
637 Calls._ID + " IN (" + callIds + ")", null);
638 // Also close the activity.
639 finish();
640 return true;
641 }
642
643 case R.id.edit_number_before_call:
644 startActivity(
645 new Intent(Intent.ACTION_DIAL, mPhoneNumberHelper.getCallUri(mNumber)));
646 return true;
647
Flavio Lerdad44e83a2011-07-24 23:10:17 +0100648 case android.R.id.home: {
649 onHomeSelected();
650 return true;
651 }
652
Flavio Lerda94d7bab2011-07-22 16:52:34 +0100653 default:
654 throw new IllegalArgumentException();
655 }
656 }
Flavio Lerdad44e83a2011-07-24 23:10:17 +0100657
658 private void configureActionBar() {
659 ActionBar actionBar = getActionBar();
660 if (actionBar != null) {
661 actionBar.setDisplayOptions(ActionBar.DISPLAY_HOME_AS_UP | ActionBar.DISPLAY_SHOW_HOME,
662 ActionBar.DISPLAY_HOME_AS_UP | ActionBar.DISPLAY_SHOW_TITLE
663 | ActionBar.DISPLAY_SHOW_HOME);
664 actionBar.setIcon(R.drawable.ic_ab_dialer_holo_dark);
665 }
666 }
667
668 /** Invoked when the user presses the home button in the action bar. */
669 private void onHomeSelected() {
670 Intent intent = new Intent(Intent.ACTION_VIEW, Calls.CONTENT_URI);
671 // This will open the call log even if the detail view has been opened directly.
672 intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
673 startActivity(intent);
674 finish();
675 }
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800676}