blob: cd5ba148a5d0f5efacafa7fa2efd90dc2b082fe5 [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 Lerda9a208cc2011-07-12 21:05:47 +010080 private CallTypeHelper mCallTypeHelper;
Flavio Lerda178eeeb2011-07-11 19:51:40 +010081 private PhoneNumberHelper mPhoneNumberHelper;
Flavio Lerdaafb93bb2011-07-05 14:46:10 +010082 private PhoneCallDetailsHelper mPhoneCallDetailsHelper;
Flavio Lerdab88abaa2011-08-02 23:38:36 +010083 private TextView mHeaderTextView;
Flavio Lerdafb63c432011-07-07 17:16:53 +010084 private ImageView mMainActionView;
Daniel Lehmannd260de42011-08-01 18:08:38 -070085 private ImageButton mMainActionPushLayerView;
Flavio Lerda9cafe472011-06-08 14:06:13 +010086 private ImageView mContactBackgroundView;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080087
88 private String mNumber = null;
Bai Tao09eb04f2010-09-01 15:34:16 +080089 private String mDefaultCountryIso;
Dmitri Plotnikovd0d776d2009-08-19 17:38:04 -070090
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080091 /* package */ LayoutInflater mInflater;
92 /* package */ Resources mResources;
Flavio Lerda9cafe472011-06-08 14:06:13 +010093 /** Helper to load contact photos. */
94 private ContactPhotoManager mContactPhotoManager;
Debashish Chatterjee0cb82242011-07-19 19:29:37 +010095 /** Helper to make async queries to content resolver. */
96 private CallDetailActivityQueryHandler mAsyncQueryHandler;
97 /** Helper to get voicemail status messages. */
98 private VoicemailStatusHelper mVoicemailStatusHelper;
99 // Views related to voicemail status message.
100 private View mStatusMessageView;
101 private TextView mStatusMessageText;
102 private TextView mStatusMessageAction;
Dmitri Plotnikovd0d776d2009-08-19 17:38:04 -0700103
Flavio Lerda94d7bab2011-07-22 16:52:34 +0100104 /** Whether we should show "edit number before call" in the options menu. */
105 private boolean mHasEditNumberBeforeCall;
106
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800107 static final String[] CALL_LOG_PROJECTION = new String[] {
108 CallLog.Calls.DATE,
109 CallLog.Calls.DURATION,
110 CallLog.Calls.NUMBER,
111 CallLog.Calls.TYPE,
Bai Tao09eb04f2010-09-01 15:34:16 +0800112 CallLog.Calls.COUNTRY_ISO,
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800113 };
Dmitri Plotnikovd0d776d2009-08-19 17:38:04 -0700114
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800115 static final int DATE_COLUMN_INDEX = 0;
116 static final int DURATION_COLUMN_INDEX = 1;
117 static final int NUMBER_COLUMN_INDEX = 2;
118 static final int CALL_TYPE_COLUMN_INDEX = 3;
Bai Tao09eb04f2010-09-01 15:34:16 +0800119 static final int COUNTRY_ISO_COLUMN_INDEX = 4;
Dmitri Plotnikovd0d776d2009-08-19 17:38:04 -0700120
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800121 static final String[] PHONES_PROJECTION = new String[] {
Dmitri Plotnikovd0d776d2009-08-19 17:38:04 -0700122 PhoneLookup._ID,
123 PhoneLookup.DISPLAY_NAME,
124 PhoneLookup.TYPE,
125 PhoneLookup.LABEL,
126 PhoneLookup.NUMBER,
Bai Tao09eb04f2010-09-01 15:34:16 +0800127 PhoneLookup.NORMALIZED_NUMBER,
Daniel Lehmann362654a2011-07-17 14:16:47 -0700128 PhoneLookup.PHOTO_URI,
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800129 };
130 static final int COLUMN_INDEX_ID = 0;
131 static final int COLUMN_INDEX_NAME = 1;
132 static final int COLUMN_INDEX_TYPE = 2;
133 static final int COLUMN_INDEX_LABEL = 3;
134 static final int COLUMN_INDEX_NUMBER = 4;
Bai Tao09eb04f2010-09-01 15:34:16 +0800135 static final int COLUMN_INDEX_NORMALIZED_NUMBER = 5;
Daniel Lehmann362654a2011-07-17 14:16:47 -0700136 static final int COLUMN_INDEX_PHOTO_URI = 6;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800137
138 @Override
139 protected void onCreate(Bundle icicle) {
140 super.onCreate(icicle);
141
142 setContentView(R.layout.call_detail);
143
144 mInflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
145 mResources = getResources();
Dmitri Plotnikovd0d776d2009-08-19 17:38:04 -0700146
Daniel Lehmann6ecb7322011-08-01 21:39:29 -0700147 mCallTypeHelper = new CallTypeHelper(getResources());
Flavio Lerda178eeeb2011-07-11 19:51:40 +0100148 mPhoneNumberHelper = new PhoneNumberHelper(mResources, getVoicemailNumber());
Flavio Lerda40569562011-07-22 21:51:29 +0100149 mPhoneCallDetailsHelper = new PhoneCallDetailsHelper(mResources, mCallTypeHelper,
Flavio Lerda178eeeb2011-07-11 19:51:40 +0100150 mPhoneNumberHelper);
Debashish Chatterjee0cb82242011-07-19 19:29:37 +0100151 mVoicemailStatusHelper = new VoicemailStatusHelperImpl();
152 mAsyncQueryHandler = new CallDetailActivityQueryHandler(this);
Flavio Lerdab88abaa2011-08-02 23:38:36 +0100153 mHeaderTextView = (TextView) findViewById(R.id.header_text);
Debashish Chatterjee0cb82242011-07-19 19:29:37 +0100154 mStatusMessageView = findViewById(R.id.voicemail_status);
155 mStatusMessageText = (TextView) findViewById(R.id.voicemail_status_message);
156 mStatusMessageAction = (TextView) findViewById(R.id.voicemail_status_action);
Flavio Lerdafb63c432011-07-07 17:16:53 +0100157 mMainActionView = (ImageView) findViewById(R.id.main_action);
Daniel Lehmannd260de42011-08-01 18:08:38 -0700158 mMainActionPushLayerView = (ImageButton) findViewById(R.id.main_action_push_layer);
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 Hudson7b02fde2011-08-02 20:56:48 +0100186 fragmentArguments.putParcelable(EXTRA_VOICEMAIL_URI, getVoicemailUri());
Hugo Hudsona9ad6942011-07-29 17:06:04 +0100187 if (getIntent().getBooleanExtra(EXTRA_VOICEMAIL_START_PLAYBACK, false)) {
188 fragmentArguments.putBoolean(EXTRA_VOICEMAIL_START_PLAYBACK, true);
189 }
190 playbackFragment.setArguments(fragmentArguments);
191 getFragmentManager().beginTransaction()
192 .add(R.id.voicemail_container, playbackFragment).commit();
Hugo Hudson7b02fde2011-08-02 20:56:48 +0100193 mAsyncQueryHandler.startVoicemailStatusQuery(getVoicemailUri());
Hugo Hudson757aa552011-07-20 01:15:25 +0100194 } else {
Hugo Hudsona9ad6942011-07-29 17:06:04 +0100195 // No voicemail uri: hide the status view.
Hugo Hudson757aa552011-07-20 01:15:25 +0100196 mStatusMessageView.setVisibility(View.GONE);
Hugo Hudsonb002f512011-07-15 17:41:12 +0100197 }
Flavio Lerda178eeeb2011-07-11 19:51:40 +0100198 }
199
Hugo Hudson157dde12011-07-21 23:28:13 +0100200 private boolean hasVoicemail() {
Hugo Hudson7b02fde2011-08-02 20:56:48 +0100201 return getVoicemailUri() != null;
202 }
203
204 private Uri getVoicemailUri() {
205 return getIntent().getParcelableExtra(EXTRA_VOICEMAIL_URI);
Hugo Hudson157dde12011-07-21 23:28:13 +0100206 }
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.
Flavio Lerdab88abaa2011-08-02 23:38:36 +0100278 mPhoneCallDetailsHelper.setPhoneCallName(mHeaderTextView, details[0]);
Flavio Lerda178eeeb2011-07-11 19:51:40 +0100279
Flavio Lerdacfff16d2011-07-12 23:54:40 +0100280 // Cache the details about the phone number.
281 final Uri numberCallUri = mPhoneNumberHelper.getCallUri(mNumber);
282 final boolean canPlaceCallsTo = mPhoneNumberHelper.canPlaceCallsTo(mNumber);
283 final boolean isVoicemailNumber = mPhoneNumberHelper.isVoicemailNumber(mNumber);
284 final boolean isSipNumber = mPhoneNumberHelper.isSipNumber(mNumber);
285
Flavio Lerda178eeeb2011-07-11 19:51:40 +0100286 // Let user view contact details if they exist, otherwise add option to create new contact
287 // from this number.
288 final Intent mainActionIntent;
289 final int mainActionIcon;
290
291 if (details[0].personId != -1) {
292 Uri personUri = ContentUris.withAppendedId(Contacts.CONTENT_URI, personId);
293 mainActionIntent = new Intent(Intent.ACTION_VIEW, personUri);
Daniel Lehmannd260de42011-08-01 18:08:38 -0700294 mainActionIcon = R.drawable.ic_contacts_holo_dark;
Flavio Lerdacfff16d2011-07-12 23:54:40 +0100295 } else if (isVoicemailNumber) {
296 mainActionIntent = null;
297 mainActionIcon = 0;
298 } else if (isSipNumber) {
299 // TODO: This item is currently disabled for SIP addresses, because
300 // the Insert.PHONE extra only works correctly for PSTN numbers.
301 //
302 // To fix this for SIP addresses, we need to:
303 // - define ContactsContract.Intents.Insert.SIP_ADDRESS, and use it here if
304 // the current number is a SIP address
305 // - update the contacts UI code to handle Insert.SIP_ADDRESS by
306 // updating the SipAddress field
307 // and then we can remove the "!isSipNumber" check above.
308 mainActionIntent = null;
309 mainActionIcon = 0;
Flavio Lerda8e403402011-07-20 16:25:49 +0100310 } else if (canPlaceCallsTo) {
Flavio Lerda178eeeb2011-07-11 19:51:40 +0100311 mainActionIntent = new Intent(Intent.ACTION_INSERT_OR_EDIT);
312 mainActionIntent.setType(Contacts.CONTENT_ITEM_TYPE);
313 mainActionIntent.putExtra(Insert.PHONE, mNumber);
Daniel Lehmannd260de42011-08-01 18:08:38 -0700314 mainActionIcon = R.drawable.ic_add_contact_holo_dark;
Flavio Lerda8e403402011-07-20 16:25:49 +0100315 } else {
316 // If we cannot call the number, when we probably cannot add it as a contact either.
317 // This is usually the case of private, unknown, or payphone numbers.
318 mainActionIntent = null;
319 mainActionIcon = 0;
Flavio Lerda178eeeb2011-07-11 19:51:40 +0100320 }
321
Flavio Lerdacfff16d2011-07-12 23:54:40 +0100322 if (mainActionIntent == null) {
323 mMainActionView.setVisibility(View.INVISIBLE);
Daniel Lehmannd260de42011-08-01 18:08:38 -0700324 mMainActionPushLayerView.setVisibility(View.GONE);
Flavio Lerdacfff16d2011-07-12 23:54:40 +0100325 } else {
326 mMainActionView.setVisibility(View.VISIBLE);
327 mMainActionView.setImageResource(mainActionIcon);
Daniel Lehmannd260de42011-08-01 18:08:38 -0700328 mMainActionPushLayerView.setVisibility(View.VISIBLE);
329 mMainActionPushLayerView.setOnClickListener(new View.OnClickListener() {
Flavio Lerdacfff16d2011-07-12 23:54:40 +0100330 @Override
331 public void onClick(View v) {
332 startActivity(mainActionIntent);
333 }
334 });
335 }
Flavio Lerda178eeeb2011-07-11 19:51:40 +0100336
337 // Build list of various available actions.
338 final List<ViewEntry> actions = new ArrayList<ViewEntry>();
339
Flavio Lerda178eeeb2011-07-11 19:51:40 +0100340 // This action allows to call the number that places the call.
Flavio Lerdacfff16d2011-07-12 23:54:40 +0100341 if (canPlaceCallsTo) {
Flavio Lerda178eeeb2011-07-11 19:51:40 +0100342 actions.add(new ViewEntry(android.R.drawable.sym_action_call,
343 getString(R.string.menu_callNumber, mNumber),
344 new Intent(Intent.ACTION_CALL_PRIVILEGED, numberCallUri)));
345 }
346
Flavio Lerdacfff16d2011-07-12 23:54:40 +0100347 // This action allows to send an SMS to the number that placed the call.
348 if (mPhoneNumberHelper.canSendSmsTo(mNumber)) {
Flavio Lerda178eeeb2011-07-11 19:51:40 +0100349 Intent smsIntent = new Intent(Intent.ACTION_SENDTO,
350 Uri.fromParts("sms", mNumber, null));
351 actions.add(new ViewEntry(R.drawable.sym_action_sms,
352 getString(R.string.menu_sendTextMessage), smsIntent));
353 }
354
Flavio Lerda94d7bab2011-07-22 16:52:34 +0100355 mHasEditNumberBeforeCall = canPlaceCallsTo && !isSipNumber && !isVoicemailNumber;
Flavio Lerda178eeeb2011-07-11 19:51:40 +0100356
Flavio Lerda94d7bab2011-07-22 16:52:34 +0100357 if (actions.size() != 0) {
358 // Set the actions for this phone number.
359 setListAdapter(new ViewAdapter(this, actions));
360 getListView().setVisibility(View.VISIBLE);
361 } else {
362 getListView().setVisibility(View.GONE);
Hugo Hudson157dde12011-07-21 23:28:13 +0100363 }
Flavio Lerda178eeeb2011-07-11 19:51:40 +0100364
Flavio Lerda178eeeb2011-07-11 19:51:40 +0100365 ListView historyList = (ListView) findViewById(R.id.history);
366 historyList.setAdapter(
367 new CallDetailHistoryAdapter(this, mInflater, mCallTypeHelper, details));
Daniel Lehmann362654a2011-07-17 14:16:47 -0700368 loadContactPhotos(photoUri);
Flavio Lerda178eeeb2011-07-11 19:51:40 +0100369 }
370
371 /** Return the phone call details for a given call log URI. */
372 private PhoneCallDetails getPhoneCallDetailsForUri(Uri callUri) {
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800373 ContentResolver resolver = getContentResolver();
374 Cursor callCursor = resolver.query(callUri, CALL_LOG_PROJECTION, null, null, null);
375 try {
Flavio Lerda178eeeb2011-07-11 19:51:40 +0100376 if (callCursor == null || !callCursor.moveToFirst()) {
377 throw new IllegalArgumentException("Cannot find content: " + callUri);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800378 }
Flavio Lerda178eeeb2011-07-11 19:51:40 +0100379
380 // Read call log specifics.
381 String number = callCursor.getString(NUMBER_COLUMN_INDEX);
382 long date = callCursor.getLong(DATE_COLUMN_INDEX);
383 long duration = callCursor.getLong(DURATION_COLUMN_INDEX);
384 int callType = callCursor.getInt(CALL_TYPE_COLUMN_INDEX);
385 String countryIso = callCursor.getString(COUNTRY_ISO_COLUMN_INDEX);
386 if (TextUtils.isEmpty(countryIso)) {
387 countryIso = mDefaultCountryIso;
388 }
389
390 // Formatted phone number.
391 final CharSequence numberText;
392 // Read contact specifics.
393 CharSequence nameText = "";
394 int numberType = 0;
395 CharSequence numberLabel = "";
396 long personId = -1L;
Daniel Lehmann362654a2011-07-17 14:16:47 -0700397 Uri photoUri = null;
Flavio Lerda178eeeb2011-07-11 19:51:40 +0100398 // If this is not a regular number, there is no point in looking it up in the contacts.
399 if (!mPhoneNumberHelper.canPlaceCallsTo(number)) {
400 numberText = mPhoneNumberHelper.getDisplayNumber(number, null);
401 } else {
402 // Perform a reverse-phonebook lookup to find the contact details.
403 Uri phoneUri = Uri.withAppendedPath(PhoneLookup.CONTENT_FILTER_URI,
404 Uri.encode(number));
405 Cursor phonesCursor = resolver.query(phoneUri, PHONES_PROJECTION, null, null, null);
406 String candidateNumberText = number;
407 try {
408 if (phonesCursor != null && phonesCursor.moveToFirst()) {
409 personId = phonesCursor.getLong(COLUMN_INDEX_ID);
410 nameText = phonesCursor.getString(COLUMN_INDEX_NAME);
Daniel Lehmann362654a2011-07-17 14:16:47 -0700411 String photoUriString = phonesCursor.getString(COLUMN_INDEX_PHOTO_URI);
412 photoUri = photoUriString == null ? null : Uri.parse(photoUriString);
Flavio Lerda178eeeb2011-07-11 19:51:40 +0100413 candidateNumberText = PhoneNumberUtils.formatNumber(
414 phonesCursor.getString(COLUMN_INDEX_NUMBER),
415 phonesCursor.getString(COLUMN_INDEX_NORMALIZED_NUMBER),
416 countryIso);
417 numberType = phonesCursor.getInt(COLUMN_INDEX_TYPE);
418 numberLabel = phonesCursor.getString(COLUMN_INDEX_LABEL);
419 } else {
420 // We could not find this contact in the contacts, just format the phone
421 // number as best as we can. All the other fields will have their default
422 // values.
423 candidateNumberText =
424 PhoneNumberUtils.formatNumber(number, countryIso);
425 }
426 } finally {
427 if (phonesCursor != null) phonesCursor.close();
428 numberText = candidateNumberText;
429 }
430 }
Flavio Lerda9c466372011-07-19 17:38:17 +0100431 return new PhoneCallDetails(number, numberText, countryIso,
Flavio Lerdacb805e82011-07-18 10:31:44 +0100432 new int[]{ callType }, date, duration,
Daniel Lehmann362654a2011-07-17 14:16:47 -0700433 nameText, numberType, numberLabel, personId, photoUri);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800434 } finally {
435 if (callCursor != null) {
436 callCursor.close();
437 }
438 }
439 }
440
Flavio Lerda9cafe472011-06-08 14:06:13 +0100441 /** Load the contact photos and places them in the corresponding views. */
Daniel Lehmann362654a2011-07-17 14:16:47 -0700442 private void loadContactPhotos(Uri photoUri) {
443 mContactPhotoManager.loadPhoto(mContactBackgroundView, photoUri);
Flavio Lerda9cafe472011-06-08 14:06:13 +0100444 }
445
Flavio Lerda696e8132011-07-05 19:00:23 +0100446 private String getVoicemailNumber() {
447 TelephonyManager telephonyManager =
448 (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
449 return telephonyManager.getVoiceMailNumber();
450 }
451
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800452 static final class ViewEntry {
Flavio Lerdaa8956882011-07-09 21:34:38 +0100453 public final int icon;
454 public final String text;
455 public final Intent intent;
456 public final View.OnClickListener action;
457
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800458 public String label = null;
459 public String number = null;
Dmitri Plotnikovd0d776d2009-08-19 17:38:04 -0700460
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800461 public ViewEntry(int icon, String text, Intent intent) {
462 this.icon = icon;
463 this.text = text;
464 this.intent = intent;
Flavio Lerdaa8956882011-07-09 21:34:38 +0100465 this.action = null;
466 }
467
468 public ViewEntry(int icon, String text, View.OnClickListener listener) {
469 this.icon = icon;
470 this.text = text;
471 this.intent = null;
472 this.action = listener;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800473 }
474 }
475
476 static final class ViewAdapter extends BaseAdapter {
Dmitri Plotnikovd0d776d2009-08-19 17:38:04 -0700477
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800478 private final List<ViewEntry> mActions;
Dmitri Plotnikovd0d776d2009-08-19 17:38:04 -0700479
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800480 private final LayoutInflater mInflater;
Dmitri Plotnikovd0d776d2009-08-19 17:38:04 -0700481
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800482 public ViewAdapter(Context context, List<ViewEntry> actions) {
483 mActions = actions;
484 mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
485 }
Dmitri Plotnikovd0d776d2009-08-19 17:38:04 -0700486
Flavio Lerda9cafe472011-06-08 14:06:13 +0100487 @Override
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800488 public int getCount() {
489 return mActions.size();
490 }
491
Flavio Lerda9cafe472011-06-08 14:06:13 +0100492 @Override
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800493 public Object getItem(int position) {
494 return mActions.get(position);
495 }
496
Flavio Lerda9cafe472011-06-08 14:06:13 +0100497 @Override
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800498 public long getItemId(int position) {
499 return position;
500 }
Dmitri Plotnikovd0d776d2009-08-19 17:38:04 -0700501
Flavio Lerda9cafe472011-06-08 14:06:13 +0100502 @Override
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800503 public View getView(int position, View convertView, ViewGroup parent) {
504 // Make sure we have a valid convertView to start with
505 if (convertView == null) {
506 convertView = mInflater.inflate(R.layout.call_detail_list_item, parent, false);
507 }
508
509 // Fill action with icon and text.
510 ViewEntry entry = mActions.get(position);
511 convertView.setTag(entry);
Dmitri Plotnikovd0d776d2009-08-19 17:38:04 -0700512
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800513 ImageView icon = (ImageView) convertView.findViewById(R.id.icon);
514 TextView text = (TextView) convertView.findViewById(android.R.id.text1);
515
516 icon.setImageResource(entry.icon);
517 text.setText(entry.text);
518
519 View line2 = convertView.findViewById(R.id.line2);
520 boolean numberEmpty = TextUtils.isEmpty(entry.number);
521 boolean labelEmpty = TextUtils.isEmpty(entry.label) || numberEmpty;
522 if (labelEmpty && numberEmpty) {
523 line2.setVisibility(View.GONE);
524 } else {
525 line2.setVisibility(View.VISIBLE);
Dmitri Plotnikovd0d776d2009-08-19 17:38:04 -0700526
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800527 TextView label = (TextView) convertView.findViewById(R.id.label);
528 if (labelEmpty) {
529 label.setVisibility(View.GONE);
530 } else {
531 label.setText(entry.label);
532 label.setVisibility(View.VISIBLE);
533 }
534
535 TextView number = (TextView) convertView.findViewById(R.id.number);
536 number.setText(entry.number);
537 }
Dmitri Plotnikovd0d776d2009-08-19 17:38:04 -0700538
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800539 return convertView;
540 }
541 }
Dmitri Plotnikovd0d776d2009-08-19 17:38:04 -0700542
Flavio Lerda9cafe472011-06-08 14:06:13 +0100543 @Override
544 public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800545 // Handle passing action off to correct handler.
546 if (view.getTag() instanceof ViewEntry) {
547 ViewEntry entry = (ViewEntry) view.getTag();
548 if (entry.intent != null) {
549 startActivity(entry.intent);
Flavio Lerdaa8956882011-07-09 21:34:38 +0100550 } else if (entry.action != null) {
551 entry.action.onClick(view);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800552 }
553 }
Dmitri Plotnikovd0d776d2009-08-19 17:38:04 -0700554 }
Dmitri Plotnikov8e86b752010-02-22 17:47:57 -0800555
556 @Override
557 public void startSearch(String initialQuery, boolean selectInitialQuery, Bundle appSearchData,
558 boolean globalSearch) {
559 if (globalSearch) {
560 super.startSearch(initialQuery, selectInitialQuery, appSearchData, globalSearch);
561 } else {
562 ContactsSearchManager.startSearch(this, initialQuery);
563 }
564 }
Debashish Chatterjee0cb82242011-07-19 19:29:37 +0100565
566 protected void updateVoicemailStatusMessage(Cursor statusCursor) {
567 if (statusCursor == null) {
568 mStatusMessageView.setVisibility(View.GONE);
569 return;
570 }
571 final StatusMessage message = getStatusMessage(statusCursor);
572 if (message == null || !message.showInCallDetails()) {
573 mStatusMessageView.setVisibility(View.GONE);
574 return;
575 }
576
577 mStatusMessageView.setVisibility(View.VISIBLE);
578 mStatusMessageText.setText(message.callDetailsMessageId);
579 if (message.actionMessageId != -1) {
580 mStatusMessageAction.setText(message.actionMessageId);
581 }
582 if (message.actionUri != null) {
583 mStatusMessageAction.setClickable(true);
584 mStatusMessageAction.setOnClickListener(new View.OnClickListener() {
585 @Override
586 public void onClick(View v) {
587 startActivity(new Intent(Intent.ACTION_VIEW, message.actionUri));
588 }
589 });
590 } else {
591 mStatusMessageAction.setClickable(false);
592 }
593 }
594
595 private StatusMessage getStatusMessage(Cursor statusCursor) {
596 List<StatusMessage> messages = mVoicemailStatusHelper.getStatusMessages(statusCursor);
597 Log.d(TAG, "Num status messages: " + messages.size());
598 if (messages.size() == 0) {
599 return null;
600 }
601 // There can only be a single status message per source package, so num of messages can
602 // at most be 1.
603 if (messages.size() > 1) {
604 Log.w(TAG, String.format("Expected 1, found (%d) num of status messages." +
605 " Will use the first one.", messages.size()));
606 }
607 return messages.get(0);
608 }
Flavio Lerda94d7bab2011-07-22 16:52:34 +0100609
610 @Override
611 public boolean onCreateOptionsMenu(Menu menu) {
612 getMenuInflater().inflate(R.menu.call_details_options, menu);
Hugo Hudsonc2f09c32011-07-30 16:31:28 +0100613 return super.onCreateOptionsMenu(menu);
Flavio Lerda94d7bab2011-07-22 16:52:34 +0100614 }
615
616 @Override
617 public boolean onPrepareOptionsMenu(Menu menu) {
618 // This action deletes all elements in the group from the call log.
619 // We don't have this action for voicemails, because you can just use the trash button.
Hugo Hudsonc2f09c32011-07-30 16:31:28 +0100620 menu.findItem(R.id.menu_remove_from_call_log).setVisible(!hasVoicemail());
621 menu.findItem(R.id.menu_edit_number_before_call).setVisible(mHasEditNumberBeforeCall);
622 menu.findItem(R.id.menu_trash).setVisible(hasVoicemail());
623 menu.findItem(R.id.menu_share_voicemail).setVisible(hasVoicemail());
624 return super.onPrepareOptionsMenu(menu);
Flavio Lerda94d7bab2011-07-22 16:52:34 +0100625 }
626
627 @Override
628 public boolean onMenuItemSelected(int featureId, MenuItem item) {
629 switch (item.getItemId()) {
Flavio Lerdad44e83a2011-07-24 23:10:17 +0100630 case android.R.id.home: {
631 onHomeSelected();
632 return true;
633 }
634
Hugo Hudsonc2f09c32011-07-30 16:31:28 +0100635 // All the options menu items are handled by onMenu... methods.
Flavio Lerda94d7bab2011-07-22 16:52:34 +0100636 default:
637 throw new IllegalArgumentException();
638 }
639 }
Flavio Lerdad44e83a2011-07-24 23:10:17 +0100640
Hugo Hudsonc2f09c32011-07-30 16:31:28 +0100641 public void onMenuRemoveFromCallLog(MenuItem menuItem) {
642 StringBuilder callIds = new StringBuilder();
643 for (Uri callUri : getCallLogEntryUris()) {
644 if (callIds.length() != 0) {
645 callIds.append(",");
646 }
647 callIds.append(ContentUris.parseId(callUri));
648 }
649
650 getContentResolver().delete(Calls.CONTENT_URI_WITH_VOICEMAIL,
651 Calls._ID + " IN (" + callIds + ")", null);
652 // Also close the activity.
653 finish();
654 }
655
656 public void onMenuEditNumberBeforeCall(MenuItem menuItem) {
657 startActivity(new Intent(Intent.ACTION_DIAL, mPhoneNumberHelper.getCallUri(mNumber)));
658 }
659
660 public void onMenuShareVoicemail(MenuItem menuItem) {
661 Log.w(TAG, "onMenuShareVoicemail not yet implemented");
662 }
663
664 public void onMenuTrashVoicemail(MenuItem menuItem) {
Hugo Hudson7b02fde2011-08-02 20:56:48 +0100665 getContentResolver().delete(getVoicemailUri(), null, null);
666 finish();
Hugo Hudsonc2f09c32011-07-30 16:31:28 +0100667 }
668
Flavio Lerdad44e83a2011-07-24 23:10:17 +0100669 private void configureActionBar() {
670 ActionBar actionBar = getActionBar();
671 if (actionBar != null) {
Hugo Hudsonc2f09c32011-07-30 16:31:28 +0100672 actionBar.setDisplayOptions(ActionBar.DISPLAY_HOME_AS_UP | ActionBar.DISPLAY_SHOW_HOME);
Flavio Lerdad44e83a2011-07-24 23:10:17 +0100673 }
674 }
675
676 /** Invoked when the user presses the home button in the action bar. */
677 private void onHomeSelected() {
678 Intent intent = new Intent(Intent.ACTION_VIEW, Calls.CONTENT_URI);
679 // This will open the call log even if the detail view has been opened directly.
680 intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
681 startActivity(intent);
682 finish();
683 }
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800684}