adding the function fetching recent calls

This commit is contained in:
tibbi 2018-08-03 15:59:24 +02:00
parent a83886a5bd
commit 79e8234d43
5 changed files with 62 additions and 6 deletions

View File

@ -45,7 +45,7 @@ ext {
}
dependencies {
implementation 'com.simplemobiletools:commons:4.5.16'
implementation 'com.simplemobiletools:commons:4.5.17'
implementation 'joda-time:joda-time:2.9.9'
implementation 'com.facebook.stetho:stetho:1.5.0'

View File

@ -11,6 +11,8 @@
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.GET_ACCOUNTS"/>
<uses-permission android:name="android.permission.READ_SYNC_SETTINGS"/>
<uses-permission android:name="android.permission.READ_CALL_LOG"/>
<uses-permission android:name="android.permission.WRITE_CALL_LOG"/>
<uses-permission
android:name="android.permission.USE_FINGERPRINT"

View File

@ -37,7 +37,6 @@ import kotlinx.android.synthetic.main.activity_main.*
import kotlinx.android.synthetic.main.fragment_contacts.*
import kotlinx.android.synthetic.main.fragment_favorites.*
import kotlinx.android.synthetic.main.fragment_groups.*
import kotlinx.android.synthetic.main.fragment_recents.*
import java.io.FileOutputStream
class MainActivity : SimpleActivity(), RefreshContactsListener {
@ -76,6 +75,11 @@ class MainActivity : SimpleActivity(), RefreshContactsListener {
ActivityCompat.requestPermissions(this, arrayOf(Manifest.permission.GET_ACCOUNTS), 34)
}
val hasWriteCallLogPermission = ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_CALL_LOG) == PackageManager.PERMISSION_GRANTED
if (!hasWriteCallLogPermission) {
ActivityCompat.requestPermissions(this, arrayOf(Manifest.permission.WRITE_CALL_LOG), 35)
}
storeLocalAccountData()
initFragments()
}
@ -467,10 +471,6 @@ class MainActivity : SimpleActivity(), RefreshContactsListener {
favorites_fragment?.refreshContacts(it)
}
if (refreshTabsMask and RECENTS_TAB_MASK != 0) {
recents_fragment?.refreshContacts(it)
}
if (refreshTabsMask and GROUPS_TAB_MASK != 0) {
if (refreshTabsMask == GROUPS_TAB_MASK) {
groups_fragment.skipHashComparing = true
@ -478,6 +478,12 @@ class MainActivity : SimpleActivity(), RefreshContactsListener {
groups_fragment?.refreshContacts(it)
}
}
if (refreshTabsMask and RECENTS_TAB_MASK != 0) {
ContactsHelper(this).getRecents {
}
}
}
private fun getAllFragments() = arrayListOf(contacts_fragment, favorites_fragment, groups_fragment)

View File

@ -2,11 +2,13 @@ package com.simplemobiletools.contacts.helpers
import android.accounts.Account
import android.accounts.AccountManager
import android.annotation.SuppressLint
import android.app.Activity
import android.content.*
import android.database.Cursor
import android.graphics.Bitmap
import android.net.Uri
import android.provider.CallLog
import android.provider.ContactsContract
import android.provider.ContactsContract.CommonDataKinds
import android.provider.ContactsContract.CommonDataKinds.Note
@ -1309,4 +1311,47 @@ class ContactsHelper(val activity: Activity) {
}
}.start()
}
@SuppressLint("MissingPermission")
fun getRecents(callback: (ArrayList<RecentCall>) -> Unit) {
Thread {
val calls = ArrayList<RecentCall>()
if (!activity.hasPermission(PERMISSION_WRITE_CALL_LOG)) {
callback(calls)
return@Thread
}
val uri = android.provider.CallLog.Calls.CONTENT_URI
val projection = arrayOf(
CallLog.Calls._ID,
CallLog.Calls.NUMBER,
CallLog.Calls.DATE,
CallLog.Calls.DURATION,
CallLog.Calls.CACHED_NAME,
CallLog.Calls.TYPE
)
val sorting = "${CallLog.Calls._ID} DESC LIMIT 50"
var cursor: Cursor? = null
try {
cursor = activity.contentResolver.query(uri, projection, null, null, sorting)
if (cursor?.moveToFirst() == true) {
do {
val id = cursor.getIntValue(CallLog.Calls._ID)
val number = cursor.getStringValue(CallLog.Calls.NUMBER)
val date = cursor.getLongValue(CallLog.Calls.DATE)
val duration = cursor.getIntValue(CallLog.Calls.DURATION)
val name = cursor.getStringValue(CallLog.Calls.CACHED_NAME) ?: ""
val type = cursor.getIntValue(CallLog.Calls.TYPE)
val recentCall = RecentCall(id, number, date, duration, name, type)
calls.add(recentCall)
} while (cursor.moveToNext())
}
} finally {
cursor?.close()
}
callback(calls)
}.start()
}
}

View File

@ -0,0 +1,3 @@
package com.simplemobiletools.contacts.models
data class RecentCall(var id: Int, var number: String, var date: Long, var duration: Int, var name: String, var type: Int)