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.ACCESS_FINE_LOCATION;
19import static android.Manifest.permission.READ_CONTACTS;
20
21import android.app.Activity;
22import android.content.pm.PackageManager;
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;
31
32import com.android.dialer.R;
33import com.android.dialer.service.CachedNumberLookupService;
34import com.android.dialer.widget.EmptyContentView;
35import com.android.dialer.widget.EmptyContentView.OnEmptyViewActionButtonClickedListener;
36
37public 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
47    public RegularSearchFragment() {
48        configureDirectorySearch();
49    }
50
51    @Override
52    public void onStart() {
53        super.onStart();
54        AnalyticsUtil.sendScreenView(this);
55    }
56
57    public void configureDirectorySearch() {
58        setDirectorySearchEnabled(true);
59        setDirectoryResultLimit(SEARCH_DIRECTORY_RESULT_LIMIT);
60    }
61
62    @Override
63    protected void onCreateView(LayoutInflater inflater, ViewGroup container) {
64        super.onCreateView(inflater, container);
65        ((PinnedHeaderListView) getListView()).setScrollToSectionOnHeaderTouch(true);
66    }
67
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
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
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
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
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