diff options
author | Stefan Niedermann <info@niedermann.it> | 2021-02-11 14:00:26 +0300 |
---|---|---|
committer | Stefan Niedermann <info@niedermann.it> | 2021-02-11 14:08:53 +0300 |
commit | 0c01f258c25d6fa2b162730324ea0e3a821c8fd6 (patch) | |
tree | 987ef5826ee393ead604d6e88d2896b6c1fb8c5f /app/src/main/java/it/niedermann/owncloud/notes/shared | |
parent | a0112185804066d054f555ecc8fcf9ab61f39ca5 (diff) | |
parent | 4a0c16fdada3b3f84937be57f1ce355f1cfb9a0e (diff) |
Merge branch 'master' into 831-room
# Conflicts:
# app/build.gradle
# app/src/main/java/it/niedermann/owncloud/notes/AppendToNoteActivity.java
# app/src/main/java/it/niedermann/owncloud/notes/edit/EditNoteActivity.java
# app/src/main/java/it/niedermann/owncloud/notes/main/MainActivity.java
# app/src/main/java/it/niedermann/owncloud/notes/persistence/NotesDatabase.java
# app/src/main/java/it/niedermann/owncloud/notes/shared/model/CloudNote.java
# app/src/main/java/it/niedermann/owncloud/notes/shared/util/DisplayUtils.java
# app/src/main/java/it/niedermann/owncloud/notes/shared/util/NoteUtil.java
Diffstat (limited to 'app/src/main/java/it/niedermann/owncloud/notes/shared')
4 files changed, 37 insertions, 140 deletions
diff --git a/app/src/main/java/it/niedermann/owncloud/notes/shared/model/CloudNote.java b/app/src/main/java/it/niedermann/owncloud/notes/shared/model/CloudNote.java new file mode 100644 index 00000000..e69de29b --- /dev/null +++ b/app/src/main/java/it/niedermann/owncloud/notes/shared/model/CloudNote.java 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 db696d85..ad6b6793 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 @@ -1,22 +1,3 @@ -/* - * Nextcloud Notes application - * - * @author Mario Danic - * Copyright (C) 2018 Mario Danic <mario@lovelyhq.com> - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see <http://www.gnu.org/licenses/>. - */ package it.niedermann.owncloud.notes.shared.util; import android.content.Context; @@ -52,91 +33,6 @@ public class DisplayUtils { } - public static Spannable searchAndColor(Spannable spannable, CharSequence searchText, @NonNull Context context, @Nullable Integer current, @ColorInt int mainColor, @ColorInt int textColor) { - CharSequence text = spannable.toString(); - - Object[] spansToRemove = spannable.getSpans(0, text.length(), Object.class); - for (Object span : spansToRemove) { - if (span instanceof SearchSpan) - spannable.removeSpan(span); - } - - if (TextUtils.isEmpty(text) || TextUtils.isEmpty(searchText)) { - return spannable; - } - - Matcher m = Pattern.compile(searchText.toString(), Pattern.CASE_INSENSITIVE | Pattern.LITERAL) - .matcher(text); - - int i = 1; - while (m.find()) { - int start = m.start(); - int end = m.end(); - spannable.setSpan(new SearchSpan(context, mainColor, textColor, (current != null && i == current)), start, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); - i++; - } - - return spannable; - } - - - static class SearchSpan extends MetricAffectingSpan { - - private final boolean current; - @NonNull - Context context; - @ColorInt - private final int mainColor; - @ColorInt - private final int textColor; - @ColorInt - private final int highlightColor; - - SearchSpan(@NonNull Context context, @ColorInt int mainColor, @ColorInt int textColor, boolean current) { - this.context = context; - this.mainColor = mainColor; - this.textColor = textColor; - this.current = current; - this.highlightColor = ContextCompat.getColor(context, R.color.bg_highlighted); - } - - @Override - public void updateDrawState(TextPaint tp) { - if (current) { - if (NotesApplication.isDarkThemeActive(context)) { - if (ColorUtil.INSTANCE.isColorDark(mainColor)) { - tp.bgColor = Color.WHITE; - tp.setColor(mainColor); - } else { - tp.bgColor = mainColor; - tp.setColor(Color.BLACK); - } - } else { - if (ColorUtil.INSTANCE.isColorDark(mainColor)) { - tp.bgColor = mainColor; - tp.setColor(Color.WHITE); - } else { - if (NotesColorUtil.contrastRatioIsSufficient(mainColor, highlightColor)) { - tp.bgColor = highlightColor; - } else { - tp.bgColor = Color.BLACK; - } - tp.setColor(mainColor); - } - } - } else { - tp.bgColor = highlightColor; - tp.setColor(BrandingUtil.getSecondaryForegroundColorDependingOnTheme(context, mainColor)); - } - tp.setFakeBoldText(true); - } - - @Override - public void updateMeasureState(@NonNull TextPaint tp) { - tp.setFakeBoldText(true); - } - } - public static List<NavigationItem.CategoryNavigationItem> convertToCategoryNavigationItem(@NonNull Context context, @NonNull Collection<CategoryWithNotesCount> counter) { return counter.stream() .map(ctr -> convertToCategoryNavigationItem(context, ctr)) diff --git a/app/src/main/java/it/niedermann/owncloud/notes/shared/util/NoteUtil.java b/app/src/main/java/it/niedermann/owncloud/notes/shared/util/NoteUtil.java index 9254de82..32caeaaf 100644 --- a/app/src/main/java/it/niedermann/owncloud/notes/shared/util/NoteUtil.java +++ b/app/src/main/java/it/niedermann/owncloud/notes/shared/util/NoteUtil.java @@ -7,10 +7,10 @@ import android.text.TextUtils; import androidx.annotation.NonNull; import androidx.annotation.Nullable; -import java.util.regex.Pattern; - import it.niedermann.owncloud.notes.R; +import static it.niedermann.android.markdown.MarkdownUtil.removeMarkdown; + /** * Provides basic functionality for Note operations. * Created by stefan on 06.10.15. @@ -18,13 +18,6 @@ import it.niedermann.owncloud.notes.R; @SuppressWarnings("WeakerAccess") public class NoteUtil { - private static final Pattern pLists = Pattern.compile("^\\s*[*+-]\\s+", Pattern.MULTILINE); - private static final Pattern pHeadings = Pattern.compile("^#+\\s+(.*?)\\s*#*$", Pattern.MULTILINE); - private static final Pattern pHeadingLine = Pattern.compile("^(?:=*|-*)$", Pattern.MULTILINE); - private static final Pattern pEmphasis = Pattern.compile("(\\*+|_+)(.*?)\\1", Pattern.MULTILINE); - private static final Pattern pSpace1 = Pattern.compile("^\\s+", Pattern.MULTILINE); - private static final Pattern pSpace2 = Pattern.compile("\\s+$", Pattern.MULTILINE); - public static final String EXCERPT_LINE_SEPARATOR = " "; private NoteUtil() { @@ -32,26 +25,6 @@ public class NoteUtil { } /** - * Strips all MarkDown from the given String - * - * @param s String - MarkDown - * @return Plain Text-String - */ - @NonNull - public static String removeMarkDown(@Nullable String s) { - if (s == null) - return ""; - String result = s; - result = pLists.matcher(result).replaceAll(""); - result = pHeadings.matcher(result).replaceAll("$1"); - result = pHeadingLine.matcher(result).replaceAll(""); - result = pEmphasis.matcher(result).replaceAll("$2"); - result = pSpace1.matcher(result).replaceAll(""); - result = pSpace2.matcher(result).replaceAll(""); - return result; - } - - /** * Checks if a line is empty. * <pre> * " " -> empty @@ -65,7 +38,7 @@ public class NoteUtil { * @return boolean isEmpty */ public static boolean isEmptyLine(@Nullable String line) { - return removeMarkDown(line).trim().length() == 0; + return removeMarkdown(line).trim().length() == 0; } /** @@ -90,12 +63,12 @@ public class NoteUtil { */ @NonNull public static String generateNoteExcerpt(@NonNull String content, @Nullable String title) { - content = removeMarkDown(content.trim()); + content = removeMarkdown(content.trim()); if (TextUtils.isEmpty(content)) { return ""; } if (!TextUtils.isEmpty(title)) { - final String trimmedTitle = removeMarkDown(title.trim()); + final String trimmedTitle = removeMarkdown(title.trim()); if (content.startsWith(trimmedTitle)) { content = content.substring(trimmedTitle.length()); } @@ -120,18 +93,18 @@ public class NoteUtil { */ @NonNull public static String generateNoteTitle(@NonNull String content) { - return getLineWithoutMarkDown(content, 0); + return getLineWithoutMarkdown(content, 0); } /** - * Reads the requested line and strips all MarkDown. If line is empty, it will go ahead to find the next not-empty line. + * Reads the requested line and strips all Markdown. If line is empty, it will go ahead to find the next not-empty line. * * @param content String * @param lineNumber int * @return lineContent String */ @NonNull - public static String getLineWithoutMarkDown(@NonNull String content, int lineNumber) { + public static String getLineWithoutMarkdown(@NonNull String content, int lineNumber) { String line = ""; if (content.contains("\n")) { String[] lines = content.split("\n"); @@ -140,7 +113,7 @@ public class NoteUtil { currentLine++; } if (currentLine < lines.length) { - line = NoteUtil.removeMarkDown(lines[currentLine]); + line = removeMarkdown(lines[currentLine]); } } else { line = content; diff --git a/app/src/main/java/it/niedermann/owncloud/notes/shared/util/ShareUtil.java b/app/src/main/java/it/niedermann/owncloud/notes/shared/util/ShareUtil.java index 09ad7124..115d18dd 100644 --- a/app/src/main/java/it/niedermann/owncloud/notes/shared/util/ShareUtil.java +++ b/app/src/main/java/it/niedermann/owncloud/notes/shared/util/ShareUtil.java @@ -6,6 +6,11 @@ import android.content.Intent; import androidx.annotation.NonNull; import androidx.annotation.Nullable; +import java.net.MalformedURLException; +import java.net.URL; + +import it.niedermann.android.markdown.MarkdownUtil; + import static android.content.ClipDescription.MIMETYPE_TEXT_PLAIN; public class ShareUtil { @@ -17,4 +22,27 @@ public class ShareUtil { .putExtra(Intent.EXTRA_TITLE, subject) .putExtra(Intent.EXTRA_TEXT, text), subject)); } + + public static String extractSharedText(@NonNull Intent intent) { + final String text = intent.getStringExtra(Intent.EXTRA_TEXT); + if (intent.hasExtra(Intent.EXTRA_SUBJECT)) { + final String subject = intent.getStringExtra(Intent.EXTRA_SUBJECT); + try { + new URL(text); + if (text != null && subject != null && !subject.trim().isEmpty()) { + return MarkdownUtil.getMarkdownLink(subject, text); + } else { + return text; + } + } catch (MalformedURLException e) { + if (subject != null && !subject.trim().isEmpty()) { + return subject + ": " + text; + } else { + return text; + } + } + } else { + return text; + } + } } |