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

@ -127,6 +127,15 @@
android:fastScrollEnabled="true" android:fastScrollEnabled="true"
android:fastScrollAlwaysVisible="true" android:fastScrollAlwaysVisible="true"
android:dividerHeight="1dp" /> 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 <TextView
android:id="@+id/noSipContact" android:id="@+id/noSipContact"

View file

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

View file

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

View file

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

View file

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