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