Skip to content
This repository has been archived by the owner on Oct 8, 2024. It is now read-only.

Commit

Permalink
Improve android integration as syncadapter, handle caller_is_syncadap…
Browse files Browse the repository at this point in the history
…ter flag.
  • Loading branch information
jacquarg committed Jun 18, 2015
1 parent 2303f6e commit e9ab0f3
Show file tree
Hide file tree
Showing 10 changed files with 143 additions and 496 deletions.
9 changes: 5 additions & 4 deletions plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -80,17 +80,18 @@
<uses-permission android:name="android.permission.WRITE_CONTACTS" />
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<uses-permission android:name="android.permission.AUTHENTICATE_ACCOUNTS" />
<!--uses-permission android:name="android.permission.WRITE_SYNC_SETTINGS" /-->

<uses-permission android:name="android.permission.WRITE_SYNC_SETTINGS" />
</config-file>
<config-file target="AndroidManifest.xml" parent="/manifest/application">
<service android:name="org.apache.cordova.contacts.authenticator.AuthenticationService" android:exported="false" >
<service android:name="org.apache.cordova.contacts.authenticator.AuthenticationService" android:exported="true" >
<intent-filter>
<action android:name="android.accounts.AccountAuthenticator" />
</intent-filter>
<meta-data android:name="android.accounts.AccountAuthenticator"
android:resource="@xml/authenticator" />
</service>
<service android:name="org.apache.cordova.contacts.syncadapter.SyncService" android:exported="false" >
<service android:name="org.apache.cordova.contacts.syncadapter.SyncService" android:exported="true" >
<intent-filter>
<action android:name="android.content.SyncAdapter" />
</intent-filter>
Expand All @@ -107,7 +108,7 @@
<source-file src="src/android/ContactManager.java" target-dir="src/org/apache/cordova/contacts" />
<source-file src="src/android/ContactInfoDTO.java" target-dir="src/org/apache/cordova/contacts" />
<source-file src="src/android/authenticator/AuthenticationService.java" target-dir="src/org/apache/cordova/contacts/authenticator" />
<source-file src="src/android/authenticator/Authenticator.java" target-dir="src/org/apache/cordova/contacts/authenticator" />
<source-file src="src/android/authenticator/StubAuthenticator.java" target-dir="src/org/apache/cordova/contacts/authenticator" />
<source-file src="src/android/syncadapter/SyncAdapter.java" target-dir="src/org/apache/cordova/contacts/syncadapter" />
<source-file src="src/android/syncadapter/SyncService.java" target-dir="src/org/apache/cordova/contacts/syncadapter" />

Expand Down
4 changes: 4 additions & 0 deletions src/android/ContactAccessor.java
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,10 @@ protected String getJsonString(JSONObject obj, String property) {
*/
public abstract boolean remove(String id);

// Enhanced for android.
public abstract boolean remove(String id, boolean callerIsSyncAdapter);


/**
* A class that represents the where clause to be used in the database query
*/
Expand Down
45 changes: 43 additions & 2 deletions src/android/ContactAccessorSdk5.java
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,14 @@ public class ContactAccessorSdk5 extends ContactAccessor {
dbMap.put("urls.value", ContactsContract.CommonDataKinds.Website.URL);

dbMap.put("dirty", ContactsContract.RawContacts.DIRTY);
dbMap.put("deleted", ContactsContract.RawContacts.DELETED);
dbMap.put("sourceId", ContactsContract.RawContacts.SOURCE_ID);
dbMap.put("sync1", ContactsContract.RawContacts.SYNC1);
dbMap.put("sync2", ContactsContract.RawContacts.SYNC2);
dbMap.put("sync3", ContactsContract.RawContacts.SYNC3);
dbMap.put("sync4", ContactsContract.RawContacts.SYNC4);



}

Expand Down Expand Up @@ -224,6 +232,7 @@ public JSONArray search(JSONArray fields, JSONObject options) {
columnsToFetch.add(ContactsContract.RawContacts.VERSION);

columnsToFetch.add(ContactsContract.RawContacts.DIRTY);
columnsToFetch.add(ContactsContract.RawContacts.DELETED);
columnsToFetch.add(ContactsContract.RawContacts.SOURCE_ID);
columnsToFetch.add(ContactsContract.RawContacts.SYNC1);
columnsToFetch.add(ContactsContract.RawContacts.SYNC2);
Expand Down Expand Up @@ -365,6 +374,7 @@ private JSONArray populateContactArray(int limit,
int version = 0;
boolean dirty = false;
String sourceId = "";
boolean deleted = false;
String sync1 = "";
String sync2 = "";
String sync3 = "";
Expand Down Expand Up @@ -393,6 +403,7 @@ private JSONArray populateContactArray(int limit,
int colVersion = c.getColumnIndex(ContactsContract.RawContacts.VERSION);
int colDirty = c.getColumnIndex(ContactsContract.RawContacts.DIRTY);
int colSourceId = c.getColumnIndex(ContactsContract.RawContacts.SOURCE_ID);
int colDeleted = c.getColumnIndex(ContactsContract.RawContacts.DELETED);
int colSync1 = c.getColumnIndex(ContactsContract.RawContacts.SYNC1);
int colSync2 = c.getColumnIndex(ContactsContract.RawContacts.SYNC2);
int colSync3 = c.getColumnIndex(ContactsContract.RawContacts.SYNC3);
Expand All @@ -407,6 +418,7 @@ private JSONArray populateContactArray(int limit,
version = c.getInt(colVersion);
dirty = c.getInt(colDirty) == 1;
sourceId = c.getString(colSourceId);
deleted = c.getInt(colDeleted) == 1;
sync1 = c.getString(colSync1);
sync2 = c.getString(colSync2);
sync3 = c.getString(colSync3);
Expand Down Expand Up @@ -450,6 +462,7 @@ private JSONArray populateContactArray(int limit,
contact.put("version", version);
contact.put("dirty", dirty);
contact.put("sourceId", sourceId);
contact.put("deleted", deleted);
contact.put("sync1", sync1);
contact.put("sync2", sync2);
contact.put("sync3", sync3);
Expand Down Expand Up @@ -755,6 +768,10 @@ else if (key.startsWith("dirty")) {
where.add("(" + dbMap.get(key) + " LIKE ? )");
whereArgs.add(searchTerm);
}
else if (key.startsWith("deleted")) {
where.add("(" + dbMap.get(key) + " LIKE ? )");
whereArgs.add(searchTerm);
}
else if (key.startsWith("sync1")) {
where.add("(" + ContactsContract.RawContacts.SYNC1 + " LIKE ? )");
whereArgs.add(searchTerm);
Expand Down Expand Up @@ -1579,6 +1596,10 @@ private String modifyContact(String id, JSONObject contact, String accountType,
int dirty = contact.optBoolean("dirty") ? 1 : 0 ;
builder.withValue(ContactsContract.RawContacts.DIRTY, dirty);

int deleted = contact.optBoolean("deleted") ? 1 : 0 ;
builder.withValue(ContactsContract.RawContacts.DELETED, deleted);


String sync1 = getJsonString(contact, "sync1");
if (sync1 != null) {
builder.withValue(ContactsContract.RawContacts.SYNC1, sync1);
Expand Down Expand Up @@ -1846,6 +1867,9 @@ private String createNewContact(JSONObject contact, String accountType, String a
int dirty = contact.optBoolean("dirty") ? 1 : 0 ;
builder.withValue(ContactsContract.RawContacts.DIRTY, dirty);

int deleted = contact.optBoolean("deleted") ? 1 : 0 ;
builder.withValue(ContactsContract.RawContacts.DELETED, deleted);

String sync1 = getJsonString(contact, "sync1");
if (sync1 != null) {
builder.withValue(ContactsContract.RawContacts.SYNC1, sync1);
Expand Down Expand Up @@ -2039,21 +2063,38 @@ private String createNewContact(JSONObject contact, String accountType, String a
return newId;
}

@Override
/**
* This method will remove a Contact from the database based on ID.
* @param id the unique ID of the contact to remove
*/
@Override
public boolean remove(String id) {
return remove(id, false);
}


@Override
/**
* This method will remove a Contact from the database based on ID.
* @param id the unique ID of the contact to remove
* @param callerIsSyncAdapter trully remove or just set DELETED and DIRTY falgs : See http://developer.android.com/reference/android/provider/ContactsContract.RawContacts.html §delete .
*/
public boolean remove(String id, boolean callerIsSyncAdapter) {

int result = 0;
Cursor cursor = mApp.getActivity().getContentResolver().query(ContactsContract.Contacts.CONTENT_URI,
Cursor cursor = mApp.getActivity().getContentResolver().query(
ContactsContract.Contacts.CONTENT_URI,
null,
ContactsContract.Contacts._ID + " = ?",
new String[] { id }, null);
if (cursor.getCount() == 1) {
cursor.moveToFirst();
String lookupKey = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.LOOKUP_KEY));
Uri uri = Uri.withAppendedPath(ContactsContract.Contacts.CONTENT_LOOKUP_URI, lookupKey);
if (callerIsSyncAdapter) {
uri = uri.buildUpon().appendQueryParameter(
ContactsContract.CALLER_IS_SYNCADAPTER, "true").build();
}
result = mApp.getActivity().getContentResolver().delete(uri, null, null);
} else {
Log.d(LOG_TAG, "Could not find contact with ID");
Expand Down
5 changes: 3 additions & 2 deletions src/android/authenticator/AuthenticationService.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,15 @@ public class AuthenticationService extends Service {

private static final String TAG = "AuthenticationService";

private Authenticator mAuthenticator;
private StubAuthenticator mAuthenticator;
// private Authenticator mAuthenticator;

@Override
public void onCreate() {
if (Log.isLoggable(TAG, Log.VERBOSE)) {
Log.v(TAG, "SampleSyncAdapter Authentication Service started.");
}
mAuthenticator = new Authenticator(this);
mAuthenticator = new StubAuthenticator(this);
}

@Override
Expand Down
157 changes: 0 additions & 157 deletions src/android/authenticator/Authenticator.java

This file was deleted.

Loading

0 comments on commit e9ab0f3

Please sign in to comment.