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.CALL_PHONE;
19
20import android.app.Activity;
21import android.content.Loader;
22import android.database.Cursor;
23import android.net.Uri;
24import android.os.Bundle;
25import android.util.Log;
26import android.view.View;
27
28import com.android.contacts.common.list.ContactEntryListAdapter;
29import com.android.contacts.common.util.PermissionsUtil;
30import com.android.dialer.dialpad.SmartDialCursorLoader;
31import com.android.dialer.R;
32import com.android.dialer.widget.EmptyContentView;
33
34import java.util.ArrayList;
35
36/**
37 * Implements a fragment to load and display SmartDial search results.
38 */
39public class SmartDialSearchFragment extends SearchFragment
40        implements EmptyContentView.OnEmptyViewActionButtonClickedListener {
41    private static final String TAG = SmartDialSearchFragment.class.getSimpleName();
42
43    private static final int CALL_PHONE_PERMISSION_REQUEST_CODE = 1;
44
45    /**
46     * Creates a SmartDialListAdapter to display and operate on search results.
47     */
48    @Override
49    protected ContactEntryListAdapter createListAdapter() {
50        SmartDialNumberListAdapter adapter = new SmartDialNumberListAdapter(getActivity());
51        adapter.setUseCallableUri(super.usesCallableUri());
52        adapter.setQuickContactEnabled(true);
53        // Set adapter's query string to restore previous instance state.
54        adapter.setQueryString(getQueryString());
55        return adapter;
56    }
57
58    /**
59     * Creates a SmartDialCursorLoader object to load query results.
60     */
61    @Override
62    public Loader<Cursor> onCreateLoader(int id, Bundle args) {
63        // Smart dialing does not support Directory Load, falls back to normal search instead.
64        if (id == getDirectoryLoaderId()) {
65            return super.onCreateLoader(id, args);
66        } else {
67            final SmartDialNumberListAdapter adapter = (SmartDialNumberListAdapter) getAdapter();
68            SmartDialCursorLoader loader = new SmartDialCursorLoader(super.getContext());
69            adapter.configureLoader(loader);
70            return loader;
71        }
72    }
73
74    /**
75     * Gets the Phone Uri of an entry for calling.
76     * @param position Location of the data of interest.
77     * @return Phone Uri to establish a phone call.
78     */
79    @Override
80    protected Uri getPhoneUri(int position) {
81        final SmartDialNumberListAdapter adapter = (SmartDialNumberListAdapter) getAdapter();
82        return adapter.getDataUri(position);
83    }
84
85    @Override
86    protected void setupEmptyView() {
87        if (mEmptyView != null && getActivity() != null) {
88            if (!PermissionsUtil.hasPermission(getActivity(), CALL_PHONE)) {
89                mEmptyView.setImage(R.drawable.empty_contacts);
90                mEmptyView.setActionLabel(R.string.permission_single_turn_on);
91                mEmptyView.setDescription(R.string.permission_place_call);
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[] {CALL_PHONE}, CALL_PHONE_PERMISSION_REQUEST_CODE);
109    }
110
111    @Override
112    public void onRequestPermissionsResult(int requestCode, String[] permissions,
113            int[] grantResults) {
114        if (requestCode == CALL_PHONE_PERMISSION_REQUEST_CODE) {
115            setupEmptyView();
116        }
117    }
118
119    public boolean isShowingPermissionRequest() {
120        return mEmptyView != null && mEmptyView.isShowingContent();
121    }
122}
123