added fetch contact from real device icon
This commit is contained in:
@@ -1,6 +1,8 @@
|
||||
<manifest xmlns:android="http://schemas.android.com/apk/res/android">
|
||||
|
||||
<uses-permission android:name="android.permission.INTERNET" />
|
||||
<uses-permission android:name="android.permission.READ_CONTACTS" />
|
||||
|
||||
|
||||
<application
|
||||
android:name=".MainApplication"
|
||||
|
||||
@@ -0,0 +1,48 @@
|
||||
package com.circlecollect
|
||||
|
||||
import android.Manifest
|
||||
import android.content.ContentResolver
|
||||
import android.content.pm.PackageManager
|
||||
import android.provider.ContactsContract
|
||||
import androidx.core.app.ActivityCompat
|
||||
import com.facebook.react.bridge.*
|
||||
|
||||
class ContactsModule(reactContext: ReactApplicationContext) : ReactContextBaseJavaModule(reactContext) {
|
||||
|
||||
override fun getName(): String {
|
||||
return "ContactsModule"
|
||||
}
|
||||
|
||||
@ReactMethod
|
||||
fun getContacts(promise: Promise) {
|
||||
val context = reactApplicationContext
|
||||
if (ActivityCompat.checkSelfPermission(context, Manifest.permission.READ_CONTACTS) != PackageManager.PERMISSION_GRANTED) {
|
||||
promise.reject("PERMISSION_DENIED", "Read contacts permission not granted")
|
||||
return
|
||||
}
|
||||
|
||||
val contactsList = Arguments.createArray()
|
||||
val resolver: ContentResolver = context.contentResolver
|
||||
val cursor = resolver.query(
|
||||
ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
|
||||
null,
|
||||
null,
|
||||
null,
|
||||
ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME + " ASC"
|
||||
)
|
||||
|
||||
cursor?.use {
|
||||
val nameIndex = cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME)
|
||||
val numberIndex = cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER)
|
||||
|
||||
while (cursor.moveToNext()) {
|
||||
val contact = Arguments.createMap()
|
||||
contact.putString("name", cursor.getString(nameIndex))
|
||||
contact.putString("phone", cursor.getString(numberIndex))
|
||||
contactsList.pushMap(contact)
|
||||
}
|
||||
}
|
||||
|
||||
promise.resolve(contactsList)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
package com.circlecollect
|
||||
|
||||
import com.facebook.react.ReactPackage
|
||||
import com.facebook.react.bridge.NativeModule
|
||||
import com.facebook.react.bridge.ReactApplicationContext
|
||||
import com.facebook.react.uimanager.ViewManager
|
||||
|
||||
class ContactsPackage : ReactPackage {
|
||||
override fun createNativeModules(reactContext: ReactApplicationContext): List<NativeModule> {
|
||||
return listOf(ContactsModule(reactContext))
|
||||
}
|
||||
|
||||
override fun createViewManagers(reactContext: ReactApplicationContext): List<ViewManager<*, *>> {
|
||||
return emptyList()
|
||||
}
|
||||
}
|
||||
@@ -9,6 +9,7 @@ import com.facebook.react.ReactNativeHost
|
||||
import com.facebook.react.ReactPackage
|
||||
import com.facebook.react.defaults.DefaultReactHost.getDefaultReactHost
|
||||
import com.facebook.react.defaults.DefaultReactNativeHost
|
||||
import com.circlecollect.ContactsPackage
|
||||
|
||||
class MainApplication : Application(), ReactApplication {
|
||||
|
||||
@@ -18,6 +19,7 @@ class MainApplication : Application(), ReactApplication {
|
||||
PackageList(this).packages.apply {
|
||||
// Packages that cannot be autolinked yet can be added manually here, for example:
|
||||
// add(MyReactNativePackage())
|
||||
add(ContactsPackage())
|
||||
}
|
||||
|
||||
override fun getJSMainModuleName(): String = "index"
|
||||
|
||||
Reference in New Issue
Block a user