|
| 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