Use existing contact type for our linked entry. Add test to sample app.

Fixes #9431
Closes #9434

Co-authored-by: swatts <github@stargw.net>
This commit is contained in:
Greyson Parrelli
2022-03-29 13:43:41 -04:00
committed by Cody Henthorne
parent 4098f77e08
commit 0e4187b062
62 changed files with 287 additions and 113 deletions

View File

@@ -0,0 +1,33 @@
package org.signal.contactstest
import android.accounts.Account
import android.content.AbstractThreadedSyncAdapter
import android.content.ContentProviderClient
import android.content.Context
import android.content.SyncResult
import android.os.Bundle
import org.signal.core.util.logging.Log
class ContactsSyncAdapter(context: Context?, autoInitialize: Boolean) : AbstractThreadedSyncAdapter(context, autoInitialize) {
override fun onPerformSync(
account: Account,
extras: Bundle,
authority: String,
provider: ContentProviderClient,
syncResult: SyncResult
) {
Log.i(TAG, "onPerformSync()")
}
override fun onSyncCanceled() {
Log.w(TAG, "onSyncCanceled()")
}
override fun onSyncCanceled(thread: Thread) {
Log.w(TAG, "onSyncCanceled($thread)")
}
companion object {
private val TAG = Log.tag(ContactsSyncAdapter::class.java)
}
}

View File

@@ -0,0 +1,22 @@
package org.signal.contactstest
import android.app.Service
import android.content.Intent
import android.os.IBinder
class ContactsSyncAdapterService : Service() {
@Synchronized
override fun onCreate() {
if (syncAdapter == null) {
syncAdapter = ContactsSyncAdapter(this, true)
}
}
override fun onBind(intent: Intent): IBinder? {
return syncAdapter!!.syncAdapterBinder
}
companion object {
private var syncAdapter: ContactsSyncAdapter? = null
}
}

View File

@@ -1,13 +1,19 @@
package org.signal.contactstest
import android.Manifest
import android.accounts.Account
import android.content.Intent
import android.content.pm.PackageManager
import android.os.Bundle
import android.telephony.PhoneNumberUtils
import android.widget.Button
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import androidx.core.app.ActivityCompat
import androidx.core.content.ContextCompat
import org.signal.contacts.ContactLinkConfiguration
import org.signal.contacts.SystemContactsRepository
import org.signal.core.util.concurrent.SimpleTask
import org.signal.core.util.logging.Log
class MainActivity : AppCompatActivity() {
@@ -22,15 +28,64 @@ class MainActivity : AppCompatActivity() {
setContentView(R.layout.activity_main)
if (hasPermission(Manifest.permission.READ_CONTACTS) && hasPermission(Manifest.permission.WRITE_CONTACTS)) {
Log.i(TAG, "Already have permission.")
startActivity(Intent(this, ContactsActivity::class.java))
finish()
return
findViewById<Button>(R.id.contact_list_button).setOnClickListener { v ->
if (hasPermission(Manifest.permission.READ_CONTACTS) && hasPermission(Manifest.permission.WRITE_CONTACTS)) {
startActivity(Intent(this, ContactsActivity::class.java))
finish()
} else {
ActivityCompat.requestPermissions(this, arrayOf(Manifest.permission.READ_CONTACTS, Manifest.permission.WRITE_CONTACTS), PERMISSION_CODE)
}
}
findViewById<Button>(R.id.permission_button).setOnClickListener { v ->
requestPermissions(arrayOf(Manifest.permission.READ_CONTACTS, Manifest.permission.WRITE_CONTACTS), PERMISSION_CODE)
findViewById<Button>(R.id.link_contacts_button).setOnClickListener { v ->
if (hasPermission(Manifest.permission.READ_CONTACTS) && hasPermission(Manifest.permission.WRITE_CONTACTS)) {
SimpleTask.run({
val allE164s: Set<String> = SystemContactsRepository.getAllDisplayNumbers(this).map { PhoneNumberUtils.formatNumberToE164(it, "US") }.toSet()
val account: Account = SystemContactsRepository.getOrCreateSystemAccount(this, BuildConfig.APPLICATION_ID, "Contact Test") ?: return@run false
SystemContactsRepository.addMessageAndCallLinksToContacts(
context = this,
config = buildLinkConfig(account),
targetE164s = allE164s,
removeIfMissing = true
)
return@run true
}, { success ->
if (success) {
Toast.makeText(this, "Success!", Toast.LENGTH_SHORT).show()
} else {
Toast.makeText(this, "Failed to create account!", Toast.LENGTH_SHORT).show()
}
})
} else {
ActivityCompat.requestPermissions(this, arrayOf(Manifest.permission.READ_CONTACTS, Manifest.permission.WRITE_CONTACTS), PERMISSION_CODE)
}
}
findViewById<Button>(R.id.unlink_contact_button).setOnClickListener { v ->
if (hasPermission(Manifest.permission.READ_CONTACTS) && hasPermission(Manifest.permission.WRITE_CONTACTS)) {
SimpleTask.run({
val account: Account = SystemContactsRepository.getOrCreateSystemAccount(this, BuildConfig.APPLICATION_ID, "Contact Test") ?: return@run false
SystemContactsRepository.addMessageAndCallLinksToContacts(
context = this,
config = buildLinkConfig(account),
targetE164s = emptySet(),
removeIfMissing = true
)
return@run true
}, { success ->
if (success) {
Toast.makeText(this, "Success!", Toast.LENGTH_SHORT).show()
} else {
Toast.makeText(this, "Failed to create account!", Toast.LENGTH_SHORT).show()
}
})
} else {
ActivityCompat.requestPermissions(this, arrayOf(Manifest.permission.READ_CONTACTS, Manifest.permission.WRITE_CONTACTS), PERMISSION_CODE)
}
}
}
@@ -48,4 +103,17 @@ class MainActivity : AppCompatActivity() {
private fun hasPermission(permission: String): Boolean {
return ContextCompat.checkSelfPermission(this, permission) == PackageManager.PERMISSION_GRANTED
}
private fun buildLinkConfig(account: Account): ContactLinkConfiguration {
return ContactLinkConfiguration(
account = account,
appName = "Contact Test",
messagePrompt = { "(Test) Message $it" },
callPrompt = { "(Test) Call $it" },
e164Formatter = { PhoneNumberUtils.formatNumberToE164(it, "US") },
messageMimetype = "vnd.android.cursor.item/vnd.org.signal.contacts.test.message",
callMimetype = "vnd.android.cursor.item/vnd.org.signal.contacts.test.call",
syncTag = "__TEST"
)
}
}