-
Notifications
You must be signed in to change notification settings - Fork 31
Expand file tree
/
Copy pathContactAdapter.java
More file actions
166 lines (135 loc) · 6 KB
/
ContactAdapter.java
File metadata and controls
166 lines (135 loc) · 6 KB
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
/*
* Copyright (C) 2015-2017 Emanuel Moecklin
*
* 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.onegravity.contactpicker.contact;
import android.content.Context;
import androidx.recyclerview.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.SectionIndexer;
import com.onegravity.contactpicker.R;
import com.onegravity.contactpicker.picture.ContactPictureManager;
import com.onegravity.contactpicker.picture.ContactPictureType;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class ContactAdapter extends RecyclerView.Adapter<ContactViewHolder> implements SectionIndexer {
private List<? extends Contact> mContacts;
final private ContactSortOrder mSortOrder;
final private ContactPictureType mContactPictureType;
final private ContactDescription mContactDescription;
final private int mContactDescriptionType;
final private ContactPictureManager mContactPictureLoader;
private LayoutInflater mInflater;
public ContactAdapter(Context context, List<Contact> contacts,
ContactSortOrder sortOrder,
ContactPictureType contactPictureType,
ContactDescription contactDescription,
int contactDescriptionType) {
mContacts = contacts;
mSortOrder = sortOrder;
mContactPictureType = contactPictureType;
mContactDescription = contactDescription;
mContactDescriptionType = contactDescriptionType;
mContactPictureLoader = new ContactPictureManager(context, mContactPictureType == ContactPictureType.ROUND);
}
public void setData(List<? extends Contact> contacts) {
mContacts = contacts;
notifyDataSetChanged();
if (! mContacts.isEmpty()) {
calculateSections();
}
}
@Override
public ContactViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
if (mInflater == null) {
mInflater = (LayoutInflater) parent.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
View view = mInflater.inflate(R.layout.cp_contact_list_item, parent, false);
return new ContactViewHolder(view, mContactPictureLoader, mContactPictureType,
mContactDescription, mContactDescriptionType);
}
@Override
public void onBindViewHolder(ContactViewHolder holder, int position) {
if (mContacts != null) {
holder.bind( mContacts.get(position) );
}
}
@Override
public void onViewRecycled(ContactViewHolder holder) {
holder.onRecycled();
}
@Override
public int getItemCount() {
return mContacts == null ? 0 : mContacts.size();
}
@Override
public long getItemId(int position) {
return mContacts == null ? super.getItemId(position) : mContacts.get(position).getId();
}
private Map<Character, ContactSection> mSections = new HashMap<>();
private ContactSection[] mSectionArray;
synchronized private void calculateSections() {
mSections.clear();
List<ContactSection> sectionArray = new ArrayList<>();
if (mContacts != null) {
int contactPos = 0;
int sectionPos = 0;
char prevLetter = 0;
for (Contact contact : mContacts) {
char letter = contact.getContactLetter(mSortOrder);
if (letter != prevLetter) {
ContactSection newSection = new ContactSection(letter, sectionPos++, contactPos);
mSections.put(letter, newSection);
sectionArray.add(newSection);
prevLetter = letter;
}
contactPos++;
}
}
mSectionArray = sectionArray.toArray(new ContactSection[sectionArray.size()]);
}
@Override
public synchronized Object[] getSections() {
return mSectionArray;
}
@Override
public synchronized int getPositionForSection(int sectionPos) {
if (mSections == null || mSections.isEmpty() ||
mContacts == null || mContacts.isEmpty()) return 0;
int maxIndexSections = assertBoundaries(mSectionArray.length - 1, 0, mSectionArray.length);
sectionPos = assertBoundaries(sectionPos, 0, maxIndexSections);
int contactPos = mSectionArray[sectionPos].getContactPos();
int maxIndexContacts = assertBoundaries(mContacts.size() - 1, 0, mContacts.size());
return assertBoundaries(contactPos, 0, maxIndexContacts);
}
@Override
public synchronized int getSectionForPosition(int contactPosition) {
if (mSections == null || mSections.isEmpty() ||
mContacts == null || mContacts.isEmpty()) return 0;
int maxIndexContacts = assertBoundaries(mContacts.size() - 1, 0, mContacts.size());
contactPosition = assertBoundaries(contactPosition, 0, maxIndexContacts);
char contactLetter = mContacts.get(contactPosition).getContactLetter(mSortOrder);
ContactSection section = mSections.get(contactLetter);
int sectionPos = section != null ? section.getSectionPos() : 0;
int maxIndexSections = assertBoundaries(mSectionArray.length - 1, 0, mSectionArray.length);
return assertBoundaries(sectionPos, 0, maxIndexSections);
}
private int assertBoundaries(int index, int lower, int upper) {
return Math.max(lower, Math.min(index, upper));
}
}