mirror of
				https://github.com/SimpleMobileTools/Simple-Contacts.git
				synced 2025-06-05 21:59:27 +02:00 
			
		
		
		
	adding an initial implementation of a Contacts fetcher
This commit is contained in:
		| @@ -32,5 +32,5 @@ android { | |||||||
| } | } | ||||||
|  |  | ||||||
| dependencies { | dependencies { | ||||||
|     implementation 'com.simplemobiletools:commons:3.2.4' |     implementation 'com.simplemobiletools:commons:3.2.5' | ||||||
| } | } | ||||||
|   | |||||||
| @@ -13,6 +13,7 @@ import com.simplemobiletools.commons.helpers.PERMISSION_WRITE_CONTACTS | |||||||
| import com.simplemobiletools.contacts.BuildConfig | import com.simplemobiletools.contacts.BuildConfig | ||||||
| import com.simplemobiletools.contacts.R | import com.simplemobiletools.contacts.R | ||||||
| import com.simplemobiletools.contacts.extensions.config | import com.simplemobiletools.contacts.extensions.config | ||||||
|  | import com.simplemobiletools.contacts.helpers.ContactsHelper | ||||||
|  |  | ||||||
| class MainActivity : SimpleActivity() { | class MainActivity : SimpleActivity() { | ||||||
|     var storedUseEnglish = false |     var storedUseEnglish = false | ||||||
| @@ -68,6 +69,8 @@ class MainActivity : SimpleActivity() { | |||||||
|     } |     } | ||||||
|  |  | ||||||
|     private fun initContacts() { |     private fun initContacts() { | ||||||
|  |         ContactsHelper(this).getContacts { | ||||||
|  |  | ||||||
|         } |         } | ||||||
|     } |     } | ||||||
|  | } | ||||||
|   | |||||||
| @@ -0,0 +1 @@ | |||||||
|  | package com.simplemobiletools.contacts.helpers | ||||||
| @@ -0,0 +1,44 @@ | |||||||
|  | package com.simplemobiletools.contacts.helpers | ||||||
|  |  | ||||||
|  | import android.database.Cursor | ||||||
|  | import android.provider.ContactsContract | ||||||
|  | import com.simplemobiletools.commons.extensions.getIntValue | ||||||
|  | import com.simplemobiletools.commons.extensions.getStringValue | ||||||
|  | import com.simplemobiletools.commons.extensions.showErrorToast | ||||||
|  | import com.simplemobiletools.contacts.activities.SimpleActivity | ||||||
|  | import com.simplemobiletools.contacts.models.Contact | ||||||
|  |  | ||||||
|  | class ContactsHelper(val activity: SimpleActivity) { | ||||||
|  |     fun getContacts(callback: (ArrayList<Contact>) -> Unit) { | ||||||
|  |         val contacts = ArrayList<Contact>() | ||||||
|  |         Thread { | ||||||
|  |             val uri = ContactsContract.CommonDataKinds.Phone.CONTENT_URI | ||||||
|  |             val projection = arrayOf( | ||||||
|  |                     ContactsContract.CommonDataKinds.Phone.CONTACT_ID, | ||||||
|  |                     ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME, | ||||||
|  |                     ContactsContract.CommonDataKinds.Phone.NUMBER, | ||||||
|  |                     ContactsContract.CommonDataKinds.Phone.PHOTO_URI | ||||||
|  |             ) | ||||||
|  |  | ||||||
|  |             var cursor: Cursor? = null | ||||||
|  |             try { | ||||||
|  |                 cursor = activity.contentResolver.query(uri, projection, null, null, null) | ||||||
|  |                 if (cursor?.moveToFirst() == true) { | ||||||
|  |                     do { | ||||||
|  |                         val id = cursor.getIntValue(ContactsContract.CommonDataKinds.Phone.CONTACT_ID) | ||||||
|  |                         val name = cursor.getStringValue(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME) ?: continue | ||||||
|  |                         val number = cursor.getStringValue(ContactsContract.CommonDataKinds.Phone.NUMBER) ?: continue | ||||||
|  |                         val photoUri = cursor.getStringValue(ContactsContract.CommonDataKinds.Phone.PHOTO_URI) ?: "" | ||||||
|  |                         val contact = Contact(id, name, number, photoUri) | ||||||
|  |                         contacts.add(contact) | ||||||
|  |                     } while (cursor.moveToNext()) | ||||||
|  |                 } | ||||||
|  |             } catch (e: Exception) { | ||||||
|  |                 activity.showErrorToast(e) | ||||||
|  |             } finally { | ||||||
|  |                 cursor?.close() | ||||||
|  |             } | ||||||
|  |             callback(contacts) | ||||||
|  |         }.start() | ||||||
|  |     } | ||||||
|  | } | ||||||
| @@ -0,0 +1,3 @@ | |||||||
|  | package com.simplemobiletools.contacts.models | ||||||
|  |  | ||||||
|  | data class Contact(val id: Int, var name: String, var number: String, var photoUri: String) | ||||||
							
								
								
									
										50
									
								
								app/src/main/res/layout/item_contact.xml
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										50
									
								
								app/src/main/res/layout/item_contact.xml
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,50 @@ | |||||||
|  | <?xml version="1.0" encoding="utf-8"?> | ||||||
|  | <FrameLayout | ||||||
|  |     xmlns:android="http://schemas.android.com/apk/res/android" | ||||||
|  |     xmlns:tools="http://schemas.android.com/tools" | ||||||
|  |     android:id="@+id/contact_frame" | ||||||
|  |     android:layout_width="match_parent" | ||||||
|  |     android:layout_height="wrap_content" | ||||||
|  |     android:background="?attr/selectableItemBackground" | ||||||
|  |     android:clickable="true" | ||||||
|  |     android:focusable="true" | ||||||
|  |     android:foreground="@drawable/selector"> | ||||||
|  |  | ||||||
|  |     <RelativeLayout | ||||||
|  |         android:id="@+id/contact_holder" | ||||||
|  |         android:layout_width="match_parent" | ||||||
|  |         android:layout_height="wrap_content" | ||||||
|  |         android:paddingRight="@dimen/activity_margin"> | ||||||
|  |  | ||||||
|  |         <ImageView | ||||||
|  |             android:id="@+id/contact_tmb" | ||||||
|  |             android:layout_width="@dimen/normal_icon_size" | ||||||
|  |             android:layout_height="@dimen/normal_icon_size" | ||||||
|  |             android:padding="@dimen/medium_margin" | ||||||
|  |             android:src="@drawable/ic_person"/> | ||||||
|  |  | ||||||
|  |         <TextView | ||||||
|  |             android:id="@+id/contact_name" | ||||||
|  |             android:layout_width="match_parent" | ||||||
|  |             android:layout_height="wrap_content" | ||||||
|  |             android:layout_alignTop="@+id/contact_tmb" | ||||||
|  |             android:layout_toRightOf="@+id/contact_tmb" | ||||||
|  |             android:ellipsize="end" | ||||||
|  |             android:maxLines="1" | ||||||
|  |             android:paddingLeft="@dimen/small_margin" | ||||||
|  |             android:paddingTop="@dimen/small_margin" | ||||||
|  |             tools:text="John Doe"/> | ||||||
|  |  | ||||||
|  |         <TextView | ||||||
|  |             android:id="@+id/contact_number" | ||||||
|  |             android:layout_width="match_parent" | ||||||
|  |             android:layout_height="wrap_content" | ||||||
|  |             android:layout_below="@+id/contact_name" | ||||||
|  |             android:layout_toRightOf="@+id/contact_tmb" | ||||||
|  |             android:maxLines="1" | ||||||
|  |             android:paddingLeft="@dimen/small_margin" | ||||||
|  |             android:textSize="@dimen/smaller_text_size" | ||||||
|  |             tools:text="0123 456 789"/> | ||||||
|  |  | ||||||
|  |     </RelativeLayout> | ||||||
|  | </FrameLayout> | ||||||
		Reference in New Issue
	
	Block a user