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 */ 16package com.android.dialer.list; 17 18import static android.Manifest.permission.READ_CONTACTS; 19 20import android.app.Activity; 21import android.content.pm.PackageManager; 22import android.support.v13.app.FragmentCompat; 23import android.view.LayoutInflater; 24import android.view.ViewGroup; 25 26import com.android.contacts.common.list.ContactEntryListAdapter; 27import com.android.contacts.common.list.PinnedHeaderListView; 28import com.android.contacts.common.util.PermissionsUtil; 29import com.android.contacts.commonbind.analytics.AnalyticsUtil; 30import com.android.dialerbind.ObjectFactory; 31import com.android.incallui.Call.LogState; 32 33import com.android.dialer.R; 34import com.android.dialer.logging.Logger; 35import com.android.dialer.logging.ScreenEvent; 36import com.android.dialer.service.CachedNumberLookupService; 37import com.android.dialer.widget.EmptyContentView; 38import com.android.dialer.widget.EmptyContentView.OnEmptyViewActionButtonClickedListener; 39 40public class RegularSearchFragment extends SearchFragment 41 implements OnEmptyViewActionButtonClickedListener, 42 FragmentCompat.OnRequestPermissionsResultCallback { 43 44 public static final int PERMISSION_REQUEST_CODE = 1; 45 46 private static final int SEARCH_DIRECTORY_RESULT_LIMIT = 5; 47 48 private static final CachedNumberLookupService mCachedNumberLookupService = 49 ObjectFactory.newCachedNumberLookupService(); 50 51 public interface CapabilityChecker { 52 public boolean isNearbyPlacesSearchEnabled(); 53 } 54 55 protected String mPermissionToRequest; 56 57 public RegularSearchFragment() { 58 configureDirectorySearch(); 59 } 60 61 public void configureDirectorySearch() { 62 setDirectorySearchEnabled(true); 63 setDirectoryResultLimit(SEARCH_DIRECTORY_RESULT_LIMIT); 64 } 65 66 @Override 67 protected void onCreateView(LayoutInflater inflater, ViewGroup container) { 68 super.onCreateView(inflater, container); 69 ((PinnedHeaderListView) getListView()).setScrollToSectionOnHeaderTouch(true); 70 } 71 72 @Override 73 protected ContactEntryListAdapter createListAdapter() { 74 RegularSearchListAdapter adapter = new RegularSearchListAdapter(getActivity()); 75 adapter.setDisplayPhotos(true); 76 adapter.setUseCallableUri(usesCallableUri()); 77 adapter.setListener(this); 78 return adapter; 79 } 80 81 @Override 82 protected void cacheContactInfo(int position) { 83 if (mCachedNumberLookupService != null) { 84 final RegularSearchListAdapter adapter = 85 (RegularSearchListAdapter) getAdapter(); 86 mCachedNumberLookupService.addContact(getContext(), 87 adapter.getContactInfo(mCachedNumberLookupService, position)); 88 } 89 } 90 91 @Override 92 protected void setupEmptyView() { 93 if (mEmptyView != null && getActivity() != null) { 94 final int imageResource; 95 final int actionLabelResource; 96 final int descriptionResource; 97 final OnEmptyViewActionButtonClickedListener listener; 98 if (!PermissionsUtil.hasPermission(getActivity(), READ_CONTACTS)) { 99 imageResource = R.drawable.empty_contacts; 100 actionLabelResource = R.string.permission_single_turn_on; 101 descriptionResource = R.string.permission_no_search; 102 listener = this; 103 mPermissionToRequest = READ_CONTACTS; 104 } else { 105 imageResource = EmptyContentView.NO_IMAGE; 106 actionLabelResource = EmptyContentView.NO_LABEL; 107 descriptionResource = EmptyContentView.NO_LABEL; 108 listener = null; 109 mPermissionToRequest = null; 110 } 111 112 mEmptyView.setImage(imageResource); 113 mEmptyView.setActionLabel(actionLabelResource); 114 mEmptyView.setDescription(descriptionResource); 115 if (listener != null) { 116 mEmptyView.setActionClickedListener(listener); 117 } 118 } 119 } 120 121 @Override 122 public void onEmptyViewActionButtonClicked() { 123 final Activity activity = getActivity(); 124 if (activity == null) { 125 return; 126 } 127 128 if (READ_CONTACTS.equals(mPermissionToRequest)) { 129 FragmentCompat.requestPermissions(this, new String[] {mPermissionToRequest}, 130 PERMISSION_REQUEST_CODE); 131 } 132 } 133 134 @Override 135 public void onRequestPermissionsResult(int requestCode, String[] permissions, 136 int[] grantResults) { 137 if (requestCode == PERMISSION_REQUEST_CODE) { 138 setupEmptyView(); 139 if (grantResults != null && grantResults.length == 1 140 && PackageManager.PERMISSION_GRANTED == grantResults[0]) { 141 PermissionsUtil.notifyPermissionGranted(getActivity(), mPermissionToRequest); 142 } 143 } 144 } 145 146 @Override 147 protected int getCallInitiationType(boolean isRemoteDirectory) { 148 return isRemoteDirectory ? LogState.INITIATION_REMOTE_DIRECTORY 149 : LogState.INITIATION_REGULAR_SEARCH; 150 } 151} 152