-
-
Notifications
You must be signed in to change notification settings - Fork 277
Expand file tree
/
Copy pathRecentsFragment.kt
More file actions
207 lines (179 loc) · 8.4 KB
/
RecentsFragment.kt
File metadata and controls
207 lines (179 loc) · 8.4 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
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
package com.simplemobiletools.dialer.fragments
import android.content.Context
import android.util.AttributeSet
import android.util.Log
import com.simplemobiletools.commons.dialogs.CallConfirmationDialog
import com.simplemobiletools.commons.extensions.*
import com.simplemobiletools.commons.helpers.ContactsHelper
import com.simplemobiletools.commons.helpers.MyContactsContentProvider
import com.simplemobiletools.commons.helpers.PERMISSION_READ_CALL_LOG
import com.simplemobiletools.commons.helpers.SMT_PRIVATE
import com.simplemobiletools.commons.models.contacts.Contact
import com.simplemobiletools.commons.views.MyRecyclerView
import com.simplemobiletools.dialer.R
import com.simplemobiletools.dialer.activities.SimpleActivity
import com.simplemobiletools.dialer.adapters.RecentCallsAdapter
import com.simplemobiletools.dialer.databinding.FragmentRecentsBinding
import com.simplemobiletools.dialer.extensions.config
import com.simplemobiletools.dialer.helpers.MIN_RECENTS_THRESHOLD
import com.simplemobiletools.dialer.helpers.RecentsHelper
import com.simplemobiletools.dialer.interfaces.RefreshItemsListener
import com.simplemobiletools.dialer.models.RecentCall
class RecentsFragment(context: Context, attributeSet: AttributeSet) : MyViewPagerFragment<MyViewPagerFragment.RecentsInnerBinding>(context, attributeSet),
RefreshItemsListener {
private lateinit var binding: FragmentRecentsBinding
private var allRecentCalls = listOf<RecentCall>()
private var recentsAdapter: RecentCallsAdapter? = null
override fun onFinishInflate() {
super.onFinishInflate()
binding = FragmentRecentsBinding.bind(this)
innerBinding = RecentsInnerBinding(binding)
}
override fun setupFragment() {
val placeholderResId = if (context.hasPermission(PERMISSION_READ_CALL_LOG)) {
R.string.no_previous_calls
} else {
R.string.could_not_access_the_call_history
}
binding.recentsPlaceholder.text = context.getString(placeholderResId)
binding.recentsPlaceholder2.apply {
underlineText()
setOnClickListener {
requestCallLogPermission()
}
}
}
override fun setupColors(textColor: Int, primaryColor: Int, properPrimaryColor: Int) {
binding.recentsPlaceholder.setTextColor(textColor)
binding.recentsPlaceholder2.setTextColor(properPrimaryColor)
recentsAdapter?.apply {
initDrawables()
updateTextColor(textColor)
}
}
override fun refreshItems(callback: (() -> Unit)?) {
val privateCursor = context?.getMyContactsCursor(false, true)
val groupSubsequentCalls = context?.config?.groupSubsequentCalls ?: false
val querySize = allRecentCalls.size.coerceAtLeast(MIN_RECENTS_THRESHOLD)
RecentsHelper(context).getRecentCalls(groupSubsequentCalls, querySize) { recents ->
ContactsHelper(context).getContacts(showOnlyContactsWithNumbers = true) { contacts ->
val privateContacts = MyContactsContentProvider.getContacts(context, privateCursor)
allRecentCalls = recents
.setNamesIfEmpty(contacts, privateContacts)
.hidePrivateContacts(privateContacts, SMT_PRIVATE in context.baseConfig.ignoredContactSources)
activity?.runOnUiThread {
gotRecents(allRecentCalls)
}
}
}
}
private fun gotRecents(recents: List<RecentCall>) {
if (recents.isEmpty()) {
binding.apply {
recentsPlaceholder.beVisible()
recentsPlaceholder2.beGoneIf(context.hasPermission(PERMISSION_READ_CALL_LOG))
recentsList.beGone()
}
} else {
binding.apply {
recentsPlaceholder.beGone()
recentsPlaceholder2.beGone()
recentsList.beVisible()
}
if (binding.recentsList.adapter == null) {
recentsAdapter = RecentCallsAdapter(activity as SimpleActivity, recents.toMutableList(), binding.recentsList, this, true) {
val recentCall = it as RecentCall
if (context.config.showCallConfirmation) {
CallConfirmationDialog(activity as SimpleActivity, recentCall.name) {
activity?.launchCallIntent(recentCall.phoneNumber)
}
} else {
activity?.launchCallIntent(recentCall.phoneNumber)
}
}
binding.recentsList.adapter = recentsAdapter
if (context.areSystemAnimationsEnabled) {
binding.recentsList.scheduleLayoutAnimation()
}
binding.recentsList.endlessScrollListener = object : MyRecyclerView.EndlessScrollListener {
override fun updateTop() {}
override fun updateBottom() {
getMoreRecentCalls()
}
}
} else {
recentsAdapter?.updateItems(recents)
}
}
}
private fun getMoreRecentCalls() {
val privateCursor = context?.getMyContactsCursor(false, true)
val groupSubsequentCalls = context?.config?.groupSubsequentCalls ?: false
val querySize = allRecentCalls.size.plus(MIN_RECENTS_THRESHOLD)
RecentsHelper(context).getRecentCalls(groupSubsequentCalls, querySize) { recents ->
ContactsHelper(context).getContacts(showOnlyContactsWithNumbers = true) { contacts ->
val privateContacts = MyContactsContentProvider.getContacts(context, privateCursor)
allRecentCalls = recents
.setNamesIfEmpty(contacts, privateContacts)
.hidePrivateContacts(privateContacts, SMT_PRIVATE in context.baseConfig.ignoredContactSources)
activity?.runOnUiThread {
gotRecents(allRecentCalls)
}
}
}
}
private fun requestCallLogPermission() {
activity?.handlePermission(PERMISSION_READ_CALL_LOG) {
if (it) {
binding.recentsPlaceholder.text = context.getString(R.string.no_previous_calls)
binding.recentsPlaceholder2.beGone()
val groupSubsequentCalls = context?.config?.groupSubsequentCalls ?: false
RecentsHelper(context).getRecentCalls(groupSubsequentCalls) { recents ->
activity?.runOnUiThread {
gotRecents(recents)
}
}
}
}
}
override fun onSearchClosed() {
binding.recentsPlaceholder.beVisibleIf(allRecentCalls.isEmpty())
recentsAdapter?.updateItems(allRecentCalls)
}
override fun onSearchQueryChanged(text: String) {
val recentCalls = allRecentCalls.filter {
it.name.contains(text, true) || it.doesContainPhoneNumber(text)
}.sortedByDescending {
it.name.startsWith(text, true)
}.toMutableList() as ArrayList<RecentCall>
binding.recentsPlaceholder.beVisibleIf(recentCalls.isEmpty())
recentsAdapter?.updateItems(recentCalls, text)
}
}
// hide private contacts from recent calls
fun List<RecentCall>.hidePrivateContacts(privateContacts: List<Contact>, shouldHide: Boolean): List<RecentCall> {
return if (shouldHide) {
filterNot { recent ->
val privateNumbers = privateContacts.flatMap { it.phoneNumbers }.map { it.value }
recent.phoneNumber in privateNumbers
}
} else {
this
}
}
private fun List<RecentCall>.setNamesIfEmpty(contacts: List<Contact>, privateContacts: List<Contact>): ArrayList<RecentCall> {
val contactsWithNumbers = contacts.filter { it.phoneNumbers.isNotEmpty() }
return map { recent ->
if (recent.phoneNumber == recent.name) {
val privateContact = privateContacts.firstOrNull { it.doesContainPhoneNumber(recent.phoneNumber) }
val contact = contactsWithNumbers.firstOrNull { it.phoneNumbers.first().normalizedNumber == recent.phoneNumber }
when {
privateContact != null -> recent.copy(name = privateContact.getNameToDisplay())
contact != null -> recent.copy(name = contact.getNameToDisplay())
else -> recent
}
} else {
recent
}
} as ArrayList
}