mirror of
https://github.com/apache/cordova-android.git
synced 2025-02-26 12:03:28 +08:00
Small refactor on birthday and anniversary
This commit is contained in:
parent
f272748c5b
commit
f68b75c1cf
@ -70,8 +70,7 @@ public class ContactAccessorSdk5 extends ContactAccessor {
|
|||||||
System.out.println("Limit = " + limit);
|
System.out.println("Limit = " + limit);
|
||||||
System.out.println("Multiple = " + multiple);
|
System.out.println("Multiple = " + multiple);
|
||||||
} catch (JSONException e) {
|
} catch (JSONException e) {
|
||||||
// TODO Auto-generated catch block
|
Log.e(LOG_TAG, e.getMessage(), e);
|
||||||
e.printStackTrace();
|
|
||||||
}
|
}
|
||||||
// Get a cursor by creating the query.
|
// Get a cursor by creating the query.
|
||||||
ContentResolver cr = mApp.getContentResolver();
|
ContentResolver cr = mApp.getContentResolver();
|
||||||
@ -82,25 +81,20 @@ public class ContactAccessorSdk5 extends ContactAccessor {
|
|||||||
new String[] {searchTerm},
|
new String[] {searchTerm},
|
||||||
ContactsContract.Contacts.DISPLAY_NAME + " ASC");
|
ContactsContract.Contacts.DISPLAY_NAME + " ASC");
|
||||||
JSONArray contacts = new JSONArray();
|
JSONArray contacts = new JSONArray();
|
||||||
|
JSONObject contact;
|
||||||
int pos = 0;
|
int pos = 0;
|
||||||
while (cursor.moveToNext() && (pos < limit)) {
|
while (cursor.moveToNext() && (pos < limit)) {
|
||||||
JSONObject contact = new JSONObject();
|
contact = new JSONObject();
|
||||||
|
|
||||||
String contactName = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
|
String contactName = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
|
||||||
if (contactName.trim().length() == 0) continue;
|
if (contactName.trim().length() == 0) continue;
|
||||||
|
|
||||||
try {
|
|
||||||
contact.put("displayName", contactName);
|
|
||||||
} catch (JSONException e) {
|
|
||||||
// TODO Auto-generated catch block
|
|
||||||
e.printStackTrace();
|
|
||||||
}
|
|
||||||
|
|
||||||
String contactId = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));
|
String contactId = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));
|
||||||
//String hasPhone = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER));
|
//String hasPhone = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER));
|
||||||
//if (Boolean.parseBoolean(hasPhone)) {
|
//if (Boolean.parseBoolean(hasPhone)) {
|
||||||
//}
|
//}
|
||||||
try {
|
try {
|
||||||
|
contact.put("displayName", contactName);
|
||||||
contact.put("name", nameQuery(cr, contactId));
|
contact.put("name", nameQuery(cr, contactId));
|
||||||
contact.put("phoneNumbers", phoneQuery(cr, contactId));
|
contact.put("phoneNumbers", phoneQuery(cr, contactId));
|
||||||
contact.put("emails", emailQuery(cr, contactId));
|
contact.put("emails", emailQuery(cr, contactId));
|
||||||
@ -114,8 +108,7 @@ public class ContactAccessorSdk5 extends ContactAccessor {
|
|||||||
contact.put("birthday",birthdayQuery(cr, contactId));
|
contact.put("birthday",birthdayQuery(cr, contactId));
|
||||||
contact.put("anniversary",anniversaryQuery(cr, contactId));
|
contact.put("anniversary",anniversaryQuery(cr, contactId));
|
||||||
} catch (JSONException e) {
|
} catch (JSONException e) {
|
||||||
// TODO Auto-generated catch block
|
Log.e(LOG_TAG, e.getMessage(), e);
|
||||||
e.printStackTrace();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
contacts.put(contact);
|
contacts.put(contact);
|
||||||
@ -147,8 +140,7 @@ public class ContactAccessorSdk5 extends ContactAccessor {
|
|||||||
organization.put("title", cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Organization.TITLE)));
|
organization.put("title", cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Organization.TITLE)));
|
||||||
organizations.put(organization);
|
organizations.put(organization);
|
||||||
} catch (JSONException e) {
|
} catch (JSONException e) {
|
||||||
// TODO Auto-generated catch block
|
Log.e(LOG_TAG, e.getMessage(), e);
|
||||||
e.printStackTrace();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
cursor.close();
|
cursor.close();
|
||||||
@ -174,8 +166,7 @@ public class ContactAccessorSdk5 extends ContactAccessor {
|
|||||||
address.put("country", cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.COUNTRY)));
|
address.put("country", cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.COUNTRY)));
|
||||||
addresses.put(address);
|
addresses.put(address);
|
||||||
} catch (JSONException e) {
|
} catch (JSONException e) {
|
||||||
// TODO Auto-generated catch block
|
Log.e(LOG_TAG, e.getMessage(), e);
|
||||||
e.printStackTrace();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
cursor.close();
|
cursor.close();
|
||||||
@ -213,8 +204,7 @@ public class ContactAccessorSdk5 extends ContactAccessor {
|
|||||||
contactName.put("honorificSuffix", honorificSuffix);
|
contactName.put("honorificSuffix", honorificSuffix);
|
||||||
contactName.put("formatted", formatted);
|
contactName.put("formatted", formatted);
|
||||||
} catch (JSONException e) {
|
} catch (JSONException e) {
|
||||||
// TODO Auto-generated catch block
|
Log.e(LOG_TAG, e.getMessage(), e);
|
||||||
e.printStackTrace();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
name.close();
|
name.close();
|
||||||
@ -237,8 +227,7 @@ public class ContactAccessorSdk5 extends ContactAccessor {
|
|||||||
phoneNumber.put("type", phones.getInt(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.TYPE)));
|
phoneNumber.put("type", phones.getInt(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.TYPE)));
|
||||||
phoneNumbers.put(phoneNumber);
|
phoneNumbers.put(phoneNumber);
|
||||||
} catch (JSONException e) {
|
} catch (JSONException e) {
|
||||||
// TODO Auto-generated catch block
|
Log.e(LOG_TAG, e.getMessage(), e);
|
||||||
e.printStackTrace();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
phones.close();
|
phones.close();
|
||||||
@ -261,8 +250,7 @@ public class ContactAccessorSdk5 extends ContactAccessor {
|
|||||||
email.put("type", emails.getInt(emails.getColumnIndex(ContactsContract.CommonDataKinds.Email.TYPE)));
|
email.put("type", emails.getInt(emails.getColumnIndex(ContactsContract.CommonDataKinds.Email.TYPE)));
|
||||||
emailAddresses.put(email);
|
emailAddresses.put(email);
|
||||||
} catch (JSONException e) {
|
} catch (JSONException e) {
|
||||||
// TODO Auto-generated catch block
|
Log.e(LOG_TAG, e.getMessage(), e);
|
||||||
e.printStackTrace();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
emails.close();
|
emails.close();
|
||||||
@ -285,8 +273,7 @@ public class ContactAccessorSdk5 extends ContactAccessor {
|
|||||||
im.put("type", cursor.getInt(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Im.TYPE)));
|
im.put("type", cursor.getInt(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Im.TYPE)));
|
||||||
ims.put(im);
|
ims.put(im);
|
||||||
} catch (JSONException e) {
|
} catch (JSONException e) {
|
||||||
// TODO Auto-generated catch block
|
Log.e(LOG_TAG, e.getMessage(), e);
|
||||||
e.printStackTrace();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
cursor.close();
|
cursor.close();
|
||||||
@ -339,8 +326,7 @@ public class ContactAccessorSdk5 extends ContactAccessor {
|
|||||||
website.put("type", cursor.getInt(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Website.TYPE)));
|
website.put("type", cursor.getInt(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Website.TYPE)));
|
||||||
websites.put(website);
|
websites.put(website);
|
||||||
} catch (JSONException e) {
|
} catch (JSONException e) {
|
||||||
// TODO Auto-generated catch block
|
Log.e(LOG_TAG, e.getMessage(), e);
|
||||||
e.printStackTrace();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
cursor.close();
|
cursor.close();
|
||||||
@ -363,8 +349,7 @@ public class ContactAccessorSdk5 extends ContactAccessor {
|
|||||||
relationship.put("type", cursor.getInt(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Relation.TYPE)));
|
relationship.put("type", cursor.getInt(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Relation.TYPE)));
|
||||||
relationships.put(relationship);
|
relationships.put(relationship);
|
||||||
} catch (JSONException e) {
|
} catch (JSONException e) {
|
||||||
// TODO Auto-generated catch block
|
Log.e(LOG_TAG, e.getMessage(), e);
|
||||||
e.printStackTrace();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
cursor.close();
|
cursor.close();
|
||||||
@ -372,38 +357,34 @@ public class ContactAccessorSdk5 extends ContactAccessor {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private String birthdayQuery(ContentResolver cr, String contactId) {
|
private String birthdayQuery(ContentResolver cr, String contactId) {
|
||||||
String birthdayWhere = ContactsContract.Data.CONTACT_ID + " = ? AND " + ContactsContract.Data.MIMETYPE + " = ?";
|
String birthday = conditionalStringQuery(cr, contactId, ContactsContract.CommonDataKinds.Event.CONTENT_ITEM_TYPE,
|
||||||
String[] birthdayWhereParams = new String[]{contactId,
|
ContactsContract.CommonDataKinds.Event.TYPE_BIRTHDAY, ContactsContract.CommonDataKinds.Event.TYPE,
|
||||||
ContactsContract.CommonDataKinds.Event.CONTENT_ITEM_TYPE};
|
ContactsContract.CommonDataKinds.Event.START_DATE);
|
||||||
Cursor cursor = cr.query(ContactsContract.Data.CONTENT_URI,
|
Log.d(LOG_TAG, birthday);
|
||||||
null, birthdayWhere, birthdayWhereParams, null);
|
|
||||||
String birthday = new String("");
|
|
||||||
while (cursor.moveToNext()) {
|
|
||||||
Log.d(LOG_TAG, "We found a birthday!");
|
|
||||||
if (ContactsContract.CommonDataKinds.Event.TYPE_BIRTHDAY ==
|
|
||||||
cursor.getInt(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Event.TYPE))) {
|
|
||||||
birthday = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Event.START_DATE));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
cursor.close();
|
|
||||||
return birthday;
|
return birthday;
|
||||||
}
|
}
|
||||||
|
|
||||||
private String anniversaryQuery(ContentResolver cr, String contactId) {
|
private String anniversaryQuery(ContentResolver cr, String contactId) {
|
||||||
String anniversaryWhere = ContactsContract.Data.CONTACT_ID + " = ? AND " + ContactsContract.Data.MIMETYPE + " = ?";
|
String anniversary = conditionalStringQuery(cr, contactId, ContactsContract.CommonDataKinds.Event.CONTENT_ITEM_TYPE,
|
||||||
String[] anniversaryWhereParams = new String[]{contactId,
|
ContactsContract.CommonDataKinds.Event.TYPE_ANNIVERSARY, ContactsContract.CommonDataKinds.Event.TYPE,
|
||||||
ContactsContract.CommonDataKinds.Event.CONTENT_ITEM_TYPE};
|
ContactsContract.CommonDataKinds.Event.START_DATE);
|
||||||
|
Log.d(LOG_TAG, anniversary);
|
||||||
|
return anniversary;
|
||||||
|
}
|
||||||
|
|
||||||
|
private String conditionalStringQuery(ContentResolver cr, String contactId, String dataType, int type, String label, String data) {
|
||||||
|
String where = ContactsContract.Data.CONTACT_ID + " = ? AND " + ContactsContract.Data.MIMETYPE + " = ?";
|
||||||
|
String[] whereParams = new String[]{contactId, dataType};
|
||||||
Cursor cursor = cr.query(ContactsContract.Data.CONTENT_URI,
|
Cursor cursor = cr.query(ContactsContract.Data.CONTENT_URI,
|
||||||
null, anniversaryWhere, anniversaryWhereParams, null);
|
null, where, whereParams, null);
|
||||||
String anniversary = new String("");
|
String retVal = new String("");
|
||||||
while (cursor.moveToNext()) {
|
while (cursor.moveToNext()) {
|
||||||
Log.d(LOG_TAG, "We found a anniversary!");
|
Log.d(LOG_TAG, "We found an event!");
|
||||||
if (ContactsContract.CommonDataKinds.Event.TYPE_ANNIVERSARY ==
|
if (type == cursor.getInt(cursor.getColumnIndex(label))) {
|
||||||
cursor.getInt(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Event.TYPE))) {
|
retVal = cursor.getString(cursor.getColumnIndex(data));
|
||||||
anniversary = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Event.START_DATE));
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
cursor.close();
|
cursor.close();
|
||||||
return anniversary;
|
return retVal;
|
||||||
}
|
}
|
||||||
}
|
}
|
Loading…
Reference in New Issue
Block a user