Skip to content

Commit 6bca378

Browse files
Move Android specific utils from TermuxUtils to AndroidUtils
1 parent 12f910c commit 6bca378

File tree

8 files changed

+203
-172
lines changed

8 files changed

+203
-172
lines changed

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
import com.termux.shared.interact.ShareUtils;
1717
import com.termux.shared.packages.PackageUtils;
1818
import com.termux.shared.settings.preferences.TermuxTaskerAppSharedPreferences;
19+
import com.termux.shared.termux.AndroidUtils;
1920
import com.termux.shared.termux.TermuxConstants;
2021
import com.termux.shared.termux.TermuxUtils;
2122

@@ -82,7 +83,7 @@ public void run() {
8283
if (termuxPluginAppsInfo != null)
8384
aboutString.append("\n\n").append(termuxPluginAppsInfo);
8485

85-
aboutString.append("\n\n").append(TermuxUtils.getDeviceInfoMarkdownString(context));
86+
aboutString.append("\n\n").append(AndroidUtils.getDeviceInfoMarkdownString(context));
8687
aboutString.append("\n\n").append(TermuxUtils.getImportantLinksMarkdownString(context));
8788

8889
ReportActivity.startReportActivity(context, new ReportInfo(UserAction.ABOUT.getName(), TermuxConstants.TERMUX_APP.TERMUX_SETTINGS_ACTIVITY_NAME, title, null, aboutString.toString(), null, false));

app/src/main/java/com/termux/app/terminal/TermuxTerminalViewClient.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import com.termux.shared.data.UrlUtils;
2525
import com.termux.shared.shell.ShellUtils;
2626
import com.termux.shared.terminal.TermuxTerminalViewClientBase;
27+
import com.termux.shared.termux.AndroidUtils;
2728
import com.termux.shared.termux.TermuxConstants;
2829
import com.termux.shared.activities.ReportActivity;
2930
import com.termux.shared.models.ReportInfo;
@@ -664,7 +665,7 @@ public void run() {
664665
reportString.append("\n").append(MarkdownUtils.getMarkdownCodeForString(transcriptTextTruncated, true));
665666

666667
reportString.append("\n\n").append(TermuxUtils.getAppInfoMarkdownString(mActivity, true));
667-
reportString.append("\n\n").append(TermuxUtils.getDeviceInfoMarkdownString(mActivity));
668+
reportString.append("\n\n").append(AndroidUtils.getDeviceInfoMarkdownString(mActivity));
668669

669670
String termuxAptInfo = TermuxUtils.geAPTInfoMarkdownString(mActivity);
670671
if (termuxAptInfo != null)

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import com.termux.shared.notification.NotificationUtils;
1919
import com.termux.shared.shell.ResultSender;
2020
import com.termux.shared.shell.ShellUtils;
21+
import com.termux.shared.termux.AndroidUtils;
2122
import com.termux.shared.termux.TermuxConstants;
2223
import com.termux.shared.termux.TermuxConstants.TERMUX_APP.TERMUX_SERVICE;
2324
import com.termux.shared.logger.Logger;
@@ -193,7 +194,7 @@ public static void setPluginResultDirectoryVariables(ExecutionCommand executionC
193194

194195
// Set default resultFileBasename if resultSingleFile is true to `<executable_basename>-<timestamp>.log`
195196
if (resultConfig.resultSingleFile && resultConfig.resultFileBasename == null)
196-
resultConfig.resultFileBasename = ShellUtils.getExecutableBasename(executionCommand.executable) + "-" + TermuxUtils.getCurrentMilliSecondLocalTimeStamp() + ".log";
197+
resultConfig.resultFileBasename = ShellUtils.getExecutableBasename(executionCommand.executable) + "-" + AndroidUtils.getCurrentMilliSecondLocalTimeStamp() + ".log";
197198
}
198199

199200

@@ -215,7 +216,7 @@ public static void sendPluginCommandErrorNotification(Context context, String lo
215216

216217
reportString.append(ExecutionCommand.getExecutionCommandMarkdownString(executionCommand));
217218
reportString.append("\n\n").append(TermuxUtils.getAppInfoMarkdownString(context, true));
218-
reportString.append("\n\n").append(TermuxUtils.getDeviceInfoMarkdownString(context));
219+
reportString.append("\n\n").append(AndroidUtils.getDeviceInfoMarkdownString(context));
219220

220221
Intent notificationIntent = ReportActivity.newInstance(context, new ReportInfo(UserAction.PLUGIN_EXECUTION_COMMAND.getName(), logTag, title, null, reportString.toString(), null,true));
221222
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);

termux-shared/src/main/java/com/termux/shared/crash/CrashHandler.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import com.termux.shared.logger.Logger;
99
import com.termux.shared.markdown.MarkdownUtils;
1010
import com.termux.shared.models.errors.Error;
11+
import com.termux.shared.termux.AndroidUtils;
1112
import com.termux.shared.termux.TermuxConstants;
1213
import com.termux.shared.termux.TermuxUtils;
1314

@@ -56,11 +57,11 @@ public static void logCrash(final Context context, final Thread thread, final Th
5657

5758
reportString.append("## Crash Details\n");
5859
reportString.append("\n").append(MarkdownUtils.getSingleLineMarkdownStringEntry("Crash Thread", thread.toString(), "-"));
59-
reportString.append("\n").append(MarkdownUtils.getSingleLineMarkdownStringEntry("Crash Timestamp", TermuxUtils.getCurrentTimeStamp(), "-"));
60+
reportString.append("\n").append(MarkdownUtils.getSingleLineMarkdownStringEntry("Crash Timestamp", AndroidUtils.getCurrentTimeStamp(), "-"));
6061

6162
reportString.append("\n\n").append(Logger.getStackTracesMarkdownString("Stacktrace", Logger.getStackTracesStringArray(throwable)));
6263
reportString.append("\n\n").append(TermuxUtils.getAppInfoMarkdownString(context, true));
63-
reportString.append("\n\n").append(TermuxUtils.getDeviceInfoMarkdownString(context));
64+
reportString.append("\n\n").append(AndroidUtils.getDeviceInfoMarkdownString(context));
6465

6566
// Log report string to logcat
6667
Logger.logError(reportString.toString());

termux-shared/src/main/java/com/termux/shared/models/ReportInfo.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package com.termux.shared.models;
22

33
import com.termux.shared.markdown.MarkdownUtils;
4-
import com.termux.shared.termux.TermuxUtils;
4+
import com.termux.shared.termux.AndroidUtils;
55

66
import java.io.Serializable;
77

@@ -34,7 +34,7 @@ public ReportInfo(String userAction, String sender, String reportTitle, String r
3434
this.reportString = reportString;
3535
this.reportStringSuffix = reportStringSuffix;
3636
this.addReportInfoToMarkdown = addReportInfoToMarkdown;
37-
this.reportTimestamp = TermuxUtils.getCurrentTimeStamp();
37+
this.reportTimestamp = AndroidUtils.getCurrentTimeStamp();
3838
}
3939

4040
/**

termux-shared/src/main/java/com/termux/shared/shell/ResultSender.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@
1515
import com.termux.shared.models.ResultData;
1616
import com.termux.shared.models.errors.FunctionErrno;
1717
import com.termux.shared.models.errors.ResultSenderErrno;
18+
import com.termux.shared.termux.AndroidUtils;
1819
import com.termux.shared.termux.TermuxConstants.RESULT_SENDER;
19-
import com.termux.shared.termux.TermuxUtils;
2020

2121
public class ResultSender {
2222

@@ -235,7 +235,7 @@ else if (resultDataStderr.isEmpty())
235235

236236
// Write error or output to temp file
237237
// Check errCode file creation below for explanation for why temp file is used
238-
String temp_filename = resultConfig.resultFileBasename + "-" + TermuxUtils.getCurrentMilliSecondLocalTimeStamp();
238+
String temp_filename = resultConfig.resultFileBasename + "-" + AndroidUtils.getCurrentMilliSecondLocalTimeStamp();
239239
error = FileUtils.writeStringToFile(temp_filename, resultConfig.resultDirectoryPath + "/" + temp_filename,
240240
null, error_or_output, false);
241241
if (error != null) {
@@ -312,7 +312,7 @@ else if (resultDataStderr.isEmpty())
312312
// caller may otherwise read from an empty file in some cases.
313313

314314
// Write errCode to temp file
315-
String temp_filename = RESULT_SENDER.RESULT_FILE_ERR_PREFIX + "-" + TermuxUtils.getCurrentMilliSecondLocalTimeStamp();
315+
String temp_filename = RESULT_SENDER.RESULT_FILE_ERR_PREFIX + "-" + AndroidUtils.getCurrentMilliSecondLocalTimeStamp();
316316
if (!resultConfig.resultFilesSuffix.isEmpty()) temp_filename = temp_filename + "-" + resultConfig.resultFilesSuffix;
317317
error = FileUtils.writeStringToFile(temp_filename, resultConfig.resultDirectoryPath + "/" + temp_filename,
318318
null, String.valueOf(resultData.getErrCode()), false);
Lines changed: 181 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,181 @@
1+
package com.termux.shared.termux;
2+
3+
import android.annotation.SuppressLint;
4+
import android.content.Context;
5+
import android.os.Build;
6+
7+
import androidx.annotation.NonNull;
8+
9+
import com.google.common.base.Joiner;
10+
import com.termux.shared.logger.Logger;
11+
import com.termux.shared.markdown.MarkdownUtils;
12+
import com.termux.shared.packages.PackageUtils;
13+
14+
import java.io.BufferedReader;
15+
import java.io.IOException;
16+
import java.io.InputStream;
17+
import java.io.InputStreamReader;
18+
import java.text.SimpleDateFormat;
19+
import java.util.Date;
20+
import java.util.Properties;
21+
import java.util.TimeZone;
22+
import java.util.regex.Matcher;
23+
import java.util.regex.Pattern;
24+
25+
public class AndroidUtils {
26+
27+
/**
28+
* Get a markdown {@link String} for the app info for the package associated with the {@code context}.
29+
*
30+
* @param context The context for operations for the package.
31+
* @return Returns the markdown {@link String}.
32+
*/
33+
public static String getAppInfoMarkdownString(@NonNull final Context context) {
34+
StringBuilder markdownString = new StringBuilder();
35+
36+
AndroidUtils.appendPropertyToMarkdown(markdownString,"APP_NAME", PackageUtils.getAppNameForPackage(context));
37+
AndroidUtils.appendPropertyToMarkdown(markdownString,"PACKAGE_NAME", PackageUtils.getPackageNameForPackage(context));
38+
AndroidUtils.appendPropertyToMarkdown(markdownString,"VERSION_NAME", PackageUtils.getVersionNameForPackage(context));
39+
AndroidUtils.appendPropertyToMarkdown(markdownString,"VERSION_CODE", PackageUtils.getVersionCodeForPackage(context));
40+
AndroidUtils.appendPropertyToMarkdown(markdownString,"TARGET_SDK", PackageUtils.getTargetSDKForPackage(context));
41+
AndroidUtils.appendPropertyToMarkdown(markdownString,"IS_DEBUG_BUILD", PackageUtils.isAppForPackageADebugBuild(context));
42+
43+
return markdownString.toString();
44+
}
45+
46+
/**
47+
* Get a markdown {@link String} for the device info.
48+
*
49+
* @param context The context for operations.
50+
* @return Returns the markdown {@link String}.
51+
*/
52+
public static String getDeviceInfoMarkdownString(@NonNull final Context context) {
53+
// Some properties cannot be read with {@link System#getProperty(String)} but can be read
54+
// directly by running getprop command
55+
Properties systemProperties = getSystemProperties();
56+
57+
StringBuilder markdownString = new StringBuilder();
58+
59+
markdownString.append("## Device Info");
60+
61+
markdownString.append("\n\n### Software\n");
62+
appendPropertyToMarkdown(markdownString,"OS_VERSION", getSystemPropertyWithAndroidAPI("os.version"));
63+
appendPropertyToMarkdown(markdownString, "SDK_INT", Build.VERSION.SDK_INT);
64+
// If its a release version
65+
if ("REL".equals(Build.VERSION.CODENAME))
66+
appendPropertyToMarkdown(markdownString, "RELEASE", Build.VERSION.RELEASE);
67+
else
68+
appendPropertyToMarkdown(markdownString, "CODENAME", Build.VERSION.CODENAME);
69+
appendPropertyToMarkdown(markdownString, "ID", Build.ID);
70+
appendPropertyToMarkdown(markdownString, "DISPLAY", Build.DISPLAY);
71+
appendPropertyToMarkdown(markdownString, "INCREMENTAL", Build.VERSION.INCREMENTAL);
72+
appendPropertyToMarkdownIfSet(markdownString, "SECURITY_PATCH", systemProperties.getProperty("ro.build.version.security_patch"));
73+
appendPropertyToMarkdownIfSet(markdownString, "IS_DEBUGGABLE", systemProperties.getProperty("ro.debuggable"));
74+
appendPropertyToMarkdownIfSet(markdownString, "IS_EMULATOR", systemProperties.getProperty("ro.boot.qemu"));
75+
appendPropertyToMarkdownIfSet(markdownString, "IS_TREBLE_ENABLED", systemProperties.getProperty("ro.treble.enabled"));
76+
appendPropertyToMarkdown(markdownString, "TYPE", Build.TYPE);
77+
appendPropertyToMarkdown(markdownString, "TAGS", Build.TAGS);
78+
79+
markdownString.append("\n\n### Hardware\n");
80+
appendPropertyToMarkdown(markdownString, "MANUFACTURER", Build.MANUFACTURER);
81+
appendPropertyToMarkdown(markdownString, "BRAND", Build.BRAND);
82+
appendPropertyToMarkdown(markdownString, "MODEL", Build.MODEL);
83+
appendPropertyToMarkdown(markdownString, "PRODUCT", Build.PRODUCT);
84+
appendPropertyToMarkdown(markdownString, "BOARD", Build.BOARD);
85+
appendPropertyToMarkdown(markdownString, "HARDWARE", Build.HARDWARE);
86+
appendPropertyToMarkdown(markdownString, "DEVICE", Build.DEVICE);
87+
appendPropertyToMarkdown(markdownString, "SUPPORTED_ABIS", Joiner.on(", ").skipNulls().join(Build.SUPPORTED_ABIS));
88+
89+
markdownString.append("\n##\n");
90+
91+
return markdownString.toString();
92+
}
93+
94+
95+
96+
public static Properties getSystemProperties() {
97+
Properties systemProperties = new Properties();
98+
99+
// getprop commands returns values in the format `[key]: [value]`
100+
// Regex matches string starting with a literal `[`,
101+
// followed by one or more characters that do not match a closing square bracket as the key,
102+
// followed by a literal `]: [`,
103+
// followed by one or more characters as the value,
104+
// followed by string ending with literal `]`
105+
// multiline values will be ignored
106+
Pattern propertiesPattern = Pattern.compile("^\\[([^]]+)]: \\[(.+)]$");
107+
108+
try {
109+
Process process = new ProcessBuilder()
110+
.command("/system/bin/getprop")
111+
.redirectErrorStream(true)
112+
.start();
113+
114+
InputStream inputStream = process.getInputStream();
115+
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
116+
String line, key, value;
117+
118+
while ((line = bufferedReader.readLine()) != null) {
119+
Matcher matcher = propertiesPattern.matcher(line);
120+
if (matcher.matches()) {
121+
key = matcher.group(1);
122+
value = matcher.group(2);
123+
if (key != null && value != null && !key.isEmpty() && !value.isEmpty())
124+
systemProperties.put(key, value);
125+
}
126+
}
127+
128+
bufferedReader.close();
129+
process.destroy();
130+
131+
} catch (IOException e) {
132+
Logger.logStackTraceWithMessage("Failed to get run \"/system/bin/getprop\" to get system properties.", e);
133+
}
134+
135+
//for (String key : systemProperties.stringPropertyNames()) {
136+
// Logger.logVerbose(key + ": " + systemProperties.get(key));
137+
//}
138+
139+
return systemProperties;
140+
}
141+
142+
private static String getSystemPropertyWithAndroidAPI(@NonNull String property) {
143+
try {
144+
return System.getProperty(property);
145+
} catch (Exception e) {
146+
Logger.logVerbose("Failed to get system property \"" + property + "\":" + e.getMessage());
147+
return null;
148+
}
149+
}
150+
151+
private static void appendPropertyToMarkdownIfSet(StringBuilder markdownString, String label, Object value) {
152+
if (value == null) return;
153+
if (value instanceof String && (((String) value).isEmpty()) || "REL".equals(value)) return;
154+
markdownString.append("\n").append(getPropertyMarkdown(label, value));
155+
}
156+
157+
static void appendPropertyToMarkdown(StringBuilder markdownString, String label, Object value) {
158+
markdownString.append("\n").append(getPropertyMarkdown(label, value));
159+
}
160+
161+
private static String getPropertyMarkdown(String label, Object value) {
162+
return MarkdownUtils.getSingleLineMarkdownStringEntry(label, value, "-");
163+
}
164+
165+
166+
167+
public static String getCurrentTimeStamp() {
168+
@SuppressLint("SimpleDateFormat")
169+
final SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss z");
170+
df.setTimeZone(TimeZone.getTimeZone("UTC"));
171+
return df.format(new Date());
172+
}
173+
174+
public static String getCurrentMilliSecondLocalTimeStamp() {
175+
@SuppressLint("SimpleDateFormat")
176+
final SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd_HH.mm.ss.SSS");
177+
df.setTimeZone(TimeZone.getDefault());
178+
return df.format(new Date());
179+
}
180+
181+
}

0 commit comments

Comments
 (0)