blob: 9df33ba0ac73dbb7bb8900a38b0402b96e1ca711 [file] [log] [blame]
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -08001/*
2 * Copyright (C) 2009 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.contacts;
18
Flavio Lerda9a208cc2011-07-12 21:05:47 +010019import com.android.contacts.calllog.CallDetailHistoryAdapter;
20import com.android.contacts.calllog.CallTypeHelper;
Flavio Lerda178eeeb2011-07-11 19:51:40 +010021import com.android.contacts.calllog.PhoneNumberHelper;
Hugo Hudsonb002f512011-07-15 17:41:12 +010022import com.android.contacts.voicemail.VoicemailPlaybackFragment;
Debashish Chatterjee0cb82242011-07-19 19:29:37 +010023import com.android.contacts.voicemail.VoicemailStatusHelper;
24import com.android.contacts.voicemail.VoicemailStatusHelper.StatusMessage;
25import com.android.contacts.voicemail.VoicemailStatusHelperImpl;
Amith Yamasani8bbe2f22009-03-24 21:24:12 -070026
Flavio Lerdad44e83a2011-07-24 23:10:17 +010027import android.app.ActionBar;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080028import android.app.ListActivity;
29import android.content.ContentResolver;
30import android.content.ContentUris;
Debashish Chatterjee0a139002011-08-02 18:27:11 +010031import android.content.ContentValues;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080032import android.content.Context;
33import android.content.Intent;
34import android.content.res.Resources;
35import android.database.Cursor;
36import android.net.Uri;
Debashish Chatterjee0a139002011-08-02 18:27:11 +010037import android.os.AsyncTask;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080038import android.os.Bundle;
39import android.provider.CallLog;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080040import android.provider.CallLog.Calls;
Flavio Lerda9cafe472011-06-08 14:06:13 +010041import android.provider.Contacts.Intents.Insert;
Dmitri Plotnikovd0d776d2009-08-19 17:38:04 -070042import android.provider.ContactsContract.Contacts;
43import android.provider.ContactsContract.PhoneLookup;
Debashish Chatterjee0a139002011-08-02 18:27:11 +010044import android.provider.VoicemailContract.Voicemails;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080045import android.telephony.PhoneNumberUtils;
46import android.telephony.TelephonyManager;
47import android.text.TextUtils;
Flavio Lerda178eeeb2011-07-11 19:51:40 +010048import android.util.Log;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080049import android.view.KeyEvent;
50import android.view.LayoutInflater;
Flavio Lerda94d7bab2011-07-22 16:52:34 +010051import android.view.Menu;
52import android.view.MenuItem;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080053import android.view.View;
54import android.view.ViewGroup;
55import android.widget.AdapterView;
56import android.widget.BaseAdapter;
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
63import java.util.ArrayList;
64import 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 */
72public class CallDetailActivity extends ListActivity implements
73 AdapterView.OnItemClickListener {
74 private static final String TAG = "CallDetail";
75
Flavio Lerda178eeeb2011-07-11 19:51:40 +010076 /** A long array extra containing ids of call log entries to display. */
Hugo Hudsonb002f512011-07-15 17:41:12 +010077 public static final String EXTRA_CALL_LOG_IDS = "EXTRA_CALL_LOG_IDS";
78 /** If we are started with a voicemail, we'll find the uri to play with this extra. */
79 public static final String EXTRA_VOICEMAIL_URI = "EXTRA_VOICEMAIL_URI";
80 /** If we should immediately start playback of the voicemail, this extra will be set to true. */
81 public static final String EXTRA_VOICEMAIL_START_PLAYBACK = "EXTRA_VOICEMAIL_START_PLAYBACK";
Flavio Lerda178eeeb2011-07-11 19:51:40 +010082
Flavio Lerdaa024c3f2011-06-10 10:47:07 +010083 /** The views representing the details of a phone call. */
Flavio Lerdaafb93bb2011-07-05 14:46:10 +010084 private PhoneCallDetailsViews mPhoneCallDetailsViews;
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 Lerdafb63c432011-07-07 17:16:53 +010088 private ImageView mMainActionView;
Daniel Lehmannd260de42011-08-01 18:08:38 -070089 private ImageButton mMainActionPushLayerView;
Flavio Lerda9cafe472011-06-08 14:06:13 +010090 private ImageView mContactBackgroundView;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080091
92 private String mNumber = null;
Bai Tao09eb04f2010-09-01 15:34:16 +080093 private String mDefaultCountryIso;
Dmitri Plotnikovd0d776d2009-08-19 17:38:04 -070094
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080095 /* package */ LayoutInflater mInflater;
96 /* package */ Resources mResources;
Flavio Lerda9cafe472011-06-08 14:06:13 +010097 /** Helper to load contact photos. */
98 private ContactPhotoManager mContactPhotoManager;
Debashish Chatterjee0cb82242011-07-19 19:29:37 +010099 /** Helper to make async queries to content resolver. */
100 private CallDetailActivityQueryHandler mAsyncQueryHandler;
101 /** Helper to get voicemail status messages. */
102 private VoicemailStatusHelper mVoicemailStatusHelper;
103 // Views related to voicemail status message.
104 private View mStatusMessageView;
105 private TextView mStatusMessageText;
106 private TextView mStatusMessageAction;
Dmitri Plotnikovd0d776d2009-08-19 17:38:04 -0700107
Flavio Lerda94d7bab2011-07-22 16:52:34 +0100108 /** Whether we should show "edit number before call" in the options menu. */
109 private boolean mHasEditNumberBeforeCall;
110
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800111 static final String[] CALL_LOG_PROJECTION = new String[] {
112 CallLog.Calls.DATE,
113 CallLog.Calls.DURATION,
114 CallLog.Calls.NUMBER,
115 CallLog.Calls.TYPE,
Bai Tao09eb04f2010-09-01 15:34:16 +0800116 CallLog.Calls.COUNTRY_ISO,
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;
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
142 @Override
143 protected void onCreate(Bundle icicle) {
144 super.onCreate(icicle);
145
146 setContentView(R.layout.call_detail);
147
148 mInflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
149 mResources = getResources();
Dmitri Plotnikovd0d776d2009-08-19 17:38:04 -0700150
Flavio Lerda696e8132011-07-05 19:00:23 +0100151 mPhoneCallDetailsViews = PhoneCallDetailsViews.fromView(getWindow().getDecorView());
Daniel Lehmann6ecb7322011-08-01 21:39:29 -0700152 mCallTypeHelper = new CallTypeHelper(getResources());
Flavio Lerda178eeeb2011-07-11 19:51:40 +0100153 mPhoneNumberHelper = new PhoneNumberHelper(mResources, getVoicemailNumber());
Flavio Lerda40569562011-07-22 21:51:29 +0100154 mPhoneCallDetailsHelper = new PhoneCallDetailsHelper(mResources, mCallTypeHelper,
Flavio Lerda178eeeb2011-07-11 19:51:40 +0100155 mPhoneNumberHelper);
Debashish Chatterjee0cb82242011-07-19 19:29:37 +0100156 mVoicemailStatusHelper = new VoicemailStatusHelperImpl();
157 mAsyncQueryHandler = new CallDetailActivityQueryHandler(this);
158 mStatusMessageView = findViewById(R.id.voicemail_status);
159 mStatusMessageText = (TextView) findViewById(R.id.voicemail_status_message);
160 mStatusMessageAction = (TextView) findViewById(R.id.voicemail_status_action);
Flavio Lerdafb63c432011-07-07 17:16:53 +0100161 mMainActionView = (ImageView) findViewById(R.id.main_action);
Daniel Lehmannd260de42011-08-01 18:08:38 -0700162 mMainActionPushLayerView = (ImageButton) findViewById(R.id.main_action_push_layer);
Flavio Lerda9cafe472011-06-08 14:06:13 +0100163 mContactBackgroundView = (ImageView) findViewById(R.id.contact_background);
Bai Tao09eb04f2010-09-01 15:34:16 +0800164 mDefaultCountryIso = ContactsUtils.getCurrentCountryIso(this);
Flavio Lerda9cafe472011-06-08 14:06:13 +0100165 mContactPhotoManager = ContactPhotoManager.getInstance(this);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800166 getListView().setOnItemClickListener(this);
Flavio Lerdad44e83a2011-07-24 23:10:17 +0100167 configureActionBar();
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800168 }
Dmitri Plotnikovd0d776d2009-08-19 17:38:04 -0700169
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800170 @Override
171 public void onResume() {
172 super.onResume();
Flavio Lerda178eeeb2011-07-11 19:51:40 +0100173 updateData(getCallLogEntryUris());
Hugo Hudson757aa552011-07-20 01:15:25 +0100174 optionallyHandleVoicemail();
Hugo Hudsonb002f512011-07-15 17:41:12 +0100175 }
176
177 /**
178 * Handle voicemail playback or hide voicemail ui.
179 * <p>
180 * If the Intent used to start this Activity contains the suitable extras, then start voicemail
181 * playback. If it doesn't, then hide the voicemail ui.
182 */
Hugo Hudson757aa552011-07-20 01:15:25 +0100183 private void optionallyHandleVoicemail() {
Hugo Hudson157dde12011-07-21 23:28:13 +0100184 if (hasVoicemail()) {
Hugo Hudsona9ad6942011-07-29 17:06:04 +0100185 // Has voicemail: add the voicemail fragment. Add suitable arguments to set the uri
186 // to play and optionally start the playback.
Hugo Hudson757aa552011-07-20 01:15:25 +0100187 // Do a query to fetch the voicemail status messages.
Hugo Hudsona9ad6942011-07-29 17:06:04 +0100188 VoicemailPlaybackFragment playbackFragment = new VoicemailPlaybackFragment();
189 Bundle fragmentArguments = new Bundle();
Hugo Hudson7b02fde2011-08-02 20:56:48 +0100190 fragmentArguments.putParcelable(EXTRA_VOICEMAIL_URI, getVoicemailUri());
Hugo Hudsona9ad6942011-07-29 17:06:04 +0100191 if (getIntent().getBooleanExtra(EXTRA_VOICEMAIL_START_PLAYBACK, false)) {
192 fragmentArguments.putBoolean(EXTRA_VOICEMAIL_START_PLAYBACK, true);
193 }
194 playbackFragment.setArguments(fragmentArguments);
195 getFragmentManager().beginTransaction()
196 .add(R.id.voicemail_container, playbackFragment).commit();
Hugo Hudson7b02fde2011-08-02 20:56:48 +0100197 mAsyncQueryHandler.startVoicemailStatusQuery(getVoicemailUri());
Debashish Chatterjee0a139002011-08-02 18:27:11 +0100198 markVoicemailAsRead(getVoicemailUri());
Hugo Hudson757aa552011-07-20 01:15:25 +0100199 } else {
Hugo Hudsona9ad6942011-07-29 17:06:04 +0100200 // No voicemail uri: hide the status view.
Hugo Hudson757aa552011-07-20 01:15:25 +0100201 mStatusMessageView.setVisibility(View.GONE);
Hugo Hudsonb002f512011-07-15 17:41:12 +0100202 }
Flavio Lerda178eeeb2011-07-11 19:51:40 +0100203 }
204
Hugo Hudson157dde12011-07-21 23:28:13 +0100205 private boolean hasVoicemail() {
Hugo Hudson7b02fde2011-08-02 20:56:48 +0100206 return getVoicemailUri() != null;
207 }
208
209 private Uri getVoicemailUri() {
210 return getIntent().getParcelableExtra(EXTRA_VOICEMAIL_URI);
Hugo Hudson157dde12011-07-21 23:28:13 +0100211 }
212
Debashish Chatterjee0a139002011-08-02 18:27:11 +0100213 private void markVoicemailAsRead(final Uri voicemailUri) {
214 new AsyncTask<Void, Void, Void>() {
215 @Override
216 protected Void doInBackground(Void... params) {
217 ContentValues values = new ContentValues();
218 values.put(Voicemails.IS_READ, true);
219 getContentResolver().update(voicemailUri, values, null, null);
220 return null;
221 }
222 }.execute();
223 }
224
Flavio Lerda178eeeb2011-07-11 19:51:40 +0100225 /**
226 * Returns the list of URIs to show.
227 * <p>
228 * There are two ways the URIs can be provided to the activity: as the data on the intent, or as
229 * a list of ids in the call log added as an extra on the URI.
230 * <p>
231 * If both are available, the data on the intent takes precedence.
232 */
233 private Uri[] getCallLogEntryUris() {
234 Uri uri = getIntent().getData();
235 if (uri != null) {
236 // If there is a data on the intent, it takes precedence over the extra.
237 return new Uri[]{ uri };
238 }
239 long[] ids = getIntent().getLongArrayExtra(EXTRA_CALL_LOG_IDS);
240 Uri[] uris = new Uri[ids.length];
241 for (int index = 0; index < ids.length; ++index) {
242 uris[index] = ContentUris.withAppendedId(Calls.CONTENT_URI_WITH_VOICEMAIL, ids[index]);
243 }
244 return uris;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800245 }
246
247 @Override
248 public boolean onKeyDown(int keyCode, KeyEvent event) {
249 switch (keyCode) {
250 case KeyEvent.KEYCODE_CALL: {
251 // Make sure phone isn't already busy before starting direct call
252 TelephonyManager tm = (TelephonyManager)
253 getSystemService(Context.TELEPHONY_SERVICE);
254 if (tm.getCallState() == TelephonyManager.CALL_STATE_IDLE) {
255 Intent callIntent = new Intent(Intent.ACTION_CALL_PRIVILEGED,
256 Uri.fromParts("tel", mNumber, null));
257 startActivity(callIntent);
258 return true;
259 }
260 }
261 }
Dmitri Plotnikovd0d776d2009-08-19 17:38:04 -0700262
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800263 return super.onKeyDown(keyCode, event);
264 }
Dmitri Plotnikovd0d776d2009-08-19 17:38:04 -0700265
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800266 /**
267 * Update user interface with details of given call.
Dmitri Plotnikovd0d776d2009-08-19 17:38:04 -0700268 *
Flavio Lerda178eeeb2011-07-11 19:51:40 +0100269 * @param callUris URIs into {@link CallLog.Calls} of the calls to be displayed
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800270 */
Flavio Lerda178eeeb2011-07-11 19:51:40 +0100271 private void updateData(final Uri... callUris) {
272 // TODO: All phone calls correspond to the same person, so we can make a single lookup.
273 final int numCalls = callUris.length;
274 final PhoneCallDetails[] details = new PhoneCallDetails[numCalls];
275 try {
276 for (int index = 0; index < numCalls; ++index) {
277 details[index] = getPhoneCallDetailsForUri(callUris[index]);
278 }
279 } catch (IllegalArgumentException e) {
280 // Something went wrong reading in our primary data, so we're going to
281 // bail out and show error to users.
282 Log.w(TAG, "invalid URI starting call details", e);
283 Toast.makeText(this, R.string.toast_call_detail_error,
284 Toast.LENGTH_SHORT).show();
285 finish();
286 return;
287 }
288
289 // We know that all calls are from the same number and the same contact, so pick the first.
290 mNumber = details[0].number.toString();
291 final long personId = details[0].personId;
Daniel Lehmann362654a2011-07-17 14:16:47 -0700292 final Uri photoUri = details[0].photoUri;
Flavio Lerda178eeeb2011-07-11 19:51:40 +0100293
294 // Set the details header, based on the first phone call.
295 mPhoneCallDetailsHelper.setPhoneCallDetails(mPhoneCallDetailsViews,
Flavio Lerda40569562011-07-22 21:51:29 +0100296 details[0], false, false, true);
Flavio Lerda178eeeb2011-07-11 19:51:40 +0100297
Flavio Lerdacfff16d2011-07-12 23:54:40 +0100298 // Cache the details about the phone number.
299 final Uri numberCallUri = mPhoneNumberHelper.getCallUri(mNumber);
300 final boolean canPlaceCallsTo = mPhoneNumberHelper.canPlaceCallsTo(mNumber);
301 final boolean isVoicemailNumber = mPhoneNumberHelper.isVoicemailNumber(mNumber);
302 final boolean isSipNumber = mPhoneNumberHelper.isSipNumber(mNumber);
303
Flavio Lerda178eeeb2011-07-11 19:51:40 +0100304 // Let user view contact details if they exist, otherwise add option to create new contact
305 // from this number.
306 final Intent mainActionIntent;
307 final int mainActionIcon;
308
309 if (details[0].personId != -1) {
310 Uri personUri = ContentUris.withAppendedId(Contacts.CONTENT_URI, personId);
311 mainActionIntent = new Intent(Intent.ACTION_VIEW, personUri);
Daniel Lehmannd260de42011-08-01 18:08:38 -0700312 mainActionIcon = R.drawable.ic_contacts_holo_dark;
Flavio Lerdacfff16d2011-07-12 23:54:40 +0100313 } else if (isVoicemailNumber) {
314 mainActionIntent = null;
315 mainActionIcon = 0;
316 } else if (isSipNumber) {
317 // TODO: This item is currently disabled for SIP addresses, because
318 // the Insert.PHONE extra only works correctly for PSTN numbers.
319 //
320 // To fix this for SIP addresses, we need to:
321 // - define ContactsContract.Intents.Insert.SIP_ADDRESS, and use it here if
322 // the current number is a SIP address
323 // - update the contacts UI code to handle Insert.SIP_ADDRESS by
324 // updating the SipAddress field
325 // and then we can remove the "!isSipNumber" check above.
326 mainActionIntent = null;
327 mainActionIcon = 0;
Flavio Lerda8e403402011-07-20 16:25:49 +0100328 } else if (canPlaceCallsTo) {
Flavio Lerda178eeeb2011-07-11 19:51:40 +0100329 mainActionIntent = new Intent(Intent.ACTION_INSERT_OR_EDIT);
330 mainActionIntent.setType(Contacts.CONTENT_ITEM_TYPE);
331 mainActionIntent.putExtra(Insert.PHONE, mNumber);
Daniel Lehmannd260de42011-08-01 18:08:38 -0700332 mainActionIcon = R.drawable.ic_add_contact_holo_dark;
Flavio Lerda8e403402011-07-20 16:25:49 +0100333 } else {
334 // If we cannot call the number, when we probably cannot add it as a contact either.
335 // This is usually the case of private, unknown, or payphone numbers.
336 mainActionIntent = null;
337 mainActionIcon = 0;
Flavio Lerda178eeeb2011-07-11 19:51:40 +0100338 }
339
Flavio Lerdacfff16d2011-07-12 23:54:40 +0100340 if (mainActionIntent == null) {
341 mMainActionView.setVisibility(View.INVISIBLE);
Daniel Lehmannd260de42011-08-01 18:08:38 -0700342 mMainActionPushLayerView.setVisibility(View.GONE);
Flavio Lerdacfff16d2011-07-12 23:54:40 +0100343 } else {
344 mMainActionView.setVisibility(View.VISIBLE);
345 mMainActionView.setImageResource(mainActionIcon);
Daniel Lehmannd260de42011-08-01 18:08:38 -0700346 mMainActionPushLayerView.setVisibility(View.VISIBLE);
347 mMainActionPushLayerView.setOnClickListener(new View.OnClickListener() {
Flavio Lerdacfff16d2011-07-12 23:54:40 +0100348 @Override
349 public void onClick(View v) {
350 startActivity(mainActionIntent);
351 }
352 });
353 }
Flavio Lerda178eeeb2011-07-11 19:51:40 +0100354
355 // Build list of various available actions.
356 final List<ViewEntry> actions = new ArrayList<ViewEntry>();
357
Flavio Lerda178eeeb2011-07-11 19:51:40 +0100358 // This action allows to call the number that places the call.
Flavio Lerdacfff16d2011-07-12 23:54:40 +0100359 if (canPlaceCallsTo) {
Flavio Lerda178eeeb2011-07-11 19:51:40 +0100360 actions.add(new ViewEntry(android.R.drawable.sym_action_call,
361 getString(R.string.menu_callNumber, mNumber),
362 new Intent(Intent.ACTION_CALL_PRIVILEGED, numberCallUri)));
363 }
364
Flavio Lerdacfff16d2011-07-12 23:54:40 +0100365 // This action allows to send an SMS to the number that placed the call.
366 if (mPhoneNumberHelper.canSendSmsTo(mNumber)) {
Flavio Lerda178eeeb2011-07-11 19:51:40 +0100367 Intent smsIntent = new Intent(Intent.ACTION_SENDTO,
368 Uri.fromParts("sms", mNumber, null));
369 actions.add(new ViewEntry(R.drawable.sym_action_sms,
370 getString(R.string.menu_sendTextMessage), smsIntent));
371 }
372
Flavio Lerda94d7bab2011-07-22 16:52:34 +0100373 mHasEditNumberBeforeCall = canPlaceCallsTo && !isSipNumber && !isVoicemailNumber;
Flavio Lerda178eeeb2011-07-11 19:51:40 +0100374
Flavio Lerda94d7bab2011-07-22 16:52:34 +0100375 if (actions.size() != 0) {
376 // Set the actions for this phone number.
377 setListAdapter(new ViewAdapter(this, actions));
378 getListView().setVisibility(View.VISIBLE);
379 } else {
380 getListView().setVisibility(View.GONE);
Hugo Hudson157dde12011-07-21 23:28:13 +0100381 }
Flavio Lerda178eeeb2011-07-11 19:51:40 +0100382
Flavio Lerda178eeeb2011-07-11 19:51:40 +0100383 ListView historyList = (ListView) findViewById(R.id.history);
384 historyList.setAdapter(
385 new CallDetailHistoryAdapter(this, mInflater, mCallTypeHelper, details));
Daniel Lehmann362654a2011-07-17 14:16:47 -0700386 loadContactPhotos(photoUri);
Flavio Lerda178eeeb2011-07-11 19:51:40 +0100387 }
388
389 /** Return the phone call details for a given call log URI. */
390 private PhoneCallDetails getPhoneCallDetailsForUri(Uri callUri) {
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800391 ContentResolver resolver = getContentResolver();
392 Cursor callCursor = resolver.query(callUri, CALL_LOG_PROJECTION, null, null, null);
393 try {
Flavio Lerda178eeeb2011-07-11 19:51:40 +0100394 if (callCursor == null || !callCursor.moveToFirst()) {
395 throw new IllegalArgumentException("Cannot find content: " + callUri);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800396 }
Flavio Lerda178eeeb2011-07-11 19:51:40 +0100397
398 // Read call log specifics.
399 String number = callCursor.getString(NUMBER_COLUMN_INDEX);
400 long date = callCursor.getLong(DATE_COLUMN_INDEX);
401 long duration = callCursor.getLong(DURATION_COLUMN_INDEX);
402 int callType = callCursor.getInt(CALL_TYPE_COLUMN_INDEX);
403 String countryIso = callCursor.getString(COUNTRY_ISO_COLUMN_INDEX);
404 if (TextUtils.isEmpty(countryIso)) {
405 countryIso = mDefaultCountryIso;
406 }
407
408 // Formatted phone number.
409 final CharSequence numberText;
410 // Read contact specifics.
411 CharSequence nameText = "";
412 int numberType = 0;
413 CharSequence numberLabel = "";
414 long personId = -1L;
Daniel Lehmann362654a2011-07-17 14:16:47 -0700415 Uri photoUri = null;
Flavio Lerda178eeeb2011-07-11 19:51:40 +0100416 // If this is not a regular number, there is no point in looking it up in the contacts.
417 if (!mPhoneNumberHelper.canPlaceCallsTo(number)) {
418 numberText = mPhoneNumberHelper.getDisplayNumber(number, null);
419 } else {
420 // Perform a reverse-phonebook lookup to find the contact details.
421 Uri phoneUri = Uri.withAppendedPath(PhoneLookup.CONTENT_FILTER_URI,
422 Uri.encode(number));
423 Cursor phonesCursor = resolver.query(phoneUri, PHONES_PROJECTION, null, null, null);
424 String candidateNumberText = number;
425 try {
426 if (phonesCursor != null && phonesCursor.moveToFirst()) {
427 personId = phonesCursor.getLong(COLUMN_INDEX_ID);
428 nameText = phonesCursor.getString(COLUMN_INDEX_NAME);
Daniel Lehmann362654a2011-07-17 14:16:47 -0700429 String photoUriString = phonesCursor.getString(COLUMN_INDEX_PHOTO_URI);
430 photoUri = photoUriString == null ? null : Uri.parse(photoUriString);
Flavio Lerda178eeeb2011-07-11 19:51:40 +0100431 candidateNumberText = PhoneNumberUtils.formatNumber(
432 phonesCursor.getString(COLUMN_INDEX_NUMBER),
433 phonesCursor.getString(COLUMN_INDEX_NORMALIZED_NUMBER),
434 countryIso);
435 numberType = phonesCursor.getInt(COLUMN_INDEX_TYPE);
436 numberLabel = phonesCursor.getString(COLUMN_INDEX_LABEL);
437 } else {
438 // We could not find this contact in the contacts, just format the phone
439 // number as best as we can. All the other fields will have their default
440 // values.
441 candidateNumberText =
442 PhoneNumberUtils.formatNumber(number, countryIso);
443 }
444 } finally {
445 if (phonesCursor != null) phonesCursor.close();
446 numberText = candidateNumberText;
447 }
448 }
Flavio Lerda9c466372011-07-19 17:38:17 +0100449 return new PhoneCallDetails(number, numberText, countryIso,
Flavio Lerdacb805e82011-07-18 10:31:44 +0100450 new int[]{ callType }, date, duration,
Daniel Lehmann362654a2011-07-17 14:16:47 -0700451 nameText, numberType, numberLabel, personId, photoUri);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800452 } finally {
453 if (callCursor != null) {
454 callCursor.close();
455 }
456 }
457 }
458
Flavio Lerda9cafe472011-06-08 14:06:13 +0100459 /** Load the contact photos and places them in the corresponding views. */
Daniel Lehmann362654a2011-07-17 14:16:47 -0700460 private void loadContactPhotos(Uri photoUri) {
461 mContactPhotoManager.loadPhoto(mContactBackgroundView, photoUri);
Flavio Lerda9cafe472011-06-08 14:06:13 +0100462 }
463
Flavio Lerda696e8132011-07-05 19:00:23 +0100464 private String getVoicemailNumber() {
465 TelephonyManager telephonyManager =
466 (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
467 return telephonyManager.getVoiceMailNumber();
468 }
469
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800470 static final class ViewEntry {
Flavio Lerdaa8956882011-07-09 21:34:38 +0100471 public final int icon;
472 public final String text;
473 public final Intent intent;
474 public final View.OnClickListener action;
475
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800476 public String label = null;
477 public String number = null;
Dmitri Plotnikovd0d776d2009-08-19 17:38:04 -0700478
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800479 public ViewEntry(int icon, String text, Intent intent) {
480 this.icon = icon;
481 this.text = text;
482 this.intent = intent;
Flavio Lerdaa8956882011-07-09 21:34:38 +0100483 this.action = null;
484 }
485
486 public ViewEntry(int icon, String text, View.OnClickListener listener) {
487 this.icon = icon;
488 this.text = text;
489 this.intent = null;
490 this.action = listener;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800491 }
492 }
493
494 static final class ViewAdapter extends BaseAdapter {
Dmitri Plotnikovd0d776d2009-08-19 17:38:04 -0700495
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800496 private final List<ViewEntry> mActions;
Dmitri Plotnikovd0d776d2009-08-19 17:38:04 -0700497
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800498 private final LayoutInflater mInflater;
Dmitri Plotnikovd0d776d2009-08-19 17:38:04 -0700499
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800500 public ViewAdapter(Context context, List<ViewEntry> actions) {
501 mActions = actions;
502 mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
503 }
Dmitri Plotnikovd0d776d2009-08-19 17:38:04 -0700504
Flavio Lerda9cafe472011-06-08 14:06:13 +0100505 @Override
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800506 public int getCount() {
507 return mActions.size();
508 }
509
Flavio Lerda9cafe472011-06-08 14:06:13 +0100510 @Override
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800511 public Object getItem(int position) {
512 return mActions.get(position);
513 }
514
Flavio Lerda9cafe472011-06-08 14:06:13 +0100515 @Override
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800516 public long getItemId(int position) {
517 return position;
518 }
Dmitri Plotnikovd0d776d2009-08-19 17:38:04 -0700519
Flavio Lerda9cafe472011-06-08 14:06:13 +0100520 @Override
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800521 public View getView(int position, View convertView, ViewGroup parent) {
522 // Make sure we have a valid convertView to start with
523 if (convertView == null) {
524 convertView = mInflater.inflate(R.layout.call_detail_list_item, parent, false);
525 }
526
527 // Fill action with icon and text.
528 ViewEntry entry = mActions.get(position);
529 convertView.setTag(entry);
Dmitri Plotnikovd0d776d2009-08-19 17:38:04 -0700530
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800531 ImageView icon = (ImageView) convertView.findViewById(R.id.icon);
532 TextView text = (TextView) convertView.findViewById(android.R.id.text1);
533
534 icon.setImageResource(entry.icon);
535 text.setText(entry.text);
536
537 View line2 = convertView.findViewById(R.id.line2);
538 boolean numberEmpty = TextUtils.isEmpty(entry.number);
539 boolean labelEmpty = TextUtils.isEmpty(entry.label) || numberEmpty;
540 if (labelEmpty && numberEmpty) {
541 line2.setVisibility(View.GONE);
542 } else {
543 line2.setVisibility(View.VISIBLE);
Dmitri Plotnikovd0d776d2009-08-19 17:38:04 -0700544
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800545 TextView label = (TextView) convertView.findViewById(R.id.label);
546 if (labelEmpty) {
547 label.setVisibility(View.GONE);
548 } else {
549 label.setText(entry.label);
550 label.setVisibility(View.VISIBLE);
551 }
552
553 TextView number = (TextView) convertView.findViewById(R.id.number);
554 number.setText(entry.number);
555 }
Dmitri Plotnikovd0d776d2009-08-19 17:38:04 -0700556
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800557 return convertView;
558 }
559 }
Dmitri Plotnikovd0d776d2009-08-19 17:38:04 -0700560
Flavio Lerda9cafe472011-06-08 14:06:13 +0100561 @Override
562 public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800563 // Handle passing action off to correct handler.
564 if (view.getTag() instanceof ViewEntry) {
565 ViewEntry entry = (ViewEntry) view.getTag();
566 if (entry.intent != null) {
567 startActivity(entry.intent);
Flavio Lerdaa8956882011-07-09 21:34:38 +0100568 } else if (entry.action != null) {
569 entry.action.onClick(view);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800570 }
571 }
Dmitri Plotnikovd0d776d2009-08-19 17:38:04 -0700572 }
Dmitri Plotnikov8e86b752010-02-22 17:47:57 -0800573
574 @Override
575 public void startSearch(String initialQuery, boolean selectInitialQuery, Bundle appSearchData,
576 boolean globalSearch) {
577 if (globalSearch) {
578 super.startSearch(initialQuery, selectInitialQuery, appSearchData, globalSearch);
579 } else {
580 ContactsSearchManager.startSearch(this, initialQuery);
581 }
582 }
Debashish Chatterjee0cb82242011-07-19 19:29:37 +0100583
584 protected void updateVoicemailStatusMessage(Cursor statusCursor) {
585 if (statusCursor == null) {
586 mStatusMessageView.setVisibility(View.GONE);
587 return;
588 }
589 final StatusMessage message = getStatusMessage(statusCursor);
590 if (message == null || !message.showInCallDetails()) {
591 mStatusMessageView.setVisibility(View.GONE);
592 return;
593 }
594
595 mStatusMessageView.setVisibility(View.VISIBLE);
596 mStatusMessageText.setText(message.callDetailsMessageId);
597 if (message.actionMessageId != -1) {
598 mStatusMessageAction.setText(message.actionMessageId);
599 }
600 if (message.actionUri != null) {
601 mStatusMessageAction.setClickable(true);
602 mStatusMessageAction.setOnClickListener(new View.OnClickListener() {
603 @Override
604 public void onClick(View v) {
605 startActivity(new Intent(Intent.ACTION_VIEW, message.actionUri));
606 }
607 });
608 } else {
609 mStatusMessageAction.setClickable(false);
610 }
611 }
612
613 private StatusMessage getStatusMessage(Cursor statusCursor) {
614 List<StatusMessage> messages = mVoicemailStatusHelper.getStatusMessages(statusCursor);
615 Log.d(TAG, "Num status messages: " + messages.size());
616 if (messages.size() == 0) {
617 return null;
618 }
619 // There can only be a single status message per source package, so num of messages can
620 // at most be 1.
621 if (messages.size() > 1) {
622 Log.w(TAG, String.format("Expected 1, found (%d) num of status messages." +
623 " Will use the first one.", messages.size()));
624 }
625 return messages.get(0);
626 }
Flavio Lerda94d7bab2011-07-22 16:52:34 +0100627
628 @Override
629 public boolean onCreateOptionsMenu(Menu menu) {
630 getMenuInflater().inflate(R.menu.call_details_options, menu);
Hugo Hudsonc2f09c32011-07-30 16:31:28 +0100631 return super.onCreateOptionsMenu(menu);
Flavio Lerda94d7bab2011-07-22 16:52:34 +0100632 }
633
634 @Override
635 public boolean onPrepareOptionsMenu(Menu menu) {
636 // This action deletes all elements in the group from the call log.
637 // We don't have this action for voicemails, because you can just use the trash button.
Hugo Hudsonc2f09c32011-07-30 16:31:28 +0100638 menu.findItem(R.id.menu_remove_from_call_log).setVisible(!hasVoicemail());
639 menu.findItem(R.id.menu_edit_number_before_call).setVisible(mHasEditNumberBeforeCall);
640 menu.findItem(R.id.menu_trash).setVisible(hasVoicemail());
641 menu.findItem(R.id.menu_share_voicemail).setVisible(hasVoicemail());
642 return super.onPrepareOptionsMenu(menu);
Flavio Lerda94d7bab2011-07-22 16:52:34 +0100643 }
644
645 @Override
646 public boolean onMenuItemSelected(int featureId, MenuItem item) {
647 switch (item.getItemId()) {
Flavio Lerdad44e83a2011-07-24 23:10:17 +0100648 case android.R.id.home: {
649 onHomeSelected();
650 return true;
651 }
652
Hugo Hudsonc2f09c32011-07-30 16:31:28 +0100653 // All the options menu items are handled by onMenu... methods.
Flavio Lerda94d7bab2011-07-22 16:52:34 +0100654 default:
655 throw new IllegalArgumentException();
656 }
657 }
Flavio Lerdad44e83a2011-07-24 23:10:17 +0100658
Hugo Hudsonc2f09c32011-07-30 16:31:28 +0100659 public void onMenuRemoveFromCallLog(MenuItem menuItem) {
660 StringBuilder callIds = new StringBuilder();
661 for (Uri callUri : getCallLogEntryUris()) {
662 if (callIds.length() != 0) {
663 callIds.append(",");
664 }
665 callIds.append(ContentUris.parseId(callUri));
666 }
667
668 getContentResolver().delete(Calls.CONTENT_URI_WITH_VOICEMAIL,
669 Calls._ID + " IN (" + callIds + ")", null);
670 // Also close the activity.
671 finish();
672 }
673
674 public void onMenuEditNumberBeforeCall(MenuItem menuItem) {
675 startActivity(new Intent(Intent.ACTION_DIAL, mPhoneNumberHelper.getCallUri(mNumber)));
676 }
677
678 public void onMenuShareVoicemail(MenuItem menuItem) {
679 Log.w(TAG, "onMenuShareVoicemail not yet implemented");
680 }
681
682 public void onMenuTrashVoicemail(MenuItem menuItem) {
Hugo Hudson7b02fde2011-08-02 20:56:48 +0100683 getContentResolver().delete(getVoicemailUri(), null, null);
684 finish();
Hugo Hudsonc2f09c32011-07-30 16:31:28 +0100685 }
686
Flavio Lerdad44e83a2011-07-24 23:10:17 +0100687 private void configureActionBar() {
688 ActionBar actionBar = getActionBar();
689 if (actionBar != null) {
Hugo Hudsonc2f09c32011-07-30 16:31:28 +0100690 actionBar.setDisplayOptions(ActionBar.DISPLAY_HOME_AS_UP | ActionBar.DISPLAY_SHOW_HOME);
Flavio Lerdad44e83a2011-07-24 23:10:17 +0100691 }
692 }
693
694 /** Invoked when the user presses the home button in the action bar. */
695 private void onHomeSelected() {
696 Intent intent = new Intent(Intent.ACTION_VIEW, Calls.CONTENT_URI);
697 // This will open the call log even if the detail view has been opened directly.
698 intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
699 startActivity(intent);
700 finish();
701 }
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800702}