blob: d0eb8179af2a7aceabc1330d47a29973c7733c7c [file] [log] [blame]
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -08001/*
2 * Copyright (C) 2009 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.contacts;
18
Flavio Lerda9a208cc2011-07-12 21:05:47 +010019import com.android.contacts.calllog.CallDetailHistoryAdapter;
20import com.android.contacts.calllog.CallTypeHelper;
Flavio Lerda178eeeb2011-07-11 19:51:40 +010021import com.android.contacts.calllog.PhoneNumberHelper;
Hugo Hudsonb002f512011-07-15 17:41:12 +010022import com.android.contacts.voicemail.VoicemailPlaybackFragment;
Debashish Chatterjee0cb82242011-07-19 19:29:37 +010023import com.android.contacts.voicemail.VoicemailStatusHelper;
24import com.android.contacts.voicemail.VoicemailStatusHelper.StatusMessage;
25import com.android.contacts.voicemail.VoicemailStatusHelperImpl;
Amith Yamasani8bbe2f22009-03-24 21:24:12 -070026
Flavio Lerdad44e83a2011-07-24 23:10:17 +010027import android.app.ActionBar;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080028import android.app.ListActivity;
29import android.content.ContentResolver;
30import android.content.ContentUris;
31import android.content.Context;
32import android.content.Intent;
33import android.content.res.Resources;
34import android.database.Cursor;
35import android.net.Uri;
36import android.os.Bundle;
37import android.provider.CallLog;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080038import android.provider.CallLog.Calls;
Flavio Lerda9cafe472011-06-08 14:06:13 +010039import android.provider.Contacts.Intents.Insert;
Dmitri Plotnikovd0d776d2009-08-19 17:38:04 -070040import android.provider.ContactsContract.Contacts;
41import android.provider.ContactsContract.PhoneLookup;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080042import android.telephony.PhoneNumberUtils;
43import android.telephony.TelephonyManager;
44import android.text.TextUtils;
Flavio Lerda178eeeb2011-07-11 19:51:40 +010045import android.util.Log;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080046import android.view.KeyEvent;
47import android.view.LayoutInflater;
Flavio Lerda94d7bab2011-07-22 16:52:34 +010048import android.view.Menu;
49import android.view.MenuItem;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080050import android.view.View;
51import android.view.ViewGroup;
52import android.widget.AdapterView;
53import android.widget.BaseAdapter;
Daniel Lehmannd260de42011-08-01 18:08:38 -070054import android.widget.ImageButton;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080055import android.widget.ImageView;
Flavio Lerda9a208cc2011-07-12 21:05:47 +010056import android.widget.ListView;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080057import android.widget.TextView;
58import android.widget.Toast;
59
60import java.util.ArrayList;
61import java.util.List;
62
63/**
64 * Displays the details of a specific call log entry.
Flavio Lerda178eeeb2011-07-11 19:51:40 +010065 * <p>
66 * This activity can be either started with the URI of a single call log entry, or with the
67 * {@link #EXTRA_CALL_LOG_IDS} extra to specify a group of call log entries.
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080068 */
69public class CallDetailActivity extends ListActivity implements
70 AdapterView.OnItemClickListener {
71 private static final String TAG = "CallDetail";
72
Flavio Lerda178eeeb2011-07-11 19:51:40 +010073 /** A long array extra containing ids of call log entries to display. */
Hugo Hudsonb002f512011-07-15 17:41:12 +010074 public static final String EXTRA_CALL_LOG_IDS = "EXTRA_CALL_LOG_IDS";
75 /** If we are started with a voicemail, we'll find the uri to play with this extra. */
76 public static final String EXTRA_VOICEMAIL_URI = "EXTRA_VOICEMAIL_URI";
77 /** If we should immediately start playback of the voicemail, this extra will be set to true. */
78 public static final String EXTRA_VOICEMAIL_START_PLAYBACK = "EXTRA_VOICEMAIL_START_PLAYBACK";
Flavio Lerda178eeeb2011-07-11 19:51:40 +010079
Flavio Lerdaa024c3f2011-06-10 10:47:07 +010080 /** The views representing the details of a phone call. */
Flavio Lerdaafb93bb2011-07-05 14:46:10 +010081 private PhoneCallDetailsViews mPhoneCallDetailsViews;
Flavio Lerda9a208cc2011-07-12 21:05:47 +010082 private CallTypeHelper mCallTypeHelper;
Flavio Lerda178eeeb2011-07-11 19:51:40 +010083 private PhoneNumberHelper mPhoneNumberHelper;
Flavio Lerdaafb93bb2011-07-05 14:46:10 +010084 private PhoneCallDetailsHelper mPhoneCallDetailsHelper;
Flavio Lerdafb63c432011-07-07 17:16:53 +010085 private ImageView mMainActionView;
Daniel Lehmannd260de42011-08-01 18:08:38 -070086 private ImageButton mMainActionPushLayerView;
Flavio Lerda9cafe472011-06-08 14:06:13 +010087 private ImageView mContactBackgroundView;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080088
89 private String mNumber = null;
Bai Tao09eb04f2010-09-01 15:34:16 +080090 private String mDefaultCountryIso;
Dmitri Plotnikovd0d776d2009-08-19 17:38:04 -070091
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080092 /* package */ LayoutInflater mInflater;
93 /* package */ Resources mResources;
Flavio Lerda9cafe472011-06-08 14:06:13 +010094 /** Helper to load contact photos. */
95 private ContactPhotoManager mContactPhotoManager;
Debashish Chatterjee0cb82242011-07-19 19:29:37 +010096 /** Helper to make async queries to content resolver. */
97 private CallDetailActivityQueryHandler mAsyncQueryHandler;
98 /** Helper to get voicemail status messages. */
99 private VoicemailStatusHelper mVoicemailStatusHelper;
100 // Views related to voicemail status message.
101 private View mStatusMessageView;
102 private TextView mStatusMessageText;
103 private TextView mStatusMessageAction;
Dmitri Plotnikovd0d776d2009-08-19 17:38:04 -0700104
Flavio Lerda94d7bab2011-07-22 16:52:34 +0100105 /** Whether we should show "edit number before call" in the options menu. */
106 private boolean mHasEditNumberBeforeCall;
107
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800108 static final String[] CALL_LOG_PROJECTION = new String[] {
109 CallLog.Calls.DATE,
110 CallLog.Calls.DURATION,
111 CallLog.Calls.NUMBER,
112 CallLog.Calls.TYPE,
Bai Tao09eb04f2010-09-01 15:34:16 +0800113 CallLog.Calls.COUNTRY_ISO,
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800114 };
Dmitri Plotnikovd0d776d2009-08-19 17:38:04 -0700115
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800116 static final int DATE_COLUMN_INDEX = 0;
117 static final int DURATION_COLUMN_INDEX = 1;
118 static final int NUMBER_COLUMN_INDEX = 2;
119 static final int CALL_TYPE_COLUMN_INDEX = 3;
Bai Tao09eb04f2010-09-01 15:34:16 +0800120 static final int COUNTRY_ISO_COLUMN_INDEX = 4;
Dmitri Plotnikovd0d776d2009-08-19 17:38:04 -0700121
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800122 static final String[] PHONES_PROJECTION = new String[] {
Dmitri Plotnikovd0d776d2009-08-19 17:38:04 -0700123 PhoneLookup._ID,
124 PhoneLookup.DISPLAY_NAME,
125 PhoneLookup.TYPE,
126 PhoneLookup.LABEL,
127 PhoneLookup.NUMBER,
Bai Tao09eb04f2010-09-01 15:34:16 +0800128 PhoneLookup.NORMALIZED_NUMBER,
Daniel Lehmann362654a2011-07-17 14:16:47 -0700129 PhoneLookup.PHOTO_URI,
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800130 };
131 static final int COLUMN_INDEX_ID = 0;
132 static final int COLUMN_INDEX_NAME = 1;
133 static final int COLUMN_INDEX_TYPE = 2;
134 static final int COLUMN_INDEX_LABEL = 3;
135 static final int COLUMN_INDEX_NUMBER = 4;
Bai Tao09eb04f2010-09-01 15:34:16 +0800136 static final int COLUMN_INDEX_NORMALIZED_NUMBER = 5;
Daniel Lehmann362654a2011-07-17 14:16:47 -0700137 static final int COLUMN_INDEX_PHOTO_URI = 6;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800138
139 @Override
140 protected void onCreate(Bundle icicle) {
141 super.onCreate(icicle);
142
143 setContentView(R.layout.call_detail);
144
145 mInflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
146 mResources = getResources();
Dmitri Plotnikovd0d776d2009-08-19 17:38:04 -0700147
Flavio Lerda696e8132011-07-05 19:00:23 +0100148 mPhoneCallDetailsViews = PhoneCallDetailsViews.fromView(getWindow().getDecorView());
Daniel Lehmann6ecb7322011-08-01 21:39:29 -0700149 mCallTypeHelper = new CallTypeHelper(getResources());
Flavio Lerda178eeeb2011-07-11 19:51:40 +0100150 mPhoneNumberHelper = new PhoneNumberHelper(mResources, getVoicemailNumber());
Flavio Lerda40569562011-07-22 21:51:29 +0100151 mPhoneCallDetailsHelper = new PhoneCallDetailsHelper(mResources, mCallTypeHelper,
Flavio Lerda178eeeb2011-07-11 19:51:40 +0100152 mPhoneNumberHelper);
Debashish Chatterjee0cb82242011-07-19 19:29:37 +0100153 mVoicemailStatusHelper = new VoicemailStatusHelperImpl();
154 mAsyncQueryHandler = new CallDetailActivityQueryHandler(this);
155 mStatusMessageView = findViewById(R.id.voicemail_status);
156 mStatusMessageText = (TextView) findViewById(R.id.voicemail_status_message);
157 mStatusMessageAction = (TextView) findViewById(R.id.voicemail_status_action);
Flavio Lerdafb63c432011-07-07 17:16:53 +0100158 mMainActionView = (ImageView) findViewById(R.id.main_action);
Daniel Lehmannd260de42011-08-01 18:08:38 -0700159 mMainActionPushLayerView = (ImageButton) findViewById(R.id.main_action_push_layer);
Flavio Lerda9cafe472011-06-08 14:06:13 +0100160 mContactBackgroundView = (ImageView) findViewById(R.id.contact_background);
Bai Tao09eb04f2010-09-01 15:34:16 +0800161 mDefaultCountryIso = ContactsUtils.getCurrentCountryIso(this);
Flavio Lerda9cafe472011-06-08 14:06:13 +0100162 mContactPhotoManager = ContactPhotoManager.getInstance(this);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800163 getListView().setOnItemClickListener(this);
Flavio Lerdad44e83a2011-07-24 23:10:17 +0100164 configureActionBar();
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800165 }
Dmitri Plotnikovd0d776d2009-08-19 17:38:04 -0700166
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800167 @Override
168 public void onResume() {
169 super.onResume();
Flavio Lerda178eeeb2011-07-11 19:51:40 +0100170 updateData(getCallLogEntryUris());
Hugo Hudson757aa552011-07-20 01:15:25 +0100171 optionallyHandleVoicemail();
Hugo Hudsonb002f512011-07-15 17:41:12 +0100172 }
173
174 /**
175 * Handle voicemail playback or hide voicemail ui.
176 * <p>
177 * If the Intent used to start this Activity contains the suitable extras, then start voicemail
178 * playback. If it doesn't, then hide the voicemail ui.
179 */
Hugo Hudson757aa552011-07-20 01:15:25 +0100180 private void optionallyHandleVoicemail() {
Hugo Hudson157dde12011-07-21 23:28:13 +0100181 if (hasVoicemail()) {
Hugo Hudsona9ad6942011-07-29 17:06:04 +0100182 // Has voicemail: add the voicemail fragment. Add suitable arguments to set the uri
183 // to play and optionally start the playback.
Hugo Hudson757aa552011-07-20 01:15:25 +0100184 // Do a query to fetch the voicemail status messages.
Hugo Hudsona9ad6942011-07-29 17:06:04 +0100185 VoicemailPlaybackFragment playbackFragment = new VoicemailPlaybackFragment();
186 Bundle fragmentArguments = new Bundle();
Hugo Hudson157dde12011-07-21 23:28:13 +0100187 Uri voicemailUri = getIntent().getParcelableExtra(EXTRA_VOICEMAIL_URI);
Hugo Hudsona9ad6942011-07-29 17:06:04 +0100188 fragmentArguments.putParcelable(EXTRA_VOICEMAIL_URI, voicemailUri);
189 if (getIntent().getBooleanExtra(EXTRA_VOICEMAIL_START_PLAYBACK, false)) {
190 fragmentArguments.putBoolean(EXTRA_VOICEMAIL_START_PLAYBACK, true);
191 }
192 playbackFragment.setArguments(fragmentArguments);
193 getFragmentManager().beginTransaction()
194 .add(R.id.voicemail_container, playbackFragment).commit();
Hugo Hudson757aa552011-07-20 01:15:25 +0100195 mAsyncQueryHandler.startVoicemailStatusQuery(voicemailUri);
196 } else {
Hugo Hudsona9ad6942011-07-29 17:06:04 +0100197 // No voicemail uri: hide the status view.
Hugo Hudson757aa552011-07-20 01:15:25 +0100198 mStatusMessageView.setVisibility(View.GONE);
Hugo Hudsonb002f512011-07-15 17:41:12 +0100199 }
Flavio Lerda178eeeb2011-07-11 19:51:40 +0100200 }
201
Hugo Hudson157dde12011-07-21 23:28:13 +0100202 private boolean hasVoicemail() {
203 return getIntent().getParcelableExtra(EXTRA_VOICEMAIL_URI) != null;
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);
Daniel Lehmannd260de42011-08-01 18:08:38 -0700293 mainActionIcon = R.drawable.ic_contacts_holo_dark;
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);
Daniel Lehmannd260de42011-08-01 18:08:38 -0700313 mainActionIcon = R.drawable.ic_add_contact_holo_dark;
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);
Daniel Lehmannd260de42011-08-01 18:08:38 -0700323 mMainActionPushLayerView.setVisibility(View.GONE);
Flavio Lerdacfff16d2011-07-12 23:54:40 +0100324 } else {
325 mMainActionView.setVisibility(View.VISIBLE);
326 mMainActionView.setImageResource(mainActionIcon);
Daniel Lehmannd260de42011-08-01 18:08:38 -0700327 mMainActionPushLayerView.setVisibility(View.VISIBLE);
328 mMainActionPushLayerView.setOnClickListener(new View.OnClickListener() {
Flavio Lerdacfff16d2011-07-12 23:54:40 +0100329 @Override
330 public void onClick(View v) {
331 startActivity(mainActionIntent);
332 }
333 });
334 }
Flavio Lerda178eeeb2011-07-11 19:51:40 +0100335
336 // Build list of various available actions.
337 final List<ViewEntry> actions = new ArrayList<ViewEntry>();
338
Flavio Lerda178eeeb2011-07-11 19:51:40 +0100339 // This action allows to call the number that places the call.
Flavio Lerdacfff16d2011-07-12 23:54:40 +0100340 if (canPlaceCallsTo) {
Flavio Lerda178eeeb2011-07-11 19:51:40 +0100341 actions.add(new ViewEntry(android.R.drawable.sym_action_call,
342 getString(R.string.menu_callNumber, mNumber),
343 new Intent(Intent.ACTION_CALL_PRIVILEGED, numberCallUri)));
344 }
345
Flavio Lerdacfff16d2011-07-12 23:54:40 +0100346 // This action allows to send an SMS to the number that placed the call.
347 if (mPhoneNumberHelper.canSendSmsTo(mNumber)) {
Flavio Lerda178eeeb2011-07-11 19:51:40 +0100348 Intent smsIntent = new Intent(Intent.ACTION_SENDTO,
349 Uri.fromParts("sms", mNumber, null));
350 actions.add(new ViewEntry(R.drawable.sym_action_sms,
351 getString(R.string.menu_sendTextMessage), smsIntent));
352 }
353
Flavio Lerda94d7bab2011-07-22 16:52:34 +0100354 mHasEditNumberBeforeCall = canPlaceCallsTo && !isSipNumber && !isVoicemailNumber;
Flavio Lerda178eeeb2011-07-11 19:51:40 +0100355
Flavio Lerda94d7bab2011-07-22 16:52:34 +0100356 if (actions.size() != 0) {
357 // Set the actions for this phone number.
358 setListAdapter(new ViewAdapter(this, actions));
359 getListView().setVisibility(View.VISIBLE);
360 } else {
361 getListView().setVisibility(View.GONE);
Hugo Hudson157dde12011-07-21 23:28:13 +0100362 }
Flavio Lerda178eeeb2011-07-11 19:51:40 +0100363
Flavio Lerda178eeeb2011-07-11 19:51:40 +0100364 ListView historyList = (ListView) findViewById(R.id.history);
365 historyList.setAdapter(
366 new CallDetailHistoryAdapter(this, mInflater, mCallTypeHelper, details));
Daniel Lehmann362654a2011-07-17 14:16:47 -0700367 loadContactPhotos(photoUri);
Flavio Lerda178eeeb2011-07-11 19:51:40 +0100368 }
369
370 /** Return the phone call details for a given call log URI. */
371 private PhoneCallDetails getPhoneCallDetailsForUri(Uri callUri) {
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800372 ContentResolver resolver = getContentResolver();
373 Cursor callCursor = resolver.query(callUri, CALL_LOG_PROJECTION, null, null, null);
374 try {
Flavio Lerda178eeeb2011-07-11 19:51:40 +0100375 if (callCursor == null || !callCursor.moveToFirst()) {
376 throw new IllegalArgumentException("Cannot find content: " + callUri);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800377 }
Flavio Lerda178eeeb2011-07-11 19:51:40 +0100378
379 // Read call log specifics.
380 String number = callCursor.getString(NUMBER_COLUMN_INDEX);
381 long date = callCursor.getLong(DATE_COLUMN_INDEX);
382 long duration = callCursor.getLong(DURATION_COLUMN_INDEX);
383 int callType = callCursor.getInt(CALL_TYPE_COLUMN_INDEX);
384 String countryIso = callCursor.getString(COUNTRY_ISO_COLUMN_INDEX);
385 if (TextUtils.isEmpty(countryIso)) {
386 countryIso = mDefaultCountryIso;
387 }
388
389 // Formatted phone number.
390 final CharSequence numberText;
391 // Read contact specifics.
392 CharSequence nameText = "";
393 int numberType = 0;
394 CharSequence numberLabel = "";
395 long personId = -1L;
Daniel Lehmann362654a2011-07-17 14:16:47 -0700396 Uri photoUri = null;
Flavio Lerda178eeeb2011-07-11 19:51:40 +0100397 // If this is not a regular number, there is no point in looking it up in the contacts.
398 if (!mPhoneNumberHelper.canPlaceCallsTo(number)) {
399 numberText = mPhoneNumberHelper.getDisplayNumber(number, null);
400 } else {
401 // Perform a reverse-phonebook lookup to find the contact details.
402 Uri phoneUri = Uri.withAppendedPath(PhoneLookup.CONTENT_FILTER_URI,
403 Uri.encode(number));
404 Cursor phonesCursor = resolver.query(phoneUri, PHONES_PROJECTION, null, null, null);
405 String candidateNumberText = number;
406 try {
407 if (phonesCursor != null && phonesCursor.moveToFirst()) {
408 personId = phonesCursor.getLong(COLUMN_INDEX_ID);
409 nameText = phonesCursor.getString(COLUMN_INDEX_NAME);
Daniel Lehmann362654a2011-07-17 14:16:47 -0700410 String photoUriString = phonesCursor.getString(COLUMN_INDEX_PHOTO_URI);
411 photoUri = photoUriString == null ? null : Uri.parse(photoUriString);
Flavio Lerda178eeeb2011-07-11 19:51:40 +0100412 candidateNumberText = PhoneNumberUtils.formatNumber(
413 phonesCursor.getString(COLUMN_INDEX_NUMBER),
414 phonesCursor.getString(COLUMN_INDEX_NORMALIZED_NUMBER),
415 countryIso);
416 numberType = phonesCursor.getInt(COLUMN_INDEX_TYPE);
417 numberLabel = phonesCursor.getString(COLUMN_INDEX_LABEL);
418 } else {
419 // We could not find this contact in the contacts, just format the phone
420 // number as best as we can. All the other fields will have their default
421 // values.
422 candidateNumberText =
423 PhoneNumberUtils.formatNumber(number, countryIso);
424 }
425 } finally {
426 if (phonesCursor != null) phonesCursor.close();
427 numberText = candidateNumberText;
428 }
429 }
Flavio Lerda9c466372011-07-19 17:38:17 +0100430 return new PhoneCallDetails(number, numberText, countryIso,
Flavio Lerdacb805e82011-07-18 10:31:44 +0100431 new int[]{ callType }, date, duration,
Daniel Lehmann362654a2011-07-17 14:16:47 -0700432 nameText, numberType, numberLabel, personId, photoUri);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800433 } finally {
434 if (callCursor != null) {
435 callCursor.close();
436 }
437 }
438 }
439
Flavio Lerda9cafe472011-06-08 14:06:13 +0100440 /** Load the contact photos and places them in the corresponding views. */
Daniel Lehmann362654a2011-07-17 14:16:47 -0700441 private void loadContactPhotos(Uri photoUri) {
442 mContactPhotoManager.loadPhoto(mContactBackgroundView, photoUri);
Flavio Lerda9cafe472011-06-08 14:06:13 +0100443 }
444
Flavio Lerda696e8132011-07-05 19:00:23 +0100445 private String getVoicemailNumber() {
446 TelephonyManager telephonyManager =
447 (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
448 return telephonyManager.getVoiceMailNumber();
449 }
450
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800451 static final class ViewEntry {
Flavio Lerdaa8956882011-07-09 21:34:38 +0100452 public final int icon;
453 public final String text;
454 public final Intent intent;
455 public final View.OnClickListener action;
456
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800457 public String label = null;
458 public String number = null;
Dmitri Plotnikovd0d776d2009-08-19 17:38:04 -0700459
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800460 public ViewEntry(int icon, String text, Intent intent) {
461 this.icon = icon;
462 this.text = text;
463 this.intent = intent;
Flavio Lerdaa8956882011-07-09 21:34:38 +0100464 this.action = null;
465 }
466
467 public ViewEntry(int icon, String text, View.OnClickListener listener) {
468 this.icon = icon;
469 this.text = text;
470 this.intent = null;
471 this.action = listener;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800472 }
473 }
474
475 static final class ViewAdapter extends BaseAdapter {
Dmitri Plotnikovd0d776d2009-08-19 17:38:04 -0700476
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800477 private final List<ViewEntry> mActions;
Dmitri Plotnikovd0d776d2009-08-19 17:38:04 -0700478
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800479 private final LayoutInflater mInflater;
Dmitri Plotnikovd0d776d2009-08-19 17:38:04 -0700480
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800481 public ViewAdapter(Context context, List<ViewEntry> actions) {
482 mActions = actions;
483 mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
484 }
Dmitri Plotnikovd0d776d2009-08-19 17:38:04 -0700485
Flavio Lerda9cafe472011-06-08 14:06:13 +0100486 @Override
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800487 public int getCount() {
488 return mActions.size();
489 }
490
Flavio Lerda9cafe472011-06-08 14:06:13 +0100491 @Override
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800492 public Object getItem(int position) {
493 return mActions.get(position);
494 }
495
Flavio Lerda9cafe472011-06-08 14:06:13 +0100496 @Override
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800497 public long getItemId(int position) {
498 return position;
499 }
Dmitri Plotnikovd0d776d2009-08-19 17:38:04 -0700500
Flavio Lerda9cafe472011-06-08 14:06:13 +0100501 @Override
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800502 public View getView(int position, View convertView, ViewGroup parent) {
503 // Make sure we have a valid convertView to start with
504 if (convertView == null) {
505 convertView = mInflater.inflate(R.layout.call_detail_list_item, parent, false);
506 }
507
508 // Fill action with icon and text.
509 ViewEntry entry = mActions.get(position);
510 convertView.setTag(entry);
Dmitri Plotnikovd0d776d2009-08-19 17:38:04 -0700511
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800512 ImageView icon = (ImageView) convertView.findViewById(R.id.icon);
513 TextView text = (TextView) convertView.findViewById(android.R.id.text1);
514
515 icon.setImageResource(entry.icon);
516 text.setText(entry.text);
517
518 View line2 = convertView.findViewById(R.id.line2);
519 boolean numberEmpty = TextUtils.isEmpty(entry.number);
520 boolean labelEmpty = TextUtils.isEmpty(entry.label) || numberEmpty;
521 if (labelEmpty && numberEmpty) {
522 line2.setVisibility(View.GONE);
523 } else {
524 line2.setVisibility(View.VISIBLE);
Dmitri Plotnikovd0d776d2009-08-19 17:38:04 -0700525
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800526 TextView label = (TextView) convertView.findViewById(R.id.label);
527 if (labelEmpty) {
528 label.setVisibility(View.GONE);
529 } else {
530 label.setText(entry.label);
531 label.setVisibility(View.VISIBLE);
532 }
533
534 TextView number = (TextView) convertView.findViewById(R.id.number);
535 number.setText(entry.number);
536 }
Dmitri Plotnikovd0d776d2009-08-19 17:38:04 -0700537
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800538 return convertView;
539 }
540 }
Dmitri Plotnikovd0d776d2009-08-19 17:38:04 -0700541
Flavio Lerda9cafe472011-06-08 14:06:13 +0100542 @Override
543 public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800544 // Handle passing action off to correct handler.
545 if (view.getTag() instanceof ViewEntry) {
546 ViewEntry entry = (ViewEntry) view.getTag();
547 if (entry.intent != null) {
548 startActivity(entry.intent);
Flavio Lerdaa8956882011-07-09 21:34:38 +0100549 } else if (entry.action != null) {
550 entry.action.onClick(view);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800551 }
552 }
Dmitri Plotnikovd0d776d2009-08-19 17:38:04 -0700553 }
Dmitri Plotnikov8e86b752010-02-22 17:47:57 -0800554
555 @Override
556 public void startSearch(String initialQuery, boolean selectInitialQuery, Bundle appSearchData,
557 boolean globalSearch) {
558 if (globalSearch) {
559 super.startSearch(initialQuery, selectInitialQuery, appSearchData, globalSearch);
560 } else {
561 ContactsSearchManager.startSearch(this, initialQuery);
562 }
563 }
Debashish Chatterjee0cb82242011-07-19 19:29:37 +0100564
565 protected void updateVoicemailStatusMessage(Cursor statusCursor) {
566 if (statusCursor == null) {
567 mStatusMessageView.setVisibility(View.GONE);
568 return;
569 }
570 final StatusMessage message = getStatusMessage(statusCursor);
571 if (message == null || !message.showInCallDetails()) {
572 mStatusMessageView.setVisibility(View.GONE);
573 return;
574 }
575
576 mStatusMessageView.setVisibility(View.VISIBLE);
577 mStatusMessageText.setText(message.callDetailsMessageId);
578 if (message.actionMessageId != -1) {
579 mStatusMessageAction.setText(message.actionMessageId);
580 }
581 if (message.actionUri != null) {
582 mStatusMessageAction.setClickable(true);
583 mStatusMessageAction.setOnClickListener(new View.OnClickListener() {
584 @Override
585 public void onClick(View v) {
586 startActivity(new Intent(Intent.ACTION_VIEW, message.actionUri));
587 }
588 });
589 } else {
590 mStatusMessageAction.setClickable(false);
591 }
592 }
593
594 private StatusMessage getStatusMessage(Cursor statusCursor) {
595 List<StatusMessage> messages = mVoicemailStatusHelper.getStatusMessages(statusCursor);
596 Log.d(TAG, "Num status messages: " + messages.size());
597 if (messages.size() == 0) {
598 return null;
599 }
600 // There can only be a single status message per source package, so num of messages can
601 // at most be 1.
602 if (messages.size() > 1) {
603 Log.w(TAG, String.format("Expected 1, found (%d) num of status messages." +
604 " Will use the first one.", messages.size()));
605 }
606 return messages.get(0);
607 }
Flavio Lerda94d7bab2011-07-22 16:52:34 +0100608
609 @Override
610 public boolean onCreateOptionsMenu(Menu menu) {
611 getMenuInflater().inflate(R.menu.call_details_options, menu);
Hugo Hudsonc2f09c32011-07-30 16:31:28 +0100612 return super.onCreateOptionsMenu(menu);
Flavio Lerda94d7bab2011-07-22 16:52:34 +0100613 }
614
615 @Override
616 public boolean onPrepareOptionsMenu(Menu menu) {
617 // This action deletes all elements in the group from the call log.
618 // We don't have this action for voicemails, because you can just use the trash button.
Hugo Hudsonc2f09c32011-07-30 16:31:28 +0100619 menu.findItem(R.id.menu_remove_from_call_log).setVisible(!hasVoicemail());
620 menu.findItem(R.id.menu_edit_number_before_call).setVisible(mHasEditNumberBeforeCall);
621 menu.findItem(R.id.menu_trash).setVisible(hasVoicemail());
622 menu.findItem(R.id.menu_share_voicemail).setVisible(hasVoicemail());
623 return super.onPrepareOptionsMenu(menu);
Flavio Lerda94d7bab2011-07-22 16:52:34 +0100624 }
625
626 @Override
627 public boolean onMenuItemSelected(int featureId, MenuItem item) {
628 switch (item.getItemId()) {
Flavio Lerdad44e83a2011-07-24 23:10:17 +0100629 case android.R.id.home: {
630 onHomeSelected();
631 return true;
632 }
633
Hugo Hudsonc2f09c32011-07-30 16:31:28 +0100634 // All the options menu items are handled by onMenu... methods.
Flavio Lerda94d7bab2011-07-22 16:52:34 +0100635 default:
636 throw new IllegalArgumentException();
637 }
638 }
Flavio Lerdad44e83a2011-07-24 23:10:17 +0100639
Hugo Hudsonc2f09c32011-07-30 16:31:28 +0100640 public void onMenuRemoveFromCallLog(MenuItem menuItem) {
641 StringBuilder callIds = new StringBuilder();
642 for (Uri callUri : getCallLogEntryUris()) {
643 if (callIds.length() != 0) {
644 callIds.append(",");
645 }
646 callIds.append(ContentUris.parseId(callUri));
647 }
648
649 getContentResolver().delete(Calls.CONTENT_URI_WITH_VOICEMAIL,
650 Calls._ID + " IN (" + callIds + ")", null);
651 // Also close the activity.
652 finish();
653 }
654
655 public void onMenuEditNumberBeforeCall(MenuItem menuItem) {
656 startActivity(new Intent(Intent.ACTION_DIAL, mPhoneNumberHelper.getCallUri(mNumber)));
657 }
658
659 public void onMenuShareVoicemail(MenuItem menuItem) {
660 Log.w(TAG, "onMenuShareVoicemail not yet implemented");
661 }
662
663 public void onMenuTrashVoicemail(MenuItem menuItem) {
664 Log.w(TAG, "onMenuTrashVoicemail not yet implemented");
665 }
666
Flavio Lerdad44e83a2011-07-24 23:10:17 +0100667 private void configureActionBar() {
668 ActionBar actionBar = getActionBar();
669 if (actionBar != null) {
Hugo Hudsonc2f09c32011-07-30 16:31:28 +0100670 actionBar.setDisplayOptions(ActionBar.DISPLAY_HOME_AS_UP | ActionBar.DISPLAY_SHOW_HOME);
Flavio Lerdad44e83a2011-07-24 23:10:17 +0100671 actionBar.setIcon(R.drawable.ic_ab_dialer_holo_dark);
672 }
673 }
674
675 /** Invoked when the user presses the home button in the action bar. */
676 private void onHomeSelected() {
677 Intent intent = new Intent(Intent.ACTION_VIEW, Calls.CONTENT_URI);
678 // This will open the call log even if the detail view has been opened directly.
679 intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
680 startActivity(intent);
681 finish();
682 }
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800683}