Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 68ee33b2 authored by Ricki Hirner's avatar Ricki Hirner
Browse files

Resource detection, bug fixes

* resource detection is subject to change yet
* don't use UID_2445 for Android <= 4.1
* more useful sync error notification messages
* handle 401 Unauthorized and show account info when notification is tapped
parent fa766906
Loading
Loading
Loading
Loading
+3 −2
Original line number Diff line number Diff line
@@ -126,7 +126,8 @@ public class HttpClient extends OkHttpClient {
        // add User-Agent to every request
        networkInterceptors().add(userAgentInterceptor);

        // enable logs
        // enable verbose logs, if requested
        if (Constants.log.isTraceEnabled())
            enableLogs();
    }

+104 −95
Original line number Diff line number Diff line
@@ -58,6 +58,7 @@ public class DavResourceFinder {
        final HttpClient httpClient = new HttpClient(context, serverInfo.getUserName(), serverInfo.getPassword(), serverInfo.authPreemptive);

        // CardDAV
        try {
            Constants.log.info("*** CardDAV resource detection ***");
            HttpUrl principalUrl = getCurrentUserPrincipal(httpClient, serverInfo, "carddav");

@@ -99,12 +100,16 @@ public class DavResourceFinder {
                }
                serverInfo.setAddressBooks(addressBooks);
            }
        } catch(IOException|HttpException|DavException e) {
            Constants.log.info("CardDAV detection failed", e);
        }

        // CalDAV
        Constants.log.info("*** CalDAV resource detection ***");
        principalUrl = getCurrentUserPrincipal(httpClient, serverInfo, "caldav");
        try {
            HttpUrl principalUrl = getCurrentUserPrincipal(httpClient, serverInfo, "caldav");

        principal = new DavResource(httpClient, principalUrl);
            DavResource principal = new DavResource(httpClient, principalUrl);
            principal.propfind(0, CalendarHomeSet.NAME);
            CalendarHomeSet calHomeSet = (CalendarHomeSet) principal.properties.get(CalendarHomeSet.NAME);
            if (calHomeSet != null && !calHomeSet.hrefs.isEmpty()) {
@@ -168,7 +173,11 @@ public class DavResourceFinder {
                serverInfo.setCalendars(calendars);
                serverInfo.setTaskLists(taskLists);
            }
        }  catch(IOException|HttpException|DavException e) {
            Constants.log.info("CalDAV detection failed", e);
        }

        // TODO
		/*if (!serverInfo.isCalDAV() && !serverInfo.isCardDAV())
			throw new DavIncapableException(context.getString(R.string.setup_neither_caldav_nor_carddav));*/
	}
+3 −2
Original line number Diff line number Diff line
@@ -10,6 +10,7 @@ package at.bitfire.davdroid.resource;

import android.content.ContentProviderOperation;
import android.content.ContentValues;
import android.os.Build;
import android.os.RemoteException;
import android.provider.CalendarContract;
import android.provider.CalendarContract.Events;
@@ -32,8 +33,8 @@ public class LocalEvent extends AndroidEvent implements LocalResource {
    }

    static final String COLUMN_ETAG = CalendarContract.Events.SYNC_DATA1,
                        COLUMN_UID = CalendarContract.Events.UID_2445,
                        COLUMN_SEQUENCE = CalendarContract.Events.SYNC_DATA2;
                        COLUMN_UID = Build.VERSION.SDK_INT >= 17 ? Events.UID_2445 : Events.SYNC_DATA2,
                        COLUMN_SEQUENCE = CalendarContract.Events.SYNC_DATA3;

    @Getter protected String fileName;
    @Getter @Setter protected String eTag;
+2 −2
Original line number Diff line number Diff line
@@ -36,8 +36,8 @@ public class LocalTask extends AndroidTask implements LocalResource {
    }

    static final String COLUMN_ETAG = Tasks.SYNC1,
                        COLUMN_UID = Tasks._UID,
                        COLUMN_SEQUENCE = Tasks.SYNC2;
                        COLUMN_UID = Tasks.SYNC2,
                        COLUMN_SEQUENCE = Tasks.SYNC3;

    @Getter protected String fileName;
    @Getter @Setter protected String eTag;
+32 −8
Original line number Diff line number Diff line
@@ -33,6 +33,7 @@ import java.util.UUID;
import at.bitfire.dav4android.DavResource;
import at.bitfire.dav4android.exception.DavException;
import at.bitfire.dav4android.exception.HttpException;
import at.bitfire.dav4android.exception.UnauthorizedException;
import at.bitfire.dav4android.exception.PreconditionFailedException;
import at.bitfire.dav4android.exception.ServiceUnavailableException;
import at.bitfire.dav4android.property.GetCTag;
@@ -43,6 +44,7 @@ import at.bitfire.davdroid.R;
import at.bitfire.davdroid.resource.LocalCollection;
import at.bitfire.davdroid.resource.LocalResource;
import at.bitfire.davdroid.ui.DebugInfoActivity;
import at.bitfire.davdroid.ui.settings.AccountActivity;
import at.bitfire.ical4android.CalendarStorageException;
import at.bitfire.vcard4android.ContactsStorageException;

@@ -165,18 +167,36 @@ abstract public class SyncManager {
            }

        } catch(Exception e) {
            if (e instanceof HttpException || e instanceof DavException) {
            final int messageString;

            if (e instanceof UnauthorizedException) {
                Constants.log.error("Not authorized anymore", e);
                messageString = R.string.sync_error_unauthorized;
                syncResult.stats.numAuthExceptions++;
            } else if (e instanceof HttpException || e instanceof DavException) {
                Constants.log.error("HTTP/DAV Exception during sync", e);
                messageString = R.string.sync_error_http_dav;
                syncResult.stats.numParseExceptions++;
            } else if (e instanceof CalendarStorageException || e instanceof ContactsStorageException) {
                Constants.log.error("Couldn't access local storage", e);
                messageString = R.string.sync_error_local_storage;
                syncResult.databaseError = true;
            } else {
                Constants.log.error("Unknown sync error", e);
                messageString = R.string.sync_error;
                syncResult.stats.numParseExceptions++;
            }

            Intent detailsIntent = new Intent(context, DebugInfoActivity.class);
            final Intent detailsIntent;
            if (e instanceof UnauthorizedException) {
                detailsIntent = new Intent(context, AccountActivity.class);
                detailsIntent.putExtra(AccountActivity.EXTRA_ACCOUNT, account);
            } else {
                detailsIntent = new Intent(context, DebugInfoActivity.class);
                detailsIntent.putExtra(DebugInfoActivity.KEY_EXCEPTION, e);
                detailsIntent.putExtra(DebugInfoActivity.KEY_ACCOUNT, account);
                detailsIntent.putExtra(DebugInfoActivity.KEY_PHASE, syncPhase);
            }

            Notification.Builder builder = new Notification.Builder(context);
            Notification notification;
@@ -184,9 +204,13 @@ abstract public class SyncManager {
                    .setContentTitle(context.getString(R.string.sync_error_title, account.name))
                    .setContentIntent(PendingIntent.getActivity(context, 0, detailsIntent, PendingIntent.FLAG_UPDATE_CURRENT));

            try {
                String[] phases = context.getResources().getStringArray(R.array.sync_error_phases);
            if (phases.length > syncPhase)
                builder.setContentText(context.getString(R.string.sync_error_http, phases[syncPhase]));
                String message = context.getString(messageString, phases[syncPhase]);
                builder.setContentText(message);
            } catch (IndexOutOfBoundsException ex) {
                // should never happen
            }

            if (Build.VERSION.SDK_INT >= 16) {
                if (Build.VERSION.SDK_INT >= 21)
Loading