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

Commit 62e6af83 authored by Gabor Keszthelyi's avatar Gabor Keszthelyi Committed by Marten Gajda
Browse files

Allow URIs in place of the URI field. #411 (#580)

parent 866af2be
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
@@ -512,7 +512,7 @@ public final class TaskContract
        String DESCRIPTION = "description";

        /**
         * An URL for this task. Must be a valid URL if not <code>null</code>-
         * The URL iCalendar field for this task. Must be a valid URI if not <code>null</code>-
         * <p>
         * Value: String
         * </p>
+2 −2
Original line number Diff line number Diff line
@@ -32,7 +32,7 @@ import org.dmfs.tasks.model.adapters.IntegerFieldAdapter;
import org.dmfs.tasks.model.adapters.StringFieldAdapter;
import org.dmfs.tasks.model.adapters.TimeFieldAdapter;
import org.dmfs.tasks.model.adapters.TimezoneFieldAdapter;
import org.dmfs.tasks.model.adapters.UrlFieldAdapter;
import org.dmfs.tasks.model.adapters.UriFieldAdapter;
import org.dmfs.tasks.model.constraints.AdjustPercentComplete;
import org.dmfs.tasks.model.constraints.After;
import org.dmfs.tasks.model.constraints.BeforeOrShiftTime;
@@ -140,7 +140,7 @@ public final class TaskFieldAdapters
    /**
     * Adapter for the URL of a task.
     */
    public final static UrlFieldAdapter URL = new UrlFieldAdapter(TaskContract.Tasks.URL);
    public final static UriFieldAdapter URL = new UriFieldAdapter(TaskContract.Tasks.URL);

    /**
     * Adapter for the Color of the task.
+25 −24
Original line number Diff line number Diff line
@@ -18,53 +18,54 @@ package org.dmfs.tasks.model.adapters;

import android.content.ContentValues;
import android.database.Cursor;
import android.net.Uri;

import org.dmfs.tasks.model.ContentSet;
import org.dmfs.tasks.model.OnContentChangeListener;
import org.dmfs.tasks.utils.ValidatingUri;

import java.net.MalformedURLException;
import java.net.URL;
import java.net.URISyntaxException;


/**
 * Knows how to load and store {@link URL} values in a {@link ContentSet}.
 * Knows how to load and store {@link Uri} values in a {@link ContentSet}.
 *
 * @author Marten Gajda <marten@dmfs.org>
 */
public final class UrlFieldAdapter extends FieldAdapter<URL>
public final class UriFieldAdapter extends FieldAdapter<Uri>
{

    private final String mFieldName;

    private final URL mDefaultValue;
    private final Uri mDefaultValue;


    /**
     * Constructor for a new urlFieldAdapter without default value.
     * Constructor for a new {@link UriFieldAdapter} without default value.
     *
     * @param urlField
     *         The field name that holds the URL.
     * @param uriField
     *         The field name that holds the URI.
     */
    public UrlFieldAdapter(String urlField)
    public UriFieldAdapter(String uriField)
    {
        if (urlField == null)
        if (uriField == null)
        {
            throw new IllegalArgumentException("urlField must not be null");
            throw new IllegalArgumentException("uriField must not be null");
        }
        mFieldName = urlField;
        mFieldName = uriField;
        mDefaultValue = null;
    }


    /**
     * Constructor for a new UrlFieldAdapter with default value.
     * Constructor for a new {@link UriFieldAdapter} with default value.
     *
     * @param fieldName
     * @param urlField
     *         The name of the field to use when loading or storing the value.
     * @param defaultValue
     *         The defaultValue.
     */
    public UrlFieldAdapter(String urlField, URL defaultValue)
    public UriFieldAdapter(String urlField, Uri defaultValue)
    {
        if (urlField == null)
        {
@@ -76,13 +77,13 @@ public final class UrlFieldAdapter extends FieldAdapter<URL>


    @Override
    public URL get(ContentSet values)
    public Uri get(ContentSet values)
    {
        try
        {
            return new URL(values.getAsString(mFieldName));
            return new ValidatingUri(values.getAsString(mFieldName)).value();
        }
        catch (MalformedURLException e)
        catch (URISyntaxException e)
        {
            return null;
        }
@@ -90,7 +91,7 @@ public final class UrlFieldAdapter extends FieldAdapter<URL>


    @Override
    public URL get(Cursor cursor)
    public Uri get(Cursor cursor)
    {
        int columnIdx = cursor.getColumnIndex(mFieldName);
        if (columnIdx < 0)
@@ -99,9 +100,9 @@ public final class UrlFieldAdapter extends FieldAdapter<URL>
        }
        try
        {
            return new URL(cursor.getString(columnIdx));
            return new ValidatingUri(cursor.getString(columnIdx)).value();
        }
        catch (MalformedURLException e)
        catch (URISyntaxException e)
        {
            return null;
        }
@@ -109,14 +110,14 @@ public final class UrlFieldAdapter extends FieldAdapter<URL>


    @Override
    public URL getDefault(ContentSet values)
    public Uri getDefault(ContentSet values)
    {
        return mDefaultValue;
    }


    @Override
    public void set(ContentSet values, URL value)
    public void set(ContentSet values, Uri value)
    {
        if (value == null)
        {
@@ -130,7 +131,7 @@ public final class UrlFieldAdapter extends FieldAdapter<URL>


    @Override
    public void set(ContentValues values, URL value)
    public void set(ContentValues values, Uri value)
    {
        if (value == null)
        {
+37 −0
Original line number Diff line number Diff line
/*
 * Copyright 2017 dmfs GmbH
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.dmfs.tasks.utils;

import org.dmfs.jems.single.Single;

import java.util.Iterator;


/**
 * A Fragile is similar to a {@link Single} but it may throw an {@link Exception} during retrieval of the value.
 * <p>
 * It's primary use case is to allow deferring checked Exceptions if they can't be thrown right away. A common example is Iterating elements, {@link
 * Iterator#next()} doesn't allow checked Exceptions to be thrown. In this case a {@link Fragile} can be returned to defer any exception to evaluation time.
 *
 * @author Marten Gajda
 * @deprecated use it from jems when available
 */
@Deprecated
public interface Fragile<T, E extends Throwable>
{
    T value() throws E;
}
+57 −0
Original line number Diff line number Diff line
/*
 * Copyright 2017 dmfs GmbH
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.dmfs.tasks.utils;

import android.net.Uri;
import android.support.annotation.Nullable;

import java.net.URI;
import java.net.URISyntaxException;


/**
 * A {@link Fragile} to create a valid {@link Uri} from a {@link Nullable} {@link CharSequence}.
 * <p>
 * Throws {@link URISyntaxException} for invalid or <code>null</code> or empty input.
 *
 * @author Gabor Keszthelyi
 */
public final class ValidatingUri implements Fragile<Uri, URISyntaxException>
{
    private final CharSequence mUriCandidate;


    public ValidatingUri(@Nullable CharSequence uriCandidate)
    {
        mUriCandidate = uriCandidate;
    }


    @Override
    public Uri value() throws URISyntaxException
    {
        if (mUriCandidate == null)
        {
            throw new URISyntaxException("null", "Uri input cannot be null");
        }
        if (mUriCandidate.length() == 0)
        {
            throw new URISyntaxException("", "Uri input cannot be empty");
        }
        return Uri.parse(new URI(mUriCandidate.toString()).toString());
    }
}
Loading