From 8be6fdedd9ff9162fc48b5554b66e502f8d195f8 Mon Sep 17 00:00:00 2001 From: rfc2822 Date: Tue, 31 Dec 2013 16:37:31 +0100 Subject: [PATCH] Minor refactoring * update target SDK version to API level 19 * less null return values * explicit Resource generateUID/generateName methods * use StringUtils when it makes sense --- AndroidManifest.xml | 2 +- src/at/bitfire/davdroid/URIUtils.java | 3 -- src/at/bitfire/davdroid/resource/Contact.java | 7 +++- src/at/bitfire/davdroid/resource/Event.java | 14 ++++++-- .../davdroid/resource/LocalAddressBook.java | 33 ++++++++++--------- .../davdroid/resource/LocalCalendar.java | 10 +++--- .../davdroid/resource/LocalCollection.java | 3 +- .../davdroid/resource/RemoteCollection.java | 5 ++- .../bitfire/davdroid/resource/Resource.java | 5 +-- .../davdroid/syncadapter/DavSyncAdapter.java | 18 ++++------ .../davdroid/syncadapter/SyncManager.java | 16 ++++----- 11 files changed, 60 insertions(+), 56 deletions(-) diff --git a/AndroidManifest.xml b/AndroidManifest.xml index 34dc5112..0f4b3391 100644 --- a/AndroidManifest.xml +++ b/AndroidManifest.xml @@ -6,7 +6,7 @@ + android:targetSdkVersion="19" /> diff --git a/src/at/bitfire/davdroid/URIUtils.java b/src/at/bitfire/davdroid/URIUtils.java index 6af7f61b..4eb3a039 100644 --- a/src/at/bitfire/davdroid/URIUtils.java +++ b/src/at/bitfire/davdroid/URIUtils.java @@ -10,9 +10,6 @@ public class URIUtils { // handles invalid URLs/paths as good as possible public static String sanitize(String original) { - if (original == null) - return null; - String url = original; // ":" is reserved as scheme/port separator, but we assume http:// and https:// URLs only diff --git a/src/at/bitfire/davdroid/resource/Contact.java b/src/at/bitfire/davdroid/resource/Contact.java index c4580816..d4344640 100644 --- a/src/at/bitfire/davdroid/resource/Contact.java +++ b/src/at/bitfire/davdroid/resource/Contact.java @@ -97,9 +97,14 @@ public class Contact extends Resource { super(localID, resourceName, eTag); } + @Override - public void initRemoteFields() { + public void generateUID() { uid = UUID.randomUUID().toString(); + } + + @Override + public void generateName() { name = uid + ".vcf"; } diff --git a/src/at/bitfire/davdroid/resource/Event.java b/src/at/bitfire/davdroid/resource/Event.java index 86d02d69..d97f4e88 100644 --- a/src/at/bitfire/davdroid/resource/Event.java +++ b/src/at/bitfire/davdroid/resource/Event.java @@ -56,6 +56,8 @@ import net.fortuna.ical4j.model.property.Summary; import net.fortuna.ical4j.model.property.Transp; import net.fortuna.ical4j.model.property.Uid; import net.fortuna.ical4j.model.property.Version; +import net.fortuna.ical4j.util.SimpleHostInfo; +import net.fortuna.ical4j.util.UidGenerator; import android.text.format.Time; import android.util.Log; import at.bitfire.davdroid.Constants; @@ -101,10 +103,16 @@ public class Event extends Resource { public Event(long localID, String name, String ETag) { super(localID, name, ETag); } + @Override - public void initRemoteFields() { - uid = DavSyncAdapter.generateUID(); + public void generateUID() { + UidGenerator generator = new UidGenerator(new SimpleHostInfo(DavSyncAdapter.getAndroidID()), String.valueOf(android.os.Process.myPid())); + uid = generator.generateUid().getValue(); + } + + @Override + public void generateName() { name = uid.replace("@", "_") + ".ics"; } @@ -127,7 +135,7 @@ public class Event extends Resource { uid = event.getUid().getValue(); else { Log.w(TAG, "Received VEVENT without UID, generating new one"); - uid = DavSyncAdapter.generateUID(); + generateUID(); } dtStart = event.getStartDate(); validateTimeZone(dtStart); diff --git a/src/at/bitfire/davdroid/resource/LocalAddressBook.java b/src/at/bitfire/davdroid/resource/LocalAddressBook.java index acb54bb7..3334309c 100644 --- a/src/at/bitfire/davdroid/resource/LocalAddressBook.java +++ b/src/at/bitfire/davdroid/resource/LocalAddressBook.java @@ -262,7 +262,7 @@ public class LocalAddressBook extends LocalCollection { break; case Phone.TYPE_CUSTOM: String customType = cursor.getString(1); - if (customType != null && !customType.isEmpty()) + if (!StringUtils.isEmpty(customType)) number.addType(TelephoneType.get(labelToXName(customType))); } c.getPhoneNumbers().add(number); @@ -287,7 +287,7 @@ public class LocalAddressBook extends LocalCollection { break; case Email.TYPE_CUSTOM: String customType = cursor.getString(2); - if (customType != null && !customType.isEmpty()) + if (!StringUtils.isEmpty(customType)) email.addType(EmailType.get(labelToXName(customType))); } c.getEmails().add(email); @@ -314,9 +314,9 @@ public class LocalAddressBook extends LocalCollection { if (cursor != null && cursor.moveToNext()) { String org = cursor.getString(0), role = cursor.getString(1); - if (org != null && !org.isEmpty()) + if (!StringUtils.isEmpty(org)) c.setOrganization(org); - if (role != null && !role.isEmpty()) + if (!StringUtils.isEmpty(role)) c.setRole(role); } } @@ -371,7 +371,7 @@ public class LocalAddressBook extends LocalCollection { break; case Im.TYPE_CUSTOM: String customType = cursor.getString(2); - if (customType != null && !customType.isEmpty()) + if (!StringUtils.isEmpty(customType)) impp.addType(ImppType.get(labelToXName(customType))); } c.getImpps().add(impp); @@ -416,7 +416,7 @@ public class LocalAddressBook extends LocalCollection { break; case StructuredPostal.TYPE_CUSTOM: String customType = cursor.getString(2); - if (customType != null && !customType.isEmpty()) + if (!StringUtils.isEmpty(customType)) address.addType(AddressType.get(labelToXName(customType))); break; } @@ -469,15 +469,15 @@ public class LocalAddressBook extends LocalCollection { if (cursor != null && cursor.moveToNext()) { Impp impp = new Impp("sip:" + cursor.getString(0)); switch (cursor.getInt(1)) { - case Im.TYPE_HOME: + case SipAddress.TYPE_HOME: impp.addType(ImppType.HOME); break; - case Im.TYPE_WORK: + case SipAddress.TYPE_WORK: impp.addType(ImppType.WORK); break; - case Im.TYPE_CUSTOM: + case SipAddress.TYPE_CUSTOM: String customType = cursor.getString(2); - if (customType != null && !customType.isEmpty()) + if (!StringUtils.isEmpty(customType)) impp.addType(ImppType.get(labelToXName(customType))); } c.getImpps().add(impp); @@ -544,7 +544,7 @@ public class LocalAddressBook extends LocalCollection { // TODO relations - // SIP address built by buildIMPP + // SIP addresses built by buildIMPP } @Override @@ -766,7 +766,7 @@ public class LocalAddressBook extends LocalCollection { * country */ String formattedAddress = address.getLabel(); - if (formattedAddress == null || formattedAddress.isEmpty()) { + if (StringUtils.isEmpty(formattedAddress)) { String lineStreet = StringUtils.join(new String[] { address.getStreetAddress(), address.getPoBox(), address.getExtendedAddress() }, " "), lineLocality = StringUtils.join(new String[] { address.getPostalCode(), address.getLocality() }, " "); @@ -847,14 +847,15 @@ public class LocalAddressBook extends LocalCollection { } protected String xNameToLabel(String xname) { - if (xname == null) - return null; - // "x-my_property" + // "X-MY_PROPERTY" // 1. ensure lower case -> "x-my_property" // 2. remove x- from beginning -> "my_property" // 3. replace "_" by " " -> "my property" // 4. capitalize -> "My Property" - return WordUtils.capitalize(StringUtils.removeStart(xname.toLowerCase(Locale.US), "x-").replaceAll("_"," ")); + String lowerCase = StringUtils.lowerCase(xname, Locale.US), + withoutPrefix = StringUtils.removeStart(lowerCase, "x-"), + withSpaces = StringUtils.replace(withoutPrefix, "_", " "); + return WordUtils.capitalize(withSpaces); } } diff --git a/src/at/bitfire/davdroid/resource/LocalCalendar.java b/src/at/bitfire/davdroid/resource/LocalCalendar.java index 74caa89e..b9ce851a 100644 --- a/src/at/bitfire/davdroid/resource/LocalCalendar.java +++ b/src/at/bitfire/davdroid/resource/LocalCalendar.java @@ -235,29 +235,29 @@ public class LocalCalendar extends LocalCollection { // recurrence try { String duration = cursor.getString(18); - if (duration != null && !duration.isEmpty()) + if (!StringUtils.isEmpty(duration)) e.setDuration(new Duration(new Dur(duration))); String strRRule = cursor.getString(10); - if (strRRule != null && !strRRule.isEmpty()) + if (!StringUtils.isEmpty(strRRule)) e.setRrule(new RRule(strRRule)); String strRDate = cursor.getString(11); - if (strRDate != null && !strRDate.isEmpty()) { + if (!StringUtils.isEmpty(strRDate)) { RDate rDate = new RDate(); rDate.setValue(strRDate); e.setRdate(rDate); } String strExRule = cursor.getString(12); - if (strExRule != null && !strExRule.isEmpty()) { + if (!StringUtils.isEmpty(strExRule)) { ExRule exRule = new ExRule(); exRule.setValue(strExRule); e.setExrule(exRule); } String strExDate = cursor.getString(13); - if (strExDate != null && !strExDate.isEmpty()) { + if (!StringUtils.isEmpty(strExDate)) { // ignored, see https://code.google.com/p/android/issues/detail?id=21426 ExDate exDate = new ExDate(); exDate.setValue(strExDate); diff --git a/src/at/bitfire/davdroid/resource/LocalCollection.java b/src/at/bitfire/davdroid/resource/LocalCollection.java index b1b415bc..9ca4ed9b 100644 --- a/src/at/bitfire/davdroid/resource/LocalCollection.java +++ b/src/at/bitfire/davdroid/resource/LocalCollection.java @@ -122,7 +122,8 @@ public abstract class LocalCollection { // new record: generate UID + remote file name so that we can upload T resource = findById(id, false); - resource.initRemoteFields(); + resource.generateUID(); + resource.generateName(); // write generated UID + remote file name into database ContentValues values = new ContentValues(2); values.put(entryColumnUID(), resource.getUid()); diff --git a/src/at/bitfire/davdroid/resource/RemoteCollection.java b/src/at/bitfire/davdroid/resource/RemoteCollection.java index fc0c974c..c0dbeaa0 100644 --- a/src/at/bitfire/davdroid/resource/RemoteCollection.java +++ b/src/at/bitfire/davdroid/resource/RemoteCollection.java @@ -65,9 +65,8 @@ public abstract class RemoteCollection { if (collection.getMembers() != null) { for (WebDavResource member : collection.getMembers()) resources.add(newResourceSkeleton(member.getName(), member.getETag())); - return resources.toArray(new Resource[0]); - } else - return null; + } + return resources.toArray(new Resource[0]); } @SuppressWarnings("unchecked") diff --git a/src/at/bitfire/davdroid/resource/Resource.java b/src/at/bitfire/davdroid/resource/Resource.java index 80e41540..82eb571c 100644 --- a/src/at/bitfire/davdroid/resource/Resource.java +++ b/src/at/bitfire/davdroid/resource/Resource.java @@ -35,8 +35,9 @@ public abstract class Resource { this.localID = localID; } - // sets resource name and UID - public abstract void initRemoteFields(); + // sets UID and resource name (= remote file name) + public abstract void generateUID(); + public abstract void generateName(); public abstract void parseEntity(InputStream entity) throws IOException, ParserException, VCardException; public abstract ByteArrayOutputStream toEntity() throws IOException, ValidationException; diff --git a/src/at/bitfire/davdroid/syncadapter/DavSyncAdapter.java b/src/at/bitfire/davdroid/syncadapter/DavSyncAdapter.java index ce328675..2da23bc4 100644 --- a/src/at/bitfire/davdroid/syncadapter/DavSyncAdapter.java +++ b/src/at/bitfire/davdroid/syncadapter/DavSyncAdapter.java @@ -3,8 +3,7 @@ package at.bitfire.davdroid.syncadapter; import java.io.IOException; import java.util.Map; -import net.fortuna.ical4j.util.SimpleHostInfo; -import net.fortuna.ical4j.util.UidGenerator; +import lombok.Getter; import org.apache.http.HttpException; import org.apache.http.auth.AuthenticationException; @@ -29,24 +28,19 @@ public abstract class DavSyncAdapter extends AbstractThreadedSyncAdapter { protected AccountManager accountManager; - private static String androidID; + @Getter private static String androidID; - public DavSyncAdapter(Context context) { super(context, true); - androidID = Settings.Secure.getString(context.getContentResolver(), Settings.Secure.ANDROID_ID); + synchronized(this) { + if (androidID == null) + androidID = Settings.Secure.getString(context.getContentResolver(), Settings.Secure.ANDROID_ID); + } accountManager = AccountManager.get(context); } - - - public static String generateUID() { - UidGenerator generator = new UidGenerator(new SimpleHostInfo(androidID), String.valueOf(android.os.Process.myPid())); - return generator.generateUid().getValue(); - } - protected abstract Map, RemoteCollection> getSyncPairs(Account account, ContentProviderClient provider); diff --git a/src/at/bitfire/davdroid/syncadapter/SyncManager.java b/src/at/bitfire/davdroid/syncadapter/SyncManager.java index 4f8122b5..3470ecea 100644 --- a/src/at/bitfire/davdroid/syncadapter/SyncManager.java +++ b/src/at/bitfire/davdroid/syncadapter/SyncManager.java @@ -59,15 +59,13 @@ public class SyncManager { remotelyUpdated = new HashSet(); Resource[] remoteResources = remote.getMemberETags(); - if (remoteResources != null) { - for (Resource remoteResource : remoteResources) { - try { - Resource localResource = local.findByRemoteName(remoteResource.getName(), false); - if (localResource.getETag() == null || !localResource.getETag().equals(remoteResource.getETag())) - remotelyUpdated.add(remoteResource); - } catch(RecordNotFoundException e) { - remotelyAdded.add(remoteResource); - } + for (Resource remoteResource : remoteResources) { + try { + Resource localResource = local.findByRemoteName(remoteResource.getName(), false); + if (!remoteResource.getETag().equals(localResource.getETag())) + remotelyUpdated.add(remoteResource); + } catch(RecordNotFoundException e) { + remotelyAdded.add(remoteResource); } }