Skip to content

Commit 15eb56d

Browse files
Add PackageUtils and fix ReportActivity
- 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.
1 parent d7ea770 commit 15eb56d

File tree

8 files changed

+346
-98
lines changed

8 files changed

+346
-98
lines changed

app/src/main/java/com/termux/app/activities/ReportActivity.java

Lines changed: 9 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ public class ReportActivity extends AppCompatActivity {
3232
private static final String EXTRA_REPORT_INFO = "report_info";
3333

3434
ReportInfo mReportInfo;
35+
String mReportActivityMarkdownString;
3536

3637
@Override
3738
protected void onCreate(Bundle savedInstanceState) {
@@ -98,7 +99,9 @@ private void updateUI(Bundle bundle) {
9899
recyclerView.setLayoutManager(new LinearLayoutManager(this));
99100
recyclerView.setAdapter(adapter);
100101

101-
adapter.setMarkdown(markwon, mReportInfo.reportString + getReportAndDeviceDetailsMarkdownString());
102+
103+
generateReportActivityMarkdownString();
104+
adapter.setMarkdown(markwon, mReportActivityMarkdownString);
102105
adapter.notifyDataSetChanged();
103106
}
104107

@@ -129,39 +132,20 @@ public boolean onOptionsItemSelected(final MenuItem item) {
129132
int id = item.getItemId();
130133
if (id == R.id.menu_item_share_report) {
131134
if (mReportInfo != null)
132-
ShareUtils.shareText(this, getString(R.string.title_report_text), mReportInfo.reportString);
135+
ShareUtils.shareText(this, getString(R.string.title_report_text), mReportActivityMarkdownString);
133136
} else if (id == R.id.menu_item_copy_report) {
134137
if (mReportInfo != null)
135-
ShareUtils.copyTextToClipboard(this, mReportInfo.reportString, null);
138+
ShareUtils.copyTextToClipboard(this, mReportActivityMarkdownString, null);
136139
}
137140

138141
return false;
139142
}
140143

141144
/**
142-
* Get a markdown {@link String} for {@link #mReportInfo} and device details.
143-
*
144-
* @return Returns the markdown {@link String}.
145+
* Generate the markdown {@link String} to be shown in {@link ReportActivity}.
145146
*/
146-
private String getReportAndDeviceDetailsMarkdownString() {
147-
if(!mReportInfo.addReportAndDeviceDetails) return "";
148-
149-
StringBuilder markdownString = new StringBuilder();
150-
151-
markdownString.append("\n\n### Report And Device Details\n\n");
152-
153-
if (mReportInfo != null) {
154-
markdownString.append("\n").append(MarkdownUtils.getSingleLineMarkdownStringEntry("User Action", mReportInfo.userAction, "-"));
155-
markdownString.append("\n").append(MarkdownUtils.getSingleLineMarkdownStringEntry("Sender", mReportInfo.sender, "-"));
156-
}
157-
158-
markdownString.append("\n").append(MarkdownUtils.getSingleLineMarkdownStringEntry("Timestamp", TermuxUtils.getCurrentTimeStamp(), "-"));
159-
160-
markdownString.append("\n\n").append(TermuxUtils.getDeviceDetailsMarkdownString(this));
161-
162-
markdownString.append("\n##\n");
163-
164-
return markdownString.toString();
147+
private void generateReportActivityMarkdownString() {
148+
mReportActivityMarkdownString = ReportInfo.getReportInfoMarkdownString(this, mReportInfo);
165149
}
166150

167151

app/src/main/java/com/termux/app/models/ExecutionCommand.java

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ public String toString() {
160160
* @param ignoreNull Set to {@code true} if non-critical {@code null} values are to be ignored.
161161
* @return Returns the log friendly {@link String}.
162162
*/
163-
public static String getExecutionInputLogString(ExecutionCommand executionCommand, boolean ignoreNull) {
163+
public static String getExecutionInputLogString(final ExecutionCommand executionCommand, boolean ignoreNull) {
164164
if (executionCommand == null) return "null";
165165

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

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

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

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

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

272272
StringBuilder markdownString = new StringBuilder();
273273

274-
markdownString.append("### ").append(executionCommand.commandLabel).append("\n");
274+
markdownString.append("## ").append(executionCommand.commandLabel).append("\n");
275275

276276

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

302302
if(executionCommand.commandDescription != null || executionCommand.commandHelp != null) {
303303
if (executionCommand.commandDescription != null)
304-
markdownString.append("\n\n#### Command Description\n\n").append(executionCommand.commandDescription).append("\n");
304+
markdownString.append("\n\n### Command Description\n\n").append(executionCommand.commandDescription).append("\n");
305305
if (executionCommand.commandHelp != null)
306-
markdownString.append("\n\n#### Command Help\n\n").append(executionCommand.commandHelp).append("\n");
306+
markdownString.append("\n\n### Command Help\n\n").append(executionCommand.commandHelp).append("\n");
307307
markdownString.append("\n##\n");
308308
}
309309

310310
if(executionCommand.pluginAPIHelp != null) {
311-
markdownString.append("\n\n#### Plugin API Help\n\n").append(executionCommand.pluginAPIHelp);
311+
markdownString.append("\n\n### Plugin API Help\n\n").append(executionCommand.pluginAPIHelp);
312312
markdownString.append("\n##\n");
313313
}
314314

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

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

475475
if (argumentsArray != null && argumentsArray.length != 0) {

app/src/main/java/com/termux/app/models/ReportInfo.java

Lines changed: 42 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
package com.termux.app.models;
22

3+
import android.content.Context;
4+
5+
import com.termux.app.utils.MarkdownUtils;
6+
import com.termux.app.utils.TermuxUtils;
7+
38
import java.io.Serializable;
49

510
public class ReportInfo implements Serializable {
@@ -12,15 +17,48 @@ public class ReportInfo implements Serializable {
1217
public String reportTitle;
1318
/** The markdown text for the report. */
1419
public String reportString;
15-
/** If set to {@code true}, then report and device details will be added to the report. */
16-
public boolean addReportAndDeviceDetails;
20+
/** If set to {@code true}, then report, app and device info will be added to the report when
21+
* markdown is generated.
22+
*/
23+
public boolean addReportInfoToMarkdown;
24+
/** The timestamp for the report. */
25+
public String creationTimestammp;
1726

18-
public ReportInfo(UserAction userAction, String sender, String reportTitle, String reportString, boolean addReportAndDeviceDetails) {
27+
public ReportInfo(UserAction userAction, String sender, String reportTitle, String reportString, boolean addReportInfoToMarkdown) {
1928
this.userAction = userAction;
2029
this.sender = sender;
2130
this.reportTitle = reportTitle;
2231
this.reportString = reportString;
23-
this.addReportAndDeviceDetails = addReportAndDeviceDetails;
32+
this.addReportInfoToMarkdown = addReportInfoToMarkdown;
33+
this.creationTimestammp = TermuxUtils.getCurrentTimeStamp();
34+
}
35+
36+
/**
37+
* Get a markdown {@link String} for {@link ReportInfo}.
38+
*
39+
* @param currentPackageContext The context of current package.
40+
* @param reportInfo The {@link ReportInfo} to convert.
41+
* @return Returns the markdown {@link String}.
42+
*/
43+
public static String getReportInfoMarkdownString(final Context currentPackageContext, final ReportInfo reportInfo) {
44+
if (reportInfo == null) return "null";
45+
46+
StringBuilder markdownString = new StringBuilder();
47+
48+
markdownString.append(reportInfo.reportString);
49+
50+
if(reportInfo.addReportInfoToMarkdown) {
51+
markdownString.append("## Report Info\n\n");
52+
markdownString.append("\n").append(MarkdownUtils.getSingleLineMarkdownStringEntry("User Action", reportInfo.userAction, "-"));
53+
markdownString.append("\n").append(MarkdownUtils.getSingleLineMarkdownStringEntry("Sender", reportInfo.sender, "-"));
54+
markdownString.append("\n").append(MarkdownUtils.getSingleLineMarkdownStringEntry("Creation Timestamp", reportInfo.creationTimestammp, "-"));
55+
markdownString.append("\n##\n");
56+
57+
markdownString.append("\n\n").append(TermuxUtils.getAppInfoMarkdownString(currentPackageContext, true));
58+
markdownString.append("\n\n").append(TermuxUtils.getDeviceInfoMarkdownString(currentPackageContext));
59+
}
60+
61+
return markdownString.toString();
2462
}
2563

2664
}

app/src/main/java/com/termux/app/settings/preferences/TermuxAppSharedPreferences.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@ public class TermuxAppSharedPreferences {
2525
private static final String LOG_TAG = "TermuxAppSharedPreferences";
2626

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

3132
setFontVariables(context);

app/src/main/java/com/termux/app/utils/Logger.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -216,13 +216,13 @@ public static String getStackTracesString(String label, String[] stackTraceStrin
216216

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

221221
if (stackTraceStringArray == null || stackTraceStringArray.length == 0) {
222222
stackTracesString.append("\n\n`-`");
223223
} else {
224224
for (int i = 0; i != stackTraceStringArray.length; i++) {
225-
stackTracesString.append("\n\n\n##### Stacktrace ").append(i + 1).append("\n\n```\n").append(stackTraceStringArray[i]).append("\n```");
225+
stackTracesString.append("\n\n\n#### Stacktrace ").append(i + 1).append("\n\n```\n").append(stackTraceStringArray[i]).append("\n```");
226226
}
227227
}
228228

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
package com.termux.app.utils;
2+
3+
import android.content.Context;
4+
import android.content.pm.ApplicationInfo;
5+
import android.content.pm.PackageInfo;
6+
7+
import androidx.annotation.NonNull;
8+
9+
public class PackageUtils {
10+
11+
/**
12+
* Get the {@link Context} for the package name.
13+
*
14+
* @param context The {@link Context} to use to get the {@link Context} of the {@code packageName}.
15+
* @return Returns the {@link Context}. This will {@code null} if an exception is raised.
16+
*/
17+
public static Context getContextForPackage(@NonNull final Context context, String packageName) {
18+
try {
19+
return context.createPackageContext(packageName, Context.CONTEXT_RESTRICTED);
20+
} catch (Exception e) {
21+
Logger.logStackTraceWithMessage("Failed to get \"" + packageName + "\" package context.", e);
22+
return null;
23+
}
24+
}
25+
26+
/**
27+
* Get the {@link PackageInfo} for the package associated with the {@code context}.
28+
*
29+
* @param context The {@link Context} for the package.
30+
* @return Returns the {@link PackageInfo}. This will be {@code null} if an exception is raised.
31+
*/
32+
public static PackageInfo getPackageInfoForPackage(@NonNull final Context context) {
33+
try {
34+
return context.getPackageManager().getPackageInfo(context.getPackageName(), 0);
35+
} catch (final Exception e) {
36+
return null;
37+
}
38+
}
39+
40+
/**
41+
* Get the app name for the package associated with the {@code context}.
42+
*
43+
* @param context The {@link Context} for the package.
44+
* @return Returns the {@code android:name} attribute.
45+
*/
46+
public static String getAppNameForPackage(@NonNull final Context context) {
47+
return context.getApplicationInfo().loadLabel(context.getPackageManager()).toString();
48+
}
49+
50+
/**
51+
* Get the package name for the package associated with the {@code context}.
52+
*
53+
* @param context The {@link Context} for the package.
54+
* @return Returns the package name.
55+
*/
56+
public static String getPackageNameForPackage(@NonNull final Context context) {
57+
return context.getApplicationInfo().packageName;
58+
}
59+
60+
/**
61+
* Get the {@code targetSdkVersion} for the package associated with the {@code context}.
62+
*
63+
* @param context The {@link Context} for the package.
64+
* @return Returns the {@code targetSdkVersion}.
65+
*/
66+
public static int getTargetSDKForPackage(@NonNull final Context context) {
67+
return context.getApplicationInfo().targetSdkVersion;
68+
}
69+
70+
/**
71+
* Get the {@code versionName} for the package associated with the {@code context}.
72+
*
73+
* @param context The {@link Context} for the package.
74+
* @return Returns the {@code versionName}. This will be {@code null} if an exception is raised.
75+
*/
76+
public static Boolean isAppForPackageADebugBuild(@NonNull final Context context) {
77+
return ( 0 != ( context.getApplicationInfo().flags & ApplicationInfo.FLAG_DEBUGGABLE ) );
78+
}
79+
80+
/**
81+
* Get the {@code versionCode} for the package associated with the {@code context}.
82+
*
83+
* @param context The {@link Context} for the package.
84+
* @return Returns the {@code versionCode}. This will be {@code null} if an exception is raised.
85+
*/
86+
public static Integer getVersionCodeForPackage(@NonNull final Context context) {
87+
try {
88+
return getPackageInfoForPackage(context).versionCode;
89+
} catch (final Exception e) {
90+
return null;
91+
}
92+
}
93+
94+
/**
95+
* Get the {@code versionName} for the package associated with the {@code context}.
96+
*
97+
* @param context The {@link Context} for the package.
98+
* @return Returns the {@code versionName}. This will be {@code null} if an exception is raised.
99+
*/
100+
public static String getVersionNameForPackage(@NonNull final Context context) {
101+
try {
102+
return getPackageInfoForPackage(context).versionName;
103+
} catch (final Exception e) {
104+
return null;
105+
}
106+
}
107+
108+
}

app/src/main/java/com/termux/app/utils/PluginUtils.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ public static void processPluginExecutionCommandError(final Context context, Str
149149
// to show the details of the error
150150
String title = TermuxConstants.TERMUX_APP_NAME + " Plugin Execution Command Error";
151151

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

155155
// Setup the notification channel if not already set up

0 commit comments

Comments
 (0)