Skip to content

Commit bcb0ab6

Browse files
Changed: Allow shortcut and icon files under ~/.termux
1 parent ce278ca commit bcb0ab6

5 files changed

Lines changed: 29 additions & 14 deletions

File tree

app/build.gradle

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,8 @@ dependencies {
6565

6666
implementation "androidx.annotation:annotation:1.2.0"
6767
implementation 'com.google.android.material:material:1.4.0'
68-
implementation 'com.termux.termux-app:termux-shared:5e2bec0f4c'
68+
implementation "com.google.guava:guava:24.1-jre"
69+
implementation 'com.termux.termux-app:termux-shared:f3ffc36bfd'
6970

7071
// Use if below libraries are published locally by termux-app with `./gradlew publishReleasePublicationToMavenLocal` and used with `mavenLocal()`.
7172
// If updates are done, republish there and sync project with gradle files here

app/src/main/java/com/termux/widget/TermuxCreateShortcutActivity.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
import androidx.annotation.Nullable;
2121

22+
import com.google.common.base.Joiner;
2223
import com.termux.shared.data.DataUtils;
2324
import com.termux.shared.file.FileUtils;
2425
import com.termux.shared.file.TermuxFileUtils;
@@ -205,9 +206,10 @@ private File getShortcutIconFile(Context context, String shortcutFileName) {
205206
return null;
206207
}
207208

208-
// Do not allow icons files not under TermuxConstants.TERMUX_SHORTCUT_SCRIPT_ICONS_DIR_PATH
209-
if (!FileUtils.isPathInDirPath(shortcutIconFilePath, TermuxConstants.TERMUX_SHORTCUT_SCRIPT_ICONS_DIR_PATH, true)) {
210-
errmsg = context.getString(R.string.error_icon_not_under_shortcut_icons_directory) +
209+
// Do not allow shortcut icons files not under SHORTCUT_ICONS_FILES_ALLOWED_PATHS_LIST
210+
if (!FileUtils.isPathInDirPaths(shortcutIconFilePath, TermuxWidgetService.SHORTCUT_ICONS_FILES_ALLOWED_PATHS_LIST, true)) {
211+
errmsg = context.getString(R.string.error_icon_not_under_shortcut_icons_directories,
212+
Joiner.on(", ").skipNulls().join(TermuxFileUtils.getUnExpandedTermuxPaths(TermuxWidgetService.SHORTCUT_ICONS_FILES_ALLOWED_PATHS_LIST))) +
211213
"\n" + context.getString(R.string.msg_icon_absolute_path, shortcutIconFilePath);
212214
Logger.logErrorAndShowToast(context, LOG_TAG, errmsg);
213215
return null;

app/src/main/java/com/termux/widget/TermuxWidgetProvider.java

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,11 @@
1111
import android.widget.RemoteViews;
1212
import android.widget.Toast;
1313

14+
import com.google.common.base.Joiner;
1415
import com.termux.shared.data.DataUtils;
1516
import com.termux.shared.data.IntentUtils;
1617
import com.termux.shared.file.FileUtils;
18+
import com.termux.shared.file.TermuxFileUtils;
1719
import com.termux.shared.file.filesystem.FileType;
1820
import com.termux.shared.logger.Logger;
1921
import com.termux.shared.models.ExecutionCommand;
@@ -181,9 +183,10 @@ public static void sendExecutionIntentToTermuxService(final Context context, Str
181183
// Get canonical path of executable
182184
executionCommand.executable = FileUtils.getCanonicalPath(executionCommand.executable, null);
183185

184-
// If executable is not under TermuxConstants#TERMUX_SHORTCUT_SCRIPTS_DIR_PATH
185-
if (!FileUtils.isPathInDirPath(executionCommand.executable, TermuxConstants.TERMUX_SHORTCUT_SCRIPTS_DIR_PATH, true)) {
186-
errmsg = context.getString(R.string.error_executable_not_under_shortcuts_directory) +
186+
// If executable is not under SHORTCUT_FILES_ALLOWED_PATHS_LIST
187+
if (!FileUtils.isPathInDirPaths(executionCommand.executable, TermuxWidgetService.SHORTCUT_FILES_ALLOWED_PATHS_LIST, true)) {
188+
errmsg = context.getString(R.string.error_executable_not_under_shortcuts_directories,
189+
Joiner.on(", ").skipNulls().join(TermuxFileUtils.getUnExpandedTermuxPaths(TermuxWidgetService.SHORTCUT_FILES_ALLOWED_PATHS_LIST))) +
187190
"\n" + context.getString(R.string.msg_executable_absolute_path, executionCommand.executable);
188191
Logger.logErrorAndShowToast(context, logTag, errmsg);
189192
return;

app/src/main/java/com/termux/widget/TermuxWidgetService.java

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,16 @@
1717

1818
public final class TermuxWidgetService extends RemoteViewsService {
1919

20+
/* Allowed paths under which shortcut files can exist. */
21+
public static final List<String> SHORTCUT_FILES_ALLOWED_PATHS_LIST = Arrays.asList(
22+
TermuxConstants.TERMUX_SHORTCUT_SCRIPTS_DIR_PATH,
23+
TermuxConstants.TERMUX_DATA_HOME_DIR_PATH);
24+
25+
/* Allowed paths under which shortcut icons files can exist. */
26+
public static final List<String> SHORTCUT_ICONS_FILES_ALLOWED_PATHS_LIST = Arrays.asList(
27+
TermuxConstants.TERMUX_SHORTCUT_SCRIPT_ICONS_DIR_PATH,
28+
TermuxConstants.TERMUX_DATA_HOME_DIR_PATH);
29+
2030
public static final FileFilter SHORTCUT_FILES_FILTER = new FileFilter() {
2131
public boolean accept(File file) {
2232
// Do not show hidden files starting with a dot.
@@ -25,10 +35,10 @@ public boolean accept(File file) {
2535
// Do not show broken symlinks
2636
else if (!FileUtils.fileExists(file.getAbsolutePath(), true))
2737
return false;
28-
// Do not show files that are not under TermuxConstants#TERMUX_SHORTCUT_SCRIPTS_DIR_PATH
29-
else if (!FileUtils.isPathInDirPath(file.getAbsolutePath(), TermuxConstants.TERMUX_SHORTCUT_SCRIPTS_DIR_PATH, true))
38+
// Do not show files that are not under SHORTCUT_FILES_ALLOWED_PATHS_LIST
39+
else if (!FileUtils.isPathInDirPaths(file.getAbsolutePath(), SHORTCUT_FILES_ALLOWED_PATHS_LIST, true))
3040
return false;
31-
// Do not show files under TermuxConstants.TERMUX_SHORTCUT_SCRIPT_ICONS_DIR_PATH
41+
// Do not show files under TERMUX_SHORTCUT_SCRIPT_ICONS_DIR_PATH
3242
else if (TermuxConstants.TERMUX_SHORTCUT_SCRIPTS_DIR.equals(file.getParentFile()) &&
3343
file.getName().equals(TermuxConstants.TERMUX_SHORTCUT_SCRIPT_ICONS_DIR_BASENAME))
3444
return false;

app/src/main/res/values/strings.xml

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,9 @@
3737
<string name="action_refresh">Refresh</string>
3838

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

0 commit comments

Comments
 (0)