diff options
Diffstat (limited to 'app/src/main/java/it/niedermann/owncloud/notes/shared')
8 files changed, 5 insertions, 284 deletions
diff --git a/app/src/main/java/it/niedermann/owncloud/notes/shared/util/DisplayUtils.java b/app/src/main/java/it/niedermann/owncloud/notes/shared/util/DisplayUtils.java index fcd418e8..db696d85 100644 --- a/app/src/main/java/it/niedermann/owncloud/notes/shared/util/DisplayUtils.java +++ b/app/src/main/java/it/niedermann/owncloud/notes/shared/util/DisplayUtils.java @@ -30,6 +30,7 @@ import android.text.style.MetricAffectingSpan; import androidx.annotation.ColorInt; import androidx.annotation.NonNull; import androidx.annotation.Nullable; +import androidx.core.content.ContextCompat; import java.util.Collection; import java.util.List; @@ -96,7 +97,7 @@ public class DisplayUtils { this.mainColor = mainColor; this.textColor = textColor; this.current = current; - this.highlightColor = context.getResources().getColor(R.color.bg_highlighted); + this.highlightColor = ContextCompat.getColor(context, R.color.bg_highlighted); } @Override diff --git a/app/src/main/java/it/niedermann/owncloud/notes/shared/util/MarkDownUtil.java b/app/src/main/java/it/niedermann/owncloud/notes/shared/util/MarkDownUtil.java deleted file mode 100644 index 43a8937e..00000000 --- a/app/src/main/java/it/niedermann/owncloud/notes/shared/util/MarkDownUtil.java +++ /dev/null @@ -1,124 +0,0 @@ -package it.niedermann.owncloud.notes.shared.util; - -import android.content.Context; -import android.graphics.Color; -import android.text.Spanned; -import android.text.TextUtils; - -import androidx.annotation.NonNull; -import androidx.core.content.res.ResourcesCompat; - -import com.yydcdut.markdown.MarkdownConfiguration; -import com.yydcdut.markdown.MarkdownConfiguration.Builder; -import com.yydcdut.markdown.MarkdownProcessor; -import com.yydcdut.markdown.span.MDImageSpan; -import com.yydcdut.markdown.theme.ThemeDefault; -import com.yydcdut.markdown.theme.ThemeSonsOfObsidian; - -import it.niedermann.owncloud.notes.NotesApplication; -import it.niedermann.owncloud.notes.R; - -/** - * Created by stefan on 07.12.16. - */ - -@SuppressWarnings("WeakerAccess") -public class MarkDownUtil { - - private static final String TAG = MarkDownUtil.class.getSimpleName(); - - public static final String CHECKBOX_UNCHECKED_MINUS = "- [ ]"; - public static final String CHECKBOX_UNCHECKED_MINUS_TRAILING_SPACE = CHECKBOX_UNCHECKED_MINUS + " "; - public static final String CHECKBOX_UNCHECKED_STAR = "* [ ]"; - public static final String CHECKBOX_UNCHECKED_STAR_TRAILING_SPACE = CHECKBOX_UNCHECKED_STAR + " "; - public static final String CHECKBOX_CHECKED_MINUS = "- [x]"; - public static final String CHECKBOX_CHECKED_STAR = "* [x]"; - - private static final String MD_IMAGE_WITH_EMPTY_DESCRIPTION = "![]("; - private static final String MD_IMAGE_WITH_SPACE_DESCRIPTION = "![ ]("; - private static final String[] MD_IMAGE_WITH_EMPTY_DESCRIPTION_ARRAY = new String[]{MD_IMAGE_WITH_EMPTY_DESCRIPTION}; - private static final String[] MD_IMAGE_WITH_SPACE_DESCRIPTION_ARRAY = new String[]{MD_IMAGE_WITH_SPACE_DESCRIPTION}; - - /** - * Ensures every instance of RxMD uses the same configuration - * - * @param context Context - * @return RxMDConfiguration - */ - public static Builder getMarkDownConfiguration(Context context) { - return getMarkDownConfiguration(context, NotesApplication.isDarkThemeActive(context)); - } - - public static Builder getMarkDownConfiguration(Context context, Boolean darkTheme) { - return new MarkdownConfiguration.Builder(context) - .setUnOrderListColor(ResourcesCompat.getColor(context.getResources(), - darkTheme ? R.color.widget_fg_dark_theme : R.color.widget_fg_default, null)) - .setHeader2RelativeSize(1.35f) - .setHeader3RelativeSize(1.25f) - .setHeader4RelativeSize(1.15f) - .setHeader5RelativeSize(1.1f) - .setHeader6RelativeSize(1.05f) - .setHorizontalRulesHeight(2) - .setCodeBgColor(darkTheme ? ResourcesCompat.getColor(context.getResources(), R.color.fg_default_high, null) : Color.LTGRAY) - .setTheme(darkTheme ? new ThemeSonsOfObsidian() : new ThemeDefault()) - .setTodoColor(ResourcesCompat.getColor(context.getResources(), - darkTheme ? R.color.widget_fg_dark_theme : R.color.widget_fg_default, null)) - .setTodoDoneColor(ResourcesCompat.getColor(context.getResources(), - darkTheme ? R.color.widget_fg_dark_theme : R.color.widget_fg_default, null)) - .setLinkFontColor(ResourcesCompat.getColor(context.getResources(), R.color.defaultBrand, null)) - .setDefaultImageSize(400, 300); - } - - /** - * This is a compatibility-method that provides workarounds for several bugs in RxMarkdown - * <p> - * https://github.com/stefan-niedermann/nextcloud-notes/issues/772 - * - * @param markdownProcessor RxMarkdown MarkdownProcessor instance - * @param text CharSequence that should be parsed - * @return the processed text but with several workarounds for Bugs in RxMarkdown - */ - @NonNull - public static CharSequence parseCompat(@NonNull final MarkdownProcessor markdownProcessor, CharSequence text) { - if (TextUtils.isEmpty(text)) { - return ""; - } - - while (TextUtils.indexOf(text, MD_IMAGE_WITH_EMPTY_DESCRIPTION) >= 0) { - text = TextUtils.replace(text, MD_IMAGE_WITH_EMPTY_DESCRIPTION_ARRAY, MD_IMAGE_WITH_SPACE_DESCRIPTION_ARRAY); - } - - return markdownProcessor.parse(text); - } - - public static boolean containsImageSpan(@NonNull CharSequence text) { - return ((Spanned) text).getSpans(0, text.length(), MDImageSpan.class).length > 0; - } - - public static boolean lineStartsWithCheckbox(@NonNull String line) { - return lineStartsWithCheckbox(line, true) || lineStartsWithCheckbox(line, false); - } - - public static boolean lineStartsWithCheckbox(@NonNull String line, boolean starAsLeadingCharacter) { - return starAsLeadingCharacter - ? line.startsWith(CHECKBOX_UNCHECKED_STAR) || line.startsWith(CHECKBOX_CHECKED_STAR) - : line.startsWith(CHECKBOX_UNCHECKED_MINUS) || line.startsWith(CHECKBOX_CHECKED_MINUS); - } - - public static int getStartOfLine(@NonNull CharSequence s, int cursorPosition) { - int startOfLine = cursorPosition; - while (startOfLine > 0 && s.charAt(startOfLine - 1) != '\n') { - startOfLine--; - } - return startOfLine; - } - - public static int getEndOfLine(@NonNull CharSequence s, int cursorPosition) { - int nextLinebreak = s.toString().indexOf('\n', cursorPosition); - if (nextLinebreak > -1) { - return nextLinebreak; - } - return cursorPosition; - } -} - diff --git a/app/src/main/java/it/niedermann/owncloud/notes/shared/util/NoteLinksUtils.java b/app/src/main/java/it/niedermann/owncloud/notes/shared/util/NoteLinksUtils.java deleted file mode 100644 index 668d2746..00000000 --- a/app/src/main/java/it/niedermann/owncloud/notes/shared/util/NoteLinksUtils.java +++ /dev/null @@ -1,26 +0,0 @@ -package it.niedermann.owncloud.notes.shared.util; - -import it.niedermann.owncloud.notes.shared.util.text.NoteLinksProcessor; - -public class NoteLinksUtils { - - /** - * Tests if the given link is a note-link (which was transformed in {@link it.niedermann.owncloud.notes.shared.util.text.NoteLinksProcessor}) or not - * - * @param link Link under test - * @return true if the link is a note-link - */ - public static boolean isNoteLink(String link) { - return link.startsWith(NoteLinksProcessor.RELATIVE_LINK_WORKAROUND_PREFIX); - } - - /** - * Extracts the remoteId back from links that were transformed in {@link it.niedermann.owncloud.notes.shared.util.text.NoteLinksProcessor}. - * - * @param link Link that was transformed in {@link it.niedermann.owncloud.notes.shared.util.text.NoteLinksProcessor} - * @return the remoteId of the linked note - */ - public static long extractNoteRemoteId(String link) { - return Long.parseLong(link.replace(NoteLinksProcessor.RELATIVE_LINK_WORKAROUND_PREFIX, "")); - } -} diff --git a/app/src/main/java/it/niedermann/owncloud/notes/shared/util/SupportUtil.java b/app/src/main/java/it/niedermann/owncloud/notes/shared/util/SupportUtil.java index 9c117190..8bf80cb9 100644 --- a/app/src/main/java/it/niedermann/owncloud/notes/shared/util/SupportUtil.java +++ b/app/src/main/java/it/niedermann/owncloud/notes/shared/util/SupportUtil.java @@ -1,14 +1,10 @@ package it.niedermann.owncloud.notes.shared.util; -import android.os.Build; -import android.text.Html; -import android.text.Spanned; import android.text.method.LinkMovementMethod; import android.widget.TextView; import androidx.annotation.NonNull; - -import static android.os.Build.VERSION_CODES.N; +import androidx.core.text.HtmlCompat; /** * Some helper functionality in alike the Android support library. @@ -28,24 +24,8 @@ public class SupportUtil { * @param formatArgs Arguments for the string resource. */ public static void setHtml(@NonNull TextView view, int stringId, Object... formatArgs) { - view.setText(SupportUtil.fromHtml(view.getResources().getString(stringId, formatArgs))); + view.setText(HtmlCompat.fromHtml( + view.getResources().getString(stringId, formatArgs), HtmlCompat.FROM_HTML_MODE_LEGACY)); view.setMovementMethod(LinkMovementMethod.getInstance()); } - - /** - * Creates a {@link Spanned} from a HTML string on all SDK versions. - * - * @param source Source string with HTML markup - * @return Spannable for using in a {@link TextView} - * @see Html#fromHtml(String) - * @see Html#fromHtml(String, int) - */ - private static Spanned fromHtml(String source) { - if (Build.VERSION.SDK_INT >= N) { - return Html.fromHtml(source, Html.FROM_HTML_MODE_LEGACY); - } else { - //noinspection deprecation - return Html.fromHtml(source); - } - } } diff --git a/app/src/main/java/it/niedermann/owncloud/notes/shared/util/text/NoteLinksProcessor.java b/app/src/main/java/it/niedermann/owncloud/notes/shared/util/text/NoteLinksProcessor.java deleted file mode 100644 index a8c6926e..00000000 --- a/app/src/main/java/it/niedermann/owncloud/notes/shared/util/text/NoteLinksProcessor.java +++ /dev/null @@ -1,63 +0,0 @@ -package it.niedermann.owncloud.notes.shared.util.text; - -import android.text.TextUtils; -import android.util.Log; - -import androidx.annotation.VisibleForTesting; - -import java.util.HashSet; -import java.util.Set; -import java.util.regex.Matcher; -import java.util.regex.Pattern; - -public class NoteLinksProcessor extends TextProcessor { - - private static final String TAG = NoteLinksProcessor.class.getSimpleName(); - public static final String RELATIVE_LINK_WORKAROUND_PREFIX = "https://nextcloudnotes/notes/"; - - @VisibleForTesting - private static final String linksThatLookLikeNoteLinksRegEx = "\\[[^]]*]\\((\\d+)\\)"; - private static final String replaceNoteRemoteIdsRegEx = "\\[([^\\]]*)\\]\\((%s)\\)"; - - private Set<Long> existingNoteRemoteIds; - - public NoteLinksProcessor(Set<Long> existingNoteRemoteIds) { - this.existingNoteRemoteIds = existingNoteRemoteIds; - } - - /** - * Replaces all links to other notes of the form `[<link-text>](<note-file-id>)` - * in the markdown string with links to a dummy url. - * <p> - * Why is this needed? - * See discussion in issue #623 - * - * @return Markdown with all note-links replaced with dummy-url-links - */ - @Override - public String process(String s) { - return replaceNoteLinksWithDummyUrls(s, existingNoteRemoteIds); - } - - private static String replaceNoteLinksWithDummyUrls(String markdown, Set<Long> existingNoteRemoteIds) { - Pattern noteLinkCandidates = Pattern.compile(linksThatLookLikeNoteLinksRegEx); - Matcher matcher = noteLinkCandidates.matcher(markdown); - - Set<String> noteRemoteIdsToReplace = new HashSet<>(); - while (matcher.find()) { - String presumedNoteId = matcher.group(1); - try { - if (presumedNoteId != null && existingNoteRemoteIds.contains(Long.parseLong(presumedNoteId))) { - noteRemoteIdsToReplace.add(presumedNoteId); - } - } catch (NumberFormatException e) { - Log.w(TAG, e); - } - } - - String noteRemoteIdsCondition = TextUtils.join("|", noteRemoteIdsToReplace); - Pattern replacePattern = Pattern.compile(String.format(replaceNoteRemoteIdsRegEx, noteRemoteIdsCondition)); - Matcher replaceMatcher = replacePattern.matcher(markdown); - return replaceMatcher.replaceAll(String.format("[$1](%s$2)", RELATIVE_LINK_WORKAROUND_PREFIX)); - } -} diff --git a/app/src/main/java/it/niedermann/owncloud/notes/shared/util/text/TextProcessor.java b/app/src/main/java/it/niedermann/owncloud/notes/shared/util/text/TextProcessor.java deleted file mode 100644 index cd17ad17..00000000 --- a/app/src/main/java/it/niedermann/owncloud/notes/shared/util/text/TextProcessor.java +++ /dev/null @@ -1,11 +0,0 @@ -package it.niedermann.owncloud.notes.shared.util.text; - -abstract public class TextProcessor { - /** - * Applies a specified transformation on a text string and returns the updated string. - * - * @param s Text to transform - * @return Transformed text - */ - abstract public String process(String s); -}
\ No newline at end of file diff --git a/app/src/main/java/it/niedermann/owncloud/notes/shared/util/text/TextProcessorChain.java b/app/src/main/java/it/niedermann/owncloud/notes/shared/util/text/TextProcessorChain.java deleted file mode 100644 index 70af737a..00000000 --- a/app/src/main/java/it/niedermann/owncloud/notes/shared/util/text/TextProcessorChain.java +++ /dev/null @@ -1,12 +0,0 @@ -package it.niedermann.owncloud.notes.shared.util.text; - -import java.util.LinkedList; - -public class TextProcessorChain extends LinkedList<TextProcessor> { - public String apply(String s) { - for (TextProcessor textProcessor : this) { - s = textProcessor.process(s); - } - return s; - } -} diff --git a/app/src/main/java/it/niedermann/owncloud/notes/shared/util/text/WwwLinksProcessor.java b/app/src/main/java/it/niedermann/owncloud/notes/shared/util/text/WwwLinksProcessor.java deleted file mode 100644 index 73cc5832..00000000 --- a/app/src/main/java/it/niedermann/owncloud/notes/shared/util/text/WwwLinksProcessor.java +++ /dev/null @@ -1,24 +0,0 @@ -package it.niedermann.owncloud.notes.shared.util.text; - -import java.util.regex.Pattern; - -public class WwwLinksProcessor extends TextProcessor { - - private static final String WWW_URLS_PROTOCOL_PREFIX = "https://"; - private static final String REGEX_REPLACE_WWW_URLS = "\\[([^]]*)]\\((www\\..+)\\)"; - - /** - * Prefixes all links, that not not start with a protocol identifier, but with "www." with http:// - * <p> - * See https://github.com/stefan-niedermann/nextcloud-notes/issues/949 - * - * @return Markdown with all pseudo-links replaced through actual HTTP-links - */ - @Override - public String process(String s) { - return Pattern - .compile(REGEX_REPLACE_WWW_URLS) - .matcher(s) - .replaceAll(String.format("[$1](%s$2)", WWW_URLS_PROTOCOL_PREFIX)); - } -} |