blob: a46d2e260bee3d017d5248fbbc169d8003b21dfa [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 "remove from call log" in the options menu. */
106 private boolean mHasRemoveFromCallLog;
107 /** Whether we should show "edit number before call" in the options menu. */
108 private boolean mHasEditNumberBeforeCall;
109
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800110 static final String[] CALL_LOG_PROJECTION = new String[] {
111 CallLog.Calls.DATE,
112 CallLog.Calls.DURATION,
113 CallLog.Calls.NUMBER,
114 CallLog.Calls.TYPE,
Bai Tao09eb04f2010-09-01 15:34:16 +0800115 CallLog.Calls.COUNTRY_ISO,
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800116 };
Dmitri Plotnikovd0d776d2009-08-19 17:38:04 -0700117
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800118 static final int DATE_COLUMN_INDEX = 0;
119 static final int DURATION_COLUMN_INDEX = 1;
120 static final int NUMBER_COLUMN_INDEX = 2;
121 static final int CALL_TYPE_COLUMN_INDEX = 3;
Bai Tao09eb04f2010-09-01 15:34:16 +0800122 static final int COUNTRY_ISO_COLUMN_INDEX = 4;
Dmitri Plotnikovd0d776d2009-08-19 17:38:04 -0700123
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800124 static final String[] PHONES_PROJECTION = new String[] {
Dmitri Plotnikovd0d776d2009-08-19 17:38:04 -0700125 PhoneLookup._ID,
126 PhoneLookup.DISPLAY_NAME,
127 PhoneLookup.TYPE,
128 PhoneLookup.LABEL,
129 PhoneLookup.NUMBER,
Bai Tao09eb04f2010-09-01 15:34:16 +0800130 PhoneLookup.NORMALIZED_NUMBER,
Daniel Lehmann362654a2011-07-17 14:16:47 -0700131 PhoneLookup.PHOTO_URI,
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800132 };
133 static final int COLUMN_INDEX_ID = 0;
134 static final int COLUMN_INDEX_NAME = 1;
135 static final int COLUMN_INDEX_TYPE = 2;
136 static final int COLUMN_INDEX_LABEL = 3;
137 static final int COLUMN_INDEX_NUMBER = 4;
Bai Tao09eb04f2010-09-01 15:34:16 +0800138 static final int COLUMN_INDEX_NORMALIZED_NUMBER = 5;
Daniel Lehmann362654a2011-07-17 14:16:47 -0700139 static final int COLUMN_INDEX_PHOTO_URI = 6;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800140
141 @Override
142 protected void onCreate(Bundle icicle) {
143 super.onCreate(icicle);
144
145 setContentView(R.layout.call_detail);
146
147 mInflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
148 mResources = getResources();
Dmitri Plotnikovd0d776d2009-08-19 17:38:04 -0700149
Flavio Lerda696e8132011-07-05 19:00:23 +0100150 mPhoneCallDetailsViews = PhoneCallDetailsViews.fromView(getWindow().getDecorView());
Flavio Lerda40569562011-07-22 21:51:29 +0100151 mCallTypeHelper = new CallTypeHelper(getResources(), mInflater);
Flavio Lerda178eeeb2011-07-11 19:51:40 +0100152 mPhoneNumberHelper = new PhoneNumberHelper(mResources, getVoicemailNumber());
Flavio Lerda40569562011-07-22 21:51:29 +0100153 mPhoneCallDetailsHelper = new PhoneCallDetailsHelper(mResources, mCallTypeHelper,
Flavio Lerda178eeeb2011-07-11 19:51:40 +0100154 mPhoneNumberHelper);
Debashish Chatterjee0cb82242011-07-19 19:29:37 +0100155 mVoicemailStatusHelper = new VoicemailStatusHelperImpl();
156 mAsyncQueryHandler = new CallDetailActivityQueryHandler(this);
157 mStatusMessageView = findViewById(R.id.voicemail_status);
158 mStatusMessageText = (TextView) findViewById(R.id.voicemail_status_message);
159 mStatusMessageAction = (TextView) findViewById(R.id.voicemail_status_action);
Flavio Lerdafb63c432011-07-07 17:16:53 +0100160 mMainActionView = (ImageView) findViewById(R.id.main_action);
Daniel Lehmannd260de42011-08-01 18:08:38 -0700161 mMainActionPushLayerView = (ImageButton) findViewById(R.id.main_action_push_layer);
Flavio Lerda9cafe472011-06-08 14:06:13 +0100162 mContactBackgroundView = (ImageView) findViewById(R.id.contact_background);
Bai Tao09eb04f2010-09-01 15:34:16 +0800163 mDefaultCountryIso = ContactsUtils.getCurrentCountryIso(this);
Flavio Lerda9cafe472011-06-08 14:06:13 +0100164 mContactPhotoManager = ContactPhotoManager.getInstance(this);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800165 getListView().setOnItemClickListener(this);
Flavio Lerdad44e83a2011-07-24 23:10:17 +0100166 configureActionBar();
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800167 }
Dmitri Plotnikovd0d776d2009-08-19 17:38:04 -0700168
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800169 @Override
170 public void onResume() {
171 super.onResume();
Flavio Lerda178eeeb2011-07-11 19:51:40 +0100172 updateData(getCallLogEntryUris());
Hugo Hudson757aa552011-07-20 01:15:25 +0100173 optionallyHandleVoicemail();
Hugo Hudsonb002f512011-07-15 17:41:12 +0100174 }
175
176 /**
177 * Handle voicemail playback or hide voicemail ui.
178 * <p>
179 * If the Intent used to start this Activity contains the suitable extras, then start voicemail
180 * playback. If it doesn't, then hide the voicemail ui.
181 */
Hugo Hudson757aa552011-07-20 01:15:25 +0100182 private void optionallyHandleVoicemail() {
Hugo Hudson157dde12011-07-21 23:28:13 +0100183 if (hasVoicemail()) {
Hugo Hudsona9ad6942011-07-29 17:06:04 +0100184 // Has voicemail: add the voicemail fragment. Add suitable arguments to set the uri
185 // to play and optionally start the playback.
Hugo Hudson757aa552011-07-20 01:15:25 +0100186 // Do a query to fetch the voicemail status messages.
Hugo Hudsona9ad6942011-07-29 17:06:04 +0100187 VoicemailPlaybackFragment playbackFragment = new VoicemailPlaybackFragment();
188 Bundle fragmentArguments = new Bundle();
Hugo Hudson157dde12011-07-21 23:28:13 +0100189 Uri voicemailUri = getIntent().getParcelableExtra(EXTRA_VOICEMAIL_URI);
Hugo Hudsona9ad6942011-07-29 17:06:04 +0100190 fragmentArguments.putParcelable(EXTRA_VOICEMAIL_URI, voicemailUri);
191 if (getIntent().getBooleanExtra(EXTRA_VOICEMAIL_START_PLAYBACK, false)) {
192 fragmentArguments.putBoolean(EXTRA_VOICEMAIL_START_PLAYBACK, true);
193 }
194 playbackFragment.setArguments(fragmentArguments);
195 getFragmentManager().beginTransaction()
196 .add(R.id.voicemail_container, playbackFragment).commit();
Hugo Hudson757aa552011-07-20 01:15:25 +0100197 mAsyncQueryHandler.startVoicemailStatusQuery(voicemailUri);
198 } else {
Hugo Hudsona9ad6942011-07-29 17:06:04 +0100199 // No voicemail uri: hide the status view.
Hugo Hudson757aa552011-07-20 01:15:25 +0100200 mStatusMessageView.setVisibility(View.GONE);
Hugo Hudsonb002f512011-07-15 17:41:12 +0100201 }
Flavio Lerda178eeeb2011-07-11 19:51:40 +0100202 }
203
Hugo Hudson157dde12011-07-21 23:28:13 +0100204 private boolean hasVoicemail() {
205 return getIntent().getParcelableExtra(EXTRA_VOICEMAIL_URI) != null;
206 }
207
Flavio Lerda178eeeb2011-07-11 19:51:40 +0100208 /**
209 * Returns the list of URIs to show.
210 * <p>
211 * There are two ways the URIs can be provided to the activity: as the data on the intent, or as
212 * a list of ids in the call log added as an extra on the URI.
213 * <p>
214 * If both are available, the data on the intent takes precedence.
215 */
216 private Uri[] getCallLogEntryUris() {
217 Uri uri = getIntent().getData();
218 if (uri != null) {
219 // If there is a data on the intent, it takes precedence over the extra.
220 return new Uri[]{ uri };
221 }
222 long[] ids = getIntent().getLongArrayExtra(EXTRA_CALL_LOG_IDS);
223 Uri[] uris = new Uri[ids.length];
224 for (int index = 0; index < ids.length; ++index) {
225 uris[index] = ContentUris.withAppendedId(Calls.CONTENT_URI_WITH_VOICEMAIL, ids[index]);
226 }
227 return uris;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800228 }
229
230 @Override
231 public boolean onKeyDown(int keyCode, KeyEvent event) {
232 switch (keyCode) {
233 case KeyEvent.KEYCODE_CALL: {
234 // Make sure phone isn't already busy before starting direct call
235 TelephonyManager tm = (TelephonyManager)
236 getSystemService(Context.TELEPHONY_SERVICE);
237 if (tm.getCallState() == TelephonyManager.CALL_STATE_IDLE) {
238 Intent callIntent = new Intent(Intent.ACTION_CALL_PRIVILEGED,
239 Uri.fromParts("tel", mNumber, null));
240 startActivity(callIntent);
241 return true;
242 }
243 }
244 }
Dmitri Plotnikovd0d776d2009-08-19 17:38:04 -0700245
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800246 return super.onKeyDown(keyCode, event);
247 }
Dmitri Plotnikovd0d776d2009-08-19 17:38:04 -0700248
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800249 /**
250 * Update user interface with details of given call.
Dmitri Plotnikovd0d776d2009-08-19 17:38:04 -0700251 *
Flavio Lerda178eeeb2011-07-11 19:51:40 +0100252 * @param callUris URIs into {@link CallLog.Calls} of the calls to be displayed
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800253 */
Flavio Lerda178eeeb2011-07-11 19:51:40 +0100254 private void updateData(final Uri... callUris) {
255 // TODO: All phone calls correspond to the same person, so we can make a single lookup.
256 final int numCalls = callUris.length;
257 final PhoneCallDetails[] details = new PhoneCallDetails[numCalls];
258 try {
259 for (int index = 0; index < numCalls; ++index) {
260 details[index] = getPhoneCallDetailsForUri(callUris[index]);
261 }
262 } catch (IllegalArgumentException e) {
263 // Something went wrong reading in our primary data, so we're going to
264 // bail out and show error to users.
265 Log.w(TAG, "invalid URI starting call details", e);
266 Toast.makeText(this, R.string.toast_call_detail_error,
267 Toast.LENGTH_SHORT).show();
268 finish();
269 return;
270 }
271
272 // We know that all calls are from the same number and the same contact, so pick the first.
273 mNumber = details[0].number.toString();
274 final long personId = details[0].personId;
Daniel Lehmann362654a2011-07-17 14:16:47 -0700275 final Uri photoUri = details[0].photoUri;
Flavio Lerda178eeeb2011-07-11 19:51:40 +0100276
277 // Set the details header, based on the first phone call.
278 mPhoneCallDetailsHelper.setPhoneCallDetails(mPhoneCallDetailsViews,
Flavio Lerda40569562011-07-22 21:51:29 +0100279 details[0], false, false, true);
Flavio Lerda178eeeb2011-07-11 19:51:40 +0100280
Flavio Lerdacfff16d2011-07-12 23:54:40 +0100281 // Cache the details about the phone number.
282 final Uri numberCallUri = mPhoneNumberHelper.getCallUri(mNumber);
283 final boolean canPlaceCallsTo = mPhoneNumberHelper.canPlaceCallsTo(mNumber);
284 final boolean isVoicemailNumber = mPhoneNumberHelper.isVoicemailNumber(mNumber);
285 final boolean isSipNumber = mPhoneNumberHelper.isSipNumber(mNumber);
286
Flavio Lerda178eeeb2011-07-11 19:51:40 +0100287 // Let user view contact details if they exist, otherwise add option to create new contact
288 // from this number.
289 final Intent mainActionIntent;
290 final int mainActionIcon;
291
292 if (details[0].personId != -1) {
293 Uri personUri = ContentUris.withAppendedId(Contacts.CONTENT_URI, personId);
294 mainActionIntent = new Intent(Intent.ACTION_VIEW, personUri);
Daniel Lehmannd260de42011-08-01 18:08:38 -0700295 mainActionIcon = R.drawable.ic_contacts_holo_dark;
Flavio Lerdacfff16d2011-07-12 23:54:40 +0100296 } else if (isVoicemailNumber) {
297 mainActionIntent = null;
298 mainActionIcon = 0;
299 } else if (isSipNumber) {
300 // TODO: This item is currently disabled for SIP addresses, because
301 // the Insert.PHONE extra only works correctly for PSTN numbers.
302 //
303 // To fix this for SIP addresses, we need to:
304 // - define ContactsContract.Intents.Insert.SIP_ADDRESS, and use it here if
305 // the current number is a SIP address
306 // - update the contacts UI code to handle Insert.SIP_ADDRESS by
307 // updating the SipAddress field
308 // and then we can remove the "!isSipNumber" check above.
309 mainActionIntent = null;
310 mainActionIcon = 0;
Flavio Lerda8e403402011-07-20 16:25:49 +0100311 } else if (canPlaceCallsTo) {
Flavio Lerda178eeeb2011-07-11 19:51:40 +0100312 mainActionIntent = new Intent(Intent.ACTION_INSERT_OR_EDIT);
313 mainActionIntent.setType(Contacts.CONTENT_ITEM_TYPE);
314 mainActionIntent.putExtra(Insert.PHONE, mNumber);
Daniel Lehmannd260de42011-08-01 18:08:38 -0700315 mainActionIcon = R.drawable.ic_add_contact_holo_dark;
Flavio Lerda8e403402011-07-20 16:25:49 +0100316 } else {
317 // If we cannot call the number, when we probably cannot add it as a contact either.
318 // This is usually the case of private, unknown, or payphone numbers.
319 mainActionIntent = null;
320 mainActionIcon = 0;
Flavio Lerda178eeeb2011-07-11 19:51:40 +0100321 }
322
Flavio Lerdacfff16d2011-07-12 23:54:40 +0100323 if (mainActionIntent == null) {
324 mMainActionView.setVisibility(View.INVISIBLE);
Daniel Lehmannd260de42011-08-01 18:08:38 -0700325 mMainActionPushLayerView.setVisibility(View.GONE);
Flavio Lerdacfff16d2011-07-12 23:54:40 +0100326 } else {
327 mMainActionView.setVisibility(View.VISIBLE);
328 mMainActionView.setImageResource(mainActionIcon);
Daniel Lehmannd260de42011-08-01 18:08:38 -0700329 mMainActionPushLayerView.setVisibility(View.VISIBLE);
330 mMainActionPushLayerView.setOnClickListener(new View.OnClickListener() {
Flavio Lerdacfff16d2011-07-12 23:54:40 +0100331 @Override
332 public void onClick(View v) {
333 startActivity(mainActionIntent);
334 }
335 });
336 }
Flavio Lerda178eeeb2011-07-11 19:51:40 +0100337
338 // Build list of various available actions.
339 final List<ViewEntry> actions = new ArrayList<ViewEntry>();
340
Flavio Lerda178eeeb2011-07-11 19:51:40 +0100341 // This action allows to call the number that places the call.
Flavio Lerdacfff16d2011-07-12 23:54:40 +0100342 if (canPlaceCallsTo) {
Flavio Lerda178eeeb2011-07-11 19:51:40 +0100343 actions.add(new ViewEntry(android.R.drawable.sym_action_call,
344 getString(R.string.menu_callNumber, mNumber),
345 new Intent(Intent.ACTION_CALL_PRIVILEGED, numberCallUri)));
346 }
347
Flavio Lerdacfff16d2011-07-12 23:54:40 +0100348 // This action allows to send an SMS to the number that placed the call.
349 if (mPhoneNumberHelper.canSendSmsTo(mNumber)) {
Flavio Lerda178eeeb2011-07-11 19:51:40 +0100350 Intent smsIntent = new Intent(Intent.ACTION_SENDTO,
351 Uri.fromParts("sms", mNumber, null));
352 actions.add(new ViewEntry(R.drawable.sym_action_sms,
353 getString(R.string.menu_sendTextMessage), smsIntent));
354 }
355
356 // This action deletes all elements in the group from the call log.
Hugo Hudson157dde12011-07-21 23:28:13 +0100357 // We don't have this action for voicemails, because you can just use the trash button.
Flavio Lerda94d7bab2011-07-22 16:52:34 +0100358 mHasRemoveFromCallLog = !hasVoicemail();
359 mHasEditNumberBeforeCall = canPlaceCallsTo && !isSipNumber && !isVoicemailNumber;
Flavio Lerda178eeeb2011-07-11 19:51:40 +0100360
Flavio Lerda94d7bab2011-07-22 16:52:34 +0100361 if (actions.size() != 0) {
362 // Set the actions for this phone number.
363 setListAdapter(new ViewAdapter(this, actions));
364 getListView().setVisibility(View.VISIBLE);
365 } else {
366 getListView().setVisibility(View.GONE);
Hugo Hudson157dde12011-07-21 23:28:13 +0100367 }
Flavio Lerda178eeeb2011-07-11 19:51:40 +0100368
Flavio Lerda178eeeb2011-07-11 19:51:40 +0100369 ListView historyList = (ListView) findViewById(R.id.history);
370 historyList.setAdapter(
371 new CallDetailHistoryAdapter(this, mInflater, mCallTypeHelper, details));
Daniel Lehmann362654a2011-07-17 14:16:47 -0700372 loadContactPhotos(photoUri);
Flavio Lerda178eeeb2011-07-11 19:51:40 +0100373 }
374
375 /** Return the phone call details for a given call log URI. */
376 private PhoneCallDetails getPhoneCallDetailsForUri(Uri callUri) {
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800377 ContentResolver resolver = getContentResolver();
378 Cursor callCursor = resolver.query(callUri, CALL_LOG_PROJECTION, null, null, null);
379 try {
Flavio Lerda178eeeb2011-07-11 19:51:40 +0100380 if (callCursor == null || !callCursor.moveToFirst()) {
381 throw new IllegalArgumentException("Cannot find content: " + callUri);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800382 }
Flavio Lerda178eeeb2011-07-11 19:51:40 +0100383
384 // Read call log specifics.
385 String number = callCursor.getString(NUMBER_COLUMN_INDEX);
386 long date = callCursor.getLong(DATE_COLUMN_INDEX);
387 long duration = callCursor.getLong(DURATION_COLUMN_INDEX);
388 int callType = callCursor.getInt(CALL_TYPE_COLUMN_INDEX);
389 String countryIso = callCursor.getString(COUNTRY_ISO_COLUMN_INDEX);
390 if (TextUtils.isEmpty(countryIso)) {
391 countryIso = mDefaultCountryIso;
392 }
393
394 // Formatted phone number.
395 final CharSequence numberText;
396 // Read contact specifics.
397 CharSequence nameText = "";
398 int numberType = 0;
399 CharSequence numberLabel = "";
400 long personId = -1L;
Daniel Lehmann362654a2011-07-17 14:16:47 -0700401 Uri photoUri = null;
Flavio Lerda178eeeb2011-07-11 19:51:40 +0100402 // If this is not a regular number, there is no point in looking it up in the contacts.
403 if (!mPhoneNumberHelper.canPlaceCallsTo(number)) {
404 numberText = mPhoneNumberHelper.getDisplayNumber(number, null);
405 } else {
406 // Perform a reverse-phonebook lookup to find the contact details.
407 Uri phoneUri = Uri.withAppendedPath(PhoneLookup.CONTENT_FILTER_URI,
408 Uri.encode(number));
409 Cursor phonesCursor = resolver.query(phoneUri, PHONES_PROJECTION, null, null, null);
410 String candidateNumberText = number;
411 try {
412 if (phonesCursor != null && phonesCursor.moveToFirst()) {
413 personId = phonesCursor.getLong(COLUMN_INDEX_ID);
414 nameText = phonesCursor.getString(COLUMN_INDEX_NAME);
Daniel Lehmann362654a2011-07-17 14:16:47 -0700415 String photoUriString = phonesCursor.getString(COLUMN_INDEX_PHOTO_URI);
416 photoUri = photoUriString == null ? null : Uri.parse(photoUriString);
Flavio Lerda178eeeb2011-07-11 19:51:40 +0100417 candidateNumberText = PhoneNumberUtils.formatNumber(
418 phonesCursor.getString(COLUMN_INDEX_NUMBER),
419 phonesCursor.getString(COLUMN_INDEX_NORMALIZED_NUMBER),
420 countryIso);
421 numberType = phonesCursor.getInt(COLUMN_INDEX_TYPE);
422 numberLabel = phonesCursor.getString(COLUMN_INDEX_LABEL);
423 } else {
424 // We could not find this contact in the contacts, just format the phone
425 // number as best as we can. All the other fields will have their default
426 // values.
427 candidateNumberText =
428 PhoneNumberUtils.formatNumber(number, countryIso);
429 }
430 } finally {
431 if (phonesCursor != null) phonesCursor.close();
432 numberText = candidateNumberText;
433 }
434 }
Flavio Lerda9c466372011-07-19 17:38:17 +0100435 return new PhoneCallDetails(number, numberText, countryIso,
Flavio Lerdacb805e82011-07-18 10:31:44 +0100436 new int[]{ callType }, date, duration,
Daniel Lehmann362654a2011-07-17 14:16:47 -0700437 nameText, numberType, numberLabel, personId, photoUri);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800438 } finally {
439 if (callCursor != null) {
440 callCursor.close();
441 }
442 }
443 }
444
Flavio Lerda9cafe472011-06-08 14:06:13 +0100445 /** Load the contact photos and places them in the corresponding views. */
Daniel Lehmann362654a2011-07-17 14:16:47 -0700446 private void loadContactPhotos(Uri photoUri) {
447 mContactPhotoManager.loadPhoto(mContactBackgroundView, photoUri);
Flavio Lerda9cafe472011-06-08 14:06:13 +0100448 }
449
Flavio Lerda696e8132011-07-05 19:00:23 +0100450 private String getVoicemailNumber() {
451 TelephonyManager telephonyManager =
452 (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
453 return telephonyManager.getVoiceMailNumber();
454 }
455
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800456 static final class ViewEntry {
Flavio Lerdaa8956882011-07-09 21:34:38 +0100457 public final int icon;
458 public final String text;
459 public final Intent intent;
460 public final View.OnClickListener action;
461
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800462 public String label = null;
463 public String number = null;
Dmitri Plotnikovd0d776d2009-08-19 17:38:04 -0700464
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800465 public ViewEntry(int icon, String text, Intent intent) {
466 this.icon = icon;
467 this.text = text;
468 this.intent = intent;
Flavio Lerdaa8956882011-07-09 21:34:38 +0100469 this.action = null;
470 }
471
472 public ViewEntry(int icon, String text, View.OnClickListener listener) {
473 this.icon = icon;
474 this.text = text;
475 this.intent = null;
476 this.action = listener;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800477 }
478 }
479
480 static final class ViewAdapter extends BaseAdapter {
Dmitri Plotnikovd0d776d2009-08-19 17:38:04 -0700481
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800482 private final List<ViewEntry> mActions;
Dmitri Plotnikovd0d776d2009-08-19 17:38:04 -0700483
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800484 private final LayoutInflater mInflater;
Dmitri Plotnikovd0d776d2009-08-19 17:38:04 -0700485
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800486 public ViewAdapter(Context context, List<ViewEntry> actions) {
487 mActions = actions;
488 mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
489 }
Dmitri Plotnikovd0d776d2009-08-19 17:38:04 -0700490
Flavio Lerda9cafe472011-06-08 14:06:13 +0100491 @Override
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800492 public int getCount() {
493 return mActions.size();
494 }
495
Flavio Lerda9cafe472011-06-08 14:06:13 +0100496 @Override
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800497 public Object getItem(int position) {
498 return mActions.get(position);
499 }
500
Flavio Lerda9cafe472011-06-08 14:06:13 +0100501 @Override
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800502 public long getItemId(int position) {
503 return position;
504 }
Dmitri Plotnikovd0d776d2009-08-19 17:38:04 -0700505
Flavio Lerda9cafe472011-06-08 14:06:13 +0100506 @Override
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800507 public View getView(int position, View convertView, ViewGroup parent) {
508 // Make sure we have a valid convertView to start with
509 if (convertView == null) {
510 convertView = mInflater.inflate(R.layout.call_detail_list_item, parent, false);
511 }
512
513 // Fill action with icon and text.
514 ViewEntry entry = mActions.get(position);
515 convertView.setTag(entry);
Dmitri Plotnikovd0d776d2009-08-19 17:38:04 -0700516
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800517 ImageView icon = (ImageView) convertView.findViewById(R.id.icon);
518 TextView text = (TextView) convertView.findViewById(android.R.id.text1);
519
520 icon.setImageResource(entry.icon);
521 text.setText(entry.text);
522
523 View line2 = convertView.findViewById(R.id.line2);
524 boolean numberEmpty = TextUtils.isEmpty(entry.number);
525 boolean labelEmpty = TextUtils.isEmpty(entry.label) || numberEmpty;
526 if (labelEmpty && numberEmpty) {
527 line2.setVisibility(View.GONE);
528 } else {
529 line2.setVisibility(View.VISIBLE);
Dmitri Plotnikovd0d776d2009-08-19 17:38:04 -0700530
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800531 TextView label = (TextView) convertView.findViewById(R.id.label);
532 if (labelEmpty) {
533 label.setVisibility(View.GONE);
534 } else {
535 label.setText(entry.label);
536 label.setVisibility(View.VISIBLE);
537 }
538
539 TextView number = (TextView) convertView.findViewById(R.id.number);
540 number.setText(entry.number);
541 }
Dmitri Plotnikovd0d776d2009-08-19 17:38:04 -0700542
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800543 return convertView;
544 }
545 }
Dmitri Plotnikovd0d776d2009-08-19 17:38:04 -0700546
Flavio Lerda9cafe472011-06-08 14:06:13 +0100547 @Override
548 public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800549 // Handle passing action off to correct handler.
550 if (view.getTag() instanceof ViewEntry) {
551 ViewEntry entry = (ViewEntry) view.getTag();
552 if (entry.intent != null) {
553 startActivity(entry.intent);
Flavio Lerdaa8956882011-07-09 21:34:38 +0100554 } else if (entry.action != null) {
555 entry.action.onClick(view);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800556 }
557 }
Dmitri Plotnikovd0d776d2009-08-19 17:38:04 -0700558 }
Dmitri Plotnikov8e86b752010-02-22 17:47:57 -0800559
560 @Override
561 public void startSearch(String initialQuery, boolean selectInitialQuery, Bundle appSearchData,
562 boolean globalSearch) {
563 if (globalSearch) {
564 super.startSearch(initialQuery, selectInitialQuery, appSearchData, globalSearch);
565 } else {
566 ContactsSearchManager.startSearch(this, initialQuery);
567 }
568 }
Debashish Chatterjee0cb82242011-07-19 19:29:37 +0100569
570 protected void updateVoicemailStatusMessage(Cursor statusCursor) {
571 if (statusCursor == null) {
572 mStatusMessageView.setVisibility(View.GONE);
573 return;
574 }
575 final StatusMessage message = getStatusMessage(statusCursor);
576 if (message == null || !message.showInCallDetails()) {
577 mStatusMessageView.setVisibility(View.GONE);
578 return;
579 }
580
581 mStatusMessageView.setVisibility(View.VISIBLE);
582 mStatusMessageText.setText(message.callDetailsMessageId);
583 if (message.actionMessageId != -1) {
584 mStatusMessageAction.setText(message.actionMessageId);
585 }
586 if (message.actionUri != null) {
587 mStatusMessageAction.setClickable(true);
588 mStatusMessageAction.setOnClickListener(new View.OnClickListener() {
589 @Override
590 public void onClick(View v) {
591 startActivity(new Intent(Intent.ACTION_VIEW, message.actionUri));
592 }
593 });
594 } else {
595 mStatusMessageAction.setClickable(false);
596 }
597 }
598
599 private StatusMessage getStatusMessage(Cursor statusCursor) {
600 List<StatusMessage> messages = mVoicemailStatusHelper.getStatusMessages(statusCursor);
601 Log.d(TAG, "Num status messages: " + messages.size());
602 if (messages.size() == 0) {
603 return null;
604 }
605 // There can only be a single status message per source package, so num of messages can
606 // at most be 1.
607 if (messages.size() > 1) {
608 Log.w(TAG, String.format("Expected 1, found (%d) num of status messages." +
609 " Will use the first one.", messages.size()));
610 }
611 return messages.get(0);
612 }
Flavio Lerda94d7bab2011-07-22 16:52:34 +0100613
614 @Override
615 public boolean onCreateOptionsMenu(Menu menu) {
616 getMenuInflater().inflate(R.menu.call_details_options, menu);
617 return true;
618 }
619
620 @Override
621 public boolean onPrepareOptionsMenu(Menu menu) {
622 // This action deletes all elements in the group from the call log.
623 // We don't have this action for voicemails, because you can just use the trash button.
624 menu.findItem(R.id.remove_from_call_log).setVisible(mHasRemoveFromCallLog);
625 menu.findItem(R.id.edit_number_before_call).setVisible(mHasEditNumberBeforeCall);
626 return mHasRemoveFromCallLog || mHasEditNumberBeforeCall;
627 }
628
629 @Override
630 public boolean onMenuItemSelected(int featureId, MenuItem item) {
631 switch (item.getItemId()) {
632 case R.id.remove_from_call_log: {
633 StringBuilder callIds = new StringBuilder();
634 for (Uri callUri : getCallLogEntryUris()) {
635 if (callIds.length() != 0) {
636 callIds.append(",");
637 }
638 callIds.append(ContentUris.parseId(callUri));
639 }
640
641 getContentResolver().delete(Calls.CONTENT_URI_WITH_VOICEMAIL,
642 Calls._ID + " IN (" + callIds + ")", null);
643 // Also close the activity.
644 finish();
645 return true;
646 }
647
648 case R.id.edit_number_before_call:
649 startActivity(
650 new Intent(Intent.ACTION_DIAL, mPhoneNumberHelper.getCallUri(mNumber)));
651 return true;
652
Flavio Lerdad44e83a2011-07-24 23:10:17 +0100653 case android.R.id.home: {
654 onHomeSelected();
655 return true;
656 }
657
Flavio Lerda94d7bab2011-07-22 16:52:34 +0100658 default:
659 throw new IllegalArgumentException();
660 }
661 }
Flavio Lerdad44e83a2011-07-24 23:10:17 +0100662
663 private void configureActionBar() {
664 ActionBar actionBar = getActionBar();
665 if (actionBar != null) {
666 actionBar.setDisplayOptions(ActionBar.DISPLAY_HOME_AS_UP | ActionBar.DISPLAY_SHOW_HOME,
667 ActionBar.DISPLAY_HOME_AS_UP | ActionBar.DISPLAY_SHOW_TITLE
668 | ActionBar.DISPLAY_SHOW_HOME);
669 actionBar.setIcon(R.drawable.ic_ab_dialer_holo_dark);
670 }
671 }
672
673 /** Invoked when the user presses the home button in the action bar. */
674 private void onHomeSelected() {
675 Intent intent = new Intent(Intent.ACTION_VIEW, Calls.CONTENT_URI);
676 // This will open the call log even if the detail view has been opened directly.
677 intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
678 startActivity(intent);
679 finish();
680 }
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800681}