1 /* 2 * Copyright (C) 2013 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 package com.android.dialer.list; 17 18 import android.app.Activity; 19 import android.content.ActivityNotFoundException; 20 import android.content.Intent; 21 import android.widget.AbsListView; 22 import android.widget.AbsListView.OnScrollListener; 23 import android.widget.Toast; 24 25 import com.android.contacts.common.list.ContactEntryListAdapter; 26 import com.android.contacts.common.list.ContactListItemView; 27 import com.android.contacts.common.list.OnPhoneNumberPickerActionListener; 28 import com.android.contacts.common.list.PhoneNumberPickerFragment; 29 import com.android.dialer.DialtactsActivity; 30 import com.android.dialer.R; 31 import com.android.dialer.dialpad.DialpadFragment; 32 import com.android.dialer.list.OnListFragmentScrolledListener; 33 34 public class SearchFragment extends PhoneNumberPickerFragment { 35 36 private OnListFragmentScrolledListener mActivityScrollListener; 37 38 @Override 39 public void onAttach(Activity activity) { 40 super.onAttach(activity); 41 42 setQuickContactEnabled(true); 43 setDarkTheme(false); 44 setPhotoPosition(ContactListItemView.getDefaultPhotoPosition(true /* opposite */)); 45 setUseCallableUri(true); 46 47 try { 48 mActivityScrollListener = (OnListFragmentScrolledListener) activity; 49 } catch (ClassCastException e) { 50 throw new ClassCastException(activity.toString() 51 + " must implement OnListFragmentScrolledListener"); 52 } 53 } 54 55 @Override 56 public void onStart() { 57 super.onStart(); 58 if (isSearchMode()) { 59 getAdapter().setHasHeader(0, false); 60 } 61 getListView().setOnScrollListener(new OnScrollListener() { 62 @Override 63 public void onScrollStateChanged(AbsListView view, int scrollState) { 64 mActivityScrollListener.onListFragmentScrollStateChange(scrollState); 65 } 66 67 @Override 68 public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, 69 int totalItemCount) { 70 } 71 }); 72 } 73 74 @Override 75 protected void setSearchMode(boolean flag) { 76 super.setSearchMode(flag); 77 // This hides the "All contacts with phone numbers" header in the search fragment 78 final ContactEntryListAdapter adapter = getAdapter(); 79 if (adapter != null) { 80 adapter.setHasHeader(0, false); 81 } 82 } 83 84 @Override 85 protected ContactEntryListAdapter createListAdapter() { 86 DialerPhoneNumberListAdapter adapter = new DialerPhoneNumberListAdapter(getActivity()); 87 adapter.setDisplayPhotos(true); 88 adapter.setUseCallableUri(super.usesCallableUri()); 89 return adapter; 90 } 91 92 @Override 93 protected void onItemClick(int position, long id) { 94 final DialerPhoneNumberListAdapter adapter = (DialerPhoneNumberListAdapter) getAdapter(); 95 final int shortcutType = adapter.getShortcutTypeFromPosition(position); 96 97 if (shortcutType == DialerPhoneNumberListAdapter.SHORTCUT_INVALID) { 98 super.onItemClick(position, id); 99 } else if (shortcutType == DialerPhoneNumberListAdapter.SHORTCUT_DIRECT_CALL) { 100 final OnPhoneNumberPickerActionListener listener = 101 getOnPhoneNumberPickerListener(); 102 if (listener != null) { 103 listener.onCallNumberDirectly(getQueryString()); 104 } 105 } else if (shortcutType == DialerPhoneNumberListAdapter.SHORTCUT_ADD_NUMBER_TO_CONTACTS) { 106 final String number = adapter.getFormattedQueryString(); 107 final Intent intent = DialtactsActivity.getAddNumberToContactIntent(number); 108 startActivityWithErrorToast(intent); 109 } 110 } 111 112 private void startActivityWithErrorToast(Intent intent) { 113 try { 114 startActivity(intent); 115 } catch (ActivityNotFoundException e) { 116 Toast toast = Toast.makeText(getActivity(), R.string.add_contact_not_available, 117 Toast.LENGTH_SHORT); 118 toast.show(); 119 } 120 } 121 } 122