blob: dd71a90a004c98de9edefac4ddfee3c91d704d83 [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;
Hugo Hudson5bbe25d2011-08-03 17:16:42 +010036import android.os.AsyncTask;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080037import 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;
Daniel Lehmannd260de42011-08-01 18:08:38 -070055import android.widget.ImageButton;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080056import android.widget.ImageView;
Flavio Lerda9a208cc2011-07-12 21:05:47 +010057import android.widget.ListView;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080058import android.widget.TextView;
59import android.widget.Toast;
60
61import java.util.ArrayList;
62import java.util.List;
63
64/**
65 * Displays the details of a specific call log entry.
Flavio Lerda178eeeb2011-07-11 19:51:40 +010066 * <p>
67 * This activity can be either started with the URI of a single call log entry, or with the
68 * {@link #EXTRA_CALL_LOG_IDS} extra to specify a group of call log entries.
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080069 */
70public class CallDetailActivity extends ListActivity implements
71 AdapterView.OnItemClickListener {
72 private static final String TAG = "CallDetail";
73
Flavio Lerda178eeeb2011-07-11 19:51:40 +010074 /** A long array extra containing ids of call log entries to display. */
Hugo Hudsonb002f512011-07-15 17:41:12 +010075 public static final String EXTRA_CALL_LOG_IDS = "EXTRA_CALL_LOG_IDS";
76 /** If we are started with a voicemail, we'll find the uri to play with this extra. */
77 public static final String EXTRA_VOICEMAIL_URI = "EXTRA_VOICEMAIL_URI";
78 /** If we should immediately start playback of the voicemail, this extra will be set to true. */
79 public static final String EXTRA_VOICEMAIL_START_PLAYBACK = "EXTRA_VOICEMAIL_START_PLAYBACK";
Flavio Lerda178eeeb2011-07-11 19:51:40 +010080
Flavio Lerdaa024c3f2011-06-10 10:47:07 +010081 /** The views representing the details of a phone call. */
Flavio Lerdaafb93bb2011-07-05 14:46:10 +010082 private PhoneCallDetailsViews mPhoneCallDetailsViews;
Flavio Lerda9a208cc2011-07-12 21:05:47 +010083 private CallTypeHelper mCallTypeHelper;
Flavio Lerda178eeeb2011-07-11 19:51:40 +010084 private PhoneNumberHelper mPhoneNumberHelper;
Flavio Lerdaafb93bb2011-07-05 14:46:10 +010085 private PhoneCallDetailsHelper mPhoneCallDetailsHelper;
Flavio Lerdafb63c432011-07-07 17:16:53 +010086 private ImageView mMainActionView;
Daniel Lehmannd260de42011-08-01 18:08:38 -070087 private ImageButton mMainActionPushLayerView;
Flavio Lerda9cafe472011-06-08 14:06:13 +010088 private ImageView mContactBackgroundView;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080089
90 private String mNumber = null;
Bai Tao09eb04f2010-09-01 15:34:16 +080091 private String mDefaultCountryIso;
Dmitri Plotnikovd0d776d2009-08-19 17:38:04 -070092
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080093 /* package */ LayoutInflater mInflater;
94 /* package */ Resources mResources;
Flavio Lerda9cafe472011-06-08 14:06:13 +010095 /** Helper to load contact photos. */
96 private ContactPhotoManager mContactPhotoManager;
Debashish Chatterjee0cb82242011-07-19 19:29:37 +010097 /** Helper to make async queries to content resolver. */
98 private CallDetailActivityQueryHandler mAsyncQueryHandler;
99 /** Helper to get voicemail status messages. */
100 private VoicemailStatusHelper mVoicemailStatusHelper;
101 // Views related to voicemail status message.
102 private View mStatusMessageView;
103 private TextView mStatusMessageText;
104 private TextView mStatusMessageAction;
Dmitri Plotnikovd0d776d2009-08-19 17:38:04 -0700105
Flavio Lerda94d7bab2011-07-22 16:52:34 +0100106 /** 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());
Daniel Lehmann6ecb7322011-08-01 21:39:29 -0700150 mCallTypeHelper = new CallTypeHelper(getResources());
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);
Daniel Lehmannd260de42011-08-01 18:08:38 -0700160 mMainActionPushLayerView = (ImageButton) findViewById(R.id.main_action_push_layer);
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 Lerdad44e83a2011-07-24 23:10:17 +0100165 configureActionBar();
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800166 }
Dmitri Plotnikovd0d776d2009-08-19 17:38:04 -0700167
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800168 @Override
169 public void onResume() {
170 super.onResume();
Flavio Lerda178eeeb2011-07-11 19:51:40 +0100171 updateData(getCallLogEntryUris());
Hugo Hudson757aa552011-07-20 01:15:25 +0100172 optionallyHandleVoicemail();
Hugo Hudsonb002f512011-07-15 17:41:12 +0100173 }
174
175 /**
176 * Handle voicemail playback or hide voicemail ui.
177 * <p>
178 * If the Intent used to start this Activity contains the suitable extras, then start voicemail
179 * playback. If it doesn't, then hide the voicemail ui.
180 */
Hugo Hudson757aa552011-07-20 01:15:25 +0100181 private void optionallyHandleVoicemail() {
Hugo Hudson157dde12011-07-21 23:28:13 +0100182 if (hasVoicemail()) {
Hugo Hudsona9ad6942011-07-29 17:06:04 +0100183 // Has voicemail: add the voicemail fragment. Add suitable arguments to set the uri
184 // to play and optionally start the playback.
Hugo Hudson757aa552011-07-20 01:15:25 +0100185 // Do a query to fetch the voicemail status messages.
Hugo Hudsona9ad6942011-07-29 17:06:04 +0100186 VoicemailPlaybackFragment playbackFragment = new VoicemailPlaybackFragment();
187 Bundle fragmentArguments = new Bundle();
Hugo Hudson7b02fde2011-08-02 20:56:48 +0100188 fragmentArguments.putParcelable(EXTRA_VOICEMAIL_URI, getVoicemailUri());
Hugo Hudsona9ad6942011-07-29 17:06:04 +0100189 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 Hudson7b02fde2011-08-02 20:56:48 +0100195 mAsyncQueryHandler.startVoicemailStatusQuery(getVoicemailUri());
Hugo Hudson757aa552011-07-20 01:15:25 +0100196 } 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() {
Hugo Hudson7b02fde2011-08-02 20:56:48 +0100203 return getVoicemailUri() != null;
204 }
205
206 private Uri getVoicemailUri() {
207 return getIntent().getParcelableExtra(EXTRA_VOICEMAIL_URI);
Hugo Hudson157dde12011-07-21 23:28:13 +0100208 }
209
Flavio Lerda178eeeb2011-07-11 19:51:40 +0100210 /**
211 * Returns the list of URIs to show.
212 * <p>
213 * There are two ways the URIs can be provided to the activity: as the data on the intent, or as
214 * a list of ids in the call log added as an extra on the URI.
215 * <p>
216 * If both are available, the data on the intent takes precedence.
217 */
218 private Uri[] getCallLogEntryUris() {
219 Uri uri = getIntent().getData();
220 if (uri != null) {
221 // If there is a data on the intent, it takes precedence over the extra.
222 return new Uri[]{ uri };
223 }
224 long[] ids = getIntent().getLongArrayExtra(EXTRA_CALL_LOG_IDS);
225 Uri[] uris = new Uri[ids.length];
226 for (int index = 0; index < ids.length; ++index) {
227 uris[index] = ContentUris.withAppendedId(Calls.CONTENT_URI_WITH_VOICEMAIL, ids[index]);
228 }
229 return uris;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800230 }
231
232 @Override
233 public boolean onKeyDown(int keyCode, KeyEvent event) {
234 switch (keyCode) {
235 case KeyEvent.KEYCODE_CALL: {
236 // Make sure phone isn't already busy before starting direct call
237 TelephonyManager tm = (TelephonyManager)
238 getSystemService(Context.TELEPHONY_SERVICE);
239 if (tm.getCallState() == TelephonyManager.CALL_STATE_IDLE) {
240 Intent callIntent = new Intent(Intent.ACTION_CALL_PRIVILEGED,
241 Uri.fromParts("tel", mNumber, null));
242 startActivity(callIntent);
243 return true;
244 }
245 }
246 }
Dmitri Plotnikovd0d776d2009-08-19 17:38:04 -0700247
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800248 return super.onKeyDown(keyCode, event);
249 }
Dmitri Plotnikovd0d776d2009-08-19 17:38:04 -0700250
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800251 /**
252 * Update user interface with details of given call.
Dmitri Plotnikovd0d776d2009-08-19 17:38:04 -0700253 *
Flavio Lerda178eeeb2011-07-11 19:51:40 +0100254 * @param callUris URIs into {@link CallLog.Calls} of the calls to be displayed
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800255 */
Flavio Lerda178eeeb2011-07-11 19:51:40 +0100256 private void updateData(final Uri... callUris) {
257 // TODO: All phone calls correspond to the same person, so we can make a single lookup.
258 final int numCalls = callUris.length;
259 final PhoneCallDetails[] details = new PhoneCallDetails[numCalls];
260 try {
261 for (int index = 0; index < numCalls; ++index) {
262 details[index] = getPhoneCallDetailsForUri(callUris[index]);
263 }
264 } catch (IllegalArgumentException e) {
265 // Something went wrong reading in our primary data, so we're going to
266 // bail out and show error to users.
267 Log.w(TAG, "invalid URI starting call details", e);
268 Toast.makeText(this, R.string.toast_call_detail_error,
269 Toast.LENGTH_SHORT).show();
270 finish();
271 return;
272 }
273
274 // We know that all calls are from the same number and the same contact, so pick the first.
275 mNumber = details[0].number.toString();
276 final long personId = details[0].personId;
Daniel Lehmann362654a2011-07-17 14:16:47 -0700277 final Uri photoUri = details[0].photoUri;
Flavio Lerda178eeeb2011-07-11 19:51:40 +0100278
279 // Set the details header, based on the first phone call.
280 mPhoneCallDetailsHelper.setPhoneCallDetails(mPhoneCallDetailsViews,
Flavio Lerda40569562011-07-22 21:51:29 +0100281 details[0], false, false, true);
Flavio Lerda178eeeb2011-07-11 19:51:40 +0100282
Flavio Lerdacfff16d2011-07-12 23:54:40 +0100283 // Cache the details about the phone number.
284 final Uri numberCallUri = mPhoneNumberHelper.getCallUri(mNumber);
285 final boolean canPlaceCallsTo = mPhoneNumberHelper.canPlaceCallsTo(mNumber);
286 final boolean isVoicemailNumber = mPhoneNumberHelper.isVoicemailNumber(mNumber);
287 final boolean isSipNumber = mPhoneNumberHelper.isSipNumber(mNumber);
288
Flavio Lerda178eeeb2011-07-11 19:51:40 +0100289 // Let user view contact details if they exist, otherwise add option to create new contact
290 // from this number.
291 final Intent mainActionIntent;
292 final int mainActionIcon;
293
294 if (details[0].personId != -1) {
295 Uri personUri = ContentUris.withAppendedId(Contacts.CONTENT_URI, personId);
296 mainActionIntent = new Intent(Intent.ACTION_VIEW, personUri);
Daniel Lehmannd260de42011-08-01 18:08:38 -0700297 mainActionIcon = R.drawable.ic_contacts_holo_dark;
Flavio Lerdacfff16d2011-07-12 23:54:40 +0100298 } else if (isVoicemailNumber) {
299 mainActionIntent = null;
300 mainActionIcon = 0;
301 } else if (isSipNumber) {
302 // TODO: This item is currently disabled for SIP addresses, because
303 // the Insert.PHONE extra only works correctly for PSTN numbers.
304 //
305 // To fix this for SIP addresses, we need to:
306 // - define ContactsContract.Intents.Insert.SIP_ADDRESS, and use it here if
307 // the current number is a SIP address
308 // - update the contacts UI code to handle Insert.SIP_ADDRESS by
309 // updating the SipAddress field
310 // and then we can remove the "!isSipNumber" check above.
311 mainActionIntent = null;
312 mainActionIcon = 0;
Flavio Lerda8e403402011-07-20 16:25:49 +0100313 } else if (canPlaceCallsTo) {
Flavio Lerda178eeeb2011-07-11 19:51:40 +0100314 mainActionIntent = new Intent(Intent.ACTION_INSERT_OR_EDIT);
315 mainActionIntent.setType(Contacts.CONTENT_ITEM_TYPE);
316 mainActionIntent.putExtra(Insert.PHONE, mNumber);
Daniel Lehmannd260de42011-08-01 18:08:38 -0700317 mainActionIcon = R.drawable.ic_add_contact_holo_dark;
Flavio Lerda8e403402011-07-20 16:25:49 +0100318 } else {
319 // If we cannot call the number, when we probably cannot add it as a contact either.
320 // This is usually the case of private, unknown, or payphone numbers.
321 mainActionIntent = null;
322 mainActionIcon = 0;
Flavio Lerda178eeeb2011-07-11 19:51:40 +0100323 }
324
Flavio Lerdacfff16d2011-07-12 23:54:40 +0100325 if (mainActionIntent == null) {
326 mMainActionView.setVisibility(View.INVISIBLE);
Daniel Lehmannd260de42011-08-01 18:08:38 -0700327 mMainActionPushLayerView.setVisibility(View.GONE);
Flavio Lerdacfff16d2011-07-12 23:54:40 +0100328 } else {
329 mMainActionView.setVisibility(View.VISIBLE);
330 mMainActionView.setImageResource(mainActionIcon);
Daniel Lehmannd260de42011-08-01 18:08:38 -0700331 mMainActionPushLayerView.setVisibility(View.VISIBLE);
332 mMainActionPushLayerView.setOnClickListener(new View.OnClickListener() {
Flavio Lerdacfff16d2011-07-12 23:54:40 +0100333 @Override
334 public void onClick(View v) {
335 startActivity(mainActionIntent);
336 }
337 });
338 }
Flavio Lerda178eeeb2011-07-11 19:51:40 +0100339
340 // Build list of various available actions.
341 final List<ViewEntry> actions = new ArrayList<ViewEntry>();
342
Flavio Lerda178eeeb2011-07-11 19:51:40 +0100343 // This action allows to call the number that places the call.
Flavio Lerdacfff16d2011-07-12 23:54:40 +0100344 if (canPlaceCallsTo) {
Flavio Lerda178eeeb2011-07-11 19:51:40 +0100345 actions.add(new ViewEntry(android.R.drawable.sym_action_call,
346 getString(R.string.menu_callNumber, mNumber),
347 new Intent(Intent.ACTION_CALL_PRIVILEGED, numberCallUri)));
348 }
349
Flavio Lerdacfff16d2011-07-12 23:54:40 +0100350 // This action allows to send an SMS to the number that placed the call.
351 if (mPhoneNumberHelper.canSendSmsTo(mNumber)) {
Flavio Lerda178eeeb2011-07-11 19:51:40 +0100352 Intent smsIntent = new Intent(Intent.ACTION_SENDTO,
353 Uri.fromParts("sms", mNumber, null));
354 actions.add(new ViewEntry(R.drawable.sym_action_sms,
355 getString(R.string.menu_sendTextMessage), smsIntent));
356 }
357
Flavio Lerda94d7bab2011-07-22 16:52:34 +0100358 mHasEditNumberBeforeCall = canPlaceCallsTo && !isSipNumber && !isVoicemailNumber;
Flavio Lerda178eeeb2011-07-11 19:51:40 +0100359
Flavio Lerda94d7bab2011-07-22 16:52:34 +0100360 if (actions.size() != 0) {
361 // Set the actions for this phone number.
362 setListAdapter(new ViewAdapter(this, actions));
363 getListView().setVisibility(View.VISIBLE);
364 } else {
365 getListView().setVisibility(View.GONE);
Hugo Hudson157dde12011-07-21 23:28:13 +0100366 }
Flavio Lerda178eeeb2011-07-11 19:51:40 +0100367
Flavio Lerda178eeeb2011-07-11 19:51:40 +0100368 ListView historyList = (ListView) findViewById(R.id.history);
369 historyList.setAdapter(
370 new CallDetailHistoryAdapter(this, mInflater, mCallTypeHelper, details));
Daniel Lehmann362654a2011-07-17 14:16:47 -0700371 loadContactPhotos(photoUri);
Flavio Lerda178eeeb2011-07-11 19:51:40 +0100372 }
373
374 /** Return the phone call details for a given call log URI. */
375 private PhoneCallDetails getPhoneCallDetailsForUri(Uri callUri) {
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800376 ContentResolver resolver = getContentResolver();
377 Cursor callCursor = resolver.query(callUri, CALL_LOG_PROJECTION, null, null, null);
378 try {
Flavio Lerda178eeeb2011-07-11 19:51:40 +0100379 if (callCursor == null || !callCursor.moveToFirst()) {
380 throw new IllegalArgumentException("Cannot find content: " + callUri);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800381 }
Flavio Lerda178eeeb2011-07-11 19:51:40 +0100382
383 // Read call log specifics.
384 String number = callCursor.getString(NUMBER_COLUMN_INDEX);
385 long date = callCursor.getLong(DATE_COLUMN_INDEX);
386 long duration = callCursor.getLong(DURATION_COLUMN_INDEX);
387 int callType = callCursor.getInt(CALL_TYPE_COLUMN_INDEX);
388 String countryIso = callCursor.getString(COUNTRY_ISO_COLUMN_INDEX);
389 if (TextUtils.isEmpty(countryIso)) {
390 countryIso = mDefaultCountryIso;
391 }
392
393 // Formatted phone number.
394 final CharSequence numberText;
395 // Read contact specifics.
396 CharSequence nameText = "";
397 int numberType = 0;
398 CharSequence numberLabel = "";
399 long personId = -1L;
Daniel Lehmann362654a2011-07-17 14:16:47 -0700400 Uri photoUri = null;
Flavio Lerda178eeeb2011-07-11 19:51:40 +0100401 // If this is not a regular number, there is no point in looking it up in the contacts.
402 if (!mPhoneNumberHelper.canPlaceCallsTo(number)) {
403 numberText = mPhoneNumberHelper.getDisplayNumber(number, null);
404 } else {
405 // Perform a reverse-phonebook lookup to find the contact details.
406 Uri phoneUri = Uri.withAppendedPath(PhoneLookup.CONTENT_FILTER_URI,
407 Uri.encode(number));
408 Cursor phonesCursor = resolver.query(phoneUri, PHONES_PROJECTION, null, null, null);
409 String candidateNumberText = number;
410 try {
411 if (phonesCursor != null && phonesCursor.moveToFirst()) {
412 personId = phonesCursor.getLong(COLUMN_INDEX_ID);
413 nameText = phonesCursor.getString(COLUMN_INDEX_NAME);
Daniel Lehmann362654a2011-07-17 14:16:47 -0700414 String photoUriString = phonesCursor.getString(COLUMN_INDEX_PHOTO_URI);
415 photoUri = photoUriString == null ? null : Uri.parse(photoUriString);
Flavio Lerda178eeeb2011-07-11 19:51:40 +0100416 candidateNumberText = PhoneNumberUtils.formatNumber(
417 phonesCursor.getString(COLUMN_INDEX_NUMBER),
418 phonesCursor.getString(COLUMN_INDEX_NORMALIZED_NUMBER),
419 countryIso);
420 numberType = phonesCursor.getInt(COLUMN_INDEX_TYPE);
421 numberLabel = phonesCursor.getString(COLUMN_INDEX_LABEL);
422 } else {
423 // We could not find this contact in the contacts, just format the phone
424 // number as best as we can. All the other fields will have their default
425 // values.
426 candidateNumberText =
427 PhoneNumberUtils.formatNumber(number, countryIso);
428 }
429 } finally {
430 if (phonesCursor != null) phonesCursor.close();
431 numberText = candidateNumberText;
432 }
433 }
Flavio Lerda9c466372011-07-19 17:38:17 +0100434 return new PhoneCallDetails(number, numberText, countryIso,
Flavio Lerdacb805e82011-07-18 10:31:44 +0100435 new int[]{ callType }, date, duration,
Daniel Lehmann362654a2011-07-17 14:16:47 -0700436 nameText, numberType, numberLabel, personId, photoUri);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800437 } finally {
438 if (callCursor != null) {
439 callCursor.close();
440 }
441 }
442 }
443
Flavio Lerda9cafe472011-06-08 14:06:13 +0100444 /** Load the contact photos and places them in the corresponding views. */
Daniel Lehmann362654a2011-07-17 14:16:47 -0700445 private void loadContactPhotos(Uri photoUri) {
446 mContactPhotoManager.loadPhoto(mContactBackgroundView, photoUri);
Flavio Lerda9cafe472011-06-08 14:06:13 +0100447 }
448
Flavio Lerda696e8132011-07-05 19:00:23 +0100449 private String getVoicemailNumber() {
450 TelephonyManager telephonyManager =
451 (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
452 return telephonyManager.getVoiceMailNumber();
453 }
454
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800455 static final class ViewEntry {
Flavio Lerdaa8956882011-07-09 21:34:38 +0100456 public final int icon;
457 public final String text;
458 public final Intent intent;
459 public final View.OnClickListener action;
460
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800461 public String label = null;
462 public String number = null;
Dmitri Plotnikovd0d776d2009-08-19 17:38:04 -0700463
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800464 public ViewEntry(int icon, String text, Intent intent) {
465 this.icon = icon;
466 this.text = text;
467 this.intent = intent;
Flavio Lerdaa8956882011-07-09 21:34:38 +0100468 this.action = null;
469 }
470
471 public ViewEntry(int icon, String text, View.OnClickListener listener) {
472 this.icon = icon;
473 this.text = text;
474 this.intent = null;
475 this.action = listener;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800476 }
477 }
478
479 static final class ViewAdapter extends BaseAdapter {
Dmitri Plotnikovd0d776d2009-08-19 17:38:04 -0700480
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800481 private final List<ViewEntry> mActions;
Dmitri Plotnikovd0d776d2009-08-19 17:38:04 -0700482
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800483 private final LayoutInflater mInflater;
Dmitri Plotnikovd0d776d2009-08-19 17:38:04 -0700484
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800485 public ViewAdapter(Context context, List<ViewEntry> actions) {
486 mActions = actions;
487 mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
488 }
Dmitri Plotnikovd0d776d2009-08-19 17:38:04 -0700489
Flavio Lerda9cafe472011-06-08 14:06:13 +0100490 @Override
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800491 public int getCount() {
492 return mActions.size();
493 }
494
Flavio Lerda9cafe472011-06-08 14:06:13 +0100495 @Override
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800496 public Object getItem(int position) {
497 return mActions.get(position);
498 }
499
Flavio Lerda9cafe472011-06-08 14:06:13 +0100500 @Override
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800501 public long getItemId(int position) {
502 return position;
503 }
Dmitri Plotnikovd0d776d2009-08-19 17:38:04 -0700504
Flavio Lerda9cafe472011-06-08 14:06:13 +0100505 @Override
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800506 public View getView(int position, View convertView, ViewGroup parent) {
507 // Make sure we have a valid convertView to start with
508 if (convertView == null) {
509 convertView = mInflater.inflate(R.layout.call_detail_list_item, parent, false);
510 }
511
512 // Fill action with icon and text.
513 ViewEntry entry = mActions.get(position);
514 convertView.setTag(entry);
Dmitri Plotnikovd0d776d2009-08-19 17:38:04 -0700515
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800516 ImageView icon = (ImageView) convertView.findViewById(R.id.icon);
517 TextView text = (TextView) convertView.findViewById(android.R.id.text1);
518
519 icon.setImageResource(entry.icon);
520 text.setText(entry.text);
521
522 View line2 = convertView.findViewById(R.id.line2);
523 boolean numberEmpty = TextUtils.isEmpty(entry.number);
524 boolean labelEmpty = TextUtils.isEmpty(entry.label) || numberEmpty;
525 if (labelEmpty && numberEmpty) {
526 line2.setVisibility(View.GONE);
527 } else {
528 line2.setVisibility(View.VISIBLE);
Dmitri Plotnikovd0d776d2009-08-19 17:38:04 -0700529
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800530 TextView label = (TextView) convertView.findViewById(R.id.label);
531 if (labelEmpty) {
532 label.setVisibility(View.GONE);
533 } else {
534 label.setText(entry.label);
535 label.setVisibility(View.VISIBLE);
536 }
537
538 TextView number = (TextView) convertView.findViewById(R.id.number);
539 number.setText(entry.number);
540 }
Dmitri Plotnikovd0d776d2009-08-19 17:38:04 -0700541
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800542 return convertView;
543 }
544 }
Dmitri Plotnikovd0d776d2009-08-19 17:38:04 -0700545
Flavio Lerda9cafe472011-06-08 14:06:13 +0100546 @Override
547 public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800548 // Handle passing action off to correct handler.
549 if (view.getTag() instanceof ViewEntry) {
550 ViewEntry entry = (ViewEntry) view.getTag();
551 if (entry.intent != null) {
552 startActivity(entry.intent);
Flavio Lerdaa8956882011-07-09 21:34:38 +0100553 } else if (entry.action != null) {
554 entry.action.onClick(view);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800555 }
556 }
Dmitri Plotnikovd0d776d2009-08-19 17:38:04 -0700557 }
Dmitri Plotnikov8e86b752010-02-22 17:47:57 -0800558
559 @Override
560 public void startSearch(String initialQuery, boolean selectInitialQuery, Bundle appSearchData,
561 boolean globalSearch) {
562 if (globalSearch) {
563 super.startSearch(initialQuery, selectInitialQuery, appSearchData, globalSearch);
564 } else {
565 ContactsSearchManager.startSearch(this, initialQuery);
566 }
567 }
Debashish Chatterjee0cb82242011-07-19 19:29:37 +0100568
569 protected void updateVoicemailStatusMessage(Cursor statusCursor) {
570 if (statusCursor == null) {
571 mStatusMessageView.setVisibility(View.GONE);
572 return;
573 }
574 final StatusMessage message = getStatusMessage(statusCursor);
575 if (message == null || !message.showInCallDetails()) {
576 mStatusMessageView.setVisibility(View.GONE);
577 return;
578 }
579
580 mStatusMessageView.setVisibility(View.VISIBLE);
581 mStatusMessageText.setText(message.callDetailsMessageId);
582 if (message.actionMessageId != -1) {
583 mStatusMessageAction.setText(message.actionMessageId);
584 }
585 if (message.actionUri != null) {
586 mStatusMessageAction.setClickable(true);
587 mStatusMessageAction.setOnClickListener(new View.OnClickListener() {
588 @Override
589 public void onClick(View v) {
590 startActivity(new Intent(Intent.ACTION_VIEW, message.actionUri));
591 }
592 });
593 } else {
594 mStatusMessageAction.setClickable(false);
595 }
596 }
597
598 private StatusMessage getStatusMessage(Cursor statusCursor) {
599 List<StatusMessage> messages = mVoicemailStatusHelper.getStatusMessages(statusCursor);
600 Log.d(TAG, "Num status messages: " + messages.size());
601 if (messages.size() == 0) {
602 return null;
603 }
604 // There can only be a single status message per source package, so num of messages can
605 // at most be 1.
606 if (messages.size() > 1) {
607 Log.w(TAG, String.format("Expected 1, found (%d) num of status messages." +
608 " Will use the first one.", messages.size()));
609 }
610 return messages.get(0);
611 }
Flavio Lerda94d7bab2011-07-22 16:52:34 +0100612
613 @Override
614 public boolean onCreateOptionsMenu(Menu menu) {
615 getMenuInflater().inflate(R.menu.call_details_options, menu);
Hugo Hudsonc2f09c32011-07-30 16:31:28 +0100616 return super.onCreateOptionsMenu(menu);
Flavio Lerda94d7bab2011-07-22 16:52:34 +0100617 }
618
619 @Override
620 public boolean onPrepareOptionsMenu(Menu menu) {
621 // This action deletes all elements in the group from the call log.
622 // We don't have this action for voicemails, because you can just use the trash button.
Hugo Hudsonc2f09c32011-07-30 16:31:28 +0100623 menu.findItem(R.id.menu_remove_from_call_log).setVisible(!hasVoicemail());
624 menu.findItem(R.id.menu_edit_number_before_call).setVisible(mHasEditNumberBeforeCall);
625 menu.findItem(R.id.menu_trash).setVisible(hasVoicemail());
626 menu.findItem(R.id.menu_share_voicemail).setVisible(hasVoicemail());
627 return super.onPrepareOptionsMenu(menu);
Flavio Lerda94d7bab2011-07-22 16:52:34 +0100628 }
629
630 @Override
631 public boolean onMenuItemSelected(int featureId, MenuItem item) {
632 switch (item.getItemId()) {
Flavio Lerdad44e83a2011-07-24 23:10:17 +0100633 case android.R.id.home: {
634 onHomeSelected();
635 return true;
636 }
637
Hugo Hudsonc2f09c32011-07-30 16:31:28 +0100638 // All the options menu items are handled by onMenu... methods.
Flavio Lerda94d7bab2011-07-22 16:52:34 +0100639 default:
640 throw new IllegalArgumentException();
641 }
642 }
Flavio Lerdad44e83a2011-07-24 23:10:17 +0100643
Hugo Hudsonc2f09c32011-07-30 16:31:28 +0100644 public void onMenuRemoveFromCallLog(MenuItem menuItem) {
Hugo Hudson5bbe25d2011-08-03 17:16:42 +0100645 final StringBuilder callIds = new StringBuilder();
Hugo Hudsonc2f09c32011-07-30 16:31:28 +0100646 for (Uri callUri : getCallLogEntryUris()) {
647 if (callIds.length() != 0) {
648 callIds.append(",");
649 }
650 callIds.append(ContentUris.parseId(callUri));
651 }
Hugo Hudson5bbe25d2011-08-03 17:16:42 +0100652 runInBackgroundThenFinishActivity(new Runnable() {
653 @Override
654 public void run() {
655 getContentResolver().delete(Calls.CONTENT_URI_WITH_VOICEMAIL,
656 Calls._ID + " IN (" + callIds + ")", null);
657 }
658 });
Hugo Hudsonc2f09c32011-07-30 16:31:28 +0100659 }
Hugo Hudsonc2f09c32011-07-30 16:31:28 +0100660 public void onMenuEditNumberBeforeCall(MenuItem menuItem) {
661 startActivity(new Intent(Intent.ACTION_DIAL, mPhoneNumberHelper.getCallUri(mNumber)));
662 }
663
664 public void onMenuShareVoicemail(MenuItem menuItem) {
665 Log.w(TAG, "onMenuShareVoicemail not yet implemented");
666 }
667
668 public void onMenuTrashVoicemail(MenuItem menuItem) {
Hugo Hudson5bbe25d2011-08-03 17:16:42 +0100669 final Uri voicemailUri = getVoicemailUri();
670 runInBackgroundThenFinishActivity(new Runnable() {
671 @Override
672 public void run() {
673 getContentResolver().delete(voicemailUri, null, null);
674 }
675 });
676 }
677
678 /**
679 * Run a task in the background, and then finish this activity when the task is done.
680 */
681 private void runInBackgroundThenFinishActivity(final Runnable runnable) {
682 new AsyncTask<Void, Void, Void>() {
683 @Override
684 protected Void doInBackground(Void... params) {
685 runnable.run();
686 return null;
687 }
688 @Override
689 protected void onPostExecute(Void result) {
690 finish();
691 }
692 }.execute();
Hugo Hudsonc2f09c32011-07-30 16:31:28 +0100693 }
694
Flavio Lerdad44e83a2011-07-24 23:10:17 +0100695 private void configureActionBar() {
696 ActionBar actionBar = getActionBar();
697 if (actionBar != null) {
Hugo Hudsonc2f09c32011-07-30 16:31:28 +0100698 actionBar.setDisplayOptions(ActionBar.DISPLAY_HOME_AS_UP | ActionBar.DISPLAY_SHOW_HOME);
Flavio Lerdad44e83a2011-07-24 23:10:17 +0100699 }
700 }
701
702 /** Invoked when the user presses the home button in the action bar. */
703 private void onHomeSelected() {
704 Intent intent = new Intent(Intent.ACTION_VIEW, Calls.CONTENT_URI);
705 // This will open the call log even if the detail view has been opened directly.
706 intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
707 startActivity(intent);
708 finish();
709 }
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800710}