blob: 2ecbc29bee0a780f316d54aaa2e9c5f61cb85cbc [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;
Daniel Lehmannd260de42011-08-01 18:08:38 -070054import android.widget.ImageButton;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080055import android.widget.ImageView;
Flavio Lerda9a208cc2011-07-12 21:05:47 +010056import android.widget.ListView;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080057import android.widget.TextView;
58import android.widget.Toast;
59
60import java.util.ArrayList;
61import java.util.List;
62
63/**
64 * Displays the details of a specific call log entry.
Flavio Lerda178eeeb2011-07-11 19:51:40 +010065 * <p>
66 * This activity can be either started with the URI of a single call log entry, or with the
67 * {@link #EXTRA_CALL_LOG_IDS} extra to specify a group of call log entries.
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080068 */
69public class CallDetailActivity extends ListActivity implements
70 AdapterView.OnItemClickListener {
71 private static final String TAG = "CallDetail";
72
Flavio Lerda178eeeb2011-07-11 19:51:40 +010073 /** A long array extra containing ids of call log entries to display. */
Hugo Hudsonb002f512011-07-15 17:41:12 +010074 public static final String EXTRA_CALL_LOG_IDS = "EXTRA_CALL_LOG_IDS";
75 /** If we are started with a voicemail, we'll find the uri to play with this extra. */
76 public static final String EXTRA_VOICEMAIL_URI = "EXTRA_VOICEMAIL_URI";
77 /** If we should immediately start playback of the voicemail, this extra will be set to true. */
78 public static final String EXTRA_VOICEMAIL_START_PLAYBACK = "EXTRA_VOICEMAIL_START_PLAYBACK";
Flavio Lerda178eeeb2011-07-11 19:51:40 +010079
Flavio Lerdaa024c3f2011-06-10 10:47:07 +010080 /** The views representing the details of a phone call. */
Flavio Lerdaafb93bb2011-07-05 14:46:10 +010081 private PhoneCallDetailsViews mPhoneCallDetailsViews;
Flavio Lerda9a208cc2011-07-12 21:05:47 +010082 private CallTypeHelper mCallTypeHelper;
Flavio Lerda178eeeb2011-07-11 19:51:40 +010083 private PhoneNumberHelper mPhoneNumberHelper;
Flavio Lerdaafb93bb2011-07-05 14:46:10 +010084 private PhoneCallDetailsHelper mPhoneCallDetailsHelper;
Flavio Lerdafb63c432011-07-07 17:16:53 +010085 private ImageView mMainActionView;
Daniel Lehmannd260de42011-08-01 18:08:38 -070086 private ImageButton mMainActionPushLayerView;
Flavio Lerda9cafe472011-06-08 14:06:13 +010087 private ImageView mContactBackgroundView;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080088
89 private String mNumber = null;
Bai Tao09eb04f2010-09-01 15:34:16 +080090 private String mDefaultCountryIso;
Dmitri Plotnikovd0d776d2009-08-19 17:38:04 -070091
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080092 /* package */ LayoutInflater mInflater;
93 /* package */ Resources mResources;
Flavio Lerda9cafe472011-06-08 14:06:13 +010094 /** Helper to load contact photos. */
95 private ContactPhotoManager mContactPhotoManager;
Debashish Chatterjee0cb82242011-07-19 19:29:37 +010096 /** Helper to make async queries to content resolver. */
97 private CallDetailActivityQueryHandler mAsyncQueryHandler;
98 /** Helper to get voicemail status messages. */
99 private VoicemailStatusHelper mVoicemailStatusHelper;
100 // Views related to voicemail status message.
101 private View mStatusMessageView;
102 private TextView mStatusMessageText;
103 private TextView mStatusMessageAction;
Dmitri Plotnikovd0d776d2009-08-19 17:38:04 -0700104
Flavio Lerda94d7bab2011-07-22 16:52:34 +0100105 /** 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());
Daniel Lehmann6ecb7322011-08-01 21:39:29 -0700149 mCallTypeHelper = new CallTypeHelper(getResources());
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);
Daniel Lehmannd260de42011-08-01 18:08:38 -0700159 mMainActionPushLayerView = (ImageButton) findViewById(R.id.main_action_push_layer);
Flavio Lerda9cafe472011-06-08 14:06:13 +0100160 mContactBackgroundView = (ImageView) findViewById(R.id.contact_background);
Bai Tao09eb04f2010-09-01 15:34:16 +0800161 mDefaultCountryIso = ContactsUtils.getCurrentCountryIso(this);
Flavio Lerda9cafe472011-06-08 14:06:13 +0100162 mContactPhotoManager = ContactPhotoManager.getInstance(this);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800163 getListView().setOnItemClickListener(this);
Flavio Lerdad44e83a2011-07-24 23:10:17 +0100164 configureActionBar();
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800165 }
Dmitri Plotnikovd0d776d2009-08-19 17:38:04 -0700166
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800167 @Override
168 public void onResume() {
169 super.onResume();
Flavio Lerda178eeeb2011-07-11 19:51:40 +0100170 updateData(getCallLogEntryUris());
Hugo Hudson757aa552011-07-20 01:15:25 +0100171 optionallyHandleVoicemail();
Hugo Hudsonb002f512011-07-15 17:41:12 +0100172 }
173
174 /**
175 * Handle voicemail playback or hide voicemail ui.
176 * <p>
177 * If the Intent used to start this Activity contains the suitable extras, then start voicemail
178 * playback. If it doesn't, then hide the voicemail ui.
179 */
Hugo Hudson757aa552011-07-20 01:15:25 +0100180 private void optionallyHandleVoicemail() {
Hugo Hudson157dde12011-07-21 23:28:13 +0100181 if (hasVoicemail()) {
Hugo Hudsona9ad6942011-07-29 17:06:04 +0100182 // Has voicemail: add the voicemail fragment. Add suitable arguments to set the uri
183 // to play and optionally start the playback.
Hugo Hudson757aa552011-07-20 01:15:25 +0100184 // Do a query to fetch the voicemail status messages.
Hugo Hudsona9ad6942011-07-29 17:06:04 +0100185 VoicemailPlaybackFragment playbackFragment = new VoicemailPlaybackFragment();
186 Bundle fragmentArguments = new Bundle();
Hugo Hudson7b02fde2011-08-02 20:56:48 +0100187 fragmentArguments.putParcelable(EXTRA_VOICEMAIL_URI, getVoicemailUri());
Hugo Hudsona9ad6942011-07-29 17:06:04 +0100188 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 Hudson7b02fde2011-08-02 20:56:48 +0100194 mAsyncQueryHandler.startVoicemailStatusQuery(getVoicemailUri());
Hugo Hudson757aa552011-07-20 01:15:25 +0100195 } 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() {
Hugo Hudson7b02fde2011-08-02 20:56:48 +0100202 return getVoicemailUri() != null;
203 }
204
205 private Uri getVoicemailUri() {
206 return getIntent().getParcelableExtra(EXTRA_VOICEMAIL_URI);
Hugo Hudson157dde12011-07-21 23:28:13 +0100207 }
208
Flavio Lerda178eeeb2011-07-11 19:51:40 +0100209 /**
210 * Returns the list of URIs to show.
211 * <p>
212 * There are two ways the URIs can be provided to the activity: as the data on the intent, or as
213 * a list of ids in the call log added as an extra on the URI.
214 * <p>
215 * If both are available, the data on the intent takes precedence.
216 */
217 private Uri[] getCallLogEntryUris() {
218 Uri uri = getIntent().getData();
219 if (uri != null) {
220 // If there is a data on the intent, it takes precedence over the extra.
221 return new Uri[]{ uri };
222 }
223 long[] ids = getIntent().getLongArrayExtra(EXTRA_CALL_LOG_IDS);
224 Uri[] uris = new Uri[ids.length];
225 for (int index = 0; index < ids.length; ++index) {
226 uris[index] = ContentUris.withAppendedId(Calls.CONTENT_URI_WITH_VOICEMAIL, ids[index]);
227 }
228 return uris;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800229 }
230
231 @Override
232 public boolean onKeyDown(int keyCode, KeyEvent event) {
233 switch (keyCode) {
234 case KeyEvent.KEYCODE_CALL: {
235 // Make sure phone isn't already busy before starting direct call
236 TelephonyManager tm = (TelephonyManager)
237 getSystemService(Context.TELEPHONY_SERVICE);
238 if (tm.getCallState() == TelephonyManager.CALL_STATE_IDLE) {
239 Intent callIntent = new Intent(Intent.ACTION_CALL_PRIVILEGED,
240 Uri.fromParts("tel", mNumber, null));
241 startActivity(callIntent);
242 return true;
243 }
244 }
245 }
Dmitri Plotnikovd0d776d2009-08-19 17:38:04 -0700246
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800247 return super.onKeyDown(keyCode, event);
248 }
Dmitri Plotnikovd0d776d2009-08-19 17:38:04 -0700249
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800250 /**
251 * Update user interface with details of given call.
Dmitri Plotnikovd0d776d2009-08-19 17:38:04 -0700252 *
Flavio Lerda178eeeb2011-07-11 19:51:40 +0100253 * @param callUris URIs into {@link CallLog.Calls} of the calls to be displayed
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800254 */
Flavio Lerda178eeeb2011-07-11 19:51:40 +0100255 private void updateData(final Uri... callUris) {
256 // TODO: All phone calls correspond to the same person, so we can make a single lookup.
257 final int numCalls = callUris.length;
258 final PhoneCallDetails[] details = new PhoneCallDetails[numCalls];
259 try {
260 for (int index = 0; index < numCalls; ++index) {
261 details[index] = getPhoneCallDetailsForUri(callUris[index]);
262 }
263 } catch (IllegalArgumentException e) {
264 // Something went wrong reading in our primary data, so we're going to
265 // bail out and show error to users.
266 Log.w(TAG, "invalid URI starting call details", e);
267 Toast.makeText(this, R.string.toast_call_detail_error,
268 Toast.LENGTH_SHORT).show();
269 finish();
270 return;
271 }
272
273 // We know that all calls are from the same number and the same contact, so pick the first.
274 mNumber = details[0].number.toString();
275 final long personId = details[0].personId;
Daniel Lehmann362654a2011-07-17 14:16:47 -0700276 final Uri photoUri = details[0].photoUri;
Flavio Lerda178eeeb2011-07-11 19:51:40 +0100277
278 // Set the details header, based on the first phone call.
279 mPhoneCallDetailsHelper.setPhoneCallDetails(mPhoneCallDetailsViews,
Flavio Lerda40569562011-07-22 21:51:29 +0100280 details[0], false, false, true);
Flavio Lerda178eeeb2011-07-11 19:51:40 +0100281
Flavio Lerdacfff16d2011-07-12 23:54:40 +0100282 // Cache the details about the phone number.
283 final Uri numberCallUri = mPhoneNumberHelper.getCallUri(mNumber);
284 final boolean canPlaceCallsTo = mPhoneNumberHelper.canPlaceCallsTo(mNumber);
285 final boolean isVoicemailNumber = mPhoneNumberHelper.isVoicemailNumber(mNumber);
286 final boolean isSipNumber = mPhoneNumberHelper.isSipNumber(mNumber);
287
Flavio Lerda178eeeb2011-07-11 19:51:40 +0100288 // Let user view contact details if they exist, otherwise add option to create new contact
289 // from this number.
290 final Intent mainActionIntent;
291 final int mainActionIcon;
292
293 if (details[0].personId != -1) {
294 Uri personUri = ContentUris.withAppendedId(Contacts.CONTENT_URI, personId);
295 mainActionIntent = new Intent(Intent.ACTION_VIEW, personUri);
Daniel Lehmannd260de42011-08-01 18:08:38 -0700296 mainActionIcon = R.drawable.ic_contacts_holo_dark;
Flavio Lerdacfff16d2011-07-12 23:54:40 +0100297 } else if (isVoicemailNumber) {
298 mainActionIntent = null;
299 mainActionIcon = 0;
300 } else if (isSipNumber) {
301 // TODO: This item is currently disabled for SIP addresses, because
302 // the Insert.PHONE extra only works correctly for PSTN numbers.
303 //
304 // To fix this for SIP addresses, we need to:
305 // - define ContactsContract.Intents.Insert.SIP_ADDRESS, and use it here if
306 // the current number is a SIP address
307 // - update the contacts UI code to handle Insert.SIP_ADDRESS by
308 // updating the SipAddress field
309 // and then we can remove the "!isSipNumber" check above.
310 mainActionIntent = null;
311 mainActionIcon = 0;
Flavio Lerda8e403402011-07-20 16:25:49 +0100312 } else if (canPlaceCallsTo) {
Flavio Lerda178eeeb2011-07-11 19:51:40 +0100313 mainActionIntent = new Intent(Intent.ACTION_INSERT_OR_EDIT);
314 mainActionIntent.setType(Contacts.CONTENT_ITEM_TYPE);
315 mainActionIntent.putExtra(Insert.PHONE, mNumber);
Daniel Lehmannd260de42011-08-01 18:08:38 -0700316 mainActionIcon = R.drawable.ic_add_contact_holo_dark;
Flavio Lerda8e403402011-07-20 16:25:49 +0100317 } else {
318 // If we cannot call the number, when we probably cannot add it as a contact either.
319 // This is usually the case of private, unknown, or payphone numbers.
320 mainActionIntent = null;
321 mainActionIcon = 0;
Flavio Lerda178eeeb2011-07-11 19:51:40 +0100322 }
323
Flavio Lerdacfff16d2011-07-12 23:54:40 +0100324 if (mainActionIntent == null) {
325 mMainActionView.setVisibility(View.INVISIBLE);
Daniel Lehmannd260de42011-08-01 18:08:38 -0700326 mMainActionPushLayerView.setVisibility(View.GONE);
Flavio Lerdacfff16d2011-07-12 23:54:40 +0100327 } else {
328 mMainActionView.setVisibility(View.VISIBLE);
329 mMainActionView.setImageResource(mainActionIcon);
Daniel Lehmannd260de42011-08-01 18:08:38 -0700330 mMainActionPushLayerView.setVisibility(View.VISIBLE);
331 mMainActionPushLayerView.setOnClickListener(new View.OnClickListener() {
Flavio Lerdacfff16d2011-07-12 23:54:40 +0100332 @Override
333 public void onClick(View v) {
334 startActivity(mainActionIntent);
335 }
336 });
337 }
Flavio Lerda178eeeb2011-07-11 19:51:40 +0100338
339 // Build list of various available actions.
340 final List<ViewEntry> actions = new ArrayList<ViewEntry>();
341
Flavio Lerda178eeeb2011-07-11 19:51:40 +0100342 // This action allows to call the number that places the call.
Flavio Lerdacfff16d2011-07-12 23:54:40 +0100343 if (canPlaceCallsTo) {
Flavio Lerda178eeeb2011-07-11 19:51:40 +0100344 actions.add(new ViewEntry(android.R.drawable.sym_action_call,
345 getString(R.string.menu_callNumber, mNumber),
346 new Intent(Intent.ACTION_CALL_PRIVILEGED, numberCallUri)));
347 }
348
Flavio Lerdacfff16d2011-07-12 23:54:40 +0100349 // This action allows to send an SMS to the number that placed the call.
350 if (mPhoneNumberHelper.canSendSmsTo(mNumber)) {
Flavio Lerda178eeeb2011-07-11 19:51:40 +0100351 Intent smsIntent = new Intent(Intent.ACTION_SENDTO,
352 Uri.fromParts("sms", mNumber, null));
353 actions.add(new ViewEntry(R.drawable.sym_action_sms,
354 getString(R.string.menu_sendTextMessage), smsIntent));
355 }
356
Flavio Lerda94d7bab2011-07-22 16:52:34 +0100357 mHasEditNumberBeforeCall = canPlaceCallsTo && !isSipNumber && !isVoicemailNumber;
Flavio Lerda178eeeb2011-07-11 19:51:40 +0100358
Flavio Lerda94d7bab2011-07-22 16:52:34 +0100359 if (actions.size() != 0) {
360 // Set the actions for this phone number.
361 setListAdapter(new ViewAdapter(this, actions));
362 getListView().setVisibility(View.VISIBLE);
363 } else {
364 getListView().setVisibility(View.GONE);
Hugo Hudson157dde12011-07-21 23:28:13 +0100365 }
Flavio Lerda178eeeb2011-07-11 19:51:40 +0100366
Flavio Lerda178eeeb2011-07-11 19:51:40 +0100367 ListView historyList = (ListView) findViewById(R.id.history);
368 historyList.setAdapter(
369 new CallDetailHistoryAdapter(this, mInflater, mCallTypeHelper, details));
Daniel Lehmann362654a2011-07-17 14:16:47 -0700370 loadContactPhotos(photoUri);
Flavio Lerda178eeeb2011-07-11 19:51:40 +0100371 }
372
373 /** Return the phone call details for a given call log URI. */
374 private PhoneCallDetails getPhoneCallDetailsForUri(Uri callUri) {
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800375 ContentResolver resolver = getContentResolver();
376 Cursor callCursor = resolver.query(callUri, CALL_LOG_PROJECTION, null, null, null);
377 try {
Flavio Lerda178eeeb2011-07-11 19:51:40 +0100378 if (callCursor == null || !callCursor.moveToFirst()) {
379 throw new IllegalArgumentException("Cannot find content: " + callUri);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800380 }
Flavio Lerda178eeeb2011-07-11 19:51:40 +0100381
382 // Read call log specifics.
383 String number = callCursor.getString(NUMBER_COLUMN_INDEX);
384 long date = callCursor.getLong(DATE_COLUMN_INDEX);
385 long duration = callCursor.getLong(DURATION_COLUMN_INDEX);
386 int callType = callCursor.getInt(CALL_TYPE_COLUMN_INDEX);
387 String countryIso = callCursor.getString(COUNTRY_ISO_COLUMN_INDEX);
388 if (TextUtils.isEmpty(countryIso)) {
389 countryIso = mDefaultCountryIso;
390 }
391
392 // Formatted phone number.
393 final CharSequence numberText;
394 // Read contact specifics.
395 CharSequence nameText = "";
396 int numberType = 0;
397 CharSequence numberLabel = "";
398 long personId = -1L;
Daniel Lehmann362654a2011-07-17 14:16:47 -0700399 Uri photoUri = null;
Flavio Lerda178eeeb2011-07-11 19:51:40 +0100400 // If this is not a regular number, there is no point in looking it up in the contacts.
401 if (!mPhoneNumberHelper.canPlaceCallsTo(number)) {
402 numberText = mPhoneNumberHelper.getDisplayNumber(number, null);
403 } else {
404 // Perform a reverse-phonebook lookup to find the contact details.
405 Uri phoneUri = Uri.withAppendedPath(PhoneLookup.CONTENT_FILTER_URI,
406 Uri.encode(number));
407 Cursor phonesCursor = resolver.query(phoneUri, PHONES_PROJECTION, null, null, null);
408 String candidateNumberText = number;
409 try {
410 if (phonesCursor != null && phonesCursor.moveToFirst()) {
411 personId = phonesCursor.getLong(COLUMN_INDEX_ID);
412 nameText = phonesCursor.getString(COLUMN_INDEX_NAME);
Daniel Lehmann362654a2011-07-17 14:16:47 -0700413 String photoUriString = phonesCursor.getString(COLUMN_INDEX_PHOTO_URI);
414 photoUri = photoUriString == null ? null : Uri.parse(photoUriString);
Flavio Lerda178eeeb2011-07-11 19:51:40 +0100415 candidateNumberText = PhoneNumberUtils.formatNumber(
416 phonesCursor.getString(COLUMN_INDEX_NUMBER),
417 phonesCursor.getString(COLUMN_INDEX_NORMALIZED_NUMBER),
418 countryIso);
419 numberType = phonesCursor.getInt(COLUMN_INDEX_TYPE);
420 numberLabel = phonesCursor.getString(COLUMN_INDEX_LABEL);
421 } else {
422 // We could not find this contact in the contacts, just format the phone
423 // number as best as we can. All the other fields will have their default
424 // values.
425 candidateNumberText =
426 PhoneNumberUtils.formatNumber(number, countryIso);
427 }
428 } finally {
429 if (phonesCursor != null) phonesCursor.close();
430 numberText = candidateNumberText;
431 }
432 }
Flavio Lerda9c466372011-07-19 17:38:17 +0100433 return new PhoneCallDetails(number, numberText, countryIso,
Flavio Lerdacb805e82011-07-18 10:31:44 +0100434 new int[]{ callType }, date, duration,
Daniel Lehmann362654a2011-07-17 14:16:47 -0700435 nameText, numberType, numberLabel, personId, photoUri);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800436 } finally {
437 if (callCursor != null) {
438 callCursor.close();
439 }
440 }
441 }
442
Flavio Lerda9cafe472011-06-08 14:06:13 +0100443 /** Load the contact photos and places them in the corresponding views. */
Daniel Lehmann362654a2011-07-17 14:16:47 -0700444 private void loadContactPhotos(Uri photoUri) {
445 mContactPhotoManager.loadPhoto(mContactBackgroundView, photoUri);
Flavio Lerda9cafe472011-06-08 14:06:13 +0100446 }
447
Flavio Lerda696e8132011-07-05 19:00:23 +0100448 private String getVoicemailNumber() {
449 TelephonyManager telephonyManager =
450 (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
451 return telephonyManager.getVoiceMailNumber();
452 }
453
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800454 static final class ViewEntry {
Flavio Lerdaa8956882011-07-09 21:34:38 +0100455 public final int icon;
456 public final String text;
457 public final Intent intent;
458 public final View.OnClickListener action;
459
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800460 public String label = null;
461 public String number = null;
Dmitri Plotnikovd0d776d2009-08-19 17:38:04 -0700462
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800463 public ViewEntry(int icon, String text, Intent intent) {
464 this.icon = icon;
465 this.text = text;
466 this.intent = intent;
Flavio Lerdaa8956882011-07-09 21:34:38 +0100467 this.action = null;
468 }
469
470 public ViewEntry(int icon, String text, View.OnClickListener listener) {
471 this.icon = icon;
472 this.text = text;
473 this.intent = null;
474 this.action = listener;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800475 }
476 }
477
478 static final class ViewAdapter extends BaseAdapter {
Dmitri Plotnikovd0d776d2009-08-19 17:38:04 -0700479
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800480 private final List<ViewEntry> mActions;
Dmitri Plotnikovd0d776d2009-08-19 17:38:04 -0700481
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800482 private final LayoutInflater mInflater;
Dmitri Plotnikovd0d776d2009-08-19 17:38:04 -0700483
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800484 public ViewAdapter(Context context, List<ViewEntry> actions) {
485 mActions = actions;
486 mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
487 }
Dmitri Plotnikovd0d776d2009-08-19 17:38:04 -0700488
Flavio Lerda9cafe472011-06-08 14:06:13 +0100489 @Override
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800490 public int getCount() {
491 return mActions.size();
492 }
493
Flavio Lerda9cafe472011-06-08 14:06:13 +0100494 @Override
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800495 public Object getItem(int position) {
496 return mActions.get(position);
497 }
498
Flavio Lerda9cafe472011-06-08 14:06:13 +0100499 @Override
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800500 public long getItemId(int position) {
501 return position;
502 }
Dmitri Plotnikovd0d776d2009-08-19 17:38:04 -0700503
Flavio Lerda9cafe472011-06-08 14:06:13 +0100504 @Override
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800505 public View getView(int position, View convertView, ViewGroup parent) {
506 // Make sure we have a valid convertView to start with
507 if (convertView == null) {
508 convertView = mInflater.inflate(R.layout.call_detail_list_item, parent, false);
509 }
510
511 // Fill action with icon and text.
512 ViewEntry entry = mActions.get(position);
513 convertView.setTag(entry);
Dmitri Plotnikovd0d776d2009-08-19 17:38:04 -0700514
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800515 ImageView icon = (ImageView) convertView.findViewById(R.id.icon);
516 TextView text = (TextView) convertView.findViewById(android.R.id.text1);
517
518 icon.setImageResource(entry.icon);
519 text.setText(entry.text);
520
521 View line2 = convertView.findViewById(R.id.line2);
522 boolean numberEmpty = TextUtils.isEmpty(entry.number);
523 boolean labelEmpty = TextUtils.isEmpty(entry.label) || numberEmpty;
524 if (labelEmpty && numberEmpty) {
525 line2.setVisibility(View.GONE);
526 } else {
527 line2.setVisibility(View.VISIBLE);
Dmitri Plotnikovd0d776d2009-08-19 17:38:04 -0700528
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800529 TextView label = (TextView) convertView.findViewById(R.id.label);
530 if (labelEmpty) {
531 label.setVisibility(View.GONE);
532 } else {
533 label.setText(entry.label);
534 label.setVisibility(View.VISIBLE);
535 }
536
537 TextView number = (TextView) convertView.findViewById(R.id.number);
538 number.setText(entry.number);
539 }
Dmitri Plotnikovd0d776d2009-08-19 17:38:04 -0700540
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800541 return convertView;
542 }
543 }
Dmitri Plotnikovd0d776d2009-08-19 17:38:04 -0700544
Flavio Lerda9cafe472011-06-08 14:06:13 +0100545 @Override
546 public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800547 // Handle passing action off to correct handler.
548 if (view.getTag() instanceof ViewEntry) {
549 ViewEntry entry = (ViewEntry) view.getTag();
550 if (entry.intent != null) {
551 startActivity(entry.intent);
Flavio Lerdaa8956882011-07-09 21:34:38 +0100552 } else if (entry.action != null) {
553 entry.action.onClick(view);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800554 }
555 }
Dmitri Plotnikovd0d776d2009-08-19 17:38:04 -0700556 }
Dmitri Plotnikov8e86b752010-02-22 17:47:57 -0800557
558 @Override
559 public void startSearch(String initialQuery, boolean selectInitialQuery, Bundle appSearchData,
560 boolean globalSearch) {
561 if (globalSearch) {
562 super.startSearch(initialQuery, selectInitialQuery, appSearchData, globalSearch);
563 } else {
564 ContactsSearchManager.startSearch(this, initialQuery);
565 }
566 }
Debashish Chatterjee0cb82242011-07-19 19:29:37 +0100567
568 protected void updateVoicemailStatusMessage(Cursor statusCursor) {
569 if (statusCursor == null) {
570 mStatusMessageView.setVisibility(View.GONE);
571 return;
572 }
573 final StatusMessage message = getStatusMessage(statusCursor);
574 if (message == null || !message.showInCallDetails()) {
575 mStatusMessageView.setVisibility(View.GONE);
576 return;
577 }
578
579 mStatusMessageView.setVisibility(View.VISIBLE);
580 mStatusMessageText.setText(message.callDetailsMessageId);
581 if (message.actionMessageId != -1) {
582 mStatusMessageAction.setText(message.actionMessageId);
583 }
584 if (message.actionUri != null) {
585 mStatusMessageAction.setClickable(true);
586 mStatusMessageAction.setOnClickListener(new View.OnClickListener() {
587 @Override
588 public void onClick(View v) {
589 startActivity(new Intent(Intent.ACTION_VIEW, message.actionUri));
590 }
591 });
592 } else {
593 mStatusMessageAction.setClickable(false);
594 }
595 }
596
597 private StatusMessage getStatusMessage(Cursor statusCursor) {
598 List<StatusMessage> messages = mVoicemailStatusHelper.getStatusMessages(statusCursor);
599 Log.d(TAG, "Num status messages: " + messages.size());
600 if (messages.size() == 0) {
601 return null;
602 }
603 // There can only be a single status message per source package, so num of messages can
604 // at most be 1.
605 if (messages.size() > 1) {
606 Log.w(TAG, String.format("Expected 1, found (%d) num of status messages." +
607 " Will use the first one.", messages.size()));
608 }
609 return messages.get(0);
610 }
Flavio Lerda94d7bab2011-07-22 16:52:34 +0100611
612 @Override
613 public boolean onCreateOptionsMenu(Menu menu) {
614 getMenuInflater().inflate(R.menu.call_details_options, menu);
Hugo Hudsonc2f09c32011-07-30 16:31:28 +0100615 return super.onCreateOptionsMenu(menu);
Flavio Lerda94d7bab2011-07-22 16:52:34 +0100616 }
617
618 @Override
619 public boolean onPrepareOptionsMenu(Menu menu) {
620 // This action deletes all elements in the group from the call log.
621 // We don't have this action for voicemails, because you can just use the trash button.
Hugo Hudsonc2f09c32011-07-30 16:31:28 +0100622 menu.findItem(R.id.menu_remove_from_call_log).setVisible(!hasVoicemail());
623 menu.findItem(R.id.menu_edit_number_before_call).setVisible(mHasEditNumberBeforeCall);
624 menu.findItem(R.id.menu_trash).setVisible(hasVoicemail());
625 menu.findItem(R.id.menu_share_voicemail).setVisible(hasVoicemail());
626 return super.onPrepareOptionsMenu(menu);
Flavio Lerda94d7bab2011-07-22 16:52:34 +0100627 }
628
629 @Override
630 public boolean onMenuItemSelected(int featureId, MenuItem item) {
631 switch (item.getItemId()) {
Flavio Lerdad44e83a2011-07-24 23:10:17 +0100632 case android.R.id.home: {
633 onHomeSelected();
634 return true;
635 }
636
Hugo Hudsonc2f09c32011-07-30 16:31:28 +0100637 // All the options menu items are handled by onMenu... methods.
Flavio Lerda94d7bab2011-07-22 16:52:34 +0100638 default:
639 throw new IllegalArgumentException();
640 }
641 }
Flavio Lerdad44e83a2011-07-24 23:10:17 +0100642
Hugo Hudsonc2f09c32011-07-30 16:31:28 +0100643 public void onMenuRemoveFromCallLog(MenuItem menuItem) {
644 StringBuilder callIds = new StringBuilder();
645 for (Uri callUri : getCallLogEntryUris()) {
646 if (callIds.length() != 0) {
647 callIds.append(",");
648 }
649 callIds.append(ContentUris.parseId(callUri));
650 }
651
652 getContentResolver().delete(Calls.CONTENT_URI_WITH_VOICEMAIL,
653 Calls._ID + " IN (" + callIds + ")", null);
654 // Also close the activity.
655 finish();
656 }
657
658 public void onMenuEditNumberBeforeCall(MenuItem menuItem) {
659 startActivity(new Intent(Intent.ACTION_DIAL, mPhoneNumberHelper.getCallUri(mNumber)));
660 }
661
662 public void onMenuShareVoicemail(MenuItem menuItem) {
663 Log.w(TAG, "onMenuShareVoicemail not yet implemented");
664 }
665
666 public void onMenuTrashVoicemail(MenuItem menuItem) {
Hugo Hudson7b02fde2011-08-02 20:56:48 +0100667 getContentResolver().delete(getVoicemailUri(), null, null);
668 finish();
Hugo Hudsonc2f09c32011-07-30 16:31:28 +0100669 }
670
Flavio Lerdad44e83a2011-07-24 23:10:17 +0100671 private void configureActionBar() {
672 ActionBar actionBar = getActionBar();
673 if (actionBar != null) {
Hugo Hudsonc2f09c32011-07-30 16:31:28 +0100674 actionBar.setDisplayOptions(ActionBar.DISPLAY_HOME_AS_UP | ActionBar.DISPLAY_SHOW_HOME);
Flavio Lerdad44e83a2011-07-24 23:10:17 +0100675 actionBar.setIcon(R.drawable.ic_ab_dialer_holo_dark);
676 }
677 }
678
679 /** Invoked when the user presses the home button in the action bar. */
680 private void onHomeSelected() {
681 Intent intent = new Intent(Intent.ACTION_VIEW, Calls.CONTENT_URI);
682 // This will open the call log even if the detail view has been opened directly.
683 intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
684 startActivity(intent);
685 finish();
686 }
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800687}