Skip to content

Commit

Permalink
Changed: Allow shortcut and icon files under ~/.termux
Browse files Browse the repository at this point in the history
  • Loading branch information
agnostic-apollo committed Sep 23, 2021
1 parent ce278ca commit bcb0ab6
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 14 deletions.
3 changes: 2 additions & 1 deletion app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,8 @@ dependencies {

implementation "androidx.annotation:annotation:1.2.0"
implementation 'com.google.android.material:material:1.4.0'
implementation 'com.termux.termux-app:termux-shared:5e2bec0f4c'
implementation "com.google.guava:guava:24.1-jre"
implementation 'com.termux.termux-app:termux-shared:f3ffc36bfd'

// Use if below libraries are published locally by termux-app with `./gradlew publishReleasePublicationToMavenLocal` and used with `mavenLocal()`.
// If updates are done, republish there and sync project with gradle files here
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

import androidx.annotation.Nullable;

import com.google.common.base.Joiner;
import com.termux.shared.data.DataUtils;
import com.termux.shared.file.FileUtils;
import com.termux.shared.file.TermuxFileUtils;
Expand Down Expand Up @@ -205,9 +206,10 @@ private File getShortcutIconFile(Context context, String shortcutFileName) {
return null;
}

// Do not allow icons files not under TermuxConstants.TERMUX_SHORTCUT_SCRIPT_ICONS_DIR_PATH
if (!FileUtils.isPathInDirPath(shortcutIconFilePath, TermuxConstants.TERMUX_SHORTCUT_SCRIPT_ICONS_DIR_PATH, true)) {
errmsg = context.getString(R.string.error_icon_not_under_shortcut_icons_directory) +
// Do not allow shortcut icons files not under SHORTCUT_ICONS_FILES_ALLOWED_PATHS_LIST
if (!FileUtils.isPathInDirPaths(shortcutIconFilePath, TermuxWidgetService.SHORTCUT_ICONS_FILES_ALLOWED_PATHS_LIST, true)) {
errmsg = context.getString(R.string.error_icon_not_under_shortcut_icons_directories,
Joiner.on(", ").skipNulls().join(TermuxFileUtils.getUnExpandedTermuxPaths(TermuxWidgetService.SHORTCUT_ICONS_FILES_ALLOWED_PATHS_LIST))) +
"\n" + context.getString(R.string.msg_icon_absolute_path, shortcutIconFilePath);
Logger.logErrorAndShowToast(context, LOG_TAG, errmsg);
return null;
Expand Down
9 changes: 6 additions & 3 deletions app/src/main/java/com/termux/widget/TermuxWidgetProvider.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,11 @@
import android.widget.RemoteViews;
import android.widget.Toast;

import com.google.common.base.Joiner;
import com.termux.shared.data.DataUtils;
import com.termux.shared.data.IntentUtils;
import com.termux.shared.file.FileUtils;
import com.termux.shared.file.TermuxFileUtils;
import com.termux.shared.file.filesystem.FileType;
import com.termux.shared.logger.Logger;
import com.termux.shared.models.ExecutionCommand;
Expand Down Expand Up @@ -181,9 +183,10 @@ public static void sendExecutionIntentToTermuxService(final Context context, Str
// Get canonical path of executable
executionCommand.executable = FileUtils.getCanonicalPath(executionCommand.executable, null);

// If executable is not under TermuxConstants#TERMUX_SHORTCUT_SCRIPTS_DIR_PATH
if (!FileUtils.isPathInDirPath(executionCommand.executable, TermuxConstants.TERMUX_SHORTCUT_SCRIPTS_DIR_PATH, true)) {
errmsg = context.getString(R.string.error_executable_not_under_shortcuts_directory) +
// If executable is not under SHORTCUT_FILES_ALLOWED_PATHS_LIST
if (!FileUtils.isPathInDirPaths(executionCommand.executable, TermuxWidgetService.SHORTCUT_FILES_ALLOWED_PATHS_LIST, true)) {
errmsg = context.getString(R.string.error_executable_not_under_shortcuts_directories,
Joiner.on(", ").skipNulls().join(TermuxFileUtils.getUnExpandedTermuxPaths(TermuxWidgetService.SHORTCUT_FILES_ALLOWED_PATHS_LIST))) +
"\n" + context.getString(R.string.msg_executable_absolute_path, executionCommand.executable);
Logger.logErrorAndShowToast(context, logTag, errmsg);
return;
Expand Down
16 changes: 13 additions & 3 deletions app/src/main/java/com/termux/widget/TermuxWidgetService.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,16 @@

public final class TermuxWidgetService extends RemoteViewsService {

/* Allowed paths under which shortcut files can exist. */
public static final List<String> SHORTCUT_FILES_ALLOWED_PATHS_LIST = Arrays.asList(
TermuxConstants.TERMUX_SHORTCUT_SCRIPTS_DIR_PATH,
TermuxConstants.TERMUX_DATA_HOME_DIR_PATH);

/* Allowed paths under which shortcut icons files can exist. */
public static final List<String> SHORTCUT_ICONS_FILES_ALLOWED_PATHS_LIST = Arrays.asList(
TermuxConstants.TERMUX_SHORTCUT_SCRIPT_ICONS_DIR_PATH,
TermuxConstants.TERMUX_DATA_HOME_DIR_PATH);

public static final FileFilter SHORTCUT_FILES_FILTER = new FileFilter() {
public boolean accept(File file) {
// Do not show hidden files starting with a dot.
Expand All @@ -25,10 +35,10 @@ public boolean accept(File file) {
// Do not show broken symlinks
else if (!FileUtils.fileExists(file.getAbsolutePath(), true))
return false;
// Do not show files that are not under TermuxConstants#TERMUX_SHORTCUT_SCRIPTS_DIR_PATH
else if (!FileUtils.isPathInDirPath(file.getAbsolutePath(), TermuxConstants.TERMUX_SHORTCUT_SCRIPTS_DIR_PATH, true))
// Do not show files that are not under SHORTCUT_FILES_ALLOWED_PATHS_LIST
else if (!FileUtils.isPathInDirPaths(file.getAbsolutePath(), SHORTCUT_FILES_ALLOWED_PATHS_LIST, true))
return false;
// Do not show files under TermuxConstants.TERMUX_SHORTCUT_SCRIPT_ICONS_DIR_PATH
// Do not show files under TERMUX_SHORTCUT_SCRIPT_ICONS_DIR_PATH
else if (TermuxConstants.TERMUX_SHORTCUT_SCRIPTS_DIR.equals(file.getParentFile()) &&
file.getName().equals(TermuxConstants.TERMUX_SHORTCUT_SCRIPT_ICONS_DIR_BASENAME))
return false;
Expand Down
7 changes: 3 additions & 4 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,9 @@
<string name="action_refresh">Refresh</string>

<string name="error_null_or_empty_executable">The executable is null or empty.</string>
<string name="error_executable_not_under_shortcuts_directory">An executable can only be
executed if its under the &TERMUX_SHORTCUT_SCRIPTS_DIR_PATH_SHORT; directory.</string>
<string name="error_icon_not_under_shortcut_icons_directory">The icon is not under the
&TERMUX_SHORTCUT_SCRIPT_ICONS_DIR_PATH_SHORT; directory.</string>
<string name="error_executable_not_under_shortcuts_directories">An executable can only be
executed if its under the following directories: %1$s</string>
<string name="error_icon_not_under_shortcut_icons_directories">The icon is not under the following directories: %1$s</string>
<string name="error_icon_not_a_regular_file">The icon file is not a regular file but is instead
a \"%1$s\".</string>
<string name="error_create_pinned_shortcut_failed">Failed to create pinned shortcut for \"%1$s\"</string>
Expand Down

0 comments on commit bcb0ab6

Please sign in to comment.