mirror of
https://github.com/nerzhul/ownCloud-SMS-App.git
synced 2025-06-20 22:36:20 +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:
parent
b937c4ebdd
commit
f7dcb8e51c
@ -29,6 +29,9 @@
|
|||||||
<uses-permission android:name="android.permission.MANAGE_ACCOUNTS" />
|
<uses-permission android:name="android.permission.MANAGE_ACCOUNTS" />
|
||||||
<uses-permission android:name="android.permission.USE_CREDENTIALS" />
|
<uses-permission android:name="android.permission.USE_CREDENTIALS" />
|
||||||
|
|
||||||
|
<!-- For backup restauration -->
|
||||||
|
<uses-permission android:name="android.permission.READ_CONTACTS" />
|
||||||
|
|
||||||
<application
|
<application
|
||||||
android:allowBackup="true"
|
android:allowBackup="true"
|
||||||
android:icon="@drawable/ic_launcher"
|
android:icon="@drawable/ic_launcher"
|
||||||
|
@ -32,6 +32,10 @@ public class ContactListAdapter extends ArrayAdapter<String> {
|
|||||||
v = inflater.inflate(_itemLayout, null);
|
v = inflater.inflate(_itemLayout, null);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (_objects.size() == 0) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
final String element = _objects.get(position);
|
final String element = _objects.get(position);
|
||||||
|
|
||||||
if (element != null) {
|
if (element != null) {
|
||||||
|
@ -2,9 +2,12 @@ package fr.unix_experience.owncloud_sms.engine;
|
|||||||
|
|
||||||
import android.accounts.Account;
|
import android.accounts.Account;
|
||||||
import android.accounts.AccountManager;
|
import android.accounts.AccountManager;
|
||||||
|
import android.content.ContentResolver;
|
||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
|
import android.database.Cursor;
|
||||||
import android.net.Uri;
|
import android.net.Uri;
|
||||||
import android.os.AsyncTask;
|
import android.os.AsyncTask;
|
||||||
|
import android.provider.ContactsContract;
|
||||||
|
|
||||||
import org.json.JSONArray;
|
import org.json.JSONArray;
|
||||||
import org.json.JSONException;
|
import org.json.JSONException;
|
||||||
@ -58,9 +61,48 @@ public interface ASyncContactLoad {
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
ArrayList<String> serverPhoneList = new ArrayList<>();
|
||||||
|
|
||||||
JSONArray phoneNumbers = _client.getServerPhoneNumbers();
|
JSONArray phoneNumbers = _client.getServerPhoneNumbers();
|
||||||
for (int i = 0; i < phoneNumbers.length(); i++) {
|
for (int i = 0; i < phoneNumbers.length(); i++) {
|
||||||
String phone = phoneNumbers.getString(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);
|
_objects.add(phone);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -87,7 +87,6 @@ public class OCSMSOwnCloudClient {
|
|||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
Log.d(TAG, obj.toString());
|
|
||||||
try {
|
try {
|
||||||
return obj.getJSONArray("phoneList");
|
return obj.getJSONArray("phoneList");
|
||||||
} catch (final JSONException e) {
|
} catch (final JSONException e) {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user