forked from cardinalblue/Obama-Chat-Atlas-Android
-
Notifications
You must be signed in to change notification settings - Fork 0
/
AtlasParticipantPicker.java
409 lines (346 loc) · 18.6 KB
/
AtlasParticipantPicker.java
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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
/*
* Copyright (c) 2015 Layer. All rights reserved.
*
* 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.layer.atlas;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Map;
import java.util.TreeSet;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Typeface;
import android.graphics.drawable.GradientDrawable;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.AttributeSet;
import android.util.Log;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.ListView;
import android.widget.TextView;
import com.layer.atlas.Atlas.Participant;
import com.layer.atlas.Atlas.ParticipantProvider;
/**
* @author Oleg Orlov
* @since 27 Apr 2015
*/
public class AtlasParticipantPicker extends FrameLayout {
private static final String TAG = AtlasParticipantPicker.class.getSimpleName();
private static final boolean debug = false;
// participants picker
private View rootView;
private EditText textFilter;
private ListView participantsList;
private ViewGroup selectedParticipantsContainer;
private ParticipantProvider participantProvider;
private TreeSet<String> skipUserIds = new TreeSet<String>();
private final Map<String, Participant> filteredParticipants = new HashMap<String, Participant>();
private ArrayList<String> selectedParticipantIds = new ArrayList<String>();
private final ArrayList<ParticipantEntry> participantsForAdapter = new ArrayList<ParticipantEntry>();
private BaseAdapter participantsAdapter;
// styles
private int inputTextColor;
private Typeface inputTextTypeface;
private int inputTextStyle;
private int listTextColor;
private Typeface listTextTypeface;
private int listTextStyle;
private int chipBackgroundColor;
private int chipTextColor;
private Typeface chipTextTypeface;
private int chipTextStyle;
public AtlasParticipantPicker(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
parseStyle(context, attrs, defStyle);
}
public AtlasParticipantPicker(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public AtlasParticipantPicker(Context context) {
super(context);
}
public void init(String[] userIdToSkip, ParticipantProvider participantProvider) {
if (participantProvider == null) throw new IllegalArgumentException("ParticipantProvider cannot be null");
LayoutInflater.from(getContext()).inflate(R.layout.atlas_participants_picker, this);
this.participantProvider = participantProvider;
if (userIdToSkip != null) skipUserIds.addAll(Arrays.asList(userIdToSkip));
// START OF -------------------- Participant Picker ----------------------------------------
this.rootView = this;
textFilter = (EditText) rootView.findViewById(R.id.atlas_participants_picker_text);
participantsList = (ListView) rootView.findViewById(R.id.atlas_participants_picker_list);
selectedParticipantsContainer = (ViewGroup) rootView.findViewById(R.id.atlas_participants_picker_names);
if (rootView.getVisibility() == View.VISIBLE) {
textFilter.requestFocus();
}
// log focuses
final View scroller = rootView.findViewById(R.id.atlas_participants_picker_scroll);
scroller.setOnFocusChangeListener(new OnFocusChangeListener() {
@Override
public void onFocusChange(View v, boolean hasFocus) {
if (debug) Log.w(TAG, "scroller.onFocusChange() hasFocus: " + hasFocus);
}
});
selectedParticipantsContainer.setOnFocusChangeListener(new OnFocusChangeListener() {
public void onFocusChange(View v, boolean hasFocus) {
if (debug) Log.w(TAG, "names.onFocusChange() hasFocus: " + hasFocus);
}
});
// If filter.requestFocus is called from .onClickListener - filter receives focus, but
// NamesLayout receives it immediately after that. So filter lose it.
// XXX: scroller also receives focus
selectedParticipantsContainer.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
if (debug) Log.w(TAG, "names.onTouch() event: " + event);
if (event.getAction() == MotionEvent.ACTION_DOWN) // ACTION_UP never comes if
textFilter.requestFocus(); // there is no .onClickListener
return false;
}
});
textFilter.setOnFocusChangeListener(new OnFocusChangeListener() {
public void onFocusChange(View v, boolean hasFocus) {
View focused = selectedParticipantsContainer.hasFocus() ? selectedParticipantsContainer : selectedParticipantsContainer.findFocus();
if (debug) Log.w(TAG, "filter.onFocusChange() hasFocus: " + hasFocus + ", focused: " + focused);
if (hasFocus) {
participantsList.setVisibility(View.VISIBLE);
}
v.post(new Runnable() { // check focus runnable
@Override
public void run() {
if (debug) Log.w(TAG, "filter.onFocusChange.run() filter.focus: " + textFilter.hasFocus());
if (debug) Log.w(TAG, "filter.onFocusChange.run() names.focus: " + selectedParticipantsContainer.hasFocus());
if (debug) Log.w(TAG, "filter.onFocusChange.run() scroller.focus: " + scroller.hasFocus());
// check focus is on any descendants and hide list otherwise
View focused = selectedParticipantsContainer.hasFocus() ? selectedParticipantsContainer : selectedParticipantsContainer.findFocus();
if (focused == null) {
participantsList.setVisibility(View.GONE);
textFilter.setText("");
}
}
});
}
});
participantsList.setAdapter(participantsAdapter = new BaseAdapter() {
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.atlas_view_participants_picker_convert, parent, false);
}
TextView name = (TextView) convertView.findViewById(R.id.atlas_view_participants_picker_convert_name);
TextView avatarText = (TextView) convertView.findViewById(R.id.atlas_view_participants_picker_convert_ava);
ParticipantEntry entry = participantsForAdapter.get(position);
if (entry != null) {
name.setText(Atlas.getFullName(entry.participant));
avatarText.setText(Atlas.getInitials(entry.participant));
} else {
name.setText(null);
avatarText.setText(null);
}
// apply styles
name.setTextColor(listTextColor);
name.setTypeface(listTextTypeface, listTextStyle);
avatarText.setTextColor(listTextColor);
avatarText.setTypeface(listTextTypeface, listTextStyle);
return convertView;
}
public long getItemId(int position) {
return participantsForAdapter.get(position).id.hashCode();
}
public Object getItem(int position) {
return participantsForAdapter.get(position);
}
public int getCount() {
return participantsForAdapter.size();
}
});
participantsList.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
ParticipantEntry entry = participantsForAdapter.get(position);
selectedParticipantIds.add(entry.id);
refreshParticipants(selectedParticipantIds);
textFilter.setText("");
textFilter.requestFocus();
filterParticipants(""); // refresh participantList
}
});
// track text and filter participant list
textFilter.addTextChangedListener(new TextWatcher() {
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
if (debug) Log.w(TAG, "beforeTextChanged() s: " + s + " start: " + start + " count: " + count + " after: " + after);
}
public void onTextChanged(CharSequence s, int start, int before, int count) {
if (debug) Log.w(TAG, "onTextChanged() s: " + s + " start: " + start + " before: " + before + " count: " + count);
final String filter = s.toString().toLowerCase();
filterParticipants(filter);
}
public void afterTextChanged(Editable s) {
if (debug) Log.w(TAG, "afterTextChanged() s: " + s);
}
});
// select last added participant when press "Backspace/Del"
textFilter.setOnKeyListener(new OnKeyListener() {
public boolean onKey(View v, int keyCode, KeyEvent event) {
if (debug) Log.w(TAG, "onKey() keyCode: " + keyCode + ", event: " + event);
if (keyCode == KeyEvent.KEYCODE_DEL && event.getAction() == KeyEvent.ACTION_DOWN && textFilter.getText().length() == 0 && selectedParticipantIds.size() > 0) {
selectedParticipantIds.remove(selectedParticipantIds.size() - 1);
refreshParticipants(selectedParticipantIds);
filterParticipants("");
textFilter.requestFocus();
}
return false;
}
});
// END OF ---------------------- Participant Picker ----------------------------------------
filterParticipants("");
applyStyle();
}
private void refreshParticipants(final ArrayList<String> selectedParticipantIds) {
// remove name_converts first. Better to keep editText in place rather than add/remove that force keyboard to blink
for (int i = selectedParticipantsContainer.getChildCount() - 1; i >= 0; i--) {
View child = selectedParticipantsContainer.getChildAt(i);
if (child != textFilter) {
selectedParticipantsContainer.removeView(child);
}
}
if (debug) Log.w(TAG, "refreshParticipants() childs left: " + selectedParticipantsContainer.getChildCount());
for (String id : selectedParticipantIds) {
Participant entry = participantProvider.getParticipant(id);
View participantView = LayoutInflater.from(selectedParticipantsContainer.getContext()).inflate(R.layout.atlas_view_participants_picker_name_convert, selectedParticipantsContainer, false);
TextView avaText = (TextView) participantView.findViewById(R.id.atlas_view_participants_picker_name_convert_ava);
avaText.setText(Atlas.getInitials(entry));
TextView nameText = (TextView) participantView.findViewById(R.id.atlas_view_participants_picker_name_convert_name);
nameText.setText(Atlas.getFullName(entry));
participantView.setTag(entry);
selectedParticipantsContainer.addView(participantView, selectedParticipantsContainer.getChildCount() - 1);
if (debug) Log.w(TAG, "refreshParticipants() child added: " + participantView + ", for: " + entry);
// apply styles
avaText.setTextColor(chipTextColor);
avaText.setTypeface(chipTextTypeface, chipTextStyle);
nameText.setTextColor(chipTextColor);
nameText.setTypeface(chipTextTypeface, chipTextStyle);
View container = participantView.findViewById(R.id.atlas_view_participants_picker_name_convert);
GradientDrawable drawable = (GradientDrawable) container.getBackground();
drawable.setColor(chipBackgroundColor);
}
if (selectedParticipantIds.size() == 0) {
LayoutParams params = new LayoutParams(textFilter.getLayoutParams());
params.width = LayoutParams.MATCH_PARENT;
}
selectedParticipantsContainer.requestLayout();
}
private void filterParticipants(final String filter) {
filteredParticipants.clear();
participantProvider.getParticipants(filter, filteredParticipants);
participantsForAdapter.clear();
for (Map.Entry<String, Participant> entry : filteredParticipants.entrySet()) {
if (selectedParticipantIds.contains(entry.getKey())) continue;
if (skipUserIds.contains(entry.getKey())) continue;
participantsForAdapter.add(new ParticipantEntry(entry.getValue(), entry.getKey()));
}
Collections.sort(participantsForAdapter, new ParticipantEntryFilteringComparator(filter));
participantsAdapter.notifyDataSetChanged();
}
private void parseStyle(Context context, AttributeSet attrs, int defStyle) {
TypedArray ta = context.getTheme().obtainStyledAttributes(attrs, R.styleable.AtlasParticipantPicker, R.attr.AtlasParticipantPicker, defStyle);
this.inputTextColor = ta.getColor(R.styleable.AtlasParticipantPicker_inputTextColor, context.getResources().getColor(R.color.atlas_text_black));
this.inputTextStyle = ta.getInt(R.styleable.AtlasParticipantPicker_inputTextStyle, Typeface.NORMAL);
String inputTextTypefaceName = ta.getString(R.styleable.AtlasParticipantPicker_inputTextTypeface);
this.inputTextTypeface = inputTextTypefaceName != null ? Typeface.create(inputTextTypefaceName, inputTextStyle) : null;
this.listTextColor = ta.getColor(R.styleable.AtlasParticipantPicker_listTextColor, context.getResources().getColor(R.color.atlas_text_black));
this.listTextStyle = ta.getInt(R.styleable.AtlasParticipantPicker_listTextStyle, Typeface.NORMAL);
String listTextTypefaceName = ta.getString(R.styleable.AtlasParticipantPicker_listTextTypeface);
this.listTextTypeface = listTextTypefaceName != null ? Typeface.create(listTextTypefaceName, inputTextStyle) : null;
this.chipBackgroundColor = ta.getColor(R.styleable.AtlasParticipantPicker_chipBackgroundColor, context.getResources().getColor(R.color.atlas_background_gray));
this.chipTextColor = ta.getColor(R.styleable.AtlasParticipantPicker_chipTextColor, context.getResources().getColor(R.color.atlas_text_black));
this.chipTextStyle = ta.getInt(R.styleable.AtlasParticipantPicker_chipTextStyle, Typeface.NORMAL);
String chipTextTypefaceName = ta.getString(R.styleable.AtlasParticipantPicker_chipTextTypeface);
this.chipTextTypeface = chipTextTypefaceName != null ? Typeface.create(chipTextTypefaceName, inputTextStyle) : null;
ta.recycle();
}
private void applyStyle() {
refreshParticipants(selectedParticipantIds);
participantsAdapter.notifyDataSetChanged();
textFilter.setTextColor(inputTextColor);
textFilter.setTypeface(inputTextTypeface, inputTextStyle);
}
public String[] getSelectedUserIds() {
String[] userIds = new String[selectedParticipantIds.size()];
int i = 0;
for (String id : selectedParticipantIds) {
userIds[i++] = id;
}
return userIds;
}
public void setVisibility(int visibility) {
super.setVisibility(visibility);
if (visibility == View.VISIBLE) {
textFilter.requestFocus();
}
}
private static final class FilteringComparator implements Comparator<Participant> {
private final String filter;
/**
* @param filter - the less indexOf(filter) the less order of participant
*/
public FilteringComparator(String filter) {
this.filter = filter;
}
@Override
public int compare(Participant lhs, Participant rhs) {
int result = subCompareCaseInsensitive(lhs.getFirstName(), rhs.getFirstName());
if (result != 0) return result;
return subCompareCaseInsensitive(lhs.getLastName(), rhs.getLastName());
}
private int subCompareCaseInsensitive(String lhs, String rhs) {
int left = lhs != null ? lhs.toLowerCase().indexOf(filter) : -1;
int right = rhs != null ? rhs.toLowerCase().indexOf(filter) : -1;
if (left == -1 && right == -1) return 0;
if (left != -1 && right == -1) return -1;
if (left == -1 && right != -1) return 1;
if (left - right != 0) return left - right;
return String.CASE_INSENSITIVE_ORDER.compare(lhs, rhs);
}
}
private static final class ParticipantEntryFilteringComparator implements Comparator<ParticipantEntry> {
FilteringComparator comparator;
public ParticipantEntryFilteringComparator(String filter) {
this.comparator = new FilteringComparator(filter);
}
@Override
public int compare(ParticipantEntry lhs, ParticipantEntry rhs) {
return comparator.compare(lhs.participant, rhs.participant);
}
}
private static class ParticipantEntry {
final Participant participant;
final String id;
public ParticipantEntry(Participant participant, String id) {
if (participant == null) throw new IllegalArgumentException("Participant cannot be null");
if (id == null) throw new IllegalArgumentException("ID cannot be null");
this.participant = participant;
this.id = id;
}
}
}