├── .gitignore ├── CHANGELOG.md ├── README.md ├── build.gradle ├── contacts ├── .gitignore ├── build.gradle └── src │ └── main │ ├── AndroidManifest.xml │ └── java │ └── com │ └── github │ └── tamir7 │ └── contacts │ ├── Address.java │ ├── Contact.java │ ├── Contacts.java │ ├── CursorHelper.java │ ├── Email.java │ ├── Event.java │ ├── PhoneNumber.java │ ├── Query.java │ └── Where.java ├── gradle.properties ├── gradle └── wrapper │ ├── gradle-wrapper.jar │ └── gradle-wrapper.properties ├── gradlew ├── gradlew.bat ├── sample ├── .gitignore ├── build.gradle └── src │ └── main │ ├── AndroidManifest.xml │ ├── java │ └── com │ │ └── github │ │ └── tamir7 │ │ └── contacts │ │ └── sample │ │ ├── SampleActivity.java │ │ └── SampleApplication.java │ └── res │ ├── layout │ └── activity_sample.xml │ ├── mipmap-hdpi │ └── ic_launcher.png │ ├── mipmap-mdpi │ └── ic_launcher.png │ ├── mipmap-xhdpi │ └── ic_launcher.png │ ├── mipmap-xxhdpi │ └── ic_launcher.png │ ├── mipmap-xxxhdpi │ └── ic_launcher.png │ └── values │ ├── colors.xml │ ├── strings.xml │ └── styles.xml └── settings.gradle /.gitignore: -------------------------------------------------------------------------------- 1 | *.iml 2 | .gradle 3 | /local.properties 4 | /.idea 5 | .DS_Store 6 | /build 7 | /captures 8 | *.apk 9 | *.pro -------------------------------------------------------------------------------- /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | Change Log 2 | ========== 3 | 4 | Version 1.1.7 *(22-05-2017)* 5 | ------------------ 6 | * Feature: added a getter for contact note. 7 | * Feature: made ContactId field public. 8 | 9 | 10 | Version 1.1.6 *(21-05-2017)* 11 | ------------------ 12 | * Feature: added a getter for address, company info and websites. 13 | 14 | Version 1.1.5 *(14-05-2017)* 15 | ------------------ 16 | * Feature: added a getter for contact ID. 17 | 18 | Version 1.1.4 *(04-01-2017)* 19 | ------------------ 20 | * Bug fix: getGivenName now returns correct value. 21 | 22 | Version 1.1.3 *(10-11-2016)* 23 | ------------------ 24 | * Bug fix: Fixed gradle upload script. 25 | 26 | Version 1.1.2 *(10-11-2016)* (Version not uploaded to Jcenter correctly) 27 | ------------------ 28 | * Feature: Added whereNotEqualTo api to query. 29 | * Bug Fix: getPhoneNumber now returns phoneNumber instead of serializedPhoneNumber. 30 | 31 | 32 | Version 1.1.1 *(22-08-2016)* 33 | ------------------ 34 | * Fetched Data: 35 | * Added StructuredName: GivenName and FamilyName. 36 | 37 | Version 1.1.0 *(17-08-2016)* 38 | ------------------ 39 | * Feature: Added phoneNumber field to PhoneNumber. (renamed old phoneNumber field to phoneNormalizedNumber). 40 | * Feature: Added OR constraints. (See readme for usage). 41 | * Bug Fix:Fixed AND constrains. 42 | 43 | Version 1.0.2 *(27-04-2016)* 44 | ------------------ 45 | * Fetched Data: 46 | * Added eventStartDate, eventType and event Label 47 | * Implemented Query Constraints: 48 | * whereExists (This constraint is implemented in code instead of sql. See function javadoc for more info.) 49 | 50 | Version 1.0.1 *(24-04-2016)* 51 | ------------------ 52 | * Fetched Data: 53 | * Phone NormalizedNumber, Type and Label are now distinct fields. (Were aggregated in previous version) 54 | * Email Address, Type and Label are now distinct fields (Were aggregated in previous version) 55 | * Implemented Query Constraints: 56 | * startsWith 57 | * equalTo 58 | * Added findFirst query function. 59 | * Implemented Comparator functionality. Allows you to provide a comparator to choose between values of same type in same contact. 60 | 61 | Version 1.0.0 *(21-04-2016)* 62 | ------------------ 63 | * Fetched Data: 64 | * DisplayName 65 | * Phone NormalizedNumber and Type (and Label) 66 | * Email Address and Type (and Label) 67 | * PhotoURI 68 | * Implemented Query Constraints: 69 | * whereContains 70 | * hasPhoneNumber 71 | * include 72 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Contacts 2 | 3 | Android Contacts API. 4 | 5 | ## Quick Start 6 | 7 | Initialize Contacts Library 8 | 9 | ```java 10 | public class MyApplication extends Application { 11 | 12 | @Override 13 | public void onCreate() { 14 | super.onCreate(); 15 | Contacts.initialize(this); 16 | } 17 | ``` 18 | Get All Contacts 19 | 20 | ```java 21 | List contacts = Contacts.getQuery().find(); 22 | ``` 23 | 24 | Get Contacts with phone numbers only 25 | 26 | ```java 27 | Query q = Contacts.getQuery(); 28 | q.hasPhoneNumber(); 29 | List contacts = q.find(); 30 | ``` 31 | 32 | Get Specific fields 33 | 34 | ```java 35 | Query q = Contacts.getQuery(); 36 | q.include(Contact.Field.DisplayName, Contact.Field.Email, Contact.Field.PhotoUri); 37 | List contacts = q.find(); 38 | ``` 39 | 40 | Search By Display Name 41 | 42 | ```java 43 | Query q = Contacts.getQuery(); 44 | q.whereContains(Contact.Field.DisplayName, "some string"); 45 | List contacts = q.find(); 46 | ``` 47 | 48 | Find all numbers with a specific E164 code 49 | 50 | ```java 51 | Query q = Contacts.getQuery(); 52 | q.whereStartsWith(Contact.Field.PhoneNormalizedNumber, "+972"); 53 | List contacts = q.find(); 54 | ``` 55 | 56 | Find a Contact by phone Number 57 | 58 | ```java 59 | Query q = Contacts.getQuery(); 60 | q.whereEqualTo(Contact.Field.PhoneNumber, "Some phone Number"); 61 | List contacts = q.find(); 62 | ``` 63 | 64 | Get all Contacts that their name begins with a specific string OR their phone begings with a specific prefix. 65 | ```java 66 | Query mainQuery = Contacts.getQuery(); 67 | Query q1 = Contacts.getQuery(); 68 | q1.whereStartsWith(Contact.Field.DisplayName, "Some String"); 69 | Query q2 = Contacts.getQuery(); 70 | q2.whereStartsWith(Contact.Field.PhoneNormalizedNumber, "+972"); 71 | List qs = new ArrayList<>(); 72 | qs.add(q1); 73 | qs.add(q2); 74 | mainQuery.or(qs); 75 | List contacts = mainQuery.find(); 76 | 77 | ``` 78 | 79 | ## Installation 80 | 81 | Published to JCenter 82 | 83 | ```java 84 | 85 | compile 'com.github.tamir7.contacts:contacts:1.1.7' 86 | ``` 87 | 88 | ## License 89 | 90 | Copyright 2016 Tamir Shomer 91 | 92 | Licensed under the Apache License, Version 2.0 (the "License"); 93 | you may not use this file except in compliance with the License. 94 | You may obtain a copy of the License at 95 | 96 | http://www.apache.org/licenses/LICENSE-2.0 97 | 98 | Unless required by applicable law or agreed to in writing, software 99 | distributed under the License is distributed on an "AS IS" BASIS, 100 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 101 | See the License for the specific language governing permissions and 102 | limitations under the License. 103 | -------------------------------------------------------------------------------- /build.gradle: -------------------------------------------------------------------------------- 1 | 2 | buildscript { 3 | repositories { 4 | jcenter() 5 | } 6 | dependencies { 7 | classpath 'com.android.tools.build:gradle:2.3.2' 8 | } 9 | } 10 | 11 | allprojects { 12 | repositories { 13 | jcenter() 14 | } 15 | } 16 | 17 | task clean(type: Delete) { 18 | delete rootProject.buildDir 19 | } 20 | -------------------------------------------------------------------------------- /contacts/.gitignore: -------------------------------------------------------------------------------- 1 | /build 2 | -------------------------------------------------------------------------------- /contacts/build.gradle: -------------------------------------------------------------------------------- 1 | buildscript { 2 | repositories { 3 | jcenter() 4 | } 5 | 6 | dependencies { 7 | } 8 | } 9 | 10 | plugins { 11 | id "com.jfrog.bintray" version "1.7" 12 | id "com.github.dcendents.android-maven" version "1.5" 13 | } 14 | 15 | apply plugin: 'com.android.library' 16 | 17 | version = VERSION_NAME 18 | group = GROUP 19 | 20 | android { 21 | compileSdkVersion 25 22 | buildToolsVersion "25.0.3" 23 | 24 | defaultConfig { 25 | minSdkVersion 11 26 | targetSdkVersion 25 27 | versionCode 1 28 | versionName VERSION_NAME 29 | } 30 | buildTypes { 31 | release { 32 | minifyEnabled false 33 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' 34 | } 35 | } 36 | } 37 | 38 | install { 39 | repositories.mavenInstaller { 40 | pom.project { 41 | packaging 'aar' 42 | name NAME 43 | description DESCRIPTION 44 | url SITE_URL 45 | 46 | licenses { 47 | license { 48 | name LICENCE_NAME 49 | url LICENCE_URL 50 | } 51 | } 52 | developers { 53 | developer { 54 | id DEVELOPER_ID 55 | name DEVELOPER_NAME 56 | email DEVELOPER_EMAIL 57 | } 58 | } 59 | scm { 60 | connection GIT_URL 61 | developerConnection GIT_URL 62 | url SITE_URL 63 | } 64 | } 65 | } 66 | } 67 | 68 | task sourcesJar(type: Jar) { 69 | from android.sourceSets.main.java.srcDirs 70 | classifier = 'sources' 71 | } 72 | 73 | task javadoc(type: Javadoc) { 74 | source = android.sourceSets.main.java.srcDirs 75 | classpath += project.files(android.getBootClasspath().join(File.pathSeparator)) 76 | } 77 | 78 | task javadocJar(type: Jar, dependsOn: javadoc) { 79 | classifier = 'javadoc' 80 | from javadoc.destinationDir 81 | } 82 | artifacts { 83 | archives javadocJar 84 | archives sourcesJar 85 | } 86 | 87 | bintray { 88 | user = BINTRAY_USER 89 | key = BINTRAY_API_KEY 90 | 91 | configurations = ['archives'] 92 | pkg { 93 | repo = 'maven' 94 | name = NAME 95 | desc = DESCRIPTION 96 | websiteUrl = SITE_URL 97 | vcsUrl = GIT_URL 98 | issueTrackerUrl = ISSUE_TRACKER_URL 99 | licenses = ['Apache-2.0'] 100 | labels = ['Android', 'Contacts'] 101 | publicDownloadNumbers = true 102 | publish = true 103 | } 104 | } 105 | 106 | dependencies { 107 | } 108 | -------------------------------------------------------------------------------- /contacts/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | -------------------------------------------------------------------------------- /contacts/src/main/java/com/github/tamir7/contacts/Address.java: -------------------------------------------------------------------------------- 1 | package com.github.tamir7.contacts; 2 | 3 | import android.provider.ContactsContract; 4 | 5 | /** 6 | * Represents an Address 7 | */ 8 | public class Address { 9 | private final String formattedAddress; 10 | private final Type type; 11 | private final String street; 12 | private final String city; 13 | private final String region; 14 | private final String postcode; 15 | private final String country; 16 | private final String label; 17 | 18 | 19 | public enum Type { 20 | CUSTOM, 21 | HOME, 22 | WORK, 23 | OTHER, 24 | UNKNOWN; 25 | 26 | static Type fromValue(int value) { 27 | switch (value) { 28 | case ContactsContract.CommonDataKinds.StructuredPostal.TYPE_CUSTOM: 29 | return CUSTOM; 30 | case ContactsContract.CommonDataKinds.StructuredPostal.TYPE_HOME: 31 | return HOME; 32 | case ContactsContract.CommonDataKinds.StructuredPostal.TYPE_WORK: 33 | return WORK; 34 | case ContactsContract.CommonDataKinds.StructuredPostal.TYPE_OTHER: 35 | return OTHER; 36 | default: 37 | return UNKNOWN; 38 | } 39 | } 40 | } 41 | 42 | private Address(String address, 43 | String street, 44 | String city, 45 | String region, 46 | String postcode, 47 | String country, 48 | Type type, 49 | String label) { 50 | this.formattedAddress = address; 51 | this.street = street; 52 | this.city = city; 53 | this.region = region; 54 | this.postcode = postcode; 55 | this.country = country; 56 | this.type = type; 57 | this.label = label; 58 | } 59 | 60 | Address(String address, 61 | String street, 62 | String city, 63 | String region, 64 | String postcode, 65 | String country, 66 | Type type) { 67 | this(address, street, city, region, postcode, country, type, null); 68 | } 69 | 70 | Address(String address, 71 | String street, 72 | String city, 73 | String region, 74 | String postcode, 75 | String country, 76 | String label) { 77 | this(address, street, city, region, postcode, country, Type.CUSTOM, label); 78 | } 79 | 80 | /** 81 | * Gets the formatted address 82 | * 83 | * @return Formatted address 84 | */ 85 | public String getFormattedAddress() { 86 | return formattedAddress; 87 | } 88 | 89 | /** 90 | * Gets the type of the address 91 | * 92 | * @return type of address 93 | */ 94 | public Type getType() { 95 | return type; 96 | } 97 | 98 | /** 99 | * Gets the street name 100 | * 101 | * @return street name 102 | */ 103 | public String getStreet() { 104 | return street; 105 | } 106 | 107 | /** 108 | * Gets the city name 109 | * 110 | * @return city name 111 | */ 112 | public String getCity() { 113 | return city; 114 | } 115 | 116 | /** 117 | * Gets the region name 118 | * 119 | * @return region name 120 | */ 121 | public String getRegion() { 122 | return region; 123 | } 124 | 125 | /** 126 | * Gets the post code 127 | * 128 | * @return post code 129 | */ 130 | public String getPostcode() { 131 | return postcode; 132 | } 133 | 134 | /** 135 | * Gets the country of the address 136 | * 137 | * @return country of the address 138 | */ 139 | public String getCountry() { 140 | return country; 141 | } 142 | 143 | /** 144 | * The label of the address. (null unless type = TYPE_CUSTOM) 145 | * 146 | * @return the label of the address 147 | */ 148 | public String getLabel() { 149 | return label; 150 | } 151 | 152 | } 153 | -------------------------------------------------------------------------------- /contacts/src/main/java/com/github/tamir7/contacts/Contact.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016 Tamir Shomer 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package com.github.tamir7.contacts; 18 | 19 | import android.annotation.SuppressLint; 20 | import android.provider.ContactsContract; 21 | 22 | import java.util.Arrays; 23 | import java.util.HashSet; 24 | import java.util.List; 25 | import java.util.Set; 26 | 27 | /** 28 | * Represents a compound contact. aggregating all phones, email and photo's a contact has. 29 | */ 30 | public final class Contact { 31 | private Long id; 32 | private String displayName; 33 | private String givenName; 34 | private String familyName; 35 | 36 | private final Set phoneNumbers = new HashSet<>(); 37 | private String photoUri; 38 | private final Set emails = new HashSet<>(); 39 | private final Set events = new HashSet<>(); 40 | private String companyName; 41 | private String companyTitle; 42 | private final Set websites = new HashSet<>(); 43 | private final Set
addresses = new HashSet<>(); 44 | private String note; 45 | 46 | interface AbstractField { 47 | String getMimeType(); 48 | 49 | String getColumn(); 50 | } 51 | 52 | public enum Field implements AbstractField { 53 | ContactId(null, ContactsContract.RawContacts.CONTACT_ID), 54 | DisplayName(null, ContactsContract.Data.DISPLAY_NAME), 55 | GivenName(ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE, 56 | ContactsContract.CommonDataKinds.StructuredName.GIVEN_NAME), 57 | FamilyName(ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE, 58 | ContactsContract.CommonDataKinds.StructuredName.FAMILY_NAME), 59 | PhoneNumber(ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE, 60 | ContactsContract.CommonDataKinds.Phone.NUMBER), 61 | PhoneType(ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE, 62 | ContactsContract.CommonDataKinds.Phone.TYPE), 63 | PhoneLabel(ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE, 64 | ContactsContract.CommonDataKinds.Phone.LABEL), 65 | @SuppressLint("InlinedApi") 66 | PhoneNormalizedNumber(ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE, 67 | ContactsContract.CommonDataKinds.Phone.NORMALIZED_NUMBER), 68 | Email(ContactsContract.CommonDataKinds.Email.CONTENT_ITEM_TYPE, 69 | ContactsContract.CommonDataKinds.Email.ADDRESS), 70 | EmailType(ContactsContract.CommonDataKinds.Email.CONTENT_ITEM_TYPE, 71 | ContactsContract.CommonDataKinds.Email.TYPE), 72 | EmailLabel(ContactsContract.CommonDataKinds.Email.CONTENT_ITEM_TYPE, 73 | ContactsContract.CommonDataKinds.Email.LABEL), 74 | PhotoUri(null, ContactsContract.Data.PHOTO_URI), 75 | EventStartDate(ContactsContract.CommonDataKinds.Event.CONTENT_ITEM_TYPE, 76 | ContactsContract.CommonDataKinds.Event.START_DATE), 77 | EventType(ContactsContract.CommonDataKinds.Event.CONTENT_ITEM_TYPE, 78 | ContactsContract.CommonDataKinds.Event.TYPE), 79 | EventLabel(ContactsContract.CommonDataKinds.Event.CONTENT_ITEM_TYPE, 80 | ContactsContract.CommonDataKinds.Event.LABEL), 81 | CompanyName(ContactsContract.CommonDataKinds.Organization.CONTENT_ITEM_TYPE, 82 | ContactsContract.CommonDataKinds.Organization.COMPANY), 83 | CompanyTitle(ContactsContract.CommonDataKinds.Organization.CONTENT_ITEM_TYPE, 84 | ContactsContract.CommonDataKinds.Organization.TITLE), 85 | Website(ContactsContract.CommonDataKinds.Website.CONTENT_ITEM_TYPE, 86 | ContactsContract.CommonDataKinds.Website.URL), 87 | Note(ContactsContract.CommonDataKinds.Note.CONTENT_ITEM_TYPE, 88 | ContactsContract.CommonDataKinds.Note.NOTE), 89 | Address(ContactsContract.CommonDataKinds.StructuredPostal.CONTENT_ITEM_TYPE, 90 | ContactsContract.CommonDataKinds.StructuredPostal.FORMATTED_ADDRESS), 91 | AddressType(ContactsContract.CommonDataKinds.StructuredPostal.CONTENT_ITEM_TYPE, 92 | ContactsContract.CommonDataKinds.StructuredPostal.TYPE), 93 | AddressStreet(ContactsContract.CommonDataKinds.StructuredPostal.CONTENT_ITEM_TYPE, 94 | ContactsContract.CommonDataKinds.StructuredPostal.STREET), 95 | AddressCity(ContactsContract.CommonDataKinds.StructuredPostal.CONTENT_ITEM_TYPE, 96 | ContactsContract.CommonDataKinds.StructuredPostal.CITY), 97 | AddressRegion(ContactsContract.CommonDataKinds.StructuredPostal.CONTENT_ITEM_TYPE, 98 | ContactsContract.CommonDataKinds.StructuredPostal.REGION), 99 | AddressPostcode(ContactsContract.CommonDataKinds.StructuredPostal.CONTENT_ITEM_TYPE, 100 | ContactsContract.CommonDataKinds.StructuredPostal.POSTCODE), 101 | AddressCountry(ContactsContract.CommonDataKinds.StructuredPostal.CONTENT_ITEM_TYPE, 102 | ContactsContract.CommonDataKinds.StructuredPostal.COUNTRY), 103 | AddressLabel(ContactsContract.CommonDataKinds.StructuredPostal.CONTENT_ITEM_TYPE, 104 | ContactsContract.CommonDataKinds.StructuredPostal.LABEL); 105 | 106 | private final String column; 107 | private final String mimeType; 108 | 109 | Field(String mimeType, String column) { 110 | this.mimeType = mimeType; 111 | this.column = column; 112 | } 113 | 114 | @Override 115 | public String getColumn() { 116 | return column; 117 | } 118 | 119 | @Override 120 | public String getMimeType() { 121 | return mimeType; 122 | } 123 | } 124 | 125 | enum InternalField implements AbstractField { 126 | MimeType(null, ContactsContract.Data.MIMETYPE); 127 | 128 | private final String column; 129 | private final String mimeType; 130 | 131 | InternalField(String mimeType, String column) { 132 | this.mimeType = mimeType; 133 | this.column = column; 134 | } 135 | 136 | @Override 137 | public String getColumn() { 138 | return column; 139 | } 140 | 141 | @Override 142 | public String getMimeType() { 143 | return mimeType; 144 | } 145 | } 146 | 147 | Contact() {} 148 | 149 | void setId(Long id) { 150 | this.id = id; 151 | } 152 | 153 | Contact addDisplayName(String displayName) { 154 | this.displayName = displayName; 155 | return this; 156 | } 157 | 158 | Contact addGivenName(String givenName) { 159 | this.givenName = givenName; 160 | return this; 161 | } 162 | 163 | Contact addFamilyName(String familyName) { 164 | this.familyName = familyName; 165 | return this; 166 | } 167 | 168 | Contact addPhoneNumber(PhoneNumber phoneNumber) { 169 | phoneNumbers.add(phoneNumber); 170 | return this; 171 | } 172 | 173 | Contact addPhotoUri(String photoUri) { 174 | this.photoUri = photoUri; 175 | return this; 176 | } 177 | 178 | Contact addEmail(Email email) { 179 | emails.add(email); 180 | return this; 181 | } 182 | 183 | Contact addEvent(Event event) { 184 | events.add(event); 185 | return this; 186 | } 187 | 188 | Contact addCompanyName(String companyName) { 189 | this.companyName = companyName; 190 | return this; 191 | } 192 | 193 | Contact addCompanyTitle(String companyTitle) { 194 | this.companyTitle = companyTitle; 195 | return this; 196 | } 197 | 198 | Contact addWebsite(String website) { 199 | websites.add(website); 200 | return this; 201 | } 202 | 203 | Contact addNote(String note) { 204 | this.note = note; 205 | return this; 206 | } 207 | 208 | Contact addAddress(Address address) { 209 | addresses.add(address); 210 | return this; 211 | } 212 | 213 | /** 214 | * Gets a the phone contact id. 215 | * 216 | * @return contact id. 217 | */ 218 | public Long getId() { 219 | return id; 220 | } 221 | 222 | /** 223 | * Gets a the display name the contact. 224 | * 225 | * @return Display Name. 226 | */ 227 | public String getDisplayName() { 228 | return displayName; 229 | } 230 | 231 | /** 232 | * Gets a the given name the contact. 233 | * 234 | * @return Given Name. 235 | */ 236 | public String getGivenName() { 237 | return givenName; 238 | } 239 | 240 | /** 241 | * Gets a the Family name the contact. 242 | * 243 | * @return Family Name. 244 | */ 245 | public String getFamilyName() { 246 | return familyName; 247 | } 248 | 249 | /** 250 | * Gets a list of all phone numbers the contact has. 251 | * 252 | * @return A List of phone numbers. 253 | */ 254 | public List getPhoneNumbers() { 255 | return Arrays.asList(phoneNumbers.toArray(new PhoneNumber[phoneNumbers.size()])); 256 | } 257 | 258 | /** 259 | * Gets a contacts photo uri. 260 | * 261 | * @return Photo URI. 262 | */ 263 | public String getPhotoUri() { 264 | return photoUri; 265 | } 266 | 267 | /** 268 | * Gets a list of all emails the contact has. 269 | * 270 | * @return A List of emails. 271 | */ 272 | public List getEmails() { 273 | return Arrays.asList(emails.toArray(new Email[emails.size()])); 274 | } 275 | 276 | /** 277 | * Gets a list of all events the contact has. 278 | * 279 | * @return A List of emails. 280 | */ 281 | public List getEvents() { 282 | return Arrays.asList(events.toArray(new Event[events.size()])); 283 | } 284 | 285 | /** 286 | * Gets the birthday event if exists. 287 | * 288 | * @return Birthday event or null. 289 | */ 290 | public Event getBirthday() { 291 | return getEvent(Event.Type.BIRTHDAY); 292 | } 293 | 294 | /** 295 | * Gets the anniversary event if exists. 296 | * 297 | * @return Anniversary event or null. 298 | */ 299 | public Event getAnniversary() { 300 | return getEvent(Event.Type.ANNIVERSARY); 301 | 302 | } 303 | 304 | /** 305 | * Gets the name of the company the contact works on 306 | * 307 | * @return the company name 308 | */ 309 | public String getCompanyName() { 310 | return companyName; 311 | } 312 | 313 | /** 314 | * Gets the job title of the contact 315 | * 316 | * @return the job title 317 | */ 318 | public String getCompanyTitle() { 319 | return companyTitle; 320 | } 321 | 322 | /** 323 | * Gets the list of all websites the contact has 324 | * 325 | * @return A list of websites 326 | */ 327 | public List getWebsites() { 328 | return Arrays.asList(websites.toArray(new String[websites.size()])); 329 | } 330 | 331 | /** 332 | * Gets the note of the contact 333 | * 334 | * @return the note 335 | */ 336 | public String getNote() { 337 | return note; 338 | } 339 | 340 | /** 341 | * Gets the list of addresses 342 | * 343 | * @return A list of addresses 344 | */ 345 | public List
getAddresses() { 346 | return Arrays.asList(addresses.toArray(new Address[addresses.size()])); 347 | } 348 | 349 | private Event getEvent(Event.Type type) { 350 | for (Event event: events) { 351 | if (type.equals(event.getType())) { 352 | return event; 353 | } 354 | } 355 | 356 | return null; 357 | } 358 | } 359 | -------------------------------------------------------------------------------- /contacts/src/main/java/com/github/tamir7/contacts/Contacts.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016 Tamir Shomer 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package com.github.tamir7.contacts; 18 | 19 | import android.content.Context; 20 | 21 | public final class Contacts { 22 | private static Context context; 23 | 24 | private Contacts() {} 25 | 26 | /** 27 | * Initialize the Contacts library 28 | * 29 | * @param context context 30 | */ 31 | public static void initialize(Context context) { 32 | Contacts.context = context.getApplicationContext(); 33 | } 34 | 35 | /** 36 | * Get a new Query object to find contacts. 37 | * 38 | * @return A new Query object. 39 | */ 40 | public static Query getQuery() { 41 | if (Contacts.context == null) { 42 | throw new IllegalStateException("Contacts library not initialized"); 43 | } 44 | 45 | return new Query(context); 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /contacts/src/main/java/com/github/tamir7/contacts/CursorHelper.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016 Tamir Shomer 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package com.github.tamir7.contacts; 18 | 19 | import android.database.Cursor; 20 | import android.provider.ContactsContract; 21 | 22 | class CursorHelper { 23 | private final Cursor c; 24 | 25 | CursorHelper(Cursor c) { 26 | this.c = c; 27 | } 28 | 29 | Long getContactId() { 30 | return getLong(c, ContactsContract.RawContacts.CONTACT_ID); 31 | } 32 | 33 | String getMimeType() { 34 | return getString(c, ContactsContract.Data.MIMETYPE); 35 | } 36 | 37 | String getDisplayName() { 38 | return getString(c, ContactsContract.Data.DISPLAY_NAME); 39 | } 40 | 41 | String getGivenName() { 42 | return getString(c, ContactsContract.CommonDataKinds.StructuredName.GIVEN_NAME); 43 | } 44 | 45 | String getFamilyName() { 46 | return getString(c, ContactsContract.CommonDataKinds.StructuredName.FAMILY_NAME); 47 | } 48 | 49 | String getCompanyName() { 50 | return getString(c, ContactsContract.CommonDataKinds.Organization.COMPANY); 51 | } 52 | 53 | String getCompanyTitle() { 54 | return getString(c, ContactsContract.CommonDataKinds.Organization.TITLE); 55 | } 56 | 57 | String getWebsite() { 58 | return getString(c, ContactsContract.CommonDataKinds.Website.URL); 59 | } 60 | 61 | String getNote() { 62 | return getString(c, ContactsContract.CommonDataKinds.Note.NOTE); 63 | } 64 | 65 | Address getAddress() { 66 | String address = getString(c, ContactsContract.CommonDataKinds.StructuredPostal.FORMATTED_ADDRESS); 67 | if (address == null) { 68 | return null; 69 | } 70 | 71 | Integer typeValue = getInt(c, ContactsContract.CommonDataKinds.StructuredPostal.TYPE); 72 | Address.Type type = typeValue == null ? Address.Type.UNKNOWN : Address.Type.fromValue(typeValue); 73 | 74 | String street = getString(c, ContactsContract.CommonDataKinds.StructuredPostal.STREET); 75 | String city = getString(c, ContactsContract.CommonDataKinds.StructuredPostal.CITY); 76 | String region = getString(c, ContactsContract.CommonDataKinds.StructuredPostal.REGION); 77 | String postcode = getString(c, ContactsContract.CommonDataKinds.StructuredPostal.POSTCODE); 78 | String country = getString(c, ContactsContract.CommonDataKinds.StructuredPostal.COUNTRY); 79 | 80 | if (!type.equals(Address.Type.CUSTOM)) { 81 | return new Address(address, street, city, region, postcode, country, type); 82 | } 83 | 84 | String label = getString(c, ContactsContract.CommonDataKinds.StructuredPostal.LABEL); 85 | return new Address(address, street, city, region, postcode, country, label); 86 | } 87 | 88 | PhoneNumber getPhoneNumber() { 89 | String number = getString(c, ContactsContract.CommonDataKinds.Phone.NUMBER); 90 | if (number == null) { 91 | return null; 92 | } 93 | 94 | String normalizedNumber = null; 95 | if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.JELLY_BEAN) { 96 | normalizedNumber = getString(c, ContactsContract.CommonDataKinds.Phone.NORMALIZED_NUMBER); 97 | } 98 | 99 | Integer typeValue = getInt(c, ContactsContract.CommonDataKinds.Phone.TYPE); 100 | PhoneNumber.Type type = typeValue == null ? PhoneNumber.Type.UNKNOWN : 101 | PhoneNumber.Type.fromValue(typeValue); 102 | if (!type.equals(PhoneNumber.Type.CUSTOM)) { 103 | return new PhoneNumber(number, type, normalizedNumber); 104 | } 105 | 106 | return new PhoneNumber(number, 107 | getString(c, ContactsContract.CommonDataKinds.Phone.LABEL), normalizedNumber); 108 | } 109 | 110 | Email getEmail() { 111 | String address = getString(c, ContactsContract.CommonDataKinds.Email.ADDRESS); 112 | if (address == null) { 113 | return null; 114 | } 115 | 116 | Integer typeValue = getInt(c, ContactsContract.CommonDataKinds.Email.TYPE); 117 | Email.Type type = typeValue == null ? Email.Type.UNKNOWN : Email.Type.fromValue(typeValue); 118 | if (!type.equals(Email.Type.CUSTOM)) { 119 | return new Email(address, type); 120 | } 121 | 122 | return new Email(address, getString(c, ContactsContract.CommonDataKinds.Email.LABEL)); 123 | } 124 | 125 | String getPhotoUri() { 126 | return getString(c, ContactsContract.Data.PHOTO_URI); 127 | } 128 | 129 | 130 | Event getEvent() { 131 | String startDate = getString(c, ContactsContract.CommonDataKinds.Event.START_DATE); 132 | if (startDate == null) { 133 | return null; 134 | } 135 | 136 | Integer typeValue = getInt(c, ContactsContract.CommonDataKinds.Event.TYPE); 137 | Event.Type type = typeValue == null ? Event.Type.UNKNOWN : Event.Type.fromValue(typeValue); 138 | if (!type.equals(Event.Type.CUSTOM)) { 139 | return new Event(startDate, type); 140 | } 141 | 142 | return new Event(startDate, getString(c, ContactsContract.CommonDataKinds.Event.LABEL)); 143 | } 144 | 145 | private String getString(Cursor c, String column) { 146 | int index = c.getColumnIndex(column); 147 | return index == -1 ? null : c.getString(index); 148 | } 149 | 150 | private Integer getInt(Cursor c, String column) { 151 | int index = c.getColumnIndex(column); 152 | return index == -1 ? null : c.getInt(index); 153 | } 154 | 155 | private Long getLong(Cursor c, String column) { 156 | int index = c.getColumnIndex(column); 157 | return index == -1 ? null : c.getLong(index); 158 | } 159 | } 160 | -------------------------------------------------------------------------------- /contacts/src/main/java/com/github/tamir7/contacts/Email.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016 Tamir Shomer 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package com.github.tamir7.contacts; 18 | 19 | import android.provider.ContactsContract; 20 | 21 | /** 22 | * Represents an Email. 23 | * 24 | */ 25 | public class Email { 26 | private final String address; 27 | private final Type type; 28 | private final String label; 29 | 30 | public enum Type { 31 | CUSTOM, 32 | HOME, 33 | WORK, 34 | OTHER, 35 | MOBILE, 36 | UNKNOWN; 37 | 38 | static Type fromValue(int value) { 39 | switch (value) { 40 | case ContactsContract.CommonDataKinds.Email.TYPE_CUSTOM: 41 | return CUSTOM; 42 | case ContactsContract.CommonDataKinds.Email.TYPE_HOME: 43 | return HOME; 44 | case ContactsContract.CommonDataKinds.Email.TYPE_WORK: 45 | return WORK; 46 | case ContactsContract.CommonDataKinds.Email.TYPE_OTHER: 47 | return OTHER; 48 | case ContactsContract.CommonDataKinds.Email.TYPE_MOBILE: 49 | return MOBILE; 50 | default: 51 | return UNKNOWN; 52 | } 53 | } 54 | } 55 | 56 | Email(String address, Type type) { 57 | this.address = address; 58 | this.type = type; 59 | this.label = null; 60 | } 61 | 62 | Email(String address, String label) { 63 | this.address = address; 64 | this.type = Type.CUSTOM; 65 | this.label = label; 66 | } 67 | 68 | /** 69 | * Gets the email address. 70 | * 71 | * @return address. 72 | */ 73 | public String getAddress() { 74 | return address; 75 | } 76 | 77 | /** 78 | * Gets the type of email. 79 | * 80 | * @return type of email. 81 | */ 82 | public Type getType() { 83 | return type; 84 | } 85 | 86 | /** 87 | * Gets the label. (null unless type = TYPE_CUSTOM) 88 | * 89 | * @return label. 90 | */ 91 | public String getLabel() { 92 | return label; 93 | } 94 | 95 | @Override 96 | public boolean equals(Object o) { 97 | if (this == o) return true; 98 | if (o == null || getClass() != o.getClass()) return false; 99 | 100 | Email email = (Email) o; 101 | 102 | return address.equals(email.address) && type == email.type && 103 | !(label != null ? !label.equals(email.label) : email.label != null); 104 | } 105 | 106 | @Override 107 | public int hashCode() { 108 | int result = address.hashCode(); 109 | result = 31 * result + type.hashCode(); 110 | result = 31 * result + (label != null ? label.hashCode() : 0); 111 | return result; 112 | } 113 | } 114 | -------------------------------------------------------------------------------- /contacts/src/main/java/com/github/tamir7/contacts/Event.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016 Tamir Shomer 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package com.github.tamir7.contacts; 18 | 19 | import android.provider.ContactsContract; 20 | 21 | /** 22 | * Represents an Event. 23 | * 24 | */ 25 | public class Event { 26 | private final String startDate; 27 | private final Type type; 28 | private final String label; 29 | 30 | public enum Type { 31 | CUSTOM, 32 | ANNIVERSARY, 33 | OTHER, 34 | BIRTHDAY, 35 | UNKNOWN; 36 | 37 | static Type fromValue(int value) { 38 | switch (value) { 39 | case ContactsContract.CommonDataKinds.Event.TYPE_CUSTOM: 40 | return CUSTOM; 41 | case ContactsContract.CommonDataKinds.Event.TYPE_ANNIVERSARY: 42 | return ANNIVERSARY; 43 | case ContactsContract.CommonDataKinds.Event.TYPE_OTHER: 44 | return OTHER; 45 | case ContactsContract.CommonDataKinds.Event.TYPE_BIRTHDAY: 46 | return BIRTHDAY; 47 | default: 48 | return UNKNOWN; 49 | } 50 | } 51 | } 52 | 53 | Event(String startDate, Type type) { 54 | this.startDate = startDate; 55 | this.type = type; 56 | this.label = null; 57 | } 58 | 59 | Event(String startDate, String label) { 60 | this.startDate = startDate; 61 | this.type = Type.CUSTOM; 62 | this.label = label; 63 | } 64 | 65 | /** 66 | * Gets the event start date as a string. 67 | * 68 | * @return start date. 69 | */ 70 | public String getStartDate() { 71 | return startDate; 72 | } 73 | 74 | /** 75 | * Gets the type of event. 76 | * 77 | * @return type of event. 78 | */ 79 | public Type getType() { 80 | return type; 81 | } 82 | 83 | /** 84 | * Gets the label. (null unless type = TYPE_CUSTOM) 85 | * 86 | * @return label. 87 | */ 88 | public String getLabel() { 89 | return label; 90 | } 91 | 92 | @Override 93 | public boolean equals(Object o) { 94 | if (this == o) return true; 95 | if (o == null || getClass() != o.getClass()) return false; 96 | 97 | Event email = (Event) o; 98 | 99 | return startDate.equals(email.startDate) && type == email.type && 100 | !(label != null ? !label.equals(email.label) : email.label != null); 101 | } 102 | 103 | @Override 104 | public int hashCode() { 105 | int result = startDate.hashCode(); 106 | result = 31 * result + type.hashCode(); 107 | result = 31 * result + (label != null ? label.hashCode() : 0); 108 | return result; 109 | } 110 | } 111 | -------------------------------------------------------------------------------- /contacts/src/main/java/com/github/tamir7/contacts/PhoneNumber.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016 Tamir Shomer 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package com.github.tamir7.contacts; 18 | 19 | import android.provider.ContactsContract; 20 | 21 | /** 22 | * Represents a phone number 23 | */ 24 | public class PhoneNumber { 25 | private final String number; 26 | private final Type type; 27 | private final String label; 28 | private final String normalizedNumber; 29 | 30 | public enum Type { 31 | CUSTOM, 32 | HOME, 33 | MOBILE, 34 | WORK, 35 | FAX_WORK, 36 | FAX_HOME, 37 | PAGER, 38 | OTHER, 39 | CALLBACK, 40 | CAR, 41 | COMPANY_MAIN, 42 | ISDN, 43 | MAIN, 44 | OTHER_FAX, 45 | RADIO, 46 | TELEX, 47 | TTY_TDD, 48 | WORK_MOBILE, 49 | WORK_PAGER, 50 | ASSISTANT, 51 | MMS, 52 | UNKNOWN; 53 | 54 | static Type fromValue(int value) { 55 | switch (value) { 56 | case ContactsContract.CommonDataKinds.Phone.TYPE_CUSTOM: 57 | return CUSTOM; 58 | case ContactsContract.CommonDataKinds.Phone.TYPE_HOME: 59 | return HOME; 60 | case ContactsContract.CommonDataKinds.Phone.TYPE_MOBILE: 61 | return MOBILE; 62 | case ContactsContract.CommonDataKinds.Phone.TYPE_WORK: 63 | return WORK; 64 | case ContactsContract.CommonDataKinds.Phone.TYPE_FAX_WORK: 65 | return FAX_WORK; 66 | case ContactsContract.CommonDataKinds.Phone.TYPE_FAX_HOME: 67 | return FAX_HOME; 68 | case ContactsContract.CommonDataKinds.Phone.TYPE_PAGER: 69 | return PAGER; 70 | case ContactsContract.CommonDataKinds.Phone.TYPE_OTHER: 71 | return OTHER; 72 | case ContactsContract.CommonDataKinds.Phone.TYPE_CALLBACK: 73 | return CALLBACK; 74 | case ContactsContract.CommonDataKinds.Phone.TYPE_CAR: 75 | return CAR; 76 | case ContactsContract.CommonDataKinds.Phone.TYPE_COMPANY_MAIN: 77 | return COMPANY_MAIN; 78 | case ContactsContract.CommonDataKinds.Phone.TYPE_ISDN: 79 | return ISDN; 80 | case ContactsContract.CommonDataKinds.Phone.TYPE_MAIN: 81 | return MAIN; 82 | case ContactsContract.CommonDataKinds.Phone.TYPE_OTHER_FAX: 83 | return OTHER_FAX; 84 | case ContactsContract.CommonDataKinds.Phone.TYPE_RADIO: 85 | return RADIO; 86 | case ContactsContract.CommonDataKinds.Phone.TYPE_TELEX: 87 | return TELEX; 88 | case ContactsContract.CommonDataKinds.Phone.TYPE_TTY_TDD: 89 | return TTY_TDD; 90 | case ContactsContract.CommonDataKinds.Phone.TYPE_WORK_MOBILE: 91 | return WORK_MOBILE; 92 | case ContactsContract.CommonDataKinds.Phone.TYPE_WORK_PAGER: 93 | return WORK_PAGER; 94 | case ContactsContract.CommonDataKinds.Phone.TYPE_ASSISTANT: 95 | return ASSISTANT; 96 | case ContactsContract.CommonDataKinds.Phone.TYPE_MMS: 97 | return MMS; 98 | default: 99 | return UNKNOWN; 100 | } 101 | } 102 | } 103 | 104 | PhoneNumber(String number, Type type, String normalizedNumber) { 105 | this.number = number; 106 | this.type = type; 107 | this.label = null; 108 | this.normalizedNumber = normalizedNumber; 109 | } 110 | 111 | PhoneNumber(String number, String label, String normalizedNumber) { 112 | this.number = number; 113 | this.type = Type.CUSTOM; 114 | this.label = label; 115 | this.normalizedNumber = normalizedNumber; 116 | } 117 | 118 | /** 119 | * Gets the phone number. 120 | * 121 | * @return phone number. 122 | */ 123 | public String getNumber() { 124 | return number; 125 | } 126 | 127 | /** 128 | * Gets the label of the phone number. (null unless type = CUSTOM) 129 | * 130 | * @return label. 131 | */ 132 | public String getLabel() { 133 | return label; 134 | } 135 | 136 | /** 137 | * Gets the normalized phone number. 138 | * 139 | * @return normalized phone number. 140 | */ 141 | public String getNormalizedNumber() { 142 | return normalizedNumber; 143 | } 144 | 145 | /** 146 | * Gets the type of the phone number. 147 | * 148 | * @return type. 149 | */ 150 | public Type getType() { 151 | return type; 152 | } 153 | 154 | @Override 155 | public boolean equals(Object o) { 156 | if (this == o) return true; 157 | if (o == null || getClass() != o.getClass()) return false; 158 | 159 | PhoneNumber that = (PhoneNumber) o; 160 | 161 | return number.equals(that.number) && type == that.type && 162 | !(label != null ? !label.equals(that.label) : that.label != null) && 163 | !(normalizedNumber != null ? !normalizedNumber.equals(that.normalizedNumber) : 164 | that.normalizedNumber != null); 165 | } 166 | 167 | @Override 168 | public int hashCode() { 169 | int result = number.hashCode(); 170 | result = 31 * result + type.hashCode(); 171 | result = 31 * result + (label != null ? label.hashCode() : 0); 172 | result = 31 * result + (normalizedNumber != null ? normalizedNumber.hashCode() : 0); 173 | return result; 174 | } 175 | } 176 | -------------------------------------------------------------------------------- /contacts/src/main/java/com/github/tamir7/contacts/Query.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016 Tamir Shomer 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package com.github.tamir7.contacts; 18 | 19 | import android.content.Context; 20 | import android.database.Cursor; 21 | import android.provider.ContactsContract; 22 | 23 | import java.util.ArrayList; 24 | import java.util.Arrays; 25 | import java.util.HashMap; 26 | import java.util.HashSet; 27 | import java.util.LinkedHashMap; 28 | import java.util.List; 29 | import java.util.Map; 30 | import java.util.Set; 31 | 32 | /** 33 | * The Query class defines a query that is used to fetch Contact objects. 34 | */ 35 | public final class Query { 36 | private final Context context; 37 | private final Map mimeWhere = new HashMap<>(); 38 | private Where defaultWhere = null; 39 | private Set include = new HashSet<>(); 40 | private List innerQueries; 41 | 42 | Query(Context context) { 43 | this.context = context; 44 | include.addAll(Arrays.asList(Contact.Field.values())); 45 | } 46 | 47 | /** 48 | * Add a constraint to the query for finding string values that contain the provided string. 49 | * 50 | * @param field The field that the string to match is stored in. 51 | * @param value The substring that the value must contain. 52 | * @return this, so you can chain this call. 53 | */ 54 | public Query whereContains(Contact.Field field, Object value) { 55 | addNewConstraint(field, Where.contains(field.getColumn(), value)); 56 | return this; 57 | } 58 | 59 | /** 60 | * Add a constraint to the query for finding string values that start with the provided string. 61 | * 62 | * @param field The field that the string to match is stored in. 63 | * @param value The substring that the value must start with. 64 | * @return this, so you can chain this call. 65 | */ 66 | public Query whereStartsWith(Contact.Field field, Object value) { 67 | addNewConstraint(field, Where.startsWith(field.getColumn(), value)); 68 | return this; 69 | } 70 | 71 | /** 72 | * Add a constraint to the query for finding values that equal the provided value. 73 | * 74 | * @param field The field that the value to match is stored in. 75 | * @param value The value that the field value must be equal to. 76 | * @return this, so you can chain this call. 77 | */ 78 | public Query whereEqualTo(Contact.Field field, Object value) { 79 | addNewConstraint(field, Where.equalTo(field.getColumn(), value)); 80 | return this; 81 | } 82 | 83 | 84 | /** 85 | * Add a constraint to the query for finding values that NOT equal the provided value. 86 | * 87 | * @param field The field that the value to match is stored in. 88 | * @param value The value that the field value must be NOT equal to. 89 | * @return this, so you can chain this call. 90 | */ 91 | public Query whereNotEqualTo(Contact.Field field, Object value) { 92 | addNewConstraint(field, Where.notEqualTo(field.getColumn(), value)); 93 | return this; 94 | } 95 | 96 | /** 97 | * Restrict the return contacts to only include contacts with a phone number. 98 | * 99 | * @return this, so you can chain this call. 100 | */ 101 | public Query hasPhoneNumber() { 102 | defaultWhere = addWhere(defaultWhere, Where.notEqualTo(ContactsContract.Data.HAS_PHONE_NUMBER, 0)); 103 | return this; 104 | } 105 | 106 | /** 107 | * Constructs a query that is the or of the given queries. 108 | * Previous calls to include are disregarded for the inner queries. 109 | * Calling those functions on the returned query will have the desired effect. 110 | * Calling where* functions on the return query is not permitted. 111 | * 112 | * @param queries The list of Queries to 'or' together. 113 | * @return A query that is the 'or' of the passed in queries. 114 | */ 115 | public Query or(List queries) { 116 | innerQueries = queries; 117 | return this; 118 | } 119 | 120 | /** 121 | * Restrict the fields of returned Contacts to only include the provided fields. 122 | * 123 | * @param fields The array of keys to include in the result. 124 | * @return this, so you can chain this call. 125 | */ 126 | public Query include(Contact.Field... fields) { 127 | include.clear(); 128 | include.addAll(Arrays.asList(fields)); 129 | return this; 130 | } 131 | 132 | /** 133 | * Retrieves a list of contacts that satisfy this query. 134 | * 135 | * @return A list of all contacts obeying the conditions set in this query. 136 | */ 137 | public List find() { 138 | List ids = new ArrayList<>(); 139 | 140 | if (innerQueries != null) { 141 | for (Query query : innerQueries) { 142 | ids.addAll(query.findInner()); 143 | } 144 | } else { 145 | if (mimeWhere.isEmpty()) { 146 | return find(null); 147 | } 148 | 149 | for (Map.Entry entry : mimeWhere.entrySet()) { 150 | ids = findIds(ids, entry.getKey(), entry.getValue()); 151 | } 152 | } 153 | 154 | return find(ids); 155 | } 156 | 157 | private List findIds(List ids, String mimeType, Where innerWhere) { 158 | String[] projection = { ContactsContract.RawContacts.CONTACT_ID}; 159 | Where where = Where.equalTo(ContactsContract.Data.MIMETYPE, mimeType); 160 | where = addWhere(where, innerWhere); 161 | if (!ids.isEmpty()) { 162 | where = addWhere(where, Where.in(ContactsContract.RawContacts.CONTACT_ID, new ArrayList(ids))); 163 | } 164 | 165 | Cursor c = context.getContentResolver().query(ContactsContract.Data.CONTENT_URI, 166 | projection, 167 | where.toString(), 168 | null, 169 | ContactsContract.RawContacts.CONTACT_ID); 170 | 171 | List returnIds = new ArrayList<>(); 172 | 173 | if (c != null) { 174 | while (c.moveToNext()) { 175 | CursorHelper helper = new CursorHelper(c); 176 | returnIds.add(helper.getContactId()); 177 | } 178 | 179 | c.close(); 180 | } 181 | 182 | return returnIds; 183 | } 184 | 185 | private List findInner() { 186 | List ids = new ArrayList<>(); 187 | 188 | if (mimeWhere.isEmpty()) { 189 | Cursor c = context.getContentResolver().query(ContactsContract.Data.CONTENT_URI, 190 | new String[]{ContactsContract.RawContacts.CONTACT_ID}, 191 | defaultWhere.toString(), 192 | null, 193 | ContactsContract.RawContacts.CONTACT_ID); 194 | if (c != null) { 195 | while (c.moveToNext()) { 196 | CursorHelper helper = new CursorHelper(c); 197 | ids.add(helper.getContactId()); 198 | } 199 | 200 | c.close(); 201 | } 202 | } else { 203 | for (Map.Entry entry : mimeWhere.entrySet()) { 204 | ids = findIds(ids, entry.getKey(), entry.getValue()); 205 | } 206 | } 207 | return ids; 208 | } 209 | 210 | private List find(List ids) { 211 | Where where; 212 | if (ids == null) { 213 | where = defaultWhere; 214 | } else if (ids.isEmpty()) { 215 | return new ArrayList<>(); 216 | } else { 217 | where = Where.in(ContactsContract.RawContacts.CONTACT_ID, new ArrayList<>(ids)); 218 | } 219 | 220 | Cursor c = context.getContentResolver().query(ContactsContract.Data.CONTENT_URI, 221 | buildProjection(), 222 | addWhere(where, buildWhereFromInclude()).toString(), 223 | null, 224 | ContactsContract.Data.DISPLAY_NAME); 225 | 226 | Map contactsMap = new LinkedHashMap<>(); 227 | 228 | if (c != null) { 229 | while (c.moveToNext()) { 230 | CursorHelper helper = new CursorHelper(c); 231 | Long contactId = helper.getContactId(); 232 | Contact contact = contactsMap.get(contactId); 233 | if (contact == null) { 234 | contact = new Contact(); 235 | contactsMap.put(contactId, contact); 236 | } 237 | 238 | contact.setId(contactId); 239 | updateContact(contact, helper); 240 | } 241 | 242 | c.close(); 243 | } 244 | 245 | return new ArrayList<>(contactsMap.values()); 246 | } 247 | 248 | private Where buildWhereFromInclude() { 249 | Set mimes = new HashSet<>(); 250 | for (Contact.Field field : include) { 251 | if (field.getMimeType() != null) { 252 | mimes.add(field.getMimeType()); 253 | } 254 | } 255 | return Where.in(ContactsContract.Data.MIMETYPE, new ArrayList(mimes)); 256 | } 257 | 258 | private void addNewConstraint(Contact.Field field, Where where) { 259 | if (field.getMimeType() == null) { 260 | defaultWhere = addWhere(defaultWhere, where); 261 | } else { 262 | Where existingWhere = mimeWhere.get(field.getMimeType()); 263 | mimeWhere.put(field.getMimeType(), addWhere(existingWhere, where)); 264 | } 265 | } 266 | 267 | private void updateContact(Contact contact, CursorHelper helper) { 268 | String displayName = helper.getDisplayName(); 269 | if (displayName != null) { 270 | contact.addDisplayName(displayName); 271 | } 272 | 273 | String photoUri = helper.getPhotoUri(); 274 | if (photoUri != null) { 275 | contact.addPhotoUri(photoUri); 276 | } 277 | 278 | String mimeType = helper.getMimeType(); 279 | switch (mimeType) { 280 | case ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE: 281 | PhoneNumber phoneNumber = helper.getPhoneNumber(); 282 | if (phoneNumber != null) { 283 | contact.addPhoneNumber(phoneNumber); 284 | } 285 | break; 286 | case ContactsContract.CommonDataKinds.Email.CONTENT_ITEM_TYPE: 287 | Email email = helper.getEmail(); 288 | if (email != null) { 289 | contact.addEmail(email); 290 | } 291 | break; 292 | case ContactsContract.CommonDataKinds.Event.CONTENT_ITEM_TYPE: 293 | Event event = helper.getEvent(); 294 | if (event != null) { 295 | contact.addEvent(event); 296 | } 297 | break; 298 | case ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE: 299 | String givenName = helper.getGivenName(); 300 | if (givenName != null) { 301 | contact.addGivenName(givenName); 302 | } 303 | 304 | String familyName = helper.getFamilyName(); 305 | if (familyName != null) { 306 | contact.addFamilyName(familyName); 307 | } 308 | break; 309 | case ContactsContract.CommonDataKinds.Organization.CONTENT_ITEM_TYPE: 310 | String companyName = helper.getCompanyName(); 311 | 312 | if (companyName != null) { 313 | contact.addCompanyName(companyName); 314 | } 315 | 316 | String companyTitle = helper.getCompanyTitle(); 317 | 318 | if (companyTitle != null) { 319 | contact.addCompanyTitle(companyTitle); 320 | } 321 | break; 322 | case ContactsContract.CommonDataKinds.Website.CONTENT_ITEM_TYPE: 323 | String website = helper.getWebsite(); 324 | if (website != null) { 325 | contact.addWebsite(website); 326 | } 327 | break; 328 | case ContactsContract.CommonDataKinds.Note.CONTENT_ITEM_TYPE: 329 | String note = helper.getNote(); 330 | if (note != null) { 331 | contact.addNote(note); 332 | } 333 | break; 334 | case ContactsContract.CommonDataKinds.StructuredPostal.CONTENT_ITEM_TYPE: 335 | Address address = helper.getAddress(); 336 | if (address != null) { 337 | contact.addAddress(address); 338 | } 339 | break; 340 | } 341 | } 342 | 343 | private String[] buildProjection() { 344 | Set projection = new HashSet<>(); 345 | 346 | for (Contact.AbstractField field : Contact.InternalField.values()) { 347 | projection.add(field.getColumn()); 348 | } 349 | 350 | for (Contact.AbstractField field : include) { 351 | projection.add(field.getColumn()); 352 | } 353 | 354 | return projection.toArray(new String[projection.size()]); 355 | } 356 | 357 | private Where addWhere(Where where, Where otherWhere) { 358 | return where == null ? otherWhere : where.and(otherWhere); 359 | } 360 | } 361 | -------------------------------------------------------------------------------- /contacts/src/main/java/com/github/tamir7/contacts/Where.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016 Tamir Shomer 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package com.github.tamir7.contacts; 18 | 19 | import android.database.DatabaseUtils; 20 | 21 | import java.util.List; 22 | 23 | final class Where { 24 | private StringBuilder where; 25 | 26 | private Where(String key, String value, Operator operator) { 27 | where = new StringBuilder(key).append(operator.toString()).append(value); 28 | } 29 | 30 | private Where(String key, List objects, Operator operator) { 31 | where = new StringBuilder(key).append(operator).append("("); 32 | boolean first = true; 33 | for (Object o : objects) { 34 | if (first) { 35 | first = false; 36 | } else { 37 | where.append(", "); 38 | } 39 | where.append(toSafeString(o)); 40 | } 41 | 42 | where.append(")"); 43 | } 44 | 45 | static Where in(String key, List objects) { 46 | return new Where(key, objects, Operator.In); 47 | } 48 | 49 | static Where in(String key, String statement) { 50 | return new Where(key, statement, Operator.In); 51 | } 52 | 53 | static Where notIn(String key, List objects) { 54 | return new Where(key, objects, Operator.NotIn); 55 | } 56 | 57 | static Where equalTo(String key, Object value) { 58 | return new Where(key, toSafeString(value), Operator.Equal); 59 | } 60 | 61 | static Where startsWith(String key, Object prefix) { 62 | return new Where(key, String.format("'%s%%'", prefix.toString()), Operator.Like); 63 | } 64 | 65 | static Where endsWith(String key, Object suffix) { 66 | return new Where(key, String.format("'%%%s'", suffix.toString()), Operator.Like); 67 | } 68 | 69 | static Where contains(String key, Object substring) { 70 | return new Where(key, String.format("'%%%s%%'", substring.toString()), Operator.Like); 71 | } 72 | 73 | static Where doesNotStartWith(String key, Object prefix) { 74 | return new Where(key, String.format("'%s%%'", prefix.toString()), Operator.NotLike); 75 | } 76 | 77 | static Where notEqualTo(String key, Object value) { 78 | return new Where(key, toSafeString(value), Operator.NotEqual); 79 | } 80 | 81 | static Where greaterThan(String key, Object value) { 82 | return new Where(key, toSafeString(value), Operator.GreaterThan); 83 | } 84 | 85 | static Where greaterThanOrEqual(String key, Object value) { 86 | return new Where(key, toSafeString(value), Operator.GreaterThanOrEqual); 87 | } 88 | 89 | static Where lessThan(String key, Object value) { 90 | return new Where(key, toSafeString(value), Operator.LessThan); 91 | } 92 | 93 | static Where lessThanOrEqual(String key, Object value) { 94 | return new Where(key, toSafeString(value), Operator.LessThanOrEqual); 95 | } 96 | 97 | static Where is(String key, Object value) { 98 | return new Where(key, toSafeString(value), Operator.Is); 99 | } 100 | 101 | static Where isNot(String key, Object value) { 102 | return new Where(key, toSafeString(value), Operator.IsNot); 103 | } 104 | 105 | Where and(Where andWhere) { 106 | where = new StringBuilder(String.format("( %s AND %s )", where.toString(), 107 | andWhere.toString())); 108 | return this; 109 | } 110 | 111 | Where or(Where orWhere) { 112 | where = new StringBuilder(String.format("( %s OR %s )", where.toString(), 113 | orWhere.toString())); 114 | return this; 115 | } 116 | 117 | private static String toSafeString(Object o) { 118 | return o instanceof String ? DatabaseUtils.sqlEscapeString(o.toString()) : o.toString(); 119 | } 120 | 121 | @Override 122 | public String toString() { 123 | return where.toString(); 124 | } 125 | 126 | private enum Operator { 127 | Equal("="), 128 | NotEqual("!="), 129 | GreaterThan(">"), 130 | GreaterThanOrEqual(">="), 131 | LessThan("<"), 132 | LessThanOrEqual("<="), 133 | Like(" LIKE "), 134 | NotLike(" NOT LIKE "), 135 | Is(" IS "), 136 | IsNot(" IS NOT "), 137 | In(" IN "), 138 | NotIn(" NOT IN "); 139 | 140 | private final String value; 141 | 142 | Operator(String value) { 143 | this.value = value; 144 | } 145 | 146 | @Override 147 | public String toString() { 148 | return value; 149 | } 150 | } 151 | } 152 | -------------------------------------------------------------------------------- /gradle.properties: -------------------------------------------------------------------------------- 1 | # suppress inspection "UnusedProperty" for whole file 2 | NAME=Contacts 3 | VERSION_NAME=1.1.7 4 | GROUP=com.github.tamir7.contacts 5 | DESCRIPTION=Android Contacts API 6 | SITE_URL=https://github.com/tamir7/Contacts 7 | GIT_URL=https://github.com/tamir7/Contacts.git 8 | ISSUE_TRACKER_URL=https://github.com/tamir7/Contacts/issues 9 | LICENCE_NAME=Apache 2.0 10 | LICENCE_URL=http://www.apache.org/licenses/LICENSE-2.0.txt 11 | DEVELOPER_ID=tamir7 12 | DEVELOPER_NAME=Tamir Shomer 13 | DEVELOPER_EMAIL=tamir7@gmail.com -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tamir7/Contacts/bfdda52801b85908ef77ea9e63a9348c6e2cd8f4/gradle/wrapper/gradle-wrapper.jar -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.properties: -------------------------------------------------------------------------------- 1 | #Sun May 14 14:51:43 IDT 2017 2 | distributionBase=GRADLE_USER_HOME 3 | distributionPath=wrapper/dists 4 | zipStoreBase=GRADLE_USER_HOME 5 | zipStorePath=wrapper/dists 6 | distributionUrl=https\://services.gradle.org/distributions/gradle-3.3-all.zip 7 | -------------------------------------------------------------------------------- /gradlew: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | 3 | ############################################################################## 4 | ## 5 | ## Gradle start up script for UN*X 6 | ## 7 | ############################################################################## 8 | 9 | # Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 10 | DEFAULT_JVM_OPTS="" 11 | 12 | APP_NAME="Gradle" 13 | APP_BASE_NAME=`basename "$0"` 14 | 15 | # Use the maximum available, or set MAX_FD != -1 to use that value. 16 | MAX_FD="maximum" 17 | 18 | warn ( ) { 19 | echo "$*" 20 | } 21 | 22 | die ( ) { 23 | echo 24 | echo "$*" 25 | echo 26 | exit 1 27 | } 28 | 29 | # OS specific support (must be 'true' or 'false'). 30 | cygwin=false 31 | msys=false 32 | darwin=false 33 | case "`uname`" in 34 | CYGWIN* ) 35 | cygwin=true 36 | ;; 37 | Darwin* ) 38 | darwin=true 39 | ;; 40 | MINGW* ) 41 | msys=true 42 | ;; 43 | esac 44 | 45 | # Attempt to set APP_HOME 46 | # Resolve links: $0 may be a link 47 | PRG="$0" 48 | # Need this for relative symlinks. 49 | while [ -h "$PRG" ] ; do 50 | ls=`ls -ld "$PRG"` 51 | link=`expr "$ls" : '.*-> \(.*\)$'` 52 | if expr "$link" : '/.*' > /dev/null; then 53 | PRG="$link" 54 | else 55 | PRG=`dirname "$PRG"`"/$link" 56 | fi 57 | done 58 | SAVED="`pwd`" 59 | cd "`dirname \"$PRG\"`/" >/dev/null 60 | APP_HOME="`pwd -P`" 61 | cd "$SAVED" >/dev/null 62 | 63 | CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar 64 | 65 | # Determine the Java command to use to start the JVM. 66 | if [ -n "$JAVA_HOME" ] ; then 67 | if [ -x "$JAVA_HOME/jre/sh/java" ] ; then 68 | # IBM's JDK on AIX uses strange locations for the executables 69 | JAVACMD="$JAVA_HOME/jre/sh/java" 70 | else 71 | JAVACMD="$JAVA_HOME/bin/java" 72 | fi 73 | if [ ! -x "$JAVACMD" ] ; then 74 | die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME 75 | 76 | Please set the JAVA_HOME variable in your environment to match the 77 | location of your Java installation." 78 | fi 79 | else 80 | JAVACMD="java" 81 | which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 82 | 83 | Please set the JAVA_HOME variable in your environment to match the 84 | location of your Java installation." 85 | fi 86 | 87 | # Increase the maximum file descriptors if we can. 88 | if [ "$cygwin" = "false" -a "$darwin" = "false" ] ; then 89 | MAX_FD_LIMIT=`ulimit -H -n` 90 | if [ $? -eq 0 ] ; then 91 | if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then 92 | MAX_FD="$MAX_FD_LIMIT" 93 | fi 94 | ulimit -n $MAX_FD 95 | if [ $? -ne 0 ] ; then 96 | warn "Could not set maximum file descriptor limit: $MAX_FD" 97 | fi 98 | else 99 | warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT" 100 | fi 101 | fi 102 | 103 | # For Darwin, add options to specify how the application appears in the dock 104 | if $darwin; then 105 | GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\"" 106 | fi 107 | 108 | # For Cygwin, switch paths to Windows format before running java 109 | if $cygwin ; then 110 | APP_HOME=`cygpath --path --mixed "$APP_HOME"` 111 | CLASSPATH=`cygpath --path --mixed "$CLASSPATH"` 112 | JAVACMD=`cygpath --unix "$JAVACMD"` 113 | 114 | # We build the pattern for arguments to be converted via cygpath 115 | ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null` 116 | SEP="" 117 | for dir in $ROOTDIRSRAW ; do 118 | ROOTDIRS="$ROOTDIRS$SEP$dir" 119 | SEP="|" 120 | done 121 | OURCYGPATTERN="(^($ROOTDIRS))" 122 | # Add a user-defined pattern to the cygpath arguments 123 | if [ "$GRADLE_CYGPATTERN" != "" ] ; then 124 | OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)" 125 | fi 126 | # Now convert the arguments - kludge to limit ourselves to /bin/sh 127 | i=0 128 | for arg in "$@" ; do 129 | CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -` 130 | CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option 131 | 132 | if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition 133 | eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"` 134 | else 135 | eval `echo args$i`="\"$arg\"" 136 | fi 137 | i=$((i+1)) 138 | done 139 | case $i in 140 | (0) set -- ;; 141 | (1) set -- "$args0" ;; 142 | (2) set -- "$args0" "$args1" ;; 143 | (3) set -- "$args0" "$args1" "$args2" ;; 144 | (4) set -- "$args0" "$args1" "$args2" "$args3" ;; 145 | (5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;; 146 | (6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;; 147 | (7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;; 148 | (8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;; 149 | (9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;; 150 | esac 151 | fi 152 | 153 | # Split up the JVM_OPTS And GRADLE_OPTS values into an array, following the shell quoting and substitution rules 154 | function splitJvmOpts() { 155 | JVM_OPTS=("$@") 156 | } 157 | eval splitJvmOpts $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS 158 | JVM_OPTS[${#JVM_OPTS[*]}]="-Dorg.gradle.appname=$APP_BASE_NAME" 159 | 160 | exec "$JAVACMD" "${JVM_OPTS[@]}" -classpath "$CLASSPATH" org.gradle.wrapper.GradleWrapperMain "$@" 161 | -------------------------------------------------------------------------------- /gradlew.bat: -------------------------------------------------------------------------------- 1 | @if "%DEBUG%" == "" @echo off 2 | @rem ########################################################################## 3 | @rem 4 | @rem Gradle startup script for Windows 5 | @rem 6 | @rem ########################################################################## 7 | 8 | @rem Set local scope for the variables with windows NT shell 9 | if "%OS%"=="Windows_NT" setlocal 10 | 11 | @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 12 | set DEFAULT_JVM_OPTS= 13 | 14 | set DIRNAME=%~dp0 15 | if "%DIRNAME%" == "" set DIRNAME=. 16 | set APP_BASE_NAME=%~n0 17 | set APP_HOME=%DIRNAME% 18 | 19 | @rem Find java.exe 20 | if defined JAVA_HOME goto findJavaFromJavaHome 21 | 22 | set JAVA_EXE=java.exe 23 | %JAVA_EXE% -version >NUL 2>&1 24 | if "%ERRORLEVEL%" == "0" goto init 25 | 26 | echo. 27 | echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 28 | echo. 29 | echo Please set the JAVA_HOME variable in your environment to match the 30 | echo location of your Java installation. 31 | 32 | goto fail 33 | 34 | :findJavaFromJavaHome 35 | set JAVA_HOME=%JAVA_HOME:"=% 36 | set JAVA_EXE=%JAVA_HOME%/bin/java.exe 37 | 38 | if exist "%JAVA_EXE%" goto init 39 | 40 | echo. 41 | echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% 42 | echo. 43 | echo Please set the JAVA_HOME variable in your environment to match the 44 | echo location of your Java installation. 45 | 46 | goto fail 47 | 48 | :init 49 | @rem Get command-line arguments, handling Windowz variants 50 | 51 | if not "%OS%" == "Windows_NT" goto win9xME_args 52 | if "%@eval[2+2]" == "4" goto 4NT_args 53 | 54 | :win9xME_args 55 | @rem Slurp the command line arguments. 56 | set CMD_LINE_ARGS= 57 | set _SKIP=2 58 | 59 | :win9xME_args_slurp 60 | if "x%~1" == "x" goto execute 61 | 62 | set CMD_LINE_ARGS=%* 63 | goto execute 64 | 65 | :4NT_args 66 | @rem Get arguments from the 4NT Shell from JP Software 67 | set CMD_LINE_ARGS=%$ 68 | 69 | :execute 70 | @rem Setup the command line 71 | 72 | set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar 73 | 74 | @rem Execute Gradle 75 | "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS% 76 | 77 | :end 78 | @rem End local scope for the variables with windows NT shell 79 | if "%ERRORLEVEL%"=="0" goto mainEnd 80 | 81 | :fail 82 | rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of 83 | rem the _cmd.exe /c_ return code! 84 | if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1 85 | exit /b 1 86 | 87 | :mainEnd 88 | if "%OS%"=="Windows_NT" endlocal 89 | 90 | :omega 91 | -------------------------------------------------------------------------------- /sample/.gitignore: -------------------------------------------------------------------------------- 1 | /build 2 | -------------------------------------------------------------------------------- /sample/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'com.android.application' 2 | 3 | android { 4 | compileSdkVersion 25 5 | buildToolsVersion "25.0.3" 6 | 7 | defaultConfig { 8 | applicationId "com.github.tamir7.contacts.sample" 9 | minSdkVersion 16 10 | targetSdkVersion 25 11 | versionCode 1 12 | versionName "1.0" 13 | } 14 | buildTypes { 15 | release { 16 | minifyEnabled false 17 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' 18 | } 19 | } 20 | } 21 | 22 | dependencies { 23 | compile 'com.android.support:appcompat-v7:25.3.1' 24 | compile 'com.google.code.gson:gson:2.7' 25 | compile 'com.parse.bolts:bolts-android:1.4.0' 26 | compile project(':contacts') 27 | } 28 | -------------------------------------------------------------------------------- /sample/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 3 | 4 | 5 | 6 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | -------------------------------------------------------------------------------- /sample/src/main/java/com/github/tamir7/contacts/sample/SampleActivity.java: -------------------------------------------------------------------------------- 1 | package com.github.tamir7.contacts.sample; 2 | 3 | import android.Manifest; 4 | import android.content.pm.PackageManager; 5 | import android.os.Build; 6 | import android.os.Bundle; 7 | import android.support.v4.content.ContextCompat; 8 | import android.support.v7.app.AppCompatActivity; 9 | import android.util.Log; 10 | 11 | import com.github.tamir7.contacts.Contact; 12 | import com.github.tamir7.contacts.Contacts; 13 | import com.github.tamir7.contacts.Query; 14 | import com.google.gson.GsonBuilder; 15 | 16 | import java.util.ArrayList; 17 | import java.util.List; 18 | import java.util.concurrent.Callable; 19 | 20 | import bolts.Continuation; 21 | import bolts.Task; 22 | 23 | public class SampleActivity extends AppCompatActivity { 24 | private static final String TAG = SampleActivity.class.getSimpleName(); 25 | private static final int READ_CONTACT_PERMISSION_REQUEST_CODE = 76; 26 | 27 | @Override 28 | protected void onCreate(Bundle savedInstanceState) { 29 | super.onCreate(savedInstanceState); 30 | setContentView(R.layout.activity_sample); 31 | 32 | checkPermission(); 33 | } 34 | 35 | private void queryContacts() { 36 | Task.callInBackground(new Callable() { 37 | @Override 38 | public Void call() throws Exception { 39 | Query q = Contacts.getQuery(); 40 | q.include(Contact.Field.ContactId, Contact.Field.DisplayName, Contact.Field.PhoneNumber, Contact.Field.PhoneNormalizedNumber, Contact.Field.Email); 41 | Query q1 = Contacts.getQuery(); 42 | q1.whereEqualTo(Contact.Field.DisplayName, "Tamir Shomer"); 43 | q1.hasPhoneNumber(); 44 | 45 | Query q2 = Contacts.getQuery(); 46 | q2.whereStartsWith(Contact.Field.ContactId, "791"); 47 | q2.hasPhoneNumber(); 48 | List queries = new ArrayList<>(); 49 | queries.add(q1); 50 | queries.add(q2); 51 | q.or(queries); 52 | 53 | List contacts = q.find(); 54 | Log.e(TAG, new GsonBuilder().setPrettyPrinting().create().toJson(contacts)); 55 | return null; 56 | } 57 | }).continueWith(new Continuation() { 58 | @Override 59 | public Void then(Task task) throws Exception { 60 | if (task.isFaulted()) { 61 | Log.e(TAG, "find failed", task.getError()); 62 | } 63 | return null; 64 | } 65 | }); 66 | } 67 | 68 | private void checkPermission() { 69 | if (ContextCompat.checkSelfPermission(this, Manifest.permission.READ_CONTACTS) == 70 | PackageManager.PERMISSION_GRANTED) { 71 | queryContacts(); 72 | } else { 73 | if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { 74 | requestPermissions(new String[]{Manifest.permission.READ_CONTACTS}, 75 | READ_CONTACT_PERMISSION_REQUEST_CODE); 76 | } 77 | } 78 | } 79 | 80 | public void onRequestPermissionsResult(int requestCode, String[] permissions, 81 | int[] grantResults) { 82 | if (requestCode == READ_CONTACT_PERMISSION_REQUEST_CODE 83 | && grantResults[0] == PackageManager.PERMISSION_GRANTED) { 84 | queryContacts(); 85 | } 86 | } 87 | } 88 | -------------------------------------------------------------------------------- /sample/src/main/java/com/github/tamir7/contacts/sample/SampleApplication.java: -------------------------------------------------------------------------------- 1 | package com.github.tamir7.contacts.sample; 2 | 3 | import android.app.Application; 4 | 5 | import com.github.tamir7.contacts.Contacts; 6 | 7 | public class SampleApplication extends Application { 8 | 9 | @Override 10 | public void onCreate() { 11 | super.onCreate(); 12 | Contacts.initialize(this); 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /sample/src/main/res/layout/activity_sample.xml: -------------------------------------------------------------------------------- 1 | 2 | 7 | 8 | 13 | 14 | -------------------------------------------------------------------------------- /sample/src/main/res/mipmap-hdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tamir7/Contacts/bfdda52801b85908ef77ea9e63a9348c6e2cd8f4/sample/src/main/res/mipmap-hdpi/ic_launcher.png -------------------------------------------------------------------------------- /sample/src/main/res/mipmap-mdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tamir7/Contacts/bfdda52801b85908ef77ea9e63a9348c6e2cd8f4/sample/src/main/res/mipmap-mdpi/ic_launcher.png -------------------------------------------------------------------------------- /sample/src/main/res/mipmap-xhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tamir7/Contacts/bfdda52801b85908ef77ea9e63a9348c6e2cd8f4/sample/src/main/res/mipmap-xhdpi/ic_launcher.png -------------------------------------------------------------------------------- /sample/src/main/res/mipmap-xxhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tamir7/Contacts/bfdda52801b85908ef77ea9e63a9348c6e2cd8f4/sample/src/main/res/mipmap-xxhdpi/ic_launcher.png -------------------------------------------------------------------------------- /sample/src/main/res/mipmap-xxxhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tamir7/Contacts/bfdda52801b85908ef77ea9e63a9348c6e2cd8f4/sample/src/main/res/mipmap-xxxhdpi/ic_launcher.png -------------------------------------------------------------------------------- /sample/src/main/res/values/colors.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | #3F51B5 4 | #303F9F 5 | #FF4081 6 | 7 | -------------------------------------------------------------------------------- /sample/src/main/res/values/strings.xml: -------------------------------------------------------------------------------- 1 | 2 | Contacts 3 | 4 | -------------------------------------------------------------------------------- /sample/src/main/res/values/styles.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 10 | 11 | 12 | -------------------------------------------------------------------------------- /settings.gradle: -------------------------------------------------------------------------------- 1 | include ':sample', ':contacts' 2 | --------------------------------------------------------------------------------