Fetch contacts on AsyncTask

This commit is contained in:
Sylvain Berfini 2016-07-21 13:53:59 +02:00
parent fbac8c01cd
commit 34e6b15e32
5 changed files with 42 additions and 13 deletions

View file

@ -128,6 +128,15 @@
android:fastScrollAlwaysVisible="true"
android:dividerHeight="1dp" />
<ProgressBar
android:id="@+id/contactsFetchInProgress"
style="?android:attr/progressBarStyle"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:visibility="gone"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<TextView
android:id="@+id/noSipContact"
android:text="@string/no_sip_contact"

View file

@ -48,6 +48,7 @@ import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.SectionIndexer;
import android.widget.TextView;
@ -67,6 +68,7 @@ public class ContactsListFragment extends Fragment implements OnClickListener, O
private String sipAddressToAdd;
private ImageView clearSearchField;
private EditText searchField;
private ProgressBar contactsFetchInProgress;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
@ -142,6 +144,8 @@ public class ContactsListFragment extends Fragment implements OnClickListener, O
}
});
contactsFetchInProgress = (ProgressBar) view.findViewById(R.id.contactsFetchInProgress);
return view;
}
@ -339,6 +343,11 @@ public class ContactsListFragment extends Fragment implements OnClickListener, O
edit.setEnabled(true);
}
ContactsManager.getInstance().setLinphoneContactsPrefered(onlyDisplayLinphoneContacts);
if (contactsList.getCount() == 0) {
contactsFetchInProgress.setVisibility(View.VISIBLE);
} else {
contactsFetchInProgress.setVisibility(View.GONE);
}
}
private void changeContactsToggle() {

View file

@ -19,6 +19,7 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
package org.linphone;
import java.net.URL;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
@ -39,6 +40,7 @@ import android.content.Context;
import android.database.ContentObserver;
import android.database.Cursor;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Handler;
import android.os.Message;
import android.provider.ContactsContract;
@ -93,11 +95,7 @@ public class ContactsManager extends ContentObserver {
@Override
public void onChange(boolean selfChange, Uri uri) {
List<LinphoneContact> contacts = fetchContactsAsync();
Message msg = handler.obtainMessage();
msg.what = CONTACTS_UPDATED;
msg.obj = contacts;
handler.sendMessage(msg);
fetchContactsAsync();
}
public ContentResolver getContentResolver() {
@ -224,11 +222,11 @@ public class ContactsManager extends ContentObserver {
}
}
public synchronized void fetchContacts() {
setContacts(fetchContactsAsync());
public synchronized void fetchContactsAsync() {
new ContactsFetchTask().execute();
}
public List<LinphoneContact> fetchContactsAsync() {
public List<LinphoneContact> fetchContactsSync() {
List<LinphoneContact> contacts = new ArrayList<LinphoneContact>();
if (hasContactsAccess()) {
@ -283,9 +281,22 @@ public class ContactsManager extends ContentObserver {
return contacts;
}
private class ContactsFetchTask extends AsyncTask<Void, Void, List<LinphoneContact>> {
protected List<LinphoneContact> doInBackground(Void... params) {
return fetchContactsSync();
}
protected void onPostExecute(List<LinphoneContact> result) {
setContacts(result);
for (ContactsUpdatedListener listener : contactsUpdatedListeners) {
listener.onContactsUpdated();
}
}
}
public static String getAddressOrNumberForAndroidContact(ContentResolver resolver, Uri contactUri) {
// Phone Numbers
String[] projection = new String[]{ ContactsContract.CommonDataKinds.Phone.NUMBER };
String[] projection = new String[] { ContactsContract.CommonDataKinds.Phone.NUMBER };
Cursor c = resolver.query(contactUri, projection, null, null, null);
if (c != null) {
while (c.moveToNext()) {

View file

@ -1264,7 +1264,7 @@ public class LinphoneActivity extends Activity implements OnClickListener, Conta
if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
ContactsManager.getInstance().enableContactsAccess();
}
ContactsManager.getInstance().fetchContacts();
ContactsManager.getInstance().fetchContactsAsync();
fetchedContactsOnce = true;
break;
case PERMISSIONS_RECORD_AUDIO_ECHO_CANCELLER:
@ -1289,7 +1289,7 @@ public class LinphoneActivity extends Activity implements OnClickListener, Conta
if (contacts == PackageManager.PERMISSION_GRANTED && !fetchedContactsOnce) {
ContactsManager.getInstance().enableContactsAccess();
ContactsManager.getInstance().fetchContacts();
ContactsManager.getInstance().fetchContactsAsync();
fetchedContactsOnce = true;
} else {
checkAndRequestReadContactsPermission();

View file

@ -400,7 +400,7 @@ public class LinphoneContact implements Serializable, Comparable<LinphoneContact
if (lc.findFriendByAddress(friend.getAddress().asString()) == null) {
try {
lc.addFriend(friend);
ContactsManager.getInstance().fetchContacts();
ContactsManager.getInstance().fetchContactsAsync();
} catch (LinphoneCoreException e) {
Log.e(e);
}