Skip to content
This repository has been archived by the owner on Feb 25, 2022. It is now read-only.

Commit

Permalink
Various fixes for email synchronization
Browse files Browse the repository at this point in the history
Read correct mapping preference "email" instead of "mail"
Synchronize all addresses, regardless of their type

Aside: Display current preference value in the settings screen
  • Loading branch information
nguillaumin committed May 27, 2013
1 parent aed03f6 commit 9e9dbf7
Show file tree
Hide file tree
Showing 5 changed files with 117 additions and 107 deletions.
137 changes: 64 additions & 73 deletions res/xml/preference_resources.xml
Original file line number Diff line number Diff line change
@@ -1,79 +1,70 @@
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" >
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">

<PreferenceCategory android:title="Search options" />
<PreferenceCategory android:title="Search options">

<EditTextPreference
android:defaultValue=""
android:key="baseDN"
android:summary="BaseDN for the LDAP query"
android:title="BaseDN" />
<EditTextPreference
android:defaultValue="(objectClass=inetOrgPerson)"
android:key="searchFilter"
android:summary="LDAP query for the search"
android:title="Search Filter" />
<EditTextPreference
android:defaultValue=""
android:key="baseDN"
android:summary="BaseDN for the LDAP query"
android:title="BaseDN"/>
<EditTextPreference
android:defaultValue="(objectClass=inetOrgPerson)"
android:key="searchFilter"
android:summary="LDAP query for the search"
android:title="Search Filter"/>

<PreferenceCategory android:title="Attribute Mappings" />
</PreferenceCategory>

<EditTextPreference
android:defaultValue="givenName"
android:key="first_name"
android:summary="first_name"
android:title="First Name" />
<EditTextPreference
android:defaultValue="sn"
android:key="last_name"
android:summary="last_name"
android:title="Last Name" />
<EditTextPreference
android:defaultValue="telephonenumber"
android:key="office_phone"
android:summary="office_phone"
android:title="Office Phone" />
<EditTextPreference
android:defaultValue="mobile"
android:key="cell_phone"
android:summary="cell_phone"
android:title="Cell Phone" />
<EditTextPreference
android:defaultValue="homephone"
android:key="home_phone"
android:summary="home_phone"
android:title="Home Phone" />
<EditTextPreference
android:defaultValue="mail"
android:key="email"
android:summary="email"
android:title="E-mail" />
<EditTextPreference
android:defaultValue="jpegphoto"
android:key="photo"
android:summary="photo"
android:title="Photo" />
<EditTextPreference
android:defaultValue="street"
android:key="street"
android:summary="street"
android:title="Street" />
<EditTextPreference
android:defaultValue="postalCode"
android:key="postalCode"
android:summary="postalCode"
android:title="Postal Code" />
<EditTextPreference
android:defaultValue="l"
android:key="city"
android:summary="city"
android:title="City" />
<EditTextPreference
android:defaultValue="st"
android:key="state"
android:summary="state"
android:title="State" />
<EditTextPreference
android:defaultValue="co"
android:key="country"
android:summary="country"
android:title="Country" />
<PreferenceCategory android:title="Attribute Mappings">

<EditTextPreference
android:defaultValue="givenName"
android:key="first_name"
android:title="First Name"/>
<EditTextPreference
android:defaultValue="sn"
android:key="last_name"
android:title="Last Name"/>
<EditTextPreference
android:defaultValue="telephonenumber"
android:key="office_phone"
android:title="Office Phone"/>
<EditTextPreference
android:defaultValue="mobile"
android:key="cell_phone"
android:title="Cell Phone"/>
<EditTextPreference
android:defaultValue="homephone"
android:key="home_phone"
android:title="Home Phone"/>
<EditTextPreference
android:defaultValue="mail"
android:key="email"
android:title="E-mail"/>
<EditTextPreference
android:defaultValue="jpegphoto"
android:key="photo"
android:title="Photo"/>
<EditTextPreference
android:defaultValue="street"
android:key="street"
android:title="Street"/>
<EditTextPreference
android:defaultValue="postalCode"
android:key="postalCode"
android:title="Postal Code"/>
<EditTextPreference
android:defaultValue="l"
android:key="city"
android:title="City"/>
<EditTextPreference
android:defaultValue="st"
android:key="state"
android:title="State"/>
<EditTextPreference
android:defaultValue="co"
android:key="country"
android:title="Country"/>

</PreferenceCategory>
</PreferenceScreen>
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@
import android.accounts.AccountManager;
import android.content.ContentResolver;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.Preference;
import android.preference.PreferenceActivity;
import android.provider.ContactsContract;
import android.util.Log;
Expand All @@ -14,7 +16,9 @@
import de.danielweisser.android.ldapsync.client.LDAPServerInstance;
import de.danielweisser.android.ldapsync.platform.ContactManager;

public class AccountSettingsActivity extends PreferenceActivity {
import java.util.Map;

public class AccountSettingsActivity extends PreferenceActivity implements SharedPreferences.OnSharedPreferenceChangeListener {

private static final String TAG = "AccountSettingsActivity";
private String accountName;
Expand All @@ -33,10 +37,18 @@ public void onCreate(Bundle savedInstanceState) {
ldapServerInstance = (LDAPServerInstance) getIntent().getSerializableExtra("ldapServer");
}
getPreferenceManager().setSharedPreferencesName(accountName);
getPreferenceManager().getSharedPreferences().registerOnSharedPreferenceChangeListener(this);
Log.i(TAG, "Get preferences for " + accountName);

addPreferencesFromResource(R.xml.preference_resources);
setContentView(R.layout.preference_layout);

// Initialize all summaries to values
for (String key: getPreferenceManager().getSharedPreferences().getAll().keySet()) {
onSharedPreferenceChanged(getPreferenceManager().getSharedPreferences(), key);
}


// this.getIntent().getExtras()) and the key "account

// ArrayAdapter<CharSequence> adapter = new ArrayAdapter<CharSequence>(this, android.R.layout.simple_spinner_item, baseDNs);
Expand All @@ -45,7 +57,7 @@ public void onCreate(Bundle savedInstanceState) {
// set the preferences file name
}

/**
/**
* Called when the user touches the done button.
*
* @param view
Expand All @@ -69,4 +81,16 @@ public void createAccount(View view) {
setResult(RESULT_OK, new Intent());
finish();
}

@Override
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String s) {
Log.d(TAG, "onContentChanged for " + s);
Preference p = findPreference(s);
if (p != null) {
Object value = sharedPreferences.getAll().get(s);
if (value != null) {
p.setSummary(value.toString());
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import android.content.SharedPreferences;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.provider.ContactsContract;
import android.util.Log;

import com.unboundid.ldap.sdk.ReadOnlyEntry;
Expand Down Expand Up @@ -135,7 +136,11 @@ public static Contact valueOf(ReadOnlyEntry user, SharedPreferences preferences)
c.setWorkPhone(getAttributevalue(user, preferences, "office_phone"));
c.setCellWorkPhone(getAttributevalue(user, preferences, "cell_phone"));
c.setHomePhone(getAttributevalue(user, preferences, "home_phone"));
c.setEmails(user.hasAttribute(preferences.getString("mail", "")) ? user.getAttributeValues(preferences.getString("mail", "")) : null);
c.setEmails(
user.hasAttribute(preferences.getString("email", ""))
? user.getAttributeValues(preferences.getString("email", ""))
: null);

byte[] image = null;
if (user.hasAttribute(preferences.getString("photo", ""))) {
byte[] array = user.getAttributeValueBytes(preferences.getString("photo", ""));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,8 @@ private void updateContact(ContentResolver resolver, long rawContactId, Contact
}
}
}

c.close();
}

prepareFields(rawContactId, contact, existingContact, ops, false);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;

import android.content.ContentProviderOperation;
import android.content.ContentValues;
Expand Down Expand Up @@ -94,39 +95,26 @@ private Uri addCallerIsSyncAdapterFlag(Uri uri) {
}

public void updateMail(int mailType) {
String newMail = null;
String existingMail = null;
if (mailType == Email.TYPE_WORK) {
if (newC.getEmails() != null && newC.getEmails().length > 0) {
newMail = newC.getEmails()[0];
}
if (existingC.getEmails() != null && existingC.getEmails().length > 0) {
existingMail = existingC.getEmails()[0];
}
}
updateMail(newMail, existingMail, mailType);
updateMail(newC.getEmails(), existingC.getEmails());
}

private void updateMail(String newMail, String existingMail, int mailType) {
String selection = Data.RAW_CONTACT_ID + "=? AND " + Email.MIMETYPE + "=? AND " + Email.TYPE + "=?";
if (TextUtils.isEmpty(newMail) && !TextUtils.isEmpty(existingMail)) {
l.d("Delete mail data " + mailType + " (" + existingMail + ")");
ops.add(ContentProviderOperation.newDelete(addCallerIsSyncAdapterFlag(Data.CONTENT_URI)).withSelection(selection,
new String[] { rawContactId + "", Email.CONTENT_ITEM_TYPE, mailType + "" }).build());
} else if (!TextUtils.isEmpty(newMail) && TextUtils.isEmpty(existingMail)) {
l.d("Add mail data " + mailType + " (" + newMail + ")");
ContentValues cv = new ContentValues();
cv.put(Email.DATA, newMail);
cv.put(Email.TYPE, mailType);
cv.put(Email.MIMETYPE, Email.CONTENT_ITEM_TYPE);
Builder insertOp = createInsert(rawContactId, cv);
ops.add(insertOp.build());
} else if (!TextUtils.isEmpty(newMail) && !newMail.equals(existingMail)) {
l.d("Update mail data " + mailType + " (" + existingMail + " => " + newMail + ")");
Builder updateOp = ContentProviderOperation.newUpdate(addCallerIsSyncAdapterFlag(Data.CONTENT_URI)).withSelection(selection,
new String[] { rawContactId + "", Email.CONTENT_ITEM_TYPE, mailType + "" }).withValue(Email.DATA, newMail);
ops.add(updateOp.build());
}
private void updateMail(String[] newMails, String[] existingMails) {
// Always delete then add
String selection = Data.RAW_CONTACT_ID + "=? AND " + Email.MIMETYPE + "=?";

ops.add(ContentProviderOperation
.newDelete(addCallerIsSyncAdapterFlag(Data.CONTENT_URI))
.withSelection( selection, new String[] { rawContactId + "", }).build());

for (String address: newMails) {
ContentValues cv = new ContentValues();
cv.put(Email.DATA, address);
cv.put(Email.TYPE, Email.TYPE_OTHER);
cv.put(Email.MIMETYPE, Email.CONTENT_ITEM_TYPE);
Builder insertOp = createInsert(rawContactId, cv);
ops.add(insertOp.build());
}

}

public void updatePhone(int phoneType) {
Expand Down

0 comments on commit 9e9dbf7

Please sign in to comment.