blob: 14f54c97bad408214a647a370f23a61199ace2bc [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));
Sang-il, Lee177c77f2010-03-09 20:37:19 +0900211 mNumber = PhoneNumberUtils.formatNumber(
212 phonesCursor.getString(COLUMN_INDEX_NUMBER));
Dmitri Plotnikovd0d776d2009-08-19 17:38:04 -0700213 callLabel = Phone.getDisplayLabel(this,
Amith Yamasani8bbe2f22009-03-24 21:24:12 -0700214 phonesCursor.getInt(COLUMN_INDEX_TYPE),
215 phonesCursor.getString(COLUMN_INDEX_LABEL)).toString();
216 } else {
217 mNumber = PhoneNumberUtils.formatNumber(mNumber);
218 }
219 } finally {
220 if (phonesCursor != null) phonesCursor.close();
221 }
Dmitri Plotnikovd0d776d2009-08-19 17:38:04 -0700222
Amith Yamasani8bbe2f22009-03-24 21:24:12 -0700223 // Build list of various available actions
224 List<ViewEntry> actions = new ArrayList<ViewEntry>();
Dmitri Plotnikovd0d776d2009-08-19 17:38:04 -0700225
Amith Yamasani8bbe2f22009-03-24 21:24:12 -0700226 Intent callIntent = new Intent(Intent.ACTION_CALL_PRIVILEGED,
227 Uri.fromParts("tel", mNumber, null));
228 ViewEntry entry = new ViewEntry(android.R.drawable.sym_action_call, callText,
229 callIntent);
230 entry.number = mNumber;
231 entry.label = callLabel;
232 actions.add(entry);
Dmitri Plotnikovd0d776d2009-08-19 17:38:04 -0700233
Amith Yamasani8bbe2f22009-03-24 21:24:12 -0700234 Intent smsIntent = new Intent(Intent.ACTION_SENDTO,
235 Uri.fromParts("sms", mNumber, null));
236 actions.add(new ViewEntry(R.drawable.sym_action_sms,
237 getString(R.string.menu_sendTextMessage), smsIntent));
Dmitri Plotnikovd0d776d2009-08-19 17:38:04 -0700238
Amith Yamasani8bbe2f22009-03-24 21:24:12 -0700239 // Let user view contact details if they exist, otherwise add option
240 // to create new contact from this number.
241 if (personUri != null) {
242 Intent viewIntent = new Intent(Intent.ACTION_VIEW, personUri);
243 actions.add(new ViewEntry(R.drawable.sym_action_view_contact,
244 getString(R.string.menu_viewContact), viewIntent));
245 } else {
246 Intent createIntent = new Intent(Intent.ACTION_INSERT_OR_EDIT);
Dmitri Plotnikovd0d776d2009-08-19 17:38:04 -0700247 createIntent.setType(Contacts.CONTENT_ITEM_TYPE);
Amith Yamasani8bbe2f22009-03-24 21:24:12 -0700248 createIntent.putExtra(Insert.PHONE, mNumber);
249 actions.add(new ViewEntry(R.drawable.sym_action_add,
250 getString(R.string.recentCalls_addToContact), createIntent));
251 }
Dmitri Plotnikovd0d776d2009-08-19 17:38:04 -0700252
Amith Yamasani8bbe2f22009-03-24 21:24:12 -0700253 ViewAdapter adapter = new ViewAdapter(this, actions);
254 setListAdapter(adapter);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800255 }
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800256 } else {
257 // Something went wrong reading in our primary data, so we're going to
258 // bail out and show error to users.
259 Toast.makeText(this, R.string.toast_call_detail_error,
260 Toast.LENGTH_SHORT).show();
261 finish();
262 }
263 } finally {
264 if (callCursor != null) {
265 callCursor.close();
266 }
267 }
268 }
269
270 private String formatDuration(long elapsedSeconds) {
271 long minutes = 0;
272 long seconds = 0;
273
274 if (elapsedSeconds >= 60) {
275 minutes = elapsedSeconds / 60;
276 elapsedSeconds -= minutes * 60;
277 }
278 seconds = elapsedSeconds;
279
280 return getString(R.string.callDetailsDurationFormat, minutes, seconds);
281 }
282
283 static final class ViewEntry {
284 public int icon = -1;
285 public String text = null;
286 public Intent intent = null;
287 public String label = null;
288 public String number = null;
Dmitri Plotnikovd0d776d2009-08-19 17:38:04 -0700289
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800290 public ViewEntry(int icon, String text, Intent intent) {
291 this.icon = icon;
292 this.text = text;
293 this.intent = intent;
294 }
295 }
296
297 static final class ViewAdapter extends BaseAdapter {
Dmitri Plotnikovd0d776d2009-08-19 17:38:04 -0700298
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800299 private final List<ViewEntry> mActions;
Dmitri Plotnikovd0d776d2009-08-19 17:38:04 -0700300
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800301 private final LayoutInflater mInflater;
Dmitri Plotnikovd0d776d2009-08-19 17:38:04 -0700302
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800303 public ViewAdapter(Context context, List<ViewEntry> actions) {
304 mActions = actions;
305 mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
306 }
Dmitri Plotnikovd0d776d2009-08-19 17:38:04 -0700307
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800308 public int getCount() {
309 return mActions.size();
310 }
311
312 public Object getItem(int position) {
313 return mActions.get(position);
314 }
315
316 public long getItemId(int position) {
317 return position;
318 }
Dmitri Plotnikovd0d776d2009-08-19 17:38:04 -0700319
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800320 public View getView(int position, View convertView, ViewGroup parent) {
321 // Make sure we have a valid convertView to start with
322 if (convertView == null) {
323 convertView = mInflater.inflate(R.layout.call_detail_list_item, parent, false);
324 }
325
326 // Fill action with icon and text.
327 ViewEntry entry = mActions.get(position);
328 convertView.setTag(entry);
Dmitri Plotnikovd0d776d2009-08-19 17:38:04 -0700329
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800330 ImageView icon = (ImageView) convertView.findViewById(R.id.icon);
331 TextView text = (TextView) convertView.findViewById(android.R.id.text1);
332
333 icon.setImageResource(entry.icon);
334 text.setText(entry.text);
335
336 View line2 = convertView.findViewById(R.id.line2);
337 boolean numberEmpty = TextUtils.isEmpty(entry.number);
338 boolean labelEmpty = TextUtils.isEmpty(entry.label) || numberEmpty;
339 if (labelEmpty && numberEmpty) {
340 line2.setVisibility(View.GONE);
341 } else {
342 line2.setVisibility(View.VISIBLE);
Dmitri Plotnikovd0d776d2009-08-19 17:38:04 -0700343
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800344 TextView label = (TextView) convertView.findViewById(R.id.label);
345 if (labelEmpty) {
346 label.setVisibility(View.GONE);
347 } else {
348 label.setText(entry.label);
349 label.setVisibility(View.VISIBLE);
350 }
351
352 TextView number = (TextView) convertView.findViewById(R.id.number);
353 number.setText(entry.number);
354 }
Dmitri Plotnikovd0d776d2009-08-19 17:38:04 -0700355
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800356 return convertView;
357 }
358 }
Dmitri Plotnikovd0d776d2009-08-19 17:38:04 -0700359
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800360 public void onItemClick(AdapterView parent, View view, int position, long id) {
361 // Handle passing action off to correct handler.
362 if (view.getTag() instanceof ViewEntry) {
363 ViewEntry entry = (ViewEntry) view.getTag();
364 if (entry.intent != null) {
365 startActivity(entry.intent);
366 }
367 }
Dmitri Plotnikovd0d776d2009-08-19 17:38:04 -0700368 }
Dmitri Plotnikov8e86b752010-02-22 17:47:57 -0800369
370 @Override
371 public void startSearch(String initialQuery, boolean selectInitialQuery, Bundle appSearchData,
372 boolean globalSearch) {
373 if (globalSearch) {
374 super.startSearch(initialQuery, selectInitialQuery, appSearchData, globalSearch);
375 } else {
376 ContactsSearchManager.startSearch(this, initialQuery);
377 }
378 }
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800379}