• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 static android.Manifest.permission.ACCESS_FINE_LOCATION;
19 import static android.Manifest.permission.READ_CONTACTS;
20 
21 import android.app.Activity;
22 import android.content.pm.PackageManager;
23 import android.view.LayoutInflater;
24 import android.view.ViewGroup;
25 
26 import com.android.contacts.common.list.ContactEntryListAdapter;
27 import com.android.contacts.common.list.PinnedHeaderListView;
28 import com.android.contacts.common.util.PermissionsUtil;
29 import com.android.contacts.commonbind.analytics.AnalyticsUtil;
30 import com.android.dialerbind.ObjectFactory;
31 
32 import com.android.dialer.R;
33 import com.android.dialer.service.CachedNumberLookupService;
34 import com.android.dialer.widget.EmptyContentView;
35 import com.android.dialer.widget.EmptyContentView.OnEmptyViewActionButtonClickedListener;
36 
37 public class RegularSearchFragment extends SearchFragment
38         implements OnEmptyViewActionButtonClickedListener {
39 
40     private static final int READ_CONTACTS_PERMISSION_REQUEST_CODE = 1;
41 
42     private static final int SEARCH_DIRECTORY_RESULT_LIMIT = 5;
43 
44     private static final CachedNumberLookupService mCachedNumberLookupService =
45         ObjectFactory.newCachedNumberLookupService();
46 
RegularSearchFragment()47     public RegularSearchFragment() {
48         configureDirectorySearch();
49     }
50 
51     @Override
onStart()52     public void onStart() {
53         super.onStart();
54         AnalyticsUtil.sendScreenView(this);
55     }
56 
configureDirectorySearch()57     public void configureDirectorySearch() {
58         setDirectorySearchEnabled(true);
59         setDirectoryResultLimit(SEARCH_DIRECTORY_RESULT_LIMIT);
60     }
61 
62     @Override
onCreateView(LayoutInflater inflater, ViewGroup container)63     protected void onCreateView(LayoutInflater inflater, ViewGroup container) {
64         super.onCreateView(inflater, container);
65         ((PinnedHeaderListView) getListView()).setScrollToSectionOnHeaderTouch(true);
66     }
67 
createListAdapter()68     protected ContactEntryListAdapter createListAdapter() {
69         RegularSearchListAdapter adapter = new RegularSearchListAdapter(getActivity());
70         adapter.setDisplayPhotos(true);
71         adapter.setUseCallableUri(usesCallableUri());
72         return adapter;
73     }
74 
75     @Override
cacheContactInfo(int position)76     protected void cacheContactInfo(int position) {
77         if (mCachedNumberLookupService != null) {
78             final RegularSearchListAdapter adapter =
79                 (RegularSearchListAdapter) getAdapter();
80             mCachedNumberLookupService.addContact(getContext(),
81                     adapter.getContactInfo(mCachedNumberLookupService, position));
82         }
83     }
84 
85     @Override
setupEmptyView()86     protected void setupEmptyView() {
87         if (mEmptyView != null && getActivity() != null) {
88             if (!PermissionsUtil.hasPermission(getActivity(), READ_CONTACTS)) {
89                 mEmptyView.setImage(R.drawable.empty_contacts);
90                 mEmptyView.setActionLabel(R.string.permission_single_turn_on);
91                 mEmptyView.setDescription(R.string.permission_no_search);
92                 mEmptyView.setActionClickedListener(this);
93             } else {
94                 mEmptyView.setImage(EmptyContentView.NO_IMAGE);
95                 mEmptyView.setActionLabel(EmptyContentView.NO_LABEL);
96                 mEmptyView.setDescription(EmptyContentView.NO_LABEL);
97             }
98         }
99     }
100 
101     @Override
onEmptyViewActionButtonClicked()102     public void onEmptyViewActionButtonClicked() {
103         final Activity activity = getActivity();
104         if (activity == null) {
105             return;
106         }
107 
108         requestPermissions(new String[] {READ_CONTACTS}, READ_CONTACTS_PERMISSION_REQUEST_CODE);
109     }
110 
111     @Override
onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults)112     public void onRequestPermissionsResult(int requestCode, String[] permissions,
113             int[] grantResults) {
114         if (requestCode == READ_CONTACTS_PERMISSION_REQUEST_CODE) {
115             setupEmptyView();
116         }
117     }
118 }
119