From 9886507b7d42265fcb269831317bd37ba1f2e202 Mon Sep 17 00:00:00 2001 From: Ricki Hirner Date: Fri, 20 May 2016 21:38:04 +0200 Subject: [PATCH] Minor improvements * use weak references for DavService RefreshingStatusListener * additional null checks for cases which shouldn't appear, but apparently appear * additional database conflict handling for cases which shouldn't appear, but apparently appear * setup by URL: null check for empty host names * vcard4android: upgrade to ezvcard 0.9.10 --- app/build.gradle | 7 +---- .../java/at/bitfire/davdroid/DavService.java | 29 +++++++++++++------ .../bitfire/davdroid/ui/AccountActivity.java | 21 +++++++++----- .../ui/setup/AccountDetailsFragment.java | 6 ++-- .../ui/setup/LoginCredentialsFragment.java | 15 ++++++---- ical4android | 2 +- vcard4android | 2 +- 7 files changed, 48 insertions(+), 34 deletions(-) diff --git a/app/build.gradle b/app/build.gradle index 02c06371..6996fe71 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -55,13 +55,7 @@ android { } } -configurations.all { - exclude module: 'commons-logging' // undocumented part of Android -} - dependencies { - provided 'org.projectlombok:lombok:1.16.8' - compile project(':dav4android') compile project(':ical4android') compile project(':vcard4android') @@ -77,6 +71,7 @@ dependencies { compile 'dnsjava:dnsjava:2.1.7' compile 'org.apache.commons:commons-lang3:3.4' compile 'org.apache.commons:commons-collections4:4.1' + provided 'org.projectlombok:lombok:1.16.8' // for tests testCompile 'junit:junit:4.12' diff --git a/app/src/main/java/at/bitfire/davdroid/DavService.java b/app/src/main/java/at/bitfire/davdroid/DavService.java index 58492087..ff4ea019 100644 --- a/app/src/main/java/at/bitfire/davdroid/DavService.java +++ b/app/src/main/java/at/bitfire/davdroid/DavService.java @@ -31,6 +31,7 @@ import org.apache.commons.collections4.iterators.IteratorChain; import org.apache.commons.collections4.iterators.SingletonIterator; import java.io.IOException; +import java.lang.ref.WeakReference; import java.util.HashSet; import java.util.Iterator; import java.util.LinkedHashMap; @@ -70,7 +71,7 @@ public class DavService extends Service { private final IBinder binder = new InfoBinder(); private final Set runningRefresh = new HashSet<>(); - private final List refreshingStatusListeners = new LinkedList<>(); + private final List> refreshingStatusListeners = new LinkedList<>(); @Override @@ -86,8 +87,11 @@ public class DavService extends Service { case ACTION_REFRESH_COLLECTIONS: if (runningRefresh.add(id)) { new Thread(new RefreshCollections(id)).start(); - for (RefreshingStatusListener listener : refreshingStatusListeners) - listener.onDavRefreshStatusChanged(id, true); + for (WeakReference ref : refreshingStatusListeners) { + RefreshingStatusListener listener = ref.get(); + if (listener != null) + listener.onDavRefreshStatusChanged(id, true); + } } break; } @@ -115,15 +119,19 @@ public class DavService extends Service { return runningRefresh.contains(id); } - public void addRefreshingStatusListener(RefreshingStatusListener listener, boolean callImmediate) { - refreshingStatusListeners.add(listener); + public void addRefreshingStatusListener(@NonNull RefreshingStatusListener listener, boolean callImmediate) { + refreshingStatusListeners.add(new WeakReference<>(listener)); if (callImmediate) for (long id : runningRefresh) listener.onDavRefreshStatusChanged(id, true); } - public void removeRefreshingStatusListener(RefreshingStatusListener listener) { - refreshingStatusListeners.remove(listener); + public void removeRefreshingStatusListener(@NonNull RefreshingStatusListener listener) { + for (Iterator> iterator = refreshingStatusListeners.iterator(); iterator.hasNext(); ) { + RefreshingStatusListener item = iterator.next().get(); + if (listener.equals(item)) + iterator.remove(); + } } } @@ -314,8 +322,11 @@ public class DavService extends Service { dbHelper.close(); runningRefresh.remove(service); - for (RefreshingStatusListener listener : refreshingStatusListeners) - listener.onDavRefreshStatusChanged(service, false); + for (WeakReference ref : refreshingStatusListeners) { + RefreshingStatusListener listener = ref.get(); + if (listener != null) + listener.onDavRefreshStatusChanged(service, false); + } } } diff --git a/app/src/main/java/at/bitfire/davdroid/ui/AccountActivity.java b/app/src/main/java/at/bitfire/davdroid/ui/AccountActivity.java index 577d2e16..f2d12832 100644 --- a/app/src/main/java/at/bitfire/davdroid/ui/AccountActivity.java +++ b/app/src/main/java/at/bitfire/davdroid/ui/AccountActivity.java @@ -149,12 +149,15 @@ public class AccountActivity extends AppCompatActivity implements Toolbar.OnMenu @Override public boolean onMenuItemClick(MenuItem item) { + Intent intent; switch (item.getItemId()) { case R.id.refresh_address_books: - Intent intent = new Intent(this, DavService.class); - intent.setAction(DavService.ACTION_REFRESH_COLLECTIONS); - intent.putExtra(DavService.EXTRA_DAV_SERVICE_ID, accountInfo.carddav.id); - startService(intent); + if (accountInfo.carddav != null) { + intent = new Intent(this, DavService.class); + intent.setAction(DavService.ACTION_REFRESH_COLLECTIONS); + intent.putExtra(DavService.EXTRA_DAV_SERVICE_ID, accountInfo.carddav.id); + startService(intent); + } break; case R.id.create_address_book: intent = new Intent(this, CreateAddressBookActivity.class); @@ -162,10 +165,12 @@ public class AccountActivity extends AppCompatActivity implements Toolbar.OnMenu startActivity(intent); break; case R.id.refresh_calendars: - intent = new Intent(this, DavService.class); - intent.setAction(DavService.ACTION_REFRESH_COLLECTIONS); - intent.putExtra(DavService.EXTRA_DAV_SERVICE_ID, accountInfo.caldav.id); - startService(intent); + if (accountInfo.caldav != null) { + intent = new Intent(this, DavService.class); + intent.setAction(DavService.ACTION_REFRESH_COLLECTIONS); + intent.putExtra(DavService.EXTRA_DAV_SERVICE_ID, accountInfo.caldav.id); + startService(intent); + } break; case R.id.create_calendar: intent = new Intent(this, CreateCalendarActivity.class); diff --git a/app/src/main/java/at/bitfire/davdroid/ui/setup/AccountDetailsFragment.java b/app/src/main/java/at/bitfire/davdroid/ui/setup/AccountDetailsFragment.java index 9271dc07..a7ddcd53 100644 --- a/app/src/main/java/at/bitfire/davdroid/ui/setup/AccountDetailsFragment.java +++ b/app/src/main/java/at/bitfire/davdroid/ui/setup/AccountDetailsFragment.java @@ -163,21 +163,21 @@ public class AccountDetailsFragment extends Fragment { values.put(Services.SERVICE, service); if (info.principal != null) values.put(Services.PRINCIPAL, info.principal.toString()); - long serviceID = db.insertOrThrow(Services._TABLE, null, values); + long serviceID = db.insertWithOnConflict(Services._TABLE, null, values, SQLiteDatabase.CONFLICT_REPLACE); // insert home sets for (URI homeSet : info.homeSets) { values.clear(); values.put(HomeSets.SERVICE_ID, serviceID); values.put(HomeSets.URL, homeSet.toString()); - db.insertOrThrow(HomeSets._TABLE, null, values); + db.insertWithOnConflict(HomeSets._TABLE, null, values, SQLiteDatabase.CONFLICT_REPLACE); } // insert collections for (CollectionInfo collection : info.collections.values()) { values = collection.toDB(); values.put(Collections.SERVICE_ID, serviceID); - db.insertOrThrow(Collections._TABLE, null, values); + db.insertWithOnConflict(Collections._TABLE, null, values, SQLiteDatabase.CONFLICT_REPLACE); } return serviceID; diff --git a/app/src/main/java/at/bitfire/davdroid/ui/setup/LoginCredentialsFragment.java b/app/src/main/java/at/bitfire/davdroid/ui/setup/LoginCredentialsFragment.java index 8949b007..029c2df9 100644 --- a/app/src/main/java/at/bitfire/davdroid/ui/setup/LoginCredentialsFragment.java +++ b/app/src/main/java/at/bitfire/davdroid/ui/setup/LoginCredentialsFragment.java @@ -21,6 +21,8 @@ import android.widget.EditText; import android.widget.LinearLayout; import android.widget.RadioButton; +import org.apache.commons.lang3.StringUtils; + import java.net.IDN; import java.net.URI; import java.net.URISyntaxException; @@ -121,14 +123,15 @@ public class LoginCredentialsFragment extends Fragment implements CompoundButton String scheme = baseUrl.getScheme(); if ("https".equalsIgnoreCase(scheme) || "http".equalsIgnoreCase(scheme)) { String host = baseUrl.getHost(); - if (host.isEmpty()) { + if (StringUtils.isEmpty(host)) { editBaseURL.setError(getString(R.string.login_url_host_name_required)); valid = false; - } else try { - host = IDN.toASCII(host); - } catch(IllegalArgumentException e) { - Constants.log.log(Level.WARNING, "Host name not conforming to RFC 3490", e); - } + } else + try { + host = IDN.toASCII(host); + } catch(IllegalArgumentException e) { + Constants.log.log(Level.WARNING, "Host name not conforming to RFC 3490", e); + } String path = baseUrl.getEncodedPath(); int port = baseUrl.getPort(); diff --git a/ical4android b/ical4android index 3c86db90..7577fcaa 160000 --- a/ical4android +++ b/ical4android @@ -1 +1 @@ -Subproject commit 3c86db909052c9d01d1979a9a5c32ef0fefd69dd +Subproject commit 7577fcaa02d83ea5fbeeefa35511b45f3285ce95 diff --git a/vcard4android b/vcard4android index 046cdbf7..fda39538 160000 --- a/vcard4android +++ b/vcard4android @@ -1 +1 @@ -Subproject commit 046cdbf76911b2979c19e9219b069386d4b4cba7 +Subproject commit fda39538a3407f2a6f6a208f06a4ef1dbc1bed40