The Android Open Source Project | 7aa0e4c | 2009-03-03 19:32:21 -0800 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package com.android.contacts; |
| 18 | |
Flavio Lerda | 9a208cc | 2011-07-12 21:05:47 +0100 | [diff] [blame] | 19 | import com.android.contacts.calllog.CallDetailHistoryAdapter; |
| 20 | import com.android.contacts.calllog.CallTypeHelper; |
Flavio Lerda | 178eeeb | 2011-07-11 19:51:40 +0100 | [diff] [blame] | 21 | import com.android.contacts.calllog.PhoneNumberHelper; |
Hugo Hudson | b002f51 | 2011-07-15 17:41:12 +0100 | [diff] [blame] | 22 | import com.android.contacts.voicemail.VoicemailPlaybackFragment; |
Debashish Chatterjee | 0cb8224 | 2011-07-19 19:29:37 +0100 | [diff] [blame] | 23 | import com.android.contacts.voicemail.VoicemailStatusHelper; |
| 24 | import com.android.contacts.voicemail.VoicemailStatusHelper.StatusMessage; |
| 25 | import com.android.contacts.voicemail.VoicemailStatusHelperImpl; |
Amith Yamasani | 8bbe2f2 | 2009-03-24 21:24:12 -0700 | [diff] [blame] | 26 | |
Hugo Hudson | b002f51 | 2011-07-15 17:41:12 +0100 | [diff] [blame] | 27 | import android.app.FragmentManager; |
The Android Open Source Project | 7aa0e4c | 2009-03-03 19:32:21 -0800 | [diff] [blame] | 28 | import android.app.ListActivity; |
| 29 | import android.content.ContentResolver; |
| 30 | import android.content.ContentUris; |
| 31 | import android.content.Context; |
| 32 | import android.content.Intent; |
| 33 | import android.content.res.Resources; |
| 34 | import android.database.Cursor; |
| 35 | import android.net.Uri; |
| 36 | import android.os.Bundle; |
| 37 | import android.provider.CallLog; |
The Android Open Source Project | 7aa0e4c | 2009-03-03 19:32:21 -0800 | [diff] [blame] | 38 | import android.provider.CallLog.Calls; |
Flavio Lerda | 9cafe47 | 2011-06-08 14:06:13 +0100 | [diff] [blame] | 39 | import android.provider.Contacts.Intents.Insert; |
Dmitri Plotnikov | d0d776d | 2009-08-19 17:38:04 -0700 | [diff] [blame] | 40 | import android.provider.ContactsContract.Contacts; |
| 41 | import android.provider.ContactsContract.PhoneLookup; |
The Android Open Source Project | 7aa0e4c | 2009-03-03 19:32:21 -0800 | [diff] [blame] | 42 | import android.telephony.PhoneNumberUtils; |
| 43 | import android.telephony.TelephonyManager; |
| 44 | import android.text.TextUtils; |
Flavio Lerda | 178eeeb | 2011-07-11 19:51:40 +0100 | [diff] [blame] | 45 | import android.util.Log; |
The Android Open Source Project | 7aa0e4c | 2009-03-03 19:32:21 -0800 | [diff] [blame] | 46 | import android.view.KeyEvent; |
| 47 | import android.view.LayoutInflater; |
| 48 | import android.view.View; |
| 49 | import android.view.ViewGroup; |
| 50 | import android.widget.AdapterView; |
| 51 | import android.widget.BaseAdapter; |
| 52 | import android.widget.ImageView; |
Flavio Lerda | 9a208cc | 2011-07-12 21:05:47 +0100 | [diff] [blame] | 53 | import android.widget.ListView; |
The Android Open Source Project | 7aa0e4c | 2009-03-03 19:32:21 -0800 | [diff] [blame] | 54 | import android.widget.TextView; |
| 55 | import android.widget.Toast; |
| 56 | |
| 57 | import java.util.ArrayList; |
| 58 | import java.util.List; |
| 59 | |
| 60 | /** |
| 61 | * Displays the details of a specific call log entry. |
Flavio Lerda | 178eeeb | 2011-07-11 19:51:40 +0100 | [diff] [blame] | 62 | * <p> |
| 63 | * This activity can be either started with the URI of a single call log entry, or with the |
| 64 | * {@link #EXTRA_CALL_LOG_IDS} extra to specify a group of call log entries. |
The Android Open Source Project | 7aa0e4c | 2009-03-03 19:32:21 -0800 | [diff] [blame] | 65 | */ |
| 66 | public class CallDetailActivity extends ListActivity implements |
| 67 | AdapterView.OnItemClickListener { |
| 68 | private static final String TAG = "CallDetail"; |
| 69 | |
Flavio Lerda | 178eeeb | 2011-07-11 19:51:40 +0100 | [diff] [blame] | 70 | /** A long array extra containing ids of call log entries to display. */ |
Hugo Hudson | b002f51 | 2011-07-15 17:41:12 +0100 | [diff] [blame] | 71 | public static final String EXTRA_CALL_LOG_IDS = "EXTRA_CALL_LOG_IDS"; |
| 72 | /** If we are started with a voicemail, we'll find the uri to play with this extra. */ |
| 73 | public static final String EXTRA_VOICEMAIL_URI = "EXTRA_VOICEMAIL_URI"; |
| 74 | /** If we should immediately start playback of the voicemail, this extra will be set to true. */ |
| 75 | public static final String EXTRA_VOICEMAIL_START_PLAYBACK = "EXTRA_VOICEMAIL_START_PLAYBACK"; |
Flavio Lerda | 178eeeb | 2011-07-11 19:51:40 +0100 | [diff] [blame] | 76 | |
Flavio Lerda | a024c3f | 2011-06-10 10:47:07 +0100 | [diff] [blame] | 77 | /** The views representing the details of a phone call. */ |
Flavio Lerda | afb93bb | 2011-07-05 14:46:10 +0100 | [diff] [blame] | 78 | private PhoneCallDetailsViews mPhoneCallDetailsViews; |
Flavio Lerda | 9a208cc | 2011-07-12 21:05:47 +0100 | [diff] [blame] | 79 | private CallTypeHelper mCallTypeHelper; |
Flavio Lerda | 178eeeb | 2011-07-11 19:51:40 +0100 | [diff] [blame] | 80 | private PhoneNumberHelper mPhoneNumberHelper; |
Flavio Lerda | afb93bb | 2011-07-05 14:46:10 +0100 | [diff] [blame] | 81 | private PhoneCallDetailsHelper mPhoneCallDetailsHelper; |
Flavio Lerda | fb63c43 | 2011-07-07 17:16:53 +0100 | [diff] [blame] | 82 | private View mHomeActionView; |
| 83 | private ImageView mMainActionView; |
Flavio Lerda | 9cafe47 | 2011-06-08 14:06:13 +0100 | [diff] [blame] | 84 | private ImageView mContactBackgroundView; |
The Android Open Source Project | 7aa0e4c | 2009-03-03 19:32:21 -0800 | [diff] [blame] | 85 | |
| 86 | private String mNumber = null; |
Bai Tao | 09eb04f | 2010-09-01 15:34:16 +0800 | [diff] [blame] | 87 | private String mDefaultCountryIso; |
Dmitri Plotnikov | d0d776d | 2009-08-19 17:38:04 -0700 | [diff] [blame] | 88 | |
The Android Open Source Project | 7aa0e4c | 2009-03-03 19:32:21 -0800 | [diff] [blame] | 89 | /* package */ LayoutInflater mInflater; |
| 90 | /* package */ Resources mResources; |
Flavio Lerda | 9cafe47 | 2011-06-08 14:06:13 +0100 | [diff] [blame] | 91 | /** Helper to load contact photos. */ |
| 92 | private ContactPhotoManager mContactPhotoManager; |
Debashish Chatterjee | 0cb8224 | 2011-07-19 19:29:37 +0100 | [diff] [blame] | 93 | /** Helper to make async queries to content resolver. */ |
| 94 | private CallDetailActivityQueryHandler mAsyncQueryHandler; |
| 95 | /** Helper to get voicemail status messages. */ |
| 96 | private VoicemailStatusHelper mVoicemailStatusHelper; |
| 97 | // Views related to voicemail status message. |
| 98 | private View mStatusMessageView; |
| 99 | private TextView mStatusMessageText; |
| 100 | private TextView mStatusMessageAction; |
Dmitri Plotnikov | d0d776d | 2009-08-19 17:38:04 -0700 | [diff] [blame] | 101 | |
The Android Open Source Project | 7aa0e4c | 2009-03-03 19:32:21 -0800 | [diff] [blame] | 102 | static final String[] CALL_LOG_PROJECTION = new String[] { |
| 103 | CallLog.Calls.DATE, |
| 104 | CallLog.Calls.DURATION, |
| 105 | CallLog.Calls.NUMBER, |
| 106 | CallLog.Calls.TYPE, |
Bai Tao | 09eb04f | 2010-09-01 15:34:16 +0800 | [diff] [blame] | 107 | CallLog.Calls.COUNTRY_ISO, |
The Android Open Source Project | 7aa0e4c | 2009-03-03 19:32:21 -0800 | [diff] [blame] | 108 | }; |
Dmitri Plotnikov | d0d776d | 2009-08-19 17:38:04 -0700 | [diff] [blame] | 109 | |
The Android Open Source Project | 7aa0e4c | 2009-03-03 19:32:21 -0800 | [diff] [blame] | 110 | static final int DATE_COLUMN_INDEX = 0; |
| 111 | static final int DURATION_COLUMN_INDEX = 1; |
| 112 | static final int NUMBER_COLUMN_INDEX = 2; |
| 113 | static final int CALL_TYPE_COLUMN_INDEX = 3; |
Bai Tao | 09eb04f | 2010-09-01 15:34:16 +0800 | [diff] [blame] | 114 | static final int COUNTRY_ISO_COLUMN_INDEX = 4; |
Dmitri Plotnikov | d0d776d | 2009-08-19 17:38:04 -0700 | [diff] [blame] | 115 | |
The Android Open Source Project | 7aa0e4c | 2009-03-03 19:32:21 -0800 | [diff] [blame] | 116 | static final String[] PHONES_PROJECTION = new String[] { |
Dmitri Plotnikov | d0d776d | 2009-08-19 17:38:04 -0700 | [diff] [blame] | 117 | PhoneLookup._ID, |
| 118 | PhoneLookup.DISPLAY_NAME, |
| 119 | PhoneLookup.TYPE, |
| 120 | PhoneLookup.LABEL, |
| 121 | PhoneLookup.NUMBER, |
Bai Tao | 09eb04f | 2010-09-01 15:34:16 +0800 | [diff] [blame] | 122 | PhoneLookup.NORMALIZED_NUMBER, |
Daniel Lehmann | 362654a | 2011-07-17 14:16:47 -0700 | [diff] [blame] | 123 | PhoneLookup.PHOTO_URI, |
The Android Open Source Project | 7aa0e4c | 2009-03-03 19:32:21 -0800 | [diff] [blame] | 124 | }; |
| 125 | static final int COLUMN_INDEX_ID = 0; |
| 126 | static final int COLUMN_INDEX_NAME = 1; |
| 127 | static final int COLUMN_INDEX_TYPE = 2; |
| 128 | static final int COLUMN_INDEX_LABEL = 3; |
| 129 | static final int COLUMN_INDEX_NUMBER = 4; |
Bai Tao | 09eb04f | 2010-09-01 15:34:16 +0800 | [diff] [blame] | 130 | static final int COLUMN_INDEX_NORMALIZED_NUMBER = 5; |
Daniel Lehmann | 362654a | 2011-07-17 14:16:47 -0700 | [diff] [blame] | 131 | static final int COLUMN_INDEX_PHOTO_URI = 6; |
The Android Open Source Project | 7aa0e4c | 2009-03-03 19:32:21 -0800 | [diff] [blame] | 132 | |
| 133 | @Override |
| 134 | protected void onCreate(Bundle icicle) { |
| 135 | super.onCreate(icicle); |
| 136 | |
| 137 | setContentView(R.layout.call_detail); |
| 138 | |
| 139 | mInflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE); |
| 140 | mResources = getResources(); |
Dmitri Plotnikov | d0d776d | 2009-08-19 17:38:04 -0700 | [diff] [blame] | 141 | |
Flavio Lerda | 696e813 | 2011-07-05 19:00:23 +0100 | [diff] [blame] | 142 | mPhoneCallDetailsViews = PhoneCallDetailsViews.fromView(getWindow().getDecorView()); |
Flavio Lerda | 9a208cc | 2011-07-12 21:05:47 +0100 | [diff] [blame] | 143 | mCallTypeHelper = new CallTypeHelper(getResources(), |
Flavio Lerda | 693d1f8 | 2011-07-13 18:20:46 +0100 | [diff] [blame] | 144 | getResources().getDrawable(R.drawable.ic_call_incoming_holo_dark), |
| 145 | getResources().getDrawable(R.drawable.ic_call_outgoing_holo_dark), |
| 146 | getResources().getDrawable(R.drawable.ic_call_missed_holo_dark), |
| 147 | getResources().getDrawable(R.drawable.ic_call_voicemail_holo_dark)); |
Flavio Lerda | 178eeeb | 2011-07-11 19:51:40 +0100 | [diff] [blame] | 148 | mPhoneNumberHelper = new PhoneNumberHelper(mResources, getVoicemailNumber()); |
| 149 | mPhoneCallDetailsHelper = new PhoneCallDetailsHelper(this, mResources, mCallTypeHelper, |
| 150 | mPhoneNumberHelper); |
Debashish Chatterjee | 0cb8224 | 2011-07-19 19:29:37 +0100 | [diff] [blame] | 151 | mVoicemailStatusHelper = new VoicemailStatusHelperImpl(); |
| 152 | mAsyncQueryHandler = new CallDetailActivityQueryHandler(this); |
| 153 | mStatusMessageView = findViewById(R.id.voicemail_status); |
| 154 | mStatusMessageText = (TextView) findViewById(R.id.voicemail_status_message); |
| 155 | mStatusMessageAction = (TextView) findViewById(R.id.voicemail_status_action); |
Flavio Lerda | fb63c43 | 2011-07-07 17:16:53 +0100 | [diff] [blame] | 156 | mHomeActionView = findViewById(R.id.action_bar_home); |
| 157 | mMainActionView = (ImageView) findViewById(R.id.main_action); |
Flavio Lerda | 9cafe47 | 2011-06-08 14:06:13 +0100 | [diff] [blame] | 158 | mContactBackgroundView = (ImageView) findViewById(R.id.contact_background); |
Bai Tao | 09eb04f | 2010-09-01 15:34:16 +0800 | [diff] [blame] | 159 | mDefaultCountryIso = ContactsUtils.getCurrentCountryIso(this); |
Flavio Lerda | 9cafe47 | 2011-06-08 14:06:13 +0100 | [diff] [blame] | 160 | mContactPhotoManager = ContactPhotoManager.getInstance(this); |
The Android Open Source Project | 7aa0e4c | 2009-03-03 19:32:21 -0800 | [diff] [blame] | 161 | getListView().setOnItemClickListener(this); |
Flavio Lerda | fb63c43 | 2011-07-07 17:16:53 +0100 | [diff] [blame] | 162 | mHomeActionView.setOnClickListener(new View.OnClickListener() { |
| 163 | @Override |
| 164 | public void onClick(View v) { |
| 165 | // We want this to start the call log if this activity was not started from the |
| 166 | // call log itself. |
| 167 | CallDetailActivity.this.finish(); |
| 168 | } |
| 169 | }); |
The Android Open Source Project | 7aa0e4c | 2009-03-03 19:32:21 -0800 | [diff] [blame] | 170 | } |
Dmitri Plotnikov | d0d776d | 2009-08-19 17:38:04 -0700 | [diff] [blame] | 171 | |
Debashish Chatterjee | 0cb8224 | 2011-07-19 19:29:37 +0100 | [diff] [blame] | 172 | |
The Android Open Source Project | 7aa0e4c | 2009-03-03 19:32:21 -0800 | [diff] [blame] | 173 | @Override |
| 174 | public void onResume() { |
| 175 | super.onResume(); |
Flavio Lerda | 178eeeb | 2011-07-11 19:51:40 +0100 | [diff] [blame] | 176 | updateData(getCallLogEntryUris()); |
Hugo Hudson | 757aa55 | 2011-07-20 01:15:25 +0100 | [diff] [blame] | 177 | optionallyHandleVoicemail(); |
Hugo Hudson | b002f51 | 2011-07-15 17:41:12 +0100 | [diff] [blame] | 178 | } |
| 179 | |
| 180 | /** |
| 181 | * Handle voicemail playback or hide voicemail ui. |
| 182 | * <p> |
| 183 | * If the Intent used to start this Activity contains the suitable extras, then start voicemail |
| 184 | * playback. If it doesn't, then hide the voicemail ui. |
| 185 | */ |
Hugo Hudson | 757aa55 | 2011-07-20 01:15:25 +0100 | [diff] [blame] | 186 | private void optionallyHandleVoicemail() { |
| 187 | Uri voicemailUri = getIntent().getParcelableExtra(EXTRA_VOICEMAIL_URI); |
Hugo Hudson | b002f51 | 2011-07-15 17:41:12 +0100 | [diff] [blame] | 188 | FragmentManager manager = getFragmentManager(); |
| 189 | VoicemailPlaybackFragment fragment = (VoicemailPlaybackFragment) manager.findFragmentById( |
| 190 | R.id.voicemail_playback_fragment); |
Hugo Hudson | 757aa55 | 2011-07-20 01:15:25 +0100 | [diff] [blame] | 191 | if (voicemailUri != null) { |
| 192 | // Has voicemail uri: leave the fragment visible. Optionally start the playback. |
| 193 | // Do a query to fetch the voicemail status messages. |
| 194 | boolean startPlayback = getIntent().getBooleanExtra( |
Hugo Hudson | b002f51 | 2011-07-15 17:41:12 +0100 | [diff] [blame] | 195 | EXTRA_VOICEMAIL_START_PLAYBACK, false); |
| 196 | fragment.setVoicemailUri(voicemailUri, startPlayback); |
Hugo Hudson | 757aa55 | 2011-07-20 01:15:25 +0100 | [diff] [blame] | 197 | mAsyncQueryHandler.startVoicemailStatusQuery(voicemailUri); |
| 198 | } else { |
| 199 | // No voicemail uri: hide the voicemail fragment and the status view. |
| 200 | manager.beginTransaction().hide(fragment).commit(); |
| 201 | mStatusMessageView.setVisibility(View.GONE); |
Hugo Hudson | b002f51 | 2011-07-15 17:41:12 +0100 | [diff] [blame] | 202 | } |
Flavio Lerda | 178eeeb | 2011-07-11 19:51:40 +0100 | [diff] [blame] | 203 | } |
| 204 | |
| 205 | /** |
| 206 | * Returns the list of URIs to show. |
| 207 | * <p> |
| 208 | * There are two ways the URIs can be provided to the activity: as the data on the intent, or as |
| 209 | * a list of ids in the call log added as an extra on the URI. |
| 210 | * <p> |
| 211 | * If both are available, the data on the intent takes precedence. |
| 212 | */ |
| 213 | private Uri[] getCallLogEntryUris() { |
| 214 | Uri uri = getIntent().getData(); |
| 215 | if (uri != null) { |
| 216 | // If there is a data on the intent, it takes precedence over the extra. |
| 217 | return new Uri[]{ uri }; |
| 218 | } |
| 219 | long[] ids = getIntent().getLongArrayExtra(EXTRA_CALL_LOG_IDS); |
| 220 | Uri[] uris = new Uri[ids.length]; |
| 221 | for (int index = 0; index < ids.length; ++index) { |
| 222 | uris[index] = ContentUris.withAppendedId(Calls.CONTENT_URI_WITH_VOICEMAIL, ids[index]); |
| 223 | } |
| 224 | return uris; |
The Android Open Source Project | 7aa0e4c | 2009-03-03 19:32:21 -0800 | [diff] [blame] | 225 | } |
| 226 | |
| 227 | @Override |
| 228 | public boolean onKeyDown(int keyCode, KeyEvent event) { |
| 229 | switch (keyCode) { |
| 230 | case KeyEvent.KEYCODE_CALL: { |
| 231 | // Make sure phone isn't already busy before starting direct call |
| 232 | TelephonyManager tm = (TelephonyManager) |
| 233 | getSystemService(Context.TELEPHONY_SERVICE); |
| 234 | if (tm.getCallState() == TelephonyManager.CALL_STATE_IDLE) { |
| 235 | Intent callIntent = new Intent(Intent.ACTION_CALL_PRIVILEGED, |
| 236 | Uri.fromParts("tel", mNumber, null)); |
| 237 | startActivity(callIntent); |
| 238 | return true; |
| 239 | } |
| 240 | } |
| 241 | } |
Dmitri Plotnikov | d0d776d | 2009-08-19 17:38:04 -0700 | [diff] [blame] | 242 | |
The Android Open Source Project | 7aa0e4c | 2009-03-03 19:32:21 -0800 | [diff] [blame] | 243 | return super.onKeyDown(keyCode, event); |
| 244 | } |
Dmitri Plotnikov | d0d776d | 2009-08-19 17:38:04 -0700 | [diff] [blame] | 245 | |
The Android Open Source Project | 7aa0e4c | 2009-03-03 19:32:21 -0800 | [diff] [blame] | 246 | /** |
| 247 | * Update user interface with details of given call. |
Dmitri Plotnikov | d0d776d | 2009-08-19 17:38:04 -0700 | [diff] [blame] | 248 | * |
Flavio Lerda | 178eeeb | 2011-07-11 19:51:40 +0100 | [diff] [blame] | 249 | * @param callUris URIs into {@link CallLog.Calls} of the calls to be displayed |
The Android Open Source Project | 7aa0e4c | 2009-03-03 19:32:21 -0800 | [diff] [blame] | 250 | */ |
Flavio Lerda | 178eeeb | 2011-07-11 19:51:40 +0100 | [diff] [blame] | 251 | private void updateData(final Uri... callUris) { |
| 252 | // TODO: All phone calls correspond to the same person, so we can make a single lookup. |
| 253 | final int numCalls = callUris.length; |
| 254 | final PhoneCallDetails[] details = new PhoneCallDetails[numCalls]; |
| 255 | try { |
| 256 | for (int index = 0; index < numCalls; ++index) { |
| 257 | details[index] = getPhoneCallDetailsForUri(callUris[index]); |
| 258 | } |
| 259 | } catch (IllegalArgumentException e) { |
| 260 | // Something went wrong reading in our primary data, so we're going to |
| 261 | // bail out and show error to users. |
| 262 | Log.w(TAG, "invalid URI starting call details", e); |
| 263 | Toast.makeText(this, R.string.toast_call_detail_error, |
| 264 | Toast.LENGTH_SHORT).show(); |
| 265 | finish(); |
| 266 | return; |
| 267 | } |
| 268 | |
| 269 | // We know that all calls are from the same number and the same contact, so pick the first. |
| 270 | mNumber = details[0].number.toString(); |
| 271 | final long personId = details[0].personId; |
Daniel Lehmann | 362654a | 2011-07-17 14:16:47 -0700 | [diff] [blame] | 272 | final Uri photoUri = details[0].photoUri; |
Flavio Lerda | 178eeeb | 2011-07-11 19:51:40 +0100 | [diff] [blame] | 273 | |
| 274 | // Set the details header, based on the first phone call. |
| 275 | mPhoneCallDetailsHelper.setPhoneCallDetails(mPhoneCallDetailsViews, |
Flavio Lerda | 7692190 | 2011-07-13 21:11:51 +0100 | [diff] [blame] | 276 | details[0], false, false); |
Flavio Lerda | 178eeeb | 2011-07-11 19:51:40 +0100 | [diff] [blame] | 277 | |
Flavio Lerda | cfff16d | 2011-07-12 23:54:40 +0100 | [diff] [blame] | 278 | // Cache the details about the phone number. |
| 279 | final Uri numberCallUri = mPhoneNumberHelper.getCallUri(mNumber); |
| 280 | final boolean canPlaceCallsTo = mPhoneNumberHelper.canPlaceCallsTo(mNumber); |
| 281 | final boolean isVoicemailNumber = mPhoneNumberHelper.isVoicemailNumber(mNumber); |
| 282 | final boolean isSipNumber = mPhoneNumberHelper.isSipNumber(mNumber); |
| 283 | |
Flavio Lerda | 178eeeb | 2011-07-11 19:51:40 +0100 | [diff] [blame] | 284 | // Let user view contact details if they exist, otherwise add option to create new contact |
| 285 | // from this number. |
| 286 | final Intent mainActionIntent; |
| 287 | final int mainActionIcon; |
| 288 | |
| 289 | if (details[0].personId != -1) { |
| 290 | Uri personUri = ContentUris.withAppendedId(Contacts.CONTENT_URI, personId); |
| 291 | mainActionIntent = new Intent(Intent.ACTION_VIEW, personUri); |
| 292 | mainActionIcon = R.drawable.sym_action_view_contact; |
Flavio Lerda | cfff16d | 2011-07-12 23:54:40 +0100 | [diff] [blame] | 293 | } else if (isVoicemailNumber) { |
| 294 | mainActionIntent = null; |
| 295 | mainActionIcon = 0; |
| 296 | } else if (isSipNumber) { |
| 297 | // TODO: This item is currently disabled for SIP addresses, because |
| 298 | // the Insert.PHONE extra only works correctly for PSTN numbers. |
| 299 | // |
| 300 | // To fix this for SIP addresses, we need to: |
| 301 | // - define ContactsContract.Intents.Insert.SIP_ADDRESS, and use it here if |
| 302 | // the current number is a SIP address |
| 303 | // - update the contacts UI code to handle Insert.SIP_ADDRESS by |
| 304 | // updating the SipAddress field |
| 305 | // and then we can remove the "!isSipNumber" check above. |
| 306 | mainActionIntent = null; |
| 307 | mainActionIcon = 0; |
Flavio Lerda | 8e40340 | 2011-07-20 16:25:49 +0100 | [diff] [blame] | 308 | } else if (canPlaceCallsTo) { |
Flavio Lerda | 178eeeb | 2011-07-11 19:51:40 +0100 | [diff] [blame] | 309 | mainActionIntent = new Intent(Intent.ACTION_INSERT_OR_EDIT); |
| 310 | mainActionIntent.setType(Contacts.CONTENT_ITEM_TYPE); |
| 311 | mainActionIntent.putExtra(Insert.PHONE, mNumber); |
| 312 | mainActionIcon = R.drawable.sym_action_add; |
Flavio Lerda | 8e40340 | 2011-07-20 16:25:49 +0100 | [diff] [blame] | 313 | } else { |
| 314 | // If we cannot call the number, when we probably cannot add it as a contact either. |
| 315 | // This is usually the case of private, unknown, or payphone numbers. |
| 316 | mainActionIntent = null; |
| 317 | mainActionIcon = 0; |
Flavio Lerda | 178eeeb | 2011-07-11 19:51:40 +0100 | [diff] [blame] | 318 | } |
| 319 | |
Flavio Lerda | cfff16d | 2011-07-12 23:54:40 +0100 | [diff] [blame] | 320 | if (mainActionIntent == null) { |
| 321 | mMainActionView.setVisibility(View.INVISIBLE); |
| 322 | } else { |
| 323 | mMainActionView.setVisibility(View.VISIBLE); |
| 324 | mMainActionView.setImageResource(mainActionIcon); |
| 325 | mMainActionView.setOnClickListener(new View.OnClickListener() { |
| 326 | @Override |
| 327 | public void onClick(View v) { |
| 328 | startActivity(mainActionIntent); |
| 329 | } |
| 330 | }); |
| 331 | } |
Flavio Lerda | 178eeeb | 2011-07-11 19:51:40 +0100 | [diff] [blame] | 332 | |
| 333 | // Build list of various available actions. |
| 334 | final List<ViewEntry> actions = new ArrayList<ViewEntry>(); |
| 335 | |
Flavio Lerda | 178eeeb | 2011-07-11 19:51:40 +0100 | [diff] [blame] | 336 | // This action allows to call the number that places the call. |
Flavio Lerda | cfff16d | 2011-07-12 23:54:40 +0100 | [diff] [blame] | 337 | if (canPlaceCallsTo) { |
Flavio Lerda | 178eeeb | 2011-07-11 19:51:40 +0100 | [diff] [blame] | 338 | actions.add(new ViewEntry(android.R.drawable.sym_action_call, |
| 339 | getString(R.string.menu_callNumber, mNumber), |
| 340 | new Intent(Intent.ACTION_CALL_PRIVILEGED, numberCallUri))); |
| 341 | } |
| 342 | |
Flavio Lerda | cfff16d | 2011-07-12 23:54:40 +0100 | [diff] [blame] | 343 | // This action allows to send an SMS to the number that placed the call. |
| 344 | if (mPhoneNumberHelper.canSendSmsTo(mNumber)) { |
Flavio Lerda | 178eeeb | 2011-07-11 19:51:40 +0100 | [diff] [blame] | 345 | Intent smsIntent = new Intent(Intent.ACTION_SENDTO, |
| 346 | Uri.fromParts("sms", mNumber, null)); |
| 347 | actions.add(new ViewEntry(R.drawable.sym_action_sms, |
| 348 | getString(R.string.menu_sendTextMessage), smsIntent)); |
| 349 | } |
| 350 | |
| 351 | // This action deletes all elements in the group from the call log. |
| 352 | actions.add(new ViewEntry(android.R.drawable.ic_menu_close_clear_cancel, |
| 353 | getString(R.string.recentCalls_removeFromRecentList), |
| 354 | new View.OnClickListener() { |
| 355 | @Override |
| 356 | public void onClick(View v) { |
| 357 | StringBuilder callIds = new StringBuilder(); |
| 358 | for (Uri callUri : callUris) { |
| 359 | if (callIds.length() != 0) { |
| 360 | callIds.append(","); |
| 361 | } |
| 362 | callIds.append(ContentUris.parseId(callUri)); |
| 363 | } |
| 364 | |
| 365 | getContentResolver().delete(Calls.CONTENT_URI_WITH_VOICEMAIL, |
| 366 | Calls._ID + " IN (" + callIds + ")", null); |
| 367 | finish(); |
| 368 | } |
| 369 | })); |
| 370 | |
Flavio Lerda | cfff16d | 2011-07-12 23:54:40 +0100 | [diff] [blame] | 371 | if (canPlaceCallsTo && !isSipNumber && !isVoicemailNumber) { |
Flavio Lerda | 178eeeb | 2011-07-11 19:51:40 +0100 | [diff] [blame] | 372 | // "Edit the number before calling" is only available for PSTN numbers. |
| 373 | actions.add(new ViewEntry(android.R.drawable.sym_action_call, |
| 374 | getString(R.string.recentCalls_editNumberBeforeCall), |
| 375 | new Intent(Intent.ACTION_DIAL, numberCallUri))); |
| 376 | } |
| 377 | |
| 378 | // Set the actions for this phone number. |
| 379 | setListAdapter(new ViewAdapter(this, actions)); |
| 380 | |
| 381 | ListView historyList = (ListView) findViewById(R.id.history); |
| 382 | historyList.setAdapter( |
| 383 | new CallDetailHistoryAdapter(this, mInflater, mCallTypeHelper, details)); |
Daniel Lehmann | 362654a | 2011-07-17 14:16:47 -0700 | [diff] [blame] | 384 | loadContactPhotos(photoUri); |
Flavio Lerda | 178eeeb | 2011-07-11 19:51:40 +0100 | [diff] [blame] | 385 | } |
| 386 | |
| 387 | /** Return the phone call details for a given call log URI. */ |
| 388 | private PhoneCallDetails getPhoneCallDetailsForUri(Uri callUri) { |
The Android Open Source Project | 7aa0e4c | 2009-03-03 19:32:21 -0800 | [diff] [blame] | 389 | ContentResolver resolver = getContentResolver(); |
| 390 | Cursor callCursor = resolver.query(callUri, CALL_LOG_PROJECTION, null, null, null); |
| 391 | try { |
Flavio Lerda | 178eeeb | 2011-07-11 19:51:40 +0100 | [diff] [blame] | 392 | if (callCursor == null || !callCursor.moveToFirst()) { |
| 393 | throw new IllegalArgumentException("Cannot find content: " + callUri); |
The Android Open Source Project | 7aa0e4c | 2009-03-03 19:32:21 -0800 | [diff] [blame] | 394 | } |
Flavio Lerda | 178eeeb | 2011-07-11 19:51:40 +0100 | [diff] [blame] | 395 | |
| 396 | // Read call log specifics. |
| 397 | String number = callCursor.getString(NUMBER_COLUMN_INDEX); |
| 398 | long date = callCursor.getLong(DATE_COLUMN_INDEX); |
| 399 | long duration = callCursor.getLong(DURATION_COLUMN_INDEX); |
| 400 | int callType = callCursor.getInt(CALL_TYPE_COLUMN_INDEX); |
| 401 | String countryIso = callCursor.getString(COUNTRY_ISO_COLUMN_INDEX); |
| 402 | if (TextUtils.isEmpty(countryIso)) { |
| 403 | countryIso = mDefaultCountryIso; |
| 404 | } |
| 405 | |
| 406 | // Formatted phone number. |
| 407 | final CharSequence numberText; |
| 408 | // Read contact specifics. |
| 409 | CharSequence nameText = ""; |
| 410 | int numberType = 0; |
| 411 | CharSequence numberLabel = ""; |
| 412 | long personId = -1L; |
Daniel Lehmann | 362654a | 2011-07-17 14:16:47 -0700 | [diff] [blame] | 413 | Uri photoUri = null; |
Flavio Lerda | 178eeeb | 2011-07-11 19:51:40 +0100 | [diff] [blame] | 414 | // If this is not a regular number, there is no point in looking it up in the contacts. |
| 415 | if (!mPhoneNumberHelper.canPlaceCallsTo(number)) { |
| 416 | numberText = mPhoneNumberHelper.getDisplayNumber(number, null); |
| 417 | } else { |
| 418 | // Perform a reverse-phonebook lookup to find the contact details. |
| 419 | Uri phoneUri = Uri.withAppendedPath(PhoneLookup.CONTENT_FILTER_URI, |
| 420 | Uri.encode(number)); |
| 421 | Cursor phonesCursor = resolver.query(phoneUri, PHONES_PROJECTION, null, null, null); |
| 422 | String candidateNumberText = number; |
| 423 | try { |
| 424 | if (phonesCursor != null && phonesCursor.moveToFirst()) { |
| 425 | personId = phonesCursor.getLong(COLUMN_INDEX_ID); |
| 426 | nameText = phonesCursor.getString(COLUMN_INDEX_NAME); |
Daniel Lehmann | 362654a | 2011-07-17 14:16:47 -0700 | [diff] [blame] | 427 | String photoUriString = phonesCursor.getString(COLUMN_INDEX_PHOTO_URI); |
| 428 | photoUri = photoUriString == null ? null : Uri.parse(photoUriString); |
Flavio Lerda | 178eeeb | 2011-07-11 19:51:40 +0100 | [diff] [blame] | 429 | candidateNumberText = PhoneNumberUtils.formatNumber( |
| 430 | phonesCursor.getString(COLUMN_INDEX_NUMBER), |
| 431 | phonesCursor.getString(COLUMN_INDEX_NORMALIZED_NUMBER), |
| 432 | countryIso); |
| 433 | numberType = phonesCursor.getInt(COLUMN_INDEX_TYPE); |
| 434 | numberLabel = phonesCursor.getString(COLUMN_INDEX_LABEL); |
| 435 | } else { |
| 436 | // We could not find this contact in the contacts, just format the phone |
| 437 | // number as best as we can. All the other fields will have their default |
| 438 | // values. |
| 439 | candidateNumberText = |
| 440 | PhoneNumberUtils.formatNumber(number, countryIso); |
| 441 | } |
| 442 | } finally { |
| 443 | if (phonesCursor != null) phonesCursor.close(); |
| 444 | numberText = candidateNumberText; |
| 445 | } |
| 446 | } |
Flavio Lerda | 9c46637 | 2011-07-19 17:38:17 +0100 | [diff] [blame] | 447 | return new PhoneCallDetails(number, numberText, countryIso, |
Flavio Lerda | cb805e8 | 2011-07-18 10:31:44 +0100 | [diff] [blame] | 448 | new int[]{ callType }, date, duration, |
Daniel Lehmann | 362654a | 2011-07-17 14:16:47 -0700 | [diff] [blame] | 449 | nameText, numberType, numberLabel, personId, photoUri); |
The Android Open Source Project | 7aa0e4c | 2009-03-03 19:32:21 -0800 | [diff] [blame] | 450 | } finally { |
| 451 | if (callCursor != null) { |
| 452 | callCursor.close(); |
| 453 | } |
| 454 | } |
| 455 | } |
| 456 | |
Flavio Lerda | 9cafe47 | 2011-06-08 14:06:13 +0100 | [diff] [blame] | 457 | /** Load the contact photos and places them in the corresponding views. */ |
Daniel Lehmann | 362654a | 2011-07-17 14:16:47 -0700 | [diff] [blame] | 458 | private void loadContactPhotos(Uri photoUri) { |
| 459 | mContactPhotoManager.loadPhoto(mContactBackgroundView, photoUri); |
Flavio Lerda | 9cafe47 | 2011-06-08 14:06:13 +0100 | [diff] [blame] | 460 | } |
| 461 | |
Flavio Lerda | 696e813 | 2011-07-05 19:00:23 +0100 | [diff] [blame] | 462 | private String getVoicemailNumber() { |
| 463 | TelephonyManager telephonyManager = |
| 464 | (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE); |
| 465 | return telephonyManager.getVoiceMailNumber(); |
| 466 | } |
| 467 | |
The Android Open Source Project | 7aa0e4c | 2009-03-03 19:32:21 -0800 | [diff] [blame] | 468 | static final class ViewEntry { |
Flavio Lerda | a895688 | 2011-07-09 21:34:38 +0100 | [diff] [blame] | 469 | public final int icon; |
| 470 | public final String text; |
| 471 | public final Intent intent; |
| 472 | public final View.OnClickListener action; |
| 473 | |
The Android Open Source Project | 7aa0e4c | 2009-03-03 19:32:21 -0800 | [diff] [blame] | 474 | public String label = null; |
| 475 | public String number = null; |
Dmitri Plotnikov | d0d776d | 2009-08-19 17:38:04 -0700 | [diff] [blame] | 476 | |
The Android Open Source Project | 7aa0e4c | 2009-03-03 19:32:21 -0800 | [diff] [blame] | 477 | public ViewEntry(int icon, String text, Intent intent) { |
| 478 | this.icon = icon; |
| 479 | this.text = text; |
| 480 | this.intent = intent; |
Flavio Lerda | a895688 | 2011-07-09 21:34:38 +0100 | [diff] [blame] | 481 | this.action = null; |
| 482 | } |
| 483 | |
| 484 | public ViewEntry(int icon, String text, View.OnClickListener listener) { |
| 485 | this.icon = icon; |
| 486 | this.text = text; |
| 487 | this.intent = null; |
| 488 | this.action = listener; |
The Android Open Source Project | 7aa0e4c | 2009-03-03 19:32:21 -0800 | [diff] [blame] | 489 | } |
| 490 | } |
| 491 | |
| 492 | static final class ViewAdapter extends BaseAdapter { |
Dmitri Plotnikov | d0d776d | 2009-08-19 17:38:04 -0700 | [diff] [blame] | 493 | |
The Android Open Source Project | 7aa0e4c | 2009-03-03 19:32:21 -0800 | [diff] [blame] | 494 | private final List<ViewEntry> mActions; |
Dmitri Plotnikov | d0d776d | 2009-08-19 17:38:04 -0700 | [diff] [blame] | 495 | |
The Android Open Source Project | 7aa0e4c | 2009-03-03 19:32:21 -0800 | [diff] [blame] | 496 | private final LayoutInflater mInflater; |
Dmitri Plotnikov | d0d776d | 2009-08-19 17:38:04 -0700 | [diff] [blame] | 497 | |
The Android Open Source Project | 7aa0e4c | 2009-03-03 19:32:21 -0800 | [diff] [blame] | 498 | public ViewAdapter(Context context, List<ViewEntry> actions) { |
| 499 | mActions = actions; |
| 500 | mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); |
| 501 | } |
Dmitri Plotnikov | d0d776d | 2009-08-19 17:38:04 -0700 | [diff] [blame] | 502 | |
Flavio Lerda | 9cafe47 | 2011-06-08 14:06:13 +0100 | [diff] [blame] | 503 | @Override |
The Android Open Source Project | 7aa0e4c | 2009-03-03 19:32:21 -0800 | [diff] [blame] | 504 | public int getCount() { |
| 505 | return mActions.size(); |
| 506 | } |
| 507 | |
Flavio Lerda | 9cafe47 | 2011-06-08 14:06:13 +0100 | [diff] [blame] | 508 | @Override |
The Android Open Source Project | 7aa0e4c | 2009-03-03 19:32:21 -0800 | [diff] [blame] | 509 | public Object getItem(int position) { |
| 510 | return mActions.get(position); |
| 511 | } |
| 512 | |
Flavio Lerda | 9cafe47 | 2011-06-08 14:06:13 +0100 | [diff] [blame] | 513 | @Override |
The Android Open Source Project | 7aa0e4c | 2009-03-03 19:32:21 -0800 | [diff] [blame] | 514 | public long getItemId(int position) { |
| 515 | return position; |
| 516 | } |
Dmitri Plotnikov | d0d776d | 2009-08-19 17:38:04 -0700 | [diff] [blame] | 517 | |
Flavio Lerda | 9cafe47 | 2011-06-08 14:06:13 +0100 | [diff] [blame] | 518 | @Override |
The Android Open Source Project | 7aa0e4c | 2009-03-03 19:32:21 -0800 | [diff] [blame] | 519 | public View getView(int position, View convertView, ViewGroup parent) { |
| 520 | // Make sure we have a valid convertView to start with |
| 521 | if (convertView == null) { |
| 522 | convertView = mInflater.inflate(R.layout.call_detail_list_item, parent, false); |
| 523 | } |
| 524 | |
| 525 | // Fill action with icon and text. |
| 526 | ViewEntry entry = mActions.get(position); |
| 527 | convertView.setTag(entry); |
Dmitri Plotnikov | d0d776d | 2009-08-19 17:38:04 -0700 | [diff] [blame] | 528 | |
The Android Open Source Project | 7aa0e4c | 2009-03-03 19:32:21 -0800 | [diff] [blame] | 529 | ImageView icon = (ImageView) convertView.findViewById(R.id.icon); |
| 530 | TextView text = (TextView) convertView.findViewById(android.R.id.text1); |
| 531 | |
| 532 | icon.setImageResource(entry.icon); |
| 533 | text.setText(entry.text); |
| 534 | |
| 535 | View line2 = convertView.findViewById(R.id.line2); |
| 536 | boolean numberEmpty = TextUtils.isEmpty(entry.number); |
| 537 | boolean labelEmpty = TextUtils.isEmpty(entry.label) || numberEmpty; |
| 538 | if (labelEmpty && numberEmpty) { |
| 539 | line2.setVisibility(View.GONE); |
| 540 | } else { |
| 541 | line2.setVisibility(View.VISIBLE); |
Dmitri Plotnikov | d0d776d | 2009-08-19 17:38:04 -0700 | [diff] [blame] | 542 | |
The Android Open Source Project | 7aa0e4c | 2009-03-03 19:32:21 -0800 | [diff] [blame] | 543 | TextView label = (TextView) convertView.findViewById(R.id.label); |
| 544 | if (labelEmpty) { |
| 545 | label.setVisibility(View.GONE); |
| 546 | } else { |
| 547 | label.setText(entry.label); |
| 548 | label.setVisibility(View.VISIBLE); |
| 549 | } |
| 550 | |
| 551 | TextView number = (TextView) convertView.findViewById(R.id.number); |
| 552 | number.setText(entry.number); |
| 553 | } |
Dmitri Plotnikov | d0d776d | 2009-08-19 17:38:04 -0700 | [diff] [blame] | 554 | |
The Android Open Source Project | 7aa0e4c | 2009-03-03 19:32:21 -0800 | [diff] [blame] | 555 | return convertView; |
| 556 | } |
| 557 | } |
Dmitri Plotnikov | d0d776d | 2009-08-19 17:38:04 -0700 | [diff] [blame] | 558 | |
Flavio Lerda | 9cafe47 | 2011-06-08 14:06:13 +0100 | [diff] [blame] | 559 | @Override |
| 560 | public void onItemClick(AdapterView<?> parent, View view, int position, long id) { |
The Android Open Source Project | 7aa0e4c | 2009-03-03 19:32:21 -0800 | [diff] [blame] | 561 | // Handle passing action off to correct handler. |
| 562 | if (view.getTag() instanceof ViewEntry) { |
| 563 | ViewEntry entry = (ViewEntry) view.getTag(); |
| 564 | if (entry.intent != null) { |
| 565 | startActivity(entry.intent); |
Flavio Lerda | a895688 | 2011-07-09 21:34:38 +0100 | [diff] [blame] | 566 | } else if (entry.action != null) { |
| 567 | entry.action.onClick(view); |
The Android Open Source Project | 7aa0e4c | 2009-03-03 19:32:21 -0800 | [diff] [blame] | 568 | } |
| 569 | } |
Dmitri Plotnikov | d0d776d | 2009-08-19 17:38:04 -0700 | [diff] [blame] | 570 | } |
Dmitri Plotnikov | 8e86b75 | 2010-02-22 17:47:57 -0800 | [diff] [blame] | 571 | |
| 572 | @Override |
| 573 | public void startSearch(String initialQuery, boolean selectInitialQuery, Bundle appSearchData, |
| 574 | boolean globalSearch) { |
| 575 | if (globalSearch) { |
| 576 | super.startSearch(initialQuery, selectInitialQuery, appSearchData, globalSearch); |
| 577 | } else { |
| 578 | ContactsSearchManager.startSearch(this, initialQuery); |
| 579 | } |
| 580 | } |
Debashish Chatterjee | 0cb8224 | 2011-07-19 19:29:37 +0100 | [diff] [blame] | 581 | |
| 582 | protected void updateVoicemailStatusMessage(Cursor statusCursor) { |
| 583 | if (statusCursor == null) { |
| 584 | mStatusMessageView.setVisibility(View.GONE); |
| 585 | return; |
| 586 | } |
| 587 | final StatusMessage message = getStatusMessage(statusCursor); |
| 588 | if (message == null || !message.showInCallDetails()) { |
| 589 | mStatusMessageView.setVisibility(View.GONE); |
| 590 | return; |
| 591 | } |
| 592 | |
| 593 | mStatusMessageView.setVisibility(View.VISIBLE); |
| 594 | mStatusMessageText.setText(message.callDetailsMessageId); |
| 595 | if (message.actionMessageId != -1) { |
| 596 | mStatusMessageAction.setText(message.actionMessageId); |
| 597 | } |
| 598 | if (message.actionUri != null) { |
| 599 | mStatusMessageAction.setClickable(true); |
| 600 | mStatusMessageAction.setOnClickListener(new View.OnClickListener() { |
| 601 | @Override |
| 602 | public void onClick(View v) { |
| 603 | startActivity(new Intent(Intent.ACTION_VIEW, message.actionUri)); |
| 604 | } |
| 605 | }); |
| 606 | } else { |
| 607 | mStatusMessageAction.setClickable(false); |
| 608 | } |
| 609 | } |
| 610 | |
| 611 | private StatusMessage getStatusMessage(Cursor statusCursor) { |
| 612 | List<StatusMessage> messages = mVoicemailStatusHelper.getStatusMessages(statusCursor); |
| 613 | Log.d(TAG, "Num status messages: " + messages.size()); |
| 614 | if (messages.size() == 0) { |
| 615 | return null; |
| 616 | } |
| 617 | // There can only be a single status message per source package, so num of messages can |
| 618 | // at most be 1. |
| 619 | if (messages.size() > 1) { |
| 620 | Log.w(TAG, String.format("Expected 1, found (%d) num of status messages." + |
| 621 | " Will use the first one.", messages.size())); |
| 622 | } |
| 623 | return messages.get(0); |
| 624 | } |
The Android Open Source Project | 7aa0e4c | 2009-03-03 19:32:21 -0800 | [diff] [blame] | 625 | } |