blob: 43e6fc78b51097f50873595c38d5dce47055cf48 [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;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080031import android.app.ListActivity;
32import 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;
Dmitri Plotnikovd0d776d2009-08-19 17:38:04 -070044import android.provider.ContactsContract.Contacts;
45import android.provider.ContactsContract.PhoneLookup;
Debashish Chatterjee0a139002011-08-02 18:27:11 +010046import android.provider.VoicemailContract.Voicemails;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080047import android.telephony.PhoneNumberUtils;
48import android.telephony.TelephonyManager;
49import android.text.TextUtils;
Flavio Lerda178eeeb2011-07-11 19:51:40 +010050import android.util.Log;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080051import android.view.KeyEvent;
52import android.view.LayoutInflater;
Flavio Lerda94d7bab2011-07-22 16:52:34 +010053import android.view.Menu;
54import android.view.MenuItem;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080055import android.view.View;
56import android.view.ViewGroup;
57import android.widget.AdapterView;
58import android.widget.BaseAdapter;
Daniel Lehmannd260de42011-08-01 18:08:38 -070059import android.widget.ImageButton;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080060import android.widget.ImageView;
Flavio Lerda9a208cc2011-07-12 21:05:47 +010061import android.widget.ListView;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080062import android.widget.TextView;
63import android.widget.Toast;
64
65import java.util.ArrayList;
66import java.util.List;
67
68/**
69 * Displays the details of a specific call log entry.
Flavio Lerda178eeeb2011-07-11 19:51:40 +010070 * <p>
71 * This activity can be either started with the URI of a single call log entry, or with the
72 * {@link #EXTRA_CALL_LOG_IDS} extra to specify a group of call log entries.
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080073 */
74public class CallDetailActivity extends ListActivity implements
75 AdapterView.OnItemClickListener {
76 private static final String TAG = "CallDetail";
77
Flavio Lerda178eeeb2011-07-11 19:51:40 +010078 /** A long array extra containing ids of call log entries to display. */
Hugo Hudsonb002f512011-07-15 17:41:12 +010079 public static final String EXTRA_CALL_LOG_IDS = "EXTRA_CALL_LOG_IDS";
80 /** If we are started with a voicemail, we'll find the uri to play with this extra. */
81 public static final String EXTRA_VOICEMAIL_URI = "EXTRA_VOICEMAIL_URI";
82 /** If we should immediately start playback of the voicemail, this extra will be set to true. */
83 public static final String EXTRA_VOICEMAIL_START_PLAYBACK = "EXTRA_VOICEMAIL_START_PLAYBACK";
Flavio Lerda178eeeb2011-07-11 19:51:40 +010084
Flavio Lerda9a208cc2011-07-12 21:05:47 +010085 private CallTypeHelper mCallTypeHelper;
Flavio Lerda178eeeb2011-07-11 19:51:40 +010086 private PhoneNumberHelper mPhoneNumberHelper;
Flavio Lerdaafb93bb2011-07-05 14:46:10 +010087 private PhoneCallDetailsHelper mPhoneCallDetailsHelper;
Flavio Lerdab88abaa2011-08-02 23:38:36 +010088 private TextView mHeaderTextView;
Flavio Lerdafb63c432011-07-07 17:16:53 +010089 private ImageView mMainActionView;
Daniel Lehmannd260de42011-08-01 18:08:38 -070090 private ImageButton mMainActionPushLayerView;
Flavio Lerda9cafe472011-06-08 14:06:13 +010091 private ImageView mContactBackgroundView;
Hugo Hudson51ada362011-08-05 14:36:14 +010092 private BackgroundTaskService mBackgroundTaskService;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080093
94 private String mNumber = null;
Bai Tao09eb04f2010-09-01 15:34:16 +080095 private String mDefaultCountryIso;
Dmitri Plotnikovd0d776d2009-08-19 17:38:04 -070096
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080097 /* package */ LayoutInflater mInflater;
98 /* package */ Resources mResources;
Flavio Lerda9cafe472011-06-08 14:06:13 +010099 /** Helper to load contact photos. */
100 private ContactPhotoManager mContactPhotoManager;
Debashish Chatterjee0cb82242011-07-19 19:29:37 +0100101 /** Helper to make async queries to content resolver. */
102 private CallDetailActivityQueryHandler mAsyncQueryHandler;
103 /** Helper to get voicemail status messages. */
104 private VoicemailStatusHelper mVoicemailStatusHelper;
105 // Views related to voicemail status message.
106 private View mStatusMessageView;
107 private TextView mStatusMessageText;
108 private TextView mStatusMessageAction;
Dmitri Plotnikovd0d776d2009-08-19 17:38:04 -0700109
Flavio Lerda94d7bab2011-07-22 16:52:34 +0100110 /** Whether we should show "edit number before call" in the options menu. */
111 private boolean mHasEditNumberBeforeCall;
112
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800113 static final String[] CALL_LOG_PROJECTION = new String[] {
114 CallLog.Calls.DATE,
115 CallLog.Calls.DURATION,
116 CallLog.Calls.NUMBER,
117 CallLog.Calls.TYPE,
Bai Tao09eb04f2010-09-01 15:34:16 +0800118 CallLog.Calls.COUNTRY_ISO,
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800119 };
Dmitri Plotnikovd0d776d2009-08-19 17:38:04 -0700120
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800121 static final int DATE_COLUMN_INDEX = 0;
122 static final int DURATION_COLUMN_INDEX = 1;
123 static final int NUMBER_COLUMN_INDEX = 2;
124 static final int CALL_TYPE_COLUMN_INDEX = 3;
Bai Tao09eb04f2010-09-01 15:34:16 +0800125 static final int COUNTRY_ISO_COLUMN_INDEX = 4;
Dmitri Plotnikovd0d776d2009-08-19 17:38:04 -0700126
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800127 static final String[] PHONES_PROJECTION = new String[] {
Dmitri Plotnikovd0d776d2009-08-19 17:38:04 -0700128 PhoneLookup._ID,
129 PhoneLookup.DISPLAY_NAME,
130 PhoneLookup.TYPE,
131 PhoneLookup.LABEL,
132 PhoneLookup.NUMBER,
Bai Tao09eb04f2010-09-01 15:34:16 +0800133 PhoneLookup.NORMALIZED_NUMBER,
Daniel Lehmann362654a2011-07-17 14:16:47 -0700134 PhoneLookup.PHOTO_URI,
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800135 };
136 static final int COLUMN_INDEX_ID = 0;
137 static final int COLUMN_INDEX_NAME = 1;
138 static final int COLUMN_INDEX_TYPE = 2;
139 static final int COLUMN_INDEX_LABEL = 3;
140 static final int COLUMN_INDEX_NUMBER = 4;
Bai Tao09eb04f2010-09-01 15:34:16 +0800141 static final int COLUMN_INDEX_NORMALIZED_NUMBER = 5;
Daniel Lehmann362654a2011-07-17 14:16:47 -0700142 static final int COLUMN_INDEX_PHOTO_URI = 6;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800143
144 @Override
145 protected void onCreate(Bundle icicle) {
146 super.onCreate(icicle);
147
148 setContentView(R.layout.call_detail);
149
Hugo Hudson51ada362011-08-05 14:36:14 +0100150 mBackgroundTaskService = (BackgroundTaskService) getApplicationContext().getSystemService(
151 BackgroundTaskService.BACKGROUND_TASK_SERVICE);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800152 mInflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
153 mResources = getResources();
Dmitri Plotnikovd0d776d2009-08-19 17:38:04 -0700154
Daniel Lehmann6ecb7322011-08-01 21:39:29 -0700155 mCallTypeHelper = new CallTypeHelper(getResources());
Flavio Lerda178eeeb2011-07-11 19:51:40 +0100156 mPhoneNumberHelper = new PhoneNumberHelper(mResources, getVoicemailNumber());
Flavio Lerda40569562011-07-22 21:51:29 +0100157 mPhoneCallDetailsHelper = new PhoneCallDetailsHelper(mResources, mCallTypeHelper,
Flavio Lerda178eeeb2011-07-11 19:51:40 +0100158 mPhoneNumberHelper);
Debashish Chatterjee0cb82242011-07-19 19:29:37 +0100159 mVoicemailStatusHelper = new VoicemailStatusHelperImpl();
160 mAsyncQueryHandler = new CallDetailActivityQueryHandler(this);
Flavio Lerdab88abaa2011-08-02 23:38:36 +0100161 mHeaderTextView = (TextView) findViewById(R.id.header_text);
Debashish Chatterjee0cb82242011-07-19 19:29:37 +0100162 mStatusMessageView = findViewById(R.id.voicemail_status);
163 mStatusMessageText = (TextView) findViewById(R.id.voicemail_status_message);
164 mStatusMessageAction = (TextView) findViewById(R.id.voicemail_status_action);
Flavio Lerdafb63c432011-07-07 17:16:53 +0100165 mMainActionView = (ImageView) findViewById(R.id.main_action);
Daniel Lehmannd260de42011-08-01 18:08:38 -0700166 mMainActionPushLayerView = (ImageButton) findViewById(R.id.main_action_push_layer);
Flavio Lerda9cafe472011-06-08 14:06:13 +0100167 mContactBackgroundView = (ImageView) findViewById(R.id.contact_background);
Bai Tao09eb04f2010-09-01 15:34:16 +0800168 mDefaultCountryIso = ContactsUtils.getCurrentCountryIso(this);
Flavio Lerda9cafe472011-06-08 14:06:13 +0100169 mContactPhotoManager = ContactPhotoManager.getInstance(this);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800170 getListView().setOnItemClickListener(this);
Flavio Lerdad44e83a2011-07-24 23:10:17 +0100171 configureActionBar();
Hugo Hudson51ada362011-08-05 14:36:14 +0100172 optionallyHandleVoicemail();
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800173 }
Dmitri Plotnikovd0d776d2009-08-19 17:38:04 -0700174
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800175 @Override
176 public void onResume() {
177 super.onResume();
Flavio Lerda178eeeb2011-07-11 19:51:40 +0100178 updateData(getCallLogEntryUris());
Hugo Hudsonb002f512011-07-15 17:41:12 +0100179 }
180
181 /**
182 * Handle voicemail playback or hide voicemail ui.
183 * <p>
184 * If the Intent used to start this Activity contains the suitable extras, then start voicemail
185 * playback. If it doesn't, then hide the voicemail ui.
186 */
Hugo Hudson757aa552011-07-20 01:15:25 +0100187 private void optionallyHandleVoicemail() {
Hugo Hudson157dde12011-07-21 23:28:13 +0100188 if (hasVoicemail()) {
Hugo Hudsona9ad6942011-07-29 17:06:04 +0100189 // Has voicemail: add the voicemail fragment. Add suitable arguments to set the uri
190 // to play and optionally start the playback.
Hugo Hudson757aa552011-07-20 01:15:25 +0100191 // Do a query to fetch the voicemail status messages.
Hugo Hudsona9ad6942011-07-29 17:06:04 +0100192 VoicemailPlaybackFragment playbackFragment = new VoicemailPlaybackFragment();
193 Bundle fragmentArguments = new Bundle();
Hugo Hudson7b02fde2011-08-02 20:56:48 +0100194 fragmentArguments.putParcelable(EXTRA_VOICEMAIL_URI, getVoicemailUri());
Hugo Hudsona9ad6942011-07-29 17:06:04 +0100195 if (getIntent().getBooleanExtra(EXTRA_VOICEMAIL_START_PLAYBACK, false)) {
196 fragmentArguments.putBoolean(EXTRA_VOICEMAIL_START_PLAYBACK, true);
197 }
198 playbackFragment.setArguments(fragmentArguments);
199 getFragmentManager().beginTransaction()
200 .add(R.id.voicemail_container, playbackFragment).commit();
Hugo Hudson7b02fde2011-08-02 20:56:48 +0100201 mAsyncQueryHandler.startVoicemailStatusQuery(getVoicemailUri());
Debashish Chatterjee0a139002011-08-02 18:27:11 +0100202 markVoicemailAsRead(getVoicemailUri());
Hugo Hudson757aa552011-07-20 01:15:25 +0100203 } else {
Hugo Hudsona9ad6942011-07-29 17:06:04 +0100204 // No voicemail uri: hide the status view.
Hugo Hudson757aa552011-07-20 01:15:25 +0100205 mStatusMessageView.setVisibility(View.GONE);
Hugo Hudsonb002f512011-07-15 17:41:12 +0100206 }
Flavio Lerda178eeeb2011-07-11 19:51:40 +0100207 }
208
Hugo Hudson157dde12011-07-21 23:28:13 +0100209 private boolean hasVoicemail() {
Hugo Hudson7b02fde2011-08-02 20:56:48 +0100210 return getVoicemailUri() != null;
211 }
212
213 private Uri getVoicemailUri() {
214 return getIntent().getParcelableExtra(EXTRA_VOICEMAIL_URI);
Hugo Hudson157dde12011-07-21 23:28:13 +0100215 }
216
Debashish Chatterjee0a139002011-08-02 18:27:11 +0100217 private void markVoicemailAsRead(final Uri voicemailUri) {
Hugo Hudson51ada362011-08-05 14:36:14 +0100218 mBackgroundTaskService.submit(new AbstractBackgroundTask() {
Debashish Chatterjee0a139002011-08-02 18:27:11 +0100219 @Override
Hugo Hudson51ada362011-08-05 14:36:14 +0100220 public void doInBackground() {
Debashish Chatterjee0a139002011-08-02 18:27:11 +0100221 ContentValues values = new ContentValues();
222 values.put(Voicemails.IS_READ, true);
223 getContentResolver().update(voicemailUri, values, null, null);
Debashish Chatterjee0a139002011-08-02 18:27:11 +0100224 }
Hugo Hudson51ada362011-08-05 14:36:14 +0100225 });
Debashish Chatterjee0a139002011-08-02 18:27:11 +0100226 }
227
Flavio Lerda178eeeb2011-07-11 19:51:40 +0100228 /**
229 * Returns the list of URIs to show.
230 * <p>
231 * There are two ways the URIs can be provided to the activity: as the data on the intent, or as
232 * a list of ids in the call log added as an extra on the URI.
233 * <p>
234 * If both are available, the data on the intent takes precedence.
235 */
236 private Uri[] getCallLogEntryUris() {
237 Uri uri = getIntent().getData();
238 if (uri != null) {
239 // If there is a data on the intent, it takes precedence over the extra.
240 return new Uri[]{ uri };
241 }
242 long[] ids = getIntent().getLongArrayExtra(EXTRA_CALL_LOG_IDS);
243 Uri[] uris = new Uri[ids.length];
244 for (int index = 0; index < ids.length; ++index) {
245 uris[index] = ContentUris.withAppendedId(Calls.CONTENT_URI_WITH_VOICEMAIL, ids[index]);
246 }
247 return uris;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800248 }
249
250 @Override
251 public boolean onKeyDown(int keyCode, KeyEvent event) {
252 switch (keyCode) {
253 case KeyEvent.KEYCODE_CALL: {
254 // Make sure phone isn't already busy before starting direct call
255 TelephonyManager tm = (TelephonyManager)
256 getSystemService(Context.TELEPHONY_SERVICE);
257 if (tm.getCallState() == TelephonyManager.CALL_STATE_IDLE) {
258 Intent callIntent = new Intent(Intent.ACTION_CALL_PRIVILEGED,
259 Uri.fromParts("tel", mNumber, null));
260 startActivity(callIntent);
261 return true;
262 }
263 }
264 }
Dmitri Plotnikovd0d776d2009-08-19 17:38:04 -0700265
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800266 return super.onKeyDown(keyCode, event);
267 }
Dmitri Plotnikovd0d776d2009-08-19 17:38:04 -0700268
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800269 /**
270 * Update user interface with details of given call.
Dmitri Plotnikovd0d776d2009-08-19 17:38:04 -0700271 *
Flavio Lerda178eeeb2011-07-11 19:51:40 +0100272 * @param callUris URIs into {@link CallLog.Calls} of the calls to be displayed
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800273 */
Flavio Lerda178eeeb2011-07-11 19:51:40 +0100274 private void updateData(final Uri... callUris) {
275 // TODO: All phone calls correspond to the same person, so we can make a single lookup.
276 final int numCalls = callUris.length;
277 final PhoneCallDetails[] details = new PhoneCallDetails[numCalls];
278 try {
279 for (int index = 0; index < numCalls; ++index) {
280 details[index] = getPhoneCallDetailsForUri(callUris[index]);
281 }
282 } catch (IllegalArgumentException e) {
283 // Something went wrong reading in our primary data, so we're going to
284 // bail out and show error to users.
285 Log.w(TAG, "invalid URI starting call details", e);
286 Toast.makeText(this, R.string.toast_call_detail_error,
287 Toast.LENGTH_SHORT).show();
288 finish();
289 return;
290 }
291
292 // We know that all calls are from the same number and the same contact, so pick the first.
293 mNumber = details[0].number.toString();
294 final long personId = details[0].personId;
Daniel Lehmann362654a2011-07-17 14:16:47 -0700295 final Uri photoUri = details[0].photoUri;
Flavio Lerda178eeeb2011-07-11 19:51:40 +0100296
297 // Set the details header, based on the first phone call.
Flavio Lerdab88abaa2011-08-02 23:38:36 +0100298 mPhoneCallDetailsHelper.setPhoneCallName(mHeaderTextView, details[0]);
Flavio Lerda178eeeb2011-07-11 19:51:40 +0100299
Flavio Lerdacfff16d2011-07-12 23:54:40 +0100300 // Cache the details about the phone number.
301 final Uri numberCallUri = mPhoneNumberHelper.getCallUri(mNumber);
302 final boolean canPlaceCallsTo = mPhoneNumberHelper.canPlaceCallsTo(mNumber);
303 final boolean isVoicemailNumber = mPhoneNumberHelper.isVoicemailNumber(mNumber);
304 final boolean isSipNumber = mPhoneNumberHelper.isSipNumber(mNumber);
305
Flavio Lerda178eeeb2011-07-11 19:51:40 +0100306 // Let user view contact details if they exist, otherwise add option to create new contact
307 // from this number.
308 final Intent mainActionIntent;
309 final int mainActionIcon;
310
311 if (details[0].personId != -1) {
312 Uri personUri = ContentUris.withAppendedId(Contacts.CONTENT_URI, personId);
313 mainActionIntent = new Intent(Intent.ACTION_VIEW, personUri);
Daniel Lehmannd260de42011-08-01 18:08:38 -0700314 mainActionIcon = R.drawable.ic_contacts_holo_dark;
Flavio Lerdacfff16d2011-07-12 23:54:40 +0100315 } else if (isVoicemailNumber) {
316 mainActionIntent = null;
317 mainActionIcon = 0;
318 } else if (isSipNumber) {
319 // TODO: This item is currently disabled for SIP addresses, because
320 // the Insert.PHONE extra only works correctly for PSTN numbers.
321 //
322 // To fix this for SIP addresses, we need to:
323 // - define ContactsContract.Intents.Insert.SIP_ADDRESS, and use it here if
324 // the current number is a SIP address
325 // - update the contacts UI code to handle Insert.SIP_ADDRESS by
326 // updating the SipAddress field
327 // and then we can remove the "!isSipNumber" check above.
328 mainActionIntent = null;
329 mainActionIcon = 0;
Flavio Lerda8e403402011-07-20 16:25:49 +0100330 } else if (canPlaceCallsTo) {
Flavio Lerda178eeeb2011-07-11 19:51:40 +0100331 mainActionIntent = new Intent(Intent.ACTION_INSERT_OR_EDIT);
332 mainActionIntent.setType(Contacts.CONTENT_ITEM_TYPE);
333 mainActionIntent.putExtra(Insert.PHONE, mNumber);
Daniel Lehmannd260de42011-08-01 18:08:38 -0700334 mainActionIcon = R.drawable.ic_add_contact_holo_dark;
Flavio Lerda8e403402011-07-20 16:25:49 +0100335 } else {
336 // If we cannot call the number, when we probably cannot add it as a contact either.
337 // This is usually the case of private, unknown, or payphone numbers.
338 mainActionIntent = null;
339 mainActionIcon = 0;
Flavio Lerda178eeeb2011-07-11 19:51:40 +0100340 }
341
Flavio Lerdacfff16d2011-07-12 23:54:40 +0100342 if (mainActionIntent == null) {
343 mMainActionView.setVisibility(View.INVISIBLE);
Daniel Lehmannd260de42011-08-01 18:08:38 -0700344 mMainActionPushLayerView.setVisibility(View.GONE);
Flavio Lerdacfff16d2011-07-12 23:54:40 +0100345 } else {
346 mMainActionView.setVisibility(View.VISIBLE);
347 mMainActionView.setImageResource(mainActionIcon);
Daniel Lehmannd260de42011-08-01 18:08:38 -0700348 mMainActionPushLayerView.setVisibility(View.VISIBLE);
349 mMainActionPushLayerView.setOnClickListener(new View.OnClickListener() {
Flavio Lerdacfff16d2011-07-12 23:54:40 +0100350 @Override
351 public void onClick(View v) {
352 startActivity(mainActionIntent);
353 }
354 });
355 }
Flavio Lerda178eeeb2011-07-11 19:51:40 +0100356
357 // Build list of various available actions.
358 final List<ViewEntry> actions = new ArrayList<ViewEntry>();
359
Flavio Lerda178eeeb2011-07-11 19:51:40 +0100360 // This action allows to call the number that places the call.
Flavio Lerdacfff16d2011-07-12 23:54:40 +0100361 if (canPlaceCallsTo) {
Flavio Lerda178eeeb2011-07-11 19:51:40 +0100362 actions.add(new ViewEntry(android.R.drawable.sym_action_call,
363 getString(R.string.menu_callNumber, mNumber),
364 new Intent(Intent.ACTION_CALL_PRIVILEGED, numberCallUri)));
365 }
366
Flavio Lerdacfff16d2011-07-12 23:54:40 +0100367 // This action allows to send an SMS to the number that placed the call.
368 if (mPhoneNumberHelper.canSendSmsTo(mNumber)) {
Flavio Lerda178eeeb2011-07-11 19:51:40 +0100369 Intent smsIntent = new Intent(Intent.ACTION_SENDTO,
370 Uri.fromParts("sms", mNumber, null));
371 actions.add(new ViewEntry(R.drawable.sym_action_sms,
372 getString(R.string.menu_sendTextMessage), smsIntent));
373 }
374
Flavio Lerda94d7bab2011-07-22 16:52:34 +0100375 mHasEditNumberBeforeCall = canPlaceCallsTo && !isSipNumber && !isVoicemailNumber;
Flavio Lerda178eeeb2011-07-11 19:51:40 +0100376
Flavio Lerda94d7bab2011-07-22 16:52:34 +0100377 if (actions.size() != 0) {
378 // Set the actions for this phone number.
379 setListAdapter(new ViewAdapter(this, actions));
380 getListView().setVisibility(View.VISIBLE);
381 } else {
382 getListView().setVisibility(View.GONE);
Hugo Hudson157dde12011-07-21 23:28:13 +0100383 }
Flavio Lerda178eeeb2011-07-11 19:51:40 +0100384
Flavio Lerda178eeeb2011-07-11 19:51:40 +0100385 ListView historyList = (ListView) findViewById(R.id.history);
386 historyList.setAdapter(
387 new CallDetailHistoryAdapter(this, mInflater, mCallTypeHelper, details));
Daniel Lehmann362654a2011-07-17 14:16:47 -0700388 loadContactPhotos(photoUri);
Flavio Lerda178eeeb2011-07-11 19:51:40 +0100389 }
390
391 /** Return the phone call details for a given call log URI. */
392 private PhoneCallDetails getPhoneCallDetailsForUri(Uri callUri) {
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800393 ContentResolver resolver = getContentResolver();
394 Cursor callCursor = resolver.query(callUri, CALL_LOG_PROJECTION, null, null, null);
395 try {
Flavio Lerda178eeeb2011-07-11 19:51:40 +0100396 if (callCursor == null || !callCursor.moveToFirst()) {
397 throw new IllegalArgumentException("Cannot find content: " + callUri);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800398 }
Flavio Lerda178eeeb2011-07-11 19:51:40 +0100399
400 // Read call log specifics.
401 String number = callCursor.getString(NUMBER_COLUMN_INDEX);
402 long date = callCursor.getLong(DATE_COLUMN_INDEX);
403 long duration = callCursor.getLong(DURATION_COLUMN_INDEX);
404 int callType = callCursor.getInt(CALL_TYPE_COLUMN_INDEX);
405 String countryIso = callCursor.getString(COUNTRY_ISO_COLUMN_INDEX);
406 if (TextUtils.isEmpty(countryIso)) {
407 countryIso = mDefaultCountryIso;
408 }
409
410 // Formatted phone number.
411 final CharSequence numberText;
412 // Read contact specifics.
413 CharSequence nameText = "";
414 int numberType = 0;
415 CharSequence numberLabel = "";
416 long personId = -1L;
Daniel Lehmann362654a2011-07-17 14:16:47 -0700417 Uri photoUri = null;
Flavio Lerda178eeeb2011-07-11 19:51:40 +0100418 // If this is not a regular number, there is no point in looking it up in the contacts.
419 if (!mPhoneNumberHelper.canPlaceCallsTo(number)) {
420 numberText = mPhoneNumberHelper.getDisplayNumber(number, null);
421 } else {
422 // Perform a reverse-phonebook lookup to find the contact details.
423 Uri phoneUri = Uri.withAppendedPath(PhoneLookup.CONTENT_FILTER_URI,
424 Uri.encode(number));
425 Cursor phonesCursor = resolver.query(phoneUri, PHONES_PROJECTION, null, null, null);
426 String candidateNumberText = number;
427 try {
428 if (phonesCursor != null && phonesCursor.moveToFirst()) {
429 personId = phonesCursor.getLong(COLUMN_INDEX_ID);
430 nameText = phonesCursor.getString(COLUMN_INDEX_NAME);
Daniel Lehmann362654a2011-07-17 14:16:47 -0700431 String photoUriString = phonesCursor.getString(COLUMN_INDEX_PHOTO_URI);
432 photoUri = photoUriString == null ? null : Uri.parse(photoUriString);
Flavio Lerda178eeeb2011-07-11 19:51:40 +0100433 candidateNumberText = PhoneNumberUtils.formatNumber(
434 phonesCursor.getString(COLUMN_INDEX_NUMBER),
435 phonesCursor.getString(COLUMN_INDEX_NORMALIZED_NUMBER),
436 countryIso);
437 numberType = phonesCursor.getInt(COLUMN_INDEX_TYPE);
438 numberLabel = phonesCursor.getString(COLUMN_INDEX_LABEL);
439 } else {
440 // We could not find this contact in the contacts, just format the phone
441 // number as best as we can. All the other fields will have their default
442 // values.
443 candidateNumberText =
444 PhoneNumberUtils.formatNumber(number, countryIso);
445 }
446 } finally {
447 if (phonesCursor != null) phonesCursor.close();
448 numberText = candidateNumberText;
449 }
450 }
Flavio Lerda9c466372011-07-19 17:38:17 +0100451 return new PhoneCallDetails(number, numberText, countryIso,
Flavio Lerdacb805e82011-07-18 10:31:44 +0100452 new int[]{ callType }, date, duration,
Daniel Lehmann362654a2011-07-17 14:16:47 -0700453 nameText, numberType, numberLabel, personId, photoUri);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800454 } finally {
455 if (callCursor != null) {
456 callCursor.close();
457 }
458 }
459 }
460
Flavio Lerda9cafe472011-06-08 14:06:13 +0100461 /** Load the contact photos and places them in the corresponding views. */
Daniel Lehmann362654a2011-07-17 14:16:47 -0700462 private void loadContactPhotos(Uri photoUri) {
463 mContactPhotoManager.loadPhoto(mContactBackgroundView, photoUri);
Flavio Lerda9cafe472011-06-08 14:06:13 +0100464 }
465
Flavio Lerda696e8132011-07-05 19:00:23 +0100466 private String getVoicemailNumber() {
467 TelephonyManager telephonyManager =
468 (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
469 return telephonyManager.getVoiceMailNumber();
470 }
471
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800472 static final class ViewEntry {
Flavio Lerdaa8956882011-07-09 21:34:38 +0100473 public final int icon;
474 public final String text;
475 public final Intent intent;
476 public final View.OnClickListener action;
477
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800478 public String label = null;
479 public String number = null;
Dmitri Plotnikovd0d776d2009-08-19 17:38:04 -0700480
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800481 public ViewEntry(int icon, String text, Intent intent) {
482 this.icon = icon;
483 this.text = text;
484 this.intent = intent;
Flavio Lerdaa8956882011-07-09 21:34:38 +0100485 this.action = null;
486 }
487
488 public ViewEntry(int icon, String text, View.OnClickListener listener) {
489 this.icon = icon;
490 this.text = text;
491 this.intent = null;
492 this.action = listener;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800493 }
494 }
495
496 static final class ViewAdapter extends BaseAdapter {
Dmitri Plotnikovd0d776d2009-08-19 17:38:04 -0700497
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800498 private final List<ViewEntry> mActions;
Dmitri Plotnikovd0d776d2009-08-19 17:38:04 -0700499
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800500 private final LayoutInflater mInflater;
Dmitri Plotnikovd0d776d2009-08-19 17:38:04 -0700501
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800502 public ViewAdapter(Context context, List<ViewEntry> actions) {
503 mActions = actions;
504 mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
505 }
Dmitri Plotnikovd0d776d2009-08-19 17:38:04 -0700506
Flavio Lerda9cafe472011-06-08 14:06:13 +0100507 @Override
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800508 public int getCount() {
509 return mActions.size();
510 }
511
Flavio Lerda9cafe472011-06-08 14:06:13 +0100512 @Override
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800513 public Object getItem(int position) {
514 return mActions.get(position);
515 }
516
Flavio Lerda9cafe472011-06-08 14:06:13 +0100517 @Override
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800518 public long getItemId(int position) {
519 return position;
520 }
Dmitri Plotnikovd0d776d2009-08-19 17:38:04 -0700521
Flavio Lerda9cafe472011-06-08 14:06:13 +0100522 @Override
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800523 public View getView(int position, View convertView, ViewGroup parent) {
524 // Make sure we have a valid convertView to start with
525 if (convertView == null) {
526 convertView = mInflater.inflate(R.layout.call_detail_list_item, parent, false);
527 }
528
529 // Fill action with icon and text.
530 ViewEntry entry = mActions.get(position);
531 convertView.setTag(entry);
Dmitri Plotnikovd0d776d2009-08-19 17:38:04 -0700532
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800533 ImageView icon = (ImageView) convertView.findViewById(R.id.icon);
534 TextView text = (TextView) convertView.findViewById(android.R.id.text1);
535
536 icon.setImageResource(entry.icon);
537 text.setText(entry.text);
538
539 View line2 = convertView.findViewById(R.id.line2);
540 boolean numberEmpty = TextUtils.isEmpty(entry.number);
541 boolean labelEmpty = TextUtils.isEmpty(entry.label) || numberEmpty;
542 if (labelEmpty && numberEmpty) {
543 line2.setVisibility(View.GONE);
544 } else {
545 line2.setVisibility(View.VISIBLE);
Dmitri Plotnikovd0d776d2009-08-19 17:38:04 -0700546
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800547 TextView label = (TextView) convertView.findViewById(R.id.label);
548 if (labelEmpty) {
549 label.setVisibility(View.GONE);
550 } else {
551 label.setText(entry.label);
552 label.setVisibility(View.VISIBLE);
553 }
554
555 TextView number = (TextView) convertView.findViewById(R.id.number);
556 number.setText(entry.number);
557 }
Dmitri Plotnikovd0d776d2009-08-19 17:38:04 -0700558
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800559 return convertView;
560 }
561 }
Dmitri Plotnikovd0d776d2009-08-19 17:38:04 -0700562
Flavio Lerda9cafe472011-06-08 14:06:13 +0100563 @Override
564 public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800565 // Handle passing action off to correct handler.
566 if (view.getTag() instanceof ViewEntry) {
567 ViewEntry entry = (ViewEntry) view.getTag();
568 if (entry.intent != null) {
569 startActivity(entry.intent);
Flavio Lerdaa8956882011-07-09 21:34:38 +0100570 } else if (entry.action != null) {
571 entry.action.onClick(view);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800572 }
573 }
Dmitri Plotnikovd0d776d2009-08-19 17:38:04 -0700574 }
Dmitri Plotnikov8e86b752010-02-22 17:47:57 -0800575
576 @Override
577 public void startSearch(String initialQuery, boolean selectInitialQuery, Bundle appSearchData,
578 boolean globalSearch) {
579 if (globalSearch) {
580 super.startSearch(initialQuery, selectInitialQuery, appSearchData, globalSearch);
581 } else {
582 ContactsSearchManager.startSearch(this, initialQuery);
583 }
584 }
Debashish Chatterjee0cb82242011-07-19 19:29:37 +0100585
586 protected void updateVoicemailStatusMessage(Cursor statusCursor) {
587 if (statusCursor == null) {
588 mStatusMessageView.setVisibility(View.GONE);
589 return;
590 }
591 final StatusMessage message = getStatusMessage(statusCursor);
592 if (message == null || !message.showInCallDetails()) {
593 mStatusMessageView.setVisibility(View.GONE);
594 return;
595 }
596
597 mStatusMessageView.setVisibility(View.VISIBLE);
598 mStatusMessageText.setText(message.callDetailsMessageId);
599 if (message.actionMessageId != -1) {
600 mStatusMessageAction.setText(message.actionMessageId);
601 }
602 if (message.actionUri != null) {
603 mStatusMessageAction.setClickable(true);
604 mStatusMessageAction.setOnClickListener(new View.OnClickListener() {
605 @Override
606 public void onClick(View v) {
607 startActivity(new Intent(Intent.ACTION_VIEW, message.actionUri));
608 }
609 });
610 } else {
611 mStatusMessageAction.setClickable(false);
612 }
613 }
614
615 private StatusMessage getStatusMessage(Cursor statusCursor) {
616 List<StatusMessage> messages = mVoicemailStatusHelper.getStatusMessages(statusCursor);
617 Log.d(TAG, "Num status messages: " + messages.size());
618 if (messages.size() == 0) {
619 return null;
620 }
621 // There can only be a single status message per source package, so num of messages can
622 // at most be 1.
623 if (messages.size() > 1) {
624 Log.w(TAG, String.format("Expected 1, found (%d) num of status messages." +
625 " Will use the first one.", messages.size()));
626 }
627 return messages.get(0);
628 }
Flavio Lerda94d7bab2011-07-22 16:52:34 +0100629
630 @Override
631 public boolean onCreateOptionsMenu(Menu menu) {
632 getMenuInflater().inflate(R.menu.call_details_options, menu);
Hugo Hudsonc2f09c32011-07-30 16:31:28 +0100633 return super.onCreateOptionsMenu(menu);
Flavio Lerda94d7bab2011-07-22 16:52:34 +0100634 }
635
636 @Override
637 public boolean onPrepareOptionsMenu(Menu menu) {
638 // This action deletes all elements in the group from the call log.
639 // We don't have this action for voicemails, because you can just use the trash button.
Hugo Hudsonc2f09c32011-07-30 16:31:28 +0100640 menu.findItem(R.id.menu_remove_from_call_log).setVisible(!hasVoicemail());
641 menu.findItem(R.id.menu_edit_number_before_call).setVisible(mHasEditNumberBeforeCall);
642 menu.findItem(R.id.menu_trash).setVisible(hasVoicemail());
643 menu.findItem(R.id.menu_share_voicemail).setVisible(hasVoicemail());
644 return super.onPrepareOptionsMenu(menu);
Flavio Lerda94d7bab2011-07-22 16:52:34 +0100645 }
646
647 @Override
648 public boolean onMenuItemSelected(int featureId, MenuItem item) {
649 switch (item.getItemId()) {
Flavio Lerdad44e83a2011-07-24 23:10:17 +0100650 case android.R.id.home: {
651 onHomeSelected();
652 return true;
653 }
654
Hugo Hudsonc2f09c32011-07-30 16:31:28 +0100655 // All the options menu items are handled by onMenu... methods.
Flavio Lerda94d7bab2011-07-22 16:52:34 +0100656 default:
657 throw new IllegalArgumentException();
658 }
659 }
Flavio Lerdad44e83a2011-07-24 23:10:17 +0100660
Hugo Hudsonc2f09c32011-07-30 16:31:28 +0100661 public void onMenuRemoveFromCallLog(MenuItem menuItem) {
Hugo Hudson5bbe25d2011-08-03 17:16:42 +0100662 final StringBuilder callIds = new StringBuilder();
Hugo Hudsonc2f09c32011-07-30 16:31:28 +0100663 for (Uri callUri : getCallLogEntryUris()) {
664 if (callIds.length() != 0) {
665 callIds.append(",");
666 }
667 callIds.append(ContentUris.parseId(callUri));
668 }
Hugo Hudson51ada362011-08-05 14:36:14 +0100669 mBackgroundTaskService.submit(new BackgroundTask() {
Hugo Hudson5bbe25d2011-08-03 17:16:42 +0100670 @Override
Hugo Hudson51ada362011-08-05 14:36:14 +0100671 public void doInBackground() {
Hugo Hudson5bbe25d2011-08-03 17:16:42 +0100672 getContentResolver().delete(Calls.CONTENT_URI_WITH_VOICEMAIL,
673 Calls._ID + " IN (" + callIds + ")", null);
674 }
Hugo Hudson51ada362011-08-05 14:36:14 +0100675 @Override
676 public void onPostExecute() {
677 finish();
678 }
Hugo Hudson5bbe25d2011-08-03 17:16:42 +0100679 });
Hugo Hudsonc2f09c32011-07-30 16:31:28 +0100680 }
Hugo Hudsonc2f09c32011-07-30 16:31:28 +0100681 public void onMenuEditNumberBeforeCall(MenuItem menuItem) {
682 startActivity(new Intent(Intent.ACTION_DIAL, mPhoneNumberHelper.getCallUri(mNumber)));
683 }
684
685 public void onMenuShareVoicemail(MenuItem menuItem) {
686 Log.w(TAG, "onMenuShareVoicemail not yet implemented");
687 }
688
689 public void onMenuTrashVoicemail(MenuItem menuItem) {
Hugo Hudson5bbe25d2011-08-03 17:16:42 +0100690 final Uri voicemailUri = getVoicemailUri();
Hugo Hudson51ada362011-08-05 14:36:14 +0100691 mBackgroundTaskService.submit(new BackgroundTask() {
Hugo Hudson5bbe25d2011-08-03 17:16:42 +0100692 @Override
Hugo Hudson51ada362011-08-05 14:36:14 +0100693 public void doInBackground() {
Hugo Hudson5bbe25d2011-08-03 17:16:42 +0100694 getContentResolver().delete(voicemailUri, null, null);
695 }
Hugo Hudson5bbe25d2011-08-03 17:16:42 +0100696 @Override
Hugo Hudson51ada362011-08-05 14:36:14 +0100697 public void onPostExecute() {
Hugo Hudson5bbe25d2011-08-03 17:16:42 +0100698 finish();
699 }
Hugo Hudson51ada362011-08-05 14:36:14 +0100700 });
Hugo Hudsonc2f09c32011-07-30 16:31:28 +0100701 }
702
Flavio Lerdad44e83a2011-07-24 23:10:17 +0100703 private void configureActionBar() {
704 ActionBar actionBar = getActionBar();
705 if (actionBar != null) {
Hugo Hudsonc2f09c32011-07-30 16:31:28 +0100706 actionBar.setDisplayOptions(ActionBar.DISPLAY_HOME_AS_UP | ActionBar.DISPLAY_SHOW_HOME);
Flavio Lerdad44e83a2011-07-24 23:10:17 +0100707 }
708 }
709
710 /** Invoked when the user presses the home button in the action bar. */
711 private void onHomeSelected() {
712 Intent intent = new Intent(Intent.ACTION_VIEW, Calls.CONTENT_URI);
713 // This will open the call log even if the detail view has been opened directly.
714 intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
715 startActivity(intent);
716 finish();
717 }
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800718}