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

Commit f99f4e60 authored by Stefan Niedermann's avatar Stefan Niedermann Committed by Niedermann IT-Dienstleistungen
Browse files

Possible fix for #854 NullPointer during login attempt

parent b7c637a4
Loading
Loading
Loading
Loading
+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 = Integer.toHexString(ContextCompat.getColor(context, R.color.defaultBrand) & 0x00ffffff);
        }

        String textColor;
        try {
            textColor = ColorUtil.formatColorToParsableHexString(capabilities.getTextColor()).substring(1);
        } catch (Exception e) {
            textColor = Integer.toHexString(ContextCompat.getColor(context, android.R.color.white) & 0x00ffffff);
        }

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