浏览代码

1.修改Android Apex Mobile联系人。

Pen Li 7 年之前
父节点
当前提交
4a4834e89f

+ 0 - 1
ApexDrivers/RAUtilsLibrary/src/main/java/com/usai/redant/rautils/contactkit/Contact.java

@@ -43,5 +43,4 @@ public class Contact {
     public Integer getID() {
         return ID;
     }
-
 }

+ 17 - 6
ApexDrivers/RAUtilsLibrary/src/main/java/com/usai/redant/rautils/contactkit/ContactHelper.java

@@ -968,10 +968,11 @@ public class ContactHelper {
     /**
      * 新增联系人
      * */
-    public void insertContact(ArrayList<Contact> contacts) {
+    public ArrayList<Uri> insertContact(ArrayList<Contact> contacts) {
 
         if (contacts != null && contacts.size() > 0) {
 
+            ArrayList<Uri> uriArrayList = new ArrayList<>();
             for (Contact contact : contacts) {
 
                 if (contact.getID() == Contact.NEW_CONTACT_ID) {
@@ -1045,7 +1046,13 @@ public class ContactHelper {
                     try {
                         ContentProviderResult[] results = mContentResolver.applyBatch(ContactsContract.AUTHORITY, operations);
                         for (ContentProviderResult result : results) {
+
+                            if (result.uri.toString().contains("raw_contacts") && !uriArrayList.contains(result.uri)) {
+                                uriArrayList.add(result.uri);
+                            }
+
                             Log.i("Insert Contact", result.uri.toString());
+
                         }
                     } catch (Exception e) {
                         e.printStackTrace();
@@ -1054,7 +1061,9 @@ public class ContactHelper {
                 } // if
 
             }  // for
+            return uriArrayList;
         }
+        return null;
     }
 
     /**
@@ -1661,13 +1670,15 @@ public class ContactHelper {
             }
 
             // update note
-            ContentProviderOperation noteOp;
-            if (contact.note.getId().intValue() != BaseElement.NEW_ELEMENT_ID.intValue()) {
+            ContentProviderOperation noteOp = null;
+            if (contact.note != null) {
+                if (contact.note.getId().intValue() != BaseElement.NEW_ELEMENT_ID.intValue()) {
 
-                noteOp = updateNote(contact.note);
-            } else {
+                    noteOp = updateNote(contact.note);
+                } else {
 
-                noteOp = insertNoteForContact(contact.note, contact.getID());
+                    noteOp = insertNoteForContact(contact.note, contact.getID());
+                }
             }
             if (noteOp != null) {
                 operations.add(noteOp);

+ 12 - 5
ApexDrivers/RAUtilsLibrary/src/main/java/com/usai/redant/rautils/contactkit/ContactUIHelper.java

@@ -415,14 +415,21 @@ public class ContactUIHelper {
         if (contact != null) {
 
             Uri uri = ContactHelper.sharedHelper(context).getUriOfContact(contact);
-            if (uri != null) {
-                Intent editIntent = new Intent(Intent.ACTION_EDIT);
-                editIntent.setDataAndType(uri,ContactsContract.Contacts.CONTENT_ITEM_TYPE);
-                context.startActivity(editIntent);
-            }
+            startActivityForEditContact(context, uri);
         }
     }
 
+    public static void startActivityForEditContact(Context context, Uri uri) {
+
+        if (uri != null) {
+            Intent editIntent = new Intent(Intent.ACTION_EDIT);
+            editIntent.setDataAndType(uri,ContactsContract.Contacts.CONTENT_ITEM_TYPE);
+            context.startActivity(editIntent);
+        }
+    }
+
+
+
     // endregion
 
 

+ 13 - 0
ApexDrivers/RAUtilsLibrary/src/main/java/com/usai/redant/rautils/contactkit/element/Photo.java

@@ -1,7 +1,10 @@
 package com.usai.redant.rautils.contactkit.element;
 
+import android.graphics.Bitmap;
 import android.net.Uri;
 
+import java.io.ByteArrayOutputStream;
+
 /**
  * 修改Contact Photo时,应当修改thumbnail的值
  * */
@@ -13,4 +16,14 @@ public class Photo extends BaseElement {
     public Photo(Integer id) {
         super(id);
     }
+
+    public static byte[] bytesFromBitmap(Bitmap bitmap) {
+        if (bitmap != null) {
+            ByteArrayOutputStream baos = new ByteArrayOutputStream();
+            bitmap.compress(Bitmap.CompressFormat.PNG, 100, baos);
+            byte[] data = baos.toByteArray();
+            return data;
+        }
+        return null;
+    }
 }

+ 9 - 9
ApexDrivers/apexmobile/src/main/AndroidManifest.xml

@@ -125,15 +125,15 @@
         <!-- debug key -->
 
 
-        <!-- <meta-data -->
-        <!-- android:name="com.google.android.maps.v2.API_KEY" -->
-        <!-- android:value="AIzaSyD6Snyg2SDUGtkC3sOAr979__IDCZnGGuU" /> -->
-        <!-- <meta-data -->
-        <!-- android:name="com.baidu.lbsapi.API_KEY" -->
-        <!-- android:value="tznWFxd3RvSoul1TGQp6GSzo" /> -->
-        <!-- <meta-data -->
-        <!-- android:name="com.google.android.gms.version" -->
-        <!-- android:value="@integer/google_play_services_version" /> -->
+         <!--<meta-data-->
+         <!--android:name="com.google.android.maps.v2.API_KEY"-->
+         <!--android:value="AIzaSyD6Snyg2SDUGtkC3sOAr979__IDCZnGGuU" />-->
+         <!--<meta-data-->
+         <!--android:name="com.baidu.lbsapi.API_KEY"-->
+         <!--android:value="tznWFxd3RvSoul1TGQp6GSzo" />-->
+         <!--<meta-data-->
+         <!--android:name="com.google.android.gms.version"-->
+         <!--android:value="@integer/google_play_services_version" />-->
 
         <activity
             android:name=".DetailActivity"

+ 122 - 35
ApexDrivers/apexmobile/src/main/java/com/usai/apex/InnerMapActivity.java

@@ -23,8 +23,20 @@ import android.view.View;
 import android.widget.TextView;
 
 import com.baidu.mapapi.map.SupportMapFragment;
-import com.usai.Contacts.Contact;
-import com.usai.Contacts.ContactsManager;
+import com.usai.redant.rautils.contactkit.Contact;
+import com.usai.redant.rautils.contactkit.ContactHelper;
+import com.usai.redant.rautils.contactkit.ContactUIHelper;
+import com.usai.redant.rautils.contactkit.element.BaseElement;
+import com.usai.redant.rautils.contactkit.element.EmailAddress;
+import com.usai.redant.rautils.contactkit.element.Name;
+import com.usai.redant.rautils.contactkit.element.PhoneNumber;
+import com.usai.redant.rautils.contactkit.element.Photo;
+import com.usai.redant.rautils.contactkit.element.PostalAddress;
+import com.usai.redant.rautils.contactkit.element.WebSite;
+
+import java.io.ByteArrayOutputStream;
+import java.util.ArrayList;
+
 
 //import com.baidu.mapapi.MKGeneralListener;
 //import com.baidu.mapapi.map.MKEvent;
@@ -38,7 +50,8 @@ public class InnerMapActivity extends AppCompatActivity implements BaiduMapFragm
 	private static final String	LTAG					= "test";
 	SupportMapFragment			map;
 
-	boolean						m_bhasgoogleframework	= true;  
+	boolean						m_bhasgoogleframework	= true;
+	private InnerMapActivity self = this;
 
 //	static class MyGeneralListener implements MKGeneralListener
 //	{
@@ -166,7 +179,7 @@ public class InnerMapActivity extends AppCompatActivity implements BaiduMapFragm
 		}
 		// setUpMapIfNeeded();
 
-//		m_bhasgoogleframework = false;
+		m_bhasgoogleframework = false;
 
 
 
@@ -247,63 +260,137 @@ public class InnerMapActivity extends AppCompatActivity implements BaiduMapFragm
 		}
 		String homePage = "http://www.apexshipping.com";
 		Bitmap photo = BitmapFactory.decodeResource(getResources(),R.drawable.ic_launcher);
+		byte[] data = Photo.bytesFromBitmap(photo);
 
-		final Contact contact = new Contact();
-		contact.setName(detail.Name);
-		contact.setAddress(detail.Addr);
-		contact.setNumber(detail.Tel);
-		contact.setFax(detail.Fax);
-		contact.setEmail(detail.Email);
-		contact.setHomePage(homePage);
-		contact.setPhoto(photo);
+		String name = detail.Name;
+		String addr = detail.Addr;
+		String tel = detail.Tel;
+		String fax = detail.Fax;
+		String email = detail.Email;
 
-		final ContactsManager cm = new ContactsManager(this.getContentResolver());
-		if (cm.contactExist(contact.getName())) {
+		final Contact contact = new Contact();
+		// name
+		Name cName = new Name(BaseElement.NEW_ELEMENT_ID);
+		cName.givenName = name;
+		contact.name = cName;
+
+		// photo
+		Photo cPhoto = new Photo(BaseElement.NEW_ELEMENT_ID);
+		cPhoto.thumbnail = data;
+		contact.photo = cPhoto;
+
+		// phone
+		PhoneNumber phoneNumber = new PhoneNumber(BaseElement.NEW_ELEMENT_ID);
+		phoneNumber.type = PhoneNumber.PhoneNumberType.TYPE_WORK;
+		phoneNumber.value = tel;
+
+		// fax
+		PhoneNumber faxNumber = new PhoneNumber(BaseElement.NEW_ELEMENT_ID);
+		faxNumber.type = PhoneNumber.PhoneNumberType.TYPE_FAX_WORK;
+		faxNumber.value = fax;
+
+		ArrayList<PhoneNumber> phoneNumbers = new ArrayList<>();
+		phoneNumbers.add(phoneNumber);
+		phoneNumbers.add(faxNumber);
+		contact.phoneNumbers = phoneNumbers;
+
+		// addr
+		PostalAddress postalAddress = new PostalAddress(BaseElement.NEW_ELEMENT_ID);
+		postalAddress.type = PostalAddress.AddressType.TYPE_WORK;
+		postalAddress.street = addr;
+
+		ArrayList<PostalAddress> postalAddressArrayList = new ArrayList<>();
+		postalAddressArrayList.add(postalAddress);
+		contact.postalAddresses = postalAddressArrayList;
+
+		// email
+		EmailAddress emailAddress = new EmailAddress(BaseElement.NEW_ELEMENT_ID);
+		emailAddress.type = EmailAddress.EmailType.TYPE_WORK;
+		emailAddress.value = email;
+
+		ArrayList<EmailAddress> emailAddressArrayList = new ArrayList<>();
+		emailAddressArrayList.add(emailAddress);
+		contact.emailAddresses = emailAddressArrayList;
+
+		// web
+		WebSite webSite = new WebSite(BaseElement.NEW_ELEMENT_ID);
+		webSite.url = homePage;
+
+		ArrayList<WebSite> webSiteArrayList = new ArrayList<>();
+		webSiteArrayList.add(webSite);
+		contact.webSites = webSiteArrayList;
+
+
+		final ContactHelper cm = ContactHelper.sharedHelper(getApplicationContext());
+		final ArrayList<Contact> contacts = cm.searchContactsByKeyword(name);
+		boolean exists = contacts != null && contacts.size() > 0;
+		if (exists) {
 
 			new AlertDialog.Builder(this)
 					.setTitle("Warning")
-					.setMessage(contact.getName() + " has exist in contacts,do you want to update it?")
+					.setMessage(name + " has exist in contacts,do you want to update it?")
 					.setNegativeButton("No", new DialogInterface.OnClickListener() {
 						@Override
 						public void onClick(DialogInterface dialog, int which) {
-							// 添加一条新记录
-							cm.insertContact(contact,mContext);
-//							Contact existContact = cm.searchContact(contact.getName());
-//							String contactId = cm.getContactID(existContact.getName());
-//							Intent editIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("content://com.android.contacts/contacts/"+contactId));
-//							startActivity(editIntent);
+//							// 添加一条新记录
+//							cm.insertContact(contact,mContext);
+////							Contact existContact = cm.searchContact(contact.getName());
+////							String contactId = cm.getContactID(existContact.getName());
+////							Intent editIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("content://com.android.contacts/contacts/"+contactId));
+////							startActivity(editIntent);
+
+							ContactUIHelper.startActivityForNewContact(self, contact, 0);
+
 						}
 					})
 					.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
 						@Override
 						public void onClick(DialogInterface dialog, int which) {
 
-							//Intent.ACTION_INSERT_OR_EDIT
-							//参考 https://blog.csdn.net/mls1454001840/article/details/60764351 之二
-							Contact existContact = cm.searchContact(contact.getName());
-							cm.updateContact(existContact,contact);
-
-							String contactId = cm.getContactID(existContact.getName());
-							Intent editIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("content://com.android.contacts/contacts/"+contactId));
-							startActivity(editIntent);
+//							//Intent.ACTION_INSERT_OR_EDIT
+//							//参考 https://blog.csdn.net/mls1454001840/article/details/60764351 之二
+//							Contact existContact = cm.searchContact(contact.getName());
+//							cm.updateContact(existContact,contact);
+//
+//							String contactId = cm.getContactID(existContact.getName());
+//							Intent editIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("content://com.android.contacts/contacts/"+contactId));
+//							startActivity(editIntent);
 //
 //							// 显示编辑界面
 //							String contactId = cm.getContactID(existContact.getName());
 //							cm.editContact(contactId,mContext);
 
+							Contact oldContact = contacts.get(0);
+
+							// 删除旧的
+							ArrayList<Contact> deleteArr = new ArrayList<>();
+							deleteArr.add(oldContact);
+							cm.deleteContact(deleteArr);
+
+							// 导入新的
+							ArrayList<Contact> newContacts = new ArrayList<>();
+							newContacts.add(contact);
+							ArrayList<Uri> uris = cm.insertContact(newContacts);
+
+							if (uris != null && uris.size() > 0) {
+								// 显示编辑界面
+								ContactUIHelper.startActivityForEditContact(self,uris.get(0));
+							}
 
 						}
 					})
 					.show();
 		} else {
 			// 自动保存,然后显示编辑
-//			cm.addContact(contact,true);
-//			// 显示编辑界面
-//			String contactId = cm.getContactID(contact.getName());
-//			editContact(contactId);
+////			cm.addContact(contact,true);
+////			// 显示编辑界面
+////			String contactId = cm.getContactID(contact.getName());
+////			editContact(contactId);
+//
+//			// 需要用户手动保存
+//			cm.insertContact(contact,mContext);
 
-			// 需要用户手动保存
-			cm.insertContact(contact,mContext);
+			ContactUIHelper.startActivityForNewContact(self, contact, 0);
 		}
 
 	}