blob: d732d66e4e45c792cf4800a0277cab79e729384d [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
Hugo Hudsonb002f512011-07-15 17:41:12 +010027import android.app.FragmentManager;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080028import android.app.ListActivity;
29import android.content.ContentResolver;
30import android.content.ContentUris;
31import android.content.Context;
32import android.content.Intent;
33import android.content.res.Resources;
34import android.database.Cursor;
35import android.net.Uri;
36import android.os.Bundle;
37import android.provider.CallLog;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080038import android.provider.CallLog.Calls;
Flavio Lerda9cafe472011-06-08 14:06:13 +010039import android.provider.Contacts.Intents.Insert;
Dmitri Plotnikovd0d776d2009-08-19 17:38:04 -070040import android.provider.ContactsContract.Contacts;
41import android.provider.ContactsContract.PhoneLookup;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080042import android.telephony.PhoneNumberUtils;
43import android.telephony.TelephonyManager;
44import android.text.TextUtils;
Flavio Lerda178eeeb2011-07-11 19:51:40 +010045import android.util.Log;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080046import android.view.KeyEvent;
47import android.view.LayoutInflater;
Flavio Lerda94d7bab2011-07-22 16:52:34 +010048import android.view.Menu;
49import android.view.MenuItem;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080050import android.view.View;
51import android.view.ViewGroup;
52import android.widget.AdapterView;
53import android.widget.BaseAdapter;
54import android.widget.ImageView;
Flavio Lerda9a208cc2011-07-12 21:05:47 +010055import android.widget.ListView;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080056import android.widget.TextView;
57import android.widget.Toast;
58
59import java.util.ArrayList;
60import java.util.List;
61
62/**
63 * Displays the details of a specific call log entry.
Flavio Lerda178eeeb2011-07-11 19:51:40 +010064 * <p>
65 * This activity can be either started with the URI of a single call log entry, or with the
66 * {@link #EXTRA_CALL_LOG_IDS} extra to specify a group of call log entries.
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080067 */
68public class CallDetailActivity extends ListActivity implements
69 AdapterView.OnItemClickListener {
70 private static final String TAG = "CallDetail";
71
Flavio Lerda178eeeb2011-07-11 19:51:40 +010072 /** A long array extra containing ids of call log entries to display. */
Hugo Hudsonb002f512011-07-15 17:41:12 +010073 public static final String EXTRA_CALL_LOG_IDS = "EXTRA_CALL_LOG_IDS";
74 /** If we are started with a voicemail, we'll find the uri to play with this extra. */
75 public static final String EXTRA_VOICEMAIL_URI = "EXTRA_VOICEMAIL_URI";
76 /** If we should immediately start playback of the voicemail, this extra will be set to true. */
77 public static final String EXTRA_VOICEMAIL_START_PLAYBACK = "EXTRA_VOICEMAIL_START_PLAYBACK";
Flavio Lerda178eeeb2011-07-11 19:51:40 +010078
Flavio Lerdaa024c3f2011-06-10 10:47:07 +010079 /** The views representing the details of a phone call. */
Flavio Lerdaafb93bb2011-07-05 14:46:10 +010080 private PhoneCallDetailsViews mPhoneCallDetailsViews;
Flavio Lerda9a208cc2011-07-12 21:05:47 +010081 private CallTypeHelper mCallTypeHelper;
Flavio Lerda178eeeb2011-07-11 19:51:40 +010082 private PhoneNumberHelper mPhoneNumberHelper;
Flavio Lerdaafb93bb2011-07-05 14:46:10 +010083 private PhoneCallDetailsHelper mPhoneCallDetailsHelper;
Flavio Lerdafb63c432011-07-07 17:16:53 +010084 private View mHomeActionView;
85 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 mHomeActionView = findViewById(R.id.action_bar_home);
160 mMainActionView = (ImageView) findViewById(R.id.main_action);
Flavio Lerda9cafe472011-06-08 14:06:13 +0100161 mContactBackgroundView = (ImageView) findViewById(R.id.contact_background);
Bai Tao09eb04f2010-09-01 15:34:16 +0800162 mDefaultCountryIso = ContactsUtils.getCurrentCountryIso(this);
Flavio Lerda9cafe472011-06-08 14:06:13 +0100163 mContactPhotoManager = ContactPhotoManager.getInstance(this);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800164 getListView().setOnItemClickListener(this);
Flavio Lerdafb63c432011-07-07 17:16:53 +0100165 mHomeActionView.setOnClickListener(new View.OnClickListener() {
166 @Override
167 public void onClick(View v) {
168 // We want this to start the call log if this activity was not started from the
169 // call log itself.
170 CallDetailActivity.this.finish();
171 }
172 });
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800173 }
Dmitri Plotnikovd0d776d2009-08-19 17:38:04 -0700174
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800175 @Override
176 public void onResume() {
177 super.onResume();
Flavio Lerda178eeeb2011-07-11 19:51:40 +0100178 updateData(getCallLogEntryUris());
Hugo Hudson757aa552011-07-20 01:15:25 +0100179 optionallyHandleVoicemail();
Hugo Hudsonb002f512011-07-15 17:41:12 +0100180 }
181
182 /**
183 * Handle voicemail playback or hide voicemail ui.
184 * <p>
185 * If the Intent used to start this Activity contains the suitable extras, then start voicemail
186 * playback. If it doesn't, then hide the voicemail ui.
187 */
Hugo Hudson757aa552011-07-20 01:15:25 +0100188 private void optionallyHandleVoicemail() {
Hugo Hudson157dde12011-07-21 23:28:13 +0100189 if (hasVoicemail()) {
190 // Has voicemail: leave the fragment visible. Optionally start the playback.
Hugo Hudson757aa552011-07-20 01:15:25 +0100191 // Do a query to fetch the voicemail status messages.
192 boolean startPlayback = getIntent().getBooleanExtra(
Hugo Hudsonb002f512011-07-15 17:41:12 +0100193 EXTRA_VOICEMAIL_START_PLAYBACK, false);
Hugo Hudson157dde12011-07-21 23:28:13 +0100194 Uri voicemailUri = getIntent().getParcelableExtra(EXTRA_VOICEMAIL_URI);
195 getVoicemailPlaybackFragment().setVoicemailUri(voicemailUri, startPlayback);
Hugo Hudson757aa552011-07-20 01:15:25 +0100196 mAsyncQueryHandler.startVoicemailStatusQuery(voicemailUri);
197 } else {
198 // No voicemail uri: hide the voicemail fragment and the status view.
Hugo Hudson157dde12011-07-21 23:28:13 +0100199 getFragmentManager().beginTransaction().hide(getVoicemailPlaybackFragment()).commit();
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
208 private VoicemailPlaybackFragment getVoicemailPlaybackFragment() {
209 FragmentManager manager = getFragmentManager();
210 return (VoicemailPlaybackFragment) manager.findFragmentById(
211 R.id.voicemail_playback_fragment);
212 }
213
Flavio Lerda178eeeb2011-07-11 19:51:40 +0100214 /**
215 * Returns the list of URIs to show.
216 * <p>
217 * There are two ways the URIs can be provided to the activity: as the data on the intent, or as
218 * a list of ids in the call log added as an extra on the URI.
219 * <p>
220 * If both are available, the data on the intent takes precedence.
221 */
222 private Uri[] getCallLogEntryUris() {
223 Uri uri = getIntent().getData();
224 if (uri != null) {
225 // If there is a data on the intent, it takes precedence over the extra.
226 return new Uri[]{ uri };
227 }
228 long[] ids = getIntent().getLongArrayExtra(EXTRA_CALL_LOG_IDS);
229 Uri[] uris = new Uri[ids.length];
230 for (int index = 0; index < ids.length; ++index) {
231 uris[index] = ContentUris.withAppendedId(Calls.CONTENT_URI_WITH_VOICEMAIL, ids[index]);
232 }
233 return uris;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800234 }
235
236 @Override
237 public boolean onKeyDown(int keyCode, KeyEvent event) {
238 switch (keyCode) {
239 case KeyEvent.KEYCODE_CALL: {
240 // Make sure phone isn't already busy before starting direct call
241 TelephonyManager tm = (TelephonyManager)
242 getSystemService(Context.TELEPHONY_SERVICE);
243 if (tm.getCallState() == TelephonyManager.CALL_STATE_IDLE) {
244 Intent callIntent = new Intent(Intent.ACTION_CALL_PRIVILEGED,
245 Uri.fromParts("tel", mNumber, null));
246 startActivity(callIntent);
247 return true;
248 }
249 }
250 }
Dmitri Plotnikovd0d776d2009-08-19 17:38:04 -0700251
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800252 return super.onKeyDown(keyCode, event);
253 }
Dmitri Plotnikovd0d776d2009-08-19 17:38:04 -0700254
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800255 /**
256 * Update user interface with details of given call.
Dmitri Plotnikovd0d776d2009-08-19 17:38:04 -0700257 *
Flavio Lerda178eeeb2011-07-11 19:51:40 +0100258 * @param callUris URIs into {@link CallLog.Calls} of the calls to be displayed
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800259 */
Flavio Lerda178eeeb2011-07-11 19:51:40 +0100260 private void updateData(final Uri... callUris) {
261 // TODO: All phone calls correspond to the same person, so we can make a single lookup.
262 final int numCalls = callUris.length;
263 final PhoneCallDetails[] details = new PhoneCallDetails[numCalls];
264 try {
265 for (int index = 0; index < numCalls; ++index) {
266 details[index] = getPhoneCallDetailsForUri(callUris[index]);
267 }
268 } catch (IllegalArgumentException e) {
269 // Something went wrong reading in our primary data, so we're going to
270 // bail out and show error to users.
271 Log.w(TAG, "invalid URI starting call details", e);
272 Toast.makeText(this, R.string.toast_call_detail_error,
273 Toast.LENGTH_SHORT).show();
274 finish();
275 return;
276 }
277
278 // We know that all calls are from the same number and the same contact, so pick the first.
279 mNumber = details[0].number.toString();
280 final long personId = details[0].personId;
Daniel Lehmann362654a2011-07-17 14:16:47 -0700281 final Uri photoUri = details[0].photoUri;
Flavio Lerda178eeeb2011-07-11 19:51:40 +0100282
283 // Set the details header, based on the first phone call.
284 mPhoneCallDetailsHelper.setPhoneCallDetails(mPhoneCallDetailsViews,
Flavio Lerda40569562011-07-22 21:51:29 +0100285 details[0], false, false, true);
Flavio Lerda178eeeb2011-07-11 19:51:40 +0100286
Flavio Lerdacfff16d2011-07-12 23:54:40 +0100287 // Cache the details about the phone number.
288 final Uri numberCallUri = mPhoneNumberHelper.getCallUri(mNumber);
289 final boolean canPlaceCallsTo = mPhoneNumberHelper.canPlaceCallsTo(mNumber);
290 final boolean isVoicemailNumber = mPhoneNumberHelper.isVoicemailNumber(mNumber);
291 final boolean isSipNumber = mPhoneNumberHelper.isSipNumber(mNumber);
292
Flavio Lerda178eeeb2011-07-11 19:51:40 +0100293 // Let user view contact details if they exist, otherwise add option to create new contact
294 // from this number.
295 final Intent mainActionIntent;
296 final int mainActionIcon;
297
298 if (details[0].personId != -1) {
299 Uri personUri = ContentUris.withAppendedId(Contacts.CONTENT_URI, personId);
300 mainActionIntent = new Intent(Intent.ACTION_VIEW, personUri);
301 mainActionIcon = R.drawable.sym_action_view_contact;
Flavio Lerdacfff16d2011-07-12 23:54:40 +0100302 } else if (isVoicemailNumber) {
303 mainActionIntent = null;
304 mainActionIcon = 0;
305 } else if (isSipNumber) {
306 // TODO: This item is currently disabled for SIP addresses, because
307 // the Insert.PHONE extra only works correctly for PSTN numbers.
308 //
309 // To fix this for SIP addresses, we need to:
310 // - define ContactsContract.Intents.Insert.SIP_ADDRESS, and use it here if
311 // the current number is a SIP address
312 // - update the contacts UI code to handle Insert.SIP_ADDRESS by
313 // updating the SipAddress field
314 // and then we can remove the "!isSipNumber" check above.
315 mainActionIntent = null;
316 mainActionIcon = 0;
Flavio Lerda8e403402011-07-20 16:25:49 +0100317 } else if (canPlaceCallsTo) {
Flavio Lerda178eeeb2011-07-11 19:51:40 +0100318 mainActionIntent = new Intent(Intent.ACTION_INSERT_OR_EDIT);
319 mainActionIntent.setType(Contacts.CONTENT_ITEM_TYPE);
320 mainActionIntent.putExtra(Insert.PHONE, mNumber);
321 mainActionIcon = R.drawable.sym_action_add;
Flavio Lerda8e403402011-07-20 16:25:49 +0100322 } else {
323 // If we cannot call the number, when we probably cannot add it as a contact either.
324 // This is usually the case of private, unknown, or payphone numbers.
325 mainActionIntent = null;
326 mainActionIcon = 0;
Flavio Lerda178eeeb2011-07-11 19:51:40 +0100327 }
328
Flavio Lerdacfff16d2011-07-12 23:54:40 +0100329 if (mainActionIntent == null) {
330 mMainActionView.setVisibility(View.INVISIBLE);
331 } else {
332 mMainActionView.setVisibility(View.VISIBLE);
333 mMainActionView.setImageResource(mainActionIcon);
334 mMainActionView.setOnClickListener(new View.OnClickListener() {
335 @Override
336 public void onClick(View v) {
337 startActivity(mainActionIntent);
338 }
339 });
340 }
Flavio Lerda178eeeb2011-07-11 19:51:40 +0100341
342 // Build list of various available actions.
343 final List<ViewEntry> actions = new ArrayList<ViewEntry>();
344
Flavio Lerda178eeeb2011-07-11 19:51:40 +0100345 // This action allows to call the number that places the call.
Flavio Lerdacfff16d2011-07-12 23:54:40 +0100346 if (canPlaceCallsTo) {
Flavio Lerda178eeeb2011-07-11 19:51:40 +0100347 actions.add(new ViewEntry(android.R.drawable.sym_action_call,
348 getString(R.string.menu_callNumber, mNumber),
349 new Intent(Intent.ACTION_CALL_PRIVILEGED, numberCallUri)));
350 }
351
Flavio Lerdacfff16d2011-07-12 23:54:40 +0100352 // This action allows to send an SMS to the number that placed the call.
353 if (mPhoneNumberHelper.canSendSmsTo(mNumber)) {
Flavio Lerda178eeeb2011-07-11 19:51:40 +0100354 Intent smsIntent = new Intent(Intent.ACTION_SENDTO,
355 Uri.fromParts("sms", mNumber, null));
356 actions.add(new ViewEntry(R.drawable.sym_action_sms,
357 getString(R.string.menu_sendTextMessage), smsIntent));
358 }
359
360 // This action deletes all elements in the group from the call log.
Hugo Hudson157dde12011-07-21 23:28:13 +0100361 // We don't have this action for voicemails, because you can just use the trash button.
Flavio Lerda94d7bab2011-07-22 16:52:34 +0100362 mHasRemoveFromCallLog = !hasVoicemail();
363 mHasEditNumberBeforeCall = canPlaceCallsTo && !isSipNumber && !isVoicemailNumber;
Flavio Lerda178eeeb2011-07-11 19:51:40 +0100364
Flavio Lerda94d7bab2011-07-22 16:52:34 +0100365 if (actions.size() != 0) {
366 // Set the actions for this phone number.
367 setListAdapter(new ViewAdapter(this, actions));
368 getListView().setVisibility(View.VISIBLE);
369 } else {
370 getListView().setVisibility(View.GONE);
Hugo Hudson157dde12011-07-21 23:28:13 +0100371 }
Flavio Lerda178eeeb2011-07-11 19:51:40 +0100372
Flavio Lerda178eeeb2011-07-11 19:51:40 +0100373 ListView historyList = (ListView) findViewById(R.id.history);
374 historyList.setAdapter(
375 new CallDetailHistoryAdapter(this, mInflater, mCallTypeHelper, details));
Daniel Lehmann362654a2011-07-17 14:16:47 -0700376 loadContactPhotos(photoUri);
Flavio Lerda178eeeb2011-07-11 19:51:40 +0100377 }
378
379 /** Return the phone call details for a given call log URI. */
380 private PhoneCallDetails getPhoneCallDetailsForUri(Uri callUri) {
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800381 ContentResolver resolver = getContentResolver();
382 Cursor callCursor = resolver.query(callUri, CALL_LOG_PROJECTION, null, null, null);
383 try {
Flavio Lerda178eeeb2011-07-11 19:51:40 +0100384 if (callCursor == null || !callCursor.moveToFirst()) {
385 throw new IllegalArgumentException("Cannot find content: " + callUri);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800386 }
Flavio Lerda178eeeb2011-07-11 19:51:40 +0100387
388 // Read call log specifics.
389 String number = callCursor.getString(NUMBER_COLUMN_INDEX);
390 long date = callCursor.getLong(DATE_COLUMN_INDEX);
391 long duration = callCursor.getLong(DURATION_COLUMN_INDEX);
392 int callType = callCursor.getInt(CALL_TYPE_COLUMN_INDEX);
393 String countryIso = callCursor.getString(COUNTRY_ISO_COLUMN_INDEX);
394 if (TextUtils.isEmpty(countryIso)) {
395 countryIso = mDefaultCountryIso;
396 }
397
398 // Formatted phone number.
399 final CharSequence numberText;
400 // Read contact specifics.
401 CharSequence nameText = "";
402 int numberType = 0;
403 CharSequence numberLabel = "";
404 long personId = -1L;
Daniel Lehmann362654a2011-07-17 14:16:47 -0700405 Uri photoUri = null;
Flavio Lerda178eeeb2011-07-11 19:51:40 +0100406 // If this is not a regular number, there is no point in looking it up in the contacts.
407 if (!mPhoneNumberHelper.canPlaceCallsTo(number)) {
408 numberText = mPhoneNumberHelper.getDisplayNumber(number, null);
409 } else {
410 // Perform a reverse-phonebook lookup to find the contact details.
411 Uri phoneUri = Uri.withAppendedPath(PhoneLookup.CONTENT_FILTER_URI,
412 Uri.encode(number));
413 Cursor phonesCursor = resolver.query(phoneUri, PHONES_PROJECTION, null, null, null);
414 String candidateNumberText = number;
415 try {
416 if (phonesCursor != null && phonesCursor.moveToFirst()) {
417 personId = phonesCursor.getLong(COLUMN_INDEX_ID);
418 nameText = phonesCursor.getString(COLUMN_INDEX_NAME);
Daniel Lehmann362654a2011-07-17 14:16:47 -0700419 String photoUriString = phonesCursor.getString(COLUMN_INDEX_PHOTO_URI);
420 photoUri = photoUriString == null ? null : Uri.parse(photoUriString);
Flavio Lerda178eeeb2011-07-11 19:51:40 +0100421 candidateNumberText = PhoneNumberUtils.formatNumber(
422 phonesCursor.getString(COLUMN_INDEX_NUMBER),
423 phonesCursor.getString(COLUMN_INDEX_NORMALIZED_NUMBER),
424 countryIso);
425 numberType = phonesCursor.getInt(COLUMN_INDEX_TYPE);
426 numberLabel = phonesCursor.getString(COLUMN_INDEX_LABEL);
427 } else {
428 // We could not find this contact in the contacts, just format the phone
429 // number as best as we can. All the other fields will have their default
430 // values.
431 candidateNumberText =
432 PhoneNumberUtils.formatNumber(number, countryIso);
433 }
434 } finally {
435 if (phonesCursor != null) phonesCursor.close();
436 numberText = candidateNumberText;
437 }
438 }
Flavio Lerda9c466372011-07-19 17:38:17 +0100439 return new PhoneCallDetails(number, numberText, countryIso,
Flavio Lerdacb805e82011-07-18 10:31:44 +0100440 new int[]{ callType }, date, duration,
Daniel Lehmann362654a2011-07-17 14:16:47 -0700441 nameText, numberType, numberLabel, personId, photoUri);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800442 } finally {
443 if (callCursor != null) {
444 callCursor.close();
445 }
446 }
447 }
448
Flavio Lerda9cafe472011-06-08 14:06:13 +0100449 /** Load the contact photos and places them in the corresponding views. */
Daniel Lehmann362654a2011-07-17 14:16:47 -0700450 private void loadContactPhotos(Uri photoUri) {
451 mContactPhotoManager.loadPhoto(mContactBackgroundView, photoUri);
Flavio Lerda9cafe472011-06-08 14:06:13 +0100452 }
453
Flavio Lerda696e8132011-07-05 19:00:23 +0100454 private String getVoicemailNumber() {
455 TelephonyManager telephonyManager =
456 (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
457 return telephonyManager.getVoiceMailNumber();
458 }
459
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800460 static final class ViewEntry {
Flavio Lerdaa8956882011-07-09 21:34:38 +0100461 public final int icon;
462 public final String text;
463 public final Intent intent;
464 public final View.OnClickListener action;
465
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800466 public String label = null;
467 public String number = null;
Dmitri Plotnikovd0d776d2009-08-19 17:38:04 -0700468
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800469 public ViewEntry(int icon, String text, Intent intent) {
470 this.icon = icon;
471 this.text = text;
472 this.intent = intent;
Flavio Lerdaa8956882011-07-09 21:34:38 +0100473 this.action = null;
474 }
475
476 public ViewEntry(int icon, String text, View.OnClickListener listener) {
477 this.icon = icon;
478 this.text = text;
479 this.intent = null;
480 this.action = listener;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800481 }
482 }
483
484 static final class ViewAdapter extends BaseAdapter {
Dmitri Plotnikovd0d776d2009-08-19 17:38:04 -0700485
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800486 private final List<ViewEntry> mActions;
Dmitri Plotnikovd0d776d2009-08-19 17:38:04 -0700487
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800488 private final LayoutInflater mInflater;
Dmitri Plotnikovd0d776d2009-08-19 17:38:04 -0700489
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800490 public ViewAdapter(Context context, List<ViewEntry> actions) {
491 mActions = actions;
492 mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
493 }
Dmitri Plotnikovd0d776d2009-08-19 17:38:04 -0700494
Flavio Lerda9cafe472011-06-08 14:06:13 +0100495 @Override
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800496 public int getCount() {
497 return mActions.size();
498 }
499
Flavio Lerda9cafe472011-06-08 14:06:13 +0100500 @Override
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800501 public Object getItem(int position) {
502 return mActions.get(position);
503 }
504
Flavio Lerda9cafe472011-06-08 14:06:13 +0100505 @Override
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800506 public long getItemId(int position) {
507 return position;
508 }
Dmitri Plotnikovd0d776d2009-08-19 17:38:04 -0700509
Flavio Lerda9cafe472011-06-08 14:06:13 +0100510 @Override
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800511 public View getView(int position, View convertView, ViewGroup parent) {
512 // Make sure we have a valid convertView to start with
513 if (convertView == null) {
514 convertView = mInflater.inflate(R.layout.call_detail_list_item, parent, false);
515 }
516
517 // Fill action with icon and text.
518 ViewEntry entry = mActions.get(position);
519 convertView.setTag(entry);
Dmitri Plotnikovd0d776d2009-08-19 17:38:04 -0700520
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800521 ImageView icon = (ImageView) convertView.findViewById(R.id.icon);
522 TextView text = (TextView) convertView.findViewById(android.R.id.text1);
523
524 icon.setImageResource(entry.icon);
525 text.setText(entry.text);
526
527 View line2 = convertView.findViewById(R.id.line2);
528 boolean numberEmpty = TextUtils.isEmpty(entry.number);
529 boolean labelEmpty = TextUtils.isEmpty(entry.label) || numberEmpty;
530 if (labelEmpty && numberEmpty) {
531 line2.setVisibility(View.GONE);
532 } else {
533 line2.setVisibility(View.VISIBLE);
Dmitri Plotnikovd0d776d2009-08-19 17:38:04 -0700534
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800535 TextView label = (TextView) convertView.findViewById(R.id.label);
536 if (labelEmpty) {
537 label.setVisibility(View.GONE);
538 } else {
539 label.setText(entry.label);
540 label.setVisibility(View.VISIBLE);
541 }
542
543 TextView number = (TextView) convertView.findViewById(R.id.number);
544 number.setText(entry.number);
545 }
Dmitri Plotnikovd0d776d2009-08-19 17:38:04 -0700546
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800547 return convertView;
548 }
549 }
Dmitri Plotnikovd0d776d2009-08-19 17:38:04 -0700550
Flavio Lerda9cafe472011-06-08 14:06:13 +0100551 @Override
552 public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800553 // Handle passing action off to correct handler.
554 if (view.getTag() instanceof ViewEntry) {
555 ViewEntry entry = (ViewEntry) view.getTag();
556 if (entry.intent != null) {
557 startActivity(entry.intent);
Flavio Lerdaa8956882011-07-09 21:34:38 +0100558 } else if (entry.action != null) {
559 entry.action.onClick(view);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800560 }
561 }
Dmitri Plotnikovd0d776d2009-08-19 17:38:04 -0700562 }
Dmitri Plotnikov8e86b752010-02-22 17:47:57 -0800563
564 @Override
565 public void startSearch(String initialQuery, boolean selectInitialQuery, Bundle appSearchData,
566 boolean globalSearch) {
567 if (globalSearch) {
568 super.startSearch(initialQuery, selectInitialQuery, appSearchData, globalSearch);
569 } else {
570 ContactsSearchManager.startSearch(this, initialQuery);
571 }
572 }
Debashish Chatterjee0cb82242011-07-19 19:29:37 +0100573
574 protected void updateVoicemailStatusMessage(Cursor statusCursor) {
575 if (statusCursor == null) {
576 mStatusMessageView.setVisibility(View.GONE);
577 return;
578 }
579 final StatusMessage message = getStatusMessage(statusCursor);
580 if (message == null || !message.showInCallDetails()) {
581 mStatusMessageView.setVisibility(View.GONE);
582 return;
583 }
584
585 mStatusMessageView.setVisibility(View.VISIBLE);
586 mStatusMessageText.setText(message.callDetailsMessageId);
587 if (message.actionMessageId != -1) {
588 mStatusMessageAction.setText(message.actionMessageId);
589 }
590 if (message.actionUri != null) {
591 mStatusMessageAction.setClickable(true);
592 mStatusMessageAction.setOnClickListener(new View.OnClickListener() {
593 @Override
594 public void onClick(View v) {
595 startActivity(new Intent(Intent.ACTION_VIEW, message.actionUri));
596 }
597 });
598 } else {
599 mStatusMessageAction.setClickable(false);
600 }
601 }
602
603 private StatusMessage getStatusMessage(Cursor statusCursor) {
604 List<StatusMessage> messages = mVoicemailStatusHelper.getStatusMessages(statusCursor);
605 Log.d(TAG, "Num status messages: " + messages.size());
606 if (messages.size() == 0) {
607 return null;
608 }
609 // There can only be a single status message per source package, so num of messages can
610 // at most be 1.
611 if (messages.size() > 1) {
612 Log.w(TAG, String.format("Expected 1, found (%d) num of status messages." +
613 " Will use the first one.", messages.size()));
614 }
615 return messages.get(0);
616 }
Flavio Lerda94d7bab2011-07-22 16:52:34 +0100617
618 @Override
619 public boolean onCreateOptionsMenu(Menu menu) {
620 getMenuInflater().inflate(R.menu.call_details_options, menu);
621 return true;
622 }
623
624 @Override
625 public boolean onPrepareOptionsMenu(Menu menu) {
626 // This action deletes all elements in the group from the call log.
627 // We don't have this action for voicemails, because you can just use the trash button.
628 menu.findItem(R.id.remove_from_call_log).setVisible(mHasRemoveFromCallLog);
629 menu.findItem(R.id.edit_number_before_call).setVisible(mHasEditNumberBeforeCall);
630 return mHasRemoveFromCallLog || mHasEditNumberBeforeCall;
631 }
632
633 @Override
634 public boolean onMenuItemSelected(int featureId, MenuItem item) {
635 switch (item.getItemId()) {
636 case R.id.remove_from_call_log: {
637 StringBuilder callIds = new StringBuilder();
638 for (Uri callUri : getCallLogEntryUris()) {
639 if (callIds.length() != 0) {
640 callIds.append(",");
641 }
642 callIds.append(ContentUris.parseId(callUri));
643 }
644
645 getContentResolver().delete(Calls.CONTENT_URI_WITH_VOICEMAIL,
646 Calls._ID + " IN (" + callIds + ")", null);
647 // Also close the activity.
648 finish();
649 return true;
650 }
651
652 case R.id.edit_number_before_call:
653 startActivity(
654 new Intent(Intent.ACTION_DIAL, mPhoneNumberHelper.getCallUri(mNumber)));
655 return true;
656
657 default:
658 throw new IllegalArgumentException();
659 }
660 }
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800661}