1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
|
/*
* Copyright (C) 2017 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License
*/
package com.android.dialer.searchfragment.list;
import android.app.Activity;
import android.content.Intent;
import android.support.annotation.VisibleForTesting;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.RecyclerView.ViewHolder;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.ViewGroup;
import com.android.dialer.callcomposer.CallComposerActivity;
import com.android.dialer.callintent.CallInitiationType;
import com.android.dialer.callintent.CallIntentBuilder;
import com.android.dialer.callintent.CallSpecificAppData;
import com.android.dialer.common.Assert;
import com.android.dialer.constants.ActivityRequestCodes;
import com.android.dialer.dialercontact.DialerContact;
import com.android.dialer.lightbringer.LightbringerComponent;
import com.android.dialer.logging.DialerImpression;
import com.android.dialer.logging.Logger;
import com.android.dialer.searchfragment.common.RowClickListener;
import com.android.dialer.searchfragment.common.SearchCursor;
import com.android.dialer.searchfragment.cp2.SearchContactViewHolder;
import com.android.dialer.searchfragment.list.SearchCursorManager.RowType;
import com.android.dialer.searchfragment.nearbyplaces.NearbyPlaceViewHolder;
import com.android.dialer.searchfragment.remote.RemoteContactViewHolder;
import com.android.dialer.util.DialerUtils;
import java.util.List;
/** RecyclerView adapter for {@link NewSearchFragment}. */
@VisibleForTesting(otherwise = VisibleForTesting.PACKAGE_PRIVATE)
public final class SearchAdapter extends RecyclerView.Adapter<ViewHolder>
implements RowClickListener {
private final SearchCursorManager searchCursorManager;
private final Activity activity;
private boolean showZeroSuggest;
private String query;
private CallInitiationType.Type callInitiationType = CallInitiationType.Type.UNKNOWN_INITIATION;
@VisibleForTesting(otherwise = VisibleForTesting.PACKAGE_PRIVATE)
public SearchAdapter(Activity activity, SearchCursorManager searchCursorManager) {
this.activity = activity;
this.searchCursorManager = searchCursorManager;
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup root, @RowType int rowType) {
switch (rowType) {
case RowType.CONTACT_ROW:
return new SearchContactViewHolder(
LayoutInflater.from(activity).inflate(R.layout.search_contact_row, root, false), this);
case RowType.NEARBY_PLACES_ROW:
return new NearbyPlaceViewHolder(
LayoutInflater.from(activity).inflate(R.layout.search_contact_row, root, false));
case RowType.CONTACT_HEADER:
case RowType.DIRECTORY_HEADER:
case RowType.NEARBY_PLACES_HEADER:
return new HeaderViewHolder(
LayoutInflater.from(activity).inflate(R.layout.header_layout, root, false));
case RowType.DIRECTORY_ROW:
return new RemoteContactViewHolder(
LayoutInflater.from(activity).inflate(R.layout.search_contact_row, root, false));
case RowType.SEARCH_ACTION:
return new SearchActionViewHolder(
LayoutInflater.from(activity).inflate(R.layout.search_action_layout, root, false));
case RowType.INVALID:
default:
throw Assert.createIllegalStateFailException("Invalid RowType: " + rowType);
}
}
@Override
public @RowType int getItemViewType(int position) {
return searchCursorManager.getRowType(position);
}
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
if (holder instanceof SearchContactViewHolder) {
((SearchContactViewHolder) holder).bind(searchCursorManager.getCursor(position), query);
} else if (holder instanceof NearbyPlaceViewHolder) {
((NearbyPlaceViewHolder) holder).bind(searchCursorManager.getCursor(position), query);
} else if (holder instanceof RemoteContactViewHolder) {
((RemoteContactViewHolder) holder).bind(searchCursorManager.getCursor(position), query);
} else if (holder instanceof HeaderViewHolder) {
String header =
searchCursorManager.getCursor(position).getString(SearchCursor.HEADER_TEXT_POSITION);
((HeaderViewHolder) holder).setHeader(header);
} else if (holder instanceof SearchActionViewHolder) {
((SearchActionViewHolder) holder)
.setAction(searchCursorManager.getSearchAction(position), position, query);
} else {
throw Assert.createIllegalStateFailException("Invalid ViewHolder: " + holder);
}
}
public void setContactsCursor(SearchCursor cursor) {
if (searchCursorManager.setContactsCursor(cursor)) {
// Since this is a new contacts cursor, we need to reapply the filter.
searchCursorManager.setQuery(query);
notifyDataSetChanged();
}
}
void clear() {
searchCursorManager.clear();
}
@Override
public int getItemCount() {
if (TextUtils.isEmpty(query) && !showZeroSuggest) {
return 0;
}
return searchCursorManager.getCount();
}
/**
* @param visible If true and query is empty, the adapter won't show any list elements.
* @see #setQuery(String)
* @see #getItemCount()
*/
public void setZeroSuggestVisible(boolean visible) {
showZeroSuggest = visible;
}
public void setQuery(String query) {
this.query = query;
if (searchCursorManager.setQuery(query)) {
notifyDataSetChanged();
}
}
/** Sets the actions to be shown at the bottom of the search results. */
void setSearchActions(List<Integer> actions) {
if (searchCursorManager.setSearchActions(actions)) {
notifyDataSetChanged();
}
}
void setCallInitiationType(CallInitiationType.Type callInitiationType) {
this.callInitiationType = callInitiationType;
}
public void setNearbyPlacesCursor(SearchCursor nearbyPlacesCursor) {
if (searchCursorManager.setNearbyPlacesCursor(nearbyPlacesCursor)) {
notifyDataSetChanged();
}
}
public void setRemoteContactsCursor(SearchCursor remoteContactsCursor) {
if (searchCursorManager.setCorpDirectoryCursor(remoteContactsCursor)) {
notifyDataSetChanged();
}
}
@Override
public void placeVoiceCall(String phoneNumber, int ranking) {
placeCall(phoneNumber, ranking, false);
}
@Override
public void placeVideoCall(String phoneNumber, int ranking) {
placeCall(phoneNumber, ranking, true);
}
private void placeCall(String phoneNumber, int position, boolean isVideoCall) {
CallSpecificAppData callSpecificAppData =
CallSpecificAppData.newBuilder()
.setCallInitiationType(callInitiationType)
.setPositionOfSelectedSearchResult(position)
.setCharactersInSearchString(query == null ? 0 : query.length())
.build();
Intent intent =
new CallIntentBuilder(phoneNumber, callSpecificAppData).setIsVideoCall(isVideoCall).build();
DialerUtils.startActivityWithErrorToast(activity, intent);
}
@Override
public void placeDuoCall(String phoneNumber) {
Logger.get(activity)
.logImpression(DialerImpression.Type.LIGHTBRINGER_VIDEO_REQUESTED_FROM_SEARCH);
Intent intent =
LightbringerComponent.get(activity).getLightbringer().getIntent(activity, phoneNumber);
activity.startActivityForResult(intent, ActivityRequestCodes.DIALTACTS_LIGHTBRINGER);
}
@Override
public void openCallAndShare(DialerContact contact) {
Intent intent = CallComposerActivity.newIntent(activity, contact);
DialerUtils.startActivityWithErrorToast(activity, intent);
}
}
|