blob: d7d888cad9788ef439543810bcb26702082d8590 [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;
31import android.provider.Contacts;
32import android.provider.CallLog.Calls;
33import android.provider.Contacts.People;
34import android.provider.Contacts.Phones;
35import 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;
66
67 /* package */ LayoutInflater mInflater;
68 /* package */ Resources mResources;
69
70 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 };
76
77 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;
81
82 static final String[] PHONES_PROJECTION = new String[] {
83 Phones.PERSON_ID,
84 Phones.DISPLAY_NAME,
85 Phones.TYPE,
86 Phones.LABEL,
87 Phones.NUMBER,
88 };
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();
103
104 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);
108
109 getListView().setOnItemClickListener(this);
110 }
111
112 @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 }
133
134 return super.onKeyDown(keyCode, event);
135 }
136
137 /**
138 * Update user interface with details of given call.
139 *
140 * @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);
152
153 // 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);
158
159 // 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 }
166
167 // 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;
175
176 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;
181
182 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 }
188
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) {
194 emptyText.setText(mNumber.equals(CallerInfo.PRIVATE_NUMBER)
195 ? 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;
201 Uri phoneUri = Uri.withAppendedPath(Phones.CONTENT_FILTER_URL, Uri.encode(mNumber));
202 Cursor phonesCursor = resolver.query(phoneUri, PHONES_PROJECTION, null, null, null);
203 try {
204 if (phonesCursor != null && phonesCursor.moveToFirst()) {
205 long personId = phonesCursor.getLong(COLUMN_INDEX_ID);
206 personUri = ContentUris.withAppendedId(
207 Contacts.People.CONTENT_URI, personId);
208 callText = getString(R.string.recentCalls_callNumber,
209 phonesCursor.getString(COLUMN_INDEX_NAME));
210 mNumber = phonesCursor.getString(COLUMN_INDEX_NUMBER);
211 callLabel = Phones.getDisplayLabel(this,
212 phonesCursor.getInt(COLUMN_INDEX_TYPE),
213 phonesCursor.getString(COLUMN_INDEX_LABEL)).toString();
214 } else {
215 mNumber = PhoneNumberUtils.formatNumber(mNumber);
216 }
217 } finally {
218 if (phonesCursor != null) phonesCursor.close();
219 }
220
221 // Build list of various available actions
222 List<ViewEntry> actions = new ArrayList<ViewEntry>();
223
224 Intent callIntent = new Intent(Intent.ACTION_CALL_PRIVILEGED,
225 Uri.fromParts("tel", mNumber, null));
226 ViewEntry entry = new ViewEntry(android.R.drawable.sym_action_call, callText,
227 callIntent);
228 entry.number = mNumber;
229 entry.label = callLabel;
230 actions.add(entry);
231
232 Intent smsIntent = new Intent(Intent.ACTION_SENDTO,
233 Uri.fromParts("sms", mNumber, null));
234 actions.add(new ViewEntry(R.drawable.sym_action_sms,
235 getString(R.string.menu_sendTextMessage), smsIntent));
236
237 // Let user view contact details if they exist, otherwise add option
238 // to create new contact from this number.
239 if (personUri != null) {
240 Intent viewIntent = new Intent(Intent.ACTION_VIEW, personUri);
241 actions.add(new ViewEntry(R.drawable.sym_action_view_contact,
242 getString(R.string.menu_viewContact), viewIntent));
243 } else {
244 Intent createIntent = new Intent(Intent.ACTION_INSERT_OR_EDIT);
245 createIntent.setType(People.CONTENT_ITEM_TYPE);
246 createIntent.putExtra(Insert.PHONE, mNumber);
247 actions.add(new ViewEntry(R.drawable.sym_action_add,
248 getString(R.string.recentCalls_addToContact), createIntent));
249 }
250
251 ViewAdapter adapter = new ViewAdapter(this, actions);
252 setListAdapter(adapter);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800253 }
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800254 } else {
255 // Something went wrong reading in our primary data, so we're going to
256 // bail out and show error to users.
257 Toast.makeText(this, R.string.toast_call_detail_error,
258 Toast.LENGTH_SHORT).show();
259 finish();
260 }
261 } finally {
262 if (callCursor != null) {
263 callCursor.close();
264 }
265 }
266 }
267
268 private String formatDuration(long elapsedSeconds) {
269 long minutes = 0;
270 long seconds = 0;
271
272 if (elapsedSeconds >= 60) {
273 minutes = elapsedSeconds / 60;
274 elapsedSeconds -= minutes * 60;
275 }
276 seconds = elapsedSeconds;
277
278 return getString(R.string.callDetailsDurationFormat, minutes, seconds);
279 }
280
281 static final class ViewEntry {
282 public int icon = -1;
283 public String text = null;
284 public Intent intent = null;
285 public String label = null;
286 public String number = null;
287
288 public ViewEntry(int icon, String text, Intent intent) {
289 this.icon = icon;
290 this.text = text;
291 this.intent = intent;
292 }
293 }
294
295 static final class ViewAdapter extends BaseAdapter {
296
297 private final List<ViewEntry> mActions;
298
299 private final LayoutInflater mInflater;
300
301 public ViewAdapter(Context context, List<ViewEntry> actions) {
302 mActions = actions;
303 mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
304 }
305
306 public int getCount() {
307 return mActions.size();
308 }
309
310 public Object getItem(int position) {
311 return mActions.get(position);
312 }
313
314 public long getItemId(int position) {
315 return position;
316 }
317
318 public View getView(int position, View convertView, ViewGroup parent) {
319 // Make sure we have a valid convertView to start with
320 if (convertView == null) {
321 convertView = mInflater.inflate(R.layout.call_detail_list_item, parent, false);
322 }
323
324 // Fill action with icon and text.
325 ViewEntry entry = mActions.get(position);
326 convertView.setTag(entry);
327
328 ImageView icon = (ImageView) convertView.findViewById(R.id.icon);
329 TextView text = (TextView) convertView.findViewById(android.R.id.text1);
330
331 icon.setImageResource(entry.icon);
332 text.setText(entry.text);
333
334 View line2 = convertView.findViewById(R.id.line2);
335 boolean numberEmpty = TextUtils.isEmpty(entry.number);
336 boolean labelEmpty = TextUtils.isEmpty(entry.label) || numberEmpty;
337 if (labelEmpty && numberEmpty) {
338 line2.setVisibility(View.GONE);
339 } else {
340 line2.setVisibility(View.VISIBLE);
341
342 TextView label = (TextView) convertView.findViewById(R.id.label);
343 if (labelEmpty) {
344 label.setVisibility(View.GONE);
345 } else {
346 label.setText(entry.label);
347 label.setVisibility(View.VISIBLE);
348 }
349
350 TextView number = (TextView) convertView.findViewById(R.id.number);
351 number.setText(entry.number);
352 }
353
354 return convertView;
355 }
356 }
357
358 public void onItemClick(AdapterView parent, View view, int position, long id) {
359 // Handle passing action off to correct handler.
360 if (view.getTag() instanceof ViewEntry) {
361 ViewEntry entry = (ViewEntry) view.getTag();
362 if (entry.intent != null) {
363 startActivity(entry.intent);
364 }
365 }
366 }
367}