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

Commit b15f5105 authored by Stefan Niedermann's avatar Stefan Niedermann
Browse files

Merge remote-tracking branch 'origin/master'

parents 86516b57 bb5c2221
Loading
Loading
Loading
Loading
+25 −0
Original line number Diff line number Diff line
@@ -6,7 +6,9 @@ import androidx.annotation.ColorInt;
import androidx.core.util.Pair;
import androidx.test.ext.junit.runners.AndroidJUnit4;

import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.junit.runner.RunWith;

import java.util.ArrayList;
@@ -98,4 +100,27 @@ public class ColorUtilTest {
            );
        }
    }

    @Rule
    public final ExpectedException exception = ExpectedException.none();

    @Test
    public void testGetCleanHexaColorString() {
        final List<Pair<String, String>> validColors = new ArrayList<>();
        validColors.add(new Pair<>("#0082C9", "#0082C9"));
        validColors.add(new Pair<>("0082C9", "#0082C9"));
        validColors.add(new Pair<>("#CCC", "#CCCCCC"));
        validColors.add(new Pair<>("ccc", "#cccccc"));
        validColors.add(new Pair<>("af0", "#aaff00"));
        validColors.add(new Pair<>("#af0", "#aaff00"));
        for (Pair<String, String> color : validColors) {
            assertEquals("Expect " + color.first + " to be cleaned up to " + color.second, color.second, ColorUtil.formatColorToParsableHexString(color.first));
        }

        final String[] invalidColors = new String[]{null, "", "cc", "c", "#a", "#55L", "55L"};
        for (String color : invalidColors) {
            exception.expect(IllegalArgumentException.class);
            ColorUtil.formatColorToParsableHexString(color);
        }
    }
}
+1 −1
Original line number Diff line number Diff line
@@ -34,7 +34,7 @@ abstract class AbstractNotesDatabase extends SQLiteOpenHelper {

    private static final int database_version = 16;
    @NonNull
    private final Context context;
    protected final Context context;

    protected static final String database_name = "OWNCLOUD_NOTES";
    protected static final String table_notes = "NOTES";
+20 −8
Original line number Diff line number Diff line
@@ -20,6 +20,7 @@ import android.util.Log;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.WorkerThread;
import androidx.core.content.ContextCompat;

import com.nextcloud.android.sso.AccountImporter;
import com.nextcloud.android.sso.exceptions.NextcloudFilesAppAccountNotFoundException;
@@ -52,6 +53,7 @@ import it.niedermann.owncloud.notes.model.LocalAccount;
import it.niedermann.owncloud.notes.model.NavigationAdapter;
import it.niedermann.owncloud.notes.model.NoteListsWidgetData;
import it.niedermann.owncloud.notes.model.SingleNoteWidgetData;
import it.niedermann.owncloud.notes.util.ColorUtil;
import it.niedermann.owncloud.notes.util.NoteUtil;

import static it.niedermann.owncloud.notes.android.activity.EditNoteActivity.ACTION_SHORTCUT;
@@ -701,10 +703,9 @@ public class NotesDatabase extends AbstractNotesDatabase {
        values.put(key_url, url);
        values.put(key_username, username);
        values.put(key_account_name, accountName);
        values.put(key_color, capabilities.getColor().substring(1));
        values.put(key_text_color, capabilities.getTextColor().substring(1));
        values.put(key_capabilities_etag, capabilities.getETag());
        db.insertOrThrow(table_accounts, null, values);
        long accountId = db.insertOrThrow(table_accounts, null, values);
        updateBrand(accountId, capabilities);
    }

    /**
@@ -792,15 +793,26 @@ public class NotesDatabase extends AbstractNotesDatabase {

    public void updateBrand(long accountId, @NonNull Capabilities capabilities) throws IllegalArgumentException {
        validateAccountId(accountId);
        // Validate color format
        Color.parseColor(capabilities.getColor());
        Color.parseColor(capabilities.getTextColor());

        String color;
        try {
            color = ColorUtil.formatColorToParsableHexString(capabilities.getColor()).substring(1);
        } catch (Exception e) {
            color = String.format("%06X", (0xFFFFFF & ContextCompat.getColor(context, R.color.defaultBrand)));
        }

        String textColor;
        try {
            textColor = ColorUtil.formatColorToParsableHexString(capabilities.getTextColor()).substring(1);
        } catch (Exception e) {
            textColor = String.format("%06X", (0xFFFFFF & ContextCompat.getColor(context, android.R.color.white)));
        }

        final SQLiteDatabase db = this.getWritableDatabase();
        final ContentValues values = new ContentValues();

        values.put(key_color, capabilities.getColor().substring(1));
        values.put(key_text_color, capabilities.getTextColor().substring(1));
        values.put(key_color, color);
        values.put(key_text_color, textColor);

        final int updatedRows = db.update(table_accounts, values, key_id + " = ?", new String[]{String.valueOf(accountId)});
        if (updatedRows == 1) {
+44 −0
Original line number Diff line number Diff line
@@ -3,6 +3,7 @@ package it.niedermann.owncloud.notes.util;
import android.graphics.Color;

import androidx.annotation.ColorInt;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.core.util.Pair;

@@ -107,4 +108,47 @@ public final class ColorUtil {
            return result;
        }
    }

    /**
     * @return well formatted string starting with a hash followed by 6 hex numbers that is parsable by {@link Color#parseColor(String)}.
     */
    public static String formatColorToParsableHexString(String input) {
        if (input == null) {
            throw new IllegalArgumentException("input color string is null");
        }
        if (isParsableValidHexColorString(input)) {
            return input;
        }
        final char[] chars = input.replaceAll("#", "").toCharArray();
        final StringBuilder sb = new StringBuilder(7).append("#");
        if (chars.length == 6) {
            sb.append(chars);
        } else if (chars.length == 3) {
            for (char c : chars) {
                sb.append(c).append(c);
            }
        } else {
            throw new IllegalArgumentException("unparsable color string: \"" + input + "\"");
        }
        final String formattedHexColor = sb.toString();
        if (isParsableValidHexColorString(formattedHexColor)) {
            return formattedHexColor;
        } else {
            throw new IllegalArgumentException("\"" + input + "\" is not a valid color string. Result of tried normalizing: " + formattedHexColor);
        }
    }

    /**
     * Checking for {@link Color#parseColor(String)} being able to parse the input is the important part because we don't know the implementation and rely on it to be able to parse the color.
     *
     * @return true, if the input starts with a hash followed by 6 characters of hex numbers and is parsable by {@link Color#parseColor(String)}.
     */
    private static boolean isParsableValidHexColorString(@NonNull String input) {
        try {
            Color.parseColor(input);
            return input.matches("#[a-fA-F0-9]{6}");
        } catch (Exception e) {
            return false;
        }
    }
}