From 22a2fbb33114e25835300821b71df9883ced2c62 Mon Sep 17 00:00:00 2001 From: merkost Date: Sun, 2 Jul 2023 10:07:26 +1000 Subject: [PATCH] ArrayList to Kotlin List refactoring --- .../dialer/activities/MainActivity.kt | 4 ++-- .../dialer/activities/SettingsActivity.kt | 2 +- .../dialer/adapters/RecentCallsAdapter.kt | 6 +++--- .../dialer/extensions/Context.kt | 4 ++-- .../dialer/fragments/RecentsFragment.kt | 18 ++++++++++-------- .../dialer/helpers/RecentsHelper.kt | 10 +++++----- .../dialer/models/RecentCall.kt | 2 +- 7 files changed, 24 insertions(+), 22 deletions(-) diff --git a/app/src/main/kotlin/com/simplemobiletools/dialer/activities/MainActivity.kt b/app/src/main/kotlin/com/simplemobiletools/dialer/activities/MainActivity.kt index 53991176..43d85fcd 100644 --- a/app/src/main/kotlin/com/simplemobiletools/dialer/activities/MainActivity.kt +++ b/app/src/main/kotlin/com/simplemobiletools/dialer/activities/MainActivity.kt @@ -272,9 +272,9 @@ class MainActivity : SimpleActivity() { private fun getInactiveTabIndexes(activeIndex: Int) = (0 until main_tabs_holder.tabCount).filter { it != activeIndex } - private fun getSelectedTabDrawableIds(): ArrayList { + private fun getSelectedTabDrawableIds(): List { val showTabs = config.showTabs - val icons = ArrayList() + val icons = mutableListOf() if (showTabs and TAB_CONTACTS != 0) { icons.add(R.drawable.ic_person_vector) diff --git a/app/src/main/kotlin/com/simplemobiletools/dialer/activities/SettingsActivity.kt b/app/src/main/kotlin/com/simplemobiletools/dialer/activities/SettingsActivity.kt index 379fa20b..8403d368 100644 --- a/app/src/main/kotlin/com/simplemobiletools/dialer/activities/SettingsActivity.kt +++ b/app/src/main/kotlin/com/simplemobiletools/dialer/activities/SettingsActivity.kt @@ -320,7 +320,7 @@ class SettingsActivity : SimpleActivity() { } } - private fun exportCallHistory(recents: ArrayList, uri: Uri) { + private fun exportCallHistory(recents: List, uri: Uri) { if (recents.isEmpty()) { toast(R.string.no_entries_for_exporting) } else { diff --git a/app/src/main/kotlin/com/simplemobiletools/dialer/adapters/RecentCallsAdapter.kt b/app/src/main/kotlin/com/simplemobiletools/dialer/adapters/RecentCallsAdapter.kt index cbc0c4ce..e060229b 100644 --- a/app/src/main/kotlin/com/simplemobiletools/dialer/adapters/RecentCallsAdapter.kt +++ b/app/src/main/kotlin/com/simplemobiletools/dialer/adapters/RecentCallsAdapter.kt @@ -28,7 +28,7 @@ import kotlinx.android.synthetic.main.item_recent_call.view.* class RecentCallsAdapter( activity: SimpleActivity, - var recentCalls: ArrayList, + private var recentCalls: MutableList, recyclerView: MyRecyclerView, private val refreshItemsListener: RefreshItemsListener?, private val showOverflowMenu: Boolean, @@ -260,9 +260,9 @@ class RecentCallsAdapter( } } - fun updateItems(newItems: ArrayList, highlightText: String = "") { + fun updateItems(newItems: List, highlightText: String = "") { if (newItems.hashCode() != recentCalls.hashCode()) { - recentCalls = newItems.clone() as ArrayList + recentCalls = newItems.toMutableList() textToHighlight = highlightText recyclerView.resetItemCount() notifyDataSetChanged() diff --git a/app/src/main/kotlin/com/simplemobiletools/dialer/extensions/Context.kt b/app/src/main/kotlin/com/simplemobiletools/dialer/extensions/Context.kt index ac7a3e65..b64f599f 100644 --- a/app/src/main/kotlin/com/simplemobiletools/dialer/extensions/Context.kt +++ b/app/src/main/kotlin/com/simplemobiletools/dialer/extensions/Context.kt @@ -16,8 +16,8 @@ val Context.audioManager: AudioManager get() = getSystemService(Context.AUDIO_SE val Context.powerManager: PowerManager get() = getSystemService(Context.POWER_SERVICE) as PowerManager @SuppressLint("MissingPermission") -fun Context.getAvailableSIMCardLabels(): ArrayList { - val SIMAccounts = ArrayList() +fun Context.getAvailableSIMCardLabels(): List { + val SIMAccounts = mutableListOf() try { telecomManager.callCapablePhoneAccounts.forEachIndexed { index, account -> val phoneAccount = telecomManager.getPhoneAccount(account) diff --git a/app/src/main/kotlin/com/simplemobiletools/dialer/fragments/RecentsFragment.kt b/app/src/main/kotlin/com/simplemobiletools/dialer/fragments/RecentsFragment.kt index 82aeaa8a..2ef37e88 100644 --- a/app/src/main/kotlin/com/simplemobiletools/dialer/fragments/RecentsFragment.kt +++ b/app/src/main/kotlin/com/simplemobiletools/dialer/fragments/RecentsFragment.kt @@ -18,10 +18,12 @@ 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 -import kotlinx.android.synthetic.main.fragment_recents.view.* +import kotlinx.android.synthetic.main.fragment_recents.view.recents_list +import kotlinx.android.synthetic.main.fragment_recents.view.recents_placeholder +import kotlinx.android.synthetic.main.fragment_recents.view.recents_placeholder_2 class RecentsFragment(context: Context, attributeSet: AttributeSet) : MyViewPagerFragment(context, attributeSet), RefreshItemsListener { - private var allRecentCalls = ArrayList() + private var allRecentCalls = listOf() private var recentsAdapter: RecentCallsAdapter? = null override fun setupFragment() { @@ -69,7 +71,7 @@ class RecentsFragment(context: Context, attributeSet: AttributeSet) : MyViewPage } } - private fun gotRecents(recents: ArrayList) { + private fun gotRecents(recents: List) { if (recents.isEmpty()) { recents_placeholder.beVisible() recents_placeholder_2.beGoneIf(context.hasPermission(PERMISSION_READ_CALL_LOG)) @@ -81,7 +83,7 @@ class RecentsFragment(context: Context, attributeSet: AttributeSet) : MyViewPage val currAdapter = recents_list.adapter if (currAdapter == null) { - recentsAdapter = RecentCallsAdapter(activity as SimpleActivity, recents, recents_list, this, true) { + recentsAdapter = RecentCallsAdapter(activity as SimpleActivity, recents.toMutableList(), recents_list, this, true) { val recentCall = it as RecentCall if (context.config.showCallConfirmation) { CallConfirmationDialog(activity as SimpleActivity, recentCall.name) { @@ -165,18 +167,18 @@ class RecentsFragment(context: Context, attributeSet: AttributeSet) : MyViewPage } // hide private contacts from recent calls -private fun List.hidePrivateContacts(privateContacts: ArrayList, shouldHide: Boolean): ArrayList { +private fun List.hidePrivateContacts(privateContacts: List, shouldHide: Boolean): List { return if (shouldHide) { filterNot { recent -> val privateNumbers = privateContacts.flatMap { it.phoneNumbers }.map { it.value } recent.phoneNumber in privateNumbers - } as ArrayList + } } else { - this as ArrayList + this } } -private fun ArrayList.setNamesIfEmpty(contacts: ArrayList, privateContacts: ArrayList): ArrayList { +private fun List.setNamesIfEmpty(contacts: List, privateContacts: List): ArrayList { val contactsWithNumbers = contacts.filter { it.phoneNumbers.isNotEmpty() } return map { recent -> if (recent.phoneNumber == recent.name) { diff --git a/app/src/main/kotlin/com/simplemobiletools/dialer/helpers/RecentsHelper.kt b/app/src/main/kotlin/com/simplemobiletools/dialer/helpers/RecentsHelper.kt index ad09c272..65eccb03 100644 --- a/app/src/main/kotlin/com/simplemobiletools/dialer/helpers/RecentsHelper.kt +++ b/app/src/main/kotlin/com/simplemobiletools/dialer/helpers/RecentsHelper.kt @@ -17,7 +17,7 @@ class RecentsHelper(private val context: Context) { private val QUERY_LIMIT = 200 private val contentUri = Calls.CONTENT_URI - fun getRecentCalls(groupSubsequentCalls: Boolean, maxSize: Int = QUERY_LIMIT, callback: (ArrayList) -> Unit) { + fun getRecentCalls(groupSubsequentCalls: Boolean, maxSize: Int = QUERY_LIMIT, callback: (List) -> Unit) { val privateCursor = context.getMyContactsCursor(false, true) ensureBackgroundThread { if (!context.hasPermission(PERMISSION_READ_CALL_LOG)) { @@ -37,7 +37,7 @@ class RecentsHelper(private val context: Context) { } @SuppressLint("NewApi") - private fun getRecents(contacts: ArrayList, groupSubsequentCalls: Boolean, maxSize: Int, callback: (ArrayList) -> Unit) { + private fun getRecents(contacts: List, groupSubsequentCalls: Boolean, maxSize: Int, callback: (List) -> Unit) { val recentCalls = mutableListOf() var previousRecentCallFrom = "" @@ -151,7 +151,7 @@ class RecentsHelper(private val context: Context) { val type = cursor.getIntValue(Calls.TYPE) val accountId = cursor.getStringValue(Calls.PHONE_ACCOUNT_ID) val simID = accountIdToSimIDMap[accountId] ?: -1 - val neighbourIDs = ArrayList() + val neighbourIDs = mutableListOf() var specificNumber = "" var specificType = "" @@ -196,10 +196,10 @@ class RecentsHelper(private val context: Context) { val recentResult = recentCalls .filter { !context.isNumberBlocked(it.phoneNumber, blockedNumbers) } - callback(ArrayList(recentResult)) + callback(recentResult) } - fun removeRecentCalls(ids: ArrayList, callback: () -> Unit) { + fun removeRecentCalls(ids: List, callback: () -> Unit) { ensureBackgroundThread { ids.chunked(30).forEach { chunk -> val selection = "${Calls._ID} IN (${getQuestionMarks(chunk.size)})" diff --git a/app/src/main/kotlin/com/simplemobiletools/dialer/models/RecentCall.kt b/app/src/main/kotlin/com/simplemobiletools/dialer/models/RecentCall.kt index c82f48e1..e745998c 100644 --- a/app/src/main/kotlin/com/simplemobiletools/dialer/models/RecentCall.kt +++ b/app/src/main/kotlin/com/simplemobiletools/dialer/models/RecentCall.kt @@ -16,7 +16,7 @@ data class RecentCall( val startTS: Int, val duration: Int, val type: Int, - val neighbourIDs: ArrayList, + val neighbourIDs: MutableList, val simID: Int, val specificNumber: String, val specificType: String,