ContactResolver.applyBatch(...)有时返回空结果

时间:2013-08-28 21:41:34

标签: android android-contacts android-contentresolver

我正在通过以下方式将jsonned联系人添加到我的Android设备中(请注意下面的代码是草案 - 也就是说它可能包含一些效率低下的内容);

    ...

    if(!is_duplicated)
    {                   
        String firstName = p_contact.getContactFirstName();
        String middleName = p_contact.getContactMiddleName();
        String lastName = p_contact.getContactLastName();
        String orgName = p_contact.getOrganizationName();           

        boolean isFirstNameNull = (firstName != null && firstName.equalsIgnoreCase("null"));
        boolean isMiddleNameNull = (middleName != null && middleName.equalsIgnoreCase("null"));
        boolean isLastNameNull = (lastName != null && lastName.equalsIgnoreCase("null"));
        boolean isOrgNameNull = (orgName != null && orgName.equalsIgnoreCase("null"));

        ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>();
        ops.add(ContentProviderOperation.newInsert(ContactsContract.RawContacts.CONTENT_URI)
               .withValue(ContactsContract.RawContacts.ACCOUNT_TYPE, null)
               .withValue(ContactsContract.RawContacts.ACCOUNT_NAME, null)
               .build());

        //----------------- Names 
        if(!isFirstNameNull && !isMiddleNameNull && !isLastNameNull) // 111
        {
           ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)
                   .withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, 0)
                   .withValue(ContactsContract.Data.MIMETYPE,
                           ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE)
                   .withValue(ContactsContract.CommonDataKinds.StructuredName.GIVEN_NAME, firstName)
                   .withValue(ContactsContract.CommonDataKinds.StructuredName.MIDDLE_NAME, middleName)
                   .withValue(ContactsContract.CommonDataKinds.StructuredName.FAMILY_NAME, lastName)
                   .build());
        }                                   
        else if(isFirstNameNull && !isMiddleNameNull && !isLastNameNull) //  011
        {
           ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)
                   .withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, 0)
                   .withValue(ContactsContract.Data.MIMETYPE,
                           ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE)
                   .withValue(ContactsContract.CommonDataKinds.StructuredName.MIDDLE_NAME, middleName)
                   .withValue(ContactsContract.CommonDataKinds.StructuredName.FAMILY_NAME, lastName)
                   .build());
        }                       
        else if(!isFirstNameNull && isMiddleNameNull && !isLastNameNull) // 101
        {
           ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)
                   .withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, 0)
                   .withValue(ContactsContract.Data.MIMETYPE,
                           ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE)
                   .withValue(ContactsContract.CommonDataKinds.StructuredName.GIVEN_NAME, firstName)
                   .withValue(ContactsContract.CommonDataKinds.StructuredName.FAMILY_NAME, lastName)
                   .build());
        }                       
        else if(!isFirstNameNull && !isMiddleNameNull && isLastNameNull) // 110
        {
           ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)
                   .withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, 0)
                   .withValue(ContactsContract.Data.MIMETYPE,
                           ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE)
                   .withValue(ContactsContract.CommonDataKinds.StructuredName.GIVEN_NAME, firstName)
                   .withValue(ContactsContract.CommonDataKinds.StructuredName.MIDDLE_NAME, middleName)
                   .build());
        }           
        else if(isFirstNameNull && isMiddleNameNull && !isLastNameNull) // 001
        {
           ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)
                   .withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, 0)
                   .withValue(ContactsContract.Data.MIMETYPE,
                           ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE)
                   .withValue(ContactsContract.CommonDataKinds.StructuredName.FAMILY_NAME, lastName)
                   .build());
        }           
        else if(isFirstNameNull && !isMiddleNameNull && isLastNameNull) // 010
        {
           ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)
                   .withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, 0)
                   .withValue(ContactsContract.Data.MIMETYPE,
                           ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE)
                   .withValue(ContactsContract.CommonDataKinds.StructuredName.MIDDLE_NAME, middleName)
                   .build());
        }           
        else if(!isFirstNameNull && isMiddleNameNull && isLastNameNull) // 100
        {
           ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)
                   .withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, 0)
                   .withValue(ContactsContract.Data.MIMETYPE,
                           ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE)
                   .withValue(ContactsContract.CommonDataKinds.StructuredName.GIVEN_NAME, firstName)
                   .build());
        }           
        else if(isFirstNameNull && isMiddleNameNull && isLastNameNull) // 000
        {
           /*ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)
                   .withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, 0)
                   .withValue(ContactsContract.Data.MIMETYPE,
                           ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE)
                   .withValue(ContactsContract.CommonDataKinds.StructuredName.GIVEN_NAME, "Anonim")
                   .build());*/
        }            

        if(!isOrgNameNull)
        {
           ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)
                   .withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, pIndex)
                   .withValue(ContactsContract.Data.MIMETYPE,
                           ContactsContract.CommonDataKinds.Organization.CONTENT_ITEM_TYPE)
                   .withValue(ContactsContract.CommonDataKinds.Organization.COMPANY, orgName)
                   .build());
        }

        Iterator<TreeMap.Entry<String, String>> entries = p_contact.getPhonesHashMap().entrySet().iterator();
        while (entries.hasNext())
        {
              TreeMap.Entry<String, String> entry = entries.next();
              String type = entry.getKey();
              String number = entry.getValue();    

              if(type.equalsIgnoreCase("home"))
              {
                  ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)
                       .withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, 0)
                       .withValue(ContactsContract.Data.MIMETYPE,
                               ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE)
                       .withValue(ContactsContract.CommonDataKinds.Phone.NUMBER, number)
                       .withValue(ContactsContract.CommonDataKinds.Phone.TYPE, 
                               ContactsContract.CommonDataKinds.Phone.TYPE_HOME)
                       .build());
              }
              else if(type.equalsIgnoreCase("work"))
              {
                  ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)
                       .withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, 0)
                       .withValue(ContactsContract.Data.MIMETYPE,
                               ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE)
                       .withValue(ContactsContract.CommonDataKinds.Phone.NUMBER, number)
                       .withValue(ContactsContract.CommonDataKinds.Phone.TYPE, 
                               ContactsContract.CommonDataKinds.Phone.TYPE_WORK)
                       .build());
              }
              else if(type.equalsIgnoreCase("mobile"))
              {
                  ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)
                       .withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, 0)
                       .withValue(ContactsContract.Data.MIMETYPE,
                               ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE)
                       .withValue(ContactsContract.CommonDataKinds.Phone.NUMBER, number)
                       .withValue(ContactsContract.CommonDataKinds.Phone.TYPE, 
                               ContactsContract.CommonDataKinds.Phone.TYPE_MOBILE)
                       .build());
              }
              else if(type.equalsIgnoreCase("faxhome"))
              {
                  ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)
                       .withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, 0)
                       .withValue(ContactsContract.Data.MIMETYPE,
                               ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE)
                       .withValue(ContactsContract.CommonDataKinds.Phone.NUMBER, number)
                       .withValue(ContactsContract.CommonDataKinds.Phone.TYPE, 
                               ContactsContract.CommonDataKinds.Phone.TYPE_FAX_HOME)
                       .build());
              }

                 ...
        }           

        Iterator<TreeMap.Entry<String, String>> email_entries = p_contact.getEmailsHashMap().entrySet().iterator();
        while (email_entries.hasNext())
        {
              TreeMap.Entry<String, String> entry = email_entries.next();
              String type = entry.getKey();
              String address = entry.getValue();

              if(firstName != null && firstName.equals("Yasin"))    
              {
                  Log.d("ContactIssueHandler", "email is: " + address);
                  Log.d("ContactIssueHandler", "email type is: " + type);
              }

              if(type.equalsIgnoreCase("home"))
              {
                  ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)
                       .withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, 0)
                       .withValue(ContactsContract.Data.MIMETYPE,
                               ContactsContract.CommonDataKinds.Email.CONTENT_ITEM_TYPE)
                       .withValue(ContactsContract.CommonDataKinds.Email.DATA, address)
                       .withValue(ContactsContract.CommonDataKinds.Email.TYPE, 
                               ContactsContract.CommonDataKinds.Email.TYPE_HOME)
                       .build());
              }
              else if(type.equalsIgnoreCase("work"))
              {
                  ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)
                       .withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, 0)
                       .withValue(ContactsContract.Data.MIMETYPE,
                               ContactsContract.CommonDataKinds.Email.CONTENT_ITEM_TYPE)
                       .withValue(ContactsContract.CommonDataKinds.Email.ADDRESS, address)
                       .withValue(ContactsContract.CommonDataKinds.Email.TYPE, 
                               ContactsContract.CommonDataKinds.Email.TYPE_WORK)
                       .build());
              }
              else if(type.equalsIgnoreCase("mobile"))
              {
                  ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)
                       .withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, 0)
                       .withValue(ContactsContract.Data.MIMETYPE,
                               ContactsContract.CommonDataKinds.Email.CONTENT_ITEM_TYPE)
                       .withValue(ContactsContract.CommonDataKinds.Email.ADDRESS, address)
                       .withValue(ContactsContract.CommonDataKinds.Email.TYPE, 
                               ContactsContract.CommonDataKinds.Email.TYPE_CUSTOM)
                       .build());
              }
              else if(type.equalsIgnoreCase("other"))
              {
                  ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)
                       .withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, 0)
                       .withValue(ContactsContract.Data.MIMETYPE,
                               ContactsContract.CommonDataKinds.Email.CONTENT_ITEM_TYPE)
                       .withValue(ContactsContract.CommonDataKinds.Email.ADDRESS, address)
                       .withValue(ContactsContract.CommonDataKinds.Email.TYPE, 
                               ContactsContract.CommonDataKinds.Email.TYPE_OTHER)
                       .build());
              }
              else
              {                                       
                  ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)
                           .withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, 0)
                           .withValue(ContactsContract.Data.MIMETYPE,
                                   ContactsContract.CommonDataKinds.Email.CONTENT_ITEM_TYPE)
                           .withValue(ContactsContract.CommonDataKinds.Email.ADDRESS, address)
                           .withValue(ContactsContract.CommonDataKinds.Email.TYPE, 
                                   ContactsContract.CommonDataKinds.Email.TYPE_CUSTOM)
                           .withValue(ContactsContract.CommonDataKinds.Email.LABEL, type)       
                           .build());
              }
        }           

        // Asking the Contact provider to create a new contact                  
        try 
        {   
            p_contact.setIsAdded(true);
            p_contact.setReasonForFail(REASON_FOR_FAIL.NO_FAIL);    
            ContentProviderResult[] results = p_application_context.getContentResolver().applyBatch(ContactsContract.AUTHORITY, ops);

            Log.d("ContactIssueHandler", "result: " + results.length);              
            Log.d("ContactIssueHandler", "opssize: " + ops.size());

            if(results.length != ops.size())
            {
                p_contact.setIsAdded(false);
                p_contact.setReasonForFail(REASON_FOR_FAIL.DATABASE);
                        //or throw exp.
            }

        } 
        catch (OperationApplicationException oaexp) 
        {               
            oaexp.printStackTrace();
            p_contact.setIsAdded(false);
            p_contact.setReasonForFail(REASON_FOR_FAIL.DATABASE);
        }
        catch (RemoteException rexp) 
        {               
            rexp.printStackTrace();
            p_contact.setIsAdded(false);
            p_contact.setReasonForFail(REASON_FOR_FAIL.DATABASE);
        }
        catch (Exception e) 
        {               
           e.printStackTrace();
           p_contact.setIsAdded(false);
           p_contact.setReasonForFail(REASON_FOR_FAIL.DATABASE);
        }

        ...

无论如何..每次我添加一个jsonned联系人,我需要执行上面的代码。有趣的是 - 当我插入例如50个联系人时,总是会插入一些特定的联系人,因为ContentResolverResult通过applyBatch方法返回一个空数组。(不是因为抛出异常)。但是,如果我尝试逐个添加相同的“无法添加”联系人,则会添加它们!

那么可能是什么问题?

0 个答案:

没有答案