[ 
https://issues.apache.org/jira/browse/CB-5308?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=14033818#comment-14033818
 ] 

Shingo Toda commented on CB-5308:
---------------------------------

I think save() in Java should be like below.

{code:title=ContactManager#execute()|borderStyle=solid}
        else if (action.equals("save")) {
            final JSONObject contact = args.getJSONObject(0);
            this.cordova.getThreadPool().execute(new Runnable() {
                public void run() {
                    JSONObject res = null;
                    String id = contactAccessor.save(contact);
                    if (id != null) {
                        try {
                                // for new contact data
                            if( (contactAccessor.getJsonString(contact, "id")) 
== null){
                                res = contactAccessor.getContactByRawId(id);
                            }
                            // for existing contact data
                            else {
                                res = contactAccessor.getContactById(id);
                            }
                        } catch (JSONException e) {
                            Log.e(LOG_TAG, "JSON fail.", e);
                        }
                    }
                    if (res != null) {
                        callbackContext.success(res);
                    } else {
                        callbackContext.sendPluginResult(new 
PluginResult(PluginResult.Status.ERROR, UNKNOWN_ERROR));
                    }
                }
            });
        }
{code}

{code:title=ContactAccessorSdk5.java|borderStyle=solid}
    public JSONObject getContactByRawId(String rawid) throws JSONException {
        // Do the id query
        Cursor c = 
mApp.getActivity().getContentResolver().query(ContactsContract.Data.CONTENT_URI,
                null,
                ContactsContract.Data.RAW_CONTACT_ID + " = ? ",
                new String[] { rawid },
                ContactsContract.Data.RAW_CONTACT_ID + " ASC");

        JSONArray fields = new JSONArray();
        fields.put("*");

        HashMap<String, Boolean> populate = buildPopulationSet(fields);

        JSONArray contacts = populateContactArray(1, populate, c);

        if (contacts.length() == 1) {
            return contacts.getJSONObject(0);
        } else {
            return null;
        }
    }

    
    public JSONObject getContactById(String id) throws JSONException {
        // Do the id query
        Cursor c = 
mApp.getActivity().getContentResolver().query(ContactsContract.Data.CONTENT_URI,
                null,
                ContactsContract.Data.CONTACT_ID + " = ? ",
                new String[] { id },
                ContactsContract.Data.CONTACT_ID + " ASC");

        JSONArray fields = new JSONArray();
        fields.put("*");

        HashMap<String, Boolean> populate = buildPopulationSet(fields);

        JSONArray contacts = populateContactArray(1, populate, c);

        if (contacts.length() == 1) {
            return contacts.getJSONObject(0);
        } else {
            return null;
        }
    }
{code}

I just expect:
   if "id" in JSONObject is null, a user want to save a new contact data
   else if "id" is not null, a user want to update a existing contact data

But I wonder if there may be a number of use cases or user operations which are 
not covered by that code.

> Contact.save() call failure callback even if contact data is successfully 
> saved on device
> -----------------------------------------------------------------------------------------
>
>                 Key: CB-5308
>                 URL: https://issues.apache.org/jira/browse/CB-5308
>             Project: Apache Cordova
>          Issue Type: Bug
>          Components: Android, mobile-spec, Plugin Contacts
>    Affects Versions: 3.1.0
>         Environment: Android 4.1.2 physical device
>            Reporter: Shingo Toda
>            Priority: Minor
>
> When I run contact mobile-spec, following specs sometimes fail.
> - contacts.spec.6
> - contacts.spec.20
> - contacts.spec.21
> - contacts.spec.24
> In Java side, it looks that {{ContactAccessorSdk5.save()}} returns contact id 
> but {{ContactAccessorSdk5.getContactById()}} returns {{null}} instead of 
> contact data associated with the id. Actually some of contact data such as 
> "Test Delete" are found on my device so the save operation is actually done.
> This save() returns "newId" in the following code.
> {code:title=ContactAccessorSdk5.java|borderStyle=solid}
>           //Add contact
>         try {
>             ContentProviderResult[] cpResults = 
> mApp.getActivity().getContentResolver().applyBatch(ContactsContract.AUTHORITY,
>  ops);
>             if (cpResults.length >= 0) {
>                 newId = cpResults[0].uri.getLastPathSegment();
>             }
>         } catch (RemoteException e) {
>             Log.e(LOG_TAG, e.getMessage(), e);
>         } catch (OperationApplicationException e) {
>             Log.e(LOG_TAG, e.getMessage(), e);
>         }
>         return newId;
> {code}
> When I debug it, {{cpResults\[0\].uri}} is 
> {{content://com.android.contacts/raw_contacts/xxxx}} so, this is just my 
> assumption, I think {{save()}} returns raw contact id.
> But at {{getContactById()}}, query filter uses {{CONTACT_ID}} column as a 
> projection.
> {code:title=ContactAccessorSdk5.java|borderStyle=solid}
>         Cursor c = 
> mApp.getActivity().getContentResolver().query(ContactsContract.Data.CONTENT_URI,
>                 null,
>                 ContactsContract.Data.CONTACT_ID + " = ? ",
>                 new String[] { id },
>                 ContactsContract.Data.CONTACT_ID + " ASC");
> {code}
> So if contact id and raw contact id is different then fetching contact data 
> might fail. Actually in my case, if I fail the specs, they are always 
> different.
> I try modifying {{getContactById()}} to process following code if contact 
> JSONObject doesn't have id property, then I didn't get failure for those 
> specs even if I run them some times.
> {code:title=ContactAccessorSdk5.java|borderStyle=solid}
>         Cursor c = 
> mApp.getActivity().getContentResolver().query(ContactsContract.Data.CONTENT_URI,
>                 null,
>                 ContactsContract.Data.RAW_CONTACT_ID + " = ? ",
>                 new String[] { rawid },
>                 ContactsContract.Data.RAW_CONTACT_ID + " ASC");
> {code}
> I am just learning how contact data is managed on Android so I might be wrong 
> for some use cases, but it works fine for mobile-spec. 



--
This message was sent by Atlassian JIRA
(v6.2#6252)

Reply via email to