blob: 5f682fa787e360d0c6093a2c6def7c085c156995 [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 Hudson51ada362011-08-05 14:36:14 +010022import com.android.contacts.util.AbstractBackgroundTask;
23import com.android.contacts.util.BackgroundTask;
24import com.android.contacts.util.BackgroundTaskService;
Hugo Hudsonb002f512011-07-15 17:41:12 +010025import com.android.contacts.voicemail.VoicemailPlaybackFragment;
Debashish Chatterjee0cb82242011-07-19 19:29:37 +010026import com.android.contacts.voicemail.VoicemailStatusHelper;
27import com.android.contacts.voicemail.VoicemailStatusHelper.StatusMessage;
28import com.android.contacts.voicemail.VoicemailStatusHelperImpl;
Amith Yamasani8bbe2f22009-03-24 21:24:12 -070029
Flavio Lerdad44e83a2011-07-24 23:10:17 +010030import android.app.ActionBar;
Flavio Lerdab184ed62011-08-10 18:17:55 +010031import android.app.Activity;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080032import android.content.ContentResolver;
33import android.content.ContentUris;
Debashish Chatterjee0a139002011-08-02 18:27:11 +010034import android.content.ContentValues;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080035import android.content.Context;
36import android.content.Intent;
37import android.content.res.Resources;
38import android.database.Cursor;
39import android.net.Uri;
40import android.os.Bundle;
41import android.provider.CallLog;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080042import android.provider.CallLog.Calls;
Flavio Lerda9cafe472011-06-08 14:06:13 +010043import android.provider.Contacts.Intents.Insert;
Flavio Lerda8ebfa3d2011-08-10 14:35:22 +010044import android.provider.ContactsContract.CommonDataKinds.Phone;
Dmitri Plotnikovd0d776d2009-08-19 17:38:04 -070045import android.provider.ContactsContract.Contacts;
46import android.provider.ContactsContract.PhoneLookup;
Debashish Chatterjee0a139002011-08-02 18:27:11 +010047import android.provider.VoicemailContract.Voicemails;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080048import android.telephony.PhoneNumberUtils;
49import android.telephony.TelephonyManager;
50import android.text.TextUtils;
Flavio Lerda178eeeb2011-07-11 19:51:40 +010051import android.util.Log;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080052import android.view.KeyEvent;
53import android.view.LayoutInflater;
Flavio Lerda94d7bab2011-07-22 16:52:34 +010054import android.view.Menu;
55import android.view.MenuItem;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080056import android.view.View;
Daniel Lehmannd260de42011-08-01 18:08:38 -070057import android.widget.ImageButton;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080058import android.widget.ImageView;
Flavio Lerda9a208cc2011-07-12 21:05:47 +010059import android.widget.ListView;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080060import android.widget.TextView;
61import android.widget.Toast;
62
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080063import java.util.List;
64
65/**
66 * Displays the details of a specific call log entry.
Flavio Lerda178eeeb2011-07-11 19:51:40 +010067 * <p>
68 * This activity can be either started with the URI of a single call log entry, or with the
69 * {@link #EXTRA_CALL_LOG_IDS} extra to specify a group of call log entries.
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080070 */
Flavio Lerdab184ed62011-08-10 18:17:55 +010071public class CallDetailActivity extends Activity {
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080072 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 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 Lerdab88abaa2011-08-02 23:38:36 +010084 private TextView mHeaderTextView;
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;
Hugo Hudson51ada362011-08-05 14:36:14 +010088 private BackgroundTaskService mBackgroundTaskService;
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,
Flavio Lerda71fc6ec2011-08-09 17:24:29 +0100115 CallLog.Calls.GEOCODED_LOCATION,
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800116 };
Dmitri Plotnikovd0d776d2009-08-19 17:38:04 -0700117
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800118 static final int DATE_COLUMN_INDEX = 0;
119 static final int DURATION_COLUMN_INDEX = 1;
120 static final int NUMBER_COLUMN_INDEX = 2;
121 static final int CALL_TYPE_COLUMN_INDEX = 3;
Bai Tao09eb04f2010-09-01 15:34:16 +0800122 static final int COUNTRY_ISO_COLUMN_INDEX = 4;
Flavio Lerda71fc6ec2011-08-09 17:24:29 +0100123 static final int GEOCODED_LOCATION_COLUMN_INDEX = 5;
Dmitri Plotnikovd0d776d2009-08-19 17:38:04 -0700124
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800125 static final String[] PHONES_PROJECTION = new String[] {
Dmitri Plotnikovd0d776d2009-08-19 17:38:04 -0700126 PhoneLookup._ID,
127 PhoneLookup.DISPLAY_NAME,
128 PhoneLookup.TYPE,
129 PhoneLookup.LABEL,
130 PhoneLookup.NUMBER,
Bai Tao09eb04f2010-09-01 15:34:16 +0800131 PhoneLookup.NORMALIZED_NUMBER,
Daniel Lehmann362654a2011-07-17 14:16:47 -0700132 PhoneLookup.PHOTO_URI,
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800133 };
134 static final int COLUMN_INDEX_ID = 0;
135 static final int COLUMN_INDEX_NAME = 1;
136 static final int COLUMN_INDEX_TYPE = 2;
137 static final int COLUMN_INDEX_LABEL = 3;
138 static final int COLUMN_INDEX_NUMBER = 4;
Bai Tao09eb04f2010-09-01 15:34:16 +0800139 static final int COLUMN_INDEX_NORMALIZED_NUMBER = 5;
Daniel Lehmann362654a2011-07-17 14:16:47 -0700140 static final int COLUMN_INDEX_PHOTO_URI = 6;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800141
Flavio Lerdab184ed62011-08-10 18:17:55 +0100142 private final View.OnClickListener mPrimaryActionListener = new View.OnClickListener() {
143 @Override
144 public void onClick(View view) {
145 startActivity(((ViewEntry) view.getTag()).primaryIntent);
146 }
147 };
148
149 private final View.OnClickListener mSecondaryActionListener = new View.OnClickListener() {
150 @Override
151 public void onClick(View view) {
152 startActivity(((ViewEntry) view.getTag()).secondaryIntent);
153 }
154 };
155
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800156 @Override
157 protected void onCreate(Bundle icicle) {
158 super.onCreate(icicle);
159
160 setContentView(R.layout.call_detail);
161
Hugo Hudson51ada362011-08-05 14:36:14 +0100162 mBackgroundTaskService = (BackgroundTaskService) getApplicationContext().getSystemService(
163 BackgroundTaskService.BACKGROUND_TASK_SERVICE);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800164 mInflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
165 mResources = getResources();
Dmitri Plotnikovd0d776d2009-08-19 17:38:04 -0700166
Daniel Lehmann6ecb7322011-08-01 21:39:29 -0700167 mCallTypeHelper = new CallTypeHelper(getResources());
Flavio Lerda178eeeb2011-07-11 19:51:40 +0100168 mPhoneNumberHelper = new PhoneNumberHelper(mResources, getVoicemailNumber());
Flavio Lerda40569562011-07-22 21:51:29 +0100169 mPhoneCallDetailsHelper = new PhoneCallDetailsHelper(mResources, mCallTypeHelper,
Flavio Lerda178eeeb2011-07-11 19:51:40 +0100170 mPhoneNumberHelper);
Debashish Chatterjee0cb82242011-07-19 19:29:37 +0100171 mVoicemailStatusHelper = new VoicemailStatusHelperImpl();
172 mAsyncQueryHandler = new CallDetailActivityQueryHandler(this);
Flavio Lerdab88abaa2011-08-02 23:38:36 +0100173 mHeaderTextView = (TextView) findViewById(R.id.header_text);
Debashish Chatterjee0cb82242011-07-19 19:29:37 +0100174 mStatusMessageView = findViewById(R.id.voicemail_status);
175 mStatusMessageText = (TextView) findViewById(R.id.voicemail_status_message);
176 mStatusMessageAction = (TextView) findViewById(R.id.voicemail_status_action);
Flavio Lerdafb63c432011-07-07 17:16:53 +0100177 mMainActionView = (ImageView) findViewById(R.id.main_action);
Daniel Lehmannd260de42011-08-01 18:08:38 -0700178 mMainActionPushLayerView = (ImageButton) findViewById(R.id.main_action_push_layer);
Flavio Lerda9cafe472011-06-08 14:06:13 +0100179 mContactBackgroundView = (ImageView) findViewById(R.id.contact_background);
Bai Tao09eb04f2010-09-01 15:34:16 +0800180 mDefaultCountryIso = ContactsUtils.getCurrentCountryIso(this);
Flavio Lerda9cafe472011-06-08 14:06:13 +0100181 mContactPhotoManager = ContactPhotoManager.getInstance(this);
Flavio Lerdad44e83a2011-07-24 23:10:17 +0100182 configureActionBar();
Hugo Hudson51ada362011-08-05 14:36:14 +0100183 optionallyHandleVoicemail();
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800184 }
Dmitri Plotnikovd0d776d2009-08-19 17:38:04 -0700185
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800186 @Override
187 public void onResume() {
188 super.onResume();
Flavio Lerda178eeeb2011-07-11 19:51:40 +0100189 updateData(getCallLogEntryUris());
Hugo Hudsonb002f512011-07-15 17:41:12 +0100190 }
191
192 /**
193 * Handle voicemail playback or hide voicemail ui.
194 * <p>
195 * If the Intent used to start this Activity contains the suitable extras, then start voicemail
196 * playback. If it doesn't, then hide the voicemail ui.
197 */
Hugo Hudson757aa552011-07-20 01:15:25 +0100198 private void optionallyHandleVoicemail() {
Hugo Hudson157dde12011-07-21 23:28:13 +0100199 if (hasVoicemail()) {
Hugo Hudsona9ad6942011-07-29 17:06:04 +0100200 // Has voicemail: add the voicemail fragment. Add suitable arguments to set the uri
201 // to play and optionally start the playback.
Hugo Hudson757aa552011-07-20 01:15:25 +0100202 // Do a query to fetch the voicemail status messages.
Hugo Hudsona9ad6942011-07-29 17:06:04 +0100203 VoicemailPlaybackFragment playbackFragment = new VoicemailPlaybackFragment();
204 Bundle fragmentArguments = new Bundle();
Hugo Hudson7b02fde2011-08-02 20:56:48 +0100205 fragmentArguments.putParcelable(EXTRA_VOICEMAIL_URI, getVoicemailUri());
Hugo Hudsona9ad6942011-07-29 17:06:04 +0100206 if (getIntent().getBooleanExtra(EXTRA_VOICEMAIL_START_PLAYBACK, false)) {
207 fragmentArguments.putBoolean(EXTRA_VOICEMAIL_START_PLAYBACK, true);
208 }
209 playbackFragment.setArguments(fragmentArguments);
210 getFragmentManager().beginTransaction()
211 .add(R.id.voicemail_container, playbackFragment).commit();
Hugo Hudson7b02fde2011-08-02 20:56:48 +0100212 mAsyncQueryHandler.startVoicemailStatusQuery(getVoicemailUri());
Debashish Chatterjee0a139002011-08-02 18:27:11 +0100213 markVoicemailAsRead(getVoicemailUri());
Hugo Hudson757aa552011-07-20 01:15:25 +0100214 } else {
Hugo Hudsona9ad6942011-07-29 17:06:04 +0100215 // No voicemail uri: hide the status view.
Hugo Hudson757aa552011-07-20 01:15:25 +0100216 mStatusMessageView.setVisibility(View.GONE);
Hugo Hudsonb002f512011-07-15 17:41:12 +0100217 }
Flavio Lerda178eeeb2011-07-11 19:51:40 +0100218 }
219
Hugo Hudson157dde12011-07-21 23:28:13 +0100220 private boolean hasVoicemail() {
Hugo Hudson7b02fde2011-08-02 20:56:48 +0100221 return getVoicemailUri() != null;
222 }
223
224 private Uri getVoicemailUri() {
225 return getIntent().getParcelableExtra(EXTRA_VOICEMAIL_URI);
Hugo Hudson157dde12011-07-21 23:28:13 +0100226 }
227
Debashish Chatterjee0a139002011-08-02 18:27:11 +0100228 private void markVoicemailAsRead(final Uri voicemailUri) {
Hugo Hudson51ada362011-08-05 14:36:14 +0100229 mBackgroundTaskService.submit(new AbstractBackgroundTask() {
Debashish Chatterjee0a139002011-08-02 18:27:11 +0100230 @Override
Hugo Hudson51ada362011-08-05 14:36:14 +0100231 public void doInBackground() {
Debashish Chatterjee0a139002011-08-02 18:27:11 +0100232 ContentValues values = new ContentValues();
233 values.put(Voicemails.IS_READ, true);
234 getContentResolver().update(voicemailUri, values, null, null);
Debashish Chatterjee0a139002011-08-02 18:27:11 +0100235 }
Hugo Hudson51ada362011-08-05 14:36:14 +0100236 });
Debashish Chatterjee0a139002011-08-02 18:27:11 +0100237 }
238
Flavio Lerda178eeeb2011-07-11 19:51:40 +0100239 /**
240 * Returns the list of URIs to show.
241 * <p>
242 * There are two ways the URIs can be provided to the activity: as the data on the intent, or as
243 * a list of ids in the call log added as an extra on the URI.
244 * <p>
245 * If both are available, the data on the intent takes precedence.
246 */
247 private Uri[] getCallLogEntryUris() {
248 Uri uri = getIntent().getData();
249 if (uri != null) {
250 // If there is a data on the intent, it takes precedence over the extra.
251 return new Uri[]{ uri };
252 }
253 long[] ids = getIntent().getLongArrayExtra(EXTRA_CALL_LOG_IDS);
254 Uri[] uris = new Uri[ids.length];
255 for (int index = 0; index < ids.length; ++index) {
256 uris[index] = ContentUris.withAppendedId(Calls.CONTENT_URI_WITH_VOICEMAIL, ids[index]);
257 }
258 return uris;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800259 }
260
261 @Override
262 public boolean onKeyDown(int keyCode, KeyEvent event) {
263 switch (keyCode) {
264 case KeyEvent.KEYCODE_CALL: {
265 // Make sure phone isn't already busy before starting direct call
266 TelephonyManager tm = (TelephonyManager)
267 getSystemService(Context.TELEPHONY_SERVICE);
268 if (tm.getCallState() == TelephonyManager.CALL_STATE_IDLE) {
269 Intent callIntent = new Intent(Intent.ACTION_CALL_PRIVILEGED,
270 Uri.fromParts("tel", mNumber, null));
271 startActivity(callIntent);
272 return true;
273 }
274 }
275 }
Dmitri Plotnikovd0d776d2009-08-19 17:38:04 -0700276
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800277 return super.onKeyDown(keyCode, event);
278 }
Dmitri Plotnikovd0d776d2009-08-19 17:38:04 -0700279
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800280 /**
281 * Update user interface with details of given call.
Dmitri Plotnikovd0d776d2009-08-19 17:38:04 -0700282 *
Flavio Lerda178eeeb2011-07-11 19:51:40 +0100283 * @param callUris URIs into {@link CallLog.Calls} of the calls to be displayed
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800284 */
Flavio Lerda178eeeb2011-07-11 19:51:40 +0100285 private void updateData(final Uri... callUris) {
Flavio Lerda12592e82011-08-10 15:36:32 +0100286 mBackgroundTaskService.submit(new BackgroundTask() {
287 private PhoneCallDetails[] details;
Flavio Lerda178eeeb2011-07-11 19:51:40 +0100288
Flavio Lerda12592e82011-08-10 15:36:32 +0100289 @Override
290 public void doInBackground() {
291 // TODO: All phone calls correspond to the same person, so we can make a single
292 // lookup.
293 final int numCalls = callUris.length;
294 details = new PhoneCallDetails[numCalls];
295 try {
296 for (int index = 0; index < numCalls; ++index) {
297 details[index] = getPhoneCallDetailsForUri(callUris[index]);
298 }
299 } catch (IllegalArgumentException e) {
300 // Something went wrong reading in our primary data.
301 Log.w(TAG, "invalid URI starting call details", e);
302 details = null;
Flavio Lerdacfff16d2011-07-12 23:54:40 +0100303 }
Flavio Lerda8ebfa3d2011-08-10 14:35:22 +0100304 }
305
Flavio Lerda12592e82011-08-10 15:36:32 +0100306 @Override
307 public void onPostExecute() {
308 if (details == null) {
309 // Somewhere went wrong: we're going to bail out and show error to users.
310 Toast.makeText(CallDetailActivity.this, R.string.toast_call_detail_error,
311 Toast.LENGTH_SHORT).show();
312 finish();
313 return;
314 }
315
316 // We know that all calls are from the same number and the same contact, so pick the
317 // first.
318 PhoneCallDetails firstDetails = details[0];
319 mNumber = firstDetails.number.toString();
320 final long personId = firstDetails.personId;
321 final Uri photoUri = firstDetails.photoUri;
322
323 // Set the details header, based on the first phone call.
324 mPhoneCallDetailsHelper.setPhoneCallName(mHeaderTextView, firstDetails);
325
326 // Cache the details about the phone number.
327 final Uri numberCallUri = mPhoneNumberHelper.getCallUri(mNumber);
328 final boolean canPlaceCallsTo = mPhoneNumberHelper.canPlaceCallsTo(mNumber);
329 final boolean isVoicemailNumber = mPhoneNumberHelper.isVoicemailNumber(mNumber);
330 final boolean isSipNumber = mPhoneNumberHelper.isSipNumber(mNumber);
331
332 // Let user view contact details if they exist, otherwise add option to create new
333 // contact from this number.
334 final Intent mainActionIntent;
335 final int mainActionIcon;
Flavio Lerdab184ed62011-08-10 18:17:55 +0100336 final String mainActionDescription;
337
338 final CharSequence nameOrNumber;
339 if (!TextUtils.isEmpty(firstDetails.name)) {
340 nameOrNumber = firstDetails.name;
341 } else {
342 nameOrNumber = firstDetails.number;
343 }
Flavio Lerda12592e82011-08-10 15:36:32 +0100344
345 if (firstDetails.personId != -1) {
346 Uri personUri = ContentUris.withAppendedId(Contacts.CONTENT_URI, personId);
347 mainActionIntent = new Intent(Intent.ACTION_VIEW, personUri);
348 mainActionIcon = R.drawable.ic_contacts_holo_dark;
Flavio Lerdab184ed62011-08-10 18:17:55 +0100349 mainActionDescription =
350 getString(R.string.description_view_contact, nameOrNumber);
Flavio Lerda12592e82011-08-10 15:36:32 +0100351 } else if (isVoicemailNumber) {
352 mainActionIntent = null;
353 mainActionIcon = 0;
Flavio Lerdab184ed62011-08-10 18:17:55 +0100354 mainActionDescription = null;
Flavio Lerda12592e82011-08-10 15:36:32 +0100355 } else if (isSipNumber) {
356 // TODO: This item is currently disabled for SIP addresses, because
357 // the Insert.PHONE extra only works correctly for PSTN numbers.
358 //
359 // To fix this for SIP addresses, we need to:
360 // - define ContactsContract.Intents.Insert.SIP_ADDRESS, and use it here if
361 // the current number is a SIP address
362 // - update the contacts UI code to handle Insert.SIP_ADDRESS by
363 // updating the SipAddress field
364 // and then we can remove the "!isSipNumber" check above.
365 mainActionIntent = null;
366 mainActionIcon = 0;
Flavio Lerdab184ed62011-08-10 18:17:55 +0100367 mainActionDescription = null;
Flavio Lerda12592e82011-08-10 15:36:32 +0100368 } else if (canPlaceCallsTo) {
369 mainActionIntent = new Intent(Intent.ACTION_INSERT_OR_EDIT);
370 mainActionIntent.setType(Contacts.CONTENT_ITEM_TYPE);
371 mainActionIntent.putExtra(Insert.PHONE, mNumber);
372 mainActionIcon = R.drawable.ic_add_contact_holo_dark;
Flavio Lerdab184ed62011-08-10 18:17:55 +0100373 mainActionDescription = getString(R.string.description_add_contact);
Flavio Lerda12592e82011-08-10 15:36:32 +0100374 } else {
375 // If we cannot call the number, when we probably cannot add it as a contact either.
376 // This is usually the case of private, unknown, or payphone numbers.
377 mainActionIntent = null;
378 mainActionIcon = 0;
Flavio Lerdab184ed62011-08-10 18:17:55 +0100379 mainActionDescription = null;
Flavio Lerda12592e82011-08-10 15:36:32 +0100380 }
381
382 if (mainActionIntent == null) {
383 mMainActionView.setVisibility(View.INVISIBLE);
384 mMainActionPushLayerView.setVisibility(View.GONE);
385 } else {
386 mMainActionView.setVisibility(View.VISIBLE);
387 mMainActionView.setImageResource(mainActionIcon);
388 mMainActionPushLayerView.setVisibility(View.VISIBLE);
389 mMainActionPushLayerView.setOnClickListener(new View.OnClickListener() {
390 @Override
391 public void onClick(View v) {
392 startActivity(mainActionIntent);
393 }
394 });
Flavio Lerdab184ed62011-08-10 18:17:55 +0100395 mMainActionPushLayerView.setContentDescription(mainActionDescription);
Flavio Lerda12592e82011-08-10 15:36:32 +0100396 }
397
Flavio Lerda12592e82011-08-10 15:36:32 +0100398 // This action allows to call the number that places the call.
399 if (canPlaceCallsTo) {
400 final CharSequence displayNumber =
401 mPhoneNumberHelper.getDisplayNumber(
402 firstDetails.number, firstDetails.formattedNumber);
403
404 ViewEntry entry = new ViewEntry(
405 getString(R.string.menu_callNumber, displayNumber),
Flavio Lerdab184ed62011-08-10 18:17:55 +0100406 new Intent(Intent.ACTION_CALL_PRIVILEGED, numberCallUri),
407 getString(R.string.description_call, nameOrNumber));
Flavio Lerda12592e82011-08-10 15:36:32 +0100408
409 // Only show a label if the number is shown and it is not a SIP address.
410 if (!TextUtils.isEmpty(firstDetails.number)
411 && !PhoneNumberUtils.isUriNumber(firstDetails.number.toString())) {
412 entry.label = Phone.getTypeLabel(mResources, firstDetails.numberType,
413 firstDetails.numberLabel);
414 }
415
416 // The secondary action allows to send an SMS to the number that placed the
417 // call.
418 if (mPhoneNumberHelper.canSendSmsTo(mNumber)) {
Flavio Lerdab184ed62011-08-10 18:17:55 +0100419 entry.setSecondaryAction(
420 R.drawable.ic_text_holo_dark,
Flavio Lerda12592e82011-08-10 15:36:32 +0100421 new Intent(Intent.ACTION_SENDTO,
Flavio Lerdab184ed62011-08-10 18:17:55 +0100422 Uri.fromParts("sms", mNumber, null)),
423 getString(R.string.description_send_text_message, nameOrNumber));
Flavio Lerda12592e82011-08-10 15:36:32 +0100424 }
425
Flavio Lerdab184ed62011-08-10 18:17:55 +0100426 configureCallButton(entry);
427 } else {
428 disableCallButton();
Flavio Lerda12592e82011-08-10 15:36:32 +0100429 }
430
431 mHasEditNumberBeforeCall = canPlaceCallsTo && !isSipNumber && !isVoicemailNumber;
432
Flavio Lerda12592e82011-08-10 15:36:32 +0100433 ListView historyList = (ListView) findViewById(R.id.history);
434 historyList.setAdapter(
435 new CallDetailHistoryAdapter(CallDetailActivity.this, mInflater,
436 mCallTypeHelper, details));
437 loadContactPhotos(photoUri);
Flavio Lerda8ebfa3d2011-08-10 14:35:22 +0100438 }
Flavio Lerda12592e82011-08-10 15:36:32 +0100439 });
Flavio Lerda178eeeb2011-07-11 19:51:40 +0100440 }
441
442 /** Return the phone call details for a given call log URI. */
443 private PhoneCallDetails getPhoneCallDetailsForUri(Uri callUri) {
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800444 ContentResolver resolver = getContentResolver();
445 Cursor callCursor = resolver.query(callUri, CALL_LOG_PROJECTION, null, null, null);
446 try {
Flavio Lerda178eeeb2011-07-11 19:51:40 +0100447 if (callCursor == null || !callCursor.moveToFirst()) {
448 throw new IllegalArgumentException("Cannot find content: " + callUri);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800449 }
Flavio Lerda178eeeb2011-07-11 19:51:40 +0100450
451 // Read call log specifics.
452 String number = callCursor.getString(NUMBER_COLUMN_INDEX);
453 long date = callCursor.getLong(DATE_COLUMN_INDEX);
454 long duration = callCursor.getLong(DURATION_COLUMN_INDEX);
455 int callType = callCursor.getInt(CALL_TYPE_COLUMN_INDEX);
456 String countryIso = callCursor.getString(COUNTRY_ISO_COLUMN_INDEX);
Flavio Lerda71fc6ec2011-08-09 17:24:29 +0100457 final String geocode = callCursor.getString(GEOCODED_LOCATION_COLUMN_INDEX);
458
Flavio Lerda178eeeb2011-07-11 19:51:40 +0100459 if (TextUtils.isEmpty(countryIso)) {
460 countryIso = mDefaultCountryIso;
461 }
462
463 // Formatted phone number.
464 final CharSequence numberText;
465 // Read contact specifics.
466 CharSequence nameText = "";
467 int numberType = 0;
468 CharSequence numberLabel = "";
469 long personId = -1L;
Daniel Lehmann362654a2011-07-17 14:16:47 -0700470 Uri photoUri = null;
Flavio Lerda178eeeb2011-07-11 19:51:40 +0100471 // If this is not a regular number, there is no point in looking it up in the contacts.
472 if (!mPhoneNumberHelper.canPlaceCallsTo(number)) {
473 numberText = mPhoneNumberHelper.getDisplayNumber(number, null);
474 } else {
475 // Perform a reverse-phonebook lookup to find the contact details.
476 Uri phoneUri = Uri.withAppendedPath(PhoneLookup.CONTENT_FILTER_URI,
477 Uri.encode(number));
478 Cursor phonesCursor = resolver.query(phoneUri, PHONES_PROJECTION, null, null, null);
479 String candidateNumberText = number;
480 try {
481 if (phonesCursor != null && phonesCursor.moveToFirst()) {
482 personId = phonesCursor.getLong(COLUMN_INDEX_ID);
483 nameText = phonesCursor.getString(COLUMN_INDEX_NAME);
Daniel Lehmann362654a2011-07-17 14:16:47 -0700484 String photoUriString = phonesCursor.getString(COLUMN_INDEX_PHOTO_URI);
485 photoUri = photoUriString == null ? null : Uri.parse(photoUriString);
Flavio Lerda178eeeb2011-07-11 19:51:40 +0100486 candidateNumberText = PhoneNumberUtils.formatNumber(
487 phonesCursor.getString(COLUMN_INDEX_NUMBER),
488 phonesCursor.getString(COLUMN_INDEX_NORMALIZED_NUMBER),
489 countryIso);
490 numberType = phonesCursor.getInt(COLUMN_INDEX_TYPE);
491 numberLabel = phonesCursor.getString(COLUMN_INDEX_LABEL);
492 } else {
493 // We could not find this contact in the contacts, just format the phone
494 // number as best as we can. All the other fields will have their default
495 // values.
496 candidateNumberText =
497 PhoneNumberUtils.formatNumber(number, countryIso);
498 }
499 } finally {
500 if (phonesCursor != null) phonesCursor.close();
501 numberText = candidateNumberText;
502 }
503 }
Flavio Lerda71fc6ec2011-08-09 17:24:29 +0100504 return new PhoneCallDetails(number, numberText, countryIso, geocode,
Flavio Lerdacb805e82011-07-18 10:31:44 +0100505 new int[]{ callType }, date, duration,
Daniel Lehmann362654a2011-07-17 14:16:47 -0700506 nameText, numberType, numberLabel, personId, photoUri);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800507 } finally {
508 if (callCursor != null) {
509 callCursor.close();
510 }
511 }
512 }
513
Flavio Lerda9cafe472011-06-08 14:06:13 +0100514 /** Load the contact photos and places them in the corresponding views. */
Daniel Lehmann362654a2011-07-17 14:16:47 -0700515 private void loadContactPhotos(Uri photoUri) {
516 mContactPhotoManager.loadPhoto(mContactBackgroundView, photoUri);
Flavio Lerda9cafe472011-06-08 14:06:13 +0100517 }
518
Flavio Lerda696e8132011-07-05 19:00:23 +0100519 private String getVoicemailNumber() {
520 TelephonyManager telephonyManager =
521 (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
522 return telephonyManager.getVoiceMailNumber();
523 }
524
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800525 static final class ViewEntry {
Flavio Lerdaa8956882011-07-09 21:34:38 +0100526 public final String text;
Flavio Lerda8ebfa3d2011-08-10 14:35:22 +0100527 public final Intent primaryIntent;
Flavio Lerdab184ed62011-08-10 18:17:55 +0100528 /** The description for accessibility of the primary action. */
529 public final String primaryDescription;
Flavio Lerdaa8956882011-07-09 21:34:38 +0100530
Flavio Lerda8ebfa3d2011-08-10 14:35:22 +0100531 public CharSequence label = null;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800532 public String number = null;
Flavio Lerda8ebfa3d2011-08-10 14:35:22 +0100533 /** Icon for the secondary action. */
534 public int secondaryIcon = 0;
535 /** Intent for the secondary action. If not null, an icon must be defined. */
536 public Intent secondaryIntent = null;
Flavio Lerdab184ed62011-08-10 18:17:55 +0100537 /** The description for accessibility of the secondary action. */
538 public String secondaryDescription = null;
Dmitri Plotnikovd0d776d2009-08-19 17:38:04 -0700539
Flavio Lerdab184ed62011-08-10 18:17:55 +0100540 public ViewEntry(String text, Intent intent, String description) {
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800541 this.text = text;
Flavio Lerdab184ed62011-08-10 18:17:55 +0100542 primaryIntent = intent;
543 primaryDescription = description;
Flavio Lerdaa8956882011-07-09 21:34:38 +0100544 }
545
Flavio Lerdab184ed62011-08-10 18:17:55 +0100546 public void setSecondaryAction(int icon, Intent intent, String description) {
Flavio Lerda8ebfa3d2011-08-10 14:35:22 +0100547 secondaryIcon = icon;
548 secondaryIntent = intent;
Flavio Lerdab184ed62011-08-10 18:17:55 +0100549 secondaryDescription = description;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800550 }
551 }
552
Flavio Lerdab184ed62011-08-10 18:17:55 +0100553 /** Disables the call button area, e.g., for private numbers. */
554 private void disableCallButton() {
555 findViewById(R.id.call_and_sms).setVisibility(View.GONE);
556 }
Dmitri Plotnikovd0d776d2009-08-19 17:38:04 -0700557
Flavio Lerdab184ed62011-08-10 18:17:55 +0100558 /** Configures the call button area using the given entry. */
559 private void configureCallButton(ViewEntry entry) {
560 View convertView = findViewById(R.id.call_and_sms);
561 convertView.setVisibility(View.VISIBLE);
Flavio Lerda8ebfa3d2011-08-10 14:35:22 +0100562
Flavio Lerdab184ed62011-08-10 18:17:55 +0100563 ImageView icon = (ImageView) convertView.findViewById(R.id.call_and_sms_icon);
564 View divider = convertView.findViewById(R.id.call_and_sms_divider);
565 TextView text = (TextView) convertView.findViewById(R.id.call_and_sms_text1);
Flavio Lerda8ebfa3d2011-08-10 14:35:22 +0100566
Flavio Lerdab184ed62011-08-10 18:17:55 +0100567 View mainAction = convertView.findViewById(R.id.call_and_sms_main_action);
568 mainAction.setOnClickListener(mPrimaryActionListener);
569 mainAction.setTag(entry);
570 mainAction.setContentDescription(entry.primaryDescription);
571
572 if (entry.secondaryIntent != null) {
573 icon.setOnClickListener(mSecondaryActionListener);
574 icon.setImageResource(entry.secondaryIcon);
575 icon.setVisibility(View.VISIBLE);
576 icon.setTag(entry);
577 icon.setContentDescription(entry.secondaryDescription);
578 divider.setVisibility(View.VISIBLE);
579 } else {
580 icon.setVisibility(View.GONE);
581 divider.setVisibility(View.GONE);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800582 }
Flavio Lerdab184ed62011-08-10 18:17:55 +0100583 text.setText(entry.text);
Dmitri Plotnikovd0d776d2009-08-19 17:38:04 -0700584
Flavio Lerdab184ed62011-08-10 18:17:55 +0100585 View line2 = convertView.findViewById(R.id.call_and_sms_line2);
586 boolean numberEmpty = TextUtils.isEmpty(entry.number);
587 boolean labelEmpty = TextUtils.isEmpty(entry.label) || numberEmpty;
588 if (labelEmpty && numberEmpty) {
589 line2.setVisibility(View.GONE);
590 } else {
591 line2.setVisibility(View.VISIBLE);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800592
Flavio Lerdab184ed62011-08-10 18:17:55 +0100593 TextView label = (TextView) convertView.findViewById(R.id.call_and_sms_label);
594 if (labelEmpty) {
595 label.setVisibility(View.GONE);
Flavio Lerda8ebfa3d2011-08-10 14:35:22 +0100596 } else {
Flavio Lerdab184ed62011-08-10 18:17:55 +0100597 label.setText(entry.label);
598 label.setVisibility(View.VISIBLE);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800599 }
Dmitri Plotnikovd0d776d2009-08-19 17:38:04 -0700600
Flavio Lerdab184ed62011-08-10 18:17:55 +0100601 TextView number = (TextView) convertView.findViewById(R.id.call_and_sms_number);
602 number.setText(entry.number);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800603 }
604 }
Dmitri Plotnikovd0d776d2009-08-19 17:38:04 -0700605
Flavio Lerda9cafe472011-06-08 14:06:13 +0100606 @Override
Dmitri Plotnikov8e86b752010-02-22 17:47:57 -0800607 public void startSearch(String initialQuery, boolean selectInitialQuery, Bundle appSearchData,
608 boolean globalSearch) {
609 if (globalSearch) {
610 super.startSearch(initialQuery, selectInitialQuery, appSearchData, globalSearch);
611 } else {
612 ContactsSearchManager.startSearch(this, initialQuery);
613 }
614 }
Debashish Chatterjee0cb82242011-07-19 19:29:37 +0100615
616 protected void updateVoicemailStatusMessage(Cursor statusCursor) {
617 if (statusCursor == null) {
618 mStatusMessageView.setVisibility(View.GONE);
619 return;
620 }
621 final StatusMessage message = getStatusMessage(statusCursor);
622 if (message == null || !message.showInCallDetails()) {
623 mStatusMessageView.setVisibility(View.GONE);
624 return;
625 }
626
627 mStatusMessageView.setVisibility(View.VISIBLE);
628 mStatusMessageText.setText(message.callDetailsMessageId);
629 if (message.actionMessageId != -1) {
630 mStatusMessageAction.setText(message.actionMessageId);
631 }
632 if (message.actionUri != null) {
633 mStatusMessageAction.setClickable(true);
634 mStatusMessageAction.setOnClickListener(new View.OnClickListener() {
635 @Override
636 public void onClick(View v) {
637 startActivity(new Intent(Intent.ACTION_VIEW, message.actionUri));
638 }
639 });
640 } else {
641 mStatusMessageAction.setClickable(false);
642 }
643 }
644
645 private StatusMessage getStatusMessage(Cursor statusCursor) {
646 List<StatusMessage> messages = mVoicemailStatusHelper.getStatusMessages(statusCursor);
647 Log.d(TAG, "Num status messages: " + messages.size());
648 if (messages.size() == 0) {
649 return null;
650 }
651 // There can only be a single status message per source package, so num of messages can
652 // at most be 1.
653 if (messages.size() > 1) {
654 Log.w(TAG, String.format("Expected 1, found (%d) num of status messages." +
655 " Will use the first one.", messages.size()));
656 }
657 return messages.get(0);
658 }
Flavio Lerda94d7bab2011-07-22 16:52:34 +0100659
660 @Override
661 public boolean onCreateOptionsMenu(Menu menu) {
662 getMenuInflater().inflate(R.menu.call_details_options, menu);
Hugo Hudsonc2f09c32011-07-30 16:31:28 +0100663 return super.onCreateOptionsMenu(menu);
Flavio Lerda94d7bab2011-07-22 16:52:34 +0100664 }
665
666 @Override
667 public boolean onPrepareOptionsMenu(Menu menu) {
668 // This action deletes all elements in the group from the call log.
669 // We don't have this action for voicemails, because you can just use the trash button.
Hugo Hudsonc2f09c32011-07-30 16:31:28 +0100670 menu.findItem(R.id.menu_remove_from_call_log).setVisible(!hasVoicemail());
671 menu.findItem(R.id.menu_edit_number_before_call).setVisible(mHasEditNumberBeforeCall);
672 menu.findItem(R.id.menu_trash).setVisible(hasVoicemail());
Hugo Hudsonc2f09c32011-07-30 16:31:28 +0100673 return super.onPrepareOptionsMenu(menu);
Flavio Lerda94d7bab2011-07-22 16:52:34 +0100674 }
675
676 @Override
677 public boolean onMenuItemSelected(int featureId, MenuItem item) {
678 switch (item.getItemId()) {
Flavio Lerdad44e83a2011-07-24 23:10:17 +0100679 case android.R.id.home: {
680 onHomeSelected();
681 return true;
682 }
683
Hugo Hudsonc2f09c32011-07-30 16:31:28 +0100684 // All the options menu items are handled by onMenu... methods.
Flavio Lerda94d7bab2011-07-22 16:52:34 +0100685 default:
686 throw new IllegalArgumentException();
687 }
688 }
Flavio Lerdad44e83a2011-07-24 23:10:17 +0100689
Hugo Hudsonc2f09c32011-07-30 16:31:28 +0100690 public void onMenuRemoveFromCallLog(MenuItem menuItem) {
Hugo Hudson5bbe25d2011-08-03 17:16:42 +0100691 final StringBuilder callIds = new StringBuilder();
Hugo Hudsonc2f09c32011-07-30 16:31:28 +0100692 for (Uri callUri : getCallLogEntryUris()) {
693 if (callIds.length() != 0) {
694 callIds.append(",");
695 }
696 callIds.append(ContentUris.parseId(callUri));
697 }
Hugo Hudson51ada362011-08-05 14:36:14 +0100698 mBackgroundTaskService.submit(new BackgroundTask() {
Hugo Hudson5bbe25d2011-08-03 17:16:42 +0100699 @Override
Hugo Hudson51ada362011-08-05 14:36:14 +0100700 public void doInBackground() {
Hugo Hudson5bbe25d2011-08-03 17:16:42 +0100701 getContentResolver().delete(Calls.CONTENT_URI_WITH_VOICEMAIL,
702 Calls._ID + " IN (" + callIds + ")", null);
703 }
Hugo Hudson51ada362011-08-05 14:36:14 +0100704 @Override
705 public void onPostExecute() {
706 finish();
707 }
Hugo Hudson5bbe25d2011-08-03 17:16:42 +0100708 });
Hugo Hudsonc2f09c32011-07-30 16:31:28 +0100709 }
Hugo Hudsonc2f09c32011-07-30 16:31:28 +0100710 public void onMenuEditNumberBeforeCall(MenuItem menuItem) {
711 startActivity(new Intent(Intent.ACTION_DIAL, mPhoneNumberHelper.getCallUri(mNumber)));
712 }
713
Hugo Hudsonc2f09c32011-07-30 16:31:28 +0100714 public void onMenuTrashVoicemail(MenuItem menuItem) {
Hugo Hudson5bbe25d2011-08-03 17:16:42 +0100715 final Uri voicemailUri = getVoicemailUri();
Hugo Hudson51ada362011-08-05 14:36:14 +0100716 mBackgroundTaskService.submit(new BackgroundTask() {
Hugo Hudson5bbe25d2011-08-03 17:16:42 +0100717 @Override
Hugo Hudson51ada362011-08-05 14:36:14 +0100718 public void doInBackground() {
Hugo Hudson5bbe25d2011-08-03 17:16:42 +0100719 getContentResolver().delete(voicemailUri, null, null);
720 }
Hugo Hudson5bbe25d2011-08-03 17:16:42 +0100721 @Override
Hugo Hudson51ada362011-08-05 14:36:14 +0100722 public void onPostExecute() {
Hugo Hudson5bbe25d2011-08-03 17:16:42 +0100723 finish();
724 }
Hugo Hudson51ada362011-08-05 14:36:14 +0100725 });
Hugo Hudsonc2f09c32011-07-30 16:31:28 +0100726 }
727
Flavio Lerdad44e83a2011-07-24 23:10:17 +0100728 private void configureActionBar() {
729 ActionBar actionBar = getActionBar();
730 if (actionBar != null) {
Hugo Hudsonc2f09c32011-07-30 16:31:28 +0100731 actionBar.setDisplayOptions(ActionBar.DISPLAY_HOME_AS_UP | ActionBar.DISPLAY_SHOW_HOME);
Flavio Lerdad44e83a2011-07-24 23:10:17 +0100732 }
733 }
734
735 /** Invoked when the user presses the home button in the action bar. */
736 private void onHomeSelected() {
737 Intent intent = new Intent(Intent.ACTION_VIEW, Calls.CONTENT_URI);
738 // This will open the call log even if the detail view has been opened directly.
739 intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
740 startActivity(intent);
741 finish();
742 }
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800743}