diff --git a/app/src/main/java/com/termux/app/terminal/TermuxTerminalViewClient.java b/app/src/main/java/com/termux/app/terminal/TermuxTerminalViewClient.java index b0e5bf6a..39ca33d9 100644 --- a/app/src/main/java/com/termux/app/terminal/TermuxTerminalViewClient.java +++ b/app/src/main/java/com/termux/app/terminal/TermuxTerminalViewClient.java @@ -420,6 +420,10 @@ public class TermuxTerminalViewClient implements TerminalViewClient { reportString.append("\n\n").append(TermuxUtils.getAppInfoMarkdownString(mActivity, true)); reportString.append("\n\n").append(TermuxUtils.getDeviceInfoMarkdownString(mActivity)); + String termuxAptInfo = TermuxUtils.geAPTInfoMarkdownString(mActivity); + if (termuxAptInfo != null) + reportString.append("\n\n").append(termuxAptInfo); + ReportActivity.startReportActivity(mActivity, new ReportInfo(UserAction.REPORT_ISSUE_FROM_TRANSCRIPT, TermuxConstants.TERMUX_APP.TERMUX_ACTIVITY_NAME, title, null, reportString.toString(), "\n\n" + TermuxUtils.getReportIssueMarkdownString(mActivity), false)); } diff --git a/termux-shared/src/main/java/com/termux/shared/termux/TermuxUtils.java b/termux-shared/src/main/java/com/termux/shared/termux/TermuxUtils.java index aabce776..374355c7 100644 --- a/termux-shared/src/main/java/com/termux/shared/termux/TermuxUtils.java +++ b/termux-shared/src/main/java/com/termux/shared/termux/TermuxUtils.java @@ -14,12 +14,17 @@ import com.google.common.base.Joiner; import com.termux.shared.R; import com.termux.shared.logger.Logger; import com.termux.shared.markdown.MarkdownUtils; +import com.termux.shared.models.ExecutionCommand; import com.termux.shared.packages.PackageUtils; +import com.termux.shared.shell.TermuxTask; + +import org.apache.commons.io.IOUtils; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; +import java.nio.charset.Charset; import java.text.SimpleDateFormat; import java.util.Date; import java.util.List; @@ -30,6 +35,8 @@ import java.util.regex.Pattern; public class TermuxUtils { + private static final String LOG_TAG = "TermuxUtils"; + /** * Get the {@link Context} for {@link TermuxConstants#TERMUX_PACKAGE_NAME} package. * @@ -286,6 +293,54 @@ public class TermuxUtils { + /** + * Get a markdown {@link String} for APT info of the app. + * + * This will take a few seconds to run due to running {@code apt update} command. + * + * @param context The context for operations. + * @return Returns the markdown {@link String}. + */ + public static String geAPTInfoMarkdownString(@NonNull final Context context) { + + String aptInfoScript = null; + InputStream inputStream = context.getResources().openRawResource(com.termux.shared.R.raw.apt_info_script); + try { + aptInfoScript = IOUtils.toString(inputStream, Charset.defaultCharset()); + } catch (IOException e) { + Logger.logError(LOG_TAG, "Failed to get APT info script: " + e.getMessage()); + return null; + } + + IOUtils.closeQuietly(inputStream); + + if (aptInfoScript == null || aptInfoScript.isEmpty()) { + Logger.logError(LOG_TAG, "The APT info script is null or empty"); + return null; + } + + aptInfoScript = aptInfoScript.replaceAll(Pattern.quote("@TERMUX_PREFIX@"), TermuxConstants.TERMUX_PREFIX_DIR_PATH); + + ExecutionCommand executionCommand = new ExecutionCommand(1, TermuxConstants.TERMUX_BIN_PREFIX_DIR_PATH + "/bash", null, aptInfoScript, null, true, false); + TermuxTask termuxTask = TermuxTask.execute(context, executionCommand, null, true); + if (termuxTask == null || !executionCommand.isSuccessful() || executionCommand.exitCode != 0) { + Logger.logError(LOG_TAG, executionCommand.toString()); + return null; + } + + if (executionCommand.stderr != null && !executionCommand.stderr.isEmpty()) + Logger.logError(LOG_TAG, executionCommand.toString()); + + StringBuilder markdownString = new StringBuilder(); + + markdownString.append("## ").append(TermuxConstants.TERMUX_APP_NAME).append(" APT Info\n\n"); + markdownString.append(executionCommand.stdout); + + return markdownString.toString(); + } + + + public static Properties getSystemProperties() { Properties systemProperties = new Properties(); diff --git a/termux-shared/src/main/res/raw/apt_info_script. b/termux-shared/src/main/res/raw/apt_info_script. new file mode 100644 index 00000000..3319e9ba --- /dev/null +++ b/termux-shared/src/main/res/raw/apt_info_script. @@ -0,0 +1,58 @@ +#!/bin/bash + +subscribed_repositories() { + local main_sources + main_sources=$(grep -P '^\s*deb\s' "@TERMUX_PREFIX@/etc/apt/sources.list") + + if [ -n "$main_sources" ]; then + echo "#### sources.list" + echo "\`$main_sources\`" + fi + + local filename repo_package supl_sources + while read -r filename; do + repo_package=$(dpkg -S "$filename" 2>/dev/null | cut -d : -f 1) + supl_sources=$(grep -P '^\s*deb\s' "$filename") + + if [ -n "$supl_sources" ]; then + if [ -n "$repo_package" ]; then + echo "#### $repo_package (sources.list.d/$(basename "$filename"))" + else + echo "#### sources.list.d/$(basename "$filename")" + fi + echo "\`$supl_sources\`" + fi + done < <(find "@TERMUX_PREFIX@/etc/apt/sources.list.d" -maxdepth 1 ! -type d) +} + +updatable_packages() { + local updatable + + if [ "$(id -u)" = "0" ]; then + echo "Running as root. Cannot check updatable packages." + else + apt update >/dev/null 2>&1 + updatable=$(apt list --upgradable 2>/dev/null | tail -n +2) + + if [ -z "$updatable" ];then + echo "All packages up to date" + else + echo "\`$updatable\`" + fi + fi +} + +output=" +### Subscribed Repositories + +$(subscribed_repositories) +## + + +### Updatable Packages +$(updatable_packages) +## + +" + +echo "$output"