blob: 32286920ff7c21b2d73fbb160d62add18d144dc5 [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
Amith Yamasani8bbe2f22009-03-24 21:24:12 -070019import com.android.internal.telephony.CallerInfo;
20
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080021import android.app.ListActivity;
22import android.content.ContentResolver;
23import android.content.ContentUris;
24import android.content.Context;
25import android.content.Intent;
26import android.content.res.Resources;
27import android.database.Cursor;
28import android.net.Uri;
29import android.os.Bundle;
30import android.provider.CallLog;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080031import android.provider.CallLog.Calls;
Dmitri Plotnikovd0d776d2009-08-19 17:38:04 -070032import android.provider.ContactsContract.Contacts;
33import android.provider.ContactsContract.PhoneLookup;
34import android.provider.ContactsContract.CommonDataKinds.Phone;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080035import android.provider.Contacts.Intents.Insert;
36import android.telephony.PhoneNumberUtils;
37import android.telephony.TelephonyManager;
38import android.text.TextUtils;
39import android.text.format.DateUtils;
40import android.view.KeyEvent;
41import android.view.LayoutInflater;
42import android.view.View;
43import android.view.ViewGroup;
44import android.widget.AdapterView;
45import android.widget.BaseAdapter;
46import android.widget.ImageView;
47import android.widget.TextView;
48import android.widget.Toast;
49
50import java.util.ArrayList;
51import java.util.List;
52
53/**
54 * Displays the details of a specific call log entry.
55 */
56public class CallDetailActivity extends ListActivity implements
57 AdapterView.OnItemClickListener {
58 private static final String TAG = "CallDetail";
59
60 private TextView mCallType;
61 private ImageView mCallTypeIcon;
62 private TextView mCallTime;
63 private TextView mCallDuration;
64
65 private String mNumber = null;
Dmitri Plotnikovd0d776d2009-08-19 17:38:04 -070066
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080067 /* package */ LayoutInflater mInflater;
68 /* package */ Resources mResources;
Dmitri Plotnikovd0d776d2009-08-19 17:38:04 -070069
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080070 static final String[] CALL_LOG_PROJECTION = new String[] {
71 CallLog.Calls.DATE,
72 CallLog.Calls.DURATION,
73 CallLog.Calls.NUMBER,
74 CallLog.Calls.TYPE,
75 };
Dmitri Plotnikovd0d776d2009-08-19 17:38:04 -070076
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080077 static final int DATE_COLUMN_INDEX = 0;
78 static final int DURATION_COLUMN_INDEX = 1;
79 static final int NUMBER_COLUMN_INDEX = 2;
80 static final int CALL_TYPE_COLUMN_INDEX = 3;
Dmitri Plotnikovd0d776d2009-08-19 17:38:04 -070081
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080082 static final String[] PHONES_PROJECTION = new String[] {
Dmitri Plotnikovd0d776d2009-08-19 17:38:04 -070083 PhoneLookup._ID,
84 PhoneLookup.DISPLAY_NAME,
85 PhoneLookup.TYPE,
86 PhoneLookup.LABEL,
87 PhoneLookup.NUMBER,
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080088 };
89 static final int COLUMN_INDEX_ID = 0;
90 static final int COLUMN_INDEX_NAME = 1;
91 static final int COLUMN_INDEX_TYPE = 2;
92 static final int COLUMN_INDEX_LABEL = 3;
93 static final int COLUMN_INDEX_NUMBER = 4;
94
95 @Override
96 protected void onCreate(Bundle icicle) {
97 super.onCreate(icicle);
98
99 setContentView(R.layout.call_detail);
100
101 mInflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
102 mResources = getResources();
Dmitri Plotnikovd0d776d2009-08-19 17:38:04 -0700103
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800104 mCallType = (TextView) findViewById(R.id.type);
105 mCallTypeIcon = (ImageView) findViewById(R.id.icon);
106 mCallTime = (TextView) findViewById(R.id.time);
107 mCallDuration = (TextView) findViewById(R.id.duration);
Dmitri Plotnikovd0d776d2009-08-19 17:38:04 -0700108
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800109 getListView().setOnItemClickListener(this);
110 }
Dmitri Plotnikovd0d776d2009-08-19 17:38:04 -0700111
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800112 @Override
113 public void onResume() {
114 super.onResume();
115 updateData(getIntent().getData());
116 }
117
118 @Override
119 public boolean onKeyDown(int keyCode, KeyEvent event) {
120 switch (keyCode) {
121 case KeyEvent.KEYCODE_CALL: {
122 // Make sure phone isn't already busy before starting direct call
123 TelephonyManager tm = (TelephonyManager)
124 getSystemService(Context.TELEPHONY_SERVICE);
125 if (tm.getCallState() == TelephonyManager.CALL_STATE_IDLE) {
126 Intent callIntent = new Intent(Intent.ACTION_CALL_PRIVILEGED,
127 Uri.fromParts("tel", mNumber, null));
128 startActivity(callIntent);
129 return true;
130 }
131 }
132 }
Dmitri Plotnikovd0d776d2009-08-19 17:38:04 -0700133
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800134 return super.onKeyDown(keyCode, event);
135 }
Dmitri Plotnikovd0d776d2009-08-19 17:38:04 -0700136
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800137 /**
138 * Update user interface with details of given call.
Dmitri Plotnikovd0d776d2009-08-19 17:38:04 -0700139 *
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800140 * @param callUri Uri into {@link CallLog.Calls}
141 */
142 private void updateData(Uri callUri) {
143 ContentResolver resolver = getContentResolver();
144 Cursor callCursor = resolver.query(callUri, CALL_LOG_PROJECTION, null, null, null);
145 try {
146 if (callCursor != null && callCursor.moveToFirst()) {
147 // Read call log specifics
148 mNumber = callCursor.getString(NUMBER_COLUMN_INDEX);
149 long date = callCursor.getLong(DATE_COLUMN_INDEX);
150 long duration = callCursor.getLong(DURATION_COLUMN_INDEX);
151 int callType = callCursor.getInt(CALL_TYPE_COLUMN_INDEX);
Dmitri Plotnikovd0d776d2009-08-19 17:38:04 -0700152
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800153 // Pull out string in format [relative], [date]
154 CharSequence dateClause = DateUtils.formatDateRange(this, date, date,
155 DateUtils.FORMAT_SHOW_TIME | DateUtils.FORMAT_SHOW_DATE |
156 DateUtils.FORMAT_SHOW_WEEKDAY | DateUtils.FORMAT_SHOW_YEAR);
157 mCallTime.setText(dateClause);
Dmitri Plotnikovd0d776d2009-08-19 17:38:04 -0700158
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800159 // Set the duration
160 if (callType == Calls.MISSED_TYPE) {
161 mCallDuration.setVisibility(View.GONE);
162 } else {
163 mCallDuration.setVisibility(View.VISIBLE);
164 mCallDuration.setText(formatDuration(duration));
165 }
Dmitri Plotnikovd0d776d2009-08-19 17:38:04 -0700166
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800167 // Set the call type icon and caption
168 String callText = null;
169 switch (callType) {
170 case Calls.INCOMING_TYPE:
The Android Open Source Project37a16ac2009-03-18 17:39:48 -0700171 mCallTypeIcon.setImageResource(R.drawable.ic_call_log_header_incoming_call);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800172 mCallType.setText(R.string.type_incoming);
173 callText = getString(R.string.callBack);
174 break;
Dmitri Plotnikovd0d776d2009-08-19 17:38:04 -0700175
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800176 case Calls.OUTGOING_TYPE:
The Android Open Source Project37a16ac2009-03-18 17:39:48 -0700177 mCallTypeIcon.setImageResource(R.drawable.ic_call_log_header_outgoing_call);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800178 mCallType.setText(R.string.type_outgoing);
179 callText = getString(R.string.callAgain);
180 break;
Dmitri Plotnikovd0d776d2009-08-19 17:38:04 -0700181
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800182 case Calls.MISSED_TYPE:
The Android Open Source Project37a16ac2009-03-18 17:39:48 -0700183 mCallTypeIcon.setImageResource(R.drawable.ic_call_log_header_missed_call);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800184 mCallType.setText(R.string.type_missed);
185 callText = getString(R.string.returnCall);
186 break;
187 }
Dmitri Plotnikovd0d776d2009-08-19 17:38:04 -0700188
Amith Yamasani8bbe2f22009-03-24 21:24:12 -0700189 if (mNumber.equals(CallerInfo.UNKNOWN_NUMBER) ||
190 mNumber.equals(CallerInfo.PRIVATE_NUMBER)) {
191 // List is empty, let the empty view show instead.
192 TextView emptyText = (TextView) findViewById(R.id.emptyText);
193 if (emptyText != null) {
Dmitri Plotnikovd0d776d2009-08-19 17:38:04 -0700194 emptyText.setText(mNumber.equals(CallerInfo.PRIVATE_NUMBER)
Amith Yamasani8bbe2f22009-03-24 21:24:12 -0700195 ? R.string.private_num : R.string.unknown);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800196 }
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800197 } else {
Amith Yamasani8bbe2f22009-03-24 21:24:12 -0700198 // Perform a reverse-phonebook lookup to find the PERSON_ID
199 String callLabel = null;
200 Uri personUri = null;
Dmitri Plotnikovd0d776d2009-08-19 17:38:04 -0700201 Uri phoneUri = Uri.withAppendedPath(PhoneLookup.CONTENT_FILTER_URI,
202 Uri.encode(mNumber));
Amith Yamasani8bbe2f22009-03-24 21:24:12 -0700203 Cursor phonesCursor = resolver.query(phoneUri, PHONES_PROJECTION, null, null, null);
204 try {
205 if (phonesCursor != null && phonesCursor.moveToFirst()) {
206 long personId = phonesCursor.getLong(COLUMN_INDEX_ID);
207 personUri = ContentUris.withAppendedId(
Dmitri Plotnikovd0d776d2009-08-19 17:38:04 -0700208 Contacts.CONTENT_URI, personId);
Amith Yamasani8bbe2f22009-03-24 21:24:12 -0700209 callText = getString(R.string.recentCalls_callNumber,
210 phonesCursor.getString(COLUMN_INDEX_NAME));
211 mNumber = phonesCursor.getString(COLUMN_INDEX_NUMBER);
Dmitri Plotnikovd0d776d2009-08-19 17:38:04 -0700212 callLabel = Phone.getDisplayLabel(this,
Amith Yamasani8bbe2f22009-03-24 21:24:12 -0700213 phonesCursor.getInt(COLUMN_INDEX_TYPE),
214 phonesCursor.getString(COLUMN_INDEX_LABEL)).toString();
215 } else {
216 mNumber = PhoneNumberUtils.formatNumber(mNumber);
217 }
218 } finally {
219 if (phonesCursor != null) phonesCursor.close();
220 }
Dmitri Plotnikovd0d776d2009-08-19 17:38:04 -0700221
Amith Yamasani8bbe2f22009-03-24 21:24:12 -0700222 // Build list of various available actions
223 List<ViewEntry> actions = new ArrayList<ViewEntry>();
Dmitri Plotnikovd0d776d2009-08-19 17:38:04 -0700224
Amith Yamasani8bbe2f22009-03-24 21:24:12 -0700225 Intent callIntent = new Intent(Intent.ACTION_CALL_PRIVILEGED,
226 Uri.fromParts("tel", mNumber, null));
227 ViewEntry entry = new ViewEntry(android.R.drawable.sym_action_call, callText,
228 callIntent);
229 entry.number = mNumber;
230 entry.label = callLabel;
231 actions.add(entry);
Dmitri Plotnikovd0d776d2009-08-19 17:38:04 -0700232
Amith Yamasani8bbe2f22009-03-24 21:24:12 -0700233 Intent smsIntent = new Intent(Intent.ACTION_SENDTO,
234 Uri.fromParts("sms", mNumber, null));
235 actions.add(new ViewEntry(R.drawable.sym_action_sms,
236 getString(R.string.menu_sendTextMessage), smsIntent));
Dmitri Plotnikovd0d776d2009-08-19 17:38:04 -0700237
Amith Yamasani8bbe2f22009-03-24 21:24:12 -0700238 // Let user view contact details if they exist, otherwise add option
239 // to create new contact from this number.
240 if (personUri != null) {
241 Intent viewIntent = new Intent(Intent.ACTION_VIEW, personUri);
242 actions.add(new ViewEntry(R.drawable.sym_action_view_contact,
243 getString(R.string.menu_viewContact), viewIntent));
244 } else {
245 Intent createIntent = new Intent(Intent.ACTION_INSERT_OR_EDIT);
Dmitri Plotnikovd0d776d2009-08-19 17:38:04 -0700246 createIntent.setType(Contacts.CONTENT_ITEM_TYPE);
Amith Yamasani8bbe2f22009-03-24 21:24:12 -0700247 createIntent.putExtra(Insert.PHONE, mNumber);
248 actions.add(new ViewEntry(R.drawable.sym_action_add,
249 getString(R.string.recentCalls_addToContact), createIntent));
250 }
Dmitri Plotnikovd0d776d2009-08-19 17:38:04 -0700251
Amith Yamasani8bbe2f22009-03-24 21:24:12 -0700252 ViewAdapter adapter = new ViewAdapter(this, actions);
253 setListAdapter(adapter);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800254 }
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800255 } else {
256 // Something went wrong reading in our primary data, so we're going to
257 // bail out and show error to users.
258 Toast.makeText(this, R.string.toast_call_detail_error,
259 Toast.LENGTH_SHORT).show();
260 finish();
261 }
262 } finally {
263 if (callCursor != null) {
264 callCursor.close();
265 }
266 }
267 }
268
269 private String formatDuration(long elapsedSeconds) {
270 long minutes = 0;
271 long seconds = 0;
272
273 if (elapsedSeconds >= 60) {
274 minutes = elapsedSeconds / 60;
275 elapsedSeconds -= minutes * 60;
276 }
277 seconds = elapsedSeconds;
278
279 return getString(R.string.callDetailsDurationFormat, minutes, seconds);
280 }
281
282 static final class ViewEntry {
283 public int icon = -1;
284 public String text = null;
285 public Intent intent = null;
286 public String label = null;
287 public String number = null;
Dmitri Plotnikovd0d776d2009-08-19 17:38:04 -0700288
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800289 public ViewEntry(int icon, String text, Intent intent) {
290 this.icon = icon;
291 this.text = text;
292 this.intent = intent;
293 }
294 }
295
296 static final class ViewAdapter extends BaseAdapter {
Dmitri Plotnikovd0d776d2009-08-19 17:38:04 -0700297
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800298 private final List<ViewEntry> mActions;
Dmitri Plotnikovd0d776d2009-08-19 17:38:04 -0700299
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800300 private final LayoutInflater mInflater;
Dmitri Plotnikovd0d776d2009-08-19 17:38:04 -0700301
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800302 public ViewAdapter(Context context, List<ViewEntry> actions) {
303 mActions = actions;
304 mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
305 }
Dmitri Plotnikovd0d776d2009-08-19 17:38:04 -0700306
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800307 public int getCount() {
308 return mActions.size();
309 }
310
311 public Object getItem(int position) {
312 return mActions.get(position);
313 }
314
315 public long getItemId(int position) {
316 return position;
317 }
Dmitri Plotnikovd0d776d2009-08-19 17:38:04 -0700318
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800319 public View getView(int position, View convertView, ViewGroup parent) {
320 // Make sure we have a valid convertView to start with
321 if (convertView == null) {
322 convertView = mInflater.inflate(R.layout.call_detail_list_item, parent, false);
323 }
324
325 // Fill action with icon and text.
326 ViewEntry entry = mActions.get(position);
327 convertView.setTag(entry);
Dmitri Plotnikovd0d776d2009-08-19 17:38:04 -0700328
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800329 ImageView icon = (ImageView) convertView.findViewById(R.id.icon);
330 TextView text = (TextView) convertView.findViewById(android.R.id.text1);
331
332 icon.setImageResource(entry.icon);
333 text.setText(entry.text);
334
335 View line2 = convertView.findViewById(R.id.line2);
336 boolean numberEmpty = TextUtils.isEmpty(entry.number);
337 boolean labelEmpty = TextUtils.isEmpty(entry.label) || numberEmpty;
338 if (labelEmpty && numberEmpty) {
339 line2.setVisibility(View.GONE);
340 } else {
341 line2.setVisibility(View.VISIBLE);
Dmitri Plotnikovd0d776d2009-08-19 17:38:04 -0700342
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800343 TextView label = (TextView) convertView.findViewById(R.id.label);
344 if (labelEmpty) {
345 label.setVisibility(View.GONE);
346 } else {
347 label.setText(entry.label);
348 label.setVisibility(View.VISIBLE);
349 }
350
351 TextView number = (TextView) convertView.findViewById(R.id.number);
352 number.setText(entry.number);
353 }
Dmitri Plotnikovd0d776d2009-08-19 17:38:04 -0700354
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800355 return convertView;
356 }
357 }
Dmitri Plotnikovd0d776d2009-08-19 17:38:04 -0700358
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800359 public void onItemClick(AdapterView parent, View view, int position, long id) {
360 // Handle passing action off to correct handler.
361 if (view.getTag() instanceof ViewEntry) {
362 ViewEntry entry = (ViewEntry) view.getTag();
363 if (entry.intent != null) {
364 startActivity(entry.intent);
365 }
366 }
Dmitri Plotnikovd0d776d2009-08-19 17:38:04 -0700367 }
Dmitri Plotnikov8e86b752010-02-22 17:47:57 -0800368
369 @Override
370 public void startSearch(String initialQuery, boolean selectInitialQuery, Bundle appSearchData,
371 boolean globalSearch) {
372 if (globalSearch) {
373 super.startSearch(initialQuery, selectInitialQuery, appSearchData, globalSearch);
374 } else {
375 ContactsSearchManager.startSearch(this, initialQuery);
376 }
377 }
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800378}