Skip to content

Commit

Permalink
Cache columnIndex
Browse files Browse the repository at this point in the history
  • Loading branch information
ismartcoding committed Sep 26, 2023
1 parent 5689633 commit 8e0371e
Show file tree
Hide file tree
Showing 22 changed files with 175 additions and 139 deletions.
5 changes: 5 additions & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@

<uses-permission android:name="android.permission.SCHEDULE_EXACT_ALARM" />
<!-- <uses-permission android:name="android.permission.SYSTEM_OVERLAY_WINDOW" />-->

<uses-permission android:name="android.permission.READ_MEDIA_IMAGES" />
<uses-permission android:name="android.permission.READ_MEDIA_VIDEO" />
<uses-permission android:name="android.permission.READ_MEDIA_AUDIO" />

<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -145,8 +145,9 @@ abstract class BaseContentHelper {
val cursor = getSearchCursor(context, query)
val ids = mutableSetOf<String>()
if (cursor?.moveToFirst() == true) {
val cache = mutableMapOf<String, Int>()
do {
ids.add(cursor.getStringValue(idKey))
ids.add(cursor.getStringValue(idKey, cache))
} while (cursor.moveToNext())
}

Expand Down Expand Up @@ -212,9 +213,10 @@ abstract class BaseContentHelper {
)
if (cursor != null) {
cursor.moveToFirst()
val cache = mutableMapOf<String, Int>()
while (!cursor.isAfterLast) {
val id = cursor.getStringValue(MediaStore.MediaColumns._ID)
val path = cursor.getStringValue(MediaStore.MediaColumns.DATA)
val id = cursor.getStringValue(MediaStore.MediaColumns._ID, cache)
val path = cursor.getStringValue(MediaStore.MediaColumns.DATA, cache)
paths.add(path)
try { // File.delete can throw a security exception
val f = File(path)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@ enum class Permission {
CAMERA,
SYSTEM_ALERT_WINDOW,
RECORD_AUDIO,
READ_MEDIA_IMAGES,
READ_MEDIA_VIDEOS,
READ_MEDIA_AUDIO,
NONE;

fun getText(): String {
Expand Down Expand Up @@ -236,6 +239,9 @@ object Permissions {
Permission.SEND_SMS,
Permission.POST_NOTIFICATIONS,
Permission.RECORD_AUDIO,
Permission.READ_MEDIA_IMAGES,
Permission.READ_MEDIA_VIDEOS,
Permission.READ_MEDIA_AUDIO,
).forEach { permission ->
map[permission] = activity.registerForActivityResult(ActivityResultContracts.RequestPermission()) {
canContinue = true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,14 +64,15 @@ object AudioHelper : BaseContentHelper() {
val cursor = getSearchCursor(context, query, limit, offset, sortBy.toSortBy())
val result = mutableListOf<DAudio>()
if (cursor?.moveToFirst() == true) {
val cache = mutableMapOf<String, Int>()
do {
val id = cursor.getStringValue(MediaStore.Audio.Media._ID)
val title = cursor.getStringValue(MediaStore.Audio.Media.TITLE)
val artist = cursor.getStringValue(MediaStore.Audio.Media.ARTIST).replace(MediaStore.UNKNOWN_STRING, "")
val size = cursor.getLongValue(MediaStore.Audio.Media.SIZE)
val duration = cursor.getLongValue(MediaStore.Audio.Media.DURATION) / 1000
val path = cursor.getStringValue(MediaStore.Audio.Media.DATA)
val bucketId = cursor.getStringValue(MediaStore.Audio.Media.BUCKET_ID)
val id = cursor.getStringValue(MediaStore.Audio.Media._ID, cache)
val title = cursor.getStringValue(MediaStore.Audio.Media.TITLE, cache)
val artist = cursor.getStringValue(MediaStore.Audio.Media.ARTIST, cache).replace(MediaStore.UNKNOWN_STRING, "")
val size = cursor.getLongValue(MediaStore.Audio.Media.SIZE, cache)
val duration = cursor.getLongValue(MediaStore.Audio.Media.DURATION, cache) / 1000
val path = cursor.getStringValue(MediaStore.Audio.Media.DATA, cache)
val bucketId = cursor.getStringValue(MediaStore.Audio.Media.BUCKET_ID, cache)
result.add(DAudio(id, title, artist, path, duration, size, bucketId))
} while (cursor.moveToNext())
}
Expand All @@ -82,10 +83,11 @@ object AudioHelper : BaseContentHelper() {
val cursor = getSearchCursor(context, query)
val result = mutableListOf<TagRelationStub>()
if (cursor?.moveToFirst() == true) {
val cache = mutableMapOf<String, Int>()
do {
val id = cursor.getStringValue(MediaStore.Audio.Media._ID)
val title = cursor.getStringValue(MediaStore.Audio.Media.TITLE)
val size = cursor.getLongValue(MediaStore.Audio.Media.SIZE)
val id = cursor.getStringValue(MediaStore.Audio.Media._ID, cache)
val title = cursor.getStringValue(MediaStore.Audio.Media.TITLE, cache)
val size = cursor.getLongValue(MediaStore.Audio.Media.SIZE, cache)
result.add(TagRelationStub(id, title,size))
} while (cursor.moveToNext())
}
Expand All @@ -111,10 +113,11 @@ object AudioHelper : BaseContentHelper() {
)

cursor?.use { c ->
val cache = mutableMapOf<String, Int>()
while (c.moveToNext()) {
val bucketId = c.getStringValue(MediaStore.Audio.Media.BUCKET_ID)
val bucketName = c.getStringValue(MediaStore.Audio.Media.BUCKET_DISPLAY_NAME)
val path = c.getStringValue(MediaStore.Audio.Media.DATA)
val bucketId = c.getStringValue(MediaStore.Audio.Media.BUCKET_ID, cache)
val bucketName = c.getStringValue(MediaStore.Audio.Media.BUCKET_DISPLAY_NAME, cache)
val path = c.getStringValue(MediaStore.Audio.Media.DATA, cache)
val bucket = bucketMap[bucketName]
if (bucket != null) {
if (bucket.topItems.size < 4) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@ object BlockedNumberHelper {
BlockedNumberContract.BlockedNumbers.COLUMN_E164_NUMBER
)

MainApp.instance.queryCursor(uri, projection) { cursor ->
val id = cursor.getStringValue(BlockedNumberContract.BlockedNumbers.COLUMN_ID)
val number = cursor.getStringValue(BlockedNumberContract.BlockedNumbers.COLUMN_ORIGINAL_NUMBER)
val normalizedNumber = cursor.getStringValue(BlockedNumberContract.BlockedNumbers.COLUMN_E164_NUMBER)
MainApp.instance.queryCursor(uri, projection) { cursor, cache ->
val id = cursor.getStringValue(BlockedNumberContract.BlockedNumbers.COLUMN_ID, cache)
val number = cursor.getStringValue(BlockedNumberContract.BlockedNumbers.COLUMN_ORIGINAL_NUMBER, cache)
val normalizedNumber = cursor.getStringValue(BlockedNumberContract.BlockedNumbers.COLUMN_E164_NUMBER, cache)
val comparableNumber = normalizedNumber.trimToComparableNumber()
blockedNumbers.add(BlockedNumber(id, number, normalizedNumber, comparableNumber))
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,15 +94,16 @@ object CallHelper : BaseContentHelper() {
val cursor = getSearchCursor(context, query, limit, offset, SortBy(CallLog.Calls._ID, SortDirection.DESC))
val items = mutableListOf<DCall>()
if (cursor?.moveToFirst() == true) {
val cache = mutableMapOf<String, Int>()
do {
val id = cursor.getStringValue(CallLog.Calls._ID)
val number = cursor.getStringValue(CallLog.Calls.NUMBER)
val name = cursor.getStringValue(CallLog.Calls.CACHED_NAME)
val photoUri = cursor.getStringValue(CallLog.Calls.CACHED_PHOTO_URI)
val startTS = cursor.getTimeValue(CallLog.Calls.DATE)
val duration = cursor.getIntValue(CallLog.Calls.DURATION)
val type = cursor.getIntValue(CallLog.Calls.TYPE)
val accountId = cursor.getStringValue(CallLog.Calls.PHONE_ACCOUNT_ID)
val id = cursor.getStringValue(CallLog.Calls._ID, cache)
val number = cursor.getStringValue(CallLog.Calls.NUMBER, cache)
val name = cursor.getStringValue(CallLog.Calls.CACHED_NAME, cache)
val photoUri = cursor.getStringValue(CallLog.Calls.CACHED_PHOTO_URI, cache)
val startTS = cursor.getTimeValue(CallLog.Calls.DATE, cache)
val duration = cursor.getIntValue(CallLog.Calls.DURATION, cache)
val type = cursor.getIntValue(CallLog.Calls.TYPE, cache)
val accountId = cursor.getStringValue(CallLog.Calls.PHONE_ACCOUNT_ID, cache)
items.add(DCall(id, number, name, photoUri, startTS, duration, type, accountId))
} while (cursor.moveToNext())
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -225,20 +225,21 @@ object ContactHelper : BaseContentHelper() {
val cursor = getSearchCursorWithSortOrder(context, query, limit, offset, SortBy(ContactsContract.Data.DISPLAY_NAME_PRIMARY, SortDirection.ASC))
val items = mutableListOf<DContact>()
if (cursor?.moveToFirst() == true) {
val cache = mutableMapOf<String, Int>()
do {
val accountName = cursor.getStringValue(ContactsContract.RawContacts.ACCOUNT_NAME)
val rawId = cursor.getStringValue(ContactsContract.Data.RAW_CONTACT_ID)
val prefix = cursor.getStringValue(ContactsContract.CommonDataKinds.StructuredName.PREFIX)
val givenName = cursor.getStringValue(ContactsContract.CommonDataKinds.StructuredName.GIVEN_NAME)
val middleName = cursor.getStringValue(ContactsContract.CommonDataKinds.StructuredName.MIDDLE_NAME)
val familyName = cursor.getStringValue(ContactsContract.CommonDataKinds.StructuredName.FAMILY_NAME)
val suffix = cursor.getStringValue(ContactsContract.CommonDataKinds.StructuredName.SUFFIX)
val photoUri = cursor.getStringValue(ContactsContract.CommonDataKinds.StructuredName.PHOTO_URI)
val starred = cursor.getIntValue(ContactsContract.CommonDataKinds.StructuredName.STARRED)
val contactId = cursor.getStringValue(ContactsContract.Data.CONTACT_ID)
val thumbnailUri = cursor.getStringValue(ContactsContract.CommonDataKinds.StructuredName.PHOTO_THUMBNAIL_URI)
val ringtone = cursor.getStringValue(ContactsContract.CommonDataKinds.StructuredName.CUSTOM_RINGTONE)
val updatedAt = cursor.getTimeValue(ContactsContract.Data.CONTACT_LAST_UPDATED_TIMESTAMP)
val accountName = cursor.getStringValue(ContactsContract.RawContacts.ACCOUNT_NAME, cache)
val rawId = cursor.getStringValue(ContactsContract.Data.RAW_CONTACT_ID, cache)
val prefix = cursor.getStringValue(ContactsContract.CommonDataKinds.StructuredName.PREFIX, cache)
val givenName = cursor.getStringValue(ContactsContract.CommonDataKinds.StructuredName.GIVEN_NAME, cache)
val middleName = cursor.getStringValue(ContactsContract.CommonDataKinds.StructuredName.MIDDLE_NAME, cache)
val familyName = cursor.getStringValue(ContactsContract.CommonDataKinds.StructuredName.FAMILY_NAME, cache)
val suffix = cursor.getStringValue(ContactsContract.CommonDataKinds.StructuredName.SUFFIX, cache)
val photoUri = cursor.getStringValue(ContactsContract.CommonDataKinds.StructuredName.PHOTO_URI, cache)
val starred = cursor.getIntValue(ContactsContract.CommonDataKinds.StructuredName.STARRED, cache)
val contactId = cursor.getStringValue(ContactsContract.Data.CONTACT_ID, cache)
val thumbnailUri = cursor.getStringValue(ContactsContract.CommonDataKinds.StructuredName.PHOTO_THUMBNAIL_URI, cache)
val ringtone = cursor.getStringValue(ContactsContract.CommonDataKinds.StructuredName.CUSTOM_RINGTONE, cache)
val updatedAt = cursor.getTimeValue(ContactsContract.Data.CONTACT_LAST_UPDATED_TIMESTAMP, cache)

val nicknames = contentMap[rawId]?.nicknames ?: arrayListOf()
val notes = contentMap[rawId]?.notes ?: arrayListOf()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,65 +43,65 @@ object ContentHelper {
ContactsContract.Data.DATA6,
)

context.queryCursor(uri, projection) { cursor ->
val id = cursor.getStringValue(ContactsContract.Data.RAW_CONTACT_ID)
context.queryCursor(uri, projection) { cursor, cache ->
val id = cursor.getStringValue(ContactsContract.Data.RAW_CONTACT_ID, cache)
if (map[id] == null) {
map[id] = Content()
}

when (cursor.getStringValue(ContactsContract.Data.MIMETYPE)) {
when (cursor.getStringValue(ContactsContract.Data.MIMETYPE, cache)) {
ContactsContract.CommonDataKinds.Event.CONTENT_ITEM_TYPE -> {
val startDate = cursor.getStringValue(ContactsContract.CommonDataKinds.Event.START_DATE)
val type = cursor.getIntValue(ContactsContract.CommonDataKinds.Event.TYPE)
val label = cursor.getStringValue(ContactsContract.CommonDataKinds.Event.LABEL)
val startDate = cursor.getStringValue(ContactsContract.CommonDataKinds.Event.START_DATE, cache)
val type = cursor.getIntValue(ContactsContract.CommonDataKinds.Event.TYPE, cache)
val label = cursor.getStringValue(ContactsContract.CommonDataKinds.Event.LABEL, cache)
map[id]?.events?.add(ContentItem(startDate, type, label))
}
ContactsContract.CommonDataKinds.StructuredPostal.CONTENT_ITEM_TYPE -> {
val address = cursor.getStringValue(ContactsContract.CommonDataKinds.StructuredPostal.FORMATTED_ADDRESS)
val type = cursor.getIntValue(ContactsContract.CommonDataKinds.StructuredPostal.TYPE)
val label = cursor.getStringValue(ContactsContract.CommonDataKinds.StructuredPostal.LABEL)
val address = cursor.getStringValue(ContactsContract.CommonDataKinds.StructuredPostal.FORMATTED_ADDRESS, cache)
val type = cursor.getIntValue(ContactsContract.CommonDataKinds.StructuredPostal.TYPE, cache)
val label = cursor.getStringValue(ContactsContract.CommonDataKinds.StructuredPostal.LABEL, cache)
map[id]?.addresses?.add(ContentItem(address, type, label))
}
ContactsContract.CommonDataKinds.Email.CONTENT_ITEM_TYPE -> {
val email = cursor.getStringValue(ContactsContract.CommonDataKinds.Email.DATA)
val type = cursor.getIntValue(ContactsContract.CommonDataKinds.Email.TYPE)
val label = cursor.getStringValue(ContactsContract.CommonDataKinds.Email.LABEL)
val email = cursor.getStringValue(ContactsContract.CommonDataKinds.Email.DATA, cache)
val type = cursor.getIntValue(ContactsContract.CommonDataKinds.Email.TYPE, cache)
val label = cursor.getStringValue(ContactsContract.CommonDataKinds.Email.LABEL, cache)
map[id]?.emails?.add(ContentItem(email, type, label))
}
ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE -> {
val number = cursor.getStringValue(ContactsContract.CommonDataKinds.Phone.NUMBER)
val normalizedNumber = cursor.getStringValue(ContactsContract.CommonDataKinds.Phone.NORMALIZED_NUMBER)
val type = cursor.getIntValue(ContactsContract.CommonDataKinds.Phone.TYPE)
val label = cursor.getStringValue(ContactsContract.CommonDataKinds.Phone.LABEL)
val number = cursor.getStringValue(ContactsContract.CommonDataKinds.Phone.NUMBER, cache)
val normalizedNumber = cursor.getStringValue(ContactsContract.CommonDataKinds.Phone.NORMALIZED_NUMBER, cache)
val type = cursor.getIntValue(ContactsContract.CommonDataKinds.Phone.TYPE, cache)
val label = cursor.getStringValue(ContactsContract.CommonDataKinds.Phone.LABEL, cache)
map[id]?.phoneNumbers?.add(PhoneNumber(number, type, label, normalizedNumber))
}
ContactsContract.CommonDataKinds.Website.CONTENT_ITEM_TYPE -> {
val url = cursor.getStringValue(ContactsContract.CommonDataKinds.Website.URL)
val type = cursor.getIntValue(ContactsContract.CommonDataKinds.Website.TYPE)
val label = cursor.getStringValue(ContactsContract.CommonDataKinds.Website.LABEL)
val url = cursor.getStringValue(ContactsContract.CommonDataKinds.Website.URL, cache)
val type = cursor.getIntValue(ContactsContract.CommonDataKinds.Website.TYPE, cache)
val label = cursor.getStringValue(ContactsContract.CommonDataKinds.Website.LABEL, cache)
map[id]?.websites?.add(ContentItem(url, type, label))
}
ContactsContract.CommonDataKinds.Nickname.CONTENT_ITEM_TYPE -> {
val name = cursor.getStringValue(ContactsContract.CommonDataKinds.Nickname.NAME)
val name = cursor.getStringValue(ContactsContract.CommonDataKinds.Nickname.NAME, cache)
map[id]?.nicknames?.add(name)
}
ContactsContract.CommonDataKinds.Im.CONTENT_ITEM_TYPE -> {
val value = cursor.getStringValue(ContactsContract.CommonDataKinds.Im.DATA)
val type = cursor.getIntValue(ContactsContract.CommonDataKinds.Im.PROTOCOL)
val label = cursor.getStringValue(ContactsContract.CommonDataKinds.Im.CUSTOM_PROTOCOL)
val value = cursor.getStringValue(ContactsContract.CommonDataKinds.Im.DATA, cache)
val type = cursor.getIntValue(ContactsContract.CommonDataKinds.Im.PROTOCOL, cache)
val label = cursor.getStringValue(ContactsContract.CommonDataKinds.Im.CUSTOM_PROTOCOL, cache)
map[id]?.ims?.add(ContentItem(value, type, label))
}
ContactsContract.CommonDataKinds.Note.CONTENT_ITEM_TYPE -> {
val note = cursor.getStringValue(ContactsContract.CommonDataKinds.Note.NOTE)
val note = cursor.getStringValue(ContactsContract.CommonDataKinds.Note.NOTE, cache)
map[id]?.notes?.add(note)
}
ContactsContract.CommonDataKinds.Organization.CONTENT_ITEM_TYPE -> {
val company = cursor.getStringValue(ContactsContract.CommonDataKinds.Organization.COMPANY)
val title = cursor.getStringValue(ContactsContract.CommonDataKinds.Organization.TITLE)
val company = cursor.getStringValue(ContactsContract.CommonDataKinds.Organization.COMPANY, cache)
val title = cursor.getStringValue(ContactsContract.CommonDataKinds.Organization.TITLE, cache)
map[id]?.organizations?.add(Organization(company, title))
}
ContactsContract.CommonDataKinds.GroupMembership.CONTENT_ITEM_TYPE -> {
val groupId = cursor.getIntValue(ContactsContract.CommonDataKinds.GroupMembership.GROUP_ROW_ID)
val groupId = cursor.getIntValue(ContactsContract.CommonDataKinds.GroupMembership.GROUP_ROW_ID, cache)
map[id]?.groupIds?.add(groupId)
}
}
Expand Down
Loading

0 comments on commit 8e0371e

Please sign in to comment.