blob: b3c68ef9c6acf111b67949ba454f9e2f6b1ccff1 [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;
Bai Tao09eb04f2010-09-01 15:34:16 +080066 private String mDefaultCountryIso;
Dmitri Plotnikovd0d776d2009-08-19 17:38:04 -070067
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080068 /* package */ LayoutInflater mInflater;
69 /* package */ Resources mResources;
Dmitri Plotnikovd0d776d2009-08-19 17:38:04 -070070
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080071 static final String[] CALL_LOG_PROJECTION = new String[] {
72 CallLog.Calls.DATE,
73 CallLog.Calls.DURATION,
74 CallLog.Calls.NUMBER,
75 CallLog.Calls.TYPE,
Bai Tao09eb04f2010-09-01 15:34:16 +080076 CallLog.Calls.COUNTRY_ISO,
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080077 };
Dmitri Plotnikovd0d776d2009-08-19 17:38:04 -070078
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080079 static final int DATE_COLUMN_INDEX = 0;
80 static final int DURATION_COLUMN_INDEX = 1;
81 static final int NUMBER_COLUMN_INDEX = 2;
82 static final int CALL_TYPE_COLUMN_INDEX = 3;
Bai Tao09eb04f2010-09-01 15:34:16 +080083 static final int COUNTRY_ISO_COLUMN_INDEX = 4;
Dmitri Plotnikovd0d776d2009-08-19 17:38:04 -070084
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080085 static final String[] PHONES_PROJECTION = new String[] {
Dmitri Plotnikovd0d776d2009-08-19 17:38:04 -070086 PhoneLookup._ID,
87 PhoneLookup.DISPLAY_NAME,
88 PhoneLookup.TYPE,
89 PhoneLookup.LABEL,
90 PhoneLookup.NUMBER,
Bai Tao09eb04f2010-09-01 15:34:16 +080091 PhoneLookup.NORMALIZED_NUMBER,
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080092 };
93 static final int COLUMN_INDEX_ID = 0;
94 static final int COLUMN_INDEX_NAME = 1;
95 static final int COLUMN_INDEX_TYPE = 2;
96 static final int COLUMN_INDEX_LABEL = 3;
97 static final int COLUMN_INDEX_NUMBER = 4;
Bai Tao09eb04f2010-09-01 15:34:16 +080098 static final int COLUMN_INDEX_NORMALIZED_NUMBER = 5;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080099
100 @Override
101 protected void onCreate(Bundle icicle) {
102 super.onCreate(icicle);
103
104 setContentView(R.layout.call_detail);
105
106 mInflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
107 mResources = getResources();
Dmitri Plotnikovd0d776d2009-08-19 17:38:04 -0700108
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800109 mCallType = (TextView) findViewById(R.id.type);
110 mCallTypeIcon = (ImageView) findViewById(R.id.icon);
111 mCallTime = (TextView) findViewById(R.id.time);
112 mCallDuration = (TextView) findViewById(R.id.duration);
Bai Tao09eb04f2010-09-01 15:34:16 +0800113 mDefaultCountryIso = ContactsUtils.getCurrentCountryIso(this);
Dmitri Plotnikovd0d776d2009-08-19 17:38:04 -0700114
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800115 getListView().setOnItemClickListener(this);
116 }
Dmitri Plotnikovd0d776d2009-08-19 17:38:04 -0700117
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800118 @Override
119 public void onResume() {
120 super.onResume();
121 updateData(getIntent().getData());
122 }
123
124 @Override
125 public boolean onKeyDown(int keyCode, KeyEvent event) {
126 switch (keyCode) {
127 case KeyEvent.KEYCODE_CALL: {
128 // Make sure phone isn't already busy before starting direct call
129 TelephonyManager tm = (TelephonyManager)
130 getSystemService(Context.TELEPHONY_SERVICE);
131 if (tm.getCallState() == TelephonyManager.CALL_STATE_IDLE) {
132 Intent callIntent = new Intent(Intent.ACTION_CALL_PRIVILEGED,
133 Uri.fromParts("tel", mNumber, null));
134 startActivity(callIntent);
135 return true;
136 }
137 }
138 }
Dmitri Plotnikovd0d776d2009-08-19 17:38:04 -0700139
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800140 return super.onKeyDown(keyCode, event);
141 }
Dmitri Plotnikovd0d776d2009-08-19 17:38:04 -0700142
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800143 /**
144 * Update user interface with details of given call.
Dmitri Plotnikovd0d776d2009-08-19 17:38:04 -0700145 *
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800146 * @param callUri Uri into {@link CallLog.Calls}
147 */
148 private void updateData(Uri callUri) {
149 ContentResolver resolver = getContentResolver();
150 Cursor callCursor = resolver.query(callUri, CALL_LOG_PROJECTION, null, null, null);
151 try {
152 if (callCursor != null && callCursor.moveToFirst()) {
153 // Read call log specifics
154 mNumber = callCursor.getString(NUMBER_COLUMN_INDEX);
155 long date = callCursor.getLong(DATE_COLUMN_INDEX);
156 long duration = callCursor.getLong(DURATION_COLUMN_INDEX);
157 int callType = callCursor.getInt(CALL_TYPE_COLUMN_INDEX);
Bai Tao09eb04f2010-09-01 15:34:16 +0800158 String countryIso = callCursor.getString(COUNTRY_ISO_COLUMN_INDEX);
159 if (TextUtils.isEmpty(countryIso)) {
160 countryIso = mDefaultCountryIso;
161 }
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800162 // Pull out string in format [relative], [date]
163 CharSequence dateClause = DateUtils.formatDateRange(this, date, date,
164 DateUtils.FORMAT_SHOW_TIME | DateUtils.FORMAT_SHOW_DATE |
165 DateUtils.FORMAT_SHOW_WEEKDAY | DateUtils.FORMAT_SHOW_YEAR);
166 mCallTime.setText(dateClause);
Dmitri Plotnikovd0d776d2009-08-19 17:38:04 -0700167
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800168 // Set the duration
169 if (callType == Calls.MISSED_TYPE) {
170 mCallDuration.setVisibility(View.GONE);
171 } else {
172 mCallDuration.setVisibility(View.VISIBLE);
173 mCallDuration.setText(formatDuration(duration));
174 }
Dmitri Plotnikovd0d776d2009-08-19 17:38:04 -0700175
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800176 // Set the call type icon and caption
177 String callText = null;
178 switch (callType) {
179 case Calls.INCOMING_TYPE:
The Android Open Source Project37a16ac2009-03-18 17:39:48 -0700180 mCallTypeIcon.setImageResource(R.drawable.ic_call_log_header_incoming_call);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800181 mCallType.setText(R.string.type_incoming);
182 callText = getString(R.string.callBack);
183 break;
Dmitri Plotnikovd0d776d2009-08-19 17:38:04 -0700184
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800185 case Calls.OUTGOING_TYPE:
The Android Open Source Project37a16ac2009-03-18 17:39:48 -0700186 mCallTypeIcon.setImageResource(R.drawable.ic_call_log_header_outgoing_call);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800187 mCallType.setText(R.string.type_outgoing);
188 callText = getString(R.string.callAgain);
189 break;
Dmitri Plotnikovd0d776d2009-08-19 17:38:04 -0700190
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800191 case Calls.MISSED_TYPE:
The Android Open Source Project37a16ac2009-03-18 17:39:48 -0700192 mCallTypeIcon.setImageResource(R.drawable.ic_call_log_header_missed_call);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800193 mCallType.setText(R.string.type_missed);
194 callText = getString(R.string.returnCall);
195 break;
196 }
Dmitri Plotnikovd0d776d2009-08-19 17:38:04 -0700197
Amith Yamasani8bbe2f22009-03-24 21:24:12 -0700198 if (mNumber.equals(CallerInfo.UNKNOWN_NUMBER) ||
199 mNumber.equals(CallerInfo.PRIVATE_NUMBER)) {
200 // List is empty, let the empty view show instead.
201 TextView emptyText = (TextView) findViewById(R.id.emptyText);
202 if (emptyText != null) {
Dmitri Plotnikovd0d776d2009-08-19 17:38:04 -0700203 emptyText.setText(mNumber.equals(CallerInfo.PRIVATE_NUMBER)
Amith Yamasani8bbe2f22009-03-24 21:24:12 -0700204 ? R.string.private_num : R.string.unknown);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800205 }
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800206 } else {
Amith Yamasani8bbe2f22009-03-24 21:24:12 -0700207 // Perform a reverse-phonebook lookup to find the PERSON_ID
208 String callLabel = null;
209 Uri personUri = null;
Dmitri Plotnikovd0d776d2009-08-19 17:38:04 -0700210 Uri phoneUri = Uri.withAppendedPath(PhoneLookup.CONTENT_FILTER_URI,
211 Uri.encode(mNumber));
Amith Yamasani8bbe2f22009-03-24 21:24:12 -0700212 Cursor phonesCursor = resolver.query(phoneUri, PHONES_PROJECTION, null, null, null);
213 try {
214 if (phonesCursor != null && phonesCursor.moveToFirst()) {
215 long personId = phonesCursor.getLong(COLUMN_INDEX_ID);
216 personUri = ContentUris.withAppendedId(
Dmitri Plotnikovd0d776d2009-08-19 17:38:04 -0700217 Contacts.CONTENT_URI, personId);
Amith Yamasani8bbe2f22009-03-24 21:24:12 -0700218 callText = getString(R.string.recentCalls_callNumber,
219 phonesCursor.getString(COLUMN_INDEX_NAME));
Sang-il, Lee177c77f2010-03-09 20:37:19 +0900220 mNumber = PhoneNumberUtils.formatNumber(
Bai Tao09eb04f2010-09-01 15:34:16 +0800221 phonesCursor.getString(COLUMN_INDEX_NUMBER),
222 phonesCursor.getString(COLUMN_INDEX_NORMALIZED_NUMBER),
223 countryIso);
Dmitri Plotnikovd0d776d2009-08-19 17:38:04 -0700224 callLabel = Phone.getDisplayLabel(this,
Amith Yamasani8bbe2f22009-03-24 21:24:12 -0700225 phonesCursor.getInt(COLUMN_INDEX_TYPE),
226 phonesCursor.getString(COLUMN_INDEX_LABEL)).toString();
227 } else {
Bai Tao09eb04f2010-09-01 15:34:16 +0800228 mNumber = PhoneNumberUtils.formatNumber(mNumber, countryIso);
Amith Yamasani8bbe2f22009-03-24 21:24:12 -0700229 }
230 } finally {
231 if (phonesCursor != null) phonesCursor.close();
232 }
Dmitri Plotnikovd0d776d2009-08-19 17:38:04 -0700233
Amith Yamasani8bbe2f22009-03-24 21:24:12 -0700234 // Build list of various available actions
235 List<ViewEntry> actions = new ArrayList<ViewEntry>();
Dmitri Plotnikovd0d776d2009-08-19 17:38:04 -0700236
Amith Yamasani8bbe2f22009-03-24 21:24:12 -0700237 Intent callIntent = new Intent(Intent.ACTION_CALL_PRIVILEGED,
238 Uri.fromParts("tel", mNumber, null));
239 ViewEntry entry = new ViewEntry(android.R.drawable.sym_action_call, callText,
240 callIntent);
241 entry.number = mNumber;
242 entry.label = callLabel;
243 actions.add(entry);
Dmitri Plotnikovd0d776d2009-08-19 17:38:04 -0700244
Amith Yamasani8bbe2f22009-03-24 21:24:12 -0700245 Intent smsIntent = new Intent(Intent.ACTION_SENDTO,
246 Uri.fromParts("sms", mNumber, null));
247 actions.add(new ViewEntry(R.drawable.sym_action_sms,
248 getString(R.string.menu_sendTextMessage), smsIntent));
Dmitri Plotnikovd0d776d2009-08-19 17:38:04 -0700249
Amith Yamasani8bbe2f22009-03-24 21:24:12 -0700250 // Let user view contact details if they exist, otherwise add option
251 // to create new contact from this number.
252 if (personUri != null) {
253 Intent viewIntent = new Intent(Intent.ACTION_VIEW, personUri);
254 actions.add(new ViewEntry(R.drawable.sym_action_view_contact,
255 getString(R.string.menu_viewContact), viewIntent));
256 } else {
257 Intent createIntent = new Intent(Intent.ACTION_INSERT_OR_EDIT);
Dmitri Plotnikovd0d776d2009-08-19 17:38:04 -0700258 createIntent.setType(Contacts.CONTENT_ITEM_TYPE);
Amith Yamasani8bbe2f22009-03-24 21:24:12 -0700259 createIntent.putExtra(Insert.PHONE, mNumber);
260 actions.add(new ViewEntry(R.drawable.sym_action_add,
261 getString(R.string.recentCalls_addToContact), createIntent));
262 }
Dmitri Plotnikovd0d776d2009-08-19 17:38:04 -0700263
Amith Yamasani8bbe2f22009-03-24 21:24:12 -0700264 ViewAdapter adapter = new ViewAdapter(this, actions);
265 setListAdapter(adapter);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800266 }
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800267 } else {
268 // Something went wrong reading in our primary data, so we're going to
269 // bail out and show error to users.
270 Toast.makeText(this, R.string.toast_call_detail_error,
271 Toast.LENGTH_SHORT).show();
272 finish();
273 }
274 } finally {
275 if (callCursor != null) {
276 callCursor.close();
277 }
278 }
279 }
280
281 private String formatDuration(long elapsedSeconds) {
282 long minutes = 0;
283 long seconds = 0;
284
285 if (elapsedSeconds >= 60) {
286 minutes = elapsedSeconds / 60;
287 elapsedSeconds -= minutes * 60;
288 }
289 seconds = elapsedSeconds;
290
291 return getString(R.string.callDetailsDurationFormat, minutes, seconds);
292 }
293
294 static final class ViewEntry {
295 public int icon = -1;
296 public String text = null;
297 public Intent intent = null;
298 public String label = null;
299 public String number = null;
Dmitri Plotnikovd0d776d2009-08-19 17:38:04 -0700300
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800301 public ViewEntry(int icon, String text, Intent intent) {
302 this.icon = icon;
303 this.text = text;
304 this.intent = intent;
305 }
306 }
307
308 static final class ViewAdapter extends BaseAdapter {
Dmitri Plotnikovd0d776d2009-08-19 17:38:04 -0700309
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800310 private final List<ViewEntry> mActions;
Dmitri Plotnikovd0d776d2009-08-19 17:38:04 -0700311
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800312 private final LayoutInflater mInflater;
Dmitri Plotnikovd0d776d2009-08-19 17:38:04 -0700313
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800314 public ViewAdapter(Context context, List<ViewEntry> actions) {
315 mActions = actions;
316 mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
317 }
Dmitri Plotnikovd0d776d2009-08-19 17:38:04 -0700318
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800319 public int getCount() {
320 return mActions.size();
321 }
322
323 public Object getItem(int position) {
324 return mActions.get(position);
325 }
326
327 public long getItemId(int position) {
328 return position;
329 }
Dmitri Plotnikovd0d776d2009-08-19 17:38:04 -0700330
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800331 public View getView(int position, View convertView, ViewGroup parent) {
332 // Make sure we have a valid convertView to start with
333 if (convertView == null) {
334 convertView = mInflater.inflate(R.layout.call_detail_list_item, parent, false);
335 }
336
337 // Fill action with icon and text.
338 ViewEntry entry = mActions.get(position);
339 convertView.setTag(entry);
Dmitri Plotnikovd0d776d2009-08-19 17:38:04 -0700340
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800341 ImageView icon = (ImageView) convertView.findViewById(R.id.icon);
342 TextView text = (TextView) convertView.findViewById(android.R.id.text1);
343
344 icon.setImageResource(entry.icon);
345 text.setText(entry.text);
346
347 View line2 = convertView.findViewById(R.id.line2);
348 boolean numberEmpty = TextUtils.isEmpty(entry.number);
349 boolean labelEmpty = TextUtils.isEmpty(entry.label) || numberEmpty;
350 if (labelEmpty && numberEmpty) {
351 line2.setVisibility(View.GONE);
352 } else {
353 line2.setVisibility(View.VISIBLE);
Dmitri Plotnikovd0d776d2009-08-19 17:38:04 -0700354
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800355 TextView label = (TextView) convertView.findViewById(R.id.label);
356 if (labelEmpty) {
357 label.setVisibility(View.GONE);
358 } else {
359 label.setText(entry.label);
360 label.setVisibility(View.VISIBLE);
361 }
362
363 TextView number = (TextView) convertView.findViewById(R.id.number);
364 number.setText(entry.number);
365 }
Dmitri Plotnikovd0d776d2009-08-19 17:38:04 -0700366
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800367 return convertView;
368 }
369 }
Dmitri Plotnikovd0d776d2009-08-19 17:38:04 -0700370
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800371 public void onItemClick(AdapterView parent, View view, int position, long id) {
372 // Handle passing action off to correct handler.
373 if (view.getTag() instanceof ViewEntry) {
374 ViewEntry entry = (ViewEntry) view.getTag();
375 if (entry.intent != null) {
376 startActivity(entry.intent);
377 }
378 }
Dmitri Plotnikovd0d776d2009-08-19 17:38:04 -0700379 }
Dmitri Plotnikov8e86b752010-02-22 17:47:57 -0800380
381 @Override
382 public void startSearch(String initialQuery, boolean selectInitialQuery, Bundle appSearchData,
383 boolean globalSearch) {
384 if (globalSearch) {
385 super.startSearch(initialQuery, selectInitialQuery, appSearchData, globalSearch);
386 } else {
387 ContactsSearchManager.startSearch(this, initialQuery);
388 }
389 }
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800390}