CallLogFragment.java revision 3011bc13a78b918df9c295e5c55ebe2ca09dab13
1/* 2 * Copyright (C) 2011 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.dialer.calllog; 18 19import android.app.Activity; 20import android.app.KeyguardManager; 21import android.app.ListFragment; 22import android.content.Context; 23import android.content.Intent; 24import android.database.ContentObserver; 25import android.database.Cursor; 26import android.net.Uri; 27import android.os.Bundle; 28import android.os.Handler; 29import android.provider.CallLog; 30import android.provider.CallLog.Calls; 31import android.provider.ContactsContract; 32import android.telephony.PhoneNumberUtils; 33import android.telephony.PhoneStateListener; 34import android.telephony.TelephonyManager; 35import android.util.Log; 36import android.view.LayoutInflater; 37import android.view.Menu; 38import android.view.MenuInflater; 39import android.view.MenuItem; 40import android.view.View; 41import android.view.ViewGroup; 42import android.widget.ListView; 43import android.widget.TextView; 44 45import com.android.common.io.MoreCloseables; 46import com.android.contacts.common.CallUtil; 47import com.android.contacts.common.GeoUtil; 48import com.android.dialer.R; 49import com.android.dialer.util.EmptyLoader; 50import com.android.dialer.voicemail.VoicemailStatusHelper; 51import com.android.dialer.voicemail.VoicemailStatusHelper.StatusMessage; 52import com.android.dialer.voicemail.VoicemailStatusHelperImpl; 53import com.android.internal.telephony.ITelephony; 54import com.google.common.annotations.VisibleForTesting; 55 56import java.util.List; 57 58/** 59 * Displays a list of call log entries. To filter for a particular kind of call 60 * (all, missed or voicemails), specify it in the constructor. 61 */ 62public class CallLogFragment extends ListFragment 63 implements CallLogQueryHandler.Listener, CallLogAdapter.CallFetcher { 64 private static final String TAG = "CallLogFragment"; 65 66 /** 67 * ID of the empty loader to defer other fragments. 68 */ 69 private static final int EMPTY_LOADER_ID = 0; 70 71 private CallLogAdapter mAdapter; 72 private CallLogQueryHandler mCallLogQueryHandler; 73 private boolean mScrollToTop; 74 75 /** Whether there is at least one voicemail source installed. */ 76 private boolean mVoicemailSourcesAvailable = false; 77 78 private VoicemailStatusHelper mVoicemailStatusHelper; 79 private View mStatusMessageView; 80 private TextView mStatusMessageText; 81 private TextView mStatusMessageAction; 82 private KeyguardManager mKeyguardManager; 83 84 private boolean mEmptyLoaderRunning; 85 private boolean mCallLogFetched; 86 private boolean mVoicemailStatusFetched; 87 88 private final Handler mHandler = new Handler(); 89 90 private TelephonyManager mTelephonyManager; 91 92 private class CustomContentObserver extends ContentObserver { 93 public CustomContentObserver() { 94 super(mHandler); 95 } 96 @Override 97 public void onChange(boolean selfChange) { 98 mRefreshDataRequired = true; 99 } 100 } 101 102 // See issue 6363009 103 private final ContentObserver mCallLogObserver = new CustomContentObserver(); 104 private final ContentObserver mContactsObserver = new CustomContentObserver(); 105 private boolean mRefreshDataRequired = true; 106 107 // Exactly same variable is in Fragment as a package private. 108 private boolean mMenuVisible = true; 109 110 // Default to all calls. 111 private int mCallTypeFilter = CallLogQueryHandler.CALL_TYPE_ALL; 112 113 // Log limit - if no limit is specified, then the default in {@link CallLogQueryHandler} 114 // will be used. 115 private int mLogLimit = -1; 116 117 public CallLogFragment() { 118 this(CallLogQueryHandler.CALL_TYPE_ALL, -1); 119 } 120 121 public CallLogFragment(int filterType) { 122 this(filterType, -1); 123 } 124 125 public CallLogFragment(int filterType, int logLimit) { 126 super(); 127 mCallTypeFilter = filterType; 128 mLogLimit = logLimit; 129 } 130 131 @Override 132 public void onCreate(Bundle state) { 133 super.onCreate(state); 134 135 mCallLogQueryHandler = new CallLogQueryHandler(getActivity().getContentResolver(), 136 this, mLogLimit); 137 mKeyguardManager = 138 (KeyguardManager) getActivity().getSystemService(Context.KEYGUARD_SERVICE); 139 getActivity().getContentResolver().registerContentObserver( 140 CallLog.CONTENT_URI, true, mCallLogObserver); 141 getActivity().getContentResolver().registerContentObserver( 142 ContactsContract.Contacts.CONTENT_URI, true, mContactsObserver); 143 setHasOptionsMenu(true); 144 updateCallList(mCallTypeFilter); 145 } 146 147 /** Called by the CallLogQueryHandler when the list of calls has been fetched or updated. */ 148 @Override 149 public void onCallsFetched(Cursor cursor) { 150 if (getActivity() == null || getActivity().isFinishing()) { 151 return; 152 } 153 mAdapter.setLoading(false); 154 mAdapter.changeCursor(cursor); 155 // This will update the state of the "Clear call log" menu item. 156 getActivity().invalidateOptionsMenu(); 157 if (mScrollToTop) { 158 final ListView listView = getListView(); 159 // The smooth-scroll animation happens over a fixed time period. 160 // As a result, if it scrolls through a large portion of the list, 161 // each frame will jump so far from the previous one that the user 162 // will not experience the illusion of downward motion. Instead, 163 // if we're not already near the top of the list, we instantly jump 164 // near the top, and animate from there. 165 if (listView.getFirstVisiblePosition() > 5) { 166 listView.setSelection(5); 167 } 168 // Workaround for framework issue: the smooth-scroll doesn't 169 // occur if setSelection() is called immediately before. 170 mHandler.post(new Runnable() { 171 @Override 172 public void run() { 173 if (getActivity() == null || getActivity().isFinishing()) { 174 return; 175 } 176 listView.smoothScrollToPosition(0); 177 } 178 }); 179 180 mScrollToTop = false; 181 } 182 mCallLogFetched = true; 183 destroyEmptyLoaderIfAllDataFetched(); 184 } 185 186 /** 187 * Called by {@link CallLogQueryHandler} after a successful query to voicemail status provider. 188 */ 189 @Override 190 public void onVoicemailStatusFetched(Cursor statusCursor) { 191 if (getActivity() == null || getActivity().isFinishing()) { 192 return; 193 } 194 updateVoicemailStatusMessage(statusCursor); 195 196 int activeSources = mVoicemailStatusHelper.getNumberActivityVoicemailSources(statusCursor); 197 setVoicemailSourcesAvailable(activeSources != 0); 198 MoreCloseables.closeQuietly(statusCursor); 199 mVoicemailStatusFetched = true; 200 destroyEmptyLoaderIfAllDataFetched(); 201 } 202 203 private void destroyEmptyLoaderIfAllDataFetched() { 204 if (mCallLogFetched && mVoicemailStatusFetched && mEmptyLoaderRunning) { 205 mEmptyLoaderRunning = false; 206 getLoaderManager().destroyLoader(EMPTY_LOADER_ID); 207 } 208 } 209 210 /** Sets whether there are any voicemail sources available in the platform. */ 211 private void setVoicemailSourcesAvailable(boolean voicemailSourcesAvailable) { 212 if (mVoicemailSourcesAvailable == voicemailSourcesAvailable) return; 213 mVoicemailSourcesAvailable = voicemailSourcesAvailable; 214 215 Activity activity = getActivity(); 216 if (activity != null) { 217 // This is so that the options menu content is updated. 218 activity.invalidateOptionsMenu(); 219 } 220 } 221 222 @Override 223 public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedState) { 224 View view = inflater.inflate(R.layout.call_log_fragment, container, false); 225 mVoicemailStatusHelper = new VoicemailStatusHelperImpl(); 226 mStatusMessageView = view.findViewById(R.id.voicemail_status); 227 mStatusMessageText = (TextView) view.findViewById(R.id.voicemail_status_message); 228 mStatusMessageAction = (TextView) view.findViewById(R.id.voicemail_status_action); 229 return view; 230 } 231 232 @Override 233 public void onViewCreated(View view, Bundle savedInstanceState) { 234 super.onViewCreated(view, savedInstanceState); 235 updateEmptyMessage(mCallTypeFilter); 236 String currentCountryIso = GeoUtil.getCurrentCountryIso(getActivity()); 237 mAdapter = new CallLogAdapter(getActivity(), this, 238 new ContactInfoHelper(getActivity(), currentCountryIso), false); 239 setListAdapter(mAdapter); 240 getListView().setItemsCanFocus(true); 241 } 242 243 /** 244 * Based on the new intent, decide whether the list should be configured 245 * to scroll up to display the first item. 246 */ 247 public void configureScreenFromIntent(Intent newIntent) { 248 // Typically, when switching to the call-log we want to show the user 249 // the same section of the list that they were most recently looking 250 // at. However, under some circumstances, we want to automatically 251 // scroll to the top of the list to present the newest call items. 252 // For example, immediately after a call is finished, we want to 253 // display information about that call. 254 mScrollToTop = Calls.CONTENT_TYPE.equals(newIntent.getType()); 255 } 256 257 @Override 258 public void onStart() { 259 // Start the empty loader now to defer other fragments. We destroy it when both calllog 260 // and the voicemail status are fetched. 261 getLoaderManager().initLoader(EMPTY_LOADER_ID, null, 262 new EmptyLoader.Callback(getActivity())); 263 mEmptyLoaderRunning = true; 264 super.onStart(); 265 } 266 267 @Override 268 public void onResume() { 269 super.onResume(); 270 refreshData(); 271 } 272 273 private void updateVoicemailStatusMessage(Cursor statusCursor) { 274 List<StatusMessage> messages = mVoicemailStatusHelper.getStatusMessages(statusCursor); 275 if (messages.size() == 0) { 276 mStatusMessageView.setVisibility(View.GONE); 277 } else { 278 mStatusMessageView.setVisibility(View.VISIBLE); 279 // TODO: Change the code to show all messages. For now just pick the first message. 280 final StatusMessage message = messages.get(0); 281 if (message.showInCallLog()) { 282 mStatusMessageText.setText(message.callLogMessageId); 283 } 284 if (message.actionMessageId != -1) { 285 mStatusMessageAction.setText(message.actionMessageId); 286 } 287 if (message.actionUri != null) { 288 mStatusMessageAction.setVisibility(View.VISIBLE); 289 mStatusMessageAction.setOnClickListener(new View.OnClickListener() { 290 @Override 291 public void onClick(View v) { 292 getActivity().startActivity( 293 new Intent(Intent.ACTION_VIEW, message.actionUri)); 294 } 295 }); 296 } else { 297 mStatusMessageAction.setVisibility(View.GONE); 298 } 299 } 300 } 301 302 @Override 303 public void onPause() { 304 super.onPause(); 305 // Kill the requests thread 306 mAdapter.stopRequestProcessing(); 307 } 308 309 @Override 310 public void onStop() { 311 super.onStop(); 312 updateOnExit(); 313 } 314 315 @Override 316 public void onDestroy() { 317 super.onDestroy(); 318 mAdapter.stopRequestProcessing(); 319 mAdapter.changeCursor(null); 320 getActivity().getContentResolver().unregisterContentObserver(mCallLogObserver); 321 getActivity().getContentResolver().unregisterContentObserver(mContactsObserver); 322 } 323 324 @Override 325 public void fetchCalls() { 326 mCallLogQueryHandler.fetchCalls(mCallTypeFilter); 327 } 328 329 public void startCallsQuery() { 330 mAdapter.setLoading(true); 331 mCallLogQueryHandler.fetchCalls(mCallTypeFilter); 332 } 333 334 private void startVoicemailStatusQuery() { 335 mCallLogQueryHandler.fetchVoicemailStatus(); 336 } 337 338 private void updateCallList(int filterType) { 339 mCallLogQueryHandler.fetchCalls(filterType); 340 } 341 342 private void updateEmptyMessage(int filterType) { 343 final String message; 344 switch (filterType) { 345 case Calls.MISSED_TYPE: 346 message = getString(R.string.recentMissed_empty); 347 break; 348 case CallLogQueryHandler.CALL_TYPE_ALL: 349 message = getString(R.string.recentCalls_empty); 350 break; 351 default: 352 throw new IllegalArgumentException("Unexpected filter type in CallLogFragment: " 353 + filterType); 354 } 355 ((TextView) getListView().getEmptyView()).setText(message); 356 } 357 358 public void callSelectedEntry() { 359 int position = getListView().getSelectedItemPosition(); 360 if (position < 0) { 361 // In touch mode you may often not have something selected, so 362 // just call the first entry to make sure that [send] [send] calls the 363 // most recent entry. 364 position = 0; 365 } 366 final Cursor cursor = (Cursor)mAdapter.getItem(position); 367 if (cursor != null) { 368 String number = cursor.getString(CallLogQuery.NUMBER); 369 int numberPresentation = cursor.getInt(CallLogQuery.NUMBER_PRESENTATION); 370 if (!PhoneNumberHelper.canPlaceCallsTo(number, numberPresentation)) { 371 // This number can't be called, do nothing 372 return; 373 } 374 Intent intent; 375 // If "number" is really a SIP address, construct a sip: URI. 376 if (PhoneNumberUtils.isUriNumber(number)) { 377 intent = CallUtil.getCallIntent( 378 Uri.fromParts(CallUtil.SCHEME_SIP, number, null)); 379 } else { 380 // We're calling a regular PSTN phone number. 381 // Construct a tel: URI, but do some other possible cleanup first. 382 int callType = cursor.getInt(CallLogQuery.CALL_TYPE); 383 if (!number.startsWith("+") && 384 (callType == Calls.INCOMING_TYPE 385 || callType == Calls.MISSED_TYPE)) { 386 // If the caller-id matches a contact with a better qualified number, use it 387 String countryIso = cursor.getString(CallLogQuery.COUNTRY_ISO); 388 number = mAdapter.getBetterNumberFromContacts(number, countryIso); 389 } 390 intent = CallUtil.getCallIntent( 391 Uri.fromParts(CallUtil.SCHEME_TEL, number, null)); 392 } 393 intent.setFlags( 394 Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS); 395 startActivity(intent); 396 } 397 } 398 399 CallLogAdapter getAdapter() { 400 return mAdapter; 401 } 402 403 @Override 404 public void setMenuVisibility(boolean menuVisible) { 405 super.setMenuVisibility(menuVisible); 406 if (mMenuVisible != menuVisible) { 407 mMenuVisible = menuVisible; 408 if (!menuVisible) { 409 updateOnExit(); 410 } else if (isResumed()) { 411 refreshData(); 412 } 413 } 414 } 415 416 /** Requests updates to the data to be shown. */ 417 private void refreshData() { 418 // Prevent unnecessary refresh. 419 if (mRefreshDataRequired) { 420 // Mark all entries in the contact info cache as out of date, so they will be looked up 421 // again once being shown. 422 mAdapter.invalidateCache(); 423 startCallsQuery(); 424 startVoicemailStatusQuery(); 425 updateOnEntry(); 426 mRefreshDataRequired = false; 427 } 428 } 429 430 /** Updates call data and notification state while leaving the call log tab. */ 431 private void updateOnExit() { 432 updateOnTransition(false); 433 } 434 435 /** Updates call data and notification state while entering the call log tab. */ 436 private void updateOnEntry() { 437 updateOnTransition(true); 438 } 439 440 // TODO krelease: Figure out if we still need this. If so, it should be probably be moved to 441 // the call log activity instead, or done only in a single call log fragment. 442 private void updateOnTransition(boolean onEntry) { 443 // We don't want to update any call data when keyguard is on because the user has likely not 444 // seen the new calls yet. 445 // This might be called before onCreate() and thus we need to check null explicitly. 446 if (mKeyguardManager != null && !mKeyguardManager.inKeyguardRestrictedInputMode()) { 447 // On either of the transitions we reset the new flag and update the notifications. 448 // While exiting we additionally consume all missed calls (by marking them as read). 449 // This will ensure that they no more appear in the "new" section when we return back. 450 mCallLogQueryHandler.markNewCallsAsOld(); 451 if (!onEntry) { 452 mCallLogQueryHandler.markMissedCallsAsRead(); 453 } 454 CallLogNotificationsHelper.removeMissedCallNotifications(); 455 CallLogNotificationsHelper.updateVoicemailNotifications(getActivity()); 456 } 457 } 458} 459