1
0
mirror of https://github.com/nerzhul/ownCloud-SMS-App.git synced 2025-06-20 14:26:15 +00:00

Read phone contact list to retrieve contacts from phone numbers

* ContactListAdapter: Fix a potential crash when list is empty
This commit is contained in:
Loic Blot 2015-08-30 10:05:46 +02:00
parent b937c4ebdd
commit f7dcb8e51c
4 changed files with 49 additions and 1 deletions

View File

@ -29,6 +29,9 @@
<uses-permission android:name="android.permission.MANAGE_ACCOUNTS" />
<uses-permission android:name="android.permission.USE_CREDENTIALS" />
<!-- For backup restauration -->
<uses-permission android:name="android.permission.READ_CONTACTS" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"

View File

@ -32,6 +32,10 @@ public class ContactListAdapter extends ArrayAdapter<String> {
v = inflater.inflate(_itemLayout, null);
}
if (_objects.size() == 0) {
return null;
}
final String element = _objects.get(position);
if (element != null) {

View File

@ -2,9 +2,12 @@ package fr.unix_experience.owncloud_sms.engine;
import android.accounts.Account;
import android.accounts.AccountManager;
import android.content.ContentResolver;
import android.content.Context;
import android.database.Cursor;
import android.net.Uri;
import android.os.AsyncTask;
import android.provider.ContactsContract;
import org.json.JSONArray;
import org.json.JSONException;
@ -58,9 +61,48 @@ public interface ASyncContactLoad {
return false;
}
ArrayList<String> serverPhoneList = new ArrayList<>();
JSONArray phoneNumbers = _client.getServerPhoneNumbers();
for (int i = 0; i < phoneNumbers.length(); i++) {
String phone = phoneNumbers.getString(i);
serverPhoneList.add(phone);
}
// Read all contacts
ContentResolver cr = _context.getContentResolver();
Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI,
null, null, null, null);
if (cur.getCount() > 0) {
while (cur.moveToNext()) {
String id = cur.getString(cur.getColumnIndex(ContactsContract.Contacts._ID));
String name = cur.getString(cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
if (Integer.parseInt(cur.getString(
cur.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0) {
// Fetch all phone numbers
Cursor pCur = cr.query(
ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
null,
ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = ?",
new String[]{id}, null);
while (pCur.moveToNext()) {
String phoneNo = pCur.getString(pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
phoneNo = phoneNo.replaceAll(" ", "");
if (serverPhoneList.contains(phoneNo)) {
if (!_objects.contains(name)) {
_objects.add(name);
}
serverPhoneList.remove(phoneNo);
}
}
pCur.close();
}
}
}
cur.close();
for (String phone: serverPhoneList) {
_objects.add(phone);
}

View File

@ -87,7 +87,6 @@ public class OCSMSOwnCloudClient {
return null;
}
Log.d(TAG, obj.toString());
try {
return obj.getJSONArray("phoneList");
} catch (final JSONException e) {