|
14 | 14 | import com.termux.shared.R;
|
15 | 15 | import com.termux.shared.logger.Logger;
|
16 | 16 | import com.termux.shared.markdown.MarkdownUtils;
|
| 17 | +import com.termux.shared.models.ExecutionCommand; |
17 | 18 | import com.termux.shared.packages.PackageUtils;
|
| 19 | +import com.termux.shared.shell.TermuxTask; |
| 20 | + |
| 21 | +import org.apache.commons.io.IOUtils; |
18 | 22 |
|
19 | 23 | import java.io.BufferedReader;
|
20 | 24 | import java.io.IOException;
|
21 | 25 | import java.io.InputStream;
|
22 | 26 | import java.io.InputStreamReader;
|
| 27 | +import java.nio.charset.Charset; |
23 | 28 | import java.text.SimpleDateFormat;
|
24 | 29 | import java.util.Date;
|
25 | 30 | import java.util.List;
|
|
30 | 35 |
|
31 | 36 | public class TermuxUtils {
|
32 | 37 |
|
| 38 | + private static final String LOG_TAG = "TermuxUtils"; |
| 39 | + |
33 | 40 | /**
|
34 | 41 | * Get the {@link Context} for {@link TermuxConstants#TERMUX_PACKAGE_NAME} package.
|
35 | 42 | *
|
@@ -286,6 +293,54 @@ public static String getReportIssueMarkdownString(@NonNull final Context context
|
286 | 293 |
|
287 | 294 |
|
288 | 295 |
|
| 296 | + /** |
| 297 | + * Get a markdown {@link String} for APT info of the app. |
| 298 | + * |
| 299 | + * This will take a few seconds to run due to running {@code apt update} command. |
| 300 | + * |
| 301 | + * @param context The context for operations. |
| 302 | + * @return Returns the markdown {@link String}. |
| 303 | + */ |
| 304 | + public static String geAPTInfoMarkdownString(@NonNull final Context context) { |
| 305 | + |
| 306 | + String aptInfoScript = null; |
| 307 | + InputStream inputStream = context.getResources().openRawResource(com.termux.shared.R.raw.apt_info_script); |
| 308 | + try { |
| 309 | + aptInfoScript = IOUtils.toString(inputStream, Charset.defaultCharset()); |
| 310 | + } catch (IOException e) { |
| 311 | + Logger.logError(LOG_TAG, "Failed to get APT info script: " + e.getMessage()); |
| 312 | + return null; |
| 313 | + } |
| 314 | + |
| 315 | + IOUtils.closeQuietly(inputStream); |
| 316 | + |
| 317 | + if (aptInfoScript == null || aptInfoScript.isEmpty()) { |
| 318 | + Logger.logError(LOG_TAG, "The APT info script is null or empty"); |
| 319 | + return null; |
| 320 | + } |
| 321 | + |
| 322 | + aptInfoScript = aptInfoScript.replaceAll(Pattern.quote("@TERMUX_PREFIX@"), TermuxConstants.TERMUX_PREFIX_DIR_PATH); |
| 323 | + |
| 324 | + ExecutionCommand executionCommand = new ExecutionCommand(1, TermuxConstants.TERMUX_BIN_PREFIX_DIR_PATH + "/bash", null, aptInfoScript, null, true, false); |
| 325 | + TermuxTask termuxTask = TermuxTask.execute(context, executionCommand, null, true); |
| 326 | + if (termuxTask == null || !executionCommand.isSuccessful() || executionCommand.exitCode != 0) { |
| 327 | + Logger.logError(LOG_TAG, executionCommand.toString()); |
| 328 | + return null; |
| 329 | + } |
| 330 | + |
| 331 | + if (executionCommand.stderr != null && !executionCommand.stderr.isEmpty()) |
| 332 | + Logger.logError(LOG_TAG, executionCommand.toString()); |
| 333 | + |
| 334 | + StringBuilder markdownString = new StringBuilder(); |
| 335 | + |
| 336 | + markdownString.append("## ").append(TermuxConstants.TERMUX_APP_NAME).append(" APT Info\n\n"); |
| 337 | + markdownString.append(executionCommand.stdout); |
| 338 | + |
| 339 | + return markdownString.toString(); |
| 340 | + } |
| 341 | + |
| 342 | + |
| 343 | + |
289 | 344 | public static Properties getSystemProperties() {
|
290 | 345 | Properties systemProperties = new Properties();
|
291 | 346 |
|
|
0 commit comments