1
0
mirror of https://github.com/etesync/android synced 2024-12-23 15:18:14 +00:00

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
This commit is contained in:
rfc2822 2013-12-31 16:37:31 +01:00
parent df012efe78
commit 8be6fdedd9
11 changed files with 60 additions and 56 deletions

View File

@ -6,7 +6,7 @@
<uses-sdk
android:minSdkVersion="14"
android:targetSdkVersion="17" />
android:targetSdkVersion="19" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.AUTHENTICATE_ACCOUNTS" />

View File

@ -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

View File

@ -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";
}

View File

@ -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);

View File

@ -262,7 +262,7 @@ public class LocalAddressBook extends LocalCollection<Contact> {
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<Contact> {
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<Contact> {
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<Contact> {
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<Contact> {
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<Contact> {
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<Contact> {
// TODO relations
// SIP address built by buildIMPP
// SIP addresses built by buildIMPP
}
@Override
@ -766,7 +766,7 @@ public class LocalAddressBook extends LocalCollection<Contact> {
* 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<Contact> {
}
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);
}
}

View File

@ -235,29 +235,29 @@ public class LocalCalendar extends LocalCollection<Event> {
// 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);

View File

@ -122,7 +122,8 @@ public abstract class LocalCollection<T extends Resource> {
// 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());

View File

@ -65,9 +65,8 @@ public abstract class RemoteCollection<T extends Resource> {
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")

View File

@ -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;

View File

@ -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<LocalCollection<?>, RemoteCollection<?>> getSyncPairs(Account account, ContentProviderClient provider);

View File

@ -59,15 +59,13 @@ public class SyncManager {
remotelyUpdated = new HashSet<Resource>();
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);
}
}