Started to rework contact editor
This commit is contained in:
parent
4d976609bf
commit
2a4a003780
3 changed files with 87 additions and 136 deletions
|
@ -21,19 +21,16 @@ package org.linphone;
|
|||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.SyncFailedException;
|
||||
import java.io.Serializable;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import org.linphone.compatibility.Compatibility;
|
||||
import org.linphone.core.LinphoneProxyConfig;
|
||||
import org.linphone.mediastream.Log;
|
||||
|
||||
import org.linphone.mediastream.Version;
|
||||
|
||||
import android.annotation.SuppressLint;
|
||||
import android.app.Activity;
|
||||
import android.app.Dialog;
|
||||
import android.content.ContentProviderOperation;
|
||||
import android.content.ContentResolver;
|
||||
import android.app.Fragment;
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.database.Cursor;
|
||||
|
@ -49,8 +46,6 @@ import android.net.Uri;
|
|||
import android.os.Bundle;
|
||||
import android.os.Environment;
|
||||
import android.os.Parcelable;
|
||||
import android.provider.ContactsContract;
|
||||
import android.app.Fragment;
|
||||
import android.provider.MediaStore;
|
||||
import android.support.v4.content.CursorLoader;
|
||||
import android.text.Editable;
|
||||
|
@ -78,28 +73,24 @@ public class ContactEditorFragment extends Fragment {
|
|||
|
||||
private boolean isNewContact = true;
|
||||
private LinphoneContact contact;
|
||||
private int contactID;
|
||||
//private List<NewOrUpdatedNumberOrAddress> numbersAndAddresses;
|
||||
private ArrayList<ContentProviderOperation> ops;
|
||||
private List<NewOrUpdatedNumberOrAddress> numbersAndAddresses;
|
||||
private int firstSipAddressIndex = -1;
|
||||
private LinearLayout sipAddresses, numbers;
|
||||
private String newSipOrNumberToAdd;
|
||||
private ContactsManager contactsManager;
|
||||
private Uri imageToUploadUri;
|
||||
private String fileToUploadPath;
|
||||
private Bitmap imageToUpload;
|
||||
byte[] photoToAdd;
|
||||
private byte[] photoToAdd;
|
||||
|
||||
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
|
||||
this.inflater = inflater;
|
||||
|
||||
/*contact = null;
|
||||
contact = null;
|
||||
if (getArguments() != null) {
|
||||
if (getArguments().getSerializable("Contact") != null) {
|
||||
contact = (LinphoneContact) getArguments().getSerializable("Contact");
|
||||
Serializable obj = getArguments().getSerializable("Contact");
|
||||
if (obj != null) {
|
||||
contact = (LinphoneContact) obj;
|
||||
isNewContact = false;
|
||||
contactID = Integer.parseInt(contact.getID());
|
||||
contact.refresh(getActivity().getContentResolver());
|
||||
if (getArguments().getString("NewSipAdress") != null) {
|
||||
newSipOrNumberToAdd = getArguments().getString("NewSipAdress");
|
||||
}
|
||||
|
@ -110,8 +101,6 @@ public class ContactEditorFragment extends Fragment {
|
|||
}
|
||||
}
|
||||
|
||||
contactsManager = ContactsManager.getInstance();
|
||||
|
||||
view = inflater.inflate(R.layout.contact_edit, container, false);
|
||||
|
||||
phoneNumbersSection = (LinearLayout) view.findViewById(R.id.phone_numbers);
|
||||
|
@ -151,10 +140,10 @@ public class ContactEditorFragment extends Fragment {
|
|||
getFragmentManager().popBackStackImmediate();
|
||||
return;
|
||||
}
|
||||
contactsManager.createNewContact(ops, firstName.getText().toString(), lastName.getText().toString());
|
||||
//contactsManager.createNewContact(ops, firstName.getText().toString(), lastName.getText().toString());
|
||||
setContactPhoto();
|
||||
} else {
|
||||
contactsManager.updateExistingContact(ops, contact, firstName.getText().toString(), lastName.getText().toString());
|
||||
//contactsManager.updateExistingContact(ops, contact, firstName.getText().toString(), lastName.getText().toString());
|
||||
setContactPhoto();
|
||||
}
|
||||
|
||||
|
@ -162,32 +151,23 @@ public class ContactEditorFragment extends Fragment {
|
|||
numberOrAddress.save();
|
||||
}
|
||||
|
||||
try {
|
||||
/*try {
|
||||
getActivity().getContentResolver().applyBatch(ContactsContract.AUTHORITY, ops);
|
||||
addLinphoneFriendIfNeeded();
|
||||
removeLinphoneTagIfNeeded();
|
||||
contactsManager.prepareContactsInBackground();
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}*/
|
||||
|
||||
if(isNewContact) {
|
||||
getFragmentManager().popBackStackImmediate();
|
||||
} else {
|
||||
if(!isNewContact) {
|
||||
if (LinphoneActivity.instance().getResources().getBoolean(R.bool.isTablet)) {
|
||||
if(ContactsListFragment.isInstanciated()) {
|
||||
ContactsListFragment.instance().invalidate();
|
||||
}
|
||||
getFragmentManager().popBackStackImmediate();
|
||||
} else {
|
||||
LinphoneContact updatedContact = contactsManager.findContactWithDisplayName(contactsManager.getDisplayName(firstName.getText().toString(), lastName.getText().toString()));
|
||||
if (updatedContact != null) {
|
||||
LinphoneActivity.instance().displayContact(updatedContact, false);
|
||||
} else {
|
||||
LinphoneActivity.instance().displayContacts(false);
|
||||
}
|
||||
}
|
||||
}
|
||||
getFragmentManager().popBackStackImmediate();
|
||||
}
|
||||
});
|
||||
|
||||
|
@ -242,8 +222,8 @@ public class ContactEditorFragment extends Fragment {
|
|||
});
|
||||
|
||||
if (!isNewContact) {
|
||||
String fn = findContactFirstName(String.valueOf(contactID));
|
||||
String ln = findContactLastName(String.valueOf(contactID));
|
||||
String fn = contact.getFirstName();
|
||||
String ln = contact.getLastName();
|
||||
if (fn != null || ln != null) {
|
||||
firstName.setText(fn);
|
||||
lastName.setText(ln);
|
||||
|
@ -282,12 +262,7 @@ public class ContactEditorFragment extends Fragment {
|
|||
}
|
||||
|
||||
contactPicture = (ImageView) view.findViewById(R.id.contact_picture);
|
||||
if (contact != null && contact.getPhotoUri() != null) {
|
||||
InputStream input = Compatibility.getContactPictureInputStream(getActivity().getContentResolver(), contact.getID());
|
||||
contactPicture.setImageBitmap(BitmapFactory.decodeStream(input));
|
||||
} else {
|
||||
contactPicture.setImageResource(R.drawable.avatar);
|
||||
}
|
||||
LinphoneUtils.setImagePictureFromUri(getActivity(), contactPicture, contact.getPhotoUri(), contact.getThumbnailUri());
|
||||
|
||||
contactPicture.setOnClickListener(new OnClickListener() {
|
||||
@Override
|
||||
|
@ -316,8 +291,7 @@ public class ContactEditorFragment extends Fragment {
|
|||
}
|
||||
});
|
||||
|
||||
ops = new ArrayList<ContentProviderOperation>();
|
||||
lastName.requestFocus();*/
|
||||
lastName.requestFocus();
|
||||
|
||||
return view;
|
||||
}
|
||||
|
@ -346,7 +320,7 @@ public class ContactEditorFragment extends Fragment {
|
|||
super.onPause();
|
||||
}
|
||||
|
||||
/*private void pickImage() {
|
||||
private void pickImage() {
|
||||
imageToUploadUri = null;
|
||||
final List<Intent> cameraIntents = new ArrayList<Intent>();
|
||||
final Intent captureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
|
||||
|
@ -480,8 +454,8 @@ public class ContactEditorFragment extends Fragment {
|
|||
}
|
||||
|
||||
|
||||
private void setContactPhoto(){
|
||||
ContentResolver cr = getActivity().getContentResolver();
|
||||
private void setContactPhoto() {
|
||||
/*ContentResolver cr = getActivity().getContentResolver();
|
||||
Uri updateUri = ContactsContract.Data.CONTENT_URI;
|
||||
|
||||
if(photoToAdd != null){
|
||||
|
@ -495,7 +469,7 @@ public class ContactEditorFragment extends Fragment {
|
|||
);
|
||||
} else { //update contact picture
|
||||
String w = ContactsContract.Data.CONTACT_ID + "='"
|
||||
+ contact.getID() + "' AND "
|
||||
+ contact.getAndroidId() + "' AND "
|
||||
+ ContactsContract.Data.MIMETYPE + " = '"
|
||||
+ ContactsContract.CommonDataKinds.Photo.CONTENT_ITEM_TYPE + "'";
|
||||
|
||||
|
@ -531,18 +505,17 @@ public class ContactEditorFragment extends Fragment {
|
|||
queryCursor.close();
|
||||
}
|
||||
}
|
||||
}
|
||||
}*/
|
||||
}
|
||||
|
||||
private LinearLayout initNumbersFields(final Contact contact) {
|
||||
private LinearLayout initNumbersFields(final LinphoneContact contact) {
|
||||
LinearLayout controls = (LinearLayout) view.findViewById(R.id.controls_numbers);
|
||||
controls.removeAllViews();
|
||||
|
||||
if (contact != null) {
|
||||
for (String numberOrAddress : contact.getNumbersOrAddresses()) {
|
||||
boolean isSip = LinphoneUtils.isStrictSipAddress(numberOrAddress) || !LinphoneUtils.isNumberAddress(numberOrAddress);
|
||||
if(!isSip) {
|
||||
View view = displayNumberOrAddress(controls, numberOrAddress);
|
||||
for (LinphoneNumberOrAddress numberOrAddress : contact.getNumbersOrAddresses()) {
|
||||
if (!numberOrAddress.isSIPAddress()) {
|
||||
View view = displayNumberOrAddress(controls, numberOrAddress.getValue());
|
||||
if (view != null)
|
||||
controls.addView(view);
|
||||
}
|
||||
|
@ -565,15 +538,14 @@ public class ContactEditorFragment extends Fragment {
|
|||
return controls;
|
||||
}
|
||||
|
||||
private LinearLayout initSipAddressFields(final Contact contact) {
|
||||
private LinearLayout initSipAddressFields(final LinphoneContact contact) {
|
||||
LinearLayout controls = (LinearLayout) view.findViewById(R.id.controls_sip_address);
|
||||
controls.removeAllViews();
|
||||
|
||||
if (contact != null) {
|
||||
for (String numberOrAddress : contact.getNumbersOrAddresses()) {
|
||||
boolean isSip = LinphoneUtils.isStrictSipAddress(numberOrAddress) || !LinphoneUtils.isNumberAddress(numberOrAddress);
|
||||
if(isSip) {
|
||||
View view = displayNumberOrAddress(controls, numberOrAddress);
|
||||
for (LinphoneNumberOrAddress numberOrAddress : contact.getNumbersOrAddresses()) {
|
||||
if (numberOrAddress.isSIPAddress()) {
|
||||
View view = displayNumberOrAddress(controls, numberOrAddress.getValue());
|
||||
if (view != null)
|
||||
controls.addView(view);
|
||||
}
|
||||
|
@ -582,7 +554,7 @@ public class ContactEditorFragment extends Fragment {
|
|||
|
||||
if (newSipOrNumberToAdd != null) {
|
||||
boolean isSip = LinphoneUtils.isStrictSipAddress(newSipOrNumberToAdd) || !LinphoneUtils.isNumberAddress(newSipOrNumberToAdd);
|
||||
if(isSip) {
|
||||
if (isSip) {
|
||||
View view = displayNumberOrAddress(controls, newSipOrNumberToAdd);
|
||||
if (view != null)
|
||||
controls.addView(view);
|
||||
|
@ -705,40 +677,8 @@ public class ContactEditorFragment extends Fragment {
|
|||
|
||||
controls.addView(view);
|
||||
}
|
||||
|
||||
private String findContactFirstName(String contactID) {
|
||||
Cursor c = getActivity().getContentResolver().query(ContactsContract.Data.CONTENT_URI,
|
||||
new String[]{ContactsContract.CommonDataKinds.StructuredName.GIVEN_NAME},
|
||||
ContactsContract.Data.CONTACT_ID + "=? AND " + ContactsContract.Data.MIMETYPE + "=?",
|
||||
new String[]{contactID, ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE}, null);
|
||||
if (c != null) {
|
||||
String result = null;
|
||||
if (c.moveToFirst()) {
|
||||
result = c.getString(c.getColumnIndex(ContactsContract.CommonDataKinds.StructuredName.GIVEN_NAME));
|
||||
}
|
||||
c.close();
|
||||
return result;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
private String findContactLastName(String contactID) {
|
||||
Cursor c = getActivity().getContentResolver().query(ContactsContract.Data.CONTENT_URI,
|
||||
new String[]{ContactsContract.CommonDataKinds.StructuredName.FAMILY_NAME},
|
||||
ContactsContract.Data.CONTACT_ID + "=? AND " + ContactsContract.Data.MIMETYPE + "=?",
|
||||
new String[]{contactID, ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE}, null);
|
||||
if (c != null) {
|
||||
String result = null;
|
||||
if (c.moveToFirst()) {
|
||||
result = c.getString(c.getColumnIndex(ContactsContract.CommonDataKinds.StructuredName.FAMILY_NAME));
|
||||
}
|
||||
c.close();
|
||||
return result;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
private void addLinphoneFriendIfNeeded(){
|
||||
/*private void addLinphoneFriendIfNeeded(){
|
||||
for (NewOrUpdatedNumberOrAddress numberOrAddress : numbersAndAddresses) {
|
||||
if(numberOrAddress.newNumberOrAddress != null && numberOrAddress.isSipAddress) {
|
||||
if(isNewContact){
|
||||
|
@ -758,9 +698,9 @@ public class ContactEditorFragment extends Fragment {
|
|||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}*/
|
||||
|
||||
private void removeLinphoneTagIfNeeded(){
|
||||
/*private void removeLinphoneTagIfNeeded(){
|
||||
if(!isNewContact) {
|
||||
boolean areAllSipFielsEmpty = true;
|
||||
for (NewOrUpdatedNumberOrAddress nounoa : numbersAndAddresses) {
|
||||
|
@ -773,7 +713,7 @@ public class ContactEditorFragment extends Fragment {
|
|||
contactsManager.removeLinphoneContactTag(contact);
|
||||
}
|
||||
}
|
||||
}
|
||||
}*/
|
||||
|
||||
class NewOrUpdatedNumberOrAddress {
|
||||
private String oldNumberOrAddress;
|
||||
|
@ -822,32 +762,10 @@ public class ContactEditorFragment extends Fragment {
|
|||
}
|
||||
|
||||
public void delete() {
|
||||
if(contact != null) {
|
||||
if (isSipAddress) {
|
||||
if (contact.hasFriends()) {
|
||||
ContactsManager.getInstance().removeFriend(oldNumberOrAddress);
|
||||
} else {
|
||||
Compatibility.deleteSipAddressFromContact(ops, oldNumberOrAddress, String.valueOf(contactID));
|
||||
}
|
||||
if (getResources().getBoolean(R.bool.use_linphone_tag)) {
|
||||
Compatibility.deleteLinphoneContactTag(ops, oldNumberOrAddress, contactsManager.findRawLinphoneContactID(String.valueOf(contactID)));
|
||||
}
|
||||
} else {
|
||||
String select = ContactsContract.Data.CONTACT_ID + "=? AND "
|
||||
+ ContactsContract.Data.MIMETYPE + "='" + ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE + "' AND "
|
||||
+ ContactsContract.CommonDataKinds.Phone.NUMBER + "=?";
|
||||
String[] args = new String[]{String.valueOf(contactID), oldNumberOrAddress};
|
||||
|
||||
ops.add(ContentProviderOperation.newDelete(ContactsContract.Data.CONTENT_URI)
|
||||
.withSelection(select, args)
|
||||
.build()
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void addNewNumber() {
|
||||
if (newNumberOrAddress == null || newNumberOrAddress.length() == 0) {
|
||||
/*if (newNumberOrAddress == null || newNumberOrAddress.length() == 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
|
@ -904,11 +822,11 @@ public class ContactEditorFragment extends Fragment {
|
|||
.build()
|
||||
);
|
||||
}
|
||||
}
|
||||
}*/
|
||||
}
|
||||
|
||||
private void updateNumber() {
|
||||
if (newNumberOrAddress == null || newNumberOrAddress.length() == 0) {
|
||||
/*if (newNumberOrAddress == null || newNumberOrAddress.length() == 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
|
@ -940,7 +858,7 @@ public class ContactEditorFragment extends Fragment {
|
|||
.withValue(ContactsContract.CommonDataKinds.Phone.NUMBER, newNumberOrAddress)
|
||||
.build()
|
||||
);
|
||||
}
|
||||
}*/
|
||||
}
|
||||
}*/
|
||||
}
|
||||
}
|
|
@ -88,6 +88,7 @@ public class ContactsManager extends ContentObserver {
|
|||
|
||||
@Override
|
||||
public void onChange(boolean selfChange, Uri uri) {
|
||||
Log.e("############################################ OnChange ############################################");
|
||||
List<LinphoneContact> contacts = fetchContactsAsync();
|
||||
Message msg = handler.obtainMessage();
|
||||
msg.what = CONTACTS_UPDATED;
|
||||
|
@ -104,15 +105,15 @@ public class ContactsManager extends ContentObserver {
|
|||
return instance;
|
||||
}
|
||||
|
||||
public boolean hasContacts() {
|
||||
public synchronized boolean hasContacts() {
|
||||
return contacts.size() > 0;
|
||||
}
|
||||
|
||||
public List<LinphoneContact> getContacts() {
|
||||
public synchronized List<LinphoneContact> getContacts() {
|
||||
return contacts;
|
||||
}
|
||||
|
||||
public List<LinphoneContact> getSIPContacts() {
|
||||
public synchronized List<LinphoneContact> getSIPContacts() {
|
||||
return contacts;
|
||||
}
|
||||
|
||||
|
|
|
@ -41,7 +41,7 @@ public class LinphoneContact implements Serializable {
|
|||
private static final long serialVersionUID = 9015568163905205244L;
|
||||
|
||||
private transient LinphoneFriend friend;
|
||||
private String fullName, androidId;
|
||||
private String fullName, firstName, lastName, androidId;
|
||||
private transient Uri photoUri, thumbnailUri;
|
||||
private List<LinphoneNumberOrAddress> addresses;
|
||||
|
||||
|
@ -65,6 +65,14 @@ public class LinphoneContact implements Serializable {
|
|||
return fullName;
|
||||
}
|
||||
|
||||
public String getFirstName() {
|
||||
return firstName;
|
||||
}
|
||||
|
||||
public String getLastName() {
|
||||
return lastName;
|
||||
}
|
||||
|
||||
public boolean hasPhoto() {
|
||||
return photoUri != null;
|
||||
}
|
||||
|
@ -162,13 +170,11 @@ public class LinphoneContact implements Serializable {
|
|||
photoUri = null;
|
||||
} else {
|
||||
String id = getAndroidId();
|
||||
String name = getName(id);
|
||||
Uri thumbnail = getContactPictureUri(id);
|
||||
Uri photo = getContactPhotoUri(id);
|
||||
|
||||
setFullName(name);
|
||||
setThumbnailUri(thumbnail);
|
||||
setPhotoUri(photo);
|
||||
setFullName(getName(id));
|
||||
setThumbnailUri(getContactPictureUri(id));
|
||||
setPhotoUri(getContactPhotoUri(id));
|
||||
lastName = getContactLastName(id);
|
||||
firstName = getContactFirstName(id);
|
||||
for (LinphoneNumberOrAddress noa : getAddressesAndNumbersForAndroidContact(id)) {
|
||||
addresses.add(noa);
|
||||
}
|
||||
|
@ -208,6 +214,32 @@ public class LinphoneContact implements Serializable {
|
|||
Uri person = ContentUris.withAppendedId(ContactsContract.Contacts.CONTENT_URI, Long.parseLong(id));
|
||||
return Uri.withAppendedPath(person, ContactsContract.Contacts.Photo.DISPLAY_PHOTO);
|
||||
}
|
||||
|
||||
private String getContactFirstName(String id) {
|
||||
String result = null;
|
||||
ContentResolver resolver = ContactsManager.getInstance().getContentResolver();
|
||||
Cursor c = resolver.query(ContactsContract.Data.CONTENT_URI, new String[]{ ContactsContract.CommonDataKinds.StructuredName.GIVEN_NAME }, ContactsContract.Data.CONTACT_ID + "=? AND " + ContactsContract.Data.MIMETYPE + "=?", new String[]{ id, ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE }, null);
|
||||
if (c != null) {
|
||||
if (c.moveToFirst()) {
|
||||
result = c.getString(c.getColumnIndex(ContactsContract.CommonDataKinds.StructuredName.GIVEN_NAME));
|
||||
}
|
||||
c.close();
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
private String getContactLastName(String id) {
|
||||
String result = null;
|
||||
ContentResolver resolver = ContactsManager.getInstance().getContentResolver();
|
||||
Cursor c = resolver.query(ContactsContract.Data.CONTENT_URI, new String[]{ ContactsContract.CommonDataKinds.StructuredName.FAMILY_NAME }, ContactsContract.Data.CONTACT_ID + "=? AND " + ContactsContract.Data.MIMETYPE + "=?", new String[]{ id, ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE }, null);
|
||||
if (c != null) {
|
||||
if (c.moveToFirst()) {
|
||||
result = c.getString(c.getColumnIndex(ContactsContract.CommonDataKinds.StructuredName.FAMILY_NAME));
|
||||
}
|
||||
c.close();
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
private String getName(String id) {
|
||||
ContentResolver resolver = ContactsManager.getInstance().getContentResolver();
|
||||
|
|
Loading…
Reference in a new issue