Basically we sync all the contact everytime when user do login in our app but this is time consuming task to do all the time so it would be better and effective if we sync only new contact after last sync.
so basically for the first time we have to save some sync time on response success in do in background like this :
if (response.contains("success")) {
Utils.saveStringInSP(aContext, "contactsSyncTime", "" + Utils.getCurrentTimeStamp());
}
Function to save sharedpreference in Utils class :
public static void saveStringInSP(Context context, String key, String value) {
SharedPreferences preferences = context.getSharedPreferences(Constants.SHARED_PREFERENCES_NAME, Context.MODE_PRIVATE);
SharedPreferences.Editor editor = preferences.edit();
editor.putString(key, value);
editor.commit();
}
Fucntion to get current time stamp :
public static Long getCurrentTimeStamp(){
Calendar myCalendar = Calendar.getInstance();
return myCalendar.getTimeInMillis();
}
so we are saving time stamp after getting success response during sync. Now wo we will check time stamp of every contacts if any contact is newly added then its obviuos that timestamp will be greater than last sync time.
Function to get all contacts and save only newly added contacts based on time stamp value like this :
ContactsBean is a class that contains getter setter of variable phone,time stamp etc.
public static ArrayList<ContactsBean> readContacts(Context context) {
ArrayList<ContactsBean> contactsBeanArrayList = new ArrayList<>();
StringBuilder sb = new StringBuilder();
sb.append("......Contact Details.....");
ContentResolver cr = context.getContentResolver();
Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI, null, ContactsContract.Contacts.IN_VISIBLE_GROUP + "=1", null, null);
String phone,timeStamp;
String emailContact;
String emailType;
String image_uri;
if (cur.getCount() > 0) {
while (cur.moveToNext()) {
ContactsBean bean = new ContactsBean();
String id = cur.getString(cur.getColumnIndex(ContactsContract.Contacts._ID));
String name = cur.getString(cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME_PRIMARY));
bean.setConactName(name);
image_uri = cur.getString(cur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.PHOTO_URI));
if (Integer.parseInt(cur.getString(cur.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0) {
sb.append("\n Contact Name:" + name);
bean.setContactProfilePicture(image_uri);
Cursor pCur = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ?", new String[]{id}, null);
while (pCur.moveToNext()) {
phone = pCur.getString(pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
sb.append("\n Phone number:" + phone);
timeStamp = pCur.getString(pCur.getColumnIndex(ContactsContract.Contacts.CONTACT_LAST_UPDATED_TIMESTAMP));
long time = Long.parseLong(timeStamp);
bean.setContactPhone(phone);
bean.setContactTimeStamp(time);
}
pCur.close();
Cursor emailCur = cr.query(ContactsContract.CommonDataKinds.Email.CONTENT_URI, null, ContactsContract.CommonDataKinds.Email.CONTACT_ID + " = ?", new String[]{id}, null);
while (emailCur.moveToNext()) {
emailContact = emailCur.getString(emailCur.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA));
emailType = emailCur.getString(emailCur.getColumnIndex(ContactsContract.CommonDataKinds.Email.TYPE));
sb.append("\nEmail:" + emailContact + "Email type:" + emailType);
bean.setContactMail(emailContact);
}
emailCur.close();
}
if (Utils.getStringFromSP(context,"contactsSyncTime") != null){
if (compareSyncTime(Long.parseLong(Utils.getStringFromSP(context,"contactsSyncTime")),bean.getContactTimeStamp())){
contactsBeanArrayList.add(bean);
}
}else{
contactsBeanArrayList.add(bean);
}
}
cur.close();
}
return contactsBeanArrayList;
}
public static boolean compareSyncTime(long aLastSyncTime, long aContactTimeStamp){
if(aContactTimeStamp > aLastSyncTime){
return true;
}else{
return false;
}
}
1 Comment(s)