Skip to content

Commit

Permalink
FileBrowser: Shortcut to special files (gsantner#2259 by @harshad1)
Browse files Browse the repository at this point in the history
* Fix for regex action with keep selection

---------

Co-authored-by: Gregor Santner <[email protected]>
  • Loading branch information
harshad1 and gsantner authored Apr 20, 2024
1 parent 100b189 commit 742c43d
Show file tree
Hide file tree
Showing 53 changed files with 148 additions and 43 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ protected void onNewIntent(final Intent intent) {
super.onNewIntent(intent);
final File file = MarkorContextUtils.getValidIntentFile(intent, null);
if (_notebook != null && file != null) {
if (file.isDirectory()) {
if (file.isDirectory() || GsFileBrowserListAdapter.isVirtualFolder(file)) {
_notebook.post(() -> _notebook.setCurrentFolder(file));
} else {
_notebook.post(() -> _notebook.getAdapter().showFile(file));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import net.gsantner.markor.activity.MainActivity;
import net.gsantner.markor.activity.MarkorBaseActivity;
import net.gsantner.markor.util.MarkorContextUtils;
import net.gsantner.opoc.frontend.filebrowser.GsFileBrowserListAdapter;

import java.io.File;

Expand All @@ -31,7 +32,7 @@ protected void onNewIntent(final Intent intent) {
private void launchActivityAndFinish(Intent intent) {
final Intent newIntent = new Intent(intent);
final File intentFile = MarkorContextUtils.getIntentFile(intent, null);
if (intentFile != null && intentFile.isDirectory()) {
if (intentFile != null && (intentFile.isDirectory() || GsFileBrowserListAdapter.isVirtualFolder(intentFile))) {
newIntent.setClass(this, MainActivity.class);
startActivity(newIntent);
} else {
Expand Down
55 changes: 28 additions & 27 deletions app/src/main/java/net/gsantner/markor/format/ActionButtonBase.java
Original file line number Diff line number Diff line change
Expand Up @@ -492,43 +492,36 @@ public static void runRegexReplaceAction(final Editable editable, final ReplaceP

private static void runRegexReplaceAction(final Editable editable, final List<ReplacePattern> patterns) {

final int[] sel = TextViewUtils.getSelection(editable);
final TextViewUtils.ChunkedEditable text = TextViewUtils.ChunkedEditable.wrap(editable);
TextViewUtils.withKeepSelection(editable, (selStart, selEnd) -> {

// Offset of selection start from text end - used to restore selection
final int selEndOffset = text.length() - sel[1];
// Offset of selection start from line end - used to restore selection
final int selStartOffset = sel[1] == sel[0] ? selEndOffset : TextViewUtils.getLineEnd(text, sel[0]) - sel[0];
final TextViewUtils.ChunkedEditable text = TextViewUtils.ChunkedEditable.wrap(editable);
// Start of line on which sel begins
final int selStartStart = TextViewUtils.getLineStart(text, selStart);

// Start of line on which sel begins
final int selStartStart = TextViewUtils.getLineStart(text, sel[0]);
// Number of lines we will be modifying
final int lineCount = TextViewUtils.countChars(text, sel[0], sel[1], '\n')[0] + 1;
// Number of lines we will be modifying
final int lineCount = TextViewUtils.countChars(text, selStart, selEnd, '\n')[0] + 1;
int lineStart = selStartStart;

int lineStart = selStartStart;

for (int i = 0; i < lineCount; i++) {
for (int i = 0; i < lineCount; i++) {

int lineEnd = TextViewUtils.getLineEnd(text, lineStart);
final String line = TextViewUtils.toString(text, lineStart, lineEnd);
int lineEnd = TextViewUtils.getLineEnd(text, lineStart);
final String line = TextViewUtils.toString(text, lineStart, lineEnd);

for (final ReplacePattern pattern : patterns) {
if (pattern.matcher.reset(line).find()) {
if (!pattern.isSameReplace()) {
text.replace(lineStart, lineEnd, pattern.replace());
for (final ReplacePattern pattern : patterns) {
if (pattern.matcher.reset(line).find()) {
if (!pattern.isSameReplace()) {
text.replace(lineStart, lineEnd, pattern.replace());
}
break;
}
break;
}
}

lineStart = TextViewUtils.getLineEnd(text, lineStart) + 1;
}

text.applyChanges();
lineStart = TextViewUtils.getLineEnd(text, lineStart) + 1;
}

final int newSelEnd = text.length() - selEndOffset;
final int newSelStart = sel[0] == sel[1] ? newSelEnd : TextViewUtils.getLineEnd(text, selStartStart) - selStartOffset;
Selection.setSelection(editable, newSelStart, newSelEnd);
text.applyChanges();
});
}

protected void runSurroundAction(final String delim) {
Expand Down Expand Up @@ -909,6 +902,14 @@ public static void duplicateLineSelection(final HighlightingEditor hlEditor) {
TextViewUtils.getIndexFromLineOffset(text, selEnd));
}

public void withKeepSelection(final GsCallback.a2<Integer, Integer> action) {
_hlEditor.withAutoFormatDisabled(() -> TextViewUtils.withKeepSelection(_hlEditor.getText(), action));
}

public void withKeepSelection(final GsCallback.a0 action) {
withKeepSelection((start, end) -> action.callback());
}

// Derived classes should override this to implement format-specific renumber logic
protected void renumberOrderedList() {
// No-op in base class
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ public class TodoTxtTask {
public static final SimpleDateFormat DATEF_YYYY_MM_DD = new SimpleDateFormat("yyyy-MM-dd", Locale.ROOT);
public static final int DATEF_YYYY_MM_DD_LEN = "yyyy-MM-dd".length();
public static final String PT_DATE = "\\d{4}-\\d{2}-\\d{2}";
public static final Pattern PATTERN_PROJECTS = Pattern.compile("\\B(?:\\++)(\\S+)");
public static final Pattern PATTERN_CONTEXTS = Pattern.compile("\\B(?:\\@+)(\\S+)");
public static final Pattern PATTERN_PROJECTS = Pattern.compile("(?:^|\\s)(?:\\++)(\\S+)");
public static final Pattern PATTERN_CONTEXTS = Pattern.compile("(?:^|\\s)(?:\\@+)(\\S+)");
public static final Pattern PATTERN_DONE = Pattern.compile("(?m)(^[Xx]) (.*)$");
public static final Pattern PATTERN_DATE = Pattern.compile("(?:^|\\s|:)(" + PT_DATE + ")(?:$|\\s)");
public static final Pattern PATTERN_KEY_VALUE_PAIRS__TAG_ONLY = Pattern.compile("(?i)([a-z]+):([a-z0-9_-]+)");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import androidx.annotation.NonNull;

import net.gsantner.opoc.util.GsContextUtils;
import net.gsantner.opoc.wrapper.GsCallback;

import java.lang.reflect.Array;
import java.util.ArrayList;
Expand Down Expand Up @@ -145,6 +146,22 @@ public static int[] getSelection(final CharSequence text) {
}
}

public static void withKeepSelection(final Editable text, final GsCallback.a2<Integer, Integer> action) {
final int[] sel = TextViewUtils.getSelection(text);
final int[] selStart = TextViewUtils.getLineOffsetFromIndex(text, sel[0]);
final int[] selEnd = TextViewUtils.getLineOffsetFromIndex(text, sel[1]);

action.callback(sel[0], sel[1]);

Selection.setSelection(text,
TextViewUtils.getIndexFromLineOffset(text, selStart),
TextViewUtils.getIndexFromLineOffset(text, selEnd));
}

public static void withKeepSelection(final Editable text, final GsCallback.a0 action) {
withKeepSelection(text, (start, end) -> action.callback());
}

public static String getSelectedText(final CharSequence text) {
final int[] sel = getSelection(text);
return (sel[0] >= 0 && sel[1] >= 0) ? text.subSequence(sel[0], sel[1]).toString() : "";
Expand Down
20 changes: 18 additions & 2 deletions app/src/main/java/net/gsantner/markor/util/MarkorContextUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import android.text.TextUtils;
import android.webkit.WebView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.RequiresApi;

Expand All @@ -25,6 +26,7 @@
import net.gsantner.markor.activity.openeditor.OpenFromShortcutOrWidgetActivity;
import net.gsantner.markor.activity.openeditor.OpenShareIntoActivity;
import net.gsantner.markor.model.Document;
import net.gsantner.opoc.frontend.filebrowser.GsFileBrowserListAdapter;
import net.gsantner.opoc.util.GsContextUtils;
import net.gsantner.opoc.util.GsFileUtils;

Expand All @@ -46,13 +48,27 @@ public <T extends GsContextUtils> T applySpecialLaunchersVisibility(final Contex
return thisp();
}

private static int getIconResForFile(final @NonNull File file) {
if (file.equals(GsFileBrowserListAdapter.VIRTUAL_STORAGE_POPULAR)) {
return R.mipmap.ic_shortcut_popular;
} else if (file.equals(GsFileBrowserListAdapter.VIRTUAL_STORAGE_RECENTS)) {
return R.mipmap.ic_shortcut_recent;
} else if (file.equals(GsFileBrowserListAdapter.VIRTUAL_STORAGE_FAVOURITE)) {
return R.mipmap.ic_shortcut_favourite;
} else if (file.isDirectory()) {
return R.mipmap.ic_shortcut_folder;
} else {
return R.mipmap.ic_shortcut_file;
}
}

public <T extends GsContextUtils> T createLauncherDesktopShortcut(final Context context, final File file) {
// This is only allowed to call when direct file access is possible!!
// So basically only for java.io.File Objects. Virtual files, or content://
// in private/restricted space won't work - because of missing permission grant when re-launching
final String title = file != null ? GsFileUtils.getFilenameWithoutExtension(file) : null;
if (!TextUtils.isEmpty(title)) {
final int iconRes = file.isDirectory() ? R.mipmap.ic_shortcut_folder : R.mipmap.ic_shortcut_file;
final int iconRes = getIconResForFile(file);
final Intent intent = new Intent(context, OpenFromShortcutOrWidgetActivity.class).setData(Uri.fromFile(file));
createLauncherDesktopShortcut(context, intent, iconRes, title);
}
Expand Down Expand Up @@ -94,6 +110,6 @@ public static File getIntentFile(final Intent intent, final File fallback) {

public static File getValidIntentFile(final Intent intent, final File fallback) {
final File f = getIntentFile(intent, null);
return f != null && f.exists() ? f : fallback;
return f != null && (f.exists() || GsFileBrowserListAdapter.isVirtualFolder(f)) ? f : fallback;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -328,7 +328,6 @@ public static void forEachline(final CharSequence text, GsCallback.b3<Integer, I
if (!callback.callback(i, start, end)) {
break;
}
;
start = end + 1;
}
callback.callback(i, start, text.length());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -663,7 +663,7 @@ private void importFileToCurrentDirectory(Context context, File sourceFile) {
}

public void setCurrentFolder(final File folder) {
if (folder != null && folder.canRead() && _filesystemViewerAdapter != null) {
if (folder != null && (folder.canRead() || GsFileBrowserListAdapter.isVirtualFolder(folder)) && _filesystemViewerAdapter != null) {
_filesystemViewerAdapter.setCurrentFolder(folder);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,9 @@ public class GsFileBrowserListAdapter extends RecyclerView.Adapter<GsFileBrowser
//## Static
//########################
public static final File VIRTUAL_STORAGE_ROOT = new File("/storage/");
public static final File VIRTUAL_STORAGE_RECENTS = new File(VIRTUAL_STORAGE_ROOT, "recent-files");
public static final File VIRTUAL_STORAGE_FAVOURITE = new File(VIRTUAL_STORAGE_ROOT, "favourite-files");
public static final File VIRTUAL_STORAGE_POPULAR = new File(VIRTUAL_STORAGE_ROOT, "popular-files");
public static final File VIRTUAL_STORAGE_RECENTS = new File(VIRTUAL_STORAGE_ROOT, "Recent");
public static final File VIRTUAL_STORAGE_FAVOURITE = new File(VIRTUAL_STORAGE_ROOT, "Favourites");
public static final File VIRTUAL_STORAGE_POPULAR = new File(VIRTUAL_STORAGE_ROOT, "Popular");
public static final File VIRTUAL_STORAGE_APP_DATA_PRIVATE = new File(VIRTUAL_STORAGE_ROOT, "appdata-private");
private static final StrikethroughSpan STRIKE_THROUGH_SPAN = new StrikethroughSpan();
public static final String EXTRA_CURRENT_FOLDER = "EXTRA_CURRENT_FOLDER";
Expand Down
File renamed without changes.
File renamed without changes.
20 changes: 20 additions & 0 deletions app/src/main/res/drawable/ic_shortcut_favourite_foreground.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="108dp"
android:height="108dp"
android:viewportWidth="24"
android:viewportHeight="24"
android:tint="#363B5E">
<group android:scaleX="0.5"
android:scaleY="0.5"
android:translateX="6"
android:translateY="6">
<group android:scaleX="0.75"
android:scaleY="0.75"
android:translateX="3"
android:translateY="3">
<path
android:fillColor="#FF000000"
android:pathData="M12,17.27L18.18,21l-1.64,-7.03L22,9.24l-7.19,-0.61L12,2 9.19,8.63 2,9.24l5.46,4.73L5.82,21z"/>
</group>
</group>
</vector>
20 changes: 20 additions & 0 deletions app/src/main/res/drawable/ic_shortcut_popular_foreground.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="108dp"
android:height="108dp"
android:viewportWidth="24"
android:viewportHeight="24"
android:tint="#363B5E">
<group android:scaleX="0.5"
android:scaleY="0.5"
android:translateX="6"
android:translateY="6">
<group android:scaleX="0.5"
android:scaleY="0.5"
android:translateX="6"
android:translateY="6">
<path
android:fillColor="#FF000000"
android:pathData="M12,21.35l-1.45,-1.32C5.4,15.36 2,12.28 2,8.5 2,5.42 4.42,3 7.5,3c1.74,0 3.41,0.81 4.5,2.09C13.09,3.81 14.76,3 16.5,3 19.58,3 22,5.42 22,8.5c0,3.78 -3.4,6.86 -8.55,11.54L12,21.35z"/>
</group>
</group>
</vector>
20 changes: 20 additions & 0 deletions app/src/main/res/drawable/ic_shortcut_recent_foreground.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="108dp"
android:height="108dp"
android:viewportWidth="24"
android:viewportHeight="24"
android:tint="#363B5E">
<group android:scaleX="0.5"
android:scaleY="0.5"
android:translateX="6"
android:translateY="6">
<group android:scaleX="0.75"
android:scaleY="0.75"
android:translateX="2.5"
android:translateY="3">
<path
android:fillColor="#FF000000"
android:pathData="M13,3c-4.97,0 -9,4.03 -9,9L1,12l3.89,3.89 0.07,0.14L9,12L6,12c0,-3.87 3.13,-7 7,-7s7,3.13 7,7 -3.13,7 -7,7c-1.93,0 -3.68,-0.79 -4.94,-2.06l-1.42,1.42C8.27,19.99 10.51,21 13,21c4.97,0 9,-4.03 9,-9s-4.03,-9 -9,-9zM12,8v5l4.28,2.54 0.72,-1.21 -3.5,-2.08L13.5,8L12,8z"/>
</group>
</group>
</vector>
File renamed without changes.
File renamed without changes.
5 changes: 5 additions & 0 deletions app/src/main/res/mipmap-anydpi-v26/ic_shortcut_favourite.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<adaptive-icon xmlns:android="http://schemas.android.com/apk/res/android">
<background android:drawable="@color/ic_shortcut_background"/>
<foreground android:drawable="@drawable/ic_shortcut_favourite_foreground"/>
</adaptive-icon>
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<adaptive-icon xmlns:android="http://schemas.android.com/apk/res/android">
<background android:drawable="@color/ic_shortcut_folder_background"/>
<background android:drawable="@color/ic_shortcut_background"/>
<foreground android:drawable="@drawable/ic_shortcut_folder_foreground"/>
</adaptive-icon>
5 changes: 5 additions & 0 deletions app/src/main/res/mipmap-anydpi-v26/ic_shortcut_popular.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<adaptive-icon xmlns:android="http://schemas.android.com/apk/res/android">
<background android:drawable="@color/ic_shortcut_background"/>
<foreground android:drawable="@drawable/ic_shortcut_popular_foreground"/>
</adaptive-icon>
5 changes: 5 additions & 0 deletions app/src/main/res/mipmap-anydpi-v26/ic_shortcut_recent.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<adaptive-icon xmlns:android="http://schemas.android.com/apk/res/android">
<background android:drawable="@color/ic_shortcut_background"/>
<foreground android:drawable="@drawable/ic_shortcut_recent_foreground"/>
</adaptive-icon>
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added app/src/main/res/mipmap-mdpi/ic_shortcut_popular.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
File renamed without changes.
4 changes: 0 additions & 4 deletions app/thirdparty/res/values/ic_shortcut_folder_background.xml

This file was deleted.

0 comments on commit 742c43d

Please sign in to comment.