More fixes & improvements regarding contacts
This commit is contained in:
parent
79d0f17c09
commit
5480a829ad
6 changed files with 104 additions and 56 deletions
|
@ -205,13 +205,7 @@ public class LinphoneActivity extends LinphoneGenericActivity
|
|||
Manifest.permission.WRITE_SYNC_SETTINGS, getPackageName())
|
||||
!= PackageManager.PERMISSION_GRANTED) {
|
||||
checkSyncPermission();
|
||||
} else {
|
||||
if (LinphoneService.isReady())
|
||||
ContactsManager.getInstance().initializeSyncAccount(this);
|
||||
}
|
||||
} else {
|
||||
if (LinphoneService.isReady() && !ContactsManager.getInstance().contactsFetchedOnce())
|
||||
ContactsManager.getInstance().initializeContactManager(this);
|
||||
}
|
||||
|
||||
setContentView(R.layout.main);
|
||||
|
@ -1360,15 +1354,13 @@ public class LinphoneActivity extends LinphoneGenericActivity
|
|||
readContactsI = i;
|
||||
}
|
||||
|
||||
if (readContactsI >= 0
|
||||
&& grantResults[readContactsI] == PackageManager.PERMISSION_GRANTED) {
|
||||
ContactsManager.getInstance().enableContactsAccess();
|
||||
}
|
||||
switch (requestCode) {
|
||||
case PERMISSIONS_REQUEST_SYNC:
|
||||
if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
|
||||
ContactsManager.getInstance().initializeSyncAccount(this);
|
||||
} else {
|
||||
if (!ContactsManager.getInstance().contactsFetchedOnce()) {
|
||||
ContactsManager.getInstance().initializeContactManager(this);
|
||||
}
|
||||
}
|
||||
break;
|
||||
case PERMISSIONS_RECORD_AUDIO_ECHO_CANCELLER:
|
||||
if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
|
||||
|
@ -1388,13 +1380,6 @@ public class LinphoneActivity extends LinphoneGenericActivity
|
|||
((SettingsFragment) mFragment).startEchoTester();
|
||||
break;
|
||||
}
|
||||
if (readContactsI >= 0
|
||||
&& grantResults[readContactsI] == PackageManager.PERMISSION_GRANTED) {
|
||||
ContactsManager.getInstance().enableContactsAccess();
|
||||
if (!ContactsManager.getInstance().contactsFetchedOnce()) {
|
||||
ContactsManager.getInstance().initializeContactManager(this);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -1474,10 +1459,7 @@ public class LinphoneActivity extends LinphoneGenericActivity
|
|||
permissionsList.add(Manifest.permission.READ_CONTACTS);
|
||||
}
|
||||
} else {
|
||||
if (!ContactsManager.getInstance().contactsFetchedOnce()) {
|
||||
ContactsManager.getInstance().enableContactsAccess();
|
||||
ContactsManager.getInstance().fetchContactsAsync();
|
||||
}
|
||||
}
|
||||
|
||||
if (permissionsList.size() > 0) {
|
||||
|
@ -1486,6 +1468,7 @@ public class LinphoneActivity extends LinphoneGenericActivity
|
|||
ActivityCompat.requestPermissions(
|
||||
this, permissions, PERMISSIONS_READ_EXTERNAL_STORAGE_DEVICE_RINGTONE);
|
||||
}
|
||||
ContactsManager.getInstance().initializeContactManager(this);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
|
@ -579,7 +579,7 @@ class AndroidContact implements Serializable {
|
|||
String linphoneRawId = findLinphoneRawContactId();
|
||||
if (linphoneRawId == null) {
|
||||
Log.i("[Contact] Linphone RAW ID not found for contact " + mAndroidId);
|
||||
createRawLinphoneContactFromExistingAndroidContact(fullName);
|
||||
createRawLinphoneContactFromExistingAndroidContact();
|
||||
} else {
|
||||
Log.i(
|
||||
"[Contact] Linphone RAW ID found for contact "
|
||||
|
@ -593,7 +593,7 @@ class AndroidContact implements Serializable {
|
|||
}
|
||||
}
|
||||
|
||||
private void createRawLinphoneContactFromExistingAndroidContact(String fullName) {
|
||||
private void createRawLinphoneContactFromExistingAndroidContact() {
|
||||
addChangesToCommit(
|
||||
ContentProviderOperation.newInsert(ContactsContract.RawContacts.CONTENT_URI)
|
||||
.withValue(
|
||||
|
|
|
@ -42,7 +42,7 @@ import org.linphone.utils.LinphoneUtils;
|
|||
|
||||
class AsyncContactsLoader extends AsyncTask<Void, Void, AsyncContactsLoader.AsyncContactsData> {
|
||||
@SuppressLint("InlinedApi")
|
||||
private static final String[] PROJECTION = {
|
||||
public static final String[] PROJECTION = {
|
||||
ContactsContract.Data.CONTACT_ID,
|
||||
ContactsContract.Contacts.LOOKUP_KEY,
|
||||
ContactsContract.Contacts.DISPLAY_NAME_PRIMARY,
|
||||
|
@ -132,13 +132,6 @@ class AsyncContactsLoader extends AsyncTask<Void, Void, AsyncContactsLoader.Asyn
|
|||
if (isCancelled()) return data;
|
||||
|
||||
String id = c.getString(c.getColumnIndex(ContactsContract.Data.CONTACT_ID));
|
||||
String displayName =
|
||||
c.getString(c.getColumnIndex(ContactsContract.Data.DISPLAY_NAME_PRIMARY));
|
||||
String mime = c.getString(c.getColumnIndex(ContactsContract.Data.MIMETYPE));
|
||||
String data1 = c.getString(c.getColumnIndex("data1"));
|
||||
String data2 = c.getString(c.getColumnIndex("data2"));
|
||||
String data3 = c.getString(c.getColumnIndex("data3"));
|
||||
String data4 = c.getString(c.getColumnIndex("data4"));
|
||||
String lookupKey =
|
||||
c.getString(c.getColumnIndex(ContactsContract.Contacts.LOOKUP_KEY));
|
||||
|
||||
|
@ -148,27 +141,10 @@ class AsyncContactsLoader extends AsyncTask<Void, Void, AsyncContactsLoader.Asyn
|
|||
contact = new LinphoneContact();
|
||||
contact.setAndroidId(id);
|
||||
contact.setAndroidLookupKey(lookupKey);
|
||||
contact.setFullName(displayName);
|
||||
androidContactsCache.put(id, contact);
|
||||
}
|
||||
|
||||
if (contact.getFullName() == null && displayName != null) {
|
||||
contact.setFullName(displayName);
|
||||
}
|
||||
|
||||
if (ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE.equals(mime)) {
|
||||
contact.addNumberOrAddress(new LinphoneNumberOrAddress(data1, data4));
|
||||
} else if (ContactsContract.CommonDataKinds.SipAddress.CONTENT_ITEM_TYPE.equals(
|
||||
mime)
|
||||
|| mContext.getString(R.string.linphone_address_mime_type).equals(mime)) {
|
||||
contact.addNumberOrAddress(new LinphoneNumberOrAddress(data1, true));
|
||||
} else if (ContactsContract.CommonDataKinds.Organization.CONTENT_ITEM_TYPE.equals(
|
||||
mime)) {
|
||||
contact.setOrganization(data1, false);
|
||||
} else if (ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE.equals(
|
||||
mime)) {
|
||||
contact.setFirstNameAndLastName(data2, data3, false);
|
||||
}
|
||||
contact.syncValuesFromAndroidCusor(c);
|
||||
}
|
||||
c.close();
|
||||
|
||||
|
|
|
@ -188,6 +188,14 @@ public class ContactEditorFragment extends Fragment {
|
|||
}
|
||||
|
||||
mContact.save();
|
||||
|
||||
if (mIsNewContact) {
|
||||
// Ensure fetch will be done so the new contact appears in the contacts
|
||||
// list: contacts content observer may not be notified if contacts sync
|
||||
// is disabled at system level
|
||||
ContactsManager.getInstance().fetchContactsAsync();
|
||||
}
|
||||
|
||||
getFragmentManager().popBackStackImmediate();
|
||||
if (mIsNewContact || LinphoneActivity.instance().isTablet()) {
|
||||
LinphoneActivity.instance().displayContact(mContact, false);
|
||||
|
|
|
@ -21,9 +21,9 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|||
|
||||
import static android.os.AsyncTask.THREAD_POOL_EXECUTOR;
|
||||
|
||||
import android.Manifest;
|
||||
import android.accounts.Account;
|
||||
import android.accounts.AccountManager;
|
||||
import android.app.Activity;
|
||||
import android.content.ContentProviderClient;
|
||||
import android.content.ContentProviderOperation;
|
||||
import android.content.ContentResolver;
|
||||
|
@ -67,6 +67,7 @@ public class ContactsManager extends ContentObserver implements FriendListListen
|
|||
private boolean mContactsFetchedOnce = false;
|
||||
private Context mContext;
|
||||
private AsyncContactsLoader mLoadContactTask;
|
||||
private boolean mInitialized = false;
|
||||
|
||||
public static ContactsManager getInstance() {
|
||||
if (sInstance == null) sInstance = new ContactsManager();
|
||||
|
@ -281,6 +282,21 @@ public class ContactsManager extends ContentObserver implements FriendListListen
|
|||
public void initializeContactManager(Context context) {
|
||||
mContext = context;
|
||||
|
||||
if (!mInitialized) {
|
||||
if (mContext.getResources().getBoolean(R.bool.use_linphone_tag)) {
|
||||
if (mContext.getPackageManager()
|
||||
.checkPermission(
|
||||
Manifest.permission.WRITE_SYNC_SETTINGS,
|
||||
mContext.getPackageName())
|
||||
== PackageManager.PERMISSION_GRANTED) {
|
||||
if (LinphoneService.isReady()) {
|
||||
ContactsManager.getInstance().initializeSyncAccount();
|
||||
mInitialized = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (mContext != null && getContacts().size() == 0 && hasContactsAccess()) {
|
||||
fetchContactsAsync();
|
||||
}
|
||||
|
@ -312,10 +328,9 @@ public class ContactsManager extends ContentObserver implements FriendListListen
|
|||
client.close();
|
||||
}
|
||||
|
||||
public void initializeSyncAccount(Activity activity) {
|
||||
initializeContactManager(activity);
|
||||
private void initializeSyncAccount() {
|
||||
AccountManager accountManager =
|
||||
(AccountManager) activity.getSystemService(Context.ACCOUNT_SERVICE);
|
||||
(AccountManager) mContext.getSystemService(Context.ACCOUNT_SERVICE);
|
||||
|
||||
Account[] accounts =
|
||||
accountManager.getAccountsByType(mContext.getString(R.string.sync_account_type));
|
||||
|
@ -332,6 +347,16 @@ public class ContactsManager extends ContentObserver implements FriendListListen
|
|||
} catch (Exception e) {
|
||||
Log.e("[Contacts Manager] Couldn't initialize sync account: " + e);
|
||||
}
|
||||
} else if (accounts != null) {
|
||||
for (int i = 0; i < accounts.length; i++) {
|
||||
Log.i(
|
||||
"[Contacts Manager] Found account with name \""
|
||||
+ accounts[i].name
|
||||
+ "\" and type \""
|
||||
+ accounts[i].type
|
||||
+ "\"");
|
||||
makeContactAccountVisible();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -19,12 +19,17 @@ along with this program; if not, write to the Free Software
|
|||
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
||||
*/
|
||||
|
||||
import android.content.Context;
|
||||
import android.database.Cursor;
|
||||
import android.net.Uri;
|
||||
import android.provider.ContactsContract;
|
||||
import java.io.Serializable;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Locale;
|
||||
import org.linphone.LinphoneActivity;
|
||||
import org.linphone.LinphoneManager;
|
||||
import org.linphone.R;
|
||||
import org.linphone.core.Address;
|
||||
import org.linphone.core.Core;
|
||||
import org.linphone.core.Friend;
|
||||
|
@ -475,8 +480,59 @@ public class LinphoneContact extends AndroidContact
|
|||
}
|
||||
}
|
||||
|
||||
public void syncValuesFromAndroidContact(Context context) {
|
||||
Cursor c =
|
||||
context.getContentResolver()
|
||||
.query(
|
||||
ContactsContract.Data.CONTENT_URI,
|
||||
AsyncContactsLoader.PROJECTION,
|
||||
ContactsContract.Data.IN_VISIBLE_GROUP
|
||||
+ " == 1 AND "
|
||||
+ ContactsContract.Data.CONTACT_ID
|
||||
+ " == "
|
||||
+ mAndroidId,
|
||||
null,
|
||||
null);
|
||||
if (c != null) {
|
||||
mAddresses = new ArrayList<>();
|
||||
while (c.moveToNext()) {
|
||||
syncValuesFromAndroidCusor(c);
|
||||
}
|
||||
c.close();
|
||||
}
|
||||
}
|
||||
|
||||
public void syncValuesFromAndroidCusor(Cursor c) {
|
||||
String displayName =
|
||||
c.getString(c.getColumnIndex(ContactsContract.Data.DISPLAY_NAME_PRIMARY));
|
||||
String mime = c.getString(c.getColumnIndex(ContactsContract.Data.MIMETYPE));
|
||||
String data1 = c.getString(c.getColumnIndex("data1"));
|
||||
String data2 = c.getString(c.getColumnIndex("data2"));
|
||||
String data3 = c.getString(c.getColumnIndex("data3"));
|
||||
String data4 = c.getString(c.getColumnIndex("data4"));
|
||||
String lookupKey = c.getString(c.getColumnIndex(ContactsContract.Contacts.LOOKUP_KEY));
|
||||
|
||||
setAndroidLookupKey(lookupKey);
|
||||
setFullName(displayName);
|
||||
|
||||
if (ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE.equals(mime)) {
|
||||
addNumberOrAddress(new LinphoneNumberOrAddress(data1, data4));
|
||||
} else if (ContactsContract.CommonDataKinds.SipAddress.CONTENT_ITEM_TYPE.equals(mime)
|
||||
|| LinphoneManager.getInstance()
|
||||
.getContext()
|
||||
.getString(R.string.linphone_address_mime_type)
|
||||
.equals(mime)) {
|
||||
addNumberOrAddress(new LinphoneNumberOrAddress(data1, true));
|
||||
} else if (ContactsContract.CommonDataKinds.Organization.CONTENT_ITEM_TYPE.equals(mime)) {
|
||||
setOrganization(data1, false);
|
||||
} else if (ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE.equals(mime)) {
|
||||
setFirstNameAndLastName(data2, data3, false);
|
||||
}
|
||||
}
|
||||
|
||||
public void save() {
|
||||
saveChangesCommited();
|
||||
syncValuesFromAndroidContact(LinphoneActivity.instance());
|
||||
createOrUpdateFriend();
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue