1*21373fa4SWill Schurman package host.exp.exponent.utils
2*21373fa4SWill Schurman 
3*21373fa4SWill Schurman import android.content.ContentProviderOperation
4*21373fa4SWill Schurman import android.provider.ContactsContract
5*21373fa4SWill Schurman import android.content.Context
6*21373fa4SWill Schurman import android.net.Uri
7*21373fa4SWill Schurman import java.lang.Exception
8*21373fa4SWill Schurman import java.lang.RuntimeException
9*21373fa4SWill Schurman import java.util.ArrayList
10*21373fa4SWill Schurman 
11*21373fa4SWill Schurman object TestContacts {
addnull12*21373fa4SWill Schurman   fun add(context: Context) {
13*21373fa4SWill Schurman     removeAllContacts(context)
14*21373fa4SWill Schurman     addContact(context, "JESSE", "TEST", "1234567890", "[email protected]")
15*21373fa4SWill Schurman     addContact(context, "BEN", "TEST", "1234567891", "[email protected]")
16*21373fa4SWill Schurman     addContact(context, "JAMES", "TEST", "1234567892", "[email protected]")
17*21373fa4SWill Schurman     addContact(context, "BRENT", "TEST", "1234567894", "[email protected]")
18*21373fa4SWill Schurman   }
19*21373fa4SWill Schurman 
addContactnull20*21373fa4SWill Schurman   private fun addContact(
21*21373fa4SWill Schurman     context: Context,
22*21373fa4SWill Schurman     firstName: String,
23*21373fa4SWill Schurman     lastName: String,
24*21373fa4SWill Schurman     phoneNumber: String,
25*21373fa4SWill Schurman     email: String
26*21373fa4SWill Schurman   ) {
27*21373fa4SWill Schurman     val operationList = ArrayList<ContentProviderOperation>()
28*21373fa4SWill Schurman     operationList.add(
29*21373fa4SWill Schurman       ContentProviderOperation.newInsert(ContactsContract.RawContacts.CONTENT_URI)
30*21373fa4SWill Schurman         .withValue(ContactsContract.RawContacts.ACCOUNT_TYPE, null)
31*21373fa4SWill Schurman         .withValue(ContactsContract.RawContacts.ACCOUNT_NAME, null)
32*21373fa4SWill Schurman         .build()
33*21373fa4SWill Schurman     )
34*21373fa4SWill Schurman 
35*21373fa4SWill Schurman     // first and last names
36*21373fa4SWill Schurman     operationList.add(
37*21373fa4SWill Schurman       ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)
38*21373fa4SWill Schurman         .withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, 0)
39*21373fa4SWill Schurman         .withValue(
40*21373fa4SWill Schurman           ContactsContract.Data.MIMETYPE,
41*21373fa4SWill Schurman           ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE
42*21373fa4SWill Schurman         )
43*21373fa4SWill Schurman         .withValue(ContactsContract.CommonDataKinds.StructuredName.GIVEN_NAME, firstName)
44*21373fa4SWill Schurman         .withValue(ContactsContract.CommonDataKinds.StructuredName.FAMILY_NAME, lastName)
45*21373fa4SWill Schurman         .build()
46*21373fa4SWill Schurman     )
47*21373fa4SWill Schurman     operationList.add(
48*21373fa4SWill Schurman       ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)
49*21373fa4SWill Schurman         .withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, 0)
50*21373fa4SWill Schurman         .withValue(
51*21373fa4SWill Schurman           ContactsContract.Data.MIMETYPE,
52*21373fa4SWill Schurman           ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE
53*21373fa4SWill Schurman         )
54*21373fa4SWill Schurman         .withValue(ContactsContract.CommonDataKinds.Phone.NUMBER, phoneNumber)
55*21373fa4SWill Schurman         .withValue(
56*21373fa4SWill Schurman           ContactsContract.CommonDataKinds.Phone.TYPE,
57*21373fa4SWill Schurman           ContactsContract.CommonDataKinds.Phone.TYPE_HOME
58*21373fa4SWill Schurman         )
59*21373fa4SWill Schurman         .build()
60*21373fa4SWill Schurman     )
61*21373fa4SWill Schurman     operationList.add(
62*21373fa4SWill Schurman       ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)
63*21373fa4SWill Schurman         .withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, 0)
64*21373fa4SWill Schurman         .withValue(
65*21373fa4SWill Schurman           ContactsContract.Data.MIMETYPE,
66*21373fa4SWill Schurman           ContactsContract.CommonDataKinds.Email.CONTENT_ITEM_TYPE
67*21373fa4SWill Schurman         )
68*21373fa4SWill Schurman         .withValue(ContactsContract.CommonDataKinds.Email.DATA, email)
69*21373fa4SWill Schurman         .withValue(
70*21373fa4SWill Schurman           ContactsContract.CommonDataKinds.Email.TYPE,
71*21373fa4SWill Schurman           ContactsContract.CommonDataKinds.Email.TYPE_WORK
72*21373fa4SWill Schurman         )
73*21373fa4SWill Schurman         .build()
74*21373fa4SWill Schurman     )
75*21373fa4SWill Schurman     try {
76*21373fa4SWill Schurman       context.contentResolver.applyBatch(ContactsContract.AUTHORITY, operationList)
77*21373fa4SWill Schurman     } catch (e: Exception) {
78*21373fa4SWill Schurman       throw RuntimeException(e)
79*21373fa4SWill Schurman     }
80*21373fa4SWill Schurman   }
81*21373fa4SWill Schurman 
removeAllContactsnull82*21373fa4SWill Schurman   private fun removeAllContacts(context: Context) {
83*21373fa4SWill Schurman     val cr = context.contentResolver
84*21373fa4SWill Schurman     val cursor = cr.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null) ?: return
85*21373fa4SWill Schurman     cursor.use {
86*21373fa4SWill Schurman       while (it.moveToNext()) {
87*21373fa4SWill Schurman         val id = it.getString(it.getColumnIndex(ContactsContract.Contacts._ID))
88*21373fa4SWill Schurman         val cur1 = cr.query(
89*21373fa4SWill Schurman           ContactsContract.CommonDataKinds.Email.CONTENT_URI, null,
90*21373fa4SWill Schurman           ContactsContract.CommonDataKinds.Email.CONTACT_ID + " = ?", arrayOf(id), null
91*21373fa4SWill Schurman         )
92*21373fa4SWill Schurman         var shouldDelete = false
93*21373fa4SWill Schurman         while (cur1!!.moveToNext()) {
94*21373fa4SWill Schurman           val email =
95*21373fa4SWill Schurman             cur1.getString(cur1.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA))
96*21373fa4SWill Schurman           if (email != null && email.endsWith("@testexpo.io")) {
97*21373fa4SWill Schurman             shouldDelete = true
98*21373fa4SWill Schurman             break
99*21373fa4SWill Schurman           }
100*21373fa4SWill Schurman         }
101*21373fa4SWill Schurman         cur1.close()
102*21373fa4SWill Schurman         if (shouldDelete) {
103*21373fa4SWill Schurman           val lookupKey = it.getString(it.getColumnIndex(ContactsContract.Contacts.LOOKUP_KEY))
104*21373fa4SWill Schurman           val uri = Uri.withAppendedPath(ContactsContract.Contacts.CONTENT_LOOKUP_URI, lookupKey)
105*21373fa4SWill Schurman           cr.delete(uri, null, null)
106*21373fa4SWill Schurman         }
107*21373fa4SWill Schurman       }
108*21373fa4SWill Schurman     }
109*21373fa4SWill Schurman   }
110*21373fa4SWill Schurman }
111