diff --git a/src/android/ContactAccessorSdk5.java b/src/android/ContactAccessorSdk5.java index 31708ceb..cfe6458f 100644 --- a/src/android/ContactAccessorSdk5.java +++ b/src/android/ContactAccessorSdk5.java @@ -120,7 +120,6 @@ public class ContactAccessorSdk5 extends ContactAccessor { dbMap.put("birthday", ContactsContract.CommonDataKinds.Event.CONTENT_ITEM_TYPE); dbMap.put("note", ContactsContract.CommonDataKinds.Note.NOTE); dbMap.put("photos.value", ContactsContract.CommonDataKinds.Photo.CONTENT_ITEM_TYPE); - //dbMap.put("categories.value", null); dbMap.put("urls", ContactsContract.CommonDataKinds.Website.URL); dbMap.put("urls.value", ContactsContract.CommonDataKinds.Website.URL); @@ -188,10 +187,6 @@ public JSONArray search(JSONArray fields, JSONObject options) { } - // Log.d(LOG_TAG, "Search Term = " + searchTerm); - //Log.d(LOG_TAG, "Field Length = " + fields.length()); - //Log.d(LOG_TAG, "Fields = " + fields.toString()); - // Loop through the fields the user provided to see what data should be returned. HashMap populate = buildPopulationSet(options); @@ -331,13 +326,10 @@ public JSONObject getContactById(String id) throws JSONException { public JSONObject getContactById(String id, JSONArray desiredFields) throws JSONException { // Do the id query Cursor c = mApp.getActivity().getContentResolver().query( - // ContactsContract.Data.CONTENT_URI, RawContactsEntity.CONTENT_URI, null, - // ContactsContract.Data.RAW_CONTACT_ID + " = ? ", ContactsContract.RawContacts._ID + " = ? ", new String[] { id }, - // ContactsContract.Data.RAW_CONTACT_ID + " ASC"); ContactsContract.RawContacts.Data._ID + " ASC"); HashMap populate = buildPopulationSet( @@ -390,7 +382,6 @@ private JSONArray populateContactArray(int limit, // Column indices int colContactId = c.getColumnIndex(ContactsContract.Data.CONTACT_ID); - // int colRawContactId = c.getColumnIndex(ContactsContract.Data.RAW_CONTACT_ID); int colRawContactId = c.getColumnIndex(ContactsContract.RawContacts._ID); int colMimetype = c.getColumnIndex(ContactsContract.Data.MIMETYPE); int colDisplayName = c.getColumnIndex(ContactsContract.CommonDataKinds.StructuredName.DISPLAY_NAME); @@ -436,7 +427,6 @@ private JSONArray populateContactArray(int limit, emails, ims, websites, photos)); - // Clean up the objects contact = new JSONObject(); organizations = new JSONArray(); @@ -687,7 +677,6 @@ private WhereOptions buildWhereClause(JSONArray fields, String searchTerm, Strin String key; try { - //Log.d(LOG_TAG, "How many fields do we have = " + fields.length()); for (int i = 0; i < fields.length(); i++) { key = fields.getString(i); @@ -1034,23 +1023,12 @@ private JSONObject photoQuery(Cursor cursor, String rawContactId) { byte[] photoBlob = cursor.getBlob(cursor.getColumnIndex( ContactsContract.CommonDataKinds.Photo.PHOTO)); + if (photoBlob == null) { + return null; + } + photo.put("value", Base64.encodeToString(photoBlob, Base64.DEFAULT)); - // photo.put("type", "url"); - // Uri person = ContentUris.withAppendedId(ContactsContract.RawContacts.CONTENT_URI, (Long.valueOf(rawContactId))); - // Uri photoUri = Uri.withAppendedPath(person, ContactsContract.RawContacts.DisplayPhoto.CONTENT_DIRECTORY); - // photo.put("value", photoUri.toString()); - - // // Query photo existance - // Cursor photoCursor = mApp.getActivity().getContentResolver().query(photoUri, new String[] {ContactsContract.Contacts.Photo.PHOTO}, null, null, null); - // if (photoCursor == null) { - // return null; - // } else { - // if (!photoCursor.moveToFirst()) { - // photoCursor.close(); - // return null; - // } - // } } catch (JSONException e) { Log.e(LOG_TAG, e.getMessage(), e); } @@ -1151,17 +1129,6 @@ private String modifyContact(String id, JSONObject contact, String accountType, // Create a list of attributes to add to the contact database ArrayList ops = new ArrayList(); - - // TODO: really dangerous : move all contact to accountYpe, accountName. - // May come from examples in android doc, useless (unless change account - // of a app). - // - //Add contact type - // ops.add(ContentProviderOperation.newUpdate(ContactsContract.RawContacts.CONTENT_URI) - // .withValue(ContactsContract.RawContacts.ACCOUNT_TYPE, accountType) - // .withValue(ContactsContract.RawContacts.ACCOUNT_NAME, accountName) - // .build()); - // Modify name JSONObject name; try { diff --git a/src/android/ContactManager.java b/src/android/ContactManager.java index 397f2692..19037177 100644 --- a/src/android/ContactManager.java +++ b/src/android/ContactManager.java @@ -147,11 +147,16 @@ public void run() { else if (action.equals("pickContact")) { pickContactAsync(); } - // TODO : cleanup ! else if (action.equals("createAccount")) { - createAccount(); + final String accountName = args.getString(0); + final String accountType = args.getString(1); + createAccount(accountName, accountType); + callbackContext.success(); - // listAccounts(); + } + else if (action.equals("listAccounts")) { + JSONArray accounts = listAccounts(); + callbackContext.success(accounts); } else { @@ -160,29 +165,36 @@ else if (action.equals("createAccount")) { return true; } - // TODO !! - private void createAccount() { + + private void createAccount(String accountName, String accountType) { AccountManager accountManager = AccountManager.get(ContactManager.this.cordova.getActivity()); + // Create account if not exist. for (Account c: accountManager.getAccounts()) { - Log.d("CordovaContacts", c.type + ", " + c.name); - if ("io.cozy".equals(c.type) && "myCozy".equals(c.name)) { + if (accountType.equals(c.type) && accountName.equals(c.name)) { return; // skip creation. } } - Account account = new Account("myCozy", "io.cozy"); - - // ContentResolver.setIsSyncable(account, ContactsContract.AUTHORITY, 1); - // ContentResolver.setSyncAutomatically(account, ContactsContract.AUTHORITY, true); + Account account = new Account(accountName, accountType); accountManager.addAccountExplicitly(account, null, null); } - private void listAccounts() { + private JSONArray listAccounts() { AccountManager accountManager = AccountManager.get(ContactManager.this.cordova.getActivity()); - for (Account c: accountManager.getAccounts()) { - Log.d("CordovaContacts", c.type + ", " + c.name); + JSONArray accounts = new JSONArray(); + try { + for (Account c: accountManager.getAccounts()) { + JSONObject account = new JSONObject(); + account.put("type", c.type); + account.put("name", c.name); + + accounts.put(account); + } + } catch (JSONException e) { + Log.e(LOG_TAG, "JSON fail.", e); } + return accounts; } /** diff --git a/src/android/syncadapter/SyncAdapter.java b/src/android/syncadapter/SyncAdapter.java index 7ef8cb40..8aa3bdc8 100644 --- a/src/android/syncadapter/SyncAdapter.java +++ b/src/android/syncadapter/SyncAdapter.java @@ -107,86 +107,7 @@ private static long ensureSampleGroupExists(Context context, Account account) { public void onPerformSync(Account account, Bundle extras, String authority, ContentProviderClient provider, SyncResult syncResult) { Log.d(TAG, "onPerformSync: do nothing !"); - // try { - // // see if we already have a sync-state attached to this account. By handing - // // This value to the server, we can just get the contacts that have - // // been updated on the server-side since our last sync-up - // long lastSyncMarker = getServerSyncMarker(account); - - // // By default, contacts from a 3rd party provider are hidden in the contacts - // // list. So let's set the flag that causes them to be visible, so that users - // // can actually see these contacts. - // if (lastSyncMarker == 0) { - // ContactManager.setAccountContactsVisibility(getContext(), account, true); - // } - - // List dirtyContacts; - // List updatedContacts; - - // // Use the account manager to request the AuthToken we'll need - // // to talk to our sample server. If we don't have an AuthToken - // // yet, this could involve a round-trip to the server to request - // // and AuthToken. - // final String authtoken = mAccountManager.blockingGetAuthToken(account, - // Constants.AUTHTOKEN_TYPE, NOTIFY_AUTH_FAILURE); - - // // Make sure that the sample group exists - final long groupId = ensureSampleGroupExists(mContext, account); - - - - // // Find the local 'dirty' contacts that we need to tell the server about... - // // Find the local users that need to be sync'd to the server... - // dirtyContacts = ContactManager.getDirtyContacts(mContext, account); - - // // Send the dirty contacts to the server, and retrieve the server-side changes - // updatedContacts = NetworkUtilities.syncContacts(account, authtoken, - // lastSyncMarker, dirtyContacts); - - // // Update the local contacts database with the changes. updateContacts() - // // returns a syncState value that indicates the high-water-mark for - // // the changes we received. - // Log.d(TAG, "Calling contactManager's sync contacts"); - // long newSyncState = ContactManager.updateContacts(mContext, - // account.name, - // updatedContacts, - // groupId, - // lastSyncMarker); - - // // This is a demo of how you can update IM-style status messages - // // for contacts on the client. This probably won't apply to - // // 2-way contact sync providers - it's more likely that one-way - // // sync providers (IM clients, social networking apps, etc) would - // // use this feature. - - // ContactManager.updateStatusMessages(mContext, updatedContacts); - - // // Save off the new sync marker. On our next sync, we only want to receive - // // contacts that have changed since this sync... - // setServerSyncMarker(account, newSyncState); - - // if (dirtyContacts.size() > 0) { - // ContactManager.clearSyncFlags(mContext, dirtyContacts); - // } - - // } catch (final AuthenticatorException e) { - // Log.e(TAG, "AuthenticatorException", e); - // syncResult.stats.numParseExceptions++; - // } catch (final OperationCanceledException e) { - // Log.e(TAG, "OperationCanceledExcetpion", e); - // } catch (final IOException e) { - // Log.e(TAG, "IOException", e); - // syncResult.stats.numIoExceptions++; - // } catch (final AuthenticationException e) { - // Log.e(TAG, "AuthenticationException", e); - // syncResult.stats.numAuthExceptions++; - // } catch (final ParseException e) { - // Log.e(TAG, "ParseException", e); - // syncResult.stats.numParseExceptions++; - // } catch (final JSONException e) { - // Log.e(TAG, "JSONException", e); - // syncResult.stats.numParseExceptions++; - // } + final long groupId = ensureSampleGroupExists(mContext, account); } /** diff --git a/www/Contact.js b/www/Contact.js index 5dbe391e..9b43c177 100644 --- a/www/Contact.js +++ b/www/Contact.js @@ -29,6 +29,7 @@ var argscheck = require('cordova/argscheck'), * Currently only used for Date fields */ function convertIn(contact) { + // Wrong on android ! // var value = contact.birthday; // try { // // contact.birthday = new Date(parseFloat(value)); @@ -45,6 +46,7 @@ function convertIn(contact) { **/ function convertOut(contact) { + // Wrong on android ! // var value = contact.birthday; // if (value !== null) { // // try to make it a Date object if it is not already diff --git a/www/contacts.js b/www/contacts.js index e0259520..09985561 100644 --- a/www/contacts.js +++ b/www/contacts.js @@ -94,11 +94,17 @@ var contacts = { return contact; }, - // TODO + createAccount: function(accountType, accountName, successCB, errorCB) { argscheck.checkArgs('ssfF', 'contacts.createAccount', arguments); - exec(successCB, errorCB, 'Contacts', 'createAccount', [accountName, accountType]); + exec(successCB, errorCB, 'Contacts', 'createAccount', + [accountName, accountType]); + + }, + listAccounts: function(successCB, errorCB) { + argscheck.checkArgs('fF', 'contacts.listAccounts', arguments); + exec(successCB, errorCB, "Contacts", "listAccounts", []); }, };