-
Notifications
You must be signed in to change notification settings - Fork 59
/
Copy pathRecentCallsAdapter.kt
461 lines (391 loc) · 17.8 KB
/
RecentCallsAdapter.kt
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
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
package org.fossify.phone.adapters
import android.content.Intent
import android.graphics.drawable.Drawable
import android.provider.CallLog.Calls
import android.text.SpannableString
import android.text.TextUtils
import android.util.TypedValue
import android.view.*
import android.widget.PopupMenu
import com.bumptech.glide.Glide
import org.fossify.commons.adapters.MyRecyclerViewAdapter
import org.fossify.commons.dialogs.ConfirmationDialog
import org.fossify.commons.dialogs.FeatureLockedDialog
import org.fossify.commons.extensions.*
import org.fossify.commons.helpers.*
import org.fossify.commons.models.contacts.Contact
import org.fossify.commons.views.MyRecyclerView
import org.fossify.phone.R
import org.fossify.phone.activities.MainActivity
import org.fossify.phone.activities.SimpleActivity
import org.fossify.phone.databinding.ItemRecentCallBinding
import org.fossify.phone.dialogs.ShowGroupedCallsDialog
import org.fossify.phone.extensions.*
import org.fossify.phone.helpers.RecentsHelper
import org.fossify.phone.interfaces.RefreshItemsListener
import org.fossify.phone.models.RecentCall
class RecentCallsAdapter(
activity: SimpleActivity,
private var recentCalls: MutableList<RecentCall>,
recyclerView: MyRecyclerView,
private val refreshItemsListener: RefreshItemsListener?,
private val showOverflowMenu: Boolean,
itemClick: (Any) -> Unit
) : MyRecyclerViewAdapter(activity, recyclerView, itemClick) {
private lateinit var outgoingCallIcon: Drawable
private lateinit var incomingCallIcon: Drawable
private lateinit var incomingMissedCallIcon: Drawable
var fontSize: Float = activity.getTextSize()
private val areMultipleSIMsAvailable = activity.areMultipleSIMsAvailable()
private val redColor = resources.getColor(R.color.md_red_700)
private var textToHighlight = ""
private var durationPadding = resources.getDimension(R.dimen.normal_margin).toInt()
init {
initDrawables()
setupDragListener(true)
}
override fun getActionMenuId() = R.menu.cab_recent_calls
override fun prepareActionMode(menu: Menu) {
val hasMultipleSIMs = activity.areMultipleSIMsAvailable()
val selectedItems = getSelectedItems()
val isOneItemSelected = selectedItems.size == 1
val selectedNumber = "tel:${getSelectedPhoneNumber()}"
menu.apply {
findItem(R.id.cab_call_sim_1).isVisible = hasMultipleSIMs && isOneItemSelected
findItem(R.id.cab_call_sim_2).isVisible = hasMultipleSIMs && isOneItemSelected
findItem(R.id.cab_remove_default_sim).isVisible = isOneItemSelected && (activity.config.getCustomSIM(selectedNumber) ?: "") != ""
findItem(R.id.cab_block_number).title = activity.addLockedLabelIfNeeded(R.string.block_number)
findItem(R.id.cab_block_number).isVisible = isNougatPlus()
findItem(R.id.cab_add_number).isVisible = isOneItemSelected
findItem(R.id.cab_copy_number).isVisible = isOneItemSelected
findItem(R.id.cab_show_call_details).isVisible = isOneItemSelected
findItem(R.id.cab_view_details).isVisible = isOneItemSelected && findContactByCall(selectedItems.first()) != null
}
}
override fun actionItemPressed(id: Int) {
if (selectedKeys.isEmpty()) {
return
}
when (id) {
R.id.cab_call_sim_1 -> callContact(true)
R.id.cab_call_sim_2 -> callContact(false)
R.id.cab_remove_default_sim -> removeDefaultSIM()
R.id.cab_block_number -> tryBlocking()
R.id.cab_add_number -> addNumberToContact()
R.id.cab_send_sms -> sendSMS()
R.id.cab_show_call_details -> showCallDetails()
R.id.cab_copy_number -> copyNumber()
R.id.cab_remove -> askConfirmRemove()
R.id.cab_select_all -> selectAll()
R.id.cab_view_details -> launchContactDetailsIntent(findContactByCall(getSelectedItems().first()))
}
}
override fun getSelectableItemCount() = recentCalls.size
override fun getIsItemSelectable(position: Int) = true
override fun getItemSelectionKey(position: Int) = recentCalls.getOrNull(position)?.id
override fun getItemKeyPosition(key: Int) = recentCalls.indexOfFirst { it.id == key }
override fun onActionModeCreated() {}
override fun onActionModeDestroyed() {}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
return createViewHolder(ItemRecentCallBinding.inflate(layoutInflater, parent, false).root)
}
override fun onBindViewHolder(holder: ViewHolder, position: Int) {
val recentCall = recentCalls[position]
holder.bindView(
any = recentCall,
allowSingleClick = refreshItemsListener != null && !recentCall.isUnknownNumber,
allowLongClick = refreshItemsListener != null && !recentCall.isUnknownNumber
) { itemView, _ ->
val binding = ItemRecentCallBinding.bind(itemView)
setupView(binding, recentCall)
}
bindViewHolder(holder)
}
override fun getItemCount() = recentCalls.size
override fun onViewRecycled(holder: ViewHolder) {
super.onViewRecycled(holder)
if (!activity.isDestroyed && !activity.isFinishing) {
ItemRecentCallBinding.bind(holder.itemView).apply {
Glide.with(activity).clear(itemRecentsImage)
}
}
}
fun initDrawables() {
outgoingCallIcon = resources.getColoredDrawableWithColor(R.drawable.ic_outgoing_call_vector, activity.getProperTextColor())
incomingCallIcon = resources.getColoredDrawableWithColor(R.drawable.ic_incoming_call_vector, activity.getProperTextColor())
incomingMissedCallIcon = resources.getColoredDrawableWithColor(R.drawable.ic_incoming_call_vector, redColor)
}
private fun callContact(useSimOne: Boolean) {
val phoneNumber = getSelectedPhoneNumber() ?: return
activity.callContactWithSim(phoneNumber, useSimOne)
}
private fun callContact() {
val phoneNumber = getSelectedPhoneNumber() ?: return
(activity as SimpleActivity).startCallIntent(phoneNumber)
}
private fun removeDefaultSIM() {
val phoneNumber = getSelectedPhoneNumber() ?: return
activity.config.removeCustomSIM("tel:$phoneNumber")
finishActMode()
}
private fun tryBlocking() {
if (activity.isOrWasThankYouInstalled()) {
askConfirmBlock()
} else {
FeatureLockedDialog(activity) { }
}
}
private fun askConfirmBlock() {
val numbers = TextUtils.join(", ", getSelectedItems().distinctBy { it.phoneNumber }.map { it.phoneNumber })
val baseString = R.string.block_confirmation
val question = String.format(resources.getString(baseString), numbers)
ConfirmationDialog(activity, question) {
blockNumbers()
}
}
private fun blockNumbers() {
if (selectedKeys.isEmpty()) {
return
}
val callsToBlock = getSelectedItems()
val positions = getSelectedItemPositions()
recentCalls.removeAll(callsToBlock)
ensureBackgroundThread {
callsToBlock.map { it.phoneNumber }.forEach { number ->
activity.addBlockedNumber(number)
}
activity.runOnUiThread {
removeSelectedItems(positions)
finishActMode()
}
}
}
private fun addNumberToContact() {
val phoneNumber = getSelectedPhoneNumber() ?: return
Intent().apply {
action = Intent.ACTION_INSERT_OR_EDIT
type = "vnd.android.cursor.item/contact"
putExtra(KEY_PHONE, phoneNumber)
activity.launchActivityIntent(this)
}
}
private fun sendSMS() {
val numbers = getSelectedItems().map { it.phoneNumber }
val recipient = TextUtils.join(";", numbers)
activity.launchSendSMSIntent(recipient)
}
private fun showCallDetails() {
val recentCall = getSelectedItems().firstOrNull() ?: return
val callIds = recentCall.neighbourIDs.map { it }.toMutableList() as ArrayList<Int>
callIds.add(recentCall.id)
ShowGroupedCallsDialog(activity, callIds)
}
private fun copyNumber() {
val recentCall = getSelectedItems().firstOrNull() ?: return
activity.copyToClipboard(recentCall.phoneNumber)
finishActMode()
}
private fun askConfirmRemove() {
ConfirmationDialog(activity, activity.getString(R.string.remove_confirmation)) {
activity.handlePermission(PERMISSION_WRITE_CALL_LOG) {
removeRecents()
}
}
}
private fun removeRecents() {
if (selectedKeys.isEmpty()) {
return
}
val callsToRemove = getSelectedItems()
val positions = getSelectedItemPositions()
val idsToRemove = ArrayList<Int>()
callsToRemove.forEach {
idsToRemove.add(it.id)
it.neighbourIDs.mapTo(idsToRemove, { it })
}
RecentsHelper(activity).removeRecentCalls(idsToRemove) {
recentCalls.removeAll(callsToRemove)
activity.runOnUiThread {
refreshItemsListener?.refreshItems()
if (recentCalls.isEmpty()) {
finishActMode()
} else {
removeSelectedItems(positions)
}
}
}
}
private fun findContactByCall(recentCall: RecentCall): Contact? {
return (activity as MainActivity).cachedContacts.find { it.name == recentCall.name && it.doesHavePhoneNumber(recentCall.phoneNumber) }
}
private fun launchContactDetailsIntent(contact: Contact?) {
if (contact != null) {
activity.startContactDetailsIntent(contact)
}
}
fun updateItems(newItems: List<RecentCall>, highlightText: String = "") {
if (newItems.hashCode() != recentCalls.hashCode()) {
recentCalls = newItems.toMutableList()
textToHighlight = highlightText
recyclerView.resetItemCount()
notifyDataSetChanged()
finishActMode()
} else if (textToHighlight != highlightText) {
textToHighlight = highlightText
notifyDataSetChanged()
}
}
private fun getSelectedItems() = recentCalls.filter { selectedKeys.contains(it.id) } as ArrayList<RecentCall>
private fun getSelectedPhoneNumber() = getSelectedItems().firstOrNull()?.phoneNumber
private fun setupView(binding: ItemRecentCallBinding, call: RecentCall) {
binding.apply {
val currentFontSize = fontSize
itemRecentsHolder.isSelected = selectedKeys.contains(call.id)
val name = findContactByCall(call)?.getNameToDisplay() ?: call.name
var nameToShow = SpannableString(name)
if (call.specificType.isNotEmpty()) {
nameToShow = SpannableString("${name} - ${call.specificType}")
// show specific number at "Show call details" dialog too
if (refreshItemsListener == null) {
nameToShow = SpannableString("${name} - ${call.specificType}, ${call.specificNumber}")
}
}
if (call.neighbourIDs.isNotEmpty()) {
nameToShow = SpannableString("$nameToShow (${call.neighbourIDs.size + 1})")
}
if (textToHighlight.isNotEmpty() && nameToShow.contains(textToHighlight, true)) {
nameToShow = SpannableString(nameToShow.toString().highlightTextPart(textToHighlight, properPrimaryColor))
}
itemRecentsName.apply {
text = nameToShow
setTextColor(textColor)
setTextSize(TypedValue.COMPLEX_UNIT_PX, currentFontSize)
}
itemRecentsDateTime.apply {
text = call.startTS.formatDateOrTime(context, refreshItemsListener != null, false)
setTextColor(if (call.type == Calls.MISSED_TYPE) redColor else textColor)
setTextSize(TypedValue.COMPLEX_UNIT_PX, currentFontSize * 0.8f)
}
itemRecentsDuration.apply {
text = call.duration.getFormattedDuration()
setTextColor(textColor)
beVisibleIf(call.type != Calls.MISSED_TYPE && call.type != Calls.REJECTED_TYPE && call.duration > 0)
setTextSize(TypedValue.COMPLEX_UNIT_PX, currentFontSize * 0.8f)
if (!showOverflowMenu) {
itemRecentsDuration.setPadding(0, 0, durationPadding, 0)
}
}
itemRecentsSimImage.beVisibleIf(areMultipleSIMsAvailable && call.simID != -1)
itemRecentsSimId.beVisibleIf(areMultipleSIMsAvailable && call.simID != -1)
if (areMultipleSIMsAvailable && call.simID != -1) {
itemRecentsSimImage.applyColorFilter(call.simColor.adjustSimColorForBackground(backgroundColor))
itemRecentsSimId.setTextColor(backgroundColor)
itemRecentsSimId.text = call.simID.toString()
}
SimpleContactsHelper(root.context).loadContactImage(call.photoUri, itemRecentsImage, call.name)
val drawable = when (call.type) {
Calls.OUTGOING_TYPE -> outgoingCallIcon
Calls.MISSED_TYPE -> incomingMissedCallIcon
else -> incomingCallIcon
}
itemRecentsType.setImageDrawable(drawable)
overflowMenuIcon.beVisibleIf(showOverflowMenu)
overflowMenuIcon.drawable.apply {
mutate()
setTint(activity.getProperTextColor())
}
overflowMenuIcon.setOnClickListener {
showPopupMenu(overflowMenuAnchor, call)
}
}
}
private fun showPopupMenu(view: View, call: RecentCall) {
finishActMode()
val theme = activity.getPopupMenuTheme()
val contextTheme = ContextThemeWrapper(activity, theme)
val contact = findContactByCall(call)
val selectedNumber = "tel:${call.phoneNumber}"
PopupMenu(contextTheme, view, Gravity.END).apply {
inflate(R.menu.menu_recent_item_options)
menu.apply {
val areMultipleSIMsAvailable = activity.areMultipleSIMsAvailable()
findItem(R.id.cab_call).isVisible = !areMultipleSIMsAvailable && !call.isUnknownNumber
findItem(R.id.cab_call_sim_1).isVisible = areMultipleSIMsAvailable && !call.isUnknownNumber
findItem(R.id.cab_call_sim_2).isVisible = areMultipleSIMsAvailable && !call.isUnknownNumber
findItem(R.id.cab_send_sms).isVisible = !call.isUnknownNumber
findItem(R.id.cab_view_details).isVisible = contact != null && !call.isUnknownNumber
findItem(R.id.cab_add_number).isVisible = !call.isUnknownNumber
findItem(R.id.cab_copy_number).isVisible = !call.isUnknownNumber
findItem(R.id.cab_show_call_details).isVisible = !call.isUnknownNumber
findItem(R.id.cab_block_number).title = activity.addLockedLabelIfNeeded(R.string.block_number)
findItem(R.id.cab_block_number).isVisible = isNougatPlus() && !call.isUnknownNumber
findItem(R.id.cab_remove_default_sim).isVisible = (activity.config.getCustomSIM(selectedNumber) ?: "") != "" && !call.isUnknownNumber
}
setOnMenuItemClickListener { item ->
val callId = call.id
when (item.itemId) {
R.id.cab_call -> {
executeItemMenuOperation(callId) {
callContact()
}
}
R.id.cab_call_sim_1 -> {
executeItemMenuOperation(callId) {
callContact(true)
}
}
R.id.cab_call_sim_2 -> {
executeItemMenuOperation(callId) {
callContact(false)
}
}
R.id.cab_send_sms -> {
executeItemMenuOperation(callId) {
sendSMS()
}
}
R.id.cab_view_details -> {
executeItemMenuOperation(callId) {
launchContactDetailsIntent(contact)
}
}
R.id.cab_add_number -> {
executeItemMenuOperation(callId) {
addNumberToContact()
}
}
R.id.cab_show_call_details -> {
executeItemMenuOperation(callId) {
showCallDetails()
}
}
R.id.cab_block_number -> {
selectedKeys.add(callId)
tryBlocking()
}
R.id.cab_remove -> {
selectedKeys.add(callId)
askConfirmRemove()
}
R.id.cab_copy_number -> {
executeItemMenuOperation(callId) {
copyNumber()
}
}
R.id.cab_remove_default_sim -> {
executeItemMenuOperation(callId) {
removeDefaultSIM()
}
}
}
true
}
show()
}
}
private fun executeItemMenuOperation(callId: Int, callback: () -> Unit) {
selectedKeys.add(callId)
callback()
selectedKeys.remove(callId)
}
}