blob: 813ebb15185a3fb4aedeffffe31fcdae188879c1 [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;
Hugo Hudsonb002f512011-07-15 17:41:12 +010028import android.app.FragmentManager;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080029import android.app.ListActivity;
30import android.content.ContentResolver;
31import android.content.ContentUris;
32import android.content.Context;
33import android.content.Intent;
34import android.content.res.Resources;
35import android.database.Cursor;
36import android.net.Uri;
37import android.os.Bundle;
38import android.provider.CallLog;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080039import android.provider.CallLog.Calls;
Flavio Lerda9cafe472011-06-08 14:06:13 +010040import android.provider.Contacts.Intents.Insert;
Dmitri Plotnikovd0d776d2009-08-19 17:38:04 -070041import android.provider.ContactsContract.Contacts;
42import android.provider.ContactsContract.PhoneLookup;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080043import android.telephony.PhoneNumberUtils;
44import android.telephony.TelephonyManager;
45import android.text.TextUtils;
Flavio Lerda178eeeb2011-07-11 19:51:40 +010046import android.util.Log;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080047import android.view.KeyEvent;
48import android.view.LayoutInflater;
Flavio Lerda94d7bab2011-07-22 16:52:34 +010049import android.view.Menu;
50import android.view.MenuItem;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080051import android.view.View;
52import android.view.ViewGroup;
53import android.widget.AdapterView;
54import android.widget.BaseAdapter;
55import 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;
Flavio Lerda9cafe472011-06-08 14:06:13 +010086 private ImageView mContactBackgroundView;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080087
88 private String mNumber = null;
Bai Tao09eb04f2010-09-01 15:34:16 +080089 private String mDefaultCountryIso;
Dmitri Plotnikovd0d776d2009-08-19 17:38:04 -070090
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080091 /* package */ LayoutInflater mInflater;
92 /* package */ Resources mResources;
Flavio Lerda9cafe472011-06-08 14:06:13 +010093 /** Helper to load contact photos. */
94 private ContactPhotoManager mContactPhotoManager;
Debashish Chatterjee0cb82242011-07-19 19:29:37 +010095 /** Helper to make async queries to content resolver. */
96 private CallDetailActivityQueryHandler mAsyncQueryHandler;
97 /** Helper to get voicemail status messages. */
98 private VoicemailStatusHelper mVoicemailStatusHelper;
99 // Views related to voicemail status message.
100 private View mStatusMessageView;
101 private TextView mStatusMessageText;
102 private TextView mStatusMessageAction;
Dmitri Plotnikovd0d776d2009-08-19 17:38:04 -0700103
Flavio Lerda94d7bab2011-07-22 16:52:34 +0100104 /** Whether we should show "remove from call log" in the options menu. */
105 private boolean mHasRemoveFromCallLog;
106 /** Whether we should show "edit number before call" in the options menu. */
107 private boolean mHasEditNumberBeforeCall;
108
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800109 static final String[] CALL_LOG_PROJECTION = new String[] {
110 CallLog.Calls.DATE,
111 CallLog.Calls.DURATION,
112 CallLog.Calls.NUMBER,
113 CallLog.Calls.TYPE,
Bai Tao09eb04f2010-09-01 15:34:16 +0800114 CallLog.Calls.COUNTRY_ISO,
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800115 };
Dmitri Plotnikovd0d776d2009-08-19 17:38:04 -0700116
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800117 static final int DATE_COLUMN_INDEX = 0;
118 static final int DURATION_COLUMN_INDEX = 1;
119 static final int NUMBER_COLUMN_INDEX = 2;
120 static final int CALL_TYPE_COLUMN_INDEX = 3;
Bai Tao09eb04f2010-09-01 15:34:16 +0800121 static final int COUNTRY_ISO_COLUMN_INDEX = 4;
Dmitri Plotnikovd0d776d2009-08-19 17:38:04 -0700122
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800123 static final String[] PHONES_PROJECTION = new String[] {
Dmitri Plotnikovd0d776d2009-08-19 17:38:04 -0700124 PhoneLookup._ID,
125 PhoneLookup.DISPLAY_NAME,
126 PhoneLookup.TYPE,
127 PhoneLookup.LABEL,
128 PhoneLookup.NUMBER,
Bai Tao09eb04f2010-09-01 15:34:16 +0800129 PhoneLookup.NORMALIZED_NUMBER,
Daniel Lehmann362654a2011-07-17 14:16:47 -0700130 PhoneLookup.PHOTO_URI,
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800131 };
132 static final int COLUMN_INDEX_ID = 0;
133 static final int COLUMN_INDEX_NAME = 1;
134 static final int COLUMN_INDEX_TYPE = 2;
135 static final int COLUMN_INDEX_LABEL = 3;
136 static final int COLUMN_INDEX_NUMBER = 4;
Bai Tao09eb04f2010-09-01 15:34:16 +0800137 static final int COLUMN_INDEX_NORMALIZED_NUMBER = 5;
Daniel Lehmann362654a2011-07-17 14:16:47 -0700138 static final int COLUMN_INDEX_PHOTO_URI = 6;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800139
140 @Override
141 protected void onCreate(Bundle icicle) {
142 super.onCreate(icicle);
143
144 setContentView(R.layout.call_detail);
145
146 mInflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
147 mResources = getResources();
Dmitri Plotnikovd0d776d2009-08-19 17:38:04 -0700148
Flavio Lerda696e8132011-07-05 19:00:23 +0100149 mPhoneCallDetailsViews = PhoneCallDetailsViews.fromView(getWindow().getDecorView());
Flavio Lerda40569562011-07-22 21:51:29 +0100150 mCallTypeHelper = new CallTypeHelper(getResources(), mInflater);
Flavio Lerda178eeeb2011-07-11 19:51:40 +0100151 mPhoneNumberHelper = new PhoneNumberHelper(mResources, getVoicemailNumber());
Flavio Lerda40569562011-07-22 21:51:29 +0100152 mPhoneCallDetailsHelper = new PhoneCallDetailsHelper(mResources, mCallTypeHelper,
Flavio Lerda178eeeb2011-07-11 19:51:40 +0100153 mPhoneNumberHelper);
Debashish Chatterjee0cb82242011-07-19 19:29:37 +0100154 mVoicemailStatusHelper = new VoicemailStatusHelperImpl();
155 mAsyncQueryHandler = new CallDetailActivityQueryHandler(this);
156 mStatusMessageView = findViewById(R.id.voicemail_status);
157 mStatusMessageText = (TextView) findViewById(R.id.voicemail_status_message);
158 mStatusMessageAction = (TextView) findViewById(R.id.voicemail_status_action);
Flavio Lerdafb63c432011-07-07 17:16:53 +0100159 mMainActionView = (ImageView) findViewById(R.id.main_action);
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()) {
182 // Has voicemail: leave the fragment visible. Optionally start the playback.
Hugo Hudson757aa552011-07-20 01:15:25 +0100183 // Do a query to fetch the voicemail status messages.
184 boolean startPlayback = getIntent().getBooleanExtra(
Hugo Hudsonb002f512011-07-15 17:41:12 +0100185 EXTRA_VOICEMAIL_START_PLAYBACK, false);
Hugo Hudson157dde12011-07-21 23:28:13 +0100186 Uri voicemailUri = getIntent().getParcelableExtra(EXTRA_VOICEMAIL_URI);
187 getVoicemailPlaybackFragment().setVoicemailUri(voicemailUri, startPlayback);
Hugo Hudson757aa552011-07-20 01:15:25 +0100188 mAsyncQueryHandler.startVoicemailStatusQuery(voicemailUri);
189 } else {
190 // No voicemail uri: hide the voicemail fragment and the status view.
Hugo Hudson157dde12011-07-21 23:28:13 +0100191 getFragmentManager().beginTransaction().hide(getVoicemailPlaybackFragment()).commit();
Hugo Hudson757aa552011-07-20 01:15:25 +0100192 mStatusMessageView.setVisibility(View.GONE);
Hugo Hudsonb002f512011-07-15 17:41:12 +0100193 }
Flavio Lerda178eeeb2011-07-11 19:51:40 +0100194 }
195
Hugo Hudson157dde12011-07-21 23:28:13 +0100196 private boolean hasVoicemail() {
197 return getIntent().getParcelableExtra(EXTRA_VOICEMAIL_URI) != null;
198 }
199
200 private VoicemailPlaybackFragment getVoicemailPlaybackFragment() {
201 FragmentManager manager = getFragmentManager();
202 return (VoicemailPlaybackFragment) manager.findFragmentById(
203 R.id.voicemail_playback_fragment);
204 }
205
Flavio Lerda178eeeb2011-07-11 19:51:40 +0100206 /**
207 * Returns the list of URIs to show.
208 * <p>
209 * There are two ways the URIs can be provided to the activity: as the data on the intent, or as
210 * a list of ids in the call log added as an extra on the URI.
211 * <p>
212 * If both are available, the data on the intent takes precedence.
213 */
214 private Uri[] getCallLogEntryUris() {
215 Uri uri = getIntent().getData();
216 if (uri != null) {
217 // If there is a data on the intent, it takes precedence over the extra.
218 return new Uri[]{ uri };
219 }
220 long[] ids = getIntent().getLongArrayExtra(EXTRA_CALL_LOG_IDS);
221 Uri[] uris = new Uri[ids.length];
222 for (int index = 0; index < ids.length; ++index) {
223 uris[index] = ContentUris.withAppendedId(Calls.CONTENT_URI_WITH_VOICEMAIL, ids[index]);
224 }
225 return uris;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800226 }
227
228 @Override
229 public boolean onKeyDown(int keyCode, KeyEvent event) {
230 switch (keyCode) {
231 case KeyEvent.KEYCODE_CALL: {
232 // Make sure phone isn't already busy before starting direct call
233 TelephonyManager tm = (TelephonyManager)
234 getSystemService(Context.TELEPHONY_SERVICE);
235 if (tm.getCallState() == TelephonyManager.CALL_STATE_IDLE) {
236 Intent callIntent = new Intent(Intent.ACTION_CALL_PRIVILEGED,
237 Uri.fromParts("tel", mNumber, null));
238 startActivity(callIntent);
239 return true;
240 }
241 }
242 }
Dmitri Plotnikovd0d776d2009-08-19 17:38:04 -0700243
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800244 return super.onKeyDown(keyCode, event);
245 }
Dmitri Plotnikovd0d776d2009-08-19 17:38:04 -0700246
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800247 /**
248 * Update user interface with details of given call.
Dmitri Plotnikovd0d776d2009-08-19 17:38:04 -0700249 *
Flavio Lerda178eeeb2011-07-11 19:51:40 +0100250 * @param callUris URIs into {@link CallLog.Calls} of the calls to be displayed
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800251 */
Flavio Lerda178eeeb2011-07-11 19:51:40 +0100252 private void updateData(final Uri... callUris) {
253 // TODO: All phone calls correspond to the same person, so we can make a single lookup.
254 final int numCalls = callUris.length;
255 final PhoneCallDetails[] details = new PhoneCallDetails[numCalls];
256 try {
257 for (int index = 0; index < numCalls; ++index) {
258 details[index] = getPhoneCallDetailsForUri(callUris[index]);
259 }
260 } catch (IllegalArgumentException e) {
261 // Something went wrong reading in our primary data, so we're going to
262 // bail out and show error to users.
263 Log.w(TAG, "invalid URI starting call details", e);
264 Toast.makeText(this, R.string.toast_call_detail_error,
265 Toast.LENGTH_SHORT).show();
266 finish();
267 return;
268 }
269
270 // We know that all calls are from the same number and the same contact, so pick the first.
271 mNumber = details[0].number.toString();
272 final long personId = details[0].personId;
Daniel Lehmann362654a2011-07-17 14:16:47 -0700273 final Uri photoUri = details[0].photoUri;
Flavio Lerda178eeeb2011-07-11 19:51:40 +0100274
275 // Set the details header, based on the first phone call.
276 mPhoneCallDetailsHelper.setPhoneCallDetails(mPhoneCallDetailsViews,
Flavio Lerda40569562011-07-22 21:51:29 +0100277 details[0], false, false, true);
Flavio Lerda178eeeb2011-07-11 19:51:40 +0100278
Flavio Lerdacfff16d2011-07-12 23:54:40 +0100279 // Cache the details about the phone number.
280 final Uri numberCallUri = mPhoneNumberHelper.getCallUri(mNumber);
281 final boolean canPlaceCallsTo = mPhoneNumberHelper.canPlaceCallsTo(mNumber);
282 final boolean isVoicemailNumber = mPhoneNumberHelper.isVoicemailNumber(mNumber);
283 final boolean isSipNumber = mPhoneNumberHelper.isSipNumber(mNumber);
284
Flavio Lerda178eeeb2011-07-11 19:51:40 +0100285 // Let user view contact details if they exist, otherwise add option to create new contact
286 // from this number.
287 final Intent mainActionIntent;
288 final int mainActionIcon;
289
290 if (details[0].personId != -1) {
291 Uri personUri = ContentUris.withAppendedId(Contacts.CONTENT_URI, personId);
292 mainActionIntent = new Intent(Intent.ACTION_VIEW, personUri);
293 mainActionIcon = R.drawable.sym_action_view_contact;
Flavio Lerdacfff16d2011-07-12 23:54:40 +0100294 } else if (isVoicemailNumber) {
295 mainActionIntent = null;
296 mainActionIcon = 0;
297 } else if (isSipNumber) {
298 // TODO: This item is currently disabled for SIP addresses, because
299 // the Insert.PHONE extra only works correctly for PSTN numbers.
300 //
301 // To fix this for SIP addresses, we need to:
302 // - define ContactsContract.Intents.Insert.SIP_ADDRESS, and use it here if
303 // the current number is a SIP address
304 // - update the contacts UI code to handle Insert.SIP_ADDRESS by
305 // updating the SipAddress field
306 // and then we can remove the "!isSipNumber" check above.
307 mainActionIntent = null;
308 mainActionIcon = 0;
Flavio Lerda8e403402011-07-20 16:25:49 +0100309 } else if (canPlaceCallsTo) {
Flavio Lerda178eeeb2011-07-11 19:51:40 +0100310 mainActionIntent = new Intent(Intent.ACTION_INSERT_OR_EDIT);
311 mainActionIntent.setType(Contacts.CONTENT_ITEM_TYPE);
312 mainActionIntent.putExtra(Insert.PHONE, mNumber);
313 mainActionIcon = R.drawable.sym_action_add;
Flavio Lerda8e403402011-07-20 16:25:49 +0100314 } else {
315 // If we cannot call the number, when we probably cannot add it as a contact either.
316 // This is usually the case of private, unknown, or payphone numbers.
317 mainActionIntent = null;
318 mainActionIcon = 0;
Flavio Lerda178eeeb2011-07-11 19:51:40 +0100319 }
320
Flavio Lerdacfff16d2011-07-12 23:54:40 +0100321 if (mainActionIntent == null) {
322 mMainActionView.setVisibility(View.INVISIBLE);
323 } else {
324 mMainActionView.setVisibility(View.VISIBLE);
325 mMainActionView.setImageResource(mainActionIcon);
326 mMainActionView.setOnClickListener(new View.OnClickListener() {
327 @Override
328 public void onClick(View v) {
329 startActivity(mainActionIntent);
330 }
331 });
332 }
Flavio Lerda178eeeb2011-07-11 19:51:40 +0100333
334 // Build list of various available actions.
335 final List<ViewEntry> actions = new ArrayList<ViewEntry>();
336
Flavio Lerda178eeeb2011-07-11 19:51:40 +0100337 // This action allows to call the number that places the call.
Flavio Lerdacfff16d2011-07-12 23:54:40 +0100338 if (canPlaceCallsTo) {
Flavio Lerda178eeeb2011-07-11 19:51:40 +0100339 actions.add(new ViewEntry(android.R.drawable.sym_action_call,
340 getString(R.string.menu_callNumber, mNumber),
341 new Intent(Intent.ACTION_CALL_PRIVILEGED, numberCallUri)));
342 }
343
Flavio Lerdacfff16d2011-07-12 23:54:40 +0100344 // This action allows to send an SMS to the number that placed the call.
345 if (mPhoneNumberHelper.canSendSmsTo(mNumber)) {
Flavio Lerda178eeeb2011-07-11 19:51:40 +0100346 Intent smsIntent = new Intent(Intent.ACTION_SENDTO,
347 Uri.fromParts("sms", mNumber, null));
348 actions.add(new ViewEntry(R.drawable.sym_action_sms,
349 getString(R.string.menu_sendTextMessage), smsIntent));
350 }
351
352 // This action deletes all elements in the group from the call log.
Hugo Hudson157dde12011-07-21 23:28:13 +0100353 // We don't have this action for voicemails, because you can just use the trash button.
Flavio Lerda94d7bab2011-07-22 16:52:34 +0100354 mHasRemoveFromCallLog = !hasVoicemail();
355 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);
613 return true;
614 }
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.
620 menu.findItem(R.id.remove_from_call_log).setVisible(mHasRemoveFromCallLog);
621 menu.findItem(R.id.edit_number_before_call).setVisible(mHasEditNumberBeforeCall);
622 return mHasRemoveFromCallLog || mHasEditNumberBeforeCall;
623 }
624
625 @Override
626 public boolean onMenuItemSelected(int featureId, MenuItem item) {
627 switch (item.getItemId()) {
628 case R.id.remove_from_call_log: {
629 StringBuilder callIds = new StringBuilder();
630 for (Uri callUri : getCallLogEntryUris()) {
631 if (callIds.length() != 0) {
632 callIds.append(",");
633 }
634 callIds.append(ContentUris.parseId(callUri));
635 }
636
637 getContentResolver().delete(Calls.CONTENT_URI_WITH_VOICEMAIL,
638 Calls._ID + " IN (" + callIds + ")", null);
639 // Also close the activity.
640 finish();
641 return true;
642 }
643
644 case R.id.edit_number_before_call:
645 startActivity(
646 new Intent(Intent.ACTION_DIAL, mPhoneNumberHelper.getCallUri(mNumber)));
647 return true;
648
Flavio Lerdad44e83a2011-07-24 23:10:17 +0100649 case android.R.id.home: {
650 onHomeSelected();
651 return true;
652 }
653
Flavio Lerda94d7bab2011-07-22 16:52:34 +0100654 default:
655 throw new IllegalArgumentException();
656 }
657 }
Flavio Lerdad44e83a2011-07-24 23:10:17 +0100658
659 private void configureActionBar() {
660 ActionBar actionBar = getActionBar();
661 if (actionBar != null) {
662 actionBar.setDisplayOptions(ActionBar.DISPLAY_HOME_AS_UP | ActionBar.DISPLAY_SHOW_HOME,
663 ActionBar.DISPLAY_HOME_AS_UP | ActionBar.DISPLAY_SHOW_TITLE
664 | ActionBar.DISPLAY_SHOW_HOME);
665 actionBar.setIcon(R.drawable.ic_ab_dialer_holo_dark);
666 }
667 }
668
669 /** Invoked when the user presses the home button in the action bar. */
670 private void onHomeSelected() {
671 Intent intent = new Intent(Intent.ACTION_VIEW, Calls.CONTENT_URI);
672 // This will open the call log even if the detail view has been opened directly.
673 intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
674 startActivity(intent);
675 finish();
676 }
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800677}