Skip to content

Commit

Permalink
Add PackageUtils and fix ReportActivity
Browse files Browse the repository at this point in the history
- PackageUtils has been added to get various package related info. This will be used to get info based on Context objects instead of using BuildConfig which wouldn't have been available across termux plugins.

- Support for getting Context objects of all termux plugin apps have been added to TermuxUtils.

- Support for showing more details for the app has been added for ReportActivity. This will also allow app info of Termux app to be generated when TermuxUtils.getAppInfoMarkdownString() is called by a termux plugin so that both are shown so that devs/users can more easily detect compatibility issues.

- ReportActivity has been fixed to also include report and device info instead of just the ExecutionCommand info when copying and sharing.

- Moved the generation of markdown for ReportInfo to its own class and added creationTimestamp field.

- Increased markdown headings size for some cases.
  • Loading branch information
agnostic-apollo committed Mar 28, 2021
1 parent d7ea770 commit 15eb56d
Show file tree
Hide file tree
Showing 8 changed files with 346 additions and 98 deletions.
34 changes: 9 additions & 25 deletions app/src/main/java/com/termux/app/activities/ReportActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ public class ReportActivity extends AppCompatActivity {
private static final String EXTRA_REPORT_INFO = "report_info";

ReportInfo mReportInfo;
String mReportActivityMarkdownString;

@Override
protected void onCreate(Bundle savedInstanceState) {
Expand Down Expand Up @@ -98,7 +99,9 @@ private void updateUI(Bundle bundle) {
recyclerView.setLayoutManager(new LinearLayoutManager(this));
recyclerView.setAdapter(adapter);

adapter.setMarkdown(markwon, mReportInfo.reportString + getReportAndDeviceDetailsMarkdownString());

generateReportActivityMarkdownString();
adapter.setMarkdown(markwon, mReportActivityMarkdownString);
adapter.notifyDataSetChanged();
}

Expand Down Expand Up @@ -129,39 +132,20 @@ public boolean onOptionsItemSelected(final MenuItem item) {
int id = item.getItemId();
if (id == R.id.menu_item_share_report) {
if (mReportInfo != null)
ShareUtils.shareText(this, getString(R.string.title_report_text), mReportInfo.reportString);
ShareUtils.shareText(this, getString(R.string.title_report_text), mReportActivityMarkdownString);
} else if (id == R.id.menu_item_copy_report) {
if (mReportInfo != null)
ShareUtils.copyTextToClipboard(this, mReportInfo.reportString, null);
ShareUtils.copyTextToClipboard(this, mReportActivityMarkdownString, null);
}

return false;
}

/**
* Get a markdown {@link String} for {@link #mReportInfo} and device details.
*
* @return Returns the markdown {@link String}.
* Generate the markdown {@link String} to be shown in {@link ReportActivity}.
*/
private String getReportAndDeviceDetailsMarkdownString() {
if(!mReportInfo.addReportAndDeviceDetails) return "";

StringBuilder markdownString = new StringBuilder();

markdownString.append("\n\n### Report And Device Details\n\n");

if (mReportInfo != null) {
markdownString.append("\n").append(MarkdownUtils.getSingleLineMarkdownStringEntry("User Action", mReportInfo.userAction, "-"));
markdownString.append("\n").append(MarkdownUtils.getSingleLineMarkdownStringEntry("Sender", mReportInfo.sender, "-"));
}

markdownString.append("\n").append(MarkdownUtils.getSingleLineMarkdownStringEntry("Timestamp", TermuxUtils.getCurrentTimeStamp(), "-"));

markdownString.append("\n\n").append(TermuxUtils.getDeviceDetailsMarkdownString(this));

markdownString.append("\n##\n");

return markdownString.toString();
private void generateReportActivityMarkdownString() {
mReportActivityMarkdownString = ReportInfo.getReportInfoMarkdownString(this, mReportInfo);
}


Expand Down
22 changes: 11 additions & 11 deletions app/src/main/java/com/termux/app/models/ExecutionCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ public String toString() {
* @param ignoreNull Set to {@code true} if non-critical {@code null} values are to be ignored.
* @return Returns the log friendly {@link String}.
*/
public static String getExecutionInputLogString(ExecutionCommand executionCommand, boolean ignoreNull) {
public static String getExecutionInputLogString(final ExecutionCommand executionCommand, boolean ignoreNull) {
if (executionCommand == null) return "null";

StringBuilder logString = new StringBuilder();
Expand Down Expand Up @@ -197,7 +197,7 @@ public static String getExecutionInputLogString(ExecutionCommand executionComman
* @param ignoreNull Set to {@code true} if non-critical {@code null} values are to be ignored.
* @return Returns the log friendly {@link String}.
*/
public static String getExecutionOutputLogString(ExecutionCommand executionCommand, boolean ignoreNull) {
public static String getExecutionOutputLogString(final ExecutionCommand executionCommand, boolean ignoreNull) {
if (executionCommand == null) return "null";

StringBuilder logString = new StringBuilder();
Expand All @@ -223,7 +223,7 @@ public static String getExecutionOutputLogString(ExecutionCommand executionComma
* @param ignoreNull Set to {@code true} if non-critical {@code null} values are to be ignored.
* @return Returns the log friendly {@link String}.
*/
public static String getExecutionErrLogString(ExecutionCommand executionCommand, boolean ignoreNull) {
public static String getExecutionErrLogString(final ExecutionCommand executionCommand, boolean ignoreNull) {
StringBuilder logString = new StringBuilder();

if(!ignoreNull || (executionCommand.isStateFailed())) {
Expand All @@ -243,7 +243,7 @@ public static String getExecutionErrLogString(ExecutionCommand executionCommand,
* @param executionCommand The {@link ExecutionCommand} to convert.
* @return Returns the log friendly {@link String}.
*/
public static String getDetailedLogString(ExecutionCommand executionCommand) {
public static String getDetailedLogString(final ExecutionCommand executionCommand) {
if (executionCommand == null) return "null";

StringBuilder logString = new StringBuilder();
Expand All @@ -264,14 +264,14 @@ public static String getDetailedLogString(ExecutionCommand executionCommand) {
* @param executionCommand The {@link ExecutionCommand} to convert.
* @return Returns the markdown {@link String}.
*/
public static String getDetailedMarkdownString(ExecutionCommand executionCommand) {
public static String getExecutionCommandMarkdownString(final ExecutionCommand executionCommand) {
if (executionCommand == null) return "null";

if (executionCommand.commandLabel == null) executionCommand.commandLabel = "Execution Command";

StringBuilder markdownString = new StringBuilder();

markdownString.append("### ").append(executionCommand.commandLabel).append("\n");
markdownString.append("## ").append(executionCommand.commandLabel).append("\n");


markdownString.append("\n").append(MarkdownUtils.getSingleLineMarkdownStringEntry("Previous State", executionCommand.previousState.getName(), "-"));
Expand Down Expand Up @@ -301,14 +301,14 @@ public static String getDetailedMarkdownString(ExecutionCommand executionCommand

if(executionCommand.commandDescription != null || executionCommand.commandHelp != null) {
if (executionCommand.commandDescription != null)
markdownString.append("\n\n#### Command Description\n\n").append(executionCommand.commandDescription).append("\n");
markdownString.append("\n\n### Command Description\n\n").append(executionCommand.commandDescription).append("\n");
if (executionCommand.commandHelp != null)
markdownString.append("\n\n#### Command Help\n\n").append(executionCommand.commandHelp).append("\n");
markdownString.append("\n\n### Command Help\n\n").append(executionCommand.commandHelp).append("\n");
markdownString.append("\n##\n");
}

if(executionCommand.pluginAPIHelp != null) {
markdownString.append("\n\n#### Plugin API Help\n\n").append(executionCommand.pluginAPIHelp);
markdownString.append("\n\n### Plugin API Help\n\n").append(executionCommand.pluginAPIHelp);
markdownString.append("\n##\n");
}

Expand Down Expand Up @@ -439,7 +439,7 @@ public String geStackTracesMarkdownString() {
* @param argumentsArray The {@link String[]} argumentsArray to convert.
* @return Returns the markdown {@link String}.
*/
public static String getArgumentsMarkdownString(String[] argumentsArray) {
public static String getArgumentsMarkdownString(final String[] argumentsArray) {
StringBuilder argumentsString = new StringBuilder("**Arguments:**");

if (argumentsArray != null && argumentsArray.length != 0) {
Expand Down Expand Up @@ -469,7 +469,7 @@ public static String getArgumentsMarkdownString(String[] argumentsArray) {
* @param argumentsArray The {@link String[]} argumentsArray to convert.
* @return Returns the log friendly {@link String}.
*/
public static String getArgumentsLogString(String[] argumentsArray) {
public static String getArgumentsLogString(final String[] argumentsArray) {
StringBuilder argumentsString = new StringBuilder("Arguments:");

if (argumentsArray != null && argumentsArray.length != 0) {
Expand Down
46 changes: 42 additions & 4 deletions app/src/main/java/com/termux/app/models/ReportInfo.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
package com.termux.app.models;

import android.content.Context;

import com.termux.app.utils.MarkdownUtils;
import com.termux.app.utils.TermuxUtils;

import java.io.Serializable;

public class ReportInfo implements Serializable {
Expand All @@ -12,15 +17,48 @@ public class ReportInfo implements Serializable {
public String reportTitle;
/** The markdown text for the report. */
public String reportString;
/** If set to {@code true}, then report and device details will be added to the report. */
public boolean addReportAndDeviceDetails;
/** If set to {@code true}, then report, app and device info will be added to the report when
* markdown is generated.
*/
public boolean addReportInfoToMarkdown;
/** The timestamp for the report. */
public String creationTimestammp;

public ReportInfo(UserAction userAction, String sender, String reportTitle, String reportString, boolean addReportAndDeviceDetails) {
public ReportInfo(UserAction userAction, String sender, String reportTitle, String reportString, boolean addReportInfoToMarkdown) {
this.userAction = userAction;
this.sender = sender;
this.reportTitle = reportTitle;
this.reportString = reportString;
this.addReportAndDeviceDetails = addReportAndDeviceDetails;
this.addReportInfoToMarkdown = addReportInfoToMarkdown;
this.creationTimestammp = TermuxUtils.getCurrentTimeStamp();
}

/**
* Get a markdown {@link String} for {@link ReportInfo}.
*
* @param currentPackageContext The context of current package.
* @param reportInfo The {@link ReportInfo} to convert.
* @return Returns the markdown {@link String}.
*/
public static String getReportInfoMarkdownString(final Context currentPackageContext, final ReportInfo reportInfo) {
if (reportInfo == null) return "null";

StringBuilder markdownString = new StringBuilder();

markdownString.append(reportInfo.reportString);

if(reportInfo.addReportInfoToMarkdown) {
markdownString.append("## Report Info\n\n");
markdownString.append("\n").append(MarkdownUtils.getSingleLineMarkdownStringEntry("User Action", reportInfo.userAction, "-"));
markdownString.append("\n").append(MarkdownUtils.getSingleLineMarkdownStringEntry("Sender", reportInfo.sender, "-"));
markdownString.append("\n").append(MarkdownUtils.getSingleLineMarkdownStringEntry("Creation Timestamp", reportInfo.creationTimestammp, "-"));
markdownString.append("\n##\n");

markdownString.append("\n\n").append(TermuxUtils.getAppInfoMarkdownString(currentPackageContext, true));
markdownString.append("\n\n").append(TermuxUtils.getDeviceInfoMarkdownString(currentPackageContext));
}

return markdownString.toString();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ public class TermuxAppSharedPreferences {
private static final String LOG_TAG = "TermuxAppSharedPreferences";

public TermuxAppSharedPreferences(@Nonnull Context context) {
mContext = TermuxUtils.getTermuxPackageContext(context);
// We use the default context if failed to get termux package context
mContext = TextDataUtils.getDefaultIfNull(TermuxUtils.getTermuxPackageContext(context), context);
mSharedPreferences = getPrivateSharedPreferences(mContext);

setFontVariables(context);
Expand Down
4 changes: 2 additions & 2 deletions app/src/main/java/com/termux/app/utils/Logger.java
Original file line number Diff line number Diff line change
Expand Up @@ -216,13 +216,13 @@ public static String getStackTracesString(String label, String[] stackTraceStrin

public static String getStackTracesMarkdownString(String label, String[] stackTraceStringArray) {
if(label == null) label = "StackTraces:";
StringBuilder stackTracesString = new StringBuilder("#### " + label);
StringBuilder stackTracesString = new StringBuilder("### " + label);

if (stackTraceStringArray == null || stackTraceStringArray.length == 0) {
stackTracesString.append("\n\n`-`");
} else {
for (int i = 0; i != stackTraceStringArray.length; i++) {
stackTracesString.append("\n\n\n##### Stacktrace ").append(i + 1).append("\n\n```\n").append(stackTraceStringArray[i]).append("\n```");
stackTracesString.append("\n\n\n#### Stacktrace ").append(i + 1).append("\n\n```\n").append(stackTraceStringArray[i]).append("\n```");
}
}

Expand Down
108 changes: 108 additions & 0 deletions app/src/main/java/com/termux/app/utils/PackageUtils.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
package com.termux.app.utils;

import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageInfo;

import androidx.annotation.NonNull;

public class PackageUtils {

/**
* Get the {@link Context} for the package name.
*
* @param context The {@link Context} to use to get the {@link Context} of the {@code packageName}.
* @return Returns the {@link Context}. This will {@code null} if an exception is raised.
*/
public static Context getContextForPackage(@NonNull final Context context, String packageName) {
try {
return context.createPackageContext(packageName, Context.CONTEXT_RESTRICTED);
} catch (Exception e) {
Logger.logStackTraceWithMessage("Failed to get \"" + packageName + "\" package context.", e);
return null;
}
}

/**
* Get the {@link PackageInfo} for the package associated with the {@code context}.
*
* @param context The {@link Context} for the package.
* @return Returns the {@link PackageInfo}. This will be {@code null} if an exception is raised.
*/
public static PackageInfo getPackageInfoForPackage(@NonNull final Context context) {
try {
return context.getPackageManager().getPackageInfo(context.getPackageName(), 0);
} catch (final Exception e) {
return null;
}
}

/**
* Get the app name for the package associated with the {@code context}.
*
* @param context The {@link Context} for the package.
* @return Returns the {@code android:name} attribute.
*/
public static String getAppNameForPackage(@NonNull final Context context) {
return context.getApplicationInfo().loadLabel(context.getPackageManager()).toString();
}

/**
* Get the package name for the package associated with the {@code context}.
*
* @param context The {@link Context} for the package.
* @return Returns the package name.
*/
public static String getPackageNameForPackage(@NonNull final Context context) {
return context.getApplicationInfo().packageName;
}

/**
* Get the {@code targetSdkVersion} for the package associated with the {@code context}.
*
* @param context The {@link Context} for the package.
* @return Returns the {@code targetSdkVersion}.
*/
public static int getTargetSDKForPackage(@NonNull final Context context) {
return context.getApplicationInfo().targetSdkVersion;
}

/**
* Get the {@code versionName} for the package associated with the {@code context}.
*
* @param context The {@link Context} for the package.
* @return Returns the {@code versionName}. This will be {@code null} if an exception is raised.
*/
public static Boolean isAppForPackageADebugBuild(@NonNull final Context context) {
return ( 0 != ( context.getApplicationInfo().flags & ApplicationInfo.FLAG_DEBUGGABLE ) );
}

/**
* Get the {@code versionCode} for the package associated with the {@code context}.
*
* @param context The {@link Context} for the package.
* @return Returns the {@code versionCode}. This will be {@code null} if an exception is raised.
*/
public static Integer getVersionCodeForPackage(@NonNull final Context context) {
try {
return getPackageInfoForPackage(context).versionCode;
} catch (final Exception e) {
return null;
}
}

/**
* Get the {@code versionName} for the package associated with the {@code context}.
*
* @param context The {@link Context} for the package.
* @return Returns the {@code versionName}. This will be {@code null} if an exception is raised.
*/
public static String getVersionNameForPackage(@NonNull final Context context) {
try {
return getPackageInfoForPackage(context).versionName;
} catch (final Exception e) {
return null;
}
}

}
2 changes: 1 addition & 1 deletion app/src/main/java/com/termux/app/utils/PluginUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ public static void processPluginExecutionCommandError(final Context context, Str
// to show the details of the error
String title = TermuxConstants.TERMUX_APP_NAME + " Plugin Execution Command Error";

Intent notificationIntent = ReportActivity.newInstance(context, new ReportInfo(UserAction.PLUGIN_EXECUTION_COMMAND, logTag, title, ExecutionCommand.getDetailedMarkdownString(executionCommand), true));
Intent notificationIntent = ReportActivity.newInstance(context, new ReportInfo(UserAction.PLUGIN_EXECUTION_COMMAND, logTag, title, ExecutionCommand.getExecutionCommandMarkdownString(executionCommand), true));
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);

// Setup the notification channel if not already set up
Expand Down
Loading

0 comments on commit 15eb56d

Please sign in to comment.