blob: 14b38b9703a3afa4ff990a1b9c748e6b4a60d3d3 [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;
Hugo Hudsona2625e42011-08-10 21:13:23 +010040import android.os.AsyncTask;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080041import android.os.Bundle;
42import android.provider.CallLog;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080043import android.provider.CallLog.Calls;
Flavio Lerda9cafe472011-06-08 14:06:13 +010044import android.provider.Contacts.Intents.Insert;
Flavio Lerda8ebfa3d2011-08-10 14:35:22 +010045import android.provider.ContactsContract.CommonDataKinds.Phone;
Dmitri Plotnikovd0d776d2009-08-19 17:38:04 -070046import android.provider.ContactsContract.Contacts;
47import android.provider.ContactsContract.PhoneLookup;
Debashish Chatterjee0a139002011-08-02 18:27:11 +010048import android.provider.VoicemailContract.Voicemails;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080049import android.telephony.PhoneNumberUtils;
50import android.telephony.TelephonyManager;
51import android.text.TextUtils;
Flavio Lerda178eeeb2011-07-11 19:51:40 +010052import android.util.Log;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080053import android.view.KeyEvent;
54import android.view.LayoutInflater;
Flavio Lerda94d7bab2011-07-22 16:52:34 +010055import android.view.Menu;
56import android.view.MenuItem;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080057import android.view.View;
Daniel Lehmannd260de42011-08-01 18:08:38 -070058import android.widget.ImageButton;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080059import android.widget.ImageView;
Flavio Lerda9a208cc2011-07-12 21:05:47 +010060import android.widget.ListView;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080061import android.widget.TextView;
62import android.widget.Toast;
63
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080064import java.util.List;
65
66/**
67 * Displays the details of a specific call log entry.
Flavio Lerda178eeeb2011-07-11 19:51:40 +010068 * <p>
69 * This activity can be either started with the URI of a single call log entry, or with the
70 * {@link #EXTRA_CALL_LOG_IDS} extra to specify a group of call log entries.
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080071 */
Flavio Lerdab184ed62011-08-10 18:17:55 +010072public class CallDetailActivity extends Activity {
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080073 private static final String TAG = "CallDetail";
74
Flavio Lerda178eeeb2011-07-11 19:51:40 +010075 /** A long array extra containing ids of call log entries to display. */
Hugo Hudsonb002f512011-07-15 17:41:12 +010076 public static final String EXTRA_CALL_LOG_IDS = "EXTRA_CALL_LOG_IDS";
77 /** If we are started with a voicemail, we'll find the uri to play with this extra. */
78 public static final String EXTRA_VOICEMAIL_URI = "EXTRA_VOICEMAIL_URI";
79 /** If we should immediately start playback of the voicemail, this extra will be set to true. */
80 public static final String EXTRA_VOICEMAIL_START_PLAYBACK = "EXTRA_VOICEMAIL_START_PLAYBACK";
Flavio Lerda178eeeb2011-07-11 19:51:40 +010081
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 Lerdab88abaa2011-08-02 23:38:36 +010085 private TextView mHeaderTextView;
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;
Hugo Hudson51ada362011-08-05 14:36:14 +010089 private BackgroundTaskService mBackgroundTaskService;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080090
91 private String mNumber = null;
Bai Tao09eb04f2010-09-01 15:34:16 +080092 private String mDefaultCountryIso;
Dmitri Plotnikovd0d776d2009-08-19 17:38:04 -070093
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080094 /* package */ LayoutInflater mInflater;
95 /* package */ Resources mResources;
Flavio Lerda9cafe472011-06-08 14:06:13 +010096 /** Helper to load contact photos. */
97 private ContactPhotoManager mContactPhotoManager;
Debashish Chatterjee0cb82242011-07-19 19:29:37 +010098 /** Helper to make async queries to content resolver. */
99 private CallDetailActivityQueryHandler mAsyncQueryHandler;
100 /** Helper to get voicemail status messages. */
101 private VoicemailStatusHelper mVoicemailStatusHelper;
102 // Views related to voicemail status message.
103 private View mStatusMessageView;
104 private TextView mStatusMessageText;
105 private TextView mStatusMessageAction;
Dmitri Plotnikovd0d776d2009-08-19 17:38:04 -0700106
Flavio Lerda94d7bab2011-07-22 16:52:34 +0100107 /** Whether we should show "edit number before call" in the options menu. */
108 private boolean mHasEditNumberBeforeCall;
109
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800110 static final String[] CALL_LOG_PROJECTION = new String[] {
111 CallLog.Calls.DATE,
112 CallLog.Calls.DURATION,
113 CallLog.Calls.NUMBER,
114 CallLog.Calls.TYPE,
Bai Tao09eb04f2010-09-01 15:34:16 +0800115 CallLog.Calls.COUNTRY_ISO,
Flavio Lerda71fc6ec2011-08-09 17:24:29 +0100116 CallLog.Calls.GEOCODED_LOCATION,
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800117 };
Dmitri Plotnikovd0d776d2009-08-19 17:38:04 -0700118
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800119 static final int DATE_COLUMN_INDEX = 0;
120 static final int DURATION_COLUMN_INDEX = 1;
121 static final int NUMBER_COLUMN_INDEX = 2;
122 static final int CALL_TYPE_COLUMN_INDEX = 3;
Bai Tao09eb04f2010-09-01 15:34:16 +0800123 static final int COUNTRY_ISO_COLUMN_INDEX = 4;
Flavio Lerda71fc6ec2011-08-09 17:24:29 +0100124 static final int GEOCODED_LOCATION_COLUMN_INDEX = 5;
Dmitri Plotnikovd0d776d2009-08-19 17:38:04 -0700125
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800126 static final String[] PHONES_PROJECTION = new String[] {
Dmitri Plotnikovd0d776d2009-08-19 17:38:04 -0700127 PhoneLookup._ID,
128 PhoneLookup.DISPLAY_NAME,
129 PhoneLookup.TYPE,
130 PhoneLookup.LABEL,
131 PhoneLookup.NUMBER,
Bai Tao09eb04f2010-09-01 15:34:16 +0800132 PhoneLookup.NORMALIZED_NUMBER,
Daniel Lehmann362654a2011-07-17 14:16:47 -0700133 PhoneLookup.PHOTO_URI,
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800134 };
135 static final int COLUMN_INDEX_ID = 0;
136 static final int COLUMN_INDEX_NAME = 1;
137 static final int COLUMN_INDEX_TYPE = 2;
138 static final int COLUMN_INDEX_LABEL = 3;
139 static final int COLUMN_INDEX_NUMBER = 4;
Bai Tao09eb04f2010-09-01 15:34:16 +0800140 static final int COLUMN_INDEX_NORMALIZED_NUMBER = 5;
Daniel Lehmann362654a2011-07-17 14:16:47 -0700141 static final int COLUMN_INDEX_PHOTO_URI = 6;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800142
Flavio Lerdab184ed62011-08-10 18:17:55 +0100143 private final View.OnClickListener mPrimaryActionListener = new View.OnClickListener() {
144 @Override
145 public void onClick(View view) {
146 startActivity(((ViewEntry) view.getTag()).primaryIntent);
147 }
148 };
149
150 private final View.OnClickListener mSecondaryActionListener = new View.OnClickListener() {
151 @Override
152 public void onClick(View view) {
153 startActivity(((ViewEntry) view.getTag()).secondaryIntent);
154 }
155 };
156
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800157 @Override
158 protected void onCreate(Bundle icicle) {
159 super.onCreate(icicle);
160
161 setContentView(R.layout.call_detail);
162
Hugo Hudson51ada362011-08-05 14:36:14 +0100163 mBackgroundTaskService = (BackgroundTaskService) getApplicationContext().getSystemService(
164 BackgroundTaskService.BACKGROUND_TASK_SERVICE);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800165 mInflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
166 mResources = getResources();
Dmitri Plotnikovd0d776d2009-08-19 17:38:04 -0700167
Daniel Lehmann6ecb7322011-08-01 21:39:29 -0700168 mCallTypeHelper = new CallTypeHelper(getResources());
Flavio Lerda178eeeb2011-07-11 19:51:40 +0100169 mPhoneNumberHelper = new PhoneNumberHelper(mResources, getVoicemailNumber());
Flavio Lerda40569562011-07-22 21:51:29 +0100170 mPhoneCallDetailsHelper = new PhoneCallDetailsHelper(mResources, mCallTypeHelper,
Flavio Lerda178eeeb2011-07-11 19:51:40 +0100171 mPhoneNumberHelper);
Debashish Chatterjee0cb82242011-07-19 19:29:37 +0100172 mVoicemailStatusHelper = new VoicemailStatusHelperImpl();
173 mAsyncQueryHandler = new CallDetailActivityQueryHandler(this);
Flavio Lerdab88abaa2011-08-02 23:38:36 +0100174 mHeaderTextView = (TextView) findViewById(R.id.header_text);
Debashish Chatterjee0cb82242011-07-19 19:29:37 +0100175 mStatusMessageView = findViewById(R.id.voicemail_status);
176 mStatusMessageText = (TextView) findViewById(R.id.voicemail_status_message);
177 mStatusMessageAction = (TextView) findViewById(R.id.voicemail_status_action);
Flavio Lerdafb63c432011-07-07 17:16:53 +0100178 mMainActionView = (ImageView) findViewById(R.id.main_action);
Daniel Lehmannd260de42011-08-01 18:08:38 -0700179 mMainActionPushLayerView = (ImageButton) findViewById(R.id.main_action_push_layer);
Flavio Lerda9cafe472011-06-08 14:06:13 +0100180 mContactBackgroundView = (ImageView) findViewById(R.id.contact_background);
Bai Tao09eb04f2010-09-01 15:34:16 +0800181 mDefaultCountryIso = ContactsUtils.getCurrentCountryIso(this);
Flavio Lerda9cafe472011-06-08 14:06:13 +0100182 mContactPhotoManager = ContactPhotoManager.getInstance(this);
Flavio Lerdad44e83a2011-07-24 23:10:17 +0100183 configureActionBar();
Hugo Hudson51ada362011-08-05 14:36:14 +0100184 optionallyHandleVoicemail();
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800185 }
Dmitri Plotnikovd0d776d2009-08-19 17:38:04 -0700186
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800187 @Override
188 public void onResume() {
189 super.onResume();
Flavio Lerda178eeeb2011-07-11 19:51:40 +0100190 updateData(getCallLogEntryUris());
Hugo Hudsonb002f512011-07-15 17:41:12 +0100191 }
192
193 /**
194 * Handle voicemail playback or hide voicemail ui.
195 * <p>
196 * If the Intent used to start this Activity contains the suitable extras, then start voicemail
197 * playback. If it doesn't, then hide the voicemail ui.
198 */
Hugo Hudson757aa552011-07-20 01:15:25 +0100199 private void optionallyHandleVoicemail() {
Hugo Hudson157dde12011-07-21 23:28:13 +0100200 if (hasVoicemail()) {
Hugo Hudsona9ad6942011-07-29 17:06:04 +0100201 // Has voicemail: add the voicemail fragment. Add suitable arguments to set the uri
202 // to play and optionally start the playback.
Hugo Hudson757aa552011-07-20 01:15:25 +0100203 // Do a query to fetch the voicemail status messages.
Hugo Hudsona9ad6942011-07-29 17:06:04 +0100204 VoicemailPlaybackFragment playbackFragment = new VoicemailPlaybackFragment();
205 Bundle fragmentArguments = new Bundle();
Hugo Hudson7b02fde2011-08-02 20:56:48 +0100206 fragmentArguments.putParcelable(EXTRA_VOICEMAIL_URI, getVoicemailUri());
Hugo Hudsona9ad6942011-07-29 17:06:04 +0100207 if (getIntent().getBooleanExtra(EXTRA_VOICEMAIL_START_PLAYBACK, false)) {
208 fragmentArguments.putBoolean(EXTRA_VOICEMAIL_START_PLAYBACK, true);
209 }
210 playbackFragment.setArguments(fragmentArguments);
211 getFragmentManager().beginTransaction()
212 .add(R.id.voicemail_container, playbackFragment).commit();
Hugo Hudson7b02fde2011-08-02 20:56:48 +0100213 mAsyncQueryHandler.startVoicemailStatusQuery(getVoicemailUri());
Debashish Chatterjee0a139002011-08-02 18:27:11 +0100214 markVoicemailAsRead(getVoicemailUri());
Hugo Hudson757aa552011-07-20 01:15:25 +0100215 } else {
Hugo Hudsona9ad6942011-07-29 17:06:04 +0100216 // No voicemail uri: hide the status view.
Hugo Hudson757aa552011-07-20 01:15:25 +0100217 mStatusMessageView.setVisibility(View.GONE);
Hugo Hudsonb002f512011-07-15 17:41:12 +0100218 }
Flavio Lerda178eeeb2011-07-11 19:51:40 +0100219 }
220
Hugo Hudson157dde12011-07-21 23:28:13 +0100221 private boolean hasVoicemail() {
Hugo Hudson7b02fde2011-08-02 20:56:48 +0100222 return getVoicemailUri() != null;
223 }
224
225 private Uri getVoicemailUri() {
226 return getIntent().getParcelableExtra(EXTRA_VOICEMAIL_URI);
Hugo Hudson157dde12011-07-21 23:28:13 +0100227 }
228
Debashish Chatterjee0a139002011-08-02 18:27:11 +0100229 private void markVoicemailAsRead(final Uri voicemailUri) {
Hugo Hudson51ada362011-08-05 14:36:14 +0100230 mBackgroundTaskService.submit(new AbstractBackgroundTask() {
Debashish Chatterjee0a139002011-08-02 18:27:11 +0100231 @Override
Hugo Hudson51ada362011-08-05 14:36:14 +0100232 public void doInBackground() {
Debashish Chatterjee0a139002011-08-02 18:27:11 +0100233 ContentValues values = new ContentValues();
234 values.put(Voicemails.IS_READ, true);
235 getContentResolver().update(voicemailUri, values, null, null);
Debashish Chatterjee0a139002011-08-02 18:27:11 +0100236 }
Hugo Hudsona2625e42011-08-10 21:13:23 +0100237 }, AsyncTask.THREAD_POOL_EXECUTOR);
Debashish Chatterjee0a139002011-08-02 18:27:11 +0100238 }
239
Flavio Lerda178eeeb2011-07-11 19:51:40 +0100240 /**
241 * Returns the list of URIs to show.
242 * <p>
243 * There are two ways the URIs can be provided to the activity: as the data on the intent, or as
244 * a list of ids in the call log added as an extra on the URI.
245 * <p>
246 * If both are available, the data on the intent takes precedence.
247 */
248 private Uri[] getCallLogEntryUris() {
249 Uri uri = getIntent().getData();
250 if (uri != null) {
251 // If there is a data on the intent, it takes precedence over the extra.
252 return new Uri[]{ uri };
253 }
254 long[] ids = getIntent().getLongArrayExtra(EXTRA_CALL_LOG_IDS);
255 Uri[] uris = new Uri[ids.length];
256 for (int index = 0; index < ids.length; ++index) {
257 uris[index] = ContentUris.withAppendedId(Calls.CONTENT_URI_WITH_VOICEMAIL, ids[index]);
258 }
259 return uris;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800260 }
261
262 @Override
263 public boolean onKeyDown(int keyCode, KeyEvent event) {
264 switch (keyCode) {
265 case KeyEvent.KEYCODE_CALL: {
266 // Make sure phone isn't already busy before starting direct call
267 TelephonyManager tm = (TelephonyManager)
268 getSystemService(Context.TELEPHONY_SERVICE);
269 if (tm.getCallState() == TelephonyManager.CALL_STATE_IDLE) {
270 Intent callIntent = new Intent(Intent.ACTION_CALL_PRIVILEGED,
271 Uri.fromParts("tel", mNumber, null));
272 startActivity(callIntent);
273 return true;
274 }
275 }
276 }
Dmitri Plotnikovd0d776d2009-08-19 17:38:04 -0700277
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800278 return super.onKeyDown(keyCode, event);
279 }
Dmitri Plotnikovd0d776d2009-08-19 17:38:04 -0700280
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800281 /**
282 * Update user interface with details of given call.
Dmitri Plotnikovd0d776d2009-08-19 17:38:04 -0700283 *
Flavio Lerda178eeeb2011-07-11 19:51:40 +0100284 * @param callUris URIs into {@link CallLog.Calls} of the calls to be displayed
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800285 */
Flavio Lerda178eeeb2011-07-11 19:51:40 +0100286 private void updateData(final Uri... callUris) {
Flavio Lerda12592e82011-08-10 15:36:32 +0100287 mBackgroundTaskService.submit(new BackgroundTask() {
288 private PhoneCallDetails[] details;
Flavio Lerda178eeeb2011-07-11 19:51:40 +0100289
Flavio Lerda12592e82011-08-10 15:36:32 +0100290 @Override
291 public void doInBackground() {
292 // TODO: All phone calls correspond to the same person, so we can make a single
293 // lookup.
294 final int numCalls = callUris.length;
295 details = new PhoneCallDetails[numCalls];
296 try {
297 for (int index = 0; index < numCalls; ++index) {
298 details[index] = getPhoneCallDetailsForUri(callUris[index]);
299 }
300 } catch (IllegalArgumentException e) {
301 // Something went wrong reading in our primary data.
302 Log.w(TAG, "invalid URI starting call details", e);
303 details = null;
Flavio Lerdacfff16d2011-07-12 23:54:40 +0100304 }
Flavio Lerda8ebfa3d2011-08-10 14:35:22 +0100305 }
306
Flavio Lerda12592e82011-08-10 15:36:32 +0100307 @Override
308 public void onPostExecute() {
309 if (details == null) {
310 // Somewhere went wrong: we're going to bail out and show error to users.
311 Toast.makeText(CallDetailActivity.this, R.string.toast_call_detail_error,
312 Toast.LENGTH_SHORT).show();
313 finish();
314 return;
315 }
316
317 // We know that all calls are from the same number and the same contact, so pick the
318 // first.
319 PhoneCallDetails firstDetails = details[0];
320 mNumber = firstDetails.number.toString();
321 final long personId = firstDetails.personId;
322 final Uri photoUri = firstDetails.photoUri;
323
324 // Set the details header, based on the first phone call.
325 mPhoneCallDetailsHelper.setPhoneCallName(mHeaderTextView, firstDetails);
326
327 // Cache the details about the phone number.
328 final Uri numberCallUri = mPhoneNumberHelper.getCallUri(mNumber);
329 final boolean canPlaceCallsTo = mPhoneNumberHelper.canPlaceCallsTo(mNumber);
330 final boolean isVoicemailNumber = mPhoneNumberHelper.isVoicemailNumber(mNumber);
331 final boolean isSipNumber = mPhoneNumberHelper.isSipNumber(mNumber);
332
333 // Let user view contact details if they exist, otherwise add option to create new
334 // contact from this number.
335 final Intent mainActionIntent;
336 final int mainActionIcon;
Flavio Lerdab184ed62011-08-10 18:17:55 +0100337 final String mainActionDescription;
338
339 final CharSequence nameOrNumber;
340 if (!TextUtils.isEmpty(firstDetails.name)) {
341 nameOrNumber = firstDetails.name;
342 } else {
343 nameOrNumber = firstDetails.number;
344 }
Flavio Lerda12592e82011-08-10 15:36:32 +0100345
346 if (firstDetails.personId != -1) {
347 Uri personUri = ContentUris.withAppendedId(Contacts.CONTENT_URI, personId);
348 mainActionIntent = new Intent(Intent.ACTION_VIEW, personUri);
349 mainActionIcon = R.drawable.ic_contacts_holo_dark;
Flavio Lerdab184ed62011-08-10 18:17:55 +0100350 mainActionDescription =
351 getString(R.string.description_view_contact, nameOrNumber);
Flavio Lerda12592e82011-08-10 15:36:32 +0100352 } else if (isVoicemailNumber) {
353 mainActionIntent = null;
354 mainActionIcon = 0;
Flavio Lerdab184ed62011-08-10 18:17:55 +0100355 mainActionDescription = null;
Flavio Lerda12592e82011-08-10 15:36:32 +0100356 } else if (isSipNumber) {
357 // TODO: This item is currently disabled for SIP addresses, because
358 // the Insert.PHONE extra only works correctly for PSTN numbers.
359 //
360 // To fix this for SIP addresses, we need to:
361 // - define ContactsContract.Intents.Insert.SIP_ADDRESS, and use it here if
362 // the current number is a SIP address
363 // - update the contacts UI code to handle Insert.SIP_ADDRESS by
364 // updating the SipAddress field
365 // and then we can remove the "!isSipNumber" check above.
366 mainActionIntent = null;
367 mainActionIcon = 0;
Flavio Lerdab184ed62011-08-10 18:17:55 +0100368 mainActionDescription = null;
Flavio Lerda12592e82011-08-10 15:36:32 +0100369 } else if (canPlaceCallsTo) {
370 mainActionIntent = new Intent(Intent.ACTION_INSERT_OR_EDIT);
371 mainActionIntent.setType(Contacts.CONTENT_ITEM_TYPE);
372 mainActionIntent.putExtra(Insert.PHONE, mNumber);
373 mainActionIcon = R.drawable.ic_add_contact_holo_dark;
Flavio Lerdab184ed62011-08-10 18:17:55 +0100374 mainActionDescription = getString(R.string.description_add_contact);
Flavio Lerda12592e82011-08-10 15:36:32 +0100375 } else {
376 // If we cannot call the number, when we probably cannot add it as a contact either.
377 // This is usually the case of private, unknown, or payphone numbers.
378 mainActionIntent = null;
379 mainActionIcon = 0;
Flavio Lerdab184ed62011-08-10 18:17:55 +0100380 mainActionDescription = null;
Flavio Lerda12592e82011-08-10 15:36:32 +0100381 }
382
383 if (mainActionIntent == null) {
384 mMainActionView.setVisibility(View.INVISIBLE);
385 mMainActionPushLayerView.setVisibility(View.GONE);
386 } else {
387 mMainActionView.setVisibility(View.VISIBLE);
388 mMainActionView.setImageResource(mainActionIcon);
389 mMainActionPushLayerView.setVisibility(View.VISIBLE);
390 mMainActionPushLayerView.setOnClickListener(new View.OnClickListener() {
391 @Override
392 public void onClick(View v) {
393 startActivity(mainActionIntent);
394 }
395 });
Flavio Lerdab184ed62011-08-10 18:17:55 +0100396 mMainActionPushLayerView.setContentDescription(mainActionDescription);
Flavio Lerda12592e82011-08-10 15:36:32 +0100397 }
398
Flavio Lerda12592e82011-08-10 15:36:32 +0100399 // This action allows to call the number that places the call.
400 if (canPlaceCallsTo) {
401 final CharSequence displayNumber =
402 mPhoneNumberHelper.getDisplayNumber(
403 firstDetails.number, firstDetails.formattedNumber);
404
405 ViewEntry entry = new ViewEntry(
406 getString(R.string.menu_callNumber, displayNumber),
Flavio Lerdab184ed62011-08-10 18:17:55 +0100407 new Intent(Intent.ACTION_CALL_PRIVILEGED, numberCallUri),
408 getString(R.string.description_call, nameOrNumber));
Flavio Lerda12592e82011-08-10 15:36:32 +0100409
410 // Only show a label if the number is shown and it is not a SIP address.
411 if (!TextUtils.isEmpty(firstDetails.number)
412 && !PhoneNumberUtils.isUriNumber(firstDetails.number.toString())) {
413 entry.label = Phone.getTypeLabel(mResources, firstDetails.numberType,
414 firstDetails.numberLabel);
415 }
416
417 // The secondary action allows to send an SMS to the number that placed the
418 // call.
419 if (mPhoneNumberHelper.canSendSmsTo(mNumber)) {
Flavio Lerdab184ed62011-08-10 18:17:55 +0100420 entry.setSecondaryAction(
421 R.drawable.ic_text_holo_dark,
Flavio Lerda12592e82011-08-10 15:36:32 +0100422 new Intent(Intent.ACTION_SENDTO,
Flavio Lerdab184ed62011-08-10 18:17:55 +0100423 Uri.fromParts("sms", mNumber, null)),
424 getString(R.string.description_send_text_message, nameOrNumber));
Flavio Lerda12592e82011-08-10 15:36:32 +0100425 }
426
Flavio Lerdab184ed62011-08-10 18:17:55 +0100427 configureCallButton(entry);
428 } else {
429 disableCallButton();
Flavio Lerda12592e82011-08-10 15:36:32 +0100430 }
431
432 mHasEditNumberBeforeCall = canPlaceCallsTo && !isSipNumber && !isVoicemailNumber;
433
Flavio Lerda12592e82011-08-10 15:36:32 +0100434 ListView historyList = (ListView) findViewById(R.id.history);
435 historyList.setAdapter(
436 new CallDetailHistoryAdapter(CallDetailActivity.this, mInflater,
437 mCallTypeHelper, details));
438 loadContactPhotos(photoUri);
Flavio Lerda4e63bab2011-08-10 18:26:46 +0100439 findViewById(R.id.call_detail).setVisibility(View.VISIBLE);
Flavio Lerda8ebfa3d2011-08-10 14:35:22 +0100440 }
Flavio Lerda12592e82011-08-10 15:36:32 +0100441 });
Flavio Lerda178eeeb2011-07-11 19:51:40 +0100442 }
443
444 /** Return the phone call details for a given call log URI. */
445 private PhoneCallDetails getPhoneCallDetailsForUri(Uri callUri) {
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800446 ContentResolver resolver = getContentResolver();
447 Cursor callCursor = resolver.query(callUri, CALL_LOG_PROJECTION, null, null, null);
448 try {
Flavio Lerda178eeeb2011-07-11 19:51:40 +0100449 if (callCursor == null || !callCursor.moveToFirst()) {
450 throw new IllegalArgumentException("Cannot find content: " + callUri);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800451 }
Flavio Lerda178eeeb2011-07-11 19:51:40 +0100452
453 // Read call log specifics.
454 String number = callCursor.getString(NUMBER_COLUMN_INDEX);
455 long date = callCursor.getLong(DATE_COLUMN_INDEX);
456 long duration = callCursor.getLong(DURATION_COLUMN_INDEX);
457 int callType = callCursor.getInt(CALL_TYPE_COLUMN_INDEX);
458 String countryIso = callCursor.getString(COUNTRY_ISO_COLUMN_INDEX);
Flavio Lerda71fc6ec2011-08-09 17:24:29 +0100459 final String geocode = callCursor.getString(GEOCODED_LOCATION_COLUMN_INDEX);
460
Flavio Lerda178eeeb2011-07-11 19:51:40 +0100461 if (TextUtils.isEmpty(countryIso)) {
462 countryIso = mDefaultCountryIso;
463 }
464
465 // Formatted phone number.
466 final CharSequence numberText;
467 // Read contact specifics.
468 CharSequence nameText = "";
469 int numberType = 0;
470 CharSequence numberLabel = "";
471 long personId = -1L;
Daniel Lehmann362654a2011-07-17 14:16:47 -0700472 Uri photoUri = null;
Flavio Lerda178eeeb2011-07-11 19:51:40 +0100473 // If this is not a regular number, there is no point in looking it up in the contacts.
474 if (!mPhoneNumberHelper.canPlaceCallsTo(number)) {
475 numberText = mPhoneNumberHelper.getDisplayNumber(number, null);
476 } else {
477 // Perform a reverse-phonebook lookup to find the contact details.
478 Uri phoneUri = Uri.withAppendedPath(PhoneLookup.CONTENT_FILTER_URI,
479 Uri.encode(number));
480 Cursor phonesCursor = resolver.query(phoneUri, PHONES_PROJECTION, null, null, null);
481 String candidateNumberText = number;
482 try {
483 if (phonesCursor != null && phonesCursor.moveToFirst()) {
484 personId = phonesCursor.getLong(COLUMN_INDEX_ID);
485 nameText = phonesCursor.getString(COLUMN_INDEX_NAME);
Daniel Lehmann362654a2011-07-17 14:16:47 -0700486 String photoUriString = phonesCursor.getString(COLUMN_INDEX_PHOTO_URI);
487 photoUri = photoUriString == null ? null : Uri.parse(photoUriString);
Flavio Lerda178eeeb2011-07-11 19:51:40 +0100488 candidateNumberText = PhoneNumberUtils.formatNumber(
489 phonesCursor.getString(COLUMN_INDEX_NUMBER),
490 phonesCursor.getString(COLUMN_INDEX_NORMALIZED_NUMBER),
491 countryIso);
492 numberType = phonesCursor.getInt(COLUMN_INDEX_TYPE);
493 numberLabel = phonesCursor.getString(COLUMN_INDEX_LABEL);
494 } else {
495 // We could not find this contact in the contacts, just format the phone
496 // number as best as we can. All the other fields will have their default
497 // values.
498 candidateNumberText =
499 PhoneNumberUtils.formatNumber(number, countryIso);
500 }
501 } finally {
502 if (phonesCursor != null) phonesCursor.close();
503 numberText = candidateNumberText;
504 }
505 }
Flavio Lerda71fc6ec2011-08-09 17:24:29 +0100506 return new PhoneCallDetails(number, numberText, countryIso, geocode,
Flavio Lerdacb805e82011-07-18 10:31:44 +0100507 new int[]{ callType }, date, duration,
Daniel Lehmann362654a2011-07-17 14:16:47 -0700508 nameText, numberType, numberLabel, personId, photoUri);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800509 } finally {
510 if (callCursor != null) {
511 callCursor.close();
512 }
513 }
514 }
515
Flavio Lerda9cafe472011-06-08 14:06:13 +0100516 /** Load the contact photos and places them in the corresponding views. */
Daniel Lehmann362654a2011-07-17 14:16:47 -0700517 private void loadContactPhotos(Uri photoUri) {
518 mContactPhotoManager.loadPhoto(mContactBackgroundView, photoUri);
Flavio Lerda9cafe472011-06-08 14:06:13 +0100519 }
520
Flavio Lerda696e8132011-07-05 19:00:23 +0100521 private String getVoicemailNumber() {
522 TelephonyManager telephonyManager =
523 (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
524 return telephonyManager.getVoiceMailNumber();
525 }
526
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800527 static final class ViewEntry {
Flavio Lerdaa8956882011-07-09 21:34:38 +0100528 public final String text;
Flavio Lerda8ebfa3d2011-08-10 14:35:22 +0100529 public final Intent primaryIntent;
Flavio Lerdab184ed62011-08-10 18:17:55 +0100530 /** The description for accessibility of the primary action. */
531 public final String primaryDescription;
Flavio Lerdaa8956882011-07-09 21:34:38 +0100532
Flavio Lerda8ebfa3d2011-08-10 14:35:22 +0100533 public CharSequence label = null;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800534 public String number = null;
Flavio Lerda8ebfa3d2011-08-10 14:35:22 +0100535 /** Icon for the secondary action. */
536 public int secondaryIcon = 0;
537 /** Intent for the secondary action. If not null, an icon must be defined. */
538 public Intent secondaryIntent = null;
Flavio Lerdab184ed62011-08-10 18:17:55 +0100539 /** The description for accessibility of the secondary action. */
540 public String secondaryDescription = null;
Dmitri Plotnikovd0d776d2009-08-19 17:38:04 -0700541
Flavio Lerdab184ed62011-08-10 18:17:55 +0100542 public ViewEntry(String text, Intent intent, String description) {
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800543 this.text = text;
Flavio Lerdab184ed62011-08-10 18:17:55 +0100544 primaryIntent = intent;
545 primaryDescription = description;
Flavio Lerdaa8956882011-07-09 21:34:38 +0100546 }
547
Flavio Lerdab184ed62011-08-10 18:17:55 +0100548 public void setSecondaryAction(int icon, Intent intent, String description) {
Flavio Lerda8ebfa3d2011-08-10 14:35:22 +0100549 secondaryIcon = icon;
550 secondaryIntent = intent;
Flavio Lerdab184ed62011-08-10 18:17:55 +0100551 secondaryDescription = description;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800552 }
553 }
554
Flavio Lerdab184ed62011-08-10 18:17:55 +0100555 /** Disables the call button area, e.g., for private numbers. */
556 private void disableCallButton() {
557 findViewById(R.id.call_and_sms).setVisibility(View.GONE);
558 }
Dmitri Plotnikovd0d776d2009-08-19 17:38:04 -0700559
Flavio Lerdab184ed62011-08-10 18:17:55 +0100560 /** Configures the call button area using the given entry. */
561 private void configureCallButton(ViewEntry entry) {
562 View convertView = findViewById(R.id.call_and_sms);
563 convertView.setVisibility(View.VISIBLE);
Flavio Lerda8ebfa3d2011-08-10 14:35:22 +0100564
Flavio Lerdab184ed62011-08-10 18:17:55 +0100565 ImageView icon = (ImageView) convertView.findViewById(R.id.call_and_sms_icon);
566 View divider = convertView.findViewById(R.id.call_and_sms_divider);
567 TextView text = (TextView) convertView.findViewById(R.id.call_and_sms_text1);
Flavio Lerda8ebfa3d2011-08-10 14:35:22 +0100568
Flavio Lerdab184ed62011-08-10 18:17:55 +0100569 View mainAction = convertView.findViewById(R.id.call_and_sms_main_action);
570 mainAction.setOnClickListener(mPrimaryActionListener);
571 mainAction.setTag(entry);
572 mainAction.setContentDescription(entry.primaryDescription);
573
574 if (entry.secondaryIntent != null) {
575 icon.setOnClickListener(mSecondaryActionListener);
576 icon.setImageResource(entry.secondaryIcon);
577 icon.setVisibility(View.VISIBLE);
578 icon.setTag(entry);
579 icon.setContentDescription(entry.secondaryDescription);
580 divider.setVisibility(View.VISIBLE);
581 } else {
582 icon.setVisibility(View.GONE);
583 divider.setVisibility(View.GONE);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800584 }
Flavio Lerdab184ed62011-08-10 18:17:55 +0100585 text.setText(entry.text);
Dmitri Plotnikovd0d776d2009-08-19 17:38:04 -0700586
Flavio Lerdab184ed62011-08-10 18:17:55 +0100587 View line2 = convertView.findViewById(R.id.call_and_sms_line2);
588 boolean numberEmpty = TextUtils.isEmpty(entry.number);
589 boolean labelEmpty = TextUtils.isEmpty(entry.label) || numberEmpty;
590 if (labelEmpty && numberEmpty) {
591 line2.setVisibility(View.GONE);
592 } else {
593 line2.setVisibility(View.VISIBLE);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800594
Flavio Lerdab184ed62011-08-10 18:17:55 +0100595 TextView label = (TextView) convertView.findViewById(R.id.call_and_sms_label);
596 if (labelEmpty) {
597 label.setVisibility(View.GONE);
Flavio Lerda8ebfa3d2011-08-10 14:35:22 +0100598 } else {
Flavio Lerdab184ed62011-08-10 18:17:55 +0100599 label.setText(entry.label);
600 label.setVisibility(View.VISIBLE);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800601 }
Dmitri Plotnikovd0d776d2009-08-19 17:38:04 -0700602
Flavio Lerdab184ed62011-08-10 18:17:55 +0100603 TextView number = (TextView) convertView.findViewById(R.id.call_and_sms_number);
604 number.setText(entry.number);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800605 }
606 }
Dmitri Plotnikovd0d776d2009-08-19 17:38:04 -0700607
Flavio Lerda9cafe472011-06-08 14:06:13 +0100608 @Override
Dmitri Plotnikov8e86b752010-02-22 17:47:57 -0800609 public void startSearch(String initialQuery, boolean selectInitialQuery, Bundle appSearchData,
610 boolean globalSearch) {
611 if (globalSearch) {
612 super.startSearch(initialQuery, selectInitialQuery, appSearchData, globalSearch);
613 } else {
614 ContactsSearchManager.startSearch(this, initialQuery);
615 }
616 }
Debashish Chatterjee0cb82242011-07-19 19:29:37 +0100617
618 protected void updateVoicemailStatusMessage(Cursor statusCursor) {
619 if (statusCursor == null) {
620 mStatusMessageView.setVisibility(View.GONE);
621 return;
622 }
623 final StatusMessage message = getStatusMessage(statusCursor);
624 if (message == null || !message.showInCallDetails()) {
625 mStatusMessageView.setVisibility(View.GONE);
626 return;
627 }
628
629 mStatusMessageView.setVisibility(View.VISIBLE);
630 mStatusMessageText.setText(message.callDetailsMessageId);
631 if (message.actionMessageId != -1) {
632 mStatusMessageAction.setText(message.actionMessageId);
633 }
634 if (message.actionUri != null) {
635 mStatusMessageAction.setClickable(true);
636 mStatusMessageAction.setOnClickListener(new View.OnClickListener() {
637 @Override
638 public void onClick(View v) {
639 startActivity(new Intent(Intent.ACTION_VIEW, message.actionUri));
640 }
641 });
642 } else {
643 mStatusMessageAction.setClickable(false);
644 }
645 }
646
647 private StatusMessage getStatusMessage(Cursor statusCursor) {
648 List<StatusMessage> messages = mVoicemailStatusHelper.getStatusMessages(statusCursor);
649 Log.d(TAG, "Num status messages: " + messages.size());
650 if (messages.size() == 0) {
651 return null;
652 }
653 // There can only be a single status message per source package, so num of messages can
654 // at most be 1.
655 if (messages.size() > 1) {
656 Log.w(TAG, String.format("Expected 1, found (%d) num of status messages." +
657 " Will use the first one.", messages.size()));
658 }
659 return messages.get(0);
660 }
Flavio Lerda94d7bab2011-07-22 16:52:34 +0100661
662 @Override
663 public boolean onCreateOptionsMenu(Menu menu) {
664 getMenuInflater().inflate(R.menu.call_details_options, menu);
Hugo Hudsonc2f09c32011-07-30 16:31:28 +0100665 return super.onCreateOptionsMenu(menu);
Flavio Lerda94d7bab2011-07-22 16:52:34 +0100666 }
667
668 @Override
669 public boolean onPrepareOptionsMenu(Menu menu) {
670 // This action deletes all elements in the group from the call log.
671 // We don't have this action for voicemails, because you can just use the trash button.
Hugo Hudsonc2f09c32011-07-30 16:31:28 +0100672 menu.findItem(R.id.menu_remove_from_call_log).setVisible(!hasVoicemail());
673 menu.findItem(R.id.menu_edit_number_before_call).setVisible(mHasEditNumberBeforeCall);
674 menu.findItem(R.id.menu_trash).setVisible(hasVoicemail());
Hugo Hudsonc2f09c32011-07-30 16:31:28 +0100675 return super.onPrepareOptionsMenu(menu);
Flavio Lerda94d7bab2011-07-22 16:52:34 +0100676 }
677
678 @Override
679 public boolean onMenuItemSelected(int featureId, MenuItem item) {
680 switch (item.getItemId()) {
Flavio Lerdad44e83a2011-07-24 23:10:17 +0100681 case android.R.id.home: {
682 onHomeSelected();
683 return true;
684 }
685
Hugo Hudsonc2f09c32011-07-30 16:31:28 +0100686 // All the options menu items are handled by onMenu... methods.
Flavio Lerda94d7bab2011-07-22 16:52:34 +0100687 default:
688 throw new IllegalArgumentException();
689 }
690 }
Flavio Lerdad44e83a2011-07-24 23:10:17 +0100691
Hugo Hudsonc2f09c32011-07-30 16:31:28 +0100692 public void onMenuRemoveFromCallLog(MenuItem menuItem) {
Hugo Hudson5bbe25d2011-08-03 17:16:42 +0100693 final StringBuilder callIds = new StringBuilder();
Hugo Hudsonc2f09c32011-07-30 16:31:28 +0100694 for (Uri callUri : getCallLogEntryUris()) {
695 if (callIds.length() != 0) {
696 callIds.append(",");
697 }
698 callIds.append(ContentUris.parseId(callUri));
699 }
Hugo Hudson51ada362011-08-05 14:36:14 +0100700 mBackgroundTaskService.submit(new BackgroundTask() {
Hugo Hudson5bbe25d2011-08-03 17:16:42 +0100701 @Override
Hugo Hudson51ada362011-08-05 14:36:14 +0100702 public void doInBackground() {
Hugo Hudson5bbe25d2011-08-03 17:16:42 +0100703 getContentResolver().delete(Calls.CONTENT_URI_WITH_VOICEMAIL,
704 Calls._ID + " IN (" + callIds + ")", null);
705 }
Hugo Hudson51ada362011-08-05 14:36:14 +0100706 @Override
707 public void onPostExecute() {
708 finish();
709 }
Hugo Hudson5bbe25d2011-08-03 17:16:42 +0100710 });
Hugo Hudsonc2f09c32011-07-30 16:31:28 +0100711 }
Hugo Hudsonc2f09c32011-07-30 16:31:28 +0100712 public void onMenuEditNumberBeforeCall(MenuItem menuItem) {
713 startActivity(new Intent(Intent.ACTION_DIAL, mPhoneNumberHelper.getCallUri(mNumber)));
714 }
715
Hugo Hudsonc2f09c32011-07-30 16:31:28 +0100716 public void onMenuTrashVoicemail(MenuItem menuItem) {
Hugo Hudson5bbe25d2011-08-03 17:16:42 +0100717 final Uri voicemailUri = getVoicemailUri();
Hugo Hudson51ada362011-08-05 14:36:14 +0100718 mBackgroundTaskService.submit(new BackgroundTask() {
Hugo Hudson5bbe25d2011-08-03 17:16:42 +0100719 @Override
Hugo Hudson51ada362011-08-05 14:36:14 +0100720 public void doInBackground() {
Hugo Hudson5bbe25d2011-08-03 17:16:42 +0100721 getContentResolver().delete(voicemailUri, null, null);
722 }
Hugo Hudson5bbe25d2011-08-03 17:16:42 +0100723 @Override
Hugo Hudson51ada362011-08-05 14:36:14 +0100724 public void onPostExecute() {
Hugo Hudson5bbe25d2011-08-03 17:16:42 +0100725 finish();
726 }
Hugo Hudson51ada362011-08-05 14:36:14 +0100727 });
Hugo Hudsonc2f09c32011-07-30 16:31:28 +0100728 }
729
Flavio Lerdad44e83a2011-07-24 23:10:17 +0100730 private void configureActionBar() {
731 ActionBar actionBar = getActionBar();
732 if (actionBar != null) {
Hugo Hudsonc2f09c32011-07-30 16:31:28 +0100733 actionBar.setDisplayOptions(ActionBar.DISPLAY_HOME_AS_UP | ActionBar.DISPLAY_SHOW_HOME);
Flavio Lerdad44e83a2011-07-24 23:10:17 +0100734 }
735 }
736
737 /** Invoked when the user presses the home button in the action bar. */
738 private void onHomeSelected() {
739 Intent intent = new Intent(Intent.ACTION_VIEW, Calls.CONTENT_URI);
740 // This will open the call log even if the detail view has been opened directly.
741 intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
742 startActivity(intent);
743 finish();
744 }
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800745}